-
Notifications
You must be signed in to change notification settings - Fork 2
/
sercons.c
365 lines (311 loc) · 6.87 KB
/
sercons.c
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <strings.h>
#include <pthread.h>
#define ERRNO_NON_FATAL(en) \
((en) == ENOENT || \
(en) == EACCES || \
(en) == EPERM || \
(en) == ECONNREFUSED)
typedef enum end_reason {
ER_NONE = 0,
ER_ESCAPE_CHAR = 1,
ER_ERROR = 2,
ER_EOF = 3
} end_reason_t;
static int orig_tios_stored = 0;
static struct termios orig_tios;
static pthread_cond_t g_end_cv = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t g_end_mtx = PTHREAD_MUTEX_INITIALIZER;
static end_reason_t g_end_reason = ER_NONE;
static void *
post_end(end_reason_t reason)
{
if (reason == ER_NONE)
abort();
if (pthread_mutex_lock(&g_end_mtx) != 0)
abort();
if (g_end_reason == ER_NONE) {
g_end_reason = reason;
if (pthread_cond_broadcast(&g_end_cv) != 0)
abort();
}
if (pthread_mutex_unlock(&g_end_mtx) != 0)
abort();
return (NULL);
}
static end_reason_t
wait_for_end(void)
{
end_reason_t reason = ER_NONE;
for (;;) {
if (pthread_mutex_lock(&g_end_mtx) != 0)
abort();
if (pthread_cond_wait(&g_end_cv, &g_end_mtx) != 0)
abort();
reason = g_end_reason;
if (pthread_mutex_unlock(&g_end_mtx) != 0)
abort();
if (reason != ER_NONE)
return (reason);
}
}
typedef struct copy_args {
int ca_fd_src;
int ca_fd_src_is_terminal;
int ca_fd_dst;
int ca_fd_logfile;
char ca_escape_char;
} copy_args_t;
static void *
copy_thread(void *arg)
{
char c1 = '\0', c2 = '\0';
copy_args_t *ca = arg;
for (;;) {
ssize_t sz;
char c;
sz = read(ca->ca_fd_src, &c, 1);
switch (sz) {
case 0:
if (ca->ca_fd_src_is_terminal)
continue;
else
return (post_end(ER_EOF));
case 1:
break;
default:
return (post_end(ER_ERROR));
}
if ((sz = write(ca->ca_fd_dst, &c, 1)) != 1) {
return (post_end(ER_ERROR));
}
/*
* Write log file if we have one:
*/
if (ca->ca_fd_logfile != -1) {
(void) write(ca->ca_fd_logfile, &c, 1);
(void) fsync(ca->ca_fd_logfile);
}
if (ca->ca_escape_char == '\0')
continue;
/*
* Escape character sequence detection:
*/
if ((c2 == '\r' || c2 == '\n') &&
c1 == ca->ca_escape_char &&
c == '.') {
return (post_end(ER_ESCAPE_CHAR));
}
c2 = c1;
c1 = c;
}
}
static int
reset_mode(int term_fd)
{
if (tcsetattr(term_fd, TCSAFLUSH, &orig_tios) == -1)
return (-1);
return (0);
}
static int
raw_mode(int term_fd)
{
struct termios raw;
if (orig_tios_stored == 0) {
orig_tios_stored = 1;
if (tcgetattr(term_fd, &orig_tios) == -1) {
return (-1);
}
}
raw = orig_tios;
/*
* Various raw-mode settings:
*/
raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
raw.c_oflag &= ~(OPOST);
raw.c_cflag |= (CS8);
raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
/*
* We want read() on the tty to block until there is at least one byte:
*/
raw.c_cc[VMIN] = 0;
raw.c_cc[VTIME] = 10;
if (tcsetattr(term_fd, TCSAFLUSH, &raw) == -1)
return (-1);
return (0);
}
static int
make_conn(const char *path)
{
int ret = -1;
int fd = -1;
struct sockaddr_un *ua;
int e = 0;
/*
* The "sun_path" member of a "sockaddr_un" is generally on the order
* of 100 bytes long, but is also at the end of the struct on most
* platforms. In order to support longer path names, we allocate space
* for the object on the heap, with extra trailing bytes into which the
* longer path can extend.
*/
if ((ua = calloc(1, sizeof (*ua) + strlen(path) + 1)) == NULL) {
e = errno;
goto out;
}
ua->sun_family = AF_UNIX;
strcpy(ua->sun_path, path);
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1 ||
connect(fd, (struct sockaddr *)ua, SUN_LEN(ua)) == -1) {
e = errno;
goto out;
}
ret = fd;
out:
free(ua);
if (ret == -1 && fd != -1) {
(void) close(fd);
}
errno = e;
return (ret);
}
static int
open_logfile(const char *path)
{
const char *msg = "\n * log file opened\n\n";
int logfd;
logfd = open(path, O_WRONLY | O_CREAT | O_APPEND, 0644);
if (logfd == -1) {
perror("opening log file");
exit(1);
}
(void) write(logfd, msg, strlen(msg));
(void) fsync(logfd);
return (logfd);
}
int
main(int argc, char **argv)
{
copy_args_t ca_a, ca_b;
pthread_t thr_a, thr_b;
int connfd = -1, termfd, logfd = -1;
end_reason_t reason = ER_ERROR;
int c, errflg = 0, Wflag = 0;
char *logpath = NULL, *sockpath = NULL;
while ((c = getopt(argc, argv, ":l:W")) != -1) {
switch (c) {
case 'l':
/*
* -l <log_file>
* Write all data received through the serial pipe
* (i.e. from the "remote" virtual guest) into this
* log file. Log file is opened for append.
*/
logpath = optarg;
break;
case 'W':
/*
* -W
* Disable the default behaviour, which is to keep
* trying to connect until the socket becomes available.
*/
Wflag++;
break;
case ':':
fprintf(stderr, "Option -%c requires an operand\n",
optopt);
errflg++;
break;
case '?':
fprintf(stderr, "Unrecognized option: -%c\n", optopt);
errflg++;
break;
}
}
if (errflg != 0 || optind >= argc) {
fprintf(stderr, "Usage: %s [-W] [-l <log_file>] <socket_path>\n",
argv[0]);
exit(1);
}
sockpath = argv[optind];
if (logpath != NULL)
logfd = open_logfile(logpath);
while (connfd == -1) {
static int firstloop = 1;
if ((connfd = make_conn(sockpath)) == -1) {
if (Wflag || !ERRNO_NON_FATAL(errno)) {
perror("opening serial socket");
exit(1);
}
if (firstloop) {
fprintf(stderr, " * Waiting for "
"socket (%s)...", sockpath);
} else {
fprintf(stderr, ".");
}
fflush(stderr);
sleep(1);
}
firstloop = 0;
}
fprintf(stderr, "\n * Connected. Escape sequence is <Enter>#.\n");
if ((termfd = open("/dev/tty", O_RDWR | O_NOCTTY)) == -1) {
perror("opening controlling terminal");
exit(1);
}
if (raw_mode(termfd) == -1) {
fprintf(stderr, "could not set raw mode on terminal\n");
exit(1);
}
/*
* Read from terminal, send to remote:
*/
ca_a.ca_fd_src = termfd;
ca_a.ca_fd_src_is_terminal = 1;
ca_a.ca_fd_dst = connfd;
ca_a.ca_fd_logfile = -1;
ca_a.ca_escape_char = '#';
if (pthread_create(&thr_a, NULL, copy_thread, &ca_a) != 0)
goto out;
/*
* Read from remote, send to terminal:
*/
ca_b.ca_fd_src = connfd;
ca_b.ca_fd_src_is_terminal = 0;
ca_b.ca_fd_dst = termfd;
ca_b.ca_fd_logfile = logfd;
ca_b.ca_escape_char = '\0';
if (pthread_create(&thr_b, NULL, copy_thread, &ca_b) != 0)
goto out;
reason = wait_for_end();
out:
/*
* Attempt to reset terminal and pty attributes:
*/
(void) write(termfd, "\e[0m", 4);
reset_mode(termfd);
switch (reason) {
case ER_ERROR:
fprintf(stderr, "\n * Unknown Error.\n");
exit(1);
break;
case ER_ESCAPE_CHAR:
fprintf(stderr, "\n * Escape Character Received.\n");
exit(0);
break;
case ER_EOF:
fprintf(stderr, "\n * EOF on read.\n");
exit(50);
break;
default:
abort();
}
}