]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/io-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include "time-util.h"
31 int flush_fd(int fd
) {
32 struct pollfd pollfd
= {
37 /* Read from the specified file descriptor, until POLLIN is not set anymore, throwing away everything
38 * read. Note that some file descriptors (notable IP sockets) will trigger POLLIN even when no data can be read
39 * (due to IP packet checksum mismatches), hence this function is only safe to be non-blocking if the fd used
40 * was set to non-blocking too. */
47 r
= poll(&pollfd
, 1, 0);
57 l
= read(fd
, buf
, sizeof(buf
));
72 ssize_t
loop_read(int fd
, void *buf
, size_t nbytes
, bool do_poll
) {
79 /* If called with nbytes == 0, let's call read() at least
80 * once, to validate the operation */
82 if (nbytes
> (size_t) SSIZE_MAX
)
88 k
= read(fd
, p
, nbytes
);
93 if (errno
== EAGAIN
&& do_poll
) {
95 /* We knowingly ignore any return value here,
96 * and expect that any error/EOF is reported
99 (void) fd_wait_for_event(fd
, POLLIN
, USEC_INFINITY
);
103 return n
> 0 ? n
: -errno
;
109 assert((size_t) k
<= nbytes
);
114 } while (nbytes
> 0);
119 int loop_read_exact(int fd
, void *buf
, size_t nbytes
, bool do_poll
) {
122 n
= loop_read(fd
, buf
, nbytes
, do_poll
);
125 if ((size_t) n
!= nbytes
)
131 int loop_write(int fd
, const void *buf
, size_t nbytes
, bool do_poll
) {
132 const uint8_t *p
= buf
;
137 if (nbytes
> (size_t) SSIZE_MAX
)
143 k
= write(fd
, p
, nbytes
);
148 if (errno
== EAGAIN
&& do_poll
) {
149 /* We knowingly ignore any return value here,
150 * and expect that any error/EOF is reported
153 (void) fd_wait_for_event(fd
, POLLOUT
, USEC_INFINITY
);
160 if (_unlikely_(nbytes
> 0 && k
== 0)) /* Can't really happen */
163 assert((size_t) k
<= nbytes
);
167 } while (nbytes
> 0);
172 int pipe_eof(int fd
) {
173 struct pollfd pollfd
= {
175 .events
= POLLIN
|POLLHUP
,
180 r
= poll(&pollfd
, 1, 0);
187 return pollfd
.revents
& POLLHUP
;
190 int fd_wait_for_event(int fd
, int event
, usec_t t
) {
192 struct pollfd pollfd
= {
200 r
= ppoll(&pollfd
, 1, t
== USEC_INFINITY
? NULL
: timespec_store(&ts
, t
), NULL
);
207 return pollfd
.revents
;
210 static size_t nul_length(const uint8_t *p
, size_t sz
) {
225 ssize_t
sparse_write(int fd
, const void *p
, size_t sz
, size_t run_length
) {
226 const uint8_t *q
, *w
, *e
;
234 n
= nul_length(q
, e
- q
);
236 /* If there are more than the specified run length of
237 * NUL bytes, or if this is the beginning or the end
238 * of the buffer, then seek instead of write */
239 if ((n
> run_length
) ||
241 (n
> 0 && q
+ n
>= e
)) {
243 l
= write(fd
, w
, q
- w
);
250 if (lseek(fd
, n
, SEEK_CUR
) == (off_t
) -1)
262 l
= write(fd
, w
, q
- w
);
269 return q
- (const uint8_t*) p
;