]>
Commit | Line | Data |
---|---|---|
f5e04665 LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2012 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
7 | under the terms of the GNU Lesser General Public License as published by |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
f5e04665 LP |
9 | (at your option) any later version. |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 14 | Lesser General Public License for more details. |
f5e04665 | 15 | |
5430f7f2 | 16 | You should have received a copy of the GNU Lesser General Public License |
f5e04665 LP |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
18 | ***/ | |
19 | ||
20 | #include <errno.h> | |
803a3464 LP |
21 | #include <stdio.h> |
22 | #include <sys/prctl.h> | |
cacd6403 | 23 | #include <sys/xattr.h> |
4f5dd394 | 24 | #include <unistd.h> |
f5e04665 | 25 | |
4d229b31 | 26 | #ifdef HAVE_ELFUTILS |
3c171f0b LP |
27 | #include <dwarf.h> |
28 | #include <elfutils/libdwfl.h> | |
4d229b31 UTL |
29 | #endif |
30 | ||
f11943c5 LP |
31 | #include "sd-journal.h" |
32 | #include "sd-login.h" | |
3c171f0b | 33 | #include "sd-daemon.h" |
4f5dd394 LP |
34 | |
35 | #include "acl-util.h" | |
b5efdb8a | 36 | #include "alloc-util.h" |
430f0182 | 37 | #include "capability-util.h" |
ba1261bc | 38 | #include "cgroup-util.h" |
4f5dd394 | 39 | #include "compress.h" |
34c10968 LP |
40 | #include "conf-parser.h" |
41 | #include "copy.h" | |
f11943c5 | 42 | #include "coredump-vacuum.h" |
a0956174 | 43 | #include "dirent-util.h" |
4f5dd394 | 44 | #include "escape.h" |
3ffd4af2 | 45 | #include "fd-util.h" |
4f5dd394 | 46 | #include "fileio.h" |
f4f15635 | 47 | #include "fs-util.h" |
afc5dbf3 | 48 | #include "io-util.h" |
4f5dd394 LP |
49 | #include "journald-native.h" |
50 | #include "log.h" | |
51 | #include "macro.h" | |
0c773903 | 52 | #include "missing.h" |
4f5dd394 | 53 | #include "mkdir.h" |
6bedfcbb | 54 | #include "parse-util.h" |
0b452006 | 55 | #include "process-util.h" |
3c171f0b | 56 | #include "socket-util.h" |
4f5dd394 LP |
57 | #include "special.h" |
58 | #include "stacktrace.h" | |
8b43440b | 59 | #include "string-table.h" |
07630cea | 60 | #include "string-util.h" |
4f5dd394 | 61 | #include "strv.h" |
b1d4f8e1 | 62 | #include "user-util.h" |
4f5dd394 | 63 | #include "util.h" |
34727273 | 64 | |
34c10968 | 65 | /* The maximum size up to which we process coredumps */ |
59f448cf | 66 | #define PROCESS_SIZE_MAX ((uint64_t) (2LLU*1024LLU*1024LLU*1024LLU)) |
34c10968 | 67 | |
bdfd7b2c | 68 | /* The maximum size up to which we leave the coredump around on disk */ |
34c10968 LP |
69 | #define EXTERNAL_SIZE_MAX PROCESS_SIZE_MAX |
70 | ||
bdfd7b2c | 71 | /* The maximum size up to which we store the coredump in the journal */ |
34c10968 | 72 | #define JOURNAL_SIZE_MAX ((size_t) (767LU*1024LU*1024LU)) |
f5e04665 | 73 | |
c4aa09b0 | 74 | /* Make sure to not make this larger than the maximum journal entry |
874bc134 ZJS |
75 | * size. See DATA_SIZE_MAX in journald-native.c. */ |
76 | assert_cc(JOURNAL_SIZE_MAX <= DATA_SIZE_MAX); | |
f5e04665 LP |
77 | |
78 | enum { | |
3c171f0b LP |
79 | /* We use this as array indexes for a couple of special fields we use for naming coredumping files, and |
80 | * attaching xattrs */ | |
81 | CONTEXT_PID, | |
82 | CONTEXT_UID, | |
83 | CONTEXT_GID, | |
84 | CONTEXT_SIGNAL, | |
85 | CONTEXT_TIMESTAMP, | |
bdfd7b2c | 86 | CONTEXT_RLIMIT, |
3c171f0b LP |
87 | CONTEXT_COMM, |
88 | CONTEXT_EXE, | |
89 | _CONTEXT_MAX | |
f5e04665 LP |
90 | }; |
91 | ||
34c10968 LP |
92 | typedef enum CoredumpStorage { |
93 | COREDUMP_STORAGE_NONE, | |
94 | COREDUMP_STORAGE_EXTERNAL, | |
95 | COREDUMP_STORAGE_JOURNAL, | |
96 | COREDUMP_STORAGE_BOTH, | |
97 | _COREDUMP_STORAGE_MAX, | |
98 | _COREDUMP_STORAGE_INVALID = -1 | |
99 | } CoredumpStorage; | |
100 | ||
34c10968 LP |
101 | static const char* const coredump_storage_table[_COREDUMP_STORAGE_MAX] = { |
102 | [COREDUMP_STORAGE_NONE] = "none", | |
103 | [COREDUMP_STORAGE_EXTERNAL] = "external", | |
104 | [COREDUMP_STORAGE_JOURNAL] = "journal", | |
105 | [COREDUMP_STORAGE_BOTH] = "both", | |
106 | }; | |
107 | ||
108 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP(coredump_storage, CoredumpStorage); | |
8c9571d0 | 109 | static DEFINE_CONFIG_PARSE_ENUM(config_parse_coredump_storage, coredump_storage, CoredumpStorage, "Failed to parse storage setting"); |
34727273 ZJS |
110 | |
111 | static CoredumpStorage arg_storage = COREDUMP_STORAGE_EXTERNAL; | |
8c9571d0 | 112 | static bool arg_compress = true; |
59f448cf LP |
113 | static uint64_t arg_process_size_max = PROCESS_SIZE_MAX; |
114 | static uint64_t arg_external_size_max = EXTERNAL_SIZE_MAX; | |
34727273 | 115 | static size_t arg_journal_size_max = JOURNAL_SIZE_MAX; |
59f448cf LP |
116 | static uint64_t arg_keep_free = (uint64_t) -1; |
117 | static uint64_t arg_max_use = (uint64_t) -1; | |
34c10968 LP |
118 | |
119 | static int parse_config(void) { | |
34c10968 | 120 | static const ConfigTableItem items[] = { |
8c9571d0 LP |
121 | { "Coredump", "Storage", config_parse_coredump_storage, 0, &arg_storage }, |
122 | { "Coredump", "Compress", config_parse_bool, 0, &arg_compress }, | |
59f448cf LP |
123 | { "Coredump", "ProcessSizeMax", config_parse_iec_uint64, 0, &arg_process_size_max }, |
124 | { "Coredump", "ExternalSizeMax", config_parse_iec_uint64, 0, &arg_external_size_max }, | |
8c9571d0 | 125 | { "Coredump", "JournalSizeMax", config_parse_iec_size, 0, &arg_journal_size_max }, |
59f448cf LP |
126 | { "Coredump", "KeepFree", config_parse_iec_uint64, 0, &arg_keep_free }, |
127 | { "Coredump", "MaxUse", config_parse_iec_uint64, 0, &arg_max_use }, | |
34c10968 LP |
128 | {} |
129 | }; | |
130 | ||
75eb6154 LP |
131 | return config_parse_many(PKGSYSCONFDIR "/coredump.conf", |
132 | CONF_PATHS_NULSTR("systemd/coredump.conf.d"), | |
301af7e4 JT |
133 | "Coredump\0", |
134 | config_item_table_lookup, items, | |
135 | false, NULL); | |
34c10968 LP |
136 | } |
137 | ||
138 | static int fix_acl(int fd, uid_t uid) { | |
139 | ||
140 | #ifdef HAVE_ACL | |
141 | _cleanup_(acl_freep) acl_t acl = NULL; | |
142 | acl_entry_t entry; | |
143 | acl_permset_t permset; | |
709f6e46 | 144 | int r; |
34c10968 | 145 | |
b59233e6 LP |
146 | assert(fd >= 0); |
147 | ||
34c10968 LP |
148 | if (uid <= SYSTEM_UID_MAX) |
149 | return 0; | |
150 | ||
151 | /* Make sure normal users can read (but not write or delete) | |
152 | * their own coredumps */ | |
153 | ||
154 | acl = acl_get_fd(fd); | |
4a62c710 MS |
155 | if (!acl) |
156 | return log_error_errno(errno, "Failed to get ACL: %m"); | |
34c10968 LP |
157 | |
158 | if (acl_create_entry(&acl, &entry) < 0 || | |
159 | acl_set_tag_type(entry, ACL_USER) < 0 || | |
160 | acl_set_qualifier(entry, &uid) < 0) { | |
56f64d95 | 161 | log_error_errno(errno, "Failed to patch ACL: %m"); |
34c10968 LP |
162 | return -errno; |
163 | } | |
164 | ||
165 | if (acl_get_permset(entry, &permset) < 0 || | |
709f6e46 MS |
166 | acl_add_perm(permset, ACL_READ) < 0) |
167 | return log_warning_errno(errno, "Failed to patch ACL: %m"); | |
168 | ||
169 | r = calc_acl_mask_if_needed(&acl); | |
170 | if (r < 0) | |
171 | return log_warning_errno(r, "Failed to patch ACL: %m"); | |
34c10968 | 172 | |
4a62c710 MS |
173 | if (acl_set_fd(fd, acl) < 0) |
174 | return log_error_errno(errno, "Failed to apply ACL: %m"); | |
34c10968 LP |
175 | #endif |
176 | ||
177 | return 0; | |
178 | } | |
179 | ||
3c171f0b | 180 | static int fix_xattr(int fd, const char *context[_CONTEXT_MAX]) { |
0cd77f97 | 181 | |
3c171f0b LP |
182 | static const char * const xattrs[_CONTEXT_MAX] = { |
183 | [CONTEXT_PID] = "user.coredump.pid", | |
184 | [CONTEXT_UID] = "user.coredump.uid", | |
185 | [CONTEXT_GID] = "user.coredump.gid", | |
186 | [CONTEXT_SIGNAL] = "user.coredump.signal", | |
187 | [CONTEXT_TIMESTAMP] = "user.coredump.timestamp", | |
188 | [CONTEXT_COMM] = "user.coredump.comm", | |
189 | [CONTEXT_EXE] = "user.coredump.exe", | |
0cd77f97 LP |
190 | }; |
191 | ||
34c10968 | 192 | int r = 0; |
0cd77f97 | 193 | unsigned i; |
34c10968 | 194 | |
b59233e6 LP |
195 | assert(fd >= 0); |
196 | ||
1eef15b1 | 197 | /* Attach some metadata to coredumps via extended |
34c10968 LP |
198 | * attributes. Just because we can. */ |
199 | ||
3c171f0b | 200 | for (i = 0; i < _CONTEXT_MAX; i++) { |
1eef15b1 ZJS |
201 | int k; |
202 | ||
3c171f0b | 203 | if (isempty(context[i]) || !xattrs[i]) |
0cd77f97 | 204 | continue; |
34c10968 | 205 | |
3c171f0b | 206 | k = fsetxattr(fd, xattrs[i], context[i], strlen(context[i]), XATTR_CREATE); |
1eef15b1 | 207 | if (k < 0 && r == 0) |
34c10968 | 208 | r = -errno; |
0cd77f97 | 209 | } |
34c10968 LP |
210 | |
211 | return r; | |
212 | } | |
213 | ||
b0b21dce | 214 | #define filename_escape(s) xescape((s), "./ ") |
34c10968 | 215 | |
0c773903 EV |
216 | static inline const char *coredump_tmpfile_name(const char *s) { |
217 | return s ? s : "(unnamed temporary file)"; | |
218 | } | |
219 | ||
b59233e6 LP |
220 | static int fix_permissions( |
221 | int fd, | |
222 | const char *filename, | |
223 | const char *target, | |
3c171f0b | 224 | const char *context[_CONTEXT_MAX], |
b59233e6 LP |
225 | uid_t uid) { |
226 | ||
03532f0a LP |
227 | int r; |
228 | ||
b59233e6 | 229 | assert(fd >= 0); |
b59233e6 | 230 | assert(target); |
3c171f0b | 231 | assert(context); |
cfd652ed ZJS |
232 | |
233 | /* Ignore errors on these */ | |
3c171f0b LP |
234 | (void) fchmod(fd, 0640); |
235 | (void) fix_acl(fd, uid); | |
236 | (void) fix_xattr(fd, context); | |
cfd652ed | 237 | |
4a62c710 | 238 | if (fsync(fd) < 0) |
0c773903 | 239 | return log_error_errno(errno, "Failed to sync coredump %s: %m", coredump_tmpfile_name(filename)); |
cfd652ed | 240 | |
03532f0a LP |
241 | r = link_tmpfile(fd, filename, target); |
242 | if (r < 0) | |
243 | return log_error_errno(r, "Failed to move coredump %s into place: %m", target); | |
cfd652ed ZJS |
244 | |
245 | return 0; | |
246 | } | |
247 | ||
59f448cf | 248 | static int maybe_remove_external_coredump(const char *filename, uint64_t size) { |
cfd652ed | 249 | |
b59233e6 | 250 | /* Returns 1 if might remove, 0 if will not remove, < 0 on error. */ |
cfd652ed ZJS |
251 | |
252 | if (IN_SET(arg_storage, COREDUMP_STORAGE_EXTERNAL, COREDUMP_STORAGE_BOTH) && | |
253 | size <= arg_external_size_max) | |
254 | return 0; | |
255 | ||
256 | if (!filename) | |
257 | return 1; | |
258 | ||
4a62c710 MS |
259 | if (unlink(filename) < 0 && errno != ENOENT) |
260 | return log_error_errno(errno, "Failed to unlink %s: %m", filename); | |
cfd652ed ZJS |
261 | |
262 | return 1; | |
263 | } | |
264 | ||
3c171f0b | 265 | static int make_filename(const char *context[_CONTEXT_MAX], char **ret) { |
b59233e6 | 266 | _cleanup_free_ char *c = NULL, *u = NULL, *p = NULL, *t = NULL; |
a7f7d1bd | 267 | sd_id128_t boot = {}; |
34c10968 LP |
268 | int r; |
269 | ||
3c171f0b | 270 | assert(context); |
34c10968 | 271 | |
3c171f0b | 272 | c = filename_escape(context[CONTEXT_COMM]); |
34c10968 | 273 | if (!c) |
b59233e6 | 274 | return -ENOMEM; |
34c10968 | 275 | |
3c171f0b | 276 | u = filename_escape(context[CONTEXT_UID]); |
0dc5d23c | 277 | if (!u) |
b59233e6 | 278 | return -ENOMEM; |
34c10968 LP |
279 | |
280 | r = sd_id128_get_boot(&boot); | |
b59233e6 | 281 | if (r < 0) |
34c10968 | 282 | return r; |
34c10968 | 283 | |
3c171f0b | 284 | p = filename_escape(context[CONTEXT_PID]); |
b59233e6 LP |
285 | if (!p) |
286 | return -ENOMEM; | |
287 | ||
3c171f0b | 288 | t = filename_escape(context[CONTEXT_TIMESTAMP]); |
b59233e6 LP |
289 | if (!t) |
290 | return -ENOMEM; | |
291 | ||
292 | if (asprintf(ret, | |
0dc5d23c | 293 | "/var/lib/systemd/coredump/core.%s.%s." SD_ID128_FORMAT_STR ".%s.%s000000", |
34c10968 | 294 | c, |
0dc5d23c | 295 | u, |
34c10968 LP |
296 | SD_ID128_FORMAT_VAL(boot), |
297 | p, | |
b59233e6 LP |
298 | t) < 0) |
299 | return -ENOMEM; | |
300 | ||
301 | return 0; | |
302 | } | |
303 | ||
304 | static int save_external_coredump( | |
3c171f0b LP |
305 | const char *context[_CONTEXT_MAX], |
306 | int input_fd, | |
b59233e6 | 307 | char **ret_filename, |
5f3e0a74 HW |
308 | int *ret_node_fd, |
309 | int *ret_data_fd, | |
59f448cf | 310 | uint64_t *ret_size) { |
b59233e6 LP |
311 | |
312 | _cleanup_free_ char *fn = NULL, *tmp = NULL; | |
313 | _cleanup_close_ int fd = -1; | |
bdfd7b2c | 314 | uint64_t rlimit, max_size; |
b59233e6 | 315 | struct stat st; |
3c171f0b | 316 | uid_t uid; |
b59233e6 LP |
317 | int r; |
318 | ||
3c171f0b | 319 | assert(context); |
b59233e6 | 320 | assert(ret_filename); |
5f3e0a74 HW |
321 | assert(ret_node_fd); |
322 | assert(ret_data_fd); | |
b59233e6 LP |
323 | assert(ret_size); |
324 | ||
3c171f0b LP |
325 | r = parse_uid(context[CONTEXT_UID], &uid); |
326 | if (r < 0) | |
327 | return log_error_errno(r, "Failed to parse UID: %m"); | |
328 | ||
bdfd7b2c LP |
329 | r = safe_atou64(context[CONTEXT_RLIMIT], &rlimit); |
330 | if (r < 0) | |
331 | return log_error_errno(r, "Failed to parse resource limit: %s", context[CONTEXT_RLIMIT]); | |
332 | if (rlimit <= 0) { | |
333 | /* Is coredumping disabled? Then don't bother saving/processing the coredump */ | |
334 | log_info("Core Dumping has been disabled for process %s (%s).", context[CONTEXT_PID], context[CONTEXT_COMM]); | |
335 | return -EBADSLT; | |
336 | } | |
337 | ||
338 | /* Never store more than the process configured, or than we actually shall keep or process */ | |
339 | max_size = MIN(rlimit, MAX(arg_process_size_max, arg_external_size_max)); | |
340 | ||
3c171f0b | 341 | r = make_filename(context, &fn); |
23bbb0de MS |
342 | if (r < 0) |
343 | return log_error_errno(r, "Failed to determine coredump file name: %m"); | |
34c10968 | 344 | |
d2e54fae | 345 | mkdir_p_label("/var/lib/systemd/coredump", 0755); |
803a3464 | 346 | |
03532f0a | 347 | fd = open_tmpfile_linkable(fn, O_RDWR|O_CLOEXEC, &tmp); |
4a62c710 | 348 | if (fd < 0) |
03532f0a | 349 | return log_error_errno(fd, "Failed to create temporary file for coredump %s: %m", fn); |
803a3464 | 350 | |
bdfd7b2c | 351 | r = copy_bytes(input_fd, fd, max_size, false); |
84ee0960 | 352 | if (r == -EFBIG) { |
3c171f0b | 353 | log_error("Coredump of %s (%s) is larger than configured processing limit, refusing.", context[CONTEXT_PID], context[CONTEXT_COMM]); |
93240d3a LP |
354 | goto fail; |
355 | } else if (IN_SET(r, -EDQUOT, -ENOSPC)) { | |
3c171f0b | 356 | log_error("Not enough disk space for coredump of %s (%s), refusing.", context[CONTEXT_PID], context[CONTEXT_COMM]); |
93240d3a LP |
357 | goto fail; |
358 | } else if (r < 0) { | |
da927ba9 | 359 | log_error_errno(r, "Failed to dump coredump to file: %m"); |
34c10968 LP |
360 | goto fail; |
361 | } | |
803a3464 | 362 | |
34c10968 | 363 | if (fstat(fd, &st) < 0) { |
0c773903 | 364 | log_error_errno(errno, "Failed to fstat coredump %s: %m", coredump_tmpfile_name(tmp)); |
34c10968 LP |
365 | goto fail; |
366 | } | |
367 | ||
7849c2ac | 368 | if (lseek(fd, 0, SEEK_SET) == (off_t) -1) { |
0c773903 | 369 | log_error_errno(errno, "Failed to seek on %s: %m", coredump_tmpfile_name(tmp)); |
b59233e6 | 370 | goto fail; |
7849c2ac TA |
371 | } |
372 | ||
d89c8fdf | 373 | #if defined(HAVE_XZ) || defined(HAVE_LZ4) |
cfd652ed | 374 | /* If we will remove the coredump anyway, do not compress. */ |
34727273 | 375 | if (maybe_remove_external_coredump(NULL, st.st_size) == 0 |
8c9571d0 | 376 | && arg_compress) { |
cfd652ed | 377 | |
b59233e6 LP |
378 | _cleanup_free_ char *fn_compressed = NULL, *tmp_compressed = NULL; |
379 | _cleanup_close_ int fd_compressed = -1; | |
cfd652ed | 380 | |
d89c8fdf | 381 | fn_compressed = strappend(fn, COMPRESSED_EXT); |
b59233e6 | 382 | if (!fn_compressed) { |
d89c8fdf | 383 | log_oom(); |
cfd652ed ZJS |
384 | goto uncompressed; |
385 | } | |
386 | ||
03532f0a LP |
387 | fd_compressed = open_tmpfile_linkable(fn_compressed, O_RDWR|O_CLOEXEC, &tmp_compressed); |
388 | if (fd_compressed < 0) { | |
389 | log_error_errno(fd_compressed, "Failed to create temporary file for coredump %s: %m", fn_compressed); | |
b59233e6 | 390 | goto uncompressed; |
03532f0a | 391 | } |
cfd652ed | 392 | |
d89c8fdf | 393 | r = compress_stream(fd, fd_compressed, -1); |
b59233e6 | 394 | if (r < 0) { |
0c773903 | 395 | log_error_errno(r, "Failed to compress %s: %m", coredump_tmpfile_name(tmp_compressed)); |
b59233e6 LP |
396 | goto fail_compressed; |
397 | } | |
398 | ||
3c171f0b | 399 | r = fix_permissions(fd_compressed, tmp_compressed, fn_compressed, context, uid); |
cfd652ed | 400 | if (r < 0) |
b59233e6 LP |
401 | goto fail_compressed; |
402 | ||
403 | /* OK, this worked, we can get rid of the uncompressed version now */ | |
0c773903 EV |
404 | if (tmp) |
405 | unlink_noerrno(tmp); | |
cfd652ed | 406 | |
59f448cf | 407 | *ret_filename = fn_compressed; /* compressed */ |
5f3e0a74 HW |
408 | *ret_node_fd = fd_compressed; /* compressed */ |
409 | *ret_data_fd = fd; /* uncompressed */ | |
59f448cf | 410 | *ret_size = (uint64_t) st.st_size; /* uncompressed */ |
cfd652ed | 411 | |
b59233e6 | 412 | fn_compressed = NULL; |
5f3e0a74 | 413 | fd = fd_compressed = -1; |
cfd652ed ZJS |
414 | |
415 | return 0; | |
416 | ||
b59233e6 | 417 | fail_compressed: |
0c773903 EV |
418 | if (tmp_compressed) |
419 | (void) unlink(tmp_compressed); | |
34c10968 | 420 | } |
cfd652ed ZJS |
421 | |
422 | uncompressed: | |
3b1a55e1 | 423 | #endif |
5f3e0a74 | 424 | |
3c171f0b | 425 | r = fix_permissions(fd, tmp, fn, context, uid); |
cfd652ed ZJS |
426 | if (r < 0) |
427 | goto fail; | |
34c10968 LP |
428 | |
429 | *ret_filename = fn; | |
5f3e0a74 HW |
430 | *ret_data_fd = fd; |
431 | *ret_node_fd = -1; | |
59f448cf | 432 | *ret_size = (uint64_t) st.st_size; |
34c10968 LP |
433 | |
434 | fn = NULL; | |
435 | fd = -1; | |
436 | ||
437 | return 0; | |
438 | ||
439 | fail: | |
0c773903 EV |
440 | if (tmp) |
441 | (void) unlink(tmp); | |
34c10968 LP |
442 | return r; |
443 | } | |
444 | ||
445 | static int allocate_journal_field(int fd, size_t size, char **ret, size_t *ret_size) { | |
446 | _cleanup_free_ char *field = NULL; | |
447 | ssize_t n; | |
448 | ||
8d4e028f | 449 | assert(fd >= 0); |
34c10968 LP |
450 | assert(ret); |
451 | assert(ret_size); | |
452 | ||
4a62c710 MS |
453 | if (lseek(fd, 0, SEEK_SET) == (off_t) -1) |
454 | return log_warning_errno(errno, "Failed to seek: %m"); | |
803a3464 | 455 | |
34c10968 LP |
456 | field = malloc(9 + size); |
457 | if (!field) { | |
cfd652ed | 458 | log_warning("Failed to allocate memory for coredump, coredump will not be stored."); |
34c10968 LP |
459 | return -ENOMEM; |
460 | } | |
461 | ||
462 | memcpy(field, "COREDUMP=", 9); | |
463 | ||
464 | n = read(fd, field + 9, size); | |
23bbb0de MS |
465 | if (n < 0) |
466 | return log_error_errno((int) n, "Failed to read core data: %m"); | |
34c10968 LP |
467 | if ((size_t) n < size) { |
468 | log_error("Core data too short."); | |
469 | return -EIO; | |
470 | } | |
471 | ||
472 | *ret = field; | |
473 | *ret_size = size + 9; | |
474 | ||
475 | field = NULL; | |
476 | ||
477 | return 0; | |
478 | } | |
803a3464 | 479 | |
3f132692 JF |
480 | /* Joins /proc/[pid]/fd/ and /proc/[pid]/fdinfo/ into the following lines: |
481 | * 0:/dev/pts/23 | |
482 | * pos: 0 | |
483 | * flags: 0100002 | |
484 | * | |
485 | * 1:/dev/pts/23 | |
486 | * pos: 0 | |
487 | * flags: 0100002 | |
488 | * | |
489 | * 2:/dev/pts/23 | |
490 | * pos: 0 | |
491 | * flags: 0100002 | |
492 | * EOF | |
493 | */ | |
494 | static int compose_open_fds(pid_t pid, char **open_fds) { | |
4d84bc2f LP |
495 | _cleanup_closedir_ DIR *proc_fd_dir = NULL; |
496 | _cleanup_close_ int proc_fdinfo_fd = -1; | |
497 | _cleanup_free_ char *buffer = NULL; | |
3f132692 | 498 | _cleanup_fclose_ FILE *stream = NULL; |
59059b4a | 499 | const char *fddelim = "", *path; |
3f132692 | 500 | struct dirent *dent = NULL; |
4d84bc2f | 501 | size_t size = 0; |
3f132692 JF |
502 | int r = 0; |
503 | ||
504 | assert(pid >= 0); | |
505 | assert(open_fds != NULL); | |
506 | ||
59059b4a | 507 | path = procfs_file_alloca(pid, "fd"); |
3f132692 | 508 | proc_fd_dir = opendir(path); |
59059b4a ZJS |
509 | if (!proc_fd_dir) |
510 | return -errno; | |
3f132692 | 511 | |
4d84bc2f | 512 | proc_fdinfo_fd = openat(dirfd(proc_fd_dir), "../fdinfo", O_DIRECTORY|O_NOFOLLOW|O_CLOEXEC|O_PATH); |
59059b4a ZJS |
513 | if (proc_fdinfo_fd < 0) |
514 | return -errno; | |
3f132692 | 515 | |
4d84bc2f | 516 | stream = open_memstream(&buffer, &size); |
3f132692 JF |
517 | if (!stream) |
518 | return -ENOMEM; | |
519 | ||
4d84bc2f | 520 | FOREACH_DIRENT(dent, proc_fd_dir, return -errno) { |
3f132692 | 521 | _cleanup_fclose_ FILE *fdinfo = NULL; |
4d84bc2f | 522 | _cleanup_free_ char *fdname = NULL; |
59059b4a | 523 | char line[LINE_MAX]; |
4d84bc2f | 524 | int fd; |
3f132692 | 525 | |
59059b4a | 526 | r = readlinkat_malloc(dirfd(proc_fd_dir), dent->d_name, &fdname); |
3f132692 JF |
527 | if (r < 0) |
528 | return r; | |
529 | ||
530 | fprintf(stream, "%s%s:%s\n", fddelim, dent->d_name, fdname); | |
531 | fddelim = "\n"; | |
532 | ||
533 | /* Use the directory entry from /proc/[pid]/fd with /proc/[pid]/fdinfo */ | |
59059b4a ZJS |
534 | fd = openat(proc_fdinfo_fd, dent->d_name, O_NOFOLLOW|O_CLOEXEC|O_RDONLY); |
535 | if (fd < 0) | |
3f132692 JF |
536 | continue; |
537 | ||
59059b4a ZJS |
538 | fdinfo = fdopen(fd, "re"); |
539 | if (fdinfo == NULL) { | |
540 | close(fd); | |
3f132692 | 541 | continue; |
59059b4a | 542 | } |
3f132692 | 543 | |
4d84bc2f LP |
544 | FOREACH_LINE(line, fdinfo, break) { |
545 | fputs(line, stream); | |
546 | if (!endswith(line, "\n")) | |
547 | fputc('\n', stream); | |
548 | } | |
3f132692 JF |
549 | } |
550 | ||
4d84bc2f | 551 | errno = 0; |
74ca738f | 552 | stream = safe_fclose(stream); |
4d84bc2f | 553 | |
b3267152 | 554 | if (errno > 0) |
4d84bc2f LP |
555 | return -errno; |
556 | ||
557 | *open_fds = buffer; | |
558 | buffer = NULL; | |
559 | ||
3f132692 JF |
560 | return 0; |
561 | } | |
562 | ||
3c171f0b LP |
563 | static int change_uid_gid(const char *context[]) { |
564 | uid_t uid; | |
565 | gid_t gid; | |
566 | int r; | |
34c10968 | 567 | |
3c171f0b LP |
568 | r = parse_uid(context[CONTEXT_UID], &uid); |
569 | if (r < 0) | |
570 | return r; | |
8c8549db | 571 | |
888e378d LP |
572 | if (uid <= SYSTEM_UID_MAX) { |
573 | const char *user = "systemd-coredump"; | |
574 | ||
575 | r = get_user_creds(&user, &uid, &gid, NULL, NULL); | |
576 | if (r < 0) { | |
577 | log_warning_errno(r, "Cannot resolve %s user. Proceeding to dump core as root: %m", user); | |
578 | uid = gid = 0; | |
579 | } | |
580 | } else { | |
581 | r = parse_gid(context[CONTEXT_GID], &gid); | |
582 | if (r < 0) | |
583 | return r; | |
584 | } | |
3c171f0b LP |
585 | |
586 | return drop_privileges(uid, gid, 0); | |
587 | } | |
8c8549db | 588 | |
3c171f0b LP |
589 | static int submit_coredump( |
590 | const char *context[_CONTEXT_MAX], | |
591 | struct iovec *iovec, | |
592 | size_t n_iovec_allocated, | |
593 | size_t n_iovec, | |
594 | int input_fd) { | |
34c10968 | 595 | |
5f3e0a74 | 596 | _cleanup_close_ int coredump_fd = -1, coredump_node_fd = -1; |
3c171f0b | 597 | _cleanup_free_ char *core_message = NULL, *filename = NULL, *coredump_data = NULL; |
59f448cf | 598 | uint64_t coredump_size; |
3c171f0b | 599 | int r; |
f5e04665 | 600 | |
3c171f0b LP |
601 | assert(context); |
602 | assert(iovec); | |
603 | assert(n_iovec_allocated >= n_iovec + 3); | |
604 | assert(input_fd >= 0); | |
f5e04665 | 605 | |
3c171f0b LP |
606 | /* Vacuum before we write anything again */ |
607 | (void) coredump_vacuum(-1, arg_keep_free, arg_max_use); | |
803a3464 | 608 | |
3c171f0b LP |
609 | /* Always stream the coredump to disk, if that's possible */ |
610 | r = save_external_coredump(context, input_fd, &filename, &coredump_node_fd, &coredump_fd, &coredump_size); | |
611 | if (r < 0) | |
612 | /* Skip whole core dumping part */ | |
613 | goto log; | |
614 | ||
615 | /* If we don't want to keep the coredump on disk, remove it now, as later on we will lack the privileges for | |
616 | * it. However, we keep the fd to it, so that we can still process it and log it. */ | |
617 | r = maybe_remove_external_coredump(filename, coredump_size); | |
618 | if (r < 0) | |
619 | return r; | |
620 | if (r == 0) { | |
621 | const char *coredump_filename; | |
622 | ||
623 | coredump_filename = strjoina("COREDUMP_FILENAME=", filename); | |
624 | IOVEC_SET_STRING(iovec[n_iovec++], coredump_filename); | |
f5e04665 LP |
625 | } |
626 | ||
3c171f0b LP |
627 | /* Vacuum again, but exclude the coredump we just created */ |
628 | (void) coredump_vacuum(coredump_node_fd >= 0 ? coredump_node_fd : coredump_fd, arg_keep_free, arg_max_use); | |
8c9571d0 | 629 | |
3c171f0b LP |
630 | /* Now, let's drop privileges to become the user who owns the segfaulted process and allocate the coredump |
631 | * memory under the user's uid. This also ensures that the credentials journald will see are the ones of the | |
632 | * coredumping user, thus making sure the user gets access to the core dump. Let's also get rid of all | |
633 | * capabilities, if we run as root, we won't need them anymore. */ | |
634 | r = change_uid_gid(context); | |
635 | if (r < 0) | |
636 | return log_error_errno(r, "Failed to drop privileges: %m"); | |
34c10968 | 637 | |
3c171f0b LP |
638 | #ifdef HAVE_ELFUTILS |
639 | /* Try to get a strack trace if we can */ | |
640 | if (coredump_size <= arg_process_size_max) { | |
641 | _cleanup_free_ char *stacktrace = NULL; | |
642 | ||
643 | r = coredump_make_stack_trace(coredump_fd, context[CONTEXT_EXE], &stacktrace); | |
644 | if (r >= 0) | |
645 | core_message = strjoin("MESSAGE=Process ", context[CONTEXT_PID], " (", context[CONTEXT_COMM], ") of user ", context[CONTEXT_UID], " dumped core.\n\n", stacktrace, NULL); | |
646 | else if (r == -EINVAL) | |
647 | log_warning("Failed to generate stack trace: %s", dwfl_errmsg(dwfl_errno())); | |
648 | else | |
649 | log_warning_errno(r, "Failed to generate stack trace: %m"); | |
34c10968 | 650 | } |
803a3464 | 651 | |
3c171f0b LP |
652 | if (!core_message) |
653 | #endif | |
654 | log: | |
655 | core_message = strjoin("MESSAGE=Process ", context[CONTEXT_PID], " (", context[CONTEXT_COMM], ") of user ", context[CONTEXT_UID], " dumped core.", NULL); | |
656 | if (core_message) | |
657 | IOVEC_SET_STRING(iovec[n_iovec++], core_message); | |
658 | ||
659 | /* Optionally store the entire coredump in the journal */ | |
660 | if (IN_SET(arg_storage, COREDUMP_STORAGE_JOURNAL, COREDUMP_STORAGE_BOTH) && | |
661 | coredump_size <= arg_journal_size_max) { | |
662 | size_t sz = 0; | |
663 | ||
664 | /* Store the coredump itself in the journal */ | |
665 | ||
666 | r = allocate_journal_field(coredump_fd, (size_t) coredump_size, &coredump_data, &sz); | |
667 | if (r >= 0) { | |
668 | iovec[n_iovec].iov_base = coredump_data; | |
669 | iovec[n_iovec].iov_len = sz; | |
670 | n_iovec++; | |
671 | } | |
f5e04665 LP |
672 | } |
673 | ||
3c171f0b LP |
674 | assert(n_iovec <= n_iovec_allocated); |
675 | ||
676 | r = sd_journal_sendv(iovec, n_iovec); | |
677 | if (r < 0) | |
678 | return log_error_errno(r, "Failed to log coredump: %m"); | |
679 | ||
680 | return 0; | |
681 | } | |
682 | ||
683 | static void map_context_fields(const struct iovec *iovec, const char *context[]) { | |
684 | ||
685 | static const char * const context_field_names[_CONTEXT_MAX] = { | |
686 | [CONTEXT_PID] = "COREDUMP_PID=", | |
687 | [CONTEXT_UID] = "COREDUMP_UID=", | |
688 | [CONTEXT_GID] = "COREDUMP_GID=", | |
689 | [CONTEXT_SIGNAL] = "COREDUMP_SIGNAL=", | |
690 | [CONTEXT_TIMESTAMP] = "COREDUMP_TIMESTAMP=", | |
691 | [CONTEXT_COMM] = "COREDUMP_COMM=", | |
692 | [CONTEXT_EXE] = "COREDUMP_EXE=", | |
bdfd7b2c | 693 | [CONTEXT_RLIMIT] = "COREDUMP_RLIMIT=", |
3c171f0b LP |
694 | }; |
695 | ||
696 | unsigned i; | |
697 | ||
698 | assert(iovec); | |
699 | assert(context); | |
700 | ||
701 | for (i = 0; i < _CONTEXT_MAX; i++) { | |
702 | size_t l; | |
703 | ||
704 | l = strlen(context_field_names[i]); | |
705 | if (iovec->iov_len < l) | |
706 | continue; | |
707 | ||
708 | if (memcmp(iovec->iov_base, context_field_names[i], l) != 0) | |
709 | continue; | |
710 | ||
711 | /* Note that these strings are NUL terminated, because we made sure that a trailing NUL byte is in the | |
712 | * buffer, though not included in the iov_len count. (see below) */ | |
713 | context[i] = (char*) iovec->iov_base + l; | |
714 | break; | |
715 | } | |
716 | } | |
717 | ||
718 | static int process_socket(int fd) { | |
719 | _cleanup_close_ int coredump_fd = -1; | |
720 | struct iovec *iovec = NULL; | |
721 | size_t n_iovec = 0, n_iovec_allocated = 0, i; | |
722 | const char *context[_CONTEXT_MAX] = {}; | |
723 | int r; | |
724 | ||
725 | assert(fd >= 0); | |
726 | ||
727 | log_set_target(LOG_TARGET_AUTO); | |
728 | log_parse_environment(); | |
729 | log_open(); | |
730 | ||
731 | for (;;) { | |
732 | union { | |
733 | struct cmsghdr cmsghdr; | |
734 | uint8_t buf[CMSG_SPACE(sizeof(int))]; | |
735 | } control = {}; | |
736 | struct msghdr mh = { | |
737 | .msg_control = &control, | |
738 | .msg_controllen = sizeof(control), | |
739 | .msg_iovlen = 1, | |
740 | }; | |
741 | ssize_t n; | |
fe1ef0f8 | 742 | ssize_t l; |
3c171f0b LP |
743 | |
744 | if (!GREEDY_REALLOC(iovec, n_iovec_allocated, n_iovec + 3)) { | |
745 | r = log_oom(); | |
746 | goto finish; | |
747 | } | |
748 | ||
fe1ef0f8 EV |
749 | l = next_datagram_size_fd(fd); |
750 | if (l < 0) { | |
751 | r = log_error_errno(l, "Failed to determine datagram size to read: %m"); | |
3c171f0b LP |
752 | goto finish; |
753 | } | |
754 | ||
755 | assert(l >= 0); | |
756 | ||
757 | iovec[n_iovec].iov_len = l; | |
758 | iovec[n_iovec].iov_base = malloc(l + 1); | |
759 | ||
760 | if (!iovec[n_iovec].iov_base) { | |
761 | r = log_oom(); | |
762 | goto finish; | |
763 | } | |
764 | ||
765 | mh.msg_iov = iovec + n_iovec; | |
766 | ||
767 | n = recvmsg(fd, &mh, MSG_NOSIGNAL|MSG_CMSG_CLOEXEC); | |
768 | if (n < 0) { | |
769 | free(iovec[n_iovec].iov_base); | |
770 | r = log_error_errno(errno, "Failed to receive datagram: %m"); | |
771 | goto finish; | |
772 | } | |
773 | ||
774 | if (n == 0) { | |
775 | struct cmsghdr *cmsg, *found = NULL; | |
776 | /* The final zero-length datagram carries the file descriptor and tells us that we're done. */ | |
777 | ||
778 | free(iovec[n_iovec].iov_base); | |
779 | ||
780 | CMSG_FOREACH(cmsg, &mh) { | |
781 | if (cmsg->cmsg_level == SOL_SOCKET && | |
782 | cmsg->cmsg_type == SCM_RIGHTS && | |
783 | cmsg->cmsg_len == CMSG_LEN(sizeof(int))) { | |
784 | assert(!found); | |
785 | found = cmsg; | |
786 | } | |
787 | } | |
788 | ||
789 | if (!found) { | |
790 | log_error("Coredump file descriptor missing."); | |
791 | r = -EBADMSG; | |
792 | goto finish; | |
793 | } | |
794 | ||
795 | assert(coredump_fd < 0); | |
796 | coredump_fd = *(int*) CMSG_DATA(found); | |
797 | break; | |
798 | } | |
799 | ||
800 | /* Add trailing NUL byte, in case these are strings */ | |
801 | ((char*) iovec[n_iovec].iov_base)[n] = 0; | |
802 | iovec[n_iovec].iov_len = (size_t) n; | |
803 | ||
804 | cmsg_close_all(&mh); | |
805 | map_context_fields(iovec + n_iovec, context); | |
806 | n_iovec++; | |
807 | } | |
808 | ||
809 | if (!GREEDY_REALLOC(iovec, n_iovec_allocated, n_iovec + 3)) { | |
810 | r = log_oom(); | |
34c10968 LP |
811 | goto finish; |
812 | } | |
813 | ||
3c171f0b LP |
814 | /* Make sure we we got all data we really need */ |
815 | assert(context[CONTEXT_PID]); | |
816 | assert(context[CONTEXT_UID]); | |
817 | assert(context[CONTEXT_GID]); | |
818 | assert(context[CONTEXT_SIGNAL]); | |
819 | assert(context[CONTEXT_TIMESTAMP]); | |
bdfd7b2c | 820 | assert(context[CONTEXT_RLIMIT]); |
3c171f0b LP |
821 | assert(context[CONTEXT_COMM]); |
822 | assert(coredump_fd >= 0); | |
823 | ||
824 | r = submit_coredump(context, iovec, n_iovec_allocated, n_iovec, coredump_fd); | |
825 | ||
826 | finish: | |
827 | for (i = 0; i < n_iovec; i++) | |
828 | free(iovec[i].iov_base); | |
829 | free(iovec); | |
830 | ||
831 | return r; | |
832 | } | |
833 | ||
834 | static int send_iovec(const struct iovec iovec[], size_t n_iovec, int input_fd) { | |
835 | ||
836 | static const union sockaddr_union sa = { | |
837 | .un.sun_family = AF_UNIX, | |
838 | .un.sun_path = "/run/systemd/coredump", | |
839 | }; | |
840 | _cleanup_close_ int fd = -1; | |
841 | size_t i; | |
842 | int r; | |
843 | ||
844 | assert(iovec || n_iovec <= 0); | |
845 | assert(input_fd >= 0); | |
846 | ||
847 | fd = socket(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0); | |
848 | if (fd < 0) | |
849 | return log_error_errno(errno, "Failed to create coredump socket: %m"); | |
850 | ||
fc2fffe7 | 851 | if (connect(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0) |
3c171f0b LP |
852 | return log_error_errno(errno, "Failed to connect to coredump service: %m"); |
853 | ||
854 | for (i = 0; i < n_iovec; i++) { | |
855 | ssize_t n; | |
856 | assert(iovec[i].iov_len > 0); | |
857 | ||
858 | n = send(fd, iovec[i].iov_base, iovec[i].iov_len, MSG_NOSIGNAL); | |
859 | if (n < 0) | |
860 | return log_error_errno(errno, "Failed to send coredump datagram: %m"); | |
1eef15b1 ZJS |
861 | } |
862 | ||
3c171f0b LP |
863 | r = send_one_fd(fd, input_fd, 0); |
864 | if (r < 0) | |
865 | return log_error_errno(r, "Failed to send coredump fd: %m"); | |
1eef15b1 | 866 | |
3c171f0b LP |
867 | return 0; |
868 | } | |
1eef15b1 | 869 | |
3c171f0b LP |
870 | static int process_journald_crash(const char *context[], int input_fd) { |
871 | _cleanup_close_ int coredump_fd = -1, coredump_node_fd = -1; | |
872 | _cleanup_free_ char *filename = NULL; | |
873 | uint64_t coredump_size; | |
874 | int r; | |
803a3464 | 875 | |
3c171f0b LP |
876 | assert(context); |
877 | assert(input_fd >= 0); | |
803a3464 | 878 | |
3c171f0b | 879 | /* If we are journald, we cut things short, don't write to the journal, but still create a coredump. */ |
34c10968 | 880 | |
3c171f0b LP |
881 | if (arg_storage != COREDUMP_STORAGE_NONE) |
882 | arg_storage = COREDUMP_STORAGE_EXTERNAL; | |
34c10968 | 883 | |
3c171f0b LP |
884 | r = save_external_coredump(context, input_fd, &filename, &coredump_node_fd, &coredump_fd, &coredump_size); |
885 | if (r < 0) | |
886 | return r; | |
34c10968 | 887 | |
3c171f0b LP |
888 | r = maybe_remove_external_coredump(filename, coredump_size); |
889 | if (r < 0) | |
890 | return r; | |
34c10968 | 891 | |
3c171f0b LP |
892 | log_info("Detected coredump of the journal daemon itself, diverted to %s.", filename); |
893 | return 0; | |
894 | } | |
895 | ||
896 | static int process_kernel(int argc, char* argv[]) { | |
897 | ||
898 | /* The small core field we allocate on the stack, to keep things simple */ | |
899 | char | |
900 | *core_pid = NULL, *core_uid = NULL, *core_gid = NULL, *core_signal = NULL, | |
901 | *core_session = NULL, *core_exe = NULL, *core_comm = NULL, *core_cmdline = NULL, | |
902 | *core_cgroup = NULL, *core_cwd = NULL, *core_root = NULL, *core_unit = NULL, | |
bdfd7b2c | 903 | *core_user_unit = NULL, *core_slice = NULL, *core_timestamp = NULL, *core_rlimit = NULL; |
3c171f0b LP |
904 | |
905 | /* The larger ones we allocate on the heap */ | |
906 | _cleanup_free_ char | |
907 | *core_owner_uid = NULL, *core_open_fds = NULL, *core_proc_status = NULL, | |
908 | *core_proc_maps = NULL, *core_proc_limits = NULL, *core_proc_cgroup = NULL, *core_environ = NULL; | |
909 | ||
910 | _cleanup_free_ char *exe = NULL, *comm = NULL; | |
911 | const char *context[_CONTEXT_MAX]; | |
bdfd7b2c | 912 | struct iovec iovec[25]; |
3c171f0b LP |
913 | size_t n_iovec = 0; |
914 | uid_t owner_uid; | |
915 | const char *p; | |
916 | pid_t pid; | |
917 | char *t; | |
918 | int r; | |
919 | ||
920 | if (argc < CONTEXT_COMM + 1) { | |
921 | log_error("Not enough arguments passed from kernel (%i, expected %i).", argc - 1, CONTEXT_COMM + 1 - 1); | |
922 | return -EINVAL; | |
923 | } | |
924 | ||
925 | r = parse_pid(argv[CONTEXT_PID + 1], &pid); | |
926 | if (r < 0) | |
927 | return log_error_errno(r, "Failed to parse PID."); | |
928 | ||
929 | r = get_process_comm(pid, &comm); | |
930 | if (r < 0) { | |
931 | log_warning_errno(r, "Failed to get COMM, falling back to the command line: %m"); | |
932 | comm = strv_join(argv + CONTEXT_COMM + 1, " "); | |
933 | if (!comm) | |
934 | return log_oom(); | |
935 | } | |
936 | ||
937 | r = get_process_exe(pid, &exe); | |
938 | if (r < 0) | |
939 | log_warning_errno(r, "Failed to get EXE, ignoring: %m"); | |
940 | ||
941 | context[CONTEXT_PID] = argv[CONTEXT_PID + 1]; | |
942 | context[CONTEXT_UID] = argv[CONTEXT_UID + 1]; | |
943 | context[CONTEXT_GID] = argv[CONTEXT_GID + 1]; | |
944 | context[CONTEXT_SIGNAL] = argv[CONTEXT_SIGNAL + 1]; | |
945 | context[CONTEXT_TIMESTAMP] = argv[CONTEXT_TIMESTAMP + 1]; | |
bdfd7b2c | 946 | context[CONTEXT_RLIMIT] = argv[CONTEXT_RLIMIT + 1]; |
3c171f0b LP |
947 | context[CONTEXT_COMM] = comm; |
948 | context[CONTEXT_EXE] = exe; | |
949 | ||
950 | if (cg_pid_get_unit(pid, &t) >= 0) { | |
951 | ||
952 | if (streq(t, SPECIAL_JOURNALD_SERVICE)) { | |
953 | free(t); | |
954 | return process_journald_crash(context, STDIN_FILENO); | |
803a3464 LP |
955 | } |
956 | ||
63c372cb | 957 | core_unit = strjoina("COREDUMP_UNIT=", t); |
8c8549db LP |
958 | free(t); |
959 | ||
3c171f0b | 960 | IOVEC_SET_STRING(iovec[n_iovec++], core_unit); |
8c8549db | 961 | } |
803a3464 | 962 | |
3c171f0b | 963 | /* OK, now we know it's not the journal, hence we can make use of it now. */ |
803a3464 LP |
964 | log_set_target(LOG_TARGET_JOURNAL_OR_KMSG); |
965 | log_open(); | |
966 | ||
3c171f0b LP |
967 | if (cg_pid_get_user_unit(pid, &t) >= 0) { |
968 | core_user_unit = strjoina("COREDUMP_USER_UNIT=", t); | |
969 | free(t); | |
970 | ||
971 | IOVEC_SET_STRING(iovec[n_iovec++], core_user_unit); | |
972 | } | |
973 | ||
974 | core_pid = strjoina("COREDUMP_PID=", context[CONTEXT_PID]); | |
975 | IOVEC_SET_STRING(iovec[n_iovec++], core_pid); | |
f5e04665 | 976 | |
3c171f0b LP |
977 | core_uid = strjoina("COREDUMP_UID=", context[CONTEXT_UID]); |
978 | IOVEC_SET_STRING(iovec[n_iovec++], core_uid); | |
f5e04665 | 979 | |
3c171f0b LP |
980 | core_gid = strjoina("COREDUMP_GID=", context[CONTEXT_GID]); |
981 | IOVEC_SET_STRING(iovec[n_iovec++], core_gid); | |
f5e04665 | 982 | |
3c171f0b LP |
983 | core_signal = strjoina("COREDUMP_SIGNAL=", context[CONTEXT_SIGNAL]); |
984 | IOVEC_SET_STRING(iovec[n_iovec++], core_signal); | |
f5e04665 | 985 | |
bdfd7b2c LP |
986 | core_rlimit = strjoina("COREDUMP_RLIMIT=", context[CONTEXT_RLIMIT]); |
987 | IOVEC_SET_STRING(iovec[n_iovec++], core_rlimit); | |
988 | ||
f5e04665 | 989 | if (sd_pid_get_session(pid, &t) >= 0) { |
63c372cb | 990 | core_session = strjoina("COREDUMP_SESSION=", t); |
f5e04665 LP |
991 | free(t); |
992 | ||
3c171f0b | 993 | IOVEC_SET_STRING(iovec[n_iovec++], core_session); |
f5e04665 LP |
994 | } |
995 | ||
a035f819 | 996 | if (sd_pid_get_owner_uid(pid, &owner_uid) >= 0) { |
3c171f0b | 997 | r = asprintf(&core_owner_uid, "COREDUMP_OWNER_UID=" UID_FMT, owner_uid); |
7de80bfe | 998 | if (r > 0) |
3c171f0b | 999 | IOVEC_SET_STRING(iovec[n_iovec++], core_owner_uid); |
a035f819 LP |
1000 | } |
1001 | ||
1002 | if (sd_pid_get_slice(pid, &t) >= 0) { | |
63c372cb | 1003 | core_slice = strjoina("COREDUMP_SLICE=", t); |
a035f819 LP |
1004 | free(t); |
1005 | ||
3c171f0b | 1006 | IOVEC_SET_STRING(iovec[n_iovec++], core_slice); |
a035f819 LP |
1007 | } |
1008 | ||
1eef15b1 | 1009 | if (comm) { |
63c372cb | 1010 | core_comm = strjoina("COREDUMP_COMM=", comm); |
3c171f0b | 1011 | IOVEC_SET_STRING(iovec[n_iovec++], core_comm); |
1eef15b1 ZJS |
1012 | } |
1013 | ||
1014 | if (exe) { | |
63c372cb | 1015 | core_exe = strjoina("COREDUMP_EXE=", exe); |
3c171f0b | 1016 | IOVEC_SET_STRING(iovec[n_iovec++], core_exe); |
f5e04665 LP |
1017 | } |
1018 | ||
9bdbc2e2 | 1019 | if (get_process_cmdline(pid, 0, false, &t) >= 0) { |
63c372cb | 1020 | core_cmdline = strjoina("COREDUMP_CMDLINE=", t); |
f5e04665 LP |
1021 | free(t); |
1022 | ||
3c171f0b | 1023 | IOVEC_SET_STRING(iovec[n_iovec++], core_cmdline); |
f5e04665 LP |
1024 | } |
1025 | ||
a035f819 | 1026 | if (cg_pid_get_path_shifted(pid, NULL, &t) >= 0) { |
63c372cb | 1027 | core_cgroup = strjoina("COREDUMP_CGROUP=", t); |
a035f819 LP |
1028 | free(t); |
1029 | ||
3c171f0b | 1030 | IOVEC_SET_STRING(iovec[n_iovec++], core_cgroup); |
a035f819 LP |
1031 | } |
1032 | ||
3f132692 JF |
1033 | if (compose_open_fds(pid, &t) >= 0) { |
1034 | core_open_fds = strappend("COREDUMP_OPEN_FDS=", t); | |
1035 | free(t); | |
1036 | ||
1037 | if (core_open_fds) | |
3c171f0b | 1038 | IOVEC_SET_STRING(iovec[n_iovec++], core_open_fds); |
3f132692 JF |
1039 | } |
1040 | ||
1041 | p = procfs_file_alloca(pid, "status"); | |
1042 | if (read_full_file(p, &t, NULL) >= 0) { | |
1043 | core_proc_status = strappend("COREDUMP_PROC_STATUS=", t); | |
1044 | free(t); | |
1045 | ||
1046 | if (core_proc_status) | |
3c171f0b | 1047 | IOVEC_SET_STRING(iovec[n_iovec++], core_proc_status); |
3f132692 JF |
1048 | } |
1049 | ||
1050 | p = procfs_file_alloca(pid, "maps"); | |
1051 | if (read_full_file(p, &t, NULL) >= 0) { | |
1052 | core_proc_maps = strappend("COREDUMP_PROC_MAPS=", t); | |
1053 | free(t); | |
1054 | ||
1055 | if (core_proc_maps) | |
3c171f0b | 1056 | IOVEC_SET_STRING(iovec[n_iovec++], core_proc_maps); |
3f132692 JF |
1057 | } |
1058 | ||
1059 | p = procfs_file_alloca(pid, "limits"); | |
1060 | if (read_full_file(p, &t, NULL) >= 0) { | |
1061 | core_proc_limits = strappend("COREDUMP_PROC_LIMITS=", t); | |
1062 | free(t); | |
1063 | ||
1064 | if (core_proc_limits) | |
3c171f0b | 1065 | IOVEC_SET_STRING(iovec[n_iovec++], core_proc_limits); |
3f132692 JF |
1066 | } |
1067 | ||
1068 | p = procfs_file_alloca(pid, "cgroup"); | |
1069 | if (read_full_file(p, &t, NULL) >=0) { | |
1070 | core_proc_cgroup = strappend("COREDUMP_PROC_CGROUP=", t); | |
1071 | free(t); | |
1072 | ||
1073 | if (core_proc_cgroup) | |
3c171f0b | 1074 | IOVEC_SET_STRING(iovec[n_iovec++], core_proc_cgroup); |
3f132692 JF |
1075 | } |
1076 | ||
1077 | if (get_process_cwd(pid, &t) >= 0) { | |
63c372cb | 1078 | core_cwd = strjoina("COREDUMP_CWD=", t); |
3f132692 JF |
1079 | free(t); |
1080 | ||
3c171f0b | 1081 | IOVEC_SET_STRING(iovec[n_iovec++], core_cwd); |
3f132692 JF |
1082 | } |
1083 | ||
1084 | if (get_process_root(pid, &t) >= 0) { | |
63c372cb | 1085 | core_root = strjoina("COREDUMP_ROOT=", t); |
3f132692 JF |
1086 | free(t); |
1087 | ||
3c171f0b | 1088 | IOVEC_SET_STRING(iovec[n_iovec++], core_root); |
3f132692 JF |
1089 | } |
1090 | ||
1091 | if (get_process_environ(pid, &t) >= 0) { | |
1092 | core_environ = strappend("COREDUMP_ENVIRON=", t); | |
1093 | free(t); | |
1094 | ||
1095 | if (core_environ) | |
3c171f0b | 1096 | IOVEC_SET_STRING(iovec[n_iovec++], core_environ); |
3f132692 JF |
1097 | } |
1098 | ||
81d62103 | 1099 | core_timestamp = strjoina("COREDUMP_TIMESTAMP=", context[CONTEXT_TIMESTAMP], "000000"); |
3c171f0b | 1100 | IOVEC_SET_STRING(iovec[n_iovec++], core_timestamp); |
f5e04665 | 1101 | |
3c171f0b | 1102 | IOVEC_SET_STRING(iovec[n_iovec++], "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1"); |
4850d39a LP |
1103 | |
1104 | assert_cc(2 == LOG_CRIT); | |
3c171f0b | 1105 | IOVEC_SET_STRING(iovec[n_iovec++], "PRIORITY=2"); |
0dc5d23c | 1106 | |
3c171f0b | 1107 | assert(n_iovec <= ELEMENTSOF(iovec)); |
34c10968 | 1108 | |
3c171f0b LP |
1109 | return send_iovec(iovec, n_iovec, STDIN_FILENO); |
1110 | } | |
34c10968 | 1111 | |
3c171f0b LP |
1112 | int main(int argc, char *argv[]) { |
1113 | int r; | |
fee80f69 | 1114 | |
3c171f0b LP |
1115 | /* First, log to a safe place, since we don't know what crashed and it might be journald which we'd rather not |
1116 | * log to then. */ | |
8d4e028f | 1117 | |
3c171f0b LP |
1118 | log_set_target(LOG_TARGET_KMSG); |
1119 | log_open(); | |
8d4e028f | 1120 | |
3c171f0b LP |
1121 | /* Make sure we never enter a loop */ |
1122 | (void) prctl(PR_SET_DUMPABLE, 0); | |
8d4e028f | 1123 | |
3c171f0b LP |
1124 | /* Ignore all parse errors */ |
1125 | (void) parse_config(); | |
fee80f69 | 1126 | |
3c171f0b LP |
1127 | log_debug("Selected storage '%s'.", coredump_storage_to_string(arg_storage)); |
1128 | log_debug("Selected compression %s.", yes_no(arg_compress)); | |
fee80f69 | 1129 | |
3c171f0b LP |
1130 | r = sd_listen_fds(false); |
1131 | if (r < 0) { | |
1132 | log_error_errno(r, "Failed to determine number of file descriptor: %m"); | |
1133 | goto finish; | |
fee80f69 LP |
1134 | } |
1135 | ||
3c171f0b LP |
1136 | /* If we got an fd passed, we are running in coredumpd mode. Otherwise we are invoked from the kernel as |
1137 | * coredump handler */ | |
1138 | if (r == 0) | |
1139 | r = process_kernel(argc, argv); | |
1140 | else if (r == 1) | |
1141 | r = process_socket(SD_LISTEN_FDS_START); | |
1142 | else { | |
1143 | log_error("Received unexpected number of file descriptors."); | |
1144 | r = -EINVAL; | |
1145 | } | |
f5e04665 LP |
1146 | |
1147 | finish: | |
f5e04665 LP |
1148 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
1149 | } |