From: Lennart Poettering Date: Fri, 26 Jan 2024 17:40:09 +0000 (+0100) Subject: bus-util: add helper for getting PidRef structs from bus X-Git-Tag: v256-rc1~1021^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1b78be0bb567d07f00922809411c16887a084033;p=thirdparty%2Fsystemd.git bus-util: add helper for getting PidRef structs from bus This adds two helpers: one for extracting a PidRef from an sd_bus_creds object, and one from doing this from and sd_bus_message object. --- diff --git a/src/shared/bus-util.c b/src/shared/bus-util.c index 09a3734aebc..eea6c2321af 100644 --- a/src/shared/bus-util.c +++ b/src/shared/bus-util.c @@ -710,3 +710,47 @@ int bus_property_get_string_set( return bus_message_append_string_set(reply, *s); } + +int bus_creds_get_pidref( + sd_bus_creds *c, + PidRef *ret) { + + int pidfd = -EBADF; + pid_t pid; + int r; + + assert(c); + assert(ret); + + r = sd_bus_creds_get_pid(c, &pid); + if (r < 0) + return r; + + r = sd_bus_creds_get_pidfd_dup(c, &pidfd); + if (r < 0 && r != -ENODATA) + return r; + + *ret = (PidRef) { + .pid = pid, + .fd = pidfd, + }; + + return 0; +} + +int bus_query_sender_pidref( + sd_bus_message *m, + PidRef *ret) { + + _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; + int r; + + assert(m); + assert(ret); + + r = sd_bus_query_sender_creds(m, SD_BUS_CREDS_PID|SD_BUS_CREDS_PIDFD, &creds); + if (r < 0) + return r; + + return bus_creds_get_pidref(creds, ret); +} diff --git a/src/shared/bus-util.h b/src/shared/bus-util.h index 869c639aeb4..1004c794069 100644 --- a/src/shared/bus-util.h +++ b/src/shared/bus-util.h @@ -11,6 +11,7 @@ #include "errno-util.h" #include "macro.h" +#include "pidref.h" #include "runtime-scope.h" #include "set.h" #include "string-util.h" @@ -73,3 +74,6 @@ extern const struct hash_ops bus_message_hash_ops; int bus_message_append_string_set(sd_bus_message *m, Set *s); int bus_property_get_string_set(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error); + +int bus_creds_get_pidref(sd_bus_creds *c, PidRef *ret); +int bus_query_sender_pidref(sd_bus_message *m, PidRef *ret);