3
0

slattach.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  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. //usage:#define slattach_trivial_usage
  16. //usage: "[-cehmLF] [-s SPEED] [-p PROTOCOL] DEVICE"
  17. //usage:#define slattach_full_usage "\n\n"
  18. //usage: "Attach network interface(s) to serial line(s)\n"
  19. //usage: "\n -p PROT Set protocol (slip, cslip, slip6, clisp6 or adaptive)"
  20. //usage: "\n -s SPD Set line speed"
  21. //usage: "\n -e Exit after initializing device"
  22. //usage: "\n -h Exit when the carrier is lost"
  23. //usage: "\n -c PROG Run PROG when the line is hung up"
  24. //usage: "\n -m Do NOT initialize the line in raw 8 bits mode"
  25. //usage: "\n -L Enable 3-wire operation"
  26. //usage: "\n -F Disable RTS/CTS flow control"
  27. #include "libbb.h"
  28. #include "libiproute/utils.h" /* invarg_1_to_2() */
  29. struct globals {
  30. int handle;
  31. int saved_disc;
  32. struct termios saved_state;
  33. } FIX_ALIASING;
  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. * Save tty state and line discipline
  41. *
  42. * It is fine here to bail out on errors, since we haven modified anything yet
  43. */
  44. static void save_state(void)
  45. {
  46. /* Save line status */
  47. if (tcgetattr(handle, &saved_state) < 0)
  48. bb_perror_msg_and_die("get state");
  49. /* Save line discipline */
  50. xioctl(handle, TIOCGETD, &saved_disc);
  51. }
  52. static int set_termios_state_or_warn(struct termios *state)
  53. {
  54. int ret;
  55. ret = tcsetattr(handle, TCSANOW, state);
  56. if (ret < 0) {
  57. bb_perror_msg("set state");
  58. return 1; /* used as exitcode */
  59. }
  60. return 0;
  61. }
  62. /*
  63. * Restore state and line discipline for ALL managed ttys
  64. *
  65. * Restoring ALL managed ttys is the only way to have a single
  66. * hangup delay.
  67. *
  68. * Go on after errors: we want to restore as many controlled ttys
  69. * as possible.
  70. */
  71. static void restore_state_and_exit(int exitcode) NORETURN;
  72. static void restore_state_and_exit(int exitcode)
  73. {
  74. struct termios state;
  75. /* Restore line discipline */
  76. if (ioctl_or_warn(handle, TIOCSETD, &saved_disc) < 0) {
  77. exitcode = 1;
  78. }
  79. /* Hangup */
  80. memcpy(&state, &saved_state, sizeof(state));
  81. cfsetispeed(&state, B0);
  82. cfsetospeed(&state, B0);
  83. if (set_termios_state_or_warn(&state))
  84. exitcode = 1;
  85. sleep(1);
  86. /* Restore line status */
  87. if (set_termios_state_or_warn(&saved_state))
  88. exit(EXIT_FAILURE);
  89. if (ENABLE_FEATURE_CLEAN_UP)
  90. close(handle);
  91. exit(exitcode);
  92. }
  93. /*
  94. * Set tty state, line discipline and encapsulation
  95. */
  96. static void set_state(struct termios *state, int encap)
  97. {
  98. int disc;
  99. /* Set line status */
  100. if (set_termios_state_or_warn(state))
  101. goto bad;
  102. /* Set line discliple (N_SLIP always) */
  103. disc = N_SLIP;
  104. if (ioctl_or_warn(handle, TIOCSETD, &disc) < 0) {
  105. goto bad;
  106. }
  107. /* Set encapsulation (SLIP, CSLIP, etc) */
  108. if (ioctl_or_warn(handle, SIOCSIFENCAP, &encap) < 0) {
  109. bad:
  110. restore_state_and_exit(EXIT_FAILURE);
  111. }
  112. }
  113. static void sig_handler(int signo UNUSED_PARAM)
  114. {
  115. restore_state_and_exit(EXIT_SUCCESS);
  116. }
  117. int slattach_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  118. int slattach_main(int argc UNUSED_PARAM, char **argv)
  119. {
  120. /* Line discipline code table */
  121. static const char proto_names[] ALIGN1 =
  122. "slip\0" /* 0 */
  123. "cslip\0" /* 1 */
  124. "slip6\0" /* 2 */
  125. "cslip6\0" /* 3 */
  126. "adaptive\0" /* 8 */
  127. ;
  128. int i, encap, opt;
  129. struct termios state;
  130. const char *proto = "cslip";
  131. const char *extcmd; /* Command to execute after hangup */
  132. const char *baud_str;
  133. int baud_code = -1; /* Line baud rate (system code) */
  134. enum {
  135. OPT_p_proto = 1 << 0,
  136. OPT_s_baud = 1 << 1,
  137. OPT_c_extcmd = 1 << 2,
  138. OPT_e_quit = 1 << 3,
  139. OPT_h_watch = 1 << 4,
  140. OPT_m_nonraw = 1 << 5,
  141. OPT_L_local = 1 << 6,
  142. OPT_F_noflow = 1 << 7
  143. };
  144. INIT_G();
  145. /* Parse command line options */
  146. opt = getopt32(argv, "p:s:c:ehmLF", &proto, &baud_str, &extcmd);
  147. /*argc -= optind;*/
  148. argv += optind;
  149. if (!*argv)
  150. bb_show_usage();
  151. encap = index_in_strings(proto_names, proto);
  152. if (encap < 0)
  153. invarg_1_to_2(proto, "protocol");
  154. if (encap > 3)
  155. encap = 8;
  156. /* We want to know if the baud rate is valid before we start touching the ttys */
  157. if (opt & OPT_s_baud) {
  158. baud_code = tty_value_to_baud(xatoi(baud_str));
  159. if (baud_code < 0)
  160. invarg_1_to_2(baud_str, "baud rate");
  161. }
  162. /* Trap signals in order to restore tty states upon exit */
  163. if (!(opt & OPT_e_quit)) {
  164. bb_signals(0
  165. + (1 << SIGHUP)
  166. + (1 << SIGINT)
  167. + (1 << SIGQUIT)
  168. + (1 << SIGTERM)
  169. , sig_handler);
  170. }
  171. /* Open tty */
  172. handle = open(*argv, O_RDWR | O_NDELAY);
  173. if (handle < 0) {
  174. char *buf = concat_path_file("/dev", *argv);
  175. handle = xopen(buf, O_RDWR | O_NDELAY);
  176. /* maybe if (ENABLE_FEATURE_CLEAN_UP) ?? */
  177. free(buf);
  178. }
  179. /* Save current tty state */
  180. save_state();
  181. /* Configure tty */
  182. memcpy(&state, &saved_state, sizeof(state));
  183. if (!(opt & OPT_m_nonraw)) { /* raw not suppressed */
  184. memset(&state.c_cc, 0, sizeof(state.c_cc));
  185. state.c_cc[VMIN] = 1;
  186. state.c_iflag = IGNBRK | IGNPAR;
  187. state.c_oflag = 0;
  188. state.c_lflag = 0;
  189. state.c_cflag = CS8 | HUPCL | CREAD
  190. | ((opt & OPT_L_local) ? CLOCAL : 0)
  191. | ((opt & OPT_F_noflow) ? 0 : CRTSCTS);
  192. cfsetispeed(&state, cfgetispeed(&saved_state));
  193. cfsetospeed(&state, cfgetospeed(&saved_state));
  194. }
  195. if (opt & OPT_s_baud) {
  196. cfsetispeed(&state, baud_code);
  197. cfsetospeed(&state, baud_code);
  198. }
  199. set_state(&state, encap);
  200. /* Exit now if option -e was passed */
  201. if (opt & OPT_e_quit)
  202. return 0;
  203. /* If we're not requested to watch, just keep descriptor open
  204. * until we are killed */
  205. if (!(opt & OPT_h_watch))
  206. while (1)
  207. sleep(24*60*60);
  208. /* Watch line for hangup */
  209. while (1) {
  210. if (ioctl(handle, TIOCMGET, &i) < 0 || !(i & TIOCM_CAR))
  211. goto no_carrier;
  212. sleep(15);
  213. }
  214. no_carrier:
  215. /* Execute command on hangup */
  216. if (opt & OPT_c_extcmd)
  217. system(extcmd);
  218. /* Restore states and exit */
  219. restore_state_and_exit(EXIT_SUCCESS);
  220. }