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