Debian Codegraph @

src/libsystemd/sd-event/event-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-event.h"
4
5 #include "alloc-util.h"
6 #include "event-source.h"
7 #include "event-util.h"
8 #include "fd-util.h"
9 #include "hash-funcs.h"
10 #include "log.h"
11 #include "pidref.h"
12 #include "string-util.h"
13 #include "time-util.h"
14
15 #define SI_FLAG_FORWARD (INT32_C(1) << 30)
16 #define SI_FLAG_POSITIVE (INT32_C(1) << 29)
17
18 DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
19 event_source_hash_ops,
20 void, trivial_hash_func, trivial_compare_func,
21 sd_event_source, sd_event_source_disable_unref);
22
23 int event_reset_time(
24 sd_event *e,
25 sd_event_source **s,
26 clockid_t clock,
27 uint64_t usec,
28 uint64_t accuracy,
29 sd_event_time_handler_t callback,
30 void *userdata,
31 int64_t priority,
32 const char *description,
33 bool force_reset) {
34
35 bool created = false;
36 int enabled, r;
37 clockid_t c;
38
39 assert(e);
40 assert(s);
41
42 if (*s) {
43 if (!force_reset) {
44 r = sd_event_source_get_enabled(*s, &enabled);
45 if (r < 0)
46 return log_debug_errno(r, "sd-event: Failed to query whether event source \"%s\" is enabled or not: %m",
47 strna((*s)->description ?: description));
48
49 if (enabled != SD_EVENT_OFF)
50 return 0;
51 }
52
53 r = sd_event_source_get_time_clock(*s, &c);
54 if (r < 0)
55 return log_debug_errno(r, "sd-event: Failed to get clock id of event source \"%s\": %m", strna((*s)->description ?: description));
56
57 if (c != clock)
58 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
59 "sd-event: Current clock id %i of event source \"%s\" is different from specified one %i.",
60 (int)c,
61 strna((*s)->description ?: description),
62 (int)clock);
63
64 r = sd_event_source_set_time(*s, usec);
65 if (r < 0)
66 return log_debug_errno(r, "sd-event: Failed to set time for event source \"%s\": %m", strna((*s)->description ?: description));
67
68 r = sd_event_source_set_time_accuracy(*s, accuracy);
69 if (r < 0)
70 return log_debug_errno(r, "sd-event: Failed to set accuracy for event source \"%s\": %m", strna((*s)->description ?: description));
71
72 /* callback function is not updated, as we do not have sd_event_source_set_time_callback(). */
73
74 (void) sd_event_source_set_userdata(*s, userdata);
75
76 r = sd_event_source_set_enabled(*s, SD_EVENT_ONESHOT);
77 if (r < 0)
78 return log_debug_errno(r, "sd-event: Failed to enable event source \"%s\": %m", strna((*s)->description ?: description));
79 } else {
80 r = sd_event_add_time(e, s, clock, usec, accuracy, callback, userdata);
81 if (r < 0)
82 return log_debug_errno(r, "sd-event: Failed to create timer event \"%s\": %m", strna(description));
83
84 created = true;
85 }
86
87 r = sd_event_source_set_priority(*s, priority);
88 if (r < 0)
89 return log_debug_errno(r, "sd-event: Failed to set priority for event source \"%s\": %m", strna((*s)->description ?: description));
90
91 if (description) {
92 r = sd_event_source_set_description(*s, description);
93 if (r < 0)
94 return log_debug_errno(r, "sd-event: Failed to set description for event source \"%s\": %m", description);
95 }
96
97 return created;
98 }
99
100 int event_reset_time_relative(
101 sd_event *e,
102 sd_event_source **s,
103 clockid_t clock,
104 uint64_t usec,
105 uint64_t accuracy,
106 sd_event_time_handler_t callback,
107 void *userdata,
108 int64_t priority,
109 const char *description,
110 bool force_reset) {
111
112 int r;
113
114 assert(e);
115
116 if (usec > 0) {
117 usec_t usec_now;
118
119 r = sd_event_now(e, clock, &usec_now);
120 if (r < 0)
121 return log_debug_errno(r, "sd-event: Failed to get the current time: %m");
122
123 usec = usec_add(usec_now, usec);
124 }
125
126 return event_reset_time(e, s, clock, usec, accuracy, callback, userdata, priority, description, force_reset);
127 }
128
129 int event_add_time_change(sd_event *e, sd_event_source **ret, sd_event_io_handler_t callback, void *userdata) {
130 _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
131 _cleanup_close_ int fd = -EBADF;
132 int r;
133
134 assert(e);
135
136 /* Allocates an IO event source that gets woken up whenever the clock changes. Needs to be recreated on each event */
137
138 fd = time_change_fd();
139 if (fd < 0)
140 return fd;
141
142 r = sd_event_add_io(e, &s, fd, EPOLLIN, callback, userdata);
143 if (r < 0)
144 return r;
145
146 r = sd_event_source_set_io_fd_own(s, true);
147 if (r < 0)
148 return r;
149
150 TAKE_FD(fd);
151
152 r = sd_event_source_set_description(s, "time-change");
153 if (r < 0)
154 return r;
155
156 if (ret)
157 *ret = TAKE_PTR(s);
158 else {
159 r = sd_event_source_set_floating(s, true);
160 if (r < 0)
161 return r;
162 }
163
164 return 0;
165 }
166
167 int event_add_child_pidref(
168 sd_event *e,
169 sd_event_source **ret,
170 const PidRef *pid,
171 int options,
172 sd_event_child_handler_t callback,
173 void *userdata) {
174
175 int r;
176
177 assert(e);
178
179 if (!pidref_is_set(pid))
180 return -ESRCH;
181
182 if (pidref_is_remote(pid))
183 return -EREMOTE;
184
185 if (pid->fd < 0)
186 return sd_event_add_child(e, ret, pid->pid, options, callback, userdata);
187
188 _cleanup_close_ int copy_fd = fcntl(pid->fd, F_DUPFD_CLOEXEC, 3);
189 if (copy_fd < 0)
190 return -errno;
191
192 _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
193 r = sd_event_add_child_pidfd(e, &s, copy_fd, options, callback, userdata);
194 if (r < 0)
195 return r;
196
197 r = sd_event_source_set_child_pidfd_own(s, true);
198 if (r < 0)
199 return r;
200
201 TAKE_FD(copy_fd);
202
203 if (ret)
204 *ret = TAKE_PTR(s);
205 else {
206 r = sd_event_source_set_floating(s, true);
207 if (r < 0)
208 return r;
209 }
210
211 return 0;
212 }
213
214 int event_source_get_child_pidref(sd_event_source *s, PidRef *ret) {
215 int r;
216
217 assert(s);
218 assert(ret);
219
220 pid_t pid;
221 r = sd_event_source_get_child_pid(s, &pid);
222 if (r < 0)
223 return r;
224
225 int pidfd = sd_event_source_get_child_pidfd(s);
226 if (pidfd < 0)
227 return pidfd;
228
229 /* Note, we don't actually duplicate the fd here, i.e. we do not pass ownership of this PidRef to the caller */
230 *ret = (PidRef) {
231 .pid = pid,
232 .fd = pidfd,
233 };
234
235 return 0;
236 }
237
238 dual_timestamp* event_dual_timestamp_now(sd_event *e, dual_timestamp *ts) {
239 assert(e);
240 assert(ts);
241
242 assert_se(sd_event_now(e, CLOCK_REALTIME, &ts->realtime) >= 0);
243 assert_se(sd_event_now(e, CLOCK_MONOTONIC, &ts->monotonic) >= 0);
244 return ts;
245 }
246
247 void event_source_unref_many(sd_event_source **array, size_t n) {
248 FOREACH_ARRAY(v, array, n)
249 sd_event_source_unref(*v);
250
251 free(array);
252 }
253
254 static int event_forward_signal_callback(sd_event_source *s, const struct signalfd_siginfo *ssi, void *userdata) {
255 sd_event_source *child = ASSERT_PTR(userdata);
256
257 assert(ssi);
258
259 siginfo_t si = {
260 .si_signo = ssi->ssi_signo,
261 /* We include some extra information to indicate the signal was forwarded and originally a positive
262 * value since we can only set negative values ourselves as positive values are prohibited by the
263 * kernel. */
264 .si_code = (ssi->ssi_code & (SI_FLAG_FORWARD|SI_FLAG_POSITIVE)) ? INT_MIN :
265 (ssi->ssi_code >= 0 ? (-ssi->ssi_code - 1) | SI_FLAG_POSITIVE | SI_FLAG_FORWARD : ssi->ssi_code | SI_FLAG_FORWARD),
266 .si_errno = ssi->ssi_errno,
267 };
268
269 /* The following fields are implemented as macros, hence we cannot use compound initialization for them. */
270 si.si_pid = ssi->ssi_pid;
271 si.si_uid = ssi->ssi_uid;
272 si.si_int = ssi->ssi_int;
273 si.si_ptr = UINT64_TO_PTR(ssi->ssi_ptr);
274
275 return sd_event_source_send_child_signal(child, ssi->ssi_signo, &si, /* flags= */ 0);
276 }
277
278 static void event_forward_signal_destroy(void *userdata) {
279 sd_event_source *child = ASSERT_PTR(userdata);
280 sd_event_source_unref(child);
281 }
282
283 int event_forward_signals(
284 sd_event *e,
285 sd_event_source *child,
286 const int *signals,
287 size_t n_signals,
288 sd_event_source ***ret_sources,
289 size_t *ret_n_sources) {
290
291 sd_event_source **sources = NULL;
292 size_t n_sources = 0;
293 int r;
294
295 CLEANUP_ARRAY(sources, n_sources, event_source_unref_many);
296
297 assert(e);
298 assert(child);
299 assert(child->type == SOURCE_CHILD);
300 assert(signals || n_signals == 0);
301 assert(ret_sources);
302 assert(ret_n_sources);
303
304 if (n_signals == 0) {
305 *ret_sources = NULL;
306 *ret_n_sources = 0;
307 return 0;
308 }
309
310 sources = new0(sd_event_source*, n_signals);
311 if (!sources)
312 return -ENOMEM;
313
314 FOREACH_ARRAY(sig, signals, n_signals) {
315 _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
316 r = sd_event_add_signal(e, &s, *sig | SD_EVENT_SIGNAL_PROCMASK, event_forward_signal_callback, child);
317 if (r < 0)
318 return r;
319
320 r = sd_event_source_set_destroy_callback(s, event_forward_signal_destroy);
321 if (r < 0)
322 return r;
323
324 sd_event_source_ref(child);
325 sources[n_sources++] = TAKE_PTR(s);
326 }
327
328 *ret_sources = TAKE_PTR(sources);
329 *ret_n_sources = n_sources;
330
331 return 0;
332 }