1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "sd-journal.h"
12 #include "sd-messages.h"
14 #include "alloc-util.h"
15 #include "bus-error.h"
21 #include "journal-internal.h"
22 #include "journal-util.h"
25 #include "main-func.h"
27 #include "parse-util.h"
28 #include "path-util.h"
29 #include "pretty-print.h"
30 #include "process-util.h"
31 #include "rlimit-util.h"
33 #include "signal-util.h"
34 #include "string-util.h"
36 #include "terminal-util.h"
37 #include "tmpfile-util.h"
38 #include "user-util.h"
42 #define SHORT_BUS_CALL_TIMEOUT_USEC (3 * USEC_PER_SEC)
44 static usec_t arg_since
= USEC_INFINITY
, arg_until
= USEC_INFINITY
;
45 static const char* arg_field
= NULL
;
46 static const char *arg_debugger
= NULL
;
47 static const char *arg_directory
= NULL
;
48 static PagerFlags arg_pager_flags
= 0;
49 static int arg_no_legend
= false;
50 static int arg_one
= false;
51 static const char* arg_output
= NULL
;
52 static bool arg_reverse
= false;
53 static bool arg_quiet
= false;
55 static int add_match(sd_journal
*j
, const char *match
) {
56 _cleanup_free_
char *p
= NULL
;
57 const char* prefix
, *pattern
;
61 if (strchr(match
, '='))
63 else if (strchr(match
, '/')) {
64 r
= path_make_absolute_cwd(match
, &p
);
66 return log_error_errno(r
, "path_make_absolute_cwd(\"%s\"): %m", match
);
69 prefix
= "COREDUMP_EXE=";
70 } else if (parse_pid(match
, &pid
) >= 0)
71 prefix
= "COREDUMP_PID=";
73 prefix
= "COREDUMP_COMM=";
75 pattern
= strjoina(prefix
, match
);
76 log_debug("Adding match: %s", pattern
);
77 r
= sd_journal_add_match(j
, pattern
, 0);
79 return log_error_errno(r
, "Failed to add match \"%s\": %m", match
);
84 static int add_matches(sd_journal
*j
, char **matches
) {
88 r
= sd_journal_add_match(j
, "MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR
, 0);
90 return log_error_errno(r
, "Failed to add match \"%s\": %m", "MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR
);
92 r
= sd_journal_add_match(j
, "MESSAGE_ID=" SD_MESSAGE_BACKTRACE_STR
, 0);
94 return log_error_errno(r
, "Failed to add match \"%s\": %m", "MESSAGE_ID=" SD_MESSAGE_BACKTRACE_STR
);
96 STRV_FOREACH(match
, matches
) {
97 r
= add_match(j
, *match
);
105 static int acquire_journal(sd_journal
**ret
, char **matches
) {
106 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
112 r
= sd_journal_open_directory(&j
, arg_directory
, 0);
114 return log_error_errno(r
, "Failed to open journals in directory: %s: %m", arg_directory
);
116 r
= sd_journal_open(&j
, SD_JOURNAL_LOCAL_ONLY
);
118 return log_error_errno(r
, "Failed to open journal: %m");
121 r
= journal_access_check_and_warn(j
, arg_quiet
, true);
125 r
= add_matches(j
, matches
);
130 _cleanup_free_
char *filter
;
132 filter
= journal_make_match_string(j
);
133 log_debug("Journal filter: %s", filter
);
141 static int help(void) {
142 _cleanup_free_
char *link
= NULL
;
145 r
= terminal_urlify_man("coredumpctl", "1", &link
);
149 printf("%s [OPTIONS...]\n\n"
150 "List or retrieve coredumps from the journal.\n\n"
152 " -h --help Show this help\n"
153 " --version Print version string\n"
154 " --no-pager Do not pipe output into a pager\n"
155 " --no-legend Do not print the column headers\n"
156 " --debugger=DEBUGGER Use the given debugger\n"
157 " -1 Show information about most recent entry only\n"
158 " -S --since=DATE Only print coredumps since the date\n"
159 " -U --until=DATE Only print coredumps until the date\n"
160 " -r --reverse Show the newest entries first\n"
161 " -F --field=FIELD List all values a certain field takes\n"
162 " -o --output=FILE Write output to FILE\n"
163 " -D --directory=DIR Use journal files from directory\n\n"
164 " -q --quiet Do not show info messages and privilege warning\n"
166 " list [MATCHES...] List available coredumps (default)\n"
167 " info [MATCHES...] Show detailed information about one or more coredumps\n"
168 " dump [MATCHES...] Print first matching coredump to stdout\n"
169 " debug [MATCHES...] Start a debugger for the first matching coredump\n"
170 "\nSee the %s for details.\n"
171 , program_invocation_short_name
178 static int parse_argv(int argc
, char *argv
[]) {
188 static const struct option options
[] = {
189 { "help", no_argument
, NULL
, 'h' },
190 { "version" , no_argument
, NULL
, ARG_VERSION
},
191 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
192 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
193 { "debugger", required_argument
, NULL
, ARG_DEBUGGER
},
194 { "output", required_argument
, NULL
, 'o' },
195 { "field", required_argument
, NULL
, 'F' },
196 { "directory", required_argument
, NULL
, 'D' },
197 { "reverse", no_argument
, NULL
, 'r' },
198 { "since", required_argument
, NULL
, 'S' },
199 { "until", required_argument
, NULL
, 'U' },
200 { "quiet", no_argument
, NULL
, 'q' },
207 while ((c
= getopt_long(argc
, argv
, "ho:F:1D:rS:U:q", options
, NULL
)) >= 0)
216 arg_pager_flags
|= PAGER_DISABLE
;
220 arg_no_legend
= true;
224 arg_debugger
= optarg
;
229 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
230 "Cannot set output more than once.");
236 r
= parse_timestamp(optarg
, &arg_since
);
238 return log_error_errno(r
, "Failed to parse timestamp '%s': %m", optarg
);
242 r
= parse_timestamp(optarg
, &arg_until
);
244 return log_error_errno(r
, "Failed to parse timestamp '%s': %m", optarg
);
249 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
250 "Cannot use --field/-F more than once.");
259 arg_directory
= optarg
;
274 assert_not_reached("Unhandled option");
277 if (arg_since
!= USEC_INFINITY
&& arg_until
!= USEC_INFINITY
&&
278 arg_since
> arg_until
)
279 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
280 "--since= must be before --until=.");
285 static int retrieve(const void *data
,
293 ident
= strlen(name
) + 1; /* name + "=" */
298 if (memcmp(data
, name
, ident
- 1) != 0)
301 if (((const char*) data
)[ident
- 1] != '=')
304 v
= strndup((const char*)data
+ ident
, len
- ident
);
314 static int print_field(FILE* file
, sd_journal
*j
) {
323 /* A (user-specified) field may appear more than once for a given entry.
324 * We will print all of the occurences.
325 * This is different below for fields that systemd-coredump uses,
326 * because they cannot meaningfully appear more than once.
328 SD_JOURNAL_FOREACH_DATA(j
, d
, l
) {
329 _cleanup_free_
char *value
= NULL
;
332 r
= retrieve(d
, l
, arg_field
, &value
);
336 fprintf(file
, "%s\n", value
);
342 #define RETRIEVE(d, l, name, arg) \
344 int _r = retrieve(d, l, name, &arg); \
351 static int print_list(FILE* file
, sd_journal
*j
, int had_legend
) {
353 *mid
= NULL
, *pid
= NULL
, *uid
= NULL
, *gid
= NULL
,
354 *sgnl
= NULL
, *exe
= NULL
, *comm
= NULL
, *cmdline
= NULL
,
355 *filename
= NULL
, *truncated
= NULL
, *coredump
= NULL
;
359 char buf
[FORMAT_TIMESTAMP_MAX
];
362 bool normal_coredump
;
367 SD_JOURNAL_FOREACH_DATA(j
, d
, l
) {
368 RETRIEVE(d
, l
, "MESSAGE_ID", mid
);
369 RETRIEVE(d
, l
, "COREDUMP_PID", pid
);
370 RETRIEVE(d
, l
, "COREDUMP_UID", uid
);
371 RETRIEVE(d
, l
, "COREDUMP_GID", gid
);
372 RETRIEVE(d
, l
, "COREDUMP_SIGNAL", sgnl
);
373 RETRIEVE(d
, l
, "COREDUMP_EXE", exe
);
374 RETRIEVE(d
, l
, "COREDUMP_COMM", comm
);
375 RETRIEVE(d
, l
, "COREDUMP_CMDLINE", cmdline
);
376 RETRIEVE(d
, l
, "COREDUMP_FILENAME", filename
);
377 RETRIEVE(d
, l
, "COREDUMP_TRUNCATED", truncated
);
378 RETRIEVE(d
, l
, "COREDUMP", coredump
);
381 if (!pid
&& !uid
&& !gid
&& !sgnl
&& !exe
&& !comm
&& !cmdline
&& !filename
) {
382 log_warning("Empty coredump log entry");
386 r
= sd_journal_get_realtime_usec(j
, &t
);
388 return log_error_errno(r
, "Failed to get realtime timestamp: %m");
390 format_timestamp(buf
, sizeof(buf
), t
);
392 if (!had_legend
&& !arg_no_legend
)
393 fprintf(file
, "%-*s %*s %*s %*s %*s %-*s %s\n",
394 FORMAT_TIMESTAMP_WIDTH
, "TIME",
402 normal_coredump
= streq_ptr(mid
, SD_MESSAGE_COREDUMP_STR
);
405 if (access(filename
, R_OK
) == 0)
407 else if (errno
== ENOENT
)
413 else if (normal_coredump
)
418 if (STR_IN_SET(present
, "present", "journal") && truncated
&& parse_boolean(truncated
) > 0)
419 present
= "truncated";
421 fprintf(file
, "%-*s %*s %*s %*s %*s %-*s %s\n",
422 FORMAT_TIMESTAMP_WIDTH
, buf
,
426 3, normal_coredump
? strna(sgnl
) : "-",
428 strna(exe
?: (comm
?: cmdline
)));
433 static int print_info(FILE *file
, sd_journal
*j
, bool need_space
) {
435 *mid
= NULL
, *pid
= NULL
, *uid
= NULL
, *gid
= NULL
,
436 *sgnl
= NULL
, *exe
= NULL
, *comm
= NULL
, *cmdline
= NULL
,
437 *unit
= NULL
, *user_unit
= NULL
, *session
= NULL
,
438 *boot_id
= NULL
, *machine_id
= NULL
, *hostname
= NULL
,
439 *slice
= NULL
, *cgroup
= NULL
, *owner_uid
= NULL
,
440 *message
= NULL
, *timestamp
= NULL
, *filename
= NULL
,
441 *truncated
= NULL
, *coredump
= NULL
;
444 bool normal_coredump
;
450 SD_JOURNAL_FOREACH_DATA(j
, d
, l
) {
451 RETRIEVE(d
, l
, "MESSAGE_ID", mid
);
452 RETRIEVE(d
, l
, "COREDUMP_PID", pid
);
453 RETRIEVE(d
, l
, "COREDUMP_UID", uid
);
454 RETRIEVE(d
, l
, "COREDUMP_GID", gid
);
455 RETRIEVE(d
, l
, "COREDUMP_SIGNAL", sgnl
);
456 RETRIEVE(d
, l
, "COREDUMP_EXE", exe
);
457 RETRIEVE(d
, l
, "COREDUMP_COMM", comm
);
458 RETRIEVE(d
, l
, "COREDUMP_CMDLINE", cmdline
);
459 RETRIEVE(d
, l
, "COREDUMP_UNIT", unit
);
460 RETRIEVE(d
, l
, "COREDUMP_USER_UNIT", user_unit
);
461 RETRIEVE(d
, l
, "COREDUMP_SESSION", session
);
462 RETRIEVE(d
, l
, "COREDUMP_OWNER_UID", owner_uid
);
463 RETRIEVE(d
, l
, "COREDUMP_SLICE", slice
);
464 RETRIEVE(d
, l
, "COREDUMP_CGROUP", cgroup
);
465 RETRIEVE(d
, l
, "COREDUMP_TIMESTAMP", timestamp
);
466 RETRIEVE(d
, l
, "COREDUMP_FILENAME", filename
);
467 RETRIEVE(d
, l
, "COREDUMP_TRUNCATED", truncated
);
468 RETRIEVE(d
, l
, "COREDUMP", coredump
);
469 RETRIEVE(d
, l
, "_BOOT_ID", boot_id
);
470 RETRIEVE(d
, l
, "_MACHINE_ID", machine_id
);
471 RETRIEVE(d
, l
, "_HOSTNAME", hostname
);
472 RETRIEVE(d
, l
, "MESSAGE", message
);
478 normal_coredump
= streq_ptr(mid
, SD_MESSAGE_COREDUMP_STR
);
482 " PID: %s%s%s (%s)\n",
483 ansi_highlight(), strna(pid
), ansi_normal(), comm
);
487 ansi_highlight(), strna(pid
), ansi_normal());
492 if (parse_uid(uid
, &n
) >= 0) {
493 _cleanup_free_
char *u
= NULL
;
509 if (parse_gid(gid
, &n
) >= 0) {
510 _cleanup_free_
char *g
= NULL
;
525 const char *name
= normal_coredump
? "Signal" : "Reason";
527 if (normal_coredump
&& safe_atoi(sgnl
, &sig
) >= 0)
528 fprintf(file
, " %s: %s (%s)\n", name
, sgnl
, signal_to_string(sig
));
530 fprintf(file
, " %s: %s\n", name
, sgnl
);
536 r
= safe_atou64(timestamp
, &u
);
538 char absolute
[FORMAT_TIMESTAMP_MAX
], relative
[FORMAT_TIMESPAN_MAX
];
541 " Timestamp: %s (%s)\n",
542 format_timestamp(absolute
, sizeof(absolute
), u
),
543 format_timestamp_relative(relative
, sizeof(relative
), u
));
546 fprintf(file
, " Timestamp: %s\n", timestamp
);
550 fprintf(file
, " Command Line: %s\n", cmdline
);
552 fprintf(file
, " Executable: %s%s%s\n", ansi_highlight(), exe
, ansi_normal());
554 fprintf(file
, " Control Group: %s\n", cgroup
);
556 fprintf(file
, " Unit: %s\n", unit
);
558 fprintf(file
, " User Unit: %s\n", user_unit
);
560 fprintf(file
, " Slice: %s\n", slice
);
562 fprintf(file
, " Session: %s\n", session
);
566 if (parse_uid(owner_uid
, &n
) >= 0) {
567 _cleanup_free_
char *u
= NULL
;
571 " Owner UID: %s (%s)\n",
580 fprintf(file
, " Boot ID: %s\n", boot_id
);
582 fprintf(file
, " Machine ID: %s\n", machine_id
);
584 fprintf(file
, " Hostname: %s\n", hostname
);
589 inacc
= access(filename
, R_OK
) < 0;
590 trunc
= truncated
&& parse_boolean(truncated
) > 0;
593 fprintf(file
, " Storage: %s%s (%s%s%s)%s\n",
594 ansi_highlight_red(),
596 inacc
? "inaccessible" : "",
597 inacc
&& trunc
? ", " : "",
598 trunc
? "truncated" : "",
601 fprintf(file
, " Storage: %s\n", filename
);
605 fprintf(file
, " Storage: journal\n");
607 fprintf(file
, " Storage: none\n");
610 _cleanup_free_
char *m
= NULL
;
612 m
= strreplace(message
, "\n", "\n ");
614 fprintf(file
, " Message: %s\n", strstrip(m
?: message
));
620 static int focus(sd_journal
*j
) {
623 r
= sd_journal_seek_tail(j
);
625 r
= sd_journal_previous(j
);
627 return log_error_errno(r
, "Failed to search journal: %m");
629 return log_error_errno(SYNTHETIC_ERRNO(ESRCH
),
634 static int print_entry(sd_journal
*j
, unsigned n_found
, bool verb_is_info
) {
638 return print_info(stdout
, j
, n_found
);
640 return print_field(stdout
, j
);
642 return print_list(stdout
, j
, n_found
);
645 static int dump_list(int argc
, char **argv
, void *userdata
) {
646 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
647 unsigned n_found
= 0;
651 verb_is_info
= (argc
>= 1 && streq(argv
[0], "info"));
653 r
= acquire_journal(&j
, argv
+ 1);
657 (void) pager_open(arg_pager_flags
);
659 /* The coredumps are likely to compressed, and for just
660 * listing them we don't need to decompress them, so let's
661 * pick a fairly low data threshold here */
662 sd_journal_set_data_threshold(j
, 4096);
664 /* "info" without pattern implies "-1" */
665 if (arg_one
|| (verb_is_info
&& argc
== 1)) {
670 return print_entry(j
, 0, verb_is_info
);
672 if (arg_since
!= USEC_INFINITY
&& !arg_reverse
)
673 r
= sd_journal_seek_realtime_usec(j
, arg_since
);
674 else if (arg_until
!= USEC_INFINITY
&& arg_reverse
)
675 r
= sd_journal_seek_realtime_usec(j
, arg_until
);
676 else if (arg_reverse
)
677 r
= sd_journal_seek_tail(j
);
679 r
= sd_journal_seek_head(j
);
681 return log_error_errno(r
, "Failed to seek to date: %m");
685 r
= sd_journal_next(j
);
687 r
= sd_journal_previous(j
);
690 return log_error_errno(r
, "Failed to iterate through journal: %m");
695 if (arg_until
!= USEC_INFINITY
&& !arg_reverse
) {
698 r
= sd_journal_get_realtime_usec(j
, &usec
);
700 return log_error_errno(r
, "Failed to determine timestamp: %m");
701 if (usec
> arg_until
)
705 if (arg_since
!= USEC_INFINITY
&& arg_reverse
) {
708 r
= sd_journal_get_realtime_usec(j
, &usec
);
710 return log_error_errno(r
, "Failed to determine timestamp: %m");
711 if (usec
< arg_since
)
715 r
= print_entry(j
, n_found
++, verb_is_info
);
720 if (!arg_field
&& n_found
<= 0) {
722 log_notice("No coredumps found.");
730 static int save_core(sd_journal
*j
, FILE *file
, char **path
, bool *unlink_temp
) {
732 _cleanup_free_
char *filename
= NULL
;
735 _cleanup_close_
int fdt
= -1;
738 assert(!(file
&& path
)); /* At most one can be specified */
739 assert(!!path
== !!unlink_temp
); /* Those must be specified together */
741 /* Look for a coredump on disk first. */
742 r
= sd_journal_get_data(j
, "COREDUMP_FILENAME", (const void**) &data
, &len
);
744 retrieve(data
, len
, "COREDUMP_FILENAME", &filename
);
747 return log_error_errno(r
, "Failed to retrieve COREDUMP_FILENAME field: %m");
748 /* Check that we can have a COREDUMP field. We still haven't set a high
749 * data threshold, so we'll get a few kilobytes at most.
752 r
= sd_journal_get_data(j
, "COREDUMP", (const void**) &data
, &len
);
754 return log_error_errno(r
, "Coredump entry has no core attached (neither internally in the journal nor externally on disk).");
756 return log_error_errno(r
, "Failed to retrieve COREDUMP field: %m");
760 if (access(filename
, R_OK
) < 0)
761 return log_error_errno(errno
, "File \"%s\" is not readable: %m", filename
);
763 if (path
&& !endswith(filename
, ".xz") && !endswith(filename
, ".lz4")) {
764 *path
= TAKE_PTR(filename
);
773 /* Create a temporary file to write the uncompressed core to. */
775 r
= var_tmp_dir(&vt
);
777 return log_error_errno(r
, "Failed to acquire temporary directory path: %m");
779 temp
= strjoin(vt
, "/coredump-XXXXXX");
783 fdt
= mkostemp_safe(temp
);
785 return log_error_errno(fdt
, "Failed to create temporary file: %m");
786 log_debug("Created temporary file %s", temp
);
790 /* If neither path or file are specified, we will write to stdout. Let's now check
791 * if stdout is connected to a tty. We checked that the file exists, or that the
792 * core might be stored in the journal. In this second case, if we found the entry,
793 * in all likelyhood we will be able to access the COREDUMP= field. In either case,
794 * we stop before doing any "real" work, i.e. before starting decompression or
795 * reading from the file or creating temporary files.
799 return log_error_errno(SYNTHETIC_ERRNO(ENOTTY
),
800 "Refusing to dump core to tty"
801 " (use shell redirection or specify --output).");
809 #if HAVE_XZ || HAVE_LZ4
810 _cleanup_close_
int fdf
;
812 fdf
= open(filename
, O_RDONLY
| O_CLOEXEC
);
814 r
= log_error_errno(errno
, "Failed to open %s: %m", filename
);
818 r
= decompress_stream(filename
, fdf
, fd
, -1);
820 log_error_errno(r
, "Failed to decompress %s: %m", filename
);
824 log_error("Cannot decompress file. Compiled without compression support.");
831 /* We want full data, nothing truncated. */
832 sd_journal_set_data_threshold(j
, 0);
834 r
= sd_journal_get_data(j
, "COREDUMP", (const void**) &data
, &len
);
836 return log_error_errno(r
, "Failed to retrieve COREDUMP field: %m");
842 sz
= write(fd
, data
, len
);
844 r
= log_error_errno(errno
, "Failed to write output: %m");
847 if (sz
!= (ssize_t
) len
) {
848 log_error("Short write to output.");
863 log_debug("Removed temporary file %s", temp
);
868 static int dump_core(int argc
, char **argv
, void *userdata
) {
869 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
870 _cleanup_fclose_
FILE *f
= NULL
;
874 log_error("Option --field/-F only makes sense with list");
878 r
= acquire_journal(&j
, argv
+ 1);
887 f
= fopen(arg_output
, "we");
889 return log_error_errno(errno
, "Failed to open \"%s\" for writing: %m", arg_output
);
892 print_info(f
? stdout
: stderr
, j
, false);
894 r
= save_core(j
, f
, NULL
, NULL
);
898 r
= sd_journal_previous(j
);
899 if (r
> 0 && !arg_quiet
)
900 log_notice("More than one entry matches, ignoring rest.");
905 static int run_debug(int argc
, char **argv
, void *userdata
) {
906 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
907 _cleanup_free_
char *exe
= NULL
, *path
= NULL
;
908 bool unlink_path
= false;
909 const char *data
, *fork_name
;
917 env_debugger
= getenv("SYSTEMD_DEBUGGER");
919 arg_debugger
= env_debugger
;
921 arg_debugger
= "gdb";
925 log_error("Option --field/-F only makes sense with list");
929 r
= acquire_journal(&j
, argv
+ 1);
937 print_info(stdout
, j
, false);
940 r
= sd_journal_get_data(j
, "COREDUMP_EXE", (const void**) &data
, &len
);
942 return log_error_errno(r
, "Failed to retrieve COREDUMP_EXE field: %m");
944 assert(len
> STRLEN("COREDUMP_EXE="));
945 data
+= STRLEN("COREDUMP_EXE=");
946 len
-= STRLEN("COREDUMP_EXE=");
948 exe
= strndup(data
, len
);
952 if (endswith(exe
, " (deleted)")) {
953 log_error("Binary already deleted.");
957 if (!path_is_absolute(exe
)) {
958 log_error("Binary is not an absolute path.");
962 r
= save_core(j
, NULL
, &path
, &unlink_path
);
966 /* Don't interfere with gdb and its handling of SIGINT. */
967 (void) ignore_signals(SIGINT
, -1);
969 fork_name
= strjoina("(", arg_debugger
, ")");
971 r
= safe_fork(fork_name
, FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_CLOSE_ALL_FDS
|FORK_RLIMIT_NOFILE_SAFE
|FORK_LOG
, &pid
);
975 execlp(arg_debugger
, arg_debugger
, exe
, "-c", path
, NULL
);
977 log_error_errno(errno
, "Failed to invoke %s: %m", arg_debugger
);
981 r
= wait_for_terminate_and_check(arg_debugger
, pid
, WAIT_LOG_ABNORMAL
);
984 (void) default_signals(SIGINT
, -1);
987 log_debug("Removed temporary file %s", path
);
994 static int check_units_active(void) {
995 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
996 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
997 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
998 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1000 const char *id
, *state
, *substate
;
1005 r
= sd_bus_default_system(&bus
);
1007 return log_error_errno(r
, "Failed to acquire bus: %m");
1009 r
= sd_bus_message_new_method_call(
1012 "org.freedesktop.systemd1",
1013 "/org/freedesktop/systemd1",
1014 "org.freedesktop.systemd1.Manager",
1015 "ListUnitsByPatterns");
1017 return bus_log_create_error(r
);
1019 r
= sd_bus_message_append_strv(m
, NULL
);
1021 return bus_log_create_error(r
);
1023 r
= sd_bus_message_append_strv(m
, STRV_MAKE("systemd-coredump@*.service"));
1025 return bus_log_create_error(r
);
1027 r
= sd_bus_call(bus
, m
, SHORT_BUS_CALL_TIMEOUT_USEC
, &error
, &reply
);
1029 return log_error_errno(r
, "Failed to check if any systemd-coredump@.service units are running: %s",
1030 bus_error_message(&error
, r
));
1032 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ssssssouso)");
1034 return bus_log_parse_error(r
);
1036 while ((r
= sd_bus_message_read(
1037 reply
, "(ssssssouso)",
1038 &id
, NULL
, NULL
, &state
, &substate
,
1039 NULL
, NULL
, NULL
, NULL
, NULL
)) > 0) {
1040 bool found
= !STR_IN_SET(state
, "inactive", "dead", "failed");
1041 log_debug("Unit %s is %s/%s, %scounting it.", id
, state
, substate
, found
? "" : "not ");
1045 return bus_log_parse_error(r
);
1047 r
= sd_bus_message_exit_container(reply
);
1049 return bus_log_parse_error(r
);
1054 static int coredumpctl_main(int argc
, char *argv
[]) {
1056 static const Verb verbs
[] = {
1057 { "list", VERB_ANY
, VERB_ANY
, VERB_DEFAULT
, dump_list
},
1058 { "info", VERB_ANY
, VERB_ANY
, 0, dump_list
},
1059 { "dump", VERB_ANY
, VERB_ANY
, 0, dump_core
},
1060 { "debug", VERB_ANY
, VERB_ANY
, 0, run_debug
},
1061 { "gdb", VERB_ANY
, VERB_ANY
, 0, run_debug
},
1065 return dispatch_verb(argc
, argv
, verbs
, NULL
);
1068 static int run(int argc
, char *argv
[]) {
1069 int r
, units_active
;
1071 setlocale(LC_ALL
, "");
1072 log_parse_environment();
1075 /* The journal merging logic potentially needs a lot of fds. */
1076 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE
);
1078 r
= parse_argv(argc
, argv
);
1084 units_active
= check_units_active(); /* error is treated the same as 0 */
1086 r
= coredumpctl_main(argc
, argv
);
1088 if (units_active
> 0)
1089 printf("%s-- Notice: %d systemd-coredump@.service %s, output may be incomplete.%s\n",
1090 ansi_highlight_red(),
1091 units_active
, units_active
== 1 ? "unit is running" : "units are running",
1096 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run
);