-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathev.c
2323 lines (1891 loc) · 48 KB
/
ev.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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* libev event processing core, watcher management
*
* Copyright (c) 2007 Marc Alexander Lehmann <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef __cplusplus
extern "C" {
#endif
#ifndef EV_STANDALONE
# ifdef EV_CONFIG_H
# include EV_CONFIG_H
# else
# include "config.h"
# endif
# if HAVE_CLOCK_GETTIME
# ifndef EV_USE_MONOTONIC
# define EV_USE_MONOTONIC 1
# endif
# ifndef EV_USE_REALTIME
# define EV_USE_REALTIME 1
# endif
# else
# ifndef EV_USE_MONOTONIC
# define EV_USE_MONOTONIC 0
# endif
# ifndef EV_USE_REALTIME
# define EV_USE_REALTIME 0
# endif
# endif
# ifndef EV_USE_SELECT
# if HAVE_SELECT && HAVE_SYS_SELECT_H
# define EV_USE_SELECT 1
# else
# define EV_USE_SELECT 0
# endif
# endif
# ifndef EV_USE_POLL
# if HAVE_POLL && HAVE_POLL_H
# define EV_USE_POLL 1
# else
# define EV_USE_POLL 0
# endif
# endif
# ifndef EV_USE_EPOLL
# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
# define EV_USE_EPOLL 1
# else
# define EV_USE_EPOLL 0
# endif
# endif
# ifndef EV_USE_KQUEUE
# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H
# define EV_USE_KQUEUE 1
# else
# define EV_USE_KQUEUE 0
# endif
# endif
# ifndef EV_USE_PORT
# if HAVE_PORT_H && HAVE_PORT_CREATE
# define EV_USE_PORT 1
# else
# define EV_USE_PORT 0
# endif
# endif
# ifndef EV_USE_INOTIFY
# if HAVE_INOTIFY_INIT && HAVE_SYS_INOTIFY_H
# define EV_USE_INOTIFY 1
# else
# define EV_USE_INOTIFY 0
# endif
# endif
#endif
#include <math.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stddef.h>
#include <stdio.h>
#include <assert.h>
#include <errno.h>
#include <sys/types.h>
#include <time.h>
#include <signal.h>
#ifdef EV_H
# include EV_H
#else
# include "ev.h"
#endif
#ifndef _WIN32
# include <sys/time.h>
# include <sys/wait.h>
# include <unistd.h>
#else
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# ifndef EV_SELECT_IS_WINSOCKET
# define EV_SELECT_IS_WINSOCKET 1
# endif
#endif
/**/
#ifndef EV_USE_MONOTONIC
# define EV_USE_MONOTONIC 0
#endif
#ifndef EV_USE_REALTIME
# define EV_USE_REALTIME 0
#endif
#ifndef EV_USE_SELECT
# define EV_USE_SELECT 1
#endif
#ifndef EV_USE_POLL
# ifdef _WIN32
# define EV_USE_POLL 0
# else
# define EV_USE_POLL 1
# endif
#endif
#ifndef EV_USE_EPOLL
# define EV_USE_EPOLL 0
#endif
#ifndef EV_USE_KQUEUE
# define EV_USE_KQUEUE 0
#endif
#ifndef EV_USE_PORT
# define EV_USE_PORT 0
#endif
#ifndef EV_USE_INOTIFY
# define EV_USE_INOTIFY 0
#endif
#ifndef EV_PID_HASHSIZE
# if EV_MINIMAL
# define EV_PID_HASHSIZE 1
# else
# define EV_PID_HASHSIZE 16
# endif
#endif
#ifndef EV_INOTIFY_HASHSIZE
# if EV_MINIMAL
# define EV_INOTIFY_HASHSIZE 1
# else
# define EV_INOTIFY_HASHSIZE 16
# endif
#endif
/**/
#ifndef CLOCK_MONOTONIC
# undef EV_USE_MONOTONIC
# define EV_USE_MONOTONIC 0
#endif
#ifndef CLOCK_REALTIME
# undef EV_USE_REALTIME
# define EV_USE_REALTIME 0
#endif
#if !EV_STAT_ENABLE
# undef EV_USE_INOTIFY
# define EV_USE_INOTIFY 0
#endif
#if EV_USE_INOTIFY
# include <sys/inotify.h>
#endif
#if EV_SELECT_IS_WINSOCKET
# include <winsock.h>
#endif
/**/
/*
* This is used to avoid floating point rounding problems.
* It is added to ev_rt_now when scheduling periodics
* to ensure progress, time-wise, even when rounding
* errors are against us.
* This value is good at least till the year 4000.
* Better solutions welcome.
*/
#define TIME_EPSILON 0.0001220703125 /* 1/8192 */
#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds, TODO */
#if __GNUC__ >= 4
# define expect(expr,value) __builtin_expect ((expr),(value))
# define noinline __attribute__ ((noinline))
#else
# define expect(expr,value) (expr)
# define noinline
# if __STDC_VERSION__ < 199901L
# define inline
# endif
#endif
#define expect_false(expr) expect ((expr) != 0, 0)
#define expect_true(expr) expect ((expr) != 0, 1)
#define inline_size static inline
#if EV_MINIMAL
# define inline_speed static noinline
#else
# define inline_speed static inline
#endif
#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
#define ABSPRI(w) (((W)w)->priority - EV_MINPRI)
#define EMPTY /* required for microsofts broken pseudo-c compiler */
#define EMPTY2(a,b) /* used to suppress some warnings */
typedef ev_watcher *W;
typedef ev_watcher_list *WL;
typedef ev_watcher_time *WT;
static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
#ifdef _WIN32
# include "ev_win32.c"
#endif
/*****************************************************************************/
static void (*syserr_cb)(const char *msg);
void
ev_set_syserr_cb (void (*cb)(const char *msg))
{
syserr_cb = cb;
}
static void noinline
syserr (const char *msg)
{
if (!msg)
msg = "(libev) system error";
if (syserr_cb)
syserr_cb (msg);
else
{
perror (msg);
abort ();
}
}
static void *(*alloc)(void *ptr, long size);
void
ev_set_allocator (void *(*cb)(void *ptr, long size))
{
alloc = cb;
}
inline_speed void *
ev_realloc (void *ptr, long size)
{
ptr = alloc ? alloc (ptr, size) : realloc (ptr, size);
if (!ptr && size)
{
fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
abort ();
}
return ptr;
}
#define ev_malloc(size) ev_realloc (0, (size))
#define ev_free(ptr) ev_realloc ((ptr), 0)
/*****************************************************************************/
typedef struct
{
WL head;
unsigned char events;
unsigned char reify;
#if EV_SELECT_IS_WINSOCKET
SOCKET handle;
#endif
} ANFD;
typedef struct
{
W w;
int events;
} ANPENDING;
#if EV_USE_INOTIFY
typedef struct
{
WL head;
} ANFS;
#endif
#if EV_MULTIPLICITY
struct ev_loop
{
ev_tstamp ev_rt_now;
#define ev_rt_now ((loop)->ev_rt_now)
#define VAR(name,decl) decl;
#include "ev_vars.h"
#undef VAR
};
#include "ev_wrap.h"
static struct ev_loop default_loop_struct;
struct ev_loop *ev_default_loop_ptr;
#else
ev_tstamp ev_rt_now;
#define VAR(name,decl) static decl;
#include "ev_vars.h"
#undef VAR
static int ev_default_loop_ptr;
#endif
/*****************************************************************************/
ev_tstamp
ev_time (void)
{
#if EV_USE_REALTIME
struct timespec ts;
clock_gettime (CLOCK_REALTIME, &ts);
return ts.tv_sec + ts.tv_nsec * 1e-9;
#else
struct timeval tv;
gettimeofday (&tv, 0);
return tv.tv_sec + tv.tv_usec * 1e-6;
#endif
}
ev_tstamp inline_size
get_clock (void)
{
#if EV_USE_MONOTONIC
if (expect_true (have_monotonic))
{
struct timespec ts;
clock_gettime (CLOCK_MONOTONIC, &ts);
return ts.tv_sec + ts.tv_nsec * 1e-9;
}
#endif
return ev_time ();
}
#if EV_MULTIPLICITY
ev_tstamp
ev_now (EV_P)
{
return ev_rt_now;
}
#endif
int inline_size
array_nextsize (int elem, int cur, int cnt)
{
int ncur = cur + 1;
do
ncur <<= 1;
while (cnt > ncur);
/* if size > 4096, round to 4096 - 4 * longs to accomodate malloc overhead */
if (elem * ncur > 4096)
{
ncur *= elem;
ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095;
ncur = ncur - sizeof (void *) * 4;
ncur /= elem;
}
return ncur;
}
static noinline void *
array_realloc (int elem, void *base, int *cur, int cnt)
{
*cur = array_nextsize (elem, *cur, cnt);
return ev_realloc (base, elem * *cur);
}
#define array_needsize(type,base,cur,cnt,init) \
if (expect_false ((cnt) > (cur))) \
{ \
int ocur_ = (cur); \
(base) = (type *)array_realloc \
(sizeof (type), (base), &(cur), (cnt)); \
init ((base) + (ocur_), (cur) - ocur_); \
}
#if 0
#define array_slim(type,stem) \
if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
{ \
stem ## max = array_roundsize (stem ## cnt >> 1); \
base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
}
#endif
#define array_free(stem, idx) \
ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
/*****************************************************************************/
void noinline
ev_feed_event (EV_P_ void *w, int revents)
{
W w_ = (W)w;
int pri = ABSPRI (w_);
if (expect_false (w_->pending))
pendings [pri][w_->pending - 1].events |= revents;
else
{
w_->pending = ++pendingcnt [pri];
array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2);
pendings [pri][w_->pending - 1].w = w_;
pendings [pri][w_->pending - 1].events = revents;
}
}
void inline_speed
queue_events (EV_P_ W *events, int eventcnt, int type)
{
int i;
for (i = 0; i < eventcnt; ++i)
ev_feed_event (EV_A_ events [i], type);
}
/*****************************************************************************/
void inline_size
anfds_init (ANFD *base, int count)
{
while (count--)
{
base->head = 0;
base->events = EV_NONE;
base->reify = 0;
++base;
}
}
void inline_speed
fd_event (EV_P_ int fd, int revents)
{
ANFD *anfd = anfds + fd;
ev_io *w;
for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
{
int ev = w->events & revents;
if (ev)
ev_feed_event (EV_A_ (W)w, ev);
}
}
void
ev_feed_fd_event (EV_P_ int fd, int revents)
{
if (fd >= 0 && fd < anfdmax)
fd_event (EV_A_ fd, revents);
}
void inline_size
fd_reify (EV_P)
{
int i;
for (i = 0; i < fdchangecnt; ++i)
{
int fd = fdchanges [i];
ANFD *anfd = anfds + fd;
ev_io *w;
unsigned char events = 0;
for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
events |= (unsigned char)w->events;
#if EV_SELECT_IS_WINSOCKET
if (events)
{
unsigned long argp;
anfd->handle = _get_osfhandle (fd);
assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0));
}
#endif
{
unsigned char o_events = anfd->events;
unsigned char o_reify = anfd->reify;
anfd->reify = 0;
anfd->events = events;
if (o_events != events || o_reify & EV_IOFDSET)
backend_modify (EV_A_ fd, o_events, events);
}
}
fdchangecnt = 0;
}
void inline_size
fd_change (EV_P_ int fd, int flags)
{
unsigned char reify = anfds [fd].reify;
anfds [fd].reify |= flags;
if (expect_true (!reify))
{
++fdchangecnt;
array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
fdchanges [fdchangecnt - 1] = fd;
}
}
void inline_speed
fd_kill (EV_P_ int fd)
{
ev_io *w;
while ((w = (ev_io *)anfds [fd].head))
{
ev_io_stop (EV_A_ w);
ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
}
}
int inline_size
fd_valid (int fd)
{
#ifdef _WIN32
return _get_osfhandle (fd) != -1;
#else
return fcntl (fd, F_GETFD) != -1;
#endif
}
/* called on EBADF to verify fds */
static void noinline
fd_ebadf (EV_P)
{
int fd;
for (fd = 0; fd < anfdmax; ++fd)
if (anfds [fd].events)
if (!fd_valid (fd) == -1 && errno == EBADF)
fd_kill (EV_A_ fd);
}
/* called on ENOMEM in select/poll to kill some fds and retry */
static void noinline
fd_enomem (EV_P)
{
int fd;
for (fd = anfdmax; fd--; )
if (anfds [fd].events)
{
fd_kill (EV_A_ fd);
return;
}
}
/* usually called after fork if backend needs to re-arm all fds from scratch */
static void noinline
fd_rearm_all (EV_P)
{
int fd;
for (fd = 0; fd < anfdmax; ++fd)
if (anfds [fd].events)
{
anfds [fd].events = 0;
fd_change (EV_A_ fd, EV_IOFDSET | 1);
}
}
/*****************************************************************************/
void inline_speed
upheap (WT *heap, int k)
{
WT w = heap [k];
while (k)
{
int p = (k - 1) >> 1;
if (heap [p]->at <= w->at)
break;
heap [k] = heap [p];
((W)heap [k])->active = k + 1;
k = p;
}
heap [k] = w;
((W)heap [k])->active = k + 1;
}
void inline_speed
downheap (WT *heap, int N, int k)
{
WT w = heap [k];
for (;;)
{
int c = (k << 1) + 1;
if (c >= N)
break;
c += c + 1 < N && heap [c]->at > heap [c + 1]->at
? 1 : 0;
if (w->at <= heap [c]->at)
break;
heap [k] = heap [c];
((W)heap [k])->active = k + 1;
k = c;
}
heap [k] = w;
((W)heap [k])->active = k + 1;
}
void inline_size
adjustheap (WT *heap, int N, int k)
{
upheap (heap, k);
downheap (heap, N, k);
}
/*****************************************************************************/
typedef struct
{
WL head;
sig_atomic_t volatile gotsig;
} ANSIG;
static ANSIG *signals;
static int signalmax;
static int sigpipe [2];
static sig_atomic_t volatile gotsig;
static ev_io sigev;
void inline_size
signals_init (ANSIG *base, int count)
{
while (count--)
{
base->head = 0;
base->gotsig = 0;
++base;
}
}
static void
sighandler (int signum)
{
#if _WIN32
signal (signum, sighandler);
#endif
signals [signum - 1].gotsig = 1;
if (!gotsig)
{
int old_errno = errno;
gotsig = 1;
write (sigpipe [1], &signum, 1);
errno = old_errno;
}
}
void noinline
ev_feed_signal_event (EV_P_ int signum)
{
WL w;
#if EV_MULTIPLICITY
assert (("feeding signal events is only supported in the default loop", loop == ev_default_loop_ptr));
#endif
--signum;
if (signum < 0 || signum >= signalmax)
return;
signals [signum].gotsig = 0;
for (w = signals [signum].head; w; w = w->next)
ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
}
static void
sigcb (EV_P_ ev_io *iow, int revents)
{
int signum;
read (sigpipe [0], &revents, 1);
gotsig = 0;
for (signum = signalmax; signum--; )
if (signals [signum].gotsig)
ev_feed_signal_event (EV_A_ signum + 1);
}
void inline_speed
fd_intern (int fd)
{
#ifdef _WIN32
int arg = 1;
ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
#else
fcntl (fd, F_SETFD, FD_CLOEXEC);
fcntl (fd, F_SETFL, O_NONBLOCK);
#endif
}
static void noinline
siginit (EV_P)
{
fd_intern (sigpipe [0]);
fd_intern (sigpipe [1]);
ev_io_set (&sigev, sigpipe [0], EV_READ);
ev_io_start (EV_A_ &sigev);
ev_unref (EV_A); /* child watcher should not keep loop alive */
}
/*****************************************************************************/
static WL childs [EV_PID_HASHSIZE];
#ifndef _WIN32
static ev_signal childev;
void inline_speed
child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status)
{
ev_child *w;
for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
if (w->pid == pid || !w->pid)
{
ev_set_priority (w, ev_priority (sw)); /* need to do it *now* */
w->rpid = pid;
w->rstatus = status;
ev_feed_event (EV_A_ (W)w, EV_CHILD);
}
}
#ifndef WCONTINUED
# define WCONTINUED 0
#endif
static void
childcb (EV_P_ ev_signal *sw, int revents)
{
int pid, status;
/* some systems define WCONTINUED but then fail to support it (linux 2.4) */
if (0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
if (!WCONTINUED
|| errno != EINVAL
|| 0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED)))
return;
/* make sure we are called again until all childs have been reaped */
/* we need to do it this way so that the callback gets called before we continue */
ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
child_reap (EV_A_ sw, pid, pid, status);
if (EV_PID_HASHSIZE > 1)
child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */
}
#endif
/*****************************************************************************/
#if EV_USE_PORT
# include "ev_port.c"
#endif
#if EV_USE_KQUEUE
# include "ev_kqueue.c"
#endif
#if EV_USE_EPOLL
# include "ev_epoll.c"
#endif
#if EV_USE_POLL
# include "ev_poll.c"
#endif
#if EV_USE_SELECT
# include "ev_select.c"
#endif
int
ev_version_major (void)
{
return EV_VERSION_MAJOR;
}
int
ev_version_minor (void)
{
return EV_VERSION_MINOR;
}
/* return true if we are running with elevated privileges and should ignore env variables */
int inline_size
enable_secure (void)
{
#ifdef _WIN32
return 0;
#else
return getuid () != geteuid ()
|| getgid () != getegid ();
#endif
}
unsigned int
ev_supported_backends (void)
{
unsigned int flags = 0;
if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE;
if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL;
if (EV_USE_POLL ) flags |= EVBACKEND_POLL;
if (EV_USE_SELECT) flags |= EVBACKEND_SELECT;
return flags;
}
unsigned int
ev_recommended_backends (void)
{
unsigned int flags = ev_supported_backends ();
#ifndef __NetBSD__
/* kqueue is borked on everything but netbsd apparently */
/* it usually doesn't work correctly on anything but sockets and pipes */
flags &= ~EVBACKEND_KQUEUE;
#endif
#ifdef __APPLE__
// flags &= ~EVBACKEND_KQUEUE; for documentation
flags &= ~EVBACKEND_POLL;
#endif
return flags;
}
unsigned int
ev_embeddable_backends (void)
{
return EVBACKEND_EPOLL
| EVBACKEND_KQUEUE
| EVBACKEND_PORT;
}
unsigned int
ev_backend (EV_P)
{
return backend;
}
unsigned int
ev_loop_count (EV_P)
{
return loop_count;
}
static void noinline
loop_init (EV_P_ unsigned int flags)
{
if (!backend)
{
#if EV_USE_MONOTONIC
{
struct timespec ts;
if (!clock_gettime (CLOCK_MONOTONIC, &ts))
have_monotonic = 1;
}
#endif
ev_rt_now = ev_time ();
mn_now = get_clock ();
now_floor = mn_now;
rtmn_diff = ev_rt_now - mn_now;
/* pid check not overridable via env */
#ifndef _WIN32
if (flags & EVFLAG_FORKCHECK)
curpid = getpid ();
#endif
if (!(flags & EVFLAG_NOENV)
&& !enable_secure ()
&& getenv ("LIBEV_FLAGS"))
flags = atoi (getenv ("LIBEV_FLAGS"));
if (!(flags & 0x0000ffffUL))
flags |= ev_recommended_backends ();
backend = 0;
backend_fd = -1;
#if EV_USE_INOTIFY
fs_fd = -2;
#endif
#if EV_USE_PORT
if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
#endif
#if EV_USE_KQUEUE
if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags);
#endif
#if EV_USE_EPOLL
if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags);
#endif
#if EV_USE_POLL
if (!backend && (flags & EVBACKEND_POLL )) backend = poll_init (EV_A_ flags);
#endif
#if EV_USE_SELECT
if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags);
#endif