Skip to content

Commit

Permalink
io-util: split out "struct iovec" related calls into their own .c/.h …
Browse files Browse the repository at this point in the history
…files

This is preparation for systemd#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.
  • Loading branch information
poettering authored and ssahani committed Nov 23, 2023
1 parent 8459615 commit f1b27bd
Show file tree
Hide file tree
Showing 55 changed files with 143 additions and 113 deletions.
2 changes: 1 addition & 1 deletion src/basic/audit-util.c
Expand Up @@ -10,7 +10,7 @@
#include "audit-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "io-util.h"
#include "iovec-util.h"
#include "macro.h"
#include "parse-util.h"
#include "process-util.h"
Expand Down
28 changes: 1 addition & 27 deletions src/basic/io-util.c
Expand Up @@ -7,6 +7,7 @@

#include "errno-util.h"
#include "io-util.h"
#include "iovec-util.h"
#include "string-util.h"
#include "time-util.h"

Expand Down Expand Up @@ -305,23 +306,6 @@ ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length) {
return q - (const uint8_t*) p;
}

char* set_iovec_string_field(struct iovec *iovec, size_t *n_iovec, const char *field, const char *value) {
char *x;

x = strjoin(field, value);
if (x)
iovec[(*n_iovec)++] = IOVEC_MAKE_STRING(x);
return x;
}

char* set_iovec_string_field_free(struct iovec *iovec, size_t *n_iovec, const char *field, char *value) {
char *x;

x = set_iovec_string_field(iovec, n_iovec, field, value);
free(value);
return x;
}

struct iovec_wrapper *iovw_new(void) {
return malloc0(sizeof(struct iovec_wrapper));
}
Expand Down Expand Up @@ -429,13 +413,3 @@ int iovw_append(struct iovec_wrapper *target, const struct iovec_wrapper *source
target->count = original_count;
return r;
}

void iovec_array_free(struct iovec *iov, size_t n) {
if (!iov)
return;

for (size_t i = 0; i < n; i++)
free(iov[i].iov_base);

free(iov);
}
45 changes: 0 additions & 45 deletions src/basic/io-util.h
Expand Up @@ -28,38 +28,6 @@ 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 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;
}

