Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */
2 : #ifndef foosdeventhfoo
3 : #define foosdeventhfoo
4 :
5 : /***
6 : systemd is free software; you can redistribute it and/or modify it
7 : under the terms of the GNU Lesser General Public License as published by
8 : the Free Software Foundation; either version 2.1 of the License, or
9 : (at your option) any later version.
10 :
11 : systemd is distributed in the hope that it will be useful, but
12 : WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : Lesser General Public License for more details.
15 :
16 : You should have received a copy of the GNU Lesser General Public License
17 : along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 : ***/
19 :
20 : #include <inttypes.h>
21 : #include <signal.h>
22 : #include <sys/epoll.h>
23 : #include <sys/inotify.h>
24 : #include <sys/signalfd.h>
25 : #include <sys/types.h>
26 : #include <time.h>
27 :
28 : #include "_sd-common.h"
29 :
30 : /*
31 : Why is this better than pure epoll?
32 :
33 : - Supports event source prioritization
34 : - Scales better with a large number of time events because it does not require one timerfd each
35 : - Automatically tries to coalesce timer events system-wide
36 : - Handles signals, child PIDs, inotify events
37 : - Supports systemd-style automatic watchdog event generation
38 : */
39 :
40 : _SD_BEGIN_DECLARATIONS;
41 :
42 : #define SD_EVENT_DEFAULT ((sd_event *) 1)
43 :
44 : typedef struct sd_event sd_event;
45 : typedef struct sd_event_source sd_event_source;
46 :
47 : enum {
48 : SD_EVENT_OFF = 0,
49 : SD_EVENT_ON = 1,
50 : SD_EVENT_ONESHOT = -1
51 : };
52 :
53 : enum {
54 : SD_EVENT_INITIAL,
55 : SD_EVENT_ARMED,
56 : SD_EVENT_PENDING,
57 : SD_EVENT_RUNNING,
58 : SD_EVENT_EXITING,
59 : SD_EVENT_FINISHED,
60 : SD_EVENT_PREPARING
61 : };
62 :
63 : enum {
64 : /* And everything in-between and outside is good too */
65 : SD_EVENT_PRIORITY_IMPORTANT = -100,
66 : SD_EVENT_PRIORITY_NORMAL = 0,
67 : SD_EVENT_PRIORITY_IDLE = 100
68 : };
69 :
70 : typedef int (*sd_event_handler_t)(sd_event_source *s, void *userdata);
71 : typedef int (*sd_event_io_handler_t)(sd_event_source *s, int fd, uint32_t revents, void *userdata);
72 : typedef int (*sd_event_time_handler_t)(sd_event_source *s, uint64_t usec, void *userdata);
73 : typedef int (*sd_event_signal_handler_t)(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata);
74 : #if defined _GNU_SOURCE || (defined _POSIX_C_SOURCE && _POSIX_C_SOURCE >= 199309L)
75 : typedef int (*sd_event_child_handler_t)(sd_event_source *s, const siginfo_t *si, void *userdata);
76 : #else
77 : typedef void* sd_event_child_handler_t;
78 : #endif
79 : typedef int (*sd_event_inotify_handler_t)(sd_event_source *s, const struct inotify_event *event, void *userdata);
80 : typedef _sd_destroy_t sd_event_destroy_t;
81 :
82 : int sd_event_default(sd_event **e);
83 :
84 : int sd_event_new(sd_event **e);
85 : sd_event* sd_event_ref(sd_event *e);
86 : sd_event* sd_event_unref(sd_event *e);
87 :
88 : int sd_event_add_io(sd_event *e, sd_event_source **s, int fd, uint32_t events, sd_event_io_handler_t callback, void *userdata);
89 : int sd_event_add_time(sd_event *e, sd_event_source **s, clockid_t clock, uint64_t usec, uint64_t accuracy, sd_event_time_handler_t callback, void *userdata);
90 : int sd_event_add_signal(sd_event *e, sd_event_source **s, int sig, sd_event_signal_handler_t callback, void *userdata);
91 : int sd_event_add_child(sd_event *e, sd_event_source **s, pid_t pid, int options, sd_event_child_handler_t callback, void *userdata);
92 : int sd_event_add_inotify(sd_event *e, sd_event_source **s, const char *path, uint32_t mask, sd_event_inotify_handler_t callback, void *userdata);
93 : int sd_event_add_defer(sd_event *e, sd_event_source **s, sd_event_handler_t callback, void *userdata);
94 : int sd_event_add_post(sd_event *e, sd_event_source **s, sd_event_handler_t callback, void *userdata);
95 : int sd_event_add_exit(sd_event *e, sd_event_source **s, sd_event_handler_t callback, void *userdata);
96 :
97 : int sd_event_prepare(sd_event *e);
98 : int sd_event_wait(sd_event *e, uint64_t usec);
99 : int sd_event_dispatch(sd_event *e);
100 : int sd_event_run(sd_event *e, uint64_t usec);
101 : int sd_event_loop(sd_event *e);
102 : int sd_event_exit(sd_event *e, int code);
103 :
104 : int sd_event_now(sd_event *e, clockid_t clock, uint64_t *usec);
105 :
106 : int sd_event_get_fd(sd_event *e);
107 : int sd_event_get_state(sd_event *e);
108 : int sd_event_get_tid(sd_event *e, pid_t *tid);
109 : int sd_event_get_exit_code(sd_event *e, int *code);
110 : int sd_event_set_watchdog(sd_event *e, int b);
111 : int sd_event_get_watchdog(sd_event *e);
112 : int sd_event_get_iteration(sd_event *e, uint64_t *ret);
113 :
114 : sd_event_source* sd_event_source_ref(sd_event_source *s);
115 : sd_event_source* sd_event_source_unref(sd_event_source *s);
116 : sd_event_source* sd_event_source_disable_unref(sd_event_source *s);
117 :
118 : sd_event *sd_event_source_get_event(sd_event_source *s);
119 : void* sd_event_source_get_userdata(sd_event_source *s);
120 : void* sd_event_source_set_userdata(sd_event_source *s, void *userdata);
121 :
122 : int sd_event_source_set_description(sd_event_source *s, const char *description);
123 : int sd_event_source_get_description(sd_event_source *s, const char **description);
124 : int sd_event_source_set_prepare(sd_event_source *s, sd_event_handler_t callback);
125 : int sd_event_source_get_pending(sd_event_source *s);
126 : int sd_event_source_get_priority(sd_event_source *s, int64_t *priority);
127 : int sd_event_source_set_priority(sd_event_source *s, int64_t priority);
128 : int sd_event_source_get_enabled(sd_event_source *s, int *enabled);
129 : int sd_event_source_set_enabled(sd_event_source *s, int enabled);
130 : int sd_event_source_get_io_fd(sd_event_source *s);
131 : int sd_event_source_set_io_fd(sd_event_source *s, int fd);
132 : int sd_event_source_get_io_fd_own(sd_event_source *s);
133 : int sd_event_source_set_io_fd_own(sd_event_source *s, int own);
134 : int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events);
135 : int sd_event_source_set_io_events(sd_event_source *s, uint32_t events);
136 : int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents);
137 : int sd_event_source_get_time(sd_event_source *s, uint64_t *usec);
138 : int sd_event_source_set_time(sd_event_source *s, uint64_t usec);
139 : int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec);
140 : int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec);
141 : int sd_event_source_get_time_clock(sd_event_source *s, clockid_t *clock);
142 : int sd_event_source_get_signal(sd_event_source *s);
143 : int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid);
144 : int sd_event_source_get_inotify_mask(sd_event_source *s, uint32_t *ret);
145 : int sd_event_source_set_destroy_callback(sd_event_source *s, sd_event_destroy_t callback);
146 : int sd_event_source_get_destroy_callback(sd_event_source *s, sd_event_destroy_t *ret);
147 : int sd_event_source_get_floating(sd_event_source *s);
148 : int sd_event_source_set_floating(sd_event_source *s, int b);
149 :
150 : /* Define helpers so that __attribute__((cleanup(sd_event_unrefp))) and similar may be used. */
151 51903 : _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_event, sd_event_unref);
152 18 : _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_event_source, sd_event_source_unref);
153 : _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_event_source, sd_event_source_disable_unref);
154 :
155 : _SD_END_DECLARATIONS;
156 :
157 : #endif
|