-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpev.c
624 lines (474 loc) · 9.94 KB
/
pev.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/* This is free and unencumbered software released into the public domain. */
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "pev.h"
#define PEV_SOCK 1
#define PEV_TIMER 2
#define PEV_SIG 3
struct pev {
struct pev *prev, *next;
int id;
char type;
signed char active;
union {
int sd;
int signo;
struct {
int timeout;
int period;
int gettime;
struct timespec expiry;
};
};
void (*cb)(int, void *);
void (*cb_del)(void *);
void *arg;
};
struct pev *pl;
static int events[2];
static int max_fdnum = -1;
static int id = 1;
static int running;
static int status;
static struct pev *pev_new (int type, void (*cb)(int, void *), void *arg);
static struct pev *pev_find (int type, int signo);
/******************************* SIGNALS ******************************/
static void sig_handler(int signo)
{
char buf[1] = { (char)signo };
while (write(events[1], buf, 1) < 0) {
if (errno != EINTR)
return;
}
}
int pev_sig_add(int signo, void (*cb)(int, void *), void *arg)
{
struct sigaction sa = { 0 };
struct pev *entry;
if (pev_find(PEV_SIG, signo)) {
errno = EEXIST;
return -1;
}
entry = pev_new(PEV_SIG, cb, arg);
if (!entry)
return -1;
sa.sa_handler = sig_handler;
sa.sa_flags = SA_RESTART;
sigaction(signo, &sa, NULL);
entry->signo = signo;
return entry->id;
}
int pev_sig_del(int id)
{
struct pev *entry;
entry = pev_find(PEV_SIG, id);
if (!entry)
return -1;
sigaction(entry->signo, NULL, NULL);
return pev_sock_del(id);
}
int pev_sig_set_cb_del(int id, void (*cb)(void *))
{
struct pev *entry;
entry = pev_find(PEV_SIG, id);
if (!entry) {
errno = ENOENT;
return -1;
}
entry->cb_del = cb;
return 0;
}
/******************************* SOCKETS ******************************/
static int nfds(void)
{
return max_fdnum + 1;
}
static void sock_run(fd_set *fds)
{
struct pev *entry;
int fdmax = 0;
FD_ZERO(fds);
for (entry = pl; entry; entry = entry->next) {
if (entry->type != PEV_SOCK)
continue;
if (entry->sd > fdmax)
fdmax = entry->sd;
FD_SET(entry->sd, fds);
}
if (fdmax)
max_fdnum = fdmax;
}
int pev_sock_add(int sd, void (*cb)(int, void *), void *arg)
{
struct pev *entry;
int rc;
if (sd < 0) {
errno = EINVAL;
return -1;
}
rc = fcntl(sd, F_GETFD);
if (rc == -1)
return -1;
rc = fcntl(sd, F_SETFD, rc | O_CLOEXEC | O_NONBLOCK);
if (rc == -1)
return -1;
entry = pev_new(PEV_SOCK, cb, arg);
if (!entry)
return -1;
entry->sd = sd;
return entry->id;
}
int pev_sock_del(int id)
{
struct pev *entry;
for (entry = pl; entry; entry = entry->next) {
if (entry->id == id) {
/* Mark for deletion and issue a new run */
entry->active = 0;
sig_handler(0);
if (entry->cb_del)
entry->cb_del(entry->arg);
return 0;
}
}
errno = ENOENT;
return -1;
}
int pev_sock_open(int domain, int type, int proto, void (*cb)(int, void *), void *arg)
{
int sd;
if (!cb) {
errno = EINVAL;
return -1;
}
sd = socket(domain, type, proto);
if (sd < 0)
return -1;
if (pev_sock_add(sd, cb, arg) < 0) {
close(sd);
return -1;
}
return sd;
}
int pev_sock_close(int sd)
{
const struct pev *entry;
entry = pev_find(PEV_SOCK, sd);
if (!entry)
return -1;
pev_sock_del(entry->id);
close(sd);
return 0;
}
int pev_sock_set_cb_del(int id, void (*cb)(void *))
{
struct pev *entry;
for (entry = pl; entry; entry = entry->next) {
if (entry->id == id) {
entry->cb_del = cb;
return 0;
}
}
errno = ENOENT;
return -1;
}
/******************************* TIMERS *******************************/
static struct pev *timer_ffs(void)
{
struct pev *entry;
for (entry = pl; entry; entry = entry->next) {
if (entry->type == PEV_TIMER && entry->active > 0)
return entry;
}
return NULL;
}
static struct pev *timer_compare(struct pev *a, struct pev *b)
{
if (b->type != PEV_TIMER || b->active < 1)
return a;
if (a->expiry.tv_sec < b->expiry.tv_sec)
return a;
if (a->expiry.tv_sec == b->expiry.tv_sec &&
a->expiry.tv_nsec <= b->expiry.tv_nsec)
return a;
return b;
}
static int timer_start(const struct timespec *now)
{
struct itimerval it = { 0 };
struct pev *next, *entry;
next = timer_ffs();
if (!next)
return -1;
for (entry = pl; entry; entry = entry->next)
next = timer_compare(next, entry);
it.it_value.tv_sec = next->expiry.tv_sec - now->tv_sec;
it.it_value.tv_usec = (next->expiry.tv_nsec - now->tv_nsec) / 1000;
if (it.it_value.tv_usec < 0) {
it.it_value.tv_sec -= 1;
it.it_value.tv_usec = 1000000 + it.it_value.tv_usec;
}
/* Sanity check resulting value, prevent disabling timer */
if (it.it_value.tv_sec < 0)
it.it_value.tv_sec = 0;
if (it.it_value.tv_sec == 0 && it.it_value.tv_usec < 1)
it.it_value.tv_usec = 1;
return setitimer(ITIMER_REAL, &it, NULL);
}
static int timer_expired(const struct pev *entry, const struct timespec *now)
{
if (entry->type != PEV_TIMER || entry->active < 1)
return 0;
if (entry->expiry.tv_sec < now->tv_sec)
return 1;
if (entry->expiry.tv_sec == now->tv_sec &&
entry->expiry.tv_nsec <= now->tv_nsec)
return 1;
return 0;
}
static void timer_run(int signo, void *arg)
{
struct pev *entry, *next;
struct timespec now;
(void)arg;
clock_gettime(CLOCK_MONOTONIC, &now);
for (entry = pl; entry; entry = next) {
unsigned int sec, usec;
int timeout;
next = entry->next;
if (entry->type != PEV_TIMER)
continue;
if (!timer_expired(entry, &now))
continue;
if (entry->timeout)
timeout = entry->timeout;
else
timeout = entry->period;
if (signo && entry->cb) {
entry->timeout = 0;
entry->gettime = timeout;
entry->cb(entry->id, entry->arg);
entry->gettime = 0;
if (!entry->period && !entry->timeout) {
entry->active = -1;
continue;
}
}
sec = timeout / 1000000;
usec = timeout % 1000000;
entry->expiry.tv_sec = now.tv_sec + sec;
entry->expiry.tv_nsec = now.tv_nsec + (usec * 1000);
if (entry->expiry.tv_nsec > 1000000000) {
entry->expiry.tv_sec++;
entry->expiry.tv_nsec -= 1000000000;
}
}
timer_start(&now);
}
static int timer_init(void)
{
return pev_sig_add(SIGALRM, timer_run, NULL);
}
static int timer_exit(void)
{
struct itimerval it = { 0 };
return setitimer(ITIMER_REAL, &it, NULL);
}
int pev_timer_add(int timeout, int period, void (*cb)(int, void *), void *arg)
{
struct pev *entry;
if (timeout <= 0 && period <= 0) {
errno = EINVAL;
return -1;
}
entry = pev_new(PEV_TIMER, cb, arg);
if (!entry)
return -1;
entry->timeout = timeout;
entry->period = period;
entry->active++;
return entry->id;
}
int pev_timer_del(int id)
{
return pev_sock_del(id);
}
int pev_timer_set(int id, int timeout)
{
struct pev *entry;
for (entry = pl; entry; entry = entry->next) {
if (entry->type != PEV_TIMER)
continue;
if (entry->id != id)
continue;
entry->timeout = timeout;
memset(&entry->expiry, 0, sizeof(entry->expiry));
entry->active = 2;
return 0;
}
errno = ENOENT;
return -1;
}
int pev_timer_get(int id)
{
struct pev *entry;
for (entry = pl; entry; entry = entry->next) {
if (entry->type != PEV_TIMER)
continue;
if (entry->id != id)
continue;
if (entry->gettime)
return entry->gettime;
if (entry->timeout)
return entry->timeout;
return entry->period;
}
errno = ENOENT;
return -1;
}
int pev_timer_set_cb_del(int id, void (*cb)(void *))
{
return pev_sock_set_cb_del(id, cb);
}
/******************************* GENERIC ******************************/
static struct pev *pev_new(int type, void (*cb)(int, void *), void *arg)
{
struct pev *entry;
if (!cb) {
errno = EINVAL;
return NULL;
}
entry = calloc(1, sizeof(*entry));
if (!entry)
return NULL;
entry->id = id++;
entry->type = type;
entry->active = 1;
entry->cb = cb;
entry->arg = arg;
entry->next = pl;
entry->prev = NULL;
if (entry->next)
entry->next->prev = entry;
pl = entry;
return entry;
}
static struct pev *pev_find(int type, int signo)
{
struct pev *entry;
for (entry = pl; entry; entry = entry->next) {
if (entry->type != type)
continue;
if (entry->signo != signo)
continue;
return entry;
}
errno = ENOENT;
return NULL;
}
static void pev_cleanup(void)
{
struct pev *entry, *next, *prev;
for (entry = pl; entry; entry = next) {
next = entry->next;
prev = entry->prev;
if (entry->active)
continue;
if (next)
next->prev = prev;
if (prev)
prev->next = next;
else
pl = next;
free(entry);
}
}
static void pev_event(int sd, void *arg)
{
struct pev *entry;
char signo;
(void)arg;
while (read(sd, &signo, 1) < 0) {
if (errno != EINTR)
return;
}
entry = pev_find(PEV_SIG, signo);
if (!entry)
return;
if (!entry->cb)
return;
entry->cb(entry->signo, entry->arg);
}
int pev_init(void)
{
if (pipe(events))
return -1;
if (pev_sock_add(events[0], pev_event, NULL) < 0)
return -1;
running = 1;
return timer_init();
}
int pev_exit(int rc)
{
struct pev *entry;
pev_sock_close(events[0]);
pev_sock_close(events[1]);
for (entry = pl; entry; entry = entry->next)
entry->active = 0;
running = 0;
status = rc;
return timer_exit();
}
static void pev_check(fd_set *fds)
{
struct pev *entry;
int trestart = 0;
sock_run(fds);
pev_cleanup();
for (entry = pl; entry; entry = entry->next) {
if (entry->type == PEV_TIMER && entry->active > 1) {
entry->active = 1;
trestart = 1;
}
}
if (trestart)
timer_run(0, NULL);
}
int pev_run(void)
{
struct pev *entry, *next;
fd_set fds;
while (running) {
int num;
pev_check(&fds);
errno = 0;
num = select(nfds(), &fds, NULL, NULL, NULL);
if (num <= 0)
continue;
for (entry = pl; entry; entry = next) {
next = entry->next;
if (entry->type != PEV_SOCK)
continue;
if (!FD_ISSET(entry->sd, &fds))
continue;
if (entry->cb)
entry->cb(entry->sd, entry->arg);
}
}
pev_cleanup();
return status;
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/