microcom.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 UNUSED_PARAM, 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. // open device
  89. sfd = open_or_warn(argv[0], O_RDWR | O_NOCTTY | O_NONBLOCK);
  90. if (sfd < 0)
  91. goto done;
  92. fcntl(sfd, F_SETFL, 0);
  93. // put device to "raw mode"
  94. xget1(sfd, &tio, &tiosfd);
  95. // set device speed
  96. cfsetspeed(&tio, tty_value_to_baud(speed));
  97. if (xset1(sfd, &tio, argv[0]))
  98. goto done;
  99. // put stdin to "raw mode" (if stdin is a TTY),
  100. // handle one character at a time
  101. if (isatty(STDIN_FILENO)) {
  102. xget1(STDIN_FILENO, &tio, &tio0);
  103. if (xset1(STDIN_FILENO, &tio, "stdin"))
  104. goto done;
  105. }
  106. // main loop: check with poll(), then read/write bytes across
  107. pfd[0].fd = sfd;
  108. pfd[0].events = POLLIN;
  109. pfd[1].fd = STDIN_FILENO;
  110. pfd[1].events = POLLIN;
  111. signalled = 0;
  112. nfd = 2;
  113. while (!signalled && safe_poll(pfd, nfd, timeout) > 0) {
  114. if (nfd > 1 && pfd[1].revents) {
  115. char c;
  116. // read from stdin -> write to device
  117. if (safe_read(STDIN_FILENO, &c, 1) < 1) {
  118. // don't poll stdin anymore if we got EOF/error
  119. nfd--;
  120. goto skip_write;
  121. }
  122. // do we need special processing?
  123. if (!(opts & OPT_X)) {
  124. // ^@ sends Break
  125. if (VINTR == c) {
  126. tcsendbreak(sfd, 0);
  127. goto skip_write;
  128. }
  129. // ^X exits
  130. if (24 == c)
  131. break;
  132. }
  133. write(sfd, &c, 1);
  134. if (delay >= 0)
  135. safe_poll(pfd, 1, delay);
  136. skip_write: ;
  137. }
  138. if (pfd[0].revents) {
  139. #define iobuf bb_common_bufsiz1
  140. ssize_t len;
  141. // read from device -> write to stdout
  142. len = safe_read(sfd, iobuf, sizeof(iobuf));
  143. if (len > 0)
  144. full_write(STDOUT_FILENO, iobuf, len);
  145. else {
  146. // EOF/error -> bail out
  147. signalled = SIGHUP;
  148. break;
  149. }
  150. }
  151. }
  152. // restore device mode
  153. tcsetattr(sfd, TCSAFLUSH, &tiosfd);
  154. if (isatty(STDIN_FILENO))
  155. tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio0);
  156. done:
  157. if (device_lock_file)
  158. unlink(device_lock_file);
  159. return signalled;
  160. }