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.
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 };
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 };
pair[1] = safe_close(pair[1]);
pair[1] = safe_close(pair[1]);
- r = wait_for_terminate(child, &si);
+ r = wait_for_terminate_and_check("(sd-openpt)", child, 0);
- if (si.si_code != CLD_EXITED || si.si_status != EXIT_SUCCESS)
return -EIO;
return receive_one_fd(pair[0], 0);
return -EIO;
return receive_one_fd(pair[0], 0);
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 };
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 };
pair[1] = safe_close(pair[1]);
pair[1] = safe_close(pair[1]);
- r = wait_for_terminate(child, &si);
+ r = wait_for_terminate_and_check("(sd-terminal)", child, 0);
- if (si.si_code != CLD_EXITED || si.si_status != EXIT_SUCCESS)
return -EIO;
return receive_one_fd(pair[0], 0);
return -EIO;
return receive_one_fd(pair[0], 0);
_cleanup_close_ int unshare_ready_fd = -1;
_cleanup_(sigkill_waitp) pid_t pid = 0;
uint64_t c = 1;
_cleanup_close_ int unshare_ready_fd = -1;
_cleanup_(sigkill_waitp) pid_t pid = 0;
uint64_t c = 1;
if (n != 0) /* on success we should have read 0 bytes */
return -EIO;
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;
- 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 */
_cleanup_free_ char *exe = NULL, *path = NULL;
bool unlink_path = false;
const char *data;
_cleanup_free_ char *exe = NULL, *path = NULL;
bool unlink_path = false;
const char *data;
size_t len;
pid_t pid;
int r;
size_t len;
pid_t pid;
int r;
- 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);
finish:
(void) default_signals(SIGINT, -1);
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
const char *device, *type;
bool root_directory;
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
const char *device, *type;
bool root_directory;
pid_t pid;
if (argc > 2) {
pid_t pid;
if (argc > 2) {
(void) process_progress(progress_pipe[0]);
progress_pipe[0] = -1;
(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;
+ 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");
/* 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");
/* Some other problem */
start_target(SPECIAL_EMERGENCY_TARGET, "replace");
+ r = -EINVAL;
+ } else {
log_warning("Ignoring error.");
r = 0;
}
log_warning("Ignoring error.");
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:
(void) touch("/run/systemd/quotacheck");
finish:
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;
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;
- r = wait_for_terminate(grandchild, &si);
+ r = wait_for_terminate_and_check("(sd-buscntr2)", grandchild, 0);
if (r < 0)
_exit(EXIT_FAILURE);
if (r < 0)
_exit(EXIT_FAILURE);
- if (si.si_code != CLD_EXITED)
- _exit(EXIT_FAILURE);
-
- _exit(si.si_status);
}
pair[1] = safe_close(pair[1]);
}
pair[1] = safe_close(pair[1]);
- r = wait_for_terminate(child, &si);
+ r = wait_for_terminate_and_check("(sd-buscntr)", child, 0);
+ if (r != EXIT_SUCCESS)
+ return -EPROTO;
n = read(pair[0], &error_buf, sizeof(error_buf));
if (n < 0)
n = read(pair[0], &error_buf, sizeof(error_buf));
if (n < 0)
- if (si.si_code != CLD_EXITED)
- return -EIO;
-
- if (si.si_status != EXIT_SUCCESS)
- return -EIO;
-
return bus_socket_start_auth(b);
}
return bus_socket_start_auth(b);
}
_cleanup_free_ char *us = NULL, *them = NULL;
_cleanup_close_ int netns_fd = -1;
const char *p;
_cleanup_free_ char *us = NULL, *them = NULL;
_cleanup_close_ int netns_fd = -1;
const char *p;
pid_t child;
r = readlink_malloc("/proc/self/ns/net", &us);
pid_t child;
r = readlink_malloc("/proc/self/ns/net", &us);
- 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 (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)
return sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Child died abnormally.");
break;
}
return sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Child died abnormally.");
break;
}
_cleanup_close_ int mntns_fd = -1, root_fd = -1;
_cleanup_close_pair_ int pair[2] = { -1, -1 };
_cleanup_fclose_ FILE *f = NULL;
_cleanup_close_ int mntns_fd = -1, root_fd = -1;
_cleanup_close_pair_ int pair[2] = { -1, -1 };
_cleanup_fclose_ FILE *f = NULL;
pid_t child;
r = namespace_open(m->leader, NULL, &mntns_fd, NULL, NULL, &root_fd);
pid_t child;
r = namespace_open(m->leader, NULL, &mntns_fd, NULL, NULL, &root_fd);
- 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 (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");
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)
return sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Child died abnormally.");
break;
return sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Child died abnormally.");
break;
const char *dest, *src;
Machine *m = userdata;
struct stat st;
const char *dest, *src;
Machine *m = userdata;
struct stat st;
pid_t child;
uid_t uid;
int r;
pid_t child;
uid_t uid;
int r;
errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
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 (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
if (read(errno_pipe_fd[0], &r, sizeof(r)) == sizeof(r))
r = sd_bus_error_set_errnof(error, r, "Failed to mount: %m");
else
case MACHINE_CONTAINER: {
_cleanup_close_ int mntns_fd = -1, root_fd = -1;
_cleanup_close_pair_ int pair[2] = { -1, -1 };
case MACHINE_CONTAINER: {
_cleanup_close_ int mntns_fd = -1, root_fd = -1;
_cleanup_close_pair_ int pair[2] = { -1, -1 };
pid_t child;
r = namespace_open(m->leader, NULL, &mntns_fd, NULL, NULL, &root_fd);
pid_t child;
r = namespace_open(m->leader, NULL, &mntns_fd, NULL, NULL, &root_fd);
pair[1] = safe_close(pair[1]);
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 (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)
return sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Child died abnormally.");
fd = receive_one_fd(pair[0], MSG_DONTWAIT);
return sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Child died abnormally.");
fd = receive_one_fd(pair[0], MSG_DONTWAIT);
_cleanup_free_ char *hostname = NULL;
unsigned n_meta_initialized = 0, k;
int fds[2 * _META_MAX], r;
_cleanup_free_ char *hostname = NULL;
unsigned n_meta_initialized = 0, k;
int fds[2 * _META_MAX], r;
- r = wait_for_terminate(child, &si);
- if (r < 0)
- goto finish;
+ r = wait_for_terminate_and_check("(sd-dissect)", child, 0);
-
- if (si.si_code != CLD_EXITED || si.si_status != EXIT_SUCCESS) {
- r = -EPROTO;
+ if (r != EXIT_SUCCESS)
+ return -EPROTO;
free_and_replace(m->hostname, hostname);
m->machine_id = machine_id;
free_and_replace(m->hostname, hostname);
m->machine_id = machine_id;
_cleanup_close_pair_ int pair[2] = { -1, -1 };
_cleanup_close_ int pidnsfd = -1, mntnsfd = -1, rootfd = -1;
pid_t pid, child;
_cleanup_close_pair_ int pair[2] = { -1, -1 };
_cleanup_close_ int pidnsfd = -1, mntnsfd = -1, rootfd = -1;
pid_t pid, child;
char buf[37];
ssize_t k;
int r;
char buf[37];
ssize_t k;
int r;
pair[1] = safe_close(pair[1]);
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)
k = recv(pair[0], buf, 36, 0);
if (k != 36)
_cleanup_close_ int fd = -1;
struct statvfs ss;
pid_t pid = 0;
_cleanup_close_ int fd = -1;
struct statvfs ss;
pid_t pid = 0;
int r;
/* We want to be able to make use of btrfs-specific file
int r;
/* We want to be able to make use of btrfs-specific file
- 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);
- 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");
- if (si.si_status == 99) {
r = sd_bus_error_set_errnof(error, ENOENT, "Cannot set up /var/lib/machines, mkfs.btrfs is missing");
goto fail;
}
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);
pid_t pid;
size_t k;
int r;
pid_t pid;
size_t k;
int r;
- 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);
_cleanup_free_ char *p = NULL, *q = NULL, *l = NULL;
bool found_native = false, found_sysv;
_cleanup_free_ char *p = NULL, *q = NULL, *l = NULL;
bool found_native = false, found_sysv;
const char *name;
unsigned c = 1;
pid_t pid;
const char *name;
unsigned c = 1;
pid_t pid;
- j = wait_for_terminate(pid, &status);
+ j = wait_for_terminate_and_check("sysv-install", pid, WAIT_LOG_ABNORMAL);
- 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;
if (found_native)
continue;