slattach.c 6.2 KB

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