3
0

slattach.c 5.4 KB

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