1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#include "libbb.h"
31#include "common_bufsiz.h"
32
33
34static void xget1(int fd, struct termios *t, struct termios *oldt)
35{
36 get_termios_and_make_raw(fd, t, oldt, 0
37 | TERMIOS_CLEAR_ISIG
38 | TERMIOS_RAW_INPUT
39 | TERMIOS_RAW_CRNL
40 );
41}
42
43static int xset1(int fd, struct termios *tio, const char *device)
44{
45 int ret = tcsetattr(fd, TCSAFLUSH, tio);
46
47 if (ret) {
48 bb_perror_msg("can't tcsetattr for %s", device);
49 }
50 return ret;
51}
52
53int microcom_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
54int microcom_main(int argc UNUSED_PARAM, char **argv)
55{
56 int sfd;
57 int nfd;
58 struct pollfd pfd[2];
59 struct termios tio0, tiosfd, tio;
60 char *device_lock_file;
61 enum {
62 OPT_X = 1 << 0,
63 OPT_s = 1 << 1,
64 OPT_d = 1 << 2,
65 OPT_t = 1 << 3,
66 };
67 speed_t speed = 9600;
68 int delay = -1;
69 int timeout = -1;
70 unsigned opts;
71
72
73 opts = getopt32(argv, "^" "Xs:+d:+t:+" "\0" "=1",
74 &speed, &delay, &timeout
75 );
76
77 argv += optind;
78
79
80 device_lock_file = (char *)bb_basename(argv[0]);
81 device_lock_file = xasprintf("/var/lock/LCK..%s", device_lock_file);
82 sfd = open(device_lock_file, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0644);
83 if (sfd < 0) {
84
85 if (errno == EEXIST)
86 bb_perror_msg_and_die("can't create '%s'", device_lock_file);
87
88 if (ENABLE_FEATURE_CLEAN_UP)
89 free(device_lock_file);
90 device_lock_file = NULL;
91 } else {
92
93
94
95 fdprintf(sfd, "%4d\n", getpid());
96 close(sfd);
97 }
98
99
100 bb_signals(0
101 + (1 << SIGHUP)
102 + (1 << SIGINT)
103 + (1 << SIGTERM)
104 + (1 << SIGPIPE)
105 , record_signo);
106
107
108 bb_got_signal = 1;
109
110
111 sfd = open_or_warn(argv[0], O_RDWR | O_NOCTTY | O_NONBLOCK);
112 if (sfd < 0)
113 goto done;
114 fcntl(sfd, F_SETFL, O_RDWR);
115
116
117 xget1(sfd, &tio, &tiosfd);
118
119 cfsetspeed(&tio, tty_value_to_baud(speed));
120 if (xset1(sfd, &tio, argv[0]))
121 goto done;
122
123
124
125 if (isatty(STDIN_FILENO)) {
126 xget1(STDIN_FILENO, &tio, &tio0);
127 if (xset1(STDIN_FILENO, &tio, "stdin"))
128 goto done;
129 }
130
131
132 pfd[0].fd = sfd;
133 pfd[0].events = POLLIN;
134 pfd[1].fd = STDIN_FILENO;
135 pfd[1].events = POLLIN;
136
137 bb_got_signal = 0;
138 nfd = 2;
139
140 while (!bb_got_signal && poll(pfd, nfd, timeout) > 0) {
141 if (nfd > 1 && pfd[1].revents) {
142 char c;
143
144 if (safe_read(STDIN_FILENO, &c, 1) < 1) {
145
146 nfd--;
147 goto skip_write;
148 }
149
150 if (!(opts & OPT_X)) {
151
152 if (VINTR == c) {
153 tcsendbreak(sfd, 0);
154 goto skip_write;
155 }
156
157 if (24 == c)
158 break;
159 }
160 write(sfd, &c, 1);
161 if (delay >= 0)
162 safe_poll(pfd, 1, delay);
163skip_write: ;
164 }
165 if (pfd[0].revents) {
166 ssize_t len;
167#define iobuf bb_common_bufsiz1
168 setup_common_bufsiz();
169
170 len = safe_read(sfd, iobuf, COMMON_BUFSIZE);
171 if (len > 0)
172 full_write(STDOUT_FILENO, iobuf, len);
173 else {
174
175 bb_got_signal = SIGHUP;
176 break;
177 }
178 }
179 }
180
181
182 tcsetattr(sfd, TCSAFLUSH, &tiosfd);
183
184 if (isatty(STDIN_FILENO))
185 tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio0);
186
187done:
188 if (device_lock_file)
189 unlink(device_lock_file);
190
191 return bb_got_signal;
192}
193