slattach: code shrink (Bernhard Fischer <rep.dot.nop@gmail.com>)
[oweals/busybox.git] / networking / slattach.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Stripped down version of net-tools for busybox.
4  *
5  * Author: Ignacio Garcia Perez (iggarpe at gmail dot com)
6  *
7  * License: GPLv2 or later, see LICENSE file in this tarball.
8  *
9  * There are some differences from the standard net-tools slattach:
10  *
11  * - The -l option is not supported.
12  *
13  * - The -F options allows disabling of RTS/CTS flow control.
14  */
15
16 #include "libbb.h"
17 #include "libiproute/utils.h" /* invarg() */
18
19 /* Line discipline code table */
20 static const char *const proto_names[] = {
21         "cslip"+1,      /* 0 */
22         "cslip",        /* 1 */
23         "cslip6"+1,     /* 2 */
24         "cslip6",       /* 3 */
25         "adaptive",     /* 8 */
26         NULL
27 };
28
29 struct globals {
30         int handle;
31         int saved_disc;
32         struct termios saved_state;
33 };
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)
39
40
41 /*
42  * Save tty state and line discipline
43  *
44  * It is fine here to bail out on errors, since we haven modified anything yet
45  */
46 static void save_state(void)
47 {
48         /* Save line status */
49         if (tcgetattr(handle, &saved_state) < 0)
50                 bb_perror_msg_and_die("get state");
51
52         /* Save line discipline */
53         if (ioctl(handle, TIOCGETD, &saved_disc) < 0)
54                 bb_perror_msg_and_die("get discipline");
55 }
56
57 static int set_termios_state_and_warn(struct termios *state)
58 {
59         int ret;
60
61         ret = tcsetattr(handle, TCSANOW, state);
62         if (ret < 0) {
63                 bb_perror_msg("set state");
64                 return 1; /* used as exitcode */
65         }
66         return 0;
67 }
68
69 /*
70  * Restore state and line discipline for ALL managed ttys
71  *
72  * Restoring ALL managed ttys is the only way to have a single
73  * hangup delay.
74  *
75  * Go on after errors: we want to restore as many controlled ttys
76  * as possible.
77  */
78 static void restore_state_and_exit(int exitcode) ATTRIBUTE_NORETURN;
79 static void restore_state_and_exit(int exitcode)
80 {
81         struct termios state;
82
83         /* Restore line discipline */
84         if (ioctl(handle, TIOCSETD, &saved_disc) < 0) {
85                 bb_perror_msg("set discipline");
86                 exitcode = 1;
87         }
88
89         /* Hangup */
90         memcpy(&state, &saved_state, sizeof(state));
91         cfsetispeed(&state, B0);
92         cfsetospeed(&state, B0);
93         if (set_termios_state_and_warn(&state))
94                 exitcode = 1;
95         sleep(1);
96
97         /* Restore line status */
98         if (set_termios_state_and_warn(&saved_state))
99                 exit(EXIT_FAILURE);
100         if (ENABLE_FEATURE_CLEAN_UP)
101                 close(handle);
102
103         exit(exitcode);
104 }
105
106 /*
107  * Set tty state, line discipline and encapsulation
108  */
109 static void set_state(struct termios *state, int encap)
110 {
111         int disc;
112
113         /* Set line status */
114         if (set_termios_state_and_warn(state))
115                 goto bad;
116         /* Set line discliple (N_SLIP always) */
117         disc = N_SLIP;
118         if (ioctl(handle, TIOCSETD, &disc) < 0) {
119                 bb_perror_msg("set discipline");
120                 goto bad;
121         }
122
123         /* Set encapsulation (SLIP, CSLIP, etc) */
124         if (ioctl(handle, SIOCSIFENCAP, &encap) < 0) {
125                 bb_perror_msg("set encapsulation");
126  bad:
127                 restore_state_and_exit(1);
128         }
129 }
130
131 static void sig_handler(int signo)
132 {
133         restore_state_and_exit(0);
134 }
135
136 int slattach_main(int argc, char **argv);
137 int slattach_main(int argc, char **argv)
138 {
139         int i, encap, opt;
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) */
145
146         enum {
147                 OPT_p_proto  = 1 << 0,
148                 OPT_s_baud   = 1 << 1,
149                 OPT_c_extcmd = 1 << 2,
150                 OPT_e_quit   = 1 << 3,
151                 OPT_h_watch  = 1 << 4,
152                 OPT_m_nonraw = 1 << 5,
153                 OPT_L_local  = 1 << 6,
154                 OPT_F_noflow = 1 << 7
155         };
156
157         INIT_G();
158
159         /* Parse command line options */
160         opt = getopt32(argc, argv, "p:s:c:ehmLF", &proto, &baud_str, &extcmd);
161         /*argc -= optind;*/
162         argv += optind;
163
164         if (!*argv)
165                 bb_show_usage();
166
167         encap = index_in_str_array(proto_names, proto);
168
169         if (encap < 0)
170                 invarg(proto, "protocol");
171         if (encap > 3)
172                 encap = 8;
173
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));
177                 if (baud_code < 0)
178                         invarg(baud_str, "baud rate");
179         }
180
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);
187         }
188
189         /* Open tty */
190         handle = open(*argv, O_RDWR | O_NDELAY);
191         if (handle < 0) {
192                 char *buf = concat_path_file("/dev", *argv);
193                 handle = xopen(buf, O_RDWR | O_NDELAY);
194                 /* maybe if (ENABLE_FEATURE_CLEAN_UP) ?? */
195                 free(buf);
196         }
197
198         /* Save current tty state */
199         save_state();
200
201         /* Configure tty */
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;
207                 state.c_oflag = 0;
208                 state.c_lflag = 0;
209                 state.c_cflag = CS8 | HUPCL | CREAD
210                               | ((opt & OPT_L_local) ? CLOCAL : 0)
211                               | ((opt & OPT_F_noflow) ? 0 : CRTSCTS);
212         }
213
214         if (opt & OPT_s_baud) {
215                 cfsetispeed(&state, baud_code);
216                 cfsetospeed(&state, baud_code);
217         }
218
219         set_state(&state, encap);
220
221         /* Exit now if option -e was passed */
222         if (opt & OPT_e_quit)
223                 return 0;
224
225         /* If we're not requested to watch, just keep descriptor open
226          * until we are killed */
227         if (!(opt & OPT_h_watch))
228                 while (1)
229                         sleep(24*60*60);
230
231         /* Watch line for hangup */
232         while (1) {
233                 if (ioctl(handle, TIOCMGET, &i) < 0 || !(i & TIOCM_CAR))
234                         goto no_carrier;
235                 sleep(15);
236         }
237
238  no_carrier:
239
240         /* Execute command on hangup */
241         if (opt & OPT_c_extcmd)
242                 system(extcmd);
243
244         /* Restore states and exit */
245         restore_state_and_exit(0);
246 }