Merge pull request #29627 from poettering/io-split

split up io-util.[ch], and some clean-ups
This commit is contained in:
Luca Boccassi
2023-10-20 12:19:22 +01:00
committed by GitHub
60 changed files with 339 additions and 269 deletions
+1 -1
View File
@@ -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"
+1 -135
View File
@@ -7,6 +7,7 @@
#include "errno-util.h"
#include "io-util.h"
#include "iovec-util.h"
#include "string-util.h"
#include "time-util.h"
@@ -304,138 +305,3 @@ 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));
}
void iovw_free_contents(struct iovec_wrapper *iovw, bool free_vectors) {
if (free_vectors)
for (size_t i = 0; i < iovw->count; i++)
free(iovw->iovec[i].iov_base);
iovw->iovec = mfree(iovw->iovec);
iovw->count = 0;
}
struct iovec_wrapper *iovw_free_free(struct iovec_wrapper *iovw) {
iovw_free_contents(iovw, true);
return mfree(iovw);
}
struct iovec_wrapper *iovw_free(struct iovec_wrapper *iovw) {
iovw_free_contents(iovw, false);
return mfree(iovw);
}
int iovw_put(struct iovec_wrapper *iovw, void *data, size_t len) {
if (iovw->count >= IOV_MAX)
return -E2BIG;
if (!GREEDY_REALLOC(iovw->iovec, iovw->count + 1))
return -ENOMEM;
iovw->iovec[iovw->count++] = IOVEC_MAKE(data, len);
return 0;
}
int iovw_put_string_field(struct iovec_wrapper *iovw, const char *field, const char *value) {
_cleanup_free_ char *x = NULL;
int r;
x = strjoin(field, value);
if (!x)
return -ENOMEM;
r = iovw_put(iovw, x, strlen(x));
if (r >= 0)
TAKE_PTR(x);
return r;
}
int iovw_put_string_field_free(struct iovec_wrapper *iovw, const char *field, char *value) {
_cleanup_free_ _unused_ char *free_ptr = value;
return iovw_put_string_field(iovw, field, value);
}
void iovw_rebase(struct iovec_wrapper *iovw, char *old, char *new) {
for (size_t i = 0; i < iovw->count; i++)
iovw->iovec[i].iov_base = (char *)iovw->iovec[i].iov_base - old + new;
}
size_t iovw_size(struct iovec_wrapper *iovw) {
size_t n = 0;
for (size_t i = 0; i < iovw->count; i++)
n += iovw->iovec[i].iov_len;
return n;
}
int iovw_append(struct iovec_wrapper *target, const struct iovec_wrapper *source) {
size_t original_count;
int r;
assert(target);
/* This duplicates the source and merges it into the target. */
if (iovw_isempty(source))
return 0;
original_count = target->count;
FOREACH_ARRAY(iovec, source->iovec, source->count) {
void *dup;
dup = memdup(iovec->iov_base, iovec->iov_len);
if (!dup) {
r = -ENOMEM;
goto rollback;
}
r = iovw_consume(target, dup, iovec->iov_len);
if (r < 0)
goto rollback;
}
return 0;
rollback:
for (size_t i = original_count; i < target->count; i++)
free(target->iovec[i].iov_base);
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);
}
-81
View File
@@ -6,7 +6,6 @@
#include <stddef.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/uio.h>
#include "macro.h"
#include "time-util.h"
@@ -28,38 +27,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. */
@@ -77,51 +44,3 @@ static inline bool FILE_SIZE_VALID_OR_INFINITY(uint64_t l) {
return FILE_SIZE_VALID(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;
};
struct iovec_wrapper *iovw_new(void);
struct iovec_wrapper *iovw_free(struct iovec_wrapper *iovw);
struct iovec_wrapper *iovw_free_free(struct iovec_wrapper *iovw);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct iovec_wrapper*, iovw_free_free);
void iovw_free_contents(struct iovec_wrapper *iovw, bool free_vectors);
int iovw_put(struct iovec_wrapper *iovw, void *data, size_t len);
static inline int iovw_consume(struct iovec_wrapper *iovw, void *data, size_t len) {
/* Move data into iovw or free on error */
int r;
r = iovw_put(iovw, data, len);
if (r < 0)
free(data);
return r;
}
static inline bool iovw_isempty(const struct iovec_wrapper *iovw) {
return !iovw || iovw->count == 0;
}
int iovw_put_string_field(struct iovec_wrapper *iovw, const char *field, const char *value);
int iovw_put_string_field_free(struct iovec_wrapper *iovw, const char *field, char *value);
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);
+73
View File
@@ -0,0 +1,73 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "iovec-util.h"
#include "string-util.h"
size_t iovec_total_size(const struct iovec *i, size_t n) {
size_t sum = 0;
assert(i || n == 0);
FOREACH_ARRAY(j, i, n)
sum += j->iov_len;
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;
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);
}
+26
View File
@@ -0,0 +1,26 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include <stdbool.h>
#include <sys/types.h>
#include <sys/uio.h>
#include "macro.h"
size_t iovec_total_size(const struct iovec *i, size_t n);
bool iovec_increment(struct iovec *i, size_t n, size_t k);
#define IOVEC_NULL (const struct iovec) {}
#define IOVEC_MAKE(base, len) (struct iovec) { .iov_base = (base), .iov_len = (len) }
#define IOVEC_MAKE_STRING(string) \
({ \
const char *_s = (string); \
IOVEC_MAKE((char*) _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);
+131
View File
@@ -0,0 +1,131 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "alloc-util.h"
#include "iovec-util.h"
#include "iovec-wrapper.h"
#include "string-util.h"
struct iovec_wrapper *iovw_new(void) {
return new0(struct iovec_wrapper, 1);
}
void iovw_free_contents(struct iovec_wrapper *iovw, bool free_vectors) {
assert(iovw);
if (free_vectors)
for (size_t i = 0; i < iovw->count; i++)
free(iovw->iovec[i].iov_base);
iovw->iovec = mfree(iovw->iovec);
iovw->count = 0;
}
struct iovec_wrapper *iovw_free_free(struct iovec_wrapper *iovw) {
if (!iovw)
return NULL;
iovw_free_contents(iovw, /* free_vectors= */ true);
return mfree(iovw);
}
struct iovec_wrapper *iovw_free(struct iovec_wrapper *iovw) {
if (!iovw)
return NULL;
iovw_free_contents(iovw, /* free_vectors= */ false);
return mfree(iovw);
}
int iovw_put(struct iovec_wrapper *iovw, void *data, size_t len) {
assert(iovw);
if (len == 0)
return 0;
assert(data);
if (iovw->count >= IOV_MAX)
return -E2BIG;
if (!GREEDY_REALLOC(iovw->iovec, iovw->count + 1))
return -ENOMEM;
iovw->iovec[iovw->count++] = IOVEC_MAKE(data, len);
return 0;
}
int iovw_put_string_field(struct iovec_wrapper *iovw, const char *field, const char *value) {
_cleanup_free_ char *x = NULL;
int r;
assert(iovw);
x = strjoin(field, value);
if (!x)
return -ENOMEM;
r = iovw_put(iovw, x, strlen(x));
if (r >= 0)
TAKE_PTR(x);
return r;
}
int iovw_put_string_field_free(struct iovec_wrapper *iovw, const char *field, char *value) {
_cleanup_free_ _unused_ char *free_ptr = value;
return iovw_put_string_field(iovw, field, value);
}
void iovw_rebase(struct iovec_wrapper *iovw, void *old, void *new) {
assert(iovw);
FOREACH_ARRAY(i, iovw->iovec, iovw->count) {
assert(i->iov_base >= old);
i->iov_base = (uint8_t*) i->iov_base - (uint8_t*) old + (uint8_t*) new;
}
}
size_t iovw_size(const struct iovec_wrapper *iovw) {
if (!iovw)
return 0;
return iovec_total_size(iovw->iovec, iovw->count);
}
int iovw_append(struct iovec_wrapper *target, const struct iovec_wrapper *source) {
size_t original_count;
int r;
assert(target);
/* This duplicates the source and merges it into the target. */
if (iovw_isempty(source))
return 0;
original_count = target->count;
FOREACH_ARRAY(iovec, source->iovec, source->count) {
void *dup;
dup = memdup(iovec->iov_base, iovec->iov_len);
if (!dup) {
r = -ENOMEM;
goto rollback;
}
r = iovw_consume(target, dup, iovec->iov_len);
if (r < 0)
goto rollback;
}
return 0;
rollback:
for (size_t i = original_count; i < target->count; i++)
free(target->iovec[i].iov_base);
target->count = original_count;
return r;
}
+42
View File
@@ -0,0 +1,42 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include <sys/types.h>
#include <sys/uio.h>
#include "macro.h"
struct iovec_wrapper {
struct iovec *iovec;
size_t count;
};
struct iovec_wrapper *iovw_new(void);
struct iovec_wrapper *iovw_free(struct iovec_wrapper *iovw);
struct iovec_wrapper *iovw_free_free(struct iovec_wrapper *iovw);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct iovec_wrapper*, iovw_free_free);
void iovw_free_contents(struct iovec_wrapper *iovw, bool free_vectors);
int iovw_put(struct iovec_wrapper *iovw, void *data, size_t len);
static inline int iovw_consume(struct iovec_wrapper *iovw, void *data, size_t len) {
/* Move data into iovw or free on error */
int r;
r = iovw_put(iovw, data, len);
if (r < 0)
free(data);
return r;
}
static inline bool iovw_isempty(const struct iovec_wrapper *iovw) {
return !iovw || iovw->count == 0;
}
int iovw_put_string_field(struct iovec_wrapper *iovw, const char *field, const char *value);
int iovw_put_string_field_free(struct iovec_wrapper *iovw, const char *field, char *value);
void iovw_rebase(struct iovec_wrapper *iovw, void *old, void *new);
size_t iovw_size(const struct iovec_wrapper *iovw);
int iovw_append(struct iovec_wrapper *target, const struct iovec_wrapper *source);
+2 -2
View File
@@ -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"
@@ -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;
}
+2
View File
@@ -45,6 +45,8 @@ basic_sources = files(
'inotify-util.c',
'io-util.c',
'ioprio-util.c',
'iovec-util.c',
'iovec-wrapper.c',
'label.c',
'limits-util.c',
'locale-util.c',