chiark / gitweb /
event: clear pending-state when re-arming timers
[elogind.git] / src / libsystemd-bus / bus-convenience.c
index 3d223c03a8ac9c8d6160bc35941f45e5e57ae7b5..9bf2e2cbe508d90e9b20db99bbc8dae87d93e925 100644 (file)
 #include "bus-internal.h"
 #include "bus-message.h"
 #include "bus-signature.h"
+#include "bus-util.h"
+#include "bus-type.h"
 
-int sd_bus_emit_signal(
+_public_ int sd_bus_emit_signal(
                 sd_bus *bus,
                 const char *path,
                 const char *interface,
@@ -33,12 +35,9 @@ int sd_bus_emit_signal(
         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
         int r;
 
-        if (!bus)
-                return -EINVAL;
-        if (!BUS_IS_OPEN(bus->state))
-                return -ENOTCONN;
-        if (bus_pid_changed(bus))
-                return -ECHILD;
+        assert_return(bus, -EINVAL);
+        assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
         r = sd_bus_message_new_signal(bus, path, interface, member, &m);
         if (r < 0)
@@ -57,7 +56,7 @@ int sd_bus_emit_signal(
         return sd_bus_send(bus, m, NULL);
 }
 
-int sd_bus_call_method(
+_public_ int sd_bus_call_method(
                 sd_bus *bus,
                 const char *destination,
                 const char *path,
@@ -70,13 +69,9 @@ int sd_bus_call_method(
         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
         int r;
 
-        if (!bus)
-
-                return -EINVAL;
-        if (!BUS_IS_OPEN(bus->state))
-                return -ENOTCONN;
-        if (bus_pid_changed(bus))
-                return -ECHILD;
+        assert_return(bus, -EINVAL);
+        assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
         r = sd_bus_message_new_method_call(bus, destination, path, interface, member, &m);
         if (r < 0)
@@ -92,10 +87,10 @@ int sd_bus_call_method(
                         return r;
         }
 
-        return sd_bus_send_with_reply_and_block(bus, m, 0, error, reply);
+        return sd_bus_call(bus, m, 0, error, reply);
 }
 
-int sd_bus_reply_method_return(
+_public_ int sd_bus_reply_method_return(
                 sd_bus *bus,
                 sd_bus_message *call,
                 const char *types, ...) {
@@ -103,18 +98,12 @@ int sd_bus_reply_method_return(
         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
         int r;
 
-        if (!bus)
-                return -EINVAL;
-        if (!call)
-                return -EINVAL;
-        if (!call->sealed)
-                return -EPERM;
-        if (call->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
-                return -EINVAL;
-        if (!BUS_IS_OPEN(bus->state))
-                return -ENOTCONN;
-        if (bus_pid_changed(bus))
-                return -ECHILD;
+        assert_return(bus, -EINVAL);
+        assert_return(call, -EINVAL);
+        assert_return(call->sealed, -EPERM);
+        assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
+        assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
         if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
                 return 0;
@@ -136,7 +125,7 @@ int sd_bus_reply_method_return(
         return sd_bus_send(bus, m, NULL);
 }
 
-int sd_bus_reply_method_error(
+_public_ int sd_bus_reply_method_error(
                 sd_bus *bus,
                 sd_bus_message *call,
                 const sd_bus_error *e) {
@@ -144,20 +133,13 @@ int sd_bus_reply_method_error(
         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
         int r;
 
-        if (!bus)
-                return -EINVAL;
-        if (!call)
-                return -EINVAL;
-        if (!call->sealed)
-                return -EPERM;
-        if (call->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
-                return -EINVAL;
-        if (!sd_bus_error_is_set(e))
-                return -EINVAL;
-        if (!BUS_IS_OPEN(bus->state))
-                return -ENOTCONN;
-        if (bus_pid_changed(bus))
-                return -ECHILD;
+        assert_return(bus, -EINVAL);
+        assert_return(call, -EINVAL);
+        assert_return(call->sealed, -EPERM);
+        assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
+        assert_return(sd_bus_error_is_set(e), -EINVAL);
+        assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
         if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
                 return 0;
@@ -169,7 +151,7 @@ int sd_bus_reply_method_error(
         return sd_bus_send(bus, m, NULL);
 }
 
-int sd_bus_reply_method_errorf(
+_public_ int sd_bus_reply_method_errorf(
                 sd_bus *bus,
                 sd_bus_message *call,
                 const char *name,
@@ -180,25 +162,80 @@ int sd_bus_reply_method_errorf(
         va_list ap;
         int r;
 
-        error.name = strdup(name);
-        if (!error.name)
-                return -ENOMEM;
+        assert_return(bus, -EINVAL);
+        assert_return(call, -EINVAL);
+        assert_return(call->sealed, -EPERM);
+        assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
+        assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
-        error.need_free = true;
+        if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
+                return 0;
 
-        if (format) {
-                va_start(ap, format);
-                r = vasprintf((char**) &error.message, format, ap);
-                va_end(ap);
+        va_start(ap, format);
+        r = bus_error_setfv(&error, name, format, ap);
+        va_end(ap);
 
-                if (r < 0)
-                        return -ENOMEM;
-        }
+        if (r < 0)
+                return r;
 
         return sd_bus_reply_method_error(bus, call, &error);
 }
 
-int sd_bus_get_property(
+_public_ int sd_bus_reply_method_errno(
+                sd_bus *bus,
+                sd_bus_message *call,
+                int error,
+                const sd_bus_error *p) {
+
+        _cleanup_bus_error_free_ sd_bus_error berror = SD_BUS_ERROR_NULL;
+
+        assert_return(bus, -EINVAL);
+        assert_return(call, -EINVAL);
+        assert_return(call->sealed, -EPERM);
+        assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
+        assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
+
+        if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
+                return 0;
+
+        if (sd_bus_error_is_set(p))
+                return sd_bus_reply_method_error(bus, call, p);
+
+        sd_bus_error_set_errno(&berror, error);
+
+        return sd_bus_reply_method_error(bus, call, &berror);
+}
+
+_public_ int sd_bus_reply_method_errnof(
+                sd_bus *bus,
+                sd_bus_message *call,
+                int error,
+                const char *format,
+                ...) {
+
+        _cleanup_bus_error_free_ sd_bus_error berror = SD_BUS_ERROR_NULL;
+        va_list ap;
+
+        assert_return(bus, -EINVAL);
+        assert_return(call, -EINVAL);
+        assert_return(call->sealed, -EPERM);
+        assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
+        assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
+
+        if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
+                return 0;
+
+        va_start(ap, format);
+        bus_error_set_errnofv(&berror, error, format, ap);
+        va_end(ap);
+
+        return sd_bus_reply_method_error(bus, call, &berror);
+}
+
+_public_ int sd_bus_get_property(
                 sd_bus *bus,
                 const char *destination,
                 const char *path,
@@ -211,14 +248,13 @@ int sd_bus_get_property(
         sd_bus_message *rep = NULL;
         int r;
 
-        if (interface && !interface_name_is_valid(interface))
-                return -EINVAL;
-        if (!member_name_is_valid(member))
-                return -EINVAL;
-        if (!signature_is_single(type, false))
-                return -EINVAL;
-        if (!reply)
-                return -EINVAL;
+        assert_return(bus, -EINVAL);
+        assert_return(isempty(interface) || interface_name_is_valid(interface), -EINVAL);
+        assert_return(member_name_is_valid(member), -EINVAL);
+        assert_return(reply, -EINVAL);
+        assert_return(signature_is_single(type, false), -EINVAL);
+        assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
         r = sd_bus_call_method(bus, destination, path, "org.freedesktop.DBus.Properties", "Get", error, &rep, "ss", strempty(interface), member);
         if (r < 0)
@@ -234,7 +270,117 @@ int sd_bus_get_property(
         return 0;
 }
 
-int sd_bus_set_property(
+_public_ int sd_bus_get_property_trivial(
+                sd_bus *bus,
+                const char *destination,
+                const char *path,
+                const char *interface,
+                const char *member,
+                sd_bus_error *error,
+                char type, void *ptr) {
+
+        _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+        int r;
+
+        assert_return(bus, -EINVAL);
+        assert_return(isempty(interface) || interface_name_is_valid(interface), -EINVAL);
+        assert_return(member_name_is_valid(member), -EINVAL);
+        assert_return(bus_type_is_trivial(type), -EINVAL);
+        assert_return(ptr, -EINVAL);
+        assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
+
+        r = sd_bus_call_method(bus, destination, path, "org.freedesktop.DBus.Properties", "Get", error, &reply, "ss", strempty(interface), member);
+        if (r < 0)
+                return r;
+
+        r = sd_bus_message_enter_container(reply, 'v', CHAR_TO_STR(type));
+        if (r < 0)
+                return r;
+
+        r = sd_bus_message_read_basic(reply, type, ptr);
+        if (r < 0)
+                return r;
+
+        return 0;
+}
+
+_public_ int sd_bus_get_property_string(
+                sd_bus *bus,
+                const char *destination,
+                const char *path,
+                const char *interface,
+                const char *member,
+                sd_bus_error *error,
+                char **ret) {
+
+        _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+        const char *s;
+        char *n;
+        int r;
+
+        assert_return(bus, -EINVAL);
+        assert_return(isempty(interface) || interface_name_is_valid(interface), -EINVAL);
+        assert_return(member_name_is_valid(member), -EINVAL);
+        assert_return(ret, -EINVAL);
+        assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
+
+        r = sd_bus_call_method(bus, destination, path, "org.freedesktop.DBus.Properties", "Get", error, &reply, "ss", strempty(interface), member);
+        if (r < 0)
+                return r;
+
+        r = sd_bus_message_enter_container(reply, 'v', "s");
+        if (r < 0)
+                return r;
+
+        r = sd_bus_message_read_basic(reply, 's', &s);
+        if (r < 0)
+                return r;
+
+        n = strdup(s);
+        if (!n)
+                return -ENOMEM;
+
+        *ret = n;
+        return 0;
+}
+
+_public_ int sd_bus_get_property_strv(
+                sd_bus *bus,
+                const char *destination,
+                const char *path,
+                const char *interface,
+                const char *member,
+                sd_bus_error *error,
+                char ***ret) {
+
+        _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+        int r;
+
+        assert_return(bus, -EINVAL);
+        assert_return(isempty(interface) || interface_name_is_valid(interface), -EINVAL);
+        assert_return(member_name_is_valid(member), -EINVAL);
+        assert_return(ret, -EINVAL);
+        assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
+
+        r = sd_bus_call_method(bus, destination, path, "org.freedesktop.DBus.Properties", "Get", error, &reply, "ss", strempty(interface), member);
+        if (r < 0)
+                return r;
+
+        r = sd_bus_message_enter_container(reply, 'v', NULL);
+        if (r < 0)
+                return r;
+
+        r = sd_bus_message_read_strv(reply, ret);
+        if (r < 0)
+                return r;
+
+        return 0;
+}
+
+_public_ int sd_bus_set_property(
                 sd_bus *bus,
                 const char *destination,
                 const char *path,
@@ -247,12 +393,12 @@ int sd_bus_set_property(
         va_list ap;
         int r;
 
-        if (interface && !interface_name_is_valid(interface))
-                return -EINVAL;
-        if (!member_name_is_valid(member))
-                return -EINVAL;
-        if (!signature_is_single(type, false))
-                return -EINVAL;
+        assert_return(bus, -EINVAL);
+        assert_return(isempty(interface) || interface_name_is_valid(interface), -EINVAL);
+        assert_return(member_name_is_valid(member), -EINVAL);
+        assert_return(signature_is_single(type, false), -EINVAL);
+        assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
         r = sd_bus_message_new_method_call(bus, destination, path, "org.freedesktop.DBus.Properties", "Set", &m);
         if (r < 0)
@@ -276,5 +422,5 @@ int sd_bus_set_property(
         if (r < 0)
                 return r;
 
-        return sd_bus_send_with_reply_and_block(bus, m, 0, error, NULL);
+        return sd_bus_call(bus, m, 0, error, NULL);
 }