coredump.c revision 4f5dd3943bef8a04be7e3b838b822bb9a7ad6cb3
/*-*- 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 <stdio.h>
#include <unistd.h>
#ifdef HAVE_ELFUTILS
# include <dwarf.h>
#endif
#include "sd-journal.h"
#include "sd-login.h"
#include "acl-util.h"
#include "capability.h"
#include "cgroup-util.h"
#include "compress.h"
#include "conf-parser.h"
#include "copy.h"
#include "coredump-vacuum.h"
#include "escape.h"
#include "fileio.h"
#include "journald-native.h"
#include "log.h"
#include "macro.h"
#include "mkdir.h"
#include "process-util.h"
#include "special.h"
#include "stacktrace.h"
#include "strv.h"
#include "util.h"
/* 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 DATA_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",
};
static DEFINE_CONFIG_PARSE_ENUM(config_parse_coredump_storage, coredump_storage, CoredumpStorage, "Failed to parse storage setting");
static bool arg_compress = true;
static int parse_config(void) {
static const ConfigTableItem items[] = {
{}
};
return config_parse_many("/etc/systemd/coredump.conf",
"Coredump\0",
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)
return -errno;
}
calc_acl_mask_if_needed(&acl) < 0) {
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;
}
static int fix_permissions(
int fd,
const char *filename,
const char *target,
/* Ignore errors on these */
return 0;
}
/* Returns 1 if might remove, 0 if will not remove, < 0 on error. */
return 0;
if (!filename)
return 1;
return 1;
}
sd_id128_t boot = {};
int r;
if (!c)
return -ENOMEM;
if (!u)
return -ENOMEM;
r = sd_id128_get_boot(&boot);
if (r < 0)
return r;
if (!p)
return -ENOMEM;
if (!t)
return -ENOMEM;
c,
u,
p,
t) < 0)
return -ENOMEM;
return 0;
}
static int save_external_coredump(
char **ret_filename,
int *ret_fd,
int r;
if (r < 0)
return log_error_errno(r, "Failed to determine coredump file name: %m");
if (r < 0)
return log_error_errno(r, "Failed to determine temporary file name: %m");
if (fd < 0)
if (r == -EFBIG) {
log_error("Coredump of %s (%s) is larger than configured processing limit, refusing.", info[INFO_PID], info[INFO_COMM]);
goto fail;
log_error("Not enough disk space for coredump of %s (%s), refusing.", info[INFO_PID], info[INFO_COMM]);
goto fail;
} else if (r < 0) {
log_error_errno(r, "Failed to dump coredump to file: %m");
goto fail;
}
goto fail;
}
goto fail;
}
/* If we will remove the coredump anyway, do not compress. */
&& arg_compress) {
if (!fn_compressed) {
log_oom();
goto uncompressed;
}
if (r < 0) {
goto uncompressed;
}
if (fd_compressed < 0) {
goto uncompressed;
}
if (r < 0) {
goto fail_compressed;
}
if (r < 0)
goto fail_compressed;
/* OK, this worked, we can get rid of the uncompressed version now */
fd = -1;
return 0;
}
#endif
if (r < 0)
goto fail;
*ret_filename = fn;
fd = -1;
return 0;
fail:
return r;
}
ssize_t n;
if (!field) {
log_warning("Failed to allocate memory for coredump, coredump will not be stored.");
return -ENOMEM;
}
if (n < 0)
return log_error_errno((int) n, "Failed to read core data: %m");
log_error("Core data too short.");
return -EIO;
}
return 0;
}
/* Joins /proc/[pid]/fd/ and /proc/[pid]/fdinfo/ into the following lines:
* pos: 0
* flags: 0100002
*
* pos: 0
* flags: 0100002
*
* pos: 0
* flags: 0100002
* EOF
*/
int r = 0;
if (!proc_fd_dir)
return -errno;
if (proc_fdinfo_fd < 0)
return -errno;
if (!stream)
return -ENOMEM;
int fd;
if (r < 0)
return r;
fddelim = "\n";
/* Use the directory entry from /proc/[pid]/fd with /proc/[pid]/fdinfo */
if (fd < 0)
continue;
continue;
}
}
}
errno = 0;
if (errno != 0)
return -errno;
return 0;
}
/* The small core field we allocate on the stack, to keep things simple */
char
*core_slice = NULL;
/* The larger ones we allocate on the heap */
_cleanup_free_ char
int r, j = 0;
char *t;
const char *p;
/* 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();
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 command line.");
}
log_warning("Failed to get EXE.");
if (cg_pid_get_unit(pid, &t) >= 0) {
if (streq(t, SPECIAL_JOURNALD_SERVICE)) {
free(t);
/* 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;
}
free(t);
} else if (cg_pid_get_user_unit(pid, &t) >= 0) {
free(t);
}
if (core_unit)
/* OK, now we know it's not the journal, hence we can make use
* of it now. */
log_open();
if (sd_pid_get_session(pid, &t) >= 0) {
free(t);
}
r = asprintf(&core_owner_uid,
if (r > 0)
}
if (sd_pid_get_slice(pid, &t) >= 0) {
free(t);
}
if (comm) {
}
if (exe) {
}
if (get_process_cmdline(pid, 0, false, &t) >= 0) {
free(t);
}
free(t);
}
if (compose_open_fds(pid, &t) >= 0) {
free(t);
if (core_open_fds)
}
if (read_full_file(p, &t, NULL) >= 0) {
free(t);
if (core_proc_status)
}
if (read_full_file(p, &t, NULL) >= 0) {
free(t);
if (core_proc_maps)
}
if (read_full_file(p, &t, NULL) >= 0) {
free(t);
if (core_proc_limits)
}
if (read_full_file(p, &t, NULL) >=0) {
free(t);
if (core_proc_cgroup)
}
if (get_process_cwd(pid, &t) >= 0) {
free(t);
}
if (get_process_root(pid, &t) >= 0) {
free(t);
}
if (get_process_environ(pid, &t) >= 0) {
free(t);
if (core_environ)
}
if (core_timestamp)
/* Vacuum before we write anything again */
/* 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;
if (r == 0) {
const char *coredump_filename;
}
/* Vacuum again, but exclude the coredump we just created */
/* 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. */
if (r < 0) {
log_error_errno(r, "Failed to drop privileges: %m");
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 (r == -EINVAL)
else
log_warning_errno(r, "Failed to generate stack trace: %m");
}
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)
log_error_errno(r, "Failed to log coredump: %m");
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}