From: Ivan Kruglov Date: Thu, 10 Oct 2024 09:51:57 +0000 (+0200) Subject: machine: switch to use PidRef when lookup machine by pid in dbus and varlink interfaces X-Git-Tag: v257-rc1~259^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f6cb4d4afb1c88551088b00724140f80d1368f73;p=thirdparty%2Fsystemd.git machine: switch to use PidRef when lookup machine by pid in dbus and varlink interfaces This commit includes adding introduce manager_get_machine_by_pidref() as a replacement for manager_get_machine_by_pid() and moving surrounding code to utilise PidRef. --- diff --git a/src/machine/machine-dbus.c b/src/machine/machine-dbus.c index 85cb00a8a68..fc7c9a83031 100644 --- a/src/machine/machine-dbus.c +++ b/src/machine/machine-dbus.c @@ -995,23 +995,23 @@ static int machine_object_find(sd_bus *bus, const char *path, const char *interf assert(found); if (streq(path, "/org/freedesktop/machine1/machine/self")) { + _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; sd_bus_message *message; - pid_t pid; message = sd_bus_get_current_message(bus); if (!message) return 0; - r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds); + r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID|SD_BUS_CREDS_PIDFD, &creds); if (r < 0) return r; - r = sd_bus_creds_get_pid(creds, &pid); + r = bus_creds_get_pidref(creds, &pidref); if (r < 0) return r; - r = manager_get_machine_by_pid(m, pid, &machine); + r = manager_get_machine_by_pidref(m, &pidref, &machine); if (r <= 0) return 0; } else { diff --git a/src/machine/machine-varlink.c b/src/machine/machine-varlink.c index f75f481e6e5..f5acb33eafb 100644 --- a/src/machine/machine-varlink.c +++ b/src/machine/machine-varlink.c @@ -210,6 +210,7 @@ static int lookup_machine_by_name(sd_varlink *link, Manager *manager, const char } static int lookup_machine_by_pid(sd_varlink *link, Manager *manager, pid_t pid, Machine **ret_machine) { + _cleanup_(pidref_done) PidRef pidref = PIDREF_MAKE_FROM_PID(pid); Machine *machine; int r; @@ -219,19 +220,15 @@ static int lookup_machine_by_pid(sd_varlink *link, Manager *manager, pid_t pid, assert_cc(sizeof(pid_t) == sizeof(uint32_t)); if (pid == 0) { - int pidfd = sd_varlink_get_peer_pidfd(link); - if (pidfd < 0) - return log_debug_errno(pidfd, "Failed to get peer pidfd: %m"); - - r = pidfd_get_pid(pidfd, &pid); + r = varlink_get_peer_pidref(link, &pidref); if (r < 0) - return log_debug_errno(r, "Failed to get pid from pidfd: %m"); + return log_debug_errno(r, "Failed to get peer pidref: %m"); } - if (pid <= 0) + if (!pidref_is_set(&pidref)) return -EINVAL; - r = manager_get_machine_by_pid(manager, pid, &machine); + r = manager_get_machine_by_pidref(manager, &pidref, &machine); if (r < 0) return r; if (!machine) diff --git a/src/machine/machined-core.c b/src/machine/machined-core.c index d921dfb27e9..baa9a605ba3 100644 --- a/src/machine/machined-core.c +++ b/src/machine/machined-core.c @@ -12,19 +12,19 @@ #include "strv.h" #include "user-util.h" -int manager_get_machine_by_pid(Manager *m, pid_t pid, Machine **ret) { +int manager_get_machine_by_pidref(Manager *m, const PidRef *pidref, Machine **ret) { Machine *mm; int r; assert(m); - assert(pid_is_valid(pid)); + assert(pidref_is_set(pidref)); assert(ret); - mm = hashmap_get(m->machines_by_leader, &PIDREF_MAKE_FROM_PID(pid)); + mm = hashmap_get(m->machines_by_leader, pidref); if (!mm) { _cleanup_free_ char *unit = NULL; - r = cg_pid_get_unit(pid, &unit); + r = cg_pidref_get_unit(pidref, &unit); if (r >= 0) mm = hashmap_get(m->machines_by_unit, unit); } diff --git a/src/machine/machined-dbus.c b/src/machine/machined-dbus.c index e86be6d4d57..730b436545d 100644 --- a/src/machine/machined-dbus.c +++ b/src/machine/machined-dbus.c @@ -137,6 +137,7 @@ static int method_get_image(sd_bus_message *message, void *userdata, sd_bus_erro } static int method_get_machine_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) { + _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; _cleanup_free_ char *p = NULL; Manager *m = ASSERT_PTR(userdata); Machine *machine = NULL; @@ -154,19 +155,21 @@ static int method_get_machine_by_pid(sd_bus_message *message, void *userdata, sd if (pid < 0) return -EINVAL; + pidref = PIDREF_MAKE_FROM_PID(pid); + if (pid == 0) { _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; - r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds); + r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID|SD_BUS_CREDS_PIDFD, &creds); if (r < 0) return r; - r = sd_bus_creds_get_pid(creds, &pid); + r = bus_creds_get_pidref(creds, &pidref); if (r < 0) return r; } - r = manager_get_machine_by_pid(m, pid, &machine); + r = manager_get_machine_by_pidref(m, &pidref, &machine); if (r < 0) return r; if (r == 0) diff --git a/src/machine/machined.h b/src/machine/machined.h index 4a718f94627..721bf680122 100644 --- a/src/machine/machined.h +++ b/src/machine/machined.h @@ -16,6 +16,7 @@ typedef struct Manager Manager; #include "machine-dbus.h" #include "machine.h" #include "operation.h" +#include "pidref.h" struct Manager { sd_event *event; @@ -44,7 +45,7 @@ struct Manager { }; int manager_add_machine(Manager *m, const char *name, Machine **ret); -int manager_get_machine_by_pid(Manager *m, pid_t pid, Machine **ret); +int manager_get_machine_by_pidref(Manager *m, const PidRef *pidref, Machine **ret); extern const BusObjectImplementation manager_object;