blob: 04605d883a83e9802e65890290422c635d1cd9d8 [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenko70766952007-09-21 17:42:40 +00009 */
Denys Vlasenkofb4da162016-11-22 23:14:24 +010010//config:config MICROCOM
11//config: bool "microcom"
12//config: default y
13//config: help
14//config: The poor man's minicom utility for chatting with serial port devices.
Pere Orga5bc8c002011-04-11 03:29:49 +020015
Denys Vlasenkof88e3bf2016-11-22 23:54:17 +010016//applet:IF_MICROCOM(APPLET(microcom, BB_DIR_USR_BIN, BB_SUID_DROP))
17
18//kbuild:lib-$(CONFIG_MICROCOM) += microcom.o
19
Pere Orga5bc8c002011-04-11 03:29:49 +020020//usage:#define microcom_trivial_usage
21//usage: "[-d DELAY] [-t TIMEOUT] [-s SPEED] [-X] TTY"
22//usage:#define microcom_full_usage "\n\n"
23//usage: "Copy bytes for stdin to TTY and from TTY to stdout\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020024//usage: "\n -d Wait up to DELAY ms for TTY output before sending every"
25//usage: "\n next byte to it"
26//usage: "\n -t Exit if both stdin and TTY are silent for TIMEOUT ms"
27//usage: "\n -s Set serial line to SPEED"
28//usage: "\n -X Disable special meaning of NUL and Ctrl-X from stdin"
29
Denis Vlasenko9a4e08e2007-09-22 21:37:27 +000030#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020031#include "common_bufsiz.h"
Denis Vlasenko70766952007-09-21 17:42:40 +000032
Denis Vlasenkodc1c4572008-02-08 17:30:39 +000033// set raw tty mode
Denis Vlasenkoef67c572008-01-27 22:40:39 +000034static void xget1(int fd, struct termios *t, struct termios *oldt)
Denis Vlasenko56244732008-02-17 15:14:04 +000035{
Denis Vlasenkoef67c572008-01-27 22:40:39 +000036 tcgetattr(fd, oldt);
37 *t = *oldt;
38 cfmakeraw(t);
39// t->c_lflag &= ~(ISIG|ICANON|ECHO|IEXTEN);
40// t->c_iflag &= ~(BRKINT|IXON|ICRNL);
41// t->c_oflag &= ~(ONLCR);
42// t->c_cc[VMIN] = 1;
43// t->c_cc[VTIME] = 0;
44}
45
46static int xset1(int fd, struct termios *tio, const char *device)
47{
Denis Vlasenkodc1c4572008-02-08 17:30:39 +000048 int ret = tcsetattr(fd, TCSAFLUSH, tio);
Denis Vlasenkoef67c572008-01-27 22:40:39 +000049
50 if (ret) {
51 bb_perror_msg("can't tcsetattr for %s", device);
52 }
53 return ret;
54}
55
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000056int microcom_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000057int microcom_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko70766952007-09-21 17:42:40 +000058{
Denis Vlasenko0effc242008-01-28 09:39:30 +000059 int sfd;
Denis Vlasenkodc1c4572008-02-08 17:30:39 +000060 int nfd;
61 struct pollfd pfd[2];
Denis Vlasenko70766952007-09-21 17:42:40 +000062 struct termios tio0, tiosfd, tio;
Denis Vlasenkodc1c4572008-02-08 17:30:39 +000063 char *device_lock_file;
64 enum {
65 OPT_X = 1 << 0, // do not respect Ctrl-X, Ctrl-@
66 OPT_s = 1 << 1, // baudrate
Denis Vlasenko624188a2008-02-19 18:26:40 +000067 OPT_d = 1 << 2, // wait for device response, ms
Denis Vlasenko68e8e962008-02-09 11:39:00 +000068 OPT_t = 1 << 3, // timeout, ms
Denis Vlasenkodc1c4572008-02-08 17:30:39 +000069 };
70 speed_t speed = 9600;
Denis Vlasenkod56e3cc2008-02-09 11:37:21 +000071 int delay = -1;
72 int timeout = -1;
Denis Vlasenko624188a2008-02-19 18:26:40 +000073 unsigned opts;
Denis Vlasenko70766952007-09-21 17:42:40 +000074
Denis Vlasenkoef67c572008-01-27 22:40:39 +000075 // fetch options
Denys Vlasenko237bedd2016-07-06 21:58:02 +020076 opts = getopt32(argv, "Xs:+d:+t:+", &speed, &delay, &timeout);
Denis Vlasenkodc1c4572008-02-08 17:30:39 +000077// argc -= optind;
Denis Vlasenko70766952007-09-21 17:42:40 +000078 argv += optind;
Denis Vlasenkoef67c572008-01-27 22:40:39 +000079
Denis Vlasenko70766952007-09-21 17:42:40 +000080 // try to create lock file in /var/lock
Denis Vlasenkodc1c4572008-02-08 17:30:39 +000081 device_lock_file = (char *)bb_basename(argv[0]);
Denis Vlasenkoef67c572008-01-27 22:40:39 +000082 device_lock_file = xasprintf("/var/lock/LCK..%s", device_lock_file);
Denis Vlasenko70766952007-09-21 17:42:40 +000083 sfd = open(device_lock_file, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0644);
84 if (sfd < 0) {
Denis Vlasenko624188a2008-02-19 18:26:40 +000085 // device already locked -> bail out
Denis Vlasenko70766952007-09-21 17:42:40 +000086 if (errno == EEXIST)
Denys Vlasenko651a2692010-03-23 16:25:17 +010087 bb_perror_msg_and_die("can't create '%s'", device_lock_file);
Denis Vlasenko624188a2008-02-19 18:26:40 +000088 // can't create lock -> don't care
Denis Vlasenkoef67c572008-01-27 22:40:39 +000089 if (ENABLE_FEATURE_CLEAN_UP)
90 free(device_lock_file);
91 device_lock_file = NULL;
Denis Vlasenko624188a2008-02-19 18:26:40 +000092 } else {
93 // %4d to make concurrent mgetty (if any) happy.
94 // Mgetty treats 4-bytes lock files as binary,
Denis Vlasenko70766952007-09-21 17:42:40 +000095 // not text, PID. Making 5+ char file. Brrr...
Denis Vlasenkocd174d12008-02-28 13:14:42 +000096 fdprintf(sfd, "%4d\n", getpid());
Denis Vlasenko70766952007-09-21 17:42:40 +000097 close(sfd);
98 }
99
Denis Vlasenkoef67c572008-01-27 22:40:39 +0000100 // setup signals
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000101 bb_signals(0
102 + (1 << SIGHUP)
103 + (1 << SIGINT)
104 + (1 << SIGTERM)
105 + (1 << SIGPIPE)
Denis Vlasenko08ea11a2008-09-11 19:51:11 +0000106 , record_signo);
Denis Vlasenkoef67c572008-01-27 22:40:39 +0000107
108 // error exit code if we fail to open the device
Denis Vlasenko08ea11a2008-09-11 19:51:11 +0000109 bb_got_signal = 1;
Denis Vlasenkoef67c572008-01-27 22:40:39 +0000110
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000111 // open device
112 sfd = open_or_warn(argv[0], O_RDWR | O_NOCTTY | O_NONBLOCK);
113 if (sfd < 0)
114 goto done;
Denys Vlasenkoab19ede2009-11-11 21:05:42 +0100115 fcntl(sfd, F_SETFL, O_RDWR);
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000116
117 // put device to "raw mode"
Denis Vlasenkoef67c572008-01-27 22:40:39 +0000118 xget1(sfd, &tio, &tiosfd);
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]))
Paul Foxa50a9b02008-04-23 15:31:29 +0000122 goto done;
123
124 // put stdin to "raw mode" (if stdin is a TTY),
125 // handle one character at a time
126 if (isatty(STDIN_FILENO)) {
127 xget1(STDIN_FILENO, &tio, &tio0);
128 if (xset1(STDIN_FILENO, &tio, "stdin"))
129 goto done;
130 }
Denis Vlasenko70766952007-09-21 17:42:40 +0000131
132 // main loop: check with poll(), then read/write bytes across
Denis Vlasenko0effc242008-01-28 09:39:30 +0000133 pfd[0].fd = sfd;
Denis Vlasenko70766952007-09-21 17:42:40 +0000134 pfd[0].events = POLLIN;
Denis Vlasenkoef67c572008-01-27 22:40:39 +0000135 pfd[1].fd = STDIN_FILENO;
Denis Vlasenko70766952007-09-21 17:42:40 +0000136 pfd[1].events = POLLIN;
Denis Vlasenkoef67c572008-01-27 22:40:39 +0000137
Denis Vlasenko08ea11a2008-09-11 19:51:11 +0000138 bb_got_signal = 0;
Denis Vlasenkoef67c572008-01-27 22:40:39 +0000139 nfd = 2;
Denys Vlasenkocb23d932010-01-12 12:29:55 +0100140 // Not safe_poll: we want to exit on signal
141 while (!bb_got_signal && poll(pfd, nfd, timeout) > 0) {
Denys Vlasenko88aa5582010-03-02 15:02:45 +0100142 if (nfd > 1 && pfd[1].revents) {
Denis Vlasenko9304d6e2008-02-10 19:05:56 +0000143 char c;
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000144 // read from stdin -> write to device
145 if (safe_read(STDIN_FILENO, &c, 1) < 1) {
Denis Vlasenkod56e3cc2008-02-09 11:37:21 +0000146 // don't poll stdin anymore if we got EOF/error
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000147 nfd--;
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000148 goto skip_write;
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000149 }
150 // do we need special processing?
151 if (!(opts & OPT_X)) {
152 // ^@ sends Break
153 if (VINTR == c) {
154 tcsendbreak(sfd, 0);
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000155 goto skip_write;
Denis Vlasenko70766952007-09-21 17:42:40 +0000156 }
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000157 // ^X exits
158 if (24 == c)
159 break;
Denis Vlasenko70766952007-09-21 17:42:40 +0000160 }
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000161 write(sfd, &c, 1);
Denis Vlasenko9304d6e2008-02-10 19:05:56 +0000162 if (delay >= 0)
163 safe_poll(pfd, 1, delay);
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000164skip_write: ;
Denis Vlasenko9304d6e2008-02-10 19:05:56 +0000165 }
Denys Vlasenko88aa5582010-03-02 15:02:45 +0100166 if (pfd[0].revents) {
Denis Vlasenko9304d6e2008-02-10 19:05:56 +0000167 ssize_t len;
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200168#define iobuf bb_common_bufsiz1
169 setup_common_bufsiz();
Denis Vlasenko9304d6e2008-02-10 19:05:56 +0000170 // read from device -> write to stdout
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200171 len = safe_read(sfd, iobuf, COMMON_BUFSIZE);
Denis Vlasenko9304d6e2008-02-10 19:05:56 +0000172 if (len > 0)
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000173 full_write(STDOUT_FILENO, iobuf, len);
174 else {
175 // EOF/error -> bail out
Denis Vlasenko08ea11a2008-09-11 19:51:11 +0000176 bb_got_signal = SIGHUP;
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000177 break;
178 }
Denis Vlasenko70766952007-09-21 17:42:40 +0000179 }
180 }
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000181
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000182 // restore device mode
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000183 tcsetattr(sfd, TCSAFLUSH, &tiosfd);
184
Denis Vlasenkod56e3cc2008-02-09 11:37:21 +0000185 if (isatty(STDIN_FILENO))
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000186 tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio0);
187
Denis Vlasenkoef67c572008-01-27 22:40:39 +0000188done:
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000189 if (device_lock_file)
Denis Vlasenko70766952007-09-21 17:42:40 +0000190 unlink(device_lock_file);
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000191
Denis Vlasenko08ea11a2008-09-11 19:51:11 +0000192 return bb_got_signal;
Denis Vlasenko70766952007-09-21 17:42:40 +0000193}