]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/io-util.c
Merge pull request #28301 from berrange/cvm-lockdown
[thirdparty/systemd.git] / src / basic / io-util.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
c004493c 2
11c3a366
TA
3#include <errno.h>
4#include <limits.h>
11c3a366 5#include <stdio.h>
c004493c
LP
6#include <unistd.h>
7
8#include "io-util.h"
084eeb86 9#include "string-util.h"
11c3a366 10#include "time-util.h"
c004493c
LP
11
12int flush_fd(int fd) {
665dfe93 13 int count = 0;
c004493c 14
60d9771c
LP
15 /* Read from the specified file descriptor, until POLLIN is not set anymore, throwing away everything
16 * read. Note that some file descriptors (notable IP sockets) will trigger POLLIN even when no data can be read
17 * (due to IP packet checksum mismatches), hence this function is only safe to be non-blocking if the fd used
18 * was set to non-blocking too. */
19
c004493c
LP
20 for (;;) {
21 char buf[LINE_MAX];
22 ssize_t l;
23 int r;
24
0f2d351f 25 r = fd_wait_for_event(fd, POLLIN, 0);
c004493c 26 if (r < 0) {
0f2d351f 27 if (r == -EINTR)
c004493c
LP
28 continue;
29
0f2d351f 30 return r;
dad28bff
LP
31 }
32 if (r == 0)
665dfe93 33 return count;
c004493c
LP
34
35 l = read(fd, buf, sizeof(buf));
36 if (l < 0) {
c004493c
LP
37 if (errno == EINTR)
38 continue;
39
40 if (errno == EAGAIN)
665dfe93 41 return count;
c004493c
LP
42
43 return -errno;
44 } else if (l == 0)
665dfe93
LP
45 return count;
46
47 count += (int) l;
c004493c
LP
48 }
49}
50
51ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) {
99534007 52 uint8_t *p = ASSERT_PTR(buf);
c004493c
LP
53 ssize_t n = 0;
54
55 assert(fd >= 0);
c004493c 56
a73c74db 57 /* If called with nbytes == 0, let's call read() at least once, to validate the operation */
c004493c
LP
58
59 if (nbytes > (size_t) SSIZE_MAX)
60 return -EINVAL;
61
62 do {
63 ssize_t k;
64
65 k = read(fd, p, nbytes);
66 if (k < 0) {
67 if (errno == EINTR)
68 continue;
69
70 if (errno == EAGAIN && do_poll) {
71
72 /* We knowingly ignore any return value here,
73 * and expect that any error/EOF is reported
74 * via read() */
75
76 (void) fd_wait_for_event(fd, POLLIN, USEC_INFINITY);
77 continue;
78 }
79
80 return n > 0 ? n : -errno;
81 }
82
83 if (k == 0)
84 return n;
85
86 assert((size_t) k <= nbytes);
87
88 p += k;
89 nbytes -= k;
90 n += k;
91 } while (nbytes > 0);
92
93 return n;
94}
95
96int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll) {
97 ssize_t n;
98
99 n = loop_read(fd, buf, nbytes, do_poll);
100 if (n < 0)
101 return (int) n;
102 if ((size_t) n != nbytes)
103 return -EIO;
104
105 return 0;
106}
107
108int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) {
d89457a1 109 const uint8_t *p;
c004493c
LP
110
111 assert(fd >= 0);
c004493c 112
d89457a1
LP
113 if (nbytes == 0) {
114 static const dummy_t dummy[0];
115 assert_cc(sizeof(dummy) == 0);
116 p = (const void*) dummy; /* Some valid pointer, in case NULL was specified */
117 } else {
118 assert(buf);
119
120 if (nbytes == SIZE_MAX)
121 nbytes = strlen(buf);
122 else if (_unlikely_(nbytes > (size_t) SSIZE_MAX))
123 return -EINVAL;
124
125 p = buf;
126 }
c004493c
LP
127
128 do {
129 ssize_t k;
130
131 k = write(fd, p, nbytes);
132 if (k < 0) {
133 if (errno == EINTR)
134 continue;
135
136 if (errno == EAGAIN && do_poll) {
137 /* We knowingly ignore any return value here,
138 * and expect that any error/EOF is reported
139 * via write() */
140
141 (void) fd_wait_for_event(fd, POLLOUT, USEC_INFINITY);
142 continue;
143 }
144
145 return -errno;
146 }
147
148 if (_unlikely_(nbytes > 0 && k == 0)) /* Can't really happen */
149 return -EIO;
150
151 assert((size_t) k <= nbytes);
152
153 p += k;
154 nbytes -= k;
155 } while (nbytes > 0);
156
157 return 0;
158}
159
160int pipe_eof(int fd) {
c004493c
LP
161 int r;
162
0f2d351f 163 r = fd_wait_for_event(fd, POLLIN, 0);
60d7a202 164 if (r <= 0)
0f2d351f 165 return r;
c004493c 166
0f2d351f 167 return !!(r & POLLHUP);
c004493c
LP
168}
169
c4febde9 170int ppoll_usec(struct pollfd *fds, size_t nfds, usec_t timeout) {
c004493c
LP
171 int r;
172
c4febde9
YW
173 assert(fds || nfds == 0);
174
ffbcc8d4
LP
175 /* This is a wrapper around ppoll() that does primarily two things:
176 *
177 * ✅ Takes a usec_t instead of a struct timespec
178 *
179 * ✅ Guarantees that if an invalid fd is specified we return EBADF (i.e. converts POLLNVAL to
180 * EBADF). This is done because EBADF is a programming error usually, and hence should bubble up
181 * as error, and not be eaten up as non-error POLLNVAL event.
182 *
183 * ⚠️ ⚠️ ⚠️ Note that this function does not add any special handling for EINTR. Don't forget
184 * poll()/ppoll() will return with EINTR on any received signal always, there is no automatic
185 * restarting via SA_RESTART available. Thus, typically you want to handle EINTR not as an error,
186 * but just as reason to restart things, under the assumption you use a more appropriate mechanism
187 * to handle signals, such as signalfd() or signal handlers. ⚠️ ⚠️ ⚠️
188 */
189
c4febde9
YW
190 if (nfds == 0)
191 return 0;
192
52bb308c 193 r = ppoll(fds, nfds, timeout == USEC_INFINITY ? NULL : TIMESPEC_STORE(timeout), NULL);
c004493c
LP
194 if (r < 0)
195 return -errno;
c004493c
LP
196 if (r == 0)
197 return 0;
198
c4febde9
YW
199 for (size_t i = 0, n = r; i < nfds && n > 0; i++) {
200 if (fds[i].revents == 0)
201 continue;
202 if (fds[i].revents & POLLNVAL)
203 return -EBADF;
204 n--;
205 }
206
207 return r;
208}
209
210int fd_wait_for_event(int fd, int event, usec_t timeout) {
211 struct pollfd pollfd = {
212 .fd = fd,
213 .events = event,
214 };
215 int r;
216
ffbcc8d4
LP
217 /* ⚠️ ⚠️ ⚠️ Keep in mind you almost certainly want to handle -EINTR gracefully in the caller, see
218 * ppoll_usec() above! ⚠️ ⚠️ ⚠️ */
219
c4febde9
YW
220 r = ppoll_usec(&pollfd, 1, timeout);
221 if (r <= 0)
222 return r;
dad28bff 223
c004493c
LP
224 return pollfd.revents;
225}
226
227static size_t nul_length(const uint8_t *p, size_t sz) {
228 size_t n = 0;
229
230 while (sz > 0) {
231 if (*p != 0)
232 break;
233
234 n++;
235 p++;
236 sz--;
237 }
238
239 return n;
240}
241
242ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length) {
243 const uint8_t *q, *w, *e;
244 ssize_t l;
245
246 q = w = p;
247 e = q + sz;
248 while (q < e) {
249 size_t n;
250
251 n = nul_length(q, e - q);
252
253 /* If there are more than the specified run length of
254 * NUL bytes, or if this is the beginning or the end
255 * of the buffer, then seek instead of write */
256 if ((n > run_length) ||
257 (n > 0 && q == p) ||
258 (n > 0 && q + n >= e)) {
259 if (q > w) {
260 l = write(fd, w, q - w);
261 if (l < 0)
262 return -errno;
263 if (l != q -w)
264 return -EIO;
265 }
266
267 if (lseek(fd, n, SEEK_CUR) == (off_t) -1)
268 return -errno;
269
270 q += n;
271 w = q;
272 } else if (n > 0)
273 q += n;
274 else
313cefa1 275 q++;
c004493c
LP
276 }
277
278 if (q > w) {
279 l = write(fd, w, q - w);
280 if (l < 0)
281 return -errno;
282 if (l != q - w)
283 return -EIO;
284 }
285
286 return q - (const uint8_t*) p;
287}
084eeb86
ZJS
288
289char* set_iovec_string_field(struct iovec *iovec, size_t *n_iovec, const char *field, const char *value) {
290 char *x;
291
b910cc72 292 x = strjoin(field, value);
084eeb86
ZJS
293 if (x)
294 iovec[(*n_iovec)++] = IOVEC_MAKE_STRING(x);
295 return x;
296}
30a0554e
FB
297
298char* set_iovec_string_field_free(struct iovec *iovec, size_t *n_iovec, const char *field, char *value) {
299 char *x;
300
301 x = set_iovec_string_field(iovec, n_iovec, field, value);
302 free(value);
303 return x;
304}
11e6d971
FB
305
306struct iovec_wrapper *iovw_new(void) {
307 return malloc0(sizeof(struct iovec_wrapper));
308}
309
310void iovw_free_contents(struct iovec_wrapper *iovw, bool free_vectors) {
311 if (free_vectors)
312 for (size_t i = 0; i < iovw->count; i++)
313 free(iovw->iovec[i].iov_base);
314
315 iovw->iovec = mfree(iovw->iovec);
316 iovw->count = 0;
11e6d971
FB
317}
318
319struct iovec_wrapper *iovw_free_free(struct iovec_wrapper *iovw) {
320 iovw_free_contents(iovw, true);
321
322 return mfree(iovw);
323}
324
325struct iovec_wrapper *iovw_free(struct iovec_wrapper *iovw) {
326 iovw_free_contents(iovw, false);
327
328 return mfree(iovw);
329}
330
331int iovw_put(struct iovec_wrapper *iovw, void *data, size_t len) {
332 if (iovw->count >= IOV_MAX)
333 return -E2BIG;
334
319a4f4b 335 if (!GREEDY_REALLOC(iovw->iovec, iovw->count + 1))
c4cce957 336 return -ENOMEM;
11e6d971
FB
337
338 iovw->iovec[iovw->count++] = IOVEC_MAKE(data, len);
339 return 0;
340}
341
ae41fdb6
FB
342int iovw_put_string_field(struct iovec_wrapper *iovw, const char *field, const char *value) {
343 _cleanup_free_ char *x = NULL;
344 int r;
345
b910cc72 346 x = strjoin(field, value);
ae41fdb6 347 if (!x)
c4cce957 348 return -ENOMEM;
ae41fdb6
FB
349
350 r = iovw_put(iovw, x, strlen(x));
351 if (r >= 0)
352 TAKE_PTR(x);
353
354 return r;
355}
356
357int iovw_put_string_field_free(struct iovec_wrapper *iovw, const char *field, char *value) {
358 _cleanup_free_ _unused_ char *free_ptr = value;
359
360 return iovw_put_string_field(iovw, field, value);
361}
362
11e6d971 363void iovw_rebase(struct iovec_wrapper *iovw, char *old, char *new) {
fe96c0f8 364 for (size_t i = 0; i < iovw->count; i++)
11e6d971
FB
365 iovw->iovec[i].iov_base = (char *)iovw->iovec[i].iov_base - old + new;
366}
367
368size_t iovw_size(struct iovec_wrapper *iovw) {
fe96c0f8 369 size_t n = 0;
11e6d971 370
fe96c0f8 371 for (size_t i = 0; i < iovw->count; i++)
11e6d971
FB
372 n += iovw->iovec[i].iov_len;
373
374 return n;
375}
253a83ea
LP
376
377void iovec_array_free(struct iovec *iov, size_t n) {
378 if (!iov)
379 return;
380
381 for (size_t i = 0; i < n; i++)
382 free(iov[i].iov_base);
383
384 free(iov);
385}