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 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
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.
16 //usage:#define slattach_trivial_usage
17 //usage: "[-cehmLF] [-s SPEED] [-p PROTOCOL] DEVICE"
18 //usage:#define slattach_full_usage "\n\n"
19 //usage: "Attach network interface(s) to serial line(s)\n"
20 //usage: "\n -p PROT Set protocol (slip, cslip, slip6, clisp6 or adaptive)"
21 //usage: "\n -s SPD Set line speed"
22 //usage: "\n -e Exit after initializing device"
23 //usage: "\n -h Exit when the carrier is lost"
24 //usage: "\n -c PROG Run PROG when the line is hung up"
25 //usage: "\n -m Do NOT initialize the line in raw 8 bits mode"
26 //usage: "\n -L Enable 3-wire operation"
27 //usage: "\n -F Disable RTS/CTS flow control"
30 #include "common_bufsiz.h"
31 #include "libiproute/utils.h" /* invarg_1_to_2() */
36 struct termios saved_state
;
38 #define G (*(struct globals*)bb_common_bufsiz1)
39 #define handle (G.handle )
40 #define saved_disc (G.saved_disc )
41 #define saved_state (G.saved_state )
42 #define INIT_G() do { setup_common_bufsiz(); } while (0)
46 * Save tty state and line discipline
48 * It is fine here to bail out on errors, since we haven modified anything yet
50 static void save_state(void)
52 /* Save line status */
53 if (tcgetattr(handle
, &saved_state
) < 0)
54 bb_perror_msg_and_die("get state");
56 /* Save line discipline */
57 xioctl(handle
, TIOCGETD
, &saved_disc
);
60 static int set_termios_state_or_warn(struct termios
*state
)
64 ret
= tcsetattr(handle
, TCSANOW
, state
);
66 bb_perror_msg("set state");
67 return 1; /* used as exitcode */
73 * Restore state and line discipline for ALL managed ttys
75 * Restoring ALL managed ttys is the only way to have a single
78 * Go on after errors: we want to restore as many controlled ttys
81 static void restore_state_and_exit(int exitcode
) NORETURN
;
82 static void restore_state_and_exit(int exitcode
)
86 /* Restore line discipline */
87 if (ioctl_or_warn(handle
, TIOCSETD
, &saved_disc
) < 0) {
92 memcpy(&state
, &saved_state
, sizeof(state
));
93 cfsetispeed(&state
, B0
);
94 cfsetospeed(&state
, B0
);
95 if (set_termios_state_or_warn(&state
))
99 /* Restore line status */
100 if (set_termios_state_or_warn(&saved_state
))
102 if (ENABLE_FEATURE_CLEAN_UP
)
109 * Set tty state, line discipline and encapsulation
111 static void set_state(struct termios
*state
, int encap
)
115 /* Set line status */
116 if (set_termios_state_or_warn(state
))
118 /* Set line discliple (N_SLIP always) */
120 if (ioctl_or_warn(handle
, TIOCSETD
, &disc
) < 0) {
124 /* Set encapsulation (SLIP, CSLIP, etc) */
125 if (ioctl_or_warn(handle
, SIOCSIFENCAP
, &encap
) < 0) {
127 restore_state_and_exit(EXIT_FAILURE
);
131 static void sig_handler(int signo UNUSED_PARAM
)
133 restore_state_and_exit(EXIT_SUCCESS
);
136 int slattach_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
137 int slattach_main(int argc UNUSED_PARAM
, char **argv
)
139 /* Line discipline code table */
140 static const char proto_names
[] ALIGN1
=
149 struct termios state
;
150 const char *proto
= "cslip";
151 const char *extcmd
; /* Command to execute after hangup */
152 const char *baud_str
;
153 int baud_code
= -1; /* Line baud rate (system code) */
156 OPT_p_proto
= 1 << 0,
158 OPT_c_extcmd
= 1 << 2,
160 OPT_h_watch
= 1 << 4,
161 OPT_m_nonraw
= 1 << 5,
162 OPT_L_local
= 1 << 6,
163 OPT_F_noflow
= 1 << 7
168 /* Parse command line options */
169 opt
= getopt32(argv
, "p:s:c:ehmLF", &proto
, &baud_str
, &extcmd
);
176 encap
= index_in_strings(proto_names
, proto
);
179 invarg_1_to_2(proto
, "protocol");
183 /* We want to know if the baud rate is valid before we start touching the ttys */
184 if (opt
& OPT_s_baud
) {
185 baud_code
= tty_value_to_baud(xatoi(baud_str
));
187 invarg_1_to_2(baud_str
, "baud rate");
190 /* Trap signals in order to restore tty states upon exit */
191 if (!(opt
& OPT_e_quit
)) {
201 handle
= open(*argv
, O_RDWR
| O_NDELAY
);
203 char *buf
= concat_path_file("/dev", *argv
);
204 handle
= xopen(buf
, O_RDWR
| O_NDELAY
);
205 /* maybe if (ENABLE_FEATURE_CLEAN_UP) ?? */
209 /* Save current tty state */
213 memcpy(&state
, &saved_state
, sizeof(state
));
214 if (!(opt
& OPT_m_nonraw
)) { /* raw not suppressed */
215 memset(&state
.c_cc
, 0, sizeof(state
.c_cc
));
216 state
.c_cc
[VMIN
] = 1;
217 state
.c_iflag
= IGNBRK
| IGNPAR
;
220 state
.c_cflag
= CS8
| HUPCL
| CREAD
221 | ((opt
& OPT_L_local
) ? CLOCAL
: 0)
222 | ((opt
& OPT_F_noflow
) ? 0 : CRTSCTS
);
223 cfsetispeed(&state
, cfgetispeed(&saved_state
));
224 cfsetospeed(&state
, cfgetospeed(&saved_state
));
227 if (opt
& OPT_s_baud
) {
228 cfsetispeed(&state
, baud_code
);
229 cfsetospeed(&state
, baud_code
);
232 set_state(&state
, encap
);
234 /* Exit now if option -e was passed */
235 if (opt
& OPT_e_quit
)
238 /* If we're not requested to watch, just keep descriptor open
239 * until we are killed */
240 if (!(opt
& OPT_h_watch
))
244 /* Watch line for hangup */
246 if (ioctl(handle
, TIOCMGET
, &i
) < 0 || !(i
& TIOCM_CAR
))
253 /* Execute command on hangup */
254 if (opt
& OPT_c_extcmd
)
257 /* Restore states and exit */
258 restore_state_and_exit(EXIT_SUCCESS
);