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 xioctl(handle, TIOCGETD, &saved_disc);
56 static int set_termios_state_and_warn(struct termios *state)
60 ret = tcsetattr(handle, TCSANOW, state);
62 bb_perror_msg("set state");
63 return 1; /* used as exitcode */
69 * Restore state and line discipline for ALL managed ttys
71 * Restoring ALL managed ttys is the only way to have a single
74 * Go on after errors: we want to restore as many controlled ttys
77 static void restore_state_and_exit(int exitcode) ATTRIBUTE_NORETURN;
78 static void restore_state_and_exit(int exitcode)
82 /* Restore line discipline */
83 if (ioctl_or_warn(handle, TIOCSETD, &saved_disc) < 0) {
88 memcpy(&state, &saved_state, sizeof(state));
89 cfsetispeed(&state, B0);
90 cfsetospeed(&state, B0);
91 if (set_termios_state_and_warn(&state))
95 /* Restore line status */
96 if (set_termios_state_and_warn(&saved_state))
98 if (ENABLE_FEATURE_CLEAN_UP)
105 * Set tty state, line discipline and encapsulation
107 static void set_state(struct termios *state, int encap)
111 /* Set line status */
112 if (set_termios_state_and_warn(state))
114 /* Set line discliple (N_SLIP always) */
116 if (ioctl_or_warn(handle, TIOCSETD, &disc) < 0) {
120 /* Set encapsulation (SLIP, CSLIP, etc) */
121 if (ioctl_or_warn(handle, SIOCSIFENCAP, &encap) < 0) {
123 restore_state_and_exit(1);
127 static void sig_handler(int signo)
129 restore_state_and_exit(0);
132 int slattach_main(int argc, char **argv);
133 int slattach_main(int argc, char **argv)
136 struct termios state;
137 const char *proto = "cslip";
138 const char *extcmd; /* Command to execute after hangup */
139 const char *baud_str;
140 int baud_code = -1; /* Line baud rate (system code) */
143 OPT_p_proto = 1 << 0,
145 OPT_c_extcmd = 1 << 2,
147 OPT_h_watch = 1 << 4,
148 OPT_m_nonraw = 1 << 5,
149 OPT_L_local = 1 << 6,
150 OPT_F_noflow = 1 << 7
155 /* Parse command line options */
156 opt = getopt32(argc, argv, "p:s:c:ehmLF", &proto, &baud_str, &extcmd);
163 encap = index_in_str_array(proto_names, proto);
166 invarg(proto, "protocol");
170 /* We want to know if the baud rate is valid before we start touching the ttys */
171 if (opt & OPT_s_baud) {
172 baud_code = tty_value_to_baud(xatoi(baud_str));
174 invarg(baud_str, "baud rate");
177 /* Trap signals in order to restore tty states upon exit */
178 if (!(opt & OPT_e_quit)) {
179 signal(SIGHUP, sig_handler);
180 signal(SIGINT, sig_handler);
181 signal(SIGQUIT, sig_handler);
182 signal(SIGTERM, sig_handler);
186 handle = open(*argv, O_RDWR | O_NDELAY);
188 char *buf = concat_path_file("/dev", *argv);
189 handle = xopen(buf, O_RDWR | O_NDELAY);
190 /* maybe if (ENABLE_FEATURE_CLEAN_UP) ?? */
194 /* Save current tty state */
198 memcpy(&state, &saved_state, sizeof(state));
199 if (!(opt & OPT_m_nonraw)) { /* raw not suppressed */
200 memset(&state.c_cc, 0, sizeof(state.c_cc));
201 state.c_cc[VMIN] = 1;
202 state.c_iflag = IGNBRK | IGNPAR;
205 state.c_cflag = CS8 | HUPCL | CREAD
206 | ((opt & OPT_L_local) ? CLOCAL : 0)
207 | ((opt & OPT_F_noflow) ? 0 : CRTSCTS);
210 if (opt & OPT_s_baud) {
211 cfsetispeed(&state, baud_code);
212 cfsetospeed(&state, baud_code);
215 set_state(&state, encap);
217 /* Exit now if option -e was passed */
218 if (opt & OPT_e_quit)
221 /* If we're not requested to watch, just keep descriptor open
222 * until we are killed */
223 if (!(opt & OPT_h_watch))
227 /* Watch line for hangup */
229 if (ioctl(handle, TIOCMGET, &i) < 0 || !(i & TIOCM_CAR))
236 /* Execute command on hangup */
237 if (opt & OPT_c_extcmd)
240 /* Restore states and exit */
241 restore_state_and_exit(0);