introduce report_errno_and_exit() helper (#35028)

This is a follow for https://github.com/systemd/systemd/pull/34853. In
particular, this comment
https://github.com/systemd/systemd/pull/34853#discussion_r1825837705.
This commit is contained in:
Luca Boccassi
2024-11-06 13:51:10 +00:00
committed by GitHub
9 changed files with 45 additions and 74 deletions
+4 -9
View File
@@ -3522,7 +3522,7 @@ int dissected_image_acquire_metadata(
r = detach_mount_namespace_userns(userns_fd);
if (r < 0) {
log_debug_errno(r, "Failed to detach mount namespace: %m");
goto inner_fail;
report_errno_and_exit(error_pipe[1], r);
}
r = dissected_image_mount(
@@ -3537,7 +3537,7 @@ int dissected_image_acquire_metadata(
DISSECT_IMAGE_USR_NO_ROOT);
if (r < 0) {
log_debug_errno(r, "Failed to mount dissected image: %m");
goto inner_fail;
report_errno_and_exit(error_pipe[1], r);
}
for (unsigned k = 0; k < _META_MAX; k++) {
@@ -3609,7 +3609,7 @@ int dissected_image_acquire_metadata(
r = loop_write(fds[2*k+1], &found, sizeof(found));
if (r < 0)
goto inner_fail;
report_errno_and_exit(error_pipe[1], r);
goto next;
}
@@ -3629,18 +3629,13 @@ int dissected_image_acquire_metadata(
r = copy_bytes(fd, fds[2*k+1], UINT64_MAX, 0);
if (r < 0)
goto inner_fail;
report_errno_and_exit(error_pipe[1], r);
next:
fds[2*k+1] = safe_close(fds[2*k+1]);
}
_exit(EXIT_SUCCESS);
inner_fail:
/* Let parent know the error */
(void) write(error_pipe[1], &r, sizeof(r));
_exit(EXIT_FAILURE);
}
error_pipe[1] = safe_close(error_pipe[1]);
+5 -11
View File
@@ -827,12 +827,12 @@ int parse_elf_object(int fd, const char *executable, const char *root, bool fork
if (fork_disable_dump) {
r = RET_NERRNO(prctl(PR_SET_DUMPABLE, 0));
if (r < 0)
goto child_fail;
report_errno_and_exit(error_pipe[1], r);
}
r = parse_elf(fd, executable, root, ret ? &buf : NULL, ret_package_metadata ? &package_metadata : NULL);
if (r < 0)
goto child_fail;
report_errno_and_exit(error_pipe[1], r);
if (buf) {
size_t len = strlen(buf);
@@ -853,7 +853,7 @@ int parse_elf_object(int fd, const char *executable, const char *root, bool fork
if (r == -EAGAIN)
log_warning("Write failed, backtrace will be truncated.");
else if (r < 0)
goto child_fail;
report_errno_and_exit(error_pipe[1], r);
return_pipe[1] = safe_close(return_pipe[1]);
}
@@ -866,10 +866,8 @@ int parse_elf_object(int fd, const char *executable, const char *root, bool fork
(void) fcntl(json_pipe[1], F_SETPIPE_SZ, COREDUMP_PIPE_MAX);
json_out = take_fdopen(&json_pipe[1], "w");
if (!json_out) {
r = -errno;
goto child_fail;
}
if (!json_out)
report_errno_and_exit(error_pipe[1], -errno);
r = sd_json_variant_dump(package_metadata, SD_JSON_FORMAT_FLUSH, json_out, NULL);
if (r < 0)
@@ -877,10 +875,6 @@ int parse_elf_object(int fd, const char *executable, const char *root, bool fork
}
_exit(EXIT_SUCCESS);
child_fail:
(void) write(error_pipe[1], &r, sizeof(r));
_exit(EXIT_FAILURE);
}
error_pipe[1] = safe_close(error_pipe[1]);
+5 -19
View File
@@ -1011,26 +1011,18 @@ static int mount_in_namespace_legacy(
r = path_extract_filename(mount_outside, &mount_outside_fn);
if (r < 0) {
log_debug_errno(r, "Failed to extract filename from propagation file or directory '%s': %m", mount_outside);
goto child_fail;
report_errno_and_exit(errno_pipe_fd[1], r);
}
mount_inside = path_join(incoming_path, mount_outside_fn);
if (!mount_inside) {
r = log_oom_debug();
goto child_fail;
}
if (!mount_inside)
report_errno_and_exit(errno_pipe_fd[1], log_oom_debug());
r = mount_nofollow_verbose(LOG_DEBUG, mount_inside, dest, NULL, MS_MOVE, NULL);
if (r < 0)
goto child_fail;
report_errno_and_exit(errno_pipe_fd[1], r);
_exit(EXIT_SUCCESS);
child_fail:
(void) write(errno_pipe_fd[1], &r, sizeof(r));
errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
_exit(EXIT_FAILURE);
}
errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
@@ -1224,14 +1216,8 @@ static int mount_in_namespace(
r = mount_exchange_graceful(new_mount_fd, dest, /* mount_beneath= */ true);
}
if (r < 0) {
(void) write(errno_pipe_fd[1], &r, sizeof(r));
errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
_exit(EXIT_FAILURE);
}
_exit(EXIT_SUCCESS);
report_errno_and_exit(errno_pipe_fd[1], r);
}
errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);