]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/coredump/coredump.c
coredump: save /proc/[pid]/mountinfo
[thirdparty/systemd.git] / src / coredump / coredump.c
index 01fdcfa9090258e590e9bd755bd6806de9611d19..953f04e205c4253ce1ece52fad08a34d5dec982d 100644 (file)
@@ -157,10 +157,8 @@ static int fix_acl(int fd, uid_t uid) {
 
         if (acl_create_entry(&acl, &entry) < 0 ||
             acl_set_tag_type(entry, ACL_USER) < 0 ||
-            acl_set_qualifier(entry, &uid) < 0) {
-                log_error_errno(errno, "Failed to patch ACL: %m");
-                return -errno;
-        }
+            acl_set_qualifier(entry, &uid) < 0)
+                return log_error_errno(errno, "Failed to patch ACL: %m");
 
         if (acl_get_permset(entry, &permset) < 0 ||
             acl_add_perm(permset, ACL_READ) < 0)
@@ -739,15 +737,16 @@ static int process_socket(int fd) {
                         .msg_iovlen = 1,
                 };
                 ssize_t n;
-                int l;
+                ssize_t l;
 
                 if (!GREEDY_REALLOC(iovec, n_iovec_allocated, n_iovec + 3)) {
                         r = log_oom();
                         goto finish;
                 }
 
