blob: 3c573eaa9f50e3e39af0ea886e87a8b662090d7e [file] [log] [blame]
Bernhard Reutner-Fischerf588f702007-09-22 20:35:32 +00001/* vi: set sw=4 ts=4: */
2/*
Denis Vlasenko70766952007-09-21 17:42:40 +00003 * bare bones 'talk to modem' program - similar to 'cu -l $device'
4 * inspired by mgetty's microcom
5 *
Denis Vlasenkoffae8452008-02-16 23:40:47 +00006 * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
Denis Vlasenkof34e8212007-09-22 20:23:57 +00007 *
8 * Licensed under GPLv2, see file LICENSE in this tarball for details.
Denis Vlasenko70766952007-09-21 17:42:40 +00009 */
Denis Vlasenko9a4e08e2007-09-22 21:37:27 +000010#include "libbb.h"
Denis Vlasenko70766952007-09-21 17:42:40 +000011
Denis Vlasenkoef67c572008-01-27 22:40:39 +000012/* All known arches use small ints for signals */
13static volatile smallint signalled;
14
15static void signal_handler(int signo)
16{
17 signalled = signo;
18}
19
Denis Vlasenkodc1c4572008-02-08 17:30:39 +000020// set raw tty mode
Denis Vlasenkoef67c572008-01-27 22:40:39 +000021static void xget1(int fd, struct termios *t, struct termios *oldt)
Denis Vlasenko56244732008-02-17 15:14:04 +000022{
Denis Vlasenkoef67c572008-01-27 22:40:39 +000023 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
33static int xset1(int fd, struct termios *tio, const char *device)
34{
Denis Vlasenkodc1c4572008-02-08 17:30:39 +000035 int ret = tcsetattr(fd, TCSAFLUSH, tio);
Denis Vlasenkoef67c572008-01-27 22:40:39 +000036
37 if (ret) {
38 bb_perror_msg("can't tcsetattr for %s", device);
39 }
40 return ret;
41}
42
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000043int microcom_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko70766952007-09-21 17:42:40 +000044int microcom_main(int argc, char **argv)
45{
Denis Vlasenko0effc242008-01-28 09:39:30 +000046 int sfd;
Denis Vlasenkodc1c4572008-02-08 17:30:39 +000047 int nfd;
48 struct pollfd pfd[2];
Denis Vlasenko70766952007-09-21 17:42:40 +000049 struct termios tio0, tiosfd, tio;
Denis Vlasenkodc1c4572008-02-08 17:30:39 +000050 char *device_lock_file;
51 enum {
52 OPT_X = 1 << 0, // do not respect Ctrl-X, Ctrl-@
53 OPT_s = 1 << 1, // baudrate
Denis Vlasenko624188a2008-02-19 18:26:40 +000054 OPT_d = 1 << 2, // wait for device response, ms
Denis Vlasenko68e8e962008-02-09 11:39:00 +000055 OPT_t = 1 << 3, // timeout, ms
Denis Vlasenkodc1c4572008-02-08 17:30:39 +000056 };
57 speed_t speed = 9600;
Denis Vlasenkod56e3cc2008-02-09 11:37:21 +000058 int delay = -1;
59 int timeout = -1;
Denis Vlasenko624188a2008-02-19 18:26:40 +000060 unsigned opts;
Denis Vlasenko70766952007-09-21 17:42:40 +000061
Denis Vlasenkoef67c572008-01-27 22:40:39 +000062 // fetch options
Denis Vlasenko624188a2008-02-19 18:26:40 +000063 opt_complementary = "=1:s+:d+:t+"; // exactly one arg, numeric options
Denis Vlasenkoffae8452008-02-16 23:40:47 +000064 opts = getopt32(argv, "Xs:d:t:", &speed, &delay, &timeout);
Denis Vlasenkodc1c4572008-02-08 17:30:39 +000065// argc -= optind;
Denis Vlasenko70766952007-09-21 17:42:40 +000066 argv += optind;
Denis Vlasenkoef67c572008-01-27 22:40:39 +000067
Denis Vlasenko70766952007-09-21 17:42:40 +000068 // try to create lock file in /var/lock
Denis Vlasenkodc1c4572008-02-08 17:30:39 +000069 device_lock_file = (char *)bb_basename(argv[0]);
Denis Vlasenkoef67c572008-01-27 22:40:39 +000070 device_lock_file = xasprintf("/var/lock/LCK..%s", device_lock_file);
Denis Vlasenko70766952007-09-21 17:42:40 +000071 sfd = open(device_lock_file, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0644);
72 if (sfd < 0) {
Denis Vlasenko624188a2008-02-19 18:26:40 +000073 // device already locked -> bail out
Denis Vlasenko70766952007-09-21 17:42:40 +000074 if (errno == EEXIST)
Denis Vlasenkodc1c4572008-02-08 17:30:39 +000075 bb_perror_msg_and_die("can't create %s", device_lock_file);
Denis Vlasenko624188a2008-02-19 18:26:40 +000076 // can't create lock -> don't care
Denis Vlasenkoef67c572008-01-27 22:40:39 +000077 if (ENABLE_FEATURE_CLEAN_UP)
78 free(device_lock_file);
79 device_lock_file = NULL;
Denis Vlasenko624188a2008-02-19 18:26:40 +000080 } else {
81 // %4d to make concurrent mgetty (if any) happy.
82 // Mgetty treats 4-bytes lock files as binary,
Denis Vlasenko70766952007-09-21 17:42:40 +000083 // not text, PID. Making 5+ char file. Brrr...
Denis Vlasenkoef67c572008-01-27 22:40:39 +000084 char *s = xasprintf("%4d\n", getpid());
Denis Vlasenko70766952007-09-21 17:42:40 +000085 write(sfd, s, strlen(s));
86 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenkoef67c572008-01-27 22:40:39 +000087 free(s);
Denis Vlasenko70766952007-09-21 17:42:40 +000088 close(sfd);
89 }
90
Denis Vlasenkoef67c572008-01-27 22:40:39 +000091 // setup signals
Denis Vlasenkoffae8452008-02-16 23:40:47 +000092 bb_signals(0
93 + (1 << SIGHUP)
94 + (1 << SIGINT)
95 + (1 << SIGTERM)
96 + (1 << SIGPIPE)
97 , signal_handler);
Denis Vlasenkoef67c572008-01-27 22:40:39 +000098
99 // error exit code if we fail to open the device
100 signalled = 1;
101
Denis Vlasenkod56e3cc2008-02-09 11:37:21 +0000102 // put stdin to "raw mode" (if stdin is a TTY),
103 // handle one character at a time
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000104 if (isatty(STDIN_FILENO)) {
Denis Vlasenkoef67c572008-01-27 22:40:39 +0000105 xget1(STDIN_FILENO, &tio, &tio0);
106 if (xset1(STDIN_FILENO, &tio, "stdin"))
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000107 goto done;
Denis Vlasenko70766952007-09-21 17:42:40 +0000108 }
Denis Vlasenko70766952007-09-21 17:42:40 +0000109
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000110 // open device
111 sfd = open_or_warn(argv[0], O_RDWR | O_NOCTTY | O_NONBLOCK);
112 if (sfd < 0)
113 goto done;
114 fcntl(sfd, F_SETFL, 0);
115
116 // put device to "raw mode"
Denis Vlasenkoef67c572008-01-27 22:40:39 +0000117 xget1(sfd, &tio, &tiosfd);
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000118// tio.c_cflag |= (CREAD|HUPCL); // we just bail out on any device error
Denis Vlasenkoef67c572008-01-27 22:40:39 +0000119 // set device speed
120 cfsetspeed(&tio, tty_value_to_baud(speed));
121 if (xset1(sfd, &tio, argv[0]))
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000122 goto restore0_and_done;
Denis Vlasenko70766952007-09-21 17:42:40 +0000123
124 // main loop: check with poll(), then read/write bytes across
Denis Vlasenko0effc242008-01-28 09:39:30 +0000125 pfd[0].fd = sfd;
Denis Vlasenko70766952007-09-21 17:42:40 +0000126 pfd[0].events = POLLIN;
Denis Vlasenkoef67c572008-01-27 22:40:39 +0000127 pfd[1].fd = STDIN_FILENO;
Denis Vlasenko70766952007-09-21 17:42:40 +0000128 pfd[1].events = POLLIN;
Denis Vlasenkoef67c572008-01-27 22:40:39 +0000129
Denis Vlasenkoef67c572008-01-27 22:40:39 +0000130 signalled = 0;
Denis Vlasenkoef67c572008-01-27 22:40:39 +0000131 nfd = 2;
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000132 while (!signalled && safe_poll(pfd, nfd, timeout) > 0) {
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000133 if (nfd > 1 && pfd[1].revents) {
Denis Vlasenko9304d6e2008-02-10 19:05:56 +0000134 char c;
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000135 // read from stdin -> write to device
136 if (safe_read(STDIN_FILENO, &c, 1) < 1) {
Denis Vlasenkod56e3cc2008-02-09 11:37:21 +0000137 // don't poll stdin anymore if we got EOF/error
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000138 nfd--;
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000139 goto skip_write;
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000140 }
141 // do we need special processing?
142 if (!(opts & OPT_X)) {
143 // ^@ sends Break
144 if (VINTR == c) {
145 tcsendbreak(sfd, 0);
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000146 goto skip_write;
Denis Vlasenko70766952007-09-21 17:42:40 +0000147 }
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000148 // ^X exits
149 if (24 == c)
150 break;
Denis Vlasenko70766952007-09-21 17:42:40 +0000151 }
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000152 write(sfd, &c, 1);
Denis Vlasenko9304d6e2008-02-10 19:05:56 +0000153 if (delay >= 0)
154 safe_poll(pfd, 1, delay);
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000155skip_write: ;
Denis Vlasenko9304d6e2008-02-10 19:05:56 +0000156 }
Denis Vlasenko9304d6e2008-02-10 19:05:56 +0000157 if (pfd[0].revents) {
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000158#define iobuf bb_common_bufsiz1
Denis Vlasenko9304d6e2008-02-10 19:05:56 +0000159 ssize_t len;
160 // read from device -> write to stdout
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000161 len = safe_read(sfd, iobuf, sizeof(iobuf));
Denis Vlasenko9304d6e2008-02-10 19:05:56 +0000162 if (len > 0)
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000163 full_write(STDOUT_FILENO, iobuf, len);
164 else {
165 // EOF/error -> bail out
166 signalled = SIGHUP;
167 break;
168 }
Denis Vlasenko70766952007-09-21 17:42:40 +0000169 }
170 }
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000171
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000172 // restore device mode
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000173 tcsetattr(sfd, TCSAFLUSH, &tiosfd);
174
175restore0_and_done:
Denis Vlasenkod56e3cc2008-02-09 11:37:21 +0000176 if (isatty(STDIN_FILENO))
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000177 tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio0);
178
Denis Vlasenkoef67c572008-01-27 22:40:39 +0000179done:
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000180 if (device_lock_file)
Denis Vlasenko70766952007-09-21 17:42:40 +0000181 unlink(device_lock_file);
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000182
Denis Vlasenkoef67c572008-01-27 22:40:39 +0000183 return signalled;
Denis Vlasenko70766952007-09-21 17:42:40 +0000184}