]> git.ipfire.org Git - pakfire.git/commitdiff
jail: Refactor how to drain logging buffers with callbacks
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 19 Dec 2023 12:37:45 +0000 (12:37 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 19 Dec 2023 12:37:45 +0000 (12:37 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/jail.c

index b42594cf76e254ce7021324ad1d305e811044882..29b0198a8a09c0bfc8846b1a2699d3f03c13709d 100644 (file)
@@ -628,6 +628,47 @@ static int pakfire_jail_fill_buffer(struct pakfire_jail* jail, int fd, struct pa
        return 0;
 }
 
+static int pakfire_jail_drain_buffer_with_callback(struct pakfire_jail* jail,
+               struct pakfire_log_buffer* buffer, int priority, pakfire_jail_communicate_out callback, void* data) {
+       const char* eol = NULL;
+       int r;
+
+       while (buffer->used) {
+               // Search for the end of the first line
+               eol = memchr(buffer->data, '\n', buffer->used);
+
+               // No newline found
+               if (!eol) {
+                       // If the buffer is full, we send the entire content to make space.
+                       if (pakfire_jail_log_buffer_is_full(buffer)) {
+                               CTX_DEBUG(jail->ctx, "Buffer is full. Sending all content\n");
+
+                               eol = buffer->data + buffer->used - 1;
+
+                       // Otherwise we might have only read parts of the output...
+                       } else {
+                               break;
+                       }
+               }
+
+               // Find the length of the string
+               const size_t length = eol - buffer->data + 1;
+
+               // Call the callback
+               r = callback(jail->pakfire, data, priority, buffer->data, length);
+               if (r) {
+                       CTX_ERROR(jail->ctx, "The logging callback returned an error: %d\n", r);
+                       return r;
+               }
+
+               // Remove line from buffer
+               memmove(buffer->data, buffer->data + length, buffer->used - length);
+               buffer->used -= length;
+       }
+
+       return 0;
+}
+
 static int pakfire_jail_drain_buffer(struct pakfire_jail* jail, int fd, struct pakfire_log_buffer* buffer) {
        int r;
 
@@ -664,10 +705,9 @@ static int pakfire_jail_drain_buffer(struct pakfire_jail* jail, int fd, struct p
        If it finds a whole line in it, it will send it to the logger and repeat the process.
        If not newline character is found, it will try to read more data until it finds one.
 */
-static int pakfire_jail_handle_log(struct pakfire_jail* jail,
-               struct pakfire_jail_exec* ctx, int priority, int fd,
-               struct pakfire_log_buffer* buffer, pakfire_jail_communicate_out callback, void* data) {
-       char line[BUFFER_SIZE + 1];
+static int pakfire_jail_handle_log(struct pakfire_jail* jail, struct pakfire_jail_exec* ctx,
+               int priority, int fd, struct pakfire_log_buffer* buffer,
+               pakfire_jail_communicate_out callback, void* data) {
        int r;
 
        // Fill up buffer from fd
@@ -675,47 +715,10 @@ static int pakfire_jail_handle_log(struct pakfire_jail* jail,
        if (r)
                return r;
 
-       // See if we have any lines that we can write
-       while (buffer->used) {
-               // Search for the end of the first line
-               char* eol = memchr(buffer->data, '\n', buffer->used);
-
-               // No newline found
-               if (!eol) {
-                       // If the buffer is full, we send the content to the logger and try again
-                       // This should not happen in practise
-                       if (pakfire_jail_log_buffer_is_full(buffer)) {
-                               DEBUG(jail->pakfire, "Logging buffer is full. Sending all content\n");
-
-                               eol = buffer->data + sizeof(buffer->data) - 1;
-
-                       // Otherwise we might have only read parts of the output
-                       } else
-                               break;
-               }
-
-               // Find the length of the string
-               size_t length = eol - buffer->data + 1;
-
-               // Copy the line into the buffer
-               memcpy(line, buffer->data, length);
-
-               // Terminate the string
-               line[length] = '\0';
-
-               // Log the line
-               if (callback) {
-                       r = callback(jail->pakfire, data, priority, line, length);
-                       if (r) {
-                               ERROR(jail->pakfire, "The logging callback returned an error: %d\n", r);
-                               return r;
-                       }
-               }
-
-               // Remove line from buffer
-               memmove(buffer->data, buffer->data + length, buffer->used - length);
-               buffer->used -= length;
-       }
+       // Drain the buffer
+       r = pakfire_jail_drain_buffer_with_callback(jail, buffer, priority, callback, data);
+       if (r)
+               return r;
 
        return 0;
 }