From: Michal Sekletar Date: Tue, 10 Sep 2024 17:15:46 +0000 (+0200) Subject: coredump: rework gather_pid_mount_tree_fd() X-Git-Tag: v257-rc1~99^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b8fe1b1dc8143c3fdd0a971a6ed9beb2e83c2bed;p=thirdparty%2Fsystemd.git coredump: rework gather_pid_mount_tree_fd() --- 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; +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 + *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 fd = -EBADF, r; - pid_t child; + int r; assert(context); + assert(ret_fd); - /* Don't bother preparing environment if we can't pass it to libdwfl. */ -#if !HAVE_DWFL_SET_SYSROOT - return -EOPNOTSUPP; -#endif - - if (!arg_access_container) - return -EOPNOTSUPP; + 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