chiark / gitweb /
importd: add new bus calls for importing local tar and raw images
[elogind.git] / src / bus-proxyd / synthesize.c
index e98a97ec050c4e9f090cf85cea9d75fb2f463b29..542166f68a1f54c6287bfe697a6a5937eb9ee388 100644 (file)
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-#include <sys/types.h>
-#include <unistd.h>
-#include <string.h>
-#include <errno.h>
 #include <stddef.h>
 
-#include "log.h"
 #include "util.h"
 #include "sd-bus.h"
 #include "bus-internal.h"
 #include "bus-message.h"
 #include "bus-util.h"
-#include "strv.h"
-#include "def.h"
-#include "bus-control.h"
 #include "synthesize.h"
 
 static int synthetic_driver_send(sd_bus *b, sd_bus_message *m) {
@@ -83,7 +75,6 @@ int synthetic_reply_method_errorf(sd_bus_message *call, const char *name, const
 }
 
 int synthetic_reply_method_errno(sd_bus_message *call, int error, const sd_bus_error *p) {
-
         _cleanup_bus_error_free_ sd_bus_error berror = SD_BUS_ERROR_NULL;
 
         assert(call);
@@ -99,6 +90,22 @@ int synthetic_reply_method_errno(sd_bus_message *call, int error, const sd_bus_e
         return synthetic_reply_method_error(call, &berror);
 }
 
+int synthetic_reply_method_errnof(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(call);
+
+        if (call->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
+                return 0;
+
+        va_start(ap, format);
+        sd_bus_error_set_errnofv(&berror, error, format, ap);
+        va_end(ap);
+
+        return synthetic_reply_method_error(call, &berror);
+}
+
 int synthetic_reply_method_return(sd_bus_message *call, const char *types, ...) {
         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
         int r;
@@ -125,7 +132,7 @@ int synthetic_reply_method_return(sd_bus_message *call, const char *types, ...)
         return synthetic_driver_send(call->bus, m);
 }
 
-int synthetic_reply_return_strv(sd_bus_message *call, char **l) {
+int synthetic_reply_method_return_strv(sd_bus_message *call, char **l) {
         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
         int r;
 
@@ -144,3 +151,70 @@ int synthetic_reply_return_strv(sd_bus_message *call, char **l) {
 
         return synthetic_driver_send(call->bus, m);
 }
+
+int synthesize_name_acquired(sd_bus *a, sd_bus *b, sd_bus_message *m) {
+        _cleanup_bus_message_unref_ sd_bus_message *n = NULL;
+        const char *name, *old_owner, *new_owner;
+        int r;
+
+        assert(a);
+        assert(b);
+        assert(m);
+
+        /* If we get NameOwnerChanged for our own name, we need to
+         * synthesize NameLost/NameAcquired, since socket clients need
+         * that, even though it is obsoleted on kdbus */
+
+        if (!a->is_kernel)
+                return 0;
+
+        if (!sd_bus_message_is_signal(m, "org.freedesktop.DBus", "NameOwnerChanged") ||
+            !streq_ptr(m->path, "/org/freedesktop/DBus") ||
+            !streq_ptr(m->sender, "org.freedesktop.DBus"))
+                return 0;
+
+        r = sd_bus_message_read(m, "sss", &name, &old_owner, &new_owner);
+        if (r < 0)
+                return r;
+
+        r = sd_bus_message_rewind(m, true);
+        if (r < 0)
+                return r;
+
+        if (streq(old_owner, a->unique_name)) {
+
+                r = sd_bus_message_new_signal(
+                                b,
+                                &n,
+                                "/org/freedesktop/DBus",
+                                "org.freedesktop.DBus",
+                                "NameLost");
+
+        } else if (streq(new_owner, a->unique_name)) {
+
+                r = sd_bus_message_new_signal(
+                                b,
+                                &n,
+                                "/org/freedesktop/DBus",
+                                "org.freedesktop.DBus",
+                                "NameAcquired");
+        } else
+                return 0;
+
+        if (r < 0)
+                return r;
+
+        r = sd_bus_message_append(n, "s", name);
+        if (r < 0)
+                return r;
+
+        r = bus_message_append_sender(n, "org.freedesktop.DBus");
+        if (r < 0)
+                return r;
+
+        r = bus_seal_synthetic_message(b, n);
+        if (r < 0)
+                return r;
+
+        return sd_bus_send(b, n, NULL);
+}