Denis Vlasenko | 8333587 | 2007-06-21 07:27:43 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 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" |
Denis Vlasenko | 8333587 | 2007-06-21 07:27:43 +0000 | [diff] [blame] | 17 | #include "libiproute/utils.h" /* invarg() */ |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 18 | |
| 19 | /* Line discipline code table */ |
| 20 | static const char *const proto_names[] = { |
Denis Vlasenko | 8333587 | 2007-06-21 07:27:43 +0000 | [diff] [blame] | 21 | "cslip"+1, /* 0 */ |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 22 | "cslip", /* 1 */ |
Denis Vlasenko | 8333587 | 2007-06-21 07:27:43 +0000 | [diff] [blame] | 23 | "cslip6"+1, /* 2 */ |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 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 */ |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 53 | xioctl(handle, TIOCGETD, &saved_disc); |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Denis Vlasenko | 8333587 | 2007-06-21 07:27:43 +0000 | [diff] [blame] | 56 | static int set_termios_state_and_warn(struct termios *state) |
| 57 | { |
| 58 | int ret; |
| 59 | |
| 60 | ret = tcsetattr(handle, TCSANOW, state); |
| 61 | if (ret < 0) { |
| 62 | bb_perror_msg("set state"); |
| 63 | return 1; /* used as exitcode */ |
| 64 | } |
| 65 | return 0; |
| 66 | } |
| 67 | |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 68 | /* |
| 69 | * Restore state and line discipline for ALL managed ttys |
| 70 | * |
| 71 | * Restoring ALL managed ttys is the only way to have a single |
| 72 | * hangup delay. |
| 73 | * |
| 74 | * Go on after errors: we want to restore as many controlled ttys |
| 75 | * as possible. |
| 76 | */ |
| 77 | static void restore_state_and_exit(int exitcode) ATTRIBUTE_NORETURN; |
| 78 | static void restore_state_and_exit(int exitcode) |
| 79 | { |
| 80 | struct termios state; |
| 81 | |
| 82 | /* Restore line discipline */ |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 83 | if (ioctl_or_warn(handle, TIOCSETD, &saved_disc) < 0) { |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 84 | exitcode = 1; |
| 85 | } |
| 86 | |
| 87 | /* Hangup */ |
| 88 | memcpy(&state, &saved_state, sizeof(state)); |
| 89 | cfsetispeed(&state, B0); |
| 90 | cfsetospeed(&state, B0); |
Denis Vlasenko | 8333587 | 2007-06-21 07:27:43 +0000 | [diff] [blame] | 91 | if (set_termios_state_and_warn(&state)) |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 92 | exitcode = 1; |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 93 | sleep(1); |
| 94 | |
| 95 | /* Restore line status */ |
Denis Vlasenko | 8333587 | 2007-06-21 07:27:43 +0000 | [diff] [blame] | 96 | if (set_termios_state_and_warn(&saved_state)) |
| 97 | exit(EXIT_FAILURE); |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 98 | if (ENABLE_FEATURE_CLEAN_UP) |
| 99 | close(handle); |
| 100 | |
| 101 | exit(exitcode); |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * Set tty state, line discipline and encapsulation |
| 106 | */ |
| 107 | static void set_state(struct termios *state, int encap) |
| 108 | { |
| 109 | int disc; |
| 110 | |
| 111 | /* Set line status */ |
Denis Vlasenko | 8333587 | 2007-06-21 07:27:43 +0000 | [diff] [blame] | 112 | if (set_termios_state_and_warn(state)) |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 113 | goto bad; |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 114 | /* Set line discliple (N_SLIP always) */ |
| 115 | disc = N_SLIP; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 116 | if (ioctl_or_warn(handle, TIOCSETD, &disc) < 0) { |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 117 | goto bad; |
| 118 | } |
| 119 | |
| 120 | /* Set encapsulation (SLIP, CSLIP, etc) */ |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 121 | if (ioctl_or_warn(handle, SIOCSIFENCAP, &encap) < 0) { |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 122 | bad: |
| 123 | restore_state_and_exit(1); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | static void sig_handler(int signo) |
| 128 | { |
| 129 | restore_state_and_exit(0); |
| 130 | } |
| 131 | |
| 132 | int slattach_main(int argc, char **argv); |
| 133 | int slattach_main(int argc, char **argv) |
| 134 | { |
| 135 | int i, encap, opt; |
| 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) */ |
| 141 | |
| 142 | enum { |
| 143 | OPT_p_proto = 1 << 0, |
| 144 | OPT_s_baud = 1 << 1, |
| 145 | OPT_c_extcmd = 1 << 2, |
| 146 | OPT_e_quit = 1 << 3, |
| 147 | OPT_h_watch = 1 << 4, |
| 148 | OPT_m_nonraw = 1 << 5, |
| 149 | OPT_L_local = 1 << 6, |
Denis Vlasenko | 8333587 | 2007-06-21 07:27:43 +0000 | [diff] [blame] | 150 | OPT_F_noflow = 1 << 7 |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | INIT_G(); |
| 154 | |
| 155 | /* Parse command line options */ |
| 156 | opt = getopt32(argc, argv, "p:s:c:ehmLF", &proto, &baud_str, &extcmd); |
| 157 | /*argc -= optind;*/ |
| 158 | argv += optind; |
| 159 | |
| 160 | if (!*argv) |
| 161 | bb_show_usage(); |
| 162 | |
| 163 | encap = index_in_str_array(proto_names, proto); |
| 164 | |
| 165 | if (encap < 0) |
Denis Vlasenko | 8333587 | 2007-06-21 07:27:43 +0000 | [diff] [blame] | 166 | invarg(proto, "protocol"); |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 167 | if (encap > 3) |
| 168 | encap = 8; |
| 169 | |
| 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)); |
| 173 | if (baud_code < 0) |
Denis Vlasenko | 8333587 | 2007-06-21 07:27:43 +0000 | [diff] [blame] | 174 | invarg(baud_str, "baud rate"); |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 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); |
| 183 | } |
| 184 | |
Denis Vlasenko | 8333587 | 2007-06-21 07:27:43 +0000 | [diff] [blame] | 185 | /* Open tty */ |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 186 | handle = open(*argv, O_RDWR | O_NDELAY); |
| 187 | if (handle < 0) { |
Denis Vlasenko | 8333587 | 2007-06-21 07:27:43 +0000 | [diff] [blame] | 188 | char *buf = concat_path_file("/dev", *argv); |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 189 | handle = xopen(buf, O_RDWR | O_NDELAY); |
| 190 | /* maybe if (ENABLE_FEATURE_CLEAN_UP) ?? */ |
| 191 | free(buf); |
| 192 | } |
| 193 | |
| 194 | /* Save current tty state */ |
| 195 | save_state(); |
| 196 | |
Denis Vlasenko | 8333587 | 2007-06-21 07:27:43 +0000 | [diff] [blame] | 197 | /* Configure tty */ |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 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; |
| 203 | state.c_oflag = 0; |
| 204 | state.c_lflag = 0; |
| 205 | state.c_cflag = CS8 | HUPCL | CREAD |
| 206 | | ((opt & OPT_L_local) ? CLOCAL : 0) |
| 207 | | ((opt & OPT_F_noflow) ? 0 : CRTSCTS); |
| 208 | } |
| 209 | |
| 210 | if (opt & OPT_s_baud) { |
| 211 | cfsetispeed(&state, baud_code); |
| 212 | cfsetospeed(&state, baud_code); |
| 213 | } |
| 214 | |
| 215 | set_state(&state, encap); |
| 216 | |
| 217 | /* Exit now if option -e was passed */ |
| 218 | if (opt & OPT_e_quit) |
| 219 | return 0; |
| 220 | |
| 221 | /* If we're not requested to watch, just keep descriptor open |
Denis Vlasenko | 8333587 | 2007-06-21 07:27:43 +0000 | [diff] [blame] | 222 | * until we are killed */ |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 223 | if (!(opt & OPT_h_watch)) |
| 224 | while (1) |
| 225 | sleep(24*60*60); |
| 226 | |
| 227 | /* Watch line for hangup */ |
| 228 | while (1) { |
| 229 | if (ioctl(handle, TIOCMGET, &i) < 0 || !(i & TIOCM_CAR)) |
| 230 | goto no_carrier; |
| 231 | sleep(15); |
| 232 | } |
| 233 | |
| 234 | no_carrier: |
| 235 | |
| 236 | /* Execute command on hangup */ |
| 237 | if (opt & OPT_c_extcmd) |
| 238 | system(extcmd); |
| 239 | |
| 240 | /* Restore states and exit */ |
| 241 | restore_state_and_exit(0); |
| 242 | } |