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

systemd / systemd / 23367620641

20 Mar 2026 07:25PM UTC coverage: 72.606% (+0.3%) from 72.351%
23367620641

push

github

bluca
kmod-setup: load vsock_loopback alongside vsock

Loading vmw_vsock_virtio_transport early at boot causes vsock to be
resident before any application opens an AF_VSOCK socket. Because the
kernel skips autoloading when the vsock module is already present,
vsock_loopback never gets loaded automatically, and any subsequent
bind() to VMADDR_CID_LOCAL fails with EADDRNOTAVAIL.

Fix this by explicitly loading vsock_loopback on virtio or VMWare
machines via the new may_have_vsock_looopback() helper, wich covers both
vmw_vsock_virtio_transport and vmware_vsock_vmci_transport case.
vsock_loopback is the only module that registers a transport for
VMADDR_CID_LOCAL (CID 1) and has no hard dependency from any of the
vsock transport modules.

Fixes: #41100
Follow-up for 381c78db4

2 of 2 new or added lines in 1 file covered. (100.0%)

2634 existing lines in 55 files now uncovered.

316477 of 435881 relevant lines covered (72.61%)

1159615.85 hits per line

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

93.07
/src/basic/fileio.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <fcntl.h>
4
#include <stdio_ext.h>
5
#include <stdlib.h>
6
#include <sys/stat.h>
7
#include <unistd.h>
8

9
#include "alloc-util.h"
10
#include "chase.h"
11
#include "errno-util.h"
12
#include "extract-word.h"
13
#include "fd-util.h"
14
#include "fileio.h"
15
#include "fs-util.h"
16
#include "hexdecoct.h"
17
#include "io-util.h"
18
#include "iovec-util.h"
19
#include "label.h"
20
#include "log.h"
21
#include "mkdir.h"
22
#include "nulstr-util.h"
23
#include "parse-util.h"
24
#include "path-util.h"
25
#include "socket-util.h"
26
#include "stat-util.h"
27
#include "stdio-util.h"
28
#include "string-util.h"
29
#include "strv.h"
30
#include "sync-util.h"
31
#include "terminal-util.h"
32
#include "time-util.h"
33
#include "tmpfile-util.h"
34

35
/* The maximum size of the file we'll read in one go in read_full_file() (64M). */
36
#define READ_FULL_BYTES_MAX (64U * U64_MB - UINT64_C(1))
37
/* Used when a size is specified for read_full_file() with READ_FULL_FILE_UNBASE64 or _UNHEX */
38
#define READ_FULL_FILE_ENCODED_STRING_AMPLIFICATION_BOUNDARY 3
39

40
/* The maximum size of virtual files (i.e. procfs, sysfs, and other virtual "API" files) we'll read in one go
41
 * in read_virtual_file(). Note that this limit is different (and much lower) than the READ_FULL_BYTES_MAX
42
 * limit. This reflects the fact that we use different strategies for reading virtual and regular files:
43
 * virtual files we generally have to read in a single read() syscall since the kernel doesn't support
44
 * continuation read()s for them. Thankfully they are somewhat size constrained. Thus we can allocate the
45
 * full potential buffer in advance. Regular files OTOH can be much larger, and there we grow the allocations
46
 * exponentially in a loop. We use a size limit of 4M-2 because 4M-1 is the maximum buffer that /proc/sys/
47
 * allows us to read() (larger reads will fail with ENOMEM), and we want to read one extra byte so that we
48
 * can detect EOFs. */
49
#define READ_VIRTUAL_BYTES_MAX (4U * U64_MB - UINT64_C(2))
50

