From 4dc2ecd227453597c63548d043401eddef87b9cd Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Mon, 26 Jul 2021 14:56:34 +0200 Subject: [PATCH] basic/log: use structured initialization, drop unused initialization We had 'msghdr' and 'mh' in various places. Now 'const struct msghdr msghdr' is used consistently. With structured init the variable is only used in the call to sendmsg(), so let's make it a bit more descriptive. --- src/basic/log.c | 52 +++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/src/basic/log.c b/src/basic/log.c index 7602536691a..59c74659eb0 100644 --- a/src/basic/log.c +++ b/src/basic/log.c @@ -476,7 +476,7 @@ static int write_to_syslog( IOVEC_MAKE_STRING(header_pid), IOVEC_MAKE_STRING(buffer), }; - struct msghdr msghdr = { + const struct msghdr msghdr = { .msg_iov = iovec, .msg_iovlen = ELEMENTSOF(iovec), }; @@ -608,23 +608,24 @@ static int write_to_journal( const char *buffer) { char header[LINE_MAX]; - struct iovec iovec[4] = {}; - struct msghdr mh = {}; if (journal_fd < 0) return 0; log_do_header(header, sizeof(header), level, error, file, line, func, object_field, object, extra_field, extra); - iovec[0] = IOVEC_MAKE_STRING(header); - iovec[1] = IOVEC_MAKE_STRING("MESSAGE="); - iovec[2] = IOVEC_MAKE_STRING(buffer); - iovec[3] = IOVEC_MAKE_STRING("\n"); - - mh.msg_iov = iovec; - mh.msg_iovlen = ELEMENTSOF(iovec); + struct iovec iovec[4] = { + IOVEC_MAKE_STRING(header), + IOVEC_MAKE_STRING("MESSAGE="), + IOVEC_MAKE_STRING(buffer), + IOVEC_MAKE_STRING("\n"), + }; + const struct msghdr msghdr = { + .msg_iov = iovec, + .msg_iovlen = ELEMENTSOF(iovec), + }; - if (sendmsg(journal_fd, &mh, MSG_NOSIGNAL) < 0) + if (sendmsg(journal_fd, &msghdr, MSG_NOSIGNAL) < 0) return -errno; return 1; @@ -959,12 +960,9 @@ int log_struct_internal( if (journal_fd >= 0) { char header[LINE_MAX]; - struct iovec iovec[17] = {}; + struct iovec iovec[17]; size_t n = 0; int r; - struct msghdr mh = { - .msg_iov = iovec, - }; bool fallback = false; /* If the journal is available do structured logging. @@ -977,8 +975,12 @@ int log_struct_internal( if (r < 0) fallback = true; else { - mh.msg_iovlen = n; - (void) sendmsg(journal_fd, &mh, MSG_NOSIGNAL); + const struct msghdr msghdr = { + .msg_iov = iovec, + .msg_iovlen = n, + }; + + (void) sendmsg(journal_fd, &msghdr, MSG_NOSIGNAL); } va_end(ap); @@ -1052,22 +1054,22 @@ int log_struct_iovec_internal( LOG_TARGET_JOURNAL) && journal_fd >= 0) { - struct iovec iovec[1 + n_input_iovec*2]; char header[LINE_MAX]; - struct msghdr mh = { - .msg_iov = iovec, - .msg_iovlen = 1 + n_input_iovec*2, - }; - log_do_header(header, sizeof(header), level, error, file, line, func, NULL, NULL, NULL, NULL); - iovec[0] = IOVEC_MAKE_STRING(header); + struct iovec iovec[1 + n_input_iovec*2]; + iovec[0] = IOVEC_MAKE_STRING(header); for (i = 0; i < n_input_iovec; i++) { iovec[1+i*2] = input_iovec[i]; iovec[1+i*2+1] = IOVEC_MAKE_STRING("\n"); } - if (sendmsg(journal_fd, &mh, MSG_NOSIGNAL) >= 0) + const struct msghdr msghdr = { + .msg_iov = iovec, + .msg_iovlen = 1 + n_input_iovec*2, + }; + + if (sendmsg(journal_fd, &msghdr, MSG_NOSIGNAL) >= 0) return -ERRNO_VALUE(error); } -- 2.47.3