1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2012 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include <systemd/sd-journal.h>
26 #include <systemd/sd-login.h>
31 #define COREDUMP_MAX (24*1024*1024)
43 int main(int argc, char* argv[]) {
48 struct iovec iovec[14];
49 char *core_pid = NULL, *core_uid = NULL, *core_gid = NULL, *core_signal = NULL,
50 *core_timestamp = NULL, *core_comm = NULL, *core_exe = NULL, *core_unit = NULL,
51 *core_session = NULL, *core_message = NULL, *core_cmdline = NULL, *t;
53 log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
54 log_parse_environment();
57 if (argc != _ARG_MAX) {
58 log_error("Invalid number of arguments passed from kernel.");
63 r = parse_pid(argv[ARG_PID], &pid);
65 log_error("Failed to parse PID.");
70 p = malloc(9 + COREDUMP_MAX);
72 log_error("Out of memory");
77 memcpy(p, "COREDUMP=", 9);
79 n = loop_read(STDIN_FILENO, p + 9, COREDUMP_MAX, false);
81 log_error("Failed to read core dump data: %s", strerror(-n));
87 iovec[j].iov_base = p;
88 iovec[j].iov_len = 9 + n;
91 core_pid = strappend("COREDUMP_PID=", argv[ARG_PID]);
93 IOVEC_SET_STRING(iovec[j++], core_pid);
95 core_uid = strappend("COREDUMP_UID=", argv[ARG_UID]);
97 IOVEC_SET_STRING(iovec[j++], core_uid);
99 core_gid = strappend("COREDUMP_GID=", argv[ARG_GID]);
101 IOVEC_SET_STRING(iovec[j++], core_gid);
103 core_signal = strappend("COREDUMP_SIGNAL=", argv[ARG_SIGNAL]);
105 IOVEC_SET_STRING(iovec[j++], core_signal);
107 core_comm = strappend("COREDUMP_COMM=", argv[ARG_COMM]);
109 IOVEC_SET_STRING(iovec[j++], core_comm);
111 if (sd_pid_get_session(pid, &t) >= 0) {
112 core_session = strappend("COREDUMP_SESSION=", t);
116 IOVEC_SET_STRING(iovec[j++], core_session);
119 if (sd_pid_get_unit(pid, &t) >= 0) {
120 core_unit = strappend("COREDUMP_UNIT=", t);
124 IOVEC_SET_STRING(iovec[j++], core_unit);
127 if (get_process_exe(pid, &t) >= 0) {
128 core_exe = strappend("COREDUMP_EXE=", t);
132 IOVEC_SET_STRING(iovec[j++], core_exe);
135 if (get_process_cmdline(pid, LINE_MAX, false, &t) >= 0) {
136 core_cmdline = strappend("COREDUMP_CMDLINE=", t);
140 IOVEC_SET_STRING(iovec[j++], core_cmdline);
143 core_timestamp = join("COREDUMP_TIMESTAMP=", argv[ARG_TIMESTAMP], "000000", NULL);
145 IOVEC_SET_STRING(iovec[j++], core_timestamp);
147 IOVEC_SET_STRING(iovec[j++], "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1");
148 IOVEC_SET_STRING(iovec[j++], "PRIORITY=2");
150 core_message = join("MESSAGE=Process ", argv[ARG_PID], " (", argv[ARG_COMM], ") dumped core.", NULL);
152 IOVEC_SET_STRING(iovec[j++], core_message);
154 r = sd_journal_sendv(iovec, j);
156 log_error("Failed to send coredump: %s", strerror(-r));
164 free(core_timestamp);
172 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;