chiark / gitweb /
systemd-run: extend bash completion
[elogind.git] / src / bus-proxyd / bus-proxyd.c
index 28e8b44ccd499a26e4e6daf2a9056c0d520f6052..80f83e777fee9fc9bb66dc5e908016ad8d68e7d0 100644 (file)
@@ -157,9 +157,9 @@ static int rename_service(sd_bus *a, sd_bus *b) {
 
         /* The status string gets the full command line ... */
         sd_notifyf(false,
-                   "STATUS=Processing requests from client PID %lu (%s); UID %lu (%s)",
-                   (unsigned long) pid, p,
-                   (unsigned long) uid, name);
+                   "STATUS=Processing requests from client PID "PID_FMT" (%s); UID "UID_FMT" (%s)",
+                   pid, p,
+                   uid, name);
 
         /* ... and the argv line only the short comm */
         if (arg_command_line_buffer) {
@@ -167,17 +167,17 @@ static int rename_service(sd_bus *a, sd_bus *b) {
 
                 m = strlen(arg_command_line_buffer);
                 w = snprintf(arg_command_line_buffer, m,
-                             "[PID %lu/%s; UID %lu/%s]",
-                             (unsigned long) pid, comm,
-                             (unsigned long) uid, name);
+                             "[PID "PID_FMT"/%s; UID "UID_FMT"/%s]",
+                             pid, comm,
+                             uid, name);
 
                 if (m > w)
-                        memset(arg_command_line_buffer + w, 0, m - w);
+                        memzero(arg_command_line_buffer + w, m - w);
         }
 
-        log_debug("Running on behalf of PID %lu (%s), UID %lu (%s), %s",
-                  (unsigned long) pid, p,
-                  (unsigned long) uid, name,
+        log_debug("Running on behalf of PID "PID_FMT" (%s), UID "UID_FMT" (%s), %s",
+                  pid, p,
+                  uid, name,
                   a->unique_name);
                 ;
         return 0;
@@ -216,19 +216,19 @@ static int synthesize_name_acquired(sd_bus *a, sd_bus *b, sd_bus_message *m) {
 
                 r = sd_bus_message_new_signal(
                                 b,
+                                &n,
                                 "/org/freedesktop/DBus",
                                 "org.freedesktop.DBus",
-                                "NameLost",
-                                &n);
+                                "NameLost");
 
         } else if (streq(new_owner, a->unique_name)) {
 
                 r = sd_bus_message_new_signal(
                                 b,
+                                &n,
                                 "/org/freedesktop/DBus",
                                 "org.freedesktop.DBus",
-                                "NameAcquired",
-                                &n);
+                                "NameAcquired");
         } else
                 return 0;
 
@@ -359,10 +359,10 @@ static int process_hello(sd_bus *a, sd_bus *b, sd_bus_message *m, bool *got_hell
         n = sd_bus_message_unref(n);
         r = sd_bus_message_new_signal(
                         b,
+                        &n,
                         "/org/freedesktop/DBus",
                         "org.freedesktop.DBus",
-                        "NameAcquired",
-                        &n);
+                        "NameAcquired");
         if (r < 0) {
                 log_error("Failed to allocate initial NameAcquired message: %s", strerror(-r));
                 return r;
@@ -395,6 +395,36 @@ static int process_hello(sd_bus *a, sd_bus *b, sd_bus_message *m, bool *got_hell
         return 1;
 }
 
+static int patch_sender(sd_bus *a, sd_bus_message *m) {
+        char **well_known = NULL;
+        sd_bus_creds *c;
+        int r;
+
+        assert(a);
+        assert(m);
+
+        if (!a->is_kernel)
+                return 0;
+
+        /* We will change the sender of messages from the bus driver
+         * so that they originate from the bus driver. This is a
+         * speciality originating from dbus1, where the bus driver did
+         * not have a unique id, but only the well-known name. */
+
+        c = sd_bus_message_get_creds(m);
+        if (!c)
+                return 0;
+
+        r = sd_bus_creds_get_well_known_names(c, &well_known);
+        if (r < 0)
+                return r;
+
+        if (strv_contains(well_known, "org.freedesktop.DBus"))
+                m->sender = "org.freedesktop.DBus";
+
+        return 0;
+}
+
 int main(int argc, char *argv[]) {
 
         _cleanup_bus_unref_ sd_bus *a = NULL, *b = NULL;
@@ -440,6 +470,12 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
+        r = sd_bus_set_name(a, "sd-proxy");
+        if (r < 0) {
+                log_error("Failed to set bus name: %s", strerror(-r));
+                goto finish;
+        }
+
         r = sd_bus_set_address(a, arg_address);
         if (r < 0) {
                 log_error("Failed to set address to connect to: %s", strerror(-r));
@@ -606,10 +642,17 @@ int main(int argc, char *argv[]) {
                                         goto finish;
                                 }
 
+                                patch_sender(a, m);
+
                                 k = sd_bus_send(b, m, NULL);
                                 if (k < 0) {
-                                        r = k;
-                                        log_error("Failed to send message: %s", strerror(-r));
+                                        if (k == -ECONNRESET)
+                                                r = 0;
+                                        else {
+                                                r = k;
+                                                log_error("Failed to send message: %s", strerror(-r));
+                                        }
+
                                         goto finish;
                                 }
                         }
@@ -639,6 +682,7 @@ int main(int argc, char *argv[]) {
                         k = process_hello(a, b, m, &got_hello);
                         if (k < 0) {
                                 r = k;
+                                log_error("Failed to process HELLO: %s", strerror(-r));
                                 goto finish;
                         }
 
@@ -648,13 +692,19 @@ int main(int argc, char *argv[]) {
                                 k = process_policy(a, b, m);
                                 if (k < 0) {
                                         r = k;
+                                        log_error("Failed to process policy: %s", strerror(-r));
                                         goto finish;
                                 }
 
                                 k = sd_bus_send(a, m, NULL);
                                 if (k < 0) {
-                                        r = k;
-                                        log_error("Failed to send message: %s", strerror(-r));
+                                        if (r == -ECONNRESET)
+                                                r = 0;
+                                        else {
+                                                r = k;
+                                                log_error("Failed to send message: %s", strerror(-r));
+                                        }
+
                                         goto finish;
                                 }
                         }
@@ -724,8 +774,6 @@ int main(int argc, char *argv[]) {
                 }
         }
 
-        r = 0;
-
 finish:
         sd_bus_flush(a);
         sd_bus_flush(b);