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
+2 -1
View File
@@ -8,6 +8,7 @@
#include "bus-match.h"
#include "constants.h"
#include "list.h"
#include "pidref.h"
#include "runtime-scope.h"
#include "socket-util.h"
@@ -271,7 +272,7 @@ typedef struct sd_bus {
unsigned n_memfd_cache;
uint64_t origin_id;
pid_t busexec_pid;
PidRef busexec_pidref;
unsigned iteration_counter;
+7 -5
View File
@@ -1163,7 +1163,7 @@ int bus_socket_exec(sd_bus *b) {
assert(b->input_fd < 0);
assert(b->output_fd < 0);
assert(b->exec_path);
assert(b->busexec_pid == 0);
assert(!pidref_is_set(&b->busexec_pidref));
if (DEBUG_LOGGING) {
_cleanup_free_ char *line = NULL;
@@ -1181,10 +1181,12 @@ int bus_socket_exec(sd_bus *b) {
if (r < 0)
return -errno;
r = safe_fork_full("(sd-busexec)",
(int[]) { s[1], s[1], STDERR_FILENO },
NULL, 0,
FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_REARRANGE_STDIO|FORK_RLIMIT_NOFILE_SAFE, &b->busexec_pid);
r = pidref_safe_fork_full(
"(sd-busexec)",
(int[]) { s[1], s[1], STDERR_FILENO },
NULL, 0,
FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_REARRANGE_STDIO|FORK_RLIMIT_NOFILE_SAFE,
&b->busexec_pidref);
if (r < 0) {
safe_close_pair(s);
return r;
+4 -11
View File
@@ -41,7 +41,6 @@
#include "parse-util.h"
#include "path-util.h"
#include "prioq.h"
#include "process-util.h"
#include "set.h"
#include "string-util.h"
#include "strv.h"
@@ -262,6 +261,7 @@ _public_ int sd_bus_new(sd_bus **ret) {
.creds_mask = SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_UNIQUE_NAME,
.accept_fd = true,
.origin_id = origin_id_query(),
.busexec_pidref = PIDREF_NULL,
.n_groups = SIZE_MAX,
.close_on_exit = true,
.ucred = UCRED_INVALID,
@@ -1121,13 +1121,6 @@ static int bus_parse_next_address(sd_bus *b) {
return 1;
}
static void bus_kill_exec(sd_bus *bus) {
if (!pid_is_valid(bus->busexec_pid))
return;
sigterm_wait(TAKE_PID(bus->busexec_pid));
}
static int bus_start_address(sd_bus *b) {
int r;
@@ -1136,7 +1129,7 @@ static int bus_start_address(sd_bus *b) {
for (;;) {
bus_close_fds(b);
bus_kill_exec(b);
pidref_done_sigterm_wait(&b->busexec_pidref);
/* If you provide multiple different bus-addresses, we
* try all of them in order and use the first one that
@@ -1778,7 +1771,7 @@ _public_ void sd_bus_close(sd_bus *bus) {
return;
/* Don't leave ssh hanging around */
bus_kill_exec(bus);
pidref_done_sigterm_wait(&bus->busexec_pidref);
bus_set_state(bus, BUS_CLOSED);
@@ -1809,7 +1802,7 @@ _public_ sd_bus* sd_bus_flush_close_unref(sd_bus *bus) {
return NULL;
/* Have to do this before flush() to prevent hang */
bus_kill_exec(bus);
pidref_done_sigterm_wait(&bus->busexec_pidref);
sd_bus_flush(bus);
return sd_bus_close_unref(bus);
+17 -17
View File
@@ -26,6 +26,7 @@
#include "mkdir.h"
#include "path-util.h"
#include "pidfd-util.h"
#include "pidref.h"
#include "process-util.h"
#include "socket-util.h"
#include "string-table.h"
@@ -149,6 +150,8 @@ static int varlink_new(sd_varlink **ret) {
.allow_fd_passing_input = -1,
.af = -1,
.exec_pidref = PIDREF_NULL,
};
*ret = v;
@@ -209,7 +212,7 @@ _public_ int sd_varlink_connect_address(sd_varlink **ret, const char *address) {
_public_ int sd_varlink_connect_exec(sd_varlink **ret, const char *_command, char **_argv) {
_cleanup_close_pair_ int pair[2] = EBADF_PAIR;
_cleanup_(sigkill_waitp) pid_t pid = 0;
_cleanup_(pidref_done_sigkill_wait) PidRef pidref = PIDREF_NULL;
_cleanup_free_ char *command = NULL;
_cleanup_strv_free_ char **argv = NULL;
int r;
@@ -239,13 +242,13 @@ _public_ int sd_varlink_connect_exec(sd_varlink **ret, const char *_command, cha
if (r < 0)
return log_debug_errno(r, "Failed to disable O_NONBLOCK for varlink socket: %m");
r = safe_fork_full(
r = pidref_safe_fork_full(
"(sd-vlexec)",
/* stdio_fds= */ NULL,
/* except_fds= */ (int[]) { pair[1] },
/* n_except_fds= */ 1,
FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_REOPEN_LOG|FORK_LOG|FORK_RLIMIT_NOFILE_SAFE,
&pid);
&pidref);
if (r < 0)
return log_debug_errno(r, "Failed to spawn process: %m");
if (r == 0) {
@@ -266,7 +269,7 @@ _public_ int sd_varlink_connect_exec(sd_varlink **ret, const char *_command, cha
_exit(EXIT_FAILURE);
}
xsprintf(spid, PID_FMT, pid);
xsprintf(spid, PID_FMT, pidref.pid);
uint64_t pidfdid;
if (pidfd_get_inode_id_self_cached(&pidfdid) >= 0) {
@@ -298,7 +301,7 @@ _public_ int sd_varlink_connect_exec(sd_varlink **ret, const char *_command, cha
v->output_fd = v->input_fd = TAKE_FD(pair[0]);
v->af = AF_UNIX;
v->exec_pid = TAKE_PID(pid);
v->exec_pidref = TAKE_PIDREF(pidref);
varlink_set_state(v, VARLINK_IDLE_CLIENT);
*ret = v;
@@ -318,7 +321,7 @@ static int ssh_path(const char **ret) {
static int varlink_connect_ssh_unix(sd_varlink **ret, const char *where) {
_cleanup_close_pair_ int pair[2] = EBADF_PAIR;
_cleanup_(sigkill_waitp) pid_t pid = 0;
_cleanup_(pidref_done_sigkill_wait) PidRef pidref = PIDREF_NULL;
int r;
assert_return(ret, -EINVAL);
@@ -356,13 +359,13 @@ static int varlink_connect_ssh_unix(sd_varlink **ret, const char *where) {
if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0, pair) < 0)
return log_debug_errno(errno, "Failed to allocate AF_UNIX socket pair: %m");
r = safe_fork_full(
r = pidref_safe_fork_full(
"(sd-vlssh)",
/* stdio_fds= */ (int[]) { pair[1], pair[1], STDERR_FILENO },
/* except_fds= */ NULL,
/* n_except_fds= */ 0,
FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_REOPEN_LOG|FORK_LOG|FORK_RLIMIT_NOFILE_SAFE|FORK_REARRANGE_STDIO,
&pid);
&pidref);
if (r < 0)
return log_debug_errno(r, "Failed to spawn process: %m");
if (r == 0) {
@@ -382,7 +385,7 @@ static int varlink_connect_ssh_unix(sd_varlink **ret, const char *where) {
v->output_fd = v->input_fd = TAKE_FD(pair[0]);
v->af = AF_UNIX;
v->exec_pid = TAKE_PID(pid);
v->exec_pidref = TAKE_PIDREF(pidref);
varlink_set_state(v, VARLINK_IDLE_CLIENT);
*ret = v;
@@ -391,7 +394,7 @@ static int varlink_connect_ssh_unix(sd_varlink **ret, const char *where) {
static int varlink_connect_ssh_exec(sd_varlink **ret, const char *where) {
_cleanup_close_pair_ int input_pipe[2] = EBADF_PAIR, output_pipe[2] = EBADF_PAIR;
_cleanup_(sigkill_waitp) pid_t pid = 0;
_cleanup_(pidref_done_sigkill_wait) PidRef pidref = PIDREF_NULL;
int r;
assert_return(ret, -EINVAL);
@@ -440,13 +443,13 @@ static int varlink_connect_ssh_exec(sd_varlink **ret, const char *where) {
if (pipe2(output_pipe, O_CLOEXEC) < 0)
return log_debug_errno(errno, "Failed to allocate output pipe: %m");
r = safe_fork_full(
r = pidref_safe_fork_full(
"(sd-vlssh)",
/* stdio_fds= */ (int[]) { input_pipe[0], output_pipe[1], STDERR_FILENO },
/* except_fds= */ NULL,
/* n_except_fds= */ 0,
FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_REOPEN_LOG|FORK_LOG|FORK_RLIMIT_NOFILE_SAFE|FORK_REARRANGE_STDIO,
&pid);
&pidref);
if (r < 0)
return log_debug_errno(r, "Failed to spawn process: %m");
if (r == 0) {
@@ -475,7 +478,7 @@ static int varlink_connect_ssh_exec(sd_varlink **ret, const char *where) {
v->input_fd = TAKE_FD(output_pipe[0]);
v->output_fd = TAKE_FD(input_pipe[1]);
v->af = AF_UNSPEC;
v->exec_pid = TAKE_PID(pid);
v->exec_pidref = TAKE_PIDREF(pidref);
varlink_set_state(v, VARLINK_IDLE_CLIENT);
*ret = v;
@@ -648,10 +651,7 @@ static void varlink_clear(sd_varlink *v) {
v->event = sd_event_unref(v->event);
if (v->exec_pid > 0) {
sigterm_wait(v->exec_pid);
v->exec_pid = 0;
}
pidref_done_sigterm_wait(&v->exec_pidref);
v->peer_pidfd = safe_close(v->peer_pidfd);
}
+2 -1
View File
@@ -6,6 +6,7 @@
#include "sd-varlink.h"
#include "list.h"
#include "pidref.h"
#include "sd-forward.h"
typedef enum VarlinkState {
@@ -187,7 +188,7 @@ struct sd_varlink {
sd_event_source *quit_event_source;
sd_event_source *defer_event_source;
pid_t exec_pid;
PidRef exec_pidref;
};
typedef struct VarlinkServerSocket VarlinkServerSocket;