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"
22 #include "journal-internal.h"
23 #include "journal-util.h"
27 #include "parse-util.h"
28 #include "path-util.h"
29 #include "process-util.h"
30 #include "rlimit-util.h"
32 #include "signal-util.h"
33 #include "string-util.h"
35 #include "terminal-util.h"
36 #include "user-util.h"
40 #define SHORT_BUS_CALL_TIMEOUT_USEC (3 * USEC_PER_SEC)
42 static usec_t arg_since
= USEC_INFINITY
, arg_until
= USEC_INFINITY
;
43 static const char* arg_field
= NULL
;
44 static const char *arg_debugger
= NULL
;
45 static const char *arg_directory
= NULL
;
46 static bool arg_no_pager
= false;
47 static int arg_no_legend
= false;
48 static int arg_one
= false;
49 static FILE* arg_output
= NULL
;
50 static bool arg_reverse
= false;
51 static bool arg_quiet
= false;
53 static int add_match(sd_journal
*j
, const char *match
) {
54 _cleanup_free_
char *p
= NULL
;
55 const char* prefix
, *pattern
;
59 if (strchr(match
, '='))
61 else if (strchr(match
, '/')) {
62 r
= path_make_absolute_cwd(match
, &p
);
64 return log_error_errno(r
, "path_make_absolute_cwd(\"%s\"): %m", match
);
67 prefix
= "COREDUMP_EXE=";
68 } else if (parse_pid(match
, &pid
) >= 0)
69 prefix
= "COREDUMP_PID=";
71 prefix
= "COREDUMP_COMM=";
73 pattern
= strjoina(prefix
, match
);
74 log_debug("Adding match: %s", pattern
);
75 r
= sd_journal_add_match(j
, pattern
, 0);
77 return log_error_errno(r
, "Failed to add match \"%s\": %m", match
);
82 static int add_matches(sd_journal
*j
, char **matches
) {
86 r
= sd_journal_add_match(j
, "MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR
, 0);
88 return log_error_errno(r
, "Failed to add match \"%s\": %m", "MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR
);
90 r
= sd_journal_add_match(j
, "MESSAGE_ID=" SD_MESSAGE_BACKTRACE_STR
, 0);
92 return log_error_errno(r
, "Failed to add match \"%s\": %m", "MESSAGE_ID=" SD_MESSAGE_BACKTRACE_STR
);
94 STRV_FOREACH(match
, matches
) {
95 r
= add_match(j
, *match
);
103 static int acquire_journal(sd_journal
**ret
, char **matches
) {
104 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
110 r
= sd_journal_open_directory(&j
, arg_directory
, 0);
112 return log_error_errno(r
, "Failed to open journals in directory: %s: %m", arg_directory
);
114 r
= sd_journal_open(&j
, SD_JOURNAL_LOCAL_ONLY
);
116 return log_error_errno(r
, "Failed to open journal: %m");
119 r
= journal_access_check_and_warn(j
, arg_quiet
, true);
123 r
= add_matches(j
, matches
);
128 _cleanup_free_
char *filter
;
130 filter
= journal_make_match_string(j
);
131 log_debug("Journal filter: %s", filter
);
139 static int help(void) {
140 _cleanup_free_
char *link
= NULL
;
143 r
= terminal_urlify_man("coredumpctl", "1", &link
);
147 printf("%s [OPTIONS...]\n\n"
148 "List or retrieve coredumps from the journal.\n\n"
150 " -h --help Show this help\n"
151 " --version Print version string\n"
152 " --no-pager Do not pipe output into a pager\n"
153 " --no-legend Do not print the column headers\n"
154 " --debugger=DEBUGGER Use the given debugger\n"
155 " -1 Show information about most recent entry only\n"
156 " -S --since=DATE Only print coredumps since the date\n"
157 " -U --until=DATE Only print coredumps until the date\n"
158 " -r --reverse Show the newest entries first\n"
159 " -F --field=FIELD List all values a certain field takes\n"
160 " -o --output=FILE Write output to FILE\n"
161 " -D --directory=DIR Use journal files from directory\n\n"
162 " -q --quiet Do not show info messages and privilege warning\n"
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"
168 "\nSee the %s for details.\n"
169 , program_invocation_short_name
176 static int parse_argv(int argc
, char *argv
[]) {
186 static const struct option options
[] = {
187 { "help", no_argument
, NULL
, 'h' },
188 { "version" , no_argument
, NULL
, ARG_VERSION
},
189 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
190 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
191 { "debugger", required_argument
, NULL
, ARG_DEBUGGER
},
192 { "output", required_argument
, NULL
, 'o' },
193 { "field", required_argument
, NULL
, 'F' },
194 { "directory", required_argument
, NULL
, 'D' },
195 { "reverse", no_argument
, NULL
, 'r' },
196 { "since", required_argument
, NULL
, 'S' },
197 { "until", required_argument
, NULL
, 'U' },
198 { "quiet", no_argument
, NULL
, 'q' },
205 while ((c
= getopt_long(argc
, argv
, "ho:F:1D:rS:U:q", options
, NULL
)) >= 0)
218 arg_no_legend
= true;
222 arg_debugger
= optarg
;
227 log_error("Cannot set output more than once.");
231 arg_output
= fopen(optarg
, "we");
233 return log_error_errno(errno
, "writing to '%s': %m", optarg
);
238 r
= parse_timestamp(optarg
, &arg_since
);
240 return log_error_errno(r
, "Failed to parse timestamp '%s': %m", optarg
);
244 r
= parse_timestamp(optarg
, &arg_until
);
246 return log_error_errno(r
, "Failed to parse timestamp '%s': %m", optarg
);
251 log_error("Cannot use --field/-F more than once.");
262 arg_directory
= optarg
;
277 assert_not_reached("Unhandled option");
280 if (arg_since
!= USEC_INFINITY
&& arg_until
!= USEC_INFINITY
&&
281 arg_since
> arg_until
) {
282 log_error("--since= must be before --until=.");
289 static int retrieve(const void *data
,
297 ident
= strlen(name
) + 1; /* name + "=" */
302 if (memcmp(data
, name
, ident
- 1) != 0)
305 if (((const char*) data
)[ident
- 1] != '=')
308 v
= strndup((const char*)data
+ ident
, len
- ident
);
318 static int print_field(FILE* file
, sd_journal
*j
) {
327 /* A (user-specified) field may appear more than once for a given entry.
328 * We will print all of the occurences.
329 * This is different below for fields that systemd-coredump uses,
330 * because they cannot meaningfully appear more than once.
332 SD_JOURNAL_FOREACH_DATA(j
, d
, l
) {
333 _cleanup_free_
char *value
= NULL
;
336 r
= retrieve(d
, l
, arg_field
, &value
);
340 fprintf(file
, "%s\n", value
);
346 #define RETRIEVE(d, l, name, arg) \
348 int _r = retrieve(d, l, name, &arg); \
355 static int print_list(FILE* file
, sd_journal
*j
, int had_legend
) {
357 *mid
= NULL
, *pid
= NULL
, *uid
= NULL
, *gid
= NULL
,
358 *sgnl
= NULL
, *exe
= NULL
, *comm
= NULL
, *cmdline
= NULL
,
359 *filename
= NULL
, *truncated
= NULL
, *coredump
= NULL
;
363 char buf
[FORMAT_TIMESTAMP_MAX
];
366 bool normal_coredump
;
371 SD_JOURNAL_FOREACH_DATA(j
, d
, l
) {
372 RETRIEVE(d
, l
, "MESSAGE_ID", mid
);
373 RETRIEVE(d
, l
, "COREDUMP_PID", pid
);
374 RETRIEVE(d
, l
, "COREDUMP_UID", uid
);
375 RETRIEVE(d
, l
, "COREDUMP_GID", gid
);
376 RETRIEVE(d
, l
, "COREDUMP_SIGNAL", sgnl
);
377 RETRIEVE(d
, l
, "COREDUMP_EXE", exe
);
378 RETRIEVE(d
, l
, "COREDUMP_COMM", comm
);
379 RETRIEVE(d
, l
, "COREDUMP_CMDLINE", cmdline
);
380 RETRIEVE(d
, l
, "COREDUMP_FILENAME", filename
);
381 RETRIEVE(d
, l
, "COREDUMP_TRUNCATED", truncated
);
382 RETRIEVE(d
, l
, "COREDUMP", coredump
);
385 if (!pid
&& !uid
&& !gid
&& !sgnl
&& !exe
&& !comm
&& !cmdline
&& !filename
) {
386 log_warning("Empty coredump log entry");
390 r
= sd_journal_get_realtime_usec(j
, &t
);
392 return log_error_errno(r
, "Failed to get realtime timestamp: %m");
394 format_timestamp(buf
, sizeof(buf
), t
);
396 if (!had_legend
&& !arg_no_legend
)
397 fprintf(file
, "%-*s %*s %*s %*s %*s %-*s %s\n",
398 FORMAT_TIMESTAMP_WIDTH
, "TIME",
406 normal_coredump
= streq_ptr(mid
, SD_MESSAGE_COREDUMP_STR
);
409 if (access(filename
, R_OK
) == 0)
411 else if (errno
== ENOENT
)
417 else if (normal_coredump
)
422 if (STR_IN_SET(present
, "present", "journal") && truncated
&& parse_boolean(truncated
) > 0)
423 present
= "truncated";
425 fprintf(file
, "%-*s %*s %*s %*s %*s %-*s %s\n",
426 FORMAT_TIMESTAMP_WIDTH
, buf
,
430 3, normal_coredump
? strna(sgnl
) : "-",
432 strna(exe
?: (comm
?: cmdline
)));
437 static int print_info(FILE *file
, sd_journal
*j
, bool need_space
) {
439 *mid
= NULL
, *pid
= NULL
, *uid
= NULL
, *gid
= NULL
,
440 *sgnl
= NULL
, *exe
= NULL
, *comm
= NULL
, *cmdline
= NULL
,
441 *unit
= NULL
, *user_unit
= NULL
, *session
= NULL
,
442 *boot_id
= NULL
, *machine_id
= NULL
, *hostname
= NULL
,
443 *slice
= NULL
, *cgroup
= NULL
, *owner_uid
= NULL
,
444 *message
= NULL
, *timestamp
= NULL
, *filename
= NULL
,
445 *truncated
= NULL
, *coredump
= NULL
;
448 bool normal_coredump
;
454 SD_JOURNAL_FOREACH_DATA(j
, d
, l
) {
455 RETRIEVE(d
, l
, "MESSAGE_ID", mid
);
456 RETRIEVE(d
, l
, "COREDUMP_PID", pid
);
457 RETRIEVE(d
, l
, "COREDUMP_UID", uid
);
458 RETRIEVE(d
, l
, "COREDUMP_GID", gid
);
459 RETRIEVE(d
, l
, "COREDUMP_SIGNAL", sgnl
);
460 RETRIEVE(d
, l
, "COREDUMP_EXE", exe
);
461 RETRIEVE(d
, l
, "COREDUMP_COMM", comm
);
462 RETRIEVE(d
, l
, "COREDUMP_CMDLINE", cmdline
);
463 RETRIEVE(d
, l
, "COREDUMP_UNIT", unit
);
464 RETRIEVE(d
, l
, "COREDUMP_USER_UNIT", user_unit
);
465 RETRIEVE(d
, l
, "COREDUMP_SESSION", session
);
466 RETRIEVE(d
, l
, "COREDUMP_OWNER_UID", owner_uid
);
467 RETRIEVE(d
, l
, "COREDUMP_SLICE", slice
);
468 RETRIEVE(d
, l
, "COREDUMP_CGROUP", cgroup
);
469 RETRIEVE(d
, l
, "COREDUMP_TIMESTAMP", timestamp
);
470 RETRIEVE(d
, l
, "COREDUMP_FILENAME", filename
);
471 RETRIEVE(d
, l
, "COREDUMP_TRUNCATED", truncated
);
472 RETRIEVE(d
, l
, "COREDUMP", coredump
);
473 RETRIEVE(d
, l
, "_BOOT_ID", boot_id
);
474 RETRIEVE(d
, l
, "_MACHINE_ID", machine_id
);
475 RETRIEVE(d
, l
, "_HOSTNAME", hostname
);
476 RETRIEVE(d
, l
, "MESSAGE", message
);
482 normal_coredump
= streq_ptr(mid
, SD_MESSAGE_COREDUMP_STR
);
486 " PID: %s%s%s (%s)\n",
487 ansi_highlight(), strna(pid
), ansi_normal(), comm
);
491 ansi_highlight(), strna(pid
), ansi_normal());
496 if (parse_uid(uid
, &n
) >= 0) {
497 _cleanup_free_
char *u
= NULL
;
513 if (parse_gid(gid
, &n
) >= 0) {
514 _cleanup_free_
char *g
= NULL
;
529 const char *name
= normal_coredump
? "Signal" : "Reason";
531 if (normal_coredump
&& safe_atoi(sgnl
, &sig
) >= 0)
532 fprintf(file
, " %s: %s (%s)\n", name
, sgnl
, signal_to_string(sig
));
534 fprintf(file
, " %s: %s\n", name
, sgnl
);
540 r
= safe_atou64(timestamp
, &u
);
542 char absolute
[FORMAT_TIMESTAMP_MAX
], relative
[FORMAT_TIMESPAN_MAX
];
545 " Timestamp: %s (%s)\n",
546 format_timestamp(absolute
, sizeof(absolute
), u
),
547 format_timestamp_relative(relative
, sizeof(relative
), u
));
550 fprintf(file
, " Timestamp: %s\n", timestamp
);
554 fprintf(file
, " Command Line: %s\n", cmdline
);
556 fprintf(file
, " Executable: %s%s%s\n", ansi_highlight(), exe
, ansi_normal());
558 fprintf(file
, " Control Group: %s\n", cgroup
);
560 fprintf(file
, " Unit: %s\n", unit
);
562 fprintf(file
, " User Unit: %s\n", user_unit
);
564 fprintf(file
, " Slice: %s\n", slice
);
566 fprintf(file
, " Session: %s\n", session
);
570 if (parse_uid(owner_uid
, &n
) >= 0) {
571 _cleanup_free_
char *u
= NULL
;
575 " Owner UID: %s (%s)\n",
584 fprintf(file
, " Boot ID: %s\n", boot_id
);
586 fprintf(file
, " Machine ID: %s\n", machine_id
);
588 fprintf(file
, " Hostname: %s\n", hostname
);
593 inacc
= access(filename
, R_OK
) < 0;
594 trunc
= truncated
&& parse_boolean(truncated
) > 0;
597 fprintf(file
, " Storage: %s%s (%s%s%s)%s\n",
598 ansi_highlight_red(),
600 inacc
? "inaccessible" : "",
601 inacc
&& trunc
? ", " : "",
602 trunc
? "truncated" : "",
605 fprintf(file
, " Storage: %s\n", filename
);
609 fprintf(file
, " Storage: journal\n");
611 fprintf(file
, " Storage: none\n");
614 _cleanup_free_
char *m
= NULL
;
616 m
= strreplace(message
, "\n", "\n ");
618 fprintf(file
, " Message: %s\n", strstrip(m
?: message
));
624 static int focus(sd_journal
*j
) {
627 r
= sd_journal_seek_tail(j
);
629 r
= sd_journal_previous(j
);
631 return log_error_errno(r
, "Failed to search journal: %m");
633 log_error("No match found.");
639 static int print_entry(sd_journal
*j
, unsigned n_found
, bool verb_is_info
) {
643 return print_info(stdout
, j
, n_found
);
645 return print_field(stdout
, j
);
647 return print_list(stdout
, j
, n_found
);
650 static int dump_list(int argc
, char **argv
, void *userdata
) {
651 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
652 unsigned n_found
= 0;
656 verb_is_info
= (argc
>= 1 && streq(argv
[0], "info"));
658 r
= acquire_journal(&j
, argv
+ 1);
662 (void) pager_open(arg_no_pager
, false);
664 /* The coredumps are likely to compressed, and for just
665 * listing them we don't need to decompress them, so let's
666 * pick a fairly low data threshold here */
667 sd_journal_set_data_threshold(j
, 4096);
669 /* "info" without pattern implies "-1" */
670 if (arg_one
|| (verb_is_info
&& argc
== 1)) {
675 return print_entry(j
, 0, verb_is_info
);
677 if (arg_since
!= USEC_INFINITY
&& !arg_reverse
)
678 r
= sd_journal_seek_realtime_usec(j
, arg_since
);
679 else if (arg_until
!= USEC_INFINITY
&& arg_reverse
)
680 r
= sd_journal_seek_realtime_usec(j
, arg_until
);
681 else if (arg_reverse
)
682 r
= sd_journal_seek_tail(j
);
684 r
= sd_journal_seek_head(j
);
686 return log_error_errno(r
, "Failed to seek to date: %m");
690 r
= sd_journal_next(j
);
692 r
= sd_journal_previous(j
);
695 return log_error_errno(r
, "Failed to iterate through journal: %m");
700 if (arg_until
!= USEC_INFINITY
&& !arg_reverse
) {
703 r
= sd_journal_get_realtime_usec(j
, &usec
);
705 return log_error_errno(r
, "Failed to determine timestamp: %m");
706 if (usec
> arg_until
)
710 if (arg_since
!= USEC_INFINITY
&& arg_reverse
) {
713 r
= sd_journal_get_realtime_usec(j
, &usec
);
715 return log_error_errno(r
, "Failed to determine timestamp: %m");
716 if (usec
< arg_since
)
720 r
= print_entry(j
, n_found
++, verb_is_info
);
725 if (!arg_field
&& n_found
<= 0) {
727 log_notice("No coredumps found.");
735 static int save_core(sd_journal
*j
, FILE *file
, char **path
, bool *unlink_temp
) {
737 _cleanup_free_
char *filename
= NULL
;
740 _cleanup_close_
int fdt
= -1;
743 assert(!(file
&& path
)); /* At most one can be specified */
744 assert(!!path
== !!unlink_temp
); /* Those must be specified together */
746 /* Look for a coredump on disk first. */
747 r
= sd_journal_get_data(j
, "COREDUMP_FILENAME", (const void**) &data
, &len
);
749 retrieve(data
, len
, "COREDUMP_FILENAME", &filename
);
752 return log_error_errno(r
, "Failed to retrieve COREDUMP_FILENAME field: %m");
753 /* Check that we can have a COREDUMP field. We still haven't set a high
754 * data threshold, so we'll get a few kilobytes at most.
757 r
= sd_journal_get_data(j
, "COREDUMP", (const void**) &data
, &len
);
759 return log_error_errno(r
, "Coredump entry has no core attached (neither internally in the journal nor externally on disk).");
761 return log_error_errno(r
, "Failed to retrieve COREDUMP field: %m");
765 if (access(filename
, R_OK
) < 0)
766 return log_error_errno(errno
, "File \"%s\" is not readable: %m", filename
);
768 if (path
&& !endswith(filename
, ".xz") && !endswith(filename
, ".lz4")) {
769 *path
= TAKE_PTR(filename
);
778 /* Create a temporary file to write the uncompressed core to. */
780 r
= var_tmp_dir(&vt
);
782 return log_error_errno(r
, "Failed to acquire temporary directory path: %m");
784 temp
= strjoin(vt
, "/coredump-XXXXXX");
788 fdt
= mkostemp_safe(temp
);
790 return log_error_errno(fdt
, "Failed to create temporary file: %m");
791 log_debug("Created temporary file %s", temp
);
795 /* If neither path or file are specified, we will write to stdout. Let's now check
796 * if stdout is connected to a tty. We checked that the file exists, or that the
797 * core might be stored in the journal. In this second case, if we found the entry,
798 * in all likelyhood we will be able to access the COREDUMP= field. In either case,
799 * we stop before doing any "real" work, i.e. before starting decompression or
800 * reading from the file or creating temporary files.
804 return log_error_errno(ENOTTY
, "Refusing to dump core to tty"
805 " (use shell redirection or specify --output).");
813 #if HAVE_XZ || HAVE_LZ4
814 _cleanup_close_
int fdf
;
816 fdf
= open(filename
, O_RDONLY
| O_CLOEXEC
);
818 r
= log_error_errno(errno
, "Failed to open %s: %m", filename
);
822 r
= decompress_stream(filename
, fdf
, fd
, -1);
824 log_error_errno(r
, "Failed to decompress %s: %m", filename
);
828 log_error("Cannot decompress file. Compiled without compression support.");
835 /* We want full data, nothing truncated. */
836 sd_journal_set_data_threshold(j
, 0);
838 r
= sd_journal_get_data(j
, "COREDUMP", (const void**) &data
, &len
);
840 return log_error_errno(r
, "Failed to retrieve COREDUMP field: %m");
846 sz
= write(fd
, data
, len
);
848 r
= log_error_errno(errno
, "Failed to write output: %m");
851 if (sz
!= (ssize_t
) len
) {
852 log_error("Short write to output.");
867 log_debug("Removed temporary file %s", temp
);
872 static int dump_core(int argc
, char **argv
, void *userdata
) {
873 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
877 log_error("Option --field/-F only makes sense with list");
881 r
= acquire_journal(&j
, argv
+ 1);
889 print_info(arg_output
? stdout
: stderr
, j
, false);
891 r
= save_core(j
, arg_output
, NULL
, NULL
);
895 r
= sd_journal_previous(j
);
896 if (r
> 0 && !arg_quiet
)
897 log_notice("More than one entry matches, ignoring rest.");
902 static int run_debug(int argc
, char **argv
, void *userdata
) {
903 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
904 _cleanup_free_
char *exe
= NULL
, *path
= NULL
;
905 bool unlink_path
= false;
906 const char *data
, *fork_name
;
914 env_debugger
= getenv("SYSTEMD_DEBUGGER");
916 arg_debugger
= env_debugger
;
918 arg_debugger
= "gdb";
922 log_error("Option --field/-F only makes sense with list");
926 r
= acquire_journal(&j
, argv
+ 1);
934 print_info(stdout
, j
, false);
937 r
= sd_journal_get_data(j
, "COREDUMP_EXE", (const void**) &data
, &len
);
939 return log_error_errno(r
, "Failed to retrieve COREDUMP_EXE field: %m");
941 assert(len
> STRLEN("COREDUMP_EXE="));
942 data
+= STRLEN("COREDUMP_EXE=");
943 len
-= STRLEN("COREDUMP_EXE=");
945 exe
= strndup(data
, len
);
949 if (endswith(exe
, " (deleted)")) {
950 log_error("Binary already deleted.");
954 if (!path_is_absolute(exe
)) {
955 log_error("Binary is not an absolute path.");
959 r
= save_core(j
, NULL
, &path
, &unlink_path
);
963 /* Don't interfere with gdb and its handling of SIGINT. */
964 (void) ignore_signals(SIGINT
, -1);
966 fork_name
= strjoina("(", arg_debugger
, ")");
968 r
= safe_fork(fork_name
, FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_CLOSE_ALL_FDS
|FORK_LOG
, &pid
);
972 execlp(arg_debugger
, arg_debugger
, exe
, "-c", path
, NULL
);
974 log_error_errno(errno
, "Failed to invoke %s: %m", arg_debugger
);
978 r
= wait_for_terminate_and_check(arg_debugger
, pid
, WAIT_LOG_ABNORMAL
);
981 (void) default_signals(SIGINT
, -1);
984 log_debug("Removed temporary file %s", path
);
991 static int check_units_active(void) {
992 _cleanup_(sd_bus_unrefp
) sd_bus
*bus
= NULL
;
993 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
994 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
995 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
997 const char *id
, *state
, *substate
;
1002 r
= sd_bus_default_system(&bus
);
1004 return log_error_errno(r
, "Failed to acquire bus: %m");
1006 r
= sd_bus_message_new_method_call(
1009 "org.freedesktop.systemd1",
1010 "/org/freedesktop/systemd1",
1011 "org.freedesktop.systemd1.Manager",
1012 "ListUnitsByPatterns");
1014 return bus_log_create_error(r
);
1016 r
= sd_bus_message_append_strv(m
, NULL
);
1018 return bus_log_create_error(r
);
1020 r
= sd_bus_message_append_strv(m
, STRV_MAKE("systemd-coredump@*.service"));
1022 return bus_log_create_error(r
);
1024 r
= sd_bus_call(bus
, m
, SHORT_BUS_CALL_TIMEOUT_USEC
, &error
, &reply
);
1026 return log_error_errno(r
, "Failed to check if any systemd-coredump@.service units are running: %s",
1027 bus_error_message(&error
, r
));
1029 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ssssssouso)");
1031 return bus_log_parse_error(r
);
1033 while ((r
= sd_bus_message_read(
1034 reply
, "(ssssssouso)",
1035 &id
, NULL
, NULL
, &state
, &substate
,
1036 NULL
, NULL
, NULL
, NULL
, NULL
)) > 0) {
1037 bool found
= !STR_IN_SET(state
, "inactive", "dead", "failed");
1038 log_debug("Unit %s is %s/%s, %scounting it.", id
, state
, substate
, found
? "" : "not ");
1042 return bus_log_parse_error(r
);
1044 r
= sd_bus_message_exit_container(reply
);
1046 return bus_log_parse_error(r
);
1051 static int coredumpctl_main(int argc
, char *argv
[]) {
1053 static const Verb verbs
[] = {
1054 { "list", VERB_ANY
, VERB_ANY
, VERB_DEFAULT
, dump_list
},
1055 { "info", VERB_ANY
, VERB_ANY
, 0, dump_list
},
1056 { "dump", VERB_ANY
, VERB_ANY
, 0, dump_core
},
1057 { "debug", VERB_ANY
, VERB_ANY
, 0, run_debug
},
1058 { "gdb", VERB_ANY
, VERB_ANY
, 0, run_debug
},
1062 return dispatch_verb(argc
, argv
, verbs
, NULL
);
1065 int main(int argc
, char *argv
[]) {
1066 int r
, units_active
;
1068 setlocale(LC_ALL
, "");
1069 log_parse_environment();
1072 /* The journal merging logic potentially needs a lot of fds. */
1073 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE
);
1075 r
= parse_argv(argc
, argv
);
1081 units_active
= check_units_active(); /* error is treated the same as 0 */
1083 r
= coredumpctl_main(argc
, argv
);
1085 if (units_active
> 0)
1086 printf("%s-- Notice: %d systemd-coredump@.service %s, output may be incomplete.%s\n",
1087 ansi_highlight_red(),
1088 units_active
, units_active
== 1 ? "unit is running" : "units are running",
1093 safe_fclose(arg_output
);
1095 return r
>= 0 ? r
: EXIT_FAILURE
;