1 /* vi: set sw=4 ts=4: */
3 * bare bones 'talk to modem' program - similar to 'cu -l $device'
4 * inspired by mgetty's microcom
6 * Copyright (C) 2007 by Vladimir Dronnikov <dronnikov@gmail.ru>
8 * Licensed under GPLv2, see file LICENSE in this tarball for details.
12 /* All known arches use small ints for signals */
13 static volatile smallint signalled;
15 static void signal_handler(int signo)
21 static void xget1(int fd, struct termios *t, struct termios *oldt)
26 // t->c_lflag &= ~(ISIG|ICANON|ECHO|IEXTEN);
27 // t->c_iflag &= ~(BRKINT|IXON|ICRNL);
28 // t->c_oflag &= ~(ONLCR);
30 // t->c_cc[VTIME] = 0;
33 static int xset1(int fd, struct termios *tio, const char *device)
35 int ret = tcsetattr(fd, TCSAFLUSH, tio);
38 bb_perror_msg("can't tcsetattr for %s", device);
43 int microcom_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
44 int microcom_main(int argc, char **argv)
49 struct termios tio0, tiosfd, tio;
50 char *device_lock_file;
52 OPT_X = 1 << 0, // do not respect Ctrl-X, Ctrl-@
53 OPT_s = 1 << 1, // baudrate
54 OPT_d = 1 << 2 // wait for device response, msecs
55 OPT_t = 1 << 3 // timeout, ms
65 opt_complementary = "=1"; /* exactly one arg should be there */
66 opts = getopt32(argv, "Xs:d:t:", &opt_s, &opt_d, &opt_t);
70 speed = xatoi_u(opt_s);
72 delay = xatoi_u(opt_d);
74 timeout = xatoi_u(opt_t);
79 // try to create lock file in /var/lock
80 device_lock_file = (char *)bb_basename(argv[0]);
81 device_lock_file = xasprintf("/var/lock/LCK..%s", device_lock_file);
82 sfd = open(device_lock_file, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0644);
85 bb_perror_msg_and_die("can't create %s", device_lock_file);
86 if (ENABLE_FEATURE_CLEAN_UP)
87 free(device_lock_file);
88 device_lock_file = NULL;
91 // %4d to make mgetty happy. It treats 4-bytes lock files as binary,
92 // not text, PID. Making 5+ char file. Brrr...
93 char *s = xasprintf("%4d\n", getpid());
94 write(sfd, s, strlen(s));
95 if (ENABLE_FEATURE_CLEAN_UP)
101 sig_catch(SIGHUP, signal_handler);
102 sig_catch(SIGINT, signal_handler);
103 sig_catch(SIGTERM, signal_handler);
104 sig_catch(SIGPIPE, signal_handler);
106 // error exit code if we fail to open the device
110 sfd = open_or_warn(argv[0], O_RDWR | O_NOCTTY | O_NONBLOCK);
113 fcntl(sfd, F_SETFL, O_RDWR | O_NOCTTY);
115 // put stdin to "raw mode" (if stdin is a TTY),
116 // handle one character at a time
117 if (isatty(STDIN_FILENO)) {
118 xget1(STDIN_FILENO, &tio, &tio0);
119 if (xset1(STDIN_FILENO, &tio, "stdin"))
123 // same thing for modem
124 xget1(sfd, &tio, &tiosfd);
125 // order device to hang up at exit
126 tio.c_cflag |= (CREAD|HUPCL);
128 // tio.c_iflag |= (IGNCR);
130 cfsetspeed(&tio, tty_value_to_baud(speed));
131 if (xset1(sfd, &tio, argv[0]))
132 goto restore0_and_done;
134 // main loop: check with poll(), then read/write bytes across
136 pfd[0].events = POLLIN;
137 pfd[1].fd = STDIN_FILENO;
138 pfd[1].events = POLLIN;
142 while (!signalled && safe_poll(pfd, nfd, timeout) > 0) {
144 if (pfd[0].revents) {
146 // read from device -> write to stdout
147 if (safe_read(sfd, &c, 1) > 0)
148 write(STDOUT_FILENO, &c, 1);
149 // else { EOF/error - what to do? }
151 if (pfd[1].revents) {
153 // read from stdin -> write to device
154 if (safe_read(STDIN_FILENO, &c, 1) < 1) {
155 // don't poll stdin anymore if we got EOF/error
159 // do we need special processing?
160 if (!(opts & OPT_X)) {
171 if (delay >= 0 && safe_poll(pfd, 1, delay) > 0)
176 tcsetattr(sfd, TCSAFLUSH, &tiosfd);
179 if (isatty(STDIN_FILENO))
180 tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio0);
183 if (device_lock_file)
184 unlink(device_lock_file);