/src/systemd/src/libsystemd/sd-event/event-util.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2 | | |
3 | | #include <errno.h> |
4 | | |
5 | | #include "event-source.h" |
6 | | #include "event-util.h" |
7 | | #include "log.h" |
8 | | #include "string-util.h" |
9 | | |
10 | | int event_reset_time( |
11 | | sd_event *e, |
12 | | sd_event_source **s, |
13 | | clockid_t clock, |
14 | | uint64_t usec, |
15 | | uint64_t accuracy, |
16 | | sd_event_time_handler_t callback, |
17 | | void *userdata, |
18 | | int64_t priority, |
19 | | const char *description, |
20 | 5.24k | bool force_reset) { |
21 | 5.24k | |
22 | 5.24k | bool created = false; |
23 | 5.24k | int enabled, r; |
24 | 5.24k | clockid_t c; |
25 | 5.24k | |
26 | 5.24k | assert(e); |
27 | 5.24k | assert(s); |
28 | 5.24k | |
29 | 5.24k | if (*s) { |
30 | 1 | if (!force_reset) { |
31 | 0 | r = sd_event_source_get_enabled(*s, &enabled); |
32 | 0 | if (r < 0) |
33 | 0 | return log_debug_errno(r, "sd-event: Failed to query whether event source \"%s\" is enabled or not: %m", |
34 | 0 | strna((*s)->description ?: description)); |
35 | 0 | |
36 | 0 | if (enabled != SD_EVENT_OFF) |
37 | 0 | return 0; |
38 | 1 | } |
39 | 1 | |
40 | 1 | r = sd_event_source_get_time_clock(*s, &c); |
41 | 1 | if (r < 0) |
42 | 0 | return log_debug_errno(r, "sd-event: Failed to get clock id of event source \"%s\": %m", strna((*s)->description ?: description)); |
43 | 1 | |
44 | 1 | if (c != clock) |
45 | 0 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), |
46 | 1 | "sd-event: Current clock id %i of event source \"%s\" is different from specified one %i.", |
47 | 1 | (int)c, |
48 | 1 | strna((*s)->description ? : description), |
49 | 1 | (int)clock); |
50 | 1 | |
51 | 1 | r = sd_event_source_set_time(*s, usec); |
52 | 1 | if (r < 0) |
53 | 0 | return log_debug_errno(r, "sd-event: Failed to set time for event source \"%s\": %m", strna((*s)->description ?: description)); |
54 | 1 | |
55 | 1 | r = sd_event_source_set_time_accuracy(*s, accuracy); |
56 | 1 | if (r < 0) |
57 | 0 | return log_debug_errno(r, "sd-event: Failed to set accuracy for event source \"%s\": %m", strna((*s)->description ?: description)); |
58 | 1 | |
59 | 1 | /* callback function is not updated, as we do not have sd_event_source_set_time_callback(). */ |
60 | 1 | |
61 | 1 | (void) sd_event_source_set_userdata(*s, userdata); |
62 | 1 | |
63 | 1 | r = sd_event_source_set_enabled(*s, SD_EVENT_ONESHOT); |
64 | 1 | if (r < 0) |
65 | 0 | return log_debug_errno(r, "sd-event: Failed to enable event source \"%s\": %m", strna((*s)->description ?: description)); |
66 | 5.24k | } else { |
67 | 5.24k | r = sd_event_add_time(e, s, clock, usec, accuracy, callback, userdata); |
68 | 5.24k | if (r < 0) |
69 | 0 | return log_debug_errno(r, "sd-event: Failed to create timer event \"%s\": %m", strna(description)); |
70 | 5.24k | |
71 | 5.24k | created = true; |
72 | 5.24k | } |
73 | 5.24k | |
74 | 5.24k | r = sd_event_source_set_priority(*s, priority); |
75 | 5.24k | if (r < 0) |
76 | 0 | return log_debug_errno(r, "sd-event: Failed to set priority for event source \"%s\": %m", strna((*s)->description ?: description)); |
77 | 5.24k | |
78 | 5.24k | if (description) { |
79 | 5.24k | r = sd_event_source_set_description(*s, description); |
80 | 5.24k | if (r < 0) |
81 | 0 | return log_debug_errno(r, "sd-event: Failed to set description for event source \"%s\": %m", description); |
82 | 5.24k | } |
83 | 5.24k | |
84 | 5.24k | return created; |
85 | 5.24k | } |
86 | | |
87 | 62.4k | int event_source_disable(sd_event_source *s) { |
88 | 62.4k | if (!s) |
89 | 56.2k | return 0; |
90 | 6.22k | |
91 | 6.22k | return sd_event_source_set_enabled(s, SD_EVENT_OFF); |
92 | 6.22k | } |
93 | | |
94 | 0 | int event_source_is_enabled(sd_event_source *s) { |
95 | 0 | if (!s) |
96 | 0 | return false; |
97 | 0 | |
98 | 0 | return sd_event_source_get_enabled(s, NULL); |
99 | 0 | } |