-
Notifications
You must be signed in to change notification settings - Fork 0
/
poll-test.c
391 lines (342 loc) · 9.25 KB
/
poll-test.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
// SPDX-License-Identifier: MIT
/*
* Copyright (C) 2020 Norik systems d.o.o.
* Authors: Primoz Fiser <primoz.fiser@norik.com>
* Luka Zlatecan <luka.zlatecan@norik.com>
*/
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <poll.h>
#ifdef DEBUG
#define DBG(...) do { fprintf(stdout, "[DEBUG]: "__VA_ARGS__); } while(0)
#else
#define DBG(...) do { } while (0)
#endif
#define WARN(...) do { fprintf(stdout, " [WARN]: "__VA_ARGS__); } while(0)
#define INFO(...) do { fprintf(stdout, " [INFO]: "__VA_ARGS__); } while(0)
#define ERROR(...) do { fprintf(stderr, "[ERROR]: "__VA_ARGS__); } while(0)
#define DEFAULT_READ_SIZE (4096)
#define DEFAULT_TIMEOUT (-1)
#define DEFAULT_EVENTS_MASK (POLLIN|POLLOUT)
#define DEFAULT_FILE_FLAGS (O_RDWR)
char *prompt;
struct poll_event {
int number;
char name[11];
};
struct poll_event poll_events[] =
{
{ POLLIN, "POLLIN" },
{ POLLPRI, "POLLPRI" },
{ POLLOUT, "POLLOUT" },
{ POLLERR, "POLLERR" },
{ POLLHUP, "POLLHUP" },
{ POLLNVAL, "POLLNVAL" },
{ POLLRDNORM, "POLLRDNORM" },
{ POLLRDBAND, "POLLRDBAND" },
{ POLLWRNORM, "POLLWRNORM" },
{ POLLWRBAND, "POLLWRBAND" },
};
struct fopen_flag {
int number;
char name[11];
};
struct fopen_flag fopen_flags[] =
{
{ O_RDONLY, "O_RDONLY" },
{ O_WRONLY, "O_WRONLY" },
{ O_RDWR, "O_RDWR" },
{ O_NONBLOCK, "O_NONBLOCK" },
{ O_RDONLY, "ro" },
{ O_WRONLY, "wo" },
{ O_RDWR, "rw" },
{ O_NONBLOCK, "nonblock" },
};
int parse_optarg_file_flags(char *str)
{
char sep[] = " ,;:|";
char *token;
int flag = -1;
int temp = 0;
int i;
DBG("%s(): input string = %s\n", __func__, str);
/* Allow to specify fopen_flag as hex value */
sscanf(str, "%x", &flag);
DBG("%s(): flag = %d\n", __func__, flag);
if (flag >= 0) {
for (i = 0; i < sizeof(fopen_flags) / sizeof(struct fopen_flag); ++i) {
if (fopen_flags[i].number == flag)
goto out;
}
WARN("File open flag 0x%08X not officially supported!\n", flag);
goto out;
} else
flag = -1;
token = strtok(str, sep);
while (token != NULL)
{
for (i = 0; i < sizeof(fopen_flags) / sizeof(struct fopen_flag); ++i)
{
if (!strcmp(token, fopen_flags[i].name)) {
temp |= fopen_flags[i].number;
flag = temp;
DBG("%s(): 0x%08X -> %s == %s\n", __func__,
fopen_flags[i].number, fopen_flags[i].name, token);
}
}
token = strtok(NULL, sep);
}
out:
DBG("%s(): output file_flag = 0x%08X\n", __func__, flag);
return flag;
}
int parse_optarg_poll_mask(char *str)
{
char sep[] = " ,;:|";
char *token;
int mask = -1;
int temp = 0;
int i;
DBG("%s(): input string = %s\n", __func__, str);
/* Allow to specify poll_mask as hex value */
sscanf(str, "%x", &mask);
DBG("%s(): mask = %d\n", __func__, mask);
if (mask > 0) {
for (i = 0; i < sizeof(poll_events) / sizeof(struct poll_event); ++i) {
if (poll_events[i].number == mask)
goto out;
}
WARN("Poll mask 0x%04X not officially supported!\n", mask);
goto out;
} else
mask = -1;
token = strtok(str, sep);
while (token != NULL)
{
for (i = 0; i < sizeof(poll_events) / sizeof(struct poll_event); ++i)
{
if (!strcmp(token, poll_events[i].name)) {
temp |= poll_events[i].number;
mask = temp;
DBG("%s(): 0x%04X -> %s == %s\n", __func__,
poll_events[i].number, poll_events[i].name, token);
}
}
token = strtok(NULL, sep);
}
out:
DBG("%s(): output poll_mask = 0x%04X\n", __func__, mask);
return mask;
}
int parse_poll_revents_to_string(int revents, char *output)
{
char str[256] = "";
int i;
DBG("%s(): input revents = 0x%04X\n", __func__, revents);
for (i = 0; i < sizeof(poll_events) / sizeof(struct poll_event); ++i)
{
if (revents & (poll_events[i].number)) {
strcat(str, poll_events[i].name);
DBG("%s(): found 0x%04X -> %s in 0x%04X\n", __func__,
poll_events[i].number, poll_events[i].name, revents);
strcat(str, "|");
}
}
/* Remove last '|' delimiter */
if(str[strlen(str)-1] == '|')
str[strlen(str)-1] = '\0';
strcpy(output, str);
DBG("%s(): output string = %s\n", __func__, str);
return 0;
}
int usage(void)
{
fprintf(stderr, "usage: %s %s%s%s%s%s%s\n",
prompt,
"[-f file]",
"\n\t[-m POLLMASK]",
"\n\t[-o FOPEN_FLAGS]",
"\n\t[-t timeout]",
"\n\t[-l] [-i] [-e] [-w]",
"\n\t[-h]"
);
exit(EXIT_FAILURE);
}
int main(int argc, char **argv)
{
char data[DEFAULT_READ_SIZE];
struct pollfd poll_fds[1];
char *filename = NULL;
int fd, opt, bytes, ret = -1, i = 0;
int init_read = -1, read_after = -1, write_after = -1;
int poll_timeout = -1, poll_mask = -1;
int file_flags = -1;
int loop_mode = 0;
if ((prompt = strrchr(argv[0], '/')) != NULL)
++prompt;
else
prompt = argv[0];
while ((opt = getopt(argc, argv, "f:m:o:t:liewh")) != -1)
{
switch (opt)
{
case 'f':
DBG("ARG: filename: %s\n", optarg);
filename = strdup(optarg);
break;
case 'm':
DBG("ARG: poll_mask: %s\n", optarg);
poll_mask = parse_optarg_poll_mask(optarg);
break;
case 'o':
DBG("ARG: file_flags: %s\n", optarg);
file_flags = parse_optarg_file_flags(optarg);
break;
case 't':
DBG("ARG: poll_timeout: %s\n", optarg);
poll_timeout = atoi(optarg);
break;
case 'l':
DBG("ARG: loop mode ON\n");
loop_mode = 1;
break;
case 'i':
DBG("ARG: initial dummy read() ON\n");
init_read = 1;
break;
case 'e':
DBG("ARG: after poll() dummy read() ON\n");
read_after = 1;
break;
case 'w':
DBG("ARG: after poll() dummy write() ON\n");
write_after = 1;
break;
case 'h':
usage();
case '?':
ERROR("Unknown option switch -%c\n", optopt);
usage();
}
}
if (filename == NULL) {
ERROR("No input file specified, use -f switch\n");
usage();
}
if (file_flags == -1) {
WARN("Using default open() file flags: 0x%08X\n", DEFAULT_FILE_FLAGS);
file_flags = DEFAULT_FILE_FLAGS;
}
if (poll_mask == -1) {
parse_poll_revents_to_string(DEFAULT_EVENTS_MASK, data);
WARN("Using default poll() mask value: 0x%04X -> %s\n",
DEFAULT_EVENTS_MASK, data);
poll_mask = DEFAULT_EVENTS_MASK;
}
if (poll_timeout < 0) {
WARN("Using default poll() timeout value: %d %s\n",
DEFAULT_TIMEOUT, DEFAULT_TIMEOUT < 0 ? "(no timeout)" : "ms");
poll_timeout = DEFAULT_TIMEOUT;
}
#ifdef DEBUG
DBG("Listing all supported poll events:\n");
for (i = 0; i < sizeof(poll_events) / sizeof(struct poll_event); ++i)
{
DBG("0x%04X -> %s\n", poll_events[i].number, poll_events[i].name);
}
DBG("Listing all supported fopen flags:\n");
for (i = 0; i < sizeof(fopen_flags) / sizeof(struct fopen_flag); ++i)
{
DBG("0x%08X -> %s\n", fopen_flags[i].number, fopen_flags[i].name);
}
i = 0;
#endif
parse_poll_revents_to_string(poll_mask, data);
INFO("Do %s poll() on %s (mask: 0x%04X -> %s, poll_timeout: %d %s)\n",
loop_mode ? "loop" : "single", filename, poll_mask, data,
poll_timeout, poll_timeout < 0 ? "(no timeout)" : "ms");
/* Open file */
if ((fd = open(filename, file_flags)) < 0)
{
ERROR("Unable to open %s: %s\n", filename, strerror(errno));
goto out;
}
/* Initial dummy read() */
if (init_read == 1) {
bytes = read(fd, data, DEFAULT_READ_SIZE);
if (bytes == -1) {
ERROR("Inital dummy read() failed: %s\n", strerror(errno));
goto out;
}
INFO("Initial dummy read() returns %d bytes\n", bytes);
}
/* Setup & do poll() */
poll_fds[0].fd = fd;
poll_fds[0].revents = 0;
poll_fds[0].events = poll_mask;
do {
ret = poll(poll_fds, 1, poll_timeout);
if (ret == -1) {
ERROR("(%d) poll() error: %s\n", i, strerror(errno));
goto out;
} else if (ret == 0) {
INFO("(%d) poll() timeout of %d msec reached...\n", i,
poll_timeout);
} else {
parse_poll_revents_to_string(poll_fds[0].revents, data);
INFO("(%d) poll() returned revents: 0x%04X -> %s\n", i,
poll_fds[0].revents, data);
/* We can do dummy read() if enabled */
if ((read_after != -1) && (poll_fds[0].revents &
(POLLIN|POLLPRI|POLLRDNORM|POLLRDBAND))) {
DBG("(%d) Executing dummy read() after poll()\n", i);
/* lseek() is needed before read() for sysfs attributes */
lseek(fd, 0, SEEK_SET);
bytes = read(fd, data, DEFAULT_READ_SIZE);
if (bytes == -1) {
ERROR("Dummy read() after poll() failed: %s\n",
strerror(errno));
goto out;
}
INFO("(%d) Dummy read() after poll() returns %d bytes\n",
i, bytes);
/* Flush revents */
poll_fds[0].revents = 0;
}
/* We can do dummy write() if enabled */
if ((write_after != -1) && (poll_fds[0].revents &
(POLLOUT|POLLWRNORM|POLLWRBAND))) {
DBG("(%d) Executing dummy write() after poll()\n", i);
/* lseek() is needed before write() for sysfs attributes */
lseek(fd, 0, SEEK_SET);
bytes = write(fd, data, DEFAULT_READ_SIZE);
if (bytes == -1) {
ERROR("Dummy write() after poll() failed: %s\n",
strerror(errno));
goto out;
}
INFO("(%d) Dummy write() after poll() returns %d bytes\n",
i, bytes);
/* Flush revents */
poll_fds[0].revents = 0;
}
/* File errors & status */
if (poll_fds[0].revents & (POLLERR|POLLHUP|POLLNVAL)) {
WARN("(%d) Filedescriptor reports errors after poll()...\n",
i);
}
}
i++;
} while (loop_mode);
out:
close(fd);
DBG("Return value: %d (errno: %s)\n", errno, strerror(errno));
errno == 0 ? exit(EXIT_SUCCESS) : exit(EXIT_FAILURE);
}