diff options
author | Lennart Poettering <lennart@poettering.net> | 2023-10-19 16:36:43 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2023-10-20 10:43:50 +0200 |
commit | 986235a99a2f2c8be96720d857c7530f9e36603b (patch) | |
tree | 6fcbd933f2207cc978b650f3ef95f3ee43266d4e | |
parent | iovec-util: make IOVEC_MAKE_STRING() safer (diff) | |
download | systemd-986235a99a2f2c8be96720d857c7530f9e36603b.tar.xz systemd-986235a99a2f2c8be96720d857c7530f9e36603b.zip |
iovec-util: make IOVEC_INCREMENT a regular function too
Even more than with the previous commit, this is not a trivial function
and there's no reason to believe this will actually be inlined nor that
it would be beneficial.
-rw-r--r-- | src/basic/iovec-util.c | 25 | ||||
-rw-r--r-- | src/basic/iovec-util.h | 23 | ||||
-rw-r--r-- | src/basic/log.c | 2 | ||||
-rw-r--r-- | src/fuzz/fuzz-varlink.c | 2 | ||||
-rw-r--r-- | src/libsystemd/sd-daemon/sd-daemon.c | 2 | ||||
-rw-r--r-- | src/resolve/resolved-dns-stream.c | 2 |
6 files changed, 30 insertions, 26 deletions
diff --git a/src/basic/iovec-util.c b/src/basic/iovec-util.c index 500ac33bbd..11aa630ae6 100644 --- a/src/basic/iovec-util.c +++ b/src/basic/iovec-util.c @@ -14,6 +14,31 @@ size_t iovec_total_size(const struct iovec *i, size_t n) { return sum; } +bool iovec_increment(struct iovec *i, size_t n, size_t k) { + assert(i || n == 0); + + /* Returns true if there is nothing else to send (bytes written cover all of the iovec), + * false if there's still work to do. */ + + FOREACH_ARRAY(j, i, n) { + size_t sub; + + if (j->iov_len == 0) + continue; + if (k == 0) + return false; + + sub = MIN(j->iov_len, k); + j->iov_len -= sub; + j->iov_base = (uint8_t*) j->iov_base + sub; + k -= sub; + } + + assert(k == 0); /* Anything else would mean that we wrote more bytes than available, + * or the kernel reported writing more bytes than sent. */ + return true; +} + char* set_iovec_string_field(struct iovec *iovec, size_t *n_iovec, const char *field, const char *value) { char *x; diff --git a/src/basic/iovec-util.h b/src/basic/iovec-util.h index 245c616017..1f72898cba 100644 --- a/src/basic/iovec-util.h +++ b/src/basic/iovec-util.h @@ -9,28 +9,7 @@ size_t iovec_total_size(const struct iovec *i, size_t n); -static inline bool IOVEC_INCREMENT(struct iovec *i, size_t n, size_t k) { - /* Returns true if there is nothing else to send (bytes written cover all of the iovec), - * false if there's still work to do. */ - - for (size_t j = 0; j < n; j++) { - size_t sub; - - if (i[j].iov_len == 0) - continue; - if (k == 0) - return false; - - sub = MIN(i[j].iov_len, k); - i[j].iov_len -= sub; - i[j].iov_base = (uint8_t*) i[j].iov_base + sub; - k -= sub; - } - - assert(k == 0); /* Anything else would mean that we wrote more bytes than available, - * or the kernel reported writing more bytes than sent. */ - return true; -} +bool iovec_increment(struct iovec *i, size_t n, size_t k); #define IOVEC_NULL (const struct iovec) {} diff --git a/src/basic/log.c b/src/basic/log.c index b05620907b..e7fe84611e 100644 --- a/src/basic/log.c +++ b/src/basic/log.c @@ -541,7 +541,7 @@ static int write_to_syslog( if (!syslog_is_stream) break; - if (IOVEC_INCREMENT(iovec, ELEMENTSOF(iovec), n)) + if (iovec_increment(iovec, ELEMENTSOF(iovec), n)) break; } diff --git a/src/fuzz/fuzz-varlink.c b/src/fuzz/fuzz-varlink.c index e98929d1c6..18db78ce98 100644 --- a/src/fuzz/fuzz-varlink.c +++ b/src/fuzz/fuzz-varlink.c @@ -41,7 +41,7 @@ static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userd else assert_se(errno == EAGAIN); } else - IOVEC_INCREMENT(iov, 1, n); + iovec_increment(iov, 1, n); } if (revents & EPOLLIN) { diff --git a/src/libsystemd/sd-daemon/sd-daemon.c b/src/libsystemd/sd-daemon/sd-daemon.c index 8986596b70..7c370ab7bc 100644 --- a/src/libsystemd/sd-daemon/sd-daemon.c +++ b/src/libsystemd/sd-daemon/sd-daemon.c @@ -592,7 +592,7 @@ static int pid_notify_with_fds_internal( msghdr.msg_control = NULL; msghdr.msg_controllen = 0; } - } while (!IOVEC_INCREMENT(msghdr.msg_iov, msghdr.msg_iovlen, n)); + } while (!iovec_increment(msghdr.msg_iov, msghdr.msg_iovlen, n)); return 1; } diff --git a/src/resolve/resolved-dns-stream.c b/src/resolve/resolved-dns-stream.c index bc01613b51..ddd1db5e09 100644 --- a/src/resolve/resolved-dns-stream.c +++ b/src/resolve/resolved-dns-stream.c @@ -331,7 +331,7 @@ static int on_stream_io(sd_event_source *es, int fd, uint32_t revents, void *use IOVEC_MAKE(DNS_PACKET_DATA(s->write_packet), s->write_packet->size), }; - IOVEC_INCREMENT(iov, ELEMENTSOF(iov), s->n_written); + iovec_increment(iov, ELEMENTSOF(iov), s->n_written); ssize_t ss = dns_stream_writev(s, iov, ELEMENTSOF(iov), 0); if (ss < 0) { |