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 "PID_FMT" (%s), UID "UID_FMT" (%s), %s",
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;
return 1;
}
+static int synthetic_driver_send(sd_bus *b, sd_bus_message *m) {
+ int r;
+
+ assert(b);
+ assert(m);
+
+ r = bus_message_append_sender(m, "org.freedesktop.DBus");
+ if (r < 0)
+ return r;
+
+ r = bus_seal_synthetic_message(b, m);
+ if (r < 0)
+ return r;
+
+ return sd_bus_send(b, m, NULL);
+}
+
+static int synthetic_reply_method_error(sd_bus_message *call, const sd_bus_error *e) {
+ _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+ int r;
+
+ if (call->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
+ return 0;
+
+ r = sd_bus_message_new_method_error(call, &m, e);
+ if (r < 0)
+ return r;
+
+ return synthetic_driver_send(call->bus, m);
+}
+
+static 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;
+
+ if (call->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
+ return 0;
+
+ if (sd_bus_error_is_set(p))
+ return synthetic_reply_method_error(call, p);
+
+ sd_bus_error_set_errno(&berror, error);
+
+ return synthetic_reply_method_error(call, &berror);
+}
+
+static int synthetic_reply_method_return(sd_bus_message *call, const char *types, ...) {
+
+ _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+ int r;
+
+ if (call->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
+ return 0;
+
+ r = sd_bus_message_new_method_return(call, &m);
+ if (r < 0)
+ return r;
+
+ if (!isempty(types)) {
+ va_list ap;
+
+ va_start(ap, types);
+ r = bus_message_append_ap(m, types, ap);
+ va_end(ap);
+ if (r < 0)
+ return r;
+ }
+
+ return synthetic_driver_send(call->bus, m);
+}
+
+static int process_driver(sd_bus *a, sd_bus *b, sd_bus_message *m) {
+ int r;
+
+ assert(a);
+ assert(b);
+ assert(m);
+
+ if (!streq_ptr(sd_bus_message_get_destination(m), "org.freedesktop.DBus"))
+ return 0;
+
+ if (sd_bus_message_is_method_call(m, "org.freedesktop.DBus", "AddMatch")) {
+ const char *match;
+
+ r = sd_bus_message_read(m, "s", &match);
+ if (r < 0)
+ return synthetic_reply_method_errno(m, r, NULL);
+
+ r = sd_bus_add_match(a, match, NULL, NULL);
+ if (r < 0)
+ return synthetic_reply_method_errno(m, r, NULL);
+
+ return synthetic_reply_method_return(m, NULL);
+
+ } else if (sd_bus_message_is_method_call(m, "org.freedesktop.DBus", "RemoveMatch")) {
+ const char *match;
+
+ r = sd_bus_message_read(m, "s", &match);
+ if (r < 0)
+ return synthetic_reply_method_errno(m, r, NULL);
+
+ r = sd_bus_remove_match(a, match, NULL, NULL);
+ if (r < 0)
+ return synthetic_reply_method_errno(m, r, NULL);
+
+ return synthetic_reply_method_return(m, NULL);
+ }
+
+ return 0;
+}
+
static int process_hello(sd_bus *a, sd_bus *b, sd_bus_message *m, bool *got_hello) {
_cleanup_bus_message_unref_ sd_bus_message *n = NULL;
bool is_hello;
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;
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;
goto finish;
}
+ patch_sender(a, m);
+
k = sd_bus_send(b, m, NULL);
if (k < 0) {
if (k == -ECONNRESET)
goto finish;
}
- k = sd_bus_send(a, m, NULL);
+ k = process_driver(a, b, m);
if (k < 0) {
- if (r == -ECONNRESET)
- r = 0;
- else {
- r = k;
- log_error("Failed to send message: %s", strerror(-r));
- }
-
+ r = k;
+ log_error("Failed to process driver calls: %s", strerror(-r));
goto finish;
}
+
+ if (k > 0)
+ r = k;
+ else {
+ k = sd_bus_send(a, m, NULL);
+ if (k < 0) {
+ if (r == -ECONNRESET)
+ r = 0;
+ else {
+ r = k;
+ log_error("Failed to send message: %s", strerror(-r));
+ }
+
+ goto finish;
+ }
+ }
}
}
}
}
- r = 0;
-
finish:
sd_bus_flush(a);
sd_bus_flush(b);