]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-event/sd-event.c
util-lib: split out allocation calls into alloc-util.[ch]
[thirdparty/systemd.git] / src / libsystemd / sd-event / sd-event.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2013 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <sys/epoll.h>
23 #include <sys/timerfd.h>
24 #include <sys/wait.h>
25
26 #include "sd-daemon.h"
27 #include "sd-event.h"
28 #include "sd-id128.h"
29
30 #include "alloc-util.h"
31 #include "fd-util.h"
32 #include "hashmap.h"
33 #include "list.h"
34 #include "macro.h"
35 #include "missing.h"
36 #include "prioq.h"
37 #include "set.h"
38 #include "signal-util.h"
39 #include "string-util.h"
40 #include "time-util.h"
41 #include "util.h"
42
43 #define DEFAULT_ACCURACY_USEC (250 * USEC_PER_MSEC)
44
45 typedef enum EventSourceType {
46 SOURCE_IO,
47 SOURCE_TIME_REALTIME,
48 SOURCE_TIME_BOOTTIME,
49 SOURCE_TIME_MONOTONIC,
50 SOURCE_TIME_REALTIME_ALARM,
51 SOURCE_TIME_BOOTTIME_ALARM,
52 SOURCE_SIGNAL,
53 SOURCE_CHILD,
54 SOURCE_DEFER,
55 SOURCE_POST,
56 SOURCE_EXIT,
57 SOURCE_WATCHDOG,
58 _SOURCE_EVENT_SOURCE_TYPE_MAX,
59 _SOURCE_EVENT_SOURCE_TYPE_INVALID = -1
60 } EventSourceType;
61
62 /* All objects we use in epoll events start with this value, so that
63 * we know how to dispatch it */
64 typedef enum WakeupType {
65 WAKEUP_NONE,
66 WAKEUP_EVENT_SOURCE,
67 WAKEUP_CLOCK_DATA,
68 WAKEUP_SIGNAL_DATA,
69 _WAKEUP_TYPE_MAX,
70 _WAKEUP_TYPE_INVALID = -1,
71 } WakeupType;
72
73 #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)
74
75 struct sd_event_source {
76 WakeupType wakeup;
77
78 unsigned n_ref;
79
80 sd_event *event;
81 void *userdata;
82 sd_event_handler_t prepare;
83
84 char *description;
85
86 EventSourceType type:5;
87 int enabled:3;
88 bool pending:1;
89 bool dispatching:1;
90 bool floating:1;
91
92 int64_t priority;
93 unsigned pending_index;
94 unsigned prepare_index;
95 unsigned pending_iteration;
96 unsigned prepare_iteration;
97
98 LIST_FIELDS(sd_event_source, sources);
99
100 union {
101 struct {
102 sd_event_io_handler_t callback;
103 int fd;
104 uint32_t events;
105 uint32_t revents;
106 bool registered:1;
107 } io;
108 struct {
109 sd_event_time_handler_t callback;
110 usec_t next, accuracy;
111 unsigned earliest_index;
112 unsigned latest_index;
113 } time;
114 struct {
115 sd_event_signal_handler_t callback;
116 struct signalfd_siginfo siginfo;
117 int sig;
118 } signal;
119 struct {
120 sd_event_child_handler_t callback;
121 siginfo_t siginfo;
122 pid_t pid;
123 int options;
124 } child;
125 struct {
126 sd_event_handler_t callback;
127 } defer;
128 struct {
129 sd_event_handler_t callback;
130 } post;
131 struct {
132 sd_event_handler_t callback;
133 unsigned prioq_index;
134 } exit;
135 };
136 };
137
138 struct clock_data {
139 WakeupType wakeup;
140 int fd;
141
142 /* For all clocks we maintain two priority queues each, one
143 * ordered for the earliest times the events may be
144 * dispatched, and one ordered by the latest times they must
145 * have been dispatched. The range between the top entries in
146 * the two prioqs is the time window we can freely schedule
147 * wakeups in */
148
149 Prioq *earliest;
150 Prioq *latest;
151 usec_t next;
152
153 bool needs_rearm:1;
154 };
155
156 struct signal_data {
157 WakeupType wakeup;
158
159 /* For each priority we maintain one signal fd, so that we
160 * only have to dequeue a single event per priority at a
161 * time. */
162
163 int fd;
164 int64_t priority;
165 sigset_t sigset;
166 sd_event_source *current;
167 };
168
169 struct sd_event {
170 unsigned n_ref;
171
172 int epoll_fd;
173 int watchdog_fd;
174
175 Prioq *pending;
176 Prioq *prepare;
177
178 /* timerfd_create() only supports these five clocks so far. We
179 * can add support for more clocks when the kernel learns to
180 * deal with them, too. */
181 struct clock_data realtime;
182 struct clock_data boottime;
183 struct clock_data monotonic;
184 struct clock_data realtime_alarm;
185 struct clock_data boottime_alarm;
186
187 usec_t perturb;
188
189 sd_event_source **signal_sources; /* indexed by signal number */
190 Hashmap *signal_data; /* indexed by priority */
191
192 Hashmap *child_sources;
193 unsigned n_enabled_child_sources;
194
195 Set *post_sources;
196
197 Prioq *exit;
198
199 pid_t original_pid;
200
201 unsigned iteration;
202 dual_timestamp timestamp;
203 usec_t timestamp_boottime;
204 int state;
205
206 bool exit_requested:1;
207 bool need_process_child:1;
208 bool watchdog:1;
209
210 int exit_code;
211
212 pid_t tid;
213 sd_event **default_event_ptr;
214
215 usec_t watchdog_last, watchdog_period;
216
217 unsigned n_sources;
218
219 LIST_HEAD(sd_event_source, sources);
220 };
221
222 static void source_disconnect(sd_event_source *s);
223
224 static int pending_prioq_compare(const void *a, const void *b) {
225 const sd_event_source *x = a, *y = b;
226
227 assert(x->pending);
228 assert(y->pending);
229
230 /* Enabled ones first */
231 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
232 return -1;
233 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
234 return 1;
235
236 /* Lower priority values first */
237 if (x->priority < y->priority)
238 return -1;
239 if (x->priority > y->priority)
240 return 1;
241
242 /* Older entries first */
243 if (x->pending_iteration < y->pending_iteration)
244 return -1;
245 if (x->pending_iteration > y->pending_iteration)
246 return 1;
247
248 return 0;
249 }
250
251 static int prepare_prioq_compare(const void *a, const void *b) {
252 const sd_event_source *x = a, *y = b;
253
254 assert(x->prepare);
255 assert(y->prepare);
256
257 /* Enabled ones first */
258 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
259 return -1;
260 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
261 return 1;
262
263 /* Move most recently prepared ones last, so that we can stop
264 * preparing as soon as we hit one that has already been
265 * prepared in the current iteration */
266 if (x->prepare_iteration < y->prepare_iteration)
267 return -1;
268 if (x->prepare_iteration > y->prepare_iteration)
269 return 1;
270
271 /* Lower priority values first */
272 if (x->priority < y->priority)
273 return -1;
274 if (x->priority > y->priority)
275 return 1;
276
277 return 0;
278 }
279
280 static int earliest_time_prioq_compare(const void *a, const void *b) {
281 const sd_event_source *x = a, *y = b;
282
283 assert(EVENT_SOURCE_IS_TIME(x->type));
284 assert(x->type == y->type);
285
286 /* Enabled ones first */
287 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
288 return -1;
289 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
290 return 1;
291
292 /* Move the pending ones to the end */
293 if (!x->pending && y->pending)
294 return -1;
295 if (x->pending && !y->pending)
296 return 1;
297
298 /* Order by time */
299 if (x->time.next < y->time.next)
300 return -1;
301 if (x->time.next > y->time.next)
302 return 1;
303
304 return 0;
305 }
306
307 static int latest_time_prioq_compare(const void *a, const void *b) {
308 const sd_event_source *x = a, *y = b;
309
310 assert(EVENT_SOURCE_IS_TIME(x->type));
311 assert(x->type == y->type);
312
313 /* Enabled ones first */
314 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
315 return -1;
316 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
317 return 1;
318
319 /* Move the pending ones to the end */
320 if (!x->pending && y->pending)
321 return -1;
322 if (x->pending && !y->pending)
323 return 1;
324
325 /* Order by time */
326 if (x->time.next + x->time.accuracy < y->time.next + y->time.accuracy)
327 return -1;
328 if (x->time.next + x->time.accuracy > y->time.next + y->time.accuracy)
329 return 1;
330
331 return 0;
332 }
333
334 static int exit_prioq_compare(const void *a, const void *b) {
335 const sd_event_source *x = a, *y = b;
336
337 assert(x->type == SOURCE_EXIT);
338 assert(y->type == SOURCE_EXIT);
339
340 /* Enabled ones first */
341 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
342 return -1;
343 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
344 return 1;
345
346 /* Lower priority values first */
347 if (x->priority < y->priority)
348 return -1;
349 if (x->priority > y->priority)
350 return 1;
351
352 return 0;
353 }
354
355 static void free_clock_data(struct clock_data *d) {
356 assert(d);
357 assert(d->wakeup == WAKEUP_CLOCK_DATA);
358
359 safe_close(d->fd);
360 prioq_free(d->earliest);
361 prioq_free(d->latest);
362 }
363
364 static void event_free(sd_event *e) {
365 sd_event_source *s;
366
367 assert(e);
368
369 while ((s = e->sources)) {
370 assert(s->floating);
371 source_disconnect(s);
372 sd_event_source_unref(s);
373 }
374
375 assert(e->n_sources == 0);
376
377 if (e->default_event_ptr)
378 *(e->default_event_ptr) = NULL;
379
380 safe_close(e->epoll_fd);
381 safe_close(e->watchdog_fd);
382
383 free_clock_data(&e->realtime);
384 free_clock_data(&e->boottime);
385 free_clock_data(&e->monotonic);
386 free_clock_data(&e->realtime_alarm);
387 free_clock_data(&e->boottime_alarm);
388
389 prioq_free(e->pending);
390 prioq_free(e->prepare);
391 prioq_free(e->exit);
392
393 free(e->signal_sources);
394 hashmap_free(e->signal_data);
395
396 hashmap_free(e->child_sources);
397 set_free(e->post_sources);
398 free(e);
399 }
400
401 _public_ int sd_event_new(sd_event** ret) {
402 sd_event *e;
403 int r;
404
405 assert_return(ret, -EINVAL);
406
407 e = new0(sd_event, 1);
408 if (!e)
409 return -ENOMEM;
410
411 e->n_ref = 1;
412 e->watchdog_fd = e->epoll_fd = e->realtime.fd = e->boottime.fd = e->monotonic.fd = e->realtime_alarm.fd = e->boottime_alarm.fd = -1;
413 e->realtime.next = e->boottime.next = e->monotonic.next = e->realtime_alarm.next = e->boottime_alarm.next = USEC_INFINITY;
414 e->realtime.wakeup = e->boottime.wakeup = e->monotonic.wakeup = e->realtime_alarm.wakeup = e->boottime_alarm.wakeup = WAKEUP_CLOCK_DATA;
415 e->original_pid = getpid();
416 e->perturb = USEC_INFINITY;
417
418 e->pending = prioq_new(pending_prioq_compare);
419 if (!e->pending) {
420 r = -ENOMEM;
421 goto fail;
422 }
423
424 e->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
425 if (e->epoll_fd < 0) {
426 r = -errno;
427 goto fail;
428 }
429
430 *ret = e;
431 return 0;
432
433 fail:
434 event_free(e);
435 return r;
436 }
437
438 _public_ sd_event* sd_event_ref(sd_event *e) {
439 assert_return(e, NULL);
440
441 assert(e->n_ref >= 1);
442 e->n_ref++;
443
444 return e;
445 }
446
447 _public_ sd_event* sd_event_unref(sd_event *e) {
448
449 if (!e)
450 return NULL;
451
452 assert(e->n_ref >= 1);
453 e->n_ref--;
454
455 if (e->n_ref <= 0)
456 event_free(e);
457
458 return NULL;
459 }
460
461 static bool event_pid_changed(sd_event *e) {
462 assert(e);
463
464 /* We don't support people creating an event loop and keeping
465 * it around over a fork(). Let's complain. */
466
467 return e->original_pid != getpid();
468 }
469
470 static void source_io_unregister(sd_event_source *s) {
471 int r;
472
473 assert(s);
474 assert(s->type == SOURCE_IO);
475
476 if (event_pid_changed(s->event))
477 return;
478
479 if (!s->io.registered)
480 return;
481
482 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, s->io.fd, NULL);
483 if (r < 0)
484 log_debug_errno(errno, "Failed to remove source %s from epoll: %m", strna(s->description));
485
486 s->io.registered = false;
487 }
488
489 static int source_io_register(
490 sd_event_source *s,
491 int enabled,
492 uint32_t events) {
493
494 struct epoll_event ev = {};
495 int r;
496
497 assert(s);
498 assert(s->type == SOURCE_IO);
499 assert(enabled != SD_EVENT_OFF);
500
501 ev.events = events;
502 ev.data.ptr = s;
503
504 if (enabled == SD_EVENT_ONESHOT)
505 ev.events |= EPOLLONESHOT;
506
507 if (s->io.registered)
508 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_MOD, s->io.fd, &ev);
509 else
510 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_ADD, s->io.fd, &ev);
511 if (r < 0)
512 return -errno;
513
514 s->io.registered = true;
515
516 return 0;
517 }
518
519 static clockid_t event_source_type_to_clock(EventSourceType t) {
520
521 switch (t) {
522
523 case SOURCE_TIME_REALTIME:
524 return CLOCK_REALTIME;
525
526 case SOURCE_TIME_BOOTTIME:
527 return CLOCK_BOOTTIME;
528
529 case SOURCE_TIME_MONOTONIC:
530 return CLOCK_MONOTONIC;
531
532 case SOURCE_TIME_REALTIME_ALARM:
533 return CLOCK_REALTIME_ALARM;
534
535 case SOURCE_TIME_BOOTTIME_ALARM:
536 return CLOCK_BOOTTIME_ALARM;
537
538 default:
539 return (clockid_t) -1;
540 }
541 }
542
543 static EventSourceType clock_to_event_source_type(clockid_t clock) {
544
545 switch (clock) {
546
547 case CLOCK_REALTIME:
548 return SOURCE_TIME_REALTIME;
549
550 case CLOCK_BOOTTIME:
551 return SOURCE_TIME_BOOTTIME;
552
553 case CLOCK_MONOTONIC:
554 return SOURCE_TIME_MONOTONIC;
555
556 case CLOCK_REALTIME_ALARM:
557 return SOURCE_TIME_REALTIME_ALARM;
558
559 case CLOCK_BOOTTIME_ALARM:
560 return SOURCE_TIME_BOOTTIME_ALARM;
561
562 default:
563 return _SOURCE_EVENT_SOURCE_TYPE_INVALID;
564 }
565 }
566
567 static struct clock_data* event_get_clock_data(sd_event *e, EventSourceType t) {
568 assert(e);
569
570 switch (t) {
571
572 case SOURCE_TIME_REALTIME:
573 return &e->realtime;
574
575 case SOURCE_TIME_BOOTTIME:
576 return &e->boottime;
577
578 case SOURCE_TIME_MONOTONIC:
579 return &e->monotonic;
580
581 case SOURCE_TIME_REALTIME_ALARM:
582 return &e->realtime_alarm;
583
584 case SOURCE_TIME_BOOTTIME_ALARM:
585 return &e->boottime_alarm;
586
587 default:
588 return NULL;
589 }
590 }
591
592 static int event_make_signal_data(
593 sd_event *e,
594 int sig,
595 struct signal_data **ret) {
596
597 struct epoll_event ev = {};
598 struct signal_data *d;
599 bool added = false;
600 sigset_t ss_copy;
601 int64_t priority;
602 int r;
603
604 assert(e);
605
606 if (event_pid_changed(e))
607 return -ECHILD;
608
609 if (e->signal_sources && e->signal_sources[sig])
610 priority = e->signal_sources[sig]->priority;
611 else
612 priority = 0;
613
614 d = hashmap_get(e->signal_data, &priority);
615 if (d) {
616 if (sigismember(&d->sigset, sig) > 0) {
617 if (ret)
618 *ret = d;
619 return 0;
620 }
621 } else {
622 r = hashmap_ensure_allocated(&e->signal_data, &uint64_hash_ops);
623 if (r < 0)
624 return r;
625
626 d = new0(struct signal_data, 1);
627 if (!d)
628 return -ENOMEM;
629
630 d->wakeup = WAKEUP_SIGNAL_DATA;
631 d->fd = -1;
632 d->priority = priority;
633
634 r = hashmap_put(e->signal_data, &d->priority, d);
635 if (r < 0)
636 return r;
637
638 added = true;
639 }
640
641 ss_copy = d->sigset;
642 assert_se(sigaddset(&ss_copy, sig) >= 0);
643
644 r = signalfd(d->fd, &ss_copy, SFD_NONBLOCK|SFD_CLOEXEC);
645 if (r < 0) {
646 r = -errno;
647 goto fail;
648 }
649
650 d->sigset = ss_copy;
651
652 if (d->fd >= 0) {
653 if (ret)
654 *ret = d;
655 return 0;
656 }
657
658 d->fd = r;
659
660 ev.events = EPOLLIN;
661 ev.data.ptr = d;
662
663 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, d->fd, &ev);
664 if (r < 0) {
665 r = -errno;
666 goto fail;
667 }
668
669 if (ret)
670 *ret = d;
671
672 return 0;
673
674 fail:
675 if (added) {
676 d->fd = safe_close(d->fd);
677 hashmap_remove(e->signal_data, &d->priority);
678 free(d);
679 }
680
681 return r;
682 }
683
684 static void event_unmask_signal_data(sd_event *e, struct signal_data *d, int sig) {
685 assert(e);
686 assert(d);
687
688 /* Turns off the specified signal in the signal data
689 * object. If the signal mask of the object becomes empty that
690 * way removes it. */
691
692 if (sigismember(&d->sigset, sig) == 0)
693 return;
694
695 assert_se(sigdelset(&d->sigset, sig) >= 0);
696
697 if (sigisemptyset(&d->sigset)) {
698
699 /* If all the mask is all-zero we can get rid of the structure */
700 hashmap_remove(e->signal_data, &d->priority);
701 assert(!d->current);
702 safe_close(d->fd);
703 free(d);
704 return;
705 }
706
707 assert(d->fd >= 0);
708
709 if (signalfd(d->fd, &d->sigset, SFD_NONBLOCK|SFD_CLOEXEC) < 0)
710 log_debug_errno(errno, "Failed to unset signal bit, ignoring: %m");
711 }
712
713 static void event_gc_signal_data(sd_event *e, const int64_t *priority, int sig) {
714 struct signal_data *d;
715 static const int64_t zero_priority = 0;
716
717 assert(e);
718
719 /* Rechecks if the specified signal is still something we are
720 * interested in. If not, we'll unmask it, and possibly drop
721 * the signalfd for it. */
722
723 if (sig == SIGCHLD &&
724 e->n_enabled_child_sources > 0)
725 return;
726
727 if (e->signal_sources &&
728 e->signal_sources[sig] &&
729 e->signal_sources[sig]->enabled != SD_EVENT_OFF)
730 return;
731
732 /*
733 * The specified signal might be enabled in three different queues:
734 *
735 * 1) the one that belongs to the priority passed (if it is non-NULL)
736 * 2) the one that belongs to the priority of the event source of the signal (if there is one)
737 * 3) the 0 priority (to cover the SIGCHLD case)
738 *
739 * Hence, let's remove it from all three here.
740 */
741
742 if (priority) {
743 d = hashmap_get(e->signal_data, priority);
744 if (d)
745 event_unmask_signal_data(e, d, sig);
746 }
747
748 if (e->signal_sources && e->signal_sources[sig]) {
749 d = hashmap_get(e->signal_data, &e->signal_sources[sig]->priority);
750 if (d)
751 event_unmask_signal_data(e, d, sig);
752 }
753
754 d = hashmap_get(e->signal_data, &zero_priority);
755 if (d)
756 event_unmask_signal_data(e, d, sig);
757 }
758
759 static void source_disconnect(sd_event_source *s) {
760 sd_event *event;
761
762 assert(s);
763
764 if (!s->event)
765 return;
766
767 assert(s->event->n_sources > 0);
768
769 switch (s->type) {
770
771 case SOURCE_IO:
772 if (s->io.fd >= 0)
773 source_io_unregister(s);
774
775 break;
776
777 case SOURCE_TIME_REALTIME:
778 case SOURCE_TIME_BOOTTIME:
779 case SOURCE_TIME_MONOTONIC:
780 case SOURCE_TIME_REALTIME_ALARM:
781 case SOURCE_TIME_BOOTTIME_ALARM: {
782 struct clock_data *d;
783
784 d = event_get_clock_data(s->event, s->type);
785 assert(d);
786
787 prioq_remove(d->earliest, s, &s->time.earliest_index);
788 prioq_remove(d->latest, s, &s->time.latest_index);
789 d->needs_rearm = true;
790 break;
791 }
792
793 case SOURCE_SIGNAL:
794 if (s->signal.sig > 0) {
795
796 if (s->event->signal_sources)
797 s->event->signal_sources[s->signal.sig] = NULL;
798
799 event_gc_signal_data(s->event, &s->priority, s->signal.sig);
800 }
801
802 break;
803
804 case SOURCE_CHILD:
805 if (s->child.pid > 0) {
806 if (s->enabled != SD_EVENT_OFF) {
807 assert(s->event->n_enabled_child_sources > 0);
808 s->event->n_enabled_child_sources--;
809 }
810
811 (void) hashmap_remove(s->event->child_sources, INT_TO_PTR(s->child.pid));
812 event_gc_signal_data(s->event, &s->priority, SIGCHLD);
813 }
814
815 break;
816
817 case SOURCE_DEFER:
818 /* nothing */
819 break;
820
821 case SOURCE_POST:
822 set_remove(s->event->post_sources, s);
823 break;
824
825 case SOURCE_EXIT:
826 prioq_remove(s->event->exit, s, &s->exit.prioq_index);
827 break;
828
829 default:
830 assert_not_reached("Wut? I shouldn't exist.");
831 }
832
833 if (s->pending)
834 prioq_remove(s->event->pending, s, &s->pending_index);
835
836 if (s->prepare)
837 prioq_remove(s->event->prepare, s, &s->prepare_index);
838
839 event = s->event;
840
841 s->type = _SOURCE_EVENT_SOURCE_TYPE_INVALID;
842 s->event = NULL;
843 LIST_REMOVE(sources, event->sources, s);
844 event->n_sources--;
845
846 if (!s->floating)
847 sd_event_unref(event);
848 }
849
850 static void source_free(sd_event_source *s) {
851 assert(s);
852
853 source_disconnect(s);
854 free(s->description);
855 free(s);
856 }
857
858 static int source_set_pending(sd_event_source *s, bool b) {
859 int r;
860
861 assert(s);
862 assert(s->type != SOURCE_EXIT);
863
864 if (s->pending == b)
865 return 0;
866
867 s->pending = b;
868
869 if (b) {
870 s->pending_iteration = s->event->iteration;
871
872 r = prioq_put(s->event->pending, s, &s->pending_index);
873 if (r < 0) {
874 s->pending = false;
875 return r;
876 }
877 } else
878 assert_se(prioq_remove(s->event->pending, s, &s->pending_index));
879
880 if (EVENT_SOURCE_IS_TIME(s->type)) {
881 struct clock_data *d;
882
883 d = event_get_clock_data(s->event, s->type);
884 assert(d);
885
886 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
887 prioq_reshuffle(d->latest, s, &s->time.latest_index);
888 d->needs_rearm = true;
889 }
890
891 if (s->type == SOURCE_SIGNAL && !b) {
892 struct signal_data *d;
893
894 d = hashmap_get(s->event->signal_data, &s->priority);
895 if (d && d->current == s)
896 d->current = NULL;
897 }
898
899 return 0;
900 }
901
902 static sd_event_source *source_new(sd_event *e, bool floating, EventSourceType type) {
903 sd_event_source *s;
904
905 assert(e);
906
907 s = new0(sd_event_source, 1);
908 if (!s)
909 return NULL;
910
911 s->n_ref = 1;
912 s->event = e;
913 s->floating = floating;
914 s->type = type;
915 s->pending_index = s->prepare_index = PRIOQ_IDX_NULL;
916
917 if (!floating)
918 sd_event_ref(e);
919
920 LIST_PREPEND(sources, e->sources, s);
921 e->n_sources ++;
922
923 return s;
924 }
925
926 _public_ int sd_event_add_io(
927 sd_event *e,
928 sd_event_source **ret,
929 int fd,
930 uint32_t events,
931 sd_event_io_handler_t callback,
932 void *userdata) {
933
934 sd_event_source *s;
935 int r;
936
937 assert_return(e, -EINVAL);
938 assert_return(fd >= 0, -EBADF);
939 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
940 assert_return(callback, -EINVAL);
941 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
942 assert_return(!event_pid_changed(e), -ECHILD);
943
944 s = source_new(e, !ret, SOURCE_IO);
945 if (!s)
946 return -ENOMEM;
947
948 s->wakeup = WAKEUP_EVENT_SOURCE;
949 s->io.fd = fd;
950 s->io.events = events;
951 s->io.callback = callback;
952 s->userdata = userdata;
953 s->enabled = SD_EVENT_ON;
954
955 r = source_io_register(s, s->enabled, events);
956 if (r < 0) {
957 source_free(s);
958 return r;
959 }
960
961 if (ret)
962 *ret = s;
963
964 return 0;
965 }
966
967 static void initialize_perturb(sd_event *e) {
968 sd_id128_t bootid = {};
969
970 /* When we sleep for longer, we try to realign the wakeup to
971 the same time wihtin each minute/second/250ms, so that
972 events all across the system can be coalesced into a single
973 CPU wakeup. However, let's take some system-specific
974 randomness for this value, so that in a network of systems
975 with synced clocks timer events are distributed a
976 bit. Here, we calculate a perturbation usec offset from the
977 boot ID. */
978
979 if (_likely_(e->perturb != USEC_INFINITY))
980 return;
981
982 if (sd_id128_get_boot(&bootid) >= 0)
983 e->perturb = (bootid.qwords[0] ^ bootid.qwords[1]) % USEC_PER_MINUTE;
984 }
985
986 static int event_setup_timer_fd(
987 sd_event *e,
988 struct clock_data *d,
989 clockid_t clock) {
990
991 struct epoll_event ev = {};
992 int r, fd;
993
994 assert(e);
995 assert(d);
996
997 if (_likely_(d->fd >= 0))
998 return 0;
999
1000 fd = timerfd_create(clock, TFD_NONBLOCK|TFD_CLOEXEC);
1001 if (fd < 0)
1002 return -errno;
1003
1004 ev.events = EPOLLIN;
1005 ev.data.ptr = d;
1006
1007 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, fd, &ev);
1008 if (r < 0) {
1009 safe_close(fd);
1010 return -errno;
1011 }
1012
1013 d->fd = fd;
1014 return 0;
1015 }
1016
1017 static int time_exit_callback(sd_event_source *s, uint64_t usec, void *userdata) {
1018 assert(s);
1019
1020 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
1021 }
1022
1023 _public_ int sd_event_add_time(
1024 sd_event *e,
1025 sd_event_source **ret,
1026 clockid_t clock,
1027 uint64_t usec,
1028 uint64_t accuracy,
1029 sd_event_time_handler_t callback,
1030 void *userdata) {
1031
1032 EventSourceType type;
1033 sd_event_source *s;
1034 struct clock_data *d;
1035 int r;
1036
1037 assert_return(e, -EINVAL);
1038 assert_return(usec != (uint64_t) -1, -EINVAL);
1039 assert_return(accuracy != (uint64_t) -1, -EINVAL);
1040 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1041 assert_return(!event_pid_changed(e), -ECHILD);
1042
1043 if (!callback)
1044 callback = time_exit_callback;
1045
1046 type = clock_to_event_source_type(clock);
1047 assert_return(type >= 0, -EOPNOTSUPP);
1048
1049 d = event_get_clock_data(e, type);
1050 assert(d);
1051
1052 if (!d->earliest) {
1053 d->earliest = prioq_new(earliest_time_prioq_compare);
1054 if (!d->earliest)
1055 return -ENOMEM;
1056 }
1057
1058 if (!d->latest) {
1059 d->latest = prioq_new(latest_time_prioq_compare);
1060 if (!d->latest)
1061 return -ENOMEM;
1062 }
1063
1064 if (d->fd < 0) {
1065 r = event_setup_timer_fd(e, d, clock);
1066 if (r < 0)
1067 return r;
1068 }
1069
1070 s = source_new(e, !ret, type);
1071 if (!s)
1072 return -ENOMEM;
1073
1074 s->time.next = usec;
1075 s->time.accuracy = accuracy == 0 ? DEFAULT_ACCURACY_USEC : accuracy;
1076 s->time.callback = callback;
1077 s->time.earliest_index = s->time.latest_index = PRIOQ_IDX_NULL;
1078 s->userdata = userdata;
1079 s->enabled = SD_EVENT_ONESHOT;
1080
1081 d->needs_rearm = true;
1082
1083 r = prioq_put(d->earliest, s, &s->time.earliest_index);
1084 if (r < 0)
1085 goto fail;
1086
1087 r = prioq_put(d->latest, s, &s->time.latest_index);
1088 if (r < 0)
1089 goto fail;
1090
1091 if (ret)
1092 *ret = s;
1093
1094 return 0;
1095
1096 fail:
1097 source_free(s);
1098 return r;
1099 }
1100
1101 static int signal_exit_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
1102 assert(s);
1103
1104 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
1105 }
1106
1107 _public_ int sd_event_add_signal(
1108 sd_event *e,
1109 sd_event_source **ret,
1110 int sig,
1111 sd_event_signal_handler_t callback,
1112 void *userdata) {
1113
1114 sd_event_source *s;
1115 struct signal_data *d;
1116 sigset_t ss;
1117 int r;
1118
1119 assert_return(e, -EINVAL);
1120 assert_return(sig > 0, -EINVAL);
1121 assert_return(sig < _NSIG, -EINVAL);
1122 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1123 assert_return(!event_pid_changed(e), -ECHILD);
1124
1125 if (!callback)
1126 callback = signal_exit_callback;
1127
1128 r = pthread_sigmask(SIG_SETMASK, NULL, &ss);
1129 if (r != 0)
1130 return -r;
1131
1132 if (!sigismember(&ss, sig))
1133 return -EBUSY;
1134
1135 if (!e->signal_sources) {
1136 e->signal_sources = new0(sd_event_source*, _NSIG);
1137 if (!e->signal_sources)
1138 return -ENOMEM;
1139 } else if (e->signal_sources[sig])
1140 return -EBUSY;
1141
1142 s = source_new(e, !ret, SOURCE_SIGNAL);
1143 if (!s)
1144 return -ENOMEM;
1145
1146 s->signal.sig = sig;
1147 s->signal.callback = callback;
1148 s->userdata = userdata;
1149 s->enabled = SD_EVENT_ON;
1150
1151 e->signal_sources[sig] = s;
1152
1153 r = event_make_signal_data(e, sig, &d);
1154 if (r < 0) {
1155 source_free(s);
1156 return r;
1157 }
1158
1159 /* Use the signal name as description for the event source by default */
1160 (void) sd_event_source_set_description(s, signal_to_string(sig));
1161
1162 if (ret)
1163 *ret = s;
1164
1165 return 0;
1166 }
1167
1168 _public_ int sd_event_add_child(
1169 sd_event *e,
1170 sd_event_source **ret,
1171 pid_t pid,
1172 int options,
1173 sd_event_child_handler_t callback,
1174 void *userdata) {
1175
1176 sd_event_source *s;
1177 int r;
1178
1179 assert_return(e, -EINVAL);
1180 assert_return(pid > 1, -EINVAL);
1181 assert_return(!(options & ~(WEXITED|WSTOPPED|WCONTINUED)), -EINVAL);
1182 assert_return(options != 0, -EINVAL);
1183 assert_return(callback, -EINVAL);
1184 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1185 assert_return(!event_pid_changed(e), -ECHILD);
1186
1187 r = hashmap_ensure_allocated(&e->child_sources, NULL);
1188 if (r < 0)
1189 return r;
1190
1191 if (hashmap_contains(e->child_sources, INT_TO_PTR(pid)))
1192 return -EBUSY;
1193
1194 s = source_new(e, !ret, SOURCE_CHILD);
1195 if (!s)
1196 return -ENOMEM;
1197
1198 s->child.pid = pid;
1199 s->child.options = options;
1200 s->child.callback = callback;
1201 s->userdata = userdata;
1202 s->enabled = SD_EVENT_ONESHOT;
1203
1204 r = hashmap_put(e->child_sources, INT_TO_PTR(pid), s);
1205 if (r < 0) {
1206 source_free(s);
1207 return r;
1208 }
1209
1210 e->n_enabled_child_sources ++;
1211
1212 r = event_make_signal_data(e, SIGCHLD, NULL);
1213 if (r < 0) {
1214 e->n_enabled_child_sources--;
1215 source_free(s);
1216 return r;
1217 }
1218
1219 e->need_process_child = true;
1220
1221 if (ret)
1222 *ret = s;
1223
1224 return 0;
1225 }
1226
1227 _public_ int sd_event_add_defer(
1228 sd_event *e,
1229 sd_event_source **ret,
1230 sd_event_handler_t callback,
1231 void *userdata) {
1232
1233 sd_event_source *s;
1234 int r;
1235
1236 assert_return(e, -EINVAL);
1237 assert_return(callback, -EINVAL);
1238 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1239 assert_return(!event_pid_changed(e), -ECHILD);
1240
1241 s = source_new(e, !ret, SOURCE_DEFER);
1242 if (!s)
1243 return -ENOMEM;
1244
1245 s->defer.callback = callback;
1246 s->userdata = userdata;
1247 s->enabled = SD_EVENT_ONESHOT;
1248
1249 r = source_set_pending(s, true);
1250 if (r < 0) {
1251 source_free(s);
1252 return r;
1253 }
1254
1255 if (ret)
1256 *ret = s;
1257
1258 return 0;
1259 }
1260
1261 _public_ int sd_event_add_post(
1262 sd_event *e,
1263 sd_event_source **ret,
1264 sd_event_handler_t callback,
1265 void *userdata) {
1266
1267 sd_event_source *s;
1268 int r;
1269
1270 assert_return(e, -EINVAL);
1271 assert_return(callback, -EINVAL);
1272 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1273 assert_return(!event_pid_changed(e), -ECHILD);
1274
1275 r = set_ensure_allocated(&e->post_sources, NULL);
1276 if (r < 0)
1277 return r;
1278
1279 s = source_new(e, !ret, SOURCE_POST);
1280 if (!s)
1281 return -ENOMEM;
1282
1283 s->post.callback = callback;
1284 s->userdata = userdata;
1285 s->enabled = SD_EVENT_ON;
1286
1287 r = set_put(e->post_sources, s);
1288 if (r < 0) {
1289 source_free(s);
1290 return r;
1291 }
1292
1293 if (ret)
1294 *ret = s;
1295
1296 return 0;
1297 }
1298
1299 _public_ int sd_event_add_exit(
1300 sd_event *e,
1301 sd_event_source **ret,
1302 sd_event_handler_t callback,
1303 void *userdata) {
1304
1305 sd_event_source *s;
1306 int r;
1307
1308 assert_return(e, -EINVAL);
1309 assert_return(callback, -EINVAL);
1310 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1311 assert_return(!event_pid_changed(e), -ECHILD);
1312
1313 if (!e->exit) {
1314 e->exit = prioq_new(exit_prioq_compare);
1315 if (!e->exit)
1316 return -ENOMEM;
1317 }
1318
1319 s = source_new(e, !ret, SOURCE_EXIT);
1320 if (!s)
1321 return -ENOMEM;
1322
1323 s->exit.callback = callback;
1324 s->userdata = userdata;
1325 s->exit.prioq_index = PRIOQ_IDX_NULL;
1326 s->enabled = SD_EVENT_ONESHOT;
1327
1328 r = prioq_put(s->event->exit, s, &s->exit.prioq_index);
1329 if (r < 0) {
1330 source_free(s);
1331 return r;
1332 }
1333
1334 if (ret)
1335 *ret = s;
1336
1337 return 0;
1338 }
1339
1340 _public_ sd_event_source* sd_event_source_ref(sd_event_source *s) {
1341 assert_return(s, NULL);
1342
1343 assert(s->n_ref >= 1);
1344 s->n_ref++;
1345
1346 return s;
1347 }
1348
1349 _public_ sd_event_source* sd_event_source_unref(sd_event_source *s) {
1350
1351 if (!s)
1352 return NULL;
1353
1354 assert(s->n_ref >= 1);
1355 s->n_ref--;
1356
1357 if (s->n_ref <= 0) {
1358 /* Here's a special hack: when we are called from a
1359 * dispatch handler we won't free the event source
1360 * immediately, but we will detach the fd from the
1361 * epoll. This way it is safe for the caller to unref
1362 * the event source and immediately close the fd, but
1363 * we still retain a valid event source object after
1364 * the callback. */
1365
1366 if (s->dispatching) {
1367 if (s->type == SOURCE_IO)
1368 source_io_unregister(s);
1369
1370 source_disconnect(s);
1371 } else
1372 source_free(s);
1373 }
1374
1375 return NULL;
1376 }
1377
1378 _public_ int sd_event_source_set_description(sd_event_source *s, const char *description) {
1379 assert_return(s, -EINVAL);
1380 assert_return(!event_pid_changed(s->event), -ECHILD);
1381
1382 return free_and_strdup(&s->description, description);
1383 }
1384
1385 _public_ int sd_event_source_get_description(sd_event_source *s, const char **description) {
1386 assert_return(s, -EINVAL);
1387 assert_return(description, -EINVAL);
1388 assert_return(s->description, -ENXIO);
1389 assert_return(!event_pid_changed(s->event), -ECHILD);
1390
1391 *description = s->description;
1392 return 0;
1393 }
1394
1395 _public_ sd_event *sd_event_source_get_event(sd_event_source *s) {
1396 assert_return(s, NULL);
1397
1398 return s->event;
1399 }
1400
1401 _public_ int sd_event_source_get_pending(sd_event_source *s) {
1402 assert_return(s, -EINVAL);
1403 assert_return(s->type != SOURCE_EXIT, -EDOM);
1404 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1405 assert_return(!event_pid_changed(s->event), -ECHILD);
1406
1407 return s->pending;
1408 }
1409
1410 _public_ int sd_event_source_get_io_fd(sd_event_source *s) {
1411 assert_return(s, -EINVAL);
1412 assert_return(s->type == SOURCE_IO, -EDOM);
1413 assert_return(!event_pid_changed(s->event), -ECHILD);
1414
1415 return s->io.fd;
1416 }
1417
1418 _public_ int sd_event_source_set_io_fd(sd_event_source *s, int fd) {
1419 int r;
1420
1421 assert_return(s, -EINVAL);
1422 assert_return(fd >= 0, -EBADF);
1423 assert_return(s->type == SOURCE_IO, -EDOM);
1424 assert_return(!event_pid_changed(s->event), -ECHILD);
1425
1426 if (s->io.fd == fd)
1427 return 0;
1428
1429 if (s->enabled == SD_EVENT_OFF) {
1430 s->io.fd = fd;
1431 s->io.registered = false;
1432 } else {
1433 int saved_fd;
1434
1435 saved_fd = s->io.fd;
1436 assert(s->io.registered);
1437
1438 s->io.fd = fd;
1439 s->io.registered = false;
1440
1441 r = source_io_register(s, s->enabled, s->io.events);
1442 if (r < 0) {
1443 s->io.fd = saved_fd;
1444 s->io.registered = true;
1445 return r;
1446 }
1447
1448 epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, saved_fd, NULL);
1449 }
1450
1451 return 0;
1452 }
1453
1454 _public_ int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events) {
1455 assert_return(s, -EINVAL);
1456 assert_return(events, -EINVAL);
1457 assert_return(s->type == SOURCE_IO, -EDOM);
1458 assert_return(!event_pid_changed(s->event), -ECHILD);
1459
1460 *events = s->io.events;
1461 return 0;
1462 }
1463
1464 _public_ int sd_event_source_set_io_events(sd_event_source *s, uint32_t events) {
1465 int r;
1466
1467 assert_return(s, -EINVAL);
1468 assert_return(s->type == SOURCE_IO, -EDOM);
1469 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
1470 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1471 assert_return(!event_pid_changed(s->event), -ECHILD);
1472
1473 /* edge-triggered updates are never skipped, so we can reset edges */
1474 if (s->io.events == events && !(events & EPOLLET))
1475 return 0;
1476
1477 if (s->enabled != SD_EVENT_OFF) {
1478 r = source_io_register(s, s->enabled, events);
1479 if (r < 0)
1480 return r;
1481 }
1482
1483 s->io.events = events;
1484 source_set_pending(s, false);
1485
1486 return 0;
1487 }
1488
1489 _public_ int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents) {
1490 assert_return(s, -EINVAL);
1491 assert_return(revents, -EINVAL);
1492 assert_return(s->type == SOURCE_IO, -EDOM);
1493 assert_return(s->pending, -ENODATA);
1494 assert_return(!event_pid_changed(s->event), -ECHILD);
1495
1496 *revents = s->io.revents;
1497 return 0;
1498 }
1499
1500 _public_ int sd_event_source_get_signal(sd_event_source *s) {
1501 assert_return(s, -EINVAL);
1502 assert_return(s->type == SOURCE_SIGNAL, -EDOM);
1503 assert_return(!event_pid_changed(s->event), -ECHILD);
1504
1505 return s->signal.sig;
1506 }
1507
1508 _public_ int sd_event_source_get_priority(sd_event_source *s, int64_t *priority) {
1509 assert_return(s, -EINVAL);
1510 assert_return(!event_pid_changed(s->event), -ECHILD);
1511
1512 return s->priority;
1513 }
1514
1515 _public_ int sd_event_source_set_priority(sd_event_source *s, int64_t priority) {
1516 int r;
1517
1518 assert_return(s, -EINVAL);
1519 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1520 assert_return(!event_pid_changed(s->event), -ECHILD);
1521
1522 if (s->priority == priority)
1523 return 0;
1524
1525 if (s->type == SOURCE_SIGNAL && s->enabled != SD_EVENT_OFF) {
1526 struct signal_data *old, *d;
1527
1528 /* Move us from the signalfd belonging to the old
1529 * priority to the signalfd of the new priority */
1530
1531 assert_se(old = hashmap_get(s->event->signal_data, &s->priority));
1532
1533 s->priority = priority;
1534
1535 r = event_make_signal_data(s->event, s->signal.sig, &d);
1536 if (r < 0) {
1537 s->priority = old->priority;
1538 return r;
1539 }
1540
1541 event_unmask_signal_data(s->event, old, s->signal.sig);
1542 } else
1543 s->priority = priority;
1544
1545 if (s->pending)
1546 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1547
1548 if (s->prepare)
1549 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1550
1551 if (s->type == SOURCE_EXIT)
1552 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1553
1554 return 0;
1555 }
1556
1557 _public_ int sd_event_source_get_enabled(sd_event_source *s, int *m) {
1558 assert_return(s, -EINVAL);
1559 assert_return(m, -EINVAL);
1560 assert_return(!event_pid_changed(s->event), -ECHILD);
1561
1562 *m = s->enabled;
1563 return 0;
1564 }
1565
1566 _public_ int sd_event_source_set_enabled(sd_event_source *s, int m) {
1567 int r;
1568
1569 assert_return(s, -EINVAL);
1570 assert_return(m == SD_EVENT_OFF || m == SD_EVENT_ON || m == SD_EVENT_ONESHOT, -EINVAL);
1571 assert_return(!event_pid_changed(s->event), -ECHILD);
1572
1573 /* If we are dead anyway, we are fine with turning off
1574 * sources, but everything else needs to fail. */
1575 if (s->event->state == SD_EVENT_FINISHED)
1576 return m == SD_EVENT_OFF ? 0 : -ESTALE;
1577
1578 if (s->enabled == m)
1579 return 0;
1580
1581 if (m == SD_EVENT_OFF) {
1582
1583 switch (s->type) {
1584
1585 case SOURCE_IO:
1586 source_io_unregister(s);
1587 s->enabled = m;
1588 break;
1589
1590 case SOURCE_TIME_REALTIME:
1591 case SOURCE_TIME_BOOTTIME:
1592 case SOURCE_TIME_MONOTONIC:
1593 case SOURCE_TIME_REALTIME_ALARM:
1594 case SOURCE_TIME_BOOTTIME_ALARM: {
1595 struct clock_data *d;
1596
1597 s->enabled = m;
1598 d = event_get_clock_data(s->event, s->type);
1599 assert(d);
1600
1601 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1602 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1603 d->needs_rearm = true;
1604 break;
1605 }
1606
1607 case SOURCE_SIGNAL:
1608 s->enabled = m;
1609
1610 event_gc_signal_data(s->event, &s->priority, s->signal.sig);
1611 break;
1612
1613 case SOURCE_CHILD:
1614 s->enabled = m;
1615
1616 assert(s->event->n_enabled_child_sources > 0);
1617 s->event->n_enabled_child_sources--;
1618
1619 event_gc_signal_data(s->event, &s->priority, SIGCHLD);
1620 break;
1621
1622 case SOURCE_EXIT:
1623 s->enabled = m;
1624 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1625 break;
1626
1627 case SOURCE_DEFER:
1628 case SOURCE_POST:
1629 s->enabled = m;
1630 break;
1631
1632 default:
1633 assert_not_reached("Wut? I shouldn't exist.");
1634 }
1635
1636 } else {
1637 switch (s->type) {
1638
1639 case SOURCE_IO:
1640 r = source_io_register(s, m, s->io.events);
1641 if (r < 0)
1642 return r;
1643
1644 s->enabled = m;
1645 break;
1646
1647 case SOURCE_TIME_REALTIME:
1648 case SOURCE_TIME_BOOTTIME:
1649 case SOURCE_TIME_MONOTONIC:
1650 case SOURCE_TIME_REALTIME_ALARM:
1651 case SOURCE_TIME_BOOTTIME_ALARM: {
1652 struct clock_data *d;
1653
1654 s->enabled = m;
1655 d = event_get_clock_data(s->event, s->type);
1656 assert(d);
1657
1658 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1659 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1660 d->needs_rearm = true;
1661 break;
1662 }
1663
1664 case SOURCE_SIGNAL:
1665
1666 s->enabled = m;
1667
1668 r = event_make_signal_data(s->event, s->signal.sig, NULL);
1669 if (r < 0) {
1670 s->enabled = SD_EVENT_OFF;
1671 event_gc_signal_data(s->event, &s->priority, s->signal.sig);
1672 return r;
1673 }
1674
1675 break;
1676
1677 case SOURCE_CHILD:
1678
1679 if (s->enabled == SD_EVENT_OFF)
1680 s->event->n_enabled_child_sources++;
1681
1682 s->enabled = m;
1683
1684 r = event_make_signal_data(s->event, SIGCHLD, NULL);
1685 if (r < 0) {
1686 s->enabled = SD_EVENT_OFF;
1687 s->event->n_enabled_child_sources--;
1688 event_gc_signal_data(s->event, &s->priority, SIGCHLD);
1689 return r;
1690 }
1691
1692 break;
1693
1694 case SOURCE_EXIT:
1695 s->enabled = m;
1696 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1697 break;
1698
1699 case SOURCE_DEFER:
1700 case SOURCE_POST:
1701 s->enabled = m;
1702 break;
1703
1704 default:
1705 assert_not_reached("Wut? I shouldn't exist.");
1706 }
1707 }
1708
1709 if (s->pending)
1710 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1711
1712 if (s->prepare)
1713 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1714
1715 return 0;
1716 }
1717
1718 _public_ int sd_event_source_get_time(sd_event_source *s, uint64_t *usec) {
1719 assert_return(s, -EINVAL);
1720 assert_return(usec, -EINVAL);
1721 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1722 assert_return(!event_pid_changed(s->event), -ECHILD);
1723
1724 *usec = s->time.next;
1725 return 0;
1726 }
1727
1728 _public_ int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
1729 struct clock_data *d;
1730
1731 assert_return(s, -EINVAL);
1732 assert_return(usec != (uint64_t) -1, -EINVAL);
1733 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1734 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1735 assert_return(!event_pid_changed(s->event), -ECHILD);
1736
1737 s->time.next = usec;
1738
1739 source_set_pending(s, false);
1740
1741 d = event_get_clock_data(s->event, s->type);
1742 assert(d);
1743
1744 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1745 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1746 d->needs_rearm = true;
1747
1748 return 0;
1749 }
1750
1751 _public_ int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec) {
1752 assert_return(s, -EINVAL);
1753 assert_return(usec, -EINVAL);
1754 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1755 assert_return(!event_pid_changed(s->event), -ECHILD);
1756
1757 *usec = s->time.accuracy;
1758 return 0;
1759 }
1760
1761 _public_ int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec) {
1762 struct clock_data *d;
1763
1764 assert_return(s, -EINVAL);
1765 assert_return(usec != (uint64_t) -1, -EINVAL);
1766 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1767 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1768 assert_return(!event_pid_changed(s->event), -ECHILD);
1769
1770 if (usec == 0)
1771 usec = DEFAULT_ACCURACY_USEC;
1772
1773 s->time.accuracy = usec;
1774
1775 source_set_pending(s, false);
1776
1777 d = event_get_clock_data(s->event, s->type);
1778 assert(d);
1779
1780 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1781 d->needs_rearm = true;
1782
1783 return 0;
1784 }
1785
1786 _public_ int sd_event_source_get_time_clock(sd_event_source *s, clockid_t *clock) {
1787 assert_return(s, -EINVAL);
1788 assert_return(clock, -EINVAL);
1789 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1790 assert_return(!event_pid_changed(s->event), -ECHILD);
1791
1792 *clock = event_source_type_to_clock(s->type);
1793 return 0;
1794 }
1795
1796 _public_ int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid) {
1797 assert_return(s, -EINVAL);
1798 assert_return(pid, -EINVAL);
1799 assert_return(s->type == SOURCE_CHILD, -EDOM);
1800 assert_return(!event_pid_changed(s->event), -ECHILD);
1801
1802 *pid = s->child.pid;
1803 return 0;
1804 }
1805
1806 _public_ int sd_event_source_set_prepare(sd_event_source *s, sd_event_handler_t callback) {
1807 int r;
1808
1809 assert_return(s, -EINVAL);
1810 assert_return(s->type != SOURCE_EXIT, -EDOM);
1811 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1812 assert_return(!event_pid_changed(s->event), -ECHILD);
1813
1814 if (s->prepare == callback)
1815 return 0;
1816
1817 if (callback && s->prepare) {
1818 s->prepare = callback;
1819 return 0;
1820 }
1821
1822 r = prioq_ensure_allocated(&s->event->prepare, prepare_prioq_compare);
1823 if (r < 0)
1824 return r;
1825
1826 s->prepare = callback;
1827
1828 if (callback) {
1829 r = prioq_put(s->event->prepare, s, &s->prepare_index);
1830 if (r < 0)
1831 return r;
1832 } else
1833 prioq_remove(s->event->prepare, s, &s->prepare_index);
1834
1835 return 0;
1836 }
1837
1838 _public_ void* sd_event_source_get_userdata(sd_event_source *s) {
1839 assert_return(s, NULL);
1840
1841 return s->userdata;
1842 }
1843
1844 _public_ void *sd_event_source_set_userdata(sd_event_source *s, void *userdata) {
1845 void *ret;
1846
1847 assert_return(s, NULL);
1848
1849 ret = s->userdata;
1850 s->userdata = userdata;
1851
1852 return ret;
1853 }
1854
1855 static usec_t sleep_between(sd_event *e, usec_t a, usec_t b) {
1856 usec_t c;
1857 assert(e);
1858 assert(a <= b);
1859
1860 if (a <= 0)
1861 return 0;
1862
1863 if (b <= a + 1)
1864 return a;
1865
1866 initialize_perturb(e);
1867
1868 /*
1869 Find a good time to wake up again between times a and b. We
1870 have two goals here:
1871
1872 a) We want to wake up as seldom as possible, hence prefer
1873 later times over earlier times.
1874
1875 b) But if we have to wake up, then let's make sure to
1876 dispatch as much as possible on the entire system.
1877
1878 We implement this by waking up everywhere at the same time
1879 within any given minute if we can, synchronised via the
1880 perturbation value determined from the boot ID. If we can't,
1881 then we try to find the same spot in every 10s, then 1s and
1882 then 250ms step. Otherwise, we pick the last possible time
1883 to wake up.
1884 */
1885
1886 c = (b / USEC_PER_MINUTE) * USEC_PER_MINUTE + e->perturb;
1887 if (c >= b) {
1888 if (_unlikely_(c < USEC_PER_MINUTE))
1889 return b;
1890
1891 c -= USEC_PER_MINUTE;
1892 }
1893
1894 if (c >= a)
1895 return c;
1896
1897 c = (b / (USEC_PER_SEC*10)) * (USEC_PER_SEC*10) + (e->perturb % (USEC_PER_SEC*10));
1898 if (c >= b) {
1899 if (_unlikely_(c < USEC_PER_SEC*10))
1900 return b;
1901
1902 c -= USEC_PER_SEC*10;
1903 }
1904
1905 if (c >= a)
1906 return c;
1907
1908 c = (b / USEC_PER_SEC) * USEC_PER_SEC + (e->perturb % USEC_PER_SEC);
1909 if (c >= b) {
1910 if (_unlikely_(c < USEC_PER_SEC))
1911 return b;
1912
1913 c -= USEC_PER_SEC;
1914 }
1915
1916 if (c >= a)
1917 return c;
1918
1919 c = (b / (USEC_PER_MSEC*250)) * (USEC_PER_MSEC*250) + (e->perturb % (USEC_PER_MSEC*250));
1920 if (c >= b) {
1921 if (_unlikely_(c < USEC_PER_MSEC*250))
1922 return b;
1923
1924 c -= USEC_PER_MSEC*250;
1925 }
1926
1927 if (c >= a)
1928 return c;
1929
1930 return b;
1931 }
1932
1933 static int event_arm_timer(
1934 sd_event *e,
1935 struct clock_data *d) {
1936
1937 struct itimerspec its = {};
1938 sd_event_source *a, *b;
1939 usec_t t;
1940 int r;
1941
1942 assert(e);
1943 assert(d);
1944
1945 if (!d->needs_rearm)
1946 return 0;
1947 else
1948 d->needs_rearm = false;
1949
1950 a = prioq_peek(d->earliest);
1951 if (!a || a->enabled == SD_EVENT_OFF) {
1952
1953 if (d->fd < 0)
1954 return 0;
1955
1956 if (d->next == USEC_INFINITY)
1957 return 0;
1958
1959 /* disarm */
1960 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
1961 if (r < 0)
1962 return r;
1963
1964 d->next = USEC_INFINITY;
1965 return 0;
1966 }
1967
1968 b = prioq_peek(d->latest);
1969 assert_se(b && b->enabled != SD_EVENT_OFF);
1970
1971 t = sleep_between(e, a->time.next, b->time.next + b->time.accuracy);
1972 if (d->next == t)
1973 return 0;
1974
1975 assert_se(d->fd >= 0);
1976
1977 if (t == 0) {
1978 /* We don' want to disarm here, just mean some time looooong ago. */
1979 its.it_value.tv_sec = 0;
1980 its.it_value.tv_nsec = 1;
1981 } else
1982 timespec_store(&its.it_value, t);
1983
1984 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
1985 if (r < 0)
1986 return -errno;
1987
1988 d->next = t;
1989 return 0;
1990 }
1991
1992 static int process_io(sd_event *e, sd_event_source *s, uint32_t revents) {
1993 assert(e);
1994 assert(s);
1995 assert(s->type == SOURCE_IO);
1996
1997 /* If the event source was already pending, we just OR in the
1998 * new revents, otherwise we reset the value. The ORing is
1999 * necessary to handle EPOLLONESHOT events properly where
2000 * readability might happen independently of writability, and
2001 * we need to keep track of both */
2002
2003 if (s->pending)
2004 s->io.revents |= revents;
2005 else
2006 s->io.revents = revents;
2007
2008 return source_set_pending(s, true);
2009 }
2010
2011 static int flush_timer(sd_event *e, int fd, uint32_t events, usec_t *next) {
2012 uint64_t x;
2013 ssize_t ss;
2014
2015 assert(e);
2016 assert(fd >= 0);
2017
2018 assert_return(events == EPOLLIN, -EIO);
2019
2020 ss = read(fd, &x, sizeof(x));
2021 if (ss < 0) {
2022 if (errno == EAGAIN || errno == EINTR)
2023 return 0;
2024
2025 return -errno;
2026 }
2027
2028 if (_unlikely_(ss != sizeof(x)))
2029 return -EIO;
2030
2031 if (next)
2032 *next = USEC_INFINITY;
2033
2034 return 0;
2035 }
2036
2037 static int process_timer(
2038 sd_event *e,
2039 usec_t n,
2040 struct clock_data *d) {
2041
2042 sd_event_source *s;
2043 int r;
2044
2045 assert(e);
2046 assert(d);
2047
2048 for (;;) {
2049 s = prioq_peek(d->earliest);
2050 if (!s ||
2051 s->time.next > n ||
2052 s->enabled == SD_EVENT_OFF ||
2053 s->pending)
2054 break;
2055
2056 r = source_set_pending(s, true);
2057 if (r < 0)
2058 return r;
2059
2060 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
2061 prioq_reshuffle(d->latest, s, &s->time.latest_index);
2062 d->needs_rearm = true;
2063 }
2064
2065 return 0;
2066 }
2067
2068 static int process_child(sd_event *e) {
2069 sd_event_source *s;
2070 Iterator i;
2071 int r;
2072
2073 assert(e);
2074
2075 e->need_process_child = false;
2076
2077 /*
2078 So, this is ugly. We iteratively invoke waitid() with P_PID
2079 + WNOHANG for each PID we wait for, instead of using
2080 P_ALL. This is because we only want to get child
2081 information of very specific child processes, and not all
2082 of them. We might not have processed the SIGCHLD even of a
2083 previous invocation and we don't want to maintain a
2084 unbounded *per-child* event queue, hence we really don't
2085 want anything flushed out of the kernel's queue that we
2086 don't care about. Since this is O(n) this means that if you
2087 have a lot of processes you probably want to handle SIGCHLD
2088 yourself.
2089
2090 We do not reap the children here (by using WNOWAIT), this
2091 is only done after the event source is dispatched so that
2092 the callback still sees the process as a zombie.
2093 */
2094
2095 HASHMAP_FOREACH(s, e->child_sources, i) {
2096 assert(s->type == SOURCE_CHILD);
2097
2098 if (s->pending)
2099 continue;
2100
2101 if (s->enabled == SD_EVENT_OFF)
2102 continue;
2103
2104 zero(s->child.siginfo);
2105 r = waitid(P_PID, s->child.pid, &s->child.siginfo,
2106 WNOHANG | (s->child.options & WEXITED ? WNOWAIT : 0) | s->child.options);
2107 if (r < 0)
2108 return -errno;
2109
2110 if (s->child.siginfo.si_pid != 0) {
2111 bool zombie =
2112 s->child.siginfo.si_code == CLD_EXITED ||
2113 s->child.siginfo.si_code == CLD_KILLED ||
2114 s->child.siginfo.si_code == CLD_DUMPED;
2115
2116 if (!zombie && (s->child.options & WEXITED)) {
2117 /* If the child isn't dead then let's
2118 * immediately remove the state change
2119 * from the queue, since there's no
2120 * benefit in leaving it queued */
2121
2122 assert(s->child.options & (WSTOPPED|WCONTINUED));
2123 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|(s->child.options & (WSTOPPED|WCONTINUED)));
2124 }
2125
2126 r = source_set_pending(s, true);
2127 if (r < 0)
2128 return r;
2129 }
2130 }
2131
2132 return 0;
2133 }
2134
2135 static int process_signal(sd_event *e, struct signal_data *d, uint32_t events) {
2136 bool read_one = false;
2137 int r;
2138
2139 assert(e);
2140 assert_return(events == EPOLLIN, -EIO);
2141
2142 /* If there's a signal queued on this priority and SIGCHLD is
2143 on this priority too, then make sure to recheck the
2144 children we watch. This is because we only ever dequeue
2145 the first signal per priority, and if we dequeue one, and
2146 SIGCHLD might be enqueued later we wouldn't know, but we
2147 might have higher priority children we care about hence we
2148 need to check that explicitly. */
2149
2150 if (sigismember(&d->sigset, SIGCHLD))
2151 e->need_process_child = true;
2152
2153 /* If there's already an event source pending for this
2154 * priority we don't read another */
2155 if (d->current)
2156 return 0;
2157
2158 for (;;) {
2159 struct signalfd_siginfo si;
2160 ssize_t n;
2161 sd_event_source *s = NULL;
2162
2163 n = read(d->fd, &si, sizeof(si));
2164 if (n < 0) {
2165 if (errno == EAGAIN || errno == EINTR)
2166 return read_one;
2167
2168 return -errno;
2169 }
2170
2171 if (_unlikely_(n != sizeof(si)))
2172 return -EIO;
2173
2174 assert(si.ssi_signo < _NSIG);
2175
2176 read_one = true;
2177
2178 if (e->signal_sources)
2179 s = e->signal_sources[si.ssi_signo];
2180 if (!s)
2181 continue;
2182 if (s->pending)
2183 continue;
2184
2185 s->signal.siginfo = si;
2186 d->current = s;
2187
2188 r = source_set_pending(s, true);
2189 if (r < 0)
2190 return r;
2191
2192 return 1;
2193 }
2194 }
2195
2196 static int source_dispatch(sd_event_source *s) {
2197 int r = 0;
2198
2199 assert(s);
2200 assert(s->pending || s->type == SOURCE_EXIT);
2201
2202 if (s->type != SOURCE_DEFER && s->type != SOURCE_EXIT) {
2203 r = source_set_pending(s, false);
2204 if (r < 0)
2205 return r;
2206 }
2207
2208 if (s->type != SOURCE_POST) {
2209 sd_event_source *z;
2210 Iterator i;
2211
2212 /* If we execute a non-post source, let's mark all
2213 * post sources as pending */
2214
2215 SET_FOREACH(z, s->event->post_sources, i) {
2216 if (z->enabled == SD_EVENT_OFF)
2217 continue;
2218
2219 r = source_set_pending(z, true);
2220 if (r < 0)
2221 return r;
2222 }
2223 }
2224
2225 if (s->enabled == SD_EVENT_ONESHOT) {
2226 r = sd_event_source_set_enabled(s, SD_EVENT_OFF);
2227 if (r < 0)
2228 return r;
2229 }
2230
2231 s->dispatching = true;
2232
2233 switch (s->type) {
2234
2235 case SOURCE_IO:
2236 r = s->io.callback(s, s->io.fd, s->io.revents, s->userdata);
2237 break;
2238
2239 case SOURCE_TIME_REALTIME:
2240 case SOURCE_TIME_BOOTTIME:
2241 case SOURCE_TIME_MONOTONIC:
2242 case SOURCE_TIME_REALTIME_ALARM:
2243 case SOURCE_TIME_BOOTTIME_ALARM:
2244 r = s->time.callback(s, s->time.next, s->userdata);
2245 break;
2246
2247 case SOURCE_SIGNAL:
2248 r = s->signal.callback(s, &s->signal.siginfo, s->userdata);
2249 break;
2250
2251 case SOURCE_CHILD: {
2252 bool zombie;
2253
2254 zombie = s->child.siginfo.si_code == CLD_EXITED ||
2255 s->child.siginfo.si_code == CLD_KILLED ||
2256 s->child.siginfo.si_code == CLD_DUMPED;
2257
2258 r = s->child.callback(s, &s->child.siginfo, s->userdata);
2259
2260 /* Now, reap the PID for good. */
2261 if (zombie)
2262 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|WEXITED);
2263
2264 break;
2265 }
2266
2267 case SOURCE_DEFER:
2268 r = s->defer.callback(s, s->userdata);
2269 break;
2270
2271 case SOURCE_POST:
2272 r = s->post.callback(s, s->userdata);
2273 break;
2274
2275 case SOURCE_EXIT:
2276 r = s->exit.callback(s, s->userdata);
2277 break;
2278
2279 case SOURCE_WATCHDOG:
2280 case _SOURCE_EVENT_SOURCE_TYPE_MAX:
2281 case _SOURCE_EVENT_SOURCE_TYPE_INVALID:
2282 assert_not_reached("Wut? I shouldn't exist.");
2283 }
2284
2285 s->dispatching = false;
2286
2287 if (r < 0) {
2288 if (s->description)
2289 log_debug_errno(r, "Event source '%s' returned error, disabling: %m", s->description);
2290 else
2291 log_debug_errno(r, "Event source %p returned error, disabling: %m", s);
2292 }
2293
2294 if (s->n_ref == 0)
2295 source_free(s);
2296 else if (r < 0)
2297 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2298
2299 return 1;
2300 }
2301
2302 static int event_prepare(sd_event *e) {
2303 int r;
2304
2305 assert(e);
2306
2307 for (;;) {
2308 sd_event_source *s;
2309
2310 s = prioq_peek(e->prepare);
2311 if (!s || s->prepare_iteration == e->iteration || s->enabled == SD_EVENT_OFF)
2312 break;
2313
2314 s->prepare_iteration = e->iteration;
2315 r = prioq_reshuffle(e->prepare, s, &s->prepare_index);
2316 if (r < 0)
2317 return r;
2318
2319 assert(s->prepare);
2320
2321 s->dispatching = true;
2322 r = s->prepare(s, s->userdata);
2323 s->dispatching = false;
2324
2325 if (r < 0) {
2326 if (s->description)
2327 log_debug_errno(r, "Prepare callback of event source '%s' returned error, disabling: %m", s->description);
2328 else
2329 log_debug_errno(r, "Prepare callback of event source %p returned error, disabling: %m", s);
2330 }
2331
2332 if (s->n_ref == 0)
2333 source_free(s);
2334 else if (r < 0)
2335 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2336 }
2337
2338 return 0;
2339 }
2340
2341 static int dispatch_exit(sd_event *e) {
2342 sd_event_source *p;
2343 int r;
2344
2345 assert(e);
2346
2347 p = prioq_peek(e->exit);
2348 if (!p || p->enabled == SD_EVENT_OFF) {
2349 e->state = SD_EVENT_FINISHED;
2350 return 0;
2351 }
2352
2353 sd_event_ref(e);
2354 e->iteration++;
2355 e->state = SD_EVENT_EXITING;
2356
2357 r = source_dispatch(p);
2358
2359 e->state = SD_EVENT_INITIAL;
2360 sd_event_unref(e);
2361
2362 return r;
2363 }
2364
2365 static sd_event_source* event_next_pending(sd_event *e) {
2366 sd_event_source *p;
2367
2368 assert(e);
2369
2370 p = prioq_peek(e->pending);
2371 if (!p)
2372 return NULL;
2373
2374 if (p->enabled == SD_EVENT_OFF)
2375 return NULL;
2376
2377 return p;
2378 }
2379
2380 static int arm_watchdog(sd_event *e) {
2381 struct itimerspec its = {};
2382 usec_t t;
2383 int r;
2384
2385 assert(e);
2386 assert(e->watchdog_fd >= 0);
2387
2388 t = sleep_between(e,
2389 e->watchdog_last + (e->watchdog_period / 2),
2390 e->watchdog_last + (e->watchdog_period * 3 / 4));
2391
2392 timespec_store(&its.it_value, t);
2393
2394 /* Make sure we never set the watchdog to 0, which tells the
2395 * kernel to disable it. */
2396 if (its.it_value.tv_sec == 0 && its.it_value.tv_nsec == 0)
2397 its.it_value.tv_nsec = 1;
2398
2399 r = timerfd_settime(e->watchdog_fd, TFD_TIMER_ABSTIME, &its, NULL);
2400 if (r < 0)
2401 return -errno;
2402
2403 return 0;
2404 }
2405
2406 static int process_watchdog(sd_event *e) {
2407 assert(e);
2408
2409 if (!e->watchdog)
2410 return 0;
2411
2412 /* Don't notify watchdog too often */
2413 if (e->watchdog_last + e->watchdog_period / 4 > e->timestamp.monotonic)
2414 return 0;
2415
2416 sd_notify(false, "WATCHDOG=1");
2417 e->watchdog_last = e->timestamp.monotonic;
2418
2419 return arm_watchdog(e);
2420 }
2421
2422 _public_ int sd_event_prepare(sd_event *e) {
2423 int r;
2424
2425 assert_return(e, -EINVAL);
2426 assert_return(!event_pid_changed(e), -ECHILD);
2427 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2428 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2429
2430 if (e->exit_requested)
2431 goto pending;
2432
2433 e->iteration++;
2434
2435 r = event_prepare(e);
2436 if (r < 0)
2437 return r;
2438
2439 r = event_arm_timer(e, &e->realtime);
2440 if (r < 0)
2441 return r;
2442
2443 r = event_arm_timer(e, &e->boottime);
2444 if (r < 0)
2445 return r;
2446
2447 r = event_arm_timer(e, &e->monotonic);
2448 if (r < 0)
2449 return r;
2450
2451 r = event_arm_timer(e, &e->realtime_alarm);
2452 if (r < 0)
2453 return r;
2454
2455 r = event_arm_timer(e, &e->boottime_alarm);
2456 if (r < 0)
2457 return r;
2458
2459 if (event_next_pending(e) || e->need_process_child)
2460 goto pending;
2461
2462 e->state = SD_EVENT_ARMED;
2463
2464 return 0;
2465
2466 pending:
2467 e->state = SD_EVENT_ARMED;
2468 r = sd_event_wait(e, 0);
2469 if (r == 0)
2470 e->state = SD_EVENT_ARMED;
2471
2472 return r;
2473 }
2474
2475 _public_ int sd_event_wait(sd_event *e, uint64_t timeout) {
2476 struct epoll_event *ev_queue;
2477 unsigned ev_queue_max;
2478 int r, m, i;
2479
2480 assert_return(e, -EINVAL);
2481 assert_return(!event_pid_changed(e), -ECHILD);
2482 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2483 assert_return(e->state == SD_EVENT_ARMED, -EBUSY);
2484
2485 if (e->exit_requested) {
2486 e->state = SD_EVENT_PENDING;
2487 return 1;
2488 }
2489
2490 ev_queue_max = MAX(e->n_sources, 1u);
2491 ev_queue = newa(struct epoll_event, ev_queue_max);
2492
2493 m = epoll_wait(e->epoll_fd, ev_queue, ev_queue_max,
2494 timeout == (uint64_t) -1 ? -1 : (int) ((timeout + USEC_PER_MSEC - 1) / USEC_PER_MSEC));
2495 if (m < 0) {
2496 if (errno == EINTR) {
2497 e->state = SD_EVENT_PENDING;
2498 return 1;
2499 }
2500
2501 r = -errno;
2502 goto finish;
2503 }
2504
2505 dual_timestamp_get(&e->timestamp);
2506 e->timestamp_boottime = now(CLOCK_BOOTTIME);
2507
2508 for (i = 0; i < m; i++) {
2509
2510 if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_WATCHDOG))
2511 r = flush_timer(e, e->watchdog_fd, ev_queue[i].events, NULL);
2512 else {
2513 WakeupType *t = ev_queue[i].data.ptr;
2514
2515 switch (*t) {
2516
2517 case WAKEUP_EVENT_SOURCE:
2518 r = process_io(e, ev_queue[i].data.ptr, ev_queue[i].events);
2519 break;
2520
2521 case WAKEUP_CLOCK_DATA: {
2522 struct clock_data *d = ev_queue[i].data.ptr;
2523 r = flush_timer(e, d->fd, ev_queue[i].events, &d->next);
2524 break;
2525 }
2526
2527 case WAKEUP_SIGNAL_DATA:
2528 r = process_signal(e, ev_queue[i].data.ptr, ev_queue[i].events);
2529 break;
2530
2531 default:
2532 assert_not_reached("Invalid wake-up pointer");
2533 }
2534 }
2535 if (r < 0)
2536 goto finish;
2537 }
2538
2539 r = process_watchdog(e);
2540 if (r < 0)
2541 goto finish;
2542
2543 r = process_timer(e, e->timestamp.realtime, &e->realtime);
2544 if (r < 0)
2545 goto finish;
2546
2547 r = process_timer(e, e->timestamp_boottime, &e->boottime);
2548 if (r < 0)
2549 goto finish;
2550
2551 r = process_timer(e, e->timestamp.monotonic, &e->monotonic);
2552 if (r < 0)
2553 goto finish;
2554
2555 r = process_timer(e, e->timestamp.realtime, &e->realtime_alarm);
2556 if (r < 0)
2557 goto finish;
2558
2559 r = process_timer(e, e->timestamp_boottime, &e->boottime_alarm);
2560 if (r < 0)
2561 goto finish;
2562
2563 if (e->need_process_child) {
2564 r = process_child(e);
2565 if (r < 0)
2566 goto finish;
2567 }
2568
2569 if (event_next_pending(e)) {
2570 e->state = SD_EVENT_PENDING;
2571
2572 return 1;
2573 }
2574
2575 r = 0;
2576
2577 finish:
2578 e->state = SD_EVENT_INITIAL;
2579
2580 return r;
2581 }
2582
2583 _public_ int sd_event_dispatch(sd_event *e) {
2584 sd_event_source *p;
2585 int r;
2586
2587 assert_return(e, -EINVAL);
2588 assert_return(!event_pid_changed(e), -ECHILD);
2589 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2590 assert_return(e->state == SD_EVENT_PENDING, -EBUSY);
2591
2592 if (e->exit_requested)
2593 return dispatch_exit(e);
2594
2595 p = event_next_pending(e);
2596 if (p) {
2597 sd_event_ref(e);
2598
2599 e->state = SD_EVENT_RUNNING;
2600 r = source_dispatch(p);
2601 e->state = SD_EVENT_INITIAL;
2602
2603 sd_event_unref(e);
2604
2605 return r;
2606 }
2607
2608 e->state = SD_EVENT_INITIAL;
2609
2610 return 1;
2611 }
2612
2613 _public_ int sd_event_run(sd_event *e, uint64_t timeout) {
2614 int r;
2615
2616 assert_return(e, -EINVAL);
2617 assert_return(!event_pid_changed(e), -ECHILD);
2618 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2619 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2620
2621 r = sd_event_prepare(e);
2622 if (r == 0)
2623 /* There was nothing? Then wait... */
2624 r = sd_event_wait(e, timeout);
2625
2626 if (r > 0) {
2627 /* There's something now, then let's dispatch it */
2628 r = sd_event_dispatch(e);
2629 if (r < 0)
2630 return r;
2631
2632 return 1;
2633 }
2634
2635 return r;
2636 }
2637
2638 _public_ int sd_event_loop(sd_event *e) {
2639 int r;
2640
2641 assert_return(e, -EINVAL);
2642 assert_return(!event_pid_changed(e), -ECHILD);
2643 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2644
2645 sd_event_ref(e);
2646
2647 while (e->state != SD_EVENT_FINISHED) {
2648 r = sd_event_run(e, (uint64_t) -1);
2649 if (r < 0)
2650 goto finish;
2651 }
2652
2653 r = e->exit_code;
2654
2655 finish:
2656 sd_event_unref(e);
2657 return r;
2658 }
2659
2660 _public_ int sd_event_get_fd(sd_event *e) {
2661
2662 assert_return(e, -EINVAL);
2663 assert_return(!event_pid_changed(e), -ECHILD);
2664
2665 return e->epoll_fd;
2666 }
2667
2668 _public_ int sd_event_get_state(sd_event *e) {
2669 assert_return(e, -EINVAL);
2670 assert_return(!event_pid_changed(e), -ECHILD);
2671
2672 return e->state;
2673 }
2674
2675 _public_ int sd_event_get_exit_code(sd_event *e, int *code) {
2676 assert_return(e, -EINVAL);
2677 assert_return(code, -EINVAL);
2678 assert_return(!event_pid_changed(e), -ECHILD);
2679
2680 if (!e->exit_requested)
2681 return -ENODATA;
2682
2683 *code = e->exit_code;
2684 return 0;
2685 }
2686
2687 _public_ int sd_event_exit(sd_event *e, int code) {
2688 assert_return(e, -EINVAL);
2689 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2690 assert_return(!event_pid_changed(e), -ECHILD);
2691
2692 e->exit_requested = true;
2693 e->exit_code = code;
2694
2695 return 0;
2696 }
2697
2698 _public_ int sd_event_now(sd_event *e, clockid_t clock, uint64_t *usec) {
2699 assert_return(e, -EINVAL);
2700 assert_return(usec, -EINVAL);
2701 assert_return(!event_pid_changed(e), -ECHILD);
2702
2703 if (!dual_timestamp_is_set(&e->timestamp)) {
2704 /* Implicitly fall back to now() if we never ran
2705 * before and thus have no cached time. */
2706 *usec = now(clock);
2707 return 1;
2708 }
2709
2710 switch (clock) {
2711
2712 case CLOCK_REALTIME:
2713 case CLOCK_REALTIME_ALARM:
2714 *usec = e->timestamp.realtime;
2715 break;
2716
2717 case CLOCK_MONOTONIC:
2718 *usec = e->timestamp.monotonic;
2719 break;
2720
2721 case CLOCK_BOOTTIME:
2722 case CLOCK_BOOTTIME_ALARM:
2723 *usec = e->timestamp_boottime;
2724 break;
2725 }
2726
2727 return 0;
2728 }
2729
2730 _public_ int sd_event_default(sd_event **ret) {
2731
2732 static thread_local sd_event *default_event = NULL;
2733 sd_event *e = NULL;
2734 int r;
2735
2736 if (!ret)
2737 return !!default_event;
2738
2739 if (default_event) {
2740 *ret = sd_event_ref(default_event);
2741 return 0;
2742 }
2743
2744 r = sd_event_new(&e);
2745 if (r < 0)
2746 return r;
2747
2748 e->default_event_ptr = &default_event;
2749 e->tid = gettid();
2750 default_event = e;
2751
2752 *ret = e;
2753 return 1;
2754 }
2755
2756 _public_ int sd_event_get_tid(sd_event *e, pid_t *tid) {
2757 assert_return(e, -EINVAL);
2758 assert_return(tid, -EINVAL);
2759 assert_return(!event_pid_changed(e), -ECHILD);
2760
2761 if (e->tid != 0) {
2762 *tid = e->tid;
2763 return 0;
2764 }
2765
2766 return -ENXIO;
2767 }
2768
2769 _public_ int sd_event_set_watchdog(sd_event *e, int b) {
2770 int r;
2771
2772 assert_return(e, -EINVAL);
2773 assert_return(!event_pid_changed(e), -ECHILD);
2774
2775 if (e->watchdog == !!b)
2776 return e->watchdog;
2777
2778 if (b) {
2779 struct epoll_event ev = {};
2780
2781 r = sd_watchdog_enabled(false, &e->watchdog_period);
2782 if (r <= 0)
2783 return r;
2784
2785 /* Issue first ping immediately */
2786 sd_notify(false, "WATCHDOG=1");
2787 e->watchdog_last = now(CLOCK_MONOTONIC);
2788
2789 e->watchdog_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC);
2790 if (e->watchdog_fd < 0)
2791 return -errno;
2792
2793 r = arm_watchdog(e);
2794 if (r < 0)
2795 goto fail;
2796
2797 ev.events = EPOLLIN;
2798 ev.data.ptr = INT_TO_PTR(SOURCE_WATCHDOG);
2799
2800 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->watchdog_fd, &ev);
2801 if (r < 0) {
2802 r = -errno;
2803 goto fail;
2804 }
2805
2806 } else {
2807 if (e->watchdog_fd >= 0) {
2808 epoll_ctl(e->epoll_fd, EPOLL_CTL_DEL, e->watchdog_fd, NULL);
2809 e->watchdog_fd = safe_close(e->watchdog_fd);
2810 }
2811 }
2812
2813 e->watchdog = !!b;
2814 return e->watchdog;
2815
2816 fail:
2817 e->watchdog_fd = safe_close(e->watchdog_fd);
2818 return r;
2819 }
2820
2821 _public_ int sd_event_get_watchdog(sd_event *e) {
2822 assert_return(e, -EINVAL);
2823 assert_return(!event_pid_changed(e), -ECHILD);
2824
2825 return e->watchdog;
2826 }