]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/coredump/coredump.c
tree-wide: drop license boilerplate
[thirdparty/systemd.git] / src / coredump / coredump.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
f5e04665
LP
2/***
3 This file is part of systemd.
4
5 Copyright 2012 Lennart Poettering
f5e04665
LP
6***/
7
8#include <errno.h>
803a3464 9#include <stdio.h>
0d536673 10#include <stdio_ext.h>
803a3464 11#include <sys/prctl.h>
cacd6403 12#include <sys/xattr.h>
4f5dd394 13#include <unistd.h>
f5e04665 14
349cc4a5 15#if HAVE_ELFUTILS
3c171f0b
LP
16#include <dwarf.h>
17#include <elfutils/libdwfl.h>
4d229b31
UTL
18#endif
19
73a99163 20#include "sd-daemon.h"
f11943c5
LP
21#include "sd-journal.h"
22#include "sd-login.h"
73a99163 23#include "sd-messages.h"
4f5dd394
LP
24
25#include "acl-util.h"
b5efdb8a 26#include "alloc-util.h"
430f0182 27#include "capability-util.h"
ba1261bc 28#include "cgroup-util.h"
4f5dd394 29#include "compress.h"
34c10968
LP
30#include "conf-parser.h"
31#include "copy.h"
f11943c5 32#include "coredump-vacuum.h"
a0956174 33#include "dirent-util.h"
4f5dd394 34#include "escape.h"
3ffd4af2 35#include "fd-util.h"
4f5dd394 36#include "fileio.h"
f4f15635 37#include "fs-util.h"
afc5dbf3 38#include "io-util.h"
b18453ed 39#include "journal-importer.h"
4f5dd394
LP
40#include "log.h"
41#include "macro.h"
0c773903 42#include "missing.h"
4f5dd394 43#include "mkdir.h"
6bedfcbb 44#include "parse-util.h"
0b452006 45#include "process-util.h"
d14bcb4e 46#include "signal-util.h"
3c171f0b 47#include "socket-util.h"
4f5dd394
LP
48#include "special.h"
49#include "stacktrace.h"
8b43440b 50#include "string-table.h"
07630cea 51#include "string-util.h"
4f5dd394 52#include "strv.h"
b1d4f8e1 53#include "user-util.h"
4f5dd394 54#include "util.h"
34727273 55
34c10968 56/* The maximum size up to which we process coredumps */
59f448cf 57#define PROCESS_SIZE_MAX ((uint64_t) (2LLU*1024LLU*1024LLU*1024LLU))
34c10968 58
bdfd7b2c 59/* The maximum size up to which we leave the coredump around on disk */
34c10968
LP
60#define EXTERNAL_SIZE_MAX PROCESS_SIZE_MAX
61
bdfd7b2c 62/* The maximum size up to which we store the coredump in the journal */
34c10968 63#define JOURNAL_SIZE_MAX ((size_t) (767LU*1024LU*1024LU))
f5e04665 64
c4aa09b0 65/* Make sure to not make this larger than the maximum journal entry
874bc134
ZJS
66 * size. See DATA_SIZE_MAX in journald-native.c. */
67assert_cc(JOURNAL_SIZE_MAX <= DATA_SIZE_MAX);
f5e04665
LP
68
69enum {
ea5cc2a8
ZJS
70 /* We use this as array indexes for a couple of special fields we use for
71 * naming coredump files, and attaching xattrs, and for indexing argv[].
72
73 * Our pattern for man:systectl(1) kernel.core_pattern is such that the
74 * kernel passes fields until CONTEXT_RLIMIT as arguments in argv[]. After
75 * that it gets complicated: the kernel passes "comm" as one or more fields
76 * starting at index CONTEXT_COMM (in other words, full "comm" is under index
77 * CONTEXT_COMM when it does not contain spaces, which is the common
78 * case). This mapping is not reversible, so we prefer to retrieve "comm"
79 * from /proc. We only fall back to argv[CONTEXT_COMM...] when that fails.
80 *
81 * In the internal context[] array, fields before CONTEXT_COMM are the
82 * strings from argv[], so they should not be freed. The strings at indices
83 * CONTEXT_COMM and higher are allocated by us and should be freed at the
84 * end.
85 */
3c171f0b
LP
86 CONTEXT_PID,
87 CONTEXT_UID,
88 CONTEXT_GID,
89 CONTEXT_SIGNAL,
90 CONTEXT_TIMESTAMP,
bdfd7b2c 91 CONTEXT_RLIMIT,
f45b8015 92 CONTEXT_HOSTNAME,
3c171f0b
LP
93 CONTEXT_COMM,
94 CONTEXT_EXE,
92e92d71 95 CONTEXT_UNIT,
3c171f0b 96 _CONTEXT_MAX
f5e04665
LP
97};
98
34c10968
LP
99typedef enum CoredumpStorage {
100 COREDUMP_STORAGE_NONE,
101 COREDUMP_STORAGE_EXTERNAL,
102 COREDUMP_STORAGE_JOURNAL,
34c10968
LP
103 _COREDUMP_STORAGE_MAX,
104 _COREDUMP_STORAGE_INVALID = -1
105} CoredumpStorage;
106
34c10968
LP
107static const char* const coredump_storage_table[_COREDUMP_STORAGE_MAX] = {
108 [COREDUMP_STORAGE_NONE] = "none",
109 [COREDUMP_STORAGE_EXTERNAL] = "external",
110 [COREDUMP_STORAGE_JOURNAL] = "journal",
34c10968
LP
111};
112
113DEFINE_PRIVATE_STRING_TABLE_LOOKUP(coredump_storage, CoredumpStorage);
8c9571d0 114static DEFINE_CONFIG_PARSE_ENUM(config_parse_coredump_storage, coredump_storage, CoredumpStorage, "Failed to parse storage setting");
34727273
ZJS
115
116static CoredumpStorage arg_storage = COREDUMP_STORAGE_EXTERNAL;
8c9571d0 117static bool arg_compress = true;
59f448cf
LP
118static uint64_t arg_process_size_max = PROCESS_SIZE_MAX;
119static uint64_t arg_external_size_max = EXTERNAL_SIZE_MAX;
6e2b4a69 120static uint64_t arg_journal_size_max = JOURNAL_SIZE_MAX;
59f448cf
LP
121static uint64_t arg_keep_free = (uint64_t) -1;
122static uint64_t arg_max_use = (uint64_t) -1;
34c10968
LP
123
124static int parse_config(void) {
34c10968 125 static const ConfigTableItem items[] = {
8c9571d0
LP
126 { "Coredump", "Storage", config_parse_coredump_storage, 0, &arg_storage },
127 { "Coredump", "Compress", config_parse_bool, 0, &arg_compress },
59f448cf
LP
128 { "Coredump", "ProcessSizeMax", config_parse_iec_uint64, 0, &arg_process_size_max },
129 { "Coredump", "ExternalSizeMax", config_parse_iec_uint64, 0, &arg_external_size_max },
8c9571d0 130 { "Coredump", "JournalSizeMax", config_parse_iec_size, 0, &arg_journal_size_max },
59f448cf
LP
131 { "Coredump", "KeepFree", config_parse_iec_uint64, 0, &arg_keep_free },
132 { "Coredump", "MaxUse", config_parse_iec_uint64, 0, &arg_max_use },
34c10968
LP
133 {}
134 };
135
43688c49 136 return config_parse_many_nulstr(PKGSYSCONFDIR "/coredump.conf",
da412854
YW
137 CONF_PATHS_NULSTR("systemd/coredump.conf.d"),
138 "Coredump\0",
139 config_item_table_lookup, items,
bcde742e 140 CONFIG_PARSE_WARN, NULL);
34c10968
LP
141}
142
73a99163
ZJS
143static inline uint64_t storage_size_max(void) {
144 return arg_storage == COREDUMP_STORAGE_EXTERNAL ? arg_external_size_max : arg_journal_size_max;
145}
146
34c10968
LP
147static int fix_acl(int fd, uid_t uid) {
148
349cc4a5 149#if HAVE_ACL
34c10968
LP
150 _cleanup_(acl_freep) acl_t acl = NULL;
151 acl_entry_t entry;
152 acl_permset_t permset;
709f6e46 153 int r;
34c10968 154
b59233e6
LP
155 assert(fd >= 0);
156
05fd2156 157 if (uid_is_system(uid) || uid_is_dynamic(uid) || uid == UID_NOBODY)
34c10968
LP
158 return 0;
159
160 /* Make sure normal users can read (but not write or delete)
161 * their own coredumps */
162
163 acl = acl_get_fd(fd);
4a62c710
MS
164 if (!acl)
165 return log_error_errno(errno, "Failed to get ACL: %m");
34c10968
LP
166
167 if (acl_create_entry(&acl, &entry) < 0 ||
168 acl_set_tag_type(entry, ACL_USER) < 0 ||
d710aaf7
ZJS
169 acl_set_qualifier(entry, &uid) < 0)
170 return log_error_errno(errno, "Failed to patch ACL: %m");
34c10968
LP
171
172 if (acl_get_permset(entry, &permset) < 0 ||
709f6e46
MS
173 acl_add_perm(permset, ACL_READ) < 0)
174 return log_warning_errno(errno, "Failed to patch ACL: %m");
175
176 r = calc_acl_mask_if_needed(&acl);
177 if (r < 0)
178 return log_warning_errno(r, "Failed to patch ACL: %m");
34c10968 179
4a62c710
MS
180 if (acl_set_fd(fd, acl) < 0)
181 return log_error_errno(errno, "Failed to apply ACL: %m");
34c10968
LP
182#endif
183
184 return 0;
185}
186
3c171f0b 187static int fix_xattr(int fd, const char *context[_CONTEXT_MAX]) {
0cd77f97 188
3c171f0b
LP
189 static const char * const xattrs[_CONTEXT_MAX] = {
190 [CONTEXT_PID] = "user.coredump.pid",
191 [CONTEXT_UID] = "user.coredump.uid",
192 [CONTEXT_GID] = "user.coredump.gid",
193 [CONTEXT_SIGNAL] = "user.coredump.signal",
194 [CONTEXT_TIMESTAMP] = "user.coredump.timestamp",
6d337300 195 [CONTEXT_RLIMIT] = "user.coredump.rlimit",
f45b8015 196 [CONTEXT_HOSTNAME] = "user.coredump.hostname",
3c171f0b
LP
197 [CONTEXT_COMM] = "user.coredump.comm",
198 [CONTEXT_EXE] = "user.coredump.exe",
0cd77f97
LP
199 };
200
34c10968 201 int r = 0;
0cd77f97 202 unsigned i;
34c10968 203
b59233e6
LP
204 assert(fd >= 0);
205
1eef15b1 206 /* Attach some metadata to coredumps via extended
34c10968
LP
207 * attributes. Just because we can. */
208
3c171f0b 209 for (i = 0; i < _CONTEXT_MAX; i++) {
1eef15b1
ZJS
210 int k;
211
3c171f0b 212 if (isempty(context[i]) || !xattrs[i])
0cd77f97 213 continue;
34c10968 214
3c171f0b 215 k = fsetxattr(fd, xattrs[i], context[i], strlen(context[i]), XATTR_CREATE);
1eef15b1 216 if (k < 0 && r == 0)
34c10968 217 r = -errno;
0cd77f97 218 }
34c10968
LP
219
220 return r;
221}
222
b0b21dce 223#define filename_escape(s) xescape((s), "./ ")
34c10968 224
0c773903
EV
225static inline const char *coredump_tmpfile_name(const char *s) {
226 return s ? s : "(unnamed temporary file)";
227}
228
b59233e6
LP
229static int fix_permissions(
230 int fd,
231 const char *filename,
232 const char *target,
3c171f0b 233 const char *context[_CONTEXT_MAX],
b59233e6
LP
234 uid_t uid) {
235
03532f0a
LP
236 int r;
237
b59233e6 238 assert(fd >= 0);
b59233e6 239 assert(target);
3c171f0b 240 assert(context);
cfd652ed
ZJS
241
242 /* Ignore errors on these */
3c171f0b
LP
243 (void) fchmod(fd, 0640);
244 (void) fix_acl(fd, uid);
245 (void) fix_xattr(fd, context);
cfd652ed 246
4a62c710 247 if (fsync(fd) < 0)
0c773903 248 return log_error_errno(errno, "Failed to sync coredump %s: %m", coredump_tmpfile_name(filename));
cfd652ed 249
8ac2f74f
LP
250 (void) fsync_directory_of_file(fd);
251
03532f0a
LP
252 r = link_tmpfile(fd, filename, target);
253 if (r < 0)
254 return log_error_errno(r, "Failed to move coredump %s into place: %m", target);
cfd652ed
ZJS
255
256 return 0;
257}
258
59f448cf 259static int maybe_remove_external_coredump(const char *filename, uint64_t size) {
cfd652ed 260
b59233e6 261 /* Returns 1 if might remove, 0 if will not remove, < 0 on error. */
cfd652ed 262
fc6cec86 263 if (arg_storage == COREDUMP_STORAGE_EXTERNAL &&
cfd652ed
ZJS
264 size <= arg_external_size_max)
265 return 0;
266
267 if (!filename)
268 return 1;
269
4a62c710
MS
270 if (unlink(filename) < 0 && errno != ENOENT)
271 return log_error_errno(errno, "Failed to unlink %s: %m", filename);
cfd652ed
ZJS
272
273 return 1;
274}
275
3c171f0b 276static int make_filename(const char *context[_CONTEXT_MAX], char **ret) {
b59233e6 277 _cleanup_free_ char *c = NULL, *u = NULL, *p = NULL, *t = NULL;
a7f7d1bd 278 sd_id128_t boot = {};
34c10968
LP
279 int r;
280
3c171f0b 281 assert(context);
34c10968 282
3c171f0b 283 c = filename_escape(context[CONTEXT_COMM]);
34c10968 284 if (!c)
b59233e6 285 return -ENOMEM;
34c10968 286
3c171f0b 287 u = filename_escape(context[CONTEXT_UID]);
0dc5d23c 288 if (!u)
b59233e6 289 return -ENOMEM;
34c10968
LP
290
291 r = sd_id128_get_boot(&boot);
b59233e6 292 if (r < 0)
34c10968 293 return r;
34c10968 294
3c171f0b 295 p = filename_escape(context[CONTEXT_PID]);
b59233e6
LP
296 if (!p)
297 return -ENOMEM;
298
3c171f0b 299 t = filename_escape(context[CONTEXT_TIMESTAMP]);
b59233e6
LP
300 if (!t)
301 return -ENOMEM;
302
303 if (asprintf(ret,
0dc5d23c 304 "/var/lib/systemd/coredump/core.%s.%s." SD_ID128_FORMAT_STR ".%s.%s000000",
34c10968 305 c,
0dc5d23c 306 u,
34c10968
LP
307 SD_ID128_FORMAT_VAL(boot),
308 p,
b59233e6
LP
309 t) < 0)
310 return -ENOMEM;
311
312 return 0;
313}
314
315static int save_external_coredump(
3c171f0b
LP
316 const char *context[_CONTEXT_MAX],
317 int input_fd,
b59233e6 318 char **ret_filename,
5f3e0a74
HW
319 int *ret_node_fd,
320 int *ret_data_fd,
0cd4e913 321 uint64_t *ret_size,
cc4419ed 322 bool *ret_truncated) {
b59233e6
LP
323
324 _cleanup_free_ char *fn = NULL, *tmp = NULL;
325 _cleanup_close_ int fd = -1;
bdfd7b2c 326 uint64_t rlimit, max_size;
b59233e6 327 struct stat st;
3c171f0b 328 uid_t uid;
b59233e6
LP
329 int r;
330
3c171f0b 331 assert(context);
b59233e6 332 assert(ret_filename);
5f3e0a74
HW
333 assert(ret_node_fd);
334 assert(ret_data_fd);
b59233e6
LP
335 assert(ret_size);
336
3c171f0b
LP
337 r = parse_uid(context[CONTEXT_UID], &uid);
338 if (r < 0)
339 return log_error_errno(r, "Failed to parse UID: %m");
340
bdfd7b2c
LP
341 r = safe_atou64(context[CONTEXT_RLIMIT], &rlimit);
342 if (r < 0)
343 return log_error_errno(r, "Failed to parse resource limit: %s", context[CONTEXT_RLIMIT]);
6998b540
ZJS
344 if (rlimit < page_size()) {
345 /* Is coredumping disabled? Then don't bother saving/processing the coredump.
346 * Anything below PAGE_SIZE cannot give a readable coredump (the kernel uses
347 * ELF_EXEC_PAGESIZE which is not easily accessible, but is usually the same as PAGE_SIZE. */
73a99163
ZJS
348 log_info("Resource limits disable core dumping for process %s (%s).",
349 context[CONTEXT_PID], context[CONTEXT_COMM]);
bdfd7b2c
LP
350 return -EBADSLT;
351 }
352
353 /* Never store more than the process configured, or than we actually shall keep or process */
73a99163 354 max_size = MIN(rlimit, MAX(arg_process_size_max, storage_size_max()));
bdfd7b2c 355
3c171f0b 356 r = make_filename(context, &fn);
23bbb0de
MS
357 if (r < 0)
358 return log_error_errno(r, "Failed to determine coredump file name: %m");
34c10968 359
d2e54fae 360 mkdir_p_label("/var/lib/systemd/coredump", 0755);
803a3464 361
03532f0a 362 fd = open_tmpfile_linkable(fn, O_RDWR|O_CLOEXEC, &tmp);
4a62c710 363 if (fd < 0)
03532f0a 364 return log_error_errno(fd, "Failed to create temporary file for coredump %s: %m", fn);
803a3464 365
1c876927 366 r = copy_bytes(input_fd, fd, max_size, 0);
73a99163
ZJS
367 if (r < 0) {
368 log_error_errno(r, "Cannot store coredump of %s (%s): %m", context[CONTEXT_PID], context[CONTEXT_COMM]);
93240d3a 369 goto fail;
0cd4e913 370 }
cc4419ed
ZJS
371 *ret_truncated = r == 1;
372 if (*ret_truncated)
73a99163
ZJS
373 log_struct(LOG_INFO,
374 LOG_MESSAGE("Core file was truncated to %zu bytes.", max_size),
375 "SIZE_LIMIT=%zu", max_size,
2b044526 376 "MESSAGE_ID=" SD_MESSAGE_TRUNCATED_CORE_STR,
73a99163 377 NULL);
803a3464 378
34c10968 379 if (fstat(fd, &st) < 0) {
73a99163 380 log_error_errno(errno, "Failed to fstat core file %s: %m", coredump_tmpfile_name(tmp));
34c10968
LP
381 goto fail;
382 }
383
7849c2ac 384 if (lseek(fd, 0, SEEK_SET) == (off_t) -1) {
0c773903 385 log_error_errno(errno, "Failed to seek on %s: %m", coredump_tmpfile_name(tmp));
b59233e6 386 goto fail;
7849c2ac
TA
387 }
388
349cc4a5 389#if HAVE_XZ || HAVE_LZ4
cfd652ed 390 /* If we will remove the coredump anyway, do not compress. */
6e9ef603 391 if (arg_compress && !maybe_remove_external_coredump(NULL, st.st_size)) {
cfd652ed 392
b59233e6
LP
393 _cleanup_free_ char *fn_compressed = NULL, *tmp_compressed = NULL;
394 _cleanup_close_ int fd_compressed = -1;
cfd652ed 395
d89c8fdf 396 fn_compressed = strappend(fn, COMPRESSED_EXT);
b59233e6 397 if (!fn_compressed) {
d89c8fdf 398 log_oom();
cfd652ed
ZJS
399 goto uncompressed;
400 }
401
03532f0a
LP
402 fd_compressed = open_tmpfile_linkable(fn_compressed, O_RDWR|O_CLOEXEC, &tmp_compressed);
403 if (fd_compressed < 0) {
404 log_error_errno(fd_compressed, "Failed to create temporary file for coredump %s: %m", fn_compressed);
b59233e6 405 goto uncompressed;
03532f0a 406 }
cfd652ed 407
d89c8fdf 408 r = compress_stream(fd, fd_compressed, -1);
b59233e6 409 if (r < 0) {
0c773903 410 log_error_errno(r, "Failed to compress %s: %m", coredump_tmpfile_name(tmp_compressed));
b59233e6
LP
411 goto fail_compressed;
412 }
413
3c171f0b 414 r = fix_permissions(fd_compressed, tmp_compressed, fn_compressed, context, uid);
cfd652ed 415 if (r < 0)
b59233e6
LP
416 goto fail_compressed;
417
418 /* OK, this worked, we can get rid of the uncompressed version now */
0c773903
EV
419 if (tmp)
420 unlink_noerrno(tmp);
cfd652ed 421
1cc6c93a
YW
422 *ret_filename = TAKE_PTR(fn_compressed); /* compressed */
423 *ret_node_fd = TAKE_FD(fd_compressed); /* compressed */
424 *ret_data_fd = TAKE_FD(fd); /* uncompressed */
59f448cf 425 *ret_size = (uint64_t) st.st_size; /* uncompressed */
cfd652ed 426
cfd652ed
ZJS
427 return 0;
428
b59233e6 429 fail_compressed:
0c773903
EV
430 if (tmp_compressed)
431 (void) unlink(tmp_compressed);
34c10968 432 }
cfd652ed
ZJS
433
434uncompressed:
3b1a55e1 435#endif
5f3e0a74 436
3c171f0b 437 r = fix_permissions(fd, tmp, fn, context, uid);
cfd652ed
ZJS
438 if (r < 0)
439 goto fail;
34c10968 440
1cc6c93a
YW
441 *ret_filename = TAKE_PTR(fn);
442 *ret_data_fd = TAKE_FD(fd);
5f3e0a74 443 *ret_node_fd = -1;
59f448cf 444 *ret_size = (uint64_t) st.st_size;
34c10968 445
34c10968
LP
446 return 0;
447
448fail:
0c773903
EV
449 if (tmp)
450 (void) unlink(tmp);
34c10968
LP
451 return r;
452}
453
454static int allocate_journal_field(int fd, size_t size, char **ret, size_t *ret_size) {
455 _cleanup_free_ char *field = NULL;
456 ssize_t n;
457
8d4e028f 458 assert(fd >= 0);
34c10968
LP
459 assert(ret);
460 assert(ret_size);
461
4a62c710
MS
462 if (lseek(fd, 0, SEEK_SET) == (off_t) -1)
463 return log_warning_errno(errno, "Failed to seek: %m");
803a3464 464
34c10968
LP
465 field = malloc(9 + size);
466 if (!field) {
cfd652ed 467 log_warning("Failed to allocate memory for coredump, coredump will not be stored.");
34c10968
LP
468 return -ENOMEM;
469 }
470
471 memcpy(field, "COREDUMP=", 9);
472
473 n = read(fd, field + 9, size);
23bbb0de
MS
474 if (n < 0)
475 return log_error_errno((int) n, "Failed to read core data: %m");
34c10968
LP
476 if ((size_t) n < size) {
477 log_error("Core data too short.");
478 return -EIO;
479 }
480
1cc6c93a 481 *ret = TAKE_PTR(field);
34c10968
LP
482 *ret_size = size + 9;
483
34c10968
LP
484 return 0;
485}
803a3464 486
3f132692
JF
487/* Joins /proc/[pid]/fd/ and /proc/[pid]/fdinfo/ into the following lines:
488 * 0:/dev/pts/23
489 * pos: 0
490 * flags: 0100002
491 *
492 * 1:/dev/pts/23
493 * pos: 0
494 * flags: 0100002
495 *
496 * 2:/dev/pts/23
497 * pos: 0
498 * flags: 0100002
499 * EOF
500 */
501static int compose_open_fds(pid_t pid, char **open_fds) {
4d84bc2f
LP
502 _cleanup_closedir_ DIR *proc_fd_dir = NULL;
503 _cleanup_close_ int proc_fdinfo_fd = -1;
504 _cleanup_free_ char *buffer = NULL;
3f132692 505 _cleanup_fclose_ FILE *stream = NULL;
59059b4a 506 const char *fddelim = "", *path;
3f132692 507 struct dirent *dent = NULL;
4d84bc2f 508 size_t size = 0;
3f132692
JF
509 int r = 0;
510
511 assert(pid >= 0);
512 assert(open_fds != NULL);
513
59059b4a 514 path = procfs_file_alloca(pid, "fd");
3f132692 515 proc_fd_dir = opendir(path);
59059b4a
ZJS
516 if (!proc_fd_dir)
517 return -errno;
3f132692 518
4d84bc2f 519 proc_fdinfo_fd = openat(dirfd(proc_fd_dir), "../fdinfo", O_DIRECTORY|O_NOFOLLOW|O_CLOEXEC|O_PATH);
59059b4a
ZJS
520 if (proc_fdinfo_fd < 0)
521 return -errno;
3f132692 522
4d84bc2f 523 stream = open_memstream(&buffer, &size);
3f132692
JF
524 if (!stream)
525 return -ENOMEM;
526
0d536673
LP
527 (void) __fsetlocking(stream, FSETLOCKING_BYCALLER);
528
4d84bc2f 529 FOREACH_DIRENT(dent, proc_fd_dir, return -errno) {
3f132692 530 _cleanup_fclose_ FILE *fdinfo = NULL;
4d84bc2f 531 _cleanup_free_ char *fdname = NULL;
59059b4a 532 char line[LINE_MAX];
4d84bc2f 533 int fd;
3f132692 534
59059b4a 535 r = readlinkat_malloc(dirfd(proc_fd_dir), dent->d_name, &fdname);
3f132692
JF
536 if (r < 0)
537 return r;
538
539 fprintf(stream, "%s%s:%s\n", fddelim, dent->d_name, fdname);
540 fddelim = "\n";
541
542 /* Use the directory entry from /proc/[pid]/fd with /proc/[pid]/fdinfo */
59059b4a
ZJS
543 fd = openat(proc_fdinfo_fd, dent->d_name, O_NOFOLLOW|O_CLOEXEC|O_RDONLY);
544 if (fd < 0)
3f132692
JF
545 continue;
546
59059b4a 547 fdinfo = fdopen(fd, "re");
234519ae 548 if (!fdinfo) {
0d536673 549 safe_close(fd);
3f132692 550 continue;
59059b4a 551 }
3f132692 552
4d84bc2f 553 FOREACH_LINE(line, fdinfo, break) {
0d536673 554 fputs(line, stream);
4d84bc2f
LP
555 if (!endswith(line, "\n"))
556 fputc('\n', stream);
557 }
3f132692
JF
558 }
559
4d84bc2f 560 errno = 0;
74ca738f 561 stream = safe_fclose(stream);
4d84bc2f 562
b3267152 563 if (errno > 0)
4d84bc2f
LP
564 return -errno;
565
ae2a15bc 566 *open_fds = TAKE_PTR(buffer);
4d84bc2f 567
3f132692
JF
568 return 0;
569}
570
7ed03ce6
JF
571static int get_process_ns(pid_t pid, const char *namespace, ino_t *ns) {
572 const char *p;
573 struct stat stbuf;
574 _cleanup_close_ int proc_ns_dir_fd;
575
576 p = procfs_file_alloca(pid, "ns");
577
578 proc_ns_dir_fd = open(p, O_DIRECTORY | O_CLOEXEC | O_RDONLY);
579 if (proc_ns_dir_fd < 0)
580 return -errno;
581
582 if (fstatat(proc_ns_dir_fd, namespace, &stbuf, /* flags */0) < 0)
583 return -errno;
584
585 *ns = stbuf.st_ino;
586 return 0;
587}
588
589static int get_mount_namespace_leader(pid_t pid, pid_t *container_pid) {
590 pid_t cpid = pid, ppid = 0;
591 ino_t proc_mntns;
592 int r = 0;
593
594 r = get_process_ns(pid, "mnt", &proc_mntns);
595 if (r < 0)
596 return r;
597
aa7530d6 598 for (;;) {
7ed03ce6
JF
599 ino_t parent_mntns;
600
601 r = get_process_ppid(cpid, &ppid);
602 if (r < 0)
603 return r;
604
605 r = get_process_ns(ppid, "mnt", &parent_mntns);
606 if (r < 0)
607 return r;
608
609 if (proc_mntns != parent_mntns)
610 break;
611
612 if (ppid == 1)
613 return -ENOENT;
614
615 cpid = ppid;
616 }
617
618 *container_pid = ppid;
619 return 0;
620}
621
622/* Returns 1 if the parent was found.
623 * Returns 0 if there is not a process we can call the pid's
624 * container parent (the pid's process isn't 'containerized').
625 * Returns a negative number on errors.
626 */
627static int get_process_container_parent_cmdline(pid_t pid, char** cmdline) {
628 int r = 0;
629 pid_t container_pid;
630 const char *proc_root_path;
631 struct stat root_stat, proc_root_stat;
632
633 /* To compare inodes of / and /proc/[pid]/root */
634 if (stat("/", &root_stat) < 0)
635 return -errno;
636
637 proc_root_path = procfs_file_alloca(pid, "root");
638 if (stat(proc_root_path, &proc_root_stat) < 0)
639 return -errno;
640
641 /* The process uses system root. */
642 if (proc_root_stat.st_ino == root_stat.st_ino) {
643 *cmdline = NULL;
644 return 0;
645 }
646
647 r = get_mount_namespace_leader(pid, &container_pid);
648 if (r < 0)
649 return r;
650
d3cba4ea
EV
651 r = get_process_cmdline(container_pid, 0, false, cmdline);
652 if (r < 0)
653 return r;
654
655 return 1;
7ed03ce6
JF
656}
657
3c171f0b
LP
658static int change_uid_gid(const char *context[]) {
659 uid_t uid;
660 gid_t gid;
661 int r;
34c10968 662
3c171f0b
LP
663 r = parse_uid(context[CONTEXT_UID], &uid);
664 if (r < 0)
665 return r;
8c8549db 666
888e378d
LP
667 if (uid <= SYSTEM_UID_MAX) {
668 const char *user = "systemd-coredump";
669
670 r = get_user_creds(&user, &uid, &gid, NULL, NULL);
671 if (r < 0) {
672 log_warning_errno(r, "Cannot resolve %s user. Proceeding to dump core as root: %m", user);
673 uid = gid = 0;
674 }
675 } else {
676 r = parse_gid(context[CONTEXT_GID], &gid);
677 if (r < 0)
678 return r;
679 }
3c171f0b
LP
680
681 return drop_privileges(uid, gid, 0);
682}
8c8549db 683
b8cda92d
LP
684static bool is_journald_crash(const char *context[_CONTEXT_MAX]) {
685 assert(context);
686
687 return streq_ptr(context[CONTEXT_UNIT], SPECIAL_JOURNALD_SERVICE);
688}
689
690static bool is_pid1_crash(const char *context[_CONTEXT_MAX]) {
691 assert(context);
692
693 return streq_ptr(context[CONTEXT_UNIT], SPECIAL_INIT_SCOPE) ||
694 streq_ptr(context[CONTEXT_PID], "1");
695}
696
0cd4e913
ZJS
697#define SUBMIT_COREDUMP_FIELDS 4
698
3c171f0b
LP
699static int submit_coredump(
700 const char *context[_CONTEXT_MAX],
701 struct iovec *iovec,
702 size_t n_iovec_allocated,
703 size_t n_iovec,
704 int input_fd) {
34c10968 705
5f3e0a74 706 _cleanup_close_ int coredump_fd = -1, coredump_node_fd = -1;
3c171f0b 707 _cleanup_free_ char *core_message = NULL, *filename = NULL, *coredump_data = NULL;
a5ca3649 708 uint64_t coredump_size = UINT64_MAX;
92e92d71 709 bool truncated = false, journald_crash;
3c171f0b 710 int r;
f5e04665 711
3c171f0b
LP
712 assert(context);
713 assert(iovec);
0cd4e913 714 assert(n_iovec_allocated >= n_iovec + SUBMIT_COREDUMP_FIELDS);
3c171f0b 715 assert(input_fd >= 0);
f5e04665 716
b8cda92d 717 journald_crash = is_journald_crash(context);
92e92d71 718
3c171f0b
LP
719 /* Vacuum before we write anything again */
720 (void) coredump_vacuum(-1, arg_keep_free, arg_max_use);
803a3464 721
3c171f0b 722 /* Always stream the coredump to disk, if that's possible */
0cd4e913
ZJS
723 r = save_external_coredump(context, input_fd,
724 &filename, &coredump_node_fd, &coredump_fd, &coredump_size, &truncated);
3c171f0b
LP
725 if (r < 0)
726 /* Skip whole core dumping part */
727 goto log;
728
729 /* If we don't want to keep the coredump on disk, remove it now, as later on we will lack the privileges for
730 * it. However, we keep the fd to it, so that we can still process it and log it. */
731 r = maybe_remove_external_coredump(filename, coredump_size);
732 if (r < 0)
733 return r;
734 if (r == 0) {
735 const char *coredump_filename;
736
737 coredump_filename = strjoina("COREDUMP_FILENAME=", filename);
e6a7ec4b 738 iovec[n_iovec++] = IOVEC_MAKE_STRING(coredump_filename);
6e9ef603 739 } else if (arg_storage == COREDUMP_STORAGE_EXTERNAL)
5206a724 740 log_info("The core will not be stored: size %"PRIu64" is greater than %"PRIu64" (the configured maximum)",
6e9ef603 741 coredump_size, arg_external_size_max);
f5e04665 742
3c171f0b
LP
743 /* Vacuum again, but exclude the coredump we just created */
744 (void) coredump_vacuum(coredump_node_fd >= 0 ? coredump_node_fd : coredump_fd, arg_keep_free, arg_max_use);
8c9571d0 745
3c171f0b
LP
746 /* Now, let's drop privileges to become the user who owns the segfaulted process and allocate the coredump
747 * memory under the user's uid. This also ensures that the credentials journald will see are the ones of the
748 * coredumping user, thus making sure the user gets access to the core dump. Let's also get rid of all
749 * capabilities, if we run as root, we won't need them anymore. */
750 r = change_uid_gid(context);
751 if (r < 0)
752 return log_error_errno(r, "Failed to drop privileges: %m");
34c10968 753
349cc4a5 754#if HAVE_ELFUTILS
3c171f0b
LP
755 /* Try to get a strack trace if we can */
756 if (coredump_size <= arg_process_size_max) {
757 _cleanup_free_ char *stacktrace = NULL;
758
759 r = coredump_make_stack_trace(coredump_fd, context[CONTEXT_EXE], &stacktrace);
760 if (r >= 0)
605405c6
ZJS
761 core_message = strjoin("MESSAGE=Process ", context[CONTEXT_PID],
762 " (", context[CONTEXT_COMM], ") of user ",
92e92d71
ZJS
763 context[CONTEXT_UID], " dumped core.",
764 journald_crash ? "\nCoredump diverted to " : "",
765 journald_crash ? filename : "",
766 "\n\n", stacktrace);
3c171f0b
LP
767 else if (r == -EINVAL)
768 log_warning("Failed to generate stack trace: %s", dwfl_errmsg(dwfl_errno()));
769 else
770 log_warning_errno(r, "Failed to generate stack trace: %m");
6e9ef603 771 } else
5206a724 772 log_debug("Not generating stack trace: core size %"PRIu64" is greater than %"PRIu64" (the configured maximum)",
6e9ef603 773 coredump_size, arg_process_size_max);
803a3464 774
3c171f0b
LP
775 if (!core_message)
776#endif
777log:
92e92d71
ZJS
778 core_message = strjoin("MESSAGE=Process ", context[CONTEXT_PID],
779 " (", context[CONTEXT_COMM], ") of user ",
780 context[CONTEXT_UID], " dumped core.",
781 journald_crash ? "\nCoredump diverted to " : NULL,
782 journald_crash ? filename : NULL);
783 if (!core_message)
784 return log_oom();
785
786 if (journald_crash) {
787 /* We cannot log to the journal, so just print the MESSAGE.
788 * The target was set previously to something safe. */
ae2173d6 789 log_dispatch(LOG_ERR, 0, core_message);
92e92d71
ZJS
790 return 0;
791 }
792
e6a7ec4b 793 iovec[n_iovec++] = IOVEC_MAKE_STRING(core_message);
3c171f0b 794
0cd4e913 795 if (truncated)
e6a7ec4b 796 iovec[n_iovec++] = IOVEC_MAKE_STRING("COREDUMP_TRUNCATED=1");
0cd4e913 797
3c171f0b 798 /* Optionally store the entire coredump in the journal */
6e9ef603
ZJS
799 if (arg_storage == COREDUMP_STORAGE_JOURNAL) {
800 if (coredump_size <= arg_journal_size_max) {
801 size_t sz = 0;
802
803 /* Store the coredump itself in the journal */
804
805 r = allocate_journal_field(coredump_fd, (size_t) coredump_size, &coredump_data, &sz);
e6a7ec4b
LP
806 if (r >= 0)
807 iovec[n_iovec++] = IOVEC_MAKE(coredump_data, sz);
808 else
6e9ef603
ZJS
809 log_warning_errno(r, "Failed to attach the core to the journal entry: %m");
810 } else
5206a724 811 log_info("The core will not be stored: size %"PRIu64" is greater than %"PRIu64" (the configured maximum)",
6e9ef603 812 coredump_size, arg_journal_size_max);
f5e04665
LP
813 }
814
3c171f0b
LP
815 assert(n_iovec <= n_iovec_allocated);
816
817 r = sd_journal_sendv(iovec, n_iovec);
818 if (r < 0)
819 return log_error_errno(r, "Failed to log coredump: %m");
820
821 return 0;
822}
823
ea5cc2a8 824static void map_context_fields(const struct iovec *iovec, const char* context[]) {
3c171f0b 825
92e92d71 826 static const char * const context_field_names[] = {
3c171f0b
LP
827 [CONTEXT_PID] = "COREDUMP_PID=",
828 [CONTEXT_UID] = "COREDUMP_UID=",
829 [CONTEXT_GID] = "COREDUMP_GID=",
830 [CONTEXT_SIGNAL] = "COREDUMP_SIGNAL=",
831 [CONTEXT_TIMESTAMP] = "COREDUMP_TIMESTAMP=",
92e92d71 832 [CONTEXT_RLIMIT] = "COREDUMP_RLIMIT=",
f45b8015 833 [CONTEXT_HOSTNAME] = "COREDUMP_HOSTNAME=",
3c171f0b
LP
834 [CONTEXT_COMM] = "COREDUMP_COMM=",
835 [CONTEXT_EXE] = "COREDUMP_EXE=",
836 };
837
838 unsigned i;
839
840 assert(iovec);
841 assert(context);
842
92e92d71 843 for (i = 0; i < ELEMENTSOF(context_field_names); i++) {
3c171f0b
LP
844 size_t l;
845
92e92d71
ZJS
846 if (!context_field_names[i])
847 continue;
848
3c171f0b
LP
849 l = strlen(context_field_names[i]);
850 if (iovec->iov_len < l)
851 continue;
852
853 if (memcmp(iovec->iov_base, context_field_names[i], l) != 0)
854 continue;
855
856 /* Note that these strings are NUL terminated, because we made sure that a trailing NUL byte is in the
857 * buffer, though not included in the iov_len count. (see below) */
858 context[i] = (char*) iovec->iov_base + l;
859 break;
860 }
861}
862
863static int process_socket(int fd) {
864 _cleanup_close_ int coredump_fd = -1;
865 struct iovec *iovec = NULL;
80002f66 866 size_t n_iovec = 0, n_allocated = 0, i, k;
3c171f0b
LP
867 const char *context[_CONTEXT_MAX] = {};
868 int r;
869
870 assert(fd >= 0);
871
872 log_set_target(LOG_TARGET_AUTO);
873 log_parse_environment();
874 log_open();
875
988e89ee
ZJS
876 log_debug("Processing coredump received on stdin...");
877
3c171f0b
LP
878 for (;;) {
879 union {
880 struct cmsghdr cmsghdr;
881 uint8_t buf[CMSG_SPACE(sizeof(int))];
882 } control = {};
883 struct msghdr mh = {
884 .msg_control = &control,
885 .msg_controllen = sizeof(control),
886 .msg_iovlen = 1,
887 };
888 ssize_t n;
fe1ef0f8 889 ssize_t l;
3c171f0b 890
0cd4e913 891 if (!GREEDY_REALLOC(iovec, n_allocated, n_iovec + SUBMIT_COREDUMP_FIELDS)) {
3c171f0b
LP
892 r = log_oom();
893 goto finish;
894 }
895
fe1ef0f8
EV
896 l = next_datagram_size_fd(fd);
897 if (l < 0) {
898 r = log_error_errno(l, "Failed to determine datagram size to read: %m");
3c171f0b
LP
899 goto finish;
900 }
901
902 assert(l >= 0);
903
904 iovec[n_iovec].iov_len = l;
905 iovec[n_iovec].iov_base = malloc(l + 1);
3c171f0b
LP
906 if (!iovec[n_iovec].iov_base) {
907 r = log_oom();
908 goto finish;
909 }
910
911 mh.msg_iov = iovec + n_iovec;
912
913 n = recvmsg(fd, &mh, MSG_NOSIGNAL|MSG_CMSG_CLOEXEC);
914 if (n < 0) {
915 free(iovec[n_iovec].iov_base);
916 r = log_error_errno(errno, "Failed to receive datagram: %m");
917 goto finish;
918 }
919
920 if (n == 0) {
921 struct cmsghdr *cmsg, *found = NULL;
922 /* The final zero-length datagram carries the file descriptor and tells us that we're done. */
923
924 free(iovec[n_iovec].iov_base);
925
926 CMSG_FOREACH(cmsg, &mh) {
927 if (cmsg->cmsg_level == SOL_SOCKET &&
928 cmsg->cmsg_type == SCM_RIGHTS &&
929 cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
930 assert(!found);
931 found = cmsg;
932 }
933 }
934
935 if (!found) {
936 log_error("Coredump file descriptor missing.");
937 r = -EBADMSG;
938 goto finish;
939 }
940
941 assert(coredump_fd < 0);
942 coredump_fd = *(int*) CMSG_DATA(found);
943 break;
944 }
945
946 /* Add trailing NUL byte, in case these are strings */
947 ((char*) iovec[n_iovec].iov_base)[n] = 0;
948 iovec[n_iovec].iov_len = (size_t) n;
949
950 cmsg_close_all(&mh);
951 map_context_fields(iovec + n_iovec, context);
952 n_iovec++;
953 }
954
0cd4e913 955 if (!GREEDY_REALLOC(iovec, n_allocated, n_iovec + SUBMIT_COREDUMP_FIELDS)) {
3c171f0b 956 r = log_oom();
34c10968
LP
957 goto finish;
958 }
959
61233823 960 /* Make sure we got all data we really need */
3c171f0b
LP
961 assert(context[CONTEXT_PID]);
962 assert(context[CONTEXT_UID]);
963 assert(context[CONTEXT_GID]);
964 assert(context[CONTEXT_SIGNAL]);
965 assert(context[CONTEXT_TIMESTAMP]);
bdfd7b2c 966 assert(context[CONTEXT_RLIMIT]);
f45b8015 967 assert(context[CONTEXT_HOSTNAME]);
3c171f0b
LP
968 assert(context[CONTEXT_COMM]);
969 assert(coredump_fd >= 0);
970
80002f66
LP
971 /* Small quirk: the journal fields contain the timestamp padded with six zeroes, so that the kernel-supplied 1s
972 * granularity timestamps becomes 1µs granularity, i.e. the granularity systemd usually operates in. Since we
973 * are reconstructing the original kernel context, we chop this off again, here. */
974 k = strlen(context[CONTEXT_TIMESTAMP]);
975 if (k > 6)
976 context[CONTEXT_TIMESTAMP] = strndupa(context[CONTEXT_TIMESTAMP], k - 6);
977
5b45a160 978 r = submit_coredump(context, iovec, n_allocated, n_iovec, coredump_fd);
3c171f0b
LP
979
980finish:
981 for (i = 0; i < n_iovec; i++)
982 free(iovec[i].iov_base);
983 free(iovec);
984
985 return r;
986}
987
988static int send_iovec(const struct iovec iovec[], size_t n_iovec, int input_fd) {
989
990 static const union sockaddr_union sa = {
991 .un.sun_family = AF_UNIX,
992 .un.sun_path = "/run/systemd/coredump",
993 };
994 _cleanup_close_ int fd = -1;
995 size_t i;
996 int r;
997
998 assert(iovec || n_iovec <= 0);
999 assert(input_fd >= 0);
1000
1001 fd = socket(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0);
1002 if (fd < 0)
1003 return log_error_errno(errno, "Failed to create coredump socket: %m");
1004
fc2fffe7 1005 if (connect(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0)
3c171f0b
LP
1006 return log_error_errno(errno, "Failed to connect to coredump service: %m");
1007
1008 for (i = 0; i < n_iovec; i++) {
fec603eb
LP
1009 struct msghdr mh = {
1010 .msg_iov = (struct iovec*) iovec + i,
1011 .msg_iovlen = 1,
1012 };
1013 struct iovec copy[2];
1014
1015 for (;;) {
1016 if (sendmsg(fd, &mh, MSG_NOSIGNAL) >= 0)
1017 break;
1018
1019 if (errno == EMSGSIZE && mh.msg_iov[0].iov_len > 0) {
1020 /* This field didn't fit? That's a pity. Given that this is just metadata,
1021 * let's truncate the field at half, and try again. We append three dots, in
1022 * order to show that this is truncated. */
1023
1024 if (mh.msg_iov != copy) {
1025 /* We don't want to modify the caller's iovec, hence let's create our
1026 * own array, consisting of two new iovecs, where the first is a
1027 * (truncated) copy of what we want to send, and the second one
1028 * contains the trailing dots. */
1029 copy[0] = iovec[i];
1030 copy[1] = (struct iovec) {
1031 .iov_base = (char[]) { '.', '.', '.' },
1032 .iov_len = 3,
1033 };
1034
1035 mh.msg_iov = copy;
1036 mh.msg_iovlen = 2;
1037 }
1038
1039 copy[0].iov_len /= 2; /* halve it, and try again */
1040 continue;
1041 }
3c171f0b 1042
3c171f0b 1043 return log_error_errno(errno, "Failed to send coredump datagram: %m");
fec603eb 1044 }
1eef15b1
ZJS
1045 }
1046
3c171f0b
LP
1047 r = send_one_fd(fd, input_fd, 0);
1048 if (r < 0)
1049 return log_error_errno(r, "Failed to send coredump fd: %m");
1eef15b1 1050
3c171f0b
LP
1051 return 0;
1052}
1eef15b1 1053
f45b8015 1054static char* set_iovec_field(struct iovec *iovec, size_t *n_iovec, const char *field, const char *value) {
9aa82023 1055 char *x;
3c171f0b 1056
9aa82023
ZJS
1057 x = strappend(field, value);
1058 if (x)
e6a7ec4b 1059 iovec[(*n_iovec)++] = IOVEC_MAKE_STRING(x);
9aa82023
ZJS
1060 return x;
1061}
3c171f0b 1062
f45b8015 1063static char* set_iovec_field_free(struct iovec *iovec, size_t *n_iovec, const char *field, char *value) {
9aa82023
ZJS
1064 char *x;
1065
1066 x = set_iovec_field(iovec, n_iovec, field, value);
1067 free(value);
1068 return x;
1069}
1070
92e92d71 1071static int gather_pid_metadata(
ea5cc2a8 1072 char* context[_CONTEXT_MAX],
9aa82023 1073 char **comm_fallback,
5b45a160 1074 struct iovec *iovec, size_t *n_iovec) {
d14bcb4e 1075
f45b8015 1076 /* We need 27 empty slots in iovec!
d14bcb4e
LP
1077 *
1078 * Note that if we fail on oom later on, we do not roll-back changes to the iovec structure. (It remains valid,
1079 * with the first n_iovec fields initialized.) */
3c171f0b 1080
3c171f0b 1081 uid_t owner_uid;
3c171f0b
LP
1082 pid_t pid;
1083 char *t;
9aa82023 1084 const char *p;
d14bcb4e 1085 int r, signo;
3c171f0b 1086
9aa82023 1087 r = parse_pid(context[CONTEXT_PID], &pid);
3c171f0b 1088 if (r < 0)
988e89ee 1089 return log_error_errno(r, "Failed to parse PID \"%s\": %m", context[CONTEXT_PID]);
3c171f0b 1090
ea5cc2a8 1091 r = get_process_comm(pid, &context[CONTEXT_COMM]);
3c171f0b
LP
1092 if (r < 0) {
1093 log_warning_errno(r, "Failed to get COMM, falling back to the command line: %m");
ea5cc2a8
ZJS
1094 context[CONTEXT_COMM] = strv_join(comm_fallback, " ");
1095 if (!context[CONTEXT_COMM])
3c171f0b
LP
1096 return log_oom();
1097 }
1098
ea5cc2a8 1099 r = get_process_exe(pid, &context[CONTEXT_EXE]);
3c171f0b
LP
1100 if (r < 0)
1101 log_warning_errno(r, "Failed to get EXE, ignoring: %m");
1102
92e92d71 1103 if (cg_pid_get_unit(pid, &context[CONTEXT_UNIT]) >= 0) {
b8cda92d 1104 if (!is_journald_crash((const char**) context)) {
92e92d71
ZJS
1105 /* OK, now we know it's not the journal, hence we can make use of it now. */
1106 log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
1107 log_open();
1108 }
3c171f0b 1109
c8091d92 1110 /* If this is PID 1 disable coredump collection, we'll unlikely be able to process it later on. */
b8cda92d 1111 if (is_pid1_crash((const char**) context)) {
c8091d92 1112 log_notice("Due to PID 1 having crashed coredump collection will now be turned off.");
e557b1a6 1113 disable_coredumps();
c8091d92
LP
1114 }
1115
92e92d71 1116 set_iovec_field(iovec, n_iovec, "COREDUMP_UNIT=", context[CONTEXT_UNIT]);
8c8549db 1117 }
803a3464 1118
9aa82023
ZJS
1119 if (cg_pid_get_user_unit(pid, &t) >= 0)
1120 set_iovec_field_free(iovec, n_iovec, "COREDUMP_USER_UNIT=", t);
3c171f0b 1121
9aa82023
ZJS
1122 /* The next few are mandatory */
1123 if (!set_iovec_field(iovec, n_iovec, "COREDUMP_PID=", context[CONTEXT_PID]))
1124 return log_oom();
3c171f0b 1125
9aa82023
ZJS
1126 if (!set_iovec_field(iovec, n_iovec, "COREDUMP_UID=", context[CONTEXT_UID]))
1127 return log_oom();
f5e04665 1128
9aa82023
ZJS
1129 if (!set_iovec_field(iovec, n_iovec, "COREDUMP_GID=", context[CONTEXT_GID]))
1130 return log_oom();
f5e04665 1131
9aa82023
ZJS
1132 if (!set_iovec_field(iovec, n_iovec, "COREDUMP_SIGNAL=", context[CONTEXT_SIGNAL]))
1133 return log_oom();
f5e04665 1134
9aa82023
ZJS
1135 if (!set_iovec_field(iovec, n_iovec, "COREDUMP_RLIMIT=", context[CONTEXT_RLIMIT]))
1136 return log_oom();
f5e04665 1137
f45b8015
JF
1138 if (!set_iovec_field(iovec, n_iovec, "COREDUMP_HOSTNAME=", context[CONTEXT_HOSTNAME]))
1139 return log_oom();
1140
ea5cc2a8 1141 if (!set_iovec_field(iovec, n_iovec, "COREDUMP_COMM=", context[CONTEXT_COMM]))
9aa82023 1142 return log_oom();
bdfd7b2c 1143
ea5cc2a8
ZJS
1144 if (context[CONTEXT_EXE] &&
1145 !set_iovec_field(iovec, n_iovec, "COREDUMP_EXE=", context[CONTEXT_EXE]))
9aa82023 1146 return log_oom();
f5e04665 1147
9aa82023
ZJS
1148 if (sd_pid_get_session(pid, &t) >= 0)
1149 set_iovec_field_free(iovec, n_iovec, "COREDUMP_SESSION=", t);
f5e04665 1150
a035f819 1151 if (sd_pid_get_owner_uid(pid, &owner_uid) >= 0) {
9aa82023 1152 r = asprintf(&t, "COREDUMP_OWNER_UID=" UID_FMT, owner_uid);
7de80bfe 1153 if (r > 0)
e6a7ec4b 1154 iovec[(*n_iovec)++] = IOVEC_MAKE_STRING(t);
f5e04665
LP
1155 }
1156
9aa82023
ZJS
1157 if (sd_pid_get_slice(pid, &t) >= 0)
1158 set_iovec_field_free(iovec, n_iovec, "COREDUMP_SLICE=", t);
f5e04665 1159
9aa82023
ZJS
1160 if (get_process_cmdline(pid, 0, false, &t) >= 0)
1161 set_iovec_field_free(iovec, n_iovec, "COREDUMP_CMDLINE=", t);
a035f819 1162
9aa82023
ZJS
1163 if (cg_pid_get_path_shifted(pid, NULL, &t) >= 0)
1164 set_iovec_field_free(iovec, n_iovec, "COREDUMP_CGROUP=", t);
a035f819 1165
9aa82023
ZJS
1166 if (compose_open_fds(pid, &t) >= 0)
1167 set_iovec_field_free(iovec, n_iovec, "COREDUMP_OPEN_FDS=", t);
3f132692
JF
1168
1169 p = procfs_file_alloca(pid, "status");
9aa82023
ZJS
1170 if (read_full_file(p, &t, NULL) >= 0)
1171 set_iovec_field_free(iovec, n_iovec, "COREDUMP_PROC_STATUS=", t);
3f132692
JF
1172
1173 p = procfs_file_alloca(pid, "maps");
9aa82023
ZJS
1174 if (read_full_file(p, &t, NULL) >= 0)
1175 set_iovec_field_free(iovec, n_iovec, "COREDUMP_PROC_MAPS=", t);
3f132692
JF
1176
1177 p = procfs_file_alloca(pid, "limits");
9aa82023
ZJS
1178 if (read_full_file(p, &t, NULL) >= 0)
1179 set_iovec_field_free(iovec, n_iovec, "COREDUMP_PROC_LIMITS=", t);
3f132692
JF
1180
1181 p = procfs_file_alloca(pid, "cgroup");
9aa82023
ZJS
1182 if (read_full_file(p, &t, NULL) >=0)
1183 set_iovec_field_free(iovec, n_iovec, "COREDUMP_PROC_CGROUP=", t);
3f132692 1184
d7032b1f 1185 p = procfs_file_alloca(pid, "mountinfo");
9aa82023
ZJS
1186 if (read_full_file(p, &t, NULL) >=0)
1187 set_iovec_field_free(iovec, n_iovec, "COREDUMP_PROC_MOUNTINFO=", t);
d7032b1f 1188
9aa82023
ZJS
1189 if (get_process_cwd(pid, &t) >= 0)
1190 set_iovec_field_free(iovec, n_iovec, "COREDUMP_CWD=", t);
3f132692
JF
1191
1192 if (get_process_root(pid, &t) >= 0) {
9aa82023
ZJS
1193 bool proc_self_root_is_slash;
1194
1195 proc_self_root_is_slash = strcmp(t, "/") == 0;
3f132692 1196
9aa82023 1197 set_iovec_field_free(iovec, n_iovec, "COREDUMP_ROOT=", t);
7ed03ce6
JF
1198
1199 /* If the process' root is "/", then there is a chance it has
1200 * mounted own root and hence being containerized. */
9aa82023
ZJS
1201 if (proc_self_root_is_slash && get_process_container_parent_cmdline(pid, &t) > 0)
1202 set_iovec_field_free(iovec, n_iovec, "COREDUMP_CONTAINER_CMDLINE=", t);
3f132692
JF
1203 }
1204
9aa82023
ZJS
1205 if (get_process_environ(pid, &t) >= 0)
1206 set_iovec_field_free(iovec, n_iovec, "COREDUMP_ENVIRON=", t);
1207
4600a396 1208 t = strjoin("COREDUMP_TIMESTAMP=", context[CONTEXT_TIMESTAMP], "000000");
9aa82023 1209 if (t)
e6a7ec4b 1210 iovec[(*n_iovec)++] = IOVEC_MAKE_STRING(t);
9aa82023 1211
d14bcb4e
LP
1212 if (safe_atoi(context[CONTEXT_SIGNAL], &signo) >= 0 && SIGNAL_VALID(signo))
1213 set_iovec_field(iovec, n_iovec, "COREDUMP_SIGNAL_NAME=SIG", signal_to_string(signo));
1214
92e92d71 1215 return 0; /* we successfully acquired all metadata */
9aa82023 1216}
3f132692 1217
9aa82023
ZJS
1218static int process_kernel(int argc, char* argv[]) {
1219
ea5cc2a8 1220 char* context[_CONTEXT_MAX] = {};
f45b8015 1221 struct iovec iovec[29 + SUBMIT_COREDUMP_FIELDS];
988e89ee 1222 size_t i, n_iovec, n_to_free = 0;
9aa82023
ZJS
1223 int r;
1224
988e89ee
ZJS
1225 log_debug("Processing coredump received from the kernel...");
1226
9aa82023 1227 if (argc < CONTEXT_COMM + 1) {
988e89ee 1228 log_error("Not enough arguments passed by the kernel (%i, expected %i).", argc - 1, CONTEXT_COMM + 1 - 1);
9aa82023 1229 return -EINVAL;
3f132692
JF
1230 }
1231
ea5cc2a8
ZJS
1232 context[CONTEXT_PID] = argv[1 + CONTEXT_PID];
1233 context[CONTEXT_UID] = argv[1 + CONTEXT_UID];
1234 context[CONTEXT_GID] = argv[1 + CONTEXT_GID];
1235 context[CONTEXT_SIGNAL] = argv[1 + CONTEXT_SIGNAL];
1236 context[CONTEXT_TIMESTAMP] = argv[1 + CONTEXT_TIMESTAMP];
1237 context[CONTEXT_RLIMIT] = argv[1 + CONTEXT_RLIMIT];
f45b8015 1238 context[CONTEXT_HOSTNAME] = argv[1 + CONTEXT_HOSTNAME];
9aa82023 1239
92e92d71
ZJS
1240 r = gather_pid_metadata(context, argv + 1 + CONTEXT_COMM, iovec, &n_to_free);
1241 if (r < 0)
86562420 1242 goto finish;
86562420 1243
988e89ee 1244 n_iovec = n_to_free;
f5e04665 1245
e6a7ec4b 1246 iovec[n_iovec++] = IOVEC_MAKE_STRING("MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR);
4850d39a
LP
1247
1248 assert_cc(2 == LOG_CRIT);
e6a7ec4b 1249 iovec[n_iovec++] = IOVEC_MAKE_STRING("PRIORITY=2");
0dc5d23c 1250
3c171f0b 1251 assert(n_iovec <= ELEMENTSOF(iovec));
34c10968 1252
b8cda92d 1253 if (is_journald_crash((const char**) context) || is_pid1_crash((const char**) context))
92e92d71
ZJS
1254 r = submit_coredump((const char**) context,
1255 iovec, ELEMENTSOF(iovec), n_iovec,
1256 STDIN_FILENO);
1257 else
1258 r = send_iovec(iovec, n_iovec, STDIN_FILENO);
9aa82023
ZJS
1259
1260 finish:
1261 for (i = 0; i < n_to_free; i++)
1262 free(iovec[i].iov_base);
1263
92e92d71 1264 /* Those fields are allocated by gather_pid_metadata */
ea5cc2a8
ZJS
1265 free(context[CONTEXT_COMM]);
1266 free(context[CONTEXT_EXE]);
92e92d71 1267 free(context[CONTEXT_UNIT]);
ea5cc2a8 1268
9aa82023 1269 return r;
3c171f0b 1270}
34c10968 1271
988e89ee 1272static int process_backtrace(int argc, char *argv[]) {
ea5cc2a8
ZJS
1273 char *context[_CONTEXT_MAX] = {};
1274 _cleanup_free_ char *message = NULL;
5b45a160
ZJS
1275 _cleanup_free_ struct iovec *iovec = NULL;
1276 size_t n_iovec, n_allocated, n_to_free = 0, i;
988e89ee 1277 int r;
5b45a160
ZJS
1278 JournalImporter importer = {
1279 .fd = STDIN_FILENO,
1280 };
988e89ee
ZJS
1281
1282 log_debug("Processing backtrace on stdin...");
1283
1284 if (argc < CONTEXT_COMM + 1) {
1285 log_error("Not enough arguments passed (%i, expected %i).", argc - 1, CONTEXT_COMM + 1 - 1);
1286 return -EINVAL;
1287 }
1288
ea5cc2a8
ZJS
1289 context[CONTEXT_PID] = argv[2 + CONTEXT_PID];
1290 context[CONTEXT_UID] = argv[2 + CONTEXT_UID];
1291 context[CONTEXT_GID] = argv[2 + CONTEXT_GID];
1292 context[CONTEXT_SIGNAL] = argv[2 + CONTEXT_SIGNAL];
1293 context[CONTEXT_TIMESTAMP] = argv[2 + CONTEXT_TIMESTAMP];
1294 context[CONTEXT_RLIMIT] = argv[2 + CONTEXT_RLIMIT];
f45b8015 1295 context[CONTEXT_HOSTNAME] = argv[2 + CONTEXT_HOSTNAME];
988e89ee 1296
f45b8015
JF
1297 n_allocated = 34 + COREDUMP_STORAGE_EXTERNAL;
1298 /* 26 metadata, 2 static, +unknown input, 4 storage, rounded up */
5b45a160
ZJS
1299 iovec = new(struct iovec, n_allocated);
1300 if (!iovec)
1301 return log_oom();
1302
92e92d71 1303 r = gather_pid_metadata(context, argv + 2 + CONTEXT_COMM, iovec, &n_to_free);
988e89ee
ZJS
1304 if (r < 0)
1305 goto finish;
86562420
LP
1306 if (r > 0) {
1307 /* This was a special crash, and has already been processed. */
1308 r = 0;
1309 goto finish;
1310 }
5b45a160 1311 n_iovec = n_to_free;
988e89ee 1312
86562420 1313 for (;;) {
5b45a160
ZJS
1314 r = journal_importer_process_data(&importer);
1315 if (r < 0) {
1316 log_error_errno(r, "Failed to parse journal entry on stdin: %m");
1317 goto finish;
1318 }
d74dc4f2
ZJS
1319 if (r == 1 || /* complete entry */
1320 journal_importer_eof(&importer)) /* end of data */
5b45a160 1321 break;
988e89ee 1322 }
988e89ee 1323
5b45a160
ZJS
1324 if (!GREEDY_REALLOC(iovec, n_allocated, n_iovec + importer.iovw.count + 2))
1325 return log_oom();
1326
1327 if (journal_importer_eof(&importer)) {
1328 log_warning("Did not receive a full journal entry on stdin, ignoring message sent by reporter");
988e89ee 1329
ea5cc2a8
ZJS
1330 message = strjoin("MESSAGE=Process ", context[CONTEXT_PID],
1331 " (", context[CONTEXT_COMM], ")"
5b45a160
ZJS
1332 " of user ", context[CONTEXT_UID],
1333 " failed with ", context[CONTEXT_SIGNAL]);
1334 if (!message) {
1335 r = log_oom();
1336 goto finish;
1337 }
e6a7ec4b 1338 iovec[n_iovec++] = IOVEC_MAKE_STRING(message);
5b45a160
ZJS
1339 } else {
1340 for (i = 0; i < importer.iovw.count; i++)
1341 iovec[n_iovec++] = importer.iovw.iovec[i];
1342 }
988e89ee 1343
e6a7ec4b 1344 iovec[n_iovec++] = IOVEC_MAKE_STRING("MESSAGE_ID=" SD_MESSAGE_BACKTRACE_STR);
988e89ee 1345 assert_cc(2 == LOG_CRIT);
e6a7ec4b 1346 iovec[n_iovec++] = IOVEC_MAKE_STRING("PRIORITY=2");
988e89ee 1347
5b45a160 1348 assert(n_iovec <= n_allocated);
988e89ee
ZJS
1349
1350 r = sd_journal_sendv(iovec, n_iovec);
1351 if (r < 0)
1352 log_error_errno(r, "Failed to log backtrace: %m");
1353
1354 finish:
1355 for (i = 0; i < n_to_free; i++)
1356 free(iovec[i].iov_base);
1357
92e92d71 1358 /* Those fields are allocated by gather_pid_metadata */
ea5cc2a8
ZJS
1359 free(context[CONTEXT_COMM]);
1360 free(context[CONTEXT_EXE]);
92e92d71 1361 free(context[CONTEXT_UNIT]);
ea5cc2a8 1362
988e89ee
ZJS
1363 return r;
1364}
1365
3c171f0b
LP
1366int main(int argc, char *argv[]) {
1367 int r;
fee80f69 1368
9aa82023
ZJS
1369 /* First, log to a safe place, since we don't know what crashed and it might
1370 * be journald which we'd rather not log to then. */
8d4e028f 1371
3c171f0b
LP
1372 log_set_target(LOG_TARGET_KMSG);
1373 log_open();
8d4e028f 1374
3c171f0b
LP
1375 /* Make sure we never enter a loop */
1376 (void) prctl(PR_SET_DUMPABLE, 0);
8d4e028f 1377
3c171f0b
LP
1378 /* Ignore all parse errors */
1379 (void) parse_config();
fee80f69 1380
3c171f0b
LP
1381 log_debug("Selected storage '%s'.", coredump_storage_to_string(arg_storage));
1382 log_debug("Selected compression %s.", yes_no(arg_compress));
fee80f69 1383
3c171f0b
LP
1384 r = sd_listen_fds(false);
1385 if (r < 0) {
1386 log_error_errno(r, "Failed to determine number of file descriptor: %m");
1387 goto finish;
fee80f69
LP
1388 }
1389
9aa82023
ZJS
1390 /* If we got an fd passed, we are running in coredumpd mode. Otherwise we
1391 * are invoked from the kernel as coredump handler. */
988e89ee
ZJS
1392 if (r == 0) {
1393 if (streq_ptr(argv[1], "--backtrace"))
1394 r = process_backtrace(argc, argv);
1395 else
1396 r = process_kernel(argc, argv);
1397 } else if (r == 1)
3c171f0b
LP
1398 r = process_socket(SD_LISTEN_FDS_START);
1399 else {
1400 log_error("Received unexpected number of file descriptors.");
1401 r = -EINVAL;
1402 }
f5e04665
LP
1403
1404finish:
f5e04665
LP
1405 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1406}