]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
coredump: rework gather_pid_mount_tree_fd()
authorMichal Sekletar <msekleta@redhat.com>
Tue, 10 Sep 2024 17:15:46 +0000 (19:15 +0200)
committerLuca Boccassi <luca.boccassi@gmail.com>
Wed, 30 Oct 2024 12:38:27 +0000 (12:38 +0000)
src/coredump/coredump.c

index 0771e291c2b50a5c52036291779d06941f9bdf6a..1a138932227fb6c806a75df4aa687bc4c4d48903 100644 (file)
@@ -2,11 +2,15 @@
 
 #include <errno.h>
 #include <stdio.h>
+#include <sys/mount.h>
 #include <sys/prctl.h>
 #include <sys/statvfs.h>
 #include <sys/auxv.h>
 #include <sys/xattr.h>
 #include <unistd.h>
+#if WANT_LINUX_FS_H
+#  include <linux/fs.h>
+#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