mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-02-04 00:10:31 +01:00
This is a direct dump from systemd git.
$ git clean -fdx && \
git cat-file -p HEAD | sed '1,/^======$/ d' | bash - && \
git add .
======
SYSTEMD_DIR=../systemd
COMMIT=9eba03c7b10fe98e0443508402b3f9804230453e
(
cd "$SYSTEMD_DIR"
git checkout "$COMMIT"
git reset --hard
git clean -fdx
)
git ls-files -z :/src/libnm-systemd-core/src/ \
:/src/libnm-systemd-shared/src/ \
:/src/libnm-std-aux/unaligned-fundamental.h \
:/src/libnm-std-aux/unaligned.h | \
xargs -0 rm -f
nm_copy_sd_shared() {
mkdir -p "./src/libnm-systemd-shared/$(dirname "$1")"
cp "$SYSTEMD_DIR/$1" "./src/libnm-systemd-shared/$1"
}
nm_copy_sd_core() {
mkdir -p "./src/libnm-systemd-core/$(dirname "$1")"
cp "$SYSTEMD_DIR/$1" "./src/libnm-systemd-core/$1"
}
nm_copy_sd_stdaux() {
mkdir -p "./src/libnm-std-aux/"
cp "$SYSTEMD_DIR/$1" "./src/libnm-std-aux/${1##*/}"
}
nm_copy_sd_core "src/libsystemd-network/dhcp-identifier.c"
nm_copy_sd_core "src/libsystemd-network/dhcp-identifier.h"
nm_copy_sd_core "src/libsystemd-network/dhcp6-internal.h"
nm_copy_sd_core "src/libsystemd-network/dhcp6-lease-internal.h"
nm_copy_sd_core "src/libsystemd-network/dhcp6-network.c"
nm_copy_sd_core "src/libsystemd-network/dhcp6-option.c"
nm_copy_sd_core "src/libsystemd-network/dhcp6-option.h"
nm_copy_sd_core "src/libsystemd-network/dhcp6-protocol.c"
nm_copy_sd_core "src/libsystemd-network/dhcp6-protocol.h"
nm_copy_sd_core "src/libsystemd-network/network-common.c"
nm_copy_sd_core "src/libsystemd-network/network-common.h"
nm_copy_sd_core "src/libsystemd-network/network-internal.h"
nm_copy_sd_core "src/libsystemd-network/sd-dhcp6-client.c"
nm_copy_sd_core "src/libsystemd-network/sd-dhcp6-lease.c"
nm_copy_sd_core "src/libsystemd/sd-device/device-util.h"
nm_copy_sd_core "src/libsystemd/sd-event/event-source.h"
nm_copy_sd_core "src/libsystemd/sd-event/event-util.c"
nm_copy_sd_core "src/libsystemd/sd-event/event-util.h"
nm_copy_sd_core "src/libsystemd/sd-event/sd-event.c"
nm_copy_sd_core "src/libsystemd/sd-id128/id128-util.c"
nm_copy_sd_core "src/libsystemd/sd-id128/id128-util.h"
nm_copy_sd_core "src/libsystemd/sd-id128/sd-id128.c"
nm_copy_sd_core "src/systemd/_sd-common.h"
nm_copy_sd_core "src/systemd/sd-device.h"
nm_copy_sd_core "src/systemd/sd-dhcp6-client.h"
nm_copy_sd_core "src/systemd/sd-dhcp6-lease.h"
nm_copy_sd_core "src/systemd/sd-dhcp6-option.h"
nm_copy_sd_core "src/systemd/sd-event.h"
nm_copy_sd_core "src/systemd/sd-id128.h"
nm_copy_sd_core "src/systemd/sd-ndisc.h"
nm_copy_sd_shared "src/basic/alloc-util.c"
nm_copy_sd_shared "src/basic/alloc-util.h"
nm_copy_sd_shared "src/basic/async.h"
nm_copy_sd_shared "src/basic/cgroup-util.h"
nm_copy_sd_shared "src/basic/constants.h"
nm_copy_sd_shared "src/basic/dns-def.h"
nm_copy_sd_shared "src/basic/env-file.c"
nm_copy_sd_shared "src/basic/env-file.h"
nm_copy_sd_shared "src/basic/env-util.c"
nm_copy_sd_shared "src/basic/env-util.h"
nm_copy_sd_shared "src/basic/errno-util.h"
nm_copy_sd_shared "src/basic/escape.c"
nm_copy_sd_shared "src/basic/escape.h"
nm_copy_sd_shared "src/basic/ether-addr-util.c"
nm_copy_sd_shared "src/basic/ether-addr-util.h"
nm_copy_sd_shared "src/basic/extract-word.c"
nm_copy_sd_shared "src/basic/extract-word.h"
nm_copy_sd_shared "src/basic/fd-util.c"
nm_copy_sd_shared "src/basic/fd-util.h"
nm_copy_sd_shared "src/basic/fileio.c"
nm_copy_sd_shared "src/basic/fileio.h"
nm_copy_sd_shared "src/basic/format-util.c"
nm_copy_sd_shared "src/basic/format-util.h"
nm_copy_sd_shared "src/basic/fs-util.c"
nm_copy_sd_shared "src/basic/fs-util.h"
nm_copy_sd_shared "src/basic/glyph-util.c"
nm_copy_sd_shared "src/basic/glyph-util.h"
nm_copy_sd_shared "src/basic/hash-funcs.c"
nm_copy_sd_shared "src/basic/hash-funcs.h"
nm_copy_sd_shared "src/basic/hashmap.c"
nm_copy_sd_shared "src/basic/hashmap.h"
nm_copy_sd_shared "src/basic/hexdecoct.c"
nm_copy_sd_shared "src/basic/hexdecoct.h"
nm_copy_sd_shared "src/basic/hostname-util.c"
nm_copy_sd_shared "src/basic/hostname-util.h"
nm_copy_sd_shared "src/basic/in-addr-util.c"
nm_copy_sd_shared "src/basic/in-addr-util.h"
nm_copy_sd_shared "src/basic/inotify-util.c"
nm_copy_sd_shared "src/basic/inotify-util.h"
nm_copy_sd_shared "src/basic/io-util.c"
nm_copy_sd_shared "src/basic/io-util.h"
nm_copy_sd_shared "src/basic/list.h"
nm_copy_sd_shared "src/basic/locale-util.c"
nm_copy_sd_shared "src/basic/locale-util.h"
nm_copy_sd_shared "src/basic/log.h"
nm_copy_sd_shared "src/basic/logarithm.h"
nm_copy_sd_shared "src/basic/macro.h"
nm_copy_sd_shared "src/basic/memory-util.c"
nm_copy_sd_shared "src/basic/memory-util.h"
nm_copy_sd_shared "src/basic/mempool.c"
nm_copy_sd_shared "src/basic/mempool.h"
nm_copy_sd_shared "src/basic/missing_fcntl.h"
nm_copy_sd_shared "src/basic/missing_random.h"
nm_copy_sd_shared "src/basic/missing_socket.h"
nm_copy_sd_shared "src/basic/missing_stat.h"
nm_copy_sd_shared "src/basic/missing_syscall.h"
nm_copy_sd_shared "src/basic/missing_type.h"
nm_copy_sd_shared "src/basic/ordered-set.c"
nm_copy_sd_shared "src/basic/ordered-set.h"
nm_copy_sd_shared "src/basic/parse-util.c"
nm_copy_sd_shared "src/basic/parse-util.h"
nm_copy_sd_shared "src/basic/path-util.c"
nm_copy_sd_shared "src/basic/path-util.h"
nm_copy_sd_shared "src/basic/prioq.c"
nm_copy_sd_shared "src/basic/prioq.h"
nm_copy_sd_shared "src/basic/process-util.c"
nm_copy_sd_shared "src/basic/process-util.h"
nm_copy_sd_shared "src/basic/random-util.c"
nm_copy_sd_shared "src/basic/random-util.h"
nm_copy_sd_shared "src/basic/ratelimit.c"
nm_copy_sd_shared "src/basic/ratelimit.h"
nm_copy_sd_shared "src/basic/set.h"
nm_copy_sd_shared "src/basic/signal-util.c"
nm_copy_sd_shared "src/basic/signal-util.h"
nm_copy_sd_shared "src/basic/siphash24.h"
nm_copy_sd_shared "src/basic/socket-util.c"
nm_copy_sd_shared "src/basic/socket-util.h"
nm_copy_sd_shared "src/basic/sort-util.h"
nm_copy_sd_shared "src/basic/sparse-endian.h"
nm_copy_sd_shared "src/basic/stat-util.c"
nm_copy_sd_shared "src/basic/stat-util.h"
nm_copy_sd_shared "src/basic/stdio-util.h"
nm_copy_sd_shared "src/basic/string-table.c"
nm_copy_sd_shared "src/basic/string-table.h"
nm_copy_sd_shared "src/basic/string-util.c"
nm_copy_sd_shared "src/basic/string-util.h"
nm_copy_sd_shared "src/basic/strv.c"
nm_copy_sd_shared "src/basic/strv.h"
nm_copy_sd_shared "src/basic/strxcpyx.c"
nm_copy_sd_shared "src/basic/strxcpyx.h"
nm_copy_sd_shared "src/basic/time-util.c"
nm_copy_sd_shared "src/basic/time-util.h"
nm_copy_sd_shared "src/basic/tmpfile-util.c"
nm_copy_sd_shared "src/basic/tmpfile-util.h"
nm_copy_sd_shared "src/basic/umask-util.h"
nm_copy_sd_shared "src/basic/user-util.h"
nm_copy_sd_shared "src/basic/utf8.c"
nm_copy_sd_shared "src/basic/utf8.h"
nm_copy_sd_shared "src/fundamental/macro-fundamental.h"
nm_copy_sd_shared "src/fundamental/memory-util-fundamental.h"
nm_copy_sd_shared "src/fundamental/sha256.c"
nm_copy_sd_shared "src/fundamental/sha256.h"
nm_copy_sd_shared "src/fundamental/string-util-fundamental.c"
nm_copy_sd_shared "src/fundamental/string-util-fundamental.h"
nm_copy_sd_shared "src/shared/dns-domain.c"
nm_copy_sd_shared "src/shared/dns-domain.h"
nm_copy_sd_shared "src/shared/log-link.h"
nm_copy_sd_shared "src/shared/web-util.c"
nm_copy_sd_shared "src/shared/web-util.h"
nm_copy_sd_stdaux "src/basic/unaligned.h"
nm_copy_sd_stdaux "src/fundamental/unaligned-fundamental.h"
364 lines
10 KiB
C
364 lines
10 KiB
C
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
|
|
|
#include <errno.h>
|
|
#include <limits.h>
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
|
|
#include "io-util.h"
|
|
#include "string-util.h"
|
|
#include "time-util.h"
|
|
|
|
int flush_fd(int fd) {
|
|
int count = 0;
|
|
|
|
/* Read from the specified file descriptor, until POLLIN is not set anymore, throwing away everything
|
|
* read. Note that some file descriptors (notable IP sockets) will trigger POLLIN even when no data can be read
|
|
* (due to IP packet checksum mismatches), hence this function is only safe to be non-blocking if the fd used
|
|
* was set to non-blocking too. */
|
|
|
|
for (;;) {
|
|
char buf[LINE_MAX];
|
|
ssize_t l;
|
|
int r;
|
|
|
|
r = fd_wait_for_event(fd, POLLIN, 0);
|
|
if (r < 0) {
|
|
if (r == -EINTR)
|
|
continue;
|
|
|
|
return r;
|
|
}
|
|
if (r == 0)
|
|
return count;
|
|
|
|
l = read(fd, buf, sizeof(buf));
|
|
if (l < 0) {
|
|
if (errno == EINTR)
|
|
continue;
|
|
|
|
if (errno == EAGAIN)
|
|
return count;
|
|
|
|
return -errno;
|
|
} else if (l == 0)
|
|
return count;
|
|
|
|
count += (int) l;
|
|
}
|
|
}
|
|
|
|
ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) {
|
|
uint8_t *p = ASSERT_PTR(buf);
|
|
ssize_t n = 0;
|
|
|
|
assert(fd >= 0);
|
|
|
|
/* If called with nbytes == 0, let's call read() at least
|
|
* once, to validate the operation */
|
|
|
|
if (nbytes > (size_t) SSIZE_MAX)
|
|
return -EINVAL;
|
|
|
|
do {
|
|
ssize_t k;
|
|
|
|
k = read(fd, p, nbytes);
|
|
if (k < 0) {
|
|
if (errno == EINTR)
|
|
continue;
|
|
|
|
if (errno == EAGAIN && do_poll) {
|
|
|
|
/* We knowingly ignore any return value here,
|
|
* and expect that any error/EOF is reported
|
|
* via read() */
|
|
|
|
(void) fd_wait_for_event(fd, POLLIN, USEC_INFINITY);
|
|
continue;
|
|
}
|
|
|
|
return n > 0 ? n : -errno;
|
|
}
|
|
|
|
if (k == 0)
|
|
return n;
|
|
|
|
assert((size_t) k <= nbytes);
|
|
|
|
p += k;
|
|
nbytes -= k;
|
|
n += k;
|
|
} while (nbytes > 0);
|
|
|
|
return n;
|
|
}
|
|
|
|
int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll) {
|
|
ssize_t n;
|
|
|
|
n = loop_read(fd, buf, nbytes, do_poll);
|
|
if (n < 0)
|
|
return (int) n;
|
|
if ((size_t) n != nbytes)
|
|
return -EIO;
|
|
|
|
return 0;
|
|
}
|
|
|
|
int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) {
|
|
const uint8_t *p = ASSERT_PTR(buf);
|
|
|
|
assert(fd >= 0);
|
|
|
|
if (_unlikely_(nbytes > (size_t) SSIZE_MAX))
|
|
return -EINVAL;
|
|
|
|
do {
|
|
ssize_t k;
|
|
|
|
k = write(fd, p, nbytes);
|
|
if (k < 0) {
|
|
if (errno == EINTR)
|
|
continue;
|
|
|
|
if (errno == EAGAIN && do_poll) {
|
|
/* We knowingly ignore any return value here,
|
|
* and expect that any error/EOF is reported
|
|
* via write() */
|
|
|
|
(void) fd_wait_for_event(fd, POLLOUT, USEC_INFINITY);
|
|
continue;
|
|
}
|
|
|
|
return -errno;
|
|
}
|
|
|
|
if (_unlikely_(nbytes > 0 && k == 0)) /* Can't really happen */
|
|
return -EIO;
|
|
|
|
assert((size_t) k <= nbytes);
|
|
|
|
p += k;
|
|
nbytes -= k;
|
|
} while (nbytes > 0);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int pipe_eof(int fd) {
|
|
int r;
|
|
|
|
r = fd_wait_for_event(fd, POLLIN, 0);
|
|
if (r <= 0)
|
|
return r;
|
|
|
|
return !!(r & POLLHUP);
|
|
}
|
|
|
|
int ppoll_usec(struct pollfd *fds, size_t nfds, usec_t timeout) {
|
|
int r;
|
|
|
|
assert(fds || nfds == 0);
|
|
|
|
/* This is a wrapper around ppoll() that does primarily two things:
|
|
*
|
|
* ✅ Takes a usec_t instead of a struct timespec
|
|
*
|
|
* ✅ Guarantees that if an invalid fd is specified we return EBADF (i.e. converts POLLNVAL to
|
|
* EBADF). This is done because EBADF is a programming error usually, and hence should bubble up
|
|
* as error, and not be eaten up as non-error POLLNVAL event.
|
|
*
|
|
* ⚠️ ⚠️ ⚠️ Note that this function does not add any special handling for EINTR. Don't forget
|
|
* poll()/ppoll() will return with EINTR on any received signal always, there is no automatic
|
|
* restarting via SA_RESTART available. Thus, typically you want to handle EINTR not as an error,
|
|
* but just as reason to restart things, under the assumption you use a more appropriate mechanism
|
|
* to handle signals, such as signalfd() or signal handlers. ⚠️ ⚠️ ⚠️
|
|
*/
|
|
|
|
if (nfds == 0)
|
|
return 0;
|
|
|
|
r = ppoll(fds, nfds, timeout == USEC_INFINITY ? NULL : TIMESPEC_STORE(timeout), NULL);
|
|
if (r < 0)
|
|
return -errno;
|
|
if (r == 0)
|
|
return 0;
|
|
|
|
for (size_t i = 0, n = r; i < nfds && n > 0; i++) {
|
|
if (fds[i].revents == 0)
|
|
continue;
|
|
if (fds[i].revents & POLLNVAL)
|
|
return -EBADF;
|
|
n--;
|
|
}
|
|
|
|
return r;
|
|
}
|
|
|
|
int fd_wait_for_event(int fd, int event, usec_t timeout) {
|
|
struct pollfd pollfd = {
|
|
.fd = fd,
|
|
.events = event,
|
|
};
|
|
int r;
|
|
|
|
/* ⚠️ ⚠️ ⚠️ Keep in mind you almost certainly want to handle -EINTR gracefully in the caller, see
|
|
* ppoll_usec() above! ⚠️ ⚠️ ⚠️ */
|
|
|
|
r = ppoll_usec(&pollfd, 1, timeout);
|
|
if (r <= 0)
|
|
return r;
|
|
|
|
return pollfd.revents;
|
|
}
|
|
|
|
static size_t nul_length(const uint8_t *p, size_t sz) {
|
|
size_t n = 0;
|
|
|
|
while (sz > 0) {
|
|
if (*p != 0)
|
|
break;
|
|
|
|
n++;
|
|
p++;
|
|
sz--;
|
|
}
|
|
|
|
return n;
|
|
}
|
|
|
|
ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length) {
|
|
const uint8_t *q, *w, *e;
|
|
ssize_t l;
|
|
|
|
q = w = p;
|
|
e = q + sz;
|
|
while (q < e) {
|
|
size_t n;
|
|
|
|
n = nul_length(q, e - q);
|
|
|
|
/* If there are more than the specified run length of
|
|
* NUL bytes, or if this is the beginning or the end
|
|
* of the buffer, then seek instead of write */
|
|
if ((n > run_length) ||
|
|
(n > 0 && q == p) ||
|
|
(n > 0 && q + n >= e)) {
|
|
if (q > w) {
|
|
l = write(fd, w, q - w);
|
|
if (l < 0)
|
|
return -errno;
|
|
if (l != q -w)
|
|
return -EIO;
|
|
}
|
|
|
|
if (lseek(fd, n, SEEK_CUR) == (off_t) -1)
|
|
return -errno;
|
|
|
|
q += n;
|
|
w = q;
|
|
} else if (n > 0)
|
|
q += n;
|
|
else
|
|
q++;
|
|
}
|
|
|
|
if (q > w) {
|
|
l = write(fd, w, q - w);
|
|
if (l < 0)
|
|
return -errno;
|
|
if (l != q - w)
|
|
return -EIO;
|
|
}
|
|
|
|
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;
|
|
}
|