]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/io-util.c
Merge pull request #28766 from DaanDeMeyer/repart-copy-from
[thirdparty/systemd.git] / src / basic / io-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <limits.h>
5 #include <stdio.h>
6 #include <unistd.h>
7
8 #include "io-util.h"
9 #include "string-util.h"
10 #include "time-util.h"
11
12 int flush_fd(int fd) {
13 int count = 0;
14
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
20 for (;;) {
21 char buf[LINE_MAX];
22 ssize_t l;
23 int r;
24
25 r = fd_wait_for_event(fd, POLLIN, 0);
26 if (r < 0) {
27 if (r == -EINTR)
28 continue;
29
30 return r;
31 }
32 if (r == 0)
33 return count;
34
35 l = read(fd, buf, sizeof(buf));
36 if (l < 0) {
37 if (errno == EINTR)
38 continue;
39
40 if (errno == EAGAIN)
41 return count;
42
43 return -errno;
44 } else if (l == 0)
45 return count;
46
47 count += (int) l;
48 }
49 }
50
51 ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) {
52 uint8_t *p = ASSERT_PTR(buf);
53 ssize_t n = 0;
54
55 assert(fd >= 0);
56
57 /* If called with nbytes == 0, let's call read() at least once, to validate the operation */
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
96 int 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
108 int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) {
109 const uint8_t *p;
110
111 assert(fd >= 0);
112
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 }
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
160 int pipe_eof(int fd) {
161 int r;
162
163 r = fd_wait_for_event(fd, POLLIN, 0);
164 if (r <= 0)
165 return r;
166
167 return !!(r & POLLHUP);
168 }
169
170 int ppoll_usec(struct pollfd *fds, size_t nfds, usec_t timeout) {
171 int r;
172
173 assert(fds || nfds == 0);
174
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
190 if (nfds == 0)
191 return 0;
192
193 r = ppoll(fds, nfds, timeout == USEC_INFINITY ? NULL : TIMESPEC_STORE(timeout), NULL);
194 if (r < 0)
195 return -errno;
196 if (r == 0)
197 return 0;
198
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
210 int 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
217 /* ⚠️ ⚠️ ⚠️ Keep in mind you almost certainly want to handle -EINTR gracefully in the caller, see
218 * ppoll_usec() above! ⚠️ ⚠️ ⚠️ */
219
220 r = ppoll_usec(&pollfd, 1, timeout);
221 if (r <= 0)
222 return r;
223
224 return pollfd.revents;
225 }
226
227 static 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
242 ssize_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
275 q++;
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 }
288
289 char* set_iovec_string_field(struct iovec *iovec, size_t *n_iovec, const char *field, const char *value) {
290 char *x;
291
292 x = strjoin(field, value);
293 if (x)
294 iovec[(*n_iovec)++] = IOVEC_MAKE_STRING(x);
295 return x;
296 }
297
298 char* 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 }
305
306 struct iovec_wrapper *iovw_new(void) {
307 return malloc0(sizeof(struct iovec_wrapper));
308 }
309
310 void 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;
317 }
318
319 struct iovec_wrapper *iovw_free_free(struct iovec_wrapper *iovw) {
320 iovw_free_contents(iovw, true);
321
322 return mfree(iovw);
323 }
324
325 struct iovec_wrapper *iovw_free(struct iovec_wrapper *iovw) {
326 iovw_free_contents(iovw, false);
327
328 return mfree(iovw);
329 }
330
331 int iovw_put(struct iovec_wrapper *iovw, void *data, size_t len) {
332 if (iovw->count >= IOV_MAX)
333 return -E2BIG;
334
335 if (!GREEDY_REALLOC(iovw->iovec, iovw->count + 1))
336 return -ENOMEM;
337
338 iovw->iovec[iovw->count++] = IOVEC_MAKE(data, len);
339 return 0;
340 }
341
342 int iovw_put_string_field(struct iovec_wrapper *iovw, const char *field, const char *value) {
343 _cleanup_free_ char *x = NULL;
344 int r;
345
346 x = strjoin(field, value);
347 if (!x)
348 return -ENOMEM;
349
350 r = iovw_put(iovw, x, strlen(x));
351 if (r >= 0)
352 TAKE_PTR(x);
353
354 return r;
355 }
356
357 int 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
363 void iovw_rebase(struct iovec_wrapper *iovw, char *old, char *new) {
364 for (size_t i = 0; i < iovw->count; i++)
365 iovw->iovec[i].iov_base = (char *)iovw->iovec[i].iov_base - old + new;
366 }
367
368 size_t iovw_size(struct iovec_wrapper *iovw) {
369 size_t n = 0;
370
371 for (size_t i = 0; i < iovw->count; i++)
372 n += iovw->iovec[i].iov_len;
373
374 return n;
375 }
376
377 int iovw_append(struct iovec_wrapper *target, const struct iovec_wrapper *source) {
378 size_t original_count;
379 int r;
380
381 assert(target);
382
383 /* This duplicates the source and merges it into the target. */
384
385 if (!source || source->count == 0)
386 return 0;
387
388 original_count = target->count;
389
390 FOREACH_ARRAY(iovec, source->iovec, source->count) {
391 void *dup;
392
393 dup = memdup(iovec->iov_base, iovec->iov_len);
394 if (!dup) {
395 r = -ENOMEM;
396 goto rollback;
397 }
398
399 r = iovw_consume(target, dup, iovec->iov_len);
400 if (r < 0)
401 goto rollback;
402 }
403
404 return 0;
405
406 rollback:
407 for (size_t i = original_count; i < target->count; i++)
408 free(target->iovec[i].iov_base);
409
410 target->count = original_count;
411 return r;
412 }
413
414 void iovec_array_free(struct iovec *iov, size_t n) {
415 if (!iov)
416 return;
417
418 for (size_t i = 0; i < n; i++)
419 free(iov[i].iov_base);
420
421 free(iov);
422 }