coredump.c revision 49e942b2bc9fdedba79cd266a076ce9c9d91fc13
/*-*- 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 General Public License as published by
the Free Software Foundation; either version 2 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
General Public License for more details.
You should have received a copy of the GNU 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 "mkdir.h"
#include "special.h"
enum {
ARG_PID = 1,
};
static int divert_coredump(void) {
FILE *f;
int r;
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");
r = -errno;
goto finish;
}
r = 0;
break;
}
if (q != l) {
log_error("Failed to write coredump: %m");
r = -errno;
goto finish;
}
}
fflush(f);
if (ferror(f)) {
log_error("Failed to write coredump: %m");
r = -errno;
}
fclose(f);
return r;
}
int r, j = 0;
char *p = NULL;
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 (sd_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;
}
free(t);
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)
if (sd_pid_get_session(pid, &t) >= 0) {
free(t);
if (core_session)
}
if (get_process_exe(pid, &t) >= 0) {
free(t);
if (core_exe)
}
free(t);
if (core_cmdline)
}
if (core_timestamp)
core_message = join("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 (!p) {
log_error("Out of memory");
r = -ENOMEM;
goto finish;
}
if (n < 0) {
r = (int) n;
goto finish;
}
j++;
r = sd_journal_sendv(iovec, j);
if (r < 0)
free(p);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}