]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
5de0409e ZJS |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2012 Zbigniew Jędrzejewski-Szmek | |
5de0409e ZJS |
6 | ***/ |
7 | ||
3f6fd1ba LP |
8 | #include <fcntl.h> |
9 | #include <getopt.h> | |
a9cdc94f | 10 | #include <locale.h> |
5de0409e ZJS |
11 | #include <stdio.h> |
12 | #include <string.h> | |
ada45c78 | 13 | #include <unistd.h> |
5de0409e | 14 | |
012f2b7d | 15 | #include "sd-bus.h" |
2cf4172a | 16 | #include "sd-journal.h" |
2b044526 | 17 | #include "sd-messages.h" |
3f6fd1ba | 18 | |
b5efdb8a | 19 | #include "alloc-util.h" |
012f2b7d ZJS |
20 | #include "bus-error.h" |
21 | #include "bus-util.h" | |
3f6fd1ba | 22 | #include "compress.h" |
3ffd4af2 | 23 | #include "fd-util.h" |
0d39fa9c | 24 | #include "fileio.h" |
992e8f22 | 25 | #include "fs-util.h" |
3f6fd1ba | 26 | #include "journal-internal.h" |
b9aaa7f4 | 27 | #include "journal-util.h" |
5de0409e | 28 | #include "log.h" |
763c7aa2 | 29 | #include "macro.h" |
3f6fd1ba | 30 | #include "pager.h" |
6bedfcbb | 31 | #include "parse-util.h" |
3f6fd1ba | 32 | #include "path-util.h" |
0b452006 | 33 | #include "process-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" |
b1d4f8e1 | 39 | #include "user-util.h" |
6bedfcbb | 40 | #include "util.h" |
5ce97d33 | 41 | #include "verbs.h" |
5de0409e | 42 | |
501551e8 LP |
43 | #define SHORT_BUS_CALL_TIMEOUT_USEC (3 * USEC_PER_SEC) |
44 | ||
32485d09 | 45 | static usec_t arg_since = USEC_INFINITY, arg_until = USEC_INFINITY; |
e15758cc | 46 | static const char* arg_field = NULL; |
b73e9a02 | 47 | static const char *arg_directory = NULL; |
ea4b98e6 | 48 | static bool arg_no_pager = false; |
9a340880 | 49 | static int arg_no_legend = false; |
0c51aada | 50 | static int arg_one = false; |
3774cf57 | 51 | static FILE* arg_output = NULL; |
df65f77b | 52 | static bool arg_reverse = false; |
b9aaa7f4 | 53 | static bool arg_quiet = false; |
5de0409e | 54 | |
5ab9ed07 | 55 | static int add_match(sd_journal *j, const char *match) { |
7fd1b19b | 56 | _cleanup_free_ char *p = NULL; |
43bfe750 | 57 | const char* prefix, *pattern; |
0f474365 LP |
58 | pid_t pid; |
59 | int r; | |
5de0409e ZJS |
60 | |
61 | if (strchr(match, '=')) | |
62 | prefix = ""; | |
63 | else if (strchr(match, '/')) { | |
0f474365 LP |
64 | r = path_make_absolute_cwd(match, &p); |
65 | if (r < 0) | |
5ab9ed07 ZJS |
66 | return log_error_errno(r, "path_make_absolute_cwd(\"%s\"): %m", match); |
67 | ||
5de0409e ZJS |
68 | match = p; |
69 | prefix = "COREDUMP_EXE="; | |
0f474365 | 70 | } else if (parse_pid(match, &pid) >= 0) |
5de0409e ZJS |
71 | prefix = "COREDUMP_PID="; |
72 | else | |
73 | prefix = "COREDUMP_COMM="; | |
74 | ||
e8fb0238 | 75 | pattern = strjoina(prefix, match); |
5ab9ed07 ZJS |
76 | log_debug("Adding match: %s", pattern); |
77 | r = sd_journal_add_match(j, pattern, 0); | |
78 | if (r < 0) | |
79 | return log_error_errno(r, "Failed to add match \"%s\": %m", match); | |
43bfe750 | 80 | |
5ab9ed07 ZJS |
81 | return 0; |
82 | } | |
83 | ||
5ce97d33 | 84 | static int add_matches(sd_journal *j, char **matches) { |
5ab9ed07 ZJS |
85 | char **match; |
86 | int r; | |
5de0409e | 87 | |
2b044526 | 88 | r = sd_journal_add_match(j, "MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR, 0); |
0f474365 | 89 | if (r < 0) |
2b044526 | 90 | return log_error_errno(r, "Failed to add match \"%s\": %m", "MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR); |
5ab9ed07 | 91 | |
a7581ff9 ZJS |
92 | r = sd_journal_add_match(j, "MESSAGE_ID=" SD_MESSAGE_BACKTRACE_STR, 0); |
93 | if (r < 0) | |
94 | return log_error_errno(r, "Failed to add match \"%s\": %m", "MESSAGE_ID=" SD_MESSAGE_BACKTRACE_STR); | |
95 | ||
5ce97d33 | 96 | STRV_FOREACH(match, matches) { |
5ab9ed07 ZJS |
97 | r = add_match(j, *match); |
98 | if (r < 0) | |
99 | return r; | |
100 | } | |
5de0409e ZJS |
101 | |
102 | return 0; | |
5de0409e ZJS |
103 | } |
104 | ||
5ce97d33 YW |
105 | static int acquire_journal(sd_journal **ret, char **matches) { |
106 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; | |
107 | int r; | |
108 | ||
109 | assert(ret); | |
110 | ||
111 | if (arg_directory) { | |
112 | r = sd_journal_open_directory(&j, arg_directory, 0); | |
113 | if (r < 0) | |
114 | return log_error_errno(r, "Failed to open journals in directory: %s: %m", arg_directory); | |
115 | } else { | |
116 | r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY); | |
117 | if (r < 0) | |
118 | return log_error_errno(r, "Failed to open journal: %m"); | |
119 | } | |
120 | ||
121 | r = journal_access_check_and_warn(j, arg_quiet, true); | |
122 | if (r < 0) | |
123 | return r; | |
124 | ||
125 | r = add_matches(j, matches); | |
126 | if (r < 0) | |
127 | return r; | |
128 | ||
129 | if (DEBUG_LOGGING) { | |
130 | _cleanup_free_ char *filter; | |
131 | ||
132 | filter = journal_make_match_string(j); | |
133 | log_debug("Journal filter: %s", filter); | |
134 | } | |
135 | ||
1cc6c93a | 136 | *ret = TAKE_PTR(j); |
5ce97d33 YW |
137 | |
138 | return 0; | |
139 | } | |
140 | ||
141 | static int help(void) { | |
601185b4 ZJS |
142 | printf("%s [OPTIONS...]\n\n" |
143 | "List or retrieve coredumps from the journal.\n\n" | |
144 | "Flags:\n" | |
145 | " -h --help Show this help\n" | |
146 | " --version Print version string\n" | |
147 | " --no-pager Do not pipe output into a pager\n" | |
148 | " --no-legend Do not print the column headers.\n" | |
149 | " -1 Show information about most recent entry only\n" | |
32485d09 GS |
150 | " -S --since=DATE Only print coredumps since the date\n" |
151 | " -U --until=DATE Only print coredumps until the date\n" | |
df65f77b | 152 | " -r --reverse Show the newest entries first\n" |
601185b4 | 153 | " -F --field=FIELD List all values a certain field takes\n" |
06b76011 | 154 | " -o --output=FILE Write output to FILE\n" |
b73e9a02 | 155 | " -D --directory=DIR Use journal files from directory\n\n" |
b9aaa7f4 | 156 | " -q --quiet Do not show info messages and privilege warning\n" |
601185b4 ZJS |
157 | "Commands:\n" |
158 | " list [MATCHES...] List available coredumps (default)\n" | |
159 | " info [MATCHES...] Show detailed information about one or more coredumps\n" | |
160 | " dump [MATCHES...] Print first matching coredump to stdout\n" | |
161 | " gdb [MATCHES...] Start gdb for the first matching coredump\n" | |
162 | , program_invocation_short_name); | |
5ce97d33 YW |
163 | |
164 | return 0; | |
601185b4 ZJS |
165 | } |
166 | ||
5ab9ed07 | 167 | static int parse_argv(int argc, char *argv[]) { |
5de0409e ZJS |
168 | enum { |
169 | ARG_VERSION = 0x100, | |
170 | ARG_NO_PAGER, | |
9a340880 | 171 | ARG_NO_LEGEND, |
5de0409e ZJS |
172 | }; |
173 | ||
32485d09 | 174 | int c, r; |
5de0409e ZJS |
175 | |
176 | static const struct option options[] = { | |
57ce4bd4 ZJS |
177 | { "help", no_argument, NULL, 'h' }, |
178 | { "version" , no_argument, NULL, ARG_VERSION }, | |
179 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
9a340880 | 180 | { "no-legend", no_argument, NULL, ARG_NO_LEGEND }, |
57ce4bd4 | 181 | { "output", required_argument, NULL, 'o' }, |
4f76ae1b | 182 | { "field", required_argument, NULL, 'F' }, |
b73e9a02 | 183 | { "directory", required_argument, NULL, 'D' }, |
df65f77b | 184 | { "reverse", no_argument, NULL, 'r' }, |
32485d09 GS |
185 | { "since", required_argument, NULL, 'S' }, |
186 | { "until", required_argument, NULL, 'U' }, | |
b9aaa7f4 | 187 | { "quiet", no_argument, NULL, 'q' }, |
eb9da376 | 188 | {} |
5de0409e ZJS |
189 | }; |
190 | ||
191 | assert(argc >= 0); | |
192 | assert(argv); | |
193 | ||
b9aaa7f4 | 194 | while ((c = getopt_long(argc, argv, "ho:F:1D:rS:U:q", options, NULL)) >= 0) |
5de0409e ZJS |
195 | switch(c) { |
196 | case 'h': | |
5ce97d33 | 197 | return help(); |
5de0409e ZJS |
198 | |
199 | case ARG_VERSION: | |
3f6fd1ba | 200 | return version(); |
5de0409e ZJS |
201 | |
202 | case ARG_NO_PAGER: | |
203 | arg_no_pager = true; | |
204 | break; | |
205 | ||
9a340880 ZJS |
206 | case ARG_NO_LEGEND: |
207 | arg_no_legend = true; | |
208 | break; | |
209 | ||
5de0409e | 210 | case 'o': |
3774cf57 | 211 | if (arg_output) { |
5de0409e ZJS |
212 | log_error("cannot set output more than once"); |
213 | return -EINVAL; | |
214 | } | |
215 | ||
3774cf57 LP |
216 | arg_output = fopen(optarg, "we"); |
217 | if (!arg_output) | |
4a62c710 | 218 | return log_error_errno(errno, "writing to '%s': %m", optarg); |
5de0409e ZJS |
219 | |
220 | break; | |
57ce4bd4 | 221 | |
32485d09 GS |
222 | case 'S': |
223 | r = parse_timestamp(optarg, &arg_since); | |
224 | if (r < 0) | |
225 | return log_error_errno(r, "Failed to parse timestamp: %s", optarg); | |
226 | break; | |
227 | ||
228 | case 'U': | |
229 | r = parse_timestamp(optarg, &arg_until); | |
230 | if (r < 0) | |
231 | return log_error_errno(r, "Failed to parse timestamp: %s", optarg); | |
232 | break; | |
233 | ||
4f76ae1b | 234 | case 'F': |
a276ae74 | 235 | if (arg_field) { |
4f76ae1b ZJS |
236 | log_error("cannot use --field/-F more than once"); |
237 | return -EINVAL; | |
238 | } | |
a276ae74 | 239 | arg_field = optarg; |
4f76ae1b ZJS |
240 | break; |
241 | ||
0c51aada LP |
242 | case '1': |
243 | arg_one = true; | |
244 | break; | |
245 | ||
b73e9a02 SW |
246 | case 'D': |
247 | arg_directory = optarg; | |
248 | break; | |
249 | ||
df65f77b NK |
250 | case 'r': |
251 | arg_reverse = true; | |
252 | break; | |
253 | ||
b9aaa7f4 ZJS |
254 | case 'q': |
255 | arg_quiet = true; | |
256 | break; | |
257 | ||
57ce4bd4 ZJS |
258 | case '?': |
259 | return -EINVAL; | |
260 | ||
5de0409e | 261 | default: |
eb9da376 | 262 | assert_not_reached("Unhandled option"); |
5de0409e ZJS |
263 | } |
264 | ||
32485d09 GS |
265 | if (arg_since != USEC_INFINITY && arg_until != USEC_INFINITY && |
266 | arg_since > arg_until) { | |
267 | log_error("--since= must be before --until=."); | |
268 | return -EINVAL; | |
269 | } | |
270 | ||
5ce97d33 | 271 | return 1; |
5de0409e ZJS |
272 | } |
273 | ||
8bc8ab83 LP |
274 | static int retrieve(const void *data, |
275 | size_t len, | |
276 | const char *name, | |
a276ae74 | 277 | char **var) { |
5de0409e | 278 | |
4f76ae1b | 279 | size_t ident; |
a276ae74 | 280 | char *v; |
8bc8ab83 | 281 | |
4f76ae1b | 282 | ident = strlen(name) + 1; /* name + "=" */ |
8bc8ab83 | 283 | |
4f76ae1b | 284 | if (len < ident) |
8bc8ab83 | 285 | return 0; |
5de0409e | 286 | |
4f76ae1b | 287 | if (memcmp(data, name, ident - 1) != 0) |
8bc8ab83 LP |
288 | return 0; |
289 | ||
4f76ae1b | 290 | if (((const char*) data)[ident - 1] != '=') |
8bc8ab83 | 291 | return 0; |
5de0409e | 292 | |
a276ae74 LP |
293 | v = strndup((const char*)data + ident, len - ident); |
294 | if (!v) | |
5de0409e ZJS |
295 | return log_oom(); |
296 | ||
a276ae74 LP |
297 | free(*var); |
298 | *var = v; | |
299 | ||
062b99e8 | 300 | return 1; |
5de0409e ZJS |
301 | } |
302 | ||
062b99e8 | 303 | static int print_field(FILE* file, sd_journal *j) { |
4f76ae1b ZJS |
304 | const void *d; |
305 | size_t l; | |
306 | ||
e15758cc LP |
307 | assert(file); |
308 | assert(j); | |
309 | ||
a276ae74 | 310 | assert(arg_field); |
4f76ae1b | 311 | |
062b99e8 ZJS |
312 | /* A (user-specified) field may appear more than once for a given entry. |
313 | * We will print all of the occurences. | |
314 | * This is different below for fields that systemd-coredump uses, | |
315 | * because they cannot meaningfully appear more than once. | |
316 | */ | |
317 | SD_JOURNAL_FOREACH_DATA(j, d, l) { | |
318 | _cleanup_free_ char *value = NULL; | |
319 | int r; | |
320 | ||
321 | r = retrieve(d, l, arg_field, &value); | |
322 | if (r < 0) | |
323 | return r; | |
324 | if (r > 0) | |
325 | fprintf(file, "%s\n", value); | |
326 | } | |
a276ae74 | 327 | |
062b99e8 | 328 | return 0; |
4f76ae1b ZJS |
329 | } |
330 | ||
062b99e8 ZJS |
331 | #define RETRIEVE(d, l, name, arg) \ |
332 | { \ | |
333 | int _r = retrieve(d, l, name, &arg); \ | |
334 | if (_r < 0) \ | |
335 | return _r; \ | |
336 | if (_r > 0) \ | |
337 | continue; \ | |
338 | } | |
339 | ||
e15758cc | 340 | static int print_list(FILE* file, sd_journal *j, int had_legend) { |
a276ae74 | 341 | _cleanup_free_ char |
a7581ff9 | 342 | *mid = NULL, *pid = NULL, *uid = NULL, *gid = NULL, |
9fe13294 | 343 | *sgnl = NULL, *exe = NULL, *comm = NULL, *cmdline = NULL, |
cc4419ed | 344 | *filename = NULL, *truncated = NULL, *coredump = NULL; |
8bc8ab83 LP |
345 | const void *d; |
346 | size_t l; | |
684341b0 LP |
347 | usec_t t; |
348 | char buf[FORMAT_TIMESTAMP_MAX]; | |
349 | int r; | |
04de5879 | 350 | const char *present; |
a7581ff9 | 351 | bool normal_coredump; |
8bc8ab83 | 352 | |
e15758cc LP |
353 | assert(file); |
354 | assert(j); | |
355 | ||
8bc8ab83 | 356 | SD_JOURNAL_FOREACH_DATA(j, d, l) { |
a7581ff9 | 357 | RETRIEVE(d, l, "MESSAGE_ID", mid); |
062b99e8 ZJS |
358 | RETRIEVE(d, l, "COREDUMP_PID", pid); |
359 | RETRIEVE(d, l, "COREDUMP_UID", uid); | |
360 | RETRIEVE(d, l, "COREDUMP_GID", gid); | |
361 | RETRIEVE(d, l, "COREDUMP_SIGNAL", sgnl); | |
362 | RETRIEVE(d, l, "COREDUMP_EXE", exe); | |
363 | RETRIEVE(d, l, "COREDUMP_COMM", comm); | |
364 | RETRIEVE(d, l, "COREDUMP_CMDLINE", cmdline); | |
365 | RETRIEVE(d, l, "COREDUMP_FILENAME", filename); | |
cc4419ed | 366 | RETRIEVE(d, l, "COREDUMP_TRUNCATED", truncated); |
062b99e8 | 367 | RETRIEVE(d, l, "COREDUMP", coredump); |
8bc8ab83 | 368 | } |
5de0409e | 369 | |
9fe13294 | 370 | if (!pid && !uid && !gid && !sgnl && !exe && !comm && !cmdline && !filename) { |
684341b0 LP |
371 | log_warning("Empty coredump log entry"); |
372 | return -EINVAL; | |
373 | } | |
374 | ||
375 | r = sd_journal_get_realtime_usec(j, &t); | |
23bbb0de MS |
376 | if (r < 0) |
377 | return log_error_errno(r, "Failed to get realtime timestamp: %m"); | |
5de0409e | 378 | |
684341b0 LP |
379 | format_timestamp(buf, sizeof(buf), t); |
380 | ||
9a340880 | 381 | if (!had_legend && !arg_no_legend) |
cc4419ed | 382 | fprintf(file, "%-*s %*s %*s %*s %*s %-*s %s\n", |
c3f84106 | 383 | FORMAT_TIMESTAMP_WIDTH, "TIME", |
5de0409e ZJS |
384 | 6, "PID", |
385 | 5, "UID", | |
386 | 5, "GID", | |
684341b0 | 387 | 3, "SIG", |
cc4419ed | 388 | 9, "COREFILE", |
684341b0 | 389 | "EXE"); |
5de0409e | 390 | |
a7581ff9 ZJS |
391 | normal_coredump = streq_ptr(mid, SD_MESSAGE_COREDUMP_STR); |
392 | ||
04de5879 ZJS |
393 | if (filename) |
394 | if (access(filename, R_OK) == 0) | |
395 | present = "present"; | |
396 | else if (errno == ENOENT) | |
397 | present = "missing"; | |
398 | else | |
399 | present = "error"; | |
400 | else if (coredump) | |
401 | present = "journal"; | |
a7581ff9 | 402 | else if (normal_coredump) |
04de5879 | 403 | present = "none"; |
a7581ff9 ZJS |
404 | else |
405 | present = "-"; | |
04de5879 | 406 | |
32a1575f | 407 | if (STR_IN_SET(present, "present", "journal") && truncated && parse_boolean(truncated) > 0) |
cc4419ed ZJS |
408 | present = "truncated"; |
409 | ||
04de5879 | 410 | fprintf(file, "%-*s %*s %*s %*s %*s %-*s %s\n", |
c3f84106 | 411 | FORMAT_TIMESTAMP_WIDTH, buf, |
a276ae74 LP |
412 | 6, strna(pid), |
413 | 5, strna(uid), | |
414 | 5, strna(gid), | |
a7581ff9 | 415 | 3, normal_coredump ? strna(sgnl) : "-", |
cc4419ed | 416 | 9, present, |
a276ae74 | 417 | strna(exe ?: (comm ?: cmdline))); |
684341b0 LP |
418 | |
419 | return 0; | |
5de0409e ZJS |
420 | } |
421 | ||
e15758cc LP |
422 | static int print_info(FILE *file, sd_journal *j, bool need_space) { |
423 | _cleanup_free_ char | |
a7581ff9 | 424 | *mid = NULL, *pid = NULL, *uid = NULL, *gid = NULL, |
e15758cc LP |
425 | *sgnl = NULL, *exe = NULL, *comm = NULL, *cmdline = NULL, |
426 | *unit = NULL, *user_unit = NULL, *session = NULL, | |
a035f819 | 427 | *boot_id = NULL, *machine_id = NULL, *hostname = NULL, |
9fe13294 | 428 | *slice = NULL, *cgroup = NULL, *owner_uid = NULL, |
47f50642 | 429 | *message = NULL, *timestamp = NULL, *filename = NULL, |
cc4419ed | 430 | *truncated = NULL, *coredump = NULL; |
e15758cc LP |
431 | const void *d; |
432 | size_t l; | |
a7581ff9 | 433 | bool normal_coredump; |
4b8cbe9a | 434 | int r; |
e15758cc LP |
435 | |
436 | assert(file); | |
437 | assert(j); | |
438 | ||
439 | SD_JOURNAL_FOREACH_DATA(j, d, l) { | |
a7581ff9 | 440 | RETRIEVE(d, l, "MESSAGE_ID", mid); |
062b99e8 ZJS |
441 | RETRIEVE(d, l, "COREDUMP_PID", pid); |
442 | RETRIEVE(d, l, "COREDUMP_UID", uid); | |
443 | RETRIEVE(d, l, "COREDUMP_GID", gid); | |
444 | RETRIEVE(d, l, "COREDUMP_SIGNAL", sgnl); | |
445 | RETRIEVE(d, l, "COREDUMP_EXE", exe); | |
446 | RETRIEVE(d, l, "COREDUMP_COMM", comm); | |
447 | RETRIEVE(d, l, "COREDUMP_CMDLINE", cmdline); | |
448 | RETRIEVE(d, l, "COREDUMP_UNIT", unit); | |
449 | RETRIEVE(d, l, "COREDUMP_USER_UNIT", user_unit); | |
450 | RETRIEVE(d, l, "COREDUMP_SESSION", session); | |
451 | RETRIEVE(d, l, "COREDUMP_OWNER_UID", owner_uid); | |
452 | RETRIEVE(d, l, "COREDUMP_SLICE", slice); | |
453 | RETRIEVE(d, l, "COREDUMP_CGROUP", cgroup); | |
454 | RETRIEVE(d, l, "COREDUMP_TIMESTAMP", timestamp); | |
455 | RETRIEVE(d, l, "COREDUMP_FILENAME", filename); | |
cc4419ed | 456 | RETRIEVE(d, l, "COREDUMP_TRUNCATED", truncated); |
062b99e8 ZJS |
457 | RETRIEVE(d, l, "COREDUMP", coredump); |
458 | RETRIEVE(d, l, "_BOOT_ID", boot_id); | |
459 | RETRIEVE(d, l, "_MACHINE_ID", machine_id); | |
460 | RETRIEVE(d, l, "_HOSTNAME", hostname); | |
461 | RETRIEVE(d, l, "MESSAGE", message); | |
e15758cc LP |
462 | } |
463 | ||
464 | if (need_space) | |
465 | fputs("\n", file); | |
466 | ||
a7581ff9 ZJS |
467 | normal_coredump = streq_ptr(mid, SD_MESSAGE_COREDUMP_STR); |
468 | ||
81cef14f LP |
469 | if (comm) |
470 | fprintf(file, | |
471 | " PID: %s%s%s (%s)\n", | |
1fc464f6 | 472 | ansi_highlight(), strna(pid), ansi_normal(), comm); |
81cef14f LP |
473 | else |
474 | fprintf(file, | |
475 | " PID: %s%s%s\n", | |
1fc464f6 | 476 | ansi_highlight(), strna(pid), ansi_normal()); |
a035f819 LP |
477 | |
478 | if (uid) { | |
479 | uid_t n; | |
480 | ||
481 | if (parse_uid(uid, &n) >= 0) { | |
482 | _cleanup_free_ char *u = NULL; | |
483 | ||
484 | u = uid_to_name(n); | |
485 | fprintf(file, | |
486 | " UID: %s (%s)\n", | |
487 | uid, u); | |
488 | } else { | |
489 | fprintf(file, | |
490 | " UID: %s\n", | |
491 | uid); | |
492 | } | |
493 | } | |
494 | ||
495 | if (gid) { | |
496 | gid_t n; | |
497 | ||
498 | if (parse_gid(gid, &n) >= 0) { | |
499 | _cleanup_free_ char *g = NULL; | |
500 | ||
501 | g = gid_to_name(n); | |
502 | fprintf(file, | |
503 | " GID: %s (%s)\n", | |
504 | gid, g); | |
505 | } else { | |
506 | fprintf(file, | |
507 | " GID: %s\n", | |
508 | gid); | |
509 | } | |
510 | } | |
e15758cc LP |
511 | |
512 | if (sgnl) { | |
513 | int sig; | |
a7581ff9 | 514 | const char *name = normal_coredump ? "Signal" : "Reason"; |
e15758cc | 515 | |
a7581ff9 ZJS |
516 | if (normal_coredump && safe_atoi(sgnl, &sig) >= 0) |
517 | fprintf(file, " %s: %s (%s)\n", name, sgnl, signal_to_string(sig)); | |
e15758cc | 518 | else |
a7581ff9 | 519 | fprintf(file, " %s: %s\n", name, sgnl); |
e15758cc LP |
520 | } |
521 | ||
4b8cbe9a LP |
522 | if (timestamp) { |
523 | usec_t u; | |
524 | ||
525 | r = safe_atou64(timestamp, &u); | |
526 | if (r >= 0) { | |
527 | char absolute[FORMAT_TIMESTAMP_MAX], relative[FORMAT_TIMESPAN_MAX]; | |
528 | ||
529 | fprintf(file, | |
530 | " Timestamp: %s (%s)\n", | |
531 | format_timestamp(absolute, sizeof(absolute), u), | |
532 | format_timestamp_relative(relative, sizeof(relative), u)); | |
533 | ||
534 | } else | |
535 | fprintf(file, " Timestamp: %s\n", timestamp); | |
536 | } | |
537 | ||
e15758cc LP |
538 | if (cmdline) |
539 | fprintf(file, " Command Line: %s\n", cmdline); | |
81cef14f | 540 | if (exe) |
1fc464f6 | 541 | fprintf(file, " Executable: %s%s%s\n", ansi_highlight(), exe, ansi_normal()); |
a035f819 LP |
542 | if (cgroup) |
543 | fprintf(file, " Control Group: %s\n", cgroup); | |
e15758cc LP |
544 | if (unit) |
545 | fprintf(file, " Unit: %s\n", unit); | |
546 | if (user_unit) | |
554ed50f | 547 | fprintf(file, " User Unit: %s\n", user_unit); |
a035f819 LP |
548 | if (slice) |
549 | fprintf(file, " Slice: %s\n", slice); | |
e15758cc LP |
550 | if (session) |
551 | fprintf(file, " Session: %s\n", session); | |
a035f819 LP |
552 | if (owner_uid) { |
553 | uid_t n; | |
554 | ||
555 | if (parse_uid(owner_uid, &n) >= 0) { | |
556 | _cleanup_free_ char *u = NULL; | |
557 | ||
558 | u = uid_to_name(n); | |
559 | fprintf(file, | |
560 | " Owner UID: %s (%s)\n", | |
561 | owner_uid, u); | |
562 | } else { | |
563 | fprintf(file, | |
564 | " Owner UID: %s\n", | |
565 | owner_uid); | |
566 | } | |
567 | } | |
e15758cc LP |
568 | if (boot_id) |
569 | fprintf(file, " Boot ID: %s\n", boot_id); | |
570 | if (machine_id) | |
571 | fprintf(file, " Machine ID: %s\n", machine_id); | |
572 | if (hostname) | |
573 | fprintf(file, " Hostname: %s\n", hostname); | |
574 | ||
cc4419ed | 575 | if (filename) { |
32a1575f LP |
576 | bool inacc, trunc; |
577 | ||
578 | inacc = access(filename, R_OK) < 0; | |
579 | trunc = truncated && parse_boolean(truncated) > 0; | |
cc4419ed ZJS |
580 | |
581 | if (inacc || trunc) | |
582 | fprintf(file, " Storage: %s%s (%s%s%s)%s\n", | |
583 | ansi_highlight_red(), | |
584 | filename, | |
585 | inacc ? "inaccessible" : "", | |
586 | inacc && trunc ? ", " : "", | |
587 | trunc ? "truncated" : "", | |
588 | ansi_normal()); | |
589 | else | |
590 | fprintf(file, " Storage: %s\n", filename); | |
591 | } | |
592 | ||
47f50642 ZJS |
593 | else if (coredump) |
594 | fprintf(file, " Storage: journal\n"); | |
595 | else | |
596 | fprintf(file, " Storage: none\n"); | |
e15758cc | 597 | |
8d4e028f LP |
598 | if (message) { |
599 | _cleanup_free_ char *m = NULL; | |
600 | ||
601 | m = strreplace(message, "\n", "\n "); | |
602 | ||
603 | fprintf(file, " Message: %s\n", strstrip(m ?: message)); | |
604 | } | |
605 | ||
e15758cc LP |
606 | return 0; |
607 | } | |
608 | ||
ada45c78 | 609 | static int focus(sd_journal *j) { |
5de0409e ZJS |
610 | int r; |
611 | ||
5de0409e ZJS |
612 | r = sd_journal_seek_tail(j); |
613 | if (r == 0) | |
614 | r = sd_journal_previous(j); | |
23bbb0de MS |
615 | if (r < 0) |
616 | return log_error_errno(r, "Failed to search journal: %m"); | |
8bc8ab83 | 617 | if (r == 0) { |
0c51aada | 618 | log_error("No match found."); |
8bc8ab83 LP |
619 | return -ESRCH; |
620 | } | |
ada45c78 LP |
621 | return r; |
622 | } | |
5de0409e | 623 | |
5ce97d33 | 624 | static int print_entry(sd_journal *j, unsigned n_found, bool verb_is_info) { |
0c51aada LP |
625 | assert(j); |
626 | ||
5ce97d33 | 627 | if (verb_is_info) |
062b99e8 | 628 | return print_info(stdout, j, n_found); |
0c51aada | 629 | else if (arg_field) |
062b99e8 | 630 | return print_field(stdout, j); |
0c51aada | 631 | else |
062b99e8 | 632 | return print_list(stdout, j, n_found); |
0c51aada LP |
633 | } |
634 | ||
5ce97d33 YW |
635 | static int dump_list(int argc, char **argv, void *userdata) { |
636 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; | |
0c51aada | 637 | unsigned n_found = 0; |
5ce97d33 | 638 | bool verb_is_info; |
0c51aada LP |
639 | int r; |
640 | ||
5ce97d33 YW |
641 | verb_is_info = (argc >= 1 && streq(argv[0], "info")); |
642 | ||
643 | r = acquire_journal(&j, argv + 1); | |
644 | if (r < 0) | |
645 | return r; | |
646 | ||
ee5324aa | 647 | (void) pager_open(arg_no_pager, false); |
0c51aada LP |
648 | |
649 | /* The coredumps are likely to compressed, and for just | |
650 | * listing them we don't need to decompress them, so let's | |
651 | * pick a fairly low data threshold here */ | |
652 | sd_journal_set_data_threshold(j, 4096); | |
653 | ||
654 | if (arg_one) { | |
655 | r = focus(j); | |
656 | if (r < 0) | |
657 | return r; | |
658 | ||
5ce97d33 | 659 | return print_entry(j, 0, verb_is_info); |
0c51aada | 660 | } else { |
32485d09 GS |
661 | if (arg_since != USEC_INFINITY && !arg_reverse) |
662 | r = sd_journal_seek_realtime_usec(j, arg_since); | |
663 | else if (arg_until != USEC_INFINITY && arg_reverse) | |
664 | r = sd_journal_seek_realtime_usec(j, arg_until); | |
665 | else if (arg_reverse) | |
666 | r = sd_journal_seek_tail(j); | |
667 | else | |
668 | r = sd_journal_seek_head(j); | |
669 | if (r < 0) | |
670 | return log_error_errno(r, "Failed to seek to date: %m"); | |
671 | ||
672 | for (;;) { | |
673 | if (!arg_reverse) | |
674 | r = sd_journal_next(j); | |
675 | else | |
676 | r = sd_journal_previous(j); | |
677 | ||
678 | if (r < 0) | |
679 | return log_error_errno(r, "Failed to iterate through journal: %m"); | |
680 | ||
681 | if (r == 0) | |
682 | break; | |
683 | ||
684 | if (arg_until != USEC_INFINITY && !arg_reverse) { | |
685 | usec_t usec; | |
686 | ||
687 | r = sd_journal_get_realtime_usec(j, &usec); | |
df65f77b | 688 | if (r < 0) |
32485d09 GS |
689 | return log_error_errno(r, "Failed to determine timestamp: %m"); |
690 | if (usec > arg_until) | |
691 | continue; | |
df65f77b | 692 | } |
32485d09 GS |
693 | |
694 | if (arg_since != USEC_INFINITY && arg_reverse) { | |
695 | usec_t usec; | |
696 | ||
697 | r = sd_journal_get_realtime_usec(j, &usec); | |
df65f77b | 698 | if (r < 0) |
32485d09 GS |
699 | return log_error_errno(r, "Failed to determine timestamp: %m"); |
700 | if (usec < arg_since) | |
701 | continue; | |
df65f77b | 702 | } |
32485d09 | 703 | |
5ce97d33 | 704 | r = print_entry(j, n_found++, verb_is_info); |
32485d09 GS |
705 | if (r < 0) |
706 | return r; | |
062b99e8 | 707 | } |
0c51aada LP |
708 | |
709 | if (!arg_field && n_found <= 0) { | |
b9aaa7f4 ZJS |
710 | if (!arg_quiet) |
711 | log_notice("No coredumps found."); | |
0c51aada LP |
712 | return -ESRCH; |
713 | } | |
714 | } | |
715 | ||
716 | return 0; | |
717 | } | |
718 | ||
bb7c5bad | 719 | static int save_core(sd_journal *j, FILE *file, char **path, bool *unlink_temp) { |
9fe13294 ZJS |
720 | const char *data; |
721 | _cleanup_free_ char *filename = NULL; | |
722 | size_t len; | |
bb7c5bad | 723 | int r, fd; |
fc6cec86 ZJS |
724 | _cleanup_close_ int fdt = -1; |
725 | char *temp = NULL; | |
ada45c78 | 726 | |
bb7c5bad ZJS |
727 | assert(!(file && path)); /* At most one can be specified */ |
728 | assert(!!path == !!unlink_temp); /* Those must be specified together */ | |
47f50642 | 729 | |
fc6cec86 | 730 | /* Look for a coredump on disk first. */ |
9fe13294 | 731 | r = sd_journal_get_data(j, "COREDUMP_FILENAME", (const void**) &data, &len); |
bb7c5bad | 732 | if (r == 0) |
9fe13294 | 733 | retrieve(data, len, "COREDUMP_FILENAME", &filename); |
bb7c5bad ZJS |
734 | else { |
735 | if (r != -ENOENT) | |
736 | return log_error_errno(r, "Failed to retrieve COREDUMP_FILENAME field: %m"); | |
737 | /* Check that we can have a COREDUMP field. We still haven't set a high | |
738 | * data threshold, so we'll get a few kilobytes at most. | |
739 | */ | |
740 | ||
741 | r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len); | |
742 | if (r == -ENOENT) | |
743 | return log_error_errno(r, "Coredump entry has no core attached (neither internally in the journal nor externally on disk)."); | |
744 | if (r < 0) | |
745 | return log_error_errno(r, "Failed to retrieve COREDUMP field: %m"); | |
746 | } | |
93b73b06 | 747 | |
954d3a51 ZJS |
748 | if (filename) { |
749 | if (access(filename, R_OK) < 0) | |
750 | return log_error_errno(errno, "File \"%s\" is not readable: %m", filename); | |
5de0409e | 751 | |
954d3a51 | 752 | if (path && !endswith(filename, ".xz") && !endswith(filename, ".lz4")) { |
ae2a15bc | 753 | *path = TAKE_PTR(filename); |
a276ae74 | 754 | |
954d3a51 ZJS |
755 | return 0; |
756 | } | |
fc6cec86 | 757 | } |
a276ae74 | 758 | |
bb7c5bad | 759 | if (path) { |
fc6cec86 | 760 | const char *vt; |
992e8f22 | 761 | |
fc6cec86 | 762 | /* Create a temporary file to write the uncompressed core to. */ |
992e8f22 | 763 | |
fc6cec86 ZJS |
764 | r = var_tmp_dir(&vt); |
765 | if (r < 0) | |
766 | return log_error_errno(r, "Failed to acquire temporary directory path: %m"); | |
9fe13294 | 767 | |
605405c6 | 768 | temp = strjoin(vt, "/coredump-XXXXXX"); |
fc6cec86 ZJS |
769 | if (!temp) |
770 | return log_oom(); | |
9fe13294 | 771 | |
fc6cec86 ZJS |
772 | fdt = mkostemp_safe(temp); |
773 | if (fdt < 0) | |
774 | return log_error_errno(fdt, "Failed to create temporary file: %m"); | |
775 | log_debug("Created temporary file %s", temp); | |
9fe13294 | 776 | |
fc6cec86 | 777 | fd = fdt; |
bb7c5bad ZJS |
778 | } else { |
779 | /* If neither path or file are specified, we will write to stdout. Let's now check | |
780 | * if stdout is connected to a tty. We checked that the file exists, or that the | |
781 | * core might be stored in the journal. In this second case, if we found the entry, | |
782 | * in all likelyhood we will be able to access the COREDUMP= field. In either case, | |
783 | * we stop before doing any "real" work, i.e. before starting decompression or | |
784 | * reading from the file or creating temporary files. | |
785 | */ | |
786 | if (!file) { | |
787 | if (on_tty()) | |
788 | return log_error_errno(ENOTTY, "Refusing to dump core to tty" | |
789 | " (use shell redirection or specify --output)."); | |
790 | file = stdout; | |
791 | } | |
792 | ||
793 | fd = fileno(file); | |
fc6cec86 ZJS |
794 | } |
795 | ||
796 | if (filename) { | |
349cc4a5 | 797 | #if HAVE_XZ || HAVE_LZ4 |
fc6cec86 | 798 | _cleanup_close_ int fdf; |
9fe13294 | 799 | |
fc6cec86 ZJS |
800 | fdf = open(filename, O_RDONLY | O_CLOEXEC); |
801 | if (fdf < 0) { | |
802 | r = log_error_errno(errno, "Failed to open %s: %m", filename); | |
2fb8159f | 803 | goto error; |
fc6cec86 ZJS |
804 | } |
805 | ||
806 | r = decompress_stream(filename, fdf, fd, -1); | |
807 | if (r < 0) { | |
808 | log_error_errno(r, "Failed to decompress %s: %m", filename); | |
9fe13294 ZJS |
809 | goto error; |
810 | } | |
fc6cec86 ZJS |
811 | #else |
812 | log_error("Cannot decompress file. Compiled without compression support."); | |
813 | r = -EOPNOTSUPP; | |
814 | goto error; | |
815 | #endif | |
816 | } else { | |
817 | ssize_t sz; | |
a276ae74 | 818 | |
bb7c5bad ZJS |
819 | /* We want full data, nothing truncated. */ |
820 | sd_journal_set_data_threshold(j, 0); | |
821 | ||
fc6cec86 ZJS |
822 | r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len); |
823 | if (r < 0) | |
bb7c5bad | 824 | return log_error_errno(r, "Failed to retrieve COREDUMP field: %m"); |
fc6cec86 ZJS |
825 | |
826 | assert(len >= 9); | |
827 | data += 9; | |
828 | len -= 9; | |
829 | ||
954d3a51 | 830 | sz = write(fd, data, len); |
fc6cec86 | 831 | if (sz < 0) { |
954d3a51 | 832 | r = log_error_errno(errno, "Failed to write output: %m"); |
fc6cec86 | 833 | goto error; |
a276ae74 | 834 | } |
fc6cec86 | 835 | if (sz != (ssize_t) len) { |
954d3a51 | 836 | log_error("Short write to output."); |
fc6cec86 ZJS |
837 | r = -EIO; |
838 | goto error; | |
839 | } | |
840 | } | |
a276ae74 | 841 | |
fc6cec86 ZJS |
842 | if (temp) { |
843 | *path = temp; | |
844 | *unlink_temp = true; | |
845 | } | |
846 | return 0; | |
9fe13294 ZJS |
847 | |
848 | error: | |
fc6cec86 ZJS |
849 | if (temp) { |
850 | unlink(temp); | |
851 | log_debug("Removed temporary file %s", temp); | |
9fe13294 | 852 | } |
fc6cec86 | 853 | return r; |
9fe13294 | 854 | } |
a276ae74 | 855 | |
5ce97d33 YW |
856 | static int dump_core(int argc, char **argv, void *userdata) { |
857 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; | |
9fe13294 ZJS |
858 | int r; |
859 | ||
5ce97d33 YW |
860 | if (arg_field) { |
861 | log_error("Option --field/-F only makes sense with list"); | |
862 | return -EINVAL; | |
863 | } | |
864 | ||
865 | r = acquire_journal(&j, argv + 1); | |
866 | if (r < 0) | |
867 | return r; | |
9fe13294 ZJS |
868 | |
869 | r = focus(j); | |
870 | if (r < 0) | |
ada45c78 | 871 | return r; |
ada45c78 | 872 | |
3774cf57 | 873 | print_info(arg_output ? stdout : stderr, j, false); |
9fe13294 | 874 | |
bb7c5bad | 875 | r = save_core(j, arg_output, NULL, NULL); |
23bbb0de | 876 | if (r < 0) |
bb7c5bad | 877 | return r; |
5de0409e ZJS |
878 | |
879 | r = sd_journal_previous(j); | |
b9aaa7f4 ZJS |
880 | if (r > 0 && !arg_quiet) |
881 | log_notice("More than one entry matches, ignoring rest."); | |
5de0409e ZJS |
882 | |
883 | return 0; | |
884 | } | |
885 | ||
5ce97d33 YW |
886 | static int run_gdb(int argc, char **argv, void *userdata) { |
887 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; | |
de8f6e54 | 888 | _cleanup_free_ char *exe = NULL, *path = NULL; |
9fe13294 ZJS |
889 | bool unlink_path = false; |
890 | const char *data; | |
ada45c78 | 891 | size_t len; |
ada45c78 | 892 | pid_t pid; |
ada45c78 | 893 | int r; |
ada45c78 | 894 | |
5ce97d33 YW |
895 | if (arg_field) { |
896 | log_error("Option --field/-F only makes sense with list"); | |
897 | return -EINVAL; | |
898 | } | |
899 | ||
900 | r = acquire_journal(&j, argv + 1); | |
901 | if (r < 0) | |
902 | return r; | |
ada45c78 LP |
903 | |
904 | r = focus(j); | |
905 | if (r < 0) | |
906 | return r; | |
907 | ||
e15758cc LP |
908 | print_info(stdout, j, false); |
909 | fputs("\n", stdout); | |
ada45c78 LP |
910 | |
911 | r = sd_journal_get_data(j, "COREDUMP_EXE", (const void**) &data, &len); | |
23bbb0de MS |
912 | if (r < 0) |
913 | return log_error_errno(r, "Failed to retrieve COREDUMP_EXE field: %m"); | |
ada45c78 | 914 | |
fbd0b64f LP |
915 | assert(len > STRLEN("COREDUMP_EXE=")); |
916 | data += STRLEN("COREDUMP_EXE="); | |
917 | len -= STRLEN("COREDUMP_EXE="); | |
ada45c78 LP |
918 | |
919 | exe = strndup(data, len); | |
920 | if (!exe) | |
921 | return log_oom(); | |
922 | ||
923 | if (endswith(exe, " (deleted)")) { | |
924 | log_error("Binary already deleted."); | |
925 | return -ENOENT; | |
926 | } | |
927 | ||
a276ae74 LP |
928 | if (!path_is_absolute(exe)) { |
929 | log_error("Binary is not an absolute path."); | |
930 | return -ENOENT; | |
931 | } | |
932 | ||
bb7c5bad | 933 | r = save_core(j, NULL, &path, &unlink_path); |
23bbb0de | 934 | if (r < 0) |
bb7c5bad | 935 | return r; |
ada45c78 | 936 | |
3e7bc89b FB |
937 | /* Don't interfere with gdb and its handling of SIGINT. */ |
938 | (void) ignore_signals(SIGINT, -1); | |
939 | ||
b6e1fff1 LP |
940 | r = safe_fork("(gdb)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS|FORK_LOG, &pid); |
941 | if (r < 0) | |
ada45c78 | 942 | goto finish; |
4c253ed1 | 943 | if (r == 0) { |
ada45c78 | 944 | execlp("gdb", "gdb", exe, path, NULL); |
0b1f3c76 | 945 | log_open(); |
56f64d95 | 946 | log_error_errno(errno, "Failed to invoke gdb: %m"); |
a45d7127 | 947 | _exit(EXIT_FAILURE); |
ada45c78 LP |
948 | } |
949 | ||
2e87a1fd | 950 | r = wait_for_terminate_and_check("gdb", pid, WAIT_LOG_ABNORMAL); |
ada45c78 LP |
951 | |
952 | finish: | |
3e7bc89b FB |
953 | (void) default_signals(SIGINT, -1); |
954 | ||
9fe13294 ZJS |
955 | if (unlink_path) { |
956 | log_debug("Removed temporary file %s", path); | |
957 | unlink(path); | |
958 | } | |
a276ae74 | 959 | |
ada45c78 LP |
960 | return r; |
961 | } | |
962 | ||
012f2b7d ZJS |
963 | static int check_units_active(void) { |
964 | _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL; | |
965 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; | |
966 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
967 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
968 | int c = 0, r; | |
7bbf2d84 | 969 | const char *id, *state, *substate; |
012f2b7d | 970 | |
b9aaa7f4 ZJS |
971 | if (arg_quiet) |
972 | return false; | |
973 | ||
012f2b7d ZJS |
974 | r = sd_bus_default_system(&bus); |
975 | if (r < 0) | |
976 | return log_error_errno(r, "Failed to acquire bus: %m"); | |
977 | ||
978 | r = sd_bus_message_new_method_call( | |
979 | bus, | |
980 | &m, | |
981 | "org.freedesktop.systemd1", | |
982 | "/org/freedesktop/systemd1", | |
983 | "org.freedesktop.systemd1.Manager", | |
984 | "ListUnitsByPatterns"); | |
985 | if (r < 0) | |
986 | return bus_log_create_error(r); | |
987 | ||
988 | r = sd_bus_message_append_strv(m, NULL); | |
989 | if (r < 0) | |
990 | return bus_log_create_error(r); | |
991 | ||
992 | r = sd_bus_message_append_strv(m, STRV_MAKE("systemd-coredump@*.service")); | |
993 | if (r < 0) | |
994 | return bus_log_create_error(r); | |
995 | ||
501551e8 | 996 | r = sd_bus_call(bus, m, SHORT_BUS_CALL_TIMEOUT_USEC, &error, &reply); |
012f2b7d ZJS |
997 | if (r < 0) |
998 | return log_error_errno(r, "Failed to check if any systemd-coredump@.service units are running: %s", | |
999 | bus_error_message(&error, r)); | |
1000 | ||
1001 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)"); | |
1002 | if (r < 0) | |
1003 | return bus_log_parse_error(r); | |
1004 | ||
1005 | while ((r = sd_bus_message_read( | |
1006 | reply, "(ssssssouso)", | |
7bbf2d84 ZJS |
1007 | &id, NULL, NULL, &state, &substate, |
1008 | NULL, NULL, NULL, NULL, NULL)) > 0) { | |
eb5877a0 | 1009 | bool found = !STR_IN_SET(state, "inactive", "dead", "failed"); |
7bbf2d84 ZJS |
1010 | log_debug("Unit %s is %s/%s, %scounting it.", id, state, substate, found ? "" : "not "); |
1011 | c += found; | |
1012 | } | |
012f2b7d ZJS |
1013 | if (r < 0) |
1014 | return bus_log_parse_error(r); | |
1015 | ||
1016 | r = sd_bus_message_exit_container(reply); | |
1017 | if (r < 0) | |
1018 | return bus_log_parse_error(r); | |
1019 | ||
1020 | return c; | |
1021 | } | |
1022 | ||
5ce97d33 YW |
1023 | static int coredumpctl_main(int argc, char *argv[]) { |
1024 | ||
1025 | static const Verb verbs[] = { | |
1026 | { "list", VERB_ANY, VERB_ANY, VERB_DEFAULT, dump_list }, | |
1027 | { "info", VERB_ANY, VERB_ANY, 0, dump_list }, | |
1028 | { "dump", VERB_ANY, VERB_ANY, 0, dump_core }, | |
1029 | { "gdb", VERB_ANY, VERB_ANY, 0, run_gdb }, | |
1030 | {} | |
1031 | }; | |
1032 | ||
1033 | return dispatch_verb(argc, argv, verbs, NULL); | |
1034 | } | |
1035 | ||
5de0409e | 1036 | int main(int argc, char *argv[]) { |
5ce97d33 | 1037 | int r, units_active; |
5de0409e | 1038 | |
a9cdc94f | 1039 | setlocale(LC_ALL, ""); |
5de0409e ZJS |
1040 | log_parse_environment(); |
1041 | log_open(); | |
1042 | ||
5ab9ed07 | 1043 | r = parse_argv(argc, argv); |
5ce97d33 | 1044 | if (r <= 0) |
5de0409e ZJS |
1045 | goto end; |
1046 | ||
2cf4172a LP |
1047 | sigbus_install(); |
1048 | ||
012f2b7d ZJS |
1049 | units_active = check_units_active(); /* error is treated the same as 0 */ |
1050 | ||
5ce97d33 | 1051 | r = coredumpctl_main(argc, argv); |
5de0409e | 1052 | |
012f2b7d ZJS |
1053 | if (units_active > 0) |
1054 | printf("%s-- Notice: %d systemd-coredump@.service %s, output may be incomplete.%s\n", | |
1055 | ansi_highlight_red(), | |
1056 | units_active, units_active == 1 ? "unit is running" : "units are running", | |
1057 | ansi_normal()); | |
5de0409e | 1058 | end: |
5de0409e ZJS |
1059 | pager_close(); |
1060 | ||
f5c4b520 | 1061 | safe_fclose(arg_output); |
5de0409e | 1062 | |
ada45c78 | 1063 | return r >= 0 ? r : EXIT_FAILURE; |
5de0409e | 1064 | } |