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

systemd / systemd / 13380515387

17 Feb 2025 09:20PM UTC coverage: 71.822% (+0.1%) from 71.714%
13380515387

push

github

DaanDeMeyer
ukify: print all remaining log-like output to stderr

We want to be able to capture stdout for json and such, so convert
all remaining logging to stderr.

293883 of 409184 relevant lines covered (71.82%)

716959.33 hits per line

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

75.86
/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) {
4,540✔
15
        int count = 0;
4,540✔
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 (;;) {
9,020✔
23
                char buf[LINE_MAX];
9,020✔
24
                ssize_t l;
9,020✔
25
                int r;
9,020✔
26

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

32
                        return r;
4,540✔
33
                }
34
                if (r == 0)
9,020✔
35
                        return count;
36

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

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

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

49
                count += (int) l;
4,480✔
50
        }
51
}
52

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

57
        assert(fd >= 0);
11,906✔
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)
11,906✔
62
                return -EINVAL;
63

64
        do {
21,949✔
65
                ssize_t k;
21,949✔
66

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

88
                assert((size_t) k <= nbytes);
11,807✔
89

90
                p += k;
11,807✔
91
                nbytes -= k;
11,807✔
92
                n += k;
11,807✔
93
        } while (nbytes > 0);
11,807✔
94

95
        return n;
96
}
97

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

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

107
        return 0;
108
}
109

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

115
        assert(fd >= 0);
188,392✔
116
        assert(buf || nbytes == 0);
188,392✔
117

118
        if (nbytes == 0) {
188,392✔
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 {
123
                if (nbytes == SIZE_MAX)
188,363✔
124
                        nbytes = strlen(buf);
314✔
125
                else if (_unlikely_(nbytes > (size_t) SSIZE_MAX))
188,049✔
126
                        return -EINVAL;
127

128
                p = buf;
129
        }
130

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;
188,486✔
133

134
        do {
188,392✔
135
                ssize_t k;
188,392✔
136

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

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

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);
×
155
                        }
156

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;
166
                        continue;
×
167
                }
168

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

172
                assert((size_t) k <= nbytes);
188,391✔
173

174
                p += k;
188,391✔
175
                nbytes -= k;
188,391✔
176
        } while (nbytes > 0);
188,391✔
177

178
        return 0;
179
}
180

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

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

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

191
int ppoll_usec(struct pollfd *fds, size_t nfds, usec_t timeout) {
354,335✔
192
        int r;
354,335✔
193

194
        assert(fds || nfds == 0);
354,335✔
195

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

211
        if (nfds == 0)
354,335✔
212
                return 0;
354,335✔
213

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

220
        for (size_t i = 0, n = r; i < nfds && n > 0; i++) {
636,299✔
221
                if (fds[i].revents == 0)
318,743✔
222
                        continue;
1,137✔
223
                if (fds[i].revents & POLLNVAL)
317,606✔
224
                        return -EBADF;
225
                n--;
317,606✔
226
        }
227

228
        return r;
229
}
230

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

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

241
        r = ppoll_usec(&pollfd, 1, timeout);
54,773✔
242
        if (r <= 0)
54,773✔
243
                return r;
54,773✔
244

245
        return pollfd.revents;
17,997✔
246
}
247

248
static size_t nul_length(const uint8_t *p, size_t sz) {
311,524,749✔
249
        size_t n = 0;
311,524,749✔
250

251
        while (sz > 0) {
569,764,259✔
252
                if (*p != 0)
569,747,033✔
253
                        break;
254

255
                n++;
258,239,510✔
256
                p++;
258,239,510✔
257
                sz--;
258,239,510✔
258
        }
259

260
        return n;
311,524,749✔
261
}
262

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

267
        q = w = p;
33,132✔
268
        e = q + sz;
33,132✔
269
        while (q < e) {
311,557,881✔
270
                size_t n;
311,524,749✔
271

272
                n = nul_length(q, e - q);
311,524,749✔
273

274
                /* If there are more than the specified run length of
275
                 * NUL bytes, or if this is the beginning or the end
276
                 * of the buffer, then seek instead of write */
277
                if ((n > run_length) ||
311,524,749✔
278
                    (n > 0 && q == p) ||
311,357,257✔
279
                    (n > 0 && q + n >= e)) {
27,396,786✔
280
                        if (q > w) {
177,706✔
281
                                l = write(fd, w, q - w);
163,965✔
282
                                if (l < 0)
163,965✔
283
                                        return -errno;
×
284
                                if (l != q -w)
163,965✔
285
                                        return -EIO;
286
                        }
287

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

291
                        q += n;
177,706✔
292
                        w = q;
177,706✔
293
                } else if (n > 0)
27,390,531✔
294
                        q += n;
295
                else
296
                        q++;
283,956,512✔
297
        }
298

299
        if (q > w) {
33,132✔
300
                l = write(fd, w, q - w);
15,906✔
301
                if (l < 0)
15,906✔
302
                        return -errno;
×
303
                if (l != q - w)
15,906✔
304
                        return -EIO;
305
        }
306

307
        return q - (const uint8_t*) p;
33,132✔
308
}
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