]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/journal/coredumpctl.c
tree-wide: make use of log_error_errno() return value in more cases
[thirdparty/systemd.git] / src / journal / coredumpctl.c
index bcb0ff9c39a0ffca80d28a074632cf5512488df9..0546290318985a1ab910966c26bbf20cd78d68dc 100644 (file)
@@ -39,6 +39,7 @@
 #include "sigbus.h"
 #include "process-util.h"
 #include "terminal-util.h"
+#include "signal-util.h"
 
 static enum {
         ACTION_NONE,
@@ -48,6 +49,7 @@ static enum {
         ACTION_GDB,
 } arg_action = ACTION_LIST;
 static const char* arg_field = NULL;
+static const char *arg_directory = NULL;
 static int arg_no_pager = false;
 static int arg_no_legend = false;
 static int arg_one = false;
@@ -130,6 +132,7 @@ static void help(void) {
                "  -1                 Show information about most recent entry only\n"
                "  -F --field=FIELD   List all values a certain field takes\n"
                "  -o --output=FILE   Write output to FILE\n\n"
+               "  -D --directory=DIR Use journal files from directory\n\n"
 
                "Commands:\n"
                "  list [MATCHES...]  List available coredumps (default)\n"
@@ -155,13 +158,14 @@ static int parse_argv(int argc, char *argv[], Set *matches) {
                 { "no-legend",    no_argument,       NULL, ARG_NO_LEGEND },
                 { "output",       required_argument, NULL, 'o'           },
                 { "field",        required_argument, NULL, 'F'           },
+                { "directory",    required_argument, NULL, 'D'           },
                 {}
         };
 
         assert(argc >= 0);
         assert(argv);
 
-        while ((c = getopt_long(argc, argv, "ho:F:1", options, NULL)) >= 0)
+        while ((c = getopt_long(argc, argv, "ho:F:1D:", options, NULL)) >= 0)
                 switch(c) {
 
                 case 'h':
@@ -207,6 +211,10 @@ static int parse_argv(int argc, char *argv[], Set *matches) {
                         arg_one = true;
                         break;
 
+                case 'D':
+                        arg_directory = optarg;
+                        break;
+
                 case '?':
                         return -EINVAL;
 
@@ -586,8 +594,7 @@ static int save_core(sd_journal *j, int fd, char **path, bool *unlink_temp) {
         if (filename && access(filename, R_OK) < 0) {
                 log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING,
                          "File %s is not readable: %m", filename);
-                free(filename);
-                filename = NULL;
+                filename = mfree(filename);
         }
 
         if (filename && !endswith(filename, ".xz") && !endswith(filename, ".lz4")) {
@@ -624,8 +631,8 @@ static int save_core(sd_journal *j, int fd, char **path, bool *unlink_temp) {
 
                         sz = write(fdt, data, len);
                         if (sz < 0) {
-                                log_error_errno(errno, "Failed to write temporary file: %m");
-                                r = -errno;
+                                r = log_error_errno(errno,
+                                                    "Failed to write temporary file: %m");
                                 goto error;
                         }
                         if (sz != (ssize_t) len) {
@@ -639,8 +646,9 @@ static int save_core(sd_journal *j, int fd, char **path, bool *unlink_temp) {
 
                         fdf = open(filename, O_RDONLY | O_CLOEXEC);
                         if (fdf < 0) {
-                                log_error_errno(errno, "Failed to open %s: %m", filename);
-                                r = -errno;
+                                r = log_error_errno(errno,
+                                                    "Failed to open %s: %m",
+                                                    filename);
                                 goto error;
                         }
 
@@ -751,11 +759,13 @@ static int run_gdb(sd_journal *j) {
 
         pid = fork();
         if (pid < 0) {
-                log_error_errno(errno, "Failed to fork(): %m");
-                r = -errno;
+                r = log_error_errno(errno, "Failed to fork(): %m");
                 goto finish;
         }
         if (pid == 0) {
+                (void) reset_all_signal_handlers();
+                (void) reset_signal_mask();
+
                 execlp("gdb", "gdb", exe, path, NULL);
 
                 log_error_errno(errno, "Failed to invoke gdb: %m");
@@ -805,10 +815,18 @@ int main(int argc, char *argv[]) {
 
         sigbus_install();
 
-        r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
-        if (r < 0) {
-                log_error_errno(r, "Failed to open journal: %m");
-                goto end;
+        if (arg_directory) {
+                r = sd_journal_open_directory(&j, arg_directory, 0);
+                if (r < 0) {
+                        log_error_errno(r, "Failed to open journals in directory: %s: %m", arg_directory);
+                        goto end;
+                }
+        } else {
+                r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
+                if (r < 0) {
+                        log_error_errno(r, "Failed to open journal: %m");
+                        goto end;
+                }
         }
 
         /* We want full data, nothing truncated. */