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:
@@ -1,19 +1,17 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <signal.h>
|
||||
|
||||
#include "ask-password-agent.h"
|
||||
#include "bus-util.h"
|
||||
#include "exec-util.h"
|
||||
#include "log.h"
|
||||
#include "process-util.h"
|
||||
#include "pidref.h"
|
||||
|
||||
static pid_t agent_pid = 0;
|
||||
static PidRef agent_pidref = PIDREF_NULL;
|
||||
|
||||
int ask_password_agent_open(void) {
|
||||
int r;
|
||||
|
||||
if (agent_pid > 0)
|
||||
if (pidref_is_set(&agent_pidref))
|
||||
return 0;
|
||||
|
||||
r = shall_fork_agent();
|
||||
@@ -22,7 +20,7 @@ int ask_password_agent_open(void) {
|
||||
|
||||
r = fork_agent("(sd-askpwagent)",
|
||||
NULL, 0,
|
||||
&agent_pid,
|
||||
&agent_pidref,
|
||||
SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH,
|
||||
"--watch");
|
||||
if (r < 0)
|
||||
@@ -32,12 +30,8 @@ int ask_password_agent_open(void) {
|
||||
}
|
||||
|
||||
void ask_password_agent_close(void) {
|
||||
|
||||
if (agent_pid <= 0)
|
||||
return;
|
||||
|
||||
/* Inform agent that we are done */
|
||||
sigterm_wait(TAKE_PID(agent_pid));
|
||||
pidref_done_sigterm_wait(&agent_pidref);
|
||||
}
|
||||
|
||||
int ask_password_agent_open_if_enabled(BusTransport transport, bool ask_password) {
|
||||
|
||||
+10
-11
@@ -59,11 +59,11 @@
|
||||
#include "openssl-util.h"
|
||||
#include "os-util.h"
|
||||
#include "path-util.h"
|
||||
#include "pidref.h"
|
||||
#include "proc-cmdline.h"
|
||||
#include "process-util.h"
|
||||
#include "resize-fs.h"
|
||||
#include "runtime-scope.h"
|
||||
#include "signal-util.h"
|
||||
#include "siphash24.h"
|
||||
#include "stat-util.h"
|
||||
#include "string-util.h"
|
||||
@@ -2237,7 +2237,6 @@ static int is_loop_device(const char *path) {
|
||||
|
||||
static int run_fsck(int node_fd, const char *fstype) {
|
||||
int r, exit_status;
|
||||
pid_t pid;
|
||||
|
||||
assert(node_fd >= 0);
|
||||
assert(fstype);
|
||||
@@ -2252,12 +2251,13 @@ static int run_fsck(int node_fd, const char *fstype) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
r = safe_fork_full(
|
||||
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
|
||||
r = pidref_safe_fork_full(
|
||||
"(fsck)",
|
||||
NULL,
|
||||
&node_fd, 1, /* Leave the node fd open */
|
||||
FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_DEATHSIG_SIGTERM|FORK_REARRANGE_STDIO|FORK_CLOEXEC_OFF,
|
||||
&pid);
|
||||
&pidref);
|
||||
if (r < 0)
|
||||
return log_debug_errno(r, "Failed to fork off fsck: %m");
|
||||
if (r == 0) {
|
||||
@@ -2268,7 +2268,7 @@ static int run_fsck(int node_fd, const char *fstype) {
|
||||
_exit(FSCK_OPERATIONAL_ERROR);
|
||||
}
|
||||
|
||||
exit_status = wait_for_terminate_and_check("fsck", pid, 0);
|
||||
exit_status = pidref_wait_for_terminate_and_check("fsck", &pidref, 0);
|
||||
if (exit_status < 0)
|
||||
return log_debug_errno(exit_status, "Failed to fork off fsck: %m");
|
||||
|
||||
@@ -4170,15 +4170,13 @@ int dissected_image_acquire_metadata(
|
||||
_cleanup_strv_free_ char **machine_info = NULL, **os_release = NULL, **initrd_release = NULL, **sysext_release = NULL, **confext_release = NULL;
|
||||
_cleanup_free_ char *hostname = NULL, *t = NULL;
|
||||
_cleanup_close_pair_ int error_pipe[2] = EBADF_PAIR;
|
||||
_cleanup_(sigkill_waitp) pid_t child = 0;
|
||||
_cleanup_(pidref_done_sigkill_wait) PidRef child = PIDREF_NULL;
|
||||
sd_id128_t machine_id = SD_ID128_NULL;
|
||||
unsigned n_meta_initialized = 0;
|
||||
int fds[2 * _META_MAX], r, v;
|
||||
int has_init_system = -1;
|
||||
ssize_t n;
|
||||
|
||||
BLOCK_SIGNALS(SIGCHLD);
|
||||
|
||||
assert(m);
|
||||
|
||||
r = dlopen_libmount();
|
||||
@@ -4203,7 +4201,7 @@ int dissected_image_acquire_metadata(
|
||||
goto finish;
|
||||
}
|
||||
|
||||
r = safe_fork("(sd-dissect)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM, &child);
|
||||
r = pidref_safe_fork("(sd-dissect)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM, &child);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
if (r == 0) {
|
||||
@@ -4426,11 +4424,12 @@ int dissected_image_acquire_metadata(
|
||||
}}
|
||||
}
|
||||
|
||||
r = wait_for_terminate_and_check("(sd-dissect)", child, 0);
|
||||
child = 0;
|
||||
r = pidref_wait_for_terminate_and_check("(sd-dissect)", &child, 0);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
|
||||
TAKE_PIDREF(child);
|
||||
|
||||
n = read(error_pipe[0], &v, sizeof(v));
|
||||
if (n < 0) {
|
||||
r = -errno;
|
||||
|
||||
+35
-25
@@ -17,6 +17,7 @@
|
||||
#include "hashmap.h"
|
||||
#include "log.h"
|
||||
#include "path-util.h"
|
||||
#include "pidref.h"
|
||||
#include "process-util.h"
|
||||
#include "serialize.h"
|
||||
#include "stat-util.h"
|
||||
@@ -28,6 +29,10 @@
|
||||
|
||||
#define EXIT_SKIP_REMAINING 77
|
||||
|
||||
DEFINE_PRIVATE_HASH_OPS_FULL(pidref_hash_ops_free_free,
|
||||
PidRef, pidref_hash_func, pidref_compare_func,
|
||||
pidref_free, char*, free);
|
||||
|
||||
/* Put this test here for a lack of better place */
|
||||
assert_cc(EAGAIN == EWOULDBLOCK);
|
||||
|
||||
@@ -36,25 +41,25 @@ static int do_spawn(
|
||||
char *argv[],
|
||||
int stdout_fd,
|
||||
bool set_systemd_exec_pid,
|
||||
pid_t *ret_pid) {
|
||||
PidRef *ret) {
|
||||
|
||||
int r;
|
||||
|
||||
assert(path);
|
||||
assert(ret_pid);
|
||||
assert(ret);
|
||||
|
||||
if (null_or_empty_path(path) > 0) {
|
||||
log_debug("%s is masked, skipping.", path);
|
||||
return 0;
|
||||
}
|
||||
|
||||
pid_t pid;
|
||||
r = safe_fork_full(
|
||||
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
|
||||
r = pidref_safe_fork_full(
|
||||
"(exec-inner)",
|
||||
(const int[]) { STDIN_FILENO, stdout_fd < 0 ? STDOUT_FILENO : stdout_fd, STDERR_FILENO },
|
||||
/* except_fds= */ NULL, /* n_except_fds= */ 0,
|
||||
FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_RLIMIT_NOFILE_SAFE|FORK_REARRANGE_STDIO|FORK_CLOSE_ALL_FDS,
|
||||
&pid);
|
||||
&pidref);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r == 0) {
|
||||
@@ -78,7 +83,7 @@ static int do_spawn(
|
||||
_exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
*ret_pid = pid;
|
||||
*ret = TAKE_PIDREF(pidref);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -121,7 +126,6 @@ static int do_execute(
|
||||
STRV_FOREACH(path, paths) {
|
||||
_cleanup_free_ char *t = NULL;
|
||||
_cleanup_close_ int fd = -EBADF;
|
||||
pid_t pid;
|
||||
|
||||
t = path_join(root, *path);
|
||||
if (!t)
|
||||
@@ -161,19 +165,27 @@ static int do_execute(
|
||||
"permission bits. Proceeding anyway.", t);
|
||||
}
|
||||
|
||||
r = do_spawn(t, argv, fd, FLAGS_SET(flags, EXEC_DIR_SET_SYSTEMD_EXEC_PID), &pid);
|
||||
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
|
||||
r = do_spawn(t, argv, fd, FLAGS_SET(flags, EXEC_DIR_SET_SYSTEMD_EXEC_PID), &pidref);
|
||||
if (r <= 0)
|
||||
continue;
|
||||
|
||||
if (parallel_execution) {
|
||||
r = hashmap_ensure_put(&pids, &trivial_hash_ops_value_free, PID_TO_PTR(pid), t);
|
||||
_cleanup_(pidref_freep) PidRef *dup = NULL;
|
||||
r = pidref_dup(&pidref, &dup);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = hashmap_ensure_put(&pids, &pidref_hash_ops_free_free, dup, t);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
t = NULL;
|
||||
|
||||
TAKE_PTR(dup);
|
||||
TAKE_PTR(t);
|
||||
} else {
|
||||
bool skip_remaining = false;
|
||||
|
||||
r = wait_for_terminate_and_check(t, pid, WAIT_LOG_ABNORMAL);
|
||||
r = pidref_wait_for_terminate_and_check(t, &pidref, WAIT_LOG_ABNORMAL);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r > 0) {
|
||||
@@ -211,14 +223,12 @@ static int do_execute(
|
||||
|
||||
while (!hashmap_isempty(pids)) {
|
||||
_cleanup_free_ char *t = NULL;
|
||||
pid_t pid;
|
||||
void *p;
|
||||
|
||||
t = ASSERT_PTR(hashmap_steal_first_key_and_value(pids, &p));
|
||||
pid = PTR_TO_PID(p);
|
||||
assert(pid > 0);
|
||||
_cleanup_(pidref_freep) PidRef *pidref = p;
|
||||
|
||||
r = wait_for_terminate_and_check(t, pid, WAIT_LOG);
|
||||
r = pidref_wait_for_terminate_and_check(t, pidref, WAIT_LOG);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (!FLAGS_SET(flags, EXEC_DIR_IGNORE_ERRORS) && r > 0)
|
||||
@@ -240,7 +250,6 @@ int execute_strv(
|
||||
ExecDirFlags flags) {
|
||||
|
||||
_cleanup_close_ int fd = -EBADF;
|
||||
pid_t executor_pid;
|
||||
int r;
|
||||
|
||||
assert(name);
|
||||
@@ -266,7 +275,8 @@ int execute_strv(
|
||||
|
||||
const char *process_name = strjoina("(", name, ")");
|
||||
|
||||
r = safe_fork(process_name, FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|FORK_LOG, &executor_pid);
|
||||
PidRef executor_pidref = PIDREF_NULL;
|
||||
r = pidref_safe_fork(process_name, FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|FORK_LOG, &executor_pidref);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r == 0) {
|
||||
@@ -274,7 +284,7 @@ int execute_strv(
|
||||
_exit(r < 0 ? EXIT_FAILURE : r);
|
||||
}
|
||||
|
||||
r = wait_for_terminate_and_check(process_name, executor_pid, 0);
|
||||
r = pidref_wait_for_terminate_and_check(process_name, &executor_pidref, 0);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (!FLAGS_SET(flags, EXEC_DIR_IGNORE_ERRORS) && r > 0)
|
||||
@@ -560,19 +570,19 @@ int shall_fork_agent(void) {
|
||||
return true;
|
||||
}
|
||||
|
||||
int _fork_agent(const char *name, char * const *argv, const int except[], size_t n_except, pid_t *ret_pid) {
|
||||
int _fork_agent(const char *name, char * const *argv, const int except[], size_t n_except, PidRef *ret) {
|
||||
int r;
|
||||
|
||||
assert(!strv_isempty(argv));
|
||||
|
||||
/* Spawns a temporary TTY agent, making sure it goes away when we go away */
|
||||
|
||||
r = safe_fork_full(name,
|
||||
NULL,
|
||||
(int*) except, /* safe_fork_full only changes except if you pass in FORK_PACK_FDS, which we don't */
|
||||
n_except,
|
||||
FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|FORK_CLOSE_ALL_FDS|FORK_REOPEN_LOG|FORK_RLIMIT_NOFILE_SAFE,
|
||||
ret_pid);
|
||||
r = pidref_safe_fork_full(
|
||||
name,
|
||||
/* stdio_fds= */ NULL,
|
||||
(int*) except, n_except, /* safe_fork_full only changes except if you pass in FORK_PACK_FDS, which we don't */
|
||||
FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|FORK_CLOSE_ALL_FDS|FORK_REOPEN_LOG|FORK_RLIMIT_NOFILE_SAFE,
|
||||
ret);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r > 0)
|
||||
|
||||
@@ -62,5 +62,5 @@ ExecCommandFlags exec_command_flags_from_string(const char *s);
|
||||
int fexecve_or_execve(int executable_fd, const char *executable, char *const argv[], char *const envp[]);
|
||||
|
||||
int shall_fork_agent(void);
|
||||
int _fork_agent(const char *name, char * const *argv, const int except[], size_t n_except, pid_t *ret_pid);
|
||||
#define fork_agent(name, except, n_except, ret_pid, ...) _fork_agent(name, STRV_MAKE(__VA_ARGS__), except, n_except, ret_pid)
|
||||
int _fork_agent(const char *name, char * const *argv, const int except[], size_t n_except, PidRef *ret);
|
||||
#define fork_agent(name, except, n_except, ret, ...) _fork_agent(name, STRV_MAKE(__VA_ARGS__), except, n_except, ret)
|
||||
|
||||
+11
-5
@@ -14,6 +14,7 @@
|
||||
#include "locale-util.h"
|
||||
#include "log.h"
|
||||
#include "pager.h"
|
||||
#include "pidref.h"
|
||||
#include "process-util.h"
|
||||
#include "signal-util.h"
|
||||
#include "string-util.h"
|
||||
@@ -288,8 +289,10 @@ void pager_close(void) {
|
||||
stdout_redirected = stderr_redirected = false;
|
||||
|
||||
(void) kill(pager_pid, SIGCONT);
|
||||
(void) wait_for_terminate(TAKE_PID(pager_pid), NULL);
|
||||
pager_pid = 0;
|
||||
_cleanup_(pidref_done) PidRef pidref = PIDREF_MAKE_FROM_PID(pager_pid);
|
||||
(void) pidref_set_pid(&pidref, pager_pid);
|
||||
(void) pidref_wait_for_terminate(&pidref, NULL);
|
||||
TAKE_PID(pager_pid);
|
||||
}
|
||||
|
||||
bool pager_have(void) {
|
||||
@@ -299,7 +302,6 @@ bool pager_have(void) {
|
||||
int show_man_page(const char *desc, bool null_stdio) {
|
||||
const char *args[4] = { "man", NULL, NULL, NULL };
|
||||
const char *e = NULL;
|
||||
pid_t pid;
|
||||
size_t k;
|
||||
int r;
|
||||
|
||||
@@ -319,7 +321,11 @@ int show_man_page(const char *desc, bool null_stdio) {
|
||||
} else
|
||||
args[1] = desc;
|
||||
|
||||
r = safe_fork("(man)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|(null_stdio ? FORK_REARRANGE_STDIO : 0)|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid);
|
||||
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
|
||||
r = pidref_safe_fork(
|
||||
"(man)",
|
||||
FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|(null_stdio ? FORK_REARRANGE_STDIO : 0)|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG|FORK_WAIT,
|
||||
&pidref);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r == 0) {
|
||||
@@ -329,5 +335,5 @@ int show_man_page(const char *desc, bool null_stdio) {
|
||||
_exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return wait_for_terminate_and_check(NULL, pid, 0);
|
||||
return pidref_wait_for_terminate_and_check(NULL, &pidref, 0);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <poll.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "bus-util.h"
|
||||
@@ -9,19 +8,19 @@
|
||||
#include "fd-util.h"
|
||||
#include "io-util.h"
|
||||
#include "log.h"
|
||||
#include "pidref.h"
|
||||
#include "polkit-agent.h"
|
||||
#include "process-util.h"
|
||||
#include "stdio-util.h"
|
||||
|
||||
#if ENABLE_POLKIT
|
||||
static pid_t agent_pid = 0;
|
||||
static PidRef agent_pidref = PIDREF_NULL;
|
||||
|
||||
int polkit_agent_open(void) {
|
||||
_cleanup_close_pair_ int pipe_fd[2] = EBADF_PAIR;
|
||||
char notify_fd[DECIMAL_STR_MAX(int) + 1];
|
||||
int r;
|
||||
|
||||
if (agent_pid > 0)
|
||||
if (pidref_is_set(&agent_pidref))
|
||||
return 0;
|
||||
|
||||
/* Clients that run as root don't need to activate/query polkit */
|
||||
@@ -40,7 +39,7 @@ int polkit_agent_open(void) {
|
||||
r = fork_agent("(polkit-agent)",
|
||||
&pipe_fd[1],
|
||||
1,
|
||||
&agent_pid,
|
||||
&agent_pidref,
|
||||
POLKIT_AGENT_BINARY_PATH,
|
||||
"--notify-fd", notify_fd,
|
||||
"--fallback");
|
||||
@@ -57,12 +56,8 @@ int polkit_agent_open(void) {
|
||||
}
|
||||
|
||||
void polkit_agent_close(void) {
|
||||
|
||||
if (agent_pid <= 0)
|
||||
return;
|
||||
|
||||
/* Inform agent that we are done */
|
||||
sigterm_wait(TAKE_PID(agent_pid));
|
||||
pidref_done_sigterm_wait(&agent_pidref);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
+6
-1
@@ -448,7 +448,12 @@ int assert_signal_internal(int *ret_signal) {
|
||||
return ASSERT_SIGNAL_FORK_CHILD;
|
||||
}
|
||||
|
||||
r = wait_for_terminate(r, &siginfo);
|
||||
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
|
||||
r = pidref_set_pid(&pidref, r);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = pidref_wait_for_terminate(&pidref, &siginfo);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user