]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/io-util.c
2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "time-util.h"
30 int flush_fd(int fd
) {
31 struct pollfd pollfd
= {
41 r
= poll(&pollfd
, 1, 0);
51 l
= read(fd
, buf
, sizeof(buf
));
66 ssize_t
loop_read(int fd
, void *buf
, size_t nbytes
, bool do_poll
) {
73 /* If called with nbytes == 0, let's call read() at least
74 * once, to validate the operation */
76 if (nbytes
> (size_t) SSIZE_MAX
)
82 k
= read(fd
, p
, nbytes
);
87 if (errno
== EAGAIN
&& do_poll
) {
89 /* We knowingly ignore any return value here,
90 * and expect that any error/EOF is reported
93 (void) fd_wait_for_event(fd
, POLLIN
, USEC_INFINITY
);
97 return n
> 0 ? n
: -errno
;
103 assert((size_t) k
<= nbytes
);
108 } while (nbytes
> 0);
113 int loop_read_exact(int fd
, void *buf
, size_t nbytes
, bool do_poll
) {
116 n
= loop_read(fd
, buf
, nbytes
, do_poll
);
119 if ((size_t) n
!= nbytes
)
125 int loop_write(int fd
, const void *buf
, size_t nbytes
, bool do_poll
) {
126 const uint8_t *p
= buf
;
131 if (nbytes
> (size_t) SSIZE_MAX
)
137 k
= write(fd
, p
, nbytes
);
142 if (errno
== EAGAIN
&& do_poll
) {
143 /* We knowingly ignore any return value here,
144 * and expect that any error/EOF is reported
147 (void) fd_wait_for_event(fd
, POLLOUT
, USEC_INFINITY
);
154 if (_unlikely_(nbytes
> 0 && k
== 0)) /* Can't really happen */
157 assert((size_t) k
<= nbytes
);
161 } while (nbytes
> 0);
166 int pipe_eof(int fd
) {
167 struct pollfd pollfd
= {
169 .events
= POLLIN
|POLLHUP
,
174 r
= poll(&pollfd
, 1, 0);
181 return pollfd
.revents
& POLLHUP
;
184 int fd_wait_for_event(int fd
, int event
, usec_t t
) {
186 struct pollfd pollfd
= {
194 r
= ppoll(&pollfd
, 1, t
== USEC_INFINITY
? NULL
: timespec_store(&ts
, t
), NULL
);
201 return pollfd
.revents
;
204 static size_t nul_length(const uint8_t *p
, size_t sz
) {
219 ssize_t
sparse_write(int fd
, const void *p
, size_t sz
, size_t run_length
) {
220 const uint8_t *q
, *w
, *e
;
228 n
= nul_length(q
, e
- q
);
230 /* If there are more than the specified run length of
231 * NUL bytes, or if this is the beginning or the end
232 * of the buffer, then seek instead of write */
233 if ((n
> run_length
) ||
235 (n
> 0 && q
+ n
>= e
)) {
237 l
= write(fd
, w
, q
- w
);
244 if (lseek(fd
, n
, SEEK_CUR
) == (off_t
) -1)
256 l
= write(fd
, w
, q
- w
);
263 return q
- (const uint8_t*) p
;