Line data Source code
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 57 : 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 : bool force_reset) {
21 :
22 57 : bool created = false;
23 : int enabled, r;
24 : clockid_t c;
25 :
26 57 : assert(e);
27 57 : assert(s);
28 :
29 57 : if (*s) {
30 43 : 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 : strna((*s)->description ?: description));
35 :
36 0 : if (enabled != SD_EVENT_OFF)
37 0 : return 0;
38 : }
39 :
40 43 : r = sd_event_source_get_time_clock(*s, &c);
41 43 : 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 :
44 43 : if (c != clock)
45 0 : return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
46 : "sd-event: Current clock id %i of event source \"%s\" is different from specified one %i.",
47 : (int)c,
48 : strna((*s)->description ? : description),
49 : (int)clock);
50 :
51 43 : r = sd_event_source_set_time(*s, usec);
52 43 : 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 :
55 43 : r = sd_event_source_set_time_accuracy(*s, accuracy);
56 43 : 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 :
59 : /* callback function is not updated, as we do not have sd_event_source_set_time_callback(). */
60 :
61 43 : (void) sd_event_source_set_userdata(*s, userdata);
62 :
63 43 : r = sd_event_source_set_enabled(*s, SD_EVENT_ONESHOT);
64 43 : if (r < 0)
65 0 : return log_debug_errno(r, "sd-event: Failed to enable event source \"%s\": %m", strna((*s)->description ?: description));
66 : } else {
67 14 : r = sd_event_add_time(e, s, clock, usec, accuracy, callback, userdata);
68 14 : if (r < 0)
69 0 : return log_debug_errno(r, "sd-event: Failed to create timer event \"%s\": %m", strna(description));
70 :
71 14 : created = true;
72 : }
73 :
74 57 : r = sd_event_source_set_priority(*s, priority);
75 57 : 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 :
78 57 : if (description) {
79 57 : r = sd_event_source_set_description(*s, description);
80 57 : if (r < 0)
81 0 : return log_debug_errno(r, "sd-event: Failed to set description for event source \"%s\": %m", description);
82 : }
83 :
84 57 : return created;
85 : }
86 :
87 91 : int event_source_disable(sd_event_source *s) {
88 91 : if (!s)
89 67 : return 0;
90 :
91 24 : return sd_event_source_set_enabled(s, SD_EVENT_OFF);
92 : }
93 :
94 0 : int event_source_is_enabled(sd_event_source *s) {
95 0 : if (!s)
96 0 : return false;
97 :
98 0 : return sd_event_source_get_enabled(s, NULL);
99 : }
|