coredump.c revision d18d46ecea80a7f07415edb9264af6a254fd70bb
/*-*- 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>
#ifdef HAVE_LOGIND
#include <systemd/sd-login.h>
#endif
#include "log.h"
#include "util.h"
#include "macro.h"
#include "mkdir.h"
#include "special.h"
#include "cgroup-util.h"
#include "journald-native.h"
/* Few programs have less than 3MiB resident */
/* 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,
};
static int divert_coredump(void) {
log_info("Detected coredump of the journal daemon itself, diverting coredump to /var/lib/systemd/coredump/.");
if (!f) {
log_error("Failed to create coredump file: %m");
return -errno;
}
for (;;) {
size_t l, q;
if (l <= 0) {
if (ferror(f)) {
log_error("Failed to read coredump: %m");
return -errno;
}
break;
}
if (q != l) {
log_error("Failed to write coredump: %m");
return -errno;
}
}
fflush(f);
if (ferror(f)) {
log_error("Failed to write coredump: %m");
return -errno;
}
return 0;
}
int r, j = 0;
char *t;
ssize_t n;
prctl(PR_SET_DUMPABLE, 0);
log_open();
log_error("Invalid number of arguments passed from kernel.");
r = -EINVAL;
goto finish;
}
if (r < 0) {
log_open();
log_error("Failed to parse PID.");
goto finish;
}
if (cg_pid_get_unit(pid, &t) >= 0) {
if (streq(t, SPECIAL_JOURNALD_SERVICE)) {
/* Make sure we don't make use of the journal,
* if it's the journal which is crashing */
log_open();
r = divert_coredump();
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 make use of
* it */
log_open();
if (r < 0) {
log_error("Failed to parse UID.");
goto finish;
}
if (r < 0) {
log_error("Failed to parse GID.");
goto finish;
}
if (core_pid)
if (core_uid)
if (core_gid)
if (core_signal)
if (core_comm)
#ifdef HAVE_LOGIND
if (sd_pid_get_session(pid, &t) >= 0) {
free(t);
if (core_session)
}
#endif
if (get_process_exe(pid, &t) >= 0) {
free(t);
if (core_exe)
}
if (get_process_cmdline(pid, 0, false, &t) >= 0) {
free(t);
if (core_cmdline)
}
if (core_timestamp)
core_message = strjoin("MESSAGE=Process ", argv[ARG_PID], " (", argv[ARG_COMM], ") dumped core.", NULL);
if (core_message)
/* 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;
}
if (!coredump_data) {
log_warning("Failed to allocate memory for core, core will not be stored.");
goto finalize;
}
coredump_size = 9;
for (;;) {
coredump_bufsize - coredump_size, false);
if (n < 0) {
r = (int) n;
goto finish;
} else if (n == 0)
break;
coredump_size += n;
if (coredump_size > COREDUMP_MAX) {
log_error("Core too large, core will not be stored.");
goto finalize;
}
log_warning("Failed to allocate memory for core, core will not be stored.");
goto finalize;
}
}
j++;
r = sd_journal_sendv(iovec, j);
if (r < 0)
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}