chiark / gitweb /
Prep v228: Condense elogind source masks (1/5)
[elogind.git] / src / basic / io-util.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 #pragma once
4
5 /***
6   This file is part of systemd.
7
8   Copyright 2010 Lennart Poettering
9
10   systemd is free software; you can redistribute it and/or modify it
11   under the terms of the GNU Lesser General Public License as published by
12   the Free Software Foundation; either version 2.1 of the License, or
13   (at your option) any later version.
14
15   systemd is distributed in the hope that it will be useful, but
16   WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18   Lesser General Public License for more details.
19
20   You should have received a copy of the GNU Lesser General Public License
21   along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 ***/
23
24 #include <stdbool.h>
25 #include <sys/types.h>
26 #include <sys/uio.h>
27
28 #include "time-util.h"
29
30 int flush_fd(int fd);
31
32 ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
33 int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll);
34 int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll);
35
36 int pipe_eof(int fd);
37
38 int fd_wait_for_event(int fd, int event, usec_t timeout);
39
40 ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length);
41
42 #define IOVEC_SET_STRING(i, s)                  \
43         do {                                    \
44                 struct iovec *_i = &(i);        \
45                 char *_s = (char *)(s);         \
46                 _i->iov_base = _s;              \
47                 _i->iov_len = strlen(_s);       \
48         } while(false)
49
50 static inline size_t IOVEC_TOTAL_SIZE(const struct iovec *i, unsigned n) {
51         unsigned j;
52         size_t r = 0;
53
54         for (j = 0; j < n; j++)
55                 r += i[j].iov_len;
56
57         return r;
58 }
59
60 static inline size_t IOVEC_INCREMENT(struct iovec *i, unsigned n, size_t k) {
61         unsigned j;
62
63         for (j = 0; j < n; j++) {
64                 size_t sub;
65
66                 if (_unlikely_(k <= 0))
67                         break;
68
69                 sub = MIN(i[j].iov_len, k);
70                 i[j].iov_len -= sub;
71                 i[j].iov_base = (uint8_t*) i[j].iov_base + sub;
72                 k -= sub;
73         }
74
75         return k;
76 }