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
+5 -5
View File
@@ -15,6 +15,7 @@
#include "lock-util.h"
#include "log.h"
#include "path-util.h"
#include "pidref.h"
#include "process-util.h"
#include "string-util.h"
#include "time-util.h"
@@ -199,7 +200,6 @@ int lock_generic(int fd, LockType type, int operation) {
}
int lock_generic_with_timeout(int fd, LockType type, int operation, usec_t timeout) {
_cleanup_(sigkill_waitp) pid_t pid = 0;
int r;
assert(fd >= 0);
@@ -223,8 +223,8 @@ int lock_generic_with_timeout(int fd, LockType type, int operation, usec_t timeo
return r;
/* If that didn't work, try with a child */
r = safe_fork("(sd-flock)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL, &pid);
_cleanup_(pidref_done_sigkill_wait) PidRef pidref = PIDREF_NULL;
r = pidref_safe_fork("(sd-flock)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL, &pidref);
if (r < 0)
return log_error_errno(r, "Failed to flock block device in child process: %m");
if (r == 0) {
@@ -256,11 +256,11 @@ int lock_generic_with_timeout(int fd, LockType type, int operation, usec_t timeo
}
siginfo_t status;
r = wait_for_terminate(pid, &status);
r = pidref_wait_for_terminate(&pidref, &status);
if (r < 0)
return r;
TAKE_PID(pid);
pidref_done(&pidref);
switch (status.si_code) {
+10 -10
View File
@@ -601,25 +601,25 @@ int userns_acquire_self_root(void) {
return userns_acquire(uid_map, gid_map, /* setgroups_deny= */ true);
}
int userns_enter_and_pin(int userns_fd, pid_t *ret_pid) {
int userns_enter_and_pin(int userns_fd, PidRef *ret) {
_cleanup_close_pair_ int pfd[2] = EBADF_PAIR;
_cleanup_(sigkill_waitp) pid_t pid = 0;
_cleanup_(pidref_done_sigkill_wait) PidRef pidref = PIDREF_NULL;
ssize_t n;
char x;
int r;
assert(userns_fd >= 0);
assert(ret_pid);
assert(ret);
if (pipe2(pfd, O_CLOEXEC) < 0)
return -errno;
r = safe_fork_full(
r = pidref_safe_fork_full(
"(sd-pinuserns)",
/* stdio_fds= */ NULL,
(int[]) { pfd[1], userns_fd }, 2,
FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGKILL,
&pid);
&pidref);
if (r < 0)
return r;
if (r == 0) {
@@ -652,7 +652,7 @@ int userns_enter_and_pin(int userns_fd, pid_t *ret_pid) {
assert(n == 1);
assert(x == 'x');
*ret_pid = TAKE_PID(pid);
*ret = TAKE_PIDREF(pidref);
return 0;
}
@@ -661,22 +661,22 @@ bool userns_supported(void) {
}
int userns_get_base_uid(int userns_fd, uid_t *ret_uid, gid_t *ret_gid) {
_cleanup_(sigkill_waitp) pid_t pid = 0;
_cleanup_(pidref_done_sigkill_wait) PidRef pidref = PIDREF_NULL;
int r;
assert(userns_fd >= 0);
r = userns_enter_and_pin(userns_fd, &pid);
r = userns_enter_and_pin(userns_fd, &pidref);
if (r < 0)
return r;
uid_t uid;
r = uid_map_search_root(pid, UID_RANGE_USERNS_OUTSIDE, &uid);
r = uid_map_search_root(pidref.pid, UID_RANGE_USERNS_OUTSIDE, &uid);
if (r < 0)
return r;
gid_t gid;
r = uid_map_search_root(pid, GID_RANGE_USERNS_OUTSIDE, &gid);
r = uid_map_search_root(pidref.pid, GID_RANGE_USERNS_OUTSIDE, &gid);
if (r < 0)
return r;
+1 -1
View File
@@ -82,7 +82,7 @@ int parse_userns_uid_range(const char *s, uid_t *ret_uid_shift, uid_t *ret_uid_r
int userns_acquire_empty(void);
int userns_acquire(const char *uid_map, const char *gid_map, bool setgroups_deny);
int userns_acquire_self_root(void);
int userns_enter_and_pin(int userns_fd, pid_t *ret_pid);
int userns_enter_and_pin(int userns_fd, PidRef *ret);
bool userns_supported(void);
int userns_get_base_uid(int userns_fd, uid_t *ret_uid, gid_t *ret_gid);
+9
View File
@@ -101,6 +101,15 @@ static inline int pidref_wait_for_terminate(PidRef *pidref, siginfo_t *ret) {
return pidref_wait_for_terminate_full(pidref, USEC_INFINITY, ret);
}
static inline void pidref_done_sigterm_wait(PidRef *pidref) {
if (!pidref_is_set(pidref))
return;
(void) pidref_kill(pidref, SIGTERM);
(void) pidref_wait_for_terminate(pidref, NULL);
pidref_done(pidref);
}
static inline void pidref_done_sigkill_wait(PidRef *pidref) {
if (!pidref_is_set(pidref))
return;
+4 -52
View File
@@ -845,13 +845,9 @@ int get_process_umask(pid_t pid, mode_t *ret) {
return parse_mode(m, ret);
}
int wait_for_terminate(pid_t pid, siginfo_t *ret) {
return pidref_wait_for_terminate(&PIDREF_MAKE_FROM_PID(pid), ret);
}
/*
* Return values:
* < 0 : wait_for_terminate() failed to get the state of the
* < 0 : pidref_wait_for_terminate() failed to get the state of the
* process, the process was terminated by a signal, or
* failed for an unknown reason.
* >=0 : The process terminated normally, and its exit code is
@@ -908,52 +904,6 @@ int pidref_wait_for_terminate_and_check(const char *name, PidRef *pidref, WaitFl
return -EPROTO;
}
int wait_for_terminate_and_check(const char *name, pid_t pid, WaitFlags flags) {
return pidref_wait_for_terminate_and_check(name, &PIDREF_MAKE_FROM_PID(pid), flags);
}
void sigkill_wait(pid_t pid) {
assert(pid > 1);
(void) kill(pid, SIGKILL);
(void) wait_for_terminate(pid, NULL);
}
void sigkill_waitp(pid_t *pid) {
PROTECT_ERRNO;
if (!pid)
return;
if (*pid <= 1)
return;
sigkill_wait(*pid);
}
void sigterm_wait(pid_t pid) {
assert(pid > 1);
(void) kill_and_sigcont(pid, SIGTERM);
(void) wait_for_terminate(pid, NULL);
}
void sigkill_nowait(pid_t pid) {
assert(pid > 1);
(void) kill(pid, SIGKILL);
}
void sigkill_nowaitp(pid_t *pid) {
PROTECT_ERRNO;
if (!pid)
return;
if (*pid <= 1)
return;
sigkill_nowait(*pid);
}
int kill_and_sigcont(pid_t pid, int sig) {
int r;
@@ -1280,7 +1230,9 @@ void valgrind_summary_hack(void) {
exit(EXIT_SUCCESS);
else {
log_info("Spawned valgrind helper as PID "PID_FMT".", pid);
(void) wait_for_terminate(pid, NULL);
_cleanup_(pidref_done) PidRef pidref = PIDREF_MAKE_FROM_PID(pid);
(void) pidref_set_pid(&pidref, pid);
(void) pidref_wait_for_terminate(&pidref, NULL);
}
}
#endif
-9
View File
@@ -60,8 +60,6 @@ static inline bool SIGINFO_CODE_IS_DEAD(int code) {
return IN_SET(code, CLD_EXITED, CLD_KILLED, CLD_DUMPED);
}
int wait_for_terminate(pid_t pid, siginfo_t *ret);
typedef enum WaitFlags {
WAIT_LOG_ABNORMAL = 1 << 0,
WAIT_LOG_NON_ZERO_EXIT_STATUS = 1 << 1,
@@ -71,13 +69,6 @@ typedef enum WaitFlags {
} WaitFlags;
int pidref_wait_for_terminate_and_check(const char *name, PidRef *pidref, WaitFlags flags);
int wait_for_terminate_and_check(const char *name, pid_t pid, WaitFlags flags);
void sigkill_wait(pid_t pid);
void sigkill_waitp(pid_t *pid);
void sigterm_wait(pid_t pid);
void sigkill_nowait(pid_t pid);
void sigkill_nowaitp(pid_t *pid);
int kill_and_sigcont(pid_t pid, int sig);
+4 -3
View File
@@ -8,6 +8,7 @@
#include "format-util.h"
#include "namespace-util.h"
#include "path-util.h"
#include "pidref.h"
#include "process-util.h"
#include "sort-util.h"
#include "stat-util.h"
@@ -286,7 +287,7 @@ int uid_range_load_userns(const char *path, UIDRangeUsernsMode mode, UIDRange **
}
int uid_range_load_userns_by_fd(int userns_fd, UIDRangeUsernsMode mode, UIDRange **ret) {
_cleanup_(sigkill_waitp) pid_t pid = 0;
_cleanup_(pidref_done_sigkill_wait) PidRef pidref = PIDREF_NULL;
int r;
assert(userns_fd >= 0);
@@ -294,12 +295,12 @@ int uid_range_load_userns_by_fd(int userns_fd, UIDRangeUsernsMode mode, UIDRange
assert(mode < _UID_RANGE_USERNS_MODE_MAX);
assert(ret);
r = userns_enter_and_pin(userns_fd, &pid);
r = userns_enter_and_pin(userns_fd, &pidref);
if (r < 0)
return r;
const char *p = procfs_file_alloca(
pid,
pidref.pid,
IN_SET(mode, UID_RANGE_USERNS_INSIDE, UID_RANGE_USERNS_OUTSIDE) ? "uid_map" : "gid_map");
return uid_range_load_userns(p, mode, ret);