[systemd-devel] [PATCH 1/2] Use heap allocation for stdout journald buffer

Dan McGee dan at archlinux.org
Sun Dec 8 09:32:33 PST 2013


Previously this was a static array in the standard out stream structure.
Behavior shouldn't change with this as we do a one-time allocation of
the buffer on the heap when creating the stream, and free it when it is
released. However, it sets the stage for a growable buffer in the
future.
---

The commit message of the next patch describes the 'why' behind this a bit
better, as it actually introduces dynamic buffer resizing. This is simply a
stepping stone for that work.

Is there some dynamic buffer code I should be using rather than doing a bit of
the roll-my-own stuff as I did? Ideally it would be for dynamic buffers of
bytes rather than assuming strings, and thus not tripping over NULL bytes in
the stream. However, the current stdout journal capture code would definitely
not handle NULLs in the stream all that well. Suggestions welcome, thanks!

 src/journal/journald-stream.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/src/journal/journald-stream.c b/src/journal/journald-stream.c
index 9ca26e2..193d438 100644
--- a/src/journal/journald-stream.c
+++ b/src/journal/journald-stream.c
@@ -68,7 +68,8 @@ struct StdoutStream {
         bool forward_to_kmsg:1;
         bool forward_to_console:1;
 
-        char buffer[LINE_MAX+1];
+        char *buffer;
+        size_t size;
         size_t length;
 
         LIST_FIELDS(StdoutStream, stdout_stream);
@@ -251,13 +252,14 @@ static int stdout_stream_scan(StdoutStream *s, bool force_flush) {
                 end = memchr(p, '\n', remaining);
                 if (end)
                         skip = end - p + 1;
-                else if (remaining >= sizeof(s->buffer) - 1) {
-                        end = p + sizeof(s->buffer) - 1;
+                else if (remaining >= s->size - 1) {
+                        /* ran out of buffer space, log what we have */
+                        end = s->buffer + s->size - 1;
                         skip = remaining;
                 } else
                         break;
 
-                *end = 0;
+                *end = '\0';
 
                 r = stdout_stream_line(s, p);
                 if (r < 0)
@@ -268,7 +270,7 @@ static int stdout_stream_scan(StdoutStream *s, bool force_flush) {
         }
 
         if (force_flush && remaining > 0) {
-                p[remaining] = 0;
+                p[remaining] = '\0';
                 r = stdout_stream_line(s, p);
                 if (r < 0)
                         return r;
@@ -291,7 +293,7 @@ int stdout_stream_process(StdoutStream *s) {
 
         assert(s);
 
-        l = read(s->fd, s->buffer+s->length, sizeof(s->buffer)-1-s->length);
+        l = read(s->fd, s->buffer+s->length, s->size-1-s->length);
         if (l < 0) {
 
                 if (errno == EAGAIN)
@@ -339,6 +341,7 @@ void stdout_stream_free(StdoutStream *s) {
                 freecon(s->security_context);
 #endif
 
+        free(s->buffer);
         free(s->identifier);
         free(s);
 }
@@ -371,6 +374,13 @@ int stdout_stream_new(Server *s) {
                 close_nointr_nofail(fd);
                 return log_oom();
         }
+        stream->buffer = malloc0(LINE_MAX);
+        if (!stream->buffer) {
+                free(stream);
+                close_nointr_nofail(fd);
+                return log_oom();
+        }
+        stream->size = LINE_MAX;
 
         stream->fd = fd;
 
-- 
1.8.5.1



More information about the systemd-devel mailing list