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