X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fjournal%2Fjournalctl.c;h=b4874a77be5737b40e2e52bfbfb9606d675dac0e;hb=b0af6f41ea67c97b8beb16fd1d63042379bbf103;hp=5a1cb6e88a7ab8169d985f1aba913634fa67ec96;hpb=0d43c6944bbca30d5692d4b02885f007a0c630c8;p=elogind.git
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
index 5a1cb6e88..b4874a77b 100644
--- a/src/journal/journalctl.c
+++ b/src/journal/journalctl.c
@@ -6,16 +6,16 @@
Copyright 2011 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
- under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
+ Lesser General Public License for more details.
- You should have received a copy of the GNU General Public License
+ You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see .
***/
@@ -29,423 +29,557 @@
#include
#include
#include
+#include
+
+#include
-#include "sd-journal.h"
#include "log.h"
#include "util.h"
+#include "path-util.h"
#include "build.h"
#include "pager.h"
+#include "logs-show.h"
+#include "strv.h"
+#include "journal-internal.h"
+#include "fsprg.h"
+#include "journal-def.h"
-#define PRINT_THRESHOLD 128
-
-static enum {
- OUTPUT_SHORT,
- OUTPUT_VERBOSE,
- OUTPUT_EXPORT,
- OUTPUT_JSON,
- _OUTPUT_MAX
-} arg_output = OUTPUT_SHORT;
+#define DEFAULT_FSPRG_INTERVAL_USEC (15*USEC_PER_MINUTE)
+static OutputMode arg_output = OUTPUT_SHORT;
static bool arg_follow = false;
static bool arg_show_all = false;
static bool arg_no_pager = false;
+static int arg_lines = -1;
+static bool arg_no_tail = false;
+static bool arg_quiet = false;
+static bool arg_local = false;
+static bool arg_this_boot = false;
+static const char *arg_directory = NULL;
+static int arg_priorities = 0xFF;
-static bool contains_unprintable(const void *p, size_t l) {
- const char *j;
+static enum {
+ ACTION_SHOW,
+ ACTION_NEW_ID128,
+ ACTION_PRINT_HEADER,
+ ACTION_SETUP_KEYS
+} arg_action = ACTION_SHOW;
- for (j = p; j < (const char *) p + l; j++)
- if (*j < ' ' || *j >= 127)
- return true;
+static int help(void) {
- return false;
+ printf("%s [OPTIONS...] [MATCH]\n\n"
+ "Send control commands to or query the journal.\n\n"
+ " -h --help Show this help\n"
+ " --version Show package version\n"
+ " --no-pager Do not pipe output into a pager\n"
+ " -a --all Show all fields, including long and unprintable\n"
+ " -f --follow Follow journal\n"
+ " -n --lines=INTEGER Journal entries to show\n"
+ " --no-tail Show all lines, even in follow mode\n"
+ " -o --output=STRING Change journal output mode (short, short-monotonic,\n"
+ " verbose, export, json, cat)\n"
+ " -q --quiet Don't show privilege warning\n"
+ " -l --local Only local entries\n"
+ " -b --this-boot Show data only from current boot\n"
+ " -D --directory=PATH Show journal files from directory\n"
+ " -p --priority=RANGE Show only messages within the specified priority range\n\n"
+ "Commands:\n"
+ " --new-id128 Generate a new 128 Bit ID\n"
+ " --header Show journal header information\n"
+ " --setup-keys Generate new FSPRG key pair\n",
+ program_invocation_short_name);
+
+ return 0;
}
-static int output_short(sd_journal *j, unsigned line) {
- int r;
- uint64_t realtime;
- time_t t;
- struct tm tm;
- char buf[64];
- const void *data;
- size_t length;
- size_t n = 0;
+static int parse_argv(int argc, char *argv[]) {
- assert(j);
+ enum {
+ ARG_VERSION = 0x100,
+ ARG_NO_PAGER,
+ ARG_NO_TAIL,
+ ARG_NEW_ID128,
+ ARG_HEADER,
+ ARG_SETUP_KEYS
+ };
- r = sd_journal_get_realtime_usec(j, &realtime);
- if (r < 0) {
- log_error("Failed to get realtime: %s", strerror(-r));
- return r;
- }
+ static const struct option options[] = {
+ { "help", no_argument, NULL, 'h' },
+ { "version" , no_argument, NULL, ARG_VERSION },
+ { "no-pager", no_argument, NULL, ARG_NO_PAGER },
+ { "follow", no_argument, NULL, 'f' },
+ { "output", required_argument, NULL, 'o' },
+ { "all", no_argument, NULL, 'a' },
+ { "lines", required_argument, NULL, 'n' },
+ { "no-tail", no_argument, NULL, ARG_NO_TAIL },
+ { "new-id128", no_argument, NULL, ARG_NEW_ID128 },
+ { "quiet", no_argument, NULL, 'q' },
+ { "local", no_argument, NULL, 'l' },
+ { "this-boot", no_argument, NULL, 'b' },
+ { "directory", required_argument, NULL, 'D' },
+ { "header", no_argument, NULL, ARG_HEADER },
+ { "priority", no_argument, NULL, 'p' },
+ { "setup-keys",no_argument, NULL, ARG_SETUP_KEYS},
+ { NULL, 0, NULL, 0 }
+ };
- t = (time_t) (realtime / USEC_PER_SEC);
- if (strftime(buf, sizeof(buf), "%b %d %H:%M:%S", localtime_r(&t, &tm)) <= 0) {
- log_error("Failed to format time.");
- return -EINVAL;
- }
+ int c, r;
- fputs(buf, stdout);
- n += strlen(buf);
+ assert(argc >= 0);
+ assert(argv);
- if (sd_journal_get_data(j, "_HOSTNAME", &data, &length) >= 0 &&
- (arg_show_all || (!contains_unprintable(data, length) &&
- length < PRINT_THRESHOLD))) {
- printf(" %.*s", (int) length - 10, ((const char*) data) + 10);
- n += length - 10 + 1;
- }
+ while ((c = getopt_long(argc, argv, "hfo:an:qlbD:p:", options, NULL)) >= 0) {
- if (sd_journal_get_data(j, "MESSAGE", &data, &length) >= 0) {
- if (arg_show_all)
- printf(" %.*s", (int) length - 8, ((const char*) data) + 8);
- else if (contains_unprintable(data, length))
- fputs(" [blob data]", stdout);
- else if (length - 8 + n < columns())
- printf(" %.*s", (int) length - 8, ((const char*) data) + 8);
- else if (n < columns()) {
- char *e;
+ switch (c) {
- e = ellipsize_mem((const char *) data + 8, length - 8, columns() - n - 2, 90);
+ case 'h':
+ help();
+ return 0;
- if (!e)
- printf(" %.*s", (int) length - 8, ((const char*) data) + 8);
- else
- printf(" %s", e);
+ case ARG_VERSION:
+ puts(PACKAGE_STRING);
+ puts(DISTRIBUTION);
+ puts(SYSTEMD_FEATURES);
+ return 0;
- free(e);
- }
- }
+ case ARG_NO_PAGER:
+ arg_no_pager = true;
+ break;
- fputc('\n', stdout);
+ case 'f':
+ arg_follow = true;
+ break;
- return 0;
-}
+ case 'o':
+ arg_output = output_mode_from_string(optarg);
+ if (arg_output < 0) {
+ log_error("Unknown output '%s'.", optarg);
+ return -EINVAL;
+ }
-static int output_verbose(sd_journal *j, unsigned line) {
- const void *data;
- size_t length;
- char *cursor;
- uint64_t realtime;
- char ts[FORMAT_TIMESTAMP_MAX];
- int r;
+ break;
- assert(j);
+ case 'a':
+ arg_show_all = true;
+ break;
- r = sd_journal_get_realtime_usec(j, &realtime);
- if (r < 0) {
- log_error("Failed to get realtime timestamp: %s", strerror(-r));
- return r;
- }
+ case 'n':
+ r = safe_atoi(optarg, &arg_lines);
+ if (r < 0 || arg_lines < 0) {
+ log_error("Failed to parse lines '%s'", optarg);
+ return -EINVAL;
+ }
+ break;
- r = sd_journal_get_cursor(j, &cursor);
- if (r < 0) {
- log_error("Failed to get cursor: %s", strerror(-r));
- return r;
- }
+ case ARG_NO_TAIL:
+ arg_no_tail = true;
+ break;
- printf("%s [%s]\n",
- format_timestamp(ts, sizeof(ts), realtime),
- cursor);
+ case ARG_NEW_ID128:
+ arg_action = ACTION_NEW_ID128;
+ break;
- free(cursor);
+ case 'q':
+ arg_quiet = true;
+ break;
- SD_JOURNAL_FOREACH_DATA(j, data, length) {
- if (!arg_show_all && (length > PRINT_THRESHOLD ||
- contains_unprintable(data, length))) {
- const char *c;
+ case 'l':
+ arg_local = true;
+ break;
- c = memchr(data, '=', length);
- if (!c) {
- log_error("Invalid field.");
- return -EINVAL;
- }
+ case 'b':
+ arg_this_boot = true;
+ break;
- printf("\t%.*s=[blob data]\n",
- (int) (c - (const char*) data),
- (const char*) data);
- } else
- printf("\t%.*s\n", (int) length, (const char*) data);
- }
+ case 'D':
+ arg_directory = optarg;
+ break;
- return 0;
-}
+ case ARG_HEADER:
+ arg_action = ACTION_PRINT_HEADER;
+ break;
-static int output_export(sd_journal *j, unsigned line) {
- sd_id128_t boot_id;
- char sid[33];
- int r;
- usec_t realtime, monotonic;
- char *cursor;
- const void *data;
- size_t length;
+ case ARG_SETUP_KEYS:
+ arg_action = ACTION_SETUP_KEYS;
+ break;
- assert(j);
+ case 'p': {
+ const char *dots;
- r = sd_journal_get_realtime_usec(j, &realtime);
- if (r < 0) {
- log_error("Failed to get realtime timestamp: %s", strerror(-r));
- return r;
- }
+ dots = strstr(optarg, "..");
+ if (dots) {
+ char *a;
+ int from, to, i;
- r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id);
- if (r < 0) {
- log_error("Failed to get monotonic timestamp: %s", strerror(-r));
- return r;
- }
+ /* a range */
+ a = strndup(optarg, dots - optarg);
+ if (!a)
+ return log_oom();
- r = sd_journal_get_cursor(j, &cursor);
- if (r < 0) {
- log_error("Failed to get cursor: %s", strerror(-r));
- return r;
- }
+ from = log_level_from_string(a);
+ to = log_level_from_string(dots + 2);
+ free(a);
- printf(".cursor=%s\n"
- ".realtime=%llu\n"
- ".monotonic=%llu\n"
- ".boot_id=%s\n",
- cursor,
- (unsigned long long) realtime,
- (unsigned long long) monotonic,
- sd_id128_to_string(boot_id, sid));
+ if (from < 0 || to < 0) {
+ log_error("Failed to parse log level range %s", optarg);
+ return -EINVAL;
+ }
- free(cursor);
+ arg_priorities = 0;
- SD_JOURNAL_FOREACH_DATA(j, data, length) {
+ if (from < to) {
+ for (i = from; i <= to; i++)
+ arg_priorities |= 1 << i;
+ } else {
+ for (i = to; i <= from; i++)
+ arg_priorities |= 1 << i;
+ }
- if (contains_unprintable(data, length)) {
- const char *c;
- uint64_t le64;
+ } else {
+ int p, i;
- c = memchr(data, '=', length);
- if (!c) {
- log_error("Invalid field.");
- return -EINVAL;
+ p = log_level_from_string(optarg);
+ if (p < 0) {
+ log_error("Unknown log level %s", optarg);
+ return -EINVAL;
+ }
+
+ arg_priorities = 0;
+
+ for (i = 0; i <= p; i++)
+ arg_priorities |= 1 << i;
}
- fwrite(data, c - (const char*) data, 1, stdout);
- fputc('\n', stdout);
- le64 = htole64(length - (c - (const char*) data) - 1);
- fwrite(&le64, sizeof(le64), 1, stdout);
- fwrite(c + 1, length - (c - (const char*) data) - 1, 1, stdout);
- } else
- fwrite(data, length, 1, stdout);
+ break;
+ }
- fputc('\n', stdout);
+ case '?':
+ return -EINVAL;
+
+ default:
+ log_error("Unknown option code %c", c);
+ return -EINVAL;
+ }
+ }
+
+ if (arg_follow && !arg_no_tail && arg_lines < 0)
+ arg_lines = 10;
+
+ return 1;
+}
+
+static bool on_tty(void) {
+ static int t = -1;
+
+ /* Note that this is invoked relatively early, before we start
+ * the pager. That means the value we return reflects whether
+ * we originally were started on a tty, not if we currently
+ * are. But this is intended, since we want colour and so on
+ * when run in our own pager. */
+
+ if (_unlikely_(t < 0))
+ t = isatty(STDOUT_FILENO) > 0;
+
+ return t;
+}
+
+static int generate_new_id128(void) {
+ sd_id128_t id;
+ int r;
+ unsigned i;
+
+ r = sd_id128_randomize(&id);
+ if (r < 0) {
+ log_error("Failed to generate ID: %s", strerror(-r));
+ return r;
}
- fputc('\n', stdout);
+ printf("As string:\n"
+ SD_ID128_FORMAT_STR "\n\n"
+ "As UUID:\n"
+ "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n\n"
+ "As macro:\n"
+ "#define MESSAGE_XYZ SD_ID128_MAKE(",
+ SD_ID128_FORMAT_VAL(id),
+ SD_ID128_FORMAT_VAL(id));
+
+ for (i = 0; i < 16; i++)
+ printf("%02x%s", id.bytes[i], i != 15 ? "," : "");
+
+ fputs(")\n", stdout);
return 0;
}
-static void json_escape(const char* p, size_t l) {
+static int add_matches(sd_journal *j, char **args) {
+ char **i;
+ int r;
+
+ assert(j);
- if (contains_unprintable(p, l)) {
- bool not_first = false;
+ STRV_FOREACH(i, args) {
- fputs("[ ", stdout);
+ if (streq(*i, "+"))
+ r = sd_journal_add_disjunction(j);
+ else if (path_is_absolute(*i)) {
+ char *p, *t = NULL;
+ const char *path;
+ struct stat st;
- while (l > 0) {
- if (not_first)
- printf(", %u", (uint8_t) *p);
+ p = canonicalize_file_name(*i);
+ path = p ? p : *i;
+
+ if (stat(path, &st) < 0) {
+ free(p);
+ log_error("Couldn't stat file: %m");
+ return -errno;
+ }
+
+ if (S_ISREG(st.st_mode) && (0111 & st.st_mode))
+ t = strappend("_EXE=", path);
+ else if (S_ISCHR(st.st_mode))
+ asprintf(&t, "_KERNEL_DEVICE=c%u:%u", major(st.st_rdev), minor(st.st_rdev));
+ else if (S_ISBLK(st.st_mode))
+ asprintf(&t, "_KERNEL_DEVICE=b%u:%u", major(st.st_rdev), minor(st.st_rdev));
else {
- not_first = true;
- printf("%u", (uint8_t) *p);
+ free(p);
+ log_error("File is not a device node, regular file or is not executable: %s", *i);
+ return -EINVAL;
}
- p++;
- l--;
- }
+ free(p);
- fputs(" ]", stdout);
- } else {
- fputc('\"', stdout);
+ if (!t)
+ return log_oom();
- while (l > 0) {
- if (*p == '"' || *p == '\\') {
- fputc('\\', stdout);
- fputc(*p, stdout);
- } else
- fputc(*p, stdout);
+ r = sd_journal_add_match(j, t, 0);
+ free(t);
+ } else
+ r = sd_journal_add_match(j, *i, 0);
- p++;
- l--;
+ if (r < 0) {
+ log_error("Failed to add match '%s': %s", *i, strerror(-r));
+ return r;
}
-
- fputc('\"', stdout);
}
+
+ return 0;
}
-static int output_json(sd_journal *j, unsigned line) {
- uint64_t realtime, monotonic;
- char *cursor;
- const void *data;
- size_t length;
+static int add_this_boot(sd_journal *j) {
+ char match[9+32+1] = "_BOOT_ID=";
sd_id128_t boot_id;
- char sid[33];
int r;
assert(j);
- r = sd_journal_get_realtime_usec(j, &realtime);
+ if (!arg_this_boot)
+ return 0;
+
+ r = sd_id128_get_boot(&boot_id);
if (r < 0) {
- log_error("Failed to get realtime timestamp: %s", strerror(-r));
+ log_error("Failed to get boot id: %s", strerror(-r));
return r;
}
- r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id);
+ sd_id128_to_string(boot_id, match + 9);
+ r = sd_journal_add_match(j, match, strlen(match));
if (r < 0) {
- log_error("Failed to get monotonic timestamp: %s", strerror(-r));
+ log_error("Failed to add match: %s", strerror(-r));
return r;
}
- r = sd_journal_get_cursor(j, &cursor);
+ return 0;
+}
+
+static int add_priorities(sd_journal *j) {
+ char match[] = "PRIORITY=0";
+ int i, r;
+
+ assert(j);
+
+ if (arg_priorities == 0xFF)
+ return 0;
+
+ for (i = LOG_EMERG; i <= LOG_DEBUG; i++)
+ if (arg_priorities & (1 << i)) {
+ match[sizeof(match)-2] = '0' + i;
+
+ log_info("adding match %s", match);
+
+ r = sd_journal_add_match(j, match, strlen(match));
+ if (r < 0) {
+ log_error("Failed to add match: %s", strerror(-r));
+ return r;
+ }
+ }
+
+ return 0;
+}
+
+static int setup_keys(void) {
+#ifdef HAVE_GCRYPT
+ size_t mpk_size, seed_size, state_size, i;
+ uint8_t *mpk, *seed, *state;
+ ssize_t l;
+ int fd = -1, r;
+ sd_id128_t machine, boot;
+ char *p = NULL, *k = NULL;
+ struct FSPRGHeader h;
+ uint64_t n, interval;
+
+ r = sd_id128_get_machine(&machine);
if (r < 0) {
- log_error("Failed to get cursor: %s", strerror(-r));
+ log_error("Failed to get machine ID: %s", strerror(-r));
return r;
}
- if (line == 1)
- fputc('\n', stdout);
- else
- fputs(",\n", stdout);
-
- printf("{\n"
- "\t\".cursor\" : \"%s\",\n"
- "\t\".realtime\" : %llu,\n"
- "\t\".monotonic\" : %llu,\n"
- "\t\".boot_id\" : \"%s\"",
- cursor,
- (unsigned long long) realtime,
- (unsigned long long) monotonic,
- sd_id128_to_string(boot_id, sid));
-
- free(cursor);
-
- SD_JOURNAL_FOREACH_DATA(j, data, length) {
- const char *c;
-
- c = memchr(data, '=', length);
- if (!c) {
- log_error("Invalid field.");
- return -EINVAL;
- }
-
- fputs(",\n\t", stdout);
- json_escape(data, c - (const char*) data);
- fputs(" : ", stdout);
- json_escape(c + 1, length - (c - (const char*) data) - 1);
+ r = sd_id128_get_boot(&boot);
+ if (r < 0) {
+ log_error("Failed to get boot ID: %s", strerror(-r));
+ return r;
}
- fputs("\n}", stdout);
- fflush(stdout);
+ if (asprintf(&p, "/var/log/journal/" SD_ID128_FORMAT_STR "/fsprg",
+ SD_ID128_FORMAT_VAL(machine)) < 0)
+ return log_oom();
- return 0;
-}
+ if (access(p, F_OK) >= 0) {
+ log_error("Evolving key file %s exists already.", p);
+ r = -EEXIST;
+ goto finish;
+ }
-static int (*output_funcs[_OUTPUT_MAX])(sd_journal*j, unsigned line) = {
- [OUTPUT_SHORT] = output_short,
- [OUTPUT_VERBOSE] = output_verbose,
- [OUTPUT_EXPORT] = output_export,
- [OUTPUT_JSON] = output_json
-};
+ if (asprintf(&k, "/var/log/journal/" SD_ID128_FORMAT_STR "/fsprg.tmp.XXXXXX",
+ SD_ID128_FORMAT_VAL(machine)) < 0) {
+ r = log_oom();
+ goto finish;
+ }
-static int help(void) {
+ mpk_size = FSPRG_mskinbytes(FSPRG_RECOMMENDED_SECPAR);
+ mpk = alloca(mpk_size);
- printf("%s [OPTIONS...] {COMMAND} ...\n\n"
- "Send control commands to or query the login manager.\n\n"
- " -h --help Show this help\n"
- " --version Show package version\n"
- " --no-pager Do not pipe output into a pager\n"
- " -a --all Show all properties, including long and unprintable\n"
- " -f --follow Follow journal\n"
- " -o --output=STRING Change output mode (short, verbose, export, json)\n",
- program_invocation_short_name);
+ seed_size = FSPRG_RECOMMENDED_SEEDLEN;
+ seed = alloca(seed_size);
- return 0;
-}
+ state_size = FSPRG_stateinbytes(FSPRG_RECOMMENDED_SECPAR);
+ state = alloca(state_size);
-static int parse_argv(int argc, char *argv[]) {
+ fd = open("/dev/random", O_RDONLY|O_CLOEXEC|O_NOCTTY);
+ if (fd < 0) {
+ log_error("Failed to open /dev/random: %m");
+ r = -errno;
+ goto finish;
+ }
- enum {
- ARG_VERSION = 0x100,
- ARG_NO_PAGER
- };
+ log_info("Generating seed...");
+ l = loop_read(fd, seed, seed_size, true);
+ if (l < 0 || (size_t) l != seed_size) {
+ log_error("Failed to read random seed: %s", strerror(EIO));
+ r = -EIO;
+ goto finish;
+ }
- static const struct option options[] = {
- { "help", no_argument, NULL, 'h' },
- { "version" , no_argument, NULL, ARG_VERSION },
- { "no-pager", no_argument, NULL, ARG_NO_PAGER },
- { "follow", no_argument, NULL, 'f' },
- { "output", required_argument, NULL, 'o' },
- { "all", no_argument, NULL, 'a' },
- { NULL, 0, NULL, 0 }
- };
+ log_info("Generating key pair...");
+ FSPRG_GenMK(NULL, mpk, seed, seed_size, FSPRG_RECOMMENDED_SECPAR);
- int c;
+ log_info("Generating evolving key...");
+ FSPRG_GenState0(state, mpk, seed, seed_size);
- assert(argc >= 0);
- assert(argv);
+ interval = DEFAULT_FSPRG_INTERVAL_USEC;
+ n = now(CLOCK_REALTIME);
+ n /= interval;
- while ((c = getopt_long(argc, argv, "hfo:a", options, NULL)) >= 0) {
+ close_nointr_nofail(fd);
+ fd = mkostemp(k, O_WRONLY|O_CLOEXEC|O_NOCTTY);
+ if (fd < 0) {
+ log_error("Failed to open %s: %m", k);
+ r = -errno;
+ goto finish;
+ }
- switch (c) {
+ zero(h);
+ memcpy(h.signature, "KSHHRHLP", 8);
+ h.machine_id = machine;
+ h.boot_id = boot;
+ h.header_size = htole64(sizeof(h));
+ h.fsprg_start_usec = htole64(n * interval);
+ h.fsprg_interval_usec = htole64(interval);
+ h.secpar = htole16(FSPRG_RECOMMENDED_SECPAR);
+ h.state_size = htole64(state_size);
+
+ l = loop_write(fd, &h, sizeof(h), false);
+ if (l < 0 || (size_t) l != sizeof(h)) {
+ log_error("Failed to write header: %s", strerror(EIO));
+ r = -EIO;
+ goto finish;
+ }
- case 'h':
- help();
- return 0;
+ l = loop_write(fd, state, state_size, false);
+ if (l < 0 || (size_t) l != state_size) {
+ log_error("Failed to write state: %s", strerror(EIO));
+ r = -EIO;
+ goto finish;
+ }
- case ARG_VERSION:
- puts(PACKAGE_STRING);
- puts(DISTRIBUTION);
- puts(SYSTEMD_FEATURES);
- return 0;
+ if (link(k, p) < 0) {
+ log_error("Failed to link file: %m");
+ r = -errno;
+ goto finish;
+ }
- case ARG_NO_PAGER:
- arg_no_pager = true;
- break;
+ if (isatty(STDOUT_FILENO)) {
+ fprintf(stderr,
+ "\n"
+ "The new key pair has been generated. The evolving key has been written to the\n"
+ "following file. It will be used to protect local journal files. This file does\n"
+ "not need to be kept secret. It should not be used on multiple hosts.\n"
+ "\n"
+ "\t%s\n"
+ "\n"
+ "Please write down the following " ANSI_HIGHLIGHT_ON "secret" ANSI_HIGHLIGHT_OFF " seed value. It should not be stored\n"
+ "locally on disk, and may be used to verify journal files from this host.\n"
+ "\n\t" ANSI_HIGHLIGHT_RED_ON, p);
+ fflush(stderr);
+ }
+ for (i = 0; i < seed_size; i++) {
+ if (i > 0 && i % 3 == 0)
+ putchar('-');
+ printf("%02x", ((uint8_t*) seed)[i]);
+ }
- case 'f':
- arg_follow = true;
- break;
+ printf("/%llx-%llx\n", (unsigned long long) n, (unsigned long long) interval);
- case 'o':
- if (streq(optarg, "short"))
- arg_output = OUTPUT_SHORT;
- else if (streq(optarg, "verbose"))
- arg_output = OUTPUT_VERBOSE;
- else if (streq(optarg, "export"))
- arg_output = OUTPUT_EXPORT;
- else if (streq(optarg, "json"))
- arg_output = OUTPUT_JSON;
- else {
- log_error("Unknown output '%s'.", optarg);
- return -EINVAL;
- }
- break;
+ if (isatty(STDOUT_FILENO))
+ fputs(ANSI_HIGHLIGHT_OFF "\n", stderr);
- case 'a':
- arg_show_all = true;
- break;
+ r = 0;
- case '?':
- return -EINVAL;
+finish:
+ if (fd >= 0)
+ close_nointr_nofail(fd);
- default:
- log_error("Unknown option code %c", c);
- return -EINVAL;
- }
+ if (k) {
+ unlink(k);
+ free(k);
}
- return 1;
+ free(p);
+
+ return r;
+#else
+ log_error("Forward-secure journal verification not available.");
+#endif
}
int main(int argc, char *argv[]) {
- int r, i, fd;
+ int r;
sd_journal *j = NULL;
unsigned line = 0;
-
- log_set_max_level(LOG_DEBUG);
- log_set_target(LOG_TARGET_CONSOLE);
+ bool need_seek = false;
+ sd_id128_t previous_boot_id;
+ bool previous_boot_id_valid = false;
+ bool have_pager;
log_parse_environment();
log_open();
@@ -454,36 +588,94 @@ int main(int argc, char *argv[]) {
if (r <= 0)
goto finish;
- r = sd_journal_open(&j);
+ if (arg_action == ACTION_NEW_ID128) {
+ r = generate_new_id128();
+ goto finish;
+ }
+
+ if (arg_action == ACTION_SETUP_KEYS) {
+ r = setup_keys();
+ goto finish;
+ }
+
+#ifdef HAVE_ACL
+ if (!arg_quiet && geteuid() != 0 && in_group("adm") <= 0)
+ log_warning("Showing user generated messages only. Users in the group 'adm' can see all messages. Pass -q to turn this message off.");
+#endif
+
+ if (arg_directory)
+ r = sd_journal_open_directory(&j, arg_directory, 0);
+ else
+ r = sd_journal_open(&j, arg_local ? SD_JOURNAL_LOCAL_ONLY : 0);
+
if (r < 0) {
log_error("Failed to open journal: %s", strerror(-r));
goto finish;
}
- for (i = optind; i < argc; i++) {
- r = sd_journal_add_match(j, argv[i], strlen(argv[i]));
+ if (arg_action == ACTION_PRINT_HEADER) {
+ journal_print_header(j);
+ r = 0;
+ goto finish;
+ }
+
+ r = add_this_boot(j);
+ if (r < 0)
+ goto finish;
+
+ r = add_matches(j, argv + optind);
+ if (r < 0)
+ goto finish;
+
+ r = add_priorities(j);
+ if (r < 0)
+ goto finish;
+
+ if (!arg_quiet) {
+ usec_t start, end;
+ char start_buf[FORMAT_TIMESTAMP_MAX], end_buf[FORMAT_TIMESTAMP_MAX];
+
+ r = sd_journal_get_cutoff_realtime_usec(j, &start, &end);
if (r < 0) {
- log_error("Failed to add match: %s", strerror(-r));
+ log_error("Failed to get cutoff: %s", strerror(-r));
goto finish;
}
+
+ if (r > 0) {
+ if (arg_follow)
+ printf("Logs begin at %s.\n", format_timestamp(start_buf, sizeof(start_buf), start));
+ else
+ printf("Logs begin at %s, end at %s.\n",
+ format_timestamp(start_buf, sizeof(start_buf), start),
+ format_timestamp(end_buf, sizeof(end_buf), end));
+ }
}
- fd = sd_journal_get_fd(j);
- if (fd < 0) {
- log_error("Failed to get wakeup fd: %s", strerror(-fd));
- goto finish;
+ if (arg_lines >= 0) {
+ r = sd_journal_seek_tail(j);
+ if (r < 0) {
+ log_error("Failed to seek to tail: %s", strerror(-r));
+ goto finish;
+ }
+
+ r = sd_journal_previous_skip(j, arg_lines);
+ } else {
+ r = sd_journal_seek_head(j);
+ if (r < 0) {
+ log_error("Failed to seek to head: %s", strerror(-r));
+ goto finish;
+ }
+
+ r = sd_journal_next(j);
}
- r = sd_journal_seek_head(j);
if (r < 0) {
- log_error("Failed to seek to head: %s", strerror(-r));
+ log_error("Failed to iterate through journal: %s", strerror(-r));
goto finish;
}
- if (!arg_no_pager && !arg_follow) {
- columns();
- pager_open();
- }
+ on_tty();
+ have_pager = !arg_no_pager && !arg_follow && pager_open();
if (arg_output == OUTPUT_JSON) {
fputc('[', stdout);
@@ -491,45 +683,49 @@ int main(int argc, char *argv[]) {
}
for (;;) {
- struct pollfd pollfd;
-
for (;;) {
- r = sd_journal_next(j);
-
- if (r < 0) {
- log_error("Failed to iterate through journal: %s", strerror(-r));
- goto finish;
+ sd_id128_t boot_id;
+ int flags =
+ arg_show_all * OUTPUT_SHOW_ALL |
+ have_pager * OUTPUT_FULL_WIDTH |
+ on_tty() * OUTPUT_COLOR;
+
+ if (need_seek) {
+ r = sd_journal_next(j);
+ if (r < 0) {
+ log_error("Failed to iterate through journal: %s", strerror(-r));
+ goto finish;
+ }
}
if (r == 0)
break;
+ r = sd_journal_get_monotonic_usec(j, NULL, &boot_id);
+ if (r >= 0) {
+ if (previous_boot_id_valid &&
+ !sd_id128_equal(boot_id, previous_boot_id))
+ printf(ANSI_HIGHLIGHT_ON "----- Reboot -----" ANSI_HIGHLIGHT_OFF "\n");
+
+ previous_boot_id = boot_id;
+ previous_boot_id_valid = true;
+ }
+
line ++;
- r = output_funcs[arg_output](j, line);
+ r = output_journal(j, arg_output, line, 0, flags);
if (r < 0)
goto finish;
+
+ need_seek = true;
}
if (!arg_follow)
break;
- zero(pollfd);
- pollfd.fd = fd;
- pollfd.events = POLLIN;
-
- if (poll(&pollfd, 1, -1) < 0) {
- if (errno == EINTR)
- break;
-
- log_error("poll(): %m");
- r = -errno;
- goto finish;
- }
-
- r = sd_journal_process(j);
+ r = sd_journal_wait(j, (uint64_t) -1);
if (r < 0) {
- log_error("Failed to process: %s", strerror(-r));
+ log_error("Couldn't wait for log event: %s", strerror(-r));
goto finish;
}
}