-                if (ioctl(fd, FIONREAD, &l) < 0) {
-                        r = log_error_errno(errno, "FIONREAD failed: %m");
+                l = next_datagram_size_fd(fd);
+                if (l < 0) {
+                        r = log_error_errno(l, "Failed to determine datagram size to read: %m");
                         goto finish;
                 }
 
@@ -755,7 +754,6 @@ static int process_socket(int fd) {
 
                 iovec[n_iovec].iov_len = l;
                 iovec[n_iovec].iov_base = malloc(l + 1);
-
                 if (!iovec[n_iovec].iov_base) {
                         r = log_oom();
                         goto finish;
@@ -810,7 +808,7 @@ static int process_socket(int fd) {
                 goto finish;
         }
 
-        /* Make sure we we got all data we really need */
+        /* Make sure we got all data we really need */
         assert(context[CONTEXT_PID]);
         assert(context[CONTEXT_UID]);
         assert(context[CONTEXT_GID]);
@@ -851,12 +849,42 @@ static int send_iovec(const struct iovec iovec[], size_t n_iovec, int input_fd)
                 return log_error_errno(errno, "Failed to connect to coredump service: %m");
 
         for (i = 0; i < n_iovec; i++) {
-                ssize_t n;
-                assert(iovec[i].iov_len > 0);
+                struct msghdr mh = {
+                        .msg_iov = (struct iovec*) iovec + i,
+                        .msg_iovlen = 1,
+                };
+                struct iovec copy[2];
+
+                for (;;) {
+                        if (sendmsg(fd, &mh, MSG_NOSIGNAL) >= 0)
+                                break;
+
+                        if (errno == EMSGSIZE && mh.msg_iov[0].iov_len > 0) {
+                                /* This field didn't fit? That's a pity. Given that this is just metadata,
+                                 * let's truncate the field at half, and try again. We append three dots, in
+                                 * order to show that this is truncated. */
+
+                                if (mh.msg_iov != copy) {
+                                        /* We don't want to modify the caller's iovec, hence let's create our
+                                         * own array, consisting of two new iovecs, where the first is a
+                                         * (truncated) copy of what we want to send, and the second one
+                                         * contains the trailing dots. */
+                                        copy[0] = iovec[i];
+                                        copy[1] = (struct iovec) {
+                                                .iov_base = (char[]) { '.', '.', '.' },
+                                                .iov_len = 3,
+                                        };
+
+                                        mh.msg_iov = copy;
+                                        mh.msg_iovlen = 2;
+                                }
+
+                                copy[0].iov_len /= 2; /* halve it, and try again */
+                                continue;
+                        }
 
-                n = send(fd, iovec[i].iov_base, iovec[i].iov_len, MSG_NOSIGNAL);
-                if (n < 0)
                         return log_error_errno(errno, "Failed to send coredump datagram: %m");
+                }
         }
 
         r = send_one_fd(fd, input_fd, 0);
@@ -866,7 +894,7 @@ static int send_iovec(const struct iovec iovec[], size_t n_iovec, int input_fd)
         return 0;
 }
 
-static int process_journald_crash(const char *context[], int input_fd) {
+static int process_special_crash(const char *context[], int input_fd) {
         _cleanup_close_ int coredump_fd = -1, coredump_node_fd = -1;
         _cleanup_free_ char *filename = NULL;
         uint64_t coredump_size;
@@ -875,7 +903,7 @@ static int process_journald_crash(const char *context[], int input_fd) {
         assert(context);
         assert(input_fd >= 0);
 
-        /* If we are journald, we cut things short, don't write to the journal, but still create a coredump. */
+        /* If we are pid1 or journald, we cut things short, don't write to the journal, but still create a coredump. */
 
         if (arg_storage != COREDUMP_STORAGE_NONE)
                 arg_storage = COREDUMP_STORAGE_EXTERNAL;
@@ -888,7 +916,8 @@ static int process_journald_crash(const char *context[], int input_fd) {
         if (r < 0)
                 return r;
 
-        log_info("Detected coredump of the journal daemon itself, diverted to %s.", filename);
+        log_notice("Detected coredump of the journal daemon or PID 1, diverted to %s.", filename);
+
         return 0;
 }
 
@@ -904,11 +933,12 @@ static int process_kernel(int argc, char* argv[]) {
         /* The larger ones we allocate on the heap */
         _cleanup_free_ char
                 *core_owner_uid = NULL, *core_open_fds = NULL, *core_proc_status = NULL,
-                *core_proc_maps = NULL, *core_proc_limits = NULL, *core_proc_cgroup = NULL, *core_environ = NULL;
+                *core_proc_maps = NULL, *core_proc_limits = NULL, *core_proc_cgroup = NULL, *core_environ = NULL,
+                *core_proc_mountinfo = NULL;
 
         _cleanup_free_ char *exe = NULL, *comm = NULL;
         const char *context[_CONTEXT_MAX];
-        struct iovec iovec[25];
+        struct iovec iovec[26];
         size_t n_iovec = 0;
         uid_t owner_uid;
         const char *p;
@@ -948,9 +978,17 @@ static int process_kernel(int argc, char* argv[]) {
 
         if (cg_pid_get_unit(pid, &t) >= 0) {
 
-                if (streq(t, SPECIAL_JOURNALD_SERVICE)) {
+                /* If this is PID 1 disable coredump collection, we'll unlikely be able to process it later on. */
+                if (streq(t, SPECIAL_INIT_SCOPE)) {
+                        log_notice("Due to PID 1 having crashed coredump collection will now be turned off.");
+                        (void) write_string_file("/proc/sys/kernel/core_pattern", "|/bin/false", 0);
+                }
+
+                /* Let's avoid dead-locks when processing journald and init crashes, as socket activation and logging
+                 * are unlikely to work then. */
+                if (STR_IN_SET(t, SPECIAL_JOURNALD_SERVICE, SPECIAL_INIT_SCOPE)) {
                         free(t);
-                        return process_journald_crash(context, STDIN_FILENO);
+                        return process_special_crash(context, STDIN_FILENO);
                 }
 
                 core_unit = strjoina("COREDUMP_UNIT=", t);
@@ -1073,6 +1111,15 @@ static int process_kernel(int argc, char* argv[]) {
                         IOVEC_SET_STRING(iovec[n_iovec++], core_proc_cgroup);
         }
 
+        p = procfs_file_alloca(pid, "mountinfo");
+        if (read_full_file(p, &t, NULL) >=0) {
+                core_proc_mountinfo = strappend("COREDUMP_PROC_MOUNTINFO=", t);
+                free(t);
+
+                if (core_proc_mountinfo)
+                        IOVEC_SET_STRING(iovec[n_iovec++], core_proc_mountinfo);
+        }
+
         if (get_process_cwd(pid, &t) >= 0) {
                 core_cwd = strjoina("COREDUMP_CWD=", t);
                 free(t);