journald: shorten code a bit and return error where it's useful

Follow-up for f31cff849d.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2024-02-19 11:56:28 +01:00
parent 92d1419eb8
commit 8f9e3ea090
3 changed files with 47 additions and 57 deletions
+5 -9
View File
@@ -1155,7 +1155,7 @@ static void server_dispatch_message_real(
else
journal_uid = 0;
server_forward_socket(s, iovec, n, priority);
(void) server_forward_socket(s, iovec, n, priority);
server_write_to_journal(s, journal_uid, iovec, n, priority);
}
@@ -2488,16 +2488,12 @@ static void server_load_credentials(Server *s) {
assert(s);
/* if we already have a forward address from config don't load the credential */
if (s->forward_to_socket.sockaddr.sa.sa_family != AF_UNSPEC) {
log_debug("Socket forward address already set not loading journal.forward_to_socket");
return;
}
if (s->forward_to_socket.sockaddr.sa.sa_family != AF_UNSPEC)
return log_debug("Socket forward address already set not loading journal.forward_to_socket");
r = read_credential("journal.forward_to_socket", &data, NULL);
if (r < 0) {
log_debug_errno(r, "Failed to read credential journal.forward_to_socket, ignoring: %m");
return;
}
if (r < 0)
return (void) log_debug_errno(r, "Failed to read credential journal.forward_to_socket, ignoring: %m");
r = socket_address_parse(&s->forward_to_socket, data);
if (r < 0)
+41 -46
View File
@@ -13,39 +13,38 @@
#include "socket-util.h"
#include "sparse-endian.h"
void server_open_forward_socket(Server *s) {
static int server_open_forward_socket(Server *s) {
_cleanup_close_ int socket_fd = -EBADF;
const SocketAddress *addr;
int family;
assert(s);
/* nop if there is nothing to do */
if (s->forward_to_socket.sockaddr.sa.sa_family == AF_UNSPEC || s->namespace || s->forward_socket_fd >= 0)
return;
/* Noop if there is nothing to do. */
if (s->forward_to_socket.sockaddr.sa.sa_family == AF_UNSPEC || s->namespace)
return 0;
/* All ready, nothing to do. */
if (s->forward_socket_fd >= 0)
return 1;
addr = &s->forward_to_socket;
family = socket_address_family(addr);
if (!IN_SET(family, AF_UNIX, AF_INET, AF_INET6, AF_VSOCK)) {
log_debug("Unsupported socket type for forward socket: %d", family);
return;
}
if (!IN_SET(family, AF_UNIX, AF_INET, AF_INET6, AF_VSOCK))
return log_debug_errno(SYNTHETIC_ERRNO(ESOCKTNOSUPPORT),
"Unsupported socket type for forward socket: %d", family);
socket_fd = socket(family, SOCK_STREAM|SOCK_CLOEXEC, 0);
if (socket_fd < 0) {
log_debug_errno(errno, "Failed to create forward socket, ignoring: %m");
return;
}
if (socket_fd < 0)
return log_debug_errno(errno, "Failed to create forward socket, ignoring: %m");
if (connect(socket_fd, &addr->sockaddr.sa, addr->size) < 0) {
log_debug_errno(errno, "Failed to connect to remote address for forwarding, ignoring: %m");
return;
}
if (connect(socket_fd, &addr->sockaddr.sa, addr->size) < 0)
return log_debug_errno(errno, "Failed to connect to remote address for forwarding, ignoring: %m");
s->forward_socket_fd = TAKE_FD(socket_fd);
log_debug("Successfully connected to remote address for forwarding");
log_debug("Successfully connected to remote address for forwarding.");
return 1;
}
static inline bool must_serialise(struct iovec iov) {
@@ -58,42 +57,40 @@ static inline bool must_serialise(struct iovec iov) {
const uint8_t *s = iov.iov_base;
bool before_value = true;
FOREACH_ARRAY(c, s, iov.iov_len) {
FOREACH_ARRAY(c, s, iov.iov_len)
if (before_value)
before_value = *c != (uint8_t)'=';
else if (*c < (uint8_t)' ' && *c != (uint8_t)'\t')
return true;
}
return false;
}
void server_forward_socket(
int server_forward_socket(
Server *s,
const struct iovec *iovec,
size_t n_iovec,
int priority) {
_cleanup_free_ struct iovec *iov_alloc = NULL;
struct iovec *iov = NULL;
_cleanup_free_ struct iovec *iov_alloc = NULL;
struct iovec *iov;
_cleanup_free_ le64_t *len_alloc = NULL;
le64_t *len = NULL;
le64_t *len;
int r;
assert(s);
assert(iovec);
assert(n_iovec > 0);
if (LOG_PRI(priority) > s->max_level_socket)
return;
return 0;
server_open_forward_socket(s);
r = server_open_forward_socket(s);
if (r <= 0)
return r;
/* if we failed to open a socket just return */
if (s->forward_socket_fd < 0)
return;
/* we need a newline after each iovec + 4 for each we have to serialise in a binary safe way
* +1 for the final __REALTIME_TIMESTAMP metadata field */
/* We need a newline after each iovec + 4 for each we have to serialise in a binary safe way
* + 1 for the final __REALTIME_TIMESTAMP metadata field. */
size_t n = n_iovec * 5 + 1;
if (n < ALLOCA_MAX / (sizeof(struct iovec) + sizeof(le64_t)) / 2) {
@@ -101,18 +98,14 @@ void server_forward_socket(
len = newa(le64_t, n_iovec);
} else {
iov_alloc = new(struct iovec, n);
if (!iov_alloc) {
log_oom();
return;
}
if (!iov_alloc)
return log_oom();
iov = iov_alloc;
len_alloc = new(le64_t, n_iovec);
if (!len_alloc) {
log_oom();
return;
}
if (!len_alloc)
return log_oom();
len = len_alloc;
}
@@ -125,10 +118,9 @@ void server_forward_socket(
c = memchr(i->iov_base, '=', i->iov_len);
/* this should never happen */
if (_unlikely_(!c || c == i->iov_base)) {
log_error("Found invalid journal field, refusing to forward.");
return;
}
if (_unlikely_(!c || c == i->iov_base))
return log_warning_errno(SYNTHETIC_ERRNO(EBADMSG),
"Found invalid journal field, refusing to forward.");
/* write the field name */
iov[iov_idx++] = IOVEC_MAKE(i->iov_base, c - (uint8_t*) i->iov_base);
@@ -147,7 +139,8 @@ void server_forward_socket(
iov[iov_idx++] = nl;
}
/* synthesise __REALTIME_TIMESTAMP as the last argument so systemd-journal-upload can receive these export messages */
/* Synthesise __REALTIME_TIMESTAMP as the last argument so systemd-journal-upload can receive these
* export messages. */
char buf[sizeof("__REALTIME_TIMESTAMP=") + DECIMAL_STR_MAX(usec_t) + 2];
xsprintf(buf, "__REALTIME_TIMESTAMP="USEC_FMT"\n\n", now(CLOCK_REALTIME));
iov[iov_idx++] = IOVEC_MAKE_STRING(buf);
@@ -155,8 +148,10 @@ void server_forward_socket(
if (writev(s->forward_socket_fd, iov, iov_idx) < 0) {
log_debug_errno(errno, "Failed to forward log message over socket: %m");
/* if we failed to send once we will probably fail again so wait for a new connection to
* establish before attempting to forward again */
/* If we failed to send once we will probably fail again so wait for a new connection to
* establish before attempting to forward again. */
s->forward_socket_fd = safe_close(s->forward_socket_fd);
}
return 0;
}
+1 -2
View File
@@ -4,5 +4,4 @@
#include "journald-server.h"
#include "socket-util.h"
void server_forward_socket(Server *s, const struct iovec *iovec, size_t n, int priority);
void server_open_forward_socket(Server *s);
int server_forward_socket(Server *s, const struct iovec *iovec, size_t n, int priority);