• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 15057632786

15 May 2025 09:01PM UTC coverage: 72.267% (+0.02%) from 72.244%
15057632786

push

github

bluca
man: document how to hook stuff into system wakeup

Fixes: #6364

298523 of 413084 relevant lines covered (72.27%)

738132.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.03
/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 "errno-util.h"
9
#include "io-util.h"
10
#include "iovec-util.h"
11
#include "string-util.h"
12
#include "time-util.h"
13

14
int flush_fd(int fd) {
3,851✔
15
        int count = 0;
3,851✔
16

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

22
        for (;;) {
7,009✔
23
                char buf[LINE_MAX];
7,009✔
24
                ssize_t l;
7,009✔
25
                int r;
7,009✔
26

27
                r = fd_wait_for_event(fd, POLLIN, 0);
7,009✔
28
                if (r < 0) {
7,009✔
29
                        if (r == -EINTR)
×
30
                                continue;
×
31

32
                        return r;
3,851✔
33
                }
34
                if (r == 0)
7,009✔
35
                        return count;
36

37
                l = read(fd, buf, sizeof(buf));
3,158✔
38
                if (l < 0) {
3,158✔
39
                        if (errno == EINTR)
×
40
                                continue;
×
41

42
                        if (errno == EAGAIN)
×
43
                                return count;
44

45
                        return -errno;
×
46
                } else if (l == 0)
3,158✔
47
                        return count;
48

49
                count += (int) l;
3,158✔
50
        }
51
}
52

53
ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) {
12,977✔
54
        uint8_t *p = ASSERT_PTR(buf);
12,977✔
55
        ssize_t n = 0;
12,977✔
56

57
        assert(fd >= 0);
12,977✔
58

59
        /* If called with nbytes == 0, let's call read() at least once, to validate the operation */
60

61
        if (nbytes > (size_t) SSIZE_MAX)
12,977✔
62
                return -EINVAL;
63

64
        do {
24,250✔
65
                ssize_t k;
24,250✔
66

67
                k = read(fd, p, nbytes);
24,250✔
68
                if (k < 0) {
24,250✔
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)
24,250✔
86
                        return n;
87

88
                assert((size_t) k <= nbytes);
12,885✔
89
                assert(k <= SSIZE_MAX - n);
12,885✔
90

91
                p += k;
12,885✔
92
                nbytes -= k;
12,885✔
93
                n += k;
12,885✔
94
        } while (nbytes > 0);
12,885✔
95

96
        return n;
97
}
98

99
int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll) {
1,447✔
100
        ssize_t n;
1,447✔
101

102
        n = loop_read(fd, buf, nbytes, do_poll);
1,447✔
103
        if (n < 0)
1,447✔
104
                return (int) n;
×
105
        if ((size_t) n != nbytes)
1,447✔
106
                return -EIO;
×
107

108
        return 0;
109
}
110

111
int loop_write_full(int fd, const void *buf, size_t nbytes, usec_t timeout) {
40,486✔
112
        const uint8_t *p;
40,486✔
113
        usec_t end;
40,486✔
114
        int r;
40,486✔
115

116
        assert(fd >= 0);
40,486✔
117
        assert(buf || nbytes == 0);
40,486✔
118

119
        if (nbytes == 0) {
40,486✔
120
                static const dummy_t dummy[0];
121
                assert_cc(sizeof(dummy) == 0);
122
                p = (const void*) dummy; /* Some valid pointer, in case NULL was specified */
123
        } else {
124
                if (nbytes == SIZE_MAX)
40,464✔
125
                        nbytes = strlen(buf);
410✔
126
                else if (_unlikely_(nbytes > (size_t) SSIZE_MAX))
40,054✔
127
                        return -EINVAL;
128

129
                p = buf;
130
        }
131

132
        /* When timeout is 0 or USEC_INFINITY this is not used. But we initialize it to a sensible value. */
133
        end = timestamp_is_set(timeout) ? usec_add(now(CLOCK_MONOTONIC), timeout) : USEC_INFINITY;
40,587✔
134

135
        do {
40,486✔
136
                ssize_t k;
40,486✔
137

138
                k = write(fd, p, nbytes);
40,486✔
139
                if (k < 0) {
40,486✔
140
                        if (errno == EINTR)
1✔
141
                                continue;
×
142

143
                        if (errno != EAGAIN || timeout == 0)
1✔
144
                                return -errno;
1✔
145

146
                        usec_t wait_for;
×
147

148
                        if (timeout == USEC_INFINITY)
×
149
                                wait_for = USEC_INFINITY;
150
                        else {
151
                                usec_t t = now(CLOCK_MONOTONIC);
×
152
                                if (t >= end)
×
153
                                        return -ETIME;
154

155
                                wait_for = usec_sub_unsigned(end, t);
×
156
                        }
157

158
                        r = fd_wait_for_event(fd, POLLOUT, wait_for);
×
159
                        if (timeout == USEC_INFINITY || ERRNO_IS_NEG_TRANSIENT(r))
×
160
                                /* If timeout == USEC_INFINITY we knowingly ignore any return value
161
                                 * here, and expect that any error/EOF is reported via write() */
162
                                continue;
×
163
                        if (r < 0)
×
164
                                return r;
165
                        if (r == 0)
×
166
                                return -ETIME;
167
                        continue;
×
168
                }
169

170
                if (_unlikely_(nbytes > 0 && k == 0)) /* Can't really happen */
40,485✔
171
                        return -EIO;
172

173
                assert((size_t) k <= nbytes);
40,485✔
174

175
                p += k;
40,485✔
176
                nbytes -= k;
40,485✔
177
        } while (nbytes > 0);
40,485✔
178

179
        return 0;
180
}
181

