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