1 /* vi: set sw=4 ts=4: */
3 * Stripped down version of net-tools for busybox.
5 * Author: Ignacio Garcia Perez (iggarpe at gmail dot com)
7 * License: GPLv2 or later, see LICENSE file in this tarball.
9 * There are some differences from the standard net-tools slattach:
11 * - The -l option is not supported.
13 * - The -F options allows disabling of RTS/CTS flow control.
17 #include "libiproute/utils.h" /* invarg() */
19 /* Line discipline code table */
20 static const char *const proto_names[] = {
32 struct termios saved_state;
34 #define G (*(struct globals*)&bb_common_bufsiz1)
35 #define handle (G.handle )
36 #define saved_disc (G.saved_disc )
37 #define saved_state (G.saved_state )
38 #define INIT_G() do {} while (0)
42 * Save tty state and line discipline
44 * It is fine here to bail out on errors, since we haven modified anything yet
46 static void save_state(void)
48 /* Save line status */
49 if (tcgetattr(handle, &saved_state) < 0)
50 bb_perror_msg_and_die("get state");
52 /* Save line discipline */
53 if (ioctl(handle, TIOCGETD, &saved_disc) < 0)
54 bb_perror_msg_and_die("get discipline");
57 static int set_termios_state_and_warn(struct termios *state)
61 ret = tcsetattr(handle, TCSANOW, state);
63 bb_perror_msg("set state");
64 return 1; /* used as exitcode */
70 * Restore state and line discipline for ALL managed ttys
72 * Restoring ALL managed ttys is the only way to have a single
75 * Go on after errors: we want to restore as many controlled ttys
78 static void restore_state_and_exit(int exitcode) ATTRIBUTE_NORETURN;
79 static void restore_state_and_exit(int exitcode)
83 /* Restore line discipline */
84 if (ioctl(handle, TIOCSETD, &saved_disc) < 0) {
85 bb_perror_msg("set discipline");
90 memcpy(&state, &saved_state, sizeof(state));
91 cfsetispeed(&state, B0);
92 cfsetospeed(&state, B0);
93 if (set_termios_state_and_warn(&state))
97 /* Restore line status */
98 if (set_termios_state_and_warn(&saved_state))
100 if (ENABLE_FEATURE_CLEAN_UP)
107 * Set tty state, line discipline and encapsulation
109 static void set_state(struct termios *state, int encap)
113 /* Set line status */
114 if (set_termios_state_and_warn(state))
116 /* Set line discliple (N_SLIP always) */
118 if (ioctl(handle, TIOCSETD, &disc) < 0) {
119 bb_perror_msg("set discipline");
123 /* Set encapsulation (SLIP, CSLIP, etc) */
124 if (ioctl(handle, SIOCSIFENCAP, &encap) < 0) {
125 bb_perror_msg("set encapsulation");
127 restore_state_and_exit(1);
131 static void sig_handler(int signo)
133 restore_state_and_exit(0);
136 int slattach_main(int argc, char **argv);
137 int slattach_main(int argc, char **argv)
140 struct termios state;
141 const char *proto = "cslip";
142 const char *extcmd; /* Command to execute after hangup */
143 const char *baud_str;
144 int baud_code = -1; /* Line baud rate (system code) */
147 OPT_p_proto = 1 << 0,
149 OPT_c_extcmd = 1 << 2,
151 OPT_h_watch = 1 << 4,
152 OPT_m_nonraw = 1 << 5,
153 OPT_L_local = 1 << 6,
154 OPT_F_noflow = 1 << 7
159 /* Parse command line options */
160 opt = getopt32(argc, argv, "p:s:c:ehmLF", &proto, &baud_str, &extcmd);
167 encap = index_in_str_array(proto_names, proto);
170 invarg(proto, "protocol");
174 /* We want to know if the baud rate is valid before we start touching the ttys */
175 if (opt & OPT_s_baud) {
176 baud_code = tty_value_to_baud(xatoi(baud_str));
178 invarg(baud_str, "baud rate");
181 /* Trap signals in order to restore tty states upon exit */
182 if (!(opt & OPT_e_quit)) {
183 signal(SIGHUP, sig_handler);
184 signal(SIGINT, sig_handler);
185 signal(SIGQUIT, sig_handler);
186 signal(SIGTERM, sig_handler);
190 handle = open(*argv, O_RDWR | O_NDELAY);
192 char *buf = concat_path_file("/dev", *argv);
193 handle = xopen(buf, O_RDWR | O_NDELAY);
194 /* maybe if (ENABLE_FEATURE_CLEAN_UP) ?? */
198 /* Save current tty state */
202 memcpy(&state, &saved_state, sizeof(state));
203 if (!(opt & OPT_m_nonraw)) { /* raw not suppressed */
204 memset(&state.c_cc, 0, sizeof(state.c_cc));
205 state.c_cc[VMIN] = 1;
206 state.c_iflag = IGNBRK | IGNPAR;
209 state.c_cflag = CS8 | HUPCL | CREAD
210 | ((opt & OPT_L_local) ? CLOCAL : 0)
211 | ((opt & OPT_F_noflow) ? 0 : CRTSCTS);
214 if (opt & OPT_s_baud) {
215 cfsetispeed(&state, baud_code);
216 cfsetospeed(&state, baud_code);
219 set_state(&state, encap);
221 /* Exit now if option -e was passed */
222 if (opt & OPT_e_quit)
225 /* If we're not requested to watch, just keep descriptor open
226 * until we are killed */
227 if (!(opt & OPT_h_watch))
231 /* Watch line for hangup */
233 if (ioctl(handle, TIOCMGET, &i) < 0 || !(i & TIOCM_CAR))
240 /* Execute command on hangup */
241 if (opt & OPT_c_extcmd)
244 /* Restore states and exit */
245 restore_state_and_exit(0);