2 This file is part of systemd.
4 Copyright 2012 Zbigniew Jędrzejewski-Szmek
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "sd-journal.h"
29 #include "sd-messages.h"
31 #include "alloc-util.h"
32 #include "bus-error.h"
38 #include "journal-internal.h"
39 #include "journal-util.h"
43 #include "parse-util.h"
44 #include "path-util.h"
45 #include "process-util.h"
47 #include "signal-util.h"
48 #include "string-util.h"
50 #include "terminal-util.h"
51 #include "user-util.h"
54 #define SHORT_BUS_CALL_TIMEOUT_USEC (3 * USEC_PER_SEC)
56 static usec_t arg_since
= USEC_INFINITY
, arg_until
= USEC_INFINITY
;
64 } arg_action
= ACTION_LIST
;
65 static const char* arg_field
= NULL
;
66 static const char *arg_directory
= NULL
;
67 static bool arg_no_pager
= false;
68 static int arg_no_legend
= false;
69 static int arg_one
= false;
70 static FILE* arg_output
= NULL
;
71 static bool arg_reverse
= false;
72 static char** arg_matches
= NULL
;
73 static bool arg_quiet
= false;
75 static int add_match(sd_journal
*j
, const char *match
) {
76 _cleanup_free_
char *p
= NULL
;
82 if (strchr(match
, '='))
84 else if (strchr(match
, '/')) {
85 r
= path_make_absolute_cwd(match
, &p
);
87 return log_error_errno(r
, "path_make_absolute_cwd(\"%s\"): %m", match
);
90 prefix
= "COREDUMP_EXE=";
91 } else if (parse_pid(match
, &pid
) >= 0)
92 prefix
= "COREDUMP_PID=";
94 prefix
= "COREDUMP_COMM=";
96 pattern
= strjoin(prefix
, match
);
100 log_debug("Adding match: %s", pattern
);
101 r
= sd_journal_add_match(j
, pattern
, 0);
103 return log_error_errno(r
, "Failed to add match \"%s\": %m", match
);
107 static int add_matches(sd_journal
*j
) {
111 r
= sd_journal_add_match(j
, "MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR
, 0);
113 return log_error_errno(r
, "Failed to add match \"%s\": %m", "MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR
);
115 r
= sd_journal_add_match(j
, "MESSAGE_ID=" SD_MESSAGE_BACKTRACE_STR
, 0);
117 return log_error_errno(r
, "Failed to add match \"%s\": %m", "MESSAGE_ID=" SD_MESSAGE_BACKTRACE_STR
);
119 STRV_FOREACH(match
, arg_matches
) {
120 r
= add_match(j
, *match
);
128 static void help(void) {
129 printf("%s [OPTIONS...]\n\n"
130 "List or retrieve coredumps from the journal.\n\n"
132 " -h --help Show this help\n"
133 " --version Print version string\n"
134 " --no-pager Do not pipe output into a pager\n"
135 " --no-legend Do not print the column headers.\n"
136 " -1 Show information about most recent entry only\n"
137 " -S --since=DATE Only print coredumps since the date\n"
138 " -U --until=DATE Only print coredumps until the date\n"
139 " -r --reverse Show the newest entries first\n"
140 " -F --field=FIELD List all values a certain field takes\n"
141 " -o --output=FILE Write output to FILE\n"
142 " -D --directory=DIR Use journal files from directory\n\n"
143 " -q --quiet Do not show info messages and privilege warning\n"
145 " list [MATCHES...] List available coredumps (default)\n"
146 " info [MATCHES...] Show detailed information about one or more coredumps\n"
147 " dump [MATCHES...] Print first matching coredump to stdout\n"
148 " gdb [MATCHES...] Start gdb for the first matching coredump\n"
149 , program_invocation_short_name
);
152 static int parse_argv(int argc
, char *argv
[]) {
161 static const struct option options
[] = {
162 { "help", no_argument
, NULL
, 'h' },
163 { "version" , no_argument
, NULL
, ARG_VERSION
},
164 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
165 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
166 { "output", required_argument
, NULL
, 'o' },
167 { "field", required_argument
, NULL
, 'F' },
168 { "directory", required_argument
, NULL
, 'D' },
169 { "reverse", no_argument
, NULL
, 'r' },
170 { "since", required_argument
, NULL
, 'S' },
171 { "until", required_argument
, NULL
, 'U' },
172 { "quiet", no_argument
, NULL
, 'q' },
179 while ((c
= getopt_long(argc
, argv
, "ho:F:1D:rS:U:q", options
, NULL
)) >= 0)
182 arg_action
= ACTION_NONE
;
187 arg_action
= ACTION_NONE
;
195 arg_no_legend
= true;
200 log_error("cannot set output more than once");
204 arg_output
= fopen(optarg
, "we");
206 return log_error_errno(errno
, "writing to '%s': %m", optarg
);
211 r
= parse_timestamp(optarg
, &arg_since
);
213 return log_error_errno(r
, "Failed to parse timestamp: %s", optarg
);
217 r
= parse_timestamp(optarg
, &arg_until
);
219 return log_error_errno(r
, "Failed to parse timestamp: %s", optarg
);
224 log_error("cannot use --field/-F more than once");
235 arg_directory
= optarg
;
250 assert_not_reached("Unhandled option");
253 if (arg_since
!= USEC_INFINITY
&& arg_until
!= USEC_INFINITY
&&
254 arg_since
> arg_until
) {
255 log_error("--since= must be before --until=.");
260 const char *cmd
= argv
[optind
++];
261 if (streq(cmd
, "list"))
262 arg_action
= ACTION_LIST
;
263 else if (streq(cmd
, "dump"))
264 arg_action
= ACTION_DUMP
;
265 else if (streq(cmd
, "gdb"))
266 arg_action
= ACTION_GDB
;
267 else if (streq(cmd
, "info"))
268 arg_action
= ACTION_INFO
;
270 log_error("Unknown action '%s'", cmd
);
275 if (arg_field
&& arg_action
!= ACTION_LIST
) {
276 log_error("Option --field/-F only makes sense with list");
281 arg_matches
= argv
+ optind
;
286 static int retrieve(const void *data
,
294 ident
= strlen(name
) + 1; /* name + "=" */
299 if (memcmp(data
, name
, ident
- 1) != 0)
302 if (((const char*) data
)[ident
- 1] != '=')
305 v
= strndup((const char*)data
+ ident
, len
- ident
);
315 static int print_field(FILE* file
, sd_journal
*j
) {
324 /* A (user-specified) field may appear more than once for a given entry.
325 * We will print all of the occurences.
326 * This is different below for fields that systemd-coredump uses,
327 * because they cannot meaningfully appear more than once.
329 SD_JOURNAL_FOREACH_DATA(j
, d
, l
) {
330 _cleanup_free_
char *value
= NULL
;
333 r
= retrieve(d
, l
, arg_field
, &value
);
337 fprintf(file
, "%s\n", value
);
343 #define RETRIEVE(d, l, name, arg) \
345 int _r = retrieve(d, l, name, &arg); \
352 static int print_list(FILE* file
, sd_journal
*j
, int had_legend
) {
354 *mid
= NULL
, *pid
= NULL
, *uid
= NULL
, *gid
= NULL
,
355 *sgnl
= NULL
, *exe
= NULL
, *comm
= NULL
, *cmdline
= NULL
,
356 *filename
= NULL
, *truncated
= NULL
, *coredump
= NULL
;
360 char buf
[FORMAT_TIMESTAMP_MAX
];
363 bool normal_coredump
;
368 SD_JOURNAL_FOREACH_DATA(j
, d
, l
) {
369 RETRIEVE(d
, l
, "MESSAGE_ID", mid
);
370 RETRIEVE(d
, l
, "COREDUMP_PID", pid
);
371 RETRIEVE(d
, l
, "COREDUMP_UID", uid
);
372 RETRIEVE(d
, l
, "COREDUMP_GID", gid
);
373 RETRIEVE(d
, l
, "COREDUMP_SIGNAL", sgnl
);
374 RETRIEVE(d
, l
, "COREDUMP_EXE", exe
);
375 RETRIEVE(d
, l
, "COREDUMP_COMM", comm
);
376 RETRIEVE(d
, l
, "COREDUMP_CMDLINE", cmdline
);
377 RETRIEVE(d
, l
, "COREDUMP_FILENAME", filename
);
378 RETRIEVE(d
, l
, "COREDUMP_TRUNCATED", truncated
);
379 RETRIEVE(d
, l
, "COREDUMP", coredump
);
382 if (!pid
&& !uid
&& !gid
&& !sgnl
&& !exe
&& !comm
&& !cmdline
&& !filename
) {
383 log_warning("Empty coredump log entry");
387 r
= sd_journal_get_realtime_usec(j
, &t
);
389 return log_error_errno(r
, "Failed to get realtime timestamp: %m");
391 format_timestamp(buf
, sizeof(buf
), t
);
393 if (!had_legend
&& !arg_no_legend
)
394 fprintf(file
, "%-*s %*s %*s %*s %*s %-*s %s\n",
395 FORMAT_TIMESTAMP_WIDTH
, "TIME",
403 normal_coredump
= streq_ptr(mid
, SD_MESSAGE_COREDUMP_STR
);
406 if (access(filename
, R_OK
) == 0)
408 else if (errno
== ENOENT
)
414 else if (normal_coredump
)
419 if (STR_IN_SET(present
, "present", "journal") && truncated
&& parse_boolean(truncated
) > 0)
420 present
= "truncated";
422 fprintf(file
, "%-*s %*s %*s %*s %*s %-*s %s\n",
423 FORMAT_TIMESTAMP_WIDTH
, buf
,
427 3, normal_coredump
? strna(sgnl
) : "-",
429 strna(exe
?: (comm
?: cmdline
)));
434 static int print_info(FILE *file
, sd_journal
*j
, bool need_space
) {
436 *mid
= NULL
, *pid
= NULL
, *uid
= NULL
, *gid
= NULL
,
437 *sgnl
= NULL
, *exe
= NULL
, *comm
= NULL
, *cmdline
= NULL
,
438 *unit
= NULL
, *user_unit
= NULL
, *session
= NULL
,
439 *boot_id
= NULL
, *machine_id
= NULL
, *hostname
= NULL
,
440 *slice
= NULL
, *cgroup
= NULL
, *owner_uid
= NULL
,
441 *message
= NULL
, *timestamp
= NULL
, *filename
= NULL
,
442 *truncated
= NULL
, *coredump
= NULL
;
445 bool normal_coredump
;
451 SD_JOURNAL_FOREACH_DATA(j
, d
, l
) {
452 RETRIEVE(d
, l
, "MESSAGE_ID", mid
);
453 RETRIEVE(d
, l
, "COREDUMP_PID", pid
);
454 RETRIEVE(d
, l
, "COREDUMP_UID", uid
);
455 RETRIEVE(d
, l
, "COREDUMP_GID", gid
);
456 RETRIEVE(d
, l
, "COREDUMP_SIGNAL", sgnl
);
457 RETRIEVE(d
, l
, "COREDUMP_EXE", exe
);
458 RETRIEVE(d
, l
, "COREDUMP_COMM", comm
);
459 RETRIEVE(d
, l
, "COREDUMP_CMDLINE", cmdline
);
460 RETRIEVE(d
, l
, "COREDUMP_UNIT", unit
);
461 RETRIEVE(d
, l
, "COREDUMP_USER_UNIT", user_unit
);
462 RETRIEVE(d
, l
, "COREDUMP_SESSION", session
);
463 RETRIEVE(d
, l
, "COREDUMP_OWNER_UID", owner_uid
);
464 RETRIEVE(d
, l
, "COREDUMP_SLICE", slice
);
465 RETRIEVE(d
, l
, "COREDUMP_CGROUP", cgroup
);
466 RETRIEVE(d
, l
, "COREDUMP_TIMESTAMP", timestamp
);
467 RETRIEVE(d
, l
, "COREDUMP_FILENAME", filename
);
468 RETRIEVE(d
, l
, "COREDUMP_TRUNCATED", truncated
);
469 RETRIEVE(d
, l
, "COREDUMP", coredump
);
470 RETRIEVE(d
, l
, "_BOOT_ID", boot_id
);
471 RETRIEVE(d
, l
, "_MACHINE_ID", machine_id
);
472 RETRIEVE(d
, l
, "_HOSTNAME", hostname
);
473 RETRIEVE(d
, l
, "MESSAGE", message
);
479 normal_coredump
= streq_ptr(mid
, SD_MESSAGE_COREDUMP_STR
);
483 " PID: %s%s%s (%s)\n",
484 ansi_highlight(), strna(pid
), ansi_normal(), comm
);
488 ansi_highlight(), strna(pid
), ansi_normal());
493 if (parse_uid(uid
, &n
) >= 0) {
494 _cleanup_free_
char *u
= NULL
;
510 if (parse_gid(gid
, &n
) >= 0) {
511 _cleanup_free_
char *g
= NULL
;
526 const char *name
= normal_coredump
? "Signal" : "Reason";
528 if (normal_coredump
&& safe_atoi(sgnl
, &sig
) >= 0)
529 fprintf(file
, " %s: %s (%s)\n", name
, sgnl
, signal_to_string(sig
));
531 fprintf(file
, " %s: %s\n", name
, sgnl
);
537 r
= safe_atou64(timestamp
, &u
);
539 char absolute
[FORMAT_TIMESTAMP_MAX
], relative
[FORMAT_TIMESPAN_MAX
];
542 " Timestamp: %s (%s)\n",
543 format_timestamp(absolute
, sizeof(absolute
), u
),
544 format_timestamp_relative(relative
, sizeof(relative
), u
));
547 fprintf(file
, " Timestamp: %s\n", timestamp
);
551 fprintf(file
, " Command Line: %s\n", cmdline
);
553 fprintf(file
, " Executable: %s%s%s\n", ansi_highlight(), exe
, ansi_normal());
555 fprintf(file
, " Control Group: %s\n", cgroup
);
557 fprintf(file
, " Unit: %s\n", unit
);
559 fprintf(file
, " User Unit: %s\n", user_unit
);
561 fprintf(file
, " Slice: %s\n", slice
);
563 fprintf(file
, " Session: %s\n", session
);
567 if (parse_uid(owner_uid
, &n
) >= 0) {
568 _cleanup_free_
char *u
= NULL
;
572 " Owner UID: %s (%s)\n",
581 fprintf(file
, " Boot ID: %s\n", boot_id
);
583 fprintf(file
, " Machine ID: %s\n", machine_id
);
585 fprintf(file
, " Hostname: %s\n", hostname
);
590 inacc
= access(filename
, R_OK
) < 0;
591 trunc
= truncated
&& parse_boolean(truncated
) > 0;
594 fprintf(file
, " Storage: %s%s (%s%s%s)%s\n",
595 ansi_highlight_red(),
597 inacc
? "inaccessible" : "",
598 inacc
&& trunc
? ", " : "",
599 trunc
? "truncated" : "",
602 fprintf(file
, " Storage: %s\n", filename
);
606 fprintf(file
, " Storage: journal\n");
608 fprintf(file
, " Storage: none\n");
611 _cleanup_free_
char *m
= NULL
;
613 m
= strreplace(message
, "\n", "\n ");
615 fprintf(file
, " Message: %s\n", strstrip(m
?: message
));
621 static int focus(sd_journal
*j
) {
624 r
= sd_journal_seek_tail(j
);
626 r
= sd_journal_previous(j
);
628 return log_error_errno(r
, "Failed to search journal: %m");
630 log_error("No match found.");
636 static int print_entry(sd_journal
*j
, unsigned n_found
) {
639 if (arg_action
== ACTION_INFO
)
640 return print_info(stdout
, j
, n_found
);
642 return print_field(stdout
, j
);
644 return print_list(stdout
, j
, n_found
);
647 static int dump_list(sd_journal
*j
) {
648 unsigned n_found
= 0;
653 /* The coredumps are likely to compressed, and for just
654 * listing them we don't need to decompress them, so let's
655 * pick a fairly low data threshold here */
656 sd_journal_set_data_threshold(j
, 4096);
663 return print_entry(j
, 0);
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
++);
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")) {
767 /* Create a temporary file to write the uncompressed core to. */
769 r
= var_tmp_dir(&vt
);
771 return log_error_errno(r
, "Failed to acquire temporary directory path: %m");
773 temp
= strjoin(vt
, "/coredump-XXXXXX");
777 fdt
= mkostemp_safe(temp
);
779 return log_error_errno(fdt
, "Failed to create temporary file: %m");
780 log_debug("Created temporary file %s", temp
);
784 /* If neither path or file are specified, we will write to stdout. Let's now check
785 * if stdout is connected to a tty. We checked that the file exists, or that the
786 * core might be stored in the journal. In this second case, if we found the entry,
787 * in all likelyhood we will be able to access the COREDUMP= field. In either case,
788 * we stop before doing any "real" work, i.e. before starting decompression or
789 * reading from the file or creating temporary files.
793 return log_error_errno(ENOTTY
, "Refusing to dump core to tty"
794 " (use shell redirection or specify --output).");
802 #if HAVE_XZ || HAVE_LZ4
803 _cleanup_close_
int fdf
;
805 fdf
= open(filename
, O_RDONLY
| O_CLOEXEC
);
807 r
= log_error_errno(errno
, "Failed to open %s: %m", filename
);
811 r
= decompress_stream(filename
, fdf
, fd
, -1);
813 log_error_errno(r
, "Failed to decompress %s: %m", filename
);
817 log_error("Cannot decompress file. Compiled without compression support.");
824 /* We want full data, nothing truncated. */
825 sd_journal_set_data_threshold(j
, 0);
827 r
= sd_journal_get_data(j
, "COREDUMP", (const void**) &data
, &len
);
829 return log_error_errno(r
, "Failed to retrieve COREDUMP field: %m");
835 sz
= write(fd
, data
, len
);
837 r
= log_error_errno(errno
, "Failed to write output: %m");
840 if (sz
!= (ssize_t
) len
) {
841 log_error("Short write to output.");
856 log_debug("Removed temporary file %s", temp
);
861 static int dump_core(sd_journal
* j
) {
870 print_info(arg_output
? stdout
: stderr
, j
, false);
872 r
= save_core(j
, arg_output
, NULL
, NULL
);
876 r
= sd_journal_previous(j
);
877 if (r
> 0 && !arg_quiet
)
878 log_notice("More than one entry matches, ignoring rest.");
883 static int run_gdb(sd_journal
*j
) {
884 _cleanup_free_
char *exe
= NULL
, *path
= NULL
;
885 bool unlink_path
= false;
898 print_info(stdout
, j
, false);
901 r
= sd_journal_get_data(j
, "COREDUMP_EXE", (const void**) &data
, &len
);
903 return log_error_errno(r
, "Failed to retrieve COREDUMP_EXE field: %m");
905 assert(len
> strlen("COREDUMP_EXE="));
906 data
+= strlen("COREDUMP_EXE=");
907 len
-= strlen("COREDUMP_EXE=");
909 exe
= strndup(data
, len
);
913 if (endswith(exe
, " (deleted)")) {
914 log_error("Binary already deleted.");
918 if (!path_is_absolute(exe
)) {
919 log_error("Binary is not an absolute path.");
923 r
= save_core(j
, NULL
, &path
, &unlink_path
);
927 /* Don't interfere with gdb and its handling of SIGINT. */
928 (void) ignore_signals(SIGINT
, -1);
932 r
= log_error_errno(errno
, "Failed to fork(): %m");
936 (void) reset_all_signal_handlers();
937 (void) reset_signal_mask();
939 execlp("gdb", "gdb", exe
, path
, NULL
);
941 log_error_errno(errno
, "Failed to invoke gdb: %m");
945 r
= wait_for_terminate(pid
, &st
);
947 log_error_errno(r
, "Failed to wait for gdb: %m");
951 r
= st
.si_code
== CLD_EXITED
? st
.si_status
: 255;
954 (void) default_signals(SIGINT
, -1);
957 log_debug("Removed temporary file %s", path
);
964 static int check_units_active(void) {
965 _cleanup_(sd_bus_unrefp
) sd_bus
*bus
= NULL
;
966 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
967 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
968 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
970 const char *id
, *state
, *substate
;
975 r
= sd_bus_default_system(&bus
);
977 return log_error_errno(r
, "Failed to acquire bus: %m");
979 r
= sd_bus_message_new_method_call(
982 "org.freedesktop.systemd1",
983 "/org/freedesktop/systemd1",
984 "org.freedesktop.systemd1.Manager",
985 "ListUnitsByPatterns");
987 return bus_log_create_error(r
);
989 r
= sd_bus_message_append_strv(m
, NULL
);
991 return bus_log_create_error(r
);
993 r
= sd_bus_message_append_strv(m
, STRV_MAKE("systemd-coredump@*.service"));
995 return bus_log_create_error(r
);
997 r
= sd_bus_call(bus
, m
, SHORT_BUS_CALL_TIMEOUT_USEC
, &error
, &reply
);
999 return log_error_errno(r
, "Failed to check if any systemd-coredump@.service units are running: %s",
1000 bus_error_message(&error
, r
));
1002 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ssssssouso)");
1004 return bus_log_parse_error(r
);
1006 while ((r
= sd_bus_message_read(
1007 reply
, "(ssssssouso)",
1008 &id
, NULL
, NULL
, &state
, &substate
,
1009 NULL
, NULL
, NULL
, NULL
, NULL
)) > 0) {
1010 bool found
= !STR_IN_SET(state
, "inactive", "dead", "failed");
1011 log_debug("Unit %s is %s/%s, %scounting it.", id
, state
, substate
, found
? "" : "not ");
1015 return bus_log_parse_error(r
);
1017 r
= sd_bus_message_exit_container(reply
);
1019 return bus_log_parse_error(r
);
1024 int main(int argc
, char *argv
[]) {
1025 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1026 int r
= 0, units_active
;
1028 setlocale(LC_ALL
, "");
1029 log_parse_environment();
1032 r
= parse_argv(argc
, argv
);
1036 if (arg_action
== ACTION_NONE
)
1041 if (arg_directory
) {
1042 r
= sd_journal_open_directory(&j
, arg_directory
, 0);
1044 log_error_errno(r
, "Failed to open journals in directory: %s: %m", arg_directory
);
1048 r
= sd_journal_open(&j
, SD_JOURNAL_LOCAL_ONLY
);
1050 log_error_errno(r
, "Failed to open journal: %m");
1055 r
= journal_access_check_and_warn(j
, arg_quiet
);
1063 if (_unlikely_(log_get_max_level() >= LOG_DEBUG
)) {
1064 _cleanup_free_
char *filter
;
1066 filter
= journal_make_match_string(j
);
1067 log_debug("Journal filter: %s", filter
);
1070 units_active
= check_units_active(); /* error is treated the same as 0 */
1072 switch(arg_action
) {
1076 pager_open(arg_no_pager
, false);
1089 assert_not_reached("Shouldn't be here");
1092 if (units_active
> 0)
1093 printf("%s-- Notice: %d systemd-coredump@.service %s, output may be incomplete.%s\n",
1094 ansi_highlight_red(),
1095 units_active
, units_active
== 1 ? "unit is running" : "units are running",
1103 return r
>= 0 ? r
: EXIT_FAILURE
;