4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include <sys/types.h>
29 #include "time-util.h"
33 ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
34 int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll);
35 int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll);
39 int fd_wait_for_event(int fd, int event, usec_t timeout);
41 ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length);
43 #define IOVEC_SET_STRING(i, s) \
45 struct iovec *_i = &(i); \
46 char *_s = (char *)(s); \
48 _i->iov_len = strlen(_s); \
51 static inline size_t IOVEC_TOTAL_SIZE(const struct iovec *i, unsigned n) {
55 for (j = 0; j < n; j++)
61 static inline size_t IOVEC_INCREMENT(struct iovec *i, unsigned n, size_t k) {
64 for (j = 0; j < n; j++) {
67 if (_unlikely_(k <= 0))
70 sub = MIN(i[j].iov_len, k);
72 i[j].iov_base = (uint8_t*) i[j].iov_base + sub;
79 static inline bool FILE_SIZE_VALID(uint64_t l) {
80 /* ftruncate() and friends take an unsigned file size, but actually cannot deal with file sizes larger than
81 * 2^63 since the kernel internally handles it as signed value. This call allows checking for this early. */
83 return (l >> 63) == 0;
86 static inline bool FILE_SIZE_VALID_OR_INFINITY(uint64_t l) {
88 /* Same as above, but allows one extra value: -1 as indication for infinity. */
90 if (l == (uint64_t) -1)
93 return FILE_SIZE_VALID(l);