diff options
author | Lennart Poettering <lennart@poettering.net> | 2023-10-19 16:01:38 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2023-10-20 10:34:33 +0200 |
commit | bd1ae178336a3d9143feea35bb4885c923581e1d (patch) | |
tree | ace0e0a86173f28b68adb4359a1f11a44097ddce /src/basic/iovec-util.h | |
parent | executor: return instead of assert on invalid command line arguments (diff) | |
download | systemd-bd1ae178336a3d9143feea35bb4885c923581e1d.tar.xz systemd-bd1ae178336a3d9143feea35bb4885c923581e1d.zip |
io-util: split out "struct iovec" related calls into their own .c/.h files
This is preparation for #28891, which adds a bunch more helpers around
"struct iovec", at which point this really deserves its own .c/.h file.
The idea is that we sooner or later can consider "struct iovec" as an
entirely generic mechanism to reference some binary blob, and is the
go-to type for this purpose whenever we need one.
Diffstat (limited to 'src/basic/iovec-util.h')
-rw-r--r-- | src/basic/iovec-util.h | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/basic/iovec-util.h b/src/basic/iovec-util.h new file mode 100644 index 0000000000..7ee72671c4 --- /dev/null +++ b/src/basic/iovec-util.h @@ -0,0 +1,53 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +#include <stdbool.h> +#include <sys/types.h> +#include <sys/uio.h> + +#include "macro.h" + +static inline size_t IOVEC_TOTAL_SIZE(const struct iovec *i, size_t n) { + size_t r = 0; + + for (size_t j = 0; j < n; j++) + r += i[j].iov_len; + + return r; +} + +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; +} + +#define IOVEC_NULL (struct iovec) {} +#define IOVEC_MAKE(base, len) (struct iovec) { .iov_base = (base), .iov_len = (len) } +#define IOVEC_MAKE_STRING(string) \ + ({ \ + char *_s = (char*) (string); \ + IOVEC_MAKE(_s, strlen(_s)); \ + }) + +char* set_iovec_string_field(struct iovec *iovec, size_t *n_iovec, const char *field, const char *value); +char* set_iovec_string_field_free(struct iovec *iovec, size_t *n_iovec, const char *field, char *value); + +void iovec_array_free(struct iovec *iov, size_t n); |