]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
coredump: tweak coredump log message
authorLennart Poettering <lennart@poettering.net>
Wed, 1 Nov 2023 08:55:40 +0000 (09:55 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 1 Nov 2023 09:02:04 +0000 (10:02 +0100)
Let's not claim a process dumped core if that was disabled via resource
limits.

While we are at it, switch from stack to heap allocation for the log
message, as it includes a stack trace which can be arbitrarily large.

Fixes: #28559
src/coredump/coredump.c

index a67d425c14ee90ad84366ec767db7638d966d93e..281e7170c235aad569416f1d98c77c1e85774fb6 100644 (file)
@@ -778,10 +778,9 @@ static int submit_coredump(
         _cleanup_close_ int coredump_fd = -EBADF, coredump_node_fd = -EBADF;
         _cleanup_free_ char *filename = NULL, *coredump_data = NULL;
         _cleanup_free_ char *stacktrace = NULL;
-        char *core_message;
         const char *module_name;
         uint64_t coredump_size = UINT64_MAX, coredump_compressed_size = UINT64_MAX;
-        bool truncated = false;
+        bool truncated = false, written = false;
         JsonVariant *module_json;
         int r;
 
@@ -800,6 +799,8 @@ static int submit_coredump(
                 /* Skip whole core dumping part */
                 goto log;
 
+        written = true;
+
         /* If we don't want to keep the coredump on disk, remove it now, as later on we
          * will lack the privileges for it. However, we keep the fd to it, so that we can
          * still process it and log it. */
@@ -840,13 +841,23 @@ static int submit_coredump(
         }
 
 log:
-        core_message = strjoina("Process ", context->meta[META_ARGV_PID],
-                                " (", context->meta[META_COMM], ") of user ",
-                                context->meta[META_ARGV_UID], " dumped core.",
-                                context->is_journald && filename ? "\nCoredump diverted to " : NULL,
-                                context->is_journald && filename ? filename : NULL);
+        _cleanup_free_ char *core_message = NULL;
+
+        core_message = strjoin(
+                        "Process ", context->meta[META_ARGV_PID],
+                        " (", context->meta[META_COMM],
+                        ") of user ", context->meta[META_ARGV_UID],
+                        written ? " dumped core." : " terminated abnormally without generating a coredump.");
+        if (!core_message)
+                return log_oom();
 
-        core_message = strjoina(core_message, stacktrace ? "\n\n" : NULL, stacktrace);
+        if (context->is_journald && filename)
+                if (!strextend(&core_message, "\nCoredump diverted to ", filename))
+                        return log_oom();
+
+        if (stacktrace)
+                if (!strextend(&core_message, "\n\n", stacktrace))
+                        return log_oom();
 
         if (context->is_journald)
                 /* We might not be able to log to the journal, so let's always print the message to another