]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/io-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "string-util.h"
12 #include "time-util.h"
14 int flush_fd(int fd
) {
15 struct pollfd pollfd
= {
21 /* Read from the specified file descriptor, until POLLIN is not set anymore, throwing away everything
22 * read. Note that some file descriptors (notable IP sockets) will trigger POLLIN even when no data can be read
23 * (due to IP packet checksum mismatches), hence this function is only safe to be non-blocking if the fd used
24 * was set to non-blocking too. */
31 r
= poll(&pollfd
, 1, 0);
41 l
= read(fd
, buf
, sizeof(buf
));
58 ssize_t
loop_read(int fd
, void *buf
, size_t nbytes
, bool do_poll
) {
65 /* If called with nbytes == 0, let's call read() at least
66 * once, to validate the operation */
68 if (nbytes
> (size_t) SSIZE_MAX
)
74 k
= read(fd
, p
, nbytes
);
79 if (errno
== EAGAIN
&& do_poll
) {
81 /* We knowingly ignore any return value here,
82 * and expect that any error/EOF is reported
85 (void) fd_wait_for_event(fd
, POLLIN
, USEC_INFINITY
);
89 return n
> 0 ? n
: -errno
;
95 assert((size_t) k
<= nbytes
);
100 } while (nbytes
> 0);
105 int loop_read_exact(int fd
, void *buf
, size_t nbytes
, bool do_poll
) {
108 n
= loop_read(fd
, buf
, nbytes
, do_poll
);
111 if ((size_t) n
!= nbytes
)
117 int loop_write(int fd
, const void *buf
, size_t nbytes
, bool do_poll
) {
118 const uint8_t *p
= buf
;
123 if (_unlikely_(nbytes
> (size_t) SSIZE_MAX
))
129 k
= write(fd
, p
, nbytes
);
134 if (errno
== EAGAIN
&& do_poll
) {
135 /* We knowingly ignore any return value here,
136 * and expect that any error/EOF is reported
139 (void) fd_wait_for_event(fd
, POLLOUT
, USEC_INFINITY
);
146 if (_unlikely_(nbytes
> 0 && k
== 0)) /* Can't really happen */
149 assert((size_t) k
<= nbytes
);
153 } while (nbytes
> 0);
158 int pipe_eof(int fd
) {
159 struct pollfd pollfd
= {
161 .events
= POLLIN
|POLLHUP
,
166 r
= poll(&pollfd
, 1, 0);
173 return pollfd
.revents
& POLLHUP
;
176 int fd_wait_for_event(int fd
, int event
, usec_t t
) {
178 struct pollfd pollfd
= {
186 r
= ppoll(&pollfd
, 1, t
== USEC_INFINITY
? NULL
: timespec_store(&ts
, t
), NULL
);
192 return pollfd
.revents
;
195 static size_t nul_length(const uint8_t *p
, size_t sz
) {
210 ssize_t
sparse_write(int fd
, const void *p
, size_t sz
, size_t run_length
) {
211 const uint8_t *q
, *w
, *e
;
219 n
= nul_length(q
, e
- q
);
221 /* If there are more than the specified run length of
222 * NUL bytes, or if this is the beginning or the end
223 * of the buffer, then seek instead of write */
224 if ((n
> run_length
) ||
226 (n
> 0 && q
+ n
>= e
)) {
228 l
= write(fd
, w
, q
- w
);
235 if (lseek(fd
, n
, SEEK_CUR
) == (off_t
) -1)
247 l
= write(fd
, w
, q
- w
);
254 return q
- (const uint8_t*) p
;
257 char* set_iovec_string_field(struct iovec
*iovec
, size_t *n_iovec
, const char *field
, const char *value
) {
260 x
= strjoin(field
, value
);
262 iovec
[(*n_iovec
)++] = IOVEC_MAKE_STRING(x
);
266 char* set_iovec_string_field_free(struct iovec
*iovec
, size_t *n_iovec
, const char *field
, char *value
) {
269 x
= set_iovec_string_field(iovec
, n_iovec
, field
, value
);
274 struct iovec_wrapper
*iovw_new(void) {
275 return malloc0(sizeof(struct iovec_wrapper
));
278 void iovw_free_contents(struct iovec_wrapper
*iovw
, bool free_vectors
) {
280 for (size_t i
= 0; i
< iovw
->count
; i
++)
281 free(iovw
->iovec
[i
].iov_base
);
283 iovw
->iovec
= mfree(iovw
->iovec
);
285 iovw
->size_bytes
= 0;
288 struct iovec_wrapper
*iovw_free_free(struct iovec_wrapper
*iovw
) {
289 iovw_free_contents(iovw
, true);
294 struct iovec_wrapper
*iovw_free(struct iovec_wrapper
*iovw
) {
295 iovw_free_contents(iovw
, false);
300 int iovw_put(struct iovec_wrapper
*iovw
, void *data
, size_t len
) {
301 if (iovw
->count
>= IOV_MAX
)
304 if (!GREEDY_REALLOC(iovw
->iovec
, iovw
->size_bytes
, iovw
->count
+ 1))
307 iovw
->iovec
[iovw
->count
++] = IOVEC_MAKE(data
, len
);
311 int iovw_put_string_field(struct iovec_wrapper
*iovw
, const char *field
, const char *value
) {
312 _cleanup_free_
char *x
= NULL
;
315 x
= strjoin(field
, value
);
319 r
= iovw_put(iovw
, x
, strlen(x
));
326 int iovw_put_string_field_free(struct iovec_wrapper
*iovw
, const char *field
, char *value
) {
327 _cleanup_free_ _unused_
char *free_ptr
= value
;
329 return iovw_put_string_field(iovw
, field
, value
);
332 void iovw_rebase(struct iovec_wrapper
*iovw
, char *old
, char *new) {
335 for (i
= 0; i
< iovw
->count
; i
++)
336 iovw
->iovec
[i
].iov_base
= (char *)iovw
->iovec
[i
].iov_base
- old
+ new;
339 size_t iovw_size(struct iovec_wrapper
*iovw
) {
342 for (i
= 0; i
< iovw
->count
; i
++)
343 n
+= iovw
->iovec
[i
].iov_len
;