slattach.c 6.0 KB

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