]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/coredump/coredump.c
treewide: fix typos (#4217)
[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
43688c49 131 return config_parse_many_nulstr(PKGSYSCONFDIR "/coredump.conf",
75eb6154 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 ||
d710aaf7
ZJS
160 acl_set_qualifier(entry, &uid) < 0)
161 return log_error_errno(errno, "Failed to patch ACL: %m");
34c10968
LP
162
163 if (acl_get_permset(entry, &permset) < 0 ||
709f6e46
MS
164 acl_add_perm(permset, ACL_READ) < 0)
165 return log_warning_errno(errno, "Failed to patch ACL: %m");
166
167 r = calc_acl_mask_if_needed(&acl);
168 if (r < 0)
169 return log_warning_errno(r, "Failed to patch ACL: %m");
34c10968 170
4a62c710
MS
171 if (acl_set_fd(fd, acl) < 0)
172 return log_error_errno(errno, "Failed to apply ACL: %m");
34c10968
LP
173#endif
174
175 return 0;
176}
177
3c171f0b 178static int fix_xattr(int fd, const char *context[_CONTEXT_MAX]) {
0cd77f97 179
3c171f0b
LP
180 static const char * const xattrs[_CONTEXT_MAX] = {
181 [CONTEXT_PID] = "user.coredump.pid",
182 [CONTEXT_UID] = "user.coredump.uid",
183 [CONTEXT_GID] = "user.coredump.gid",
184 [CONTEXT_SIGNAL] = "user.coredump.signal",
185 [CONTEXT_TIMESTAMP] = "user.coredump.timestamp",
186 [CONTEXT_COMM] = "user.coredump.comm",
187 [CONTEXT_EXE] = "user.coredump.exe",
0cd77f97
LP
188 };
189
34c10968 190 int r = 0;
0cd77f97 191 unsigned i;
34c10968 192
b59233e6
LP
193 assert(fd >= 0);
194
1eef15b1 195 /* Attach some metadata to coredumps via extended
34c10968
LP
196 * attributes. Just because we can. */
197
3c171f0b 198 for (i = 0; i < _CONTEXT_MAX; i++) {
1eef15b1
ZJS
199 int k;
200
3c171f0b 201 if (isempty(context[i]) || !xattrs[i])
0cd77f97 202 continue;
34c10968 203
3c171f0b 204 k = fsetxattr(fd, xattrs[i], context[i], strlen(context[i]), XATTR_CREATE);
1eef15b1 205 if (k < 0 && r == 0)
34c10968 206 r = -errno;
0cd77f97 207 }
34c10968
LP
208
209 return r;
210}
211
b0b21dce 212#define filename_escape(s) xescape((s), "./ ")
34c10968 213
0c773903
EV
214static inline const char *coredump_tmpfile_name(const char *s) {
215 return s ? s : "(unnamed temporary file)";
216}
217
b59233e6
LP
218static int fix_permissions(
219 int fd,
220 const char *filename,
221 const char *target,
3c171f0b 222 const char *context[_CONTEXT_MAX],
b59233e6
LP
223 uid_t uid) {
224
03532f0a
LP
225 int r;
226
b59233e6 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
03532f0a
LP
239 r = link_tmpfile(fd, filename, target);
240 if (r < 0)
241 return log_error_errno(r, "Failed to move coredump %s into place: %m", target);
cfd652ed
ZJS
242
243 return 0;
244}
245
59f448cf 246static int maybe_remove_external_coredump(const char *filename, uint64_t size) {
cfd652ed 247
b59233e6 248 /* Returns 1 if might remove, 0 if will not remove, < 0 on error. */
cfd652ed
ZJS
249
250 if (IN_SET(arg_storage, COREDUMP_STORAGE_EXTERNAL, COREDUMP_STORAGE_BOTH) &&
251 size <= arg_external_size_max)
252 return 0;
253
254 if (!filename)
255 return 1;
256
4a62c710
MS
257 if (unlink(filename) < 0 && errno != ENOENT)
258 return log_error_errno(errno, "Failed to unlink %s: %m", filename);
cfd652ed
ZJS
259
260 return 1;
261}
262
3c171f0b 263static int make_filename(const char *context[_CONTEXT_MAX], char **ret) {
b59233e6 264 _cleanup_free_ char *c = NULL, *u = NULL, *p = NULL, *t = NULL;
a7f7d1bd 265 sd_id128_t boot = {};
34c10968
LP
266 int r;
267
3c171f0b 268 assert(context);
34c10968 269
3c171f0b 270 c = filename_escape(context[CONTEXT_COMM]);
34c10968 271 if (!c)
b59233e6 272 return -ENOMEM;
34c10968 273
3c171f0b 274 u = filename_escape(context[CONTEXT_UID]);
0dc5d23c 275 if (!u)
b59233e6 276 return -ENOMEM;
34c10968
LP
277
278 r = sd_id128_get_boot(&boot);
b59233e6 279 if (r < 0)
34c10968 280 return r;
34c10968 281
3c171f0b 282 p = filename_escape(context[CONTEXT_PID]);
b59233e6
LP
283 if (!p)
284 return -ENOMEM;
285
3c171f0b 286 t = filename_escape(context[CONTEXT_TIMESTAMP]);
b59233e6
LP
287 if (!t)
288 return -ENOMEM;
289
290 if (asprintf(ret,
0dc5d23c 291 "/var/lib/systemd/coredump/core.%s.%s." SD_ID128_FORMAT_STR ".%s.%s000000",
34c10968 292 c,
0dc5d23c 293 u,
34c10968
LP
294 SD_ID128_FORMAT_VAL(boot),
295 p,
b59233e6
LP
296 t) < 0)
297 return -ENOMEM;
298
299 return 0;
300}
301
302static int save_external_coredump(
3c171f0b
LP
303 const char *context[_CONTEXT_MAX],
304 int input_fd,
b59233e6 305 char **ret_filename,
5f3e0a74
HW
306 int *ret_node_fd,
307 int *ret_data_fd,
59f448cf 308 uint64_t *ret_size) {
b59233e6
LP
309
310 _cleanup_free_ char *fn = NULL, *tmp = NULL;
311 _cleanup_close_ int fd = -1;
bdfd7b2c 312 uint64_t rlimit, max_size;
b59233e6 313 struct stat st;
3c171f0b 314 uid_t uid;
b59233e6
LP
315 int r;
316
3c171f0b 317 assert(context);
b59233e6 318 assert(ret_filename);
5f3e0a74
HW
319 assert(ret_node_fd);
320 assert(ret_data_fd);
b59233e6
LP
321 assert(ret_size);
322
3c171f0b
LP
323 r = parse_uid(context[CONTEXT_UID], &uid);
324 if (r < 0)
325 return log_error_errno(r, "Failed to parse UID: %m");
326
bdfd7b2c
LP
327 r = safe_atou64(context[CONTEXT_RLIMIT], &rlimit);
328 if (r < 0)
329 return log_error_errno(r, "Failed to parse resource limit: %s", context[CONTEXT_RLIMIT]);
6998b540
ZJS
330 if (rlimit < page_size()) {
331 /* Is coredumping disabled? Then don't bother saving/processing the coredump.
332 * Anything below PAGE_SIZE cannot give a readable coredump (the kernel uses
333 * ELF_EXEC_PAGESIZE which is not easily accessible, but is usually the same as PAGE_SIZE. */
334 log_info("Core dumping has been disabled for process %s (%s).", context[CONTEXT_PID], context[CONTEXT_COMM]);
bdfd7b2c
LP
335 return -EBADSLT;
336 }
337
338 /* Never store more than the process configured, or than we actually shall keep or process */
339 max_size = MIN(rlimit, MAX(arg_process_size_max, arg_external_size_max));
340
3c171f0b 341 r = make_filename(context, &fn);
23bbb0de
MS
342 if (r < 0)
343 return log_error_errno(r, "Failed to determine coredump file name: %m");
34c10968 344
d2e54fae 345 mkdir_p_label("/var/lib/systemd/coredump", 0755);
803a3464 346
03532f0a 347 fd = open_tmpfile_linkable(fn, O_RDWR|O_CLOEXEC, &tmp);
4a62c710 348 if (fd < 0)
03532f0a 349 return log_error_errno(fd, "Failed to create temporary file for coredump %s: %m", fn);
803a3464 350
bdfd7b2c 351 r = copy_bytes(input_fd, fd, max_size, false);
84ee0960 352 if (r == -EFBIG) {
3c171f0b 353 log_error("Coredump of %s (%s) is larger than configured processing limit, refusing.", context[CONTEXT_PID], context[CONTEXT_COMM]);
93240d3a
LP
354 goto fail;
355 } else if (IN_SET(r, -EDQUOT, -ENOSPC)) {
3c171f0b 356 log_error("Not enough disk space for coredump of %s (%s), refusing.", context[CONTEXT_PID], context[CONTEXT_COMM]);
93240d3a
LP
357 goto fail;
358 } else if (r < 0) {
da927ba9 359 log_error_errno(r, "Failed to dump coredump to file: %m");
34c10968
LP
360 goto fail;
361 }
803a3464 362
34c10968 363 if (fstat(fd, &st) < 0) {
0c773903 364 log_error_errno(errno, "Failed to fstat coredump %s: %m", coredump_tmpfile_name(tmp));
34c10968
LP
365 goto fail;
366 }
367
7849c2ac 368 if (lseek(fd, 0, SEEK_SET) == (off_t) -1) {
0c773903 369 log_error_errno(errno, "Failed to seek on %s: %m", coredump_tmpfile_name(tmp));
b59233e6 370 goto fail;
7849c2ac
TA
371 }
372
d89c8fdf 373#if defined(HAVE_XZ) || defined(HAVE_LZ4)
cfd652ed 374 /* If we will remove the coredump anyway, do not compress. */
34727273 375 if (maybe_remove_external_coredump(NULL, st.st_size) == 0
8c9571d0 376 && arg_compress) {
cfd652ed 377
b59233e6
LP
378 _cleanup_free_ char *fn_compressed = NULL, *tmp_compressed = NULL;
379 _cleanup_close_ int fd_compressed = -1;
cfd652ed 380
d89c8fdf 381 fn_compressed = strappend(fn, COMPRESSED_EXT);
b59233e6 382 if (!fn_compressed) {
d89c8fdf 383 log_oom();
cfd652ed
ZJS
384 goto uncompressed;
385 }
386
03532f0a
LP
387 fd_compressed = open_tmpfile_linkable(fn_compressed, O_RDWR|O_CLOEXEC, &tmp_compressed);
388 if (fd_compressed < 0) {
389 log_error_errno(fd_compressed, "Failed to create temporary file for coredump %s: %m", fn_compressed);
b59233e6 390 goto uncompressed;
03532f0a 391 }
cfd652ed 392
d89c8fdf 393 r = compress_stream(fd, fd_compressed, -1);
b59233e6 394 if (r < 0) {
0c773903 395 log_error_errno(r, "Failed to compress %s: %m", coredump_tmpfile_name(tmp_compressed));
b59233e6
LP
396 goto fail_compressed;
397 }
398
3c171f0b 399 r = fix_permissions(fd_compressed, tmp_compressed, fn_compressed, context, uid);
cfd652ed 400 if (r < 0)
b59233e6
LP
401 goto fail_compressed;
402
403 /* OK, this worked, we can get rid of the uncompressed version now */
0c773903
EV
404 if (tmp)
405 unlink_noerrno(tmp);
cfd652ed 406
59f448cf 407 *ret_filename = fn_compressed; /* compressed */
5f3e0a74
HW
408 *ret_node_fd = fd_compressed; /* compressed */
409 *ret_data_fd = fd; /* uncompressed */
59f448cf 410 *ret_size = (uint64_t) st.st_size; /* uncompressed */
cfd652ed 411
b59233e6 412 fn_compressed = NULL;
5f3e0a74 413 fd = fd_compressed = -1;
cfd652ed
ZJS
414
415 return 0;
416
b59233e6 417 fail_compressed:
0c773903
EV
418 if (tmp_compressed)
419 (void) unlink(tmp_compressed);
34c10968 420 }
cfd652ed
ZJS
421
422uncompressed:
3b1a55e1 423#endif
5f3e0a74 424
3c171f0b 425 r = fix_permissions(fd, tmp, fn, context, uid);
cfd652ed
ZJS
426 if (r < 0)
427 goto fail;
34c10968
LP
428
429 *ret_filename = fn;
5f3e0a74
HW
430 *ret_data_fd = fd;
431 *ret_node_fd = -1;
59f448cf 432 *ret_size = (uint64_t) st.st_size;
34c10968
LP
433
434 fn = NULL;
435 fd = -1;
436
437 return 0;
438
439fail:
0c773903
EV
440 if (tmp)
441 (void) unlink(tmp);
34c10968
LP
442 return r;
443}
444
445static int allocate_journal_field(int fd, size_t size, char **ret, size_t *ret_size) {
446 _cleanup_free_ char *field = NULL;
447 ssize_t n;
448
8d4e028f 449 assert(fd >= 0);
34c10968
LP
450 assert(ret);
451 assert(ret_size);
452
4a62c710
MS
453 if (lseek(fd, 0, SEEK_SET) == (off_t) -1)
454 return log_warning_errno(errno, "Failed to seek: %m");
803a3464 455
34c10968
LP
456 field = malloc(9 + size);
457 if (!field) {
cfd652ed 458 log_warning("Failed to allocate memory for coredump, coredump will not be stored.");
34c10968
LP
459 return -ENOMEM;
460 }
461
462 memcpy(field, "COREDUMP=", 9);
463
464 n = read(fd, field + 9, size);
23bbb0de
MS
465 if (n < 0)
466 return log_error_errno((int) n, "Failed to read core data: %m");
34c10968
LP
467 if ((size_t) n < size) {
468 log_error("Core data too short.");
469 return -EIO;
470 }
471
472 *ret = field;
473 *ret_size = size + 9;
474
475 field = NULL;
476
477 return 0;
478}
803a3464 479
3f132692
JF
480/* Joins /proc/[pid]/fd/ and /proc/[pid]/fdinfo/ into the following lines:
481 * 0:/dev/pts/23
482 * pos: 0
483 * flags: 0100002
484 *
485 * 1:/dev/pts/23
486 * pos: 0
487 * flags: 0100002
488 *
489 * 2:/dev/pts/23
490 * pos: 0
491 * flags: 0100002
492 * EOF
493 */
494static int compose_open_fds(pid_t pid, char **open_fds) {
4d84bc2f
LP
495 _cleanup_closedir_ DIR *proc_fd_dir = NULL;
496 _cleanup_close_ int proc_fdinfo_fd = -1;
497 _cleanup_free_ char *buffer = NULL;
3f132692 498 _cleanup_fclose_ FILE *stream = NULL;
59059b4a 499 const char *fddelim = "", *path;
3f132692 500 struct dirent *dent = NULL;
4d84bc2f 501 size_t size = 0;
3f132692
JF
502 int r = 0;
503
504 assert(pid >= 0);
505 assert(open_fds != NULL);
506
59059b4a 507 path = procfs_file_alloca(pid, "fd");
3f132692 508 proc_fd_dir = opendir(path);
59059b4a
ZJS
509 if (!proc_fd_dir)
510 return -errno;
3f132692 511
4d84bc2f 512 proc_fdinfo_fd = openat(dirfd(proc_fd_dir), "../fdinfo", O_DIRECTORY|O_NOFOLLOW|O_CLOEXEC|O_PATH);
59059b4a
ZJS
513 if (proc_fdinfo_fd < 0)
514 return -errno;
3f132692 515
4d84bc2f 516 stream = open_memstream(&buffer, &size);
3f132692
JF
517 if (!stream)
518 return -ENOMEM;
519
4d84bc2f 520 FOREACH_DIRENT(dent, proc_fd_dir, return -errno) {
3f132692 521 _cleanup_fclose_ FILE *fdinfo = NULL;
4d84bc2f 522 _cleanup_free_ char *fdname = NULL;
59059b4a 523 char line[LINE_MAX];
4d84bc2f 524 int fd;
3f132692 525
59059b4a 526 r = readlinkat_malloc(dirfd(proc_fd_dir), dent->d_name, &fdname);
3f132692
JF
527 if (r < 0)
528 return r;
529
530 fprintf(stream, "%s%s:%s\n", fddelim, dent->d_name, fdname);
531 fddelim = "\n";
532
533 /* Use the directory entry from /proc/[pid]/fd with /proc/[pid]/fdinfo */
59059b4a
ZJS
534 fd = openat(proc_fdinfo_fd, dent->d_name, O_NOFOLLOW|O_CLOEXEC|O_RDONLY);
535 if (fd < 0)
3f132692
JF
536 continue;
537
59059b4a
ZJS
538 fdinfo = fdopen(fd, "re");
539 if (fdinfo == NULL) {
540 close(fd);
3f132692 541 continue;
59059b4a 542 }
3f132692 543
4d84bc2f
LP
544 FOREACH_LINE(line, fdinfo, break) {
545 fputs(line, stream);
546 if (!endswith(line, "\n"))
547 fputc('\n', stream);
548 }
3f132692
JF
549 }
550
4d84bc2f 551 errno = 0;
74ca738f 552 stream = safe_fclose(stream);
4d84bc2f 553
b3267152 554 if (errno > 0)
4d84bc2f
LP
555 return -errno;
556
557 *open_fds = buffer;
558 buffer = NULL;
559
3f132692
JF
560 return 0;
561}
562
7ed03ce6
JF
563static int get_process_ns(pid_t pid, const char *namespace, ino_t *ns) {
564 const char *p;
565 struct stat stbuf;
566 _cleanup_close_ int proc_ns_dir_fd;
567
568 p = procfs_file_alloca(pid, "ns");
569
570 proc_ns_dir_fd = open(p, O_DIRECTORY | O_CLOEXEC | O_RDONLY);
571 if (proc_ns_dir_fd < 0)
572 return -errno;
573
574 if (fstatat(proc_ns_dir_fd, namespace, &stbuf, /* flags */0) < 0)
575 return -errno;
576
577 *ns = stbuf.st_ino;
578 return 0;
579}
580
581static int get_mount_namespace_leader(pid_t pid, pid_t *container_pid) {
582 pid_t cpid = pid, ppid = 0;
583 ino_t proc_mntns;
584 int r = 0;
585
586 r = get_process_ns(pid, "mnt", &proc_mntns);
587 if (r < 0)
588 return r;
589
590 while (1) {
591 ino_t parent_mntns;
592
593 r = get_process_ppid(cpid, &ppid);
594 if (r < 0)
595 return r;
596
597 r = get_process_ns(ppid, "mnt", &parent_mntns);
598 if (r < 0)
599 return r;
600
601 if (proc_mntns != parent_mntns)
602 break;
603
604 if (ppid == 1)
605 return -ENOENT;
606
607 cpid = ppid;
608 }
609
610 *container_pid = ppid;
611 return 0;
612}
613
614/* Returns 1 if the parent was found.
615 * Returns 0 if there is not a process we can call the pid's
616 * container parent (the pid's process isn't 'containerized').
617 * Returns a negative number on errors.
618 */
619static int get_process_container_parent_cmdline(pid_t pid, char** cmdline) {
620 int r = 0;
621 pid_t container_pid;
622 const char *proc_root_path;
623 struct stat root_stat, proc_root_stat;
624
625 /* To compare inodes of / and /proc/[pid]/root */
626 if (stat("/", &root_stat) < 0)
627 return -errno;
628
629 proc_root_path = procfs_file_alloca(pid, "root");
630 if (stat(proc_root_path, &proc_root_stat) < 0)
631 return -errno;
632
633 /* The process uses system root. */
634 if (proc_root_stat.st_ino == root_stat.st_ino) {
635 *cmdline = NULL;
636 return 0;
637 }
638
639 r = get_mount_namespace_leader(pid, &container_pid);
640 if (r < 0)
641 return r;
642
643 return get_process_cmdline(container_pid, 0, false, cmdline);
644}
645
3c171f0b
LP
646static int change_uid_gid(const char *context[]) {
647 uid_t uid;
648 gid_t gid;
649 int r;
34c10968 650
3c171f0b
LP
651 r = parse_uid(context[CONTEXT_UID], &uid);
652 if (r < 0)
653 return r;
8c8549db 654
888e378d
LP
655 if (uid <= SYSTEM_UID_MAX) {
656 const char *user = "systemd-coredump";
657
658 r = get_user_creds(&user, &uid, &gid, NULL, NULL);
659 if (r < 0) {
660 log_warning_errno(r, "Cannot resolve %s user. Proceeding to dump core as root: %m", user);
661 uid = gid = 0;
662 }
663 } else {
664 r = parse_gid(context[CONTEXT_GID], &gid);
665 if (r < 0)
666 return r;
667 }
3c171f0b
LP
668
669 return drop_privileges(uid, gid, 0);
670}
8c8549db 671
3c171f0b
LP
672static int submit_coredump(
673 const char *context[_CONTEXT_MAX],
674 struct iovec *iovec,
675 size_t n_iovec_allocated,
676 size_t n_iovec,
677 int input_fd) {
34c10968 678
5f3e0a74 679 _cleanup_close_ int coredump_fd = -1, coredump_node_fd = -1;
3c171f0b 680 _cleanup_free_ char *core_message = NULL, *filename = NULL, *coredump_data = NULL;
59f448cf 681 uint64_t coredump_size;
3c171f0b 682 int r;
f5e04665 683
3c171f0b
LP
684 assert(context);
685 assert(iovec);
686 assert(n_iovec_allocated >= n_iovec + 3);
687 assert(input_fd >= 0);
f5e04665 688
3c171f0b
LP
689 /* Vacuum before we write anything again */
690 (void) coredump_vacuum(-1, arg_keep_free, arg_max_use);
803a3464 691
3c171f0b
LP
692 /* Always stream the coredump to disk, if that's possible */
693 r = save_external_coredump(context, input_fd, &filename, &coredump_node_fd, &coredump_fd, &coredump_size);
694 if (r < 0)
695 /* Skip whole core dumping part */
696 goto log;
697
698 /* If we don't want to keep the coredump on disk, remove it now, as later on we will lack the privileges for
699 * it. However, we keep the fd to it, so that we can still process it and log it. */
700 r = maybe_remove_external_coredump(filename, coredump_size);
701 if (r < 0)
702 return r;
703 if (r == 0) {
704 const char *coredump_filename;
705
706 coredump_filename = strjoina("COREDUMP_FILENAME=", filename);
707 IOVEC_SET_STRING(iovec[n_iovec++], coredump_filename);
f5e04665
LP
708 }
709
3c171f0b
LP
710 /* Vacuum again, but exclude the coredump we just created */
711 (void) coredump_vacuum(coredump_node_fd >= 0 ? coredump_node_fd : coredump_fd, arg_keep_free, arg_max_use);
8c9571d0 712
3c171f0b
LP
713 /* Now, let's drop privileges to become the user who owns the segfaulted process and allocate the coredump
714 * memory under the user's uid. This also ensures that the credentials journald will see are the ones of the
715 * coredumping user, thus making sure the user gets access to the core dump. Let's also get rid of all
716 * capabilities, if we run as root, we won't need them anymore. */
717 r = change_uid_gid(context);
718 if (r < 0)
719 return log_error_errno(r, "Failed to drop privileges: %m");
34c10968 720
3c171f0b
LP
721#ifdef HAVE_ELFUTILS
722 /* Try to get a strack trace if we can */
723 if (coredump_size <= arg_process_size_max) {
724 _cleanup_free_ char *stacktrace = NULL;
725
726 r = coredump_make_stack_trace(coredump_fd, context[CONTEXT_EXE], &stacktrace);
727 if (r >= 0)
728 core_message = strjoin("MESSAGE=Process ", context[CONTEXT_PID], " (", context[CONTEXT_COMM], ") of user ", context[CONTEXT_UID], " dumped core.\n\n", stacktrace, NULL);
729 else if (r == -EINVAL)
730 log_warning("Failed to generate stack trace: %s", dwfl_errmsg(dwfl_errno()));
731 else
732 log_warning_errno(r, "Failed to generate stack trace: %m");
34c10968 733 }
803a3464 734
3c171f0b
LP
735 if (!core_message)
736#endif
737log:
738 core_message = strjoin("MESSAGE=Process ", context[CONTEXT_PID], " (", context[CONTEXT_COMM], ") of user ", context[CONTEXT_UID], " dumped core.", NULL);
739 if (core_message)
740 IOVEC_SET_STRING(iovec[n_iovec++], core_message);
741
742 /* Optionally store the entire coredump in the journal */
743 if (IN_SET(arg_storage, COREDUMP_STORAGE_JOURNAL, COREDUMP_STORAGE_BOTH) &&
744 coredump_size <= arg_journal_size_max) {
745 size_t sz = 0;
746
747 /* Store the coredump itself in the journal */
748
749 r = allocate_journal_field(coredump_fd, (size_t) coredump_size, &coredump_data, &sz);
750 if (r >= 0) {
751 iovec[n_iovec].iov_base = coredump_data;
752 iovec[n_iovec].iov_len = sz;
753 n_iovec++;
754 }
f5e04665
LP
755 }
756
3c171f0b
LP
757 assert(n_iovec <= n_iovec_allocated);
758
759 r = sd_journal_sendv(iovec, n_iovec);
760 if (r < 0)
761 return log_error_errno(r, "Failed to log coredump: %m");
762
763 return 0;
764}
765
766static void map_context_fields(const struct iovec *iovec, const char *context[]) {
767
768 static const char * const context_field_names[_CONTEXT_MAX] = {
769 [CONTEXT_PID] = "COREDUMP_PID=",
770 [CONTEXT_UID] = "COREDUMP_UID=",
771 [CONTEXT_GID] = "COREDUMP_GID=",
772 [CONTEXT_SIGNAL] = "COREDUMP_SIGNAL=",
773 [CONTEXT_TIMESTAMP] = "COREDUMP_TIMESTAMP=",
774 [CONTEXT_COMM] = "COREDUMP_COMM=",
775 [CONTEXT_EXE] = "COREDUMP_EXE=",
bdfd7b2c 776 [CONTEXT_RLIMIT] = "COREDUMP_RLIMIT=",
3c171f0b
LP
777 };
778
779 unsigned i;
780
781 assert(iovec);
782 assert(context);
783
784 for (i = 0; i < _CONTEXT_MAX; i++) {
785 size_t l;
786
787 l = strlen(context_field_names[i]);
788 if (iovec->iov_len < l)
789 continue;
790
791 if (memcmp(iovec->iov_base, context_field_names[i], l) != 0)
792 continue;
793
794 /* Note that these strings are NUL terminated, because we made sure that a trailing NUL byte is in the
795 * buffer, though not included in the iov_len count. (see below) */
796 context[i] = (char*) iovec->iov_base + l;
797 break;
798 }
799}
800
801static int process_socket(int fd) {
802 _cleanup_close_ int coredump_fd = -1;
803 struct iovec *iovec = NULL;
804 size_t n_iovec = 0, n_iovec_allocated = 0, i;
805 const char *context[_CONTEXT_MAX] = {};
806 int r;
807
808 assert(fd >= 0);
809
810 log_set_target(LOG_TARGET_AUTO);
811 log_parse_environment();
812 log_open();
813
814 for (;;) {
815 union {
816 struct cmsghdr cmsghdr;
817 uint8_t buf[CMSG_SPACE(sizeof(int))];
818 } control = {};
819 struct msghdr mh = {
820 .msg_control = &control,
821 .msg_controllen = sizeof(control),
822 .msg_iovlen = 1,
823 };
824 ssize_t n;
fe1ef0f8 825 ssize_t l;
3c171f0b
LP
826
827 if (!GREEDY_REALLOC(iovec, n_iovec_allocated, n_iovec + 3)) {
828 r = log_oom();
829 goto finish;
830 }
831
fe1ef0f8
EV
832 l = next_datagram_size_fd(fd);
833 if (l < 0) {
834 r = log_error_errno(l, "Failed to determine datagram size to read: %m");
3c171f0b
LP
835 goto finish;
836 }
837
838 assert(l >= 0);
839
840 iovec[n_iovec].iov_len = l;
841 iovec[n_iovec].iov_base = malloc(l + 1);
3c171f0b
LP
842 if (!iovec[n_iovec].iov_base) {
843 r = log_oom();
844 goto finish;
845 }
846
847 mh.msg_iov = iovec + n_iovec;
848
849 n = recvmsg(fd, &mh, MSG_NOSIGNAL|MSG_CMSG_CLOEXEC);
850 if (n < 0) {
851 free(iovec[n_iovec].iov_base);
852 r = log_error_errno(errno, "Failed to receive datagram: %m");
853 goto finish;
854 }
855
856 if (n == 0) {
857 struct cmsghdr *cmsg, *found = NULL;
858 /* The final zero-length datagram carries the file descriptor and tells us that we're done. */
859
860 free(iovec[n_iovec].iov_base);
861
862 CMSG_FOREACH(cmsg, &mh) {
863 if (cmsg->cmsg_level == SOL_SOCKET &&
864 cmsg->cmsg_type == SCM_RIGHTS &&
865 cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
866 assert(!found);
867 found = cmsg;
868 }
869 }
870
871 if (!found) {
872 log_error("Coredump file descriptor missing.");
873 r = -EBADMSG;
874 goto finish;
875 }
876
877 assert(coredump_fd < 0);
878 coredump_fd = *(int*) CMSG_DATA(found);
879 break;
880 }
881
882 /* Add trailing NUL byte, in case these are strings */
883 ((char*) iovec[n_iovec].iov_base)[n] = 0;
884 iovec[n_iovec].iov_len = (size_t) n;
885
886 cmsg_close_all(&mh);
887 map_context_fields(iovec + n_iovec, context);
888 n_iovec++;
889 }
890
891 if (!GREEDY_REALLOC(iovec, n_iovec_allocated, n_iovec + 3)) {
892 r = log_oom();
34c10968
LP
893 goto finish;
894 }
895
61233823 896 /* Make sure we got all data we really need */
3c171f0b
LP
897 assert(context[CONTEXT_PID]);
898 assert(context[CONTEXT_UID]);
899 assert(context[CONTEXT_GID]);
900 assert(context[CONTEXT_SIGNAL]);
901 assert(context[CONTEXT_TIMESTAMP]);
bdfd7b2c 902 assert(context[CONTEXT_RLIMIT]);
3c171f0b
LP
903 assert(context[CONTEXT_COMM]);
904 assert(coredump_fd >= 0);
905
906 r = submit_coredump(context, iovec, n_iovec_allocated, n_iovec, coredump_fd);
907
908finish:
909 for (i = 0; i < n_iovec; i++)
910 free(iovec[i].iov_base);
911 free(iovec);
912
913 return r;
914}
915
916static int send_iovec(const struct iovec iovec[], size_t n_iovec, int input_fd) {
917
918 static const union sockaddr_union sa = {
919 .un.sun_family = AF_UNIX,
920 .un.sun_path = "/run/systemd/coredump",
921 };
922 _cleanup_close_ int fd = -1;
923 size_t i;
924 int r;
925
926 assert(iovec || n_iovec <= 0);
927 assert(input_fd >= 0);
928
929 fd = socket(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0);
930 if (fd < 0)
931 return log_error_errno(errno, "Failed to create coredump socket: %m");
932
fc2fffe7 933 if (connect(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0)
3c171f0b
LP
934 return log_error_errno(errno, "Failed to connect to coredump service: %m");
935
936 for (i = 0; i < n_iovec; i++) {
fec603eb
LP
937 struct msghdr mh = {
938 .msg_iov = (struct iovec*) iovec + i,
939 .msg_iovlen = 1,
940 };
941 struct iovec copy[2];
942
943 for (;;) {
944 if (sendmsg(fd, &mh, MSG_NOSIGNAL) >= 0)
945 break;
946
947 if (errno == EMSGSIZE && mh.msg_iov[0].iov_len > 0) {
948 /* This field didn't fit? That's a pity. Given that this is just metadata,
949 * let's truncate the field at half, and try again. We append three dots, in
950 * order to show that this is truncated. */
951
952 if (mh.msg_iov != copy) {
953 /* We don't want to modify the caller's iovec, hence let's create our
954 * own array, consisting of two new iovecs, where the first is a
955 * (truncated) copy of what we want to send, and the second one
956 * contains the trailing dots. */
957 copy[0] = iovec[i];
958 copy[1] = (struct iovec) {
959 .iov_base = (char[]) { '.', '.', '.' },
960 .iov_len = 3,
961 };
962
963 mh.msg_iov = copy;
964 mh.msg_iovlen = 2;
965 }
966
967 copy[0].iov_len /= 2; /* halve it, and try again */
968 continue;
969 }
3c171f0b 970
3c171f0b 971 return log_error_errno(errno, "Failed to send coredump datagram: %m");
fec603eb 972 }
1eef15b1
ZJS
973 }
974
3c171f0b
LP
975 r = send_one_fd(fd, input_fd, 0);
976 if (r < 0)
977 return log_error_errno(r, "Failed to send coredump fd: %m");
1eef15b1 978
3c171f0b
LP
979 return 0;
980}
1eef15b1 981
78f043f7 982static int process_special_crash(const char *context[], int input_fd) {
3c171f0b
LP
983 _cleanup_close_ int coredump_fd = -1, coredump_node_fd = -1;
984 _cleanup_free_ char *filename = NULL;
985 uint64_t coredump_size;
986 int r;
803a3464 987
3c171f0b
LP
988 assert(context);
989 assert(input_fd >= 0);
803a3464 990
78f043f7 991 /* If we are pid1 or journald, we cut things short, don't write to the journal, but still create a coredump. */
34c10968 992
3c171f0b
LP
993 if (arg_storage != COREDUMP_STORAGE_NONE)
994 arg_storage = COREDUMP_STORAGE_EXTERNAL;
34c10968 995
3c171f0b
LP
996 r = save_external_coredump(context, input_fd, &filename, &coredump_node_fd, &coredump_fd, &coredump_size);
997 if (r < 0)
998 return r;
34c10968 999
3c171f0b
LP
1000 r = maybe_remove_external_coredump(filename, coredump_size);
1001 if (r < 0)
1002 return r;
34c10968 1003
78f043f7
LP
1004 log_notice("Detected coredump of the journal daemon or PID 1, diverted to %s.", filename);
1005
3c171f0b
LP
1006 return 0;
1007}
1008
1009static int process_kernel(int argc, char* argv[]) {
1010
1011 /* The small core field we allocate on the stack, to keep things simple */
1012 char
1013 *core_pid = NULL, *core_uid = NULL, *core_gid = NULL, *core_signal = NULL,
1014 *core_session = NULL, *core_exe = NULL, *core_comm = NULL, *core_cmdline = NULL,
1015 *core_cgroup = NULL, *core_cwd = NULL, *core_root = NULL, *core_unit = NULL,
bdfd7b2c 1016 *core_user_unit = NULL, *core_slice = NULL, *core_timestamp = NULL, *core_rlimit = NULL;
3c171f0b
LP
1017
1018 /* The larger ones we allocate on the heap */
1019 _cleanup_free_ char
1020 *core_owner_uid = NULL, *core_open_fds = NULL, *core_proc_status = NULL,
d7032b1f 1021 *core_proc_maps = NULL, *core_proc_limits = NULL, *core_proc_cgroup = NULL, *core_environ = NULL,
7ed03ce6 1022 *core_proc_mountinfo = NULL, *core_container_cmdline = NULL;
3c171f0b
LP
1023
1024 _cleanup_free_ char *exe = NULL, *comm = NULL;
1025 const char *context[_CONTEXT_MAX];
7ed03ce6
JF
1026 bool proc_self_root_is_slash;
1027 struct iovec iovec[27];
3c171f0b
LP
1028 size_t n_iovec = 0;
1029 uid_t owner_uid;
1030 const char *p;
1031 pid_t pid;
1032 char *t;
1033 int r;
1034
1035 if (argc < CONTEXT_COMM + 1) {
1036 log_error("Not enough arguments passed from kernel (%i, expected %i).", argc - 1, CONTEXT_COMM + 1 - 1);
1037 return -EINVAL;
1038 }
1039
1040 r = parse_pid(argv[CONTEXT_PID + 1], &pid);
1041 if (r < 0)
1042 return log_error_errno(r, "Failed to parse PID.");
1043
1044 r = get_process_comm(pid, &comm);
1045 if (r < 0) {
1046 log_warning_errno(r, "Failed to get COMM, falling back to the command line: %m");
1047 comm = strv_join(argv + CONTEXT_COMM + 1, " ");
1048 if (!comm)
1049 return log_oom();
1050 }
1051
1052 r = get_process_exe(pid, &exe);
1053 if (r < 0)
1054 log_warning_errno(r, "Failed to get EXE, ignoring: %m");
1055
1056 context[CONTEXT_PID] = argv[CONTEXT_PID + 1];
1057 context[CONTEXT_UID] = argv[CONTEXT_UID + 1];
1058 context[CONTEXT_GID] = argv[CONTEXT_GID + 1];
1059 context[CONTEXT_SIGNAL] = argv[CONTEXT_SIGNAL + 1];
1060 context[CONTEXT_TIMESTAMP] = argv[CONTEXT_TIMESTAMP + 1];
bdfd7b2c 1061 context[CONTEXT_RLIMIT] = argv[CONTEXT_RLIMIT + 1];
3c171f0b
LP
1062 context[CONTEXT_COMM] = comm;
1063 context[CONTEXT_EXE] = exe;
1064
1065 if (cg_pid_get_unit(pid, &t) >= 0) {
1066
c8091d92
LP
1067 /* If this is PID 1 disable coredump collection, we'll unlikely be able to process it later on. */
1068 if (streq(t, SPECIAL_INIT_SCOPE)) {
1069 log_notice("Due to PID 1 having crashed coredump collection will now be turned off.");
1070 (void) write_string_file("/proc/sys/kernel/core_pattern", "|/bin/false", 0);
1071 }
1072
78f043f7
LP
1073 /* Let's avoid dead-locks when processing journald and init crashes, as socket activation and logging
1074 * are unlikely to work then. */
1075 if (STR_IN_SET(t, SPECIAL_JOURNALD_SERVICE, SPECIAL_INIT_SCOPE)) {
3c171f0b 1076 free(t);
78f043f7 1077 return process_special_crash(context, STDIN_FILENO);
803a3464
LP
1078 }
1079
63c372cb 1080 core_unit = strjoina("COREDUMP_UNIT=", t);
8c8549db
LP
1081 free(t);
1082
3c171f0b 1083 IOVEC_SET_STRING(iovec[n_iovec++], core_unit);
8c8549db 1084 }
803a3464 1085
3c171f0b 1086 /* OK, now we know it's not the journal, hence we can make use of it now. */
803a3464
LP
1087 log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
1088 log_open();
1089
3c171f0b
LP
1090 if (cg_pid_get_user_unit(pid, &t) >= 0) {
1091 core_user_unit = strjoina("COREDUMP_USER_UNIT=", t);
1092 free(t);
1093
1094 IOVEC_SET_STRING(iovec[n_iovec++], core_user_unit);
1095 }
1096
1097 core_pid = strjoina("COREDUMP_PID=", context[CONTEXT_PID]);
1098 IOVEC_SET_STRING(iovec[n_iovec++], core_pid);
f5e04665 1099
3c171f0b
LP
1100 core_uid = strjoina("COREDUMP_UID=", context[CONTEXT_UID]);
1101 IOVEC_SET_STRING(iovec[n_iovec++], core_uid);
f5e04665 1102
3c171f0b
LP
1103 core_gid = strjoina("COREDUMP_GID=", context[CONTEXT_GID]);
1104 IOVEC_SET_STRING(iovec[n_iovec++], core_gid);
f5e04665 1105
3c171f0b
LP
1106 core_signal = strjoina("COREDUMP_SIGNAL=", context[CONTEXT_SIGNAL]);
1107 IOVEC_SET_STRING(iovec[n_iovec++], core_signal);
f5e04665 1108
bdfd7b2c
LP
1109 core_rlimit = strjoina("COREDUMP_RLIMIT=", context[CONTEXT_RLIMIT]);
1110 IOVEC_SET_STRING(iovec[n_iovec++], core_rlimit);
1111
f5e04665 1112 if (sd_pid_get_session(pid, &t) >= 0) {
63c372cb 1113 core_session = strjoina("COREDUMP_SESSION=", t);
f5e04665
LP
1114 free(t);
1115
3c171f0b 1116 IOVEC_SET_STRING(iovec[n_iovec++], core_session);
f5e04665
LP
1117 }
1118
a035f819 1119 if (sd_pid_get_owner_uid(pid, &owner_uid) >= 0) {
3c171f0b 1120 r = asprintf(&core_owner_uid, "COREDUMP_OWNER_UID=" UID_FMT, owner_uid);
7de80bfe 1121 if (r > 0)
3c171f0b 1122 IOVEC_SET_STRING(iovec[n_iovec++], core_owner_uid);
a035f819
LP
1123 }
1124
1125 if (sd_pid_get_slice(pid, &t) >= 0) {
63c372cb 1126 core_slice = strjoina("COREDUMP_SLICE=", t);
a035f819
LP
1127 free(t);
1128
3c171f0b 1129 IOVEC_SET_STRING(iovec[n_iovec++], core_slice);
a035f819
LP
1130 }
1131
1eef15b1 1132 if (comm) {
63c372cb 1133 core_comm = strjoina("COREDUMP_COMM=", comm);
3c171f0b 1134 IOVEC_SET_STRING(iovec[n_iovec++], core_comm);
1eef15b1
ZJS
1135 }
1136
1137 if (exe) {
63c372cb 1138 core_exe = strjoina("COREDUMP_EXE=", exe);
3c171f0b 1139 IOVEC_SET_STRING(iovec[n_iovec++], core_exe);
f5e04665
LP
1140 }
1141
9bdbc2e2 1142 if (get_process_cmdline(pid, 0, false, &t) >= 0) {
63c372cb 1143 core_cmdline = strjoina("COREDUMP_CMDLINE=", t);
f5e04665
LP
1144 free(t);
1145
3c171f0b 1146 IOVEC_SET_STRING(iovec[n_iovec++], core_cmdline);
f5e04665
LP
1147 }
1148
a035f819 1149 if (cg_pid_get_path_shifted(pid, NULL, &t) >= 0) {
63c372cb 1150 core_cgroup = strjoina("COREDUMP_CGROUP=", t);
a035f819
LP
1151 free(t);
1152
3c171f0b 1153 IOVEC_SET_STRING(iovec[n_iovec++], core_cgroup);
a035f819
LP
1154 }
1155
3f132692
JF
1156 if (compose_open_fds(pid, &t) >= 0) {
1157 core_open_fds = strappend("COREDUMP_OPEN_FDS=", t);
1158 free(t);
1159
1160 if (core_open_fds)
3c171f0b 1161 IOVEC_SET_STRING(iovec[n_iovec++], core_open_fds);
3f132692
JF
1162 }
1163
1164 p = procfs_file_alloca(pid, "status");
1165 if (read_full_file(p, &t, NULL) >= 0) {
1166 core_proc_status = strappend("COREDUMP_PROC_STATUS=", t);
1167 free(t);
1168
1169 if (core_proc_status)
3c171f0b 1170 IOVEC_SET_STRING(iovec[n_iovec++], core_proc_status);
3f132692
JF
1171 }
1172
1173 p = procfs_file_alloca(pid, "maps");
1174 if (read_full_file(p, &t, NULL) >= 0) {
1175 core_proc_maps = strappend("COREDUMP_PROC_MAPS=", t);
1176 free(t);
1177
1178 if (core_proc_maps)
3c171f0b 1179 IOVEC_SET_STRING(iovec[n_iovec++], core_proc_maps);
3f132692
JF
1180 }
1181
1182 p = procfs_file_alloca(pid, "limits");
1183 if (read_full_file(p, &t, NULL) >= 0) {
1184 core_proc_limits = strappend("COREDUMP_PROC_LIMITS=", t);
1185 free(t);
1186
1187 if (core_proc_limits)
3c171f0b 1188 IOVEC_SET_STRING(iovec[n_iovec++], core_proc_limits);
3f132692
JF
1189 }
1190
1191 p = procfs_file_alloca(pid, "cgroup");
1192 if (read_full_file(p, &t, NULL) >=0) {
1193 core_proc_cgroup = strappend("COREDUMP_PROC_CGROUP=", t);
1194 free(t);
1195
1196 if (core_proc_cgroup)
3c171f0b 1197 IOVEC_SET_STRING(iovec[n_iovec++], core_proc_cgroup);
3f132692
JF
1198 }
1199
d7032b1f
JF
1200 p = procfs_file_alloca(pid, "mountinfo");
1201 if (read_full_file(p, &t, NULL) >=0) {
1202 core_proc_mountinfo = strappend("COREDUMP_PROC_MOUNTINFO=", t);
1203 free(t);
1204
1205 if (core_proc_mountinfo)
1206 IOVEC_SET_STRING(iovec[n_iovec++], core_proc_mountinfo);
1207 }
1208
3f132692 1209 if (get_process_cwd(pid, &t) >= 0) {
63c372cb 1210 core_cwd = strjoina("COREDUMP_CWD=", t);
3f132692
JF
1211 free(t);
1212
3c171f0b 1213 IOVEC_SET_STRING(iovec[n_iovec++], core_cwd);
3f132692
JF
1214 }
1215
1216 if (get_process_root(pid, &t) >= 0) {
63c372cb 1217 core_root = strjoina("COREDUMP_ROOT=", t);
3f132692 1218
3c171f0b 1219 IOVEC_SET_STRING(iovec[n_iovec++], core_root);
7ed03ce6
JF
1220
1221 /* If the process' root is "/", then there is a chance it has
1222 * mounted own root and hence being containerized. */
1223 proc_self_root_is_slash = strcmp(t, "/") == 0;
1224 free(t);
1225 if (proc_self_root_is_slash && get_process_container_parent_cmdline(pid, &t) > 0) {
1226 core_container_cmdline = strappend("COREDUMP_CONTAINER_CMDLINE=", t);
1227 free(t);
1228
1229 if (core_container_cmdline)
1230 IOVEC_SET_STRING(iovec[n_iovec++], core_container_cmdline);
1231 }
3f132692
JF
1232 }
1233
1234 if (get_process_environ(pid, &t) >= 0) {
1235 core_environ = strappend("COREDUMP_ENVIRON=", t);
1236 free(t);
1237
1238 if (core_environ)
3c171f0b 1239 IOVEC_SET_STRING(iovec[n_iovec++], core_environ);
3f132692
JF
1240 }
1241
81d62103 1242 core_timestamp = strjoina("COREDUMP_TIMESTAMP=", context[CONTEXT_TIMESTAMP], "000000");
3c171f0b 1243 IOVEC_SET_STRING(iovec[n_iovec++], core_timestamp);
f5e04665 1244
3c171f0b 1245 IOVEC_SET_STRING(iovec[n_iovec++], "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1");
4850d39a
LP
1246
1247 assert_cc(2 == LOG_CRIT);
3c171f0b 1248 IOVEC_SET_STRING(iovec[n_iovec++], "PRIORITY=2");
0dc5d23c 1249
3c171f0b 1250 assert(n_iovec <= ELEMENTSOF(iovec));
34c10968 1251
3c171f0b
LP
1252 return send_iovec(iovec, n_iovec, STDIN_FILENO);
1253}
34c10968 1254
3c171f0b
LP
1255int main(int argc, char *argv[]) {
1256 int r;
fee80f69 1257
3c171f0b
LP
1258 /* First, log to a safe place, since we don't know what crashed and it might be journald which we'd rather not
1259 * log to then. */
8d4e028f 1260
3c171f0b
LP
1261 log_set_target(LOG_TARGET_KMSG);
1262 log_open();
8d4e028f 1263
3c171f0b
LP
1264 /* Make sure we never enter a loop */
1265 (void) prctl(PR_SET_DUMPABLE, 0);
8d4e028f 1266
3c171f0b
LP
1267 /* Ignore all parse errors */
1268 (void) parse_config();
fee80f69 1269
3c171f0b
LP
1270 log_debug("Selected storage '%s'.", coredump_storage_to_string(arg_storage));
1271 log_debug("Selected compression %s.", yes_no(arg_compress));
fee80f69 1272
3c171f0b
LP
1273 r = sd_listen_fds(false);
1274 if (r < 0) {
1275 log_error_errno(r, "Failed to determine number of file descriptor: %m");
1276 goto finish;
fee80f69
LP
1277 }
1278
3c171f0b
LP
1279 /* If we got an fd passed, we are running in coredumpd mode. Otherwise we are invoked from the kernel as
1280 * coredump handler */
1281 if (r == 0)
1282 r = process_kernel(argc, argv);
1283 else if (r == 1)
1284 r = process_socket(SD_LISTEN_FDS_START);
1285 else {
1286 log_error("Received unexpected number of file descriptors.");
1287 r = -EINVAL;
1288 }
f5e04665
LP
1289
1290finish:
f5e04665
LP
1291 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1292}