microcom.c 4.7 KB

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