]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/io-util.h
job: change result field for log message about job result RESULT= → JOB_RESULT=
[thirdparty/systemd.git] / src / basic / io-util.h
CommitLineData
c004493c
LP
1#pragma once
2
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
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.
12
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.
17
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/>.
20***/
21
c004493c 22#include <stdbool.h>
11c3a366
TA
23#include <stddef.h>
24#include <stdint.h>
afc5dbf3
LP
25#include <sys/types.h>
26#include <sys/uio.h>
c004493c 27
11c3a366 28#include "macro.h"
c004493c
LP
29#include "time-util.h"
30
31int flush_fd(int fd);
32
33ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
34int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll);
35int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll);
36
37int pipe_eof(int fd);
38
39int fd_wait_for_event(int fd, int event, usec_t timeout);
40
41ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length);
afc5dbf3
LP
42
43#define IOVEC_SET_STRING(i, s) \
44 do { \
45 struct iovec *_i = &(i); \
46 char *_s = (char *)(s); \
47 _i->iov_base = _s; \
48 _i->iov_len = strlen(_s); \
9ed794a3 49 } while (false)
afc5dbf3
LP
50
51static inline size_t IOVEC_TOTAL_SIZE(const struct iovec *i, unsigned n) {
52 unsigned j;
53 size_t r = 0;
54
55 for (j = 0; j < n; j++)
56 r += i[j].iov_len;
57
58 return r;
59}
60
61static inline size_t IOVEC_INCREMENT(struct iovec *i, unsigned n, size_t k) {
62 unsigned j;
63
64 for (j = 0; j < n; j++) {
65 size_t sub;
66
67 if (_unlikely_(k <= 0))
68 break;
69
70 sub = MIN(i[j].iov_len, k);
71 i[j].iov_len -= sub;
72 i[j].iov_base = (uint8_t*) i[j].iov_base + sub;
73 k -= sub;
74 }
75
76 return k;
77}
a90fb858
LP
78
79static inline bool FILE_SIZE_VALID(uint64_t l) {
80 /* ftruncate() and friends take an unsigned file size, but actually cannot deal with file sizes larger than
81 * 2^63 since the kernel internally handles it as signed value. This call allows checking for this early. */
82
83 return (l >> 63) == 0;
84}
85
86static inline bool FILE_SIZE_VALID_OR_INFINITY(uint64_t l) {
87
88 /* Same as above, but allows one extra value: -1 as indication for infinity. */
89
90 if (l == (uint64_t) -1)
91 return true;
92
93 return FILE_SIZE_VALID(l);
94
95}