chiark / gitweb /
util: add a bit of syntactic sugar to run short code fragments with a different umask
[elogind.git] / src / core / dbus.c
index f05f610718e5beb50f72482c429a3e7dd6508844..b92c7d0b99923cc739f7d224fcc3f03fca3bd435 100644 (file)
@@ -406,7 +406,7 @@ static DBusHandlerResult api_bus_message_filter(DBusConnection *connection, DBus
         dbus_error_free(&error);
 
         if (reply) {
-                if (!dbus_connection_send(connection, reply, NULL))
+                if (!bus_maybe_send_reply(connection, message, reply))
                         goto oom;
 
                 dbus_message_unref(reply);
@@ -445,7 +445,7 @@ static DBusHandlerResult system_bus_message_filter(DBusConnection *connection, D
                 log_debug("System D-Bus connection terminated.");
                 bus_done_system(m);
 
-        } else if (m->running_as != MANAGER_SYSTEM &&
+        } else if (m->running_as != SYSTEMD_SYSTEM &&
                    dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
 
                 const char *cgroup;
@@ -481,7 +481,7 @@ static DBusHandlerResult private_bus_message_filter(DBusConnection *connection,
 
         if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected"))
                 shutdown_connection(m, connection);
-        else if (m->running_as == MANAGER_SYSTEM &&
+        else if (m->running_as == SYSTEMD_SYSTEM &&
                  dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
 
                 const char *cgroup;
@@ -776,7 +776,7 @@ static int init_registered_system_bus(Manager *m) {
         if (!dbus_connection_add_filter(m->system_bus, system_bus_message_filter, m, NULL))
                 return log_oom();
 
-        if (m->running_as != MANAGER_SYSTEM) {
+        if (m->running_as != SYSTEMD_SYSTEM) {
                 DBusError error;
 
                 dbus_error_init(&error);
@@ -838,7 +838,7 @@ static int init_registered_api_bus(Manager *m) {
         if (r < 0)
                 return r;
 
-        if (m->running_as == MANAGER_USER) {
+        if (m->running_as == SYSTEMD_USER) {
                 char *id;
                 log_debug("Successfully connected to API D-Bus bus %s as %s",
                          strnull((id = dbus_connection_get_server_id(m->api_bus))),
@@ -889,7 +889,7 @@ static void bus_register_cb(DBusPendingCall *pending, void *userdata) {
 
                 if (conn == &m->system_bus) {
                         r = init_registered_system_bus(m);
-                        if (r == 0 && m->running_as == MANAGER_SYSTEM)
+                        if (r == 0 && m->running_as == SYSTEMD_SYSTEM)
                                 r = init_registered_api_bus(m);
                 } else
                         r = init_registered_api_bus(m);
@@ -978,9 +978,8 @@ static DBusConnection* manager_bus_connect_private(Manager *m, DBusBusType type)
         }
 
         return connection;
+
 fail:
-        if (connection)
-                dbus_connection_close(connection);
         dbus_error_free(&error);
         return NULL;
 }
@@ -1019,7 +1018,7 @@ static int bus_init_api(Manager *m) {
         if (m->api_bus)
                 return 0;
 
-        if (m->running_as == MANAGER_SYSTEM) {
+        if (m->running_as == SYSTEMD_SYSTEM) {
                 m->api_bus = m->system_bus;
                 /* In this mode there is no distinct connection to the API bus,
                  * the API is published on the system bus.
@@ -1054,7 +1053,7 @@ fail:
 static int bus_init_private(Manager *m) {
         DBusError error;
         int r;
-        const char *const external_only[] = {
+        static const char *const external_only[] = {
                 "EXTERNAL",
                 NULL
         };
@@ -1066,7 +1065,7 @@ static int bus_init_private(Manager *m) {
         if (m->private_bus)
                 return 0;
 
-        if (m->running_as == MANAGER_SYSTEM) {
+        if (m->running_as == SYSTEMD_SYSTEM) {
 
                 /* We want the private bus only when running as init */
                 if (getpid() != 1)
@@ -1077,18 +1076,33 @@ static int bus_init_private(Manager *m) {
         } else {
                 const char *e;
                 char *p;
+                char *escaped;
 
                 e = secure_getenv("XDG_RUNTIME_DIR");
                 if (!e)
                         return 0;
 
-                if (asprintf(&p, "unix:path=%s/systemd/private", e) < 0) {
+                if (asprintf(&p, "%s/systemd/private", e) < 0) {
+                        r = log_oom();
+                        goto fail;
+                }
+
+                mkdir_parents_label(p, 0755);
+                unlink(p);
+                free(p);
+
+                escaped = dbus_address_escape_value(e);
+                if (!escaped) {
+                        r = log_oom();
+                        goto fail;
+                }
+                if (asprintf(&p, "unix:path=%s/systemd/private", escaped) < 0) {
+                        dbus_free(escaped);
                         r = log_oom();
                         goto fail;
                 }
+                dbus_free(escaped);
 
-                mkdir_parents_label(p+10, 0755);
-                unlink(p+10);
                 m->private_bus = dbus_server_listen(p, &error);
                 free(p);
         }
@@ -1190,7 +1204,7 @@ static void shutdown_connection(Manager *m, DBusConnection *c) {
 
         dbus_connection_set_dispatch_status_function(c, NULL, NULL, NULL);
         /* system manager cannot afford to block on DBus */
-        if (m->running_as != MANAGER_SYSTEM)
+        if (m->running_as != SYSTEMD_SYSTEM)
                 dbus_connection_flush(c);
         dbus_connection_close(c);
         dbus_connection_unref(c);
@@ -1200,7 +1214,7 @@ static void bus_done_api(Manager *m) {
         if (!m->api_bus)
                 return;
 
-        if (m->running_as == MANAGER_USER)
+        if (m->running_as == SYSTEMD_USER)
                 shutdown_connection(m, m->api_bus);
 
         m->api_bus = NULL;
@@ -1215,7 +1229,7 @@ static void bus_done_system(Manager *m) {
         if (!m->system_bus)
                 return;
 
-        if (m->running_as == MANAGER_SYSTEM)
+        if (m->running_as == SYSTEMD_SYSTEM)
                 bus_done_api(m);
 
         shutdown_connection(m, m->system_bus);
@@ -1362,11 +1376,11 @@ int bus_broadcast(Manager *m, DBusMessage *message) {
         assert(message);
 
         SET_FOREACH(c, m->bus_connections_for_dispatch, i)
-                if (c != m->system_bus || m->running_as == MANAGER_SYSTEM)
+                if (c != m->system_bus || m->running_as == SYSTEMD_SYSTEM)
                         oom = !dbus_connection_send(c, message, NULL);
 
         SET_FOREACH(c, m->bus_connections, i)
-                if (c != m->system_bus || m->running_as == MANAGER_SYSTEM)
+                if (c != m->system_bus || m->running_as == SYSTEMD_SYSTEM)
                         oom = !dbus_connection_send(c, message, NULL);
 
         return oom ? -ENOMEM : 0;