summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2017-12-29 18:07:00 +0100
committerLennart Poettering <lennart@poettering.net>2018-01-04 13:27:27 +0100
commit2e87a1fde9f46bee69c0980076cf6dc1f7cec7a4 (patch)
treea29438129d695c76f8326b505210963c11c5cd5e
parentprocess-spec: add another flag FORK_WAIT to safe_fork() (diff)
downloadsystemd-2e87a1fde9f46bee69c0980076cf6dc1f7cec7a4.tar.xz
systemd-2e87a1fde9f46bee69c0980076cf6dc1f7cec7a4.zip
tree-wide: make use of wait_for_terminate_and_check() at various places
Using wait_for_terminate_and_check() instead of wait_for_terminate() let's us simplify, shorten and unify the return value checking and logging of waitid(). Hence, let's use it all over the place.
-rw-r--r--src/basic/terminal-util.c10
-rw-r--r--src/core/execute.c9
-rw-r--r--src/coredump/coredumpctl.c9
-rw-r--r--src/fsck/fsck.c33
-rw-r--r--src/libsystemd/sd-bus/bus-container.c20
-rw-r--r--src/machine/machine-dbus.c27
-rw-r--r--src/shared/dissect-image.c12
-rw-r--r--src/shared/logs-show.c9
-rw-r--r--src/shared/machine-pool.c18
-rw-r--r--src/shared/pager.c8
-rw-r--r--src/systemctl/systemctl.c33
11 files changed, 64 insertions, 124 deletions
diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c
index d15fa38207..42336e8fdf 100644
--- a/src/basic/terminal-util.c
+++ b/src/basic/terminal-util.c
@@ -1093,7 +1093,6 @@ int ptsname_namespace(int pty, char **ret) {
int openpt_in_namespace(pid_t pid, int flags) {
_cleanup_close_ int pidnsfd = -1, mntnsfd = -1, usernsfd = -1, rootfd = -1;
_cleanup_close_pair_ int pair[2] = { -1, -1 };
- siginfo_t si;
pid_t child;
int r;
@@ -1133,10 +1132,10 @@ int openpt_in_namespace(pid_t pid, int flags) {
pair[1] = safe_close(pair[1]);
- r = wait_for_terminate(child, &si);
+ r = wait_for_terminate_and_check("(sd-openpt)", child, 0);
if (r < 0)
return r;
- if (si.si_code != CLD_EXITED || si.si_status != EXIT_SUCCESS)
+ if (r != EXIT_SUCCESS)
return -EIO;
return receive_one_fd(pair[0], 0);
@@ -1145,7 +1144,6 @@ int openpt_in_namespace(pid_t pid, int flags) {
int open_terminal_in_namespace(pid_t pid, const char *name, int mode) {
_cleanup_close_ int pidnsfd = -1, mntnsfd = -1, usernsfd = -1, rootfd = -1;
_cleanup_close_pair_ int pair[2] = { -1, -1 };
- siginfo_t si;
pid_t child;
int r;
@@ -1180,10 +1178,10 @@ int open_terminal_in_namespace(pid_t pid, const char *name, int mode) {
pair[1] = safe_close(pair[1]);
- r = wait_for_terminate(child, &si);
+ r = wait_for_terminate_and_check("(sd-terminal)", child, 0);
if (r < 0)
return r;
- if (si.si_code != CLD_EXITED || si.si_status != EXIT_SUCCESS)
+ if (r != EXIT_SUCCESS)
return -EIO;
return receive_one_fd(pair[0], 0);
diff --git a/src/core/execute.c b/src/core/execute.c
index 3f3d73272e..6b01d00d6e 100644
--- a/src/core/execute.c
+++ b/src/core/execute.c
@@ -1823,7 +1823,6 @@ static int setup_private_users(uid_t uid, gid_t gid) {
_cleanup_close_ int unshare_ready_fd = -1;
_cleanup_(sigkill_waitp) pid_t pid = 0;
uint64_t c = 1;
- siginfo_t si;
ssize_t n;
int r;
@@ -1963,13 +1962,11 @@ static int setup_private_users(uid_t uid, gid_t gid) {
if (n != 0) /* on success we should have read 0 bytes */
return -EIO;
- r = wait_for_terminate(pid, &si);
+ r = wait_for_terminate_and_check("(sd-userns)", pid, 0);
+ pid = 0;
if (r < 0)
return r;
- pid = 0;
-
- /* If something strange happened with the child, let's consider this fatal, too */
- if (si.si_code != CLD_EXITED || si.si_status != 0)
+ if (r != EXIT_SUCCESS) /* If something strange happened with the child, let's consider this fatal, too */
return -EIO;
return 0;
diff --git a/src/coredump/coredumpctl.c b/src/coredump/coredumpctl.c
index 4524d604b6..96e4a3e7e2 100644
--- a/src/coredump/coredumpctl.c
+++ b/src/coredump/coredumpctl.c
@@ -885,7 +885,6 @@ static int run_gdb(sd_journal *j) {
_cleanup_free_ char *exe = NULL, *path = NULL;
bool unlink_path = false;
const char *data;
- siginfo_t st;
size_t len;
pid_t pid;
int r;
@@ -937,13 +936,7 @@ static int run_gdb(sd_journal *j) {
_exit(EXIT_FAILURE);
}
- r = wait_for_terminate(pid, &st);
- if (r < 0) {
- log_error_errno(r, "Failed to wait for gdb: %m");
- goto finish;
- }
-
- r = st.si_code == CLD_EXITED ? st.si_status : 255;
+ r = wait_for_terminate_and_check("gdb", pid, WAIT_LOG_ABNORMAL);
finish:
(void) default_signals(SIGINT, -1);
diff --git a/src/fsck/fsck.c b/src/fsck/fsck.c
index d2b7cf6e3e..97d824aca4 100644
--- a/src/fsck/fsck.c
+++ b/src/fsck/fsck.c
@@ -284,9 +284,8 @@ int main(int argc, char *argv[]) {
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
const char *device, *type;
bool root_directory;
- siginfo_t status;
struct stat st;
- int r;
+ int r, exit_status;
pid_t pid;
if (argc > 2) {
@@ -449,38 +448,30 @@ int main(int argc, char *argv[]) {
(void) process_progress(progress_pipe[0]);
progress_pipe[0] = -1;
- r = wait_for_terminate(pid, &status);
- if (r < 0) {
- log_error_errno(r, "waitid(): %m");
+ exit_status = wait_for_terminate_and_check("fsck", pid, WAIT_LOG_ABNORMAL);
+ if (exit_status < 0) {
+ r = exit_status;
goto finish;
}
+ if (exit_status & ~1) {
+ log_error("fsck failed with exit status %i.", exit_status);
- if (status.si_code != CLD_EXITED || (status.si_status & ~1)) {
-
- if (IN_SET(status.si_code, CLD_KILLED, CLD_DUMPED))
- log_error("fsck terminated by signal %s.", signal_to_string(status.si_status));
- else if (status.si_code == CLD_EXITED)
- log_error("fsck failed with error code %i.", status.si_status);
- else
- log_error("fsck failed due to unknown reason.");
-
- r = -EINVAL;
-
- if (status.si_code == CLD_EXITED && (status.si_status & FSCK_SYSTEM_SHOULD_REBOOT) && root_directory)
+ if ((exit_status & FSCK_SYSTEM_SHOULD_REBOOT) && root_directory) {
/* System should be rebooted. */
start_target(SPECIAL_REBOOT_TARGET, "replace-irreversibly");
- else if (status.si_code == CLD_EXITED && (status.si_status & (FSCK_SYSTEM_SHOULD_REBOOT | FSCK_ERRORS_LEFT_UNCORRECTED)))
+ r = -EINVAL;
+ } else if (exit_status & (FSCK_SYSTEM_SHOULD_REBOOT | FSCK_ERRORS_LEFT_UNCORRECTED)) {
/* Some other problem */
start_target(SPECIAL_EMERGENCY_TARGET, "replace");
- else {
+ r = -EINVAL;
+ } else {
log_warning("Ignoring error.");
r = 0;
}
-
} else
r = 0;
- if (status.si_code == CLD_EXITED && (status.si_status & FSCK_ERROR_CORRECTED))
+ if (exit_status & FSCK_ERROR_CORRECTED)
(void) touch("/run/systemd/quotacheck");
finish:
diff --git a/src/libsystemd/sd-bus/bus-container.c b/src/libsystemd/sd-bus/bus-container.c
index a1242c5678..16156d8823 100644
--- a/src/libsystemd/sd-bus/bus-container.c
+++ b/src/libsystemd/sd-bus/bus-container.c
@@ -31,9 +31,8 @@
int bus_container_connect_socket(sd_bus *b) {
_cleanup_close_pair_ int pair[2] = { -1, -1 };
_cleanup_close_ int pidnsfd = -1, mntnsfd = -1, usernsfd = -1, rootfd = -1;
- pid_t child;
- siginfo_t si;
int r, error_buf = 0;
+ pid_t child;
ssize_t n;
assert(b);
@@ -97,21 +96,20 @@ int bus_container_connect_socket(sd_bus *b) {
_exit(EXIT_SUCCESS);
}
- r = wait_for_terminate(grandchild, &si);
+ r = wait_for_terminate_and_check("(sd-buscntr2)", grandchild, 0);
if (r < 0)
_exit(EXIT_FAILURE);
- if (si.si_code != CLD_EXITED)
- _exit(EXIT_FAILURE);
-
- _exit(si.si_status);
+ _exit(r);
}
pair[1] = safe_close(pair[1]);
- r = wait_for_terminate(child, &si);
+ r = wait_for_terminate_and_check("(sd-buscntr)", child, 0);
if (r < 0)
return r;
+ if (r != EXIT_SUCCESS)
+ return -EPROTO;
n = read(pair[0], &error_buf, sizeof(error_buf));
if (n < 0)
@@ -131,11 +129,5 @@ int bus_container_connect_socket(sd_bus *b) {
return -error_buf;
}
- if (si.si_code != CLD_EXITED)
- return -EIO;
-
- if (si.si_status != EXIT_SUCCESS)
- return -EIO;
-
return bus_socket_start_auth(b);
}
diff --git a/src/machine/machine-dbus.c b/src/machine/machine-dbus.c
index 68e0b7edcc..0ca2e54ed9 100644
--- a/src/machine/machine-dbus.c
+++ b/src/machine/machine-dbus.c
@@ -228,7 +228,6 @@ int bus_machine_method_get_addresses(sd_bus_message *message, void *userdata, sd
_cleanup_free_ char *us = NULL, *them = NULL;
_cleanup_close_ int netns_fd = -1;
const char *p;
- siginfo_t si;
pid_t child;
r = readlink_malloc("/proc/self/ns/net", &us);
@@ -337,10 +336,10 @@ int bus_machine_method_get_addresses(sd_bus_message *message, void *userdata, sd
return r;
}
- r = wait_for_terminate(child, &si);
+ r = wait_for_terminate_and_check("(sd-addr)", child, 0);
if (r < 0)
return sd_bus_error_set_errnof(error, r, "Failed to wait for child: %m");
- if (si.si_code != CLD_EXITED || si.si_status != EXIT_SUCCESS)
+ if (r != EXIT_SUCCESS)
return sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Child died abnormally.");
break;
}
@@ -379,7 +378,6 @@ int bus_machine_method_get_os_release(sd_bus_message *message, void *userdata, s
_cleanup_close_ int mntns_fd = -1, root_fd = -1;
_cleanup_close_pair_ int pair[2] = { -1, -1 };
_cleanup_fclose_ FILE *f = NULL;
- siginfo_t si;
pid_t child;
r = namespace_open(m->leader, NULL, &mntns_fd, NULL, NULL, &root_fd);
@@ -429,12 +427,12 @@ int bus_machine_method_get_os_release(sd_bus_message *message, void *userdata, s
if (r < 0)
return r;
- r = wait_for_terminate(child, &si);
+ r = wait_for_terminate_and_check("(sd-osrel)", child, 0);
if (r < 0)
return sd_bus_error_set_errnof(error, r, "Failed to wait for child: %m");
- if (si.si_code == CLD_EXITED && si.si_status == EXIT_NOT_FOUND)
+ if (r == EXIT_NOT_FOUND)
return sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Machine does not contain OS release information");
- if (si.si_code != CLD_EXITED || si.si_status != EXIT_SUCCESS)
+ if (r != EXIT_SUCCESS)
return sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Child died abnormally.");
break;
@@ -840,7 +838,6 @@ int bus_machine_method_bind_mount(sd_bus_message *message, void *userdata, sd_bu
const char *dest, *src;
Machine *m = userdata;
struct stat st;
- siginfo_t si;
pid_t child;
uid_t uid;
int r;
@@ -1046,17 +1043,12 @@ int bus_machine_method_bind_mount(sd_bus_message *message, void *userdata, sd_bu
errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
- r = wait_for_terminate(child, &si);
+ r = wait_for_terminate_and_check("(sd-bindmnt)", child, 0);
if (r < 0) {
r = sd_bus_error_set_errnof(error, r, "Failed to wait for child: %m");
goto finish;
}
- if (si.si_code != CLD_EXITED) {
- r = sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Child died abnormally.");
- goto finish;
- }
- if (si.si_status != EXIT_SUCCESS) {
-
+ if (r != EXIT_SUCCESS) {
if (read(errno_pipe_fd[0], &r, sizeof(r)) == sizeof(r))
r = sd_bus_error_set_errnof(error, r, "Failed to mount: %m");
else
@@ -1268,7 +1260,6 @@ int bus_machine_method_open_root_directory(sd_bus_message *message, void *userda
case MACHINE_CONTAINER: {
_cleanup_close_ int mntns_fd = -1, root_fd = -1;
_cleanup_close_pair_ int pair[2] = { -1, -1 };
- siginfo_t si;
pid_t child;
r = namespace_open(m->leader, NULL, &mntns_fd, NULL, NULL, &root_fd);
@@ -1304,10 +1295,10 @@ int bus_machine_method_open_root_directory(sd_bus_message *message, void *userda
pair[1] = safe_close(pair[1]);
- r = wait_for_terminate(child, &si);
+ r = wait_for_terminate_and_check("(sd-openroot)", child, 0);
if (r < 0)
return sd_bus_error_set_errnof(error, r, "Failed to wait for child: %m");
- if (si.si_code != CLD_EXITED || si.si_status != EXIT_SUCCESS)
+ if (r != EXIT_SUCCESS)
return sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Child died abnormally.");
fd = receive_one_fd(pair[0], MSG_DONTWAIT);
diff --git a/src/shared/dissect-image.c b/src/shared/dissect-image.c
index 8fc9e97bbd..05a9f37da3 100644
--- a/src/shared/dissect-image.c
+++ b/src/shared/dissect-image.c
@@ -1244,7 +1244,6 @@ int dissected_image_acquire_metadata(DissectedImage *m) {
_cleanup_free_ char *hostname = NULL;
unsigned n_meta_initialized = 0, k;
int fds[2 * _META_MAX], r;
- siginfo_t si;
BLOCK_SIGNALS(SIGCHLD);
@@ -1366,15 +1365,12 @@ int dissected_image_acquire_metadata(DissectedImage *m) {
}
}
- r = wait_for_terminate(child, &si);
- if (r < 0)
- goto finish;
+ r = wait_for_terminate_and_check("(sd-dissect)", child, 0);
child = 0;
-
- if (si.si_code != CLD_EXITED || si.si_status != EXIT_SUCCESS) {
- r = -EPROTO;
+ if (r < 0)
goto finish;
- }
+ if (r != EXIT_SUCCESS)
+ return -EPROTO;
free_and_replace(m->hostname, hostname);
m->machine_id = machine_id;
diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c
index ffb921e4a8..a861be7b32 100644
--- a/src/shared/logs-show.c
+++ b/src/shared/logs-show.c
@@ -1256,7 +1256,6 @@ static int get_boot_id_for_machine(const char *machine, sd_id128_t *boot_id) {
_cleanup_close_pair_ int pair[2] = { -1, -1 };
_cleanup_close_ int pidnsfd = -1, mntnsfd = -1, rootfd = -1;
pid_t pid, child;
- siginfo_t si;
char buf[37];
ssize_t k;
int r;
@@ -1308,9 +1307,11 @@ static int get_boot_id_for_machine(const char *machine, sd_id128_t *boot_id) {
pair[1] = safe_close(pair[1]);
- r = wait_for_terminate(child, &si);
- if (r < 0 || si.si_code != CLD_EXITED || si.si_status != EXIT_SUCCESS)
- return r < 0 ? r : -EIO;
+ r = wait_for_terminate_and_check("(sd-bootid)", child, 0);
+ if (r < 0)
+ return r;
+ if (r != EXIT_SUCCESS)
+ return -EIO;
k = recv(pair[0], buf, 36, 0);
if (k != 36)
diff --git a/src/shared/machine-pool.c b/src/shared/machine-pool.c
index fb8a6e2fd7..031d443e9e 100644
--- a/src/shared/machine-pool.c
+++ b/src/shared/machine-pool.c
@@ -79,7 +79,6 @@ static int setup_machine_raw(uint64_t size, sd_bus_error *error) {
_cleanup_close_ int fd = -1;
struct statvfs ss;
pid_t pid = 0;
- siginfo_t si;
int r;
/* We want to be able to make use of btrfs-specific file
@@ -141,24 +140,19 @@ static int setup_machine_raw(uint64_t size, sd_bus_error *error) {
_exit(EXIT_FAILURE);
}
- r = wait_for_terminate(pid, &si);
- if (r < 0) {
- sd_bus_error_set_errnof(error, r, "Failed to wait for mkfs.btrfs: %m");
- goto fail;
- }
-
+ r = wait_for_terminate_and_check("mkfs", pid, 0);
pid = 0;
- if (si.si_code != CLD_EXITED) {
- r = sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "mkfs.btrfs died abnormally.");
+ if (r < 0) {
+ sd_bus_error_set_errnof(error, r, "Failed to wait for mkfs.btrfs: %m");
goto fail;
}
- if (si.si_status == 99) {
+ if (r == 99) {
r = sd_bus_error_set_errnof(error, ENOENT, "Cannot set up /var/lib/machines, mkfs.btrfs is missing");
goto fail;
}
- if (si.si_status != 0) {
- r = sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "mkfs.btrfs failed with error code %i", si.si_status);
+ if (r != EXIT_SUCCESS) {
+ r = sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "mkfs.btrfs failed with error code %i", r);
goto fail;
}
diff --git a/src/shared/pager.c b/src/shared/pager.c
index a39abfda7b..18c8f6c18c 100644
--- a/src/shared/pager.c
+++ b/src/shared/pager.c
@@ -190,7 +190,6 @@ int show_man_page(const char *desc, bool null_stdio) {
pid_t pid;
size_t k;
int r;
- siginfo_t status;
k = strlen(desc);
@@ -218,10 +217,5 @@ int show_man_page(const char *desc, bool null_stdio) {
_exit(EXIT_FAILURE);
}
- r = wait_for_terminate(pid, &status);
- if (r < 0)
- return r;
-
- log_debug("Exit code %i status %i", status.si_code, status.si_status);
- return status.si_status;
+ return wait_for_terminate_and_check(NULL, pid, 0);
}
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index 741144b6cb..3491146e94 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -6065,7 +6065,6 @@ static int enable_sysv_units(const char *verb, char **args) {
_cleanup_free_ char *p = NULL, *q = NULL, *l = NULL;
bool found_native = false, found_sysv;
- siginfo_t status;
const char *name;
unsigned c = 1;
pid_t pid;
@@ -6129,27 +6128,21 @@ static int enable_sysv_units(const char *verb, char **args) {
_exit(EXIT_FAILURE);
}
- j = wait_for_terminate(pid, &status);
+ j = wait_for_terminate_and_check("sysv-install", pid, WAIT_LOG_ABNORMAL);
if (j < 0)
- return log_error_errno(j, "Failed to wait for child: %m");
-
- if (status.si_code == CLD_EXITED) {
- if (streq(verb, "is-enabled")) {
- if (status.si_status == 0) {
- if (!arg_quiet)
- puts("enabled");
- r = 1;
- } else {
- if (!arg_quiet)
- puts("disabled");
- }
+ return j;
+ if (streq(verb, "is-enabled")) {
+ if (j == 0) {
+ if (!arg_quiet)
+ puts("enabled");
+ r = 1;
+ } else {
+ if (!arg_quiet)
+ puts("disabled");
+ }
- } else if (status.si_status != 0)
- return -EBADE; /* We don't warn here, under the assumption the script already showed an explanation */
- } else {
- log_error("Unexpected waitid() result.");
- return -EPROTO;
- }
+ } else if (j != 0)
+ return -EBADE; /* We don't warn here, under the assumption the script already showed an explanation */
if (found_native)
continue;