]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/io-util.h
Turn VALGRIND variable into a meson configuration switch
[thirdparty/systemd.git] / src / basic / io-util.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5 This file is part of systemd.
6
7 Copyright 2010 Lennart Poettering
8 ***/
9
10 #include <stdbool.h>
11 #include <stddef.h>
12 #include <stdint.h>
13 #include <sys/types.h>
14 #include <sys/uio.h>
15
16 #include "macro.h"
17 #include "time-util.h"
18
19 int flush_fd(int fd);
20
21 ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
22 int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll);
23 int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll);
24
25 int pipe_eof(int fd);
26
27 int fd_wait_for_event(int fd, int event, usec_t timeout);
28
29 ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length);
30
31 static inline size_t IOVEC_TOTAL_SIZE(const struct iovec *i, size_t n) {
32 size_t j, r = 0;
33
34 for (j = 0; j < n; j++)
35 r += i[j].iov_len;
36
37 return r;
38 }
39
40 static inline size_t IOVEC_INCREMENT(struct iovec *i, size_t n, size_t k) {
41 size_t j;
42
43 for (j = 0; j < n; j++) {
44 size_t sub;
45
46 if (_unlikely_(k <= 0))
47 break;
48
49 sub = MIN(i[j].iov_len, k);
50 i[j].iov_len -= sub;
51 i[j].iov_base = (uint8_t*) i[j].iov_base + sub;
52 k -= sub;
53 }
54
55 return k;
56 }
57
58 static inline bool FILE_SIZE_VALID(uint64_t l) {
59 /* ftruncate() and friends take an unsigned file size, but actually cannot deal with file sizes larger than
60 * 2^63 since the kernel internally handles it as signed value. This call allows checking for this early. */
61
62 return (l >> 63) == 0;
63 }
64
65 static inline bool FILE_SIZE_VALID_OR_INFINITY(uint64_t l) {
66
67 /* Same as above, but allows one extra value: -1 as indication for infinity. */
68
69 if (l == (uint64_t) -1)
70 return true;
71
72 return FILE_SIZE_VALID(l);
73
74 }
75
76 #define IOVEC_INIT(base, len) { .iov_base = (base), .iov_len = (len) }
77 #define IOVEC_MAKE(base, len) (struct iovec) IOVEC_INIT(base, len)
78 #define IOVEC_INIT_STRING(string) IOVEC_INIT((char*) string, strlen(string))
79 #define IOVEC_MAKE_STRING(string) (struct iovec) IOVEC_INIT_STRING(string)