]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/coredump/coredump.c
systemctl: add two minor assert()s
[thirdparty/systemd.git] / src / coredump / coredump.c
CommitLineData
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. */
76assert_cc(JOURNAL_SIZE_MAX <= DATA_SIZE_MAX);
f5e04665
LP
77
78enum {
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
92typedef 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
101static 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
108DEFINE_PRIVATE_STRING_TABLE_LOOKUP(coredump_storage, CoredumpStorage);
8c9571d0 109static DEFINE_CONFIG_PARSE_ENUM(config_parse_coredump_storage, coredump_storage, CoredumpStorage, "Failed to parse storage setting");
34727273
ZJS
110
111static CoredumpStorage arg_storage = COREDUMP_STORAGE_EXTERNAL;
8c9571d0 112static bool arg_compress = true;
59f448cf
LP
113static uint64_t arg_process_size_max = PROCESS_SIZE_MAX;
114static uint64_t arg_external_size_max = EXTERNAL_SIZE_MAX;
34727273 115static size_t arg_journal_size_max = JOURNAL_SIZE_MAX;
59f448cf
LP
116static uint64_t arg_keep_free = (uint64_t) -1;
117static uint64_t arg_max_use = (uint64_t) -1;
34c10968
LP
118
119static 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
138static 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 180static 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
216static inline const char *coredump_tmpfile_name(const char *s) {
217 return s ? s : "(unnamed temporary file)";
218}
219
b59233e6
LP
220static 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
227 assert(fd >= 0);
b59233e6 228 assert(target);
3c171f0b 229 assert(context);
cfd652ed
ZJS
230
231 /* Ignore errors on these */
3c171f0b
LP
232 (void) fchmod(fd, 0640);
233 (void) fix_acl(fd, uid);
234 (void) fix_xattr(fd, context);
cfd652ed 235
4a62c710 236 if (fsync(fd) < 0)
0c773903 237 return log_error_errno(errno, "Failed to sync coredump %s: %m", coredump_tmpfile_name(filename));
cfd652ed 238
0c773903
EV
239 if (filename) {
240 if (rename(filename, target) < 0)
241 return log_error_errno(errno, "Failed to rename coredump %s -> %s: %m", filename, target);
242 } else {
243 _cleanup_free_ char *proc_fd_path = NULL;
244
245 if (asprintf(&proc_fd_path, "/proc/self/fd/%d", fd) < 0)
246 return log_oom();
247
248 if (linkat(AT_FDCWD, proc_fd_path, AT_FDCWD, target, AT_SYMLINK_FOLLOW) < 0)
249 return log_error_errno(errno, "Failed to create coredump %s: %m", target);
250 }
cfd652ed
ZJS
251
252 return 0;
253}
254
59f448cf 255static int maybe_remove_external_coredump(const char *filename, uint64_t size) {
cfd652ed 256
b59233e6 257 /* Returns 1 if might remove, 0 if will not remove, < 0 on error. */
cfd652ed
ZJS
258
259 if (IN_SET(arg_storage, COREDUMP_STORAGE_EXTERNAL, COREDUMP_STORAGE_BOTH) &&
260 size <= arg_external_size_max)
261 return 0;
262
263 if (!filename)
264 return 1;
265
4a62c710
MS
266 if (unlink(filename) < 0 && errno != ENOENT)
267 return log_error_errno(errno, "Failed to unlink %s: %m", filename);
cfd652ed
ZJS
268
269 return 1;
270}
271
3c171f0b 272static int make_filename(const char *context[_CONTEXT_MAX], char **ret) {
b59233e6 273 _cleanup_free_ char *c = NULL, *u = NULL, *p = NULL, *t = NULL;
a7f7d1bd 274 sd_id128_t boot = {};
34c10968
LP
275 int r;
276
3c171f0b 277 assert(context);
34c10968 278
3c171f0b 279 c = filename_escape(context[CONTEXT_COMM]);
34c10968 280 if (!c)
b59233e6 281 return -ENOMEM;
34c10968 282
3c171f0b 283 u = filename_escape(context[CONTEXT_UID]);
0dc5d23c 284 if (!u)
b59233e6 285 return -ENOMEM;
34c10968
LP
286
287 r = sd_id128_get_boot(&boot);
b59233e6 288 if (r < 0)
34c10968 289 return r;
34c10968 290
3c171f0b 291 p = filename_escape(context[CONTEXT_PID]);
b59233e6
LP
292 if (!p)
293 return -ENOMEM;
294
3c171f0b 295 t = filename_escape(context[CONTEXT_TIMESTAMP]);
b59233e6
LP
296 if (!t)
297 return -ENOMEM;
298
299 if (asprintf(ret,
0dc5d23c 300 "/var/lib/systemd/coredump/core.%s.%s." SD_ID128_FORMAT_STR ".%s.%s000000",
34c10968 301 c,
0dc5d23c 302 u,
34c10968
LP
303 SD_ID128_FORMAT_VAL(boot),
304 p,
b59233e6
LP
305 t) < 0)
306 return -ENOMEM;
307
308 return 0;
309}
310
0c773903
EV
311static int open_coredump_tmpfile(const char *target, char **ret_filename) {
312 _cleanup_free_ char *tmp = NULL;
313 int fd;
314 int r;
315
316 assert(target);
317 assert(ret_filename);
318
319 fd = open("/var/lib/systemd/coredump", O_TMPFILE|O_CLOEXEC|O_NOCTTY|O_RDWR, 0640);
320 if (fd < 0) {
321 log_debug_errno(errno, "Failed to use O_TMPFILE: %m");
322
323 r = tempfn_random(target, NULL, &tmp);
324 if (r < 0)
325 return log_error_errno(r, "Failed to determine temporary file name: %m");
326
327 fd = open(tmp, O_CREAT|O_EXCL|O_RDWR|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0640);
328 if (fd < 0)
329 return log_error_errno(errno, "Failed to create coredump file %s: %m", tmp);
330 }
331
332 *ret_filename = tmp;
333 tmp = NULL;
334
335 return fd;
336}
337
b59233e6 338static int save_external_coredump(
3c171f0b
LP
339 const char *context[_CONTEXT_MAX],
340 int input_fd,
b59233e6 341 char **ret_filename,
5f3e0a74
HW
342 int *ret_node_fd,
343 int *ret_data_fd,
59f448cf 344 uint64_t *ret_size) {
b59233e6
LP
345
346 _cleanup_free_ char *fn = NULL, *tmp = NULL;
347 _cleanup_close_ int fd = -1;
bdfd7b2c 348 uint64_t rlimit, max_size;
b59233e6 349 struct stat st;
3c171f0b 350 uid_t uid;
b59233e6
LP
351 int r;
352
3c171f0b 353 assert(context);
b59233e6 354 assert(ret_filename);
5f3e0a74
HW
355 assert(ret_node_fd);
356 assert(ret_data_fd);
b59233e6
LP
357 assert(ret_size);
358
3c171f0b
LP
359 r = parse_uid(context[CONTEXT_UID], &uid);
360 if (r < 0)
361 return log_error_errno(r, "Failed to parse UID: %m");
362
bdfd7b2c
LP
363 r = safe_atou64(context[CONTEXT_RLIMIT], &rlimit);
364 if (r < 0)
365 return log_error_errno(r, "Failed to parse resource limit: %s", context[CONTEXT_RLIMIT]);
366 if (rlimit <= 0) {
367 /* Is coredumping disabled? Then don't bother saving/processing the coredump */
368 log_info("Core Dumping has been disabled for process %s (%s).", context[CONTEXT_PID], context[CONTEXT_COMM]);
369 return -EBADSLT;
370 }
371
372 /* Never store more than the process configured, or than we actually shall keep or process */
373 max_size = MIN(rlimit, MAX(arg_process_size_max, arg_external_size_max));
374
3c171f0b 375 r = make_filename(context, &fn);
23bbb0de
MS
376 if (r < 0)
377 return log_error_errno(r, "Failed to determine coredump file name: %m");
34c10968 378
d2e54fae 379 mkdir_p_label("/var/lib/systemd/coredump", 0755);
803a3464 380
0c773903 381 fd = open_coredump_tmpfile(fn, &tmp);
4a62c710 382 if (fd < 0)
0c773903 383 return fd;
803a3464 384
bdfd7b2c 385 r = copy_bytes(input_fd, fd, max_size, false);
84ee0960 386 if (r == -EFBIG) {
3c171f0b 387 log_error("Coredump of %s (%s) is larger than configured processing limit, refusing.", context[CONTEXT_PID], context[CONTEXT_COMM]);
93240d3a
LP
388 goto fail;
389 } else if (IN_SET(r, -EDQUOT, -ENOSPC)) {
3c171f0b 390 log_error("Not enough disk space for coredump of %s (%s), refusing.", context[CONTEXT_PID], context[CONTEXT_COMM]);
93240d3a
LP
391 goto fail;
392 } else if (r < 0) {
da927ba9 393 log_error_errno(r, "Failed to dump coredump to file: %m");
34c10968
LP
394 goto fail;
395 }
803a3464 396
34c10968 397 if (fstat(fd, &st) < 0) {
0c773903 398 log_error_errno(errno, "Failed to fstat coredump %s: %m", coredump_tmpfile_name(tmp));
34c10968
LP
399 goto fail;
400 }
401
7849c2ac 402 if (lseek(fd, 0, SEEK_SET) == (off_t) -1) {
0c773903 403 log_error_errno(errno, "Failed to seek on %s: %m", coredump_tmpfile_name(tmp));
b59233e6 404 goto fail;
7849c2ac
TA
405 }
406
d89c8fdf 407#if defined(HAVE_XZ) || defined(HAVE_LZ4)
cfd652ed 408 /* If we will remove the coredump anyway, do not compress. */
34727273 409 if (maybe_remove_external_coredump(NULL, st.st_size) == 0
8c9571d0 410 && arg_compress) {
cfd652ed 411
b59233e6
LP
412 _cleanup_free_ char *fn_compressed = NULL, *tmp_compressed = NULL;
413 _cleanup_close_ int fd_compressed = -1;
cfd652ed 414
d89c8fdf 415 fn_compressed = strappend(fn, COMPRESSED_EXT);
b59233e6 416 if (!fn_compressed) {
d89c8fdf 417 log_oom();
cfd652ed
ZJS
418 goto uncompressed;
419 }
420
0c773903
EV
421 fd_compressed = open_coredump_tmpfile(fn_compressed, &tmp_compressed);
422 if (fd_compressed < 0)
b59233e6 423 goto uncompressed;
cfd652ed 424
d89c8fdf 425 r = compress_stream(fd, fd_compressed, -1);
b59233e6 426 if (r < 0) {
0c773903 427 log_error_errno(r, "Failed to compress %s: %m", coredump_tmpfile_name(tmp_compressed));
b59233e6
LP
428 goto fail_compressed;
429 }
430
3c171f0b 431 r = fix_permissions(fd_compressed, tmp_compressed, fn_compressed, context, uid);
cfd652ed 432 if (r < 0)
b59233e6
LP
433 goto fail_compressed;
434
435 /* OK, this worked, we can get rid of the uncompressed version now */
0c773903
EV
436 if (tmp)
437 unlink_noerrno(tmp);
cfd652ed 438
59f448cf 439 *ret_filename = fn_compressed; /* compressed */
5f3e0a74
HW
440 *ret_node_fd = fd_compressed; /* compressed */
441 *ret_data_fd = fd; /* uncompressed */
59f448cf 442 *ret_size = (uint64_t) st.st_size; /* uncompressed */
cfd652ed 443
b59233e6 444 fn_compressed = NULL;
5f3e0a74 445 fd = fd_compressed = -1;
cfd652ed
ZJS
446
447 return 0;
448
b59233e6 449 fail_compressed:
0c773903
EV
450 if (tmp_compressed)
451 (void) unlink(tmp_compressed);
34c10968 452 }
cfd652ed
ZJS
453
454uncompressed:
3b1a55e1 455#endif
5f3e0a74 456
3c171f0b 457 r = fix_permissions(fd, tmp, fn, context, uid);
cfd652ed
ZJS
458 if (r < 0)
459 goto fail;
34c10968
LP
460
461 *ret_filename = fn;
5f3e0a74
HW
462 *ret_data_fd = fd;
463 *ret_node_fd = -1;
59f448cf 464 *ret_size = (uint64_t) st.st_size;
34c10968
LP
465
466 fn = NULL;
467 fd = -1;
468
469 return 0;
470
471fail:
0c773903
EV
472 if (tmp)
473 (void) unlink(tmp);
34c10968
LP
474 return r;
475}
476
477static int allocate_journal_field(int fd, size_t size, char **ret, size_t *ret_size) {
478 _cleanup_free_ char *field = NULL;
479 ssize_t n;
480
8d4e028f 481 assert(fd >= 0);
34c10968
LP
482 assert(ret);
483 assert(ret_size);
484
4a62c710
MS
485 if (lseek(fd, 0, SEEK_SET) == (off_t) -1)
486 return log_warning_errno(errno, "Failed to seek: %m");
803a3464 487
34c10968
LP
488 field = malloc(9 + size);
489 if (!field) {
cfd652ed 490 log_warning("Failed to allocate memory for coredump, coredump will not be stored.");
34c10968
LP
491 return -ENOMEM;
492 }
493
494 memcpy(field, "COREDUMP=", 9);
495
496 n = read(fd, field + 9, size);
23bbb0de
MS
497 if (n < 0)
498 return log_error_errno((int) n, "Failed to read core data: %m");
34c10968
LP
499 if ((size_t) n < size) {
500 log_error("Core data too short.");
501 return -EIO;
502 }
503
504 *ret = field;
505 *ret_size = size + 9;
506
507 field = NULL;
508
509 return 0;
510}
803a3464 511
3f132692
JF
512/* Joins /proc/[pid]/fd/ and /proc/[pid]/fdinfo/ into the following lines:
513 * 0:/dev/pts/23
514 * pos: 0
515 * flags: 0100002
516 *
517 * 1:/dev/pts/23
518 * pos: 0
519 * flags: 0100002
520 *
521 * 2:/dev/pts/23
522 * pos: 0
523 * flags: 0100002
524 * EOF
525 */
526static int compose_open_fds(pid_t pid, char **open_fds) {
4d84bc2f
LP
527 _cleanup_closedir_ DIR *proc_fd_dir = NULL;
528 _cleanup_close_ int proc_fdinfo_fd = -1;
529 _cleanup_free_ char *buffer = NULL;
3f132692 530 _cleanup_fclose_ FILE *stream = NULL;
59059b4a 531 const char *fddelim = "", *path;
3f132692 532 struct dirent *dent = NULL;
4d84bc2f 533 size_t size = 0;
3f132692
JF
534 int r = 0;
535
536 assert(pid >= 0);
537 assert(open_fds != NULL);
538
59059b4a 539 path = procfs_file_alloca(pid, "fd");
3f132692 540 proc_fd_dir = opendir(path);
59059b4a
ZJS
541 if (!proc_fd_dir)
542 return -errno;
3f132692 543
4d84bc2f 544 proc_fdinfo_fd = openat(dirfd(proc_fd_dir), "../fdinfo", O_DIRECTORY|O_NOFOLLOW|O_CLOEXEC|O_PATH);
59059b4a
ZJS
545 if (proc_fdinfo_fd < 0)
546 return -errno;
3f132692 547
4d84bc2f 548 stream = open_memstream(&buffer, &size);
3f132692
JF
549 if (!stream)
550 return -ENOMEM;
551
4d84bc2f 552 FOREACH_DIRENT(dent, proc_fd_dir, return -errno) {
3f132692 553 _cleanup_fclose_ FILE *fdinfo = NULL;
4d84bc2f 554 _cleanup_free_ char *fdname = NULL;
59059b4a 555 char line[LINE_MAX];
4d84bc2f 556 int fd;
3f132692 557
59059b4a 558 r = readlinkat_malloc(dirfd(proc_fd_dir), dent->d_name, &fdname);
3f132692
JF
559 if (r < 0)
560 return r;
561
562 fprintf(stream, "%s%s:%s\n", fddelim, dent->d_name, fdname);
563 fddelim = "\n";
564
565 /* Use the directory entry from /proc/[pid]/fd with /proc/[pid]/fdinfo */
59059b4a
ZJS
566 fd = openat(proc_fdinfo_fd, dent->d_name, O_NOFOLLOW|O_CLOEXEC|O_RDONLY);
567 if (fd < 0)
3f132692
JF
568 continue;
569
59059b4a
ZJS
570 fdinfo = fdopen(fd, "re");
571 if (fdinfo == NULL) {
572 close(fd);
3f132692 573 continue;
59059b4a 574 }
3f132692 575
4d84bc2f
LP
576 FOREACH_LINE(line, fdinfo, break) {
577 fputs(line, stream);
578 if (!endswith(line, "\n"))
579 fputc('\n', stream);
580 }
3f132692
JF
581 }
582
4d84bc2f 583 errno = 0;
74ca738f 584 stream = safe_fclose(stream);
4d84bc2f 585
b3267152 586 if (errno > 0)
4d84bc2f
LP
587 return -errno;
588
589 *open_fds = buffer;
590 buffer = NULL;
591
3f132692
JF
592 return 0;
593}
594
3c171f0b
LP
595static int change_uid_gid(const char *context[]) {
596 uid_t uid;
597 gid_t gid;
598 int r;
34c10968 599
3c171f0b
LP
600 r = parse_uid(context[CONTEXT_UID], &uid);
601 if (r < 0)
602 return r;
8c8549db 603
888e378d
LP
604 if (uid <= SYSTEM_UID_MAX) {
605 const char *user = "systemd-coredump";
606
607 r = get_user_creds(&user, &uid, &gid, NULL, NULL);
608 if (r < 0) {
609 log_warning_errno(r, "Cannot resolve %s user. Proceeding to dump core as root: %m", user);
610 uid = gid = 0;
611 }
612 } else {
613 r = parse_gid(context[CONTEXT_GID], &gid);
614 if (r < 0)
615 return r;
616 }
3c171f0b
LP
617
618 return drop_privileges(uid, gid, 0);
619}
8c8549db 620
3c171f0b
LP
621static int submit_coredump(
622 const char *context[_CONTEXT_MAX],
623 struct iovec *iovec,
624 size_t n_iovec_allocated,
625 size_t n_iovec,
626 int input_fd) {
34c10968 627
5f3e0a74 628 _cleanup_close_ int coredump_fd = -1, coredump_node_fd = -1;
3c171f0b 629 _cleanup_free_ char *core_message = NULL, *filename = NULL, *coredump_data = NULL;
59f448cf 630 uint64_t coredump_size;
3c171f0b 631 int r;
f5e04665 632
3c171f0b
LP
633 assert(context);
634 assert(iovec);
635 assert(n_iovec_allocated >= n_iovec + 3);
636 assert(input_fd >= 0);
f5e04665 637
3c171f0b
LP
638 /* Vacuum before we write anything again */
639 (void) coredump_vacuum(-1, arg_keep_free, arg_max_use);
803a3464 640
3c171f0b
LP
641 /* Always stream the coredump to disk, if that's possible */
642 r = save_external_coredump(context, input_fd, &filename, &coredump_node_fd, &coredump_fd, &coredump_size);
643 if (r < 0)
644 /* Skip whole core dumping part */
645 goto log;
646
647 /* If we don't want to keep the coredump on disk, remove it now, as later on we will lack the privileges for
648 * it. However, we keep the fd to it, so that we can still process it and log it. */
649 r = maybe_remove_external_coredump(filename, coredump_size);
650 if (r < 0)
651 return r;
652 if (r == 0) {
653 const char *coredump_filename;
654
655 coredump_filename = strjoina("COREDUMP_FILENAME=", filename);
656 IOVEC_SET_STRING(iovec[n_iovec++], coredump_filename);
f5e04665
LP
657 }
658
3c171f0b
LP
659 /* Vacuum again, but exclude the coredump we just created */
660 (void) coredump_vacuum(coredump_node_fd >= 0 ? coredump_node_fd : coredump_fd, arg_keep_free, arg_max_use);
8c9571d0 661
3c171f0b
LP
662 /* Now, let's drop privileges to become the user who owns the segfaulted process and allocate the coredump
663 * memory under the user's uid. This also ensures that the credentials journald will see are the ones of the
664 * coredumping user, thus making sure the user gets access to the core dump. Let's also get rid of all
665 * capabilities, if we run as root, we won't need them anymore. */
666 r = change_uid_gid(context);
667 if (r < 0)
668 return log_error_errno(r, "Failed to drop privileges: %m");
34c10968 669
3c171f0b
LP
670#ifdef HAVE_ELFUTILS
671 /* Try to get a strack trace if we can */
672 if (coredump_size <= arg_process_size_max) {
673 _cleanup_free_ char *stacktrace = NULL;
674
675 r = coredump_make_stack_trace(coredump_fd, context[CONTEXT_EXE], &stacktrace);
676 if (r >= 0)
677 core_message = strjoin("MESSAGE=Process ", context[CONTEXT_PID], " (", context[CONTEXT_COMM], ") of user ", context[CONTEXT_UID], " dumped core.\n\n", stacktrace, NULL);
678 else if (r == -EINVAL)
679 log_warning("Failed to generate stack trace: %s", dwfl_errmsg(dwfl_errno()));
680 else
681 log_warning_errno(r, "Failed to generate stack trace: %m");
34c10968 682 }
803a3464 683
3c171f0b
LP
684 if (!core_message)
685#endif
686log:
687 core_message = strjoin("MESSAGE=Process ", context[CONTEXT_PID], " (", context[CONTEXT_COMM], ") of user ", context[CONTEXT_UID], " dumped core.", NULL);
688 if (core_message)
689 IOVEC_SET_STRING(iovec[n_iovec++], core_message);
690
691 /* Optionally store the entire coredump in the journal */
692 if (IN_SET(arg_storage, COREDUMP_STORAGE_JOURNAL, COREDUMP_STORAGE_BOTH) &&
693 coredump_size <= arg_journal_size_max) {
694 size_t sz = 0;
695
696 /* Store the coredump itself in the journal */
697
698 r = allocate_journal_field(coredump_fd, (size_t) coredump_size, &coredump_data, &sz);
699 if (r >= 0) {
700 iovec[n_iovec].iov_base = coredump_data;
701 iovec[n_iovec].iov_len = sz;
702 n_iovec++;
703 }
f5e04665
LP
704 }
705
3c171f0b
LP
706 assert(n_iovec <= n_iovec_allocated);
707
708 r = sd_journal_sendv(iovec, n_iovec);
709 if (r < 0)
710 return log_error_errno(r, "Failed to log coredump: %m");
711
712 return 0;
713}
714
715static void map_context_fields(const struct iovec *iovec, const char *context[]) {
716
717 static const char * const context_field_names[_CONTEXT_MAX] = {
718 [CONTEXT_PID] = "COREDUMP_PID=",
719 [CONTEXT_UID] = "COREDUMP_UID=",
720 [CONTEXT_GID] = "COREDUMP_GID=",
721 [CONTEXT_SIGNAL] = "COREDUMP_SIGNAL=",
722 [CONTEXT_TIMESTAMP] = "COREDUMP_TIMESTAMP=",
723 [CONTEXT_COMM] = "COREDUMP_COMM=",
724 [CONTEXT_EXE] = "COREDUMP_EXE=",
bdfd7b2c 725 [CONTEXT_RLIMIT] = "COREDUMP_RLIMIT=",
3c171f0b
LP
726 };
727
728 unsigned i;
729
730 assert(iovec);
731 assert(context);
732
733 for (i = 0; i < _CONTEXT_MAX; i++) {
734 size_t l;
735
736 l = strlen(context_field_names[i]);
737 if (iovec->iov_len < l)
738 continue;
739
740 if (memcmp(iovec->iov_base, context_field_names[i], l) != 0)
741 continue;
742
743 /* Note that these strings are NUL terminated, because we made sure that a trailing NUL byte is in the
744 * buffer, though not included in the iov_len count. (see below) */
745 context[i] = (char*) iovec->iov_base + l;
746 break;
747 }
748}
749
750static int process_socket(int fd) {
751 _cleanup_close_ int coredump_fd = -1;
752 struct iovec *iovec = NULL;
753 size_t n_iovec = 0, n_iovec_allocated = 0, i;
754 const char *context[_CONTEXT_MAX] = {};
755 int r;
756
757 assert(fd >= 0);
758
759 log_set_target(LOG_TARGET_AUTO);
760 log_parse_environment();
761 log_open();
762
763 for (;;) {
764 union {
765 struct cmsghdr cmsghdr;
766 uint8_t buf[CMSG_SPACE(sizeof(int))];
767 } control = {};
768 struct msghdr mh = {
769 .msg_control = &control,
770 .msg_controllen = sizeof(control),
771 .msg_iovlen = 1,
772 };
773 ssize_t n;
774 int l;
775
776 if (!GREEDY_REALLOC(iovec, n_iovec_allocated, n_iovec + 3)) {
777 r = log_oom();
778 goto finish;
779 }
780
781 if (ioctl(fd, FIONREAD, &l) < 0) {
782 r = log_error_errno(errno, "FIONREAD failed: %m");
783 goto finish;
784 }
785
786 assert(l >= 0);
787
788 iovec[n_iovec].iov_len = l;
789 iovec[n_iovec].iov_base = malloc(l + 1);
790
791 if (!iovec[n_iovec].iov_base) {
792 r = log_oom();
793 goto finish;
794 }
795
796 mh.msg_iov = iovec + n_iovec;
797
798 n = recvmsg(fd, &mh, MSG_NOSIGNAL|MSG_CMSG_CLOEXEC);
799 if (n < 0) {
800 free(iovec[n_iovec].iov_base);
801 r = log_error_errno(errno, "Failed to receive datagram: %m");
802 goto finish;
803 }
804
805 if (n == 0) {
806 struct cmsghdr *cmsg, *found = NULL;
807 /* The final zero-length datagram carries the file descriptor and tells us that we're done. */
808
809 free(iovec[n_iovec].iov_base);
810
811 CMSG_FOREACH(cmsg, &mh) {
812 if (cmsg->cmsg_level == SOL_SOCKET &&
813 cmsg->cmsg_type == SCM_RIGHTS &&
814 cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
815 assert(!found);
816 found = cmsg;
817 }
818 }
819
820 if (!found) {
821 log_error("Coredump file descriptor missing.");
822 r = -EBADMSG;
823 goto finish;
824 }
825
826 assert(coredump_fd < 0);
827 coredump_fd = *(int*) CMSG_DATA(found);
828 break;
829 }
830
831 /* Add trailing NUL byte, in case these are strings */
832 ((char*) iovec[n_iovec].iov_base)[n] = 0;
833 iovec[n_iovec].iov_len = (size_t) n;
834
835 cmsg_close_all(&mh);
836 map_context_fields(iovec + n_iovec, context);
837 n_iovec++;
838 }
839
840 if (!GREEDY_REALLOC(iovec, n_iovec_allocated, n_iovec + 3)) {
841 r = log_oom();
34c10968
LP
842 goto finish;
843 }
844
3c171f0b
LP
845 /* Make sure we we got all data we really need */
846 assert(context[CONTEXT_PID]);
847 assert(context[CONTEXT_UID]);
848 assert(context[CONTEXT_GID]);
849 assert(context[CONTEXT_SIGNAL]);
850 assert(context[CONTEXT_TIMESTAMP]);
bdfd7b2c 851 assert(context[CONTEXT_RLIMIT]);
3c171f0b
LP
852 assert(context[CONTEXT_COMM]);
853 assert(coredump_fd >= 0);
854
855 r = submit_coredump(context, iovec, n_iovec_allocated, n_iovec, coredump_fd);
856
857finish:
858 for (i = 0; i < n_iovec; i++)
859 free(iovec[i].iov_base);
860 free(iovec);
861
862 return r;
863}
864
865static int send_iovec(const struct iovec iovec[], size_t n_iovec, int input_fd) {
866
867 static const union sockaddr_union sa = {
868 .un.sun_family = AF_UNIX,
869 .un.sun_path = "/run/systemd/coredump",
870 };
871 _cleanup_close_ int fd = -1;
872 size_t i;
873 int r;
874
875 assert(iovec || n_iovec <= 0);
876 assert(input_fd >= 0);
877
878 fd = socket(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0);
879 if (fd < 0)
880 return log_error_errno(errno, "Failed to create coredump socket: %m");
881
882 if (connect(fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path)) < 0)
883 return log_error_errno(errno, "Failed to connect to coredump service: %m");
884
885 for (i = 0; i < n_iovec; i++) {
886 ssize_t n;
887 assert(iovec[i].iov_len > 0);
888
889 n = send(fd, iovec[i].iov_base, iovec[i].iov_len, MSG_NOSIGNAL);
890 if (n < 0)
891 return log_error_errno(errno, "Failed to send coredump datagram: %m");
1eef15b1
ZJS
892 }
893
3c171f0b
LP
894 r = send_one_fd(fd, input_fd, 0);
895 if (r < 0)
896 return log_error_errno(r, "Failed to send coredump fd: %m");
1eef15b1 897
3c171f0b
LP
898 return 0;
899}
1eef15b1 900
3c171f0b
LP
901static int process_journald_crash(const char *context[], int input_fd) {
902 _cleanup_close_ int coredump_fd = -1, coredump_node_fd = -1;
903 _cleanup_free_ char *filename = NULL;
904 uint64_t coredump_size;
905 int r;
803a3464 906
3c171f0b
LP
907 assert(context);
908 assert(input_fd >= 0);
803a3464 909
3c171f0b 910 /* If we are journald, we cut things short, don't write to the journal, but still create a coredump. */
34c10968 911
3c171f0b
LP
912 if (arg_storage != COREDUMP_STORAGE_NONE)
913 arg_storage = COREDUMP_STORAGE_EXTERNAL;
34c10968 914
3c171f0b
LP
915 r = save_external_coredump(context, input_fd, &filename, &coredump_node_fd, &coredump_fd, &coredump_size);
916 if (r < 0)
917 return r;
34c10968 918
3c171f0b
LP
919 r = maybe_remove_external_coredump(filename, coredump_size);
920 if (r < 0)
921 return r;
34c10968 922
3c171f0b
LP
923 log_info("Detected coredump of the journal daemon itself, diverted to %s.", filename);
924 return 0;
925}
926
927static int process_kernel(int argc, char* argv[]) {
928
929 /* The small core field we allocate on the stack, to keep things simple */
930 char
931 *core_pid = NULL, *core_uid = NULL, *core_gid = NULL, *core_signal = NULL,
932 *core_session = NULL, *core_exe = NULL, *core_comm = NULL, *core_cmdline = NULL,
933 *core_cgroup = NULL, *core_cwd = NULL, *core_root = NULL, *core_unit = NULL,
bdfd7b2c 934 *core_user_unit = NULL, *core_slice = NULL, *core_timestamp = NULL, *core_rlimit = NULL;
3c171f0b
LP
935
936 /* The larger ones we allocate on the heap */
937 _cleanup_free_ char
938 *core_owner_uid = NULL, *core_open_fds = NULL, *core_proc_status = NULL,
939 *core_proc_maps = NULL, *core_proc_limits = NULL, *core_proc_cgroup = NULL, *core_environ = NULL;
940
941 _cleanup_free_ char *exe = NULL, *comm = NULL;
942 const char *context[_CONTEXT_MAX];
bdfd7b2c 943 struct iovec iovec[25];
3c171f0b
LP
944 size_t n_iovec = 0;
945 uid_t owner_uid;
946 const char *p;
947 pid_t pid;
948 char *t;
949 int r;
950
951 if (argc < CONTEXT_COMM + 1) {
952 log_error("Not enough arguments passed from kernel (%i, expected %i).", argc - 1, CONTEXT_COMM + 1 - 1);
953 return -EINVAL;
954 }
955
956 r = parse_pid(argv[CONTEXT_PID + 1], &pid);
957 if (r < 0)
958 return log_error_errno(r, "Failed to parse PID.");
959
960 r = get_process_comm(pid, &comm);
961 if (r < 0) {
962 log_warning_errno(r, "Failed to get COMM, falling back to the command line: %m");
963 comm = strv_join(argv + CONTEXT_COMM + 1, " ");
964 if (!comm)
965 return log_oom();
966 }
967
968 r = get_process_exe(pid, &exe);
969 if (r < 0)
970 log_warning_errno(r, "Failed to get EXE, ignoring: %m");
971
972 context[CONTEXT_PID] = argv[CONTEXT_PID + 1];
973 context[CONTEXT_UID] = argv[CONTEXT_UID + 1];
974 context[CONTEXT_GID] = argv[CONTEXT_GID + 1];
975 context[CONTEXT_SIGNAL] = argv[CONTEXT_SIGNAL + 1];
976 context[CONTEXT_TIMESTAMP] = argv[CONTEXT_TIMESTAMP + 1];
bdfd7b2c 977 context[CONTEXT_RLIMIT] = argv[CONTEXT_RLIMIT + 1];
3c171f0b
LP
978 context[CONTEXT_COMM] = comm;
979 context[CONTEXT_EXE] = exe;
980
981 if (cg_pid_get_unit(pid, &t) >= 0) {
982
983 if (streq(t, SPECIAL_JOURNALD_SERVICE)) {
984 free(t);
985 return process_journald_crash(context, STDIN_FILENO);
803a3464
LP
986 }
987
63c372cb 988 core_unit = strjoina("COREDUMP_UNIT=", t);
8c8549db
LP
989 free(t);
990
3c171f0b 991 IOVEC_SET_STRING(iovec[n_iovec++], core_unit);
8c8549db 992 }
803a3464 993
3c171f0b 994 /* OK, now we know it's not the journal, hence we can make use of it now. */
803a3464
LP
995 log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
996 log_open();
997
3c171f0b
LP
998 if (cg_pid_get_user_unit(pid, &t) >= 0) {
999 core_user_unit = strjoina("COREDUMP_USER_UNIT=", t);
1000 free(t);
1001
1002 IOVEC_SET_STRING(iovec[n_iovec++], core_user_unit);
1003 }
1004
1005 core_pid = strjoina("COREDUMP_PID=", context[CONTEXT_PID]);
1006 IOVEC_SET_STRING(iovec[n_iovec++], core_pid);
f5e04665 1007
3c171f0b
LP
1008 core_uid = strjoina("COREDUMP_UID=", context[CONTEXT_UID]);
1009 IOVEC_SET_STRING(iovec[n_iovec++], core_uid);
f5e04665 1010
3c171f0b
LP
1011 core_gid = strjoina("COREDUMP_GID=", context[CONTEXT_GID]);
1012 IOVEC_SET_STRING(iovec[n_iovec++], core_gid);
f5e04665 1013
3c171f0b
LP
1014 core_signal = strjoina("COREDUMP_SIGNAL=", context[CONTEXT_SIGNAL]);
1015 IOVEC_SET_STRING(iovec[n_iovec++], core_signal);
f5e04665 1016
bdfd7b2c
LP
1017 core_rlimit = strjoina("COREDUMP_RLIMIT=", context[CONTEXT_RLIMIT]);
1018 IOVEC_SET_STRING(iovec[n_iovec++], core_rlimit);
1019
f5e04665 1020 if (sd_pid_get_session(pid, &t) >= 0) {
63c372cb 1021 core_session = strjoina("COREDUMP_SESSION=", t);
f5e04665
LP
1022 free(t);
1023
3c171f0b 1024 IOVEC_SET_STRING(iovec[n_iovec++], core_session);
f5e04665
LP
1025 }
1026
a035f819 1027 if (sd_pid_get_owner_uid(pid, &owner_uid) >= 0) {
3c171f0b 1028 r = asprintf(&core_owner_uid, "COREDUMP_OWNER_UID=" UID_FMT, owner_uid);
7de80bfe 1029 if (r > 0)
3c171f0b 1030 IOVEC_SET_STRING(iovec[n_iovec++], core_owner_uid);
a035f819
LP
1031 }
1032
1033 if (sd_pid_get_slice(pid, &t) >= 0) {
63c372cb 1034 core_slice = strjoina("COREDUMP_SLICE=", t);
a035f819
LP
1035 free(t);
1036
3c171f0b 1037 IOVEC_SET_STRING(iovec[n_iovec++], core_slice);
a035f819
LP
1038 }
1039
1eef15b1 1040 if (comm) {
63c372cb 1041 core_comm = strjoina("COREDUMP_COMM=", comm);
3c171f0b 1042 IOVEC_SET_STRING(iovec[n_iovec++], core_comm);
1eef15b1
ZJS
1043 }
1044
1045 if (exe) {
63c372cb 1046 core_exe = strjoina("COREDUMP_EXE=", exe);
3c171f0b 1047 IOVEC_SET_STRING(iovec[n_iovec++], core_exe);
f5e04665
LP
1048 }
1049
9bdbc2e2 1050 if (get_process_cmdline(pid, 0, false, &t) >= 0) {
63c372cb 1051 core_cmdline = strjoina("COREDUMP_CMDLINE=", t);
f5e04665
LP
1052 free(t);
1053
3c171f0b 1054 IOVEC_SET_STRING(iovec[n_iovec++], core_cmdline);
f5e04665
LP
1055 }
1056
a035f819 1057 if (cg_pid_get_path_shifted(pid, NULL, &t) >= 0) {
63c372cb 1058 core_cgroup = strjoina("COREDUMP_CGROUP=", t);
a035f819
LP
1059 free(t);
1060
3c171f0b 1061 IOVEC_SET_STRING(iovec[n_iovec++], core_cgroup);
a035f819
LP
1062 }
1063
3f132692
JF
1064 if (compose_open_fds(pid, &t) >= 0) {
1065 core_open_fds = strappend("COREDUMP_OPEN_FDS=", t);
1066 free(t);
1067
1068 if (core_open_fds)
3c171f0b 1069 IOVEC_SET_STRING(iovec[n_iovec++], core_open_fds);
3f132692
JF
1070 }
1071
1072 p = procfs_file_alloca(pid, "status");
1073 if (read_full_file(p, &t, NULL) >= 0) {
1074 core_proc_status = strappend("COREDUMP_PROC_STATUS=", t);
1075 free(t);
1076
1077 if (core_proc_status)
3c171f0b 1078 IOVEC_SET_STRING(iovec[n_iovec++], core_proc_status);
3f132692
JF
1079 }
1080
1081 p = procfs_file_alloca(pid, "maps");
1082 if (read_full_file(p, &t, NULL) >= 0) {
1083 core_proc_maps = strappend("COREDUMP_PROC_MAPS=", t);
1084 free(t);
1085
1086 if (core_proc_maps)
3c171f0b 1087 IOVEC_SET_STRING(iovec[n_iovec++], core_proc_maps);
3f132692
JF
1088 }
1089
1090 p = procfs_file_alloca(pid, "limits");
1091 if (read_full_file(p, &t, NULL) >= 0) {
1092 core_proc_limits = strappend("COREDUMP_PROC_LIMITS=", t);
1093 free(t);
1094
1095 if (core_proc_limits)
3c171f0b 1096 IOVEC_SET_STRING(iovec[n_iovec++], core_proc_limits);
3f132692
JF
1097 }
1098
1099 p = procfs_file_alloca(pid, "cgroup");
1100 if (read_full_file(p, &t, NULL) >=0) {
1101 core_proc_cgroup = strappend("COREDUMP_PROC_CGROUP=", t);
1102 free(t);
1103
1104 if (core_proc_cgroup)
3c171f0b 1105 IOVEC_SET_STRING(iovec[n_iovec++], core_proc_cgroup);
3f132692
JF
1106 }
1107
1108 if (get_process_cwd(pid, &t) >= 0) {
63c372cb 1109 core_cwd = strjoina("COREDUMP_CWD=", t);
3f132692
JF
1110 free(t);
1111
3c171f0b 1112 IOVEC_SET_STRING(iovec[n_iovec++], core_cwd);
3f132692
JF
1113 }
1114
1115 if (get_process_root(pid, &t) >= 0) {
63c372cb 1116 core_root = strjoina("COREDUMP_ROOT=", t);
3f132692
JF
1117 free(t);
1118
3c171f0b 1119 IOVEC_SET_STRING(iovec[n_iovec++], core_root);
3f132692
JF
1120 }
1121
1122 if (get_process_environ(pid, &t) >= 0) {
1123 core_environ = strappend("COREDUMP_ENVIRON=", t);
1124 free(t);
1125
1126 if (core_environ)
3c171f0b 1127 IOVEC_SET_STRING(iovec[n_iovec++], core_environ);
3f132692
JF
1128 }
1129
81d62103 1130 core_timestamp = strjoina("COREDUMP_TIMESTAMP=", context[CONTEXT_TIMESTAMP], "000000");
3c171f0b 1131 IOVEC_SET_STRING(iovec[n_iovec++], core_timestamp);
f5e04665 1132
3c171f0b 1133 IOVEC_SET_STRING(iovec[n_iovec++], "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1");
4850d39a
LP
1134
1135 assert_cc(2 == LOG_CRIT);
3c171f0b 1136 IOVEC_SET_STRING(iovec[n_iovec++], "PRIORITY=2");
0dc5d23c 1137
3c171f0b 1138 assert(n_iovec <= ELEMENTSOF(iovec));
34c10968 1139
3c171f0b
LP
1140 return send_iovec(iovec, n_iovec, STDIN_FILENO);
1141}
34c10968 1142
3c171f0b
LP
1143int main(int argc, char *argv[]) {
1144 int r;
fee80f69 1145
3c171f0b
LP
1146 /* First, log to a safe place, since we don't know what crashed and it might be journald which we'd rather not
1147 * log to then. */
8d4e028f 1148
3c171f0b
LP
1149 log_set_target(LOG_TARGET_KMSG);
1150 log_open();
8d4e028f 1151
3c171f0b
LP
1152 /* Make sure we never enter a loop */
1153 (void) prctl(PR_SET_DUMPABLE, 0);
8d4e028f 1154
3c171f0b
LP
1155 /* Ignore all parse errors */
1156 (void) parse_config();
fee80f69 1157
3c171f0b
LP
1158 log_debug("Selected storage '%s'.", coredump_storage_to_string(arg_storage));
1159 log_debug("Selected compression %s.", yes_no(arg_compress));
fee80f69 1160
3c171f0b
LP
1161 r = sd_listen_fds(false);
1162 if (r < 0) {
1163 log_error_errno(r, "Failed to determine number of file descriptor: %m");
1164 goto finish;
fee80f69
LP
1165 }
1166
3c171f0b
LP
1167 /* If we got an fd passed, we are running in coredumpd mode. Otherwise we are invoked from the kernel as
1168 * coredump handler */
1169 if (r == 0)
1170 r = process_kernel(argc, argv);
1171 else if (r == 1)
1172 r = process_socket(SD_LISTEN_FDS_START);
1173 else {
1174 log_error("Received unexpected number of file descriptors.");
1175 r = -EINVAL;
1176 }
f5e04665
LP
1177
1178finish:
f5e04665
LP
1179 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1180}