coredump.c revision 347272731e15d3c4a70fad7ccd7185e8e8059d01
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2012 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <systemd/sd-journal.h>
#include <systemd/sd-login.h>
#include "log.h"
#include "util.h"
#include "strv.h"
#include "macro.h"
#include "mkdir.h"
#include "special.h"
#include "cgroup-util.h"
#include "journald-native.h"
#include "conf-parser.h"
#include "copy.h"
#include "stacktrace.h"
#include "path-util.h"
#include "compress.h"
#ifdef HAVE_ACL
# include "acl-util.h"
#endif
#ifdef HAVE_XZ
# include <lzma.h>
#else
# define LZMA_PRESET_DEFAULT 0
#endif
/* The maximum size up to which we process coredumps */
/* The maximum size up to which we leave the coredump around on
* disk */
#define EXTERNAL_SIZE_MAX PROCESS_SIZE_MAX
/* The maximum size up to which we store the coredump in the
* journal */
/* Make sure to not make this larger than the maximum journal entry
* size. See ENTRY_SIZE_MAX in journald-native.c. */
enum {
};
typedef enum CoredumpStorage {
static const char* const coredump_storage_table[_COREDUMP_STORAGE_MAX] = {
[COREDUMP_STORAGE_NONE] = "none",
[COREDUMP_STORAGE_EXTERNAL] = "external",
[COREDUMP_STORAGE_JOURNAL] = "journal",
[COREDUMP_STORAGE_BOTH] = "both",
};
"Failed to parse storage setting");
typedef enum CoredumpCompression {
static const char* const coredump_compression_table[_COREDUMP_COMPRESSION_MAX] = {
[COREDUMP_COMPRESSION_NONE] = "none",
[COREDUMP_COMPRESSION_XZ] = "xz",
};
"Failed to parse compression setting");
static unsigned arg_compression_level = LZMA_PRESET_DEFAULT;
static int parse_config(void) {
static const ConfigTableItem items[] = {
{}
};
return config_parse(
NULL,
"/etc/systemd/coredump.conf",
NULL,
"Coredump\0",
(void*) items,
false,
false,
NULL);
#ifdef HAVE_XZ
if (arg_compression_level > 9) {
}
#endif
}
#ifdef HAVE_ACL
if (uid <= SYSTEM_UID_MAX)
return 0;
/* Make sure normal users can read (but not write or delete)
* their own coredumps */
if (!acl) {
log_error("Failed to get ACL: %m");
return -errno;
}
log_error("Failed to patch ACL: %m");
return -errno;
}
calc_acl_mask_if_needed(&acl) < 0) {
log_warning("Failed to patch ACL: %m");
return -errno;
}
log_error("Failed to apply ACL: %m");
return -errno;
}
#endif
return 0;
}
[INFO_SIGNAL] = "user.coredump.signal",
[INFO_TIMESTAMP] = "user.coredump.timestamp",
[INFO_COMM] = "user.coredump.comm",
};
int r = 0;
unsigned i;
/* Attach some metadata to coredumps via extended
* attributes. Just because we can. */
for (i = 0; i < _INFO_LEN; i++) {
int k;
continue;
if (k < 0 && r == 0)
r = -errno;
}
return r;
}
/* Ignore errors on these */
return -errno;
}
return -errno;
}
return 0;
}
/* Returns 1 if might remove, 0 if will not remove, <0 on error. */
return 0;
if (!filename)
return 1;
return -errno;
}
return 1;
}
char **ret_filename,
int *ret_fd,
int r;
if (!c)
return log_oom();
if (!p)
return log_oom();
if (!t)
return log_oom();
r = sd_id128_get_boot(&boot);
if (r < 0) {
return r;
}
c,
p,
t);
if (r < 0)
return log_oom();
if (!tmp)
return log_oom();
if (fd < 0) {
return -errno;
}
if (r == -E2BIG) {
log_error("Coredump of %s (%s) is larger than configured processing limit, refusing.",
goto fail;
log_error("Not enough disk space for coredump of %s (%s), refusing.",
goto fail;
} else if (r < 0) {
goto fail;
}
goto uncompressed;
}
goto fail;
}
#ifdef HAVE_XZ
/* If we will remove the coredump anyway, do not compress. */
&& arg_compression == COREDUMP_COMPRESSION_XZ) {
char *tmp2;
if (fd2 < 0) {
goto uncompressed;
}
if (r < 0) {
goto fail2;
}
if (!fn2) {
log_oom();
goto fail2;
}
if (r < 0)
goto fail2;
fd = -1;
return 0;
}
#endif
if (r < 0)
goto fail;
*ret_filename = fn;
fd = -1;
return 0;
fail:
return r;
}
ssize_t n;
log_warning("Failed to seek: %m");
return -errno;
}
if (!field) {
log_warning("Failed to allocate memory for coredump, coredump will not be stored.");
return -ENOMEM;
}
if (n < 0) {
return (int) n;
}
log_error("Core data too short.");
return -EIO;
}
return 0;
}
int r, j = 0;
char *t;
/* Make sure we never enter a loop */
prctl(PR_SET_DUMPABLE, 0);
/* First, log to a safe place, since we don't know what
* crashed and it might be journald which we'd rather not log
* to then. */
log_open();
log_error("Not enough arguments passed from kernel (%d, expected %d).",
r = -EINVAL;
goto finish;
}
/* Ignore all parse errors */
parse_config();
log_debug("Selected storage '%s'.",
log_debug("Selected compression %s:%u.",
if (r < 0) {
log_error("Failed to parse UID.");
goto finish;
}
if (r < 0) {
log_error("Failed to parse PID.");
goto finish;
}
if (r < 0) {
log_error("Failed to parse GID.");
goto finish;
}
log_warning("Failed to get COMM, falling back to the commandline.");
}
log_warning("Failed to get EXE.");
if (cg_pid_get_unit(pid, &t) >= 0) {
if (streq(t, SPECIAL_JOURNALD_SERVICE)) {
/* If we are journald, we cut things short,
* don't write to the journal, but still
* create a coredump. */
if (arg_storage != COREDUMP_STORAGE_NONE)
if (r < 0)
goto finish;
if (r < 0)
goto finish;
goto finish;
}
} else if (cg_pid_get_user_unit(pid, &t) >= 0)
if (core_unit)
/* OK, now we know it's not the journal, hence we can make use
* of it now. */
log_open();
if (core_pid)
if (core_uid)
if (core_gid)
if (core_signal)
if (sd_pid_get_session(pid, &t) >= 0) {
free(t);
if (core_session)
}
if (core_owner_uid)
}
if (sd_pid_get_slice(pid, &t) >= 0) {
free(t);
if (core_slice)
}
if (comm) {
if (core_comm)
}
if (exe) {
if (core_exe)
}
if (get_process_cmdline(pid, 0, false, &t) >= 0) {
free(t);
if (core_cmdline)
}
free(t);
if (core_cgroup)
}
if (core_timestamp)
/* Always stream the coredump to disk, if that's possible */
if (r < 0)
/* 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 (r < 0)
goto finish;
/* Now, let's drop privileges to become the user who owns the
* segfaulted process and allocate the coredump memory under
* his uid. This also ensures that the credentials journald
* will see are the ones of the coredumping user, thus making
* sure the user himself gets access to the core dump. */
log_error("Failed to drop privileges: %m");
r = -errno;
goto finish;
}
#ifdef HAVE_ELFUTILS
/* Try to get a strack trace if we can */
if (coredump_size <= arg_process_size_max) {
if (r >= 0)
core_message = strjoin("MESSAGE=Process ", info[INFO_PID], " (", comm, ") of user ", info[INFO_UID], " dumped core.\n\n", stacktrace, NULL);
else
}
if (!core_message)
#endif
log:
core_message = strjoin("MESSAGE=Process ", info[INFO_PID], " (", comm, ") of user ", info[INFO_UID], " dumped core.", NULL);
if (core_message)
/* Optionally store the entire coredump in the journal */
/* Store the coredump itself in the journal */
if (r >= 0) {
j++;
}
}
r = sd_journal_sendv(iovec, j);
if (r < 0)
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}