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 = {
36 /* Read from the specified file descriptor, until POLLIN is not set anymore, throwing away everything
37 * read. Note that some file descriptors (notable IP sockets) will trigger POLLIN even when no data can be read
38 * (due to IP packet checksum mismatches), hence this function is only safe to be non-blocking if the fd used
39 * was set to non-blocking too. */
46 r = poll(&pollfd, 1, 0);
56 l = read(fd, buf, sizeof(buf));
71 ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) {
78 /* If called with nbytes == 0, let's call read() at least
79 * once, to validate the operation */
81 if (nbytes > (size_t) SSIZE_MAX)
87 k = read(fd, p, nbytes);
92 if (errno == EAGAIN && do_poll) {
94 /* We knowingly ignore any return value here,
95 * and expect that any error/EOF is reported
98 (void) fd_wait_for_event(fd, POLLIN, USEC_INFINITY);
102 return n > 0 ? n : -errno;
108 assert((size_t) k <= nbytes);
113 } while (nbytes > 0);
118 int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll) {
121 n = loop_read(fd, buf, nbytes, do_poll);
124 if ((size_t) n != nbytes)
130 int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) {
131 const uint8_t *p = buf;
136 if (nbytes > (size_t) SSIZE_MAX)
142 k = write(fd, p, nbytes);
147 if (errno == EAGAIN && do_poll) {
148 /* We knowingly ignore any return value here,
149 * and expect that any error/EOF is reported
152 (void) fd_wait_for_event(fd, POLLOUT, USEC_INFINITY);
159 if (_unlikely_(nbytes > 0 && k == 0)) /* Can't really happen */
162 assert((size_t) k <= nbytes);
166 } while (nbytes > 0);
171 int pipe_eof(int fd) {
172 struct pollfd pollfd = {
174 .events = POLLIN|POLLHUP,
179 r = poll(&pollfd, 1, 0);
186 return pollfd.revents & POLLHUP;
189 int fd_wait_for_event(int fd, int event, usec_t t) {
191 struct pollfd pollfd = {
199 r = ppoll(&pollfd, 1, t == USEC_INFINITY ? NULL : timespec_store(&ts, t), NULL);
206 return pollfd.revents;
209 static size_t nul_length(const uint8_t *p, size_t sz) {
224 ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length) {
225 const uint8_t *q, *w, *e;
233 n = nul_length(q, e - q);
235 /* If there are more than the specified run length of
236 * NUL bytes, or if this is the beginning or the end
237 * of the buffer, then seek instead of write */
238 if ((n > run_length) ||
240 (n > 0 && q + n >= e)) {
242 l = write(fd, w, q - w);
249 if (lseek(fd, n, SEEK_CUR) == (off_t) -1)
261 l = write(fd, w, q - w);
268 return q - (const uint8_t*) p;