]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
machine: switch to use PidRef when lookup machine by pid in dbus and varlink interfaces
authorIvan Kruglov <mail@ikruglov.com>
Thu, 10 Oct 2024 09:51:57 +0000 (11:51 +0200)
committerIvan Kruglov <mail@ikruglov.com>
Thu, 10 Oct 2024 11:40:45 +0000 (13:40 +0200)
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.

src/machine/machine-dbus.c
src/machine/machine-varlink.c
src/machine/machined-core.c
src/machine/machined-dbus.c
src/machine/machined.h

index 85cb00a8a6889596a6d54d31c9590841d5607632..fc7c9a83031c0810f90d0ac8656934f8e8cd752c 100644 (file)
@@ -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 {
index f75f481e6e564b5cdaaa0d8928eec43e769bccaa..f5acb33eafb64f91f6b0a06264086a94811696e8 100644 (file)
@@ -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)
index d921dfb27e91ee45548baddd537232c61b9a887e..baa9a605ba37d095267a8d7f4cab8da4aa05a472 100644 (file)
 #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);
         }
index e86be6d4d5724cd804f7b7d03bc65daadd8603d9..730b436545d02d8dbd7697652ea0f9f52990125b 100644 (file)
@@ -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)
index 4a718f94627d7faf7dc17151a82879711f1e2c30..721bf6801225c31db082b600b80b8ae29fac03b5 100644 (file)
@@ -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;