src/basic/refcnt.h \
src/basic/util.c \
src/basic/util.h \
+ src/basic/io-util.c \
+ src/basic/io-util.h \
src/basic/string-util.c \
src/basic/string-util.h \
src/basic/fd-util.c \
#include "btrfs-util.h"
#include "copy.h"
#include "fd-util.h"
+#include "io-util.h"
#include "string-util.h"
#include "strv.h"
#include "util.h"
--- /dev/null
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright 2010 Lennart Poettering
+
+ systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <poll.h>
+#include <unistd.h>
+
+#include "io-util.h"
+
+int flush_fd(int fd) {
+ struct pollfd pollfd = {
+ .fd = fd,
+ .events = POLLIN,
+ };
+
+ for (;;) {
+ char buf[LINE_MAX];
+ ssize_t l;
+ int r;
+
+ r = poll(&pollfd, 1, 0);
+ if (r < 0) {
+ if (errno == EINTR)
+ continue;
+
+ return -errno;
+
+ } else if (r == 0)
+ return 0;
+
+ l = read(fd, buf, sizeof(buf));
+ if (l < 0) {
+
+ if (errno == EINTR)
+ continue;
+
+ if (errno == EAGAIN)
+ return 0;
+
+ return -errno;
+ } else if (l == 0)
+ return 0;
+ }
+}
+
+ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) {
+ uint8_t *p = buf;
+ ssize_t n = 0;
+
+ assert(fd >= 0);
+ assert(buf);
+
+ /* 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 = buf;
+
+ assert(fd >= 0);
+ assert(buf);
+
+ if (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) {
+ struct pollfd pollfd = {
+ .fd = fd,
+ .events = POLLIN|POLLHUP,
+ };
+
+ int r;
+
+ r = poll(&pollfd, 1, 0);
+ if (r < 0)
+ return -errno;
+
+ if (r == 0)
+ return 0;
+
+ return pollfd.revents & POLLHUP;
+}
+
+int fd_wait_for_event(int fd, int event, usec_t t) {
+
+ struct pollfd pollfd = {
+ .fd = fd,
+ .events = event,
+ };
+
+ struct timespec ts;
+ int r;
+
+ r = ppoll(&pollfd, 1, t == USEC_INFINITY ? NULL : timespec_store(&ts, t), NULL);
+ if (r < 0)
+ return -errno;
+
+ if (r == 0)
+ return 0;
+
+ 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;
+}
--- /dev/null
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#pragma once
+
+/***
+ This file is part of systemd.
+
+ Copyright 2010 Lennart Poettering
+
+ systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <sys/types.h>
+#include <stdbool.h>
+
+#include "time-util.h"
+
+int flush_fd(int fd);
+
+ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
+int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll);
+int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll);
+
+int pipe_eof(int fd);
+
+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);
#include <time.h>
#include "fd-util.h"
+#include "io-util.h"
#include "missing.h"
#include "random-util.h"
#include "time-util.h"
#include "fd-util.h"
#include "fileio.h"
+#include "io-util.h"
#include "path-util.h"
#include "process-util.h"
#include "string-util.h"
return nulstr_contains(table, fstype);
}
-int flush_fd(int fd) {
- struct pollfd pollfd = {
- .fd = fd,
- .events = POLLIN,
- };
-
- for (;;) {
- char buf[LINE_MAX];
- ssize_t l;
- int r;
-
- r = poll(&pollfd, 1, 0);
- if (r < 0) {
- if (errno == EINTR)
- continue;
-
- return -errno;
-
- } else if (r == 0)
- return 0;
-
- l = read(fd, buf, sizeof(buf));
- if (l < 0) {
-
- if (errno == EINTR)
- continue;
-
- if (errno == EAGAIN)
- return 0;
-
- return -errno;
- } else if (l == 0)
- return 0;
- }
-}
-
-ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) {
- uint8_t *p = buf;
- ssize_t n = 0;
-
- assert(fd >= 0);
- assert(buf);
-
- /* 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 = buf;
-
- assert(fd >= 0);
- assert(buf);
-
- if (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 parse_size(const char *t, uint64_t base, uint64_t *size) {
/* Soo, sometimes we want to parse IEC binary suffixes, and
return access("/run/plymouth/pid", F_OK) >= 0;
}
-int pipe_eof(int fd) {
- struct pollfd pollfd = {
- .fd = fd,
- .events = POLLIN|POLLHUP,
- };
-
- int r;
-
- r = poll(&pollfd, 1, 0);
- if (r < 0)
- return -errno;
-
- if (r == 0)
- return 0;
-
- return pollfd.revents & POLLHUP;
-}
-
-int fd_wait_for_event(int fd, int event, usec_t t) {
-
- struct pollfd pollfd = {
- .fd = fd,
- .events = event,
- };
-
- struct timespec ts;
- int r;
-
- r = ppoll(&pollfd, 1, t == USEC_INFINITY ? NULL : timespec_store(&ts, t), NULL);
- if (r < 0)
- return -errno;
-
- if (r == 0)
- return 0;
-
- return pollfd.revents;
-}
-
int fopen_temporary(const char *path, FILE **_f, char **_temp_path) {
FILE *f;
char *t;
return read_attr_fd(fd, ret);
}
-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;
-}
-
void sigkill_wait(pid_t *pid) {
if (!pid)
return;
bool fstype_is_network(const char *fstype);
-int flush_fd(int fd);
-
int fopen_temporary(const char *path, FILE **_f, char **_temp_path);
-ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
-int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll);
-int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll);
-
bool is_device_path(const char *path);
int dir_is_empty(const char *path);
bool is_temporary_fs(const struct statfs *s) _pure_;
int fd_is_temporary_fs(int fd);
-int pipe_eof(int fd);
-
#define xsprintf(buf, fmt, ...) \
assert_message_se((size_t) snprintf(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__) < ELEMENTSOF(buf), \
"xsprintf: " #buf "[] must be big enough")
char *format_bytes(char *buf, size_t l, uint64_t t);
-int fd_wait_for_event(int fd, int event, usec_t timeout);
-
void* memdup(const void *p, size_t l) _alloc_(2);
int fd_inc_sndbuf(int fd, size_t n);
int getpeercred(int fd, struct ucred *ucred);
int getpeersec(int fd, char **ret);
-int writev_safe(int fd, const struct iovec *w, int j);
-
int mkostemp_safe(char *pattern, int flags);
int open_tmpfile(const char *path, int flags);
#define RLIMIT_MAKE_CONST(lim) ((struct rlimit) { lim, lim })
-ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length);
-
void sigkill_wait(pid_t *pid);
#define _cleanup_sigkill_wait_ _cleanup_(sigkill_wait)
#include "conf-parser.h"
#include "fd-util.h"
#include "fileio.h"
+#include "io-util.h"
#include "list.h"
#include "macro.h"
#include "path-util.h"
#include "dbus-automount.h"
#include "fd-util.h"
#include "formats-util.h"
+#include "io-util.h"
#include "label.h"
#include "mkdir.h"
#include "mount.h"
#include "fd-util.h"
#include "fileio.h"
#include "formats-util.h"
+#include "io-util.h"
#include "ioprio.h"
#include "log.h"
#include "macro.h"
#include "fd-util.h"
#include "fileio.h"
+#include "io-util.h"
#include "log.h"
#include "machine-id-setup.h"
#include "macro.h"
#include "exit-status.h"
#include "fd-util.h"
#include "hashmap.h"
+#include "io-util.h"
#include "locale-setup.h"
#include "log.h"
#include "macro.h"
#include "import-common.h"
#include "import-compress.h"
#include "import-raw.h"
+#include "io-util.h"
#include "machine-pool.h"
#include "mkdir.h"
#include "path-util.h"
#include "import-common.h"
#include "import-compress.h"
#include "import-tar.h"
+#include "io-util.h"
#include "machine-pool.h"
#include "mkdir.h"
#include "path-util.h"
#include "copy.h"
#include "escape.h"
#include "fd-util.h"
+#include "io-util.h"
#include "process-util.h"
#include "pull-common.h"
#include "pull-job.h"
#include <sys/xattr.h>
#include "fd-util.h"
+#include "io-util.h"
#include "machine-pool.h"
#include "pull-job.h"
#include "string-util.h"
#include "compress.h"
#include "fd-util.h"
+#include "io-util.h"
#include "journal-def.h"
#include "macro.h"
#include "sparse-endian.h"
#include "sd-journal.h"
#include "fd-util.h"
+#include "io-util.h"
#include "memfd-util.h"
#include "socket-util.h"
#include "string-util.h"
#include "fileio.h"
#include "fsprg.h"
#include "hostname-util.h"
+#include "io-util.h"
#include "journal-def.h"
#include "journal-internal.h"
#include "journal-qrcode.h"
#include <unistd.h>
#include "sd-journal.h"
+
#include "catalog.h"
#include "compress.h"
#include "fd-util.h"
#include "formats-util.h"
#include "hashmap.h"
#include "hostname-util.h"
+#include "io-util.h"
#include "journal-def.h"
#include "journal-file.h"
#include "journal-internal.h"
#include "sd-id128.h"
+#include "fd-util.h"
+#include "io-util.h"
#include "macro.h"
#include "random-util.h"
#include "util.h"
-#include "fd-util.h"
_public_ char *sd_id128_to_string(sd_id128_t id, char s[SD_ID128_STRING_MAX]) {
unsigned n;
#include "fileio.h"
#include "formats-util.h"
#include "hostname-util.h"
+#include "io-util.h"
#include "login-util.h"
#include "macro.h"
#include "string-util.h"
#include "sd-resolve.h"
#include "fd-util.h"
+#include "io-util.h"
#include "list.h"
#include "missing.h"
#include "resolve-util.h"
#include <sys/inotify.h>
#include "fd-util.h"
+#include "io-util.h"
#include "libudev-private.h"
/**
#include "bus-util.h"
#include "escape.h"
#include "fd-util.h"
+#include "fd-util.h"
#include "fileio.h"
#include "formats-util.h"
+#include "io-util.h"
#include "logind-session.h"
#include "mkdir.h"
#include "path-util.h"
#include <unistd.h>
#include "fd-util.h"
+#include "io-util.h"
#include "log.h"
#include "mkdir.h"
#include "string-util.h"
#include "fd-util.h"
#include "fileio-label.h"
#include "hostname-util.h"
+#include "io-util.h"
#include "netlink-util.h"
#include "network-internal.h"
#include "ordered-set.h"
#include "escape.h"
#include "fd-util.h"
#include "fileio.h"
+#include "io-util.h"
#include "mkdir.h"
#include "string-util.h"
#include "udev-util.h"
#include "ask-password-api.h"
#include "fd-util.h"
#include "formats-util.h"
+#include "io-util.h"
#include "missing.h"
#include "mkdir.h"
#include "random-util.h"
#include "efivars.h"
#include "fd-util.h"
+#include "io-util.h"
#include "utf8.h"
#include "util.h"
#include "virt.h"
#include "formats-util.h"
#include "hashmap.h"
#include "hostname-util.h"
+#include "io-util.h"
#include "journal-internal.h"
#include "log.h"
#include "logs-show.h"
#include <errno.h>
#include <poll.h>
+#include "fd-util.h"
+#include "io-util.h"
#include "log.h"
-#include "util.h"
#include "process-util.h"
#include "spawn-polkit-agent.h"
-#include "fd-util.h"
+#include "util.h"
#ifdef ENABLE_POLKIT
static pid_t agent_pid = 0;
#include "hostname-util.h"
#include "initreq.h"
#include "install.h"
+#include "io-util.h"
#include "list.h"
#include "log.h"
#include "logs-show.h"
#include "cpu-set-util.h"
#include "def.h"
#include "escape.h"
+#include "fd-util.h"
#include "fileio.h"
+#include "io-util.h"
#include "mkdir.h"
#include "process-util.h"
#include "rm-rf.h"
#include "strv.h"
#include "util.h"
#include "virt.h"
-#include "fd-util.h"
static void test_streq_ptr(void) {
assert_se(streq_ptr(NULL, NULL));
#include "escape.h"
#include "fd-util.h"
#include "formats-util.h"
+#include "io-util.h"
#include "label.h"
#include "log.h"
#include "macro.h"
#include "conf-parser.h"
#include "def.h"
#include "fd-util.h"
+#include "io-util.h"
#include "mkdir.h"
#include "path-util.h"
#include "process-util.h"
#include "fileio.h"
#include "formats-util.h"
#include "hashmap.h"
+#include "io-util.h"
#include "netlink-util.h"
#include "process-util.h"
#include "selinux-util.h"
***/
#include "fd-util.h"
+#include "io-util.h"
#include "selinux-util.h"
#include "util.h"
#include "fd-util.h"
#include "fileio.h"
+#include "io-util.h"
#include "log.h"
#include "process-util.h"
#include "signal-util.h"