#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)
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)