X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fjournal%2Fcoredumpctl.c;h=3bceb48ff731351afb2dff3c9d9d675ad4da44e6;hb=b5d3e1688133077ca20542a20dcd8919147e72e1;hp=5b494e340baa3fe9f2d4a0c677e70627bc09d4cb;hpb=ada45c785fc7b0cbe0adb9cbf641943410f4953f;p=elogind.git
diff --git a/src/journal/coredumpctl.c b/src/journal/coredumpctl.c
index 5b494e340..3bceb48ff 100644
--- a/src/journal/coredumpctl.c
+++ b/src/journal/coredumpctl.c
@@ -19,6 +19,7 @@
along with systemd; If not, see .
***/
+#include
#include
#include
#include
@@ -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;
@@ -60,15 +64,14 @@ static Set *new_matches(void) {
tmp = strdup("MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1");
if (!tmp) {
log_oom();
- set_clear_free(set);
+ set_free(set);
return NULL;
}
- r = set_put(set, tmp);
+ r = set_consume(set, 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;
}
@@ -81,10 +84,13 @@ static int help(void) {
"Flags:\n"
" -o --output=FILE Write output to FILE\n"
" --no-pager Do not pipe output into a pager\n"
+ " --no-legend Do not print the column headers.\n\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"
@@ -98,7 +104,7 @@ static int add_match(Set *set, const char *match) {
unsigned pid;
const char* prefix;
char *pattern = NULL;
- char _cleanup_free_ *p = NULL;
+ _cleanup_free_ char *p = NULL;
if (strchr(match, '='))
prefix = "";
@@ -119,57 +125,64 @@ static int add_match(Set *set, const char *match) {
if (!pattern)
goto fail;
+ log_debug("Adding pattern: %s", pattern);
r = set_put(set, pattern);
if (r < 0) {
- log_error("failed to add pattern '%s': %s",
+ log_error("Failed to add pattern '%s': %s",
pattern, strerror(-r));
+ free(pattern);
goto fail;
}
- log_debug("Added pattern: %s", pattern);
return 0;
fail:
- free(pattern);
- log_error("failed to add match: %s", strerror(-r));
+ log_error("Failed to add match: %s", strerror(-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,
+ 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' },
+ {}
};
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();
arg_action = ACTION_NONE;
- return 0;
+ return help();
case ARG_VERSION:
+ arg_action = ACTION_NONE;
puts(PACKAGE_STRING);
- puts(DISTRIBUTION);
puts(SYSTEMD_FEATURES);
- arg_action = ACTION_NONE;
return 0;
case ARG_NO_PAGER:
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,14 +196,26 @@ static int parse_argv(int argc, char *argv[]) {
}
break;
- default:
- log_error("Unknown option code %c", c);
+
+ case 'F':
+ if (field) {
+ log_error("cannot use --field/-F more than once");
+ return -EINVAL;
+ }
+
+ field = optarg;
+ break;
+
+ case '?':
return -EINVAL;
+
+ default:
+ assert_not_reached("Unhandled option");
}
if (optind < argc) {
const char *cmd = argv[optind++];
- if(streq(cmd, "list"))
+ if (streq(cmd, "list"))
arg_action = ACTION_LIST;
else if (streq(cmd, "dump"))
arg_action = ACTION_DUMP;
@@ -202,6 +227,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,28 +247,41 @@ 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) {
- const char _cleanup_free_
+static void print_field(FILE* file, sd_journal *j) {
+ _cleanup_free_ const char *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) {
+ _cleanup_free_ const char
*pid = NULL, *uid = NULL, *gid = NULL,
*sgnl = NULL, *exe = NULL;
const void *d;
@@ -273,7 +316,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 +341,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 +385,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;
@@ -362,7 +417,7 @@ static int dump_core(sd_journal* j) {
r = sd_journal_previous(j);
if (r >= 0)
- log_warning("More than one entry matches, ignoring rest.\n");
+ log_warning("More than one entry matches, ignoring rest.");
return 0;
}
@@ -380,6 +435,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;
@@ -415,7 +472,7 @@ static int run_gdb(sd_journal *j) {
data = (const uint8_t*) data + 9;
len -= 9;
- fd = mkostemp(path, O_WRONLY);
+ fd = mkostemp_safe(path, O_WRONLY|O_CLOEXEC);
if (fd < 0) {
log_error("Failed to create temporary file: %m");
return -errno;
@@ -423,7 +480,7 @@ static int run_gdb(sd_journal *j) {
sz = write(fd, data, len);
if (sz < 0) {
- log_error("Failed to write temporary file: %s", strerror(errno));
+ log_error("Failed to write temporary file: %m");
r = -errno;
goto finish;
}
@@ -462,19 +519,24 @@ finish:
}
int main(int argc, char *argv[]) {
- sd_journal *j = NULL;
+ _cleanup_journal_close_ sd_journal*j = NULL;
const char* match;
Iterator it;
int r = 0;
+ _cleanup_set_free_free_ Set *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)
@@ -495,11 +557,18 @@ int main(int argc, char *argv[]) {
}
}
+ if (_unlikely_(log_get_max_level() >= LOG_PRI(LOG_DEBUG))) {
+ _cleanup_free_ char *filter;
+
+ filter = journal_make_match_string(j);
+ log_debug("Journal filter: %s", filter);
+ }
+
switch(arg_action) {
case ACTION_LIST:
if (!arg_no_pager)
- pager_open();
+ pager_open(false);
r = dump_list(j);
break;
@@ -517,11 +586,6 @@ int main(int argc, char *argv[]) {
}
end:
- if (j)
- sd_journal_close(j);
-
- set_free_free(matches);
-
pager_close();
if (output)