182
int pipe_eof(int fd) {
×
183
        int r;
×
184

185
        r = fd_wait_for_event(fd, POLLIN, 0);
×
186
        if (r <= 0)
×
187
                return r;
188

189
        return !!(r & POLLHUP);
×
190
}
191

192
int ppoll_usec_full(struct pollfd *fds, size_t nfds, usec_t timeout, const sigset_t *ss) {
373,530✔
193
        int r;
373,530✔
194

195
        assert(fds || nfds == 0);
373,530✔
196

197
        /* This is a wrapper around ppoll() that does primarily two things:
198
         *
199
         *  ✅ Takes a usec_t instead of a struct timespec
200
         *
201
         *  ✅ Guarantees that if an invalid fd is specified we return EBADF (i.e. converts POLLNVAL to
202
         *     EBADF). This is done because EBADF is a programming error usually, and hence should bubble up
203
         *     as error, and not be eaten up as non-error POLLNVAL event.
204
         *
205
         *  ⚠️ ⚠️ ⚠️ Note that this function does not add any special handling for EINTR. Don't forget
206
         *  poll()/ppoll() will return with EINTR on any received signal always, there is no automatic
207
         *  restarting via SA_RESTART available. Thus, typically you want to handle EINTR not as an error,
208
         *  but just as reason to restart things, under the assumption you use a more appropriate mechanism
209
         *  to handle signals, such as signalfd() or signal handlers. ⚠️ ⚠️ ⚠️
210
         */
211

212
        if (nfds == 0 && timeout == 0)
373,530✔
213
                return 0;
373,530✔
214

215
        r = ppoll(fds, nfds, timeout == USEC_INFINITY ? NULL : TIMESPEC_STORE(timeout), ss);
373,530✔
216
        if (r < 0)
373,530✔
217
                return -errno;
×
218
        if (r == 0)
373,530✔
219
                return 0;
220

221
        for (size_t i = 0, n = r; i < nfds && n > 0; i++) {
647,230✔
222
                if (fds[i].revents == 0)
324,234✔
223
                        continue;
1,182✔
224
                if (fds[i].revents & POLLNVAL)
323,052✔
225
                        return -EBADF;
226
                n--;
323,052✔
227
        }
228

229
        return r;
230
}
231

232
int fd_wait_for_event(int fd, int event, usec_t timeout) {
67,678✔
233
        struct pollfd pollfd = {
67,678✔
234
                .fd = fd,
235
                .events = event,
236
        };
237
        int r;
67,678✔
238

239
        /* ⚠️ ⚠️ ⚠️ Keep in mind you almost certainly want to handle -EINTR gracefully in the caller, see
240
         * ppoll_usec() above! ⚠️ ⚠️ ⚠️ */
241

242
        r = ppoll_usec(&pollfd, 1, timeout);
67,678✔
243
        if (r <= 0)
67,678✔
244
                return r;
67,678✔
245

246
        return pollfd.revents;
17,147✔
247
}
248

249
static size_t nul_length(const uint8_t *p, size_t sz) {
315,274,448✔
250
        size_t n = 0;
315,274,448✔
251

252
        while (sz > 0) {
570,103,858✔
253
                if (*p != 0)
570,086,757✔
254
                        break;
255

256
                n++;
254,829,410✔
257
                p++;
254,829,410✔
258
                sz--;
254,829,410✔
259
        }
260

261
        return n;
315,274,448✔
262
}
263

264
ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length) {
33,135✔
265
        const uint8_t *q, *w, *e;
33,135✔
266
        ssize_t l;
33,135✔
267

268
        q = w = p;
33,135✔
269
        e = q + sz;
33,135✔
270
        while (q < e) {
315,307,583✔
271
                size_t n;
315,274,448✔
272

273
                n = nul_length(q, e - q);
315,274,448✔
274

275
                /* If there are more than the specified run length of
276
                 * NUL bytes, or if this is the beginning or the end
277
                 * of the buffer, then seek instead of write */
278
                if ((n > run_length) ||
315,274,448✔
279
                    (n > 0 && q == p) ||
315,107,520✔
280
                    (n > 0 && q + n >= e)) {
27,696,129✔
281
                        if (q > w) {
177,097✔
282
                                l = write(fd, w, q - w);
163,758✔
283
                                if (l < 0)
163,758✔
284
                                        return -errno;
×
285
                                if (l != q -w)
163,758✔
286
                                        return -EIO;
287
                        }
288

289
                        if (lseek(fd, n, SEEK_CUR) < 0)
177,097✔
290
                                return -errno;
×
291

292
                        q += n;
177,097✔
293
                        w = q;
177,097✔
294
                } else if (n > 0)
27,689,775✔
295
                        q += n;
296
                else
297
                        q++;
287,407,576✔
298
        }
299

300
        if (q > w) {
33,135✔
301
                l = write(fd, w, q - w);
16,034✔
302
                if (l < 0)
16,034✔
303
                        return -errno;
×
304
                if (l != q - w)
16,034✔
305
                        return -EIO;
306
        }
307

308
        return q - (const uint8_t*) p;
33,135✔
309
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc