microcom.c 4.6 KB

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