mirror of
https://github.com/systemd/systemd-stable.git
synced 2025-01-08 21:17:47 +03:00
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.
This commit is contained in:
parent
1f5d1e0247
commit
2e87a1fde9
@ -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);
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
|
@ -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:
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
r = wait_for_terminate_and_check("(sd-dissect)", child, 0);
|
||||
child = 0;
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
child = 0;
|
||||
|
||||
if (si.si_code != CLD_EXITED || si.si_status != EXIT_SUCCESS) {
|
||||
r = -EPROTO;
|
||||
goto finish;
|
||||
}
|
||||
if (r != EXIT_SUCCESS)
|
||||
return -EPROTO;
|
||||
|
||||
free_and_replace(m->hostname, hostname);
|
||||
m->machine_id = machine_id;
|
||||
|
@ -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)
|
||||
|
@ -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);
|
||||
r = wait_for_terminate_and_check("mkfs", pid, 0);
|
||||
pid = 0;
|
||||
|
||||
if (r < 0) {
|
||||
sd_bus_error_set_errnof(error, r, "Failed to wait for mkfs.btrfs: %m");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
pid = 0;
|
||||
|
||||
if (si.si_code != CLD_EXITED) {
|
||||
r = sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "mkfs.btrfs died abnormally.");
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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");
|
||||
return j;
|
||||
if (streq(verb, "is-enabled")) {
|
||||
if (j == 0) {
|
||||
if (!arg_quiet)
|
||||
puts("enabled");
|
||||
r = 1;
|
||||
} else {
|
||||
if (!arg_quiet)
|
||||
puts("disabled");
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
} 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;
|
||||
|
Loading…
Reference in New Issue
Block a user