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