microcom.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * bare bones 'talk to modem' program - similar to 'cu -l $device'
  4. * inspired by mgetty's microcom
  5. *
  6. * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
  7. *
  8. * Licensed under GPLv2, see file LICENSE in this source tree.
  9. */
  10. //config:config MICROCOM
  11. //config: bool "microcom (5.7 kb)"
  12. //config: default y
  13. //config: help
  14. //config: The poor man's minicom utility for chatting with serial port devices.
  15. //applet:IF_MICROCOM(APPLET(microcom, BB_DIR_USR_BIN, BB_SUID_DROP))
  16. //kbuild:lib-$(CONFIG_MICROCOM) += microcom.o
  17. //usage:#define microcom_trivial_usage
  18. //usage: "[-d DELAY_MS] [-t TIMEOUT_MS ] [-s SPEED] [-X] TTY"
  19. //usage:#define microcom_full_usage "\n\n"
  20. //usage: "Copy bytes from stdin to TTY and from TTY to stdout\n"
  21. //usage: "\n -d DELAY Wait up to DELAY ms for TTY output before sending"
  22. //usage: "\n every next byte to it"
  23. //usage: "\n -t TIMEOUT Exit if both stdin and TTY are silent for TIMEOUT ms"
  24. //usage: "\n -s SPEED Set serial line to SPEED"
  25. //usage: "\n -X Disable special meaning of NUL and Ctrl-X from stdin"
  26. #include "libbb.h"
  27. #include "common_bufsiz.h"
  28. // set raw tty mode
  29. static void xget1(int fd, struct termios *t, struct termios *oldt)
  30. {
  31. get_termios_and_make_raw(fd, t, oldt, 0
  32. | TERMIOS_CLEAR_ISIG /* ^C is ASCII char 3, not "interrupt me!" */
  33. | TERMIOS_RAW_INPUT /* pass all chars verbatim, no special handling or translating CR->NL */
  34. | TERMIOS_RAW_CRNL /* dont convert NL<->CR on output too */
  35. );
  36. }
  37. static int xset1(int fd, struct termios *tio, const char *device)
  38. {
  39. int ret = tcsetattr(fd, TCSAFLUSH, tio);
  40. if (ret) {
  41. bb_perror_msg("can't tcsetattr for %s", device);
  42. }
  43. return ret;
  44. }
  45. int microcom_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  46. int microcom_main(int argc UNUSED_PARAM, char **argv)
  47. {
  48. int sfd;
  49. int nfd;
  50. struct pollfd pfd[2];
  51. struct termios tio0, tiosfd, tio;
  52. char *device_lock_file;
  53. enum {
  54. OPT_X = 1 << 0, // do not respect Ctrl-X, Ctrl-@
  55. OPT_s = 1 << 1, // baudrate
  56. OPT_d = 1 << 2, // wait for device response, ms
  57. OPT_t = 1 << 3, // timeout, ms
  58. };
  59. speed_t speed = 9600;
  60. int delay = -1;
  61. int timeout = -1;
  62. unsigned opts;
  63. // fetch options
  64. opts = getopt32(argv, "^" "Xs:+d:+t:+" "\0" "=1",
  65. &speed, &delay, &timeout
  66. );
  67. // argc -= optind;
  68. argv += optind;
  69. // try to create lock file in /var/lock
  70. device_lock_file = (char *)bb_basename(argv[0]);
  71. device_lock_file = xasprintf("/var/lock/LCK..%s", device_lock_file);
  72. sfd = open(device_lock_file, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0644);
  73. if (sfd < 0) {
  74. // device already locked -> bail out
  75. if (errno == EEXIST)
  76. bb_perror_msg_and_die("can't create '%s'", device_lock_file);
  77. // can't create lock -> don't care
  78. if (ENABLE_FEATURE_CLEAN_UP)
  79. free(device_lock_file);
  80. device_lock_file = NULL;
  81. } else {
  82. // %4d to make concurrent mgetty (if any) happy.
  83. // Mgetty treats 4-bytes lock files as binary,
  84. // not text, PID. Making 5+ char file. Brrr...
  85. fdprintf(sfd, "%4d\n", getpid());
  86. close(sfd);
  87. }
  88. // setup signals
  89. bb_signals(0
  90. + (1 << SIGHUP)
  91. + (1 << SIGINT)
  92. + (1 << SIGTERM)
  93. + (1 << SIGPIPE)
  94. , record_signo);
  95. // error exit code if we fail to open the device
  96. bb_got_signal = 1;
  97. // open device
  98. sfd = open_or_warn(argv[0], O_RDWR | O_NOCTTY | O_NONBLOCK);
  99. if (sfd < 0)
  100. goto done;
  101. fcntl(sfd, F_SETFL, O_RDWR);
  102. // put device to "raw mode"
  103. xget1(sfd, &tio, &tiosfd);
  104. // set device speed
  105. cfsetspeed(&tio, tty_value_to_baud(speed));
  106. if (xset1(sfd, &tio, argv[0]))
  107. goto done;
  108. // put stdin to "raw mode" (if stdin is a TTY),
  109. // handle one character at a time
  110. if (isatty(STDIN_FILENO)) {
  111. xget1(STDIN_FILENO, &tio, &tio0);
  112. if (xset1(STDIN_FILENO, &tio, "stdin"))
  113. goto done;
  114. }
  115. // main loop: check with poll(), then read/write bytes across
  116. pfd[0].fd = sfd;
  117. pfd[0].events = POLLIN;
  118. pfd[1].fd = STDIN_FILENO;
  119. pfd[1].events = POLLIN;
  120. bb_got_signal = 0;
  121. nfd = 2;
  122. // Not safe_poll: we want to exit on signal
  123. while (!bb_got_signal && poll(pfd, nfd, timeout) > 0) {
  124. if (nfd > 1 && pfd[1].revents) {
  125. char c;
  126. // read from stdin -> write to device
  127. if (safe_read(STDIN_FILENO, &c, 1) < 1) {
  128. // don't poll stdin anymore if we got EOF/error
  129. nfd--;
  130. goto skip_write;
  131. }
  132. // do we need special processing?
  133. if (!(opts & OPT_X)) {
  134. // ^@ sends Break
  135. if (VINTR == c) {
  136. tcsendbreak(sfd, 0);
  137. goto skip_write;
  138. }
  139. // ^X exits
  140. if (24 == c)
  141. break;
  142. }
  143. write(sfd, &c, 1);
  144. if (delay >= 0)
  145. safe_poll(pfd, 1, delay);
  146. skip_write: ;
  147. }
  148. if (pfd[0].revents) {
  149. ssize_t len;
  150. #define iobuf bb_common_bufsiz1
  151. setup_common_bufsiz();
  152. // read from device -> write to stdout
  153. len = safe_read(sfd, iobuf, COMMON_BUFSIZE);
  154. if (len > 0)
  155. full_write(STDOUT_FILENO, iobuf, len);
  156. else {
  157. // EOF/error -> bail out
  158. bb_got_signal = SIGHUP;
  159. break;
  160. }
  161. }
  162. }
  163. // restore device mode
  164. tcsetattr(sfd, TCSAFLUSH, &tiosfd);
  165. if (isatty(STDIN_FILENO))
  166. tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio0);
  167. done:
  168. if (device_lock_file)
  169. unlink(device_lock_file);
  170. return bb_got_signal;
  171. }