chiark / gitweb /
journald: malloc less when streaming messages
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 10 Dec 2013 11:17:01 +0000 (06:17 -0500)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 11 Dec 2013 01:38:33 +0000 (20:38 -0500)
src/journal/journald-stream.c
src/journal/journald-syslog.c
src/journal/journald-syslog.h

index 42a846b8eae615c65c09a5b95dd0141e77bb99d7..5c15a56bacf61e038ed9c9d8320f548e7c772cc7 100644 (file)
@@ -76,9 +76,11 @@ struct StdoutStream {
 
 static int stdout_stream_log(StdoutStream *s, const char *p) {
         struct iovec iovec[N_IOVEC_META_FIELDS + 5];
 
 static int stdout_stream_log(StdoutStream *s, const char *p) {
         struct iovec iovec[N_IOVEC_META_FIELDS + 5];
-        _cleanup_free_ char *message = NULL, *syslog_priority = NULL, *syslog_facility = NULL, *syslog_identifier = NULL;
-        unsigned n = 0;
         int priority;
         int priority;
+        char syslog_priority[] = "PRIORITY=\0";
+        char syslog_facility[sizeof("SYSLOG_FACILITY=") + DECIMAL_STR_MAX(priority)];
+        _cleanup_free_ char *message = NULL, *syslog_identifier = NULL;
+        unsigned n = 0;
         char *label = NULL;
         size_t label_len = 0;
 
         char *label = NULL;
         size_t label_len = 0;
 
@@ -91,7 +93,7 @@ static int stdout_stream_log(StdoutStream *s, const char *p) {
         priority = s->priority;
 
         if (s->level_prefix)
         priority = s->priority;
 
         if (s->level_prefix)
-                syslog_parse_priority((char**) &p, &priority, false);
+                syslog_parse_priority(&p, &priority, false);
 
         if (s->forward_to_syslog || s->server->forward_to_syslog)
                 server_forward_syslog(s->server, syslog_fixup_facility(priority), s->identifier, p, &s->ucred, NULL);
 
         if (s->forward_to_syslog || s->server->forward_to_syslog)
                 server_forward_syslog(s->server, syslog_fixup_facility(priority), s->identifier, p, &s->ucred, NULL);
@@ -104,12 +106,13 @@ static int stdout_stream_log(StdoutStream *s, const char *p) {
 
         IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=stdout");
 
 
         IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=stdout");
 
-        if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0)
-                IOVEC_SET_STRING(iovec[n++], syslog_priority);
+        syslog_priority[strlen("PRIORITY=")] = '0' + LOG_PRI(priority);
+        IOVEC_SET_STRING(iovec[n++], syslog_priority);
 
 
-        if (priority & LOG_FACMASK)
-                if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0)
-                        IOVEC_SET_STRING(iovec[n++], syslog_facility);
+        if (priority & LOG_FACMASK) {
+                snprintf(syslog_facility, sizeof(syslog_facility), "SYSLOG_FACILITY=%i", LOG_FAC(priority));
+                IOVEC_SET_STRING(iovec[n++], syslog_facility);
+        }
 
         if (s->identifier) {
                 syslog_identifier = strappend("SYSLOG_IDENTIFIER=", s->identifier);
 
         if (s->identifier) {
                 syslog_identifier = strappend("SYSLOG_IDENTIFIER=", s->identifier);
@@ -411,7 +414,7 @@ fail:
 
 int server_open_stdout_socket(Server *s) {
         int r;
 
 int server_open_stdout_socket(Server *s) {
         int r;
-        struct epoll_event ev;
+        struct epoll_event ev = { .events = EPOLLIN };
 
         assert(s);
 
 
         assert(s);
 
@@ -444,8 +447,6 @@ int server_open_stdout_socket(Server *s) {
         } else
                 fd_nonblock(s->stdout_fd, 1);
 
         } else
                 fd_nonblock(s->stdout_fd, 1);
 
-        zero(ev);
-        ev.events = EPOLLIN;
         ev.data.fd = s->stdout_fd;
         if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->stdout_fd, &ev) < 0) {
                 log_error("Failed to add stdout server fd to epoll object: %m");
         ev.data.fd = s->stdout_fd;
         if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->stdout_fd, &ev) < 0) {
                 log_error("Failed to add stdout server fd to epoll object: %m");
index dc66ba8c8fa77ed580d21785582008ab87d9b548..2caa1738d9759e5a1b7a7e1f33657252907a9e34 100644 (file)
@@ -237,7 +237,7 @@ size_t syslog_parse_identifier(const char **buf, char **identifier, char **pid)
         return e;
 }
 
         return e;
 }
 
-void syslog_parse_priority(char **p, int *priority, bool with_facility) {
+void syslog_parse_priority(const char **p, int *priority, bool with_facility) {
         int a = 0, b = 0, c = 0;
         int k;
 
         int a = 0, b = 0, c = 0;
         int k;
 
@@ -366,7 +366,7 @@ void server_process_syslog_message(
         assert(buf);
 
         orig = buf;
         assert(buf);
 
         orig = buf;
-        syslog_parse_priority((char**) &buf, &priority, true);
+        syslog_parse_priority(&buf, &priority, true);
 
         if (s->forward_to_syslog)
                 forward_syslog_raw(s, priority, orig, ucred, tv);
 
         if (s->forward_to_syslog)
                 forward_syslog_raw(s, priority, orig, ucred, tv);
index 8ccdb77a0928b0c07c59a7940ad971d86ffe361d..057ea79def6a4127bb311a99a3a27ad79043e8d0 100644 (file)
@@ -25,7 +25,7 @@
 
 int syslog_fixup_facility(int priority) _const_;
 
 
 int syslog_fixup_facility(int priority) _const_;
 
-void syslog_parse_priority(char **p, int *priority, bool with_facility);
+void syslog_parse_priority(const char **p, int *priority, bool with_facility);
 size_t syslog_parse_identifier(const char **buf, char **identifier, char **pid);
 
 void server_forward_syslog(Server *s, int priority, const char *identifier, const char *message, struct ucred *ucred, struct timeval *tv);
 size_t syslog_parse_identifier(const char **buf, char **identifier, char **pid);
 
 void server_forward_syslog(Server *s, int priority, const char *identifier, const char *message, struct ucred *ucred, struct timeval *tv);