|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
From 88cf51c38bb39b49e2b11cdde7ac20ca851ea344 Mon Sep 17 00:00:00 2001
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
Date: Sat, 13 May 2017 22:34:40 -0400
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
Subject: [PATCH] journald: properly process multiple entries in the same
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
native packet
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
For all except the last entry in a single packet, we would dispatch the
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
message to the journal, but not forward it, nor perform proper cleanup.
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
Rewrite the code to process each entry in a helper function, and make
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
server_process_native_message() just call this function in a loop.
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
Fixes #5643.
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
v2:
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
- properly decrement *remaining when processing entry separator
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
(cherry picked from commit 68944f196bc85b067de71c4fe1631d824d0aded5)
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
---
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
src/journal/journald-native.c | 83 +++++++++++++++++++++++++------------------
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
1 file changed, 49 insertions(+), 34 deletions(-)
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
diff --git a/src/journal/journald-native.c b/src/journal/journald-native.c
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
index d839e04488..83250c34e1 100644
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
--- a/src/journal/journald-native.c
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+++ b/src/journal/journald-native.c
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
@@ -81,60 +81,52 @@ static bool allow_object_pid(const struct ucred *ucred) {
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
return ucred && ucred->uid == 0;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
-void server_process_native_message(
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+static int server_process_entry(
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
Server *s,
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
- const void *buffer, size_t buffer_size,
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ const void *buffer, size_t *remaining,
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
const struct ucred *ucred,
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
const struct timeval *tv,
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
const char *label, size_t label_len) {
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ /* Process a single entry from a native message.
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ * Returns 0 if nothing special happened and the message processing should continue,
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ * and a negative or positive value otherwise.
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ *
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ * Note that *remaining is altered on both success and failure. */
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
struct iovec *iovec = NULL;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
unsigned n = 0, j, tn = (unsigned) -1;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
const char *p;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
- size_t remaining, m = 0, entry_size = 0;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ size_t m = 0, entry_size = 0;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
int priority = LOG_INFO;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
char *identifier = NULL, *message = NULL;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
pid_t object_pid = 0;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
-
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
- assert(s);
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
- assert(buffer || buffer_size == 0);
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ int r = 0;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
p = buffer;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
- remaining = buffer_size;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
- while (remaining > 0) {
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ while (*remaining > 0) {
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
const char *e, *q;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
- e = memchr(p, '\n', remaining);
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ e = memchr(p, '\n', *remaining);
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
if (!e) {
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
/* Trailing noise, let's ignore it, and flush what we collected */
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
log_debug("Received message with trailing noise, ignoring.");
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ r = 1; /* finish processing of the message */
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
break;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
if (e == p) {
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
/* Entry separator */
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
-
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
- if (entry_size + n + 1 > ENTRY_SIZE_MAX) { /* data + separators + trailer */
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
- log_debug("Entry is too big with %u properties and %zu bytes, ignoring.", n, entry_size);
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
- continue;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
- }
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
-
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
- server_dispatch_message(s, iovec, n, m, ucred, tv, label, label_len, NULL, priority, object_pid);
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
- n = 0;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
- priority = LOG_INFO;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
- entry_size = 0;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
-
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
- p++;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
- remaining--;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
- continue;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ *remaining -= 1;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ break;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
if (*p == '.' || *p == '#') {
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
/* Ignore control commands for now, and
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
* comments too. */
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
- remaining -= (e - p) + 1;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ *remaining -= (e - p) + 1;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
p = e + 1;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
continue;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
@@ -143,7 +135,7 @@ void server_process_native_message(
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
/* n existing properties, 1 new, +1 for _TRANSPORT */
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
if (!GREEDY_REALLOC(iovec, m, n + 2 + N_IOVEC_META_FIELDS + N_IOVEC_OBJECT_FIELDS)) {
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
- log_oom();
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ r = log_oom();
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
break;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
@@ -160,7 +152,7 @@ void server_process_native_message(
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
* field */
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
iovec[n].iov_base = (char*) p;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
iovec[n].iov_len = l;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
- entry_size += iovec[n].iov_len;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ entry_size += l;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
n++;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
/* We need to determine the priority
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
@@ -210,19 +202,18 @@ void server_process_native_message(
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
memcpy(buf, p + strlen("OBJECT_PID="), l - strlen("OBJECT_PID="));
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
buf[l-strlen("OBJECT_PID=")] = '\0';
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
- /* ignore error */
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
- parse_pid(buf, &object_pid);
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ (void) parse_pid(buf, &object_pid);
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
- remaining -= (e - p) + 1;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ *remaining -= (e - p) + 1;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
p = e + 1;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
continue;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
} else {
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
uint64_t l;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
char *k;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
- if (remaining < e - p + 1 + sizeof(uint64_t) + 1) {
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ if (*remaining < e - p + 1 + sizeof(uint64_t) + 1) {
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
log_debug("Failed to parse message, ignoring.");
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
break;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
@@ -234,7 +225,7 @@ void server_process_native_message(
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
break;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
- if ((uint64_t) remaining < e - p + 1 + sizeof(uint64_t) + l + 1 ||
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ if ((uint64_t) *remaining < e - p + 1 + sizeof(uint64_t) + l + 1 ||
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
e[1+sizeof(uint64_t)+l] != '\n') {
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
log_debug("Failed to parse message, ignoring.");
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
break;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
@@ -258,13 +249,15 @@ void server_process_native_message(
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
} else
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
free(k);
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
- remaining -= (e - p) + 1 + sizeof(uint64_t) + l + 1;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ *remaining -= (e - p) + 1 + sizeof(uint64_t) + l + 1;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
p = e + 1 + sizeof(uint64_t) + l + 1;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
- if (n <= 0)
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ if (n <= 0) {
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ r = 1;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
goto finish;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ }
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
tn = n++;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
IOVEC_SET_STRING(iovec[tn], "_TRANSPORT=journal");
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
@@ -298,13 +291,35 @@ finish:
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
continue;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
if (iovec[j].iov_base < buffer ||
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
- (const uint8_t*) iovec[j].iov_base >= (const uint8_t*) buffer + buffer_size)
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ (const char*) iovec[j].iov_base >= p + *remaining)
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
free(iovec[j].iov_base);
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
free(iovec);
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
free(identifier);
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
free(message);
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ return r;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+}
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+void server_process_native_message(
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ Server *s,
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ const void *buffer, size_t buffer_size,
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ const struct ucred *ucred,
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ const struct timeval *tv,
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ const char *label, size_t label_len) {
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ int r;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ size_t remaining = buffer_size;
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ assert(s);
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ assert(buffer || buffer_size == 0);
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ do {
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ r = server_process_entry(s,
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ (const uint8_t*) buffer + (buffer_size - remaining), &remaining,
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ ucred, tv, label, label_len);
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
+ } while (r == 0);
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
|
|
Zbigniew Jędrzejewski-Szmek |
f4a676 |
void server_process_native_file(
|