]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/io-util.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
30 #include "time-util.h"
32 int flush_fd(int fd
) {
33 struct pollfd pollfd
= {
43 r
= poll(&pollfd
, 1, 0);
53 l
= read(fd
, buf
, sizeof(buf
));
68 ssize_t
loop_read(int fd
, void *buf
, size_t nbytes
, bool do_poll
) {
75 /* If called with nbytes == 0, let's call read() at least
76 * once, to validate the operation */
78 if (nbytes
> (size_t) SSIZE_MAX
)
84 k
= read(fd
, p
, nbytes
);
89 if (errno
== EAGAIN
&& do_poll
) {
91 /* We knowingly ignore any return value here,
92 * and expect that any error/EOF is reported
95 (void) fd_wait_for_event(fd
, POLLIN
, USEC_INFINITY
);
99 return n
> 0 ? n
: -errno
;
105 assert((size_t) k
<= nbytes
);
110 } while (nbytes
> 0);
115 int loop_read_exact(int fd
, void *buf
, size_t nbytes
, bool do_poll
) {
118 n
= loop_read(fd
, buf
, nbytes
, do_poll
);
121 if ((size_t) n
!= nbytes
)
127 int loop_write(int fd
, const void *buf
, size_t nbytes
, bool do_poll
) {
128 const uint8_t *p
= buf
;
133 if (nbytes
> (size_t) SSIZE_MAX
)
139 k
= write(fd
, p
, nbytes
);
144 if (errno
== EAGAIN
&& do_poll
) {
145 /* We knowingly ignore any return value here,
146 * and expect that any error/EOF is reported
149 (void) fd_wait_for_event(fd
, POLLOUT
, USEC_INFINITY
);
156 if (_unlikely_(nbytes
> 0 && k
== 0)) /* Can't really happen */
159 assert((size_t) k
<= nbytes
);
163 } while (nbytes
> 0);
168 int pipe_eof(int fd
) {
169 struct pollfd pollfd
= {
171 .events
= POLLIN
|POLLHUP
,
176 r
= poll(&pollfd
, 1, 0);
183 return pollfd
.revents
& POLLHUP
;
186 int fd_wait_for_event(int fd
, int event
, usec_t t
) {
188 struct pollfd pollfd
= {
196 r
= ppoll(&pollfd
, 1, t
== USEC_INFINITY
? NULL
: timespec_store(&ts
, t
), NULL
);
203 return pollfd
.revents
;
206 static size_t nul_length(const uint8_t *p
, size_t sz
) {
221 ssize_t
sparse_write(int fd
, const void *p
, size_t sz
, size_t run_length
) {
222 const uint8_t *q
, *w
, *e
;
230 n
= nul_length(q
, e
- q
);
232 /* If there are more than the specified run length of
233 * NUL bytes, or if this is the beginning or the end
234 * of the buffer, then seek instead of write */
235 if ((n
> run_length
) ||
237 (n
> 0 && q
+ n
>= e
)) {
239 l
= write(fd
, w
, q
- w
);
246 if (lseek(fd
, n
, SEEK_CUR
) == (off_t
) -1)
258 l
= write(fd
, w
, q
- w
);
265 return q
- (const uint8_t*) p
;