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