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