coredump.c revision 93240d3aba4611dd966c5b9f7368d20612211486
/*-*- 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 "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"
#ifdef HAVE_ACL
#include "acl-util.h"
#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 {
ARG_PID = 1,
};
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",
};
static DEFINE_CONFIG_PARSE_ENUM(config_parse_coredump_storage, coredump_storage, CoredumpStorage, "Failed to parse storage setting");
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_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;
}
[ARG_SIGNAL] = "user.coredump.signal",
[ARG_TIMESTAMP] = "user.coredump.timestamp",
[ARG_COMM] = "user.coredump.comm",
};
int r = 0;
unsigned i;
/* Attach some metadate to coredumps via extended
* attributes. Just because we can. */
for (i = 0; i < _ARG_MAX; i++) {
continue;
r = -errno;
}
return r;
}
static int save_external_coredump(char **argv, uid_t uid, char **ret_filename, int *ret_fd, off_t *ret_size) {
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) {
log_error("Failed to create coredump file: %m");
return -errno;
}
if (r == -E2BIG) {
log_error("Coredump of %s (%s) is larger than configured processing limit, refusing.", argv[ARG_PID], argv[ARG_COMM]);
goto fail;
log_error("Not enough disk space for coredump of %s (%s), refusing.", argv[ARG_PID], argv[ARG_COMM]);
goto fail;
} else if (r < 0) {
goto fail;
}
/* Ignore errors on these */
log_error("Failed to sync coredump: %m");
r = -errno;
goto fail;
}
log_error("Failed to fstat coredump: %m");
r = -errno;
goto fail;
}
log_error("Failed to rename coredump: %m");
r = -errno;
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 fore coredump, coredump will not be stored.");
return -ENOMEM;
}
if (n < 0) {
return (int) n;
}
log_error("Core data too short.");
return -EIO;
}
return 0;
}
if (!filename)
return 0;
return 0;
return -errno;
}
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("Invalid number of arguments passed from kernel.");
r = -EINVAL;
goto finish;
}
/* Ignore all parse errors */
parse_config();
/* Exit early if we cannot write the coredump to disk anyway */
if (path_is_read_only_fs("/var/lib") != 0) {
log_error("Coredump directory not mounted or not writable, skipping coredump.");
r = -EROFS;
goto finish;
}
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;
}
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 (core_comm)
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 (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)
goto finish;
/* 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 ", argv[ARG_PID], " (", argv[ARG_COMM], ") of user ", argv[ARG_UID], " dumped core.\n\n", stacktrace, NULL);
else
}
if (!core_message)
#endif
core_message = strjoin("MESSAGE=Process ", argv[ARG_PID], " (", argv[ARG_COMM], ") of user ", argv[ARG_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;
}