Rework systemd listener to use module API
authorKrzysztof Opasiak <k.opasiak@samsung.com>
Mon, 24 Apr 2017 20:02:33 +0000 (22:02 +0200)
committerKrzysztof Opasiak <k.opasiak@samsung.com>
Mon, 24 Apr 2017 20:03:57 +0000 (22:03 +0200)
Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
src/listeners/systemd.c

index 7dc28bdbe57d58ac2a09ce55a97049352ce62490..b189d1cb2fb8ede8b0dc5a40292c20dc2e8d119c 100644 (file)
 
 #include "systemd.h"
 #include "log.h"
+#include "module.h"
 
-static int on_unit_properties_changed(sd_bus_message *m, void *userdata,
-                                      sd_bus_error *ret_error);
-
-int faultd_systemd_init(sd_event* loop)
-{
-        sd_bus_error error = SD_BUS_ERROR_NULL;
-        sd_bus *bus = NULL;
-        int rc;
-
-        rc = sd_bus_default_system(&bus);
-        if (rc < 0) {
-                log_error_errno(rc, "Failed to acquire the defult system bus connection.");
-                return -1;
-        }
-
-        rc = sd_bus_attach_event(bus, loop, SD_EVENT_PRIORITY_NORMAL);
-        if (rc < 0) {
-                log_error_errno(rc, "Failed to attach the bus to the event loop.");
-                return -1;
-        }
-
-        rc = sd_bus_add_match(bus,
-                              NULL,
-                              "type='signal',sender='org.freedesktop.systemd1',"
-                              "interface='org.freedesktop.DBus.Properties',"
-                              "member='PropertiesChanged',"
-                              "path_namespace='/org/freedesktop/systemd1/unit'",
-                              on_unit_properties_changed,
-                              NULL);
-        if (rc < 0) {
-                log_error_errno(rc, "Failed to add match.");
-                return -1;
-        }
-
-        rc = sd_bus_call_method(bus,
-                                "org.freedesktop.systemd1",
-                                "/org/freedesktop/systemd1",
-                                "org.freedesktop.systemd1.Manager",
-                                "Subscribe",
-                                &error,
-                                NULL, NULL);
-        if (rc < 0) {
-                log_error_errno(rc, "Failed to subscribe.");
-                return -1;
-        }
-
-        return 0;
-}
+struct systemd_listener {
+        struct faultd_module module;
+        /* Put your data here */
+};
 
-int faultd_systemd_close()
-{
-        sd_bus_error error = SD_BUS_ERROR_NULL;
-        sd_bus *bus = NULL;
-        int rc;
-
-        rc = sd_bus_default_system(&bus);
-        if (rc < 0) {
-                log_error_errno(rc, "Failed to acquire the defult system bus connection.");
-                return -1;
-        }
-
-        rc = sd_bus_call_method(bus,
-                                "org.freedesktop.systemd1",
-                                "/org/freedesktop/systemd1",
-                                "org.freedesktop.systemd1.Manager",
-                                "Unsubscribe",
-                                &error,
-                                NULL, NULL);
-        if (rc < 0) {
-                log_error_errno(rc, "Failed to unsubscribe.");
-                return -1;
-        }
-        return 0;
-}
+#define to_systemd_listener(MOD) \
+        container_of(MOD, struct systemd_listener, module)
 
 static int on_unit_properties_changed(sd_bus_message *m, void *userdata,
                                       sd_bus_error *ret_error)
@@ -195,3 +129,89 @@ static int on_unit_properties_changed(sd_bus_message *m, void *userdata,
 finish:
         return rc;
 }
+
+static int systemd_listener_init(struct faultd_module *module, sd_event* loop)
+{
+        sd_bus_error error = SD_BUS_ERROR_NULL;
+        sd_bus *bus = NULL;
+        int rc;
+
+        rc = sd_bus_default_system(&bus);
+        if (rc < 0) {
+                log_error_errno(rc, "Failed to acquire the defult system bus connection.");
+                return -1;
+        }
+
+        rc = sd_bus_attach_event(bus, loop, SD_EVENT_PRIORITY_NORMAL);
+        if (rc < 0) {
+                log_error_errno(rc, "Failed to attach the bus to the event loop.");
+                return -1;
+        }
+
+        rc = sd_bus_add_match(bus,
+                              NULL,
+                              "type='signal',sender='org.freedesktop.systemd1',"
+                              "interface='org.freedesktop.DBus.Properties',"
+                              "member='PropertiesChanged',"
+                              "path_namespace='/org/freedesktop/systemd1/unit'",
+                              on_unit_properties_changed,
+                              NULL);
+        if (rc < 0) {
+                log_error_errno(rc, "Failed to add match.");
+                return -1;
+        }
+
+        rc = sd_bus_call_method(bus,
+                                "org.freedesktop.systemd1",
+                                "/org/freedesktop/systemd1",
+                                "org.freedesktop.systemd1.Manager",
+                                "Subscribe",
+                                &error,
+                                NULL, NULL);
+        if (rc < 0) {
+                log_error_errno(rc, "Failed to subscribe.");
+                return -1;
+        }
+
+        return 0;
+}
+
+static void systemd_listener_cleanup()
+{
+        sd_bus_error error = SD_BUS_ERROR_NULL;
+        sd_bus *bus = NULL;
+        int rc;
+
+        rc = sd_bus_default_system(&bus);
+        if (rc < 0) {
+                log_error_errno(rc, "Failed to acquire the defult system bus connection.");
+                /* We cannot do anything without bus.. */
+                return;
+        }
+
+        rc = sd_bus_call_method(bus,
+                                "org.freedesktop.systemd1",
+                                "/org/freedesktop/systemd1",
+                                "org.freedesktop.systemd1.Manager",
+                                "Unsubscribe",
+                                &error,
+                                NULL, NULL);
+        
+        /* TODO: shouldn't we also detach event here? */
+
+        if (rc < 0)
+                log_error_errno(rc, "Failed to unsubscribe.");
+}
+
+struct systemd_listener systemd_listener = {
+        .module = {
+                .name = "systemd_listener",
+                .type = FAULTD_MODULE_TYPE_LISTENER,
+
+                .init = systemd_listener_init,
+                .cleanup = systemd_listener_cleanup,
+                .node = LIST_HEAD_INIT(systemd_listener.module.node),
+        },
+};
+
+FAULTD_MODULE_REGISTER(&systemd_listener)