From b8fe1b1dc8143c3fdd0a971a6ed9beb2e83c2bed Mon Sep 17 00:00:00 2001 From: Michal Sekletar Date: Tue, 10 Sep 2024 19:15:46 +0200 Subject: [PATCH] coredump: rework gather_pid_mount_tree_fd() --- src/coredump/coredump.c | 48 +++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/src/coredump/coredump.c b/src/coredump/coredump.c index 0771e291c2b..1a138932227 100644 --- a/src/coredump/coredump.c +++ b/src/coredump/coredump.c @@ -2,11 +2,15 @@ #include #include +#include #include #include #include #include #include +#if WANT_LINUX_FS_H +# include +#endif #include "sd-daemon.h" #include "sd-journal.h" @@ -1652,26 +1656,34 @@ static int forward_coredump_to_container(Context *context) { return 0; } -static int gather_pid_mount_tree_fd(const Context *context) { - _cleanup_close_ int mntns_fd = -EBADF, root_fd = -EBADF; - _cleanup_close_pair_ int pair[2] = EBADF_PAIR; - int fd = -EBADF, r; - pid_t child; - - assert(context); - +static int gather_pid_mount_tree_fd(const Context *context, int *ret_fd) { /* Don't bother preparing environment if we can't pass it to libdwfl. */ #if !HAVE_DWFL_SET_SYSROOT - return -EOPNOTSUPP; -#endif + *ret_fd = -EOPNOTSUPP; + log_debug("dwfl_set_sysroot() is not supported."); +#else + _cleanup_close_ int mntns_fd = -EBADF, root_fd = -EBADF, fd = -EBADF; + _cleanup_close_pair_ int pair[2] = EBADF_PAIR; + int r; - if (!arg_access_container) - return -EOPNOTSUPP; + assert(context); + assert(ret_fd); + + if (!arg_access_container) { + *ret_fd = -EHOSTDOWN; + log_debug("EnterNamespace=no so we won't use mount tree of the crashed process for generating backtrace."); + return 0; + } if (socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, pair) < 0) return log_error_errno(errno, "Failed to create socket pair: %m"); - r = namespace_open(context->pid, NULL, &mntns_fd, NULL, NULL, &root_fd); + r = namespace_open(context->pid, + /* ret_pidns_fd= */ NULL, + &mntns_fd, + /* ret_netns_fd= */ NULL, + /* ret_userns_fd= */ NULL, + &root_fd); if (r < 0) return log_error_errno(r, "Failed to open mount namespace of crashing process: %m"); @@ -1712,7 +1724,9 @@ static int gather_pid_mount_tree_fd(const Context *context) { if (fd < 0) return log_error_errno(fd, "Failed to receive mount tree: %m"); - return fd; + *ret_fd = TAKE_FD(fd); +#endif + return 0; } static int process_kernel(int argc, char* argv[]) { @@ -1764,11 +1778,9 @@ static int process_kernel(int argc, char* argv[]) { if (r >= 0) return 0; - r = gather_pid_mount_tree_fd(&context); - if (r < 0 && r != -EOPNOTSUPP) + r = gather_pid_mount_tree_fd(&context, &mount_tree_fd); + if (r < 0) log_warning_errno(r, "Failed to access the mount tree of a container, ignoring: %m"); - else - mount_tree_fd = r; } /* If this is PID 1 disable coredump collection, we'll unlikely be able to process