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