1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include "libbb.h"
22
23
24static void xget1(int fd, struct termios *t, struct termios *oldt)
25{
26 tcgetattr(fd, oldt);
27 *t = *oldt;
28 cfmakeraw(t);
29
30
31
32
33
34}
35
36static 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
46int microcom_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
47int 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,
56 OPT_s = 1 << 1,
57 OPT_d = 1 << 2,
58 OPT_t = 1 << 3,
59 };
60 speed_t speed = 9600;
61 int delay = -1;
62 int timeout = -1;
63 unsigned opts;
64
65
66 opt_complementary = "=1:s+:d+:t+";
67 opts = getopt32(argv, "Xs:d:t:", &speed, &delay, &timeout);
68
69 argv += optind;
70
71
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
77 if (errno == EEXIST)
78 bb_perror_msg_and_die("can't create '%s'", device_lock_file);
79
80 if (ENABLE_FEATURE_CLEAN_UP)
81 free(device_lock_file);
82 device_lock_file = NULL;
83 } else {
84
85
86
87 fdprintf(sfd, "%4d\n", getpid());
88 close(sfd);
89 }
90
91
92 bb_signals(0
93 + (1 << SIGHUP)
94 + (1 << SIGINT)
95 + (1 << SIGTERM)
96 + (1 << SIGPIPE)
97 , record_signo);
98
99
100 bb_got_signal = 1;
101
102
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
109 xget1(sfd, &tio, &tiosfd);
110
111 cfsetspeed(&tio, tty_value_to_baud(speed));
112 if (xset1(sfd, &tio, argv[0]))
113 goto done;
114
115
116
117 if (isatty(STDIN_FILENO)) {
118 xget1(STDIN_FILENO, &tio, &tio0);
119 if (xset1(STDIN_FILENO, &tio, "stdin"))
120 goto done;
121 }
122
123
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
132 while (!bb_got_signal && poll(pfd, nfd, timeout) > 0) {
133 if (nfd > 1 && pfd[1].revents) {
134 char c;
135
136 if (safe_read(STDIN_FILENO, &c, 1) < 1) {
137
138 nfd--;
139 goto skip_write;
140 }
141
142 if (!(opts & OPT_X)) {
143
144 if (VINTR == c) {
145 tcsendbreak(sfd, 0);
146 goto skip_write;
147 }
148
149 if (24 == c)
150 break;
151 }
152 write(sfd, &c, 1);
153 if (delay >= 0)
154 safe_poll(pfd, 1, delay);
155skip_write: ;
156 }
157 if (pfd[0].revents) {
158#define iobuf bb_common_bufsiz1
159 ssize_t len;
160
161 len = safe_read(sfd, iobuf, sizeof(iobuf));
162 if (len > 0)
163 full_write(STDOUT_FILENO, iobuf, len);
164 else {
165
166 bb_got_signal = SIGHUP;
167 break;
168 }
169 }
170 }
171
172
173 tcsetattr(sfd, TCSAFLUSH, &tiosfd);
174
175 if (isatty(STDIN_FILENO))
176 tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio0);
177
178done:
179 if (device_lock_file)
180 unlink(device_lock_file);
181
182 return bb_got_signal;
183}
184