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