chiark / gitweb /
unit: optionally allow making cgroup attribute changes persistent
[elogind.git] / src / shared / dbus-common.c
index 7f0dce5ad393eda0b6292f3e3d793390ffa09a26..50891a8ade4dbcde6e80e9746904d69462f41330 100644 (file)
@@ -32,6 +32,7 @@
 #include "log.h"
 #include "dbus-common.h"
 #include "util.h"
+#include "missing.h"
 #include "def.h"
 #include "strv.h"
 
@@ -121,7 +122,7 @@ int bus_connect(DBusBusType t, DBusConnection **_bus, bool *_private, DBusError
                          * try via XDG_RUNTIME_DIR first, then
                          * fallback to normal bus access */
 
-                        e = getenv("XDG_RUNTIME_DIR");
+                        e = secure_getenv("XDG_RUNTIME_DIR");
                         if (e) {
                                 char *p;
 
@@ -228,7 +229,8 @@ int bus_connect_system_polkit(DBusConnection **_bus, DBusError *error) {
 
         dbus_connection_set_exit_on_disconnect(bus, FALSE);
 
-        if ((r = sync_auth(bus, error)) < 0) {
+        r = sync_auth(bus, error);
+        if (r < 0) {
                 dbus_connection_close(bus);
                 dbus_connection_unref(bus);
                 return r;
@@ -272,7 +274,7 @@ DBusHandlerResult bus_default_message_handler(
                 const BusBoundProperties *bound_properties) {
 
         DBusError error;
-        DBusMessage *reply = NULL;
+        _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
         int r;
 
         assert(c);
@@ -282,7 +284,8 @@ DBusHandlerResult bus_default_message_handler(
 
         if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) {
 
-                if (!(reply = dbus_message_new_method_return(message)))
+                reply = dbus_message_new_method_return(message);
+                if (!reply)
                         goto oom;
 
                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID))
@@ -333,14 +336,12 @@ get_prop:
                 data = (char*)bp->base + p->offset;
                 if (p->indirect)
                         data = *(void**)data;
-                r = p->append(&sub, property, data);
-                if (r < 0) {
-                        if (r == -ENOMEM)
-                                goto oom;
 
-                        dbus_message_unref(reply);
+                r = p->append(&sub, property, data);
+                if (r == -ENOMEM)
+                        goto oom;
+                if (r < 0)
                         return bus_send_error_reply(c, message, NULL, r);
-                }
 
                 if (!dbus_message_iter_close_container(&iter, &sub))
                         goto oom;
@@ -363,7 +364,8 @@ get_prop:
                         return bus_send_error_reply(c, message, &error, -EINVAL);
                 }
 
-                if (!(reply = dbus_message_new_method_return(message)))
+                reply = dbus_message_new_method_return(message);
+                if (!reply)
                         goto oom;
 
                 dbus_message_iter_init_append(reply, &iter);
@@ -387,13 +389,10 @@ get_prop:
                                 if (p->indirect)
                                         data = *(void**)data;
                                 r = p->append(&sub3, p->property, data);
-                                if (r < 0) {
-                                        if (r == -ENOMEM)
-                                                goto oom;
-
-                                        dbus_message_unref(reply);
+                                if (r == -ENOMEM)
+                                        goto oom;
+                                if (r < 0)
                                         return bus_send_error_reply(c, message, NULL, r);
-                                }
 
                                 if (!dbus_message_iter_close_container(&sub2, &sub3) ||
                                     !dbus_message_iter_close_container(&sub, &sub2))
@@ -502,19 +501,15 @@ set_prop:
         }
 
         if (reply) {
-                if (!dbus_connection_send(c, reply, NULL))
+                if (!bus_maybe_send_reply(c, message, reply))
                         goto oom;
 
-                dbus_message_unref(reply);
                 return DBUS_HANDLER_RESULT_HANDLED;
         }
 
         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 
 oom:
-        if (reply)
-                dbus_message_unref(reply);
-
         dbus_error_free(&error);
 
         return DBUS_HANDLER_RESULT_NEED_MEMORY;
@@ -718,8 +713,17 @@ const char *bus_errno_to_dbus(int error) {
         return DBUS_ERROR_FAILED;
 }
 
+dbus_bool_t bus_maybe_send_reply (DBusConnection   *c,
+                                  DBusMessage *message,
+                                  DBusMessage *reply)
+{
+        if (dbus_message_get_no_reply (message))
+                return TRUE;
+        return dbus_connection_send (c, reply, NULL);
+}
+
 DBusHandlerResult bus_send_error_reply(DBusConnection *c, DBusMessage *message, DBusError *berror, int error) {
-        DBusMessage *reply = NULL;
+        _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
         const char *name, *text;
 
         if (berror && dbus_error_is_set(berror)) {
@@ -730,14 +734,13 @@ DBusHandlerResult bus_send_error_reply(DBusConnection *c, DBusMessage *message,
                 text = strerror(-error);
         }
 
-        if (!(reply = dbus_message_new_error(message, name, text)))
+        reply = dbus_message_new_error(message, name, text);
+        if (!reply)
                 goto oom;
 
-        if (!dbus_connection_send(c, reply, NULL))
+        if (!bus_maybe_send_reply(c, message, reply))
                 goto oom;
 
-        dbus_message_unref(reply);
-
         if (berror)
                 dbus_error_free(berror);
 
@@ -895,7 +898,8 @@ int bus_parse_strv_iter(DBusMessageIter *iter, char ***_l) {
                 dbus_message_iter_next(&sub);
         }
 
-        if (!(l = new(char*, n+1)))
+        l = new(char*, n+1);
+        if (!l)
                 return -ENOMEM;
 
         dbus_message_iter_recurse(iter, &sub);
@@ -923,6 +927,67 @@ int bus_parse_strv_iter(DBusMessageIter *iter, char ***_l) {
         return 0;
 }
 
+int bus_parse_strv_pairs_iter(DBusMessageIter *iter, char ***_l) {
+        DBusMessageIter sub, sub2;
+        unsigned n = 0, i = 0;
+        char **l;
+
+        assert(iter);
+        assert(_l);
+
+        if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY ||
+            dbus_message_iter_get_element_type(iter) != DBUS_TYPE_STRUCT)
+            return -EINVAL;
+
+        dbus_message_iter_recurse(iter, &sub);
+
+        while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
+                n++;
+                dbus_message_iter_next(&sub);
+        }
+
+        l = new(char*, n*2+1);
+        if (!l)
+                return -ENOMEM;
+
+        dbus_message_iter_recurse(iter, &sub);
+
+        while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
+                const char *a, *b;
+
+                assert_se(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT);
+
+                dbus_message_iter_recurse(&sub, &sub2);
+
+                if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &a, true) < 0 ||
+                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &b, false) < 0)
+                        return -EINVAL;
+
+                l[i] = strdup(a);
+                if (!l[i]) {
+                        strv_free(l);
+                        return -ENOMEM;
+                }
+
+                l[++i] = strdup(b);
+                if (!l[i]) {
+                        strv_free(l);
+                        return -ENOMEM;
+                }
+
+                i++;
+                dbus_message_iter_next(&sub);
+        }
+
+        assert(i == n*2);
+        l[i] = NULL;
+
+        if (_l)
+                *_l = l;
+
+        return 0;
+}
+
 int bus_append_strv_iter(DBusMessageIter *iter, char **l) {
         DBusMessageIter sub;
 
@@ -1125,7 +1190,7 @@ static void release_name_pending_cb(DBusPendingCall *pending, void *userdata) {
 }
 
 void bus_async_unregister_and_exit(DBusConnection *bus, const char *name) {
-        DBusMessage *m = NULL;
+        _cleanup_dbus_message_unref_ DBusMessage *m = NULL;
         DBusPendingCall *pending = NULL;
 
         assert(bus);
@@ -1155,7 +1220,6 @@ void bus_async_unregister_and_exit(DBusConnection *bus, const char *name) {
         if (!dbus_pending_call_set_notify(pending, release_name_pending_cb, bus, NULL))
                 goto oom;
 
-        dbus_message_unref(m);
         dbus_pending_call_unref(pending);
 
         return;
@@ -1167,9 +1231,6 @@ oom:
                 dbus_pending_call_cancel(pending);
                 dbus_pending_call_unref(pending);
         }
-
-        if (m)
-                dbus_message_unref(m);
 }
 
 DBusHandlerResult bus_exit_idle_filter(DBusConnection *bus, DBusMessage *m, void *userdata) {
@@ -1194,7 +1255,7 @@ pid_t bus_get_unix_process_id(
                 const char *name,
                 DBusError *error) {
 
-        DBusMessage *m = NULL, *reply = NULL;
+        _cleanup_dbus_message_unref_ DBusMessage *m = NULL, *reply = NULL;
         uint32_t pid = 0;
 
         m = dbus_message_new_method_call(
@@ -1204,7 +1265,7 @@ pid_t bus_get_unix_process_id(
                         "GetConnectionUnixProcessID");
         if (!m) {
                 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, NULL);
-                goto finish;
+                return 0;
         }
 
         if (!dbus_message_append_args(
@@ -1212,28 +1273,21 @@ pid_t bus_get_unix_process_id(
                             DBUS_TYPE_STRING, &name,
                             DBUS_TYPE_INVALID)) {
                 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, NULL);
-                goto finish;
+                return 0;
         }
 
         reply = dbus_connection_send_with_reply_and_block(connection, m, -1, error);
         if (!reply)
-                goto finish;
+                return 0;
 
         if (dbus_set_error_from_message(error, reply))
-                goto finish;
+                return 0;
 
         if (!dbus_message_get_args(
                             reply, error,
                             DBUS_TYPE_UINT32, &pid,
                             DBUS_TYPE_INVALID))
-                goto finish;
-
-finish:
-        if (m)
-                dbus_message_unref(m);
-
-        if (reply)
-                dbus_message_unref(reply);
+                return 0;
 
         return (pid_t) pid;
 }
@@ -1253,16 +1307,19 @@ bool bus_error_is_no_service(const DBusError *error) {
         return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
 }
 
-int bus_method_call_with_reply(DBusConnection *bus,
-                                       const char *destination,
-                                       const char *path,
-                                       const char *interface,
-                                       const char *method,
-                                       DBusMessage **return_reply,
-                                       DBusError *return_error,
-                                       int first_arg_type, ...) {
+int bus_method_call_with_reply(
+                DBusConnection *bus,
+                const char *destination,
+                const char *path,
+                const char *interface,
+                const char *method,
+                DBusMessage **return_reply,
+                DBusError *return_error,
+                int first_arg_type, ...) {
+
         DBusError error;
-        DBusMessage *m, *reply;
+        _cleanup_dbus_message_unref_ DBusMessage *m = NULL;
+        DBusMessage *reply;
         va_list ap;
         int r = 0;
 
@@ -1278,16 +1335,16 @@ int bus_method_call_with_reply(DBusConnection *bus,
         va_start(ap, first_arg_type);
         if (!dbus_message_append_args_valist(m, first_arg_type, ap)) {
                 va_end(ap);
-                dbus_message_unref(m);
                 r = log_oom();
                 goto finish;
         }
         va_end(ap);
 
         reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
-        dbus_message_unref(m);
         if (!reply) {
-                log_error("Failed to issue method call: %s", bus_error_message(&error));
+                if (!return_error)
+                        log_error("Failed to issue method call: %s", bus_error_message(&error));
+
                 if (bus_error_is_no_service(&error))
                         r = -ENOENT;
                 else if (dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED))
@@ -1298,15 +1355,42 @@ int bus_method_call_with_reply(DBusConnection *bus,
                         r = -EIO;
                 goto finish;
         }
+
         if (return_reply)
                 *return_reply = reply;
         else
                 dbus_message_unref(reply);
+
 finish:
-        if(return_error)
-                *return_error=error;
+        if (return_error)
+                *return_error = error;
         else
                 dbus_error_free(&error);
 
         return r;
 }
+
+void bus_message_unrefp(DBusMessage **reply) {
+        if (!reply)
+                return;
+
+        if (!*reply)
+                return;
+
+        dbus_message_unref(*reply);
+}
+
+const char *bus_message_get_sender_with_fallback(DBusMessage *m) {
+        const char *s;
+
+        assert(m);
+
+        s = dbus_message_get_sender(m);
+        if (s)
+                return s;
+
+        /* When the message came in from a direct connection the
+         * message will have no sender. We fix that here. */
+
+        return ":no-sender";
+}