static inline bool FILE_SIZE_VALID(uint64_t l) {
/* ftruncate() and friends take an unsigned file size, but actually cannot deal with file sizes larger than
* 2^63 since the kernel internally handles it as signed value. This call allows checking for this early. */
Expand All @@ -78,17 +46,6 @@ static inline bool FILE_SIZE_VALID_OR_INFINITY(uint64_t l) {

}

#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);

struct iovec_wrapper {
struct iovec *iovec;
size_t count;
Expand Down Expand Up @@ -123,5 +80,3 @@ int iovw_put_string_field_free(struct iovec_wrapper *iovw, const char *field, ch
void iovw_rebase(struct iovec_wrapper *iovw, char *old, char *new);
size_t iovw_size(struct iovec_wrapper *iovw);
int iovw_append(struct iovec_wrapper *target, const struct iovec_wrapper *source);

void iovec_array_free(struct iovec *iov, size_t n);
38 changes: 38 additions & 0 deletions src/basic/iovec-util.c
@@ -0,0 +1,38 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include "iovec-util.h"
#include "string-util.h"


char* set_iovec_string_field(struct iovec *iovec, size_t *n_iovec, const char *field, const char *value) {
char *x;

assert(iovec);
assert(n_iovec);

x = strjoin(field, value);
if (x)
iovec[(*n_iovec)++] = IOVEC_MAKE_STRING(x);
return x;
}

char* set_iovec_string_field_free(struct iovec *iovec, size_t *n_iovec, const char *field, char *value) {
char *x;

assert(iovec);
assert(n_iovec);

x = set_iovec_string_field(iovec, n_iovec, field, value);
free(value);
return x;
}

void iovec_array_free(struct iovec *iov, size_t n) {
if (!iov)
return;

for (size_t i = 0; i < n; i++)
free(iov[i].iov_base);

free(iov);
}
53 changes: 53 additions & 0 deletions 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);
2 changes: 1 addition & 1 deletion src/basic/log.c
Expand Up @@ -21,7 +21,7 @@
#include "errno-util.h"
#include "fd-util.h"
#include "format-util.h"
#include "io-util.h"
#include "iovec-util.h"
#include "log.h"
#include "macro.h"
#include "missing_syscall.h"
Expand Down
1 change: 1 addition & 0 deletions src/basic/meson.build
Expand Up @@ -44,6 +44,7 @@ basic_sources = files(
'initrd-util.c',
'inotify-util.c',
'io-util.c',
'iovec-util.c',
'ioprio-util.c',
'label.c',
'limits-util.c',
Expand Down
2 changes: 1 addition & 1 deletion src/core/dbus-execute.c
Expand Up @@ -20,7 +20,7 @@
#include "fd-util.h"
#include "fileio.h"
#include "hexdecoct.h"
#include "io-util.h"
#include "iovec-util.h"
#include "ioprio-util.h"
#include "journal-file.h"
#include "load-fragment.h"
Expand Down
2 changes: 1 addition & 1 deletion src/core/dynamic-user.c
Expand Up @@ -10,7 +10,7 @@
#include "fileio.h"
#include "format-util.h"
#include "fs-util.h"
#include "io-util.h"
#include "iovec-util.h"
#include "lock-util.h"
#include "nscd-flush.h"
#include "parse-util.h"
Expand Down
1 change: 1 addition & 0 deletions src/core/exec-invoke.c
Expand Up @@ -40,6 +40,7 @@
#include "fd-util.h"
#include "hexdecoct.h"
#include "io-util.h"
#include "iovec-util.h"
#include "missing_ioprio.h"
#include "missing_prctl.h"
#include "missing_securebits.h"
Expand Down
2 changes: 1 addition & 1 deletion src/core/load-fragment.c
Expand Up @@ -39,7 +39,7 @@
#include "firewall-util.h"
#include "fs-util.h"
#include "hexdecoct.h"
#include "io-util.h"
#include "iovec-util.h"
#include "ioprio-util.h"
#include "ip-protocol-list.h"
#include "journal-file.h"
Expand Down
2 changes: 1 addition & 1 deletion src/core/show-status.c
Expand Up @@ -6,7 +6,7 @@

#include "alloc-util.h"
#include "fd-util.h"
#include "io-util.h"
#include "iovec-util.h"
#include "parse-util.h"
#include "show-status.h"
#include "string-table.h"
Expand Down
2 changes: 1 addition & 1 deletion src/core/unit.c
Expand Up @@ -33,7 +33,7 @@
#include "format-util.h"
#include "id128-util.h"
#include "install.h"
#include "io-util.h"
#include "iovec-util.h"
#include "label-util.h"
#include "load-dropin.h"
#include "load-fragment.h"
Expand Down
2 changes: 1 addition & 1 deletion src/coredump/coredump.c
Expand Up @@ -29,7 +29,7 @@
#include "fd-util.h"
#include "fileio.h"
#include "fs-util.h"
#include "io-util.h"
#include "iovec-util.h"
#include "journal-importer.h"
#include "journal-send.h"
#include "log.h"
Expand Down
2 changes: 1 addition & 1 deletion src/fuzz/fuzz-varlink.c
Expand Up @@ -6,7 +6,7 @@
#include "fd-util.h"
#include "fuzz.h"
#include "hexdecoct.h"
#include "io-util.h"
#include "iovec-util.h"
#include "varlink.h"
#include "log.h"

Expand Down
2 changes: 1 addition & 1 deletion src/journal/journald-audit.c
Expand Up @@ -7,7 +7,7 @@
#include "errno-util.h"
#include "fd-util.h"
#include "hexdecoct.h"
#include "io-util.h"
#include "iovec-util.h"
#include "journal-internal.h"
#include "journald-audit.h"
#include "missing_audit.h"
Expand Down
2 changes: 1 addition & 1 deletion src/journal/journald-console.c
Expand Up @@ -8,7 +8,7 @@
#include "fd-util.h"
#include "fileio.h"
#include "format-util.h"
#include "io-util.h"
#include "iovec-util.h"
#include "journald-console.h"
#include "journald-server.h"
#include "parse-util.h"
Expand Down
2 changes: 1 addition & 1 deletion src/journal/journald-context.c
Expand Up @@ -11,7 +11,7 @@
#include "fd-util.h"
#include "fileio.h"
#include "fs-util.h"
#include "io-util.h"
#include "iovec-util.h"
#include "journal-internal.h"
#include "journal-util.h"
#include "journald-client.h"
Expand Down
2 changes: 1 addition & 1 deletion src/journal/journald-kmsg.c
Expand Up @@ -15,7 +15,7 @@
#include "fd-util.h"
#include "format-util.h"
#include "fs-util.h"
#include "io-util.h"
#include "iovec-util.h"
#include "journal-internal.h"
#include "journald-kmsg.h"
#include "journald-server.h"
Expand Down
2 changes: 1 addition & 1 deletion src/journal/journald-native.c
Expand Up @@ -9,7 +9,7 @@
#include "alloc-util.h"
#include "fd-util.h"
#include "fs-util.h"
#include "io-util.h"
#include "iovec-util.h"
#include "journal-importer.h"
#include "journal-internal.h"
#include "journal-util.h"
Expand Down
2 changes: 1 addition & 1 deletion src/journal/journald-server.c
Expand Up @@ -28,7 +28,7 @@
#include "hostname-util.h"
#include "id128-util.h"
#include "initrd-util.h"
#include "io-util.h"
#include "iovec-util.h"
#include "journal-authenticate.h"
#include "journal-file-util.h"
#include "journal-internal.h"
Expand Down
2 changes: 1 addition & 1 deletion src/journal/journald-stream.c
Expand Up @@ -18,7 +18,7 @@
#include "fd-util.h"
#include "fileio.h"
#include "fs-util.h"
#include "io-util.h"
#include "iovec-util.h"
#include "journal-internal.h"
#include "journald-client.h"
#include "journald-console.h"
Expand Down
2 changes: 1 addition & 1 deletion src/journal/journald-syslog.c
Expand Up @@ -9,7 +9,7 @@
#include "alloc-util.h"
#include "fd-util.h"
#include "format-util.h"
#include "io-util.h"
#include "iovec-util.h"
#include "journal-internal.h"
#include "journald-client.h"
#include "journald-console.h"
Expand Down
2 changes: 1 addition & 1 deletion src/libsystemd-network/icmp6-util.c
Expand Up @@ -17,7 +17,7 @@
#include "fd-util.h"
#include "icmp6-util.h"
#include "in-addr-util.h"
#include "io-util.h"
#include "iovec-util.h"
#include "socket-util.h"

#define IN6ADDR_ALL_ROUTERS_MULTICAST_INIT \
Expand Down
2 changes: 1 addition & 1 deletion src/libsystemd-network/sd-dhcp-client.c
Expand Up @@ -24,7 +24,7 @@
#include "event-util.h"
#include "fd-util.h"
#include "hostname-util.h"
#include "io-util.h"
#include "iovec-util.h"
#include "memory-util.h"
#include "network-common.h"
#include "random-util.h"
Expand Down
2 changes: 1 addition & 1 deletion src/libsystemd-network/sd-dhcp-server.c
Expand Up @@ -15,7 +15,7 @@
#include "dns-domain.h"
#include "fd-util.h"
#include "in-addr-util.h"
#include "io-util.h"
#include "iovec-util.h"
#include "memory-util.h"
#include "network-common.h"
#include "ordered-set.h"
Expand Down

0 comments on commit f1b27bd

Please sign in to comment.