-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathsd-event.c
More file actions
3541 lines (2661 loc) · 105 KB
/
Copy pathsd-event.c
File metadata and controls
3541 lines (2661 loc) · 105 KB
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
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <sys/epoll.h>
#include <sys/timerfd.h>
#include <sys/wait.h>
#include "sd-daemon.h"
#include "sd-event.h"
#include "sd-id128.h"
#include "alloc-util.h"
#include "event-source.h"
#include "fd-util.h"
#include "fs-util.h"
#include "hashmap.h"
#include "list.h"
#include "macro.h"
#include "memory-util.h"
#include "missing.h"
#include "prioq.h"
#include "process-util.h"
#include "set.h"
#include "signal-util.h"
#include "string-table.h"
#include "string-util.h"
#include "time-util.h"
#define DEFAULT_ACCURACY_USEC (250 * USEC_PER_MSEC)
static const char* const event_source_type_table[_SOURCE_EVENT_SOURCE_TYPE_MAX] = {
[SOURCE_IO] = "io",
[SOURCE_TIME_REALTIME] = "realtime",
[SOURCE_TIME_BOOTTIME] = "bootime",
[SOURCE_TIME_MONOTONIC] = "monotonic",
[SOURCE_TIME_REALTIME_ALARM] = "realtime-alarm",
[SOURCE_TIME_BOOTTIME_ALARM] = "boottime-alarm",
[SOURCE_SIGNAL] = "signal",
[SOURCE_CHILD] = "child",
[SOURCE_DEFER] = "defer",
[SOURCE_POST] = "post",
[SOURCE_EXIT] = "exit",
[SOURCE_WATCHDOG] = "watchdog",
[SOURCE_INOTIFY] = "inotify",
};
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(event_source_type, int);
#define EVENT_SOURCE_IS_TIME(t) IN_SET((t), SOURCE_TIME_REALTIME, SOURCE_TIME_BOOTTIME, SOURCE_TIME_MONOTONIC, SOURCE_TIME_REALTIME_ALARM, SOURCE_TIME_BOOTTIME_ALARM)
struct sd_event {
unsigned n_ref;
int epoll_fd;
int watchdog_fd;
Prioq *pending;
Prioq *prepare;
/* timerfd_create() only supports these five clocks so far. We
* can add support for more clocks when the kernel learns to
* deal with them, too. */
struct clock_data realtime;
struct clock_data boottime;
struct clock_data monotonic;
struct clock_data realtime_alarm;
struct clock_data boottime_alarm;
usec_t perturb;
sd_event_source **signal_sources; /* indexed by signal number */
Hashmap *signal_data; /* indexed by priority */
Hashmap *child_sources;
unsigned n_enabled_child_sources;
Set *post_sources;
Prioq *exit;
Hashmap *inotify_data; /* indexed by priority */
/* A list of inode structures that still have an fd open, that we need to close before the next loop iteration */
LIST_HEAD(struct inode_data, inode_data_to_close);
/* A list of inotify objects that already have events buffered which aren't processed yet */
LIST_HEAD(struct inotify_data, inotify_data_buffered);
pid_t original_pid;
uint64_t iteration;
triple_timestamp timestamp;
int state;
bool exit_requested:1;
bool need_process_child:1;
bool watchdog:1;
bool profile_delays:1;
int exit_code;
pid_t tid;
sd_event **default_event_ptr;
usec_t watchdog_last, watchdog_period;
unsigned n_sources;
LIST_HEAD(sd_event_source, sources);
usec_t last_run, last_log;
unsigned delays[sizeof(usec_t) * 8];
};
static thread_local sd_event *default_event = NULL;
static void source_disconnect(sd_event_source *s);
static void event_gc_inode_data(sd_event *e, struct inode_data *d);
static sd_event *event_resolve(sd_event *e) {
return e == SD_EVENT_DEFAULT ? default_event : e;
}
static int pending_prioq_compare(const void *a, const void *b) {
const sd_event_source *x = a, *y = b;
int r;
assert(x->pending);
assert(y->pending);
/* Enabled ones first */
if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
return -1;
if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
return 1;
/* Lower priority values first */
r = CMP(x->priority, y->priority);
if (r != 0)
return r;
/* Older entries first */
return CMP(x->pending_iteration, y->pending_iteration);
}
static int prepare_prioq_compare(const void *a, const void *b) {
const sd_event_source *x = a, *y = b;
int r;
assert(x->prepare);
assert(y->prepare);
/* Enabled ones first */
if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
return -1;
if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
return 1;
/* Move most recently prepared ones last, so that we can stop
* preparing as soon as we hit one that has already been
* prepared in the current iteration */
r = CMP(x->prepare_iteration, y->prepare_iteration);
if (r != 0)
return r;
/* Lower priority values first */
return CMP(x->priority, y->priority);
}
static int earliest_time_prioq_compare(const void *a, const void *b) {
const sd_event_source *x = a, *y = b;
assert(EVENT_SOURCE_IS_TIME(x->type));
assert(x->type == y->type);
/* Enabled ones first */
if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
return -1;
if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
return 1;
/* Move the pending ones to the end */
if (!x->pending && y->pending)
return -1;
if (x->pending && !y->pending)
return 1;
/* Order by time */
return CMP(x->time.next, y->time.next);
}
static usec_t time_event_source_latest(const sd_event_source *s) {
return usec_add(s->time.next, s->time.accuracy);
}
static int latest_time_prioq_compare(const void *a, const void *b) {
const sd_event_source *x = a, *y = b;
assert(EVENT_SOURCE_IS_TIME(x->type));
assert(x->type == y->type);
/* Enabled ones first */
if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
return -1;
if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
return 1;
/* Move the pending ones to the end */
if (!x->pending && y->pending)
return -1;
if (x->pending && !y->pending)
return 1;
/* Order by time */
return CMP(time_event_source_latest(x), time_event_source_latest(y));
}
static int exit_prioq_compare(const void *a, const void *b) {
const sd_event_source *x = a, *y = b;
assert(x->type == SOURCE_EXIT);
assert(y->type == SOURCE_EXIT);
/* Enabled ones first */
if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
return -1;
if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
return 1;
/* Lower priority values first */
return CMP(x->priority, y->priority);
}
static void free_clock_data(struct clock_data *d) {
assert(d);
assert(d->wakeup == WAKEUP_CLOCK_DATA);
safe_close(d->fd);
prioq_free(d->earliest);
prioq_free(d->latest);
}
static sd_event *event_free(sd_event *e) {
sd_event_source *s;
assert(e);
while ((s = e->sources)) {
assert(s->floating);
source_disconnect(s);
sd_event_source_unref(s);
}
assert(e->n_sources == 0);
if (e->default_event_ptr)
*(e->default_event_ptr) = NULL;
safe_close(e->epoll_fd);
safe_close(e->watchdog_fd);
free_clock_data(&e->realtime);
free_clock_data(&e->boottime);
free_clock_data(&e->monotonic);
free_clock_data(&e->realtime_alarm);
free_clock_data(&e->boottime_alarm);
prioq_free(e->pending);
prioq_free(e->prepare);
prioq_free(e->exit);
free(e->signal_sources);
hashmap_free(e->signal_data);
hashmap_free(e->inotify_data);
hashmap_free(e->child_sources);
set_free(e->post_sources);
return mfree(e);
}
_public_ int sd_event_new(sd_event** ret) {
sd_event *e;
int r;
assert_return(ret, -EINVAL);
e = new(sd_event, 1);
if (!e)
return -ENOMEM;
*e = (sd_event) {
.n_ref = 1,
.epoll_fd = -1,
.watchdog_fd = -1,
.realtime.wakeup = WAKEUP_CLOCK_DATA,
.realtime.fd = -1,
.realtime.next = USEC_INFINITY,
.boottime.wakeup = WAKEUP_CLOCK_DATA,
.boottime.fd = -1,
.boottime.next = USEC_INFINITY,
.monotonic.wakeup = WAKEUP_CLOCK_DATA,
.monotonic.fd = -1,
.monotonic.next = USEC_INFINITY,
.realtime_alarm.wakeup = WAKEUP_CLOCK_DATA,
.realtime_alarm.fd = -1,
.realtime_alarm.next = USEC_INFINITY,
.boottime_alarm.wakeup = WAKEUP_CLOCK_DATA,
.boottime_alarm.fd = -1,
.boottime_alarm.next = USEC_INFINITY,
.perturb = USEC_INFINITY,
.original_pid = getpid_cached(),
};
r = prioq_ensure_allocated(&e->pending, pending_prioq_compare);
if (r < 0)
goto fail;
e->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
if (e->epoll_fd < 0) {
r = -errno;
goto fail;
}
e->epoll_fd = fd_move_above_stdio(e->epoll_fd);
if (secure_getenv("SD_EVENT_PROFILE_DELAYS")) {
log_debug("Event loop profiling enabled. Logarithmic histogram of event loop iterations in the range 2^0 ... 2^63 us will be logged every 5s.");
e->profile_delays = true;
}
*ret = e;
return 0;
fail:
event_free(e);
return r;
}
DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_event, sd_event, event_free);
static bool event_pid_changed(sd_event *e) {
assert(e);
/* We don't support people creating an event loop and keeping
* it around over a fork(). Let's complain. */
return e->original_pid != getpid_cached();
}
static void source_io_unregister(sd_event_source *s) {
int r;
assert(s);
assert(s->type == SOURCE_IO);
if (event_pid_changed(s->event))
return;
if (!s->io.registered)
return;
r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, s->io.fd, NULL);
if (r < 0)
log_debug_errno(errno, "Failed to remove source %s (type %s) from epoll: %m",
strna(s->description), event_source_type_to_string(s->type));
s->io.registered = false;
}
static int source_io_register(
sd_event_source *s,
int enabled,
uint32_t events) {
struct epoll_event ev;
int r;
assert(s);
assert(s->type == SOURCE_IO);
assert(enabled != SD_EVENT_OFF);
ev = (struct epoll_event) {
.events = events | (enabled == SD_EVENT_ONESHOT ? EPOLLONESHOT : 0),
.data.ptr = s,
};
if (s->io.registered)
r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_MOD, s->io.fd, &ev);
else
r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_ADD, s->io.fd, &ev);
if (r < 0)
return -errno;
s->io.registered = true;
return 0;
}
static clockid_t event_source_type_to_clock(EventSourceType t) {
switch (t) {
case SOURCE_TIME_REALTIME:
return CLOCK_REALTIME;
case SOURCE_TIME_BOOTTIME:
return CLOCK_BOOTTIME;
case SOURCE_TIME_MONOTONIC:
return CLOCK_MONOTONIC;
case SOURCE_TIME_REALTIME_ALARM:
return CLOCK_REALTIME_ALARM;
case SOURCE_TIME_BOOTTIME_ALARM:
return CLOCK_BOOTTIME_ALARM;
default:
return (clockid_t) -1;
}
}
static EventSourceType clock_to_event_source_type(clockid_t clock) {
switch (clock) {
case CLOCK_REALTIME:
return SOURCE_TIME_REALTIME;
case CLOCK_BOOTTIME:
return SOURCE_TIME_BOOTTIME;
case CLOCK_MONOTONIC:
return SOURCE_TIME_MONOTONIC;
case CLOCK_REALTIME_ALARM:
return SOURCE_TIME_REALTIME_ALARM;
case CLOCK_BOOTTIME_ALARM:
return SOURCE_TIME_BOOTTIME_ALARM;
default:
return _SOURCE_EVENT_SOURCE_TYPE_INVALID;
}
}
static struct clock_data* event_get_clock_data(sd_event *e, EventSourceType t) {
assert(e);
switch (t) {
case SOURCE_TIME_REALTIME:
return &e->realtime;
case SOURCE_TIME_BOOTTIME:
return &e->boottime;
case SOURCE_TIME_MONOTONIC:
return &e->monotonic;
case SOURCE_TIME_REALTIME_ALARM:
return &e->realtime_alarm;
case SOURCE_TIME_BOOTTIME_ALARM:
return &e->boottime_alarm;
default:
return NULL;
}
}
static void event_free_signal_data(sd_event *e, struct signal_data *d) {
assert(e);
if (!d)
return;
hashmap_remove(e->signal_data, &d->priority);
safe_close(d->fd);
free(d);
}
static int event_make_signal_data(
sd_event *e,
int sig,
struct signal_data **ret) {
struct epoll_event ev;
struct signal_data *d;
bool added = false;
sigset_t ss_copy;
int64_t priority;
int r;
assert(e);
if (event_pid_changed(e))
return -ECHILD;
if (e->signal_sources && e->signal_sources[sig])
priority = e->signal_sources[sig]->priority;
else
priority = SD_EVENT_PRIORITY_NORMAL;
d = hashmap_get(e->signal_data, &priority);
if (d) {
if (sigismember(&d->sigset, sig) > 0) {
if (ret)
*ret = d;
return 0;
}
} else {
r = hashmap_ensure_allocated(&e->signal_data, &uint64_hash_ops);
if (r < 0)
return r;
d = new(struct signal_data, 1);
if (!d)
return -ENOMEM;
*d = (struct signal_data) {
.wakeup = WAKEUP_SIGNAL_DATA,
.fd = -1,
.priority = priority,
};
r = hashmap_put(e->signal_data, &d->priority, d);
if (r < 0) {
free(d);
return r;
}
added = true;
}
ss_copy = d->sigset;
assert_se(sigaddset(&ss_copy, sig) >= 0);
r = signalfd(d->fd, &ss_copy, SFD_NONBLOCK|SFD_CLOEXEC);
if (r < 0) {
r = -errno;
goto fail;
}
d->sigset = ss_copy;
if (d->fd >= 0) {
if (ret)
*ret = d;
return 0;
}
d->fd = fd_move_above_stdio(r);
ev = (struct epoll_event) {
.events = EPOLLIN,
.data.ptr = d,
};
r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, d->fd, &ev);
if (r < 0) {
r = -errno;
goto fail;
}
if (ret)
*ret = d;
return 0;
fail:
if (added)
event_free_signal_data(e, d);
return r;
}
static void event_unmask_signal_data(sd_event *e, struct signal_data *d, int sig) {
assert(e);
assert(d);
/* Turns off the specified signal in the signal data
* object. If the signal mask of the object becomes empty that
* way removes it. */
if (sigismember(&d->sigset, sig) == 0)
return;
assert_se(sigdelset(&d->sigset, sig) >= 0);
if (sigisemptyset(&d->sigset)) {
/* If all the mask is all-zero we can get rid of the structure */
event_free_signal_data(e, d);
return;
}
assert(d->fd >= 0);
if (signalfd(d->fd, &d->sigset, SFD_NONBLOCK|SFD_CLOEXEC) < 0)
log_debug_errno(errno, "Failed to unset signal bit, ignoring: %m");
}
static void event_gc_signal_data(sd_event *e, const int64_t *priority, int sig) {
struct signal_data *d;
static const int64_t zero_priority = 0;
assert(e);
/* Rechecks if the specified signal is still something we are
* interested in. If not, we'll unmask it, and possibly drop
* the signalfd for it. */
if (sig == SIGCHLD &&
e->n_enabled_child_sources > 0)
return;
if (e->signal_sources &&
e->signal_sources[sig] &&
e->signal_sources[sig]->enabled != SD_EVENT_OFF)
return;
/*
* The specified signal might be enabled in three different queues:
*
* 1) the one that belongs to the priority passed (if it is non-NULL)
* 2) the one that belongs to the priority of the event source of the signal (if there is one)
* 3) the 0 priority (to cover the SIGCHLD case)
*
* Hence, let's remove it from all three here.
*/
if (priority) {
d = hashmap_get(e->signal_data, priority);
if (d)
event_unmask_signal_data(e, d, sig);
}
if (e->signal_sources && e->signal_sources[sig]) {
d = hashmap_get(e->signal_data, &e->signal_sources[sig]->priority);
if (d)
event_unmask_signal_data(e, d, sig);
}
d = hashmap_get(e->signal_data, &zero_priority);
if (d)
event_unmask_signal_data(e, d, sig);
}
static void source_disconnect(sd_event_source *s) {
sd_event *event;
assert(s);
if (!s->event)
return;
assert(s->event->n_sources > 0);
switch (s->type) {
case SOURCE_IO:
if (s->io.fd >= 0)
source_io_unregister(s);
break;
case SOURCE_TIME_REALTIME:
case SOURCE_TIME_BOOTTIME:
case SOURCE_TIME_MONOTONIC:
case SOURCE_TIME_REALTIME_ALARM:
case SOURCE_TIME_BOOTTIME_ALARM: {
struct clock_data *d;
d = event_get_clock_data(s->event, s->type);
assert(d);
prioq_remove(d->earliest, s, &s->time.earliest_index);
prioq_remove(d->latest, s, &s->time.latest_index);
d->needs_rearm = true;
break;
}
case SOURCE_SIGNAL:
if (s->signal.sig > 0) {
if (s->event->signal_sources)
s->event->signal_sources[s->signal.sig] = NULL;
event_gc_signal_data(s->event, &s->priority, s->signal.sig);
}
break;
case SOURCE_CHILD:
if (s->child.pid > 0) {
if (s->enabled != SD_EVENT_OFF) {
assert(s->event->n_enabled_child_sources > 0);
s->event->n_enabled_child_sources--;
}
(void) hashmap_remove(s->event->child_sources, PID_TO_PTR(s->child.pid));
event_gc_signal_data(s->event, &s->priority, SIGCHLD);
}
break;
case SOURCE_DEFER:
/* nothing */
break;
case SOURCE_POST:
set_remove(s->event->post_sources, s);
break;
case SOURCE_EXIT:
prioq_remove(s->event->exit, s, &s->exit.prioq_index);
break;
case SOURCE_INOTIFY: {
struct inode_data *inode_data;
inode_data = s->inotify.inode_data;
if (inode_data) {
struct inotify_data *inotify_data;
assert_se(inotify_data = inode_data->inotify_data);
/* Detach this event source from the inode object */
LIST_REMOVE(inotify.by_inode_data, inode_data->event_sources, s);
s->inotify.inode_data = NULL;
if (s->pending) {
assert(inotify_data->n_pending > 0);
inotify_data->n_pending--;
}
/* Note that we don't reduce the inotify mask for the watch descriptor here if the inode is
* continued to being watched. That's because inotify doesn't really have an API for that: we
* can only change watch masks with access to the original inode either by fd or by path. But
* paths aren't stable, and keeping an O_PATH fd open all the time would mean wasting an fd
* continuously and keeping the mount busy which we can't really do. We could reconstruct the
* original inode from /proc/self/fdinfo/$INOTIFY_FD (as all watch descriptors are listed
* there), but given the need for open_by_handle_at() which is privileged and not universally
* available this would be quite an incomplete solution. Hence we go the other way, leave the
* mask set, even if it is not minimized now, and ignore all events we aren't interested in
* anymore after reception. Yes, this sucks, but … Linux … */
/* Maybe release the inode data (and its inotify) */
event_gc_inode_data(s->event, inode_data);
}
break;
}
default:
assert_not_reached("Wut? I shouldn't exist.");
}
if (s->pending)
prioq_remove(s->event->pending, s, &s->pending_index);
if (s->prepare)
prioq_remove(s->event->prepare, s, &s->prepare_index);
event = s->event;
s->type = _SOURCE_EVENT_SOURCE_TYPE_INVALID;
s->event = NULL;
LIST_REMOVE(sources, event->sources, s);
event->n_sources--;
if (!s->floating)
sd_event_unref(event);
}
static void source_free(sd_event_source *s) {
assert(s);
source_disconnect(s);
if (s->type == SOURCE_IO && s->io.owned)
s->io.fd = safe_close(s->io.fd);
if (s->destroy_callback)
s->destroy_callback(s->userdata);
free(s->description);
free(s);
}
DEFINE_TRIVIAL_CLEANUP_FUNC(sd_event_source*, source_free);
static int source_set_pending(sd_event_source *s, bool b) {
int r;
assert(s);
assert(s->type != SOURCE_EXIT);
if (s->pending == b)
return 0;
s->pending = b;
if (b) {
s->pending_iteration = s->event->iteration;
r = prioq_put(s->event->pending, s, &s->pending_index);
if (r < 0) {
s->pending = false;
return r;
}
} else
assert_se(prioq_remove(s->event->pending, s, &s->pending_index));
if (EVENT_SOURCE_IS_TIME(s->type)) {
struct clock_data *d;
d = event_get_clock_data(s->event, s->type);
assert(d);
prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
prioq_reshuffle(d->latest, s, &s->time.latest_index);
d->needs_rearm = true;
}
if (s->type == SOURCE_SIGNAL && !b) {
struct signal_data *d;
d = hashmap_get(s->event->signal_data, &s->priority);
if (d && d->current == s)
d->current = NULL;
}
if (s->type == SOURCE_INOTIFY) {
assert(s->inotify.inode_data);
assert(s->inotify.inode_data->inotify_data);
if (b)
s->inotify.inode_data->inotify_data->n_pending ++;
else {
assert(s->inotify.inode_data->inotify_data->n_pending > 0);
s->inotify.inode_data->inotify_data->n_pending --;
}
}
return 0;
}
static sd_event_source *source_new(sd_event *e, bool floating, EventSourceType type) {
sd_event_source *s;
assert(e);
s = new(sd_event_source, 1);
if (!s)
return NULL;
*s = (struct sd_event_source) {
.n_ref = 1,
.event = e,
.floating = floating,
.type = type,
.pending_index = PRIOQ_IDX_NULL,
.prepare_index = PRIOQ_IDX_NULL,
};
if (!floating)
sd_event_ref(e);
LIST_PREPEND(sources, e->sources, s);
e->n_sources++;
return s;
}
_public_ int sd_event_add_io(
sd_event *e,
sd_event_source **ret,
int fd,
uint32_t events,
sd_event_io_handler_t callback,
void *userdata) {
_cleanup_(source_freep) sd_event_source *s = NULL;
int r;
assert_return(e, -EINVAL);
assert_return(e = event_resolve(e), -ENOPKG);
assert_return(fd >= 0, -EBADF);
assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
assert_return(callback, -EINVAL);
assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_pid_changed(e), -ECHILD);
s = source_new(e, !ret, SOURCE_IO);
if (!s)
return -ENOMEM;
s->wakeup = WAKEUP_EVENT_SOURCE;
s->io.fd = fd;
s->io.events = events;
s->io.callback = callback;
s->userdata = userdata;
s->enabled = SD_EVENT_ON;
r = source_io_register(s, s->enabled, events);
if (r < 0)
return r;
if (ret)
*ret = s;
TAKE_PTR(s);
return 0;
}
static void initialize_perturb(sd_event *e) {
sd_id128_t bootid = {};
/* When we sleep for longer, we try to realign the wakeup to
the same time within each minute/second/250ms, so that
events all across the system can be coalesced into a single
CPU wakeup. However, let's take some system-specific
randomness for this value, so that in a network of systems
with synced clocks timer events are distributed a
bit. Here, we calculate a perturbation usec offset from the
boot ID. */
if (_likely_(e->perturb != USEC_INFINITY))
return;
if (sd_id128_get_boot(&bootid) >= 0)
e->perturb = (bootid.qwords[0] ^ bootid.qwords[1]) % USEC_PER_MINUTE;
}
static int event_setup_timer_fd(
sd_event *e,
struct clock_data *d,
clockid_t clock) {
struct epoll_event ev;
int r, fd;
assert(e);
assert(d);
if (_likely_(d->fd >= 0))
return 0;
fd = timerfd_create(clock, TFD_NONBLOCK|TFD_CLOEXEC);
if (fd < 0)
return -errno;
fd = fd_move_above_stdio(fd);
ev = (struct epoll_event) {
.events = EPOLLIN,
.data.ptr = d,
};
r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, fd, &ev);
if (r < 0) {
safe_close(fd);
return -errno;
}
d->fd = fd;
return 0;
}
static int time_exit_callback(sd_event_source *s, uint64_t usec, void *userdata) {
assert(s);
return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
}
_public_ int sd_event_add_time(
sd_event *e,
sd_event_source **ret,
clockid_t clock,
uint64_t usec,
uint64_t accuracy,
sd_event_time_handler_t callback,
void *userdata) {
EventSourceType type;
_cleanup_(source_freep) sd_event_source *s = NULL;
struct clock_data *d;
int r;
assert_return(e, -EINVAL);
assert_return(e = event_resolve(e), -ENOPKG);
assert_return(accuracy != (uint64_t) -1, -EINVAL);
assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_pid_changed(e), -ECHILD);
if (!clock_supported(clock)) /* Checks whether the kernel supports the clock */
return -EOPNOTSUPP;
type = clock_to_event_source_type(clock); /* checks whether sd-event supports this clock */