Bump to version 1.22.1
[platform/upstream/busybox.git] / miscutils / microcom.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * bare bones 'talk to modem' program - similar to 'cu -l $device'
4  * inspired by mgetty's microcom
5  *
6  * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
7  *
8  * Licensed under GPLv2, see file LICENSE in this source tree.
9  */
10
11 //usage:#define microcom_trivial_usage
12 //usage:       "[-d DELAY] [-t TIMEOUT] [-s SPEED] [-X] TTY"
13 //usage:#define microcom_full_usage "\n\n"
14 //usage:       "Copy bytes for stdin to TTY and from TTY to stdout\n"
15 //usage:     "\n        -d      Wait up to DELAY ms for TTY output before sending every"
16 //usage:     "\n                next byte to it"
17 //usage:     "\n        -t      Exit if both stdin and TTY are silent for TIMEOUT ms"
18 //usage:     "\n        -s      Set serial line to SPEED"
19 //usage:     "\n        -X      Disable special meaning of NUL and Ctrl-X from stdin"
20
21 #include "libbb.h"
22
23 // set raw tty mode
24 static void xget1(int fd, struct termios *t, struct termios *oldt)
25 {
26         tcgetattr(fd, oldt);
27         *t = *oldt;
28         cfmakeraw(t);
29 //      t->c_lflag &= ~(ISIG|ICANON|ECHO|IEXTEN);
30 //      t->c_iflag &= ~(BRKINT|IXON|ICRNL);
31 //      t->c_oflag &= ~(ONLCR);
32 //      t->c_cc[VMIN]  = 1;
33 //      t->c_cc[VTIME] = 0;
34 }
35
36 static int xset1(int fd, struct termios *tio, const char *device)
37 {
38         int ret = tcsetattr(fd, TCSAFLUSH, tio);
39
40         if (ret) {
41                 bb_perror_msg("can't tcsetattr for %s", device);
42         }
43         return ret;
44 }
45
46 int microcom_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
47 int microcom_main(int argc UNUSED_PARAM, char **argv)
48 {
49         int sfd;
50         int nfd;
51         struct pollfd pfd[2];
52         struct termios tio0, tiosfd, tio;
53         char *device_lock_file;
54         enum {
55                 OPT_X = 1 << 0, // do not respect Ctrl-X, Ctrl-@
56                 OPT_s = 1 << 1, // baudrate
57                 OPT_d = 1 << 2, // wait for device response, ms
58                 OPT_t = 1 << 3, // timeout, ms
59         };
60         speed_t speed = 9600;
61         int delay = -1;
62         int timeout = -1;
63         unsigned opts;
64
65         // fetch options
66         opt_complementary = "=1:s+:d+:t+"; // exactly one arg, numeric options
67         opts = getopt32(argv, "Xs:d:t:", &speed, &delay, &timeout);
68 //      argc -= optind;
69         argv += optind;
70
71         // try to create lock file in /var/lock
72         device_lock_file = (char *)bb_basename(argv[0]);
73         device_lock_file = xasprintf("/var/lock/LCK..%s", device_lock_file);
74         sfd = open(device_lock_file, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0644);
75         if (sfd < 0) {
76                 // device already locked -> bail out
77                 if (errno == EEXIST)
78                         bb_perror_msg_and_die("can't create '%s'", device_lock_file);
79                 // can't create lock -> don't care
80                 if (ENABLE_FEATURE_CLEAN_UP)
81                         free(device_lock_file);
82                 device_lock_file = NULL;
83         } else {
84                 // %4d to make concurrent mgetty (if any) happy.
85                 // Mgetty treats 4-bytes lock files as binary,
86                 // not text, PID. Making 5+ char file. Brrr...
87                 fdprintf(sfd, "%4d\n", getpid());
88                 close(sfd);
89         }
90
91         // setup signals
92         bb_signals(0
93                 + (1 << SIGHUP)
94                 + (1 << SIGINT)
95                 + (1 << SIGTERM)
96                 + (1 << SIGPIPE)
97                 , record_signo);
98
99         // error exit code if we fail to open the device
100         bb_got_signal = 1;
101
102         // open device
103         sfd = open_or_warn(argv[0], O_RDWR | O_NOCTTY | O_NONBLOCK);
104         if (sfd < 0)
105                 goto done;
106         fcntl(sfd, F_SETFL, O_RDWR);
107
108         // put device to "raw mode"
109         xget1(sfd, &tio, &tiosfd);
110         // set device speed
111         cfsetspeed(&tio, tty_value_to_baud(speed));
112         if (xset1(sfd, &tio, argv[0]))
113                 goto done;
114
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"))
120                         goto done;
121         }
122
123         // main loop: check with poll(), then read/write bytes across
124         pfd[0].fd = sfd;
125         pfd[0].events = POLLIN;
126         pfd[1].fd = STDIN_FILENO;
127         pfd[1].events = POLLIN;
128
129         bb_got_signal = 0;
130         nfd = 2;
131         // Not safe_poll: we want to exit on signal
132         while (!bb_got_signal && poll(pfd, nfd, timeout) > 0) {
133                 if (nfd > 1 && pfd[1].revents) {
134                         char c;
135                         // read from stdin -> write to device
136                         if (safe_read(STDIN_FILENO, &c, 1) < 1) {
137                                 // don't poll stdin anymore if we got EOF/error
138                                 nfd--;
139                                 goto skip_write;
140                         }
141                         // do we need special processing?
142                         if (!(opts & OPT_X)) {
143                                 // ^@ sends Break
144                                 if (VINTR == c) {
145                                         tcsendbreak(sfd, 0);
146                                         goto skip_write;
147                                 }
148                                 // ^X exits
149                                 if (24 == c)
150                                         break;
151                         }
152                         write(sfd, &c, 1);
153                         if (delay >= 0)
154                                 safe_poll(pfd, 1, delay);
155 skip_write: ;
156                 }
157                 if (pfd[0].revents) {
158 #define iobuf bb_common_bufsiz1
159                         ssize_t len;
160                         // read from device -> write to stdout
161                         len = safe_read(sfd, iobuf, sizeof(iobuf));
162                         if (len > 0)
163                                 full_write(STDOUT_FILENO, iobuf, len);
164                         else {
165                                 // EOF/error -> bail out
166                                 bb_got_signal = SIGHUP;
167                                 break;
168                         }
169                 }
170         }
171
172         // restore device mode
173         tcsetattr(sfd, TCSAFLUSH, &tiosfd);
174
175         if (isatty(STDIN_FILENO))
176                 tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio0);
177
178 done:
179         if (device_lock_file)
180                 unlink(device_lock_file);
181
182         return bb_got_signal;
183 }