diff options
author | Lennart Poettering <lennart@poettering.net> | 2013-11-28 20:41:55 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2013-11-28 20:44:26 +0100 |
commit | a4297f08ed774ed6d980b6cadaf844bfb2c7a403 (patch) | |
tree | c20b812d660eade49aa45a226e6fa041cc64c0cd /src/libsystemd-bus | |
parent | df41aaf9a2b195c9a8bb6fca6672cbf25bc147fb (diff) |
bus: merge sd_bus_get_owner() and sd_bus_get_owner_creds() into one call
Since the backing ioctl for this on kdbus is the same we retain
atomicity this way.
Diffstat (limited to 'src/libsystemd-bus')
-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 |
4 files changed, 156 insertions, 136 deletions
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 */ |