]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/io-util.c
18baadb1fed767ddb014c3d8d108a1436bf000e9
[thirdparty/systemd.git] / src / basic / io-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <limits.h>
5 #include <poll.h>
6 #include <stdio.h>
7 #include <unistd.h>
8
9 #include "io-util.h"
10 #include "string-util.h"
11 #include "time-util.h"
12
13 int flush_fd(int fd) {
14 int count = 0;
15
16 /* Read from the specified file descriptor, until POLLIN is not set anymore, throwing away everything
17 * read. Note that some file descriptors (notable IP sockets) will trigger POLLIN even when no data can be read
18 * (due to IP packet checksum mismatches), hence this function is only safe to be non-blocking if the fd used
19 * was set to non-blocking too. */
20
21 for (;;) {
22 char buf[LINE_MAX];
23 ssize_t l;
24 int r;
25
26 r = fd_wait_for_event(fd, POLLIN, 0);
27 if (r < 0) {
28 if (r == -EINTR)
29 continue;
30
31 return r;
32 }
33 if (r == 0)
34 return count;
35
36 l = read(fd, buf, sizeof(buf));
37 if (l < 0) {
38 if (errno == EINTR)
39 continue;
40
41 if (errno == EAGAIN)
42 return count;
43
44 return -errno;
45 } else if (l == 0)
46 return count;
47
48 count += (int) l;
49 }
50 }
51
52 ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) {
53 uint8_t *p = buf;
54 ssize_t n = 0;
55
56 assert(fd >= 0);
57 assert(buf);
58
59 /* If called with nbytes == 0, let's call read() at least
60 * once, to validate the operation */
61
62 if (nbytes > (size_t) SSIZE_MAX)
63 return -EINVAL;
64
65 do {
66 ssize_t k;
67
68 k = read(fd, p, nbytes);
69 if (k < 0) {
70 if (errno == EINTR)
71 continue;
72
73 if (errno == EAGAIN && do_poll) {
74
75 /* We knowingly ignore any return value here,
76 * and expect that any error/EOF is reported
77 * via read() */
78
79 (void) fd_wait_for_event(fd, POLLIN, USEC_INFINITY);
80 continue;
81 }
82
83 return n > 0 ? n : -errno;
84 }
85
86 if (k == 0)
87 return n;
88
89 assert((size_t) k <= nbytes);
90
91 p += k;
92 nbytes -= k;
93 n += k;
94 } while (nbytes > 0);
95
96 return n;
97 }
98
99 int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll) {
100 ssize_t n;
101
102 n = loop_read(fd, buf, nbytes, do_poll);
103 if (n < 0)
104 return (int) n;
105 if ((size_t) n != nbytes)
106 return -EIO;
107
108 return 0;
109 }
110
111 int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) {
112 const uint8_t *p = buf;
113
114 assert(fd >= 0);
115 assert(buf);
116
117 if (_unlikely_(nbytes > (size_t) SSIZE_MAX))
118 return -EINVAL;
119
120 do {
121 ssize_t k;
122
123 k = write(fd, p, nbytes);
124 if (k < 0) {
125 if (errno == EINTR)
126 continue;
127
128 if (errno == EAGAIN && do_poll) {
129 /* We knowingly ignore any return value here,
130 * and expect that any error/EOF is reported
131 * via write() */
132
133 (void) fd_wait_for_event(fd, POLLOUT, USEC_INFINITY);
134 continue;
135 }
136
137 return -errno;
138 }
139
140 if (_unlikely_(nbytes > 0 && k == 0)) /* Can't really happen */
141 return -EIO;
142
143 assert((size_t) k <= nbytes);
144
145 p += k;
146 nbytes -= k;
147 } while (nbytes > 0);
148
149 return 0;
150 }
151
152 int pipe_eof(int fd) {
153 int r;
154
155 r = fd_wait_for_event(fd, POLLIN, 0);
156 if (r < 0)
157 return r;
158 if (r == 0)
159 return 0;
160
161 return !!(r & POLLHUP);
162 }
163
164 int fd_wait_for_event(int fd, int event, usec_t t) {
165
166 struct pollfd pollfd = {
167 .fd = fd,
168 .events = event,
169 };
170
171 struct timespec ts;
172 int r;
173
174 r = ppoll(&pollfd, 1, t == USEC_INFINITY ? NULL : timespec_store(&ts, t), NULL);
175 if (r < 0)
176 return -errno;
177 if (r == 0)
178 return 0;
179
180 if (pollfd.revents & POLLNVAL)
181 return -EBADF;
182
183 return pollfd.revents;
184 }
185
186 static size_t nul_length(const uint8_t *p, size_t sz) {
187 size_t n = 0;
188
189 while (sz > 0) {
190 if (*p != 0)
191 break;
192
193 n++;
194 p++;
195 sz--;
196 }
197
198 return n;
199 }
200
201 ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length) {
202 const uint8_t *q, *w, *e;
203 ssize_t l;
204
205 q = w = p;
206 e = q + sz;
207 while (q < e) {
208 size_t n;
209
210 n = nul_length(q, e - q);
211
212 /* If there are more than the specified run length of
213 * NUL bytes, or if this is the beginning or the end
214 * of the buffer, then seek instead of write */
215 if ((n > run_length) ||
216 (n > 0 && q == p) ||
217 (n > 0 && q + n >= e)) {
218 if (q > w) {
219 l = write(fd, w, q - w);
220 if (l < 0)
221 return -errno;
222 if (l != q -w)
223 return -EIO;
224 }
225
226 if (lseek(fd, n, SEEK_CUR) == (off_t) -1)
227 return -errno;
228
229 q += n;
230 w = q;
231 } else if (n > 0)
232 q += n;
233 else
234 q++;
235 }
236
237 if (q > w) {
238 l = write(fd, w, q - w);
239 if (l < 0)
240 return -errno;
241 if (l != q - w)
242 return -EIO;
243 }
244
245 return q - (const uint8_t*) p;
246 }
247
248 char* set_iovec_string_field(struct iovec *iovec, size_t *n_iovec, const char *field, const char *value) {
249 char *x;
250
251 x = strjoin(field, value);
252 if (x)
253 iovec[(*n_iovec)++] = IOVEC_MAKE_STRING(x);
254 return x;
255 }
256
257 char* set_iovec_string_field_free(struct iovec *iovec, size_t *n_iovec, const char *field, char *value) {
258 char *x;
259
260 x = set_iovec_string_field(iovec, n_iovec, field, value);
261 free(value);
262 return x;
263 }
264
265 struct iovec_wrapper *iovw_new(void) {
266 return malloc0(sizeof(struct iovec_wrapper));
267 }
268
269 void iovw_free_contents(struct iovec_wrapper *iovw, bool free_vectors) {
270 if (free_vectors)
271 for (size_t i = 0; i < iovw->count; i++)
272 free(iovw->iovec[i].iov_base);
273
274 iovw->iovec = mfree(iovw->iovec);
275 iovw->count = 0;
276 iovw->size_bytes = 0;
277 }
278
279 struct iovec_wrapper *iovw_free_free(struct iovec_wrapper *iovw) {
280 iovw_free_contents(iovw, true);
281
282 return mfree(iovw);
283 }
284
285 struct iovec_wrapper *iovw_free(struct iovec_wrapper *iovw) {
286 iovw_free_contents(iovw, false);
287
288 return mfree(iovw);
289 }
290
291 int iovw_put(struct iovec_wrapper *iovw, void *data, size_t len) {
292 if (iovw->count >= IOV_MAX)
293 return -E2BIG;
294
295 if (!GREEDY_REALLOC(iovw->iovec, iovw->size_bytes, iovw->count + 1))
296 return log_oom();
297
298 iovw->iovec[iovw->count++] = IOVEC_MAKE(data, len);
299 return 0;
300 }
301
302 int iovw_put_string_field(struct iovec_wrapper *iovw, const char *field, const char *value) {
303 _cleanup_free_ char *x = NULL;
304 int r;
305
306 x = strjoin(field, value);
307 if (!x)
308 return log_oom();
309
310 r = iovw_put(iovw, x, strlen(x));
311 if (r >= 0)
312 TAKE_PTR(x);
313
314 return r;
315 }
316
317 int iovw_put_string_field_free(struct iovec_wrapper *iovw, const char *field, char *value) {
318 _cleanup_free_ _unused_ char *free_ptr = value;
319
320 return iovw_put_string_field(iovw, field, value);
321 }
322
323 void iovw_rebase(struct iovec_wrapper *iovw, char *old, char *new) {
324 size_t i;
325
326 for (i = 0; i < iovw->count; i++)
327 iovw->iovec[i].iov_base = (char *)iovw->iovec[i].iov_base - old + new;
328 }
329
330 size_t iovw_size(struct iovec_wrapper *iovw) {
331 size_t n = 0, i;
332
333 for (i = 0; i < iovw->count; i++)
334 n += iovw->iovec[i].iov_len;
335
336 return n;
337 }