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.
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 {
}
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;
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)
#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);
}
}
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;
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)
#include "machine-dbus.h"
#include "machine.h"
#include "operation.h"
+#include "pidref.h"
struct Manager {
sd_event *event;
};
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;