chiark / gitweb /
macro: rework how we define cleanup macros
[elogind.git] / src / journal / coredumpctl.c
index 51d333c..97d967d 100644 (file)
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+#include <locale.h>
 #include <stdio.h>
 #include <string.h>
 #include <getopt.h>
 #include <stdio.h>
 #include <string.h>
 #include <getopt.h>
+#include <fcntl.h>
+#include <unistd.h>
 
 #include <systemd/sd-journal.h>
 
 
 #include <systemd/sd-journal.h>
 
 #include "log.h"
 #include "path-util.h"
 #include "pager.h"
 #include "log.h"
 #include "path-util.h"
 #include "pager.h"
+#include "macro.h"
+#include "journal-internal.h"
 
 static enum {
         ACTION_NONE,
         ACTION_LIST,
         ACTION_DUMP,
 
 static enum {
         ACTION_NONE,
         ACTION_LIST,
         ACTION_DUMP,
+        ACTION_GDB,
 } arg_action = ACTION_LIST;
 
 } arg_action = ACTION_LIST;
 
-static Set *matches = NULL;
 static FILE* output = NULL;
 static FILE* output = NULL;
+static char* field = NULL;
 
 static int arg_no_pager = false;
 
 static int arg_no_pager = false;
+static int arg_no_legend = false;
 
 static Set *new_matches(void) {
         Set *set;
 
 static Set *new_matches(void) {
         Set *set;
@@ -57,7 +64,7 @@ static Set *new_matches(void) {
         tmp = strdup("MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1");
         if (!tmp) {
                 log_oom();
         tmp = strdup("MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1");
         if (!tmp) {
                 log_oom();
-                set_clear_free(set);
+                set_free(set);
                 return NULL;
         }
 
                 return NULL;
         }
 
@@ -65,7 +72,7 @@ static Set *new_matches(void) {
         if (r < 0) {
                 log_error("failed to add to set: %s", strerror(-r));
                 free(tmp);
         if (r < 0) {
                 log_error("failed to add to set: %s", strerror(-r));
                 free(tmp);
-                set_clear_free(set);
+                set_free(set);
                 return NULL;
         }
 
                 return NULL;
         }
 
@@ -82,6 +89,8 @@ static int help(void) {
                "Commands:\n"
                "  -h --help         Show this help\n"
                "  --version         Print version string\n"
                "Commands:\n"
                "  -h --help         Show this help\n"
                "  --version         Print version string\n"
+               "  -F --field=FIELD  List all values a certain field takes\n"
+               "  gdb               Start gdb for the first matching coredump\n"
                "  list              List available coredumps\n"
                "  dump PID          Print coredump to stdout\n"
                "  dump PATH         Print coredump to stdout\n"
                "  list              List available coredumps\n"
                "  dump PID          Print coredump to stdout\n"
                "  dump PATH         Print coredump to stdout\n"
@@ -131,25 +140,29 @@ fail:
         return r;
 }
 
         return r;
 }
 
-static int parse_argv(int argc, char *argv[]) {
+static int parse_argv(int argc, char *argv[], Set *matches) {
         enum {
                 ARG_VERSION = 0x100,
                 ARG_NO_PAGER,
         enum {
                 ARG_VERSION = 0x100,
                 ARG_NO_PAGER,
+                ARG_NO_LEGEND,
         };
 
         int r, c;
 
         static const struct option options[] = {
         };
 
         int r, c;
 
         static const struct option options[] = {
-                { "help",         no_argument,       NULL, 'h'              },
-                { "version" ,     no_argument,       NULL, ARG_VERSION      },
-                { "no-pager",     no_argument,       NULL, ARG_NO_PAGER     },
-                { "output",       required_argument, NULL, 'o'              },
+                { "help",         no_argument,       NULL, 'h'           },
+                { "version" ,     no_argument,       NULL, ARG_VERSION   },
+                { "no-pager",     no_argument,       NULL, ARG_NO_PAGER  },
+                { "no-legend",    no_argument,       NULL, ARG_NO_LEGEND },
+                { "output",       required_argument, NULL, 'o'           },
+                { "field",        required_argument, NULL, 'F'           },
+                { NULL,           0,                 NULL, 0             }
         };
 
         assert(argc >= 0);
         assert(argv);
 
         };
 
         assert(argc >= 0);
         assert(argv);
 
-        while ((c = getopt_long(argc, argv, "ho:", options, NULL)) >= 0)
+        while ((c = getopt_long(argc, argv, "ho:F:", options, NULL)) >= 0)
                 switch(c) {
                 case 'h':
                         help();
                 switch(c) {
                 case 'h':
                         help();
@@ -158,7 +171,6 @@ static int parse_argv(int argc, char *argv[]) {
 
                 case ARG_VERSION:
                         puts(PACKAGE_STRING);
 
                 case ARG_VERSION:
                         puts(PACKAGE_STRING);
-                        puts(DISTRIBUTION);
                         puts(SYSTEMD_FEATURES);
                         arg_action = ACTION_NONE;
                         return 0;
                         puts(SYSTEMD_FEATURES);
                         arg_action = ACTION_NONE;
                         return 0;
@@ -167,6 +179,10 @@ static int parse_argv(int argc, char *argv[]) {
                         arg_no_pager = true;
                         break;
 
                         arg_no_pager = true;
                         break;
 
+                case ARG_NO_LEGEND:
+                        arg_no_legend = true;
+                        break;
+
                 case 'o':
                         if (output) {
                                 log_error("cannot set output more than once");
                 case 'o':
                         if (output) {
                                 log_error("cannot set output more than once");
@@ -180,6 +196,19 @@ static int parse_argv(int argc, char *argv[]) {
                         }
 
                         break;
                         }
 
                         break;
+
+                case 'F':
+                        if (field) {
+                                log_error("cannot use --field/-F more than once");
+                                return -EINVAL;
+                        }
+
+                        field = optarg;
+                        break;
+
+                case '?':
+                        return -EINVAL;
+
                 default:
                         log_error("Unknown option code %c", c);
                         return -EINVAL;
                 default:
                         log_error("Unknown option code %c", c);
                         return -EINVAL;
@@ -191,12 +220,19 @@ static int parse_argv(int argc, char *argv[]) {
                         arg_action = ACTION_LIST;
                 else if (streq(cmd, "dump"))
                         arg_action = ACTION_DUMP;
                         arg_action = ACTION_LIST;
                 else if (streq(cmd, "dump"))
                         arg_action = ACTION_DUMP;
+                else if (streq(cmd, "gdb"))
+                        arg_action = ACTION_GDB;
                 else {
                         log_error("Unknown action '%s'", cmd);
                         return -EINVAL;
                 }
         }
 
                 else {
                         log_error("Unknown action '%s'", cmd);
                         return -EINVAL;
                 }
         }
 
+        if (field && arg_action != ACTION_LIST) {
+                log_error("Option --field/-F only makes sense with list");
+                return -EINVAL;
+        }
+
         while (optind < argc) {
                 r = add_match(matches, argv[optind]);
                 if (r != 0)
         while (optind < argc) {
                 r = add_match(matches, argv[optind]);
                 if (r != 0)
@@ -207,61 +243,98 @@ static int parse_argv(int argc, char *argv[]) {
         return 0;
 }
 
         return 0;
 }
 
-static int retrieve(sd_journal *j, const char *name, const char **var) {
-        const void *data;
-        size_t len, field;
-        int r;
+static int retrieve(const void *data,
+                    size_t len,
+                    const char *name,
+                    const char **var) {
 
 
-        r = sd_journal_get_data(j, name, &data, &len);
-        if (r < 0) {
-                log_warning("Failed to retrieve %s", name);
-                return r;
-        }
+        size_t ident;
+
+        ident = strlen(name) + 1; /* name + "=" */
 
 
-        field = strlen(name) + 1; // name + "="
-        assert(len >= field);
+        if (len < ident)
+                return 0;
 
 
-        *var = strndup((const char*)data + field, len - field);
-        if (!var)
+        if (memcmp(data, name, ident - 1) != 0)
+                return 0;
+
+        if (((const char*) data)[ident - 1] != '=')
+                return 0;
+
+        *var = strndup((const char*)data + ident, len - ident);
+        if (!*var)
                 return log_oom();
 
         return 0;
 }
 
                 return log_oom();
 
         return 0;
 }
 
-static void print_entry(FILE* file, sd_journal *j, int had_header) {
+static void print_field(FILE* file, sd_journal *j) {
+        const char _cleanup_free_ *value = NULL;
+        const void *d;
+        size_t l;
+
+        assert(field);
+
+        SD_JOURNAL_FOREACH_DATA(j, d, l)
+                retrieve(d, l, field, &value);
+        if (value)
+                fprintf(file, "%s\n", value);
+}
+
+static int print_entry(FILE* file, sd_journal *j, int had_legend) {
         const char _cleanup_free_
                 *pid = NULL, *uid = NULL, *gid = NULL,
                 *sgnl = NULL, *exe = NULL;
         const char _cleanup_free_
                 *pid = NULL, *uid = NULL, *gid = NULL,
                 *sgnl = NULL, *exe = NULL;
+        const void *d;
+        size_t l;
+        usec_t t;
+        char buf[FORMAT_TIMESTAMP_MAX];
+        int r;
 
 
-        retrieve(j, "COREDUMP_PID", &pid);
-        retrieve(j, "COREDUMP_UID", &uid);
-        retrieve(j, "COREDUMP_GID", &gid);
-        retrieve(j, "COREDUMP_SIGNAL", &sgnl);
-        retrieve(j, "COREDUMP_EXE", &exe);
-        if (!exe)
-                retrieve(j, "COREDUMP_COMM", &exe);
-        if (!exe)
-                retrieve(j, "COREDUMP_CMDLINE", &exe);
+        SD_JOURNAL_FOREACH_DATA(j, d, l) {
+                retrieve(d, l, "COREDUMP_PID", &pid);
+                retrieve(d, l, "COREDUMP_PID", &pid);
+                retrieve(d, l, "COREDUMP_UID", &uid);
+                retrieve(d, l, "COREDUMP_GID", &gid);
+                retrieve(d, l, "COREDUMP_SIGNAL", &sgnl);
+                retrieve(d, l, "COREDUMP_EXE", &exe);
+                if (!exe)
+                        retrieve(d, l, "COREDUMP_COMM", &exe);
+                if (!exe)
+                        retrieve(d, l, "COREDUMP_CMDLINE", &exe);
+        }
 
         if (!pid && !uid && !gid && !sgnl && !exe) {
 
         if (!pid && !uid && !gid && !sgnl && !exe) {
-                log_warning("empty coredump log entry");
-                return;
+                log_warning("Empty coredump log entry");
+                return -EINVAL;
         }
 
         }
 
-        if (!had_header)
-                fprintf(file, "%*s %*s %*s %*s %s\n",
+        r = sd_journal_get_realtime_usec(j, &t);
+        if (r < 0) {
+                log_error("Failed to get realtime timestamp: %s", strerror(-r));
+                return r;
+        }
+
+        format_timestamp(buf, sizeof(buf), t);
+
+        if (!had_legend && !arg_no_legend)
+                fprintf(file, "%-*s %*s %*s %*s %*s %s\n",
+                        FORMAT_TIMESTAMP_MAX-1, "TIME",
                         6, "PID",
                         5, "UID",
                         5, "GID",
                         6, "PID",
                         5, "UID",
                         5, "GID",
-                        3, "sig",
-                        "exe");
+                        3, "SIG",
+                           "EXE");
 
 
-        fprintf(file, "%*s %*s %*s %*s %s\n",
+        fprintf(file, "%*s %*s %*s %*s %*s %s\n",
+                FORMAT_TIMESTAMP_MAX-1, buf,
                 6, pid,
                 5, uid,
                 5, gid,
                 3, sgnl,
                 exe);
                 6, pid,
                 5, uid,
                 5, gid,
                 3, sgnl,
                 exe);
+
+        return 0;
 }
 
 static int dump_list(sd_journal *j) {
 }
 
 static int dump_list(sd_journal *j) {
@@ -269,24 +342,29 @@ static int dump_list(sd_journal *j) {
 
         assert(j);
 
 
         assert(j);
 
-        SD_JOURNAL_FOREACH(j)
-                print_entry(stdout, j, found++);
+        /* The coredumps are likely to compressed, and for just
+         * listing them we don#t need to decompress them, so let's
+         * pick a fairly low data threshold here */
+        sd_journal_set_data_threshold(j, 4096);
 
 
-        if (!found) {
-                log_error("no coredumps found");
+        SD_JOURNAL_FOREACH(j) {
+                if (field)
+                        print_field(stdout, j);
+                else
+                        print_entry(stdout, j, found++);
+        }
+
+        if (!field && !found) {
+                log_notice("No coredumps found");
                 return -ESRCH;
         }
 
         return 0;
 }
 
                 return -ESRCH;
         }
 
         return 0;
 }
 
-static int dump_core(sd_journal* j) {
-        const char *data;
-        size_t len, ret;
+static int focus(sd_journal *j) {
         int r;
 
         int r;
 
-        assert(j);
-
         r = sd_journal_seek_tail(j);
         if (r == 0)
                 r = sd_journal_previous(j);
         r = sd_journal_seek_tail(j);
         if (r == 0)
                 r = sd_journal_previous(j);
@@ -294,17 +372,26 @@ static int dump_core(sd_journal* j) {
                 log_error("Failed to search journal: %s", strerror(-r));
                 return r;
         }
                 log_error("Failed to search journal: %s", strerror(-r));
                 return r;
         }
+        if (r == 0) {
+                log_error("No match found");
+                return -ESRCH;
+        }
+        return r;
+}
+
+static int dump_core(sd_journal* j) {
+        const void *data;
+        size_t len, ret;
+        int r;
+
+        assert(j);
 
 
-       if (r == 0) {
-               log_error("No match found");
-               return -ESRCH;
-       }
+        /* We want full data, nothing truncated. */
+        sd_journal_set_data_threshold(j, 0);
 
 
-        r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len);
-        if (r != 0) {
-                log_error("retrieve COREDUMP field: %s", strerror(-r));
+        r = focus(j);
+        if (r < 0)
                 return r;
                 return r;
-        }
 
         print_entry(output ? stdout : stderr, j, false);
 
 
         print_entry(output ? stdout : stderr, j, false);
 
@@ -313,9 +400,17 @@ static int dump_core(sd_journal* j) {
                 return -ENOTTY;
         }
 
                 return -ENOTTY;
         }
 
+        r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len);
+        if (r < 0) {
+                log_error("Failed to retrieve COREDUMP field: %s", strerror(-r));
+                return r;
+        }
+
         assert(len >= 9);
         assert(len >= 9);
+        data = (const uint8_t*) data + 9;
+        len -= 9;
 
 
-        ret = fwrite(data+9, len-9, 1, output ? output : stdout);
+        ret = fwrite(data, len, 1, output ? output : stdout);
         if (ret != 1) {
                 log_error("dumping coredump: %m (%zu)", ret);
                 return -errno;
         if (ret != 1) {
                 log_error("dumping coredump: %m (%zu)", ret);
                 return -errno;
@@ -328,20 +423,121 @@ static int dump_core(sd_journal* j) {
         return 0;
 }
 
         return 0;
 }
 
+static int run_gdb(sd_journal *j) {
+        char path[] = "/var/tmp/coredump-XXXXXX";
+        const void *data;
+        size_t len;
+        ssize_t sz;
+        pid_t pid;
+        _cleanup_free_ char *exe = NULL;
+        int r;
+        _cleanup_close_ int fd = -1;
+        siginfo_t st;
+
+        assert(j);
+
+        sd_journal_set_data_threshold(j, 0);
+
+        r = focus(j);
+        if (r < 0)
+                return r;
+
+        print_entry(stdout, j, false);
+
+        r = sd_journal_get_data(j, "COREDUMP_EXE", (const void**) &data, &len);
+        if (r < 0) {
+                log_error("Failed to retrieve COREDUMP_EXE field: %s", strerror(-r));
+                return r;
+        }
+
+        assert(len >= 13);
+        data = (const uint8_t*) data + 13;
+        len -= 13;
+
+        exe = strndup(data, len);
+        if (!exe)
+                return log_oom();
+
+        if (endswith(exe, " (deleted)")) {
+                log_error("Binary already deleted.");
+                return -ENOENT;
+        }
+
+        r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len);
+        if (r < 0) {
+                log_error("Failed to retrieve COREDUMP field: %s", strerror(-r));
+                return r;
+        }
+
+        assert(len >= 9);
+        data = (const uint8_t*) data + 9;
+        len -= 9;
+
+        fd = mkostemp(path, O_WRONLY);
+        if (fd < 0) {
+                log_error("Failed to create temporary file: %m");
+                return -errno;
+        }
+
+        sz = write(fd, data, len);
+        if (sz < 0) {
+                log_error("Failed to write temporary file: %s", strerror(errno));
+                r = -errno;
+                goto finish;
+        }
+        if (sz != (ssize_t) len) {
+                log_error("Short write to temporary file.");
+                r = -EIO;
+                goto finish;
+        }
+
+        close_nointr_nofail(fd);
+        fd = -1;
+
+        pid = fork();
+        if (pid < 0) {
+                log_error("Failed to fork(): %m");
+                r = -errno;
+                goto finish;
+        }
+        if (pid == 0) {
+                execlp("gdb", "gdb", exe, path, NULL);
+                log_error("Failed to invoke gdb: %m");
+                _exit(1);
+        }
+
+        r = wait_for_terminate(pid, &st);
+        if (r < 0) {
+                log_error("Failed to wait for gdb: %m");
+                goto finish;
+        }
+
+        r = st.si_code == CLD_EXITED ? st.si_status : 255;
+
+finish:
+        unlink(path);
+        return r;
+}
+
 int main(int argc, char *argv[]) {
 int main(int argc, char *argv[]) {
-        sd_journal *j = NULL;
+        sd_journal _cleanup_journal_close_ *j = NULL;
         const char* match;
         Iterator it;
         int r = 0;
         const char* match;
         Iterator it;
         int r = 0;
+        Set _cleanup_set_free_free_ *matches = NULL;
 
 
+        setlocale(LC_ALL, "");
         log_parse_environment();
         log_open();
 
         matches = new_matches();
         log_parse_environment();
         log_open();
 
         matches = new_matches();
-        if (!matches)
+        if (!matches) {
+                r = -ENOMEM;
                 goto end;
                 goto end;
+        }
 
 
-        if (parse_argv(argc, argv))
+        r = parse_argv(argc, argv, matches);
+        if (r < 0)
                 goto end;
 
         if (arg_action == ACTION_NONE)
                 goto end;
 
         if (arg_action == ACTION_NONE)
@@ -354,8 +550,6 @@ int main(int argc, char *argv[]) {
         }
 
         SET_FOREACH(match, matches, it) {
         }
 
         SET_FOREACH(match, matches, it) {
-                log_info("Matching: %s", match);
-
                 r = sd_journal_add_match(j, match, strlen(match));
                 if (r != 0) {
                         log_error("Failed to add match '%s': %s",
                 r = sd_journal_add_match(j, match, strlen(match));
                 if (r != 0) {
                         log_error("Failed to add match '%s': %s",
@@ -365,29 +559,31 @@ int main(int argc, char *argv[]) {
         }
 
         switch(arg_action) {
         }
 
         switch(arg_action) {
+
         case ACTION_LIST:
                 if (!arg_no_pager)
         case ACTION_LIST:
                 if (!arg_no_pager)
-                        pager_open();
+                        pager_open(false);
 
                 r = dump_list(j);
                 break;
 
                 r = dump_list(j);
                 break;
+
         case ACTION_DUMP:
                 r = dump_core(j);
                 break;
         case ACTION_DUMP:
                 r = dump_core(j);
                 break;
-        case ACTION_NONE:
+
+        case  ACTION_GDB:
+                r = run_gdb(j);
+                break;
+
+        default:
                 assert_not_reached("Shouldn't be here");
         }
 
 end:
                 assert_not_reached("Shouldn't be here");
         }
 
 end:
-        if (j)
-                sd_journal_close(j);
-
-        set_free_free(matches);
-
         pager_close();
 
         if (output)
                 fclose(output);
 
         pager_close();
 
         if (output)
                 fclose(output);
 
-        return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
+        return r >= 0 ? r : EXIT_FAILURE;
 }
 }