microcom.c 4.3 KB

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