]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-event: let sd_event_source_set_enabled accept NULL
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 14 Jun 2022 12:40:30 +0000 (14:40 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 30 Jun 2022 08:35:27 +0000 (10:35 +0200)
Same story as before: disabling a non-existent event source shouldn't
need to be guarded by an if. I retained the wrapper so that that we don't
have to say SD_EVENT_OFF in the many places where this is called.

man/sd_event_source_set_enabled.xml
src/libsystemd/sd-event/event-util.c
src/libsystemd/sd-event/event-util.h
src/libsystemd/sd-event/sd-event.c

index d6cdf85eda910cf990153bfedf4ed43f44bd7701..5f13fc101d0d923465139a2f2b32482b033121f6 100644 (file)
   <refsect1>
     <title>Description</title>
 
-    <para><function>sd_event_source_set_enabled()</function> may be
-    used to enable or disable the event source object specified as
-    <parameter>source</parameter>. The <parameter>enabled</parameter>
-    parameter takes one of <constant>SD_EVENT_ON</constant> (to
-    enable), <constant>SD_EVENT_OFF</constant> (to disable) or
-    <constant>SD_EVENT_ONESHOT</constant>. If invoked with
-    <constant>SD_EVENT_ONESHOT</constant> the event source will be
-    enabled but automatically reset to
-    <constant>SD_EVENT_OFF</constant> after the event source was
-    dispatched once.</para>
+    <para><function>sd_event_source_set_enabled()</function> may be used to enable or disable the event
+    source object specified as <parameter>source</parameter>. The <parameter>enabled</parameter> parameter
+    takes one of <constant>SD_EVENT_ON</constant> (to enable), <constant>SD_EVENT_OFF</constant> (to disable)
+    or <constant>SD_EVENT_ONESHOT</constant>. If invoked with <constant>SD_EVENT_ONESHOT</constant> the event
+    source will be enabled but automatically reset to <constant>SD_EVENT_OFF</constant> after one dispatch.
+    For <constant>SD_EVENT_OFF</constant>, the event source <parameter>source</parameter> may be
+    <constant>NULL</constant>, in which case the function does nothing. Otherwise,
+    <parameter>source</parameter> must be a valid pointer to an <structname>sd_event_source</structname>
+    object.</para>
 
     <para>Event sources that are disabled will not result in event
     loop wakeups and will not be dispatched, until they are enabled
index 3a7afe9ae3c4ec56ac9f023b794f02763983d2c3..a36eba902932fabdc0ed9590e0b2a885f7327c74 100644 (file)
@@ -109,13 +109,6 @@ int event_reset_time_relative(
         return event_reset_time(e, s, clock, usec_add(usec_now, usec), accuracy, callback, userdata, priority, description, force_reset);
 }
 
-int event_source_disable(sd_event_source *s) {
-        if (!s)
-                return 0;
-
-        return sd_event_source_set_enabled(s, SD_EVENT_OFF);
-}
-
 int event_add_time_change(sd_event *e, sd_event_source **ret, sd_event_io_handler_t callback, void *userdata) {
         _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
         _cleanup_close_ int fd = -1;
index 7bab42ed8fed75656b626aeb0a9f5e4828918ea7..c185584412314dfd2066d78d0e5da1fffa7b3a0f 100644 (file)
@@ -27,6 +27,8 @@ int event_reset_time_relative(
                 int64_t priority,
                 const char *description,
                 bool force_reset);
-int event_source_disable(sd_event_source *s);
+static inline int event_source_disable(sd_event_source *s) {
+        return sd_event_source_set_enabled(s, SD_EVENT_OFF);
+}
 
 int event_add_time_change(sd_event *e, sd_event_source **ret, sd_event_io_handler_t callback, void *userdata);
index 94431d0725ca0f0d254a137ed49f91bd696bd448..cea1c009d61c64b445862f140d86b8d27be640bc 100644 (file)
@@ -2594,8 +2594,13 @@ static int event_source_online(
 _public_ int sd_event_source_set_enabled(sd_event_source *s, int m) {
         int r;
 
-        assert_return(s, -EINVAL);
         assert_return(IN_SET(m, SD_EVENT_OFF, SD_EVENT_ON, SD_EVENT_ONESHOT), -EINVAL);
+
+        /* Quick mode: if the source doesn't exist, SD_EVENT_OFF is a noop. */
+        if (m == SD_EVENT_OFF && !s)
+                return 0;
+
+        assert_return(s, -EINVAL);
         assert_return(!event_pid_changed(s->event), -ECHILD);
 
         /* If we are dead anyway, we are fine with turning off sources, but everything else needs to fail. */