]>
Commit | Line | Data |
---|---|---|
5de0409e ZJS |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2012 Zbigniew Jędrzejewski-Szmek | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
3f6fd1ba LP |
20 | #include <fcntl.h> |
21 | #include <getopt.h> | |
a9cdc94f | 22 | #include <locale.h> |
5de0409e ZJS |
23 | #include <stdio.h> |
24 | #include <string.h> | |
ada45c78 | 25 | #include <unistd.h> |
5de0409e | 26 | |
2cf4172a | 27 | #include "sd-journal.h" |
3f6fd1ba | 28 | |
b5efdb8a | 29 | #include "alloc-util.h" |
3f6fd1ba | 30 | #include "compress.h" |
3ffd4af2 | 31 | #include "fd-util.h" |
0d39fa9c | 32 | #include "fileio.h" |
992e8f22 | 33 | #include "fs-util.h" |
3f6fd1ba | 34 | #include "journal-internal.h" |
5de0409e | 35 | #include "log.h" |
763c7aa2 | 36 | #include "macro.h" |
3f6fd1ba | 37 | #include "pager.h" |
6bedfcbb | 38 | #include "parse-util.h" |
3f6fd1ba | 39 | #include "path-util.h" |
0b452006 | 40 | #include "process-util.h" |
3f6fd1ba LP |
41 | #include "set.h" |
42 | #include "sigbus.h" | |
24882e06 | 43 | #include "signal-util.h" |
07630cea | 44 | #include "string-util.h" |
3f6fd1ba | 45 | #include "terminal-util.h" |
b1d4f8e1 | 46 | #include "user-util.h" |
6bedfcbb | 47 | #include "util.h" |
5de0409e ZJS |
48 | |
49 | static enum { | |
50 | ACTION_NONE, | |
e15758cc | 51 | ACTION_INFO, |
5de0409e ZJS |
52 | ACTION_LIST, |
53 | ACTION_DUMP, | |
ada45c78 | 54 | ACTION_GDB, |
5de0409e | 55 | } arg_action = ACTION_LIST; |
e15758cc | 56 | static const char* arg_field = NULL; |
b73e9a02 | 57 | static const char *arg_directory = NULL; |
ea4b98e6 | 58 | static bool arg_no_pager = false; |
9a340880 | 59 | static int arg_no_legend = false; |
0c51aada | 60 | static int arg_one = false; |
3774cf57 | 61 | static FILE* arg_output = NULL; |
5de0409e ZJS |
62 | |
63 | static Set *new_matches(void) { | |
64 | Set *set; | |
65 | char *tmp; | |
66 | int r; | |
67 | ||
d5099efc | 68 | set = set_new(NULL); |
5de0409e ZJS |
69 | if (!set) { |
70 | log_oom(); | |
71 | return NULL; | |
72 | } | |
73 | ||
74 | tmp = strdup("MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1"); | |
75 | if (!tmp) { | |
76 | log_oom(); | |
4a207bb2 | 77 | set_free(set); |
5de0409e ZJS |
78 | return NULL; |
79 | } | |
80 | ||
ef42202a | 81 | r = set_consume(set, tmp); |
5de0409e | 82 | if (r < 0) { |
da927ba9 | 83 | log_error_errno(r, "failed to add to set: %m"); |
4a207bb2 | 84 | set_free(set); |
5de0409e ZJS |
85 | return NULL; |
86 | } | |
87 | ||
88 | return set; | |
89 | } | |
90 | ||
5de0409e | 91 | static int add_match(Set *set, const char *match) { |
7fd1b19b | 92 | _cleanup_free_ char *p = NULL; |
0f474365 LP |
93 | char *pattern = NULL; |
94 | const char* prefix; | |
95 | pid_t pid; | |
96 | int r; | |
5de0409e ZJS |
97 | |
98 | if (strchr(match, '=')) | |
99 | prefix = ""; | |
100 | else if (strchr(match, '/')) { | |
0f474365 LP |
101 | r = path_make_absolute_cwd(match, &p); |
102 | if (r < 0) | |
5de0409e | 103 | goto fail; |
5de0409e ZJS |
104 | match = p; |
105 | prefix = "COREDUMP_EXE="; | |
0f474365 | 106 | } else if (parse_pid(match, &pid) >= 0) |
5de0409e ZJS |
107 | prefix = "COREDUMP_PID="; |
108 | else | |
109 | prefix = "COREDUMP_COMM="; | |
110 | ||
111 | pattern = strjoin(prefix, match, NULL); | |
0f474365 LP |
112 | if (!pattern) { |
113 | r = -ENOMEM; | |
5de0409e | 114 | goto fail; |
0f474365 | 115 | } |
5de0409e | 116 | |
ef42202a | 117 | log_debug("Adding pattern: %s", pattern); |
f7a5bb28 | 118 | r = set_consume(set, pattern); |
0f474365 | 119 | if (r < 0) |
5de0409e | 120 | goto fail; |
5de0409e ZJS |
121 | |
122 | return 0; | |
123 | fail: | |
23bbb0de | 124 | return log_error_errno(r, "Failed to add match: %m"); |
5de0409e ZJS |
125 | } |
126 | ||
601185b4 ZJS |
127 | static void help(void) { |
128 | printf("%s [OPTIONS...]\n\n" | |
129 | "List or retrieve coredumps from the journal.\n\n" | |
130 | "Flags:\n" | |
131 | " -h --help Show this help\n" | |
132 | " --version Print version string\n" | |
133 | " --no-pager Do not pipe output into a pager\n" | |
134 | " --no-legend Do not print the column headers.\n" | |
135 | " -1 Show information about most recent entry only\n" | |
136 | " -F --field=FIELD List all values a certain field takes\n" | |
137 | " -o --output=FILE Write output to FILE\n\n" | |
b73e9a02 | 138 | " -D --directory=DIR Use journal files from directory\n\n" |
601185b4 ZJS |
139 | |
140 | "Commands:\n" | |
141 | " list [MATCHES...] List available coredumps (default)\n" | |
142 | " info [MATCHES...] Show detailed information about one or more coredumps\n" | |
143 | " dump [MATCHES...] Print first matching coredump to stdout\n" | |
144 | " gdb [MATCHES...] Start gdb for the first matching coredump\n" | |
145 | , program_invocation_short_name); | |
146 | } | |
147 | ||
763c7aa2 | 148 | static int parse_argv(int argc, char *argv[], Set *matches) { |
5de0409e ZJS |
149 | enum { |
150 | ARG_VERSION = 0x100, | |
151 | ARG_NO_PAGER, | |
9a340880 | 152 | ARG_NO_LEGEND, |
5de0409e ZJS |
153 | }; |
154 | ||
155 | int r, c; | |
156 | ||
157 | static const struct option options[] = { | |
57ce4bd4 ZJS |
158 | { "help", no_argument, NULL, 'h' }, |
159 | { "version" , no_argument, NULL, ARG_VERSION }, | |
160 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
9a340880 | 161 | { "no-legend", no_argument, NULL, ARG_NO_LEGEND }, |
57ce4bd4 | 162 | { "output", required_argument, NULL, 'o' }, |
4f76ae1b | 163 | { "field", required_argument, NULL, 'F' }, |
b73e9a02 | 164 | { "directory", required_argument, NULL, 'D' }, |
eb9da376 | 165 | {} |
5de0409e ZJS |
166 | }; |
167 | ||
168 | assert(argc >= 0); | |
169 | assert(argv); | |
170 | ||
b73e9a02 | 171 | while ((c = getopt_long(argc, argv, "ho:F:1D:", options, NULL)) >= 0) |
5de0409e | 172 | switch(c) { |
eb9da376 | 173 | |
5de0409e | 174 | case 'h': |
5de0409e | 175 | arg_action = ACTION_NONE; |
601185b4 ZJS |
176 | help(); |
177 | return 0; | |
5de0409e ZJS |
178 | |
179 | case ARG_VERSION: | |
eb9da376 | 180 | arg_action = ACTION_NONE; |
3f6fd1ba | 181 | return version(); |
5de0409e ZJS |
182 | |
183 | case ARG_NO_PAGER: | |
184 | arg_no_pager = true; | |
185 | break; | |
186 | ||
9a340880 ZJS |
187 | case ARG_NO_LEGEND: |
188 | arg_no_legend = true; | |
189 | break; | |
190 | ||
5de0409e | 191 | case 'o': |
3774cf57 | 192 | if (arg_output) { |
5de0409e ZJS |
193 | log_error("cannot set output more than once"); |
194 | return -EINVAL; | |
195 | } | |
196 | ||
3774cf57 LP |
197 | arg_output = fopen(optarg, "we"); |
198 | if (!arg_output) | |
4a62c710 | 199 | return log_error_errno(errno, "writing to '%s': %m", optarg); |
5de0409e ZJS |
200 | |
201 | break; | |
57ce4bd4 | 202 | |
4f76ae1b | 203 | case 'F': |
a276ae74 | 204 | if (arg_field) { |
4f76ae1b ZJS |
205 | log_error("cannot use --field/-F more than once"); |
206 | return -EINVAL; | |
207 | } | |
a276ae74 | 208 | arg_field = optarg; |
4f76ae1b ZJS |
209 | break; |
210 | ||
0c51aada LP |
211 | case '1': |
212 | arg_one = true; | |
213 | break; | |
214 | ||
b73e9a02 SW |
215 | case 'D': |
216 | arg_directory = optarg; | |
217 | break; | |
218 | ||
57ce4bd4 ZJS |
219 | case '?': |
220 | return -EINVAL; | |
221 | ||
5de0409e | 222 | default: |
eb9da376 | 223 | assert_not_reached("Unhandled option"); |
5de0409e ZJS |
224 | } |
225 | ||
226 | if (optind < argc) { | |
227 | const char *cmd = argv[optind++]; | |
f168c273 | 228 | if (streq(cmd, "list")) |
5de0409e ZJS |
229 | arg_action = ACTION_LIST; |
230 | else if (streq(cmd, "dump")) | |
231 | arg_action = ACTION_DUMP; | |
ada45c78 LP |
232 | else if (streq(cmd, "gdb")) |
233 | arg_action = ACTION_GDB; | |
e15758cc LP |
234 | else if (streq(cmd, "info")) |
235 | arg_action = ACTION_INFO; | |
5de0409e ZJS |
236 | else { |
237 | log_error("Unknown action '%s'", cmd); | |
238 | return -EINVAL; | |
239 | } | |
240 | } | |
241 | ||
a276ae74 | 242 | if (arg_field && arg_action != ACTION_LIST) { |
4f76ae1b ZJS |
243 | log_error("Option --field/-F only makes sense with list"); |
244 | return -EINVAL; | |
245 | } | |
246 | ||
5de0409e ZJS |
247 | while (optind < argc) { |
248 | r = add_match(matches, argv[optind]); | |
249 | if (r != 0) | |
250 | return r; | |
251 | optind++; | |
252 | } | |
253 | ||
254 | return 0; | |
255 | } | |
256 | ||
8bc8ab83 LP |
257 | static int retrieve(const void *data, |
258 | size_t len, | |
259 | const char *name, | |
a276ae74 | 260 | char **var) { |
5de0409e | 261 | |
4f76ae1b | 262 | size_t ident; |
a276ae74 | 263 | char *v; |
8bc8ab83 | 264 | |
4f76ae1b | 265 | ident = strlen(name) + 1; /* name + "=" */ |
8bc8ab83 | 266 | |
4f76ae1b | 267 | if (len < ident) |
8bc8ab83 | 268 | return 0; |
5de0409e | 269 | |
4f76ae1b | 270 | if (memcmp(data, name, ident - 1) != 0) |
8bc8ab83 LP |
271 | return 0; |
272 | ||
4f76ae1b | 273 | if (((const char*) data)[ident - 1] != '=') |
8bc8ab83 | 274 | return 0; |
5de0409e | 275 | |
a276ae74 LP |
276 | v = strndup((const char*)data + ident, len - ident); |
277 | if (!v) | |
5de0409e ZJS |
278 | return log_oom(); |
279 | ||
a276ae74 LP |
280 | free(*var); |
281 | *var = v; | |
282 | ||
062b99e8 | 283 | return 1; |
5de0409e ZJS |
284 | } |
285 | ||
062b99e8 | 286 | static int print_field(FILE* file, sd_journal *j) { |
4f76ae1b ZJS |
287 | const void *d; |
288 | size_t l; | |
289 | ||
e15758cc LP |
290 | assert(file); |
291 | assert(j); | |
292 | ||
a276ae74 | 293 | assert(arg_field); |
4f76ae1b | 294 | |
062b99e8 ZJS |
295 | /* A (user-specified) field may appear more than once for a given entry. |
296 | * We will print all of the occurences. | |
297 | * This is different below for fields that systemd-coredump uses, | |
298 | * because they cannot meaningfully appear more than once. | |
299 | */ | |
300 | SD_JOURNAL_FOREACH_DATA(j, d, l) { | |
301 | _cleanup_free_ char *value = NULL; | |
302 | int r; | |
303 | ||
304 | r = retrieve(d, l, arg_field, &value); | |
305 | if (r < 0) | |
306 | return r; | |
307 | if (r > 0) | |
308 | fprintf(file, "%s\n", value); | |
309 | } | |
a276ae74 | 310 | |
062b99e8 | 311 | return 0; |
4f76ae1b ZJS |
312 | } |
313 | ||
062b99e8 ZJS |
314 | #define RETRIEVE(d, l, name, arg) \ |
315 | { \ | |
316 | int _r = retrieve(d, l, name, &arg); \ | |
317 | if (_r < 0) \ | |
318 | return _r; \ | |
319 | if (_r > 0) \ | |
320 | continue; \ | |
321 | } | |
322 | ||
e15758cc | 323 | static int print_list(FILE* file, sd_journal *j, int had_legend) { |
a276ae74 | 324 | _cleanup_free_ char |
5de0409e | 325 | *pid = NULL, *uid = NULL, *gid = NULL, |
9fe13294 | 326 | *sgnl = NULL, *exe = NULL, *comm = NULL, *cmdline = NULL, |
47f50642 | 327 | *filename = NULL, *coredump = NULL; |
8bc8ab83 LP |
328 | const void *d; |
329 | size_t l; | |
684341b0 LP |
330 | usec_t t; |
331 | char buf[FORMAT_TIMESTAMP_MAX]; | |
332 | int r; | |
04de5879 | 333 | const char *present; |
8bc8ab83 | 334 | |
e15758cc LP |
335 | assert(file); |
336 | assert(j); | |
337 | ||
8bc8ab83 | 338 | SD_JOURNAL_FOREACH_DATA(j, d, l) { |
062b99e8 ZJS |
339 | RETRIEVE(d, l, "COREDUMP_PID", pid); |
340 | RETRIEVE(d, l, "COREDUMP_UID", uid); | |
341 | RETRIEVE(d, l, "COREDUMP_GID", gid); | |
342 | RETRIEVE(d, l, "COREDUMP_SIGNAL", sgnl); | |
343 | RETRIEVE(d, l, "COREDUMP_EXE", exe); | |
344 | RETRIEVE(d, l, "COREDUMP_COMM", comm); | |
345 | RETRIEVE(d, l, "COREDUMP_CMDLINE", cmdline); | |
346 | RETRIEVE(d, l, "COREDUMP_FILENAME", filename); | |
347 | RETRIEVE(d, l, "COREDUMP", coredump); | |
8bc8ab83 | 348 | } |
5de0409e | 349 | |
9fe13294 | 350 | if (!pid && !uid && !gid && !sgnl && !exe && !comm && !cmdline && !filename) { |
684341b0 LP |
351 | log_warning("Empty coredump log entry"); |
352 | return -EINVAL; | |
353 | } | |
354 | ||
355 | r = sd_journal_get_realtime_usec(j, &t); | |
23bbb0de MS |
356 | if (r < 0) |
357 | return log_error_errno(r, "Failed to get realtime timestamp: %m"); | |
5de0409e | 358 | |
684341b0 LP |
359 | format_timestamp(buf, sizeof(buf), t); |
360 | ||
9a340880 | 361 | if (!had_legend && !arg_no_legend) |
9fe13294 | 362 | fprintf(file, "%-*s %*s %*s %*s %*s %*s %s\n", |
c3f84106 | 363 | FORMAT_TIMESTAMP_WIDTH, "TIME", |
5de0409e ZJS |
364 | 6, "PID", |
365 | 5, "UID", | |
366 | 5, "GID", | |
684341b0 | 367 | 3, "SIG", |
04de5879 | 368 | 8, "COREFILE", |
684341b0 | 369 | "EXE"); |
5de0409e | 370 | |
04de5879 ZJS |
371 | if (filename) |
372 | if (access(filename, R_OK) == 0) | |
373 | present = "present"; | |
374 | else if (errno == ENOENT) | |
375 | present = "missing"; | |
376 | else | |
377 | present = "error"; | |
378 | else if (coredump) | |
379 | present = "journal"; | |
380 | else | |
381 | present = "none"; | |
382 | ||
383 | fprintf(file, "%-*s %*s %*s %*s %*s %-*s %s\n", | |
c3f84106 | 384 | FORMAT_TIMESTAMP_WIDTH, buf, |
a276ae74 LP |
385 | 6, strna(pid), |
386 | 5, strna(uid), | |
387 | 5, strna(gid), | |
388 | 3, strna(sgnl), | |
04de5879 | 389 | 8, present, |
a276ae74 | 390 | strna(exe ?: (comm ?: cmdline))); |
684341b0 LP |
391 | |
392 | return 0; | |
5de0409e ZJS |
393 | } |
394 | ||
e15758cc LP |
395 | static int print_info(FILE *file, sd_journal *j, bool need_space) { |
396 | _cleanup_free_ char | |
397 | *pid = NULL, *uid = NULL, *gid = NULL, | |
398 | *sgnl = NULL, *exe = NULL, *comm = NULL, *cmdline = NULL, | |
399 | *unit = NULL, *user_unit = NULL, *session = NULL, | |
a035f819 | 400 | *boot_id = NULL, *machine_id = NULL, *hostname = NULL, |
9fe13294 | 401 | *slice = NULL, *cgroup = NULL, *owner_uid = NULL, |
47f50642 ZJS |
402 | *message = NULL, *timestamp = NULL, *filename = NULL, |
403 | *coredump = NULL; | |
e15758cc LP |
404 | const void *d; |
405 | size_t l; | |
4b8cbe9a | 406 | int r; |
e15758cc LP |
407 | |
408 | assert(file); | |
409 | assert(j); | |
410 | ||
411 | SD_JOURNAL_FOREACH_DATA(j, d, l) { | |
062b99e8 ZJS |
412 | RETRIEVE(d, l, "COREDUMP_PID", pid); |
413 | RETRIEVE(d, l, "COREDUMP_UID", uid); | |
414 | RETRIEVE(d, l, "COREDUMP_GID", gid); | |
415 | RETRIEVE(d, l, "COREDUMP_SIGNAL", sgnl); | |
416 | RETRIEVE(d, l, "COREDUMP_EXE", exe); | |
417 | RETRIEVE(d, l, "COREDUMP_COMM", comm); | |
418 | RETRIEVE(d, l, "COREDUMP_CMDLINE", cmdline); | |
419 | RETRIEVE(d, l, "COREDUMP_UNIT", unit); | |
420 | RETRIEVE(d, l, "COREDUMP_USER_UNIT", user_unit); | |
421 | RETRIEVE(d, l, "COREDUMP_SESSION", session); | |
422 | RETRIEVE(d, l, "COREDUMP_OWNER_UID", owner_uid); | |
423 | RETRIEVE(d, l, "COREDUMP_SLICE", slice); | |
424 | RETRIEVE(d, l, "COREDUMP_CGROUP", cgroup); | |
425 | RETRIEVE(d, l, "COREDUMP_TIMESTAMP", timestamp); | |
426 | RETRIEVE(d, l, "COREDUMP_FILENAME", filename); | |
427 | RETRIEVE(d, l, "COREDUMP", coredump); | |
428 | RETRIEVE(d, l, "_BOOT_ID", boot_id); | |
429 | RETRIEVE(d, l, "_MACHINE_ID", machine_id); | |
430 | RETRIEVE(d, l, "_HOSTNAME", hostname); | |
431 | RETRIEVE(d, l, "MESSAGE", message); | |
e15758cc LP |
432 | } |
433 | ||
434 | if (need_space) | |
435 | fputs("\n", file); | |
436 | ||
81cef14f LP |
437 | if (comm) |
438 | fprintf(file, | |
439 | " PID: %s%s%s (%s)\n", | |
1fc464f6 | 440 | ansi_highlight(), strna(pid), ansi_normal(), comm); |
81cef14f LP |
441 | else |
442 | fprintf(file, | |
443 | " PID: %s%s%s\n", | |
1fc464f6 | 444 | ansi_highlight(), strna(pid), ansi_normal()); |
a035f819 LP |
445 | |
446 | if (uid) { | |
447 | uid_t n; | |
448 | ||
449 | if (parse_uid(uid, &n) >= 0) { | |
450 | _cleanup_free_ char *u = NULL; | |
451 | ||
452 | u = uid_to_name(n); | |
453 | fprintf(file, | |
454 | " UID: %s (%s)\n", | |
455 | uid, u); | |
456 | } else { | |
457 | fprintf(file, | |
458 | " UID: %s\n", | |
459 | uid); | |
460 | } | |
461 | } | |
462 | ||
463 | if (gid) { | |
464 | gid_t n; | |
465 | ||
466 | if (parse_gid(gid, &n) >= 0) { | |
467 | _cleanup_free_ char *g = NULL; | |
468 | ||
469 | g = gid_to_name(n); | |
470 | fprintf(file, | |
471 | " GID: %s (%s)\n", | |
472 | gid, g); | |
473 | } else { | |
474 | fprintf(file, | |
475 | " GID: %s\n", | |
476 | gid); | |
477 | } | |
478 | } | |
e15758cc LP |
479 | |
480 | if (sgnl) { | |
481 | int sig; | |
482 | ||
483 | if (safe_atoi(sgnl, &sig) >= 0) | |
484 | fprintf(file, " Signal: %s (%s)\n", sgnl, signal_to_string(sig)); | |
485 | else | |
486 | fprintf(file, " Signal: %s\n", sgnl); | |
487 | } | |
488 | ||
4b8cbe9a LP |
489 | if (timestamp) { |
490 | usec_t u; | |
491 | ||
492 | r = safe_atou64(timestamp, &u); | |
493 | if (r >= 0) { | |
494 | char absolute[FORMAT_TIMESTAMP_MAX], relative[FORMAT_TIMESPAN_MAX]; | |
495 | ||
496 | fprintf(file, | |
497 | " Timestamp: %s (%s)\n", | |
498 | format_timestamp(absolute, sizeof(absolute), u), | |
499 | format_timestamp_relative(relative, sizeof(relative), u)); | |
500 | ||
501 | } else | |
502 | fprintf(file, " Timestamp: %s\n", timestamp); | |
503 | } | |
504 | ||
e15758cc LP |
505 | if (cmdline) |
506 | fprintf(file, " Command Line: %s\n", cmdline); | |
81cef14f | 507 | if (exe) |
1fc464f6 | 508 | fprintf(file, " Executable: %s%s%s\n", ansi_highlight(), exe, ansi_normal()); |
a035f819 LP |
509 | if (cgroup) |
510 | fprintf(file, " Control Group: %s\n", cgroup); | |
e15758cc LP |
511 | if (unit) |
512 | fprintf(file, " Unit: %s\n", unit); | |
513 | if (user_unit) | |
554ed50f | 514 | fprintf(file, " User Unit: %s\n", user_unit); |
a035f819 LP |
515 | if (slice) |
516 | fprintf(file, " Slice: %s\n", slice); | |
e15758cc LP |
517 | if (session) |
518 | fprintf(file, " Session: %s\n", session); | |
a035f819 LP |
519 | if (owner_uid) { |
520 | uid_t n; | |
521 | ||
522 | if (parse_uid(owner_uid, &n) >= 0) { | |
523 | _cleanup_free_ char *u = NULL; | |
524 | ||
525 | u = uid_to_name(n); | |
526 | fprintf(file, | |
527 | " Owner UID: %s (%s)\n", | |
528 | owner_uid, u); | |
529 | } else { | |
530 | fprintf(file, | |
531 | " Owner UID: %s\n", | |
532 | owner_uid); | |
533 | } | |
534 | } | |
e15758cc LP |
535 | if (boot_id) |
536 | fprintf(file, " Boot ID: %s\n", boot_id); | |
537 | if (machine_id) | |
538 | fprintf(file, " Machine ID: %s\n", machine_id); | |
539 | if (hostname) | |
540 | fprintf(file, " Hostname: %s\n", hostname); | |
541 | ||
47f50642 ZJS |
542 | if (filename) |
543 | fprintf(file, " Storage: %s%s\n", filename, | |
04de5879 | 544 | access(filename, R_OK) < 0 ? " (inaccessible)" : ""); |
47f50642 ZJS |
545 | else if (coredump) |
546 | fprintf(file, " Storage: journal\n"); | |
547 | else | |
548 | fprintf(file, " Storage: none\n"); | |
e15758cc | 549 | |
8d4e028f LP |
550 | if (message) { |
551 | _cleanup_free_ char *m = NULL; | |
552 | ||
553 | m = strreplace(message, "\n", "\n "); | |
554 | ||
555 | fprintf(file, " Message: %s\n", strstrip(m ?: message)); | |
556 | } | |
557 | ||
e15758cc LP |
558 | return 0; |
559 | } | |
560 | ||
ada45c78 | 561 | static int focus(sd_journal *j) { |
5de0409e ZJS |
562 | int r; |
563 | ||
5de0409e ZJS |
564 | r = sd_journal_seek_tail(j); |
565 | if (r == 0) | |
566 | r = sd_journal_previous(j); | |
23bbb0de MS |
567 | if (r < 0) |
568 | return log_error_errno(r, "Failed to search journal: %m"); | |
8bc8ab83 | 569 | if (r == 0) { |
0c51aada | 570 | log_error("No match found."); |
8bc8ab83 LP |
571 | return -ESRCH; |
572 | } | |
ada45c78 LP |
573 | return r; |
574 | } | |
5de0409e | 575 | |
062b99e8 | 576 | static int print_entry(sd_journal *j, unsigned n_found) { |
0c51aada LP |
577 | assert(j); |
578 | ||
579 | if (arg_action == ACTION_INFO) | |
062b99e8 | 580 | return print_info(stdout, j, n_found); |
0c51aada | 581 | else if (arg_field) |
062b99e8 | 582 | return print_field(stdout, j); |
0c51aada | 583 | else |
062b99e8 | 584 | return print_list(stdout, j, n_found); |
0c51aada LP |
585 | } |
586 | ||
587 | static int dump_list(sd_journal *j) { | |
588 | unsigned n_found = 0; | |
589 | int r; | |
590 | ||
591 | assert(j); | |
592 | ||
593 | /* The coredumps are likely to compressed, and for just | |
594 | * listing them we don't need to decompress them, so let's | |
595 | * pick a fairly low data threshold here */ | |
596 | sd_journal_set_data_threshold(j, 4096); | |
597 | ||
598 | if (arg_one) { | |
599 | r = focus(j); | |
600 | if (r < 0) | |
601 | return r; | |
602 | ||
062b99e8 | 603 | return print_entry(j, 0); |
0c51aada | 604 | } else { |
062b99e8 ZJS |
605 | SD_JOURNAL_FOREACH(j) { |
606 | r = print_entry(j, n_found++); | |
607 | if (r < 0) | |
608 | return r; | |
609 | } | |
0c51aada LP |
610 | |
611 | if (!arg_field && n_found <= 0) { | |
612 | log_notice("No coredumps found."); | |
613 | return -ESRCH; | |
614 | } | |
615 | } | |
616 | ||
617 | return 0; | |
618 | } | |
619 | ||
9fe13294 ZJS |
620 | static int save_core(sd_journal *j, int fd, char **path, bool *unlink_temp) { |
621 | const char *data; | |
622 | _cleanup_free_ char *filename = NULL; | |
623 | size_t len; | |
ada45c78 | 624 | int r; |
fc6cec86 ZJS |
625 | _cleanup_close_ int fdt = -1; |
626 | char *temp = NULL; | |
ada45c78 | 627 | |
9fe13294 ZJS |
628 | assert((fd >= 0) != !!path); |
629 | assert(!!path == !!unlink_temp); | |
ada45c78 | 630 | |
47f50642 ZJS |
631 | /* We want full data, nothing truncated. */ |
632 | sd_journal_set_data_threshold(j, 0); | |
633 | ||
fc6cec86 | 634 | /* Look for a coredump on disk first. */ |
9fe13294 ZJS |
635 | r = sd_journal_get_data(j, "COREDUMP_FILENAME", (const void**) &data, &len); |
636 | if (r < 0 && r != -ENOENT) | |
fc6cec86 | 637 | return log_error_errno(r, "Failed to retrieve COREDUMP_FILENAME: %m"); |
9fe13294 ZJS |
638 | else if (r == 0) |
639 | retrieve(data, len, "COREDUMP_FILENAME", &filename); | |
93b73b06 | 640 | |
954d3a51 ZJS |
641 | if (filename) { |
642 | if (access(filename, R_OK) < 0) | |
643 | return log_error_errno(errno, "File \"%s\" is not readable: %m", filename); | |
5de0409e | 644 | |
954d3a51 | 645 | if (path && !endswith(filename, ".xz") && !endswith(filename, ".lz4")) { |
d0c8806d TA |
646 | *path = filename; |
647 | filename = NULL; | |
a276ae74 | 648 | |
954d3a51 ZJS |
649 | return 0; |
650 | } | |
fc6cec86 | 651 | } |
a276ae74 | 652 | |
fc6cec86 ZJS |
653 | if (fd < 0) { |
654 | const char *vt; | |
992e8f22 | 655 | |
fc6cec86 | 656 | /* Create a temporary file to write the uncompressed core to. */ |
992e8f22 | 657 | |
fc6cec86 ZJS |
658 | r = var_tmp_dir(&vt); |
659 | if (r < 0) | |
660 | return log_error_errno(r, "Failed to acquire temporary directory path: %m"); | |
9fe13294 | 661 | |
fc6cec86 ZJS |
662 | temp = strjoin(vt, "/coredump-XXXXXX", NULL); |
663 | if (!temp) | |
664 | return log_oom(); | |
9fe13294 | 665 | |
fc6cec86 ZJS |
666 | fdt = mkostemp_safe(temp); |
667 | if (fdt < 0) | |
668 | return log_error_errno(fdt, "Failed to create temporary file: %m"); | |
669 | log_debug("Created temporary file %s", temp); | |
9fe13294 | 670 | |
fc6cec86 ZJS |
671 | fd = fdt; |
672 | } | |
673 | ||
674 | if (filename) { | |
d89c8fdf | 675 | #if defined(HAVE_XZ) || defined(HAVE_LZ4) |
fc6cec86 | 676 | _cleanup_close_ int fdf; |
9fe13294 | 677 | |
fc6cec86 ZJS |
678 | fdf = open(filename, O_RDONLY | O_CLOEXEC); |
679 | if (fdf < 0) { | |
680 | r = log_error_errno(errno, "Failed to open %s: %m", filename); | |
2fb8159f | 681 | goto error; |
fc6cec86 ZJS |
682 | } |
683 | ||
684 | r = decompress_stream(filename, fdf, fd, -1); | |
685 | if (r < 0) { | |
686 | log_error_errno(r, "Failed to decompress %s: %m", filename); | |
9fe13294 ZJS |
687 | goto error; |
688 | } | |
fc6cec86 ZJS |
689 | #else |
690 | log_error("Cannot decompress file. Compiled without compression support."); | |
691 | r = -EOPNOTSUPP; | |
692 | goto error; | |
693 | #endif | |
694 | } else { | |
695 | ssize_t sz; | |
a276ae74 | 696 | |
fc6cec86 ZJS |
697 | r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len); |
698 | if (r < 0) | |
699 | return log_error_errno(r, | |
700 | r == -ENOENT ? "Core file was not saved for this entry." : | |
701 | "Failed to retrieve COREDUMP field: %m"); | |
702 | ||
703 | assert(len >= 9); | |
704 | data += 9; | |
705 | len -= 9; | |
706 | ||
954d3a51 | 707 | sz = write(fd, data, len); |
fc6cec86 | 708 | if (sz < 0) { |
954d3a51 | 709 | r = log_error_errno(errno, "Failed to write output: %m"); |
fc6cec86 | 710 | goto error; |
a276ae74 | 711 | } |
fc6cec86 | 712 | if (sz != (ssize_t) len) { |
954d3a51 | 713 | log_error("Short write to output."); |
fc6cec86 ZJS |
714 | r = -EIO; |
715 | goto error; | |
716 | } | |
717 | } | |
a276ae74 | 718 | |
fc6cec86 ZJS |
719 | if (temp) { |
720 | *path = temp; | |
721 | *unlink_temp = true; | |
722 | } | |
723 | return 0; | |
9fe13294 ZJS |
724 | |
725 | error: | |
fc6cec86 ZJS |
726 | if (temp) { |
727 | unlink(temp); | |
728 | log_debug("Removed temporary file %s", temp); | |
9fe13294 | 729 | } |
fc6cec86 | 730 | return r; |
9fe13294 | 731 | } |
a276ae74 | 732 | |
9fe13294 ZJS |
733 | static int dump_core(sd_journal* j) { |
734 | int r; | |
735 | ||
736 | assert(j); | |
737 | ||
738 | r = focus(j); | |
739 | if (r < 0) | |
ada45c78 | 740 | return r; |
ada45c78 | 741 | |
3774cf57 | 742 | print_info(arg_output ? stdout : stderr, j, false); |
9fe13294 | 743 | |
3774cf57 | 744 | if (on_tty() && !arg_output) { |
9fe13294 ZJS |
745 | log_error("Refusing to dump core to tty."); |
746 | return -ENOTTY; | |
747 | } | |
748 | ||
3774cf57 | 749 | r = save_core(j, arg_output ? fileno(arg_output) : STDOUT_FILENO, NULL, NULL); |
23bbb0de MS |
750 | if (r < 0) |
751 | return log_error_errno(r, "Coredump retrieval failed: %m"); | |
5de0409e ZJS |
752 | |
753 | r = sd_journal_previous(j); | |
cfeead6c | 754 | if (r > 0) |
9f6445e3 | 755 | log_warning("More than one entry matches, ignoring rest."); |
5de0409e ZJS |
756 | |
757 | return 0; | |
758 | } | |
759 | ||
ada45c78 | 760 | static int run_gdb(sd_journal *j) { |
de8f6e54 | 761 | _cleanup_free_ char *exe = NULL, *path = NULL; |
9fe13294 ZJS |
762 | bool unlink_path = false; |
763 | const char *data; | |
a276ae74 | 764 | siginfo_t st; |
ada45c78 | 765 | size_t len; |
ada45c78 | 766 | pid_t pid; |
ada45c78 | 767 | int r; |
ada45c78 LP |
768 | |
769 | assert(j); | |
770 | ||
771 | r = focus(j); | |
772 | if (r < 0) | |
773 | return r; | |
774 | ||
e15758cc LP |
775 | print_info(stdout, j, false); |
776 | fputs("\n", stdout); | |
ada45c78 LP |
777 | |
778 | r = sd_journal_get_data(j, "COREDUMP_EXE", (const void**) &data, &len); | |
23bbb0de MS |
779 | if (r < 0) |
780 | return log_error_errno(r, "Failed to retrieve COREDUMP_EXE field: %m"); | |
ada45c78 | 781 | |
9fe13294 ZJS |
782 | assert(len > strlen("COREDUMP_EXE=")); |
783 | data += strlen("COREDUMP_EXE="); | |
784 | len -= strlen("COREDUMP_EXE="); | |
ada45c78 LP |
785 | |
786 | exe = strndup(data, len); | |
787 | if (!exe) | |
788 | return log_oom(); | |
789 | ||
790 | if (endswith(exe, " (deleted)")) { | |
791 | log_error("Binary already deleted."); | |
792 | return -ENOENT; | |
793 | } | |
794 | ||
a276ae74 LP |
795 | if (!path_is_absolute(exe)) { |
796 | log_error("Binary is not an absolute path."); | |
797 | return -ENOENT; | |
798 | } | |
799 | ||
9fe13294 | 800 | r = save_core(j, -1, &path, &unlink_path); |
23bbb0de MS |
801 | if (r < 0) |
802 | return log_error_errno(r, "Failed to retrieve core: %m"); | |
ada45c78 LP |
803 | |
804 | pid = fork(); | |
805 | if (pid < 0) { | |
76ef789d | 806 | r = log_error_errno(errno, "Failed to fork(): %m"); |
ada45c78 LP |
807 | goto finish; |
808 | } | |
809 | if (pid == 0) { | |
ce30c8dc LP |
810 | (void) reset_all_signal_handlers(); |
811 | (void) reset_signal_mask(); | |
812 | ||
ada45c78 | 813 | execlp("gdb", "gdb", exe, path, NULL); |
a276ae74 | 814 | |
56f64d95 | 815 | log_error_errno(errno, "Failed to invoke gdb: %m"); |
ada45c78 LP |
816 | _exit(1); |
817 | } | |
818 | ||
819 | r = wait_for_terminate(pid, &st); | |
820 | if (r < 0) { | |
709f6e46 | 821 | log_error_errno(r, "Failed to wait for gdb: %m"); |
ada45c78 LP |
822 | goto finish; |
823 | } | |
824 | ||
825 | r = st.si_code == CLD_EXITED ? st.si_status : 255; | |
826 | ||
827 | finish: | |
9fe13294 ZJS |
828 | if (unlink_path) { |
829 | log_debug("Removed temporary file %s", path); | |
830 | unlink(path); | |
831 | } | |
a276ae74 | 832 | |
ada45c78 LP |
833 | return r; |
834 | } | |
835 | ||
5de0409e | 836 | int main(int argc, char *argv[]) { |
4afd3348 | 837 | _cleanup_(sd_journal_closep) sd_journal*j = NULL; |
5de0409e ZJS |
838 | const char* match; |
839 | Iterator it; | |
840 | int r = 0; | |
7fd1b19b | 841 | _cleanup_set_free_free_ Set *matches = NULL; |
5de0409e | 842 | |
a9cdc94f | 843 | setlocale(LC_ALL, ""); |
5de0409e ZJS |
844 | log_parse_environment(); |
845 | log_open(); | |
846 | ||
847 | matches = new_matches(); | |
2fb7a5ce ZJS |
848 | if (!matches) { |
849 | r = -ENOMEM; | |
5de0409e | 850 | goto end; |
2fb7a5ce | 851 | } |
5de0409e | 852 | |
763c7aa2 | 853 | r = parse_argv(argc, argv, matches); |
2fb7a5ce | 854 | if (r < 0) |
5de0409e ZJS |
855 | goto end; |
856 | ||
857 | if (arg_action == ACTION_NONE) | |
858 | goto end; | |
859 | ||
2cf4172a LP |
860 | sigbus_install(); |
861 | ||
b73e9a02 SW |
862 | if (arg_directory) { |
863 | r = sd_journal_open_directory(&j, arg_directory, 0); | |
864 | if (r < 0) { | |
865 | log_error_errno(r, "Failed to open journals in directory: %s: %m", arg_directory); | |
866 | goto end; | |
867 | } | |
868 | } else { | |
869 | r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY); | |
870 | if (r < 0) { | |
871 | log_error_errno(r, "Failed to open journal: %m"); | |
872 | goto end; | |
873 | } | |
5de0409e ZJS |
874 | } |
875 | ||
876 | SET_FOREACH(match, matches, it) { | |
5de0409e ZJS |
877 | r = sd_journal_add_match(j, match, strlen(match)); |
878 | if (r != 0) { | |
c33b3297 MS |
879 | log_error_errno(r, "Failed to add match '%s': %m", |
880 | match); | |
5de0409e ZJS |
881 | goto end; |
882 | } | |
883 | } | |
884 | ||
553d2243 | 885 | if (_unlikely_(log_get_max_level() >= LOG_DEBUG)) { |
6c17bf04 ZJS |
886 | _cleanup_free_ char *filter; |
887 | ||
888 | filter = journal_make_match_string(j); | |
889 | log_debug("Journal filter: %s", filter); | |
890 | } | |
891 | ||
5de0409e | 892 | switch(arg_action) { |
ada45c78 | 893 | |
5de0409e | 894 | case ACTION_LIST: |
e15758cc | 895 | case ACTION_INFO: |
ea4b98e6 | 896 | pager_open(arg_no_pager, false); |
5de0409e ZJS |
897 | r = dump_list(j); |
898 | break; | |
ada45c78 | 899 | |
5de0409e ZJS |
900 | case ACTION_DUMP: |
901 | r = dump_core(j); | |
902 | break; | |
ada45c78 LP |
903 | |
904 | case ACTION_GDB: | |
905 | r = run_gdb(j); | |
906 | break; | |
907 | ||
908 | default: | |
5de0409e ZJS |
909 | assert_not_reached("Shouldn't be here"); |
910 | } | |
911 | ||
912 | end: | |
5de0409e ZJS |
913 | pager_close(); |
914 | ||
3774cf57 LP |
915 | if (arg_output) |
916 | fclose(arg_output); | |
5de0409e | 917 | |
ada45c78 | 918 | return r >= 0 ? r : EXIT_FAILURE; |
5de0409e | 919 | } |