chiark / gitweb /
service: properly handle if the main process is down and we wait for the cgroup to...
[elogind.git] / src / cgroups-agent.c
index 232b63e2da9df263b804fcd05c86b79c8cf04622..30aeede943d553491422fadaed7752d755209b95 100644 (file)
@@ -1,4 +1,4 @@
-/*-*- Mode: C; c-basic-offset: 8 -*-*/
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
 
 /***
   This file is part of systemd.
 
 #include <dbus/dbus.h>
 
+#include <stdlib.h>
+
 #include "log.h"
+#include "dbus-common.h"
 
 int main(int argc, char *argv[]) {
         DBusError error;
         DBusConnection *bus = NULL;
         DBusMessage *m = NULL;
-        int r = 1;
+        int r = EXIT_FAILURE;
 
         dbus_error_init(&error);
 
@@ -36,9 +39,26 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
-        if (!(bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error))) {
-                log_error("Failed to get D-Bus connection: %s", error.message);
-                goto finish;
+        log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
+        log_parse_environment();
+        log_open();
+
+        /* If possible we go via the system bus, to make sure that
+         * session instances get the messages. If not possible we talk
+         * to the system instance directly. */
+        if (!(bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error))) {
+
+                dbus_error_free(&error);
+
+                if (!(bus = dbus_connection_open_private("unix:abstract=/org/freedesktop/systemd1/private", &error))) {
+                        log_error("Failed to get D-Bus connection: %s", bus_error_message(&error));
+                        goto finish;
+                }
+
+                if (bus_check_peercred(bus) < 0) {
+                        log_error("Bus owner not root.");
+                        goto finish;
+                }
         }
 
         if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1/agent", "org.freedesktop.systemd1.Agent", "Released"))) {
@@ -58,11 +78,14 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
-        r = 0;
+        r = EXIT_SUCCESS;
 
 finish:
-        if (bus)
+        if (bus) {
+                dbus_connection_flush(bus);
+                dbus_connection_close(bus);
                 dbus_connection_unref(bus);
+        }
 
         if (m)
                 dbus_message_unref(m);