1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <poll.h>
5 #include <stdbool.h>
6 #include <stddef.h>
7 #include <stdint.h>
8 #include <sys/types.h>
9 #include <sys/uio.h>
10
11 #include "macro.h"
12 #include "time-util.h"
13
14 int flush_fd(int fd);
15
16 ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
17 int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll);
18 int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll);
19
20 int pipe_eof(int fd);
21
22 int ppoll_usec(struct pollfd *fds, size_t nfds, usec_t timeout);
23 int fd_wait_for_event(int fd, int event, usec_t timeout);
24
25 ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length);
26
IOVEC_TOTAL_SIZE(const struct iovec * i,size_t n)27 static inline size_t IOVEC_TOTAL_SIZE(const struct iovec *i, size_t n) {
28 size_t r = 0;
29
30 for (size_t j = 0; j < n; j++)
31 r += i[j].iov_len;
32
33 return r;
34 }
35
IOVEC_INCREMENT(struct iovec * i,size_t n,size_t k)36 static inline bool IOVEC_INCREMENT(struct iovec *i, size_t n, size_t k) {
37 /* Returns true if there is nothing else to send (bytes written cover all of the iovec),
38 * false if there's still work to do. */
39
40 for (size_t j = 0; j < n; j++) {
41 size_t sub;
42
43 if (i[j].iov_len == 0)
44 continue;
45 if (k == 0)
46 return false;
47
48 sub = MIN(i[j].iov_len, k);
49 i[j].iov_len -= sub;
50 i[j].iov_base = (uint8_t*) i[j].iov_base + sub;
51 k -= sub;
52 }
53
54 assert(k == 0); /* Anything else would mean that we wrote more bytes than available,
55 * or the kernel reported writing more bytes than sent. */
56 return true;
57 }
58
FILE_SIZE_VALID(uint64_t l)59 static inline bool FILE_SIZE_VALID(uint64_t l) {
60 /* ftruncate() and friends take an unsigned file size, but actually cannot deal with file sizes larger than
61 * 2^63 since the kernel internally handles it as signed value. This call allows checking for this early. */
62
63 return (l >> 63) == 0;
64 }
65
FILE_SIZE_VALID_OR_INFINITY(uint64_t l)66 static inline bool FILE_SIZE_VALID_OR_INFINITY(uint64_t l) {
67
68 /* Same as above, but allows one extra value: -1 as indication for infinity. */
69
70 if (l == UINT64_MAX)
71 return true;
72
73 return FILE_SIZE_VALID(l);
74
75 }
76
77 #define IOVEC_INIT(base, len) { .iov_base = (base), .iov_len = (len) }
78 #define IOVEC_MAKE(base, len) (struct iovec) IOVEC_INIT(base, len)
79 #define IOVEC_INIT_STRING(string) IOVEC_INIT((char*) string, strlen(string))
80 #define IOVEC_MAKE_STRING(string) (struct iovec) IOVEC_INIT_STRING(string)
81
82 char* set_iovec_string_field(struct iovec *iovec, size_t *n_iovec, const char *field, const char *value);
83 char* set_iovec_string_field_free(struct iovec *iovec, size_t *n_iovec, const char *field, char *value);
84
85 struct iovec_wrapper {
86 struct iovec *iovec;
87 size_t count;
88 };
89
90 struct iovec_wrapper *iovw_new(void);
91 struct iovec_wrapper *iovw_free(struct iovec_wrapper *iovw);
92 struct iovec_wrapper *iovw_free_free(struct iovec_wrapper *iovw);
93 void iovw_free_contents(struct iovec_wrapper *iovw, bool free_vectors);
94 int iovw_put(struct iovec_wrapper *iovw, void *data, size_t len);
95 int iovw_put_string_field(struct iovec_wrapper *iovw, const char *field, const char *value);
96 int iovw_put_string_field_free(struct iovec_wrapper *iovw, const char *field, char *value);
97 void iovw_rebase(struct iovec_wrapper *iovw, char *old, char *new);
98 size_t iovw_size(struct iovec_wrapper *iovw);
99