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