-
Notifications
You must be signed in to change notification settings - Fork 0
/
seriald.c
432 lines (358 loc) · 7.64 KB
/
seriald.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/**
* seriald.c - Server for u_serial verification
*
* Copyright (C) 2009-2016 Felipe Balbi <felipe.balbi@linux.intel.com>
*
* This file is part of the USB Verification Tools Project
*
* USB Tools is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public Liicense as published by
* the Free Software Foundation, either version 3 of the license, or
* (at your option) any later version.
*
* USB Tools is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with USB Tools. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <poll.h>
#include <getopt.h>
#include <malloc.h>
#include <sys/prctl.h>
#include <sys/types.h>
#include <sys/stat.h>
static unsigned char debug;
#define ENABLE_DBG() debug = 1
#define DBG(fmt, args...) \
if (debug) \
printf(fmt, ## args)
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
/**
* struct usb_serial_test - Context of our test
* @fd: opened file descriptor
* @amount_read: amount read until now
* @amount_write: amount written until now
* @size: buffer size
* @buf: the buffer
*/
struct usb_serial_test {
int fd;
uint64_t amount_read;
uint64_t amount_write;
unsigned size;
char *buf;
int new_session;
};
static int hangup;
static struct usb_serial_test _serial;
static void signal_hup(int sig)
{
hangup = 1;
close(_serial.fd);
DBG("%s: caught signal %d\n", __func__, sig);
}
/**
* tty_init - initializes an opened tty
* @fd: Device Handle
*/
static int tty_init(int fd)
{
int ret = 0;
struct termios term;
tcgetattr(fd, &term);
cfmakeraw(&term);
ret = tcflush(fd, TCIOFLUSH);
if (ret < 0) {
DBG("%s: flush failed\n", __func__);
goto err;
}
ret = tcsetattr(fd, TCSANOW, &term);
if (ret < 0) {
DBG("%s: couldn't set attributes\n", __func__);
goto err;
}
err:
return ret;
}
/**
* do_write - writes our buffer to fd
* @serial: Serial Test Context
*/
static int do_write(struct usb_serial_test *serial, uint32_t bytes)
{
int done = 0;
int ret;
char *buf = serial->buf;
DBG("%s: writting %d bytes\n", __func__, bytes);
while (done < bytes) {
ret = write(serial->fd, buf + done, bytes - done);
if (ret < 0) {
DBG("%s: failed to write %u bytes\n",
__func__, bytes);
goto err;
}
done += ret;
serial->amount_write += ret;
}
fsync(serial->fd);
return done;
err:
return ret;
}
/**
* do_read - reads from fd to our buffer
* @serial: Serial Test Context
*/
static int do_read(struct usb_serial_test *serial)
{
unsigned size = serial->size;
int done = 0;
int ret;
char *buf = serial->buf;
while (done < size) {
ret = read(serial->fd, buf + done, size - done);
if (ret < 0) {
DBG("%s: failed to read\n", __func__);
goto err;
}
size = (buf[0] << 24)
| (buf[1] << 16)
| (buf[2] << 8)
| (buf[3]);
if (size > serial->size) {
DBG("%s: corrupted size %d\n", __func__, size);
goto err;
}
done += ret;
serial->amount_read += ret;
}
DBG("%s: read %d bytes\n", __func__, done);
return done;
err:
return ret;
}
/**
* do_poll - polls our fd for incoming data
* @serial: Serial Test Context
*/
static int do_poll(struct usb_serial_test *serial)
{
int ret = -1;
struct pollfd pfd;
pfd.fd = serial->fd;
pfd.events = POLLIN;
ret = poll(&pfd, 1, -1);
if (ret <= 0) {
DBG("%s: poll failed\n", __func__);
goto err;
}
return 0;
err:
return ret;
}
static int do_open(const char *pathname, int flags)
{
int fd;
struct stat st;
pid_t pid;
fd = open(pathname, flags);
if (fd < 0) {
DBG("%s: open failed\n", __func__);
goto err0;
}
/* Make sure the file is a character device */
if (fstat(fd, &st)) {
DBG("%s failed to stat %s\n", __func__, pathname);
goto err1;
}
if (!S_ISCHR(st.st_mode)) {
DBG("%s: \"%s\" is not character device\n",
__func__, pathname);
errno = EBADF;
goto err1;
}
if (isatty(fd)) {
DBG("%s is tty\n", pathname);
if (!_serial.new_session) {
close(fd);
pid = fork();
if (pid > 0) {
DBG("%s: forked a child process\n", __func__);
/*
Parent foreground process is killed
on demand disposing of child background
process, so we need it run
*/
while (1)
sleep(1);
} else if (pid == -1) {
DBG("%s: fork failed\n", __func__);
goto err0;
}
prctl(PR_SET_PDEATHSIG, SIGKILL);
if (setsid() < 0) {
DBG("%s: setsid failed\n", __func__);
goto err0;
}
fd = open(pathname, flags);
if (fd < 0) {
DBG("%s: open failed\n", __func__);
goto err0;
}
_serial.new_session = 1;
}
if (tty_init(fd) < 0) {
DBG("%s: tty_init failed\n", __func__);
goto err1;
}
}
return fd;
err1:
close(fd);
err0:
return -1;
}
/**
* do_test - poll, read the data and write it back
* @serial: Serial Test Context
*/
static int do_test(struct usb_serial_test *serial)
{
uint32_t bytes;
int ret;
ret = do_poll(serial);
if (ret < 0)
goto err;
ret = do_read(serial);
if (ret < 0)
goto err;
bytes = ret;
ret = do_write(serial, bytes);
if (ret < 0)
goto err;
return 0;
err:
return ret;
}
static void usage(char *prog)
{
fprintf(stderr, "Usage: %s\n"
" --file, -f character device to use\n"
" --size, -s size of internal buffer\n"
" --debug, -d Enables debugging messages\n"
" --help, -h this help\n", prog);
}
static struct option serial_opts[] = {
{
.name = "file",
.has_arg = 1,
.val = 'f',
},
{
.name = "size",
.has_arg = 1,
.val = 's',
},
{
.name = "debug",
.val = 'd',
},
{
.name = "help",
.val = 'h',
},
{ } /* Terminating entry */
};
int main(int argc, char *argv[])
{
struct usb_serial_test *serial = &_serial;
unsigned size = 0;
int ret = 0;
char *file = NULL;
struct sigaction sa;
sa.sa_handler = signal_hup;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
while (ARRAY_SIZE(serial_opts)) {
int optidx = 0;
int opt;
opt = getopt_long(argc, argv, "f:s:dh", serial_opts, &optidx);
if (opt < 0)
break;
switch (opt) {
case 'f':
file = optarg;
break;
case 's':
size = atoi(optarg);
break;
case 'd':
ENABLE_DBG();
break;
case 'h': /* FALLTHROUGH */
default:
usage(argv[0]);
return 1;
}
}
if (!file || !size) {
fprintf(stderr, "%s%s",
file ? "" : "seriald: need a file to open\n",
size ? "" : "seriald: need size for the buffer\n");
fprintf(stderr, "Try `%s --help' for more information.\n",
argv[0]);
ret = 1;
goto err0;
}
DBG("%s: opening %s\n", __func__, file);
serial->fd = do_open(file, O_RDWR);
if (serial->fd < 0) {
fprintf(stderr, "%s: failed to open %s: %s\n",
argv[0], file, strerror(errno));
ret = 1;
goto err0;
}
DBG("%s: buffer size %d\n", __func__, size);
serial->size = size;
serial->buf = malloc(size);
if (!serial->buf) {
fprintf(stderr, "%s: failed to allocate buffer\n", argv[0]);
ret = 1;
goto err1;
}
if (sigaction(SIGHUP, &sa, NULL) == -1)
printf("failed to handle signal\n");
while (1) {
ret = do_test(serial);
if (hangup) {
hangup = 0;
serial->fd = do_open(file, O_RDWR);
if (serial->fd < 0)
fprintf(stderr, "%s: reopen failed\n", argv[0]);
else
continue;
}
if (ret < 0) {
fprintf(stderr, "%s: test failed: %s\n",
__func__, strerror(errno));
break;
}
}
free(serial->buf);
err1:
close(serial->fd);
err0:
return ret;
}