slattach.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. //config:config SLATTACH
  16. //config: bool "slattach (6.2 kb)"
  17. //config: default y
  18. //config: help
  19. //config: slattach configures serial line as SLIP network interface.
  20. //applet:IF_SLATTACH(APPLET(slattach, BB_DIR_SBIN, BB_SUID_DROP))
  21. /* shouldn't be NOEXEC: may sleep indefinitely */
  22. //kbuild:lib-$(CONFIG_SLATTACH) += slattach.o
  23. //usage:#define slattach_trivial_usage
  24. //usage: "[-ehmLF] [-c SCRIPT] [-s BAUD] [-p PROTOCOL] SERIAL_DEVICE"
  25. //usage:#define slattach_full_usage "\n\n"
  26. //usage: "Configure serial line as SLIP network interface\n"
  27. //usage: "\n -p PROT Protocol: slip, cslip (default), slip6, clisp6, adaptive"
  28. //usage: "\n -s BAUD Line speed"
  29. //usage: "\n -e Exit after initialization"
  30. //usage: "\n -h Exit if carrier is lost (else never exits)"
  31. //usage: "\n -c PROG Run PROG on carrier loss"
  32. //usage: "\n -m Do NOT set raw 8bit mode"
  33. //usage: "\n -L Enable 3-wire operation"
  34. //usage: "\n -F Disable RTS/CTS flow control"
  35. #include "libbb.h"
  36. #include "common_bufsiz.h"
  37. #include "libiproute/utils.h" /* invarg_1_to_2() */
  38. struct globals {
  39. int saved_disc;
  40. struct termios saved_state;
  41. } FIX_ALIASING;
  42. #define G (*(struct globals*)bb_common_bufsiz1)
  43. #define INIT_G() do { setup_common_bufsiz(); } while (0)
  44. #define serial_fd 3
  45. static int tcsetattr_serial_or_warn(struct termios *state)
  46. {
  47. int ret;
  48. ret = tcsetattr(serial_fd, TCSANOW, state);
  49. if (ret != 0) {
  50. bb_simple_perror_msg("tcsetattr");
  51. return 1; /* used as exitcode */
  52. }
  53. return ret; /* 0 */
  54. }
  55. static void restore_state_and_exit(int exitcode) NORETURN;
  56. static void restore_state_and_exit(int exitcode)
  57. {
  58. struct termios state;
  59. /* Restore line discipline */
  60. if (ioctl_or_warn(serial_fd, TIOCSETD, &G.saved_disc)) {
  61. exitcode = 1;
  62. }
  63. /* Hangup */
  64. memcpy(&state, &G.saved_state, sizeof(state));
  65. cfsetispeed(&state, B0);
  66. cfsetospeed(&state, B0);
  67. exitcode |= tcsetattr_serial_or_warn(&state);
  68. sleep1();
  69. /* Restore line status */
  70. if (tcsetattr_serial_or_warn(&G.saved_state))
  71. exit_FAILURE();
  72. if (ENABLE_FEATURE_CLEAN_UP)
  73. close(serial_fd);
  74. exit(exitcode);
  75. }
  76. static void sig_handler(int signo UNUSED_PARAM)
  77. {
  78. restore_state_and_exit(EXIT_SUCCESS);
  79. }
  80. int slattach_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  81. int slattach_main(int argc UNUSED_PARAM, char **argv)
  82. {
  83. /* Line discipline code table */
  84. static const char proto_names[] ALIGN1 =
  85. "slip\0" /* 0 */
  86. "cslip\0" /* 1 */
  87. "slip6\0" /* 2 */
  88. "cslip6\0" /* 3 */
  89. "adaptive\0" /* 8 */
  90. ;
  91. static const int int_N_SLIP = N_SLIP;
  92. int encap, opt, fd;
  93. struct termios state;
  94. const char *proto = "cslip";
  95. const char *extcmd; /* Command to execute after hangup */
  96. const char *baud_str;
  97. int baud_code = baud_code; /* for compiler */
  98. enum {
  99. OPT_p_proto = 1 << 0,
  100. OPT_s_baud = 1 << 1,
  101. OPT_c_extcmd = 1 << 2,
  102. OPT_e_quit = 1 << 3,
  103. OPT_h_watch = 1 << 4,
  104. OPT_m_nonraw = 1 << 5,
  105. OPT_L_local = 1 << 6,
  106. OPT_F_noflow = 1 << 7
  107. };
  108. INIT_G();
  109. /* Parse command line options */
  110. opt = getopt32(argv, "^" "p:s:c:ehmLF" "\0" "=1",
  111. &proto, &baud_str, &extcmd
  112. );
  113. /*argc -= optind;*/
  114. argv += optind;
  115. encap = index_in_strings(proto_names, proto);
  116. if (encap < 0)
  117. invarg_1_to_2(proto, "protocol");
  118. if (encap > 3)
  119. encap = 8;
  120. /* We want to know if the baud rate is valid before we start touching the ttys */
  121. if (opt & OPT_s_baud) {
  122. baud_code = tty_value_to_baud(xatoi(baud_str));
  123. if (baud_code < 0)
  124. invarg_1_to_2(baud_str, "baud rate");
  125. }
  126. /* Open tty */
  127. fd = open(*argv, O_RDWR | O_NDELAY);
  128. if (fd < 0) {
  129. char *buf = concat_path_file("/dev", *argv);
  130. fd = xopen(buf, O_RDWR | O_NDELAY);
  131. /* maybe if (ENABLE_FEATURE_CLEAN_UP) ?? */
  132. free(buf);
  133. }
  134. xmove_fd(fd, serial_fd);
  135. /* Save current tty state */
  136. if (tcgetattr(serial_fd, &G.saved_state) != 0)
  137. bb_simple_perror_msg_and_die("tcgetattr");
  138. /* Save line discipline */
  139. xioctl(serial_fd, TIOCGETD, &G.saved_disc);
  140. /* Trap signals in order to restore tty states upon exit */
  141. if (!(opt & OPT_e_quit)) {
  142. bb_signals(0
  143. + (1 << SIGHUP)
  144. + (1 << SIGINT)
  145. + (1 << SIGQUIT)
  146. + (1 << SIGTERM)
  147. , sig_handler);
  148. }
  149. /* Configure tty */
  150. memcpy(&state, &G.saved_state, sizeof(state));
  151. if (!(opt & OPT_m_nonraw)) { /* raw not suppressed */
  152. memset(&state.c_cc, 0, sizeof(state.c_cc));
  153. state.c_cc[VMIN] = 1;
  154. state.c_iflag = IGNBRK | IGNPAR;
  155. /*state.c_oflag = 0;*/
  156. /*state.c_lflag = 0;*/
  157. state.c_cflag = CS8 | HUPCL | CREAD
  158. | ((opt & OPT_L_local) ? CLOCAL : 0)
  159. | ((opt & OPT_F_noflow) ? 0 : CRTSCTS);
  160. cfsetispeed(&state, cfgetispeed(&G.saved_state));
  161. cfsetospeed(&state, cfgetospeed(&G.saved_state));
  162. }
  163. if (opt & OPT_s_baud) {
  164. cfsetispeed(&state, baud_code);
  165. cfsetospeed(&state, baud_code);
  166. }
  167. /* Set line status */
  168. if (tcsetattr_serial_or_warn(&state))
  169. goto bad;
  170. /* Set line disclipline (N_SLIP always) */
  171. if (ioctl_or_warn(serial_fd, TIOCSETD, (void*)&int_N_SLIP))
  172. goto bad;
  173. /* Set encapsulation (SLIP, CSLIP, etc) */
  174. if (ioctl_or_warn(serial_fd, SIOCSIFENCAP, &encap))
  175. goto bad;
  176. /* Exit now if option -e was passed */
  177. if (opt & OPT_e_quit)
  178. return EXIT_SUCCESS;
  179. /* If we're not requested to watch, just keep descriptor open
  180. * until we are killed */
  181. if (!(opt & OPT_h_watch))
  182. while (1)
  183. sleep(24*60*60);
  184. /* Watch line for hangup */
  185. while (1) {
  186. int modem_stat;
  187. if (ioctl(serial_fd, TIOCMGET, &modem_stat))
  188. break;
  189. if (!(modem_stat & TIOCM_CAR))
  190. break;
  191. sleep(15);
  192. }
  193. /* Execute command on hangup */
  194. if (opt & OPT_c_extcmd)
  195. system(extcmd);
  196. /* Restore states and exit */
  197. restore_state_and_exit(EXIT_SUCCESS);
  198. bad:
  199. restore_state_and_exit(EXIT_FAILURE);
  200. }