blob: a322197b83ad411e088ba4efa759dfda66e35f1e [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 Vlasenkodc1c4572008-02-08 17:30:39 +000012// set raw tty mode
Denis Vlasenkoef67c572008-01-27 22:40:39 +000013static void xget1(int fd, struct termios *t, struct termios *oldt)
Denis Vlasenko56244732008-02-17 15:14:04 +000014{
Denis Vlasenkoef67c572008-01-27 22:40:39 +000015 tcgetattr(fd, oldt);
16 *t = *oldt;
17 cfmakeraw(t);
18// t->c_lflag &= ~(ISIG|ICANON|ECHO|IEXTEN);
19// t->c_iflag &= ~(BRKINT|IXON|ICRNL);
20// t->c_oflag &= ~(ONLCR);
21// t->c_cc[VMIN] = 1;
22// t->c_cc[VTIME] = 0;
23}
24
25static int xset1(int fd, struct termios *tio, const char *device)
26{
Denis Vlasenkodc1c4572008-02-08 17:30:39 +000027 int ret = tcsetattr(fd, TCSAFLUSH, tio);
Denis Vlasenkoef67c572008-01-27 22:40:39 +000028
29 if (ret) {
30 bb_perror_msg("can't tcsetattr for %s", device);
31 }
32 return ret;
33}
34
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000035int microcom_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000036int microcom_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko70766952007-09-21 17:42:40 +000037{
Denis Vlasenko0effc242008-01-28 09:39:30 +000038 int sfd;
Denis Vlasenkodc1c4572008-02-08 17:30:39 +000039 int nfd;
40 struct pollfd pfd[2];
Denis Vlasenko70766952007-09-21 17:42:40 +000041 struct termios tio0, tiosfd, tio;
Denis Vlasenkodc1c4572008-02-08 17:30:39 +000042 char *device_lock_file;
43 enum {
44 OPT_X = 1 << 0, // do not respect Ctrl-X, Ctrl-@
45 OPT_s = 1 << 1, // baudrate
Denis Vlasenko624188a2008-02-19 18:26:40 +000046 OPT_d = 1 << 2, // wait for device response, ms
Denis Vlasenko68e8e962008-02-09 11:39:00 +000047 OPT_t = 1 << 3, // timeout, ms
Denis Vlasenkodc1c4572008-02-08 17:30:39 +000048 };
49 speed_t speed = 9600;
Denis Vlasenkod56e3cc2008-02-09 11:37:21 +000050 int delay = -1;
51 int timeout = -1;
Denis Vlasenko624188a2008-02-19 18:26:40 +000052 unsigned opts;
Denis Vlasenko70766952007-09-21 17:42:40 +000053
Denis Vlasenkoef67c572008-01-27 22:40:39 +000054 // fetch options
Denis Vlasenko624188a2008-02-19 18:26:40 +000055 opt_complementary = "=1:s+:d+:t+"; // exactly one arg, numeric options
Denis Vlasenkoffae8452008-02-16 23:40:47 +000056 opts = getopt32(argv, "Xs:d:t:", &speed, &delay, &timeout);
Denis Vlasenkodc1c4572008-02-08 17:30:39 +000057// argc -= optind;
Denis Vlasenko70766952007-09-21 17:42:40 +000058 argv += optind;
Denis Vlasenkoef67c572008-01-27 22:40:39 +000059
Denis Vlasenko70766952007-09-21 17:42:40 +000060 // try to create lock file in /var/lock
Denis Vlasenkodc1c4572008-02-08 17:30:39 +000061 device_lock_file = (char *)bb_basename(argv[0]);
Denis Vlasenkoef67c572008-01-27 22:40:39 +000062 device_lock_file = xasprintf("/var/lock/LCK..%s", device_lock_file);
Denis Vlasenko70766952007-09-21 17:42:40 +000063 sfd = open(device_lock_file, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0644);
64 if (sfd < 0) {
Denis Vlasenko624188a2008-02-19 18:26:40 +000065 // device already locked -> bail out
Denis Vlasenko70766952007-09-21 17:42:40 +000066 if (errno == EEXIST)
Denis Vlasenkodc1c4572008-02-08 17:30:39 +000067 bb_perror_msg_and_die("can't create %s", device_lock_file);
Denis Vlasenko624188a2008-02-19 18:26:40 +000068 // can't create lock -> don't care
Denis Vlasenkoef67c572008-01-27 22:40:39 +000069 if (ENABLE_FEATURE_CLEAN_UP)
70 free(device_lock_file);
71 device_lock_file = NULL;
Denis Vlasenko624188a2008-02-19 18:26:40 +000072 } else {
73 // %4d to make concurrent mgetty (if any) happy.
74 // Mgetty treats 4-bytes lock files as binary,
Denis Vlasenko70766952007-09-21 17:42:40 +000075 // not text, PID. Making 5+ char file. Brrr...
Denis Vlasenkocd174d12008-02-28 13:14:42 +000076 fdprintf(sfd, "%4d\n", getpid());
Denis Vlasenko70766952007-09-21 17:42:40 +000077 close(sfd);
78 }
79
Denis Vlasenkoef67c572008-01-27 22:40:39 +000080 // setup signals
Denis Vlasenkoffae8452008-02-16 23:40:47 +000081 bb_signals(0
82 + (1 << SIGHUP)
83 + (1 << SIGINT)
84 + (1 << SIGTERM)
85 + (1 << SIGPIPE)
Denis Vlasenko08ea11a2008-09-11 19:51:11 +000086 , record_signo);
Denis Vlasenkoef67c572008-01-27 22:40:39 +000087
88 // error exit code if we fail to open the device
Denis Vlasenko08ea11a2008-09-11 19:51:11 +000089 bb_got_signal = 1;
Denis Vlasenkoef67c572008-01-27 22:40:39 +000090
Denis Vlasenkoffae8452008-02-16 23:40:47 +000091 // open device
92 sfd = open_or_warn(argv[0], O_RDWR | O_NOCTTY | O_NONBLOCK);
93 if (sfd < 0)
94 goto done;
95 fcntl(sfd, F_SETFL, 0);
96
97 // put device to "raw mode"
Denis Vlasenkoef67c572008-01-27 22:40:39 +000098 xget1(sfd, &tio, &tiosfd);
Denis Vlasenkoef67c572008-01-27 22:40:39 +000099 // set device speed
100 cfsetspeed(&tio, tty_value_to_baud(speed));
101 if (xset1(sfd, &tio, argv[0]))
Paul Foxa50a9b02008-04-23 15:31:29 +0000102 goto done;
103
104 // put stdin to "raw mode" (if stdin is a TTY),
105 // handle one character at a time
106 if (isatty(STDIN_FILENO)) {
107 xget1(STDIN_FILENO, &tio, &tio0);
108 if (xset1(STDIN_FILENO, &tio, "stdin"))
109 goto done;
110 }
Denis Vlasenko70766952007-09-21 17:42:40 +0000111
112 // main loop: check with poll(), then read/write bytes across
Denis Vlasenko0effc242008-01-28 09:39:30 +0000113 pfd[0].fd = sfd;
Denis Vlasenko70766952007-09-21 17:42:40 +0000114 pfd[0].events = POLLIN;
Denis Vlasenkoef67c572008-01-27 22:40:39 +0000115 pfd[1].fd = STDIN_FILENO;
Denis Vlasenko70766952007-09-21 17:42:40 +0000116 pfd[1].events = POLLIN;
Denis Vlasenkoef67c572008-01-27 22:40:39 +0000117
Denis Vlasenko08ea11a2008-09-11 19:51:11 +0000118 bb_got_signal = 0;
Denis Vlasenkoef67c572008-01-27 22:40:39 +0000119 nfd = 2;
Denis Vlasenko08ea11a2008-09-11 19:51:11 +0000120 while (!bb_got_signal && safe_poll(pfd, nfd, timeout) > 0) {
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000121 if (nfd > 1 && pfd[1].revents) {
Denis Vlasenko9304d6e2008-02-10 19:05:56 +0000122 char c;
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000123 // read from stdin -> write to device
124 if (safe_read(STDIN_FILENO, &c, 1) < 1) {
Denis Vlasenkod56e3cc2008-02-09 11:37:21 +0000125 // don't poll stdin anymore if we got EOF/error
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000126 nfd--;
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000127 goto skip_write;
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000128 }
129 // do we need special processing?
130 if (!(opts & OPT_X)) {
131 // ^@ sends Break
132 if (VINTR == c) {
133 tcsendbreak(sfd, 0);
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000134 goto skip_write;
Denis Vlasenko70766952007-09-21 17:42:40 +0000135 }
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000136 // ^X exits
137 if (24 == c)
138 break;
Denis Vlasenko70766952007-09-21 17:42:40 +0000139 }
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000140 write(sfd, &c, 1);
Denis Vlasenko9304d6e2008-02-10 19:05:56 +0000141 if (delay >= 0)
142 safe_poll(pfd, 1, delay);
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000143skip_write: ;
Denis Vlasenko9304d6e2008-02-10 19:05:56 +0000144 }
Denis Vlasenko9304d6e2008-02-10 19:05:56 +0000145 if (pfd[0].revents) {
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000146#define iobuf bb_common_bufsiz1
Denis Vlasenko9304d6e2008-02-10 19:05:56 +0000147 ssize_t len;
148 // read from device -> write to stdout
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000149 len = safe_read(sfd, iobuf, sizeof(iobuf));
Denis Vlasenko9304d6e2008-02-10 19:05:56 +0000150 if (len > 0)
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000151 full_write(STDOUT_FILENO, iobuf, len);
152 else {
153 // EOF/error -> bail out
Denis Vlasenko08ea11a2008-09-11 19:51:11 +0000154 bb_got_signal = SIGHUP;
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000155 break;
156 }
Denis Vlasenko70766952007-09-21 17:42:40 +0000157 }
158 }
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000159
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000160 // restore device mode
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000161 tcsetattr(sfd, TCSAFLUSH, &tiosfd);
162
Denis Vlasenkod56e3cc2008-02-09 11:37:21 +0000163 if (isatty(STDIN_FILENO))
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000164 tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio0);
165
Denis Vlasenkoef67c572008-01-27 22:40:39 +0000166done:
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000167 if (device_lock_file)
Denis Vlasenko70766952007-09-21 17:42:40 +0000168 unlink(device_lock_file);
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000169
Denis Vlasenko08ea11a2008-09-11 19:51:11 +0000170 return bb_got_signal;
Denis Vlasenko70766952007-09-21 17:42:40 +0000171}