diff options
-rw-r--r-- | src/core/service.c | 2 | ||||
-rw-r--r-- | src/libsystemd-bus/bus-control.c | 215 | ||||
-rw-r--r-- | src/libsystemd-bus/bus-convenience.c | 2 | ||||
-rw-r--r-- | src/libsystemd-bus/busctl.c | 74 | ||||
-rw-r--r-- | src/libsystemd-bus/libsystemd-bus.sym | 1 | ||||
-rw-r--r-- | src/systemd/sd-bus.h | 4 |
6 files changed, 159 insertions, 139 deletions
diff --git a/src/core/service.c b/src/core/service.c index cdbe4c83ee..10bf7cb5a6 100644 --- a/src/core/service.c +++ b/src/core/service.c @@ -3678,7 +3678,7 @@ static void service_bus_name_owner_change( /* Try to acquire PID from bus service */ - r = sd_bus_get_owner_creds(u->manager->api_bus, name, SD_BUS_CREDS_PID, &creds); + r = sd_bus_get_owner(u->manager->api_bus, name, SD_BUS_CREDS_PID, NULL, &creds); if (r >= 0) r = sd_bus_creds_get_pid(creds, &pid); if (r >= 0) { diff --git a/src/libsystemd-bus/bus-control.c b/src/libsystemd-bus/bus-control.c index 43e2848ecc..1d416b7dc4 100644 --- a/src/libsystemd-bus/bus-control.c +++ b/src/libsystemd-bus/bus-control.c @@ -220,73 +220,37 @@ _public_ int sd_bus_list_names(sd_bus *bus, char ***l) { return 0; } -_public_ int sd_bus_get_owner(sd_bus *bus, const char *name, char **owner) { - _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; - const char *found; - int r; - - assert_return(bus, -EINVAL); - assert_return(name, -EINVAL); - assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN); - assert_return(!bus_pid_changed(bus), -ECHILD); - - r = sd_bus_call_method( - bus, - "org.freedesktop.DBus", - "/", - "org.freedesktop.DBus", - "GetNameOwner", - NULL, - &reply, - "s", - name); - if (r < 0) - return r; - - r = sd_bus_message_read(reply, "s", &found); - if (r < 0) - return r; - - if (owner) { - char *t; - - t = strdup(found); - if (!t) - return -ENOMEM; - - *owner = t; - } - - return 0; -} +_public_ int sd_bus_get_owner( + sd_bus *bus, + const char *name, + uint64_t mask, + char **owner, + sd_bus_creds **creds) { -_public_ int sd_bus_get_owner_creds(sd_bus *bus, const char *name, uint64_t mask, sd_bus_creds **creds) { _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; _cleanup_bus_creds_unref_ sd_bus_creds *c = NULL; + _cleanup_free_ char *unique = NULL; pid_t pid = 0; int r; assert_return(bus, -EINVAL); assert_return(name, -EINVAL); assert_return(mask <= _SD_BUS_CREDS_MAX, -ENOTSUP); - assert_return(creds, -EINVAL); + assert_return(mask == 0 || creds, -EINVAL); assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN); assert_return(!bus_pid_changed(bus), -ECHILD); - c = bus_creds_new(); - if (!c) - return -ENOMEM; - - if ((mask & SD_BUS_CREDS_PID) || - mask & ~(SD_BUS_CREDS_PID|SD_BUS_CREDS_UID|SD_BUS_CREDS_SELINUX_CONTEXT)) { - uint32_t u; + /* Only query the owner if the caller wants to know it or if + * the caller just wants to check whether a name exists */ + if (owner || mask == 0) { + const char *found; r = sd_bus_call_method( bus, "org.freedesktop.DBus", "/", "org.freedesktop.DBus", - "GetConnectionUnixProcessID", + "GetNameOwner", NULL, &reply, "s", @@ -294,79 +258,120 @@ _public_ int sd_bus_get_owner_creds(sd_bus *bus, const char *name, uint64_t mask if (r < 0) return r; - r = sd_bus_message_read(reply, "u", &u); + r = sd_bus_message_read(reply, "s", &found); if (r < 0) return r; - pid = u; - if (mask & SD_BUS_CREDS_PID) { - c->pid = u; - c->mask |= SD_BUS_CREDS_PID; - } + unique = strdup(found); + if (!unique) + return -ENOMEM; reply = sd_bus_message_unref(reply); } - if (mask & SD_BUS_CREDS_UID) { - uint32_t u; - - r = sd_bus_call_method( - bus, - "org.freedesktop.DBus", - "/", - "org.freedesktop.DBus", - "GetConnectionUnixUser", - NULL, - &reply, - "s", - name); - if (r < 0) - return r; - - r = sd_bus_message_read(reply, "u", &u); - if (r < 0) - return r; + if (mask != 0) { + c = bus_creds_new(); + if (!c) + return -ENOMEM; - c->uid = u; - c->mask |= SD_BUS_CREDS_UID; + if ((mask & SD_BUS_CREDS_PID) || + mask & ~(SD_BUS_CREDS_PID|SD_BUS_CREDS_UID|SD_BUS_CREDS_SELINUX_CONTEXT)) { + uint32_t u; + + r = sd_bus_call_method( + bus, + "org.freedesktop.DBus", + "/", + "org.freedesktop.DBus", + "GetConnectionUnixProcessID", + NULL, + &reply, + "s", + name); + if (r < 0) + return r; + + r = sd_bus_message_read(reply, "u", &u); + if (r < 0) + return r; + + pid = u; + if (mask & SD_BUS_CREDS_PID) { + c->pid = u; + c->mask |= SD_BUS_CREDS_PID; + } - reply = sd_bus_message_unref(reply); - } + reply = sd_bus_message_unref(reply); + } - if (mask & SD_BUS_CREDS_SELINUX_CONTEXT) { - const void *p; - size_t sz; + if (mask & SD_BUS_CREDS_UID) { + uint32_t u; + + r = sd_bus_call_method( + bus, + "org.freedesktop.DBus", + "/", + "org.freedesktop.DBus", + "GetConnectionUnixUser", + NULL, + &reply, + "s", + name); + if (r < 0) + return r; + + r = sd_bus_message_read(reply, "u", &u); + if (r < 0) + return r; + + c->uid = u; + c->mask |= SD_BUS_CREDS_UID; + + reply = sd_bus_message_unref(reply); + } - r = sd_bus_call_method( - bus, - "org.freedesktop.DBus", - "/", - "org.freedesktop.DBus", - "GetConnectionSELinuxSecurityContext", - NULL, - &reply, - "s", - name); - if (r < 0) - return r; + if (mask & SD_BUS_CREDS_SELINUX_CONTEXT) { + const void *p; + size_t sz; + + r = sd_bus_call_method( + bus, + "org.freedesktop.DBus", + "/", + "org.freedesktop.DBus", + "GetConnectionSELinuxSecurityContext", + NULL, + &reply, + "s", + name); + if (r < 0) + return r; + + r = sd_bus_message_read_array(reply, 'y', &p, &sz); + if (r < 0) + return r; + + c->label = strndup(p, sz); + if (!c->label) + return -ENOMEM; + + c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT; + } - r = sd_bus_message_read_array(reply, 'y', &p, &sz); + r = bus_creds_add_more(c, mask, pid, 0); if (r < 0) return r; - - c->label = strndup(p, sz); - if (!c->label) - return -ENOMEM; - - c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT; } - r = bus_creds_add_more(c, mask, pid, 0); - if (r < 0) - return r; + if (creds) { + *creds = c; + c = NULL; + } - *creds = c; - c = NULL; + if (owner) { + *owner = unique; + unique = NULL; + } return 0; } diff --git a/src/libsystemd-bus/bus-convenience.c b/src/libsystemd-bus/bus-convenience.c index e57b26bf6b..1a9c51fdc7 100644 --- a/src/libsystemd-bus/bus-convenience.c +++ b/src/libsystemd-bus/bus-convenience.c @@ -433,7 +433,7 @@ _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) - return sd_bus_get_owner_creds(call->bus, call->sender, mask, creds); + return sd_bus_get_owner(call->bus, call->sender, mask, NULL, creds); else return sd_bus_get_peer_creds(call->bus, mask, creds); } diff --git a/src/libsystemd-bus/busctl.c b/src/libsystemd-bus/busctl.c index 24db48a7e6..4d4f38e0a8 100644 --- a/src/libsystemd-bus/busctl.c +++ b/src/libsystemd-bus/busctl.c @@ -36,6 +36,7 @@ static bool arg_no_pager = false; static char *arg_address = NULL; static bool arg_no_unique = false; +static bool arg_no_machine = false; static char **arg_matches = NULL; static BusTransport arg_transport = BUS_TRANSPORT_LOCAL; static char *arg_host = NULL; @@ -71,9 +72,14 @@ static int list_bus_names(sd_bus *bus, char **argv) { STRV_FOREACH(i, l) max_i = MAX(max_i, strlen(*i)); - printf("%-*s %*s %-*s %-*s %-*s MACHINE\n", + printf("%-*s %*s %-*s %-*s %-*s", (int) max_i, "NAME", 10, "PID", 15, "PROCESS", 16, "USER", 20, "CONNECTION"); + if (!arg_no_machine) + puts(" MACHINE"); + else + putchar('\n'); + STRV_FOREACH(i, l) { _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL; _cleanup_free_ char *owner = NULL; @@ -84,7 +90,7 @@ static int list_bus_names(sd_bus *bus, char **argv) { printf("%-*s", (int) max_i, *i); - r = sd_bus_get_owner_creds(bus, *i, SD_BUS_CREDS_UID|SD_BUS_CREDS_PID|SD_BUS_CREDS_COMM, &creds); + r = sd_bus_get_owner(bus, *i, SD_BUS_CREDS_UID|SD_BUS_CREDS_PID|SD_BUS_CREDS_COMM, &owner, &creds); if (r >= 0) { pid_t pid; uid_t uid; @@ -97,7 +103,7 @@ static int list_bus_names(sd_bus *bus, char **argv) { printf(" %10lu %-15s", (unsigned long) pid, strna(comm)); } else - printf(" - - "); + fputs(" - - ", stdout); r = sd_bus_creds_get_uid(creds, &uid); if (r >= 0) { @@ -112,23 +118,26 @@ static int list_bus_names(sd_bus *bus, char **argv) { printf(" %-16s", u); } else - printf(" - "); - } else - printf(" - - - "); - + fputs(" - ", stdout); - r = sd_bus_get_owner(bus, *i, &owner); - if (r >= 0) - printf(" %-20s", owner); - else - printf(" - "); + if (owner) + printf(" %-20s", owner); + else + fputs(" - ", stdout); - r = sd_bus_get_owner_machine_id(bus, *i, &mid); - if (r >= 0) { - char m[SD_ID128_STRING_MAX]; - printf(" %s\n", sd_id128_to_string(mid, m)); } else - printf(" -\n"); + printf(" - - - - "); + + if (arg_no_machine) + putchar('\n'); + else { + r = sd_bus_get_owner_machine_id(bus, *i, &mid); + if (r >= 0) { + char m[SD_ID128_STRING_MAX]; + printf(" %s\n", sd_id128_to_string(mid, m)); + } else + puts(" -"); + } } return 0; @@ -218,7 +227,8 @@ static int help(void) { " -M --machine=CONTAINER Operate on local container\n" " --address=ADDRESS Connect to bus specified by address\n" " --no-unique Only show well-known names\n" - " --match=MATCH Only show matching messages\n\n" + " --no-machine Don't show machine ID column in list\n\n" + " --match=MATCH Only show matching messages\n" "Commands:\n" " list List bus names\n" " monitor [SERVICE...] Show bus traffic\n", @@ -236,20 +246,22 @@ static int parse_argv(int argc, char *argv[]) { ARG_USER, ARG_ADDRESS, ARG_MATCH, - ARG_NO_UNIQUE + ARG_NO_UNIQUE, + ARG_NO_MACHINE, }; static const struct option options[] = { - { "help", no_argument, NULL, 'h' }, - { "version", no_argument, NULL, ARG_VERSION }, - { "no-pager", no_argument, NULL, ARG_NO_PAGER }, - { "system", no_argument, NULL, ARG_SYSTEM }, - { "user", no_argument, NULL, ARG_USER }, - { "address", required_argument, NULL, ARG_ADDRESS }, - { "no-unique", no_argument, NULL, ARG_NO_UNIQUE }, - { "match", required_argument, NULL, ARG_MATCH }, - { "host", required_argument, NULL, 'H' }, - { "machine", required_argument, NULL, 'M' }, + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, ARG_VERSION }, + { "no-pager", no_argument, NULL, ARG_NO_PAGER }, + { "system", no_argument, NULL, ARG_SYSTEM }, + { "user", no_argument, NULL, ARG_USER }, + { "address", required_argument, NULL, ARG_ADDRESS }, + { "no-unique", no_argument, NULL, ARG_NO_UNIQUE }, + { "no-machine", no_argument, NULL, ARG_NO_MACHINE }, + { "match", required_argument, NULL, ARG_MATCH }, + { "host", required_argument, NULL, 'H' }, + { "machine", required_argument, NULL, 'M' }, {}, }; @@ -290,6 +302,10 @@ static int parse_argv(int argc, char *argv[]) { arg_no_unique = true; break; + case ARG_NO_MACHINE: + arg_no_machine = true; + break; + case ARG_MATCH: if (strv_extend(&arg_matches, optarg) < 0) return log_oom(); diff --git a/src/libsystemd-bus/libsystemd-bus.sym b/src/libsystemd-bus/libsystemd-bus.sym index 435c794b14..054bc3ff0b 100644 --- a/src/libsystemd-bus/libsystemd-bus.sym +++ b/src/libsystemd-bus/libsystemd-bus.sym @@ -130,7 +130,6 @@ global: sd_bus_release_name; sd_bus_list_names; sd_bus_get_owner; - sd_bus_get_owner_creds; sd_bus_get_owner_machine_id; /* Convenience calls */ diff --git a/src/systemd/sd-bus.h b/src/systemd/sd-bus.h index 202fdb87be..1a269f53b1 100644 --- a/src/systemd/sd-bus.h +++ b/src/systemd/sd-bus.h @@ -161,6 +161,7 @@ int sd_bus_add_object_manager(sd_bus *bus, const char *path); int sd_bus_remove_object_manager(sd_bus *bus, const char *path); /* Message object */ + int sd_bus_message_new_signal(sd_bus *bus, const char *path, const char *interface, const char *member, sd_bus_message **m); int sd_bus_message_new_method_call(sd_bus *bus, const char *destination, const char *path, const char *interface, const char *member, sd_bus_message **m); int sd_bus_message_new_method_return(sd_bus_message *call, sd_bus_message **m); @@ -233,8 +234,7 @@ int sd_bus_get_unique_name(sd_bus *bus, const char **unique); int sd_bus_request_name(sd_bus *bus, const char *name, int flags); int sd_bus_release_name(sd_bus *bus, const char *name); int sd_bus_list_names(sd_bus *bus, char ***l); -int sd_bus_get_owner(sd_bus *bus, const char *name, char **owner); /* free the result! */ -int sd_bus_get_owner_creds(sd_bus *bus, const char *name, uint64_t mask, sd_bus_creds **creds); /* unref the result! */ +int sd_bus_get_owner(sd_bus *bus, const char *name, uint64_t mask, char **owner, sd_bus_creds **creds); /* free/unref the result! */ int sd_bus_get_owner_machine_id(sd_bus *bus, const char *name, sd_id128_t *machine); /* Convenience calls */ |