]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/coredump/coredumpctl.c
coredumpctl: report corefile presence properly
[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, *coredump = 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 retrieve(d, l, "COREDUMP", &coredump);
328 }
329
330 if (!pid && !uid && !gid && !sgnl && !exe && !comm && !cmdline && !filename) {
331 log_warning("Empty coredump log entry");
332 return -EINVAL;
333 }
334
335 r = sd_journal_get_realtime_usec(j, &t);
336 if (r < 0)
337 return log_error_errno(r, "Failed to get realtime timestamp: %m");
338
339 format_timestamp(buf, sizeof(buf), t);
340 present = (filename && access(filename, F_OK) == 0) || coredump;
341
342 if (!had_legend && !arg_no_legend)
343 fprintf(file, "%-*s %*s %*s %*s %*s %*s %s\n",
344 FORMAT_TIMESTAMP_WIDTH, "TIME",
345 6, "PID",
346 5, "UID",
347 5, "GID",
348 3, "SIG",
349 1, "PRESENT",
350 "EXE");
351
352 fprintf(file, "%-*s %*s %*s %*s %*s %*s %s\n",
353 FORMAT_TIMESTAMP_WIDTH, buf,
354 6, strna(pid),
355 5, strna(uid),
356 5, strna(gid),
357 3, strna(sgnl),
358 1, present ? "*" : "",
359 strna(exe ?: (comm ?: cmdline)));
360
361 return 0;
362 }
363
364 static int print_info(FILE *file, sd_journal *j, bool need_space) {
365 _cleanup_free_ char
366 *pid = NULL, *uid = NULL, *gid = NULL,
367 *sgnl = NULL, *exe = NULL, *comm = NULL, *cmdline = NULL,
368 *unit = NULL, *user_unit = NULL, *session = NULL,
369 *boot_id = NULL, *machine_id = NULL, *hostname = NULL,
370 *slice = NULL, *cgroup = NULL, *owner_uid = NULL,
371 *message = NULL, *timestamp = NULL, *filename = NULL,
372 *coredump = NULL;
373 const void *d;
374 size_t l;
375 int r;
376
377 assert(file);
378 assert(j);
379
380 SD_JOURNAL_FOREACH_DATA(j, d, l) {
381 retrieve(d, l, "COREDUMP_PID", &pid);
382 retrieve(d, l, "COREDUMP_UID", &uid);
383 retrieve(d, l, "COREDUMP_GID", &gid);
384 retrieve(d, l, "COREDUMP_SIGNAL", &sgnl);
385 retrieve(d, l, "COREDUMP_EXE", &exe);
386 retrieve(d, l, "COREDUMP_COMM", &comm);
387 retrieve(d, l, "COREDUMP_CMDLINE", &cmdline);
388 retrieve(d, l, "COREDUMP_UNIT", &unit);
389 retrieve(d, l, "COREDUMP_USER_UNIT", &user_unit);
390 retrieve(d, l, "COREDUMP_SESSION", &session);
391 retrieve(d, l, "COREDUMP_OWNER_UID", &owner_uid);
392 retrieve(d, l, "COREDUMP_SLICE", &slice);
393 retrieve(d, l, "COREDUMP_CGROUP", &cgroup);
394 retrieve(d, l, "COREDUMP_TIMESTAMP", &timestamp);
395 retrieve(d, l, "COREDUMP_FILENAME", &filename);
396 retrieve(d, l, "COREDUMP", &coredump);
397 retrieve(d, l, "_BOOT_ID", &boot_id);
398 retrieve(d, l, "_MACHINE_ID", &machine_id);
399 retrieve(d, l, "_HOSTNAME", &hostname);
400 retrieve(d, l, "MESSAGE", &message);
401 }
402
403 if (need_space)
404 fputs("\n", file);
405
406 if (comm)
407 fprintf(file,
408 " PID: %s%s%s (%s)\n",
409 ansi_highlight(), strna(pid), ansi_normal(), comm);
410 else
411 fprintf(file,
412 " PID: %s%s%s\n",
413 ansi_highlight(), strna(pid), ansi_normal());
414
415 if (uid) {
416 uid_t n;
417
418 if (parse_uid(uid, &n) >= 0) {
419 _cleanup_free_ char *u = NULL;
420
421 u = uid_to_name(n);
422 fprintf(file,
423 " UID: %s (%s)\n",
424 uid, u);
425 } else {
426 fprintf(file,
427 " UID: %s\n",
428 uid);
429 }
430 }
431
432 if (gid) {
433 gid_t n;
434
435 if (parse_gid(gid, &n) >= 0) {
436 _cleanup_free_ char *g = NULL;
437
438 g = gid_to_name(n);
439 fprintf(file,
440 " GID: %s (%s)\n",
441 gid, g);
442 } else {
443 fprintf(file,
444 " GID: %s\n",
445 gid);
446 }
447 }
448
449 if (sgnl) {
450 int sig;
451
452 if (safe_atoi(sgnl, &sig) >= 0)
453 fprintf(file, " Signal: %s (%s)\n", sgnl, signal_to_string(sig));
454 else
455 fprintf(file, " Signal: %s\n", sgnl);
456 }
457
458 if (timestamp) {
459 usec_t u;
460
461 r = safe_atou64(timestamp, &u);
462 if (r >= 0) {
463 char absolute[FORMAT_TIMESTAMP_MAX], relative[FORMAT_TIMESPAN_MAX];
464
465 fprintf(file,
466 " Timestamp: %s (%s)\n",
467 format_timestamp(absolute, sizeof(absolute), u),
468 format_timestamp_relative(relative, sizeof(relative), u));
469
470 } else
471 fprintf(file, " Timestamp: %s\n", timestamp);
472 }
473
474 if (cmdline)
475 fprintf(file, " Command Line: %s\n", cmdline);
476 if (exe)
477 fprintf(file, " Executable: %s%s%s\n", ansi_highlight(), exe, ansi_normal());
478 if (cgroup)
479 fprintf(file, " Control Group: %s\n", cgroup);
480 if (unit)
481 fprintf(file, " Unit: %s\n", unit);
482 if (user_unit)
483 fprintf(file, " User Unit: %s\n", user_unit);
484 if (slice)
485 fprintf(file, " Slice: %s\n", slice);
486 if (session)
487 fprintf(file, " Session: %s\n", session);
488 if (owner_uid) {
489 uid_t n;
490
491 if (parse_uid(owner_uid, &n) >= 0) {
492 _cleanup_free_ char *u = NULL;
493
494 u = uid_to_name(n);
495 fprintf(file,
496 " Owner UID: %s (%s)\n",
497 owner_uid, u);
498 } else {
499 fprintf(file,
500 " Owner UID: %s\n",
501 owner_uid);
502 }
503 }
504 if (boot_id)
505 fprintf(file, " Boot ID: %s\n", boot_id);
506 if (machine_id)
507 fprintf(file, " Machine ID: %s\n", machine_id);
508 if (hostname)
509 fprintf(file, " Hostname: %s\n", hostname);
510
511 if (filename)
512 fprintf(file, " Storage: %s%s\n", filename,
513 access(filename, F_OK) < 0 ? " (inaccessible)" : "");
514 else if (coredump)
515 fprintf(file, " Storage: journal\n");
516 else
517 fprintf(file, " Storage: none\n");
518
519 if (message) {
520 _cleanup_free_ char *m = NULL;
521
522 m = strreplace(message, "\n", "\n ");
523
524 fprintf(file, " Message: %s\n", strstrip(m ?: message));
525 }
526
527 return 0;
528 }
529
530 static int focus(sd_journal *j) {
531 int r;
532
533 r = sd_journal_seek_tail(j);
534 if (r == 0)
535 r = sd_journal_previous(j);
536 if (r < 0)
537 return log_error_errno(r, "Failed to search journal: %m");
538 if (r == 0) {
539 log_error("No match found.");
540 return -ESRCH;
541 }
542 return r;
543 }
544
545 static void print_entry(sd_journal *j, unsigned n_found) {
546 assert(j);
547
548 if (arg_action == ACTION_INFO)
549 print_info(stdout, j, n_found);
550 else if (arg_field)
551 print_field(stdout, j);
552 else
553 print_list(stdout, j, n_found);
554 }
555
556 static int dump_list(sd_journal *j) {
557 unsigned n_found = 0;
558 int r;
559
560 assert(j);
561
562 /* The coredumps are likely to compressed, and for just
563 * listing them we don't need to decompress them, so let's
564 * pick a fairly low data threshold here */
565 sd_journal_set_data_threshold(j, 4096);
566
567 if (arg_one) {
568 r = focus(j);
569 if (r < 0)
570 return r;
571
572 print_entry(j, 0);
573 } else {
574 SD_JOURNAL_FOREACH(j)
575 print_entry(j, n_found++);
576
577 if (!arg_field && n_found <= 0) {
578 log_notice("No coredumps found.");
579 return -ESRCH;
580 }
581 }
582
583 return 0;
584 }
585
586 static int save_core(sd_journal *j, int fd, char **path, bool *unlink_temp) {
587 const char *data;
588 _cleanup_free_ char *filename = NULL;
589 size_t len;
590 int r;
591 _cleanup_close_ int fdt = -1;
592 char *temp = NULL;
593
594 assert((fd >= 0) != !!path);
595 assert(!!path == !!unlink_temp);
596
597 /* We want full data, nothing truncated. */
598 sd_journal_set_data_threshold(j, 0);
599
600 /* Look for a coredump on disk first. */
601 r = sd_journal_get_data(j, "COREDUMP_FILENAME", (const void**) &data, &len);
602 if (r < 0 && r != -ENOENT)
603 return log_error_errno(r, "Failed to retrieve COREDUMP_FILENAME: %m");
604 else if (r == 0)
605 retrieve(data, len, "COREDUMP_FILENAME", &filename);
606
607 if (filename) {
608 if (access(filename, R_OK) < 0)
609 return log_error_errno(errno, "File \"%s\" is not readable: %m", filename);
610
611 if (path && !endswith(filename, ".xz") && !endswith(filename, ".lz4")) {
612 *path = filename;
613 filename = NULL;
614
615 return 0;
616 }
617 }
618
619 if (fd < 0) {
620 const char *vt;
621
622 /* Create a temporary file to write the uncompressed core to. */
623
624 r = var_tmp_dir(&vt);
625 if (r < 0)
626 return log_error_errno(r, "Failed to acquire temporary directory path: %m");
627
628 temp = strjoin(vt, "/coredump-XXXXXX", NULL);
629 if (!temp)
630 return log_oom();
631
632 fdt = mkostemp_safe(temp);
633 if (fdt < 0)
634 return log_error_errno(fdt, "Failed to create temporary file: %m");
635 log_debug("Created temporary file %s", temp);
636
637 fd = fdt;
638 }
639
640 if (filename) {
641 #if defined(HAVE_XZ) || defined(HAVE_LZ4)
642 _cleanup_close_ int fdf;
643
644 fdf = open(filename, O_RDONLY | O_CLOEXEC);
645 if (fdf < 0) {
646 r = log_error_errno(errno, "Failed to open %s: %m", filename);
647 goto error;
648 }
649
650 r = decompress_stream(filename, fdf, fd, -1);
651 if (r < 0) {
652 log_error_errno(r, "Failed to decompress %s: %m", filename);
653 goto error;
654 }
655 #else
656 log_error("Cannot decompress file. Compiled without compression support.");
657 r = -EOPNOTSUPP;
658 goto error;
659 #endif
660 } else {
661 ssize_t sz;
662
663 r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len);
664 if (r < 0)
665 return log_error_errno(r,
666 r == -ENOENT ? "Core file was not saved for this entry." :
667 "Failed to retrieve COREDUMP field: %m");
668
669 assert(len >= 9);
670 data += 9;
671 len -= 9;
672
673 sz = write(fd, data, len);
674 if (sz < 0) {
675 r = log_error_errno(errno, "Failed to write output: %m");
676 goto error;
677 }
678 if (sz != (ssize_t) len) {
679 log_error("Short write to output.");
680 r = -EIO;
681 goto error;
682 }
683 }
684
685 if (temp) {
686 *path = temp;
687 *unlink_temp = true;
688 }
689 return 0;
690
691 error:
692 if (temp) {
693 unlink(temp);
694 log_debug("Removed temporary file %s", temp);
695 }
696 return r;
697 }
698
699 static int dump_core(sd_journal* j) {
700 int r;
701
702 assert(j);
703
704 r = focus(j);
705 if (r < 0)
706 return r;
707
708 print_info(arg_output ? stdout : stderr, j, false);
709
710 if (on_tty() && !arg_output) {
711 log_error("Refusing to dump core to tty.");
712 return -ENOTTY;
713 }
714
715 r = save_core(j, arg_output ? fileno(arg_output) : STDOUT_FILENO, NULL, NULL);
716 if (r < 0)
717 return log_error_errno(r, "Coredump retrieval failed: %m");
718
719 r = sd_journal_previous(j);
720 if (r > 0)
721 log_warning("More than one entry matches, ignoring rest.");
722
723 return 0;
724 }
725
726 static int run_gdb(sd_journal *j) {
727 _cleanup_free_ char *exe = NULL, *path = NULL;
728 bool unlink_path = false;
729 const char *data;
730 siginfo_t st;
731 size_t len;
732 pid_t pid;
733 int r;
734
735 assert(j);
736
737 r = focus(j);
738 if (r < 0)
739 return r;
740
741 print_info(stdout, j, false);
742 fputs("\n", stdout);
743
744 r = sd_journal_get_data(j, "COREDUMP_EXE", (const void**) &data, &len);
745 if (r < 0)
746 return log_error_errno(r, "Failed to retrieve COREDUMP_EXE field: %m");
747
748 assert(len > strlen("COREDUMP_EXE="));
749 data += strlen("COREDUMP_EXE=");
750 len -= strlen("COREDUMP_EXE=");
751
752 exe = strndup(data, len);
753 if (!exe)
754 return log_oom();
755
756 if (endswith(exe, " (deleted)")) {
757 log_error("Binary already deleted.");
758 return -ENOENT;
759 }
760
761 if (!path_is_absolute(exe)) {
762 log_error("Binary is not an absolute path.");
763 return -ENOENT;
764 }
765
766 r = save_core(j, -1, &path, &unlink_path);
767 if (r < 0)
768 return log_error_errno(r, "Failed to retrieve core: %m");
769
770 pid = fork();
771 if (pid < 0) {
772 r = log_error_errno(errno, "Failed to fork(): %m");
773 goto finish;
774 }
775 if (pid == 0) {
776 (void) reset_all_signal_handlers();
777 (void) reset_signal_mask();
778
779 execlp("gdb", "gdb", exe, path, NULL);
780
781 log_error_errno(errno, "Failed to invoke gdb: %m");
782 _exit(1);
783 }
784
785 r = wait_for_terminate(pid, &st);
786 if (r < 0) {
787 log_error_errno(r, "Failed to wait for gdb: %m");
788 goto finish;
789 }
790
791 r = st.si_code == CLD_EXITED ? st.si_status : 255;
792
793 finish:
794 if (unlink_path) {
795 log_debug("Removed temporary file %s", path);
796 unlink(path);
797 }
798
799 return r;
800 }
801
802 int main(int argc, char *argv[]) {
803 _cleanup_(sd_journal_closep) sd_journal*j = NULL;
804 const char* match;
805 Iterator it;
806 int r = 0;
807 _cleanup_set_free_free_ Set *matches = NULL;
808
809 setlocale(LC_ALL, "");
810 log_parse_environment();
811 log_open();
812
813 matches = new_matches();
814 if (!matches) {
815 r = -ENOMEM;
816 goto end;
817 }
818
819 r = parse_argv(argc, argv, matches);
820 if (r < 0)
821 goto end;
822
823 if (arg_action == ACTION_NONE)
824 goto end;
825
826 sigbus_install();
827
828 if (arg_directory) {
829 r = sd_journal_open_directory(&j, arg_directory, 0);
830 if (r < 0) {
831 log_error_errno(r, "Failed to open journals in directory: %s: %m", arg_directory);
832 goto end;
833 }
834 } else {
835 r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
836 if (r < 0) {
837 log_error_errno(r, "Failed to open journal: %m");
838 goto end;
839 }
840 }
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 }