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.
This commit is contained in:
Lennart Poettering
2024-01-29 14:42:42 +01:00
parent 75e00d5e32
commit 1b78be0bb5
2 changed files with 48 additions and 0 deletions
+44
View File
@@ -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);
}
+4
View File
@@ -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);