]>
Commit | Line | Data |
---|---|---|
f5e04665 LP |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2012 Lennart Poettering | |
7 | ||
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. | |
12 | ||
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. | |
17 | ||
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/>. | |
20 | ***/ | |
21 | ||
22 | #include <errno.h> | |
23 | #include <unistd.h> | |
803a3464 LP |
24 | #include <stdio.h> |
25 | #include <sys/prctl.h> | |
f5e04665 LP |
26 | |
27 | #include <systemd/sd-journal.h> | |
28 | #include <systemd/sd-login.h> | |
29 | ||
30 | #include "log.h" | |
31 | #include "util.h" | |
803a3464 | 32 | #include "special.h" |
f5e04665 | 33 | |
de0229ca | 34 | #define COREDUMP_MAX (24*1024*1024) |
f5e04665 LP |
35 | |
36 | enum { | |
37 | ARG_PID = 1, | |
38 | ARG_UID, | |
39 | ARG_GID, | |
40 | ARG_SIGNAL, | |
41 | ARG_TIMESTAMP, | |
42 | ARG_COMM, | |
43 | _ARG_MAX | |
44 | }; | |
45 | ||
803a3464 LP |
46 | static int divert_coredump(void) { |
47 | FILE *f; | |
48 | int r; | |
49 | ||
50 | log_info("Detected coredump of the journal daemon itself, diverting coredump to /var/lib/systemd/coredump/."); | |
51 | ||
52 | mkdir_p("/var/lib/systemd/coredump", 0755); | |
53 | ||
54 | f = fopen("/var/lib/systemd/coredump/core.systemd-journald", "we"); | |
55 | if (!f) { | |
56 | log_error("Failed to create coredump file: %m"); | |
57 | return -errno; | |
58 | } | |
59 | ||
60 | for (;;) { | |
61 | uint8_t buffer[4096]; | |
62 | size_t l, q; | |
63 | ||
64 | l = fread(buffer, 1, sizeof(buffer), stdin); | |
65 | if (l <= 0) { | |
66 | if (ferror(f)) { | |
67 | log_error("Failed to read coredump: %m"); | |
68 | r = -errno; | |
69 | goto finish; | |
70 | } | |
71 | ||
72 | r = 0; | |
73 | break; | |
74 | } | |
75 | ||
76 | q = fwrite(buffer, 1, l, f); | |
77 | if (q != l) { | |
78 | log_error("Failed to write coredump: %m"); | |
79 | r = -errno; | |
80 | goto finish; | |
81 | } | |
82 | } | |
83 | ||
84 | fflush(f); | |
85 | ||
86 | if (ferror(f)) { | |
87 | log_error("Failed to write coredump: %m"); | |
88 | r = -errno; | |
89 | } | |
90 | ||
91 | finish: | |
92 | fclose(f); | |
93 | return r; | |
94 | } | |
95 | ||
f5e04665 LP |
96 | int main(int argc, char* argv[]) { |
97 | int r, j = 0; | |
98 | char *p = NULL; | |
99 | ssize_t n; | |
100 | pid_t pid; | |
fee80f69 LP |
101 | uid_t uid; |
102 | gid_t gid; | |
f5e04665 LP |
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; | |
107 | ||
803a3464 | 108 | prctl(PR_SET_DUMPABLE, 0); |
f5e04665 LP |
109 | |
110 | if (argc != _ARG_MAX) { | |
803a3464 LP |
111 | log_set_target(LOG_TARGET_JOURNAL_OR_KMSG); |
112 | log_open(); | |
113 | ||
f5e04665 LP |
114 | log_error("Invalid number of arguments passed from kernel."); |
115 | r = -EINVAL; | |
116 | goto finish; | |
117 | } | |
118 | ||
119 | r = parse_pid(argv[ARG_PID], &pid); | |
120 | if (r < 0) { | |
803a3464 LP |
121 | log_set_target(LOG_TARGET_JOURNAL_OR_KMSG); |
122 | log_open(); | |
123 | ||
f5e04665 | 124 | log_error("Failed to parse PID."); |
f5e04665 LP |
125 | goto finish; |
126 | } | |
127 | ||
803a3464 LP |
128 | if (sd_pid_get_unit(pid, &t) >= 0) { |
129 | ||
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); | |
134 | log_open(); | |
135 | ||
136 | r = divert_coredump(); | |
137 | goto finish; | |
138 | } | |
139 | ||
140 | core_unit = strappend("COREDUMP_UNIT=", t); | |
141 | free(t); | |
142 | ||
143 | if (core_unit) | |
144 | IOVEC_SET_STRING(iovec[j++], core_unit); | |
145 | } | |
146 | ||
147 | /* OK, now we know it's not the journal, hence make use of | |
148 | * it */ | |
149 | log_set_target(LOG_TARGET_JOURNAL_OR_KMSG); | |
150 | log_open(); | |
151 | ||
fee80f69 LP |
152 | r = parse_uid(argv[ARG_UID], &uid); |
153 | if (r < 0) { | |
154 | log_error("Failed to parse UID."); | |
f5e04665 LP |
155 | goto finish; |
156 | } | |
157 | ||
fee80f69 LP |
158 | r = parse_gid(argv[ARG_GID], &gid); |
159 | if (r < 0) { | |
160 | log_error("Failed to parse GID."); | |
f5e04665 LP |
161 | goto finish; |
162 | } | |
163 | ||
f5e04665 LP |
164 | core_pid = strappend("COREDUMP_PID=", argv[ARG_PID]); |
165 | if (core_pid) | |
166 | IOVEC_SET_STRING(iovec[j++], core_pid); | |
167 | ||
168 | core_uid = strappend("COREDUMP_UID=", argv[ARG_UID]); | |
169 | if (core_uid) | |
170 | IOVEC_SET_STRING(iovec[j++], core_uid); | |
171 | ||
172 | core_gid = strappend("COREDUMP_GID=", argv[ARG_GID]); | |
173 | if (core_gid) | |
174 | IOVEC_SET_STRING(iovec[j++], core_gid); | |
175 | ||
176 | core_signal = strappend("COREDUMP_SIGNAL=", argv[ARG_SIGNAL]); | |
177 | if (core_signal) | |
178 | IOVEC_SET_STRING(iovec[j++], core_signal); | |
179 | ||
180 | core_comm = strappend("COREDUMP_COMM=", argv[ARG_COMM]); | |
181 | if (core_comm) | |
182 | IOVEC_SET_STRING(iovec[j++], core_comm); | |
183 | ||
184 | if (sd_pid_get_session(pid, &t) >= 0) { | |
185 | core_session = strappend("COREDUMP_SESSION=", t); | |
186 | free(t); | |
187 | ||
188 | if (core_session) | |
189 | IOVEC_SET_STRING(iovec[j++], core_session); | |
190 | } | |
191 | ||
f5e04665 LP |
192 | if (get_process_exe(pid, &t) >= 0) { |
193 | core_exe = strappend("COREDUMP_EXE=", t); | |
194 | free(t); | |
195 | ||
196 | if (core_exe) | |
197 | IOVEC_SET_STRING(iovec[j++], core_exe); | |
198 | } | |
199 | ||
200 | if (get_process_cmdline(pid, LINE_MAX, false, &t) >= 0) { | |
201 | core_cmdline = strappend("COREDUMP_CMDLINE=", t); | |
202 | free(t); | |
203 | ||
204 | if (core_cmdline) | |
205 | IOVEC_SET_STRING(iovec[j++], core_cmdline); | |
206 | } | |
207 | ||
208 | core_timestamp = join("COREDUMP_TIMESTAMP=", argv[ARG_TIMESTAMP], "000000", NULL); | |
209 | if (core_timestamp) | |
210 | IOVEC_SET_STRING(iovec[j++], core_timestamp); | |
211 | ||
212 | IOVEC_SET_STRING(iovec[j++], "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1"); | |
213 | IOVEC_SET_STRING(iovec[j++], "PRIORITY=2"); | |
214 | ||
215 | core_message = join("MESSAGE=Process ", argv[ARG_PID], " (", argv[ARG_COMM], ") dumped core.", NULL); | |
216 | if (core_message) | |
217 | IOVEC_SET_STRING(iovec[j++], core_message); | |
218 | ||
fee80f69 LP |
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. */ | |
224 | ||
225 | if (setresgid(gid, gid, gid) < 0 || | |
226 | setresuid(uid, uid, uid) < 0) { | |
227 | log_error("Failed to drop privileges: %m"); | |
228 | r = -errno; | |
229 | goto finish; | |
230 | } | |
231 | ||
232 | p = malloc(9 + COREDUMP_MAX); | |
233 | if (!p) { | |
234 | log_error("Out of memory"); | |
235 | r = -ENOMEM; | |
236 | goto finish; | |
237 | } | |
238 | ||
239 | memcpy(p, "COREDUMP=", 9); | |
240 | ||
241 | n = loop_read(STDIN_FILENO, p + 9, COREDUMP_MAX, false); | |
242 | if (n < 0) { | |
243 | log_error("Failed to read core dump data: %s", strerror(-n)); | |
244 | r = (int) n; | |
245 | goto finish; | |
246 | } | |
247 | ||
248 | iovec[j].iov_base = p; | |
249 | iovec[j].iov_len = 9 + n; | |
250 | j++; | |
251 | ||
f5e04665 LP |
252 | r = sd_journal_sendv(iovec, j); |
253 | if (r < 0) | |
254 | log_error("Failed to send coredump: %s", strerror(-r)); | |
255 | ||
256 | finish: | |
257 | free(p); | |
258 | free(core_pid); | |
259 | free(core_uid); | |
260 | free(core_gid); | |
261 | free(core_signal); | |
262 | free(core_timestamp); | |
263 | free(core_comm); | |
264 | free(core_exe); | |
265 | free(core_cmdline); | |
266 | free(core_unit); | |
267 | free(core_session); | |
268 | free(core_message); | |
269 | ||
270 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
271 | } |