51
int fdopen_unlocked(int fd, const char *options, FILE **ret) {
398,461✔
52
        assert(ret);
398,461✔
53

54
        FILE *f = fdopen(fd, options);
398,461✔
55
        if (!f)
398,461✔
UNCOV
56
                return -errno;
×
57

58
        (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
398,461✔
59

60
        *ret = f;
398,461✔
61
        return 0;
398,461✔
62
}
63

64
int take_fdopen_unlocked(int *fd, const char *options, FILE **ret) {
398,461✔
65
        int r;
398,461✔
66

67
        assert(fd);
398,461✔
68

69
        r = fdopen_unlocked(*fd, options, ret);
398,461✔
70
        if (r < 0)
398,461✔
71
                return r;
72

73
        *fd = -EBADF;
398,461✔
74

75
        return 0;
398,461✔
76
}
77

78
FILE* take_fdopen(int *fd, const char *options) {
46,677✔
79
        assert(fd);
46,677✔
80

81
        FILE *f = fdopen(*fd, options);
46,677✔
82
        if (!f)
46,677✔
83
                return NULL;
84

85
        *fd = -EBADF;
46,677✔
86

87
        return f;
46,677✔
88
}
89

90
DIR* take_fdopendir(int *dfd) {
230,125✔
91
        assert(dfd);
230,125✔
92

93
        DIR *d = fdopendir(*dfd);
230,125✔
94
        if (!d)
230,125✔
95
                return NULL;
96

97
        *dfd = -EBADF;
230,125✔
98

99
        return d;
230,125✔
100
}
101

102
FILE* open_memstream_unlocked(char **ptr, size_t *sizeloc) {
739,920✔
103
        FILE *f = open_memstream(ptr, sizeloc);
739,920✔
104
        if (!f)
739,920✔
105
                return NULL;
106

107
        (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
739,920✔
108

109
        return f;
739,920✔
110
}
111

112
FILE* fmemopen_unlocked(void *buf, size_t size, const char *mode) {
83✔
113
        FILE *f = fmemopen(buf, size, mode);
83✔
114
        if (!f)
83✔
115
                return NULL;
116

117
        (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
83✔
118

119
        return f;
83✔
120
}
121

122
int write_string_stream_full(
190,860✔
123
                FILE *f,
124
                const char *line,
125
                WriteStringFileFlags flags,
126
                const struct timespec *ts) {
127

128
        bool needs_nl;
190,860✔
129
        int r, fd = -EBADF;
190,860✔
130

131
        assert(f);
190,860✔
132
        assert(line);
190,860✔
133

134
        if (ferror(f))
190,860✔
135
                return -EIO;
136

137
        if (ts) {
190,860✔
138
                /* If we shall set the timestamp we need the fd. But fmemopen() streams generally don't have
139
                 * an fd. Let's fail early in that case. */
140
                fd = fileno(f);
2✔
141
                if (fd < 0)
2✔
142
                        return -EBADF;
143
        }
144

145
        if (flags & WRITE_STRING_FILE_SUPPRESS_REDUNDANT_VIRTUAL) {
190,860✔
146
                _cleanup_free_ char *t = NULL;
8,962✔
147

148
                /* If value to be written is same as that of the existing value, then suppress the write. */
149

150
                if (fd < 0) {
8,962✔
151
                        fd = fileno(f);
8,962✔
152
                        if (fd < 0)
8,962✔
153
                                return -EBADF;
154
                }
155

156
                /* Read an additional byte to detect cases where the prefix matches but the rest
157
                 * doesn't. Also, 0 returned by read_virtual_file_fd() means the read was truncated and
158
                 * it won't be equal to the new value. */
159
                if (read_virtual_file_fd(fd, strlen(line)+1, &t, NULL) > 0 &&
17,733✔
160
                    streq_skip_trailing_chars(line, t, NEWLINE)) {
8,771✔
161
                        log_debug("No change in value '%s', suppressing write", line);
6,604✔
162
                        return 0;
6,604✔
163
                }
164

165
                if (lseek(fd, 0, SEEK_SET) < 0)
2,358✔
UNCOV
166
                        return -errno;
×
167
        }
168

169
        needs_nl = !(flags & WRITE_STRING_FILE_AVOID_NEWLINE) && !endswith(line, "\n");
184,256✔
170

171
        if (needs_nl && (flags & WRITE_STRING_FILE_DISABLE_BUFFER)) {
56,989✔
172
                /* If STDIO buffering was disabled, then let's append the newline character to the string
173
                 * itself, so that the write goes out in one go, instead of two */
174

175
                line = strjoina(line, "\n");
277,945✔
176
                needs_nl = false;
55,589✔
177
        }
178

179
        if (fputs(line, f) == EOF)
184,256✔
180
                return -errno;
21✔
181

182
        if (needs_nl)
184,235✔
183
                if (fputc('\n', f) == EOF)
1,399✔
UNCOV
184
                        return -errno;
×
185

186
        if (flags & WRITE_STRING_FILE_SYNC)
184,235✔
187
                r = fflush_sync_and_check(f);
2,121✔
188
        else
189
                r = fflush_and_check(f);
182,114✔
190
        if (r < 0)
184,235✔
191
                return r;
192

193
        if (ts) {
184,207✔
194
                const struct timespec twice[2] = {*ts, *ts};
2✔
195

196
                assert(fd >= 0);
2✔
197
                if (futimens(fd, twice) < 0)
2✔
UNCOV
198
                        return -errno;
×
199
        }
200

201
        return 0;
202
}
203

204
static mode_t write_string_file_flags_to_mode(WriteStringFileFlags flags) {
95,209✔
205

206
        /* We support three different modes, that are the ones that really make sense for text files like this:
207
         *
208
         *     → 0600 (i.e. root-only)
209
         *     → 0444 (i.e. read-only)
210
         *     → 0644 (i.e. writable for root, readable for everyone else)
211
         */
212

213
        return FLAGS_SET(flags, WRITE_STRING_FILE_MODE_0600) ? 0600 :
95,209✔
214
                FLAGS_SET(flags, WRITE_STRING_FILE_MODE_0444) ? 0444 : 0644;
94,971✔
215
}
216

217
static int write_string_file_atomic_at(
3,060✔
218
                int dir_fd,
219
                const char *fn,
220
                const char *line,
221
                WriteStringFileFlags flags,
222
                const struct timespec *ts) {
223

224
        _cleanup_fclose_ FILE *f = NULL;
3,060✔
225
        _cleanup_free_ char *p = NULL;
3,060✔
226
        int r;
3,060✔
227

228
        assert(fn);
3,060✔
229
        assert(line);
3,060✔
230

231
        /* Note that we'd really like to use O_TMPFILE here, but can't really, since we want replacement
232
         * semantics here, and O_TMPFILE can't offer that. i.e. rename() replaces but linkat() doesn't. */
233

234
        mode_t mode = write_string_file_flags_to_mode(flags);
3,060✔
235

236
        bool call_label_ops_post = false;
3,060✔
237
        if (FLAGS_SET(flags, WRITE_STRING_FILE_LABEL)) {
3,060✔
238
                r = label_ops_pre(dir_fd, fn, mode);
187✔
239
                if (r < 0)
187✔
240
                        return r;
241

242
                call_label_ops_post = true;
243
        }
244

245
        r = fopen_temporary_at(dir_fd, fn, &f, &p);
3,060✔
246
        int k = call_label_ops_post ? label_ops_post(f ? fileno(f) : dir_fd, f ? NULL : fn, /* created= */ !!f) : 0;
3,247✔
247
        /* If fopen_temporary_at() failed in the above, propagate the error code, and ignore failures in
248
         * label_ops_post(). */
249
        if (r < 0)
3,060✔
250
                return r;
251
        CLEANUP_TMPFILE_AT(dir_fd, p);
3,059✔
252
        if (k < 0)
3,059✔
253
                return k;
254

255
        r = write_string_stream_full(f, line, flags, ts);
3,059✔
256
        if (r < 0)
3,059✔
257
                return r;
258

259
        r = fchmod_umask(fileno(f), mode);
3,059✔
260
        if (r < 0)
3,059✔
261
                return r;
262

263
        r = RET_NERRNO(renameat(dir_fd, p, dir_fd, fn));
3,059✔
UNCOV
264
        if (r < 0)
×
265
                return r;
266

267
        p = mfree(p); /* disarm CLEANUP_TMPFILE_AT() */
3,059✔
268

269
        if (FLAGS_SET(flags, WRITE_STRING_FILE_SYNC)) {
3,059✔
270
                /* Sync the rename, too */
271
                r = fsync_directory_of_file(fileno(f));
2,121✔
272
                if (r < 0)
2,121✔
UNCOV
273
                        return r;
×
274
        }
275

276
        return 0;
277
}
278

279
int write_string_file_full(
165,052✔
280
                int dir_fd,
281
                const char *fn,
282
                const char *line,
283
                WriteStringFileFlags flags,
284
                const struct timespec *ts,
285
                const char *label_fn) {
286

287
        bool made_file = false;
165,052✔
288
        _cleanup_fclose_ FILE *f = NULL;
165,052✔
289
        _cleanup_close_ int fd = -EBADF;
165,052✔
290
        int r;
165,052✔
291

292
        assert(dir_fd == AT_FDCWD || dir_fd >= 0);
165,052✔
293
        assert(line);
165,052✔
294

295
        /* We don't know how to verify whether the file contents was already on-disk. */
296
        assert(!((flags & WRITE_STRING_FILE_VERIFY_ON_FAILURE) && (flags & WRITE_STRING_FILE_SYNC)));
165,052✔
297

298
        if (flags & WRITE_STRING_FILE_MKDIR_0755) {
165,052✔
299
                assert(fn);
547✔
300

301
                r = mkdirat_parents(dir_fd, fn, 0755);
547✔
302
                if (r < 0)
547✔
303
                        return r;
304
        }
305

306
        if (flags & WRITE_STRING_FILE_ATOMIC) {
165,052✔
307
                assert(fn);
3,060✔
308
                assert(flags & WRITE_STRING_FILE_CREATE);
3,060✔
309

310
                r = write_string_file_atomic_at(dir_fd, fn, line, flags, ts);
3,060✔
311
                if (r < 0)
3,060✔
312
                        goto fail;
1✔
313

314
                return r;
315
        }
316

317
        /* We manually build our own version of fopen(..., "we") that works without O_CREAT and with O_NOFOLLOW if needed. */
318
        if (isempty(fn))
161,992✔
319
                r = fd = fd_reopen(
69,843✔
320
                                ASSERT_FD(dir_fd), O_CLOEXEC | O_NOCTTY |
69,843✔
321
                                (FLAGS_SET(flags, WRITE_STRING_FILE_TRUNCATE) ? O_TRUNC : 0) |
139,686✔
322
                                (FLAGS_SET(flags, WRITE_STRING_FILE_SUPPRESS_REDUNDANT_VIRTUAL) ? O_RDWR : O_WRONLY) |
69,843✔
323
                                (FLAGS_SET(flags, WRITE_STRING_FILE_OPEN_NONBLOCKING) ? O_NONBLOCK : 0));
69,843✔
324
        else {
325
                mode_t mode = write_string_file_flags_to_mode(flags);
92,149✔
326
                bool call_label_ops_post = false;
92,149✔
327

328
                if (FLAGS_SET(flags, WRITE_STRING_FILE_LABEL|WRITE_STRING_FILE_CREATE)) {
92,149✔
329
                        r = label_ops_pre(dir_fd, label_fn ?: fn, mode);
828✔
330
                        if (r < 0)
828✔
UNCOV
331
                                goto fail;
×
332

333
                        call_label_ops_post = true;
334
                }
335

336
                r = fd = openat_report_new(
276,447✔
337
                                dir_fd, fn, O_CLOEXEC | O_NOCTTY |
92,149✔
338
                                (FLAGS_SET(flags, WRITE_STRING_FILE_NOFOLLOW) ? O_NOFOLLOW : 0) |
92,149✔
339
                                (FLAGS_SET(flags, WRITE_STRING_FILE_CREATE) ? O_CREAT : 0) |
92,149✔
340
                                (FLAGS_SET(flags, WRITE_STRING_FILE_TRUNCATE) ? O_TRUNC : 0) |
184,298✔
341
                                (FLAGS_SET(flags, WRITE_STRING_FILE_SUPPRESS_REDUNDANT_VIRTUAL) ? O_RDWR : O_WRONLY) |
92,149✔
342
                                (FLAGS_SET(flags, WRITE_STRING_FILE_OPEN_NONBLOCKING) ? O_NONBLOCK : 0),
92,149✔
343
                                mode,
344
                                &made_file);
345
                if (call_label_ops_post)
92,149✔
346
                        /* If openat_report_new() failed in the above, propagate the error code, and ignore
347
                         * failures in label_ops_post(). */
348
                        RET_GATHER(r, label_ops_post(fd >= 0 ? fd : dir_fd, fd >= 0 ? NULL : fn, made_file));
2,484✔
349
        }
350
        if (r < 0)
161,992✔
351
                goto fail;
1,608✔
352

353
        r = take_fdopen_unlocked(&fd, "w", &f);
160,384✔
354
        if (r < 0)
160,384✔
UNCOV
355
                goto fail;
×
356

357
        if (flags & WRITE_STRING_FILE_DISABLE_BUFFER)
160,384✔
358
                setvbuf(f, NULL, _IONBF, 0);
158,590✔
359

360
        r = write_string_stream_full(f, line, flags, ts);
160,384✔
361
        if (r < 0)
160,384✔
362
                goto fail;
26✔
363

364
        return 0;
365

366
fail:
1,635✔
367
        if (made_file)
1,635✔
UNCOV
368
                (void) unlinkat(dir_fd, fn, 0);
×
369

370
        if (!(flags & WRITE_STRING_FILE_VERIFY_ON_FAILURE))
1,635✔
371
                return r;
372

373
        f = safe_fclose(f);
1,578✔
374
        fd = safe_close(fd);
1,578✔
375

376
        /* OK, the operation failed, but let's see if the right contents in place already. If so, eat up the
377
         * error. */
378
        if (verify_file_at(dir_fd, fn, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE) || (flags & WRITE_STRING_FILE_VERIFY_IGNORE_NEWLINE)) > 0)
1,580✔
379
                return 0;
463✔
380

381
        return r;
382
}
383

384
int write_string_filef(
249✔
385
                const char *fn,
386
                WriteStringFileFlags flags,
387
                const char *format, ...) {
388

389
        _cleanup_free_ char *p = NULL;
249✔
390
        va_list ap;
249✔
391
        int r;
249✔
392

393
        va_start(ap, format);
249✔
394
        r = vasprintf(&p, format, ap);
249✔
395
        va_end(ap);
249✔
396

397
        if (r < 0)
249✔
398
                return -ENOMEM;
399

400
        return write_string_file(fn, p, flags);
249✔
401
}
402

403
int write_base64_file_at(
15✔
404
                int dir_fd,
405
                const char *fn,
406
                const struct iovec *data,
407
                WriteStringFileFlags flags) {
408

409
        _cleanup_free_ char *encoded = NULL;
15✔
410
        ssize_t n;
15✔
411

412
        n = base64mem_full(data ? data->iov_base : NULL, data ? data->iov_len : 0, 79, &encoded);
15✔
413
        if (n < 0)
15✔
UNCOV
414
                return n;
×
415

416
        return write_string_file_at(dir_fd, fn, encoded, flags);
15✔
417
}
418

419
int read_one_line_file_at(int dir_fd, const char *filename, char **ret) {
205,484✔
420
        _cleanup_fclose_ FILE *f = NULL;
205,484✔
421
        int r;
205,484✔
422

423
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
205,484✔
424
        assert(filename);
205,484✔
425
        assert(ret);
205,484✔
426

427
        r = fopen_unlocked_at(dir_fd, filename, "re", 0, &f);
205,484✔
428
        if (r < 0)
205,484✔
429
                return r;
430

431
        return read_line(f, LONG_LINE_MAX, ret);
205,484✔
432
}
433

434
int verify_file_at(int dir_fd, const char *fn, const char *blob, bool accept_extra_nl) {
1,578✔
435
        _cleanup_fclose_ FILE *f = NULL;
1,578✔
436
        _cleanup_free_ char *buf = NULL;
1,578✔
437
        size_t l, k;
1,578✔
438
        int r;
1,578✔
439

440
        assert(blob);
1,578✔
441

442
        l = strlen(blob);
1,578✔
443

444
        if (accept_extra_nl && endswith(blob, "\n"))
1,578✔
445
                accept_extra_nl = false;
1✔
446

447
        buf = malloc(l + accept_extra_nl + 1);
1,578✔
448
        if (!buf)
1,578✔
449
                return -ENOMEM;
450

451
        r = fopen_unlocked_at(dir_fd, strempty(fn), "re", 0, &f);
1,578✔
452
        if (r < 0)
1,578✔
453
                return r;
454

455
        /* We try to read one byte more than we need, so that we know whether we hit eof */
456
        errno = 0;
880✔
457
        k = fread(buf, 1, l + accept_extra_nl + 1, f);
880✔
458
        if (ferror(f))
880✔
UNCOV
459
                return errno_or_else(EIO);
×
460

461
        if (k != l && k != l + accept_extra_nl)
880✔
462
                return 0;
463
        if (memcmp(buf, blob, l) != 0)
750✔
464
                return 0;
465
        if (k > l && buf[l] != '\n')
463✔
UNCOV
466
                return 0;
×
467

468
        return 1;
469
}
470

471
int read_virtual_file_at(
844,452✔
472
                int dir_fd,
473
                const char *filename,
474
                size_t max_size,
475
                char **ret_contents,
476
                size_t *ret_size) {
477

478
        _cleanup_free_ char *buf = NULL;
844,452✔
479
        size_t n, size;
844,452✔
480
        int n_retries;
844,452✔
481
        bool truncated = false;
844,452✔
482

483
        /* Virtual filesystems such as sysfs or procfs use kernfs, and kernfs can work with two sorts of
484
         * virtual files. One sort uses "seq_file", and the results of the first read are buffered for the
485
         * second read. The other sort uses "raw" reads which always go direct to the device. In the latter
486
         * case, the content of the virtual file must be retrieved with a single read otherwise a second read
487
         * might get the new value instead of finding EOF immediately. That's the reason why the usage of
488
         * fread(3) is prohibited in this case as it always performs a second call to read(2) looking for
489
         * EOF. See issue #13585.
490
         *
491
         * max_size specifies a limit on the bytes read. If max_size is SIZE_MAX, the full file is read. If
492
         * the full file is too large to read, an error is returned. For other values of max_size, *partial
493
         * contents* may be returned. (Though the read is still done using one syscall.) Returns 0 on
494
         * partial success, 1 if untruncated contents were read.
495
         *
496
         * Rule: for kernfs files using "seq_file" → use regular read_full_file_at()
497
         *       for kernfs files using "raw" → use read_virtual_file_at()
498
         */
499

500
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
844,452✔
501
        assert(max_size <= READ_VIRTUAL_BYTES_MAX || max_size == SIZE_MAX);
844,452✔
502

503
        _cleanup_close_ int fd = -EBADF;
844,452✔
504
        if (isempty(filename))
844,452✔
505
                fd = fd_reopen(ASSERT_FD(dir_fd), O_RDONLY | O_NOCTTY | O_CLOEXEC);
673,978✔
506
        else
507
                fd = RET_NERRNO(openat(dir_fd, filename, O_RDONLY | O_NOCTTY | O_CLOEXEC));
170,474✔
508
        if (fd < 0)
844,452✔
509
                return fd;
510

511
        /* Limit the number of attempts to read the number of bytes returned by fstat(). */
512
        n_retries = 3;
513

514
        for (;;) {
790,223✔
515
                struct stat st;
790,222✔
516

517
                if (fstat(fd, &st) < 0)
790,222✔
518
                        return -errno;
1,213✔
519

520
                if (!S_ISREG(st.st_mode))
790,222✔
521
                        return -EBADF;
522

523
                /* Be prepared for files from /proc which generally report a file size of 0. */
524
                assert_cc(READ_VIRTUAL_BYTES_MAX < SSIZE_MAX);
790,222✔
525
                if (st.st_size > 0 && n_retries > 1) {
790,222✔
526
                        /* Let's use the file size if we have more than 1 attempt left. On the last attempt
527
                         * we'll ignore the file size */
528

529
                        if (st.st_size > SSIZE_MAX) { /* Avoid overflow with 32-bit size_t and 64-bit off_t. */
616,460✔
530

531
                                if (max_size == SIZE_MAX)
532
                                        return -EFBIG;
533

534
                                size = max_size;
535
                        } else {
536
                                size = MIN((size_t) st.st_size, max_size);
616,460✔
537

538
                                if (size > READ_VIRTUAL_BYTES_MAX)
616,460✔
539
                                        return -EFBIG;
540
                        }
541

542
                        n_retries--;
616,460✔
543
                } else if (n_retries > 1) {
173,762✔
544
                        /* Files in /proc are generally smaller than the page size so let's start with
545
                         * a page size buffer from malloc and only use the max buffer on the final try. */
546
                        size = MIN3(page_size() - 1, READ_VIRTUAL_BYTES_MAX, max_size);
173,761✔
547
                        n_retries = 1;
173,761✔
548
                } else {
549
                        size = MIN(READ_VIRTUAL_BYTES_MAX, max_size);
1✔
550
                        n_retries = 0;
1✔
551
                }
552

553
                buf = malloc(size + 1);
790,222✔
554
                if (!buf)
790,222✔
555
                        return -ENOMEM;
556

557
                /* Use a bigger allocation if we got it anyway, but not more than the limit. */
558
                size = MIN3(MALLOC_SIZEOF_SAFE(buf) - 1, max_size, READ_VIRTUAL_BYTES_MAX);
790,222✔
559

560
                for (;;) {
790,222✔
561
                        ssize_t k;
790,222✔
562

563
                        /* Read one more byte so we can detect whether the content of the
564
                         * file has already changed or the guessed size for files from /proc
565
                         * wasn't large enough . */
566
                        k = read(fd, buf, size + 1);
790,222✔
567
                        if (k >= 0) {
790,222✔
568
                                n = k;
789,009✔
569
                                break;
789,009✔
570
                        }
571

572
                        if (errno != EINTR)
1,213✔
573
                                return -errno;
1,213✔
574
                }
575

576
                /* Consider a short read as EOF */
577
                if (n <= size)
789,009✔
578
                        break;
579

580
                /* If a maximum size is specified and we already read more we know the file is larger, and
581
                 * can handle this as truncation case. Note that if the size of what we read equals the
582
                 * maximum size then this doesn't mean truncation, the file might or might not end on that
583
                 * byte. We need to rerun the loop in that case, with a larger buffer size, so that we read
584
                 * at least one more byte to be able to distinguish EOF from truncation. */
585
                if (max_size != SIZE_MAX && n > max_size) {
32,178✔
586
                        n = size; /* Make sure we never use more than what we sized the buffer for (so that
587
                                   * we have one free byte in it for the trailing NUL we add below). */
588
                        truncated = true;
589
                        break;
590
                }
591

592
                /* We have no further attempts left? Then the file is apparently larger than our limits. Give up. */
593
                if (n_retries <= 0)
1✔
594
                        return -EFBIG;
595

596
                /* Hmm... either we read too few bytes from /proc or less likely the content of the file
597
                 * might have been changed (and is now bigger) while we were processing, let's try again
598
                 * either with the new file size. */
599

600
                if (lseek(fd, 0, SEEK_SET) < 0)
1✔
UNCOV
601
                        return -errno;
×
602

603
                buf = mfree(buf);
1✔
604
        }
605

606
        if (ret_contents) {
789,008✔
607

608
                /* Safety check: if the caller doesn't want to know the size of what we just read it will
609
                 * rely on the trailing NUL byte. But if there's an embedded NUL byte, then we should refuse
610
                 * operation as otherwise there'd be ambiguity about what we just read. */
611
                if (!ret_size && memchr(buf, 0, n))
757,076✔
612
                        return -EBADMSG;
613

614
                if (n < size) {
757,076✔
615
                        char *p;
748,558✔
616

617
                        /* Return rest of the buffer to libc */
618
                        p = realloc(buf, n + 1);
748,558✔
619
                        if (!p)
748,558✔
620
                                return -ENOMEM;
621
                        buf = p;
748,558✔
622
                }
623

624
                buf[n] = 0;
757,076✔
625
                *ret_contents = TAKE_PTR(buf);
757,076✔
626
        }
627

628
        if (ret_size)
789,008✔
629
                *ret_size = n;
630,174✔
630

631
        return !truncated;
789,008✔
632
}
633

634
int read_full_stream_full(
537,641✔
635
                FILE *f,
636
                const char *filename,
637
                uint64_t offset,
638
                size_t size,
639
                ReadFullFileFlags flags,
640
                char **ret_contents,
641
                size_t *ret_size) {
642

643
        _cleanup_free_ char *buf = NULL;
537,641✔
644
        size_t n, n_next = 0, l, expected_decoded_size = size;
537,641✔
645
        int fd, r;
537,641✔
646

647
        assert(f);
537,641✔
648
        assert(ret_contents);
537,641✔
649
        assert(!FLAGS_SET(flags, READ_FULL_FILE_UNBASE64 | READ_FULL_FILE_UNHEX));
537,641✔
650
        assert(size != SIZE_MAX || !FLAGS_SET(flags, READ_FULL_FILE_FAIL_WHEN_LARGER));
537,641✔
651

652
        if (offset != UINT64_MAX && offset > LONG_MAX) /* fseek() can only deal with "long" offsets */
537,641✔
653
                return -ERANGE;
654

655
        if ((flags & (READ_FULL_FILE_UNBASE64 | READ_FULL_FILE_UNHEX)) != 0) {
537,641✔
656
                if (size <= SIZE_MAX / READ_FULL_FILE_ENCODED_STRING_AMPLIFICATION_BOUNDARY)
64✔
657
                        size *= READ_FULL_FILE_ENCODED_STRING_AMPLIFICATION_BOUNDARY;
62✔
658
                else
659
                        size = SIZE_MAX;
660
        }
661

662
        fd = fileno(f);
537,641✔
663
        if (fd >= 0) { /* If the FILE* object is backed by an fd (as opposed to memory or such, see
537,641✔
664
                        * fmemopen()), let's optimize our buffering */
665
                struct stat st;
537,571✔
666

667
                if (fstat(fd, &st) < 0)
537,571✔
668
                        return -errno;
×
669

670
                if (FLAGS_SET(flags, READ_FULL_FILE_VERIFY_REGULAR)) {
537,571✔
UNCOV
671
                        r = stat_verify_regular(&st);
×
UNCOV
672
                        if (r < 0)
×
673
                                return r;
674
                }
675

676
                if (S_ISREG(st.st_mode)) {
537,571✔
677

678
                        /* Try to start with the right file size if we shall read the file in full. Note
679
                         * that we increase the size to read here by one, so that the first read attempt
680
                         * already makes us notice the EOF. If the reported size of the file is zero, we
681
                         * avoid this logic however, since quite likely it might be a virtual file in procfs
682
                         * that all report a zero file size. */
683

684
                        if (st.st_size > 0 &&
536,669✔
685
                            (size == SIZE_MAX || FLAGS_SET(flags, READ_FULL_FILE_FAIL_WHEN_LARGER))) {
1,328✔
686

687
                                uint64_t rsize =
1,525,569✔
688
                                        LESS_BY((uint64_t) st.st_size, offset == UINT64_MAX ? 0 : offset);
508,523✔
689

690
                                if (rsize < SIZE_MAX) /* overflow check */
508,523✔
691
                                        n_next = rsize + 1;
508,523✔
692
                        }
693

694
                        if (flags & READ_FULL_FILE_WARN_WORLD_READABLE)
536,669✔
695
                                (void) warn_file_is_world_accessible(filename, &st, NULL, 0);
140✔
696
                }
697
        } else if (FLAGS_SET(flags, READ_FULL_FILE_VERIFY_REGULAR))
70✔
698
                return -EBADFD;
699

700
        /* If we don't know how much to read, figure it out now. If we shall read a part of the file, then
701
         * allocate the requested size. If we shall load the full file start with LINE_MAX. Note that if
702
         * READ_FULL_FILE_FAIL_WHEN_LARGER we consider the specified size a safety limit, and thus also start
703
         * with LINE_MAX, under assumption the file is most likely much shorter. */
704
        if (n_next == 0)
537,571✔
705
                n_next = size != SIZE_MAX && !FLAGS_SET(flags, READ_FULL_FILE_FAIL_WHEN_LARGER) ? size : LINE_MAX;
29,118✔
706

707
        /* Never read more than we need to determine that our own limit is hit */
708
        if (n_next > READ_FULL_BYTES_MAX)
508,716✔
709
                n_next = READ_FULL_BYTES_MAX + 1;
×
710

711
        if (offset != UINT64_MAX && fseek(f, offset, SEEK_SET) < 0)
537,641✔
UNCOV
712
                return -errno;
×
713

714
        n = l = 0;
715
        for (;;) {
538,173✔
716
                char *t;
537,907✔
717
                size_t k;
537,907✔
718

719
                /* If we shall fail when reading overly large data, then read exactly one byte more than the
720
                 * specified size at max, since that'll tell us if there's anymore data beyond the limit. */
721
                if (FLAGS_SET(flags, READ_FULL_FILE_FAIL_WHEN_LARGER) && n_next > size)
537,907✔
722
                        n_next = size + 1;
8✔
723

724
                if (flags & READ_FULL_FILE_SECURE) {
537,907✔
725
                        t = malloc(n_next + 1);
1,562✔
726
                        if (!t) {
1,562✔
UNCOV
727
                                r = -ENOMEM;
×
UNCOV
728
                                goto finalize;
×
729
                        }
730
                        memcpy_safe(t, buf, n);
1,562✔
731
                        explicit_bzero_safe(buf, n);
1,562✔
732
                        free(buf);
1,562✔
733
                } else {
734
                        t = realloc(buf, n_next + 1);
536,345✔
735
                        if (!t)
536,345✔
736
                                return -ENOMEM;
737
                }
738

739
                buf = t;
537,907✔
740
                /* Unless a size has been explicitly specified, try to read as much as fits into the memory
741
                 * we allocated (minus 1, to leave one byte for the safety NUL byte) */
742
                n = size == SIZE_MAX ? MALLOC_SIZEOF_SAFE(buf) - 1 : n_next;
537,907✔
743

744
                errno = 0;
537,907✔
745
                k = fread(buf + l, 1, n - l, f);
537,907✔
746

747
                assert(k <= n - l);
537,907✔
748
                l += k;
537,907✔
749

750
                if (ferror(f)) {
537,907✔
751
                        r = errno_or_else(EIO);
2✔
752
                        goto finalize;
2✔
753
                }
754
                if (feof(f))
537,905✔
755
                        break;
756

757
                if (size != SIZE_MAX && !FLAGS_SET(flags, READ_FULL_FILE_FAIL_WHEN_LARGER)) { /* If we got asked to read some specific size, we already sized the buffer right, hence leave */
363✔
758
                        assert(l == size);
91✔
759
                        break;
760
                }
761

762
                assert(k > 0); /* we can't have read zero bytes because that would have been EOF */
272✔
763

764
                if (FLAGS_SET(flags, READ_FULL_FILE_FAIL_WHEN_LARGER) && l > size) {
272✔
765
                        r = -E2BIG;
6✔
766
                        goto finalize;
6✔
767
                }
768

769
                if (n >= READ_FULL_BYTES_MAX) {
266✔
UNCOV
770
                        r = -E2BIG;
×
UNCOV
771
                        goto finalize;
×
772
                }
773

774
                n_next = MIN(n * 2, READ_FULL_BYTES_MAX);
266✔
775
        }
776

777
        if (flags & (READ_FULL_FILE_UNBASE64 | READ_FULL_FILE_UNHEX)) {
537,633✔
778
                _cleanup_free_ void *decoded = NULL;
62✔
779
                size_t decoded_size;
62✔
780

781
                buf[l++] = 0;
62✔
782
                if (flags & READ_FULL_FILE_UNBASE64)
62✔
783
                        r = unbase64mem_full(buf, l, flags & READ_FULL_FILE_SECURE, &decoded, &decoded_size);
60✔
784
                else
785
                        r = unhexmem_full(buf, l, flags & READ_FULL_FILE_SECURE, &decoded, &decoded_size);
2✔
786
                if (r < 0)
62✔
UNCOV
787
                        goto finalize;
×
788

789
                if (flags & READ_FULL_FILE_SECURE)
62✔
790
                        explicit_bzero_safe(buf, n);
19✔
791
                free_and_replace(buf, decoded);
62✔
792
                n = l = decoded_size;
62✔
793

794
                if (FLAGS_SET(flags, READ_FULL_FILE_FAIL_WHEN_LARGER) && l > expected_decoded_size) {
62✔
UNCOV
795
                        r = -E2BIG;
×
UNCOV
796
                        goto finalize;
×
797
                }
798
        }
799

800
        if (!ret_size) {
537,633✔
801
                /* Safety check: if the caller doesn't want to know the size of what we just read it will rely on the
802
                 * trailing NUL byte. But if there's an embedded NUL byte, then we should refuse operation as otherwise
803
                 * there'd be ambiguity about what we just read. */
804

805
                if (memchr(buf, 0, l)) {
368,263✔
806
                        r = -EBADMSG;
2✔
807
                        goto finalize;
2✔
808
                }
809
        }
810

811
        buf[l] = 0;
537,631✔
812
        *ret_contents = TAKE_PTR(buf);
537,631✔
813

814
        if (ret_size)
537,631✔
815
                *ret_size = l;
169,370✔
816

817
        return 0;
818

819
finalize:
10✔
820
        if (flags & READ_FULL_FILE_SECURE)
10✔
821
                explicit_bzero_safe(buf, n);
3✔
822

823
        return r;
824
}
825

826
int read_full_file_full(
734,758✔
827
                int dir_fd,
828
                const char *filename,
829
                uint64_t offset,
830
                size_t size,
831
                ReadFullFileFlags flags,
832
                const char *bind_name,
833
                char **ret_contents,
834
                size_t *ret_size) {
835

836
        _cleanup_fclose_ FILE *f = NULL;
734,758✔
837
        XfopenFlags xflags = XFOPEN_UNLOCKED;
734,758✔
838
        int r;
734,758✔
839

840
        assert(ret_contents);
734,758✔
841

842
        if (FLAGS_SET(flags, READ_FULL_FILE_CONNECT_SOCKET) && /* If this is enabled, let's try to connect to it */
734,758✔
843
            offset == UINT64_MAX)                              /* Seeking is not supported on AF_UNIX sockets */
844
                xflags |= XFOPEN_SOCKET;
211✔
845

846
        r = xfopenat_full(dir_fd, filename, "re", 0, xflags, bind_name, &f);
734,758✔
847
        if (r < 0)
734,758✔
848
                return r;
849

850
        return read_full_stream_full(f, filename, offset, size, flags, ret_contents, ret_size);
516,012✔
851
}
852

853
int script_get_shebang_interpreter(const char *path, char **ret) {
8✔
854
        _cleanup_fclose_ FILE *f = NULL;
8✔
855
        int r;
8✔
856

857
        assert(path);
8✔
858

859
        f = fopen(path, "re");
8✔
860
        if (!f)
8✔
861
                return -errno;
1✔
862

863
        char c;
7✔
864
        r = safe_fgetc(f, &c);
7✔
865
        if (r < 0)
7✔
866
                return r;
867
        if (r == 0)
7✔
868
                return -EBADMSG;
869
        if (c != '#')
7✔
870
                return -EMEDIUMTYPE;
871
        r = safe_fgetc(f, &c);
2✔
872
        if (r < 0)
2✔
873
                return r;
874
        if (r == 0)
2✔
875
                return -EBADMSG;
876
        if (c != '!')
2✔
877
                return -EMEDIUMTYPE;
878

879
        _cleanup_free_ char *line = NULL;
2✔
880
        r = read_line(f, LONG_LINE_MAX, &line);
2✔
881
        if (r < 0)
2✔
882
                return r;
883

884
        _cleanup_free_ char *p = NULL;
2✔
885
        const char *s = line;
2✔
886

887
        r = extract_first_word(&s, &p, /* separators= */ NULL, /* flags= */ 0);
2✔
888
        if (r < 0)
2✔
889
                return r;
890
        if (r == 0)
2✔
891
                return -ENOEXEC;
892

893
        if (ret)
2✔
894
                *ret = TAKE_PTR(p);
2✔
895
        return 0;
896
}
897

898
int get_proc_field(const char *path, const char *key, char **ret) {
42,275✔
899
        _cleanup_fclose_ FILE *f = NULL;
42,275✔
900
        int r;
42,275✔
901

902
        /* Retrieve one field from a file like /proc/self/status. "key" matches the beginning of the line
903
         * and should not include whitespace or the delimiter (':').
904
         * Whitespaces after the ':' will be skipped. Only the first element is returned
905
         * (i.e. for /proc/meminfo line "MemTotal: 1024 kB" -> return "1024"). */
906

907
        assert(path);
42,275✔
908
        assert(key);
42,275✔
909

910
        r = fopen_unlocked(path, "re", &f);
42,275✔
911
        if (r == -ENOENT && proc_mounted() == 0)
42,275✔
912
                return -ENOSYS;
913
        if (r < 0)
42,261✔
914
                return r;
915

916
        for (;;) {
224,983✔
917
                 _cleanup_free_ char *line = NULL;
130,561✔
918

919
                 r = read_line(f, LONG_LINE_MAX, &line);
130,561✔
920
                 if (r < 0)
130,561✔
921
                         return r;
922
                 if (r == 0)
130,561✔
923
                         return -ENODATA;
924

925
                 char *l = startswith(line, key);
130,561✔
926
                 if (l && *l == ':') {
130,561✔
927
                         if (ret) {
36,139✔
928
                                 char *s = strdupcspn(skip_leading_chars(l + 1, " \t"), WHITESPACE);
36,139✔
929
                                 if (!s)
36,139✔
930
                                         return -ENOMEM;
931

932
                                 *ret = s;
36,139✔
933
                         }
934

935
                         return 0;
36,139✔
936
                 }
937
        }
938
}
939

940
DIR* xopendirat(int dir_fd, const char *path, int flags) {
191,192✔
941
        _cleanup_close_ int fd = -EBADF;
191,192✔
942

943
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
191,192✔
944
        assert(!(flags & (O_CREAT|O_TMPFILE)));
191,192✔
945

946
        if ((dir_fd == AT_FDCWD || path_is_absolute(path)) &&
192,937✔
947
            (flags &~ O_DIRECTORY) == 0)
1,373✔
UNCOV
948
                return opendir(path);
×
949

950
        if (isempty(path)) {
191,192✔
951
                path = ".";
188,074✔
952
                flags |= O_NOFOLLOW;
188,074✔
953
        }
954

955
        fd = openat(dir_fd, path, O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|flags);
191,192✔
956
        if (fd < 0)
191,192✔
957
                return NULL;
958

959
        return take_fdopendir(&fd);
190,701✔
960
}
961

962
int fopen_mode_to_flags(const char *mode) {
85,766✔
963
        const char *p;
85,766✔
964
        int flags;
85,766✔
965

966
        assert(mode);
85,766✔
967

968
        if ((p = startswith(mode, "r+")))
85,766✔
969
                flags = O_RDWR;
970
        else if ((p = startswith(mode, "r")))
85,765✔
971
                flags = O_RDONLY;
UNCOV
972
        else if ((p = startswith(mode, "w+")))
×
973
                flags = O_RDWR|O_CREAT|O_TRUNC;
UNCOV
974
        else if ((p = startswith(mode, "w")))
×
975
                flags = O_WRONLY|O_CREAT|O_TRUNC;
UNCOV
976
        else if ((p = startswith(mode, "a+")))
×
977
                flags = O_RDWR|O_CREAT|O_APPEND;
UNCOV
978
        else if ((p = startswith(mode, "a")))
×
979
                flags = O_WRONLY|O_CREAT|O_APPEND;
980
        else
981
                return -EINVAL;
982

983
        for (; *p != 0; p++) {
167,026✔
984

985
                switch (*p) {
81,260✔
986

987
                case 'e':
81,260✔
988
                        flags |= O_CLOEXEC;
81,260✔
989
                        break;
81,260✔
990

UNCOV
991
                case 'x':
×
UNCOV
992
                        flags |= O_EXCL;
×
UNCOV
993
                        break;
×
994

995
                case 'm':
996
                        /* ignore this here, fdopen() might care later though */
997
                        break;
998

999
                case 'c': /* not sure what to do about this one */
1000
                default:
1001
                        return -EINVAL;
1002
                }
1003
        }
1004

1005
        return flags;
1006
}
1007

1008
static int xfopenat_regular(int dir_fd, const char *path, const char *mode, int open_flags, FILE **ret) {
1,048,924✔
1009
        FILE *f;
1,048,924✔
1010

1011
        /* A combination of fopen() with openat() */
1012

1013
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
1,048,924✔
1014
        assert(mode);
1,048,924✔
1015
        assert(ret);
1,048,924✔
1016

1017
        if (dir_fd == AT_FDCWD && path && open_flags == 0)
1,048,924✔
1018
                f = fopen(path, mode);
1,047,552✔
1019
        else {
1020
                _cleanup_close_ int fd = -EBADF;
1,372✔
1021
                int mode_flags;
1,372✔
1022

1023
                mode_flags = fopen_mode_to_flags(mode);
1,372✔
1024
                if (mode_flags < 0)
1,372✔
1025
                        return mode_flags;
1026

1027
                if (path) {
1,372✔
1028
                        fd = openat(dir_fd, path, mode_flags | open_flags);
1,268✔
1029
                        if (fd < 0)
1,268✔
1030
                                return -errno;
481✔
1031
                } else {
1032
                        if (dir_fd == AT_FDCWD)
104✔
1033
                                return -EBADF;
1034

1035
                        fd = fd_reopen(dir_fd, (mode_flags | open_flags) & ~O_NOFOLLOW);
104✔
1036
                        if (fd < 0)
104✔
1037
                                return fd;
1038
                }
1039

1040
                f = take_fdopen(&fd, mode);
891✔
1041
        }
1042
        if (!f)
1,048,443✔
1043
                return -errno;
255,058✔
1044

1045
        *ret = f;
793,385✔
1046
        return 0;
793,385✔
1047
}
1048

1049
static int xfopenat_unix_socket(int dir_fd, const char *path, const char *bind_name, FILE **ret) {
1✔
1050
        _cleanup_close_ int sk = -EBADF;
1✔
1051
        FILE *f;
1✔
1052
        int r;
1✔
1053

1054
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
1✔
1055
        assert(ret);
1✔
1056

1057
        sk = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
1✔
1058
        if (sk < 0)
1✔
UNCOV
1059
                return -errno;
×
1060

1061
        if (bind_name) {
1✔
1062
                /* If the caller specified a socket name to bind to, do so before connecting. This is
1063
                 * useful to communicate some minor, short meta-information token from the client to
1064
                 * the server. */
1065
                union sockaddr_union bsa;
1✔
1066

1067
                r = sockaddr_un_set_path(&bsa.un, bind_name);
1✔
1068
                if (r < 0)
1✔
1069
                        return r;
×
1070

1071
                if (bind(sk, &bsa.sa, r) < 0)
1✔
UNCOV
1072
                        return -errno;
×
1073
        }
1074

1075
        r = connect_unix_path(sk, dir_fd, path);
1✔
1076
        if (r < 0)
1✔
1077
                return r;
1078

1079
        if (shutdown(sk, SHUT_WR) < 0)
1✔
UNCOV
1080
                return -errno;
×
1081

1082
        f = take_fdopen(&sk, "r");
1✔
1083
        if (!f)
1✔
UNCOV
1084
                return -errno;
×
1085

1086
        *ret = f;
1✔
1087
        return 0;
1✔
1088
}
1089

1090
int xfopenat_full(
1,048,924✔
1091
                int dir_fd,
1092
                const char *path,
1093
                const char *mode,
1094
                int open_flags,
1095
                XfopenFlags flags,
1096
                const char *bind_name,
1097
                FILE **ret) {
1098

1099
        FILE *f = NULL;  /* avoid false maybe-uninitialized warning */
1,048,924✔
1100
        int r;
1,048,924✔
1101

1102
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
1,048,924✔
1103
        assert(mode);
1,048,924✔
1104
        assert(ret);
1,048,924✔
1105

1106
        r = xfopenat_regular(dir_fd, path, mode, open_flags, &f);
1,048,924✔
1107
        if (r == -ENXIO && FLAGS_SET(flags, XFOPEN_SOCKET)) {
1,048,924✔
1108
                /* ENXIO is what Linux returns if we open a node that is an AF_UNIX socket */
1109
                r = xfopenat_unix_socket(dir_fd, path, bind_name, &f);
1✔
1110
                if (IN_SET(r, -ENOTSOCK, -EINVAL))
1✔
1111
                        return -ENXIO; /* propagate original error if this is not a socket after all */
1,048,924✔
1112
        }
1113
        if (r < 0)
1,048,924✔
1114
                return r;
1115

1116
        if (FLAGS_SET(flags, XFOPEN_UNLOCKED))
793,386✔
1117
                (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
793,146✔
1118

1119
        *ret = f;
793,386✔
1120
        return 0;
793,386✔
1121
}
1122

1123
int fdopen_independent(int fd, const char *mode, FILE **ret) {
14,371✔
1124
        _cleanup_close_ int copy_fd = -EBADF;
14,371✔
1125
        _cleanup_fclose_ FILE *f = NULL;
14,371✔
1126
        int mode_flags;
14,371✔
1127

1128
        assert(fd >= 0);
14,371✔
1129
        assert(mode);
14,371✔
1130
        assert(ret);
14,371✔
1131

1132
        /* A combination of fdopen() + fd_reopen(). i.e. reopens the inode the specified fd points to and
1133
         * returns a FILE* for it */
1134

1135
        mode_flags = fopen_mode_to_flags(mode);
14,371✔
1136
        if (mode_flags < 0)
14,371✔
1137
                return mode_flags;
1138

1139
        /* Flags returned by fopen_mode_to_flags might contain O_CREAT, but it doesn't make sense for fd_reopen
1140
         * since we're working on an existing fd anyway. Let's drop it here to avoid triggering assertion. */
1141
        copy_fd = fd_reopen(fd, mode_flags & ~O_CREAT);
14,371✔
1142
        if (copy_fd < 0)
14,371✔
1143
                return copy_fd;
1144

1145
        f = take_fdopen(&copy_fd, mode);
14,371✔
1146
        if (!f)
14,371✔
UNCOV
1147
                return -errno;
×
1148

1149
        *ret = TAKE_PTR(f);
14,371✔
1150
        return 0;
14,371✔
1151
}
1152

1153
static int search_and_open_internal(
27,918✔
1154
                const char *path,
1155
                int mode,            /* if ret_fd is NULL this is an [FRWX]_OK mode for access(), otherwise an open mode for open() */
1156
                const char *root,
1157
                char **search,
1158
                int *ret_fd,
1159
                char **ret_path) {
1160

1161
        int r;
27,918✔
1162

1163
        assert(!ret_fd || !FLAGS_SET(mode, O_CREAT)); /* We don't support O_CREAT for this */
27,918✔
1164
        assert(path);
27,918✔
1165

1166
        if (path_is_absolute(path)) {
27,918✔
UNCOV
1167
                _cleanup_close_ int fd = -EBADF;
×
1168

1169
                if (ret_fd)
15✔
1170
                        /* We only specify 0777 here to appease static analyzers, it's never used since we
1171
                         * don't support O_CREAT here */
1172
                        r = fd = RET_NERRNO(open(path, mode, 0777));
13✔
1173
                else
1174
                        r = RET_NERRNO(access(path, mode));
2✔
1175
                if (r < 0)
4✔
1176
                        return r;
1177

1178
                if (ret_path) {
11✔
1179
                        r = path_simplify_alloc(path, ret_path);
11✔
1180
                        if (r < 0)
11✔
1181
                                return r;
1182
                }
1183

1184
                if (ret_fd)
11✔
1185
                        *ret_fd = TAKE_FD(fd);
10✔
1186

1187
                return 0;
11✔
1188
        }
1189

1190
        if (!path_strv_resolve_uniq(search, root))
27,903✔
1191
                return -ENOMEM;
1192

1193
        STRV_FOREACH(i, search) {
144,004✔
1194
                _cleanup_close_ int fd = -EBADF;
144,019✔
1195
                _cleanup_free_ char *p = NULL;
120,714✔
1196

1197
                p = path_join(root, *i, path);
120,714✔
1198
                if (!p)
120,714✔
1199
                        return -ENOMEM;
1200

1201
                if (ret_fd)
120,714✔
1202
                        /* as above, 0777 is static analyzer appeasement */
1203
                        r = fd = RET_NERRNO(open(p, mode, 0777));
120,274✔
1204
                else
1205
                        r = RET_NERRNO(access(p, F_OK));
440✔
1206
                if (r >= 0) {
116,101✔
1207
                        if (ret_path)
4,613✔
1208
                                *ret_path = path_simplify(TAKE_PTR(p));
4,613✔
1209

1210
                        if (ret_fd)
4,613✔
1211
                                *ret_fd = TAKE_FD(fd);
4,601✔
1212

1213
                        return 0;
4,613✔
1214
                }
1215
                if (r != -ENOENT)
116,101✔
1216
                        return r;
1217
        }
1218

1219
        return -ENOENT;
1220
}
1221

1222
int search_and_open(
27,918✔
1223
                const char *path,
1224
                int mode,
1225
                const char *root,
1226
                char **search,
1227
                int *ret_fd,
1228
                char **ret_path) {
1229

1230
        _cleanup_strv_free_ char **copy = NULL;
27,918✔
1231

1232
        assert(path);
27,918✔
1233

1234
        copy = strv_copy(search);
27,918✔
1235
        if (!copy)
27,918✔
1236
                return -ENOMEM;
1237

1238
        return search_and_open_internal(path, mode, root, copy, ret_fd, ret_path);
27,918✔
1239
}
1240

1241
static int search_and_fopen_internal(
27,908✔
1242
                const char *path,
1243
                const char *mode,
1244
                const char *root,
1245
                char **search,
1246
                FILE **ret_file,
1247
                char **ret_path) {
1248

1249
        _cleanup_free_ char *found_path = NULL;
27,908✔
1250
        _cleanup_close_ int fd = -EBADF;
27,908✔
1251
        int r;
27,908✔
1252

1253
        assert(path);
27,908✔
1254
        assert(mode || !ret_file);
27,908✔
1255

1256
        r = search_and_open(
55,923✔
1257
                        path,
1258
                        mode ? fopen_mode_to_flags(mode) : 0,
27,706✔
1259
                        root,
1260
                        search,
1261
                        ret_file ? &fd : NULL,
1262
                        ret_path ? &found_path : NULL);
1263
        if (r < 0)
27,908✔
1264
                return r;
1265

1266
        if (ret_file) {
4,614✔
1267
                FILE *f = take_fdopen(&fd, mode);
4,611✔
1268
                if (!f)
4,611✔
UNCOV
1269
                        return -errno;
×
1270

1271
                *ret_file = f;
4,611✔
1272
        }
1273

1274
        if (ret_path)
4,614✔
1275
                *ret_path = TAKE_PTR(found_path);
4,614✔
1276

1277
        return 0;
1278
}
1279

1280
int search_and_fopen(
86✔
1281
                const char *path,
1282
                const char *mode,
1283
                const char *root,
1284
                const char **search,
1285
                FILE **ret_file,
1286
                char **ret_path) {
1287

1288
        _cleanup_strv_free_ char **copy = NULL;
86✔
1289

1290
        assert(path);
86✔
1291
        assert(mode || !ret_file);
86✔
1292

1293
        copy = strv_copy((char**) search);
86✔
1294
        if (!copy)
86✔
1295
                return -ENOMEM;
1296

1297
        return search_and_fopen_internal(path, mode, root, copy, ret_file, ret_path);
86✔
1298
}
1299

1300
int search_and_fopen_nulstr(
27,822✔
1301
                const char *path,
1302
                const char *mode,
1303
                const char *root,
1304
                const char *search,
1305
                FILE **ret_file,
1306
                char **ret_path) {
1307

1308
        _cleanup_strv_free_ char **l = NULL;
27,822✔
1309

1310
        assert(path);
27,822✔
1311
        assert(mode || !ret_file);
27,822✔
1312

1313
        l = strv_split_nulstr(search);
27,822✔
1314
        if (!l)
27,822✔
1315
                return -ENOMEM;
1316

1317
        return search_and_fopen_internal(path, mode, root, l, ret_file, ret_path);
27,822✔
1318
}
1319

1320
int fflush_and_check(FILE *f) {
1,125,857✔
1321
        assert(f);
1,125,857✔
1322

1323
        errno = 0;
1,125,857✔
1324
        fflush(f);
1,125,857✔
1325

1326
        if (ferror(f))
1,125,857✔
1327
                return errno_or_else(EIO);
302✔
1328

1329
        return 0;
1330
}
1331

1332
int fflush_sync_and_check(FILE *f) {
2,560✔
1333
        int r, fd;
2,560✔
1334

1335
        assert(f);
2,560✔
1336

1337
        r = fflush_and_check(f);
2,560✔
1338
        if (r < 0)
2,560✔
1339
                return r;
1340

1341
        /* Not all file streams have an fd associated (think: fmemopen()), let's handle this gracefully and
1342
         * assume that in that case we need no explicit syncing */
1343
        fd = fileno(f);
2,560✔
1344
        if (fd < 0)
2,560✔
1345
                return 0;
1346

1347
        r = fsync_full(fd);
2,560✔
1348
        if (r < 0)
2,560✔
UNCOV
1349
                return r;
×
1350

1351
        return 0;
1352
}
1353

1354
int write_timestamp_file_atomic(const char *fn, usec_t n) {
205✔
1355
        char ln[DECIMAL_STR_MAX(n)+2];
205✔
1356

1357
        /* Creates a "timestamp" file, that contains nothing but a
1358
         * usec_t timestamp, formatted in ASCII. */
1359

1360
        if (!timestamp_is_set(n))
205✔
1361
                return -ERANGE;
205✔
1362

1363
        xsprintf(ln, USEC_FMT "\n", n);
205✔
1364

1365
        return write_string_file(fn, ln, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
205✔
1366
}
1367

1368
int read_timestamp_file(const char *fn, usec_t *ret) {
27✔
1369
        _cleanup_free_ char *ln = NULL;
27✔
1370
        uint64_t t;
27✔
1371
        int r;
27✔
1372

1373
        r = read_one_line_file(fn, &ln);
27✔
1374
        if (r < 0)
27✔
1375
                return r;
1376

UNCOV
1377
        r = safe_atou64(ln, &t);
×
1378
        if (r < 0)
×
1379
                return r;
1380

1381
        if (!timestamp_is_set(t))
×
1382
                return -ERANGE;
1383

UNCOV
1384
        *ret = (usec_t) t;
×
UNCOV
1385
        return 0;
×
1386
}
1387

1388
int fputs_with_separator(FILE *f, const char *s, const char *separator, bool *space) {
3,385✔
1389
        assert(s);
3,385✔
1390
        assert(space);
3,385✔
1391

1392
        /* Outputs the specified string with fputs(), but optionally prefixes it with a separator.
1393
         * The *space parameter when specified shall initially point to a boolean variable initialized
1394
         * to false. It is set to true after the first invocation. This call is supposed to be use in loops,
1395
         * where a separator shall be inserted between each element, but not before the first one. */
1396

1397
        if (!f)
3,385✔
UNCOV
1398
                f = stdout;
×
1399

1400
        if (!separator)
3,385✔
1401
                separator = " ";
1,659✔
1402

1403
        if (*space)
3,385✔
1404
                if (fputs(separator, f) < 0)
1,509✔
1405
                        return -EIO;
1406

1407
        *space = true;
3,385✔
1408

1409
        if (fputs(s, f) < 0)
3,385✔
UNCOV
1410
                return -EIO;
×
1411

1412
        return 0;
1413
}
1414

1415
int fputs_with_newline(FILE *f, const char *s) {
3,312✔
1416

1417
        /* This is like fputs() but outputs a trailing newline char, but only if the string isn't empty
1418
         * and doesn't end in a newline already. Returns 0 in case we didn't append a newline, > 0 otherwise. */
1419

1420
        if (isempty(s))
3,312✔
1421
                return 0;
1422

1423
        if (!f)
3,312✔
UNCOV
1424
                f = stdout;
×
1425

1426
        if (fputs(s, f) < 0)
3,312✔
1427
                return -EIO;
1428

1429
        if (endswith(s, "\n"))
3,312✔
1430
                return 0;
1431

1432
        if (fputc('\n', f) < 0)
2,992✔
UNCOV
1433
                return -EIO;
×
1434

1435
        return 1;
1436
}
1437

1438
/* A bitmask of the EOL markers we know */
1439
typedef enum EndOfLineMarker {
1440
        EOL_NONE     = 0,
1441
        EOL_ZERO     = 1 << 0,  /* \0 (aka NUL) */
1442
        EOL_TEN      = 1 << 1,  /* \n (aka NL, aka LF)  */
1443
        EOL_THIRTEEN = 1 << 2,  /* \r (aka CR)  */
1444
} EndOfLineMarker;
1445

1446
static EndOfLineMarker categorize_eol(char c, ReadLineFlags flags) {
200,818,941✔
1447

1448
        if (!FLAGS_SET(flags, READ_LINE_ONLY_NUL)) {
200,818,941✔
1449
                if (c == '\n')
199,935,096✔
1450
                        return EOL_TEN;
1451
                if (c == '\r')
193,255,660✔
1452
                        return EOL_THIRTEEN;
1453
        }
1454

1455
        if (c == '\0')
194,139,489✔
1456
                return EOL_ZERO;
27,817✔
1457

1458
        return EOL_NONE;
1459
}
1460

1461
DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(FILE*, funlockfile, NULL);
6,248,227✔
1462

1463
int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret) {
6,248,227✔
1464
        _cleanup_free_ char *buffer = NULL;
6,248,227✔
1465
        size_t n = 0, count = 0;
6,248,227✔
1466
        int r;
6,248,227✔
1467

1468
        assert(f);
6,248,227✔
1469

1470
        /* Something like a bounded version of getline().
1471
         *
1472
         * Considers EOF, \n, \r and \0 end of line delimiters (or combinations of these), and does not include these
1473
         * delimiters in the string returned. Specifically, recognizes the following combinations of markers as line
1474
         * endings:
1475
         *
1476
         *     • \n        (UNIX)
1477
         *     • \r        (old MacOS)
1478
         *     • \0        (C strings)
1479
         *     • \n\0
1480
         *     • \r\0
1481
         *     • \r\n      (Windows)
1482
         *     • \n\r
1483
         *     • \r\n\0
1484
         *     • \n\r\0
1485
         *
1486
         * Returns the number of bytes read from the files (i.e. including delimiters — this hence usually differs from
1487
         * the number of characters in the returned string). When EOF is hit, 0 is returned.
1488
         *
1489
         * The input parameter limit is the maximum numbers of characters in the returned string, i.e. excluding
1490
         * delimiters. If the limit is hit we fail and return -ENOBUFS.
1491
         *
1492
         * If a line shall be skipped ret may be initialized as NULL. */
1493

1494
        if (ret) {
6,248,227✔
1495
                if (!GREEDY_REALLOC(buffer, 1))
6,248,196✔
1496
                        return -ENOMEM;
1497
        }
1498

1499
        {
1500
                _unused_ _cleanup_(funlockfilep) FILE *flocked = f;
6,248,227✔
1501
                EndOfLineMarker previous_eol = EOL_NONE;
6,248,227✔
1502
                flockfile(f);
6,248,227✔
1503

1504
                for (;;) {
201,352,794✔
1505
                        EndOfLineMarker eol;
201,352,794✔
1506
                        char c;
201,352,794✔
1507

1508
                        if (n >= limit)
201,352,794✔
1509
                                return -ENOBUFS;
12✔
1510

1511
                        if (count >= INT_MAX) /* We couldn't return the counter anymore as "int", hence refuse this */
201,352,788✔
1512
                                return -ENOBUFS;
1513

1514
                        r = safe_fgetc(f, &c);
201,352,788✔
1515
                        if (r < 0)
201,352,788✔
1516
                                return r;
1517
                        if (r == 0) /* EOF is definitely EOL */
201,352,782✔
1518
                                break;
1519

1520
                        eol = categorize_eol(c, flags);
200,818,941✔
1521

1522
                        if (FLAGS_SET(previous_eol, EOL_ZERO) ||
200,818,941✔
1523
                            (eol == EOL_NONE && previous_eol != EOL_NONE) ||
200,794,665✔
1524
                            (eol != EOL_NONE && (previous_eol & eol) != 0)) {
6,707,263✔
1525
                                /* Previous char was a NUL? This is not an EOL, but the previous char was? This type of
1526
                                 * EOL marker has been seen right before?  In either of these three cases we are
1527
                                 * done. But first, let's put this character back in the queue. (Note that we have to
1528
                                 * cast this to (unsigned char) here as ungetc() expects a positive 'int', and if we
1529
                                 * are on an architecture where 'char' equals 'signed char' we need to ensure we don't
1530
                                 * pass a negative value here. That said, to complicate things further ungetc() is
1531
                                 * actually happy with most negative characters and implicitly casts them back to
1532
                                 * positive ones as needed, except for \xff (aka -1, aka EOF), which it refuses. What a
1533
                                 * godawful API!) */
1534
                                assert_se(ungetc((unsigned char) c, f) != EOF);
5,714,374✔
1535
                                break;
1536
                        }
1537

1538
                        count++;
195,104,567✔
1539

1540
                        if (eol != EOL_NONE) {
189,002,394✔
1541
                                /* If we are on a tty, we can't shouldn't wait for more input, because that
1542
                                 * generally means waiting for the user, interactively. In the case of a TTY
1543
                                 * we expect only \n as the single EOL marker, so we are in the lucky
1544
                                 * position that there is no need to wait. We check this condition last, to
1545
                                 * avoid isatty() check if not necessary. */
1546

1547
                                if ((flags & (READ_LINE_IS_A_TTY|READ_LINE_NOT_A_TTY)) == 0) {
6,102,173✔
1548
                                        int fd;
5,351,879✔
1549

1550
                                        fd = fileno(f);
5,351,879✔
1551
                                        if (fd < 0) /* Maybe an fmemopen() stream? Handle this gracefully,
5,351,879✔
1552
                                                     * and don't call isatty() on an invalid fd */
1553
                                                flags |= READ_LINE_NOT_A_TTY;
28✔
1554
                                        else
1555
                                                flags |= isatty_safe(fd) ? READ_LINE_IS_A_TTY : READ_LINE_NOT_A_TTY;
10,703,702✔
1556
                                }
1557
                                if (FLAGS_SET(flags, READ_LINE_IS_A_TTY))
6,102,173✔
1558
                                        break;
1559
                        }
1560

1561
                        if (eol != EOL_NONE) {
195,104,567✔
1562
                                previous_eol |= eol;
6,102,173✔
1563
                                continue;
6,102,173✔
1564
                        }
1565

1566
                        if (ret) {
189,002,394✔
1567
                                if (!GREEDY_REALLOC(buffer, n + 2))
189,002,294✔
1568
                                        return -ENOMEM;
1569

1570
                                buffer[n] = c;
189,002,294✔
1571
                        }
1572

1573
                        n++;
189,002,394✔
1574
                }
1575
        }
1576

1577
        if (ret) {
6,248,215✔
1578
                buffer[n] = 0;
6,248,184✔
1579

1580
                *ret = TAKE_PTR(buffer);
6,248,184✔
1581
        }
1582

1583
        return (int) count;
6,248,215✔
1584
}
1585

1586
int read_stripped_line(FILE *f, size_t limit, char **ret) {
2,354,514✔
1587
        _cleanup_free_ char *s = NULL;
2,354,514✔
1588
        int r, k;
2,354,514✔
1589

1590
        assert(f);
2,354,514✔
1591

1592
        r = read_line(f, limit, ret ? &s : NULL);
2,354,514✔
1593
        if (r < 0)
2,354,514✔
1594
                return r;
1595

1596
        if (ret) {
2,354,514✔
1597
                const char *p = strstrip(s);
2,354,514✔
1598
                if (p == s)
2,354,514✔
1599
                        *ret = TAKE_PTR(s);
2,354,511✔
1600
                else {
1601
                        k = strdup_to(ret, p);
3✔
1602
                        if (k < 0)
3✔
1603
                                return k;
1604
                }
1605
        }
1606

1607
        return r > 0;          /* Return 1 if something was read. */
2,354,514✔
1608
}
1609

1610
int safe_fgetc(FILE *f, char *ret) {
201,360,164✔
1611
        int k;
201,360,164✔
1612

1613
        assert(f);
201,360,164✔
1614

1615
        /* A safer version of plain fgetc(): let's propagate the error that happened while reading as such, and
1616
         * separate the EOF condition from the byte read, to avoid those confusion signed/unsigned issues fgetc()
1617
         * has. */
1618

1619
        errno = 0;
201,360,164✔
1620
        k = fgetc(f);
201,360,164✔
1621
        if (k == EOF) {
201,360,164✔
1622
                if (ferror(f))
533,949✔
1623
                        return errno_or_else(EIO);
12✔
1624

1625
                if (ret)
533,943✔
1626
                        *ret = 0;
533,857✔
1627

1628
                return 0;
533,943✔
1629
        }
1630

1631
        if (ret)
200,826,215✔
1632
                *ret = k;
200,825,608✔
1633

1634
        return 1;
1635
}
1636

1637
int warn_file_is_world_accessible(const char *filename, struct stat *st, const char *unit, unsigned line) {
162✔
1638
        struct stat _st;
162✔
1639

1640
        if (!filename)
162✔
1641
                return 0;
162✔
1642

1643
        if (!st) {
162✔
1644
                if (stat(filename, &_st) < 0)
22✔
UNCOV
1645
                        return -errno;
×
1646
                st = &_st;
1647
        }
1648

1649
        if ((st->st_mode & S_IRWXO) == 0)
162✔
1650
                return 0;
1651

1652
        if (unit)
40✔
UNCOV
1653
                log_syntax(unit, LOG_WARNING, filename, line, 0,
×
1654
                           "%s has %04o mode that is too permissive, please adjust the ownership and access mode.",
1655
                           filename, st->st_mode & 07777);
1656
        else
1657
                log_warning("%s has %04o mode that is too permissive, please adjust the ownership and access mode.",
40✔
1658
                            filename, st->st_mode & 07777);
1659
        return 0;
1660
}
1661

1662
int write_data_file_atomic_at(
12✔
1663
                int dir_fd,
1664
                const char *path,
1665
                const struct iovec *iovec,
1666
                WriteDataFileFlags flags) {
1667

1668
        int r;
12✔
1669

1670
        assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
12✔
1671

1672
        /* This is a cousin of write_string_file_atomic(), but operates with arbitrary struct iovec binary
1673
         * data (rather than strings), works without FILE* streams, and does direct syscalls instead. */
1674

1675
        _cleanup_free_ char *dn = NULL, *fn = NULL;
12✔
1676
        r = path_split_prefix_filename(path, &dn, &fn);
12✔
1677
        if (IN_SET(r, -EADDRNOTAVAIL, O_DIRECTORY))
12✔
1678
                return -EISDIR; /* path refers to "." or "/" (which are dirs, which we cannot write), or is suffixed with "/" */
1679
        if (r < 0)
9✔
1680
                return r;
1681

1682
        _cleanup_close_ int mfd = -EBADF;
12✔
1683
        if (dn) {
7✔
1684
                /* If there's a directory component, readjust our position */
1685
                r = chaseat(dir_fd,
12✔
1686
                            dn,
1687
                            FLAGS_SET(flags, WRITE_DATA_FILE_MKDIR_0755) ? CHASE_MKDIR_0755 : 0,
6✔
1688
                            /* ret_path= */ NULL,
1689
                            &mfd);
1690
                if (r < 0)
6✔
1691
                        return r;
1692

1693
                dir_fd = mfd;
5✔
1694
        }
1695

1696
        _cleanup_free_ char *t = NULL;
6✔
1697
        _cleanup_close_ int fd = open_tmpfile_linkable_at(dir_fd, fn, O_WRONLY|O_CLOEXEC, &t);
12✔
1698
        if (fd < 0)
6✔
1699
                return fd;
1700

1701
        CLEANUP_TMPFILE_AT(dir_fd, t);
6✔
1702

1703
        if (iovec_is_set(iovec)) {
6✔
1704
                r = loop_write(fd, iovec->iov_base, iovec->iov_len);
6✔
1705
                if (r < 0)
6✔
1706
                        return r;
1707
        }
1708

1709
        r = fchmod_umask(fd, 0644);
6✔
1710
        if (r < 0)
6✔
1711
                return r;
1712

1713
        r = link_tmpfile_at(fd, dir_fd, t, fn, LINK_TMPFILE_REPLACE);
6✔
1714
        if (r < 0)
6✔
1715
                return r;
1716

1717
        t = mfree(t); /* disarm CLEANUP_TMPFILE_AT */
5✔
1718
        return 0;
5✔
1719
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc