]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/coredump/coredumpctl.c
meson: build tests with -Wno-maybe-uninitialized if -O2/-flto are used
[thirdparty/systemd.git] / src / coredump / coredumpctl.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
5de0409e 2
3f6fd1ba
LP
3#include <fcntl.h>
4#include <getopt.h>
a9cdc94f 5#include <locale.h>
5de0409e 6#include <stdio.h>
ada45c78 7#include <unistd.h>
5de0409e 8
012f2b7d 9#include "sd-bus.h"
2cf4172a 10#include "sd-journal.h"
2b044526 11#include "sd-messages.h"
3f6fd1ba 12
b5efdb8a 13#include "alloc-util.h"
012f2b7d
ZJS
14#include "bus-error.h"
15#include "bus-util.h"
3f6fd1ba 16#include "compress.h"
1abaf488 17#include "def.h"
3ffd4af2 18#include "fd-util.h"
0689cfdb 19#include "format-table.h"
992e8f22 20#include "fs-util.h"
aeb56450 21#include "glob-util.h"
3f6fd1ba 22#include "journal-internal.h"
b9aaa7f4 23#include "journal-util.h"
5de0409e 24#include "log.h"
763c7aa2 25#include "macro.h"
c118b577 26#include "main-func.h"
3f6fd1ba 27#include "pager.h"
b1e8f46c 28#include "parse-argument.h"
6bedfcbb 29#include "parse-util.h"
3f6fd1ba 30#include "path-util.h"
294bf0c3 31#include "pretty-print.h"
0b452006 32#include "process-util.h"
1abaf488 33#include "rlimit-util.h"
3f6fd1ba 34#include "sigbus.h"
24882e06 35#include "signal-util.h"
07630cea 36#include "string-util.h"
5ab9ed07 37#include "strv.h"
3f6fd1ba 38#include "terminal-util.h"
e4de7287 39#include "tmpfile-util.h"
b1d4f8e1 40#include "user-util.h"
6bedfcbb 41#include "util.h"
5ce97d33 42#include "verbs.h"
5de0409e 43
501551e8
LP
44#define SHORT_BUS_CALL_TIMEOUT_USEC (3 * USEC_PER_SEC)
45
32485d09 46static usec_t arg_since = USEC_INFINITY, arg_until = USEC_INFINITY;
e15758cc 47static const char* arg_field = NULL;
c5896b6a 48static const char *arg_debugger = NULL;
a2be8be2 49static char **arg_debugger_args = NULL;
b73e9a02 50static const char *arg_directory = NULL;
aeb56450 51static char **arg_file = NULL;
0689cfdb 52static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF;
0221d68a 53static PagerFlags arg_pager_flags = 0;
0689cfdb
LP
54static int arg_legend = true;
55static size_t arg_rows_max = SIZE_MAX;
158ecef5 56static const char* arg_output = NULL;
df65f77b 57static bool arg_reverse = false;
b9aaa7f4 58static bool arg_quiet = false;
5de0409e 59
a2be8be2 60STATIC_DESTRUCTOR_REGISTER(arg_debugger_args, strv_freep);
aeb56450
FS
61STATIC_DESTRUCTOR_REGISTER(arg_file, strv_freep);
62
5ab9ed07 63static int add_match(sd_journal *j, const char *match) {
7fd1b19b 64 _cleanup_free_ char *p = NULL;
43bfe750 65 const char* prefix, *pattern;
0f474365
LP
66 pid_t pid;
67 int r;
5de0409e
ZJS
68
69 if (strchr(match, '='))
70 prefix = "";
71 else if (strchr(match, '/')) {
0f474365
LP
72 r = path_make_absolute_cwd(match, &p);
73 if (r < 0)
5ab9ed07
ZJS
74 return log_error_errno(r, "path_make_absolute_cwd(\"%s\"): %m", match);
75
5de0409e
ZJS
76 match = p;
77 prefix = "COREDUMP_EXE=";
0f474365 78 } else if (parse_pid(match, &pid) >= 0)
5de0409e
ZJS
79 prefix = "COREDUMP_PID=";
80 else
81 prefix = "COREDUMP_COMM=";
82
e8fb0238 83 pattern = strjoina(prefix, match);
5ab9ed07
ZJS
84 log_debug("Adding match: %s", pattern);
85 r = sd_journal_add_match(j, pattern, 0);
86 if (r < 0)
87 return log_error_errno(r, "Failed to add match \"%s\": %m", match);
43bfe750 88
5ab9ed07
ZJS
89 return 0;
90}
91
5ce97d33 92static int add_matches(sd_journal *j, char **matches) {
5ab9ed07
ZJS
93 char **match;
94 int r;
5de0409e 95
2b044526 96 r = sd_journal_add_match(j, "MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR, 0);
0f474365 97 if (r < 0)
2b044526 98 return log_error_errno(r, "Failed to add match \"%s\": %m", "MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR);
5ab9ed07 99
a7581ff9
ZJS
100 r = sd_journal_add_match(j, "MESSAGE_ID=" SD_MESSAGE_BACKTRACE_STR, 0);
101 if (r < 0)
102 return log_error_errno(r, "Failed to add match \"%s\": %m", "MESSAGE_ID=" SD_MESSAGE_BACKTRACE_STR);
103
5ce97d33 104 STRV_FOREACH(match, matches) {
5ab9ed07
ZJS
105 r = add_match(j, *match);
106 if (r < 0)
107 return r;
108 }
5de0409e
ZJS
109
110 return 0;
5de0409e
ZJS
111}
112
5ce97d33
YW
113static int acquire_journal(sd_journal **ret, char **matches) {
114 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
115 int r;
116
117 assert(ret);
118
119 if (arg_directory) {
120 r = sd_journal_open_directory(&j, arg_directory, 0);
121 if (r < 0)
122 return log_error_errno(r, "Failed to open journals in directory: %s: %m", arg_directory);
aeb56450
FS
123 } else if (arg_file) {
124 r = sd_journal_open_files(&j, (const char**)arg_file, 0);
125 if (r < 0)
126 return log_error_errno(r, "Failed to open journal files: %m");
5ce97d33
YW
127 } else {
128 r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
129 if (r < 0)
130 return log_error_errno(r, "Failed to open journal: %m");
131 }
132
133 r = journal_access_check_and_warn(j, arg_quiet, true);
134 if (r < 0)
135 return r;
136
137 r = add_matches(j, matches);
138 if (r < 0)
139 return r;
140
141 if (DEBUG_LOGGING) {
142 _cleanup_free_ char *filter;
143
144 filter = journal_make_match_string(j);
145 log_debug("Journal filter: %s", filter);
146 }
147
1cc6c93a 148 *ret = TAKE_PTR(j);
5ce97d33
YW
149
150 return 0;
151}
152
153static int help(void) {
37ec0fdd
LP
154 _cleanup_free_ char *link = NULL;
155 int r;
156
157 r = terminal_urlify_man("coredumpctl", "1", &link);
158 if (r < 0)
159 return log_oom();
160
0689cfdb
LP
161 printf("%1$s [OPTIONS...] COMMAND ...\n\n"
162 "%5$sList or retrieve coredumps from the journal.%6$s\n"
163 "\n%3$sCommands:%4$s\n"
e1fac8a6
ZJS
164 " list [MATCHES...] List available coredumps (default)\n"
165 " info [MATCHES...] Show detailed information about one or more coredumps\n"
166 " dump [MATCHES...] Print first matching coredump to stdout\n"
167 " debug [MATCHES...] Start a debugger for the first matching coredump\n"
0689cfdb 168 "\n%3$sOptions:%4$s\n"
a2be8be2
MF
169 " -h --help Show this help\n"
170 " --version Print version string\n"
171 " --no-pager Do not pipe output into a pager\n"
172 " --no-legend Do not print the column headers\n"
0689cfdb
LP
173 " --json=pretty|short|off\n"
174 " Generate JSON output\n"
a2be8be2
MF
175 " --debugger=DEBUGGER Use the given debugger\n"
176 " -A --debugger-arguments=ARGS Pass the given arguments to the debugger\n"
0689cfdb 177 " -n INT Show maximum number of rows\n"
a2be8be2
MF
178 " -1 Show information about most recent entry only\n"
179 " -S --since=DATE Only print coredumps since the date\n"
180 " -U --until=DATE Only print coredumps until the date\n"
181 " -r --reverse Show the newest entries first\n"
182 " -F --field=FIELD List all values a certain field takes\n"
183 " -o --output=FILE Write output to FILE\n"
184 " --file=PATH Use journal file\n"
185 " -D --directory=DIR Use journal files from directory\n\n"
186 " -q --quiet Do not show info messages and privilege warning\n"
0689cfdb 187 "\nSee the %2$s for details.\n",
bc556335 188 program_invocation_short_name,
0689cfdb
LP
189 link,
190 ansi_underline(),
bc556335 191 ansi_normal(),
0689cfdb
LP
192 ansi_highlight(),
193 ansi_normal());
5ce97d33
YW
194
195 return 0;
601185b4
ZJS
196}
197
5ab9ed07 198static int parse_argv(int argc, char *argv[]) {
5de0409e
ZJS
199 enum {
200 ARG_VERSION = 0x100,
201 ARG_NO_PAGER,
9a340880 202 ARG_NO_LEGEND,
0689cfdb 203 ARG_JSON,
c5896b6a 204 ARG_DEBUGGER,
aeb56450 205 ARG_FILE,
5de0409e
ZJS
206 };
207
32485d09 208 int c, r;
5de0409e
ZJS
209
210 static const struct option options[] = {
a2be8be2
MF
211 { "help", no_argument, NULL, 'h' },
212 { "version" , no_argument, NULL, ARG_VERSION },
213 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
214 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
215 { "debugger", required_argument, NULL, ARG_DEBUGGER },
216 { "debugger-arguments", required_argument, NULL, 'A' },
217 { "output", required_argument, NULL, 'o' },
218 { "field", required_argument, NULL, 'F' },
219 { "file", required_argument, NULL, ARG_FILE },
220 { "directory", required_argument, NULL, 'D' },
221 { "reverse", no_argument, NULL, 'r' },
222 { "since", required_argument, NULL, 'S' },
223 { "until", required_argument, NULL, 'U' },
224 { "quiet", no_argument, NULL, 'q' },
0689cfdb 225 { "json", required_argument, NULL, ARG_JSON },
eb9da376 226 {}
5de0409e
ZJS
227 };
228
229 assert(argc >= 0);
230 assert(argv);
231
0689cfdb 232 while ((c = getopt_long(argc, argv, "hA:o:F:1D:rS:U:qn:", options, NULL)) >= 0)
5de0409e
ZJS
233 switch(c) {
234 case 'h':
5ce97d33 235 return help();
5de0409e
ZJS
236
237 case ARG_VERSION:
3f6fd1ba 238 return version();
5de0409e
ZJS
239
240 case ARG_NO_PAGER:
0221d68a 241 arg_pager_flags |= PAGER_DISABLE;
5de0409e
ZJS
242 break;
243
9a340880 244 case ARG_NO_LEGEND:
0689cfdb 245 arg_legend = false;
9a340880
ZJS
246 break;
247
c5896b6a
RG
248 case ARG_DEBUGGER:
249 arg_debugger = optarg;
250 break;
251
a2be8be2
MF
252 case 'A': {
253 _cleanup_strv_free_ char **l = NULL;
254 r = strv_split_full(&l, optarg, WHITESPACE, EXTRACT_UNQUOTE);
255 if (r < 0)
256 return log_error_errno(r, "Failed to parse debugger arguments '%s': %m", optarg);
257 strv_free_and_replace(arg_debugger_args, l);
258 break;
259 }
260
aeb56450 261 case ARG_FILE:
544e146b 262 r = glob_extend(&arg_file, optarg, GLOB_NOCHECK);
aeb56450
FS
263 if (r < 0)
264 return log_error_errno(r, "Failed to add paths: %m");
265 break;
266
5de0409e 267 case 'o':
baaa35ad
ZJS
268 if (arg_output)
269 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
270 "Cannot set output more than once.");
5de0409e 271
158ecef5 272 arg_output = optarg;
5de0409e 273 break;
57ce4bd4 274
32485d09
GS
275 case 'S':
276 r = parse_timestamp(optarg, &arg_since);
277 if (r < 0)
9d77ca39 278 return log_error_errno(r, "Failed to parse timestamp '%s': %m", optarg);
32485d09
GS
279 break;
280
281 case 'U':
282 r = parse_timestamp(optarg, &arg_until);
283 if (r < 0)
9d77ca39 284 return log_error_errno(r, "Failed to parse timestamp '%s': %m", optarg);
32485d09
GS
285 break;
286
4f76ae1b 287 case 'F':
baaa35ad
ZJS
288 if (arg_field)
289 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
290 "Cannot use --field/-F more than once.");
a276ae74 291 arg_field = optarg;
4f76ae1b
ZJS
292 break;
293
0c51aada 294 case '1':
0689cfdb
LP
295 arg_rows_max = 1;
296 arg_reverse = true;
0c51aada
LP
297 break;
298
0689cfdb
LP
299 case 'n': {
300 unsigned n;
301
302 r = safe_atou(optarg, &n);
303 if (r < 0 || n < 1)
304 return log_error_errno(r < 0 ? r : SYNTHETIC_ERRNO(EINVAL),
305 "Invalid numeric parameter to -n: %s", optarg);
306
307 arg_rows_max = n;
308 break;
309 }
310
b73e9a02
SW
311 case 'D':
312 arg_directory = optarg;
313 break;
314
df65f77b
NK
315 case 'r':
316 arg_reverse = true;
317 break;
318
b9aaa7f4
ZJS
319 case 'q':
320 arg_quiet = true;
321 break;
322
0689cfdb 323 case ARG_JSON:
b1e8f46c 324 r = parse_json_argument(optarg, &arg_json_format_flags);
0689cfdb
LP
325 if (r <= 0)
326 return r;
327
328 break;
329
57ce4bd4
ZJS
330 case '?':
331 return -EINVAL;
332
5de0409e 333 default:
eb9da376 334 assert_not_reached("Unhandled option");
5de0409e
ZJS
335 }
336
32485d09 337 if (arg_since != USEC_INFINITY && arg_until != USEC_INFINITY &&
baaa35ad
ZJS
338 arg_since > arg_until)
339 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
340 "--since= must be before --until=.");
32485d09 341
5ce97d33 342 return 1;
5de0409e
ZJS
343}
344
8bc8ab83
LP
345static int retrieve(const void *data,
346 size_t len,
347 const char *name,
a276ae74 348 char **var) {
5de0409e 349
4f76ae1b 350 size_t ident;
a276ae74 351 char *v;
8bc8ab83 352
4f76ae1b 353 ident = strlen(name) + 1; /* name + "=" */
8bc8ab83 354
4f76ae1b 355 if (len < ident)
8bc8ab83 356 return 0;
5de0409e 357
4f76ae1b 358 if (memcmp(data, name, ident - 1) != 0)
8bc8ab83
LP
359 return 0;
360
4f76ae1b 361 if (((const char*) data)[ident - 1] != '=')
8bc8ab83 362 return 0;
5de0409e 363
a276ae74
LP
364 v = strndup((const char*)data + ident, len - ident);
365 if (!v)
5de0409e
ZJS
366 return log_oom();
367
eba048bb 368 free_and_replace(*var, v);
062b99e8 369 return 1;
5de0409e
ZJS
370}
371
062b99e8 372static int print_field(FILE* file, sd_journal *j) {
4f76ae1b
ZJS
373 const void *d;
374 size_t l;
375
e15758cc
LP
376 assert(file);
377 assert(j);
378
a276ae74 379 assert(arg_field);
4f76ae1b 380
062b99e8 381 /* A (user-specified) field may appear more than once for a given entry.
5238e957 382 * We will print all of the occurrences.
062b99e8
ZJS
383 * This is different below for fields that systemd-coredump uses,
384 * because they cannot meaningfully appear more than once.
385 */
386 SD_JOURNAL_FOREACH_DATA(j, d, l) {
387 _cleanup_free_ char *value = NULL;
388 int r;
389
390 r = retrieve(d, l, arg_field, &value);
391 if (r < 0)
392 return r;
393 if (r > 0)
394 fprintf(file, "%s\n", value);
395 }
a276ae74 396
062b99e8 397 return 0;
4f76ae1b
ZJS
398}
399
062b99e8
ZJS
400#define RETRIEVE(d, l, name, arg) \
401 { \
402 int _r = retrieve(d, l, name, &arg); \
403 if (_r < 0) \
404 return _r; \
405 if (_r > 0) \
406 continue; \
407 }
408
4016281c
LP
409static void analyze_coredump_file(
410 const char *path,
411 const char **ret_state,
412 const char **ret_color,
413 uint64_t *ret_size) {
414
415 _cleanup_close_ int fd = -1;
416 struct stat st;
417 int r;
418
419 assert(path);
420 assert(ret_state);
421 assert(ret_color);
422 assert(ret_size);
423
424 fd = open(path, O_PATH|O_CLOEXEC);
425 if (fd < 0) {
426 if (errno == ENOENT) {
427 *ret_state = "missing";
428 *ret_color = ansi_grey();
429 *ret_size = UINT64_MAX;
430 return;
431 }
432
433 r = -errno;
434 } else
435 r = access_fd(fd, R_OK);
436 if (ERRNO_IS_PRIVILEGE(r)) {
437 *ret_state = "inaccessible";
438 *ret_color = ansi_highlight_yellow();
439 *ret_size = UINT64_MAX;
440 return;
441 }
442 if (r < 0)
443 goto error;
444
445 if (fstat(fd, &st) < 0)
446 goto error;
447
448 if (!S_ISREG(st.st_mode))
449 goto error;
450
451 *ret_state = "present";
452 *ret_color = NULL;
453 *ret_size = st.st_size;
454 return;
455
456error:
457 *ret_state = "error";
458 *ret_color = ansi_highlight_red();
459 *ret_size = UINT64_MAX;
460}
461
0689cfdb 462static int print_list(FILE* file, sd_journal *j, Table *t) {
a276ae74 463 _cleanup_free_ char
a7581ff9 464 *mid = NULL, *pid = NULL, *uid = NULL, *gid = NULL,
9fe13294 465 *sgnl = NULL, *exe = NULL, *comm = NULL, *cmdline = NULL,
cc4419ed 466 *filename = NULL, *truncated = NULL, *coredump = NULL;
8bc8ab83
LP
467 const void *d;
468 size_t l;
0689cfdb
LP
469 usec_t ts;
470 int r, signal_as_int = 0;
4016281c
LP
471 const char *present = NULL, *color = NULL;
472 uint64_t size = UINT64_MAX;
a7581ff9 473 bool normal_coredump;
0689cfdb
LP
474 uid_t uid_as_int = UID_INVALID;
475 gid_t gid_as_int = GID_INVALID;
476 pid_t pid_as_int = 0;
8bc8ab83 477
e15758cc
LP
478 assert(file);
479 assert(j);
0689cfdb 480 assert(t);
e15758cc 481
8bc8ab83 482 SD_JOURNAL_FOREACH_DATA(j, d, l) {
a7581ff9 483 RETRIEVE(d, l, "MESSAGE_ID", mid);
062b99e8
ZJS
484 RETRIEVE(d, l, "COREDUMP_PID", pid);
485 RETRIEVE(d, l, "COREDUMP_UID", uid);
486 RETRIEVE(d, l, "COREDUMP_GID", gid);
487 RETRIEVE(d, l, "COREDUMP_SIGNAL", sgnl);
488 RETRIEVE(d, l, "COREDUMP_EXE", exe);
489 RETRIEVE(d, l, "COREDUMP_COMM", comm);
490 RETRIEVE(d, l, "COREDUMP_CMDLINE", cmdline);
491 RETRIEVE(d, l, "COREDUMP_FILENAME", filename);
cc4419ed 492 RETRIEVE(d, l, "COREDUMP_TRUNCATED", truncated);
062b99e8 493 RETRIEVE(d, l, "COREDUMP", coredump);
8bc8ab83 494 }
5de0409e 495
0689cfdb
LP
496 if (!pid && !uid && !gid && !sgnl && !exe && !comm && !cmdline && !filename)
497 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), "Empty coredump log entry");
684341b0 498
0689cfdb
LP
499 (void) parse_uid(uid, &uid_as_int);
500 (void) parse_gid(gid, &gid_as_int);
501 (void) parse_pid(pid, &pid_as_int);
502 signal_as_int = signal_from_string(sgnl);
503
504 r = sd_journal_get_realtime_usec(j, &ts);
23bbb0de
MS
505 if (r < 0)
506 return log_error_errno(r, "Failed to get realtime timestamp: %m");
5de0409e 507
a7581ff9
ZJS
508 normal_coredump = streq_ptr(mid, SD_MESSAGE_COREDUMP_STR);
509
04de5879 510 if (filename)
4016281c 511 analyze_coredump_file(filename, &present, &color, &size);
04de5879
ZJS
512 else if (coredump)
513 present = "journal";
0689cfdb 514 else if (normal_coredump) {
04de5879 515 present = "none";
0689cfdb
LP
516 color = ansi_grey();
517 } else
518 present = NULL;
04de5879 519
0689cfdb 520 if (STRPTR_IN_SET(present, "present", "journal") && truncated && parse_boolean(truncated) > 0)
cc4419ed
ZJS
521 present = "truncated";
522
0689cfdb
LP
523 r = table_add_many(
524 t,
525 TABLE_TIMESTAMP, ts,
526 TABLE_PID, pid_as_int,
527 TABLE_UID, uid_as_int,
528 TABLE_GID, gid_as_int,
529 TABLE_SIGNAL, normal_coredump ? signal_as_int : 0,
530 TABLE_STRING, present,
531 TABLE_SET_COLOR, color,
4016281c
LP
532 TABLE_STRING, exe ?: comm ?: cmdline,
533 TABLE_SIZE, size);
0689cfdb
LP
534 if (r < 0)
535 return table_log_add_error(r);
684341b0
LP
536
537 return 0;
5de0409e
ZJS
538}
539
e15758cc
LP
540static int print_info(FILE *file, sd_journal *j, bool need_space) {
541 _cleanup_free_ char
a7581ff9 542 *mid = NULL, *pid = NULL, *uid = NULL, *gid = NULL,
e15758cc
LP
543 *sgnl = NULL, *exe = NULL, *comm = NULL, *cmdline = NULL,
544 *unit = NULL, *user_unit = NULL, *session = NULL,
a035f819 545 *boot_id = NULL, *machine_id = NULL, *hostname = NULL,
9fe13294 546 *slice = NULL, *cgroup = NULL, *owner_uid = NULL,
47f50642 547 *message = NULL, *timestamp = NULL, *filename = NULL,
d1b5a0c6
LB
548 *truncated = NULL, *coredump = NULL,
549 *pkgmeta_name = NULL, *pkgmeta_version = NULL, *pkgmeta_json = NULL;
e15758cc
LP
550 const void *d;
551 size_t l;
a7581ff9 552 bool normal_coredump;
4b8cbe9a 553 int r;
e15758cc
LP
554
555 assert(file);
556 assert(j);
557
558 SD_JOURNAL_FOREACH_DATA(j, d, l) {
a7581ff9 559 RETRIEVE(d, l, "MESSAGE_ID", mid);
062b99e8
ZJS
560 RETRIEVE(d, l, "COREDUMP_PID", pid);
561 RETRIEVE(d, l, "COREDUMP_UID", uid);
562 RETRIEVE(d, l, "COREDUMP_GID", gid);
563 RETRIEVE(d, l, "COREDUMP_SIGNAL", sgnl);
564 RETRIEVE(d, l, "COREDUMP_EXE", exe);
565 RETRIEVE(d, l, "COREDUMP_COMM", comm);
566 RETRIEVE(d, l, "COREDUMP_CMDLINE", cmdline);
07461598 567 RETRIEVE(d, l, "COREDUMP_HOSTNAME", hostname);
062b99e8
ZJS
568 RETRIEVE(d, l, "COREDUMP_UNIT", unit);
569 RETRIEVE(d, l, "COREDUMP_USER_UNIT", user_unit);
570 RETRIEVE(d, l, "COREDUMP_SESSION", session);
571 RETRIEVE(d, l, "COREDUMP_OWNER_UID", owner_uid);
572 RETRIEVE(d, l, "COREDUMP_SLICE", slice);
573 RETRIEVE(d, l, "COREDUMP_CGROUP", cgroup);
574 RETRIEVE(d, l, "COREDUMP_TIMESTAMP", timestamp);
575 RETRIEVE(d, l, "COREDUMP_FILENAME", filename);
cc4419ed 576 RETRIEVE(d, l, "COREDUMP_TRUNCATED", truncated);
062b99e8 577 RETRIEVE(d, l, "COREDUMP", coredump);
671769c9
LB
578 RETRIEVE(d, l, "COREDUMP_PACKAGE_NAME", pkgmeta_name);
579 RETRIEVE(d, l, "COREDUMP_PACKAGE_VERSION", pkgmeta_version);
580 RETRIEVE(d, l, "COREDUMP_PACKAGE_JSON", pkgmeta_json);
062b99e8
ZJS
581 RETRIEVE(d, l, "_BOOT_ID", boot_id);
582 RETRIEVE(d, l, "_MACHINE_ID", machine_id);
062b99e8 583 RETRIEVE(d, l, "MESSAGE", message);
e15758cc
LP
584 }
585
586 if (need_space)
587 fputs("\n", file);
588
a7581ff9
ZJS
589 normal_coredump = streq_ptr(mid, SD_MESSAGE_COREDUMP_STR);
590
81cef14f
LP
591 if (comm)
592 fprintf(file,
593 " PID: %s%s%s (%s)\n",
1fc464f6 594 ansi_highlight(), strna(pid), ansi_normal(), comm);
81cef14f
LP
595 else
596 fprintf(file,
597 " PID: %s%s%s\n",
1fc464f6 598 ansi_highlight(), strna(pid), ansi_normal());
a035f819
LP
599
600 if (uid) {
601 uid_t n;
602
603 if (parse_uid(uid, &n) >= 0) {
604 _cleanup_free_ char *u = NULL;
605
606 u = uid_to_name(n);
607 fprintf(file,
608 " UID: %s (%s)\n",
609 uid, u);
610 } else {
611 fprintf(file,
612 " UID: %s\n",
613 uid);
614 }
615 }
616
617 if (gid) {
618 gid_t n;
619
620 if (parse_gid(gid, &n) >= 0) {
621 _cleanup_free_ char *g = NULL;
622
623 g = gid_to_name(n);
624 fprintf(file,
625 " GID: %s (%s)\n",
626 gid, g);
627 } else {
628 fprintf(file,
629 " GID: %s\n",
630 gid);
631 }
632 }
e15758cc
LP
633
634 if (sgnl) {
635 int sig;
a7581ff9 636 const char *name = normal_coredump ? "Signal" : "Reason";
e15758cc 637
a7581ff9
ZJS
638 if (normal_coredump && safe_atoi(sgnl, &sig) >= 0)
639 fprintf(file, " %s: %s (%s)\n", name, sgnl, signal_to_string(sig));
e15758cc 640 else
a7581ff9 641 fprintf(file, " %s: %s\n", name, sgnl);
e15758cc
LP
642 }
643
4b8cbe9a
LP
644 if (timestamp) {
645 usec_t u;
646
647 r = safe_atou64(timestamp, &u);
648 if (r >= 0) {
649 char absolute[FORMAT_TIMESTAMP_MAX], relative[FORMAT_TIMESPAN_MAX];
650
651 fprintf(file,
652 " Timestamp: %s (%s)\n",
653 format_timestamp(absolute, sizeof(absolute), u),
654 format_timestamp_relative(relative, sizeof(relative), u));
655
656 } else
657 fprintf(file, " Timestamp: %s\n", timestamp);
658 }
659
e15758cc
LP
660 if (cmdline)
661 fprintf(file, " Command Line: %s\n", cmdline);
81cef14f 662 if (exe)
1fc464f6 663 fprintf(file, " Executable: %s%s%s\n", ansi_highlight(), exe, ansi_normal());
a035f819
LP
664 if (cgroup)
665 fprintf(file, " Control Group: %s\n", cgroup);
e15758cc
LP
666 if (unit)
667 fprintf(file, " Unit: %s\n", unit);
668 if (user_unit)
554ed50f 669 fprintf(file, " User Unit: %s\n", user_unit);
a035f819
LP
670 if (slice)
671 fprintf(file, " Slice: %s\n", slice);
e15758cc
LP
672 if (session)
673 fprintf(file, " Session: %s\n", session);
a035f819
LP
674 if (owner_uid) {
675 uid_t n;
676
677 if (parse_uid(owner_uid, &n) >= 0) {
678 _cleanup_free_ char *u = NULL;
679
680 u = uid_to_name(n);
681 fprintf(file,
682 " Owner UID: %s (%s)\n",
683 owner_uid, u);
684 } else {
685 fprintf(file,
686 " Owner UID: %s\n",
687 owner_uid);
688 }
689 }
e15758cc
LP
690 if (boot_id)
691 fprintf(file, " Boot ID: %s\n", boot_id);
692 if (machine_id)
693 fprintf(file, " Machine ID: %s\n", machine_id);
694 if (hostname)
695 fprintf(file, " Hostname: %s\n", hostname);
696
cc4419ed 697 if (filename) {
4016281c
LP
698 const char *state = NULL, *color = NULL;
699 uint64_t size = UINT64_MAX;
700 char buf[FORMAT_BYTES_MAX];
cc4419ed 701
4016281c
LP
702 analyze_coredump_file(filename, &state, &color, &size);
703
704 if (STRPTR_IN_SET(state, "present", "journal") && truncated && parse_boolean(truncated) > 0)
705 state = "truncated";
706
707 fprintf(file,
708 " Storage: %s%s (%s)%s\n",
709 strempty(color),
710 filename,
711 state,
712 ansi_normal());
713
714 if (size != UINT64_MAX)
715 fprintf(file,
716 " Disk Size: %s\n",
717 format_bytes(buf, sizeof(buf), size));
718 } else if (coredump)
47f50642
ZJS
719 fprintf(file, " Storage: journal\n");
720 else
721 fprintf(file, " Storage: none\n");
e15758cc 722
d1b5a0c6
LB
723 if (pkgmeta_name && pkgmeta_version)
724 fprintf(file, " Package: %s/%s\n", pkgmeta_name, pkgmeta_version);
725
726 /* Print out the build-id of the 'main' ELF module, by matching the JSON key
727 * with the 'exe' field. */
728 if (exe && pkgmeta_json) {
729 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
730
731 r = json_parse(pkgmeta_json, 0, &v, NULL, NULL);
9b7205f5 732 if (r < 0)
d1b5a0c6 733 log_warning_errno(r, "json_parse on %s failed, ignoring: %m", pkgmeta_json);
9b7205f5 734 else {
d1b5a0c6
LB
735 const char *module_name;
736 JsonVariant *module_json;
737
b7ddd444 738 JSON_VARIANT_OBJECT_FOREACH(module_name, module_json, v) {
b7ddd444 739 JsonVariant *build_id;
d1b5a0c6 740
d1b5a0c6 741 /* We only print the build-id for the 'main' ELF module */
289784a7 742 if (!path_equal_filename(module_name, exe))
d1b5a0c6
LB
743 continue;
744
b7ddd444
LB
745 build_id = json_variant_by_key(module_json, "buildId");
746 if (build_id)
747 fprintf(file, " build-id: %s\n", json_variant_string(build_id));
d1b5a0c6
LB
748
749 break;
750 }
751 }
752 }
753
8d4e028f
LP
754 if (message) {
755 _cleanup_free_ char *m = NULL;
756
757 m = strreplace(message, "\n", "\n ");
758
759 fprintf(file, " Message: %s\n", strstrip(m ?: message));
760 }
761
e15758cc
LP
762 return 0;
763}
764
ada45c78 765static int focus(sd_journal *j) {
5de0409e
ZJS
766 int r;
767
5de0409e
ZJS
768 r = sd_journal_seek_tail(j);
769 if (r == 0)
770 r = sd_journal_previous(j);
23bbb0de
MS
771 if (r < 0)
772 return log_error_errno(r, "Failed to search journal: %m");
baaa35ad
ZJS
773 if (r == 0)
774 return log_error_errno(SYNTHETIC_ERRNO(ESRCH),
775 "No match found.");
ada45c78
LP
776 return r;
777}
5de0409e 778
0689cfdb
LP
779static int print_entry(
780 sd_journal *j,
781 size_t n_found,
782 Table *t) {
783
0c51aada
LP
784 assert(j);
785
0689cfdb
LP
786 if (t)
787 return print_list(stdout, j, t);
0c51aada 788 else if (arg_field)
062b99e8 789 return print_field(stdout, j);
0c51aada 790 else
0689cfdb 791 return print_info(stdout, j, n_found > 0);
0c51aada
LP
792}
793
5ce97d33
YW
794static int dump_list(int argc, char **argv, void *userdata) {
795 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
0689cfdb
LP
796 _cleanup_(table_unrefp) Table *t = NULL;
797 size_t n_found = 0;
5ce97d33 798 bool verb_is_info;
0c51aada
LP
799 int r;
800
0689cfdb 801 verb_is_info = argc >= 1 && streq(argv[0], "info");
5ce97d33
YW
802
803 r = acquire_journal(&j, argv + 1);
804 if (r < 0)
805 return r;
806
0689cfdb
LP
807 /* The coredumps are likely compressed, and for just listing them we don't need to decompress them,
808 * so let's pick a fairly low data threshold here */
809 (void) sd_journal_set_data_threshold(j, 4096);
0c51aada 810
0689cfdb 811 if (!verb_is_info && !arg_field) {
4016281c 812 t = table_new("time", "pid", "uid", "gid", "sig", "corefile", "exe", "size");
0689cfdb
LP
813 if (!t)
814 return log_oom();
815
816 (void) table_set_align_percent(t, TABLE_HEADER_CELL(1), 100);
817 (void) table_set_align_percent(t, TABLE_HEADER_CELL(2), 100);
818 (void) table_set_align_percent(t, TABLE_HEADER_CELL(3), 100);
4016281c 819 (void) table_set_align_percent(t, TABLE_HEADER_CELL(7), 100);
0689cfdb
LP
820
821 (void) table_set_empty_string(t, "-");
822 } else
823 (void) pager_open(arg_pager_flags);
0c51aada 824
2fafabfd 825 /* "info" without pattern implies "-1" */
0689cfdb 826 if ((arg_rows_max == 1 && arg_reverse) || (verb_is_info && argc == 1)) {
0c51aada
LP
827 r = focus(j);
828 if (r < 0)
829 return r;
830
0689cfdb
LP
831 r = print_entry(j, 0, t);
832 if (r < 0)
833 return r;
0c51aada 834 } else {
32485d09
GS
835 if (arg_since != USEC_INFINITY && !arg_reverse)
836 r = sd_journal_seek_realtime_usec(j, arg_since);
837 else if (arg_until != USEC_INFINITY && arg_reverse)
838 r = sd_journal_seek_realtime_usec(j, arg_until);
839 else if (arg_reverse)
840 r = sd_journal_seek_tail(j);
841 else
842 r = sd_journal_seek_head(j);
843 if (r < 0)
844 return log_error_errno(r, "Failed to seek to date: %m");
845
846 for (;;) {
847 if (!arg_reverse)
848 r = sd_journal_next(j);
849 else
850 r = sd_journal_previous(j);
32485d09
GS
851 if (r < 0)
852 return log_error_errno(r, "Failed to iterate through journal: %m");
32485d09
GS
853 if (r == 0)
854 break;
855
856 if (arg_until != USEC_INFINITY && !arg_reverse) {
857 usec_t usec;
858
859 r = sd_journal_get_realtime_usec(j, &usec);
df65f77b 860 if (r < 0)
32485d09
GS
861 return log_error_errno(r, "Failed to determine timestamp: %m");
862 if (usec > arg_until)
863 continue;
df65f77b 864 }
32485d09
GS
865
866 if (arg_since != USEC_INFINITY && arg_reverse) {
867 usec_t usec;
868
869 r = sd_journal_get_realtime_usec(j, &usec);
df65f77b 870 if (r < 0)
32485d09
GS
871 return log_error_errno(r, "Failed to determine timestamp: %m");
872 if (usec < arg_since)
873 continue;
df65f77b 874 }
32485d09 875
0689cfdb 876 r = print_entry(j, n_found++, t);
32485d09
GS
877 if (r < 0)
878 return r;
0689cfdb
LP
879
880 if (arg_rows_max != SIZE_MAX && n_found >= arg_rows_max)
881 break;
062b99e8 882 }
0c51aada
LP
883
884 if (!arg_field && n_found <= 0) {
b9aaa7f4
ZJS
885 if (!arg_quiet)
886 log_notice("No coredumps found.");
0c51aada
LP
887 return -ESRCH;
888 }
889 }
890
0689cfdb
LP
891 if (t) {
892 r = table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend);
893 if (r < 0)
894 return r;
895 }
896
0c51aada
LP
897 return 0;
898}
899
bb7c5bad 900static int save_core(sd_journal *j, FILE *file, char **path, bool *unlink_temp) {
9fe13294
ZJS
901 const char *data;
902 _cleanup_free_ char *filename = NULL;
903 size_t len;
bb7c5bad 904 int r, fd;
fc6cec86
ZJS
905 _cleanup_close_ int fdt = -1;
906 char *temp = NULL;
ada45c78 907
bb7c5bad
ZJS
908 assert(!(file && path)); /* At most one can be specified */
909 assert(!!path == !!unlink_temp); /* Those must be specified together */
47f50642 910
fc6cec86 911 /* Look for a coredump on disk first. */
9fe13294 912 r = sd_journal_get_data(j, "COREDUMP_FILENAME", (const void**) &data, &len);
2d0a880f
ZJS
913 if (r == 0) {
914 r = retrieve(data, len, "COREDUMP_FILENAME", &filename);
915 if (r < 0)
916 return r;
917 assert(r > 0);
918
919 if (access(filename, R_OK) < 0)
920 return log_error_errno(errno, "File \"%s\" is not readable: %m", filename);
921
ef5924aa 922 if (path && !ENDSWITH_SET(filename, ".xz", ".lz4", ".zst")) {
2d0a880f
ZJS
923 *path = TAKE_PTR(filename);
924
925 return 0;
926 }
927
928 } else {
bb7c5bad
ZJS
929 if (r != -ENOENT)
930 return log_error_errno(r, "Failed to retrieve COREDUMP_FILENAME field: %m");
931 /* Check that we can have a COREDUMP field. We still haven't set a high
932 * data threshold, so we'll get a few kilobytes at most.
933 */
934
935 r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len);
936 if (r == -ENOENT)
937 return log_error_errno(r, "Coredump entry has no core attached (neither internally in the journal nor externally on disk).");
938 if (r < 0)
939 return log_error_errno(r, "Failed to retrieve COREDUMP field: %m");
940 }
93b73b06 941
bb7c5bad 942 if (path) {
fc6cec86 943 const char *vt;
992e8f22 944
fc6cec86 945 /* Create a temporary file to write the uncompressed core to. */
992e8f22 946
fc6cec86
ZJS
947 r = var_tmp_dir(&vt);
948 if (r < 0)
949 return log_error_errno(r, "Failed to acquire temporary directory path: %m");
9fe13294 950
657ee2d8 951 temp = path_join(vt, "coredump-XXXXXX");
fc6cec86
ZJS
952 if (!temp)
953 return log_oom();
9fe13294 954
fc6cec86
ZJS
955 fdt = mkostemp_safe(temp);
956 if (fdt < 0)
957 return log_error_errno(fdt, "Failed to create temporary file: %m");
958 log_debug("Created temporary file %s", temp);
9fe13294 959
fc6cec86 960 fd = fdt;
bb7c5bad
ZJS
961 } else {
962 /* If neither path or file are specified, we will write to stdout. Let's now check
963 * if stdout is connected to a tty. We checked that the file exists, or that the
964 * core might be stored in the journal. In this second case, if we found the entry,
5238e957 965 * in all likelihood we will be able to access the COREDUMP= field. In either case,
bb7c5bad
ZJS
966 * we stop before doing any "real" work, i.e. before starting decompression or
967 * reading from the file or creating temporary files.
968 */
969 if (!file) {
970 if (on_tty())
886cf317
ZJS
971 return log_error_errno(SYNTHETIC_ERRNO(ENOTTY),
972 "Refusing to dump core to tty"
bb7c5bad
ZJS
973 " (use shell redirection or specify --output).");
974 file = stdout;
975 }
976
977 fd = fileno(file);
fc6cec86
ZJS
978 }
979
980 if (filename) {
d80b051c 981#if HAVE_COMPRESSION
fc6cec86 982 _cleanup_close_ int fdf;
9fe13294 983
fc6cec86
ZJS
984 fdf = open(filename, O_RDONLY | O_CLOEXEC);
985 if (fdf < 0) {
986 r = log_error_errno(errno, "Failed to open %s: %m", filename);
2fb8159f 987 goto error;
fc6cec86
ZJS
988 }
989
990 r = decompress_stream(filename, fdf, fd, -1);
991 if (r < 0) {
992 log_error_errno(r, "Failed to decompress %s: %m", filename);
9fe13294
ZJS
993 goto error;
994 }
fc6cec86 995#else
3afe5c00
LP
996 r = log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
997 "Cannot decompress file. Compiled without compression support.");
fc6cec86
ZJS
998 goto error;
999#endif
1000 } else {
1001 ssize_t sz;
a276ae74 1002
bb7c5bad
ZJS
1003 /* We want full data, nothing truncated. */
1004 sd_journal_set_data_threshold(j, 0);
1005
fc6cec86
ZJS
1006 r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len);
1007 if (r < 0)
bb7c5bad 1008 return log_error_errno(r, "Failed to retrieve COREDUMP field: %m");
fc6cec86
ZJS
1009
1010 assert(len >= 9);
1011 data += 9;
1012 len -= 9;
1013
954d3a51 1014 sz = write(fd, data, len);
fc6cec86 1015 if (sz < 0) {
954d3a51 1016 r = log_error_errno(errno, "Failed to write output: %m");
fc6cec86 1017 goto error;
a276ae74 1018 }
fc6cec86 1019 if (sz != (ssize_t) len) {
954d3a51 1020 log_error("Short write to output.");
fc6cec86
ZJS
1021 r = -EIO;
1022 goto error;
1023 }
1024 }
a276ae74 1025
fc6cec86
ZJS
1026 if (temp) {
1027 *path = temp;
1028 *unlink_temp = true;
1029 }
1030 return 0;
9fe13294
ZJS
1031
1032error:
fc6cec86 1033 if (temp) {
6990fb6b 1034 (void) unlink(temp);
fc6cec86 1035 log_debug("Removed temporary file %s", temp);
9fe13294 1036 }
fc6cec86 1037 return r;
9fe13294 1038}
a276ae74 1039
5ce97d33
YW
1040static int dump_core(int argc, char **argv, void *userdata) {
1041 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
158ecef5 1042 _cleanup_fclose_ FILE *f = NULL;
9fe13294
ZJS
1043 int r;
1044
d7a0f1f4
FS
1045 if (arg_field)
1046 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1047 "Option --field/-F only makes sense with list");
5ce97d33
YW
1048
1049 r = acquire_journal(&j, argv + 1);
1050 if (r < 0)
1051 return r;
9fe13294
ZJS
1052
1053 r = focus(j);
1054 if (r < 0)
ada45c78 1055 return r;
ada45c78 1056
158ecef5
ZJS
1057 if (arg_output) {
1058 f = fopen(arg_output, "we");
1059 if (!f)
1060 return log_error_errno(errno, "Failed to open \"%s\" for writing: %m", arg_output);
1061 }
1062
1063 print_info(f ? stdout : stderr, j, false);
9fe13294 1064
158ecef5 1065 r = save_core(j, f, NULL, NULL);
23bbb0de 1066 if (r < 0)
bb7c5bad 1067 return r;
5de0409e
ZJS
1068
1069 r = sd_journal_previous(j);
b9aaa7f4
ZJS
1070 if (r > 0 && !arg_quiet)
1071 log_notice("More than one entry matches, ignoring rest.");
5de0409e
ZJS
1072
1073 return 0;
1074}
1075
c5896b6a 1076static int run_debug(int argc, char **argv, void *userdata) {
5ce97d33 1077 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
a2be8be2
MF
1078 _cleanup_free_ char *exe = NULL, *path = NULL;
1079 _cleanup_strv_free_ char **debugger_call = NULL;
9fe13294 1080 bool unlink_path = false;
c5896b6a 1081 const char *data, *fork_name;
ada45c78 1082 size_t len;
ada45c78 1083 pid_t pid;
ada45c78 1084 int r;
ada45c78 1085
c5896b6a
RG
1086 if (!arg_debugger) {
1087 char *env_debugger;
1088
1089 env_debugger = getenv("SYSTEMD_DEBUGGER");
1090 if (env_debugger)
1091 arg_debugger = env_debugger;
1092 else
1093 arg_debugger = "gdb";
1094 }
1095
a2be8be2
MF
1096 r = strv_extend(&debugger_call, arg_debugger);
1097 if (r < 0)
1098 return log_oom();
1099
1100 r = strv_extend_strv(&debugger_call, arg_debugger_args, false);
1101 if (r < 0)
1102 return log_oom();
3f0d8b2d 1103
d7a0f1f4
FS
1104 if (arg_field)
1105 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1106 "Option --field/-F only makes sense with list");
5ce97d33
YW
1107
1108 r = acquire_journal(&j, argv + 1);
1109 if (r < 0)
1110 return r;
ada45c78
LP
1111
1112 r = focus(j);
1113 if (r < 0)
1114 return r;
1115
a174da59
FS
1116 if (!arg_quiet) {
1117 print_info(stdout, j, false);
1118 fputs("\n", stdout);
1119 }
ada45c78
LP
1120
1121 r = sd_journal_get_data(j, "COREDUMP_EXE", (const void**) &data, &len);
23bbb0de
MS
1122 if (r < 0)
1123 return log_error_errno(r, "Failed to retrieve COREDUMP_EXE field: %m");
ada45c78 1124
fbd0b64f
LP
1125 assert(len > STRLEN("COREDUMP_EXE="));
1126 data += STRLEN("COREDUMP_EXE=");
1127 len -= STRLEN("COREDUMP_EXE=");
ada45c78
LP
1128
1129 exe = strndup(data, len);
1130 if (!exe)
1131 return log_oom();
1132
d7a0f1f4
FS
1133 if (endswith(exe, " (deleted)"))
1134 return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
1135 "Binary already deleted.");
ada45c78 1136
d7a0f1f4
FS
1137 if (!path_is_absolute(exe))
1138 return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
1139 "Binary is not an absolute path.");
a276ae74 1140
bb7c5bad 1141 r = save_core(j, NULL, &path, &unlink_path);
23bbb0de 1142 if (r < 0)
bb7c5bad 1143 return r;
ada45c78 1144
a2be8be2
MF
1145 r = strv_extend_strv(&debugger_call, STRV_MAKE(exe, "-c", path), false);
1146 if (r < 0)
1147 return log_oom();
1148
3e7bc89b 1149 /* Don't interfere with gdb and its handling of SIGINT. */
9c274488 1150 (void) ignore_signals(SIGINT);
3e7bc89b 1151
a2be8be2 1152 fork_name = strjoina("(", debugger_call[0], ")");
c5896b6a 1153
48f813c4 1154 r = safe_fork(fork_name, FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG|FORK_FLUSH_STDIO, &pid);
b6e1fff1 1155 if (r < 0)
ada45c78 1156 goto finish;
4c253ed1 1157 if (r == 0) {
a2be8be2 1158 execvp(debugger_call[0], debugger_call);
0b1f3c76 1159 log_open();
a2be8be2 1160 log_error_errno(errno, "Failed to invoke %s: %m", debugger_call[0]);
a45d7127 1161 _exit(EXIT_FAILURE);
ada45c78
LP
1162 }
1163
a2be8be2 1164 r = wait_for_terminate_and_check(debugger_call[0], pid, WAIT_LOG_ABNORMAL);
ada45c78
LP
1165
1166finish:
9c274488 1167 (void) default_signals(SIGINT);
3e7bc89b 1168
9fe13294
ZJS
1169 if (unlink_path) {
1170 log_debug("Removed temporary file %s", path);
6990fb6b 1171 (void) unlink(path);
9fe13294 1172 }
a276ae74 1173
ada45c78
LP
1174 return r;
1175}
1176
012f2b7d 1177static int check_units_active(void) {
7add4883 1178 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
012f2b7d
ZJS
1179 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1180 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1181 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1182 int c = 0, r;
7bbf2d84 1183 const char *id, *state, *substate;
012f2b7d 1184
b9aaa7f4
ZJS
1185 if (arg_quiet)
1186 return false;
1187
012f2b7d
ZJS
1188 r = sd_bus_default_system(&bus);
1189 if (r < 0)
1190 return log_error_errno(r, "Failed to acquire bus: %m");
1191
1192 r = sd_bus_message_new_method_call(
1193 bus,
1194 &m,
1195 "org.freedesktop.systemd1",
1196 "/org/freedesktop/systemd1",
1197 "org.freedesktop.systemd1.Manager",
1198 "ListUnitsByPatterns");
1199 if (r < 0)
1200 return bus_log_create_error(r);
1201
1202 r = sd_bus_message_append_strv(m, NULL);
1203 if (r < 0)
1204 return bus_log_create_error(r);
1205
1206 r = sd_bus_message_append_strv(m, STRV_MAKE("systemd-coredump@*.service"));
1207 if (r < 0)
1208 return bus_log_create_error(r);
1209
501551e8 1210 r = sd_bus_call(bus, m, SHORT_BUS_CALL_TIMEOUT_USEC, &error, &reply);
012f2b7d
ZJS
1211 if (r < 0)
1212 return log_error_errno(r, "Failed to check if any systemd-coredump@.service units are running: %s",
1213 bus_error_message(&error, r));
1214
1215 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)");
1216 if (r < 0)
1217 return bus_log_parse_error(r);
1218
1219 while ((r = sd_bus_message_read(
1220 reply, "(ssssssouso)",
7bbf2d84
ZJS
1221 &id, NULL, NULL, &state, &substate,
1222 NULL, NULL, NULL, NULL, NULL)) > 0) {
eb5877a0 1223 bool found = !STR_IN_SET(state, "inactive", "dead", "failed");
7bbf2d84
ZJS
1224 log_debug("Unit %s is %s/%s, %scounting it.", id, state, substate, found ? "" : "not ");
1225 c += found;
1226 }
012f2b7d
ZJS
1227 if (r < 0)
1228 return bus_log_parse_error(r);
1229
1230 r = sd_bus_message_exit_container(reply);
1231 if (r < 0)
1232 return bus_log_parse_error(r);
1233
1234 return c;
1235}
1236
5ce97d33
YW
1237static int coredumpctl_main(int argc, char *argv[]) {
1238
1239 static const Verb verbs[] = {
c5896b6a
RG
1240 { "list", VERB_ANY, VERB_ANY, VERB_DEFAULT, dump_list },
1241 { "info", VERB_ANY, VERB_ANY, 0, dump_list },
1242 { "dump", VERB_ANY, VERB_ANY, 0, dump_core },
1243 { "debug", VERB_ANY, VERB_ANY, 0, run_debug },
1244 { "gdb", VERB_ANY, VERB_ANY, 0, run_debug },
5ce97d33
YW
1245 {}
1246 };
1247
1248 return dispatch_verb(argc, argv, verbs, NULL);
1249}
1250
c118b577 1251static int run(int argc, char *argv[]) {
5ce97d33 1252 int r, units_active;
5de0409e 1253
a9cdc94f 1254 setlocale(LC_ALL, "");
d2acb93d 1255 log_setup();
5de0409e 1256
1abaf488
LP
1257 /* The journal merging logic potentially needs a lot of fds. */
1258 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE);
1259
5ab9ed07 1260 r = parse_argv(argc, argv);
5ce97d33 1261 if (r <= 0)
c118b577 1262 return r;
5de0409e 1263
2cf4172a
LP
1264 sigbus_install();
1265
012f2b7d
ZJS
1266 units_active = check_units_active(); /* error is treated the same as 0 */
1267
5ce97d33 1268 r = coredumpctl_main(argc, argv);
5de0409e 1269
012f2b7d
ZJS
1270 if (units_active > 0)
1271 printf("%s-- Notice: %d systemd-coredump@.service %s, output may be incomplete.%s\n",
1272 ansi_highlight_red(),
1273 units_active, units_active == 1 ? "unit is running" : "units are running",
1274 ansi_normal());
aeb56450 1275
c118b577 1276 return r;
5de0409e 1277}
c118b577
ZJS
1278
1279DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);