chiark / gitweb /
Prep v239: Fix and add debug messages to method_can_shutdown_or_sleep()
[elogind.git] / src / libelogind / sd-bus / bus-convenience.c
index 6fe65479a79a6376ce47631ea4470e65267adf5d..68a419c6d27593b68476692a170caccae6927e59 100644 (file)
@@ -1,20 +1,5 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
-  This file is part of systemd.
-
-  Copyright 2013 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
 #include "bus-internal.h"
@@ -35,6 +20,7 @@ _public_ int sd_bus_emit_signal(
         int r;
 
         assert_return(bus, -EINVAL);
+        assert_return(bus = bus_resolve(bus), -ENOPKG);
         assert_return(!bus_pid_changed(bus), -ECHILD);
 
         if (!BUS_IS_OPEN(bus->state))
@@ -48,7 +34,7 @@ _public_ int sd_bus_emit_signal(
                 va_list ap;
 
                 va_start(ap, types);
-                r = bus_message_append_ap(m, types, ap);
+                r = sd_bus_message_appendv(m, types, ap);
                 va_end(ap);
                 if (r < 0)
                         return r;
@@ -57,7 +43,6 @@ _public_ int sd_bus_emit_signal(
         return sd_bus_send(bus, m, NULL);
 }
 
-#if 0 /// UNNEEDED by elogind
 _public_ int sd_bus_call_method_async(
                 sd_bus *bus,
                 sd_bus_slot **slot,
@@ -73,6 +58,7 @@ _public_ int sd_bus_call_method_async(
         int r;
 
         assert_return(bus, -EINVAL);
+        assert_return(bus = bus_resolve(bus), -ENOPKG);
         assert_return(!bus_pid_changed(bus), -ECHILD);
 
         if (!BUS_IS_OPEN(bus->state))
@@ -86,7 +72,7 @@ _public_ int sd_bus_call_method_async(
                 va_list ap;
 
                 va_start(ap, types);
-                r = bus_message_append_ap(m, types, ap);
+                r = sd_bus_message_appendv(m, types, ap);
                 va_end(ap);
                 if (r < 0)
                         return r;
@@ -94,7 +80,6 @@ _public_ int sd_bus_call_method_async(
 
         return sd_bus_call_async(bus, slot, m, callback, userdata, 0);
 }
-#endif // 0
 
 _public_ int sd_bus_call_method(
                 sd_bus *bus,
@@ -125,7 +110,7 @@ _public_ int sd_bus_call_method(
                 va_list ap;
 
                 va_start(ap, types);
-                r = bus_message_append_ap(m, types, ap);
+                r = sd_bus_message_appendv(m, types, ap);
                 va_end(ap);
                 if (r < 0)
                         goto fail;
@@ -164,7 +149,7 @@ _public_ int sd_bus_reply_method_return(
                 va_list ap;
 
                 va_start(ap, types);
-                r = bus_message_append_ap(m, types, ap);
+                r = sd_bus_message_appendv(m, types, ap);
                 va_end(ap);
                 if (r < 0)
                         return r;
@@ -255,7 +240,6 @@ _public_ int sd_bus_reply_method_errno(
         return sd_bus_reply_method_error(call, &berror);
 }
 
-#if 0 /// UNNEEDED by elogind
 _public_ int sd_bus_reply_method_errnof(
                 sd_bus_message *call,
                 int error,
@@ -283,7 +267,6 @@ _public_ int sd_bus_reply_method_errnof(
 
         return sd_bus_reply_method_error(call, &berror);
 }
-#endif // 0
 
 _public_ int sd_bus_get_property(
                 sd_bus *bus,
@@ -327,7 +310,6 @@ fail:
         return sd_bus_error_set_errno(error, r);
 }
 
-#if 0 /// UNNEEDED by elogind
 _public_ int sd_bus_get_property_trivial(
                 sd_bus *bus,
                 const char *destination,
@@ -369,7 +351,6 @@ _public_ int sd_bus_get_property_trivial(
 fail:
         return sd_bus_error_set_errno(error, r);
 }
-#endif // 0
 
 _public_ int sd_bus_get_property_string(
                 sd_bus *bus,
@@ -421,7 +402,6 @@ fail:
         return sd_bus_error_set_errno(error, r);
 }
 
-#if 0 /// UNNEEDED by elogind
 _public_ int sd_bus_get_property_strv(
                 sd_bus *bus,
                 const char *destination,
@@ -500,7 +480,7 @@ _public_ int sd_bus_set_property(
                 goto fail;
 
         va_start(ap, type);
-        r = bus_message_append_ap(m, type, ap);
+        r = sd_bus_message_appendv(m, type, ap);
         va_end(ap);
         if (r < 0)
                 goto fail;
@@ -514,7 +494,6 @@ _public_ int sd_bus_set_property(
 fail:
         return sd_bus_error_set_errno(error, r);
 }
-#endif // 0
 
 _public_ int sd_bus_query_sender_creds(sd_bus_message *call, uint64_t mask, sd_bus_creds **creds) {
         sd_bus_creds *c;
@@ -541,22 +520,20 @@ _public_ int sd_bus_query_sender_creds(sd_bus_message *call, uint64_t mask, sd_b
                  * to get it from the sender or peer. */
 
                 if (call->sender)
-                        /* There's a sender, but the creds are
-                         * missing. This means we are talking via
-                         * dbus1, or are getting a message that was
-                         * sent to us via kdbus, but was converted
-                         * from a dbus1 message by the bus-proxy and
-                         * thus also lacks the creds. */
+                        /* There's a sender, but the creds are missing. */
                         return sd_bus_get_name_creds(call->bus, call->sender, mask, creds);
                 else
-                        /* There's no sender, hence we are on a dbus1
-                         * direct connection. For direct connections
+                        /* There's no sender. For direct connections
                          * the credentials of the AF_UNIX peer matter,
-                         * which may be queried via
-                         * sd_bus_get_owner_creds(). */
+                         * which may be queried via sd_bus_get_owner_creds(). */
                         return sd_bus_get_owner_creds(call->bus, mask, creds);
         }
 
+        log_debug_elogind("Called by UID %u ; %s [%s] (%s)", c->uid,
+                          c->unique_name ? c->unique_name : "no name",
+                          c->label       ? c->label       : "no label",
+                          c->description ? c->description : "no desc");
+
         return bus_creds_extend_by_pid(c, mask, creds);
 }
 
@@ -587,9 +564,6 @@ _public_ int sd_bus_query_sender_privilege(sd_bus_message *call, int capability)
                  * here. */
                 assert_return((sd_bus_creds_get_augmented_mask(creds) & SD_BUS_CREDS_EFFECTIVE_CAPS) == 0, -EPERM);
 
-                /* Note that not even on kdbus we might have the caps
-                 * field, due to faked identities, or namespace
-                 * translation issues. */
                 r = sd_bus_creds_has_effective_cap(creds, capability);
                 if (r > 0)
                         return 1;
@@ -632,3 +606,71 @@ _public_ int sd_bus_query_sender_privilege(sd_bus_message *call, int capability)
 
         return 0;
 }
+
+#define make_expression(sender, path, interface, member)        \
+        strjoina(                                               \
+                "type='signal'",                                \
+                sender ? ",sender='" : "",                      \
+                sender ?: "",                                   \
+                sender ? "'" : "",                              \
+                path ? ",path='" : "",                          \
+                path ?: "",                                     \
+                path ? "'" : "",                                \
+                interface ? ",interface='" : "",                \
+                interface ?: "",                                \
+                interface ? "'" : "",                           \
+                member ? ",member='" : "",                      \
+                member ?: "",                                   \
+                member ? "'" : ""                               \
+        )
+
+_public_ int sd_bus_match_signal(
+                sd_bus *bus,
+                sd_bus_slot **ret,
+                const char *sender,
+                const char *path,
+                const char *interface,
+                const char *member,
+                sd_bus_message_handler_t callback,
+                void *userdata) {
+
+        const char *expression;
+
+        assert_return(bus, -EINVAL);
+        assert_return(bus = bus_resolve(bus), -ENOPKG);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
+        assert_return(!sender || service_name_is_valid(sender), -EINVAL);
+        assert_return(!path || object_path_is_valid(path), -EINVAL);
+        assert_return(!interface || interface_name_is_valid(interface), -EINVAL);
+        assert_return(!member || member_name_is_valid(member), -EINVAL);
+
+        expression = make_expression(sender, path, interface, member);
+
+        return sd_bus_add_match(bus, ret, expression, callback, userdata);
+}
+
+_public_ int sd_bus_match_signal_async(
+                sd_bus *bus,
+                sd_bus_slot **ret,
+                const char *sender,
+                const char *path,
+                const char *interface,
+                const char *member,
+                sd_bus_message_handler_t callback,
+                sd_bus_message_handler_t install_callback,
+                void *userdata) {
+
+        const char *expression;
+
+        assert_return(bus, -EINVAL);
+        assert_return(bus = bus_resolve(bus), -ENOPKG);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
+        assert_return(!sender || service_name_is_valid(sender), -EINVAL);
+        assert_return(!path || object_path_is_valid(path), -EINVAL);
+        assert_return(!interface || interface_name_is_valid(interface), -EINVAL);
+        assert_return(!member || member_name_is_valid(member), -EINVAL);
+
+        expression = make_expression(sender, path, interface, member);
+
+        return sd_bus_add_match_async(bus, ret, expression, callback, install_callback, userdata);
+}