X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fjournal%2Fcoredump.c;h=c56d2832682778aa1541030e3cfb6f397188fbc0;hb=34f856cbd013a73ba5d7f05fe6e9ff0233b92f15;hp=6b8809972d5f211ba45e50ede19f2dd882190aa2;hpb=fee80f695687ab3bfb0590e73d6d97a8a19787cd;p=elogind.git diff --git a/src/journal/coredump.c b/src/journal/coredump.c index 6b8809972..c56d28326 100644 --- a/src/journal/coredump.c +++ b/src/journal/coredump.c @@ -6,29 +6,38 @@ Copyright 2012 Lennart Poettering systemd is free software; you can redistribute it and/or modify it - under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or + 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 - General Public License for more details. + Lesser General Public License for more details. - You should have received a copy of the GNU General Public License + You should have received a copy of the GNU Lesser General Public License along with systemd; If not, see . ***/ #include #include +#include +#include #include + +#ifdef HAVE_LOGIND #include +#endif #include "log.h" #include "util.h" +#include "macro.h" +#include "mkdir.h" +#include "special.h" +#include "cgroup-util.h" -#define COREDUMP_MAX (24*1024*1024) +#define COREDUMP_MAX (768*1024*1024) enum { ARG_PID = 1, @@ -40,23 +49,68 @@ enum { _ARG_MAX }; +static int divert_coredump(void) { + _cleanup_fclose_ FILE *f = NULL; + + log_info("Detected coredump of the journal daemon itself, diverting coredump to /var/lib/systemd/coredump/."); + + mkdir_p_label("/var/lib/systemd/coredump", 0755); + + f = fopen("/var/lib/systemd/coredump/core.systemd-journald", "we"); + if (!f) { + log_error("Failed to create coredump file: %m"); + return -errno; + } + + for (;;) { + uint8_t buffer[4096]; + size_t l, q; + + l = fread(buffer, 1, sizeof(buffer), stdin); + if (l <= 0) { + if (ferror(f)) { + log_error("Failed to read coredump: %m"); + return -errno; + } + + break; + } + + q = fwrite(buffer, 1, l, f); + 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 main(int argc, char* argv[]) { int r, j = 0; - char *p = NULL; + char *t; ssize_t n; pid_t pid; uid_t uid; gid_t gid; struct iovec iovec[14]; - char *core_pid = NULL, *core_uid = NULL, *core_gid = NULL, *core_signal = NULL, + char _cleanup_free_ *core_pid = NULL, *core_uid = NULL, *core_gid = NULL, *core_signal = NULL, *core_timestamp = NULL, *core_comm = NULL, *core_exe = NULL, *core_unit = NULL, - *core_session = NULL, *core_message = NULL, *core_cmdline = NULL, *t; + *core_session = NULL, *core_message = NULL, *core_cmdline = NULL, *p = NULL; - log_set_target(LOG_TARGET_JOURNAL_OR_KMSG); - log_parse_environment(); - log_open(); + prctl(PR_SET_DUMPABLE, 0); if (argc != _ARG_MAX) { + log_set_target(LOG_TARGET_JOURNAL_OR_KMSG); + log_open(); + log_error("Invalid number of arguments passed from kernel."); r = -EINVAL; goto finish; @@ -64,10 +118,37 @@ int main(int argc, char* argv[]) { r = parse_pid(argv[ARG_PID], &pid); if (r < 0) { + log_set_target(LOG_TARGET_JOURNAL_OR_KMSG); + 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_set_target(LOG_TARGET_KMSG); + log_open(); + + r = divert_coredump(); + goto finish; + } + + core_unit = strappend("COREDUMP_UNIT=", t); + } else if (cg_pid_get_user_unit(pid, &t) >= 0) + core_unit = strappend("COREDUMP_USER_UNIT=", t); + + if (core_unit) + IOVEC_SET_STRING(iovec[j++], core_unit); + + /* OK, now we know it's not the journal, hence make use of + * it */ + log_set_target(LOG_TARGET_JOURNAL_OR_KMSG); + log_open(); + r = parse_uid(argv[ARG_UID], &uid); if (r < 0) { log_error("Failed to parse UID."); @@ -100,6 +181,7 @@ int main(int argc, char* argv[]) { if (core_comm) IOVEC_SET_STRING(iovec[j++], core_comm); +#ifdef HAVE_LOGIND if (sd_pid_get_session(pid, &t) >= 0) { core_session = strappend("COREDUMP_SESSION=", t); free(t); @@ -108,13 +190,7 @@ int main(int argc, char* argv[]) { IOVEC_SET_STRING(iovec[j++], core_session); } - if (sd_pid_get_unit(pid, &t) >= 0) { - core_unit = strappend("COREDUMP_UNIT=", t); - free(t); - - if (core_unit) - IOVEC_SET_STRING(iovec[j++], core_unit); - } +#endif if (get_process_exe(pid, &t) >= 0) { core_exe = strappend("COREDUMP_EXE=", t); @@ -124,7 +200,7 @@ int main(int argc, char* argv[]) { IOVEC_SET_STRING(iovec[j++], core_exe); } - if (get_process_cmdline(pid, LINE_MAX, false, &t) >= 0) { + if (get_process_cmdline(pid, 0, false, &t) >= 0) { core_cmdline = strappend("COREDUMP_CMDLINE=", t); free(t); @@ -132,14 +208,14 @@ int main(int argc, char* argv[]) { IOVEC_SET_STRING(iovec[j++], core_cmdline); } - core_timestamp = join("COREDUMP_TIMESTAMP=", argv[ARG_TIMESTAMP], "000000", NULL); + core_timestamp = strjoin("COREDUMP_TIMESTAMP=", argv[ARG_TIMESTAMP], "000000", NULL); if (core_timestamp) IOVEC_SET_STRING(iovec[j++], core_timestamp); IOVEC_SET_STRING(iovec[j++], "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1"); IOVEC_SET_STRING(iovec[j++], "PRIORITY=2"); - core_message = join("MESSAGE=Process ", argv[ARG_PID], " (", argv[ARG_COMM], ") dumped core.", NULL); + core_message = strjoin("MESSAGE=Process ", argv[ARG_PID], " (", argv[ARG_COMM], ") dumped core.", NULL); if (core_message) IOVEC_SET_STRING(iovec[j++], core_message); @@ -158,8 +234,7 @@ int main(int argc, char* argv[]) { p = malloc(9 + COREDUMP_MAX); if (!p) { - log_error("Out of memory"); - r = -ENOMEM; + r = log_oom(); goto finish; } @@ -181,18 +256,5 @@ int main(int argc, char* argv[]) { log_error("Failed to send coredump: %s", strerror(-r)); finish: - free(p); - free(core_pid); - free(core_uid); - free(core_gid); - free(core_signal); - free(core_timestamp); - free(core_comm); - free(core_exe); - free(core_cmdline); - free(core_unit); - free(core_session); - free(core_message); - return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; }