]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/io-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include "errno-util.h"
10 #include "string-util.h"
11 #include "time-util.h"
13 int flush_fd(int fd
) {
16 /* Read from the specified file descriptor, until POLLIN is not set anymore, throwing away everything
17 * read. Note that some file descriptors (notable IP sockets) will trigger POLLIN even when no data can be read
18 * (due to IP packet checksum mismatches), hence this function is only safe to be non-blocking if the fd used
19 * was set to non-blocking too. */
26 r
= fd_wait_for_event(fd
, POLLIN
, 0);
36 l
= read(fd
, buf
, sizeof(buf
));
52 ssize_t
loop_read(int fd
, void *buf
, size_t nbytes
, bool do_poll
) {
53 uint8_t *p
= ASSERT_PTR(buf
);
58 /* If called with nbytes == 0, let's call read() at least once, to validate the operation */
60 if (nbytes
> (size_t) SSIZE_MAX
)
66 k
= read(fd
, p
, nbytes
);
71 if (errno
== EAGAIN
&& do_poll
) {
73 /* We knowingly ignore any return value here,
74 * and expect that any error/EOF is reported
77 (void) fd_wait_for_event(fd
, POLLIN
, USEC_INFINITY
);
81 return n
> 0 ? n
: -errno
;
87 assert((size_t) k
<= nbytes
);
97 int loop_read_exact(int fd
, void *buf
, size_t nbytes
, bool do_poll
) {
100 n
= loop_read(fd
, buf
, nbytes
, do_poll
);
103 if ((size_t) n
!= nbytes
)
109 int loop_write_full(int fd
, const void *buf
, size_t nbytes
, usec_t timeout
) {
115 assert(buf
|| nbytes
== 0);
118 static const dummy_t dummy
[0];
119 assert_cc(sizeof(dummy
) == 0);
120 p
= (const void*) dummy
; /* Some valid pointer, in case NULL was specified */
122 if (nbytes
== SIZE_MAX
)
123 nbytes
= strlen(buf
);
124 else if (_unlikely_(nbytes
> (size_t) SSIZE_MAX
))
130 /* When timeout is 0 or USEC_INFINITY this is not used. But we initialize it to a sensible value. */
131 end
= timestamp_is_set(timeout
) ? usec_add(now(CLOCK_MONOTONIC
), timeout
) : USEC_INFINITY
;
136 k
= write(fd
, p
, nbytes
);
141 if (errno
!= EAGAIN
|| timeout
== 0)
146 if (timeout
== USEC_INFINITY
)
147 wait_for
= USEC_INFINITY
;
149 usec_t t
= now(CLOCK_MONOTONIC
);
153 wait_for
= usec_sub_unsigned(end
, t
);
156 r
= fd_wait_for_event(fd
, POLLOUT
, wait_for
);
157 if (timeout
== USEC_INFINITY
|| ERRNO_IS_NEG_TRANSIENT(r
))
158 /* If timeout == USEC_INFINITY we knowingly ignore any return value
159 * here, and expect that any error/EOF is reported via write() */
167 if (_unlikely_(nbytes
> 0 && k
== 0)) /* Can't really happen */
170 assert((size_t) k
<= nbytes
);
174 } while (nbytes
> 0);
179 int pipe_eof(int fd
) {
182 r
= fd_wait_for_event(fd
, POLLIN
, 0);
186 return !!(r
& POLLHUP
);
189 int ppoll_usec(struct pollfd
*fds
, size_t nfds
, usec_t timeout
) {
192 assert(fds
|| nfds
== 0);
194 /* This is a wrapper around ppoll() that does primarily two things:
196 * ✅ Takes a usec_t instead of a struct timespec
198 * ✅ Guarantees that if an invalid fd is specified we return EBADF (i.e. converts POLLNVAL to
199 * EBADF). This is done because EBADF is a programming error usually, and hence should bubble up
200 * as error, and not be eaten up as non-error POLLNVAL event.
202 * ⚠️ ⚠️ ⚠️ Note that this function does not add any special handling for EINTR. Don't forget
203 * poll()/ppoll() will return with EINTR on any received signal always, there is no automatic
204 * restarting via SA_RESTART available. Thus, typically you want to handle EINTR not as an error,
205 * but just as reason to restart things, under the assumption you use a more appropriate mechanism
206 * to handle signals, such as signalfd() or signal handlers. ⚠️ ⚠️ ⚠️
212 r
= ppoll(fds
, nfds
, timeout
== USEC_INFINITY
? NULL
: TIMESPEC_STORE(timeout
), NULL
);
218 for (size_t i
= 0, n
= r
; i
< nfds
&& n
> 0; i
++) {
219 if (fds
[i
].revents
== 0)
221 if (fds
[i
].revents
& POLLNVAL
)
229 int fd_wait_for_event(int fd
, int event
, usec_t timeout
) {
230 struct pollfd pollfd
= {
236 /* ⚠️ ⚠️ ⚠️ Keep in mind you almost certainly want to handle -EINTR gracefully in the caller, see
237 * ppoll_usec() above! ⚠️ ⚠️ ⚠️ */
239 r
= ppoll_usec(&pollfd
, 1, timeout
);
243 return pollfd
.revents
;
246 static size_t nul_length(const uint8_t *p
, size_t sz
) {
261 ssize_t
sparse_write(int fd
, const void *p
, size_t sz
, size_t run_length
) {
262 const uint8_t *q
, *w
, *e
;
270 n
= nul_length(q
, e
- q
);
272 /* If there are more than the specified run length of
273 * NUL bytes, or if this is the beginning or the end
274 * of the buffer, then seek instead of write */
275 if ((n
> run_length
) ||
277 (n
> 0 && q
+ n
>= e
)) {
279 l
= write(fd
, w
, q
- w
);
286 if (lseek(fd
, n
, SEEK_CUR
) == (off_t
) -1)
298 l
= write(fd
, w
, q
- w
);
305 return q
- (const uint8_t*) p
;
308 char* set_iovec_string_field(struct iovec
*iovec
, size_t *n_iovec
, const char *field
, const char *value
) {
311 x
= strjoin(field
, value
);
313 iovec
[(*n_iovec
)++] = IOVEC_MAKE_STRING(x
);
317 char* set_iovec_string_field_free(struct iovec
*iovec
, size_t *n_iovec
, const char *field
, char *value
) {
320 x
= set_iovec_string_field(iovec
, n_iovec
, field
, value
);
325 struct iovec_wrapper
*iovw_new(void) {
326 return malloc0(sizeof(struct iovec_wrapper
));
329 void iovw_free_contents(struct iovec_wrapper
*iovw
, bool free_vectors
) {
331 for (size_t i
= 0; i
< iovw
->count
; i
++)
332 free(iovw
->iovec
[i
].iov_base
);
334 iovw
->iovec
= mfree(iovw
->iovec
);
338 struct iovec_wrapper
*iovw_free_free(struct iovec_wrapper
*iovw
) {
339 iovw_free_contents(iovw
, true);
344 struct iovec_wrapper
*iovw_free(struct iovec_wrapper
*iovw
) {
345 iovw_free_contents(iovw
, false);
350 int iovw_put(struct iovec_wrapper
*iovw
, void *data
, size_t len
) {
351 if (iovw
->count
>= IOV_MAX
)
354 if (!GREEDY_REALLOC(iovw
->iovec
, iovw
->count
+ 1))
357 iovw
->iovec
[iovw
->count
++] = IOVEC_MAKE(data
, len
);
361 int iovw_put_string_field(struct iovec_wrapper
*iovw
, const char *field
, const char *value
) {
362 _cleanup_free_
char *x
= NULL
;
365 x
= strjoin(field
, value
);
369 r
= iovw_put(iovw
, x
, strlen(x
));
376 int iovw_put_string_field_free(struct iovec_wrapper
*iovw
, const char *field
, char *value
) {
377 _cleanup_free_ _unused_
char *free_ptr
= value
;
379 return iovw_put_string_field(iovw
, field
, value
);
382 void iovw_rebase(struct iovec_wrapper
*iovw
, char *old
, char *new) {
383 for (size_t i
= 0; i
< iovw
->count
; i
++)
384 iovw
->iovec
[i
].iov_base
= (char *)iovw
->iovec
[i
].iov_base
- old
+ new;
387 size_t iovw_size(struct iovec_wrapper
*iovw
) {
390 for (size_t i
= 0; i
< iovw
->count
; i
++)
391 n
+= iovw
->iovec
[i
].iov_len
;
396 int iovw_append(struct iovec_wrapper
*target
, const struct iovec_wrapper
*source
) {
397 size_t original_count
;
402 /* This duplicates the source and merges it into the target. */
404 if (iovw_isempty(source
))
407 original_count
= target
->count
;
409 FOREACH_ARRAY(iovec
, source
->iovec
, source
->count
) {
412 dup
= memdup(iovec
->iov_base
, iovec
->iov_len
);
418 r
= iovw_consume(target
, dup
, iovec
->iov_len
);
426 for (size_t i
= original_count
; i
< target
->count
; i
++)
427 free(target
->iovec
[i
].iov_base
);
429 target
->count
= original_count
;
433 void iovec_array_free(struct iovec
*iov
, size_t n
) {
437 for (size_t i
= 0; i
< n
; i
++)
438 free(iov
[i
].iov_base
);