Bernhard Reutner-Fischer | f588f70 | 2007-09-22 20:35:32 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 3 | * bare bones 'talk to modem' program - similar to 'cu -l $device' |
| 4 | * inspired by mgetty's microcom |
| 5 | * |
Denis Vlasenko | f34e821 | 2007-09-22 20:23:57 +0000 | [diff] [blame] | 6 | * Copyright (C) 2007 by Vladimir Dronnikov <dronnikov@gmail.ru> |
| 7 | * |
| 8 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 9 | */ |
Denis Vlasenko | 9a4e08e | 2007-09-22 21:37:27 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 11 | |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 12 | /* All known arches use small ints for signals */ |
| 13 | static volatile smallint signalled; |
| 14 | |
| 15 | static void signal_handler(int signo) |
| 16 | { |
| 17 | signalled = signo; |
| 18 | } |
| 19 | |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 20 | // set raw tty mode |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 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 | |
| 33 | static int xset1(int fd, struct termios *tio, const char *device) |
| 34 | { |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 35 | int ret = tcsetattr(fd, TCSAFLUSH, tio); |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 36 | |
| 37 | if (ret) { |
| 38 | bb_perror_msg("can't tcsetattr for %s", device); |
| 39 | } |
| 40 | return ret; |
| 41 | } |
| 42 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 43 | int microcom_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 44 | int microcom_main(int argc, char **argv) |
| 45 | { |
Denis Vlasenko | 0effc24 | 2008-01-28 09:39:30 +0000 | [diff] [blame] | 46 | int sfd; |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 47 | int nfd; |
| 48 | struct pollfd pfd[2]; |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 49 | struct termios tio0, tiosfd, tio; |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 50 | char *device_lock_file; |
| 51 | enum { |
| 52 | OPT_X = 1 << 0, // do not respect Ctrl-X, Ctrl-@ |
| 53 | OPT_s = 1 << 1, // baudrate |
Denis Vlasenko | 68e8e96 | 2008-02-09 11:39:00 +0000 | [diff] [blame] | 54 | OPT_d = 1 << 2, // wait for device response, msecs |
| 55 | OPT_t = 1 << 3, // timeout, ms |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 56 | }; |
| 57 | speed_t speed = 9600; |
Denis Vlasenko | d56e3cc | 2008-02-09 11:37:21 +0000 | [diff] [blame] | 58 | int delay = -1; |
| 59 | int timeout = -1; |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 60 | |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 61 | // fetch options |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 62 | char *opt_s; |
Denis Vlasenko | 68e8e96 | 2008-02-09 11:39:00 +0000 | [diff] [blame] | 63 | char *opt_d; |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 64 | char *opt_t; |
| 65 | unsigned opts; |
Denis Vlasenko | 9304d6e | 2008-02-10 19:05:56 +0000 | [diff] [blame] | 66 | opt_complementary = "=1"; // exactly one arg should be there |
Denis Vlasenko | d56e3cc | 2008-02-09 11:37:21 +0000 | [diff] [blame] | 67 | opts = getopt32(argv, "Xs:d:t:", &opt_s, &opt_d, &opt_t); |
| 68 | |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 69 | // apply options |
| 70 | if (opts & OPT_s) |
| 71 | speed = xatoi_u(opt_s); |
Denis Vlasenko | d56e3cc | 2008-02-09 11:37:21 +0000 | [diff] [blame] | 72 | if (opts & OPT_d) |
| 73 | delay = xatoi_u(opt_d); |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 74 | if (opts & OPT_t) |
| 75 | timeout = xatoi_u(opt_t); |
Denis Vlasenko | d56e3cc | 2008-02-09 11:37:21 +0000 | [diff] [blame] | 76 | |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 77 | // argc -= optind; |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 78 | argv += optind; |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 79 | |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 80 | // try to create lock file in /var/lock |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 81 | device_lock_file = (char *)bb_basename(argv[0]); |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 82 | device_lock_file = xasprintf("/var/lock/LCK..%s", device_lock_file); |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 83 | sfd = open(device_lock_file, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0644); |
| 84 | if (sfd < 0) { |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 85 | if (errno == EEXIST) |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 86 | bb_perror_msg_and_die("can't create %s", device_lock_file); |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 87 | if (ENABLE_FEATURE_CLEAN_UP) |
| 88 | free(device_lock_file); |
| 89 | device_lock_file = NULL; |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 90 | } |
| 91 | if (sfd > 0) { |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 92 | // %4d to make mgetty happy. It treats 4-bytes lock files as binary, |
| 93 | // not text, PID. Making 5+ char file. Brrr... |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 94 | char *s = xasprintf("%4d\n", getpid()); |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 95 | write(sfd, s, strlen(s)); |
| 96 | if (ENABLE_FEATURE_CLEAN_UP) |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 97 | free(s); |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 98 | close(sfd); |
| 99 | } |
| 100 | |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 101 | // setup signals |
| 102 | sig_catch(SIGHUP, signal_handler); |
| 103 | sig_catch(SIGINT, signal_handler); |
| 104 | sig_catch(SIGTERM, signal_handler); |
| 105 | sig_catch(SIGPIPE, signal_handler); |
| 106 | |
| 107 | // error exit code if we fail to open the device |
| 108 | signalled = 1; |
| 109 | |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 110 | // open device |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 111 | sfd = open_or_warn(argv[0], O_RDWR | O_NOCTTY | O_NONBLOCK); |
| 112 | if (sfd < 0) |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 113 | goto done; |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 114 | fcntl(sfd, F_SETFL, O_RDWR | O_NOCTTY); |
| 115 | |
Denis Vlasenko | d56e3cc | 2008-02-09 11:37:21 +0000 | [diff] [blame] | 116 | // put stdin to "raw mode" (if stdin is a TTY), |
| 117 | // handle one character at a time |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 118 | if (isatty(STDIN_FILENO)) { |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 119 | xget1(STDIN_FILENO, &tio, &tio0); |
| 120 | if (xset1(STDIN_FILENO, &tio, "stdin")) |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 121 | goto done; |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 122 | } |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 123 | |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 124 | // same thing for modem |
| 125 | xget1(sfd, &tio, &tiosfd); |
| 126 | // order device to hang up at exit |
| 127 | tio.c_cflag |= (CREAD|HUPCL); |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 128 | // if (!istty) |
| 129 | // tio.c_iflag |= (IGNCR); |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 130 | // set device speed |
| 131 | cfsetspeed(&tio, tty_value_to_baud(speed)); |
| 132 | if (xset1(sfd, &tio, argv[0])) |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 133 | goto restore0_and_done; |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 134 | |
| 135 | // main loop: check with poll(), then read/write bytes across |
Denis Vlasenko | 0effc24 | 2008-01-28 09:39:30 +0000 | [diff] [blame] | 136 | pfd[0].fd = sfd; |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 137 | pfd[0].events = POLLIN; |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 138 | pfd[1].fd = STDIN_FILENO; |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 139 | pfd[1].events = POLLIN; |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 140 | |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 141 | signalled = 0; |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 142 | nfd = 2; |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 143 | while (!signalled && safe_poll(pfd, nfd, timeout) > 0) { |
Denis Vlasenko | d56e3cc | 2008-02-09 11:37:21 +0000 | [diff] [blame] | 144 | if (pfd[1].revents) { |
Denis Vlasenko | 9304d6e | 2008-02-10 19:05:56 +0000 | [diff] [blame] | 145 | char c; |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 146 | // read from stdin -> write to device |
| 147 | if (safe_read(STDIN_FILENO, &c, 1) < 1) { |
Denis Vlasenko | d56e3cc | 2008-02-09 11:37:21 +0000 | [diff] [blame] | 148 | // don't poll stdin anymore if we got EOF/error |
Denis Vlasenko | 9304d6e | 2008-02-10 19:05:56 +0000 | [diff] [blame] | 149 | pfd[1].revents = 0; |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 150 | nfd--; |
Denis Vlasenko | 9304d6e | 2008-02-10 19:05:56 +0000 | [diff] [blame] | 151 | goto check_stdin; |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 152 | } |
| 153 | // do we need special processing? |
| 154 | if (!(opts & OPT_X)) { |
| 155 | // ^@ sends Break |
| 156 | if (VINTR == c) { |
| 157 | tcsendbreak(sfd, 0); |
Denis Vlasenko | 9304d6e | 2008-02-10 19:05:56 +0000 | [diff] [blame] | 158 | goto check_stdin; |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 159 | } |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 160 | // ^X exits |
| 161 | if (24 == c) |
| 162 | break; |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 163 | } |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 164 | write(sfd, &c, 1); |
Denis Vlasenko | 9304d6e | 2008-02-10 19:05:56 +0000 | [diff] [blame] | 165 | if (delay >= 0) |
| 166 | safe_poll(pfd, 1, delay); |
| 167 | } |
| 168 | check_stdin: |
| 169 | if (pfd[0].revents) { |
| 170 | ssize_t len; |
| 171 | // read from device -> write to stdout |
| 172 | len = safe_read(sfd, bb_common_bufsiz1, sizeof(bb_common_bufsiz1)); |
| 173 | if (len > 0) |
| 174 | full_write(STDOUT_FILENO, bb_common_bufsiz1, len); |
| 175 | // else { EOF/error - what to do? } |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 178 | |
| 179 | tcsetattr(sfd, TCSAFLUSH, &tiosfd); |
| 180 | |
| 181 | restore0_and_done: |
Denis Vlasenko | d56e3cc | 2008-02-09 11:37:21 +0000 | [diff] [blame] | 182 | if (isatty(STDIN_FILENO)) |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 183 | tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio0); |
| 184 | |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 185 | done: |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 186 | if (device_lock_file) |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 187 | unlink(device_lock_file); |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 188 | |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 189 | return signalled; |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 190 | } |