[systemd-commits] Branch 'journal' - 2 commits - Makefile.am src/journal

Lennart Poettering lennart at kemper.freedesktop.org
Wed Dec 21 10:00:23 PST 2011


 Makefile.am              |    3 
 src/journal/compress.c   |  208 +++++++++++++++++++++++++++++++++++++++++++++++
 src/journal/compress.h   |   38 ++++++++
 src/journal/journalctl.c |  125 +++++++++++++++++++++++++++-
 4 files changed, 369 insertions(+), 5 deletions(-)

New commits:
commit e4e61fdbed832a2bd3f5dcd47623872d9081599c
Author: Lennart Poettering <lennart at poettering.net>
Date:   Wed Dec 21 19:00:10 2011 +0100

    journal: add missing compress.[ch]

diff --git a/src/journal/compress.c b/src/journal/compress.c
new file mode 100644
index 0000000..ff90658
--- /dev/null
+++ b/src/journal/compress.c
@@ -0,0 +1,208 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  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
+  (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.
+
+  You should have received a copy of the GNU General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+#include <lzma.h>
+
+#include "compress.h"
+
+bool compress_blob(const void *src, uint64_t src_size, void *dst, uint64_t *dst_size) {
+        lzma_stream s = LZMA_STREAM_INIT;
+        lzma_ret ret;
+        bool b = false;
+
+        assert(src);
+        assert(src_size > 0);
+        assert(dst);
+        assert(dst_size);
+
+        /* Returns false if we couldn't compress the data or the
+         * compressed result is longer than the original */
+
+        ret = lzma_easy_encoder(&s, LZMA_PRESET_DEFAULT, LZMA_CHECK_NONE);
+        if (ret != LZMA_OK)
+                return false;
+
+        s.next_in = src;
+        s.avail_in = src_size;
+        s.next_out = dst;
+        s.avail_out = src_size;
+
+        /* Does it fit? */
+        if (lzma_code(&s, LZMA_FINISH) != LZMA_STREAM_END)
+                goto fail;
+
+        /* Is it actually shorter? */
+        if (s.avail_out == 0)
+                goto fail;
+
+        *dst_size = src_size - s.avail_out;
+        b = true;
+
+fail:
+        lzma_end(&s);
+
+        return b;
+}
+
+bool uncompress_blob(const void *src, uint64_t src_size,
+                     void **dst, uint64_t *dst_alloc_size, uint64_t* dst_size) {
+
+        lzma_stream s = LZMA_STREAM_INIT;
+        lzma_ret ret;
+        bool b = false;
+
+        assert(src);
+        assert(src_size > 0);
+        assert(dst);
+        assert(dst_alloc_size);
+        assert(dst_size);
+        assert(*dst_alloc_size == 0 || *dst);
+
+        ret = lzma_stream_decoder(&s, UINT64_MAX, 0);
+        if (ret != LZMA_OK)
+                return false;
+
+        if (*dst_alloc_size <= src_size) {
+                void *p;
+
+                p = realloc(*dst, src_size*2);
+                if (!p)
+                        return false;
+
+                *dst = p;
+                *dst_alloc_size = src_size*2;
+        }
+
+        s.next_in = src;
+        s.avail_in = src_size;
+
+        s.next_out = *dst;
+        s.avail_out = *dst_alloc_size;
+
+        for (;;) {
+                void *p;
+
+                ret = lzma_code(&s, LZMA_FINISH);
+
+                if (ret == LZMA_STREAM_END)
+                        break;
+
+                if (ret != LZMA_OK)
+                        goto fail;
+
+                p = realloc(*dst, *dst_alloc_size*2);
+                if (!p)
+                        goto fail;
+
+                s.next_out = (uint8_t*) p + ((uint8_t*) s.next_out - (uint8_t*) *dst);
+                s.avail_out += *dst_alloc_size;
+
+                *dst = p;
+                *dst_alloc_size *= 2;
+        }
+
+        *dst_size = *dst_alloc_size - s.avail_out;
+        b = true;
+
+fail:
+        lzma_end(&s);
+
+        return b;
+}
+
+bool uncompress_startswith(const void *src, uint64_t src_size,
+                           void **buffer, uint64_t *buffer_size,
+                           const void *prefix, uint64_t prefix_len,
+                           uint8_t extra) {
+
+        lzma_stream s = LZMA_STREAM_INIT;
+        lzma_ret ret;
+        bool b = false;
+
+        /* Checks whether the uncompressed blob starts with the
+         * mentioned prefix. The byte extra needs to follow the
+         * prefix */
+
+        assert(src);
+        assert(src_size > 0);
+        assert(buffer);
+        assert(buffer_size);
+        assert(prefix);
+        assert(*buffer_size == 0 || *buffer);
+
+        ret = lzma_stream_decoder(&s, UINT64_MAX, 0);
+        if (ret != LZMA_OK)
+                return false;
+
+        if (*buffer_size <= prefix_len) {
+                void *p;
+
+                p = realloc(*buffer, prefix_len*2);
+                if (!p)
+                        return false;
+
+                *buffer = p;
+                *buffer_size = prefix_len*2;
+        }
+
+        s.next_in = src;
+        s.avail_in = src_size;
+
+        s.next_out = *buffer;
+        s.avail_out = *buffer_size;
+
+        for (;;) {
+                void *p;
+
+                ret = lzma_code(&s, LZMA_FINISH);
+
+                if (ret != LZMA_STREAM_END && ret != LZMA_OK)
+                        goto fail;
+
+                if ((*buffer_size - s.avail_out > prefix_len) &&
+                    memcmp(*buffer, prefix, prefix_len) == 0 &&
+                    ((const uint8_t*) *buffer)[prefix_len] == extra)
+                        break;
+
+                if (ret == LZMA_STREAM_END)
+                        goto fail;
+
+                p = realloc(*buffer, *buffer_size*2);
+                if (!p)
+                        goto fail;
+
+                s.next_out = (uint8_t*) p + ((uint8_t*) s.next_out - (uint8_t*) *buffer);
+                s.avail_out += *buffer_size;
+
+                *buffer = p;
+                *buffer_size *= 2;
+        }
+
+        b = true;
+
+fail:
+        lzma_end(&s);
+
+        return b;
+}
diff --git a/src/journal/compress.h b/src/journal/compress.h
new file mode 100644
index 0000000..f187a6e
--- /dev/null
+++ b/src/journal/compress.h
@@ -0,0 +1,38 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#ifndef foocompresshfoo
+#define foocompresshfoo
+
+/***
+  This file is part of systemd.
+
+  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
+  (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.
+
+  You should have received a copy of the GNU General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <inttypes.h>
+#include <stdbool.h>
+
+bool compress_blob(const void *src, uint64_t src_size, void *dst, uint64_t *dst_size);
+
+bool uncompress_blob(const void *src, uint64_t src_size,
+                     void **dst, uint64_t *dst_alloc_size, uint64_t* dst_size);
+
+bool uncompress_startswith(const void *src, uint64_t src_size,
+                           void **buffer, uint64_t *buffer_size,
+                           const void *prefix, uint64_t prefix_len,
+                           uint8_t extra);
+
+#endif

commit 0d43c6944bbca30d5692d4b02885f007a0c630c8
Author: Lennart Poettering <lennart at poettering.net>
Date:   Wed Dec 21 18:59:56 2011 +0100

    journalctl: add command line parsing

diff --git a/Makefile.am b/Makefile.am
index 7d551a9..81b5c50 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1069,7 +1069,8 @@ systemd_journalctl_SOURCES = \
 	src/journal/sd-journal.c \
         src/journal/journal-file.c \
         src/journal/lookup3.c \
-        src/sd-id128.c
+        src/sd-id128.c \
+        src/pager.c
 
 systemd_journalctl_CFLAGS = \
 	$(AM_CFLAGS)
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
index 6f4342e..5a1cb6e 100644
--- a/src/journal/journalctl.c
+++ b/src/journal/journalctl.c
@@ -28,10 +28,13 @@
 #include <stdlib.h>
 #include <sys/poll.h>
 #include <time.h>
+#include <getopt.h>
 
 #include "sd-journal.h"
 #include "log.h"
 #include "util.h"
+#include "build.h"
+#include "pager.h"
 
 #define PRINT_THRESHOLD 128
 
@@ -41,10 +44,11 @@ static enum {
         OUTPUT_EXPORT,
         OUTPUT_JSON,
         _OUTPUT_MAX
-} arg_output = OUTPUT_JSON;
+} arg_output = OUTPUT_SHORT;
 
 static bool arg_follow = false;
 static bool arg_show_all = false;
+static bool arg_no_pager = false;
 
 static bool contains_unprintable(const void *p, size_t l) {
         const char *j;
@@ -345,6 +349,96 @@ static int (*output_funcs[_OUTPUT_MAX])(sd_journal*j, unsigned line) = {
         [OUTPUT_JSON] = output_json
 };
 
+static int help(void) {
+
+        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);
+
+        return 0;
+}
+
+static int parse_argv(int argc, char *argv[]) {
+
+        enum {
+                ARG_VERSION = 0x100,
+                ARG_NO_PAGER
+        };
+
+        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             }
+        };
+
+        int c;
+
+        assert(argc >= 0);
+        assert(argv);
+
+        while ((c = getopt_long(argc, argv, "hfo:a", options, NULL)) >= 0) {
+
+                switch (c) {
+
+                case 'h':
+                        help();
+                        return 0;
+
+                case ARG_VERSION:
+                        puts(PACKAGE_STRING);
+                        puts(DISTRIBUTION);
+                        puts(SYSTEMD_FEATURES);
+                        return 0;
+
+                case ARG_NO_PAGER:
+                        arg_no_pager = true;
+                        break;
+
+                case 'f':
+                        arg_follow = true;
+                        break;
+
+                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;
+
+                case 'a':
+                        arg_show_all = true;
+                        break;
+
+                case '?':
+                        return -EINVAL;
+
+                default:
+                        log_error("Unknown option code %c", c);
+                        return -EINVAL;
+                }
+        }
+
+        return 1;
+}
+
 int main(int argc, char *argv[]) {
         int r, i, fd;
         sd_journal *j = NULL;
@@ -356,13 +450,17 @@ int main(int argc, char *argv[]) {
         log_parse_environment();
         log_open();
 
+        r = parse_argv(argc, argv);
+        if (r <= 0)
+                goto finish;
+
         r = sd_journal_open(&j);
         if (r < 0) {
                 log_error("Failed to open journal: %s", strerror(-r));
                 goto finish;
         }
 
-        for (i = 1; i < argc; i++) {
+        for (i = optind; i < argc; i++) {
                 r = sd_journal_add_match(j, argv[i], strlen(argv[i]));
                 if (r < 0) {
                         log_error("Failed to add match: %s", strerror(-r));
@@ -382,13 +480,30 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
-        if (arg_output == OUTPUT_JSON)
+        if (!arg_no_pager && !arg_follow) {
+                columns();
+                pager_open();
+        }
+
+        if (arg_output == OUTPUT_JSON) {
                 fputc('[', stdout);
+                fflush(stdout);
+        }
 
         for (;;) {
                 struct pollfd pollfd;
 
-                while (sd_journal_next(j) > 0) {
+                for (;;) {
+                        r = sd_journal_next(j);
+
+                        if (r < 0) {
+                                log_error("Failed to iterate through journal: %s", strerror(-r));
+                                goto finish;
+                        }
+
+                        if (r == 0)
+                                break;
+
                         line ++;
 
                         r = output_funcs[arg_output](j, line);
@@ -426,5 +541,7 @@ finish:
         if (j)
                 sd_journal_close(j);
 
+        pager_close();
+
         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
 }



More information about the systemd-commits mailing list