summaryrefslogtreecommitdiff
path: root/src/coredump
diff options
context:
space:
mode:
authorFranck Bui <fbui@suse.com>2019-06-25 14:04:46 +0200
committerFranck Bui <fbui@suse.com>2019-06-27 19:01:32 +0200
commit51d3783d87abfe85e688bee1db3d01d02d0f9557 (patch)
tree80d8249ecabf61d7caf008b81ca40742d74b8166 /src/coredump
parent2705fcd63bd2cd4203cbc5ff00cd2914e39febed (diff)
downloadsystemd-51d3783d87abfe85e688bee1db3d01d02d0f9557.tar.gz
coredump: slighlty simplify stack trace generation logic
The main advantage is to avoid the code duplication used to build MESSAGE= field. No functional changes.
Diffstat (limited to 'src/coredump')
-rw-r--r--src/coredump/coredump.c50
-rw-r--r--src/coredump/stacktrace.c12
-rw-r--r--src/coredump/stacktrace.h2
3 files changed, 33 insertions, 31 deletions
diff --git a/src/coredump/coredump.c b/src/coredump/coredump.c
index 51e8a5f083..6a6c11971e 100644
--- a/src/coredump/coredump.c
+++ b/src/coredump/coredump.c
@@ -711,6 +711,7 @@ static int submit_coredump(
_cleanup_close_ int coredump_fd = -1, coredump_node_fd = -1;
_cleanup_free_ char *core_message = NULL, *filename = NULL, *coredump_data = NULL;
+ _cleanup_free_ char *stacktrace = NULL;
uint64_t coredump_size = UINT64_MAX;
bool truncated = false, journald_crash;
int r;
@@ -732,8 +733,9 @@ static int submit_coredump(
/* Skip whole core dumping part */
goto log;
- /* 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. */
+ /* 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. */
r = maybe_remove_external_coredump(filename, coredump_size);
if (r < 0)
return r;
@@ -749,9 +751,10 @@ static int submit_coredump(
/* Vacuum again, but exclude the coredump we just created */
(void) coredump_vacuum(coredump_node_fd >= 0 ? coredump_node_fd : coredump_fd, arg_keep_free, arg_max_use);
- /* Now, let's drop privileges to become the user who owns the segfaulted process and allocate the coredump
- * memory under the user's uid. This also ensures that the credentials journald will see are the ones of the
- * coredumping user, thus making sure the user gets access to the core dump. Let's also get rid of all
+ /* Now, let's drop privileges to become the user who owns the segfaulted process
+ * and allocate the coredump memory under the user's uid. This also ensures that
+ * the credentials journald will see are the ones of the coredumping user, thus
+ * making sure the user gets access to the core dump. Let's also get rid of all
* capabilities, if we run as root, we won't need them anymore. */
r = change_uid_gid(context);
if (r < 0)
@@ -759,33 +762,22 @@ static int submit_coredump(
#if HAVE_ELFUTILS
/* Try to get a stack trace if we can */
- if (coredump_size <= arg_process_size_max) {
- _cleanup_free_ char *stacktrace = NULL;
-
- r = coredump_make_stack_trace(coredump_fd, context[CONTEXT_EXE], &stacktrace);
- if (r >= 0)
- core_message = strjoin("MESSAGE=Process ", context[CONTEXT_PID],
- " (", context[CONTEXT_COMM], ") of user ",
- context[CONTEXT_UID], " dumped core.",
- journald_crash ? "\nCoredump diverted to " : "",
- journald_crash ? filename : "",
- "\n\n", stacktrace);
- else if (r == -EINVAL)
- log_warning("Failed to generate stack trace: %s", dwfl_errmsg(dwfl_errno()));
- else
- log_warning_errno(r, "Failed to generate stack trace: %m");
- } else
- log_debug("Not generating stack trace: core size %"PRIu64" is greater than %"PRIu64" (the configured maximum)",
+ if (coredump_size > arg_process_size_max) {
+ log_debug("Not generating stack trace: core size %"PRIu64" is greater "
+ "than %"PRIu64" (the configured maximum)",
coredump_size, arg_process_size_max);
-
- if (!core_message)
+ } else
+ coredump_make_stack_trace(coredump_fd, context[CONTEXT_EXE], &stacktrace);
#endif
+
log:
- core_message = strjoin("MESSAGE=Process ", context[CONTEXT_PID],
- " (", context[CONTEXT_COMM], ") of user ",
- context[CONTEXT_UID], " dumped core.",
- journald_crash && filename ? "\nCoredump diverted to " : NULL,
- journald_crash && filename ? filename : NULL);
+ core_message = strjoina("MESSAGE=Process ", context[CONTEXT_PID],
+ " (", context[CONTEXT_COMM], ") of user ",
+ context[CONTEXT_UID], " dumped core.",
+ journald_crash && filename ? "\nCoredump diverted to " : NULL,
+ journald_crash && filename ? filename : NULL);
+
+ core_message = strjoin(core_message, stacktrace ? "\n\n" : NULL, stacktrace);
if (!core_message)
return log_oom();
diff --git a/src/coredump/stacktrace.c b/src/coredump/stacktrace.c
index 66b2ec8cca..a962cde125 100644
--- a/src/coredump/stacktrace.c
+++ b/src/coredump/stacktrace.c
@@ -108,7 +108,7 @@ static int thread_callback(Dwfl_Thread *thread, void *userdata) {
return DWARF_CB_OK;
}
-int coredump_make_stack_trace(int fd, const char *executable, char **ret) {
+static int make_stack_trace(int fd, const char *executable, char **ret) {
static const Dwfl_Callbacks callbacks = {
.find_elf = dwfl_build_id_find_elf,
@@ -183,3 +183,13 @@ finish:
return r;
}
+
+void coredump_make_stack_trace(int fd, const char *executable, char **ret) {
+ int r;
+
+ r = make_stack_trace(fd, executable, ret);
+ if (r == -EINVAL)
+ log_warning("Failed to generate stack trace: %s", dwfl_errmsg(dwfl_errno()));
+ else if (r < 0)
+ log_warning_errno(r, "Failed to generate stack trace: %m");
+}
diff --git a/src/coredump/stacktrace.h b/src/coredump/stacktrace.h
index 52900424a7..2462c763f9 100644
--- a/src/coredump/stacktrace.h
+++ b/src/coredump/stacktrace.h
@@ -1,4 +1,4 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#pragma once
-int coredump_make_stack_trace(int fd, const char *executable, char **ret);
+void coredump_make_stack_trace(int fd, const char *executable, char **ret);