]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
c004493c | 2 | |
11c3a366 TA |
3 | #include <errno.h> |
4 | #include <limits.h> | |
11c3a366 | 5 | #include <stdio.h> |
c004493c LP |
6 | #include <unistd.h> |
7 | ||
e22c60a9 | 8 | #include "errno-util.h" |
c004493c | 9 | #include "io-util.h" |
bd1ae178 | 10 | #include "iovec-util.h" |
084eeb86 | 11 | #include "string-util.h" |
11c3a366 | 12 | #include "time-util.h" |
c004493c LP |
13 | |
14 | int flush_fd(int fd) { | |
665dfe93 | 15 | int count = 0; |
c004493c | 16 | |
60d9771c LP |
17 | /* Read from the specified file descriptor, until POLLIN is not set anymore, throwing away everything |
18 | * read. Note that some file descriptors (notable IP sockets) will trigger POLLIN even when no data can be read | |
19 | * (due to IP packet checksum mismatches), hence this function is only safe to be non-blocking if the fd used | |
20 | * was set to non-blocking too. */ | |
21 | ||
c004493c LP |
22 | for (;;) { |
23 | char buf[LINE_MAX]; | |
24 | ssize_t l; | |
25 | int r; | |
26 | ||
0f2d351f | 27 | r = fd_wait_for_event(fd, POLLIN, 0); |
c004493c | 28 | if (r < 0) { |
0f2d351f | 29 | if (r == -EINTR) |
c004493c LP |
30 | continue; |
31 | ||
0f2d351f | 32 | return r; |
dad28bff LP |
33 | } |
34 | if (r == 0) | |
665dfe93 | 35 | return count; |
c004493c LP |
36 | |
37 | l = read(fd, buf, sizeof(buf)); | |
38 | if (l < 0) { | |
c004493c LP |
39 | if (errno == EINTR) |
40 | continue; | |
41 | ||
42 | if (errno == EAGAIN) | |
665dfe93 | 43 | return count; |
c004493c LP |
44 | |
45 | return -errno; | |
46 | } else if (l == 0) | |
665dfe93 LP |
47 | return count; |
48 | ||
49 | count += (int) l; | |
c004493c LP |
50 | } |
51 | } | |
52 | ||
53 | ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) { | |
99534007 | 54 | uint8_t *p = ASSERT_PTR(buf); |
c004493c LP |
55 | ssize_t n = 0; |
56 | ||
57 | assert(fd >= 0); | |
c004493c | 58 | |
a73c74db | 59 | /* If called with nbytes == 0, let's call read() at least once, to validate the operation */ |
c004493c LP |
60 | |
61 | if (nbytes > (size_t) SSIZE_MAX) | |
62 | return -EINVAL; | |
63 | ||
64 | do { | |
65 | ssize_t k; | |
66 | ||
67 | k = read(fd, p, nbytes); | |
68 | if (k < 0) { | |
69 | if (errno == EINTR) | |
70 | continue; | |
71 | ||
72 | if (errno == EAGAIN && do_poll) { | |
73 | ||
74 | /* We knowingly ignore any return value here, | |
75 | * and expect that any error/EOF is reported | |
76 | * via read() */ | |
77 | ||
78 | (void) fd_wait_for_event(fd, POLLIN, USEC_INFINITY); | |
79 | continue; | |
80 | } | |
81 | ||
82 | return n > 0 ? n : -errno; | |
83 | } | |
84 | ||
85 | if (k == 0) | |
86 | return n; | |
87 | ||
88 | assert((size_t) k <= nbytes); | |
89 | ||
90 | p += k; | |
91 | nbytes -= k; | |
92 | n += k; | |
93 | } while (nbytes > 0); | |
94 | ||
95 | return n; | |
96 | } | |
97 | ||
98 | int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll) { | |
99 | ssize_t n; | |
100 | ||
101 | n = loop_read(fd, buf, nbytes, do_poll); | |
102 | if (n < 0) | |
103 | return (int) n; | |
104 | if ((size_t) n != nbytes) | |
105 | return -EIO; | |
106 | ||
107 | return 0; | |
108 | } | |
109 | ||
e22c60a9 | 110 | int loop_write_full(int fd, const void *buf, size_t nbytes, usec_t timeout) { |
d89457a1 | 111 | const uint8_t *p; |
e22c60a9 MY |
112 | usec_t end; |
113 | int r; | |
c004493c LP |
114 | |
115 | assert(fd >= 0); | |
e22c60a9 | 116 | assert(buf || nbytes == 0); |
c004493c | 117 | |
d89457a1 LP |
118 | if (nbytes == 0) { |
119 | static const dummy_t dummy[0]; | |
120 | assert_cc(sizeof(dummy) == 0); | |
121 | p = (const void*) dummy; /* Some valid pointer, in case NULL was specified */ | |
122 | } else { | |
d89457a1 LP |
123 | if (nbytes == SIZE_MAX) |
124 | nbytes = strlen(buf); | |
125 | else if (_unlikely_(nbytes > (size_t) SSIZE_MAX)) | |
126 | return -EINVAL; | |
127 | ||
128 | p = buf; | |
129 | } | |
c004493c | 130 | |
e22c60a9 MY |
131 | /* When timeout is 0 or USEC_INFINITY this is not used. But we initialize it to a sensible value. */ |
132 | end = timestamp_is_set(timeout) ? usec_add(now(CLOCK_MONOTONIC), timeout) : USEC_INFINITY; | |
133 | ||
c004493c LP |
134 | do { |
135 | ssize_t k; | |
136 | ||
137 | k = write(fd, p, nbytes); | |
138 | if (k < 0) { | |
139 | if (errno == EINTR) | |
140 | continue; | |
141 | ||
e22c60a9 MY |
142 | if (errno != EAGAIN || timeout == 0) |
143 | return -errno; | |
c004493c | 144 | |
e22c60a9 MY |
145 | usec_t wait_for; |
146 | ||
147 | if (timeout == USEC_INFINITY) | |
148 | wait_for = USEC_INFINITY; | |
149 | else { | |
150 | usec_t t = now(CLOCK_MONOTONIC); | |
151 | if (t >= end) | |
152 | return -ETIME; | |
153 | ||
154 | wait_for = usec_sub_unsigned(end, t); | |
c004493c LP |
155 | } |
156 | ||
e22c60a9 MY |
157 | r = fd_wait_for_event(fd, POLLOUT, wait_for); |
158 | if (timeout == USEC_INFINITY || ERRNO_IS_NEG_TRANSIENT(r)) | |
159 | /* If timeout == USEC_INFINITY we knowingly ignore any return value | |
160 | * here, and expect that any error/EOF is reported via write() */ | |
161 | continue; | |
162 | if (r < 0) | |
163 | return r; | |
164 | if (r == 0) | |
165 | return -ETIME; | |
c004493c LP |
166 | } |
167 | ||
168 | if (_unlikely_(nbytes > 0 && k == 0)) /* Can't really happen */ | |
169 | return -EIO; | |
170 | ||
171 | assert((size_t) k <= nbytes); | |
172 | ||
173 | p += k; | |
174 | nbytes -= k; | |
175 | } while (nbytes > 0); | |
176 | ||
177 | return 0; | |
178 | } | |
179 | ||
180 | int pipe_eof(int fd) { | |
c004493c LP |
181 | int r; |
182 | ||
0f2d351f | 183 | r = fd_wait_for_event(fd, POLLIN, 0); |
60d7a202 | 184 | if (r <= 0) |
0f2d351f | 185 | return r; |
c004493c | 186 | |
0f2d351f | 187 | return !!(r & POLLHUP); |
c004493c LP |
188 | } |
189 | ||
c4febde9 | 190 | int ppoll_usec(struct pollfd *fds, size_t nfds, usec_t timeout) { |
c004493c LP |
191 | int r; |
192 | ||
c4febde9 YW |
193 | assert(fds || nfds == 0); |
194 | ||
ffbcc8d4 LP |
195 | /* This is a wrapper around ppoll() that does primarily two things: |
196 | * | |
197 | * ✅ Takes a usec_t instead of a struct timespec | |
198 | * | |
199 | * ✅ Guarantees that if an invalid fd is specified we return EBADF (i.e. converts POLLNVAL to | |
200 | * EBADF). This is done because EBADF is a programming error usually, and hence should bubble up | |
201 | * as error, and not be eaten up as non-error POLLNVAL event. | |
202 | * | |
203 | * ⚠️ ⚠️ ⚠️ Note that this function does not add any special handling for EINTR. Don't forget | |
204 | * poll()/ppoll() will return with EINTR on any received signal always, there is no automatic | |
205 | * restarting via SA_RESTART available. Thus, typically you want to handle EINTR not as an error, | |
206 | * but just as reason to restart things, under the assumption you use a more appropriate mechanism | |
207 | * to handle signals, such as signalfd() or signal handlers. ⚠️ ⚠️ ⚠️ | |
208 | */ | |
209 | ||
c4febde9 YW |
210 | if (nfds == 0) |
211 | return 0; | |
212 | ||
52bb308c | 213 | r = ppoll(fds, nfds, timeout == USEC_INFINITY ? NULL : TIMESPEC_STORE(timeout), NULL); |
c004493c LP |
214 | if (r < 0) |
215 | return -errno; | |
c004493c LP |
216 | if (r == 0) |
217 | return 0; | |
218 | ||
c4febde9 YW |
219 | for (size_t i = 0, n = r; i < nfds && n > 0; i++) { |
220 | if (fds[i].revents == 0) | |
221 | continue; | |
222 | if (fds[i].revents & POLLNVAL) | |
223 | return -EBADF; | |
224 | n--; | |
225 | } | |
226 | ||
227 | return r; | |
228 | } | |
229 | ||
230 | int fd_wait_for_event(int fd, int event, usec_t timeout) { | |
231 | struct pollfd pollfd = { | |
232 | .fd = fd, | |
233 | .events = event, | |
234 | }; | |
235 | int r; | |
236 | ||
ffbcc8d4 LP |
237 | /* ⚠️ ⚠️ ⚠️ Keep in mind you almost certainly want to handle -EINTR gracefully in the caller, see |
238 | * ppoll_usec() above! ⚠️ ⚠️ ⚠️ */ | |
239 | ||
c4febde9 YW |
240 | r = ppoll_usec(&pollfd, 1, timeout); |
241 | if (r <= 0) | |
242 | return r; | |
dad28bff | 243 | |
c004493c LP |
244 | return pollfd.revents; |
245 | } | |
246 | ||
247 | static size_t nul_length(const uint8_t *p, size_t sz) { | |
248 | size_t n = 0; | |
249 | ||
250 | while (sz > 0) { | |
251 | if (*p != 0) | |
252 | break; | |
253 | ||
254 | n++; | |
255 | p++; | |
256 | sz--; | |
257 | } | |
258 | ||
259 | return n; | |
260 | } | |
261 | ||
262 | ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length) { | |
263 | const uint8_t *q, *w, *e; | |
264 | ssize_t l; | |
265 | ||
266 | q = w = p; | |
267 | e = q + sz; | |
268 | while (q < e) { | |
269 | size_t n; | |
270 | ||
271 | n = nul_length(q, e - q); | |
272 | ||
273 | /* If there are more than the specified run length of | |
274 | * NUL bytes, or if this is the beginning or the end | |
275 | * of the buffer, then seek instead of write */ | |
276 | if ((n > run_length) || | |
277 | (n > 0 && q == p) || | |
278 | (n > 0 && q + n >= e)) { | |
279 | if (q > w) { | |
280 | l = write(fd, w, q - w); | |
281 | if (l < 0) | |
282 | return -errno; | |
283 | if (l != q -w) | |
284 | return -EIO; | |
285 | } | |
286 | ||
287 | if (lseek(fd, n, SEEK_CUR) == (off_t) -1) | |
288 | return -errno; | |
289 | ||
290 | q += n; | |
291 | w = q; | |
292 | } else if (n > 0) | |
293 | q += n; | |
294 | else | |
313cefa1 | 295 | q++; |
c004493c LP |
296 | } |
297 | ||
298 | if (q > w) { | |
299 | l = write(fd, w, q - w); | |
300 | if (l < 0) | |
301 | return -errno; | |
302 | if (l != q - w) | |
303 | return -EIO; | |
304 | } | |
305 | ||
306 | return q - (const uint8_t*) p; | |
307 | } |