X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;ds=sidebyside;f=src%2Fjournal%2Fjournalctl.c;h=90845097049b183098417325c74239118b6e1697;hb=37d3ab1b7e114f0fb6dfb2e7273569b42794b76a;hp=4d46beb3fe3cf0f71e6add9c283de41c95ce88a5;hpb=e3657ecd7f9f9bd6a1719fc808639cd4b780674a;p=elogind.git diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index 4d46beb3f..908450970 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -60,7 +60,7 @@ static bool arg_follow = false; static bool arg_full = false; static bool arg_all = false; static bool arg_no_pager = false; -static unsigned arg_lines = 0; +static int arg_lines = -1; static bool arg_no_tail = false; static bool arg_quiet = false; static bool arg_merge = false; @@ -202,7 +202,6 @@ static int parse_argv(int argc, char *argv[]) { case ARG_VERSION: puts(PACKAGE_STRING); - puts(DISTRIBUTION); puts(SYSTEMD_FEATURES); return 0; @@ -240,13 +239,30 @@ static int parse_argv(int argc, char *argv[]) { case 'n': if (optarg) { - r = safe_atou(optarg, &arg_lines); - if (r < 0 || arg_lines <= 0) { + r = safe_atoi(optarg, &arg_lines); + if (r < 0 || arg_lines < 0) { log_error("Failed to parse lines '%s'", optarg); return -EINVAL; } - } else - arg_lines = 10; + } else { + int n; + + /* Hmm, no argument? Maybe the next + * word on the command line is + * supposed to be the argument? Let's + * see if there is one, and is + * parsable as a positive + * integer... */ + + if (optind < argc && + safe_atoi(argv[optind], &n) >= 0 && + n >= 0) { + + arg_lines = n; + optind++; + } else + arg_lines = 10; + } break; @@ -414,10 +430,10 @@ static int parse_argv(int argc, char *argv[]) { } } - if (arg_follow && !arg_no_tail && arg_lines <= 0) + if (arg_follow && !arg_no_tail && arg_lines < 0) arg_lines = 10; - if (arg_since_set && arg_until_set && arg_since_set > arg_until_set) { + if (arg_since_set && arg_until_set && arg_since > arg_until) { log_error("--since= must be before --until=."); return -EINVAL; } @@ -446,14 +462,17 @@ static int generate_new_id128(void) { "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(", + "#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\n", stdout); - fputs(")\n", stdout); + printf("As Python constant:\n" + ">>> import uuid\n" + ">>> MESSAGE_XYZ = uuid.UUID('" SD_ID128_FORMAT_STR "')\n", + SD_ID128_FORMAT_VAL(id)); return 0; } @@ -850,8 +869,8 @@ int main(int argc, char *argv[]) { sd_journal *j = NULL; bool need_seek = false; sd_id128_t previous_boot_id; - bool previous_boot_id_valid = false; - unsigned n_shown = 0; + bool previous_boot_id_valid = false, first_line = true; + int n_shown = 0; setlocale(LC_ALL, ""); log_parse_environment(); @@ -938,6 +957,11 @@ int main(int argc, char *argv[]) { if (r < 0) goto finish; + /* Opening the fd now means the first sd_journal_wait() will actually wait */ + r = sd_journal_get_fd(j); + if (r < 0) + goto finish; + if (arg_field) { const void *data; size_t size; @@ -951,7 +975,7 @@ int main(int argc, char *argv[]) { SD_JOURNAL_FOREACH_UNIQUE(j, data, size) { const void *eq; - if (arg_lines > 0 && n_shown >= arg_lines) + if (arg_lines >= 0 && n_shown >= arg_lines) break; eq = memchr(data, '=', size); @@ -984,7 +1008,7 @@ int main(int argc, char *argv[]) { } r = sd_journal_next(j); - } else if (arg_lines > 0) { + } else if (arg_lines >= 0) { r = sd_journal_seek_tail(j); if (r < 0) { log_error("Failed to seek to tail: %s", strerror(-r)); @@ -1033,7 +1057,7 @@ int main(int argc, char *argv[]) { } for (;;) { - while (arg_lines == 0 || arg_follow || n_shown < arg_lines) { + while (arg_lines < 0 || n_shown < arg_lines || (arg_follow && !first_line)) { int flags; if (need_seek) { @@ -1055,6 +1079,8 @@ int main(int argc, char *argv[]) { log_error("Failed to determine timestamp: %s", strerror(-r)); goto finish; } + if (usec > arg_until) + goto finish; } if (!arg_merge) { @@ -1078,7 +1104,7 @@ int main(int argc, char *argv[]) { arg_catalog * OUTPUT_CATALOG; r = output_journal(stdout, j, arg_output, 0, flags); - if (r < 0) + if (r < 0 || ferror(stdout)) goto finish; need_seek = true; @@ -1093,6 +1119,8 @@ int main(int argc, char *argv[]) { log_error("Couldn't wait for journal event: %s", strerror(-r)); goto finish; } + + first_line = false; } finish: