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 <sys/prctl.h>
27 #include <systemd/sd-journal.h>
28 #include <systemd/sd-login.h>
34 #define COREDUMP_MAX (24*1024*1024)
46 static int divert_coredump(void) {
50 log_info("Detected coredump of the journal daemon itself, diverting coredump to /var/lib/systemd/coredump/.");
52 mkdir_p("/var/lib/systemd/coredump", 0755);
54 f
= fopen("/var/lib/systemd/coredump/core.systemd-journald", "we");
56 log_error("Failed to create coredump file: %m");
64 l
= fread(buffer
, 1, sizeof(buffer
), stdin
);
67 log_error("Failed to read coredump: %m");
76 q
= fwrite(buffer
, 1, l
, f
);
78 log_error("Failed to write coredump: %m");
87 log_error("Failed to write coredump: %m");
96 int main(int argc
, char* argv
[]) {
103 struct iovec iovec
[14];
104 char *core_pid
= NULL
, *core_uid
= NULL
, *core_gid
= NULL
, *core_signal
= NULL
,
105 *core_timestamp
= NULL
, *core_comm
= NULL
, *core_exe
= NULL
, *core_unit
= NULL
,
106 *core_session
= NULL
, *core_message
= NULL
, *core_cmdline
= NULL
, *t
;
108 prctl(PR_SET_DUMPABLE
, 0);
110 if (argc
!= _ARG_MAX
) {
111 log_set_target(LOG_TARGET_JOURNAL_OR_KMSG
);
114 log_error("Invalid number of arguments passed from kernel.");
119 r
= parse_pid(argv
[ARG_PID
], &pid
);
121 log_set_target(LOG_TARGET_JOURNAL_OR_KMSG
);
124 log_error("Failed to parse PID.");
128 if (sd_pid_get_unit(pid
, &t
) >= 0) {
130 if (streq(t
, SPECIAL_JOURNALD_SERVICE
)) {
131 /* Make sure we don't make use of the journal,
132 * if it's the journal which is crashing */
133 log_set_target(LOG_TARGET_KMSG
);
136 r
= divert_coredump();
140 core_unit
= strappend("COREDUMP_UNIT=", t
);
144 IOVEC_SET_STRING(iovec
[j
++], core_unit
);
147 /* OK, now we know it's not the journal, hence make use of
149 log_set_target(LOG_TARGET_JOURNAL_OR_KMSG
);
152 r
= parse_uid(argv
[ARG_UID
], &uid
);
154 log_error("Failed to parse UID.");
158 r
= parse_gid(argv
[ARG_GID
], &gid
);
160 log_error("Failed to parse GID.");
164 core_pid
= strappend("COREDUMP_PID=", argv
[ARG_PID
]);
166 IOVEC_SET_STRING(iovec
[j
++], core_pid
);
168 core_uid
= strappend("COREDUMP_UID=", argv
[ARG_UID
]);
170 IOVEC_SET_STRING(iovec
[j
++], core_uid
);
172 core_gid
= strappend("COREDUMP_GID=", argv
[ARG_GID
]);
174 IOVEC_SET_STRING(iovec
[j
++], core_gid
);
176 core_signal
= strappend("COREDUMP_SIGNAL=", argv
[ARG_SIGNAL
]);
178 IOVEC_SET_STRING(iovec
[j
++], core_signal
);
180 core_comm
= strappend("COREDUMP_COMM=", argv
[ARG_COMM
]);
182 IOVEC_SET_STRING(iovec
[j
++], core_comm
);
184 if (sd_pid_get_session(pid
, &t
) >= 0) {
185 core_session
= strappend("COREDUMP_SESSION=", t
);
189 IOVEC_SET_STRING(iovec
[j
++], core_session
);
192 if (get_process_exe(pid
, &t
) >= 0) {
193 core_exe
= strappend("COREDUMP_EXE=", t
);
197 IOVEC_SET_STRING(iovec
[j
++], core_exe
);
200 if (get_process_cmdline(pid
, LINE_MAX
, false, &t
) >= 0) {
201 core_cmdline
= strappend("COREDUMP_CMDLINE=", t
);
205 IOVEC_SET_STRING(iovec
[j
++], core_cmdline
);
208 core_timestamp
= join("COREDUMP_TIMESTAMP=", argv
[ARG_TIMESTAMP
], "000000", NULL
);
210 IOVEC_SET_STRING(iovec
[j
++], core_timestamp
);
212 IOVEC_SET_STRING(iovec
[j
++], "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1");
213 IOVEC_SET_STRING(iovec
[j
++], "PRIORITY=2");
215 core_message
= join("MESSAGE=Process ", argv
[ARG_PID
], " (", argv
[ARG_COMM
], ") dumped core.", NULL
);
217 IOVEC_SET_STRING(iovec
[j
++], core_message
);
219 /* Now, let's drop privileges to become the user who owns the
220 * segfaulted process and allocate the coredump memory under
221 * his uid. This also ensures that the credentials journald
222 * will see are the ones of the coredumping user, thus making
223 * sure the user himself gets access to the core dump. */
225 if (setresgid(gid
, gid
, gid
) < 0 ||
226 setresuid(uid
, uid
, uid
) < 0) {
227 log_error("Failed to drop privileges: %m");
232 p
= malloc(9 + COREDUMP_MAX
);
234 log_error("Out of memory");
239 memcpy(p
, "COREDUMP=", 9);
241 n
= loop_read(STDIN_FILENO
, p
+ 9, COREDUMP_MAX
, false);
243 log_error("Failed to read core dump data: %s", strerror(-n
));
248 iovec
[j
].iov_base
= p
;
249 iovec
[j
].iov_len
= 9 + n
;
252 r
= sd_journal_sendv(iovec
, j
);
254 log_error("Failed to send coredump: %s", strerror(-r
));
262 free(core_timestamp
);
270 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;