]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
5de0409e | 2 | |
3f6fd1ba LP |
3 | #include <fcntl.h> |
4 | #include <getopt.h> | |
a9cdc94f | 5 | #include <locale.h> |
5de0409e | 6 | #include <stdio.h> |
ada45c78 | 7 | #include <unistd.h> |
5de0409e | 8 | |
012f2b7d | 9 | #include "sd-bus.h" |
2cf4172a | 10 | #include "sd-journal.h" |
2b044526 | 11 | #include "sd-messages.h" |
3f6fd1ba | 12 | |
b5efdb8a | 13 | #include "alloc-util.h" |
012f2b7d ZJS |
14 | #include "bus-error.h" |
15 | #include "bus-util.h" | |
3f6fd1ba | 16 | #include "compress.h" |
1abaf488 | 17 | #include "def.h" |
3ffd4af2 | 18 | #include "fd-util.h" |
0689cfdb | 19 | #include "format-table.h" |
992e8f22 | 20 | #include "fs-util.h" |
aeb56450 | 21 | #include "glob-util.h" |
3f6fd1ba | 22 | #include "journal-internal.h" |
b9aaa7f4 | 23 | #include "journal-util.h" |
5de0409e | 24 | #include "log.h" |
763c7aa2 | 25 | #include "macro.h" |
c118b577 | 26 | #include "main-func.h" |
3f6fd1ba | 27 | #include "pager.h" |
b1e8f46c | 28 | #include "parse-argument.h" |
6bedfcbb | 29 | #include "parse-util.h" |
3f6fd1ba | 30 | #include "path-util.h" |
294bf0c3 | 31 | #include "pretty-print.h" |
0b452006 | 32 | #include "process-util.h" |
1abaf488 | 33 | #include "rlimit-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" |
e4de7287 | 39 | #include "tmpfile-util.h" |
b1d4f8e1 | 40 | #include "user-util.h" |
6bedfcbb | 41 | #include "util.h" |
5ce97d33 | 42 | #include "verbs.h" |
5de0409e | 43 | |
501551e8 LP |
44 | #define SHORT_BUS_CALL_TIMEOUT_USEC (3 * USEC_PER_SEC) |
45 | ||
32485d09 | 46 | static usec_t arg_since = USEC_INFINITY, arg_until = USEC_INFINITY; |
e15758cc | 47 | static const char* arg_field = NULL; |
c5896b6a | 48 | static const char *arg_debugger = NULL; |
a2be8be2 | 49 | static char **arg_debugger_args = NULL; |
b73e9a02 | 50 | static const char *arg_directory = NULL; |
aeb56450 | 51 | static char **arg_file = NULL; |
0689cfdb | 52 | static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF; |
0221d68a | 53 | static PagerFlags arg_pager_flags = 0; |
0689cfdb LP |
54 | static int arg_legend = true; |
55 | static size_t arg_rows_max = SIZE_MAX; | |
158ecef5 | 56 | static const char* arg_output = NULL; |
df65f77b | 57 | static bool arg_reverse = false; |
b9aaa7f4 | 58 | static bool arg_quiet = false; |
d888ef68 | 59 | static bool arg_all = false; |
5de0409e | 60 | |
a2be8be2 | 61 | STATIC_DESTRUCTOR_REGISTER(arg_debugger_args, strv_freep); |
aeb56450 FS |
62 | STATIC_DESTRUCTOR_REGISTER(arg_file, strv_freep); |
63 | ||
5ab9ed07 | 64 | static int add_match(sd_journal *j, const char *match) { |
7fd1b19b | 65 | _cleanup_free_ char *p = NULL; |
43bfe750 | 66 | const char* prefix, *pattern; |
0f474365 LP |
67 | pid_t pid; |
68 | int r; | |
5de0409e ZJS |
69 | |
70 | if (strchr(match, '=')) | |
71 | prefix = ""; | |
72 | else if (strchr(match, '/')) { | |
0f474365 LP |
73 | r = path_make_absolute_cwd(match, &p); |
74 | if (r < 0) | |
5ab9ed07 ZJS |
75 | return log_error_errno(r, "path_make_absolute_cwd(\"%s\"): %m", match); |
76 | ||
5de0409e ZJS |
77 | match = p; |
78 | prefix = "COREDUMP_EXE="; | |
0f474365 | 79 | } else if (parse_pid(match, &pid) >= 0) |
5de0409e ZJS |
80 | prefix = "COREDUMP_PID="; |
81 | else | |
82 | prefix = "COREDUMP_COMM="; | |
83 | ||
e8fb0238 | 84 | pattern = strjoina(prefix, match); |
5ab9ed07 ZJS |
85 | log_debug("Adding match: %s", pattern); |
86 | r = sd_journal_add_match(j, pattern, 0); | |
87 | if (r < 0) | |
88 | return log_error_errno(r, "Failed to add match \"%s\": %m", match); | |
43bfe750 | 89 | |
5ab9ed07 ZJS |
90 | return 0; |
91 | } | |
92 | ||
5ce97d33 | 93 | static int add_matches(sd_journal *j, char **matches) { |
5ab9ed07 | 94 | int r; |
5de0409e | 95 | |
2b044526 | 96 | r = sd_journal_add_match(j, "MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR, 0); |
0f474365 | 97 | if (r < 0) |
2b044526 | 98 | return log_error_errno(r, "Failed to add match \"%s\": %m", "MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR); |
5ab9ed07 | 99 | |
a7581ff9 ZJS |
100 | r = sd_journal_add_match(j, "MESSAGE_ID=" SD_MESSAGE_BACKTRACE_STR, 0); |
101 | if (r < 0) | |
102 | return log_error_errno(r, "Failed to add match \"%s\": %m", "MESSAGE_ID=" SD_MESSAGE_BACKTRACE_STR); | |
103 | ||
5ce97d33 | 104 | STRV_FOREACH(match, matches) { |
5ab9ed07 ZJS |
105 | r = add_match(j, *match); |
106 | if (r < 0) | |
107 | return r; | |
108 | } | |
5de0409e ZJS |
109 | |
110 | return 0; | |
5de0409e ZJS |
111 | } |
112 | ||
5ce97d33 YW |
113 | static int acquire_journal(sd_journal **ret, char **matches) { |
114 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; | |
115 | int r; | |
116 | ||
117 | assert(ret); | |
118 | ||
119 | if (arg_directory) { | |
120 | r = sd_journal_open_directory(&j, arg_directory, 0); | |
121 | if (r < 0) | |
122 | return log_error_errno(r, "Failed to open journals in directory: %s: %m", arg_directory); | |
aeb56450 FS |
123 | } else if (arg_file) { |
124 | r = sd_journal_open_files(&j, (const char**)arg_file, 0); | |
125 | if (r < 0) | |
126 | return log_error_errno(r, "Failed to open journal files: %m"); | |
5ce97d33 | 127 | } else { |
d888ef68 | 128 | r = sd_journal_open(&j, arg_all ? 0 : SD_JOURNAL_LOCAL_ONLY); |
5ce97d33 YW |
129 | if (r < 0) |
130 | return log_error_errno(r, "Failed to open journal: %m"); | |
131 | } | |
132 | ||
133 | r = journal_access_check_and_warn(j, arg_quiet, true); | |
134 | if (r < 0) | |
135 | return r; | |
136 | ||
137 | r = add_matches(j, matches); | |
138 | if (r < 0) | |
139 | return r; | |
140 | ||
141 | if (DEBUG_LOGGING) { | |
c2b2df60 | 142 | _cleanup_free_ char *filter = NULL; |
5ce97d33 YW |
143 | |
144 | filter = journal_make_match_string(j); | |
145 | log_debug("Journal filter: %s", filter); | |
146 | } | |
147 | ||
1cc6c93a | 148 | *ret = TAKE_PTR(j); |
5ce97d33 YW |
149 | |
150 | return 0; | |
151 | } | |
152 | ||
6d8be376 | 153 | static int verb_help(int argc, char **argv, void *userdata) { |
37ec0fdd LP |
154 | _cleanup_free_ char *link = NULL; |
155 | int r; | |
156 | ||
157 | r = terminal_urlify_man("coredumpctl", "1", &link); | |
158 | if (r < 0) | |
159 | return log_oom(); | |
160 | ||
0689cfdb LP |
161 | printf("%1$s [OPTIONS...] COMMAND ...\n\n" |
162 | "%5$sList or retrieve coredumps from the journal.%6$s\n" | |
163 | "\n%3$sCommands:%4$s\n" | |
e1fac8a6 ZJS |
164 | " list [MATCHES...] List available coredumps (default)\n" |
165 | " info [MATCHES...] Show detailed information about one or more coredumps\n" | |
166 | " dump [MATCHES...] Print first matching coredump to stdout\n" | |
167 | " debug [MATCHES...] Start a debugger for the first matching coredump\n" | |
0689cfdb | 168 | "\n%3$sOptions:%4$s\n" |
a2be8be2 MF |
169 | " -h --help Show this help\n" |
170 | " --version Print version string\n" | |
171 | " --no-pager Do not pipe output into a pager\n" | |
172 | " --no-legend Do not print the column headers\n" | |
0689cfdb LP |
173 | " --json=pretty|short|off\n" |
174 | " Generate JSON output\n" | |
a2be8be2 MF |
175 | " --debugger=DEBUGGER Use the given debugger\n" |
176 | " -A --debugger-arguments=ARGS Pass the given arguments to the debugger\n" | |
0689cfdb | 177 | " -n INT Show maximum number of rows\n" |
a2be8be2 MF |
178 | " -1 Show information about most recent entry only\n" |
179 | " -S --since=DATE Only print coredumps since the date\n" | |
180 | " -U --until=DATE Only print coredumps until the date\n" | |
181 | " -r --reverse Show the newest entries first\n" | |
182 | " -F --field=FIELD List all values a certain field takes\n" | |
183 | " -o --output=FILE Write output to FILE\n" | |
184 | " --file=PATH Use journal file\n" | |
185 | " -D --directory=DIR Use journal files from directory\n\n" | |
186 | " -q --quiet Do not show info messages and privilege warning\n" | |
d888ef68 | 187 | " --all Look at all journal files instead of local ones\n" |
0689cfdb | 188 | "\nSee the %2$s for details.\n", |
bc556335 | 189 | program_invocation_short_name, |
0689cfdb LP |
190 | link, |
191 | ansi_underline(), | |
bc556335 | 192 | ansi_normal(), |
0689cfdb LP |
193 | ansi_highlight(), |
194 | ansi_normal()); | |
5ce97d33 YW |
195 | |
196 | return 0; | |
601185b4 ZJS |
197 | } |
198 | ||
5ab9ed07 | 199 | static int parse_argv(int argc, char *argv[]) { |
5de0409e ZJS |
200 | enum { |
201 | ARG_VERSION = 0x100, | |
202 | ARG_NO_PAGER, | |
9a340880 | 203 | ARG_NO_LEGEND, |
0689cfdb | 204 | ARG_JSON, |
c5896b6a | 205 | ARG_DEBUGGER, |
aeb56450 | 206 | ARG_FILE, |
d888ef68 | 207 | ARG_ALL, |
5de0409e ZJS |
208 | }; |
209 | ||
32485d09 | 210 | int c, r; |
5de0409e ZJS |
211 | |
212 | static const struct option options[] = { | |
a2be8be2 MF |
213 | { "help", no_argument, NULL, 'h' }, |
214 | { "version" , no_argument, NULL, ARG_VERSION }, | |
215 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
216 | { "no-legend", no_argument, NULL, ARG_NO_LEGEND }, | |
217 | { "debugger", required_argument, NULL, ARG_DEBUGGER }, | |
218 | { "debugger-arguments", required_argument, NULL, 'A' }, | |
219 | { "output", required_argument, NULL, 'o' }, | |
220 | { "field", required_argument, NULL, 'F' }, | |
221 | { "file", required_argument, NULL, ARG_FILE }, | |
222 | { "directory", required_argument, NULL, 'D' }, | |
223 | { "reverse", no_argument, NULL, 'r' }, | |
224 | { "since", required_argument, NULL, 'S' }, | |
225 | { "until", required_argument, NULL, 'U' }, | |
226 | { "quiet", no_argument, NULL, 'q' }, | |
0689cfdb | 227 | { "json", required_argument, NULL, ARG_JSON }, |
d888ef68 | 228 | { "all", no_argument, NULL, ARG_ALL }, |
eb9da376 | 229 | {} |
5de0409e ZJS |
230 | }; |
231 | ||
232 | assert(argc >= 0); | |
233 | assert(argv); | |
234 | ||
0689cfdb | 235 | while ((c = getopt_long(argc, argv, "hA:o:F:1D:rS:U:qn:", options, NULL)) >= 0) |
5de0409e ZJS |
236 | switch(c) { |
237 | case 'h': | |
6d8be376 | 238 | return verb_help(0, NULL, NULL); |
5de0409e ZJS |
239 | |
240 | case ARG_VERSION: | |
3f6fd1ba | 241 | return version(); |
5de0409e ZJS |
242 | |
243 | case ARG_NO_PAGER: | |
0221d68a | 244 | arg_pager_flags |= PAGER_DISABLE; |
5de0409e ZJS |
245 | break; |
246 | ||
9a340880 | 247 | case ARG_NO_LEGEND: |
0689cfdb | 248 | arg_legend = false; |
9a340880 ZJS |
249 | break; |
250 | ||
c5896b6a RG |
251 | case ARG_DEBUGGER: |
252 | arg_debugger = optarg; | |
253 | break; | |
254 | ||
a2be8be2 MF |
255 | case 'A': { |
256 | _cleanup_strv_free_ char **l = NULL; | |
257 | r = strv_split_full(&l, optarg, WHITESPACE, EXTRACT_UNQUOTE); | |
258 | if (r < 0) | |
259 | return log_error_errno(r, "Failed to parse debugger arguments '%s': %m", optarg); | |
260 | strv_free_and_replace(arg_debugger_args, l); | |
261 | break; | |
262 | } | |
263 | ||
aeb56450 | 264 | case ARG_FILE: |
544e146b | 265 | r = glob_extend(&arg_file, optarg, GLOB_NOCHECK); |
aeb56450 FS |
266 | if (r < 0) |
267 | return log_error_errno(r, "Failed to add paths: %m"); | |
268 | break; | |
269 | ||
5de0409e | 270 | case 'o': |
baaa35ad ZJS |
271 | if (arg_output) |
272 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
273 | "Cannot set output more than once."); | |
5de0409e | 274 | |
158ecef5 | 275 | arg_output = optarg; |
5de0409e | 276 | break; |
57ce4bd4 | 277 | |
32485d09 GS |
278 | case 'S': |
279 | r = parse_timestamp(optarg, &arg_since); | |
280 | if (r < 0) | |
9d77ca39 | 281 | return log_error_errno(r, "Failed to parse timestamp '%s': %m", optarg); |
32485d09 GS |
282 | break; |
283 | ||
284 | case 'U': | |
285 | r = parse_timestamp(optarg, &arg_until); | |
286 | if (r < 0) | |
9d77ca39 | 287 | return log_error_errno(r, "Failed to parse timestamp '%s': %m", optarg); |
32485d09 GS |
288 | break; |
289 | ||
4f76ae1b | 290 | case 'F': |
baaa35ad ZJS |
291 | if (arg_field) |
292 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
293 | "Cannot use --field/-F more than once."); | |
a276ae74 | 294 | arg_field = optarg; |
4f76ae1b ZJS |
295 | break; |
296 | ||
0c51aada | 297 | case '1': |
0689cfdb LP |
298 | arg_rows_max = 1; |
299 | arg_reverse = true; | |
0c51aada LP |
300 | break; |
301 | ||
0689cfdb LP |
302 | case 'n': { |
303 | unsigned n; | |
304 | ||
305 | r = safe_atou(optarg, &n); | |
306 | if (r < 0 || n < 1) | |
307 | return log_error_errno(r < 0 ? r : SYNTHETIC_ERRNO(EINVAL), | |
308 | "Invalid numeric parameter to -n: %s", optarg); | |
309 | ||
310 | arg_rows_max = n; | |
311 | break; | |
312 | } | |
313 | ||
b73e9a02 SW |
314 | case 'D': |
315 | arg_directory = optarg; | |
316 | break; | |
317 | ||
df65f77b NK |
318 | case 'r': |
319 | arg_reverse = true; | |
320 | break; | |
321 | ||
b9aaa7f4 ZJS |
322 | case 'q': |
323 | arg_quiet = true; | |
324 | break; | |
325 | ||
0689cfdb | 326 | case ARG_JSON: |
b1e8f46c | 327 | r = parse_json_argument(optarg, &arg_json_format_flags); |
0689cfdb LP |
328 | if (r <= 0) |
329 | return r; | |
330 | ||
331 | break; | |
332 | ||
d888ef68 DDM |
333 | case ARG_ALL: |
334 | arg_all = true; | |
335 | break; | |
336 | ||
57ce4bd4 ZJS |
337 | case '?': |
338 | return -EINVAL; | |
339 | ||
5de0409e | 340 | default: |
04499a70 | 341 | assert_not_reached(); |
5de0409e ZJS |
342 | } |
343 | ||
32485d09 | 344 | if (arg_since != USEC_INFINITY && arg_until != USEC_INFINITY && |
baaa35ad ZJS |
345 | arg_since > arg_until) |
346 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
347 | "--since= must be before --until=."); | |
32485d09 | 348 | |
5ce97d33 | 349 | return 1; |
5de0409e ZJS |
350 | } |
351 | ||
8bc8ab83 LP |
352 | static int retrieve(const void *data, |
353 | size_t len, | |
354 | const char *name, | |
a276ae74 | 355 | char **var) { |
5de0409e | 356 | |
4f76ae1b | 357 | size_t ident; |
a276ae74 | 358 | char *v; |
8bc8ab83 | 359 | |
4f76ae1b | 360 | ident = strlen(name) + 1; /* name + "=" */ |
8bc8ab83 | 361 | |
4f76ae1b | 362 | if (len < ident) |
8bc8ab83 | 363 | return 0; |
5de0409e | 364 | |
4f76ae1b | 365 | if (memcmp(data, name, ident - 1) != 0) |
8bc8ab83 LP |
366 | return 0; |
367 | ||
4f76ae1b | 368 | if (((const char*) data)[ident - 1] != '=') |
8bc8ab83 | 369 | return 0; |
5de0409e | 370 | |
a276ae74 LP |
371 | v = strndup((const char*)data + ident, len - ident); |
372 | if (!v) | |
5de0409e ZJS |
373 | return log_oom(); |
374 | ||
eba048bb | 375 | free_and_replace(*var, v); |
062b99e8 | 376 | return 1; |
5de0409e ZJS |
377 | } |
378 | ||
062b99e8 | 379 | static int print_field(FILE* file, sd_journal *j) { |
4f76ae1b ZJS |
380 | const void *d; |
381 | size_t l; | |
382 | ||
e15758cc LP |
383 | assert(file); |
384 | assert(j); | |
385 | ||
a276ae74 | 386 | assert(arg_field); |
4f76ae1b | 387 | |
062b99e8 | 388 | /* A (user-specified) field may appear more than once for a given entry. |
5238e957 | 389 | * We will print all of the occurrences. |
062b99e8 ZJS |
390 | * This is different below for fields that systemd-coredump uses, |
391 | * because they cannot meaningfully appear more than once. | |
392 | */ | |
393 | SD_JOURNAL_FOREACH_DATA(j, d, l) { | |
394 | _cleanup_free_ char *value = NULL; | |
395 | int r; | |
396 | ||
397 | r = retrieve(d, l, arg_field, &value); | |
398 | if (r < 0) | |
399 | return r; | |
400 | if (r > 0) | |
401 | fprintf(file, "%s\n", value); | |
402 | } | |
a276ae74 | 403 | |
062b99e8 | 404 | return 0; |
4f76ae1b ZJS |
405 | } |
406 | ||
062b99e8 ZJS |
407 | #define RETRIEVE(d, l, name, arg) \ |
408 | { \ | |
409 | int _r = retrieve(d, l, name, &arg); \ | |
410 | if (_r < 0) \ | |
411 | return _r; \ | |
412 | if (_r > 0) \ | |
413 | continue; \ | |
414 | } | |
415 | ||
4016281c LP |
416 | static void analyze_coredump_file( |
417 | const char *path, | |
418 | const char **ret_state, | |
419 | const char **ret_color, | |
420 | uint64_t *ret_size) { | |
421 | ||
422 | _cleanup_close_ int fd = -1; | |
423 | struct stat st; | |
424 | int r; | |
425 | ||
426 | assert(path); | |
427 | assert(ret_state); | |
428 | assert(ret_color); | |
429 | assert(ret_size); | |
430 | ||
431 | fd = open(path, O_PATH|O_CLOEXEC); | |
432 | if (fd < 0) { | |
433 | if (errno == ENOENT) { | |
434 | *ret_state = "missing"; | |
435 | *ret_color = ansi_grey(); | |
436 | *ret_size = UINT64_MAX; | |
437 | return; | |
438 | } | |
439 | ||
440 | r = -errno; | |
441 | } else | |
442 | r = access_fd(fd, R_OK); | |
443 | if (ERRNO_IS_PRIVILEGE(r)) { | |
444 | *ret_state = "inaccessible"; | |
445 | *ret_color = ansi_highlight_yellow(); | |
446 | *ret_size = UINT64_MAX; | |
447 | return; | |
448 | } | |
449 | if (r < 0) | |
450 | goto error; | |
451 | ||
452 | if (fstat(fd, &st) < 0) | |
453 | goto error; | |
454 | ||
455 | if (!S_ISREG(st.st_mode)) | |
456 | goto error; | |
457 | ||
458 | *ret_state = "present"; | |
459 | *ret_color = NULL; | |
460 | *ret_size = st.st_size; | |
461 | return; | |
462 | ||
463 | error: | |
464 | *ret_state = "error"; | |
465 | *ret_color = ansi_highlight_red(); | |
466 | *ret_size = UINT64_MAX; | |
467 | } | |
468 | ||
0689cfdb | 469 | static int print_list(FILE* file, sd_journal *j, Table *t) { |
a276ae74 | 470 | _cleanup_free_ char |
a7581ff9 | 471 | *mid = NULL, *pid = NULL, *uid = NULL, *gid = NULL, |
9fe13294 | 472 | *sgnl = NULL, *exe = NULL, *comm = NULL, *cmdline = NULL, |
cc4419ed | 473 | *filename = NULL, *truncated = NULL, *coredump = NULL; |
8bc8ab83 LP |
474 | const void *d; |
475 | size_t l; | |
0689cfdb LP |
476 | usec_t ts; |
477 | int r, signal_as_int = 0; | |
4016281c LP |
478 | const char *present = NULL, *color = NULL; |
479 | uint64_t size = UINT64_MAX; | |
a7581ff9 | 480 | bool normal_coredump; |
0689cfdb LP |
481 | uid_t uid_as_int = UID_INVALID; |
482 | gid_t gid_as_int = GID_INVALID; | |
483 | pid_t pid_as_int = 0; | |
8bc8ab83 | 484 | |
e15758cc LP |
485 | assert(file); |
486 | assert(j); | |
0689cfdb | 487 | assert(t); |
e15758cc | 488 | |
8bc8ab83 | 489 | SD_JOURNAL_FOREACH_DATA(j, d, l) { |
a7581ff9 | 490 | RETRIEVE(d, l, "MESSAGE_ID", mid); |
062b99e8 ZJS |
491 | RETRIEVE(d, l, "COREDUMP_PID", pid); |
492 | RETRIEVE(d, l, "COREDUMP_UID", uid); | |
493 | RETRIEVE(d, l, "COREDUMP_GID", gid); | |
494 | RETRIEVE(d, l, "COREDUMP_SIGNAL", sgnl); | |
495 | RETRIEVE(d, l, "COREDUMP_EXE", exe); | |
496 | RETRIEVE(d, l, "COREDUMP_COMM", comm); | |
497 | RETRIEVE(d, l, "COREDUMP_CMDLINE", cmdline); | |
498 | RETRIEVE(d, l, "COREDUMP_FILENAME", filename); | |
cc4419ed | 499 | RETRIEVE(d, l, "COREDUMP_TRUNCATED", truncated); |
062b99e8 | 500 | RETRIEVE(d, l, "COREDUMP", coredump); |
8bc8ab83 | 501 | } |
5de0409e | 502 | |
0689cfdb LP |
503 | if (!pid && !uid && !gid && !sgnl && !exe && !comm && !cmdline && !filename) |
504 | return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), "Empty coredump log entry"); | |
684341b0 | 505 | |
0689cfdb LP |
506 | (void) parse_uid(uid, &uid_as_int); |
507 | (void) parse_gid(gid, &gid_as_int); | |
508 | (void) parse_pid(pid, &pid_as_int); | |
509 | signal_as_int = signal_from_string(sgnl); | |
510 | ||
511 | r = sd_journal_get_realtime_usec(j, &ts); | |
23bbb0de MS |
512 | if (r < 0) |
513 | return log_error_errno(r, "Failed to get realtime timestamp: %m"); | |
5de0409e | 514 | |
a7581ff9 ZJS |
515 | normal_coredump = streq_ptr(mid, SD_MESSAGE_COREDUMP_STR); |
516 | ||
04de5879 | 517 | if (filename) |
4016281c | 518 | analyze_coredump_file(filename, &present, &color, &size); |
04de5879 ZJS |
519 | else if (coredump) |
520 | present = "journal"; | |
0689cfdb | 521 | else if (normal_coredump) { |
04de5879 | 522 | present = "none"; |
0689cfdb LP |
523 | color = ansi_grey(); |
524 | } else | |
525 | present = NULL; | |
04de5879 | 526 | |
0689cfdb | 527 | if (STRPTR_IN_SET(present, "present", "journal") && truncated && parse_boolean(truncated) > 0) |
cc4419ed ZJS |
528 | present = "truncated"; |
529 | ||
0689cfdb LP |
530 | r = table_add_many( |
531 | t, | |
532 | TABLE_TIMESTAMP, ts, | |
533 | TABLE_PID, pid_as_int, | |
534 | TABLE_UID, uid_as_int, | |
535 | TABLE_GID, gid_as_int, | |
536 | TABLE_SIGNAL, normal_coredump ? signal_as_int : 0, | |
537 | TABLE_STRING, present, | |
538 | TABLE_SET_COLOR, color, | |
4016281c LP |
539 | TABLE_STRING, exe ?: comm ?: cmdline, |
540 | TABLE_SIZE, size); | |
0689cfdb LP |
541 | if (r < 0) |
542 | return table_log_add_error(r); | |
684341b0 LP |
543 | |
544 | return 0; | |
5de0409e ZJS |
545 | } |
546 | ||
e15758cc LP |
547 | static int print_info(FILE *file, sd_journal *j, bool need_space) { |
548 | _cleanup_free_ char | |
a7581ff9 | 549 | *mid = NULL, *pid = NULL, *uid = NULL, *gid = NULL, |
e15758cc LP |
550 | *sgnl = NULL, *exe = NULL, *comm = NULL, *cmdline = NULL, |
551 | *unit = NULL, *user_unit = NULL, *session = NULL, | |
a035f819 | 552 | *boot_id = NULL, *machine_id = NULL, *hostname = NULL, |
9fe13294 | 553 | *slice = NULL, *cgroup = NULL, *owner_uid = NULL, |
47f50642 | 554 | *message = NULL, *timestamp = NULL, *filename = NULL, |
d1b5a0c6 LB |
555 | *truncated = NULL, *coredump = NULL, |
556 | *pkgmeta_name = NULL, *pkgmeta_version = NULL, *pkgmeta_json = NULL; | |
e15758cc LP |
557 | const void *d; |
558 | size_t l; | |
a7581ff9 | 559 | bool normal_coredump; |
4b8cbe9a | 560 | int r; |
e15758cc LP |
561 | |
562 | assert(file); | |
563 | assert(j); | |
564 | ||
384c6207 ZJS |
565 | (void) sd_journal_set_data_threshold(j, 0); |
566 | ||
e15758cc | 567 | SD_JOURNAL_FOREACH_DATA(j, d, l) { |
a7581ff9 | 568 | RETRIEVE(d, l, "MESSAGE_ID", mid); |
062b99e8 ZJS |
569 | RETRIEVE(d, l, "COREDUMP_PID", pid); |
570 | RETRIEVE(d, l, "COREDUMP_UID", uid); | |
571 | RETRIEVE(d, l, "COREDUMP_GID", gid); | |
572 | RETRIEVE(d, l, "COREDUMP_SIGNAL", sgnl); | |
573 | RETRIEVE(d, l, "COREDUMP_EXE", exe); | |
574 | RETRIEVE(d, l, "COREDUMP_COMM", comm); | |
575 | RETRIEVE(d, l, "COREDUMP_CMDLINE", cmdline); | |
07461598 | 576 | RETRIEVE(d, l, "COREDUMP_HOSTNAME", hostname); |
062b99e8 ZJS |
577 | RETRIEVE(d, l, "COREDUMP_UNIT", unit); |
578 | RETRIEVE(d, l, "COREDUMP_USER_UNIT", user_unit); | |
579 | RETRIEVE(d, l, "COREDUMP_SESSION", session); | |
580 | RETRIEVE(d, l, "COREDUMP_OWNER_UID", owner_uid); | |
581 | RETRIEVE(d, l, "COREDUMP_SLICE", slice); | |
582 | RETRIEVE(d, l, "COREDUMP_CGROUP", cgroup); | |
583 | RETRIEVE(d, l, "COREDUMP_TIMESTAMP", timestamp); | |
584 | RETRIEVE(d, l, "COREDUMP_FILENAME", filename); | |
cc4419ed | 585 | RETRIEVE(d, l, "COREDUMP_TRUNCATED", truncated); |
062b99e8 | 586 | RETRIEVE(d, l, "COREDUMP", coredump); |
671769c9 LB |
587 | RETRIEVE(d, l, "COREDUMP_PACKAGE_NAME", pkgmeta_name); |
588 | RETRIEVE(d, l, "COREDUMP_PACKAGE_VERSION", pkgmeta_version); | |
589 | RETRIEVE(d, l, "COREDUMP_PACKAGE_JSON", pkgmeta_json); | |
062b99e8 ZJS |
590 | RETRIEVE(d, l, "_BOOT_ID", boot_id); |
591 | RETRIEVE(d, l, "_MACHINE_ID", machine_id); | |
062b99e8 | 592 | RETRIEVE(d, l, "MESSAGE", message); |
e15758cc LP |
593 | } |
594 | ||
595 | if (need_space) | |
596 | fputs("\n", file); | |
597 | ||
a7581ff9 ZJS |
598 | normal_coredump = streq_ptr(mid, SD_MESSAGE_COREDUMP_STR); |
599 | ||
81cef14f LP |
600 | if (comm) |
601 | fprintf(file, | |
602 | " PID: %s%s%s (%s)\n", | |
1fc464f6 | 603 | ansi_highlight(), strna(pid), ansi_normal(), comm); |
81cef14f LP |
604 | else |
605 | fprintf(file, | |
606 | " PID: %s%s%s\n", | |
1fc464f6 | 607 | ansi_highlight(), strna(pid), ansi_normal()); |
a035f819 LP |
608 | |
609 | if (uid) { | |
610 | uid_t n; | |
611 | ||
612 | if (parse_uid(uid, &n) >= 0) { | |
613 | _cleanup_free_ char *u = NULL; | |
614 | ||
615 | u = uid_to_name(n); | |
616 | fprintf(file, | |
617 | " UID: %s (%s)\n", | |
618 | uid, u); | |
619 | } else { | |
620 | fprintf(file, | |
621 | " UID: %s\n", | |
622 | uid); | |
623 | } | |
624 | } | |
625 | ||
626 | if (gid) { | |
627 | gid_t n; | |
628 | ||
629 | if (parse_gid(gid, &n) >= 0) { | |
630 | _cleanup_free_ char *g = NULL; | |
631 | ||
632 | g = gid_to_name(n); | |
633 | fprintf(file, | |
634 | " GID: %s (%s)\n", | |
635 | gid, g); | |
636 | } else { | |
637 | fprintf(file, | |
638 | " GID: %s\n", | |
639 | gid); | |
640 | } | |
641 | } | |
e15758cc LP |
642 | |
643 | if (sgnl) { | |
644 | int sig; | |
a7581ff9 | 645 | const char *name = normal_coredump ? "Signal" : "Reason"; |
e15758cc | 646 | |
a7581ff9 ZJS |
647 | if (normal_coredump && safe_atoi(sgnl, &sig) >= 0) |
648 | fprintf(file, " %s: %s (%s)\n", name, sgnl, signal_to_string(sig)); | |
e15758cc | 649 | else |
a7581ff9 | 650 | fprintf(file, " %s: %s\n", name, sgnl); |
e15758cc LP |
651 | } |
652 | ||
4b8cbe9a LP |
653 | if (timestamp) { |
654 | usec_t u; | |
655 | ||
656 | r = safe_atou64(timestamp, &u); | |
32fc5c47 ZJS |
657 | if (r >= 0) |
658 | fprintf(file, " Timestamp: %s (%s)\n", | |
659 | FORMAT_TIMESTAMP(u), FORMAT_TIMESTAMP_RELATIVE(u)); | |
4b8cbe9a | 660 | |
32fc5c47 | 661 | else |
4b8cbe9a LP |
662 | fprintf(file, " Timestamp: %s\n", timestamp); |
663 | } | |
664 | ||
e15758cc LP |
665 | if (cmdline) |
666 | fprintf(file, " Command Line: %s\n", cmdline); | |
81cef14f | 667 | if (exe) |
1fc464f6 | 668 | fprintf(file, " Executable: %s%s%s\n", ansi_highlight(), exe, ansi_normal()); |
a035f819 LP |
669 | if (cgroup) |
670 | fprintf(file, " Control Group: %s\n", cgroup); | |
e15758cc LP |
671 | if (unit) |
672 | fprintf(file, " Unit: %s\n", unit); | |
673 | if (user_unit) | |
554ed50f | 674 | fprintf(file, " User Unit: %s\n", user_unit); |
a035f819 LP |
675 | if (slice) |
676 | fprintf(file, " Slice: %s\n", slice); | |
e15758cc LP |
677 | if (session) |
678 | fprintf(file, " Session: %s\n", session); | |
a035f819 LP |
679 | if (owner_uid) { |
680 | uid_t n; | |
681 | ||
682 | if (parse_uid(owner_uid, &n) >= 0) { | |
683 | _cleanup_free_ char *u = NULL; | |
684 | ||
685 | u = uid_to_name(n); | |
686 | fprintf(file, | |
687 | " Owner UID: %s (%s)\n", | |
688 | owner_uid, u); | |
689 | } else { | |
690 | fprintf(file, | |
691 | " Owner UID: %s\n", | |
692 | owner_uid); | |
693 | } | |
694 | } | |
e15758cc LP |
695 | if (boot_id) |
696 | fprintf(file, " Boot ID: %s\n", boot_id); | |
697 | if (machine_id) | |
698 | fprintf(file, " Machine ID: %s\n", machine_id); | |
699 | if (hostname) | |
700 | fprintf(file, " Hostname: %s\n", hostname); | |
701 | ||
cc4419ed | 702 | if (filename) { |
4016281c LP |
703 | const char *state = NULL, *color = NULL; |
704 | uint64_t size = UINT64_MAX; | |
cc4419ed | 705 | |
4016281c LP |
706 | analyze_coredump_file(filename, &state, &color, &size); |
707 | ||
708 | if (STRPTR_IN_SET(state, "present", "journal") && truncated && parse_boolean(truncated) > 0) | |
709 | state = "truncated"; | |
710 | ||
711 | fprintf(file, | |
712 | " Storage: %s%s (%s)%s\n", | |
713 | strempty(color), | |
714 | filename, | |
715 | state, | |
716 | ansi_normal()); | |
717 | ||
718 | if (size != UINT64_MAX) | |
2b59bf51 ZJS |
719 | fprintf(file, " Disk Size: %s\n", FORMAT_BYTES(size)); |
720 | ||
4016281c | 721 | } else if (coredump) |
47f50642 ZJS |
722 | fprintf(file, " Storage: journal\n"); |
723 | else | |
724 | fprintf(file, " Storage: none\n"); | |
e15758cc | 725 | |
d1b5a0c6 LB |
726 | if (pkgmeta_name && pkgmeta_version) |
727 | fprintf(file, " Package: %s/%s\n", pkgmeta_name, pkgmeta_version); | |
728 | ||
729 | /* Print out the build-id of the 'main' ELF module, by matching the JSON key | |
730 | * with the 'exe' field. */ | |
731 | if (exe && pkgmeta_json) { | |
732 | _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; | |
733 | ||
734 | r = json_parse(pkgmeta_json, 0, &v, NULL, NULL); | |
9b7205f5 | 735 | if (r < 0) |
d1b5a0c6 | 736 | log_warning_errno(r, "json_parse on %s failed, ignoring: %m", pkgmeta_json); |
9b7205f5 | 737 | else { |
d1b5a0c6 LB |
738 | const char *module_name; |
739 | JsonVariant *module_json; | |
740 | ||
b7ddd444 | 741 | JSON_VARIANT_OBJECT_FOREACH(module_name, module_json, v) { |
b7ddd444 | 742 | JsonVariant *build_id; |
d1b5a0c6 | 743 | |
d1b5a0c6 | 744 | /* We only print the build-id for the 'main' ELF module */ |
289784a7 | 745 | if (!path_equal_filename(module_name, exe)) |
d1b5a0c6 LB |
746 | continue; |
747 | ||
b7ddd444 LB |
748 | build_id = json_variant_by_key(module_json, "buildId"); |
749 | if (build_id) | |
750 | fprintf(file, " build-id: %s\n", json_variant_string(build_id)); | |
d1b5a0c6 LB |
751 | |
752 | break; | |
753 | } | |
754 | } | |
755 | } | |
756 | ||
8d4e028f LP |
757 | if (message) { |
758 | _cleanup_free_ char *m = NULL; | |
759 | ||
760 | m = strreplace(message, "\n", "\n "); | |
761 | ||
762 | fprintf(file, " Message: %s\n", strstrip(m ?: message)); | |
763 | } | |
764 | ||
e15758cc LP |
765 | return 0; |
766 | } | |
767 | ||
ada45c78 | 768 | static int focus(sd_journal *j) { |
5de0409e ZJS |
769 | int r; |
770 | ||
5de0409e ZJS |
771 | r = sd_journal_seek_tail(j); |
772 | if (r == 0) | |
773 | r = sd_journal_previous(j); | |
23bbb0de MS |
774 | if (r < 0) |
775 | return log_error_errno(r, "Failed to search journal: %m"); | |
baaa35ad ZJS |
776 | if (r == 0) |
777 | return log_error_errno(SYNTHETIC_ERRNO(ESRCH), | |
778 | "No match found."); | |
ada45c78 LP |
779 | return r; |
780 | } | |
5de0409e | 781 | |
0689cfdb LP |
782 | static int print_entry( |
783 | sd_journal *j, | |
784 | size_t n_found, | |
785 | Table *t) { | |
786 | ||
0c51aada LP |
787 | assert(j); |
788 | ||
0689cfdb LP |
789 | if (t) |
790 | return print_list(stdout, j, t); | |
0c51aada | 791 | else if (arg_field) |
062b99e8 | 792 | return print_field(stdout, j); |
0c51aada | 793 | else |
0689cfdb | 794 | return print_info(stdout, j, n_found > 0); |
0c51aada LP |
795 | } |
796 | ||
5ce97d33 YW |
797 | static int dump_list(int argc, char **argv, void *userdata) { |
798 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; | |
0689cfdb LP |
799 | _cleanup_(table_unrefp) Table *t = NULL; |
800 | size_t n_found = 0; | |
5ce97d33 | 801 | bool verb_is_info; |
0c51aada LP |
802 | int r; |
803 | ||
0689cfdb | 804 | verb_is_info = argc >= 1 && streq(argv[0], "info"); |
5ce97d33 YW |
805 | |
806 | r = acquire_journal(&j, argv + 1); | |
807 | if (r < 0) | |
808 | return r; | |
809 | ||
0689cfdb LP |
810 | /* The coredumps are likely compressed, and for just listing them we don't need to decompress them, |
811 | * so let's pick a fairly low data threshold here */ | |
812 | (void) sd_journal_set_data_threshold(j, 4096); | |
0c51aada | 813 | |
0689cfdb | 814 | if (!verb_is_info && !arg_field) { |
4016281c | 815 | t = table_new("time", "pid", "uid", "gid", "sig", "corefile", "exe", "size"); |
0689cfdb LP |
816 | if (!t) |
817 | return log_oom(); | |
818 | ||
819 | (void) table_set_align_percent(t, TABLE_HEADER_CELL(1), 100); | |
820 | (void) table_set_align_percent(t, TABLE_HEADER_CELL(2), 100); | |
821 | (void) table_set_align_percent(t, TABLE_HEADER_CELL(3), 100); | |
4016281c | 822 | (void) table_set_align_percent(t, TABLE_HEADER_CELL(7), 100); |
0689cfdb LP |
823 | |
824 | (void) table_set_empty_string(t, "-"); | |
825 | } else | |
384c2c32 | 826 | pager_open(arg_pager_flags); |
0c51aada | 827 | |
2fafabfd | 828 | /* "info" without pattern implies "-1" */ |
0689cfdb | 829 | if ((arg_rows_max == 1 && arg_reverse) || (verb_is_info && argc == 1)) { |
0c51aada LP |
830 | r = focus(j); |
831 | if (r < 0) | |
832 | return r; | |
833 | ||
0689cfdb LP |
834 | r = print_entry(j, 0, t); |
835 | if (r < 0) | |
836 | return r; | |
0c51aada | 837 | } else { |
32485d09 GS |
838 | if (arg_since != USEC_INFINITY && !arg_reverse) |
839 | r = sd_journal_seek_realtime_usec(j, arg_since); | |
840 | else if (arg_until != USEC_INFINITY && arg_reverse) | |
841 | r = sd_journal_seek_realtime_usec(j, arg_until); | |
842 | else if (arg_reverse) | |
843 | r = sd_journal_seek_tail(j); | |
844 | else | |
845 | r = sd_journal_seek_head(j); | |
846 | if (r < 0) | |
847 | return log_error_errno(r, "Failed to seek to date: %m"); | |
848 | ||
849 | for (;;) { | |
850 | if (!arg_reverse) | |
851 | r = sd_journal_next(j); | |
852 | else | |
853 | r = sd_journal_previous(j); | |
32485d09 GS |
854 | if (r < 0) |
855 | return log_error_errno(r, "Failed to iterate through journal: %m"); | |
32485d09 GS |
856 | if (r == 0) |
857 | break; | |
858 | ||
859 | if (arg_until != USEC_INFINITY && !arg_reverse) { | |
860 | usec_t usec; | |
861 | ||
862 | r = sd_journal_get_realtime_usec(j, &usec); | |
df65f77b | 863 | if (r < 0) |
32485d09 GS |
864 | return log_error_errno(r, "Failed to determine timestamp: %m"); |
865 | if (usec > arg_until) | |
866 | continue; | |
df65f77b | 867 | } |
32485d09 GS |
868 | |
869 | if (arg_since != USEC_INFINITY && arg_reverse) { | |
870 | usec_t usec; | |
871 | ||
872 | r = sd_journal_get_realtime_usec(j, &usec); | |
df65f77b | 873 | if (r < 0) |
32485d09 GS |
874 | return log_error_errno(r, "Failed to determine timestamp: %m"); |
875 | if (usec < arg_since) | |
876 | continue; | |
df65f77b | 877 | } |
32485d09 | 878 | |
0689cfdb | 879 | r = print_entry(j, n_found++, t); |
32485d09 GS |
880 | if (r < 0) |
881 | return r; | |
0689cfdb LP |
882 | |
883 | if (arg_rows_max != SIZE_MAX && n_found >= arg_rows_max) | |
884 | break; | |
062b99e8 | 885 | } |
0c51aada LP |
886 | |
887 | if (!arg_field && n_found <= 0) { | |
b9aaa7f4 ZJS |
888 | if (!arg_quiet) |
889 | log_notice("No coredumps found."); | |
0c51aada LP |
890 | return -ESRCH; |
891 | } | |
892 | } | |
893 | ||
0689cfdb LP |
894 | if (t) { |
895 | r = table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend); | |
896 | if (r < 0) | |
897 | return r; | |
898 | } | |
899 | ||
0c51aada LP |
900 | return 0; |
901 | } | |
902 | ||
bb7c5bad | 903 | static int save_core(sd_journal *j, FILE *file, char **path, bool *unlink_temp) { |
9fe13294 ZJS |
904 | const char *data; |
905 | _cleanup_free_ char *filename = NULL; | |
906 | size_t len; | |
bb7c5bad | 907 | int r, fd; |
fc6cec86 ZJS |
908 | _cleanup_close_ int fdt = -1; |
909 | char *temp = NULL; | |
ada45c78 | 910 | |
bb7c5bad ZJS |
911 | assert(!(file && path)); /* At most one can be specified */ |
912 | assert(!!path == !!unlink_temp); /* Those must be specified together */ | |
47f50642 | 913 | |
fc6cec86 | 914 | /* Look for a coredump on disk first. */ |
9fe13294 | 915 | r = sd_journal_get_data(j, "COREDUMP_FILENAME", (const void**) &data, &len); |
2d0a880f ZJS |
916 | if (r == 0) { |
917 | r = retrieve(data, len, "COREDUMP_FILENAME", &filename); | |
918 | if (r < 0) | |
919 | return r; | |
920 | assert(r > 0); | |
921 | ||
922 | if (access(filename, R_OK) < 0) | |
923 | return log_error_errno(errno, "File \"%s\" is not readable: %m", filename); | |
924 | ||
ef5924aa | 925 | if (path && !ENDSWITH_SET(filename, ".xz", ".lz4", ".zst")) { |
2d0a880f ZJS |
926 | *path = TAKE_PTR(filename); |
927 | ||
928 | return 0; | |
929 | } | |
930 | ||
931 | } else { | |
bb7c5bad ZJS |
932 | if (r != -ENOENT) |
933 | return log_error_errno(r, "Failed to retrieve COREDUMP_FILENAME field: %m"); | |
934 | /* Check that we can have a COREDUMP field. We still haven't set a high | |
935 | * data threshold, so we'll get a few kilobytes at most. | |
936 | */ | |
937 | ||
938 | r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len); | |
939 | if (r == -ENOENT) | |
940 | return log_error_errno(r, "Coredump entry has no core attached (neither internally in the journal nor externally on disk)."); | |
941 | if (r < 0) | |
942 | return log_error_errno(r, "Failed to retrieve COREDUMP field: %m"); | |
943 | } | |
93b73b06 | 944 | |
bb7c5bad | 945 | if (path) { |
fc6cec86 | 946 | const char *vt; |
992e8f22 | 947 | |
fc6cec86 | 948 | /* Create a temporary file to write the uncompressed core to. */ |
992e8f22 | 949 | |
fc6cec86 ZJS |
950 | r = var_tmp_dir(&vt); |
951 | if (r < 0) | |
952 | return log_error_errno(r, "Failed to acquire temporary directory path: %m"); | |
9fe13294 | 953 | |
657ee2d8 | 954 | temp = path_join(vt, "coredump-XXXXXX"); |
fc6cec86 ZJS |
955 | if (!temp) |
956 | return log_oom(); | |
9fe13294 | 957 | |
fc6cec86 ZJS |
958 | fdt = mkostemp_safe(temp); |
959 | if (fdt < 0) | |
960 | return log_error_errno(fdt, "Failed to create temporary file: %m"); | |
961 | log_debug("Created temporary file %s", temp); | |
9fe13294 | 962 | |
fc6cec86 | 963 | fd = fdt; |
bb7c5bad ZJS |
964 | } else { |
965 | /* If neither path or file are specified, we will write to stdout. Let's now check | |
966 | * if stdout is connected to a tty. We checked that the file exists, or that the | |
967 | * core might be stored in the journal. In this second case, if we found the entry, | |
5238e957 | 968 | * in all likelihood we will be able to access the COREDUMP= field. In either case, |
bb7c5bad ZJS |
969 | * we stop before doing any "real" work, i.e. before starting decompression or |
970 | * reading from the file or creating temporary files. | |
971 | */ | |
972 | if (!file) { | |
973 | if (on_tty()) | |
886cf317 ZJS |
974 | return log_error_errno(SYNTHETIC_ERRNO(ENOTTY), |
975 | "Refusing to dump core to tty" | |
bb7c5bad ZJS |
976 | " (use shell redirection or specify --output)."); |
977 | file = stdout; | |
978 | } | |
979 | ||
980 | fd = fileno(file); | |
fc6cec86 ZJS |
981 | } |
982 | ||
983 | if (filename) { | |
d80b051c | 984 | #if HAVE_COMPRESSION |
c2b2df60 | 985 | _cleanup_close_ int fdf = -1; |
9fe13294 | 986 | |
fc6cec86 ZJS |
987 | fdf = open(filename, O_RDONLY | O_CLOEXEC); |
988 | if (fdf < 0) { | |
989 | r = log_error_errno(errno, "Failed to open %s: %m", filename); | |
2fb8159f | 990 | goto error; |
fc6cec86 ZJS |
991 | } |
992 | ||
993 | r = decompress_stream(filename, fdf, fd, -1); | |
994 | if (r < 0) { | |
995 | log_error_errno(r, "Failed to decompress %s: %m", filename); | |
9fe13294 ZJS |
996 | goto error; |
997 | } | |
fc6cec86 | 998 | #else |
3afe5c00 LP |
999 | r = log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
1000 | "Cannot decompress file. Compiled without compression support."); | |
fc6cec86 ZJS |
1001 | goto error; |
1002 | #endif | |
1003 | } else { | |
1004 | ssize_t sz; | |
a276ae74 | 1005 | |
bb7c5bad ZJS |
1006 | /* We want full data, nothing truncated. */ |
1007 | sd_journal_set_data_threshold(j, 0); | |
1008 | ||
fc6cec86 ZJS |
1009 | r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len); |
1010 | if (r < 0) | |
bb7c5bad | 1011 | return log_error_errno(r, "Failed to retrieve COREDUMP field: %m"); |
fc6cec86 ZJS |
1012 | |
1013 | assert(len >= 9); | |
1014 | data += 9; | |
1015 | len -= 9; | |
1016 | ||
954d3a51 | 1017 | sz = write(fd, data, len); |
fc6cec86 | 1018 | if (sz < 0) { |
954d3a51 | 1019 | r = log_error_errno(errno, "Failed to write output: %m"); |
fc6cec86 | 1020 | goto error; |
a276ae74 | 1021 | } |
fc6cec86 | 1022 | if (sz != (ssize_t) len) { |
954d3a51 | 1023 | log_error("Short write to output."); |
fc6cec86 ZJS |
1024 | r = -EIO; |
1025 | goto error; | |
1026 | } | |
1027 | } | |
a276ae74 | 1028 | |
fc6cec86 ZJS |
1029 | if (temp) { |
1030 | *path = temp; | |
1031 | *unlink_temp = true; | |
1032 | } | |
1033 | return 0; | |
9fe13294 ZJS |
1034 | |
1035 | error: | |
fc6cec86 | 1036 | if (temp) { |
6990fb6b | 1037 | (void) unlink(temp); |
fc6cec86 | 1038 | log_debug("Removed temporary file %s", temp); |
9fe13294 | 1039 | } |
fc6cec86 | 1040 | return r; |
9fe13294 | 1041 | } |
a276ae74 | 1042 | |
5ce97d33 YW |
1043 | static int dump_core(int argc, char **argv, void *userdata) { |
1044 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; | |
158ecef5 | 1045 | _cleanup_fclose_ FILE *f = NULL; |
9fe13294 ZJS |
1046 | int r; |
1047 | ||
d7a0f1f4 FS |
1048 | if (arg_field) |
1049 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1050 | "Option --field/-F only makes sense with list"); | |
5ce97d33 YW |
1051 | |
1052 | r = acquire_journal(&j, argv + 1); | |
1053 | if (r < 0) | |
1054 | return r; | |
9fe13294 ZJS |
1055 | |
1056 | r = focus(j); | |
1057 | if (r < 0) | |
ada45c78 | 1058 | return r; |
ada45c78 | 1059 | |
158ecef5 ZJS |
1060 | if (arg_output) { |
1061 | f = fopen(arg_output, "we"); | |
1062 | if (!f) | |
1063 | return log_error_errno(errno, "Failed to open \"%s\" for writing: %m", arg_output); | |
1064 | } | |
1065 | ||
1066 | print_info(f ? stdout : stderr, j, false); | |
9fe13294 | 1067 | |
158ecef5 | 1068 | r = save_core(j, f, NULL, NULL); |
23bbb0de | 1069 | if (r < 0) |
bb7c5bad | 1070 | return r; |
5de0409e ZJS |
1071 | |
1072 | r = sd_journal_previous(j); | |
b9aaa7f4 ZJS |
1073 | if (r > 0 && !arg_quiet) |
1074 | log_notice("More than one entry matches, ignoring rest."); | |
5de0409e ZJS |
1075 | |
1076 | return 0; | |
1077 | } | |
1078 | ||
c5896b6a | 1079 | static int run_debug(int argc, char **argv, void *userdata) { |
5ce97d33 | 1080 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; |
a2be8be2 MF |
1081 | _cleanup_free_ char *exe = NULL, *path = NULL; |
1082 | _cleanup_strv_free_ char **debugger_call = NULL; | |
9fe13294 | 1083 | bool unlink_path = false; |
c5896b6a | 1084 | const char *data, *fork_name; |
ada45c78 | 1085 | size_t len; |
ada45c78 | 1086 | pid_t pid; |
ada45c78 | 1087 | int r; |
ada45c78 | 1088 | |
c5896b6a RG |
1089 | if (!arg_debugger) { |
1090 | char *env_debugger; | |
1091 | ||
1092 | env_debugger = getenv("SYSTEMD_DEBUGGER"); | |
1093 | if (env_debugger) | |
1094 | arg_debugger = env_debugger; | |
1095 | else | |
1096 | arg_debugger = "gdb"; | |
1097 | } | |
1098 | ||
a2be8be2 MF |
1099 | r = strv_extend(&debugger_call, arg_debugger); |
1100 | if (r < 0) | |
1101 | return log_oom(); | |
1102 | ||
1103 | r = strv_extend_strv(&debugger_call, arg_debugger_args, false); | |
1104 | if (r < 0) | |
1105 | return log_oom(); | |
3f0d8b2d | 1106 | |
d7a0f1f4 FS |
1107 | if (arg_field) |
1108 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1109 | "Option --field/-F only makes sense with list"); | |
5ce97d33 YW |
1110 | |
1111 | r = acquire_journal(&j, argv + 1); | |
1112 | if (r < 0) | |
1113 | return r; | |
ada45c78 LP |
1114 | |
1115 | r = focus(j); | |
1116 | if (r < 0) | |
1117 | return r; | |
1118 | ||
a174da59 FS |
1119 | if (!arg_quiet) { |
1120 | print_info(stdout, j, false); | |
1121 | fputs("\n", stdout); | |
1122 | } | |
ada45c78 LP |
1123 | |
1124 | r = sd_journal_get_data(j, "COREDUMP_EXE", (const void**) &data, &len); | |
23bbb0de MS |
1125 | if (r < 0) |
1126 | return log_error_errno(r, "Failed to retrieve COREDUMP_EXE field: %m"); | |
ada45c78 | 1127 | |
fbd0b64f LP |
1128 | assert(len > STRLEN("COREDUMP_EXE=")); |
1129 | data += STRLEN("COREDUMP_EXE="); | |
1130 | len -= STRLEN("COREDUMP_EXE="); | |
ada45c78 LP |
1131 | |
1132 | exe = strndup(data, len); | |
1133 | if (!exe) | |
1134 | return log_oom(); | |
1135 | ||
d7a0f1f4 FS |
1136 | if (endswith(exe, " (deleted)")) |
1137 | return log_error_errno(SYNTHETIC_ERRNO(ENOENT), | |
1138 | "Binary already deleted."); | |
ada45c78 | 1139 | |
d7a0f1f4 FS |
1140 | if (!path_is_absolute(exe)) |
1141 | return log_error_errno(SYNTHETIC_ERRNO(ENOENT), | |
1142 | "Binary is not an absolute path."); | |
a276ae74 | 1143 | |
bb7c5bad | 1144 | r = save_core(j, NULL, &path, &unlink_path); |
23bbb0de | 1145 | if (r < 0) |
bb7c5bad | 1146 | return r; |
ada45c78 | 1147 | |
a2be8be2 MF |
1148 | r = strv_extend_strv(&debugger_call, STRV_MAKE(exe, "-c", path), false); |
1149 | if (r < 0) | |
1150 | return log_oom(); | |
1151 | ||
3e7bc89b | 1152 | /* Don't interfere with gdb and its handling of SIGINT. */ |
9c274488 | 1153 | (void) ignore_signals(SIGINT); |
3e7bc89b | 1154 | |
a2be8be2 | 1155 | fork_name = strjoina("(", debugger_call[0], ")"); |
c5896b6a | 1156 | |
48f813c4 | 1157 | r = safe_fork(fork_name, FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG|FORK_FLUSH_STDIO, &pid); |
b6e1fff1 | 1158 | if (r < 0) |
ada45c78 | 1159 | goto finish; |
4c253ed1 | 1160 | if (r == 0) { |
a2be8be2 | 1161 | execvp(debugger_call[0], debugger_call); |
0b1f3c76 | 1162 | log_open(); |
a2be8be2 | 1163 | log_error_errno(errno, "Failed to invoke %s: %m", debugger_call[0]); |
a45d7127 | 1164 | _exit(EXIT_FAILURE); |
ada45c78 LP |
1165 | } |
1166 | ||
a2be8be2 | 1167 | r = wait_for_terminate_and_check(debugger_call[0], pid, WAIT_LOG_ABNORMAL); |
ada45c78 LP |
1168 | |
1169 | finish: | |
9c274488 | 1170 | (void) default_signals(SIGINT); |
3e7bc89b | 1171 | |
9fe13294 ZJS |
1172 | if (unlink_path) { |
1173 | log_debug("Removed temporary file %s", path); | |
6990fb6b | 1174 | (void) unlink(path); |
9fe13294 | 1175 | } |
a276ae74 | 1176 | |
ada45c78 LP |
1177 | return r; |
1178 | } | |
1179 | ||
012f2b7d | 1180 | static int check_units_active(void) { |
7add4883 | 1181 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
012f2b7d ZJS |
1182 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
1183 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
1184 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
1185 | int c = 0, r; | |
7bbf2d84 | 1186 | const char *id, *state, *substate; |
012f2b7d | 1187 | |
b9aaa7f4 ZJS |
1188 | if (arg_quiet) |
1189 | return false; | |
1190 | ||
012f2b7d | 1191 | r = sd_bus_default_system(&bus); |
414bd2e7 DDM |
1192 | if (r == -ENOENT) { |
1193 | log_debug("D-Bus is not running, skipping active unit check"); | |
1194 | return 0; | |
1195 | } | |
012f2b7d ZJS |
1196 | if (r < 0) |
1197 | return log_error_errno(r, "Failed to acquire bus: %m"); | |
1198 | ||
1199 | r = sd_bus_message_new_method_call( | |
1200 | bus, | |
1201 | &m, | |
1202 | "org.freedesktop.systemd1", | |
1203 | "/org/freedesktop/systemd1", | |
1204 | "org.freedesktop.systemd1.Manager", | |
1205 | "ListUnitsByPatterns"); | |
1206 | if (r < 0) | |
1207 | return bus_log_create_error(r); | |
1208 | ||
1209 | r = sd_bus_message_append_strv(m, NULL); | |
1210 | if (r < 0) | |
1211 | return bus_log_create_error(r); | |
1212 | ||
1213 | r = sd_bus_message_append_strv(m, STRV_MAKE("systemd-coredump@*.service")); | |
1214 | if (r < 0) | |
1215 | return bus_log_create_error(r); | |
1216 | ||
501551e8 | 1217 | r = sd_bus_call(bus, m, SHORT_BUS_CALL_TIMEOUT_USEC, &error, &reply); |
012f2b7d ZJS |
1218 | if (r < 0) |
1219 | return log_error_errno(r, "Failed to check if any systemd-coredump@.service units are running: %s", | |
1220 | bus_error_message(&error, r)); | |
1221 | ||
1222 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)"); | |
1223 | if (r < 0) | |
1224 | return bus_log_parse_error(r); | |
1225 | ||
1226 | while ((r = sd_bus_message_read( | |
1227 | reply, "(ssssssouso)", | |
7bbf2d84 ZJS |
1228 | &id, NULL, NULL, &state, &substate, |
1229 | NULL, NULL, NULL, NULL, NULL)) > 0) { | |
eb5877a0 | 1230 | bool found = !STR_IN_SET(state, "inactive", "dead", "failed"); |
7bbf2d84 ZJS |
1231 | log_debug("Unit %s is %s/%s, %scounting it.", id, state, substate, found ? "" : "not "); |
1232 | c += found; | |
1233 | } | |
012f2b7d ZJS |
1234 | if (r < 0) |
1235 | return bus_log_parse_error(r); | |
1236 | ||
1237 | r = sd_bus_message_exit_container(reply); | |
1238 | if (r < 0) | |
1239 | return bus_log_parse_error(r); | |
1240 | ||
1241 | return c; | |
1242 | } | |
1243 | ||
5ce97d33 YW |
1244 | static int coredumpctl_main(int argc, char *argv[]) { |
1245 | ||
1246 | static const Verb verbs[] = { | |
c5896b6a RG |
1247 | { "list", VERB_ANY, VERB_ANY, VERB_DEFAULT, dump_list }, |
1248 | { "info", VERB_ANY, VERB_ANY, 0, dump_list }, | |
1249 | { "dump", VERB_ANY, VERB_ANY, 0, dump_core }, | |
1250 | { "debug", VERB_ANY, VERB_ANY, 0, run_debug }, | |
1251 | { "gdb", VERB_ANY, VERB_ANY, 0, run_debug }, | |
6d8be376 | 1252 | { "help", VERB_ANY, 1, 0, verb_help }, |
5ce97d33 YW |
1253 | {} |
1254 | }; | |
1255 | ||
1256 | return dispatch_verb(argc, argv, verbs, NULL); | |
1257 | } | |
1258 | ||
c118b577 | 1259 | static int run(int argc, char *argv[]) { |
5ce97d33 | 1260 | int r, units_active; |
5de0409e | 1261 | |
a9cdc94f | 1262 | setlocale(LC_ALL, ""); |
d2acb93d | 1263 | log_setup(); |
5de0409e | 1264 | |
1abaf488 LP |
1265 | /* The journal merging logic potentially needs a lot of fds. */ |
1266 | (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE); | |
1267 | ||
5ab9ed07 | 1268 | r = parse_argv(argc, argv); |
5ce97d33 | 1269 | if (r <= 0) |
c118b577 | 1270 | return r; |
5de0409e | 1271 | |
2cf4172a LP |
1272 | sigbus_install(); |
1273 | ||
012f2b7d ZJS |
1274 | units_active = check_units_active(); /* error is treated the same as 0 */ |
1275 | ||
5ce97d33 | 1276 | r = coredumpctl_main(argc, argv); |
5de0409e | 1277 | |
012f2b7d ZJS |
1278 | if (units_active > 0) |
1279 | printf("%s-- Notice: %d systemd-coredump@.service %s, output may be incomplete.%s\n", | |
1280 | ansi_highlight_red(), | |
1281 | units_active, units_active == 1 ? "unit is running" : "units are running", | |
1282 | ansi_normal()); | |
aeb56450 | 1283 | |
c118b577 | 1284 | return r; |
5de0409e | 1285 | } |
c118b577 ZJS |
1286 | |
1287 | DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |