[systemd-commits] 5 commits - src/journal src/shared

Lennart Poettering lennart at kemper.freedesktop.org
Thu Oct 18 13:36:58 PDT 2012


 src/journal/browse.html        |  158 +++++++++++++++++++++++++++++------
 src/journal/journal-gatewayd.c |  185 +++++++++++++++++++++++++++++++++++++++++
 src/journal/sd-journal.c       |    4 
 src/shared/logs-show.c         |    2 
 src/shared/logs-show.h         |    6 +
 5 files changed, 328 insertions(+), 27 deletions(-)

New commits:
commit bdc02927f78180816bcc46e25e72ca0436a2268f
Author: Lennart Poettering <lennart at poettering.net>
Date:   Thu Oct 18 22:36:50 2012 +0200

    journal: typo fix

diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c
index 772a4fb..09b0eb8 100644
--- a/src/journal/sd-journal.c
+++ b/src/journal/sd-journal.c
@@ -2259,7 +2259,7 @@ _public_ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_
                 size_t ol;
                 bool found;
 
-                /* Proceed to next data object in list the field's linked list */
+                /* Proceed to next data object in the field's linked list */
                 if (j->unique_offset == 0) {
                         r = journal_file_find_field_object(j->unique_file, j->unique_field, k, &o, NULL);
                         if (r < 0)

commit 1cd8a002ff459791967122c2c8be2ccd44dafcf2
Author: Lennart Poettering <lennart at poettering.net>
Date:   Thu Oct 18 22:33:39 2012 +0200

    journal: implement filtering in browse.html

diff --git a/src/journal/browse.html b/src/journal/browse.html
index 2da0b0a..3594f70 100644
--- a/src/journal/browse.html
+++ b/src/journal/browse.html
@@ -6,7 +6,7 @@
         <style type="text/css">
                 div#divlogs, div#diventry {
                         font-family: monospace;
-                        font-size: 8pt;
+                        font-size: 7pt;
                         background-color: #ffffff;
                         padding: 1em;
                         margin: 2em 0em;
@@ -25,7 +25,7 @@
                         background-color: #ededed;
                         color: #313739;
                         font: message-box;
-                        margin: 5em;
+                        margin: 3em;
                 }
                 td.timestamp {
                         text-align: right;
@@ -82,16 +82,24 @@
                         color: #818789;
                         padding-top: 2em;
                 }
-                .key {
+                span.key {
                         font-weight: bold;
                         color: #313739;
                 }
-                form {
+                div#buttonnav {
                         text-align: center;
                 }
-                input {
+                button {
                         font-size: 18pt;
                         font-weight: bold;
+                        width: 2em;
+                        height: 2em;
+                }
+                div#filternav {
+                        text-align: center;
+                }
+                select {
+                        width: 50em;
                 }
         </style>
 </head>
@@ -110,19 +118,27 @@
         <div id="usage"></div>
         <div id="showing"></div>
 
+        <div id="filternav">
+                <select id="filter" onchange="onFilterChange(this);" onfocus="onFilterFocus(this);">
+                        <option>No filter</option>
+                </select>
+                    
+                <input id="boot" type="checkbox" onchange="onBootChange(this);">Only current boot</input>
+        </div>
+
         <div id="divlogs"><table id="tablelogs"></table></div>
         <a name="entry"></a>
         <div id="diventry"><table id="tableentry"></table></div>
 
-        <form>
-                <input id="head" type="button" value="⇤" onclick="entriesLoadHead();" title="First Page"/>
-                <input id="previous" type="button" value="←" onclick="entriesLoadPrevious();" title="Previous Page"/>
-                <input id="next" type="button" value="→" onclick="entriesLoadNext();" title="Next Page"/>
-                <input id="tail" type="button" value="⇥" onclick="entriesLoadTail();" title="Last Page"/>
+        <div id="buttonnav">
+                <button id="head" onclick="entriesLoadHead();" title="First Page">⇤</button>
+                <button id="previous" type="button" onclick="entriesLoadPrevious();" title="Previous Page"/>←</button>
+                <button id="next" type="button" onclick="entriesLoadNext();" title="Next Page"/>→</button>
+                <button id="tail" type="button" onclick="entriesLoadTail();" title="Last Page"/>⇥</button>
                     
-                <input id="more" type="button" value="+" onclick="entriesMore();" title="More Entries"/>
-                <input id="less" type="button" value="-" onclick="entriesLess();" title="Fewer Entries"/>
-        </form>
+                <button id="more" type="button" onclick="entriesMore();" title="More Entries"/>+</button>
+                <button id="less" type="button" onclick="entriesLess();" title="Fewer Entries"/>-</button>
+        </div>
 
         <div id="keynav">
                 <span class="key">g</span>: First Page     
@@ -139,7 +155,7 @@
 
                 function getNEntries() {
                         var n;
-                        n = window.localStorage["n_entries"];
+                        n = localStorage["n_entries"];
                         if (n == null)
                                 return 50;
                         n = parseInt(n);
@@ -160,7 +176,7 @@
                                 return 10;
                         if (n > 1000)
                                 return 1000;
-                        window.localStorage["n_entries"] = n.toString();
+                        localStorage["n_entries"] = n.toString();
                         showNEntries(n);
                 }
 
@@ -221,12 +237,24 @@
                 function entriesLoad(range) {
 
                         if (range == null)
-                                range = window.localStorage["cursor"] + ":0";
+                                range = localStorage["cursor"] + ":0";
                         if (range == null)
                                 range = "";
 
+                        var url = "/entries";
+
+                        if (localStorage["filter"] != "" && localStorage["filter"] != null) {
+                                url += "?_SYSTEMD_UNIT=" + escape(localStorage["filter"]);
+
+                                if (localStorage["boot"] == "1")
+                                        url += "&boot";
+                        } else {
+                                if (localStorage["boot"] == "1")
+                                        url += "?boot";
+                        }
+
                         var request = new XMLHttpRequest();
-                        request.open("GET", "/entries");
+                        request.open("GET", url);
                         request.onreadystatechange = entriesOnResult;
                         request.setRequestHeader("Accept", "application/json");
                         request.setRequestHeader("Range", "entries=" + range + ":" + getNEntries().toString());
@@ -266,8 +294,7 @@
                         var lc = null;
                         var fc = null;
 
-                        var i;
-                        var l = event.currentTarget.responseText.split('\n');
+                        var i, l = event.currentTarget.responseText.split('\n');
 
                         if (l.length <= 1) {
                                 logs.innerHTML = '<tbody><tr><td colspan="3"><i>No further entries...</i></td></tr></tbody>';
@@ -337,7 +364,7 @@
 
                         if (fc != null) {
                                 first_cursor = fc;
-                                window.localStorage["cursor"] = fc;
+                                localStorage["cursor"] = fc;
                         }
                         if (lc != null)
                                 last_cursor = lc;
@@ -423,14 +450,95 @@
                                 entriesLoadNext();
                 }
 
+                function onResultFilterFocus(event) {
+                        if ((event.currentTarget.readyState != 4) ||
+                                (event.currentTarget.status != 200 && event.currentTarget.status != 0))
+                                return;
+
+                        f = document.getElementById("filter");
+
+                        var l = event.currentTarget.responseText.split('\n');
+                        var buf = '<option>No filter</option>';
+                        var j = -1;
+
+                        for (i in l) {
+
+                                if (l[i] == '')
+                                      continue;
+
+                                var d = JSON.parse(l[i]);
+                                if (d._SYSTEMD_UNIT == undefined)
+                                      continue;
+
+                                buf += '<option value="' + escape(d._SYSTEMD_UNIT) + '">' + escapeHTML(d._SYSTEMD_UNIT) + '</option>';
+
+                                if (d._SYSTEMD_UNIT == localStorage["filter"])
+                                        j = i;
+                        }
+
+                        if (j < 0) {
+                                if (localStorage["filter"] != null && localStorage["filter"] != "") {
+                                          buf += '<option value="' + escape(localStorage["filter"]) + '">' + escapeHTML(localStorage["filter"]) + '</option>';
+                                          j = i + 1;
+                                } else
+                                          j = 0;
+                        }
+
+                        f.innerHTML = buf;
+                        f.selectedIndex = j;
+                }
+
+                function onFilterFocus(w) {
+                        var request = new XMLHttpRequest();
+                        request.open("GET", "/fields/_SYSTEMD_UNIT");
+                        request.onreadystatechange = onResultFilterFocus;
+                        request.setRequestHeader("Accept", "application/json");
+                        request.send(null);
+                }
+
+                function onFilterChange(w) {
+                        if (w.selectedIndex <= 0)
+                                localStorage["filter"] = "";
+                        else
+                                localStorage["filter"] = unescape(w.options[w.selectedIndex].value);
+
+                        entriesLoadHead();
+                }
+
+                function onBootChange(w) {
+                        localStorage["boot"] = w.checked ? "1" : "0";
+                        entriesLoadHead();
+                }
+
+                function initFilter() {
+                        f = document.getElementById("filter");
+
+                        var buf = '<option>No filter</option>';
+
+                        var filter = localStorage["filter"];
+                        if (filter != null && filter != "") {
+                                buf += '<option value="' + escape(filter) + '">' + escapeHTML(filter) + '</option>';
+                                j = 1;
+                        } else
+                                j = 0;
+
+                        f.innerHTML = buf;
+                        f.selectedIndex = j;
+                }
+
+                function installHandlers() {
+                        document.onkeyup = onKeyUp;
+
+                        logs = document.getElementById("divlogs");
+                        logs.addEventListener("mousewheel", onMouseWheel, false);
+                        logs.addEventListener("DOMMouseScroll", onMouseWheel, false);
+                }
+
                 machineLoad();
                 entriesLoad(null);
                 showNEntries(getNEntries());
-                document.onkeyup = onKeyUp;
-
-                logs = document.getElementById("divlogs");
-                logs.addEventListener("mousewheel", onMouseWheel, false);
-                logs.addEventListener("DOMMouseScroll", onMouseWheel, false);
+                initFilter();
+                installHandlers();
         </script>
 </body>
 </html>

commit 082d0180f98199803c5db0dd0e86f2486843c2f1
Author: Lennart Poettering <lennart at poettering.net>
Date:   Thu Oct 18 22:31:58 2012 +0200

    journal: add high-level match option to filter by current boot id

diff --git a/src/journal/journal-gatewayd.c b/src/journal/journal-gatewayd.c
index 1d0559f..6a5db96 100644
--- a/src/journal/journal-gatewayd.c
+++ b/src/journal/journal-gatewayd.c
@@ -416,6 +416,38 @@ static int request_parse_arguments_iterator(
                 return MHD_YES;
         }
 
+        if (streq(key, "boot")) {
+                if (isempty(value))
+                        r = true;
+                else {
+                        r = parse_boolean(value);
+                        if (r < 0) {
+                                m->argument_parse_error = r;
+                                return MHD_NO;
+                        }
+                }
+
+                if (r) {
+                        char match[9 + 32 + 1] = "_BOOT_ID=";
+                        sd_id128_t bid;
+
+                        r = sd_id128_get_boot(&bid);
+                        if (r < 0) {
+                                log_error("Failed to get boot ID: %s", strerror(-r));
+                                return MHD_NO;
+                        }
+
+                        sd_id128_to_string(bid, match + 9);
+                        r = sd_journal_add_match(m->journal, match, sizeof(match)-1);
+                        if (r < 0) {
+                                m->argument_parse_error = r;
+                                return MHD_NO;
+                        }
+                }
+
+                return MHD_YES;
+        }
+
         p = strjoin(key, "=", strempty(value), NULL);
         if (!p) {
                 m->argument_parse_error = log_oom();

commit 240a5fe83e772d08d9e1f22e90163b37eea5ab45
Author: Lennart Poettering <lennart at poettering.net>
Date:   Thu Oct 18 22:31:27 2012 +0200

    journal: add ability to list unique fields to gatewayd

diff --git a/src/journal/journal-gatewayd.c b/src/journal/journal-gatewayd.c
index eea3530..1d0559f 100644
--- a/src/journal/journal-gatewayd.c
+++ b/src/journal/journal-gatewayd.c
@@ -50,6 +50,9 @@ typedef struct RequestMeta {
 
         bool follow;
         bool discrete;
+
+        uint64_t n_fields;
+        bool n_fields_set;
 } RequestMeta;
 
 static const char* const mime_types[_OUTPUT_MODE_MAX] = {
@@ -498,6 +501,153 @@ static int request_handler_entries(
         return r;
 }
 
+static int output_field(FILE *f, OutputMode m, const char *d, size_t l) {
+        const char *eq;
+        size_t j;
+
+        eq = memchr(d, '=', l);
+        if (!eq)
+                return -EINVAL;
+
+        j = l - (eq - d + 1);
+
+        if (m == OUTPUT_JSON) {
+                fprintf(f, "{ \"%.*s\" : ", (int) (eq - d), d);
+                json_escape(f, eq+1, j, OUTPUT_FULL_WIDTH);
+                fputs(" }\n", f);
+        } else {
+                fwrite(eq+1, 1, j, f);
+                fputc('\n', f);
+        }
+
+        return 0;
+}
+
+static ssize_t request_reader_fields(
+                void *cls,
+                uint64_t pos,
+                char *buf,
+                size_t max) {
+
+        RequestMeta *m = cls;
+        int r;
+        size_t n, k;
+
+        assert(m);
+        assert(buf);
+        assert(max > 0);
+        assert(pos >= m->delta);
+
+        pos -= m->delta;
+
+        while (pos >= m->size) {
+                off_t sz;
+                const void *d;
+                size_t l;
+
+                /* End of this field, so let's serialize the next
+                 * one */
+
+                if (m->n_fields_set &&
+                    m->n_fields <= 0)
+                        return MHD_CONTENT_READER_END_OF_STREAM;
+
+                r = sd_journal_enumerate_unique(m->journal, &d, &l);
+                if (r < 0) {
+                        log_error("Failed to advance field index: %s", strerror(-r));
+                        return MHD_CONTENT_READER_END_WITH_ERROR;
+                } else if (r == 0)
+                        return MHD_CONTENT_READER_END_OF_STREAM;
+
+                pos -= m->size;
+                m->delta += m->size;
+
+                if (m->n_fields_set)
+                        m->n_fields -= 1;
+
+                if (m->tmp)
+                        rewind(m->tmp);
+                else {
+                        m->tmp = tmpfile();
+                        if (!m->tmp) {
+                                log_error("Failed to create temporary file: %m");
+                                return MHD_CONTENT_READER_END_WITH_ERROR;;
+                        }
+                }
+
+                r = output_field(m->tmp, m->mode, d, l);
+                if (r < 0) {
+                        log_error("Failed to serialize item: %s", strerror(-r));
+                        return MHD_CONTENT_READER_END_WITH_ERROR;
+                }
+
+                sz = ftello(m->tmp);
+                if (sz == (off_t) -1) {
+                        log_error("Failed to retrieve file position: %m");
+                        return MHD_CONTENT_READER_END_WITH_ERROR;
+                }
+
+                m->size = (uint64_t) sz;
+        }
+
+        if (fseeko(m->tmp, pos, SEEK_SET) < 0) {
+                log_error("Failed to seek to position: %m");
+                return MHD_CONTENT_READER_END_WITH_ERROR;
+        }
+
+        n = m->size - pos;
+        if (n > max)
+                n = max;
+
+        errno = 0;
+        k = fread(buf, 1, n, m->tmp);
+        if (k != n) {
+                log_error("Failed to read from file: %s", errno ? strerror(errno) : "Premature EOF");
+                return MHD_CONTENT_READER_END_WITH_ERROR;
+        }
+
+        return (ssize_t) k;
+}
+
+static int request_handler_fields(
+                struct MHD_Connection *connection,
+                const char *field,
+                void *connection_cls) {
+
+        struct MHD_Response *response;
+        RequestMeta *m;
+        int r;
+
+        assert(connection);
+        assert(connection_cls);
+
+        m = request_meta(connection_cls);
+        if (!m)
+                return respond_oom(connection);
+
+        r = open_journal(m);
+        if (r < 0)
+                return respond_error(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, "Failed to open journal: %s\n", strerror(-r));
+
+        if (request_parse_accept(m, connection) < 0)
+                return respond_error(connection, MHD_HTTP_BAD_REQUEST, "Failed to parse Accept header.\n");
+
+        r = sd_journal_query_unique(m->journal, field);
+        if (r < 0)
+                return respond_error(connection, MHD_HTTP_BAD_REQUEST, "Failed to query unique fields.\n");
+
+        response = MHD_create_response_from_callback(MHD_SIZE_UNKNOWN, 4*1024, request_reader_fields, m, NULL);
+        if (!response)
+                return respond_oom(connection);
+
+        MHD_add_response_header(response, "Content-Type", mime_types[m->mode == OUTPUT_JSON ? OUTPUT_JSON : OUTPUT_SHORT]);
+
+        r = MHD_queue_response(connection, MHD_HTTP_OK, response);
+        MHD_destroy_response(response);
+
+        return r;
+}
+
 static int request_handler_redirect(
                 struct MHD_Connection *connection,
                 const char *target) {
@@ -666,6 +816,9 @@ static int request_handler(
         if (streq(url, "/entries"))
                 return request_handler_entries(connection, connection_cls);
 
+        if (startswith(url, "/fields/"))
+                return request_handler_fields(connection, url + 8, connection_cls);
+
         if (streq(url, "/browse"))
                 return request_handler_file(connection, DOCUMENT_ROOT "/browse.html", "text/html");
 
diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c
index 28d12d3..0589536 100644
--- a/src/shared/logs-show.c
+++ b/src/shared/logs-show.c
@@ -405,7 +405,7 @@ static int output_export(
         return 0;
 }
 
-static void json_escape(
+void json_escape(
                 FILE *f,
                 const char* p,
                 size_t l,
diff --git a/src/shared/logs-show.h b/src/shared/logs-show.h
index 1b67876..0608280 100644
--- a/src/shared/logs-show.h
+++ b/src/shared/logs-show.h
@@ -64,5 +64,11 @@ int show_journal_by_unit(
                 unsigned how_many,
                 OutputFlags flags);
 
+void json_escape(
+                FILE *f,
+                const char* p,
+                size_t l,
+                OutputFlags flags);
+
 const char* output_mode_to_string(OutputMode m);
 OutputMode output_mode_from_string(const char *s);

commit d5c4ed623b3b83b8caaf6b26f5b4c8006ff372ac
Author: Lennart Poettering <lennart at poettering.net>
Date:   Thu Oct 18 22:30:27 2012 +0200

    journal: validate field name in sd_journal_query_unique()

diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c
index a874ca5..772a4fb 100644
--- a/src/journal/sd-journal.c
+++ b/src/journal/sd-journal.c
@@ -2214,6 +2214,8 @@ _public_ int sd_journal_query_unique(sd_journal *j, const char *field) {
                 return -EINVAL;
         if (isempty(field))
                 return -EINVAL;
+        if (!field_is_valid(field))
+                return -EINVAL;
 
         f = strdup(field);
         if (!f)



More information about the systemd-commits mailing list