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"
26 #include "parse-util.h"
27 #include "path-util.h"
28 #include "process-util.h"
30 #include "signal-util.h"
31 #include "string-util.h"
33 #include "terminal-util.h"
34 #include "user-util.h"
38 #define SHORT_BUS_CALL_TIMEOUT_USEC (3 * USEC_PER_SEC)
40 static usec_t arg_since
= USEC_INFINITY
, arg_until
= USEC_INFINITY
;
41 static const char* arg_field
= NULL
;
42 static const char *arg_debugger
= NULL
;
43 static const char *arg_directory
= NULL
;
44 static bool arg_no_pager
= false;
45 static int arg_no_legend
= false;
46 static int arg_one
= false;
47 static FILE* arg_output
= NULL
;
48 static bool arg_reverse
= false;
49 static bool arg_quiet
= false;
51 static int add_match(sd_journal
*j
, const char *match
) {
52 _cleanup_free_
char *p
= NULL
;
53 const char* prefix
, *pattern
;
57 if (strchr(match
, '='))
59 else if (strchr(match
, '/')) {
60 r
= path_make_absolute_cwd(match
, &p
);
62 return log_error_errno(r
, "path_make_absolute_cwd(\"%s\"): %m", match
);
65 prefix
= "COREDUMP_EXE=";
66 } else if (parse_pid(match
, &pid
) >= 0)
67 prefix
= "COREDUMP_PID=";
69 prefix
= "COREDUMP_COMM=";
71 pattern
= strjoina(prefix
, match
);
72 log_debug("Adding match: %s", pattern
);
73 r
= sd_journal_add_match(j
, pattern
, 0);
75 return log_error_errno(r
, "Failed to add match \"%s\": %m", match
);
80 static int add_matches(sd_journal
*j
, char **matches
) {
84 r
= sd_journal_add_match(j
, "MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR
, 0);
86 return log_error_errno(r
, "Failed to add match \"%s\": %m", "MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR
);
88 r
= sd_journal_add_match(j
, "MESSAGE_ID=" SD_MESSAGE_BACKTRACE_STR
, 0);
90 return log_error_errno(r
, "Failed to add match \"%s\": %m", "MESSAGE_ID=" SD_MESSAGE_BACKTRACE_STR
);
92 STRV_FOREACH(match
, matches
) {
93 r
= add_match(j
, *match
);
101 static int acquire_journal(sd_journal
**ret
, char **matches
) {
102 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
108 r
= sd_journal_open_directory(&j
, arg_directory
, 0);
110 return log_error_errno(r
, "Failed to open journals in directory: %s: %m", arg_directory
);
112 r
= sd_journal_open(&j
, SD_JOURNAL_LOCAL_ONLY
);
114 return log_error_errno(r
, "Failed to open journal: %m");
117 r
= journal_access_check_and_warn(j
, arg_quiet
, true);
121 r
= add_matches(j
, matches
);
126 _cleanup_free_
char *filter
;
128 filter
= journal_make_match_string(j
);
129 log_debug("Journal filter: %s", filter
);
137 static int help(void) {
138 printf("%s [OPTIONS...]\n\n"
139 "List or retrieve coredumps from the journal.\n\n"
141 " -h --help Show this help\n"
142 " --version Print version string\n"
143 " --no-pager Do not pipe output into a pager\n"
144 " --no-legend Do not print the column headers\n"
145 " --debugger=DEBUGGER Use the given debugger\n"
146 " -1 Show information about most recent entry only\n"
147 " -S --since=DATE Only print coredumps since the date\n"
148 " -U --until=DATE Only print coredumps until the date\n"
149 " -r --reverse Show the newest entries first\n"
150 " -F --field=FIELD List all values a certain field takes\n"
151 " -o --output=FILE Write output to FILE\n"
152 " -D --directory=DIR Use journal files from directory\n\n"
153 " -q --quiet Do not show info messages and privilege warning\n"
155 " list [MATCHES...] List available coredumps (default)\n"
156 " info [MATCHES...] Show detailed information about one or more coredumps\n"
157 " dump [MATCHES...] Print first matching coredump to stdout\n"
158 " debug [MATCHES...] Start a debugger for the first matching coredump\n"
159 , program_invocation_short_name
);
164 static int parse_argv(int argc
, char *argv
[]) {
174 static const struct option options
[] = {
175 { "help", no_argument
, NULL
, 'h' },
176 { "version" , no_argument
, NULL
, ARG_VERSION
},
177 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
178 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
179 { "debugger", required_argument
, NULL
, ARG_DEBUGGER
},
180 { "output", required_argument
, NULL
, 'o' },
181 { "field", required_argument
, NULL
, 'F' },
182 { "directory", required_argument
, NULL
, 'D' },
183 { "reverse", no_argument
, NULL
, 'r' },
184 { "since", required_argument
, NULL
, 'S' },
185 { "until", required_argument
, NULL
, 'U' },
186 { "quiet", no_argument
, NULL
, 'q' },
193 while ((c
= getopt_long(argc
, argv
, "ho:F:1D:rS:U:q", options
, NULL
)) >= 0)
206 arg_no_legend
= true;
210 arg_debugger
= optarg
;
215 log_error("Cannot set output more than once.");
219 arg_output
= fopen(optarg
, "we");
221 return log_error_errno(errno
, "writing to '%s': %m", optarg
);
226 r
= parse_timestamp(optarg
, &arg_since
);
228 return log_error_errno(r
, "Failed to parse timestamp: %s", optarg
);
232 r
= parse_timestamp(optarg
, &arg_until
);
234 return log_error_errno(r
, "Failed to parse timestamp: %s", optarg
);
239 log_error("Cannot use --field/-F more than once.");
250 arg_directory
= optarg
;
265 assert_not_reached("Unhandled option");
268 if (arg_since
!= USEC_INFINITY
&& arg_until
!= USEC_INFINITY
&&
269 arg_since
> arg_until
) {
270 log_error("--since= must be before --until=.");
277 static int retrieve(const void *data
,
285 ident
= strlen(name
) + 1; /* name + "=" */
290 if (memcmp(data
, name
, ident
- 1) != 0)
293 if (((const char*) data
)[ident
- 1] != '=')
296 v
= strndup((const char*)data
+ ident
, len
- ident
);
306 static int print_field(FILE* file
, sd_journal
*j
) {
315 /* A (user-specified) field may appear more than once for a given entry.
316 * We will print all of the occurences.
317 * This is different below for fields that systemd-coredump uses,
318 * because they cannot meaningfully appear more than once.
320 SD_JOURNAL_FOREACH_DATA(j
, d
, l
) {
321 _cleanup_free_
char *value
= NULL
;
324 r
= retrieve(d
, l
, arg_field
, &value
);
328 fprintf(file
, "%s\n", value
);
334 #define RETRIEVE(d, l, name, arg) \
336 int _r = retrieve(d, l, name, &arg); \
343 static int print_list(FILE* file
, sd_journal
*j
, int had_legend
) {
345 *mid
= NULL
, *pid
= NULL
, *uid
= NULL
, *gid
= NULL
,
346 *sgnl
= NULL
, *exe
= NULL
, *comm
= NULL
, *cmdline
= NULL
,
347 *filename
= NULL
, *truncated
= NULL
, *coredump
= NULL
;
351 char buf
[FORMAT_TIMESTAMP_MAX
];
354 bool normal_coredump
;
359 SD_JOURNAL_FOREACH_DATA(j
, d
, l
) {
360 RETRIEVE(d
, l
, "MESSAGE_ID", mid
);
361 RETRIEVE(d
, l
, "COREDUMP_PID", pid
);
362 RETRIEVE(d
, l
, "COREDUMP_UID", uid
);
363 RETRIEVE(d
, l
, "COREDUMP_GID", gid
);
364 RETRIEVE(d
, l
, "COREDUMP_SIGNAL", sgnl
);
365 RETRIEVE(d
, l
, "COREDUMP_EXE", exe
);
366 RETRIEVE(d
, l
, "COREDUMP_COMM", comm
);
367 RETRIEVE(d
, l
, "COREDUMP_CMDLINE", cmdline
);
368 RETRIEVE(d
, l
, "COREDUMP_FILENAME", filename
);
369 RETRIEVE(d
, l
, "COREDUMP_TRUNCATED", truncated
);
370 RETRIEVE(d
, l
, "COREDUMP", coredump
);
373 if (!pid
&& !uid
&& !gid
&& !sgnl
&& !exe
&& !comm
&& !cmdline
&& !filename
) {
374 log_warning("Empty coredump log entry");
378 r
= sd_journal_get_realtime_usec(j
, &t
);
380 return log_error_errno(r
, "Failed to get realtime timestamp: %m");
382 format_timestamp(buf
, sizeof(buf
), t
);
384 if (!had_legend
&& !arg_no_legend
)
385 fprintf(file
, "%-*s %*s %*s %*s %*s %-*s %s\n",
386 FORMAT_TIMESTAMP_WIDTH
, "TIME",
394 normal_coredump
= streq_ptr(mid
, SD_MESSAGE_COREDUMP_STR
);
397 if (access(filename
, R_OK
) == 0)
399 else if (errno
== ENOENT
)
405 else if (normal_coredump
)
410 if (STR_IN_SET(present
, "present", "journal") && truncated
&& parse_boolean(truncated
) > 0)
411 present
= "truncated";
413 fprintf(file
, "%-*s %*s %*s %*s %*s %-*s %s\n",
414 FORMAT_TIMESTAMP_WIDTH
, buf
,
418 3, normal_coredump
? strna(sgnl
) : "-",
420 strna(exe
?: (comm
?: cmdline
)));
425 static int print_info(FILE *file
, sd_journal
*j
, bool need_space
) {
427 *mid
= NULL
, *pid
= NULL
, *uid
= NULL
, *gid
= NULL
,
428 *sgnl
= NULL
, *exe
= NULL
, *comm
= NULL
, *cmdline
= NULL
,
429 *unit
= NULL
, *user_unit
= NULL
, *session
= NULL
,
430 *boot_id
= NULL
, *machine_id
= NULL
, *hostname
= NULL
,
431 *slice
= NULL
, *cgroup
= NULL
, *owner_uid
= NULL
,
432 *message
= NULL
, *timestamp
= NULL
, *filename
= NULL
,
433 *truncated
= NULL
, *coredump
= NULL
;
436 bool normal_coredump
;
442 SD_JOURNAL_FOREACH_DATA(j
, d
, l
) {
443 RETRIEVE(d
, l
, "MESSAGE_ID", mid
);
444 RETRIEVE(d
, l
, "COREDUMP_PID", pid
);
445 RETRIEVE(d
, l
, "COREDUMP_UID", uid
);
446 RETRIEVE(d
, l
, "COREDUMP_GID", gid
);
447 RETRIEVE(d
, l
, "COREDUMP_SIGNAL", sgnl
);
448 RETRIEVE(d
, l
, "COREDUMP_EXE", exe
);
449 RETRIEVE(d
, l
, "COREDUMP_COMM", comm
);
450 RETRIEVE(d
, l
, "COREDUMP_CMDLINE", cmdline
);
451 RETRIEVE(d
, l
, "COREDUMP_UNIT", unit
);
452 RETRIEVE(d
, l
, "COREDUMP_USER_UNIT", user_unit
);
453 RETRIEVE(d
, l
, "COREDUMP_SESSION", session
);
454 RETRIEVE(d
, l
, "COREDUMP_OWNER_UID", owner_uid
);
455 RETRIEVE(d
, l
, "COREDUMP_SLICE", slice
);
456 RETRIEVE(d
, l
, "COREDUMP_CGROUP", cgroup
);
457 RETRIEVE(d
, l
, "COREDUMP_TIMESTAMP", timestamp
);
458 RETRIEVE(d
, l
, "COREDUMP_FILENAME", filename
);
459 RETRIEVE(d
, l
, "COREDUMP_TRUNCATED", truncated
);
460 RETRIEVE(d
, l
, "COREDUMP", coredump
);
461 RETRIEVE(d
, l
, "_BOOT_ID", boot_id
);
462 RETRIEVE(d
, l
, "_MACHINE_ID", machine_id
);
463 RETRIEVE(d
, l
, "_HOSTNAME", hostname
);
464 RETRIEVE(d
, l
, "MESSAGE", message
);
470 normal_coredump
= streq_ptr(mid
, SD_MESSAGE_COREDUMP_STR
);
474 " PID: %s%s%s (%s)\n",
475 ansi_highlight(), strna(pid
), ansi_normal(), comm
);
479 ansi_highlight(), strna(pid
), ansi_normal());
484 if (parse_uid(uid
, &n
) >= 0) {
485 _cleanup_free_
char *u
= NULL
;
501 if (parse_gid(gid
, &n
) >= 0) {
502 _cleanup_free_
char *g
= NULL
;
517 const char *name
= normal_coredump
? "Signal" : "Reason";
519 if (normal_coredump
&& safe_atoi(sgnl
, &sig
) >= 0)
520 fprintf(file
, " %s: %s (%s)\n", name
, sgnl
, signal_to_string(sig
));
522 fprintf(file
, " %s: %s\n", name
, sgnl
);
528 r
= safe_atou64(timestamp
, &u
);
530 char absolute
[FORMAT_TIMESTAMP_MAX
], relative
[FORMAT_TIMESPAN_MAX
];
533 " Timestamp: %s (%s)\n",
534 format_timestamp(absolute
, sizeof(absolute
), u
),
535 format_timestamp_relative(relative
, sizeof(relative
), u
));
538 fprintf(file
, " Timestamp: %s\n", timestamp
);
542 fprintf(file
, " Command Line: %s\n", cmdline
);
544 fprintf(file
, " Executable: %s%s%s\n", ansi_highlight(), exe
, ansi_normal());
546 fprintf(file
, " Control Group: %s\n", cgroup
);
548 fprintf(file
, " Unit: %s\n", unit
);
550 fprintf(file
, " User Unit: %s\n", user_unit
);
552 fprintf(file
, " Slice: %s\n", slice
);
554 fprintf(file
, " Session: %s\n", session
);
558 if (parse_uid(owner_uid
, &n
) >= 0) {
559 _cleanup_free_
char *u
= NULL
;
563 " Owner UID: %s (%s)\n",
572 fprintf(file
, " Boot ID: %s\n", boot_id
);
574 fprintf(file
, " Machine ID: %s\n", machine_id
);
576 fprintf(file
, " Hostname: %s\n", hostname
);
581 inacc
= access(filename
, R_OK
) < 0;
582 trunc
= truncated
&& parse_boolean(truncated
) > 0;
585 fprintf(file
, " Storage: %s%s (%s%s%s)%s\n",
586 ansi_highlight_red(),
588 inacc
? "inaccessible" : "",
589 inacc
&& trunc
? ", " : "",
590 trunc
? "truncated" : "",
593 fprintf(file
, " Storage: %s\n", filename
);
597 fprintf(file
, " Storage: journal\n");
599 fprintf(file
, " Storage: none\n");
602 _cleanup_free_
char *m
= NULL
;
604 m
= strreplace(message
, "\n", "\n ");
606 fprintf(file
, " Message: %s\n", strstrip(m
?: message
));
612 static int focus(sd_journal
*j
) {
615 r
= sd_journal_seek_tail(j
);
617 r
= sd_journal_previous(j
);
619 return log_error_errno(r
, "Failed to search journal: %m");
621 log_error("No match found.");
627 static int print_entry(sd_journal
*j
, unsigned n_found
, bool verb_is_info
) {
631 return print_info(stdout
, j
, n_found
);
633 return print_field(stdout
, j
);
635 return print_list(stdout
, j
, n_found
);
638 static int dump_list(int argc
, char **argv
, void *userdata
) {
639 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
640 unsigned n_found
= 0;
644 verb_is_info
= (argc
>= 1 && streq(argv
[0], "info"));
646 r
= acquire_journal(&j
, argv
+ 1);
650 (void) pager_open(arg_no_pager
, false);
652 /* The coredumps are likely to compressed, and for just
653 * listing them we don't need to decompress them, so let's
654 * pick a fairly low data threshold here */
655 sd_journal_set_data_threshold(j
, 4096);
657 /* "info" without pattern implies "-1" */
658 if (arg_one
|| (verb_is_info
&& argc
== 1)) {
663 return print_entry(j
, 0, verb_is_info
);
665 if (arg_since
!= USEC_INFINITY
&& !arg_reverse
)
666 r
= sd_journal_seek_realtime_usec(j
, arg_since
);
667 else if (arg_until
!= USEC_INFINITY
&& arg_reverse
)
668 r
= sd_journal_seek_realtime_usec(j
, arg_until
);
669 else if (arg_reverse
)
670 r
= sd_journal_seek_tail(j
);
672 r
= sd_journal_seek_head(j
);
674 return log_error_errno(r
, "Failed to seek to date: %m");
678 r
= sd_journal_next(j
);
680 r
= sd_journal_previous(j
);
683 return log_error_errno(r
, "Failed to iterate through journal: %m");
688 if (arg_until
!= USEC_INFINITY
&& !arg_reverse
) {
691 r
= sd_journal_get_realtime_usec(j
, &usec
);
693 return log_error_errno(r
, "Failed to determine timestamp: %m");
694 if (usec
> arg_until
)
698 if (arg_since
!= USEC_INFINITY
&& arg_reverse
) {
701 r
= sd_journal_get_realtime_usec(j
, &usec
);
703 return log_error_errno(r
, "Failed to determine timestamp: %m");
704 if (usec
< arg_since
)
708 r
= print_entry(j
, n_found
++, verb_is_info
);
713 if (!arg_field
&& n_found
<= 0) {
715 log_notice("No coredumps found.");
723 static int save_core(sd_journal
*j
, FILE *file
, char **path
, bool *unlink_temp
) {
725 _cleanup_free_
char *filename
= NULL
;
728 _cleanup_close_
int fdt
= -1;
731 assert(!(file
&& path
)); /* At most one can be specified */
732 assert(!!path
== !!unlink_temp
); /* Those must be specified together */
734 /* Look for a coredump on disk first. */
735 r
= sd_journal_get_data(j
, "COREDUMP_FILENAME", (const void**) &data
, &len
);
737 retrieve(data
, len
, "COREDUMP_FILENAME", &filename
);
740 return log_error_errno(r
, "Failed to retrieve COREDUMP_FILENAME field: %m");
741 /* Check that we can have a COREDUMP field. We still haven't set a high
742 * data threshold, so we'll get a few kilobytes at most.
745 r
= sd_journal_get_data(j
, "COREDUMP", (const void**) &data
, &len
);
747 return log_error_errno(r
, "Coredump entry has no core attached (neither internally in the journal nor externally on disk).");
749 return log_error_errno(r
, "Failed to retrieve COREDUMP field: %m");
753 if (access(filename
, R_OK
) < 0)
754 return log_error_errno(errno
, "File \"%s\" is not readable: %m", filename
);
756 if (path
&& !endswith(filename
, ".xz") && !endswith(filename
, ".lz4")) {
757 *path
= TAKE_PTR(filename
);
766 /* Create a temporary file to write the uncompressed core to. */
768 r
= var_tmp_dir(&vt
);
770 return log_error_errno(r
, "Failed to acquire temporary directory path: %m");
772 temp
= strjoin(vt
, "/coredump-XXXXXX");
776 fdt
= mkostemp_safe(temp
);
778 return log_error_errno(fdt
, "Failed to create temporary file: %m");
779 log_debug("Created temporary file %s", temp
);
783 /* If neither path or file are specified, we will write to stdout. Let's now check
784 * if stdout is connected to a tty. We checked that the file exists, or that the
785 * core might be stored in the journal. In this second case, if we found the entry,
786 * in all likelyhood we will be able to access the COREDUMP= field. In either case,
787 * we stop before doing any "real" work, i.e. before starting decompression or
788 * reading from the file or creating temporary files.
792 return log_error_errno(ENOTTY
, "Refusing to dump core to tty"
793 " (use shell redirection or specify --output).");
801 #if HAVE_XZ || HAVE_LZ4
802 _cleanup_close_
int fdf
;
804 fdf
= open(filename
, O_RDONLY
| O_CLOEXEC
);
806 r
= log_error_errno(errno
, "Failed to open %s: %m", filename
);
810 r
= decompress_stream(filename
, fdf
, fd
, -1);
812 log_error_errno(r
, "Failed to decompress %s: %m", filename
);
816 log_error("Cannot decompress file. Compiled without compression support.");
823 /* We want full data, nothing truncated. */
824 sd_journal_set_data_threshold(j
, 0);
826 r
= sd_journal_get_data(j
, "COREDUMP", (const void**) &data
, &len
);
828 return log_error_errno(r
, "Failed to retrieve COREDUMP field: %m");
834 sz
= write(fd
, data
, len
);
836 r
= log_error_errno(errno
, "Failed to write output: %m");
839 if (sz
!= (ssize_t
) len
) {
840 log_error("Short write to output.");
855 log_debug("Removed temporary file %s", temp
);
860 static int dump_core(int argc
, char **argv
, void *userdata
) {
861 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
865 log_error("Option --field/-F only makes sense with list");
869 r
= acquire_journal(&j
, argv
+ 1);
877 print_info(arg_output
? stdout
: stderr
, j
, false);
879 r
= save_core(j
, arg_output
, NULL
, NULL
);
883 r
= sd_journal_previous(j
);
884 if (r
> 0 && !arg_quiet
)
885 log_notice("More than one entry matches, ignoring rest.");
890 static int run_debug(int argc
, char **argv
, void *userdata
) {
891 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
892 _cleanup_free_
char *exe
= NULL
, *path
= NULL
;
893 bool unlink_path
= false;
894 const char *data
, *fork_name
;
902 env_debugger
= getenv("SYSTEMD_DEBUGGER");
904 arg_debugger
= env_debugger
;
906 arg_debugger
= "gdb";
910 log_error("Option --field/-F only makes sense with list");
914 r
= acquire_journal(&j
, argv
+ 1);
922 print_info(stdout
, j
, false);
925 r
= sd_journal_get_data(j
, "COREDUMP_EXE", (const void**) &data
, &len
);
927 return log_error_errno(r
, "Failed to retrieve COREDUMP_EXE field: %m");
929 assert(len
> STRLEN("COREDUMP_EXE="));
930 data
+= STRLEN("COREDUMP_EXE=");
931 len
-= STRLEN("COREDUMP_EXE=");
933 exe
= strndup(data
, len
);
937 if (endswith(exe
, " (deleted)")) {
938 log_error("Binary already deleted.");
942 if (!path_is_absolute(exe
)) {
943 log_error("Binary is not an absolute path.");
947 r
= save_core(j
, NULL
, &path
, &unlink_path
);
951 /* Don't interfere with gdb and its handling of SIGINT. */
952 (void) ignore_signals(SIGINT
, -1);
954 fork_name
= strjoina("(", arg_debugger
, ")");
956 r
= safe_fork(fork_name
, FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_CLOSE_ALL_FDS
|FORK_LOG
, &pid
);
960 execlp(arg_debugger
, arg_debugger
, exe
, "-c", path
, NULL
);
962 log_error_errno(errno
, "Failed to invoke %s: %m", arg_debugger
);
966 r
= wait_for_terminate_and_check(arg_debugger
, pid
, WAIT_LOG_ABNORMAL
);
969 (void) default_signals(SIGINT
, -1);
972 log_debug("Removed temporary file %s", path
);
979 static int check_units_active(void) {
980 _cleanup_(sd_bus_unrefp
) sd_bus
*bus
= NULL
;
981 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
982 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
983 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
985 const char *id
, *state
, *substate
;
990 r
= sd_bus_default_system(&bus
);
992 return log_error_errno(r
, "Failed to acquire bus: %m");
994 r
= sd_bus_message_new_method_call(
997 "org.freedesktop.systemd1",
998 "/org/freedesktop/systemd1",
999 "org.freedesktop.systemd1.Manager",
1000 "ListUnitsByPatterns");
1002 return bus_log_create_error(r
);
1004 r
= sd_bus_message_append_strv(m
, NULL
);
1006 return bus_log_create_error(r
);
1008 r
= sd_bus_message_append_strv(m
, STRV_MAKE("systemd-coredump@*.service"));
1010 return bus_log_create_error(r
);
1012 r
= sd_bus_call(bus
, m
, SHORT_BUS_CALL_TIMEOUT_USEC
, &error
, &reply
);
1014 return log_error_errno(r
, "Failed to check if any systemd-coredump@.service units are running: %s",
1015 bus_error_message(&error
, r
));
1017 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ssssssouso)");
1019 return bus_log_parse_error(r
);
1021 while ((r
= sd_bus_message_read(
1022 reply
, "(ssssssouso)",
1023 &id
, NULL
, NULL
, &state
, &substate
,
1024 NULL
, NULL
, NULL
, NULL
, NULL
)) > 0) {
1025 bool found
= !STR_IN_SET(state
, "inactive", "dead", "failed");
1026 log_debug("Unit %s is %s/%s, %scounting it.", id
, state
, substate
, found
? "" : "not ");
1030 return bus_log_parse_error(r
);
1032 r
= sd_bus_message_exit_container(reply
);
1034 return bus_log_parse_error(r
);
1039 static int coredumpctl_main(int argc
, char *argv
[]) {
1041 static const Verb verbs
[] = {
1042 { "list", VERB_ANY
, VERB_ANY
, VERB_DEFAULT
, dump_list
},
1043 { "info", VERB_ANY
, VERB_ANY
, 0, dump_list
},
1044 { "dump", VERB_ANY
, VERB_ANY
, 0, dump_core
},
1045 { "debug", VERB_ANY
, VERB_ANY
, 0, run_debug
},
1046 { "gdb", VERB_ANY
, VERB_ANY
, 0, run_debug
},
1050 return dispatch_verb(argc
, argv
, verbs
, NULL
);
1053 int main(int argc
, char *argv
[]) {
1054 int r
, units_active
;
1056 setlocale(LC_ALL
, "");
1057 log_parse_environment();
1060 r
= parse_argv(argc
, argv
);
1066 units_active
= check_units_active(); /* error is treated the same as 0 */
1068 r
= coredumpctl_main(argc
, argv
);
1070 if (units_active
> 0)
1071 printf("%s-- Notice: %d systemd-coredump@.service %s, output may be incomplete.%s\n",
1072 ansi_highlight_red(),
1073 units_active
, units_active
== 1 ? "unit is running" : "units are running",
1078 safe_fclose(arg_output
);
1080 return r
>= 0 ? r
: EXIT_FAILURE
;