microcom.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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"
  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] [-t TIMEOUT] [-s SPEED] [-X] TTY"
  19. //usage:#define microcom_full_usage "\n\n"
  20. //usage: "Copy bytes for stdin to TTY and from TTY to stdout\n"
  21. //usage: "\n -d Wait up to DELAY ms for TTY output before sending every"
  22. //usage: "\n next byte to it"
  23. //usage: "\n -t Exit if both stdin and TTY are silent for TIMEOUT ms"
  24. //usage: "\n -s 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. //TODO: use set_termios_to_raw()
  32. tcgetattr(fd, oldt);
  33. *t = *oldt;
  34. cfmakeraw(t);
  35. // t->c_lflag &= ~(ISIG|ICANON|ECHO|IEXTEN);
  36. // t->c_iflag &= ~(BRKINT|IXON|ICRNL);
  37. // t->c_oflag &= ~(ONLCR);
  38. // t->c_cc[VMIN] = 1;
  39. // t->c_cc[VTIME] = 0;
  40. }
  41. static int xset1(int fd, struct termios *tio, const char *device)
  42. {
  43. int ret = tcsetattr(fd, TCSAFLUSH, tio);
  44. if (ret) {
  45. bb_perror_msg("can't tcsetattr for %s", device);
  46. }
  47. return ret;
  48. }
  49. int microcom_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  50. int microcom_main(int argc UNUSED_PARAM, char **argv)
  51. {
  52. int sfd;
  53. int nfd;
  54. struct pollfd pfd[2];
  55. struct termios tio0, tiosfd, tio;
  56. char *device_lock_file;
  57. enum {
  58. OPT_X = 1 << 0, // do not respect Ctrl-X, Ctrl-@
  59. OPT_s = 1 << 1, // baudrate
  60. OPT_d = 1 << 2, // wait for device response, ms
  61. OPT_t = 1 << 3, // timeout, ms
  62. };
  63. speed_t speed = 9600;
  64. int delay = -1;
  65. int timeout = -1;
  66. unsigned opts;
  67. // fetch options
  68. opts = getopt32(argv, "Xs:+d:+t:+", &speed, &delay, &timeout);
  69. // argc -= optind;
  70. argv += optind;
  71. // try to create lock file in /var/lock
  72. device_lock_file = (char *)bb_basename(argv[0]);
  73. device_lock_file = xasprintf("/var/lock/LCK..%s", device_lock_file);
  74. sfd = open(device_lock_file, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0644);
  75. if (sfd < 0) {
  76. // device already locked -> bail out
  77. if (errno == EEXIST)
  78. bb_perror_msg_and_die("can't create '%s'", device_lock_file);
  79. // can't create lock -> don't care
  80. if (ENABLE_FEATURE_CLEAN_UP)
  81. free(device_lock_file);
  82. device_lock_file = NULL;
  83. } else {
  84. // %4d to make concurrent mgetty (if any) happy.
  85. // Mgetty treats 4-bytes lock files as binary,
  86. // not text, PID. Making 5+ char file. Brrr...
  87. fdprintf(sfd, "%4d\n", getpid());
  88. close(sfd);
  89. }
  90. // setup signals
  91. bb_signals(0
  92. + (1 << SIGHUP)
  93. + (1 << SIGINT)
  94. + (1 << SIGTERM)
  95. + (1 << SIGPIPE)
  96. , record_signo);
  97. // error exit code if we fail to open the device
  98. bb_got_signal = 1;
  99. // open device
  100. sfd = open_or_warn(argv[0], O_RDWR | O_NOCTTY | O_NONBLOCK);
  101. if (sfd < 0)
  102. goto done;
  103. fcntl(sfd, F_SETFL, O_RDWR);
  104. // put device to "raw mode"
  105. xget1(sfd, &tio, &tiosfd);
  106. // set device speed
  107. cfsetspeed(&tio, tty_value_to_baud(speed));
  108. if (xset1(sfd, &tio, argv[0]))
  109. goto done;
  110. // put stdin to "raw mode" (if stdin is a TTY),
  111. // handle one character at a time
  112. if (isatty(STDIN_FILENO)) {
  113. xget1(STDIN_FILENO, &tio, &tio0);
  114. if (xset1(STDIN_FILENO, &tio, "stdin"))
  115. goto done;
  116. }
  117. // main loop: check with poll(), then read/write bytes across
  118. pfd[0].fd = sfd;
  119. pfd[0].events = POLLIN;
  120. pfd[1].fd = STDIN_FILENO;
  121. pfd[1].events = POLLIN;
  122. bb_got_signal = 0;
  123. nfd = 2;
  124. // Not safe_poll: we want to exit on signal
  125. while (!bb_got_signal && poll(pfd, nfd, timeout) > 0) {
  126. if (nfd > 1 && pfd[1].revents) {
  127. char c;
  128. // read from stdin -> write to device
  129. if (safe_read(STDIN_FILENO, &c, 1) < 1) {
  130. // don't poll stdin anymore if we got EOF/error
  131. nfd--;
  132. goto skip_write;
  133. }
  134. // do we need special processing?
  135. if (!(opts & OPT_X)) {
  136. // ^@ sends Break
  137. if (VINTR == c) {
  138. tcsendbreak(sfd, 0);
  139. goto skip_write;
  140. }
  141. // ^X exits
  142. if (24 == c)
  143. break;
  144. }
  145. write(sfd, &c, 1);
  146. if (delay >= 0)
  147. safe_poll(pfd, 1, delay);
  148. skip_write: ;
  149. }
  150. if (pfd[0].revents) {
  151. ssize_t len;
  152. #define iobuf bb_common_bufsiz1
  153. setup_common_bufsiz();
  154. // read from device -> write to stdout
  155. len = safe_read(sfd, iobuf, COMMON_BUFSIZE);
  156. if (len > 0)
  157. full_write(STDOUT_FILENO, iobuf, len);
  158. else {
  159. // EOF/error -> bail out
  160. bb_got_signal = SIGHUP;
  161. break;
  162. }
  163. }
  164. }
  165. // restore device mode
  166. tcsetattr(sfd, TCSAFLUSH, &tiosfd);
  167. if (isatty(STDIN_FILENO))
  168. tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio0);
  169. done:
  170. if (device_lock_file)
  171. unlink(device_lock_file);
  172. return bb_got_signal;
  173. }