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;
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
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;
}