tree-wide: Use pidref_wait_for_terminate_and_check()

Let's use the pidref variants for waiting and
checking a process everywhere and get rid of the
non pidref variants.
This commit is contained in:
Daan De Meyer
2025-12-20 15:50:47 +01:00
parent e74b571004
commit fbd276cb86
56 changed files with 620 additions and 654 deletions
+12 -12
View File
@@ -32,7 +32,7 @@ int bus_image_method_remove(
_cleanup_close_pair_ int errno_pipe_fd[2] = EBADF_PAIR;
Image *image = ASSERT_PTR(userdata);
Manager *m = image->userdata;
pid_t child;
_cleanup_(pidref_done_sigkill_wait) PidRef child = PIDREF_NULL;
int r;
assert(message);
@@ -62,7 +62,7 @@ int bus_image_method_remove(
if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0)
return sd_bus_error_set_errnof(error, errno, "Failed to create pipe: %m");
r = safe_fork("(sd-imgrm)", FORK_RESET_SIGNALS, &child);
r = pidref_safe_fork("(sd-imgrm)", FORK_RESET_SIGNALS, &child);
if (r < 0)
return sd_bus_error_set_errnof(error, r, "Failed to fork(): %m");
if (r == 0) {
@@ -73,11 +73,11 @@ int bus_image_method_remove(
errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
r = operation_new_with_bus_reply(m, /* machine= */ NULL, child, message, errno_pipe_fd[0], /* ret= */ NULL);
if (r < 0) {
sigkill_wait(child);
r = operation_new_with_bus_reply(m, /* machine= */ NULL, &child, message, errno_pipe_fd[0], /* ret= */ NULL);
if (r < 0)
return r;
}
TAKE_PIDREF(child);
errno_pipe_fd[0] = -EBADF;
@@ -140,7 +140,7 @@ int bus_image_method_clone(
Manager *m = ASSERT_PTR(image->userdata);
const char *new_name;
int r, read_only;
pid_t child;
_cleanup_(pidref_done_sigkill_wait) PidRef child = PIDREF_NULL;
assert(message);
@@ -177,7 +177,7 @@ int bus_image_method_clone(
if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0)
return sd_bus_error_set_errnof(error, errno, "Failed to create pipe: %m");
r = safe_fork("(sd-imgclone)", FORK_RESET_SIGNALS, &child);
r = pidref_safe_fork("(sd-imgclone)", FORK_RESET_SIGNALS, &child);
if (r < 0)
return sd_bus_error_set_errnof(error, r, "Failed to fork(): %m");
if (r == 0) {
@@ -188,11 +188,11 @@ int bus_image_method_clone(
errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
r = operation_new_with_bus_reply(m, /* machine= */ NULL, child, message, errno_pipe_fd[0], /* ret= */ NULL);
if (r < 0) {
sigkill_wait(child);
r = operation_new_with_bus_reply(m, /* machine= */ NULL, &child, message, errno_pipe_fd[0], /* ret= */ NULL);
if (r < 0)
return r;
}
TAKE_PIDREF(child);
errno_pipe_fd[0] = -EBADF;
+10 -12
View File
@@ -115,7 +115,7 @@ int vl_method_clone_image(sd_varlink *link, sd_json_variant *parameters, sd_varl
_cleanup_close_pair_ int errno_pipe_fd[2] = EBADF_PAIR;
ImageUpdateParameters p = IMAGE_UPDATE_PARAMETERS_NULL;
Image *image;
pid_t child;
_cleanup_(pidref_done_sigkill_wait) PidRef child = PIDREF_NULL;
int r;
assert(link);
@@ -156,7 +156,7 @@ int vl_method_clone_image(sd_varlink *link, sd_json_variant *parameters, sd_varl
if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0)
return log_debug_errno(errno, "Failed to open pipe: %m");
r = safe_fork("(sd-imgclone)", FORK_RESET_SIGNALS, &child);
r = pidref_safe_fork("(sd-imgclone)", FORK_RESET_SIGNALS, &child);
if (r < 0)
return log_debug_errno(r, "Failed to fork: %m");
if (r == 0) {
@@ -167,12 +167,11 @@ int vl_method_clone_image(sd_varlink *link, sd_json_variant *parameters, sd_varl
errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
r = operation_new_with_varlink_reply(manager, /* machine= */ NULL, child, link, errno_pipe_fd[0], /* ret= */ NULL);
if (r < 0) {
sigkill_wait(child);
r = operation_new_with_varlink_reply(manager, /* machine= */ NULL, &child, link, errno_pipe_fd[0], /* ret= */ NULL);
if (r < 0)
return r;
}
TAKE_PIDREF(child);
TAKE_FD(errno_pipe_fd[0]);
return 1;
}
@@ -188,7 +187,7 @@ int vl_method_remove_image(sd_varlink *link, sd_json_variant *parameters, sd_var
_cleanup_close_pair_ int errno_pipe_fd[2] = EBADF_PAIR;
const char *image_name;
Image *image;
pid_t child;
_cleanup_(pidref_done_sigkill_wait) PidRef child = PIDREF_NULL;
int r;
assert(link);
@@ -225,7 +224,7 @@ int vl_method_remove_image(sd_varlink *link, sd_json_variant *parameters, sd_var
if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0)
return log_debug_errno(errno, "Failed to open pipe: %m");
r = safe_fork("(sd-imgrm)", FORK_RESET_SIGNALS, &child);
r = pidref_safe_fork("(sd-imgrm)", FORK_RESET_SIGNALS, &child);
if (r < 0)
return log_debug_errno(r, "Failed to fork: %m");
if (r == 0) {
@@ -236,12 +235,11 @@ int vl_method_remove_image(sd_varlink *link, sd_json_variant *parameters, sd_var
errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
r = operation_new_with_varlink_reply(manager, /* machine= */ NULL, child, link, errno_pipe_fd[0], /* ret= */ NULL);
if (r < 0) {
sigkill_wait(child);
r = operation_new_with_varlink_reply(manager, /* machine= */ NULL, &child, link, errno_pipe_fd[0], /* ret= */ NULL);
if (r < 0)
return r;
}
TAKE_PIDREF(child);
TAKE_FD(errno_pipe_fd[0]);
return 1;
}
+4 -4
View File
@@ -96,7 +96,7 @@ int clean_pool_read_next_entry(FILE *file, char **ret_name, uint64_t *ret_usage)
int image_clean_pool_operation(Manager *manager, ImageCleanPoolMode mode, Operation **ret_operation) {
_cleanup_close_pair_ int errno_pipe_fd[2] = EBADF_PAIR;
_cleanup_close_ int result_fd = -EBADF;
_cleanup_(sigkill_waitp) pid_t child = 0;
_cleanup_(pidref_done_sigkill_wait) PidRef child = PIDREF_NULL;
int r;
assert(manager);
@@ -113,7 +113,7 @@ int image_clean_pool_operation(Manager *manager, ImageCleanPoolMode mode, Operat
return log_debug_errno(result_fd, "Failed to open tmpfile: %m");
/* This might be a slow operation, run it asynchronously in a background process */
r = safe_fork("(sd-clean)", FORK_RESET_SIGNALS, &child);
r = pidref_safe_fork("(sd-clean)", FORK_RESET_SIGNALS, &child);
if (r < 0)
return log_debug_errno(r, "Failed to fork(): %m");
if (r == 0) {
@@ -183,13 +183,13 @@ int image_clean_pool_operation(Manager *manager, ImageCleanPoolMode mode, Operat
errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
/* The clean-up might take a while, hence install a watch on the child and return */
r = operation_new(manager, /* machine= */ NULL, child, errno_pipe_fd[0], ret_operation);
r = operation_new(manager, /* machine= */ NULL, &child, errno_pipe_fd[0], ret_operation);
if (r < 0)
return r;
(*ret_operation)->extra_fd = TAKE_FD(result_fd);
TAKE_FD(errno_pipe_fd[0]);
TAKE_PID(child);
TAKE_PIDREF(child);
return 0;
}
+1 -1
View File
@@ -1244,7 +1244,7 @@ int machine_copy_from_to_operation(
// TODO: port to PidRef and donate child rather than destroying it
Operation *operation;
r = operation_new(manager, machine, child.pid, errno_pipe_fd[0], &operation);
r = operation_new(manager, machine, &child, errno_pipe_fd[0], &operation);
if (r < 0)
return r;
+9 -9
View File
@@ -8,6 +8,7 @@
#include "sd-varlink.h"
#include "alloc-util.h"
#include "event-util.h"
#include "fd-util.h"
#include "format-util.h"
#include "log.h"
@@ -45,10 +46,10 @@ static int operation_done(sd_event_source *s, const siginfo_t *si, void *userdat
assert(si);
log_debug("Operation " PID_FMT " is now complete with code=%s status=%i",
o->pid,
o->pidref.pid,
sigchld_code_to_string(si->si_code), si->si_status);
o->pid = 0;
pidref_done(&o->pidref);
r = read_operation_errno(si, o);
if (r < 0)
@@ -96,12 +97,12 @@ static int operation_done(sd_event_source *s, const siginfo_t *si, void *userdat
return 0;
}
int operation_new(Manager *manager, Machine *machine, pid_t child, int errno_fd, Operation **ret) {
int operation_new(Manager *manager, Machine *machine, PidRef *child, int errno_fd, Operation **ret) {
Operation *o;
int r;
assert(manager);
assert(child > 1);
assert(pidref_is_set(child));
assert(errno_fd >= 0);
assert(ret);
@@ -110,12 +111,12 @@ int operation_new(Manager *manager, Machine *machine, pid_t child, int errno_fd,
return -ENOMEM;
*o = (Operation) {
.pid = child,
.pidref = TAKE_PIDREF(*child),
.errno_fd = errno_fd,
.extra_fd = -EBADF
};
r = sd_event_add_child(manager->event, &o->event_source, child, WEXITED, operation_done, o);
r = event_add_child_pidref(manager->event, &o->event_source, &o->pidref, WEXITED, operation_done, o);
if (r < 0) {
free(o);
return r;
@@ -130,7 +131,7 @@ int operation_new(Manager *manager, Machine *machine, pid_t child, int errno_fd,
o->machine = machine;
}
log_debug("Started new operation " PID_FMT ".", child);
log_debug("Started new operation " PID_FMT ".", o->pidref.pid);
/* At this point we took ownership of both the child and the errno file descriptor! */
@@ -147,8 +148,7 @@ Operation *operation_free(Operation *o) {
safe_close(o->errno_fd);
safe_close(o->extra_fd);
if (o->pid > 1)
sigkill_wait(o->pid);
pidref_done_sigkill_wait(&o->pidref);
sd_bus_message_unref(o->message);
sd_varlink_unref(o->link);
+5 -4
View File
@@ -3,13 +3,14 @@
#include "list.h"
#include "machine-forward.h"
#include "pidref.h"
#define OPERATIONS_MAX 64
typedef struct Operation {
Manager *manager;
Machine *machine;
pid_t pid;
PidRef pidref;
/* only one of these two fields should be set */
sd_varlink *link;
@@ -23,7 +24,7 @@ typedef struct Operation {
LIST_FIELDS(Operation, operations_by_machine);
} Operation;
int operation_new(Manager *manager, Machine *machine, pid_t child, int errno_fd, Operation **ret);
int operation_new(Manager *manager, Machine *machine, PidRef *child, int errno_fd, Operation **ret);
Operation *operation_free(Operation *o);
void operation_attach_bus_reply(Operation *op, sd_bus_message *message);
@@ -32,7 +33,7 @@ void operation_attach_varlink_reply(Operation *op, sd_varlink *link);
static inline int operation_new_with_bus_reply(
Manager *manager,
Machine *machine,
pid_t child,
PidRef *child,
sd_bus_message *message,
int errno_fd,
Operation **ret) {
@@ -55,7 +56,7 @@ static inline int operation_new_with_bus_reply(
static inline int operation_new_with_varlink_reply(
Manager *manager,
Machine *machine,
pid_t child,
PidRef *child,
sd_varlink *link,
int errno_fd,
Operation **ret) {