diff --git a/src/basic/io-util.h b/src/basic/io-util.h index d98817f7609..ce54ca228bf 100644 --- a/src/basic/io-util.h +++ b/src/basic/io-util.h @@ -25,22 +25,25 @@ int fd_wait_for_event(int fd, int event, usec_t timeout); ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length); static inline size_t IOVEC_TOTAL_SIZE(const struct iovec *i, size_t n) { - size_t j, r = 0; + size_t r = 0; - for (j = 0; j < n; j++) + for (size_t j = 0; j < n; j++) r += i[j].iov_len; return r; } -static inline size_t IOVEC_INCREMENT(struct iovec *i, size_t n, size_t k) { - size_t j; +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 (j = 0; j < n; j++) { + for (size_t j = 0; j < n; j++) { size_t sub; - if (_unlikely_(k <= 0)) - break; + if (i[j].iov_len == 0) + continue; + if (k == 0) + return false; sub = MIN(i[j].iov_len, k); i[j].iov_len -= sub; @@ -48,7 +51,9 @@ static inline size_t IOVEC_INCREMENT(struct iovec *i, size_t n, size_t k) { k -= sub; } - return k; + 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; } static inline bool FILE_SIZE_VALID(uint64_t l) { diff --git a/src/basic/log.c b/src/basic/log.c index 595db0c395a..b4c16a3e1b9 100644 --- a/src/basic/log.c +++ b/src/basic/log.c @@ -452,11 +452,6 @@ static int write_to_syslog( char header_priority[2 + DECIMAL_STR_MAX(int) + 1], header_time[64], header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1]; - struct iovec iovec[5] = {}; - struct msghdr msghdr = { - .msg_iov = iovec, - .msg_iovlen = ELEMENTSOF(iovec), - }; time_t t; struct tm tm; @@ -474,15 +469,21 @@ static int write_to_syslog( xsprintf(header_pid, "["PID_FMT"]: ", getpid_cached()); - iovec[0] = IOVEC_MAKE_STRING(header_priority); - iovec[1] = IOVEC_MAKE_STRING(header_time); - iovec[2] = IOVEC_MAKE_STRING(program_invocation_short_name); - iovec[3] = IOVEC_MAKE_STRING(header_pid); - iovec[4] = IOVEC_MAKE_STRING(buffer); + struct iovec iovec[] = { + IOVEC_MAKE_STRING(header_priority), + IOVEC_MAKE_STRING(header_time), + IOVEC_MAKE_STRING(program_invocation_short_name), + IOVEC_MAKE_STRING(header_pid), + IOVEC_MAKE_STRING(buffer), + }; + struct msghdr msghdr = { + .msg_iov = iovec, + .msg_iovlen = ELEMENTSOF(iovec), + }; /* When using syslog via SOCK_STREAM separate the messages by NUL chars */ if (syslog_is_stream) - iovec[4].iov_len++; + iovec[ELEMENTSOF(iovec) - 1].iov_len++; for (;;) { ssize_t n; @@ -491,11 +492,11 @@ static int write_to_syslog( if (n < 0) return -errno; - if (!syslog_is_stream || - (size_t) n >= IOVEC_TOTAL_SIZE(iovec, ELEMENTSOF(iovec))) + if (!syslog_is_stream) break; - IOVEC_INCREMENT(iovec, ELEMENTSOF(iovec), n); + if (IOVEC_INCREMENT(iovec, ELEMENTSOF(iovec), n)) + break; } return 1; @@ -520,7 +521,6 @@ static int write_to_kmsg( char header_priority[2 + DECIMAL_STR_MAX(int) + 1], header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1]; - struct iovec iovec[5] = {}; if (kmsg_fd < 0) return 0; @@ -531,11 +531,13 @@ static int write_to_kmsg( xsprintf(header_priority, "<%i>", level); xsprintf(header_pid, "["PID_FMT"]: ", getpid_cached()); - iovec[0] = IOVEC_MAKE_STRING(header_priority); - iovec[1] = IOVEC_MAKE_STRING(program_invocation_short_name); - iovec[2] = IOVEC_MAKE_STRING(header_pid); - iovec[3] = IOVEC_MAKE_STRING(buffer); - iovec[4] = IOVEC_MAKE_STRING("\n"); + const struct iovec iovec[] = { + IOVEC_MAKE_STRING(header_priority), + IOVEC_MAKE_STRING(program_invocation_short_name), + IOVEC_MAKE_STRING(header_pid), + IOVEC_MAKE_STRING(buffer), + IOVEC_MAKE_STRING("\n"), + }; if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0) return -errno; @@ -959,7 +961,7 @@ int log_struct_internal( if (journal_fd >= 0) { char header[LINE_MAX]; struct iovec iovec[17] = {}; - size_t n = 0, i; + size_t n = 0; int r; struct msghdr mh = { .msg_iov = iovec, @@ -981,7 +983,7 @@ int log_struct_internal( } va_end(ap); - for (i = 1; i < n; i += 2) + for (size_t i = 1; i < n; i += 2) free(iovec[i].iov_base); if (!fallback) { diff --git a/src/boot/efi/meson.build b/src/boot/efi/meson.build index 574feedb98d..47768931082 100644 --- a/src/boot/efi/meson.build +++ b/src/boot/efi/meson.build @@ -190,7 +190,7 @@ if have_gnu_efi '-znocombreloc', '-L', efi_libdir, efi_crt0] - if efi_arch == 'aarch64' or efi_arch == 'arm' or efi_arch == 'riscv64' + if ['aarch64', 'arm', 'riscv64'].contains(efi_arch) # Aarch64, ARM32 and 64bit RISC-V don't have an EFI capable objcopy. # Use 'binary' instead, and add required symbols manually. efi_ldflags += ['--defsym=EFI_SUBSYSTEM=0xa'] diff --git a/src/core/automount.c b/src/core/automount.c index f0fa5c8ca98..dc92f9c0e44 100644 --- a/src/core/automount.c +++ b/src/core/automount.c @@ -651,7 +651,7 @@ fail: static void *expire_thread(void *p) { struct autofs_dev_ioctl param; - _cleanup_(expire_data_freep) struct expire_data *data = (struct expire_data*)p; + _cleanup_(expire_data_freep) struct expire_data *data = p; int r; assert(data->dev_autofs_fd >= 0); diff --git a/src/core/cgroup.c b/src/core/cgroup.c index 9fbf64b7bee..a44cf9368c7 100644 --- a/src/core/cgroup.c +++ b/src/core/cgroup.c @@ -305,8 +305,7 @@ static int unit_compare_memory_limit(Unit *u, const char *property_name, uint64_ if (!FLAGS_SET(m, CGROUP_MASK_MEMORY)) return -ENODATA; - c = unit_get_cgroup_context(u); - assert(c); + assert_se(c = unit_get_cgroup_context(u)); if (streq(property_name, "MemoryLow")) { unit_value = unit_get_ancestor_memory_low(u); @@ -407,8 +406,7 @@ void cgroup_context_dump(Unit *u, FILE* f, const char *prefix) { assert(u); assert(f); - c = unit_get_cgroup_context(u); - assert(c); + assert_se(c = unit_get_cgroup_context(u)); prefix = strempty(prefix); @@ -451,7 +449,7 @@ void cgroup_context_dump(Unit *u, FILE* f, const char *prefix) { "%sManagedOOMSwap: %s\n" "%sManagedOOMMemoryPressure: %s\n" "%sManagedOOMMemoryPressureLimit: " PERMYRIAD_AS_PERCENT_FORMAT_STR "\n" - "%sManagedOOMPreference: %s%%\n", + "%sManagedOOMPreference: %s\n", prefix, yes_no(c->cpu_accounting), prefix, yes_no(c->io_accounting), prefix, yes_no(c->blockio_accounting), @@ -1028,17 +1026,14 @@ static void cgroup_apply_io_device_latency(Unit *u, const char *dev_path, usec_t } static void cgroup_apply_io_device_limit(Unit *u, const char *dev_path, uint64_t *limits) { - char limit_bufs[_CGROUP_IO_LIMIT_TYPE_MAX][DECIMAL_STR_MAX(uint64_t)]; - char buf[DECIMAL_STR_MAX(dev_t)*2+2+(6+DECIMAL_STR_MAX(uint64_t)+1)*4]; - CGroupIOLimitType type; + char limit_bufs[_CGROUP_IO_LIMIT_TYPE_MAX][DECIMAL_STR_MAX(uint64_t)], + buf[DECIMAL_STR_MAX(dev_t)*2+2+(6+DECIMAL_STR_MAX(uint64_t)+1)*4]; dev_t dev; - int r; - r = lookup_block_device(dev_path, &dev); - if (r < 0) + if (lookup_block_device(dev_path, &dev) < 0) return; - for (type = 0; type < _CGROUP_IO_LIMIT_TYPE_MAX; type++) + for (CGroupIOLimitType type = 0; type < _CGROUP_IO_LIMIT_TYPE_MAX; type++) if (limits[type] != cgroup_io_limit_defaults[type]) xsprintf(limit_bufs[type], "%" PRIu64, limits[type]); else @@ -1053,10 +1048,8 @@ static void cgroup_apply_io_device_limit(Unit *u, const char *dev_path, uint64_t static void cgroup_apply_blkio_device_limit(Unit *u, const char *dev_path, uint64_t rbps, uint64_t wbps) { char buf[DECIMAL_STR_MAX(dev_t)*2+2+DECIMAL_STR_MAX(uint64_t)+1]; dev_t dev; - int r; - r = lookup_block_device(dev_path, &dev); - if (r < 0) + if (lookup_block_device(dev_path, &dev) < 0) return; sprintf(buf, "%u:%u %" PRIu64 "\n", major(dev), minor(dev), rbps); @@ -1071,8 +1064,7 @@ static bool unit_has_unified_memory_config(Unit *u) { assert(u); - c = unit_get_cgroup_context(u); - assert(c); + assert_se(c = unit_get_cgroup_context(u)); return unit_get_ancestor_memory_min(u) > 0 || unit_get_ancestor_memory_low(u) > 0 || c->memory_high != CGROUP_LIMIT_MAX || c->memory_max != CGROUP_LIMIT_MAX || @@ -1597,9 +1589,7 @@ static CGroupMask unit_get_cgroup_mask(Unit *u) { assert(u); - c = unit_get_cgroup_context(u); - - assert(c); + assert_se(c = unit_get_cgroup_context(u)); /* Figure out which controllers we need, based on the cgroup context object */ diff --git a/src/core/main.c b/src/core/main.c index 2f36d752bca..12948ca964c 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -2549,18 +2549,16 @@ static void setup_console_terminal(bool skip_setup) { static bool early_skip_setup_check(int argc, char *argv[]) { bool found_deserialize = false; - int i; /* Determine if this is a reexecution or normal bootup. We do the full command line parsing much later, so * let's just have a quick peek here. Note that if we have switched root, do all the special setup things * anyway, even if in that case we also do deserialization. */ - for (i = 1; i < argc; i++) { + for (int i = 1; i < argc; i++) if (streq(argv[i], "--switched-root")) return false; /* If we switched root, don't skip the setup. */ else if (streq(argv[i], "--deserialize")) found_deserialize = true; - } return found_deserialize; /* When we are deserializing, then we are reexecuting, hence avoid the extensive setup */ } diff --git a/src/resolve/resolved-dns-stream.c b/src/resolve/resolved-dns-stream.c index 10641f6ac59..3e6505cd75b 100644 --- a/src/resolve/resolved-dns-stream.c +++ b/src/resolve/resolved-dns-stream.c @@ -315,15 +315,14 @@ static int on_stream_io(sd_event_source *es, int fd, uint32_t revents, void *use s->write_packet && s->n_written < sizeof(s->write_size) + s->write_packet->size) { - struct iovec iov[2]; - ssize_t ss; + struct iovec iov[] = { + IOVEC_MAKE(&s->write_size, sizeof(s->write_size)), + IOVEC_MAKE(DNS_PACKET_DATA(s->write_packet), s->write_packet->size), + }; - iov[0] = IOVEC_MAKE(&s->write_size, sizeof(s->write_size)); - iov[1] = IOVEC_MAKE(DNS_PACKET_DATA(s->write_packet), s->write_packet->size); + IOVEC_INCREMENT(iov, ELEMENTSOF(iov), s->n_written); - IOVEC_INCREMENT(iov, 2, s->n_written); - - ss = dns_stream_writev(s, iov, 2, 0); + ssize_t ss = dns_stream_writev(s, iov, ELEMENTSOF(iov), 0); if (ss < 0) { if (!IN_SET(-ss, EINTR, EAGAIN)) return dns_stream_complete(s, -ss);