chiark / gitweb /
macro: rework how we define cleanup macros
[elogind.git] / src / journal / coredumpctl.c
index 311c18b..97d967d 100644 (file)
@@ -19,6 +19,7 @@
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+#include <locale.h>
 #include <stdio.h>
 #include <string.h>
 #include <getopt.h>
@@ -33,6 +34,8 @@
 #include "log.h"
 #include "path-util.h"
 #include "pager.h"
+#include "macro.h"
+#include "journal-internal.h"
 
 static enum {
         ACTION_NONE,
@@ -41,10 +44,11 @@ static enum {
         ACTION_GDB,
 } arg_action = ACTION_LIST;
 
-static Set *matches = NULL;
 static FILE* output = NULL;
+static char* field = NULL;
 
 static int arg_no_pager = false;
+static int arg_no_legend = false;
 
 static Set *new_matches(void) {
         Set *set;
@@ -85,6 +89,8 @@ static int help(void) {
                "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"
@@ -134,25 +140,29 @@ fail:
         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,
+                ARG_NO_LEGEND,
         };
 
         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);
 
-        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();
@@ -161,7 +171,6 @@ static int parse_argv(int argc, char *argv[]) {
 
                 case ARG_VERSION:
                         puts(PACKAGE_STRING);
-                        puts(DISTRIBUTION);
                         puts(SYSTEMD_FEATURES);
                         arg_action = ACTION_NONE;
                         return 0;
@@ -170,6 +179,10 @@ static int parse_argv(int argc, char *argv[]) {
                         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");
@@ -183,6 +196,19 @@ static int parse_argv(int argc, char *argv[]) {
                         }
 
                         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;
@@ -202,6 +228,11 @@ static int parse_argv(int argc, char *argv[]) {
                 }
         }
 
+        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)
@@ -217,27 +248,40 @@ static int retrieve(const void *data,
                     const char *name,
                     const char **var) {
 
-        size_t field;
+        size_t ident;
 
-        field = strlen(name) + 1; /* name + "=" */
+        ident = strlen(name) + 1; /* name + "=" */
 
-        if (len < field)
+        if (len < ident)
                 return 0;
 
-        if (memcmp(data, name, field - 1) != 0)
+        if (memcmp(data, name, ident - 1) != 0)
                 return 0;
 
-        if (((const char*) data)[field - 1] != '=')
+        if (((const char*) data)[ident - 1] != '=')
                 return 0;
 
-        *var = strndup((const char*)data + field, len - field);
-        if (!var)
+        *var = strndup((const char*)data + ident, len - ident);
+        if (!*var)
                 return log_oom();
 
         return 0;
 }
 
-static int 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;
@@ -273,7 +317,7 @@ static int print_entry(FILE* file, sd_journal *j, int had_header) {
 
         format_timestamp(buf, sizeof(buf), t);
 
-        if (!had_header)
+        if (!had_legend && !arg_no_legend)
                 fprintf(file, "%-*s %*s %*s %*s %*s %s\n",
                         FORMAT_TIMESTAMP_MAX-1, "TIME",
                         6, "PID",
@@ -298,10 +342,19 @@ static int dump_list(sd_journal *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);
+
+        SD_JOURNAL_FOREACH(j) {
+                if (field)
+                        print_field(stdout, j);
+                else
+                        print_entry(stdout, j, found++);
+        }
 
-        if (!found) {
+        if (!field && !found) {
                 log_notice("No coredumps found");
                 return -ESRCH;
         }
@@ -333,6 +386,9 @@ static int dump_core(sd_journal* j) {
 
         assert(j);
 
+        /* We want full data, nothing truncated. */
+        sd_journal_set_data_threshold(j, 0);
+
         r = focus(j);
         if (r < 0)
                 return r;
@@ -380,6 +436,8 @@ static int run_gdb(sd_journal *j) {
 
         assert(j);
 
+        sd_journal_set_data_threshold(j, 0);
+
         r = focus(j);
         if (r < 0)
                 return r;
@@ -462,19 +520,24 @@ finish:
 }
 
 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;
+        Set _cleanup_set_free_free_ *matches = NULL;
 
+        setlocale(LC_ALL, "");
         log_parse_environment();
         log_open();
 
         matches = new_matches();
-        if (!matches)
+        if (!matches) {
+                r = -ENOMEM;
                 goto end;
+        }
 
-        if (parse_argv(argc, argv))
+        r = parse_argv(argc, argv, matches);
+        if (r < 0)
                 goto end;
 
         if (arg_action == ACTION_NONE)
@@ -499,7 +562,7 @@ int main(int argc, char *argv[]) {
 
         case ACTION_LIST:
                 if (!arg_no_pager)
-                        pager_open();
+                        pager_open(false);
 
                 r = dump_list(j);
                 break;
@@ -517,11 +580,6 @@ int main(int argc, char *argv[]) {
         }
 
 end:
-        if (j)
-                sd_journal_close(j);
-
-        set_free_free(matches);
-
         pager_close();
 
         if (output)