Skip to content

Commit

Permalink
machined: watch leader PID's lifetime via pidfd
Browse files Browse the repository at this point in the history
If we have a pidfd, we might as well track the machine's leader PID's
lifetime, and enqueue the machine for a GC run.

(This is similar to what we are already doing for logind's session
leaders)
  • Loading branch information
poettering committed Jun 20, 2024
1 parent 9b27811 commit 1762c2c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/machine/machine.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ Machine* machine_free(Machine *m) {
m->manager->host_machine = NULL;
}

m->leader_pidfd_event_source = sd_event_source_disable_unref(m->leader_pidfd_event_source);
if (pidref_is_set(&m->leader)) {
if (m->manager)
(void) hashmap_remove_value(m->manager->machine_leaders, PID_TO_PTR(m->leader.pid), m);
Expand Down Expand Up @@ -475,6 +476,38 @@ static int machine_ensure_scope(Machine *m, sd_bus_message *properties, sd_bus_e
return 0;
}

static int machine_dispatch_leader_pidfd(sd_event_source *s, int fd, unsigned revents, void *userdata) {
Machine *m = ASSERT_PTR(userdata);

m->leader_pidfd_event_source = sd_event_source_disable_unref(m->leader_pidfd_event_source);
machine_add_to_gc_queue(m);

return 0;
}

static int machine_watch_pidfd(Machine *m) {
int r;

assert(m);
assert(m->manager);
assert(pidref_is_set(&m->leader));
assert(!m->leader_pidfd_event_source);

if (m->leader.fd < 0)
return 0;

/* If we have a pidfd for the leader, let's also track it for POLLIN, and GC the machine
* automatically if it dies */

r = sd_event_add_io(m->manager->event, &m->leader_pidfd_event_source, m->leader.fd, EPOLLIN, machine_dispatch_leader_pidfd, m);
if (r < 0)
return r;

(void) sd_event_source_set_description(m->leader_pidfd_event_source, "machine-pidfd");

return 0;
}

int machine_start(Machine *m, sd_bus_message *properties, sd_bus_error *error) {
int r;

Expand All @@ -490,6 +523,10 @@ int machine_start(Machine *m, sd_bus_message *properties, sd_bus_error *error) {
if (r < 0)
return r;

r = machine_watch_pidfd(m);
if (r < 0)
return r;

/* Create cgroup */
r = machine_ensure_scope(m, properties, error);
if (r < 0)
Expand Down
1 change: 1 addition & 0 deletions src/machine/machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ struct Machine {
char *scope_job;

PidRef leader;
sd_event_source *leader_pidfd_event_source;

dual_timestamp timestamp;

Expand Down

0 comments on commit 1762c2c

Please sign in to comment.