[systemd-commits] 3 commits - src/journal src/logs-show.c src/util.c src/util.h units/syslog.socket units/systemd-journald.socket

Lennart Poettering lennart at kemper.freedesktop.org
Thu Jan 5 11:24:44 PST 2012


 src/journal/journal-send.c    |    4 -
 src/journal/journald.c        |   38 +++++++---
 src/logs-show.c               |  150 ++++++++++++++++++++++++++++++++++--------
 src/util.c                    |   13 +++
 src/util.h                    |    2 
 units/syslog.socket           |    2 
 units/systemd-journald.socket |    4 -
 7 files changed, 171 insertions(+), 42 deletions(-)

New commits:
commit 33eb8abfa51d5bffbf8819215fafdd1123a4a8c0
Author: Lennart Poettering <lennart at poettering.net>
Date:   Thu Jan 5 20:24:34 2012 +0100

    journal: store used transport in journal

diff --git a/src/journal/journald.c b/src/journal/journald.c
index e0dfb35..22ac7d9 100644
--- a/src/journal/journald.c
+++ b/src/journal/journald.c
@@ -578,7 +578,7 @@ retry:
 static void driver_message(Server *s, sd_id128_t message_id, const char *format, ...) {
         char mid[11 + 32 + 1];
         char buffer[16 + LINE_MAX + 1];
-        struct iovec iovec[N_IOVEC_META_FIELDS + 3];
+        struct iovec iovec[N_IOVEC_META_FIELDS + 4];
         int n = 0;
         va_list ap;
         struct ucred ucred;
@@ -587,6 +587,7 @@ static void driver_message(Server *s, sd_id128_t message_id, const char *format,
         assert(format);
 
         IOVEC_SET_STRING(iovec[n++], "PRIORITY=5");
+        IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=driver");
 
         memcpy(buffer, "MESSAGE=", 8);
         va_start(ap, format);
@@ -947,7 +948,7 @@ static void read_tag(const char **buf, char **tag) {
 
 static void process_syslog_message(Server *s, const char *buf, struct ucred *ucred, struct timeval *tv) {
         char *message = NULL, *syslog_priority = NULL, *syslog_facility = NULL, *syslog_tag = NULL;
-        struct iovec iovec[N_IOVEC_META_FIELDS + 4];
+        struct iovec iovec[N_IOVEC_META_FIELDS + 5];
         unsigned n = 0;
         int priority = LOG_USER | LOG_INFO;
         char *tag = NULL;
@@ -968,6 +969,8 @@ static void process_syslog_message(Server *s, const char *buf, struct ucred *ucr
         if (s->forward_to_console)
                 forward_console(s, tag, buf, ucred);
 
+        IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=syslog");
+
         if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0)
                 IOVEC_SET_STRING(iovec[n++], syslog_priority);
 
@@ -1031,7 +1034,7 @@ static bool valid_user_field(const char *p, size_t l) {
 
 static void process_native_message(Server *s, const void *buffer, size_t buffer_size, struct ucred *ucred, struct timeval *tv) {
         struct iovec *iovec = NULL;
-        unsigned n = 0, m = 0, j;
+        unsigned n = 0, m = 0, j, tn = (unsigned) -1;
         const char *p;
         size_t remaining;
         int priority = LOG_INFO;
@@ -1079,7 +1082,7 @@ static void process_native_message(Server *s, const void *buffer, size_t buffer_
                         struct iovec *c;
                         unsigned u;
 
-                        u = MAX((n+N_IOVEC_META_FIELDS) * 2U, 4U);
+                        u = MAX((n+N_IOVEC_META_FIELDS+1) * 2U, 4U);
                         c = realloc(iovec, u * sizeof(struct iovec));
                         if (!c) {
                                 log_error("Out of memory");
@@ -1188,6 +1191,12 @@ static void process_native_message(Server *s, const void *buffer, size_t buffer_
                 }
         }
 
+        if (n <= 0)
+                goto finish;
+
+        tn = n++;
+        IOVEC_SET_STRING(iovec[tn], "_TRANSPORT=journal");
+
         if (message) {
                 if (s->forward_to_syslog)
                         forward_syslog(s, priority, tag, message, ucred, tv);
@@ -1201,17 +1210,22 @@ static void process_native_message(Server *s, const void *buffer, size_t buffer_
 
         dispatch_message(s, iovec, n, m, ucred, tv, priority);
 
-        for (j = 0; j < n; j++)
+finish:
+        for (j = 0; j < n; j++)  {
+                if (j == tn)
+                        continue;
+
                 if (iovec[j].iov_base < buffer ||
                     (const uint8_t*) iovec[j].iov_base >= (const uint8_t*) buffer + buffer_size)
                         free(iovec[j].iov_base);
+        }
 
         free(tag);
         free(message);
 }
 
 static int stdout_stream_log(StdoutStream *s, const char *p) {
-        struct iovec iovec[N_IOVEC_META_FIELDS + 4];
+        struct iovec iovec[N_IOVEC_META_FIELDS + 5];
         char *message = NULL, *syslog_priority = NULL, *syslog_facility = NULL, *syslog_tag = NULL;
         unsigned n = 0;
         int priority;
@@ -1233,6 +1247,8 @@ static int stdout_stream_log(StdoutStream *s, const char *p) {
         if (s->forward_to_console || s->server->forward_to_console)
                 forward_console(s->server, s->tag, p, &s->ucred);
 
+        IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=stdout");
+
         if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0)
                 IOVEC_SET_STRING(iovec[n++], syslog_priority);
 

commit 259d2e762041d8d50c2a17bfea90b1a96f6b880b
Author: Lennart Poettering <lennart at poettering.net>
Date:   Thu Jan 5 20:24:16 2012 +0100

    journal: move sockets into their own subdir

diff --git a/src/journal/journal-send.c b/src/journal/journal-send.c
index e7e3fa2..176aac2 100644
--- a/src/journal/journal-send.c
+++ b/src/journal/journal-send.c
@@ -195,7 +195,7 @@ _public_ int sd_journal_sendv(const struct iovec *iov, int n) {
 
         zero(sa);
         sa.sun_family = AF_UNIX;
-        strncpy(sa.sun_path,"/run/systemd/journal", sizeof(sa.sun_path));
+        strncpy(sa.sun_path,"/run/systemd/journal/socket", sizeof(sa.sun_path));
 
         zero(mh);
         mh.msg_name = &sa;
@@ -225,7 +225,7 @@ _public_ int sd_journal_stream_fd(const char *tag, int priority, int priority_pr
 
         zero(sa);
         sa.un.sun_family = AF_UNIX;
-        strncpy(sa.un.sun_path, "/run/systemd/stdout", sizeof(sa.un.sun_path));
+        strncpy(sa.un.sun_path, "/run/systemd/journal/stdout", sizeof(sa.un.sun_path));
 
         r = connect(fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
         if (r < 0) {
diff --git a/src/journal/journald.c b/src/journal/journald.c
index d7f0ed5..e0dfb35 100644
--- a/src/journal/journald.c
+++ b/src/journal/journald.c
@@ -680,7 +680,7 @@ static void forward_syslog_iovec(Server *s, const struct iovec *iovec, unsigned
 
         zero(sa);
         sa.un.sun_family = AF_UNIX;
-        strncpy(sa.un.sun_path, "/run/systemd/syslog", sizeof(sa.un.sun_path));
+        strncpy(sa.un.sun_path, "/run/systemd/journal/syslog", sizeof(sa.un.sun_path));
         msghdr.msg_name = &sa;
         msghdr.msg_namelen = offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path);
 
@@ -1929,7 +1929,7 @@ static int open_native_socket(Server*s) {
 
                 zero(sa);
                 sa.un.sun_family = AF_UNIX;
-                strncpy(sa.un.sun_path, "/run/systemd/journal", sizeof(sa.un.sun_path));
+                strncpy(sa.un.sun_path, "/run/systemd/journal/socket", sizeof(sa.un.sun_path));
 
                 unlink(sa.un.sun_path);
 
@@ -1984,7 +1984,7 @@ static int open_stdout_socket(Server *s) {
 
                 zero(sa);
                 sa.un.sun_family = AF_UNIX;
-                strncpy(sa.un.sun_path, "/run/systemd/stdout", sizeof(sa.un.sun_path));
+                strncpy(sa.un.sun_path, "/run/systemd/journal/stdout", sizeof(sa.un.sun_path));
 
                 unlink(sa.un.sun_path);
 
@@ -2106,7 +2106,7 @@ static int server_init(Server *s) {
 
         for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) {
 
-                if (sd_is_socket_unix(fd, SOCK_DGRAM, -1, "/run/systemd/native", 0) > 0) {
+                if (sd_is_socket_unix(fd, SOCK_DGRAM, -1, "/run/systemd/journal/socket", 0) > 0) {
 
                         if (s->native_fd >= 0) {
                                 log_error("Too many native sockets passed.");
@@ -2115,7 +2115,7 @@ static int server_init(Server *s) {
 
                         s->native_fd = fd;
 
-                } else if (sd_is_socket_unix(fd, SOCK_STREAM, 1, "/run/systemd/stdout", 0) > 0) {
+                } else if (sd_is_socket_unix(fd, SOCK_STREAM, 1, "/run/systemd/journal/stdout", 0) > 0) {
 
                         if (s->stdout_fd >= 0) {
                                 log_error("Too many stdout sockets passed.");
diff --git a/units/syslog.socket b/units/syslog.socket
index ce07a66..2d42305 100644
--- a/units/syslog.socket
+++ b/units/syslog.socket
@@ -16,7 +16,7 @@ Before=sockets.target syslog.target
 Wants=syslog.target
 
 [Socket]
-ListenDatagram=/run/systemd/syslog
+ListenDatagram=/run/systemd/journal/syslog
 SocketMode=0666
 PassCredentials=yes
 
diff --git a/units/systemd-journald.socket b/units/systemd-journald.socket
index ccb35b1..1062390 100644
--- a/units/systemd-journald.socket
+++ b/units/systemd-journald.socket
@@ -18,8 +18,8 @@ Before=sockets.target syslog.target
 IgnoreOnIsolate=yes
 
 [Socket]
-ListenStream=/run/systemd/stdout
-ListenDatagram=/run/systemd/native
+ListenStream=/run/systemd/journal/stdout
+ListenDatagram=/run/systemd/journal/socket
 ListenDatagram=/dev/log
 SocketMode=0666
 PassCredentials=yes

commit 55d7bfc19b72eca09d2bb7c9c73a62c886d8b9b4
Author: Lennart Poettering <lennart at poettering.net>
Date:   Thu Jan 5 20:11:47 2012 +0100

    journalctl: rework short output mode to rebuild full syslog message

diff --git a/src/logs-show.c b/src/logs-show.c
index 539dfed..358aea1 100644
--- a/src/logs-show.c
+++ b/src/logs-show.c
@@ -23,6 +23,7 @@
 #include <assert.h>
 #include <errno.h>
 #include <sys/poll.h>
+#include <string.h>
 
 #include "logs-show.h"
 #include "log.h"
@@ -40,6 +41,48 @@ static bool contains_unprintable(const void *p, size_t l) {
         return false;
 }
 
+static int parse_field(const void *data, size_t length, const char *field, char **target, size_t *target_size) {
+        size_t fl, nl;
+        void *buf;
+
+        assert(data);
+        assert(field);
+        assert(target);
+        assert(target_size);
+
+        fl = strlen(field);
+        if (length < fl)
+                return 0;
+
+        if (memcmp(data, field, fl))
+                return 0;
+
+        nl = length - fl;
+        buf = memdup((const char*) data + fl, nl);
+        if (!buf) {
+                log_error("Out of memory");
+                return -ENOMEM;
+        }
+
+        *target = buf;
+        *target_size = nl;
+
+        return 1;
+}
+
+static bool shall_print(bool show_all, char *p, size_t l) {
+        if (show_all)
+                return true;
+
+        if (l > PRINT_THRESHOLD)
+                return false;
+
+        if (contains_unprintable(p, l))
+                return false;
+
+        return true;
+}
+
 static int output_short(sd_journal *j, unsigned line, bool show_all) {
         int r;
         uint64_t realtime;
@@ -49,55 +92,110 @@ static int output_short(sd_journal *j, unsigned line, bool show_all) {
         const void *data;
         size_t length;
         size_t n = 0;
+        char *hostname = NULL, *tag = NULL, *comm = NULL, *pid = NULL, *message = NULL;
+        size_t hostname_len = 0, tag_len = 0, comm_len = 0, pid_len = 0, message_len = 0;
 
         assert(j);
 
+        SD_JOURNAL_FOREACH_DATA(j, data, length) {
+
+                r = parse_field(data, length, "_HOSTNAME=", &hostname, &hostname_len);
+                if (r < 0)
+                        goto finish;
+                else if (r > 0)
+                        continue;
+
+                r = parse_field(data, length, "SYSLOG_TAG=", &tag, &tag_len);
+                if (r < 0)
+                        goto finish;
+                else if (r > 0)
+                        continue;
+
+                r = parse_field(data, length, "_COMM=", &comm, &comm_len);
+                if (r < 0)
+                        goto finish;
+                else if (r > 0)
+                        continue;
+
+                r = parse_field(data, length, "_PID=", &pid, &pid_len);
+                if (r < 0)
+                        goto finish;
+                else if (r > 0)
+                        continue;
+
+                r = parse_field(data, length, "MESSAGE=", &message, &message_len);
+                if (r < 0)
+                        goto finish;
+        }
+
+        if (!message) {
+                r = 0;
+                goto finish;
+        }
+
         r = sd_journal_get_realtime_usec(j, &realtime);
         if (r < 0) {
                 log_error("Failed to get realtime: %s", strerror(-r));
-                return r;
+                goto finish;
         }
 
         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;
+                goto finish;
         }
 
         fputs(buf, stdout);
         n += strlen(buf);
 
-        if (sd_journal_get_data(j, "_HOSTNAME", &data, &length) >= 0 &&
-            (show_all || (!contains_unprintable(data, length) &&
-                          length < PRINT_THRESHOLD))) {
-                printf(" %.*s", (int) length - 10, ((const char*) data) + 10);
-                n += length - 10 + 1;
+        if (hostname && shall_print(show_all, hostname, hostname_len)) {
+                printf(" %.*s", (int) hostname_len, hostname);
+                n += hostname_len + 1;
         }
 
-        if (sd_journal_get_data(j, "MESSAGE", &data, &length) >= 0) {
-                if (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;
+        if (tag && shall_print(show_all, tag, tag_len)) {
+                printf(" %.*s", (int) tag_len, tag);
+                n += tag_len + 1;
+        } else if (comm && shall_print(show_all, comm, comm_len)) {
+                printf(" %.*s", (int) comm_len, comm);
+                n += comm_len + 1;
+        }
 
-                        e = ellipsize_mem((const char *) data + 8, length - 8, columns() - n - 2, 90);
+        if (pid && shall_print(show_all, pid, pid_len)) {
+                printf("[%.*s]", (int) pid_len, pid);
+                n += pid_len + 2;
+        }
 
-                        if (!e)
-                                printf(" %.*s", (int) length - 8, ((const char*) data) + 8);
-                        else
-                                printf(" %s", e);
+        if (show_all)
+                printf(": %.*s\n", (int) message_len, message);
+        else if (contains_unprintable(message, message_len))
+                fputs(": [blob data]\n", stdout);
+        else if (message_len + n < columns())
+                printf(": %.*s\n", (int) message_len, message);
+        else if (n < columns()) {
+                char *e;
 
-                        free(e);
-                }
-        }
+                e = ellipsize_mem(message, message_len, columns() - n - 2, 90);
 
-        fputc('\n', stdout);
+                if (!e)
+                        printf(": %.*s\n", (int) message_len, message);
+                else
+                        printf(": %s", e);
 
-        return 0;
+                free(e);
+        } else
+                fputs("\n", stdout);
+
+        r = 0;
+
+finish:
+        free(hostname);
+        free(tag);
+        free(comm);
+        free(pid);
+        free(message);
+
+        return r;
 }
 
 static int output_verbose(sd_journal *j, unsigned line, bool show_all) {
diff --git a/src/util.c b/src/util.c
index d3d521b..2b735e8 100644
--- a/src/util.c
+++ b/src/util.c
@@ -6050,3 +6050,16 @@ finish:
         return buf;
 
 }
+
+void* memdup(const void *p, size_t l) {
+        void *r;
+
+        assert(p);
+
+        r = malloc(l);
+        if (!r)
+                return NULL;
+
+        memcpy(r, p, l);
+        return r;
+}
diff --git a/src/util.h b/src/util.h
index 4d27c4b..be05cc8 100644
--- a/src/util.h
+++ b/src/util.h
@@ -523,4 +523,6 @@ char *format_bytes(char *buf, size_t l, off_t t);
 
 int fd_wait_for_event(int fd, int event);
 
+void* memdup(const void *p, size_t l);
+
 #endif



More information about the systemd-commits mailing list