libbb: introduce bb_signals and bb_signals_recursive,
[oweals/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) 2007 by Vladimir Dronnikov <dronnikov@gmail.ru>
7  *
8  * Licensed under GPLv2, see file LICENSE in this tarball for details.
9  */
10 #include "libbb.h"
11
12 /* All known arches use small ints for signals */
13 static volatile smallint signalled;
14
15 static void signal_handler(int signo)
16 {
17         signalled = signo;
18 }
19
20 // set raw tty mode
21 static void xget1(int fd, struct termios *t, struct termios *oldt)
22
23         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
33 static int xset1(int fd, struct termios *tio, const char *device)
34 {
35         int ret = tcsetattr(fd, TCSAFLUSH, tio);
36
37         if (ret) {
38                 bb_perror_msg("can't tcsetattr for %s", device);
39         }
40         return ret;
41 }
42
43 int microcom_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
44 int microcom_main(int argc, char **argv)
45 {
46         int sfd;
47         int nfd;
48         struct pollfd pfd[2];
49         struct termios tio0, tiosfd, tio;
50         char *device_lock_file;
51         enum {
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
56         };
57         speed_t speed = 9600;
58         int delay = -1;
59         int timeout = -1;
60
61         // fetch options
62         char *opt_s;
63         char *opt_d;
64         char *opt_t;
65         unsigned opts;
66         opt_complementary = "=1"; // exactly one arg should be there
67         opts = getopt32(argv, "Xs:d:t:", &opt_s, &opt_d, &opt_t);
68
69         // apply options
70         if (opts & OPT_s)
71                 speed = xatoi_u(opt_s);
72         if (opts & OPT_d)
73                 delay = xatoi_u(opt_d);
74         if (opts & OPT_t)
75                 timeout = xatoi_u(opt_t);
76
77 //      argc -= optind;
78         argv += optind;
79
80         // try to create lock file in /var/lock
81         device_lock_file = (char *)bb_basename(argv[0]);
82         device_lock_file = xasprintf("/var/lock/LCK..%s", device_lock_file);
83         sfd = open(device_lock_file, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0644);
84         if (sfd < 0) {
85                 if (errno == EEXIST)
86                         bb_perror_msg_and_die("can't create %s", device_lock_file);
87                 if (ENABLE_FEATURE_CLEAN_UP)
88                         free(device_lock_file);
89                 device_lock_file = NULL;
90         }
91         if (sfd > 0) {
92                 // %4d to make mgetty happy. It treats 4-bytes lock files as binary,
93                 // not text, PID. Making 5+ char file. Brrr...
94                 char *s = xasprintf("%4d\n", getpid());
95                 write(sfd, s, strlen(s));
96                 if (ENABLE_FEATURE_CLEAN_UP)
97                         free(s);
98                 close(sfd);
99         }
100
101         // setup signals
102         bb_signals_recursive(0
103                         + (1 << SIGHUP)
104                         + (1 << SIGINT)
105                         + (1 << SIGTERM)
106                         + (1 << SIGPIPE)
107                         , signal_handler);
108
109         // error exit code if we fail to open the device
110         signalled = 1;
111
112         // open device
113         sfd = open_or_warn(argv[0], O_RDWR | O_NOCTTY | O_NONBLOCK);
114         if (sfd < 0)
115                 goto done;
116         fcntl(sfd, F_SETFL, O_RDWR | O_NOCTTY);
117
118         // put stdin to "raw mode" (if stdin is a TTY),
119         // handle one character at a time
120         if (isatty(STDIN_FILENO)) {
121                 xget1(STDIN_FILENO, &tio, &tio0);
122                 if (xset1(STDIN_FILENO, &tio, "stdin"))
123                         goto done;
124         }
125
126         // same thing for modem
127         xget1(sfd, &tio, &tiosfd);
128         // order device to hang up at exit
129         tio.c_cflag |= (CREAD|HUPCL);
130 //      if (!istty)
131 //              tio.c_iflag |= (IGNCR);
132         // set device speed
133         cfsetspeed(&tio, tty_value_to_baud(speed));
134         if (xset1(sfd, &tio, argv[0]))
135                 goto restore0_and_done;
136
137         // main loop: check with poll(), then read/write bytes across
138         pfd[0].fd = sfd;
139         pfd[0].events = POLLIN;
140         pfd[1].fd = STDIN_FILENO;
141         pfd[1].events = POLLIN;
142
143         signalled = 0;
144         nfd = 2;
145         while (!signalled && safe_poll(pfd, nfd, timeout) > 0) {
146                 if (pfd[1].revents) {
147                         char c;
148                         // read from stdin -> write to device
149                         if (safe_read(STDIN_FILENO, &c, 1) < 1) {
150                                 // don't poll stdin anymore if we got EOF/error
151                                 pfd[1].revents = 0;
152                                 nfd--;
153                                 goto check_stdin;
154                         }
155                         // do we need special processing?
156                         if (!(opts & OPT_X)) {
157                                 // ^@ sends Break
158                                 if (VINTR == c) {
159                                         tcsendbreak(sfd, 0);
160                                         goto check_stdin;
161                                 }
162                                 // ^X exits
163                                 if (24 == c)
164                                         break;
165                         }
166                         write(sfd, &c, 1);
167                         if (delay >= 0)
168                                 safe_poll(pfd, 1, delay);
169                 }
170 check_stdin:
171                 if (pfd[0].revents) {
172                         ssize_t len;
173                         // read from device -> write to stdout
174                         len = safe_read(sfd, bb_common_bufsiz1, sizeof(bb_common_bufsiz1));
175                         if (len > 0)
176                                 full_write(STDOUT_FILENO, bb_common_bufsiz1, len);
177                         // else { EOF/error - what to do? }
178                 }
179         }
180
181         tcsetattr(sfd, TCSAFLUSH, &tiosfd);
182
183 restore0_and_done:
184         if (isatty(STDIN_FILENO))
185                 tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio0);
186
187 done:
188         if (device_lock_file)
189                 unlink(device_lock_file);
190
191         return signalled;
192 }