]>
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/>.
27 int flush_fd(int fd
) {
28 struct pollfd pollfd
= {
38 r
= poll(&pollfd
, 1, 0);
48 l
= read(fd
, buf
, sizeof(buf
));
63 ssize_t
loop_read(int fd
, void *buf
, size_t nbytes
, bool do_poll
) {
70 /* If called with nbytes == 0, let's call read() at least
71 * once, to validate the operation */
73 if (nbytes
> (size_t) SSIZE_MAX
)
79 k
= read(fd
, p
, nbytes
);
84 if (errno
== EAGAIN
&& do_poll
) {
86 /* We knowingly ignore any return value here,
87 * and expect that any error/EOF is reported
90 (void) fd_wait_for_event(fd
, POLLIN
, USEC_INFINITY
);
94 return n
> 0 ? n
: -errno
;
100 assert((size_t) k
<= nbytes
);
105 } while (nbytes
> 0);
110 int loop_read_exact(int fd
, void *buf
, size_t nbytes
, bool do_poll
) {
113 n
= loop_read(fd
, buf
, nbytes
, do_poll
);
116 if ((size_t) n
!= nbytes
)
122 int loop_write(int fd
, const void *buf
, size_t nbytes
, bool do_poll
) {
123 const uint8_t *p
= buf
;
128 if (nbytes
> (size_t) SSIZE_MAX
)
134 k
= write(fd
, p
, nbytes
);
139 if (errno
== EAGAIN
&& do_poll
) {
140 /* We knowingly ignore any return value here,
141 * and expect that any error/EOF is reported
144 (void) fd_wait_for_event(fd
, POLLOUT
, USEC_INFINITY
);
151 if (_unlikely_(nbytes
> 0 && k
== 0)) /* Can't really happen */
154 assert((size_t) k
<= nbytes
);
158 } while (nbytes
> 0);
163 int pipe_eof(int fd
) {
164 struct pollfd pollfd
= {
166 .events
= POLLIN
|POLLHUP
,
171 r
= poll(&pollfd
, 1, 0);
178 return pollfd
.revents
& POLLHUP
;
181 int fd_wait_for_event(int fd
, int event
, usec_t t
) {
183 struct pollfd pollfd
= {
191 r
= ppoll(&pollfd
, 1, t
== USEC_INFINITY
? NULL
: timespec_store(&ts
, t
), NULL
);
198 return pollfd
.revents
;
201 static size_t nul_length(const uint8_t *p
, size_t sz
) {
216 ssize_t
sparse_write(int fd
, const void *p
, size_t sz
, size_t run_length
) {
217 const uint8_t *q
, *w
, *e
;
225 n
= nul_length(q
, e
- q
);
227 /* If there are more than the specified run length of
228 * NUL bytes, or if this is the beginning or the end
229 * of the buffer, then seek instead of write */
230 if ((n
> run_length
) ||
232 (n
> 0 && q
+ n
>= e
)) {
234 l
= write(fd
, w
, q
- w
);
241 if (lseek(fd
, n
, SEEK_CUR
) == (off_t
) -1)
253 l
= write(fd
, w
, q
- w
);
260 return q
- (const uint8_t*) p
;