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

systemd / systemd / 29461261567

15 Jul 2026 04:59PM UTC coverage: 72.983% (+0.05%) from 72.936%
29461261567

push

github

yuwata
tpm2-util: keep the measurement log's torn-write marker intact

The userspace measurement log carries a sticky-bit marker while a writer
is between updating a measurement register and appending the matching
record, so that a writer dying in between leaves the log detectably
incomplete.

However, the next successful writer used to clear the marker again after
appending its own record, erasing the evidence that an earlier writer
had died and the log is still missing a record. Keep the marker set in
that case instead; the new record is appended and synced regardless. The
marker likewise stays set if the measurement itself fails, so that any
non-clean completion remains flagged: a spurious flag on a failed but
harmless measurement is preferable to erasing evidence of a real gap,
and PCR replay stays authoritative either way.

Finally, warn when systemd-pcrlock loads a marked log, so the resulting
PCR validation failures come with a hint at their cause.

Signed-off-by: Paul Meyer <katexochen0@gmail.com>

11 of 12 new or added lines in 2 files covered. (91.67%)

2475 existing lines in 54 files now uncovered.

346100 of 474222 relevant lines covered (72.98%)

1321758.71 hits per line

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

80.85
/src/basic/fs-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <linux/falloc.h>
4
#include <stdlib.h>
5
#include <sys/file.h>
6
#include <sys/mount.h>
7
#include <unistd.h>
8

9
#include "alloc-util.h"
10
#include "btrfs-util.h"
11
#include "chattr-util.h"
12
#include "dirent-util.h"
13
#include "errno-util.h"
14
#include "fd-util.h"
15
#include "fs-util.h"
16
#include "hostname-util.h"
17
#include "label-util.h"
18
#include "lock-util.h"
19
#include "log.h"
20
#include "mkdir.h"
21
#include "path-util.h"
22
#include "process-util.h"
23
#include "random-util.h"
24
#include "ratelimit.h"
25
#include "stat-util.h"
26
#include "string-util.h"
27
#include "strv.h"
28
#include "time-util.h"
29
#include "tmpfile-util.h"
30
#include "umask-util.h"
31

32
int rmdir_parents(const char *path, const char *stop) {
45,965✔
33
        char *p;
45,965✔
34
        int r;
45,965✔
35

36
        assert(path);
45,965✔
37
        assert(stop);
45,965✔
38

39
        if (!path_is_safe(path))
45,965✔
40
                return -EINVAL;
41

42
        if (!path_is_safe(stop))
45,964✔
43
                return -EINVAL;
44

45
        p = strdupa_safe(path);
45,963✔
46

47
        for (;;) {
2,368✔
48
                char *slash = NULL;
48,331✔
49

50
                /* skip the last component. */
51
                r = path_find_last_component(p, /* accept_dot_dot= */ false, (const char **) &slash, NULL);
48,331✔
52
                if (r <= 0)
48,331✔
53
                        return r;
45,963✔
54
                if (slash == p)
48,331✔
55
                        return 0;
56

57
                assert(*slash == '/');
48,331✔
58
                *slash = '\0';
48,331✔
59

60
                if (path_startswith_full(stop, p, PATH_STARTSWITH_REFUSE_DOT_DOT))
48,331✔
61
                        return 0;
62

63
                if (rmdir(p) < 0 && errno != ENOENT)
47,848✔
64
                        return -errno;
45,480✔
65
        }
66
}
67

68
int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath) {
179✔
69
        int r;
179✔
70

71
        assert(olddirfd >= 0 || olddirfd == AT_FDCWD);
179✔
72
        assert(oldpath);
179✔
73
        assert(newdirfd >= 0 || newdirfd == AT_FDCWD);
179✔
74
        assert(newpath);
179✔
75

76
        /* Try the ideal approach first */
77
        if (renameat2(olddirfd, oldpath, newdirfd, newpath, RENAME_NOREPLACE) >= 0)
179✔
78
                return 0;
79

80
        /* renameat2() exists since Linux 3.15, btrfs and FAT added support for it later. If it is not implemented,
81
         * fall back to a different method. */
82
        if (!ERRNO_IS_NOT_SUPPORTED(errno) && errno != EINVAL)
93✔
83
                return -errno;
93✔
84

85
        /* Let's try to use linkat()+unlinkat() as fallback. This doesn't work on directories and on some file systems
86
         * that do not support hard links (such as FAT, most prominently), but for files it's pretty close to what we
87
         * want — though not atomic (i.e. for a short period both the new and the old filename will exist). */
88
        if (linkat(olddirfd, oldpath, newdirfd, newpath, 0) >= 0) {
×
89

90
                r = RET_NERRNO(unlinkat(olddirfd, oldpath, 0));
×
91
                if (r < 0) {
×
92
                        (void) unlinkat(newdirfd, newpath, 0);
×
93
                        return r;
×
94
                }
95

96
                return 0;
97
        }
98

99
        if (!ERRNO_IS_NOT_SUPPORTED(errno) && !IN_SET(errno, EINVAL, EPERM)) /* FAT returns EPERM on link()… */
×
100
                return -errno;
×
101

102
        /* OK, neither RENAME_NOREPLACE nor linkat()+unlinkat() worked. Let's then fall back to the racy TOCTOU
103
         * vulnerable accessat(F_OK) check followed by classic, replacing renameat(), we have nothing better. */
104

105
        if (faccessat(newdirfd, newpath, F_OK, AT_SYMLINK_NOFOLLOW) >= 0)
×
106
                return -EEXIST;
107
        if (errno != ENOENT)
×
108
                return -errno;
×
109

110
        return RET_NERRNO(renameat(olddirfd, oldpath, newdirfd, newpath));
×
111
}
112

113
int readlinkat_malloc(int fd, const char *p, char **ret) {
5,187,216✔
114
        size_t l = PATH_MAX;
5,187,216✔
115

116
        assert(fd >= 0 || fd == AT_FDCWD);
5,187,216✔
117

118
        if (fd < 0 && isempty(p))
5,187,216✔
119
                return -EISDIR; /* In this case, the fd points to the current working directory, and is
120
                                 * definitely not a symlink. Let's return earlier. */
121

122
        for (;;) {
5,187,216✔
123
                _cleanup_free_ char *c = NULL;
5,187,216✔
124
                ssize_t n;
5,187,216✔
125

126
                c = new(char, l+1);
5,187,216✔
127
                if (!c)
5,187,216✔
128
                        return -ENOMEM;
129

130
                n = readlinkat(fd, strempty(p), c, l);
5,187,218✔
131
                if (n < 0)
5,187,216✔
132
                        return -errno;
392,610✔
133

134
                if ((size_t) n < l) {
4,794,606✔
135
                        c[n] = 0;
4,794,606✔
136

137
                        if (ret)
4,794,606✔
138
                                *ret = TAKE_PTR(c);
4,794,606✔
139

140
                        return 0;
141
                }
142

143
                if (l > (SSIZE_MAX-1)/2) /* readlinkat() returns an ssize_t, and we want an extra byte for a
×
144
                                          * trailing NUL, hence do an overflow check relative to SSIZE_MAX-1
145
                                          * here */
146
                        return -EFBIG;
147

148
                l *= 2;
×
149
        }
150
}
151

152
int readlink_value(const char *p, char **ret) {
638,902✔
153
        _cleanup_free_ char *link = NULL, *name = NULL;
638,902✔
154
        int r;
638,902✔
155

156
        assert(p);
638,902✔
157
        assert(ret);
638,902✔
158

159
        r = readlink_malloc(p, &link);
638,902✔
160
        if (r < 0)
638,902✔
161
                return r;
162

163
        r = path_extract_filename(link, &name);
361,664✔
164
        if (r < 0)
361,664✔
165
                return r;
166
        if (r == O_DIRECTORY)
361,664✔
167
                return -EINVAL;
168

169
        *ret = TAKE_PTR(name);
361,664✔
170
        return 0;
361,664✔
171
}
172

173
int readlink_and_make_absolute(const char *p, char **ret) {
3,384✔
174
        _cleanup_free_ char *target = NULL;
3,384✔
175
        int r;
3,384✔
176

177
        assert(p);
3,384✔
178
        assert(ret);
3,384✔
179

180
        r = readlink_malloc(p, &target);
3,384✔
181
        if (r < 0)
3,384✔
182
                return r;
183

184
        return file_in_same_dir(p, target, ret);
27✔
185
}
186

187
int chmod_and_chown_at(int dir_fd, const char *path, mode_t mode, uid_t uid, gid_t gid) {
25,768✔
188
        _cleanup_close_ int fd = -EBADF;
25,768✔
189

190
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
25,768✔
191

192
        if (path) {
25,768✔
193
                /* Let's acquire an O_PATH fd, as precaution to change mode/owner on the same file */
194
                fd = openat(dir_fd, path, O_PATH|O_CLOEXEC|O_NOFOLLOW);
25,768✔
195
                if (fd < 0)
25,768✔
196
                        return -errno;
×
197
                dir_fd = fd;
198

199
        } else if (dir_fd == AT_FDCWD) {
×
200
                /* Let's acquire an O_PATH fd of the current directory */
201
                fd = openat(dir_fd, ".", O_PATH|O_CLOEXEC|O_NOFOLLOW|O_DIRECTORY);
×
202
                if (fd < 0)
×
203
                        return -errno;
×
204
                dir_fd = fd;
205
        }
206

207
        return fchmod_and_chown(dir_fd, mode, uid, gid);
25,768✔
208
}
209

210
int fchmod_and_chown_with_fallback(int fd, const char *path, mode_t mode, uid_t uid, gid_t gid) {
133,791✔
211
        bool do_chown, do_chmod;
133,791✔
212
        struct stat st;
133,791✔
213
        int r;
133,791✔
214

215
        /* Change ownership and access mode of the specified fd. Tries to do so safely, ensuring that at no
216
         * point in time the access mode is above the old access mode under the old ownership or the new
217
         * access mode under the new ownership. Note: this call tries hard to leave the access mode
218
         * unaffected if the uid/gid is changed, i.e. it undoes implicit suid/sgid dropping the kernel does
219
         * on chown().
220
         *
221
         * This call is happy with O_PATH fds.
222
         *
223
         * If path is given, allow a fallback path which does not use /proc/self/fd/. On any normal system
224
         * /proc will be mounted, but in certain improperly assembled environments it might not be. This is
225
         * less secure (potential TOCTOU), so should only be used after consideration. */
226

227
        if (fstat(fd, &st) < 0)
133,791✔
228
                return -errno;
×
229

230
        do_chown =
267,582✔
231
                (uid != UID_INVALID && st.st_uid != uid) ||
133,791✔
232
                (gid != GID_INVALID && st.st_gid != gid);
32,463✔
233

234
        do_chmod =
267,582✔
235
                !S_ISLNK(st.st_mode) && /* chmod is not defined on symlinks */
133,791✔
236
                ((mode != MODE_INVALID && ((st.st_mode ^ mode) & 07777) != 0) ||
128,527✔
237
                 do_chown); /* If we change ownership, make sure we reset the mode afterwards, since chown()
238
                             * modifies the access mode too */
239

240
        if (mode == MODE_INVALID)
133,791✔
241
                mode = st.st_mode; /* If we only shall do a chown(), save original mode, since chown() might break it. */
242
        else if ((mode & S_IFMT) != 0 && ((mode ^ st.st_mode) & S_IFMT) != 0)
92,279✔
243
                return -EINVAL; /* insist on the right file type if it was specified */
244

245
        if (do_chown && do_chmod) {
133,787✔
246
                mode_t minimal = st.st_mode & mode; /* the subset of the old and the new mask */
13,595✔
247

248
                if (((minimal ^ st.st_mode) & 07777) != 0) {
13,595✔
249
                        r = fchmod_opath(fd, minimal & 07777);
8✔
250
                        if (r < 0) {
8✔
251
                                if (!path || r != -ENOSYS)
×
252
                                        return r;
253

254
                                /* Fallback path which doesn't use /proc/self/fd/. */
255
                                if (chmod(path, minimal & 07777) < 0)
×
256
                                        return -errno;
×
257
                        }
258
                }
259
        }
260

261
        if (do_chown)
133,787✔
262
                if (fchownat(fd, "", uid, gid, AT_EMPTY_PATH) < 0)
13,598✔
263
                        return -errno;
1✔
264

265
        if (do_chmod) {
133,786✔
266
                r = fchmod_opath(fd, mode & 07777);
30,593✔
267
                if (r < 0) {
30,593✔
268
                        if (!path || r != -ENOSYS)
×
269
                                return r;
270

271
                        /* Fallback path which doesn't use /proc/self/fd/. */
272
                        if (chmod(path, mode & 07777) < 0)
×
273
                                return -errno;
×
274
                }
275
        }
276

277
        return do_chown || do_chmod;
133,786✔
278
}
279

280
int fchmod_umask(int fd, mode_t m) {
3,459✔
281
        _cleanup_umask_ mode_t u = umask(0777);
3,459✔
282

283
        return RET_NERRNO(fchmod(fd, m & (~u)));
3,459✔
284
}
285

286
int fchmod_opath(int fd, mode_t m) {
33,946✔
287
        /* This function operates also on fd that might have been opened with
288
         * O_PATH. The tool set we have is non-intuitive:
289
         * - fchmod(2) only operates on open files (i. e., fds with an open file description);
290
         * - fchmodat(2) does not have a flag arg like fchownat(2) does, so no way to pass AT_EMPTY_PATH;
291
         *   + it should not be confused with the libc fchmodat(3) interface, which adds 4th flag argument,
292
         *     and supports AT_EMPTY_PATH since v2.39 (previously only supported AT_SYMLINK_NOFOLLOW). So if
293
         *     the kernel has fchmodat2(2), since v2.39 glibc will call into it directly. If the kernel
294
         *     doesn't, or glibc is older than v2.39, glibc's internal fallback will return EINVAL if
295
         *     AT_EMPTY_PATH is passed.
296
         * - fchmodat2(2) supports all the AT_* flags, but is still very recent.
297
         *
298
         * We try to use fchmodat(3) first, and on EINVAL fall back to fchmodat2(), and, if that is also not
299
         * supported, resort to the /proc/self/fd dance. */
300

301
        assert(fd >= 0);
33,946✔
302

303
        if (fchmodat(fd, "", m, AT_EMPTY_PATH) >= 0)
33,946✔
304
                return 0;
305
        if (errno == EINVAL && fchmodat2(fd, "", m, AT_EMPTY_PATH) >= 0) /* glibc too old? */
2✔
306
                return 0;
307
        if (!IN_SET(errno, ENOSYS, EPERM)) /* Some container managers block unknown syscalls with EPERM */
2✔
308
                return -errno;
2✔
309

310
        if (chmod(FORMAT_PROC_FD_PATH(fd), m) < 0) {
×
311
                if (errno != ENOENT)
×
312
                        return -errno;
×
313

314
                return proc_fd_enoent_errno();
×
315
        }
316

317
        return 0;
×
318
}
319

320
int futimens_opath(int fd, const struct timespec ts[2]) {
132,162✔
321
        /* Similar to fchmod_opath() but for futimens() */
322

323
        assert(fd >= 0);
132,162✔
324

325
        return RET_NERRNO(utimensat(fd, "", ts, AT_EMPTY_PATH));
132,162✔
326
}
327

328
int stat_warn_permissions(const char *path, const struct stat *st) {
168,999✔
329
        assert(path);
168,999✔
330
        assert(st);
168,999✔
331

332
        /* Don't complain if we are reading something that is not a file, for example /dev/null */
333
        if (!S_ISREG(st->st_mode))
168,999✔
334
                return 0;
335

336
        if (st->st_mode & 0111)
168,999✔
337
                log_warning("Configuration file %s is marked executable. Please remove executable permission bits. Proceeding anyway.", path);
×
338

339
        if (st->st_mode & 0002)
168,999✔
340
                log_warning("Configuration file %s is marked world-writable. Please remove world writability permission bits. Proceeding anyway.", path);
×
341

342
        if (getpid_cached() == 1 && (st->st_mode & 0044) != 0044)
168,999✔
343
                log_warning("Configuration file %s is marked world-inaccessible. This has no effect as configuration data is accessible via APIs without restrictions. Proceeding anyway.", path);
53✔
344

345
        return 0;
346
}
347

348
int fd_warn_permissions(const char *path, int fd) {
×
349
        struct stat st;
×
350

351
        assert(path);
×
352
        assert(fd >= 0);
×
353

354
        if (fstat(fd, &st) < 0)
×
355
                return -errno;
×
356

357
        return stat_warn_permissions(path, &st);
×
358
}
359

360
int access_nofollow(const char *path, int mode) {
33,750✔
361
        return RET_NERRNO(faccessat(AT_FDCWD, path, mode, AT_SYMLINK_NOFOLLOW));
33,750✔
362
}
363

364
int access_fd(int fd, int mode) {
29,805✔
365
        /* Like access() but operates on an already open fd */
366

367
        if (fd == AT_FDCWD)
29,805✔
368
                return RET_NERRNO(access(".", mode));
×
369
        if (fd == XAT_FDROOT)
29,805✔
370
                return RET_NERRNO(access("/", mode));
1✔
371

372
        assert(fd >= 0);
29,804✔
373

374
        return RET_NERRNO(faccessat(fd, "", mode, AT_EMPTY_PATH));
29,804✔
375
}
376

377
int touch_fd(int fd, usec_t stamp) {
75,029✔
378
        assert(fd >= 0);
75,029✔
379

380
        if (stamp == USEC_INFINITY)
75,029✔
381
                return futimens_opath(fd, /* ts= */ NULL);
74,245✔
382

383
        struct timespec ts[2];
784✔
384
        timespec_store(ts + 0, stamp);
784✔
385
        ts[1] = ts[0];
784✔
386
        return futimens_opath(fd, ts);
784✔
387
}
388

389
int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode) {
75,032✔
390
        _cleanup_close_ int fd = -EBADF;
75,032✔
391
        int ret;
75,032✔
392

393
        assert(path);
75,032✔
394

395
        /* Note that touch_file() does not follow symlinks: if invoked on an existing symlink, then it is the symlink
396
         * itself which is updated, not its target
397
         *
398
         * Returns the first error we encounter, but tries to apply as much as possible. */
399

400
        if (parents)
75,032✔
401
                (void) mkdir_parents(path, 0755);
32,820✔
402

403
        /* Initially, we try to open the node with O_PATH, so that we get a reference to the node. This is useful in
404
         * case the path refers to an existing device or socket node, as we can open it successfully in all cases, and
405
         * won't trigger any driver magic or so. */
406
        fd = open(path, O_PATH|O_CLOEXEC|O_NOFOLLOW);
75,032✔
407
        if (fd < 0) {
75,032✔
408
                if (errno != ENOENT)
47,473✔
409
                        return -errno;
×
410

411
                /* if the node doesn't exist yet, we create it, but with O_EXCL, so that we only create a regular file
412
                 * here, and nothing else */
413
                fd = open(path, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, IN_SET(mode, 0, MODE_INVALID) ? 0644 : mode);
47,473✔
414
                if (fd < 0)
47,473✔
415
                        return -errno;
3✔
416
        }
417

418
        /* Let's make a path from the fd, and operate on that. With this logic, we can adjust the access mode,
419
         * ownership and time of the file node in all cases, even if the fd refers to an O_PATH object — which is
420
         * something fchown(), fchmod(), futimensat() don't allow. */
421
        ret = fchmod_and_chown(fd, mode, uid, gid);
75,029✔
422

423
        return RET_GATHER(ret, touch_fd(fd, stamp));
75,029✔
424
}
425

426
int touch(const char *path) {
41,407✔
427
        return touch_file(path, false, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID);
41,407✔
428
}
429

430
int symlinkat_idempotent(const char *target, int atfd, const char *linkpath, bool make_relative) {
3,889✔
431
        _cleanup_free_ char *relpath = NULL;
3,889✔
432
        int r;
3,889✔
433

434
        assert(target);
3,889✔
435
        assert(linkpath);
3,889✔
436

437
        if (make_relative) {
3,889✔
438
                r = path_make_relative_parent(linkpath, target, &relpath);
188✔
439
                if (r < 0)
188✔
440
                        return r;
441

442
                target = relpath;
188✔
443
        }
444

445
        if (symlinkat(target, atfd, linkpath) < 0) {
3,889✔
446
                _cleanup_free_ char *p = NULL;
1,203✔
447

448
                if (errno != EEXIST)
1,203✔
449
                        return -errno;
×
450

451
                r = readlinkat_malloc(atfd, linkpath, &p);
1,203✔
452
                if (r == -EINVAL) /* Not a symlink? In that case return the original error we encountered: -EEXIST */
1,203✔
453
                        return -EEXIST;
454
                if (r < 0) /* Any other error? In that case propagate it as is */
1,203✔
455
                        return r;
456

457
                if (!streq(p, target)) /* Not the symlink we want it to be? In that case, propagate the original -EEXIST */
1,203✔
458
                        return -EEXIST;
459
        }
460

461
        return 0;
462
}
463

464
int symlinkat_atomic_full(const char *target, int atfd, const char *linkpath, SymlinkFlags flags) {
113,747✔
465
        int r;
113,747✔
466

467
        assert(target);
113,747✔
468
        assert(linkpath);
113,747✔
469

470
        _cleanup_free_ char *relpath = NULL;
113,747✔
471
        if (FLAGS_SET(flags, SYMLINK_MAKE_RELATIVE)) {
113,747✔
472
                r = path_make_relative_parent(linkpath, target, &relpath);
103,407✔
473
                if (r < 0)
103,407✔
474
                        return r;
475

476
                target = relpath;
103,407✔
477
        }
478

479
        _cleanup_free_ char *t = NULL;
113,747✔
480
        r = tempfn_random(linkpath, NULL, &t);
113,747✔
481
        if (r < 0)
113,747✔
482
                return r;
483

484
        bool call_label_ops_post = false;
113,747✔
485
        if (FLAGS_SET(flags, SYMLINK_LABEL)) {
113,747✔
486
                r = label_ops_pre(atfd, linkpath, S_IFLNK);
107,766✔
487
                if (r < 0)
107,766✔
488
                        return r;
489

490
                call_label_ops_post = true;
491
        }
492

493
        r = RET_NERRNO(symlinkat(target, atfd, t));
113,747✔
494
        if (call_label_ops_post)
113,747✔
495
                RET_GATHER(r, label_ops_post(atfd, t, /* created= */ r >= 0));
107,766✔
496
        if (r < 0)
113,747✔
497
                return r;
498

499
        r = RET_NERRNO(renameat(atfd, t, atfd, linkpath));
113,748✔
500
        if (r < 0) {
1✔
501
                (void) unlinkat(atfd, t, 0);
1✔
502
                return r;
1✔
503
        }
504

505
        return 0;
506
}
507

508
int mknodat_atomic(int atfd, const char *path, mode_t mode, dev_t dev) {
×
509
        _cleanup_free_ char *t = NULL;
×
510
        int r;
×
511

512
        assert(path);
×
513

514
        r = tempfn_random(path, NULL, &t);
×
515
        if (r < 0)
×
516
                return r;
517

518
        if (mknodat(atfd, t, mode, dev) < 0)
×
519
                return -errno;
×
520

521
        r = RET_NERRNO(renameat(atfd, t, atfd, path));
×
522
        if (r < 0) {
×
523
                (void) unlinkat(atfd, t, 0);
×
524
                return r;
×
525
        }
526

527
        return 0;
528
}
529

530
int mkfifoat_atomic(int dir_fd, const char *path, mode_t mode) {
1✔
531
        _cleanup_free_ char *t = NULL;
1✔
532
        int r;
1✔
533

534
        assert(path);
1✔
535

536
        /* We're only interested in the (random) filename.  */
537
        r = tempfn_random(path, NULL, &t);
1✔
538
        if (r < 0)
1✔
539
                return r;
540

541
        if (mkfifoat(dir_fd, t, mode) < 0)
1✔
542
                return -errno;
×
543

544
        r = RET_NERRNO(renameat(dir_fd, t, dir_fd, path));
1✔
545
        if (r < 0) {
×
546
                (void) unlinkat(dir_fd, t, 0);
×
547
                return r;
×
548
        }
549

550
        return 0;
551
}
552

553
int get_files_in_directory(const char *path, char ***ret_list) {
493✔
554
        _cleanup_strv_free_ char **l = NULL;
×
555
        _cleanup_closedir_ DIR *d = NULL;
493✔
556
        size_t n = 0;
493✔
557

558
        assert(path);
493✔
559

560
        /* Returns all files in a directory in *list, and the number
561
         * of files as return value. If list is NULL returns only the
562
         * number. */
563

564
        d = opendir(path);
493✔
565
        if (!d)
493✔
566
                return -errno;
2✔
567

568
        FOREACH_DIRENT_ALL(de, d, return -errno) {
2,059✔
569
                if (!dirent_is_file(de))
1,568✔
570
                        continue;
1,030✔
571

572
                if (ret_list) {
538✔
573
                        /* one extra slot is needed for the terminating NULL */
574
                        if (!GREEDY_REALLOC(l, n + 2))
525✔
575
                                return -ENOMEM;
576

577
                        l[n] = strdup(de->d_name);
525✔
578
                        if (!l[n])
525✔
579
                                return -ENOMEM;
580

581
                        l[++n] = NULL;
525✔
582
                } else
583
                        n++;
13✔
584
        }
585

586
        if (ret_list)
491✔
587
                *ret_list = TAKE_PTR(l);
488✔
588

589
        return n;
491✔
590
}
591

592
static int getenv_tmp_dir(const char **ret_path) {
2,461✔
593
        int r, ret = 0;
2,461✔
594

595
        assert(ret_path);
2,461✔
596

597
        /* We use the same order of environment variables python uses in tempfile.gettempdir():
598
         * https://docs.python.org/3/library/tempfile.html#tempfile.gettempdir */
599
        FOREACH_STRING(n, "TMPDIR", "TEMP", "TMP") {
9,841✔
600
                const char *e;
7,381✔
601

602
                e = secure_getenv(n);
7,381✔
603
                if (!e)
7,381✔
604
                        continue;
7,379✔
605
                if (!path_is_absolute(e)) {
2✔
606
                        r = -ENOTDIR;
×
607
                        goto next;
×
608
                }
609
                if (!path_is_normalized(e)) {
2✔
610
                        r = -EPERM;
×
611
                        goto next;
×
612
                }
613

614
                r = is_dir(e, true);
2✔
615
                if (r < 0)
2✔
616
                        goto next;
1✔
617
                if (r == 0) {
1✔
618
                        r = -ENOTDIR;
×
619
                        goto next;
×
620
                }
621

622
                *ret_path = e;
1✔
623
                return 1;
1✔
624

625
        next:
1✔
626
                /* Remember first error, to make this more debuggable */
627
                if (ret >= 0)
1✔
628
                        ret = r;
1✔
629
        }
630

631
        if (ret < 0)
2,460✔
632
                return ret;
633

634
        *ret_path = NULL;
2,459✔
635
        return ret;
2,459✔
636
}
637

638
static int tmp_dir_internal(const char *def, const char **ret) {
2,461✔
639
        const char *e;
2,461✔
640
        int r, k;
2,461✔
641

642
        assert(def);
2,461✔
643
        assert(ret);
2,461✔
644

645
        r = getenv_tmp_dir(&e);
2,461✔
646
        if (r > 0) {
2,461✔
647
                *ret = e;
1✔
648
                return 0;
1✔
649
        }
650

651
        k = is_dir(def, /* follow= */ true);
2,460✔
652
        if (k == 0)
2,460✔
653
                k = -ENOTDIR;
654
        if (k < 0)
2,460✔
655
                return RET_GATHER(r, k);
×
656

657
        *ret = def;
2,460✔
658
        return 0;
2,460✔
659
}
660

661
int var_tmp_dir(const char **ret) {
2,259✔
662
        assert(ret);
2,259✔
663

664
        /* Returns the location for "larger" temporary files, that is backed by physical storage if available, and thus
665
         * even might survive a boot: /var/tmp. If $TMPDIR (or related environment variables) are set, its value is
666
         * returned preferably however. Note that both this function and tmp_dir() below are affected by $TMPDIR,
667
         * making it a variable that overrides all temporary file storage locations. */
668

669
        return tmp_dir_internal("/var/tmp", ret);
2,259✔
670
}
671

672
int tmp_dir(const char **ret) {
202✔
673
        assert(ret);
202✔
674

675
        /* Similar to var_tmp_dir() above, but returns the location for "smaller" temporary files, which is usually
676
         * backed by an in-memory file system: /tmp. */
677

678
        return tmp_dir_internal("/tmp", ret);
202✔
679
}
680

681
int unlink_or_warn(const char *filename) {
228✔
682
        assert(filename);
228✔
683

684
        if (unlink(filename) < 0 && errno != ENOENT)
228✔
685
                /* If the file doesn't exist and the fs simply was read-only (in which
686
                 * case unlink() returns EROFS even if the file doesn't exist), don't
687
                 * complain */
688
                if (errno != EROFS || access(filename, F_OK) >= 0)
×
689
                        return log_error_errno(errno, "Failed to remove \"%s\": %m", filename);
×
690

691
        return 0;
692
}
693

694
char *rmdir_and_free(char *p) {
168,275✔
695
        PROTECT_ERRNO;
168,275✔
696

697
        if (!p)
168,275✔
698
                return NULL;
699

700
        (void) rmdir(p);
168,275✔
701
        return mfree(p);
168,275✔
702
}
703

704
char* unlink_and_free(char *p) {
5,392✔
705
        PROTECT_ERRNO;
5,392✔
706

707
        if (!p)
5,392✔
708
                return NULL;
709

710
        (void) unlink(p);
2,067✔
711
        return mfree(p);
2,067✔
712
}
713

714
int unlinkat_deallocate(int fd, const char *name, UnlinkDeallocateFlags flags) {
83✔
715
        _cleanup_close_ int truncate_fd = -EBADF;
83✔
716
        struct stat st;
83✔
717
        off_t l, bs;
83✔
718

719
        assert(fd >= 0 || fd == AT_FDCWD);
83✔
720
        assert(name);
83✔
721
        assert((flags & ~(UNLINK_REMOVEDIR|UNLINK_ERASE)) == 0);
83✔
722

723
        /* Operates like unlinkat() but also deallocates the file contents if it is a regular file and there's no other
724
         * link to it. This is useful to ensure that other processes that might have the file open for reading won't be
725
         * able to keep the data pinned on disk forever. This call is particular useful whenever we execute clean-up
726
         * jobs ("vacuuming"), where we want to make sure the data is really gone and the disk space released and
727
         * returned to the free pool.
728
         *
729
         * Deallocation is preferably done by FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE (👊) if supported, which means
730
         * the file won't change size. That's a good thing since we shouldn't needlessly trigger SIGBUS in other
731
         * programs that have mmap()ed the file. (The assumption here is that changing file contents to all zeroes
732
         * underneath those programs is the better choice than simply triggering SIGBUS in them which truncation does.)
733
         * However if hole punching is not implemented in the kernel or file system we'll fall back to normal file
734
         * truncation (🔪), as our goal of deallocating the data space trumps our goal of being nice to readers (💐).
735
         *
736
         * Note that we attempt deallocation, but failure to succeed with that is not considered fatal, as long as the
737
         * primary job – to delete the file – is accomplished. */
738

739
        if (!FLAGS_SET(flags, UNLINK_REMOVEDIR)) {
83✔
740
                truncate_fd = openat(fd, name, O_WRONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW|O_NONBLOCK);
83✔
741
                if (truncate_fd < 0) {
83✔
742

743
                        /* If this failed because the file doesn't exist propagate the error right-away. Also,
744
                         * AT_REMOVEDIR wasn't set, and we tried to open the file for writing, which means EISDIR is
745
                         * returned when this is a directory but we are not supposed to delete those, hence propagate
746
                         * the error right-away too. */
747
                        if (IN_SET(errno, ENOENT, EISDIR))
×
748
                                return -errno;
×
749

750
                        if (errno != ELOOP) /* don't complain if this is a symlink */
×
751
                                log_debug_errno(errno, "Failed to open file '%s' for deallocation, ignoring: %m", name);
×
752
                }
753
        }
754

755
        if (unlinkat(fd, name, FLAGS_SET(flags, UNLINK_REMOVEDIR) ? AT_REMOVEDIR : 0) < 0)
83✔
756
                return -errno;
×
757

758
        if (truncate_fd < 0) /* Don't have a file handle, can't do more ☹️ */
83✔
759
                return 0;
760

761
        if (fstat(truncate_fd, &st) < 0) {
83✔
762
                log_debug_errno(errno, "Failed to stat file '%s' for deallocation, ignoring: %m", name);
×
763
                return 0;
764
        }
765

766
        if (!S_ISREG(st.st_mode))
83✔
767
                return 0;
768

769
        if (FLAGS_SET(flags, UNLINK_ERASE) && st.st_size > 0 && st.st_nlink == 0) {
83✔
770
                uint64_t left = st.st_size;
3✔
771
                char buffer[64 * 1024];
3✔
772

773
                /* If erasing is requested, let's overwrite the file with random data once before deleting
774
                 * it. This isn't going to give you shred(1) semantics, but hopefully should be good enough
775
                 * for stuff backed by tmpfs at least.
776
                 *
777
                 * Note that we only erase like this if the link count of the file is zero. If it is higher it
778
                 * is still linked by someone else and we'll leave it to them to remove it securely
779
                 * eventually! */
780

781
                random_bytes(buffer, sizeof(buffer));
3✔
782

783
                while (left > 0) {
6✔
784
                        ssize_t n;
3✔
785

786
                        n = write(truncate_fd, buffer, MIN(sizeof(buffer), left));
3✔
787
                        if (n < 0) {
3✔
788
                                log_debug_errno(errno, "Failed to erase data in file '%s', ignoring.", name);
×
789
                                break;
790
                        }
791

792
                        assert(left >= (size_t) n);
3✔
793
                        left -= n;
3✔
794
                }
795

796
                /* Let's refresh metadata */
797
                if (fstat(truncate_fd, &st) < 0) {
3✔
798
                        log_debug_errno(errno, "Failed to stat file '%s' for deallocation, ignoring: %m", name);
×
799
                        return 0;
×
800
                }
801
        }
802

803
        /* Don't deallocate if there's nothing to deallocate or if the file is linked elsewhere */
804
        if (st.st_blocks == 0 || st.st_nlink > 0)
83✔
805
                return 0;
806

807
        /* If this is a regular file, it actually took up space on disk and there are no other links it's time to
808
         * punch-hole/truncate this to release the disk space. */
809

810
        bs = MAX(st.st_blksize, 512);
83✔
811
        l = ROUND_UP(st.st_size, bs); /* Round up to next block size */
83✔
812

813
        if (fallocate(truncate_fd, FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE, 0, l) >= 0)
83✔
814
                return 0; /* Successfully punched a hole! 😊 */
815

816
        /* Fall back to truncation */
817
        if (ftruncate(truncate_fd, 0) < 0) {
×
818
                log_debug_errno(errno, "Failed to truncate file to 0, ignoring: %m");
83✔
819
                return 0;
820
        }
821

822
        return 0;
823
}
824

825
int open_parent_at(int dir_fd, const char *path, int flags, mode_t mode) {
1,678,816✔
826
        _cleanup_free_ char *parent = NULL;
1,678,816✔
827
        int r;
1,678,816✔
828

829
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
1,678,816✔
830
        assert(path);
1,678,816✔
831

832
        r = path_extract_directory(path, &parent);
1,678,816✔
833
        if (r == -EDESTADDRREQ) {
1,678,816✔
834
                parent = strdup(".");
15,605✔
835
                if (!parent)
15,605✔
836
                        return -ENOMEM;
837
        } else if (r == -EADDRNOTAVAIL) {
1,663,211✔
838
                parent = strdup(path);
×
839
                if (!parent)
×
840
                        return -ENOMEM;
841
        } else if (r < 0)
1,663,211✔
842
                return r;
843

844
        /* Let's insist on O_DIRECTORY since the parent of a file or directory is a directory. Except if we open an
845
         * O_TMPFILE file, because in that case we are actually create a regular file below the parent directory. */
846

847
        if (!FLAGS_SET(flags, O_TMPFILE))
1,678,816✔
848
                flags |= O_DIRECTORY;
1,653,795✔
849

850
        return RET_NERRNO(openat(dir_fd, parent, flags, mode));
1,678,945✔
851
}
852

853
int conservative_renameat(
36,001✔
854
                int olddirfd, const char *oldpath,
855
                int newdirfd, const char *newpath) {
856

857
        _cleanup_close_ int old_fd = -EBADF, new_fd = -EBADF;
36,001✔
858
        struct stat old_stat, new_stat;
36,001✔
859

860
        /* Renames the old path to the new path, much like renameat() — except if both are regular files and
861
         * have the exact same contents and basic file attributes already. In that case remove the new file
862
         * instead. This call is useful for reducing inotify wakeups on files that are updated but don't
863
         * actually change. This function is written in a style that we rather rename too often than suppress
864
         * too much. I.e. whenever we are in doubt, we rather rename than fail. After all reducing inotify
865
         * events is an optimization only, not more. */
866

867
        old_fd = openat(olddirfd, oldpath, O_CLOEXEC|O_RDONLY|O_NOCTTY|O_NOFOLLOW);
36,001✔
868
        if (old_fd < 0)
36,001✔
869
                goto do_rename;
×
870

871
        new_fd = openat(newdirfd, newpath, O_CLOEXEC|O_RDONLY|O_NOCTTY|O_NOFOLLOW);
36,001✔
872
        if (new_fd < 0)
36,001✔
873
                goto do_rename;
2,491✔
874

875
        if (fstat(old_fd, &old_stat) < 0)
33,510✔
876
                goto do_rename;
×
877

878
        if (!S_ISREG(old_stat.st_mode))
33,510✔
879
                goto do_rename;
×
880

881
        if (fstat(new_fd, &new_stat) < 0)
33,510✔
882
                goto do_rename;
×
883

884
        if (stat_inode_same(&new_stat, &old_stat))
33,510✔
885
                goto is_same;
1✔
886

887
        if (old_stat.st_mode != new_stat.st_mode ||
33,509✔
888
            old_stat.st_size != new_stat.st_size ||
33,509✔
889
            old_stat.st_uid != new_stat.st_uid ||
23,586✔
890
            old_stat.st_gid != new_stat.st_gid)
23,586✔
891
                goto do_rename;
9,923✔
892

893
        for (;;) {
3✔
894
                uint8_t buf1[16*1024];
23,589✔
895
                uint8_t buf2[sizeof(buf1)];
23,589✔
896
                ssize_t l1, l2;
23,589✔
897

898
                l1 = read(old_fd, buf1, sizeof(buf1));
23,589✔
899
                if (l1 < 0)
23,589✔
900
                        goto do_rename;
348✔
901

902
                if (l1 == sizeof(buf1))
23,589✔
903
                        /* Read the full block, hence read a full block in the other file too */
904

905
                        l2 = read(new_fd, buf2, l1);
4✔
906
                else {
907
                        assert((size_t) l1 < sizeof(buf1));
23,585✔
908

909
                        /* Short read. This hence was the last block in the first file, and then came
910
                         * EOF. Read one byte more in the second file, so that we can verify we hit EOF there
911
                         * too. */
912

913
                        assert((size_t) (l1 + 1) <= sizeof(buf2));
23,585✔
914
                        l2 = read(new_fd, buf2, l1 + 1);
23,585✔
915
                }
916
                if (l2 != l1)
23,589✔
917
                        goto do_rename;
×
918

919
                if (memcmp(buf1, buf2, l1) != 0)
23,589✔
920
                        goto do_rename;
348✔
921

922
                if ((size_t) l1 < sizeof(buf1)) /* We hit EOF on the first file, and the second file too, hence exit
23,241✔
923
                                                 * now. */
924
                        break;
925
        }
926

927
is_same:
23,239✔
928
        /* Everything matches? Then don't rename, instead remove the source file, and leave the existing
929
         * destination in place */
930

931
        if (unlinkat(olddirfd, oldpath, 0) < 0)
23,239✔
932
                goto do_rename;
×
933

934
        return 0;
935

936
do_rename:
12,762✔
937
        if (renameat(olddirfd, oldpath, newdirfd, newpath) < 0)
12,762✔
938
                return -errno;
×
939

940
        return 1;
941
}
942

943
int posix_fallocate_loop(int fd, uint64_t offset, uint64_t size) {
1,883✔
944
        RateLimit rl;
1,883✔
945
        int r;
1,883✔
946

947
        r = posix_fallocate(fd, offset, size); /* returns positive errnos on error */
1,883✔
948
        if (r != EINTR)
1,883✔
949
                return -r; /* Let's return negative errnos, like common in our codebase */
1,883✔
950

951
        /* On EINTR try a couple of times more, but protect against busy looping
952
         * (not more than 16 times per 10s) */
953
        rl = (const RateLimit) { 10 * USEC_PER_SEC, 16 };
×
954
        while (ratelimit_below(&rl)) {
×
955
                r = posix_fallocate(fd, offset, size);
×
956
                if (r != EINTR)
×
957
                        return -r;
×
958
        }
959

960
        return -EINTR;
961
}
962

963
int parse_cifs_service(
15✔
964
                const char *s,
965
                char **ret_host,
966
                char **ret_service,
967
                char **ret_path) {
968

969
        _cleanup_free_ char *h = NULL, *ss = NULL, *x = NULL;
15✔
970
        const char *p, *e, *d;
15✔
971
        char delimiter;
15✔
972

973
        /* Parses a CIFS service in form of //host/service/path… and splitting it in three parts. The last
974
         * part is optional, in which case NULL is returned there. To maximize compatibility syntax with
975
         * backslashes instead of slashes is accepted too. */
976

977
        if (!s)
15✔
978
                return -EINVAL;
979

980
        p = startswith(s, "//");
14✔
981
        if (!p) {
14✔
982
                p = startswith(s, "\\\\");
6✔
983
                if (!p)
6✔
984
                        return -EINVAL;
985
        }
986

987
        delimiter = s[0];
11✔
988
        e = strchr(p, delimiter);
11✔
989
        if (!e)
11✔
990
                return -EINVAL;
991

992
        h = strndup(p, e - p);
11✔
993
        if (!h)
11✔
994
                return -ENOMEM;
995

996
        if (!hostname_is_valid(h, 0))
11✔
997
                return -EINVAL;
998

999
        e++;
10✔
1000

1001
        d = strchrnul(e, delimiter);
10✔
1002

1003
        ss = strndup(e, d - e);
10✔
1004
        if (!ss)
10✔
1005
                return -ENOMEM;
1006

1007
        if (!filename_is_valid(ss))
10✔
1008
                return -EINVAL;
1009

1010
        if (!isempty(d)) {
8✔
1011
                x = strdup(skip_leading_chars(d, CHAR_TO_STR(delimiter)));
6✔
1012
                if (!x)
6✔
1013
                        return -EINVAL;
2✔
1014

1015
                /* Make sure to convert Windows-style "\" → Unix-style / */
1016
                for (char *i = x; *i; i++)
33✔
1017
                        if (*i == delimiter)
27✔
1018
                                *i = '/';
3✔
1019

1020
                if (!path_is_valid(x))
6✔
1021
                        return -EINVAL;
1022

1023
                path_simplify(x);
6✔
1024
                if (!path_is_normalized(x))
6✔
1025
                        return -EINVAL;
1026
        }
1027

1028
        if (ret_host)
6✔
1029
                *ret_host = TAKE_PTR(h);
6✔
1030
        if (ret_service)
6✔
1031
                *ret_service = TAKE_PTR(ss);
6✔
1032
        if (ret_path)
6✔
1033
                *ret_path = TAKE_PTR(x);
6✔
1034

1035
        return 0;
1036
}
1037

1038
int open_mkdir_at_full(int dirfd, const char *path, int flags, XOpenFlags xopen_flags, mode_t mode) {
175,142✔
1039
        _cleanup_close_ int fd = -EBADF, parent_fd = -EBADF;
175,142✔
1040
        _cleanup_free_ char *fname = NULL, *parent = NULL;
175,142✔
1041
        int r;
175,142✔
1042

1043
        /* Creates a directory with mkdirat() and then opens it, in the "most atomic" fashion we can
1044
         * do. Guarantees that the returned fd refers to a directory. If O_EXCL is specified will fail if the
1045
         * dir already exists. Otherwise will open an existing dir, but only if it is one.  */
1046

1047
        if (flags & ~(O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_EXCL|O_NOATIME|O_NOFOLLOW|O_PATH))
175,142✔
1048
                return -EINVAL;
1049
        if ((flags & O_ACCMODE_STRICT) != O_RDONLY)
175,142✔
1050
                return -EINVAL;
1051

1052
        /* Note that O_DIRECTORY|O_NOFOLLOW is implied, but we allow specifying it anyway. The following
1053
         * flags actually make sense to specify: O_CLOEXEC, O_EXCL, O_NOATIME, O_PATH */
1054

1055
        /* If this is not a valid filename, it's a path. Let's open the parent directory then, so
1056
         * that we can pin it, and operate below it. */
1057
        r = path_extract_directory(path, &parent);
175,142✔
1058
        if (r < 0) {
175,142✔
1059
                if (!IN_SET(r, -EDESTADDRREQ, -EADDRNOTAVAIL))
8,611✔
1060
                        return r;
1061
        } else {
1062
                r = path_extract_filename(path, &fname);
166,531✔
1063
                if (r < 0)
166,531✔
1064
                        return r;
1065

1066
                parent_fd = openat(dirfd, parent, O_PATH|O_DIRECTORY|O_CLOEXEC);
166,531✔
1067
                if (parent_fd < 0)
166,531✔
1068
                        return -errno;
×
1069

1070
                dirfd = parent_fd;
166,531✔
1071
                path = fname;
166,531✔
1072
        }
1073

1074
        fd = xopenat_full(dirfd, path, flags|O_CREAT|O_DIRECTORY|O_NOFOLLOW, xopen_flags, mode);
175,142✔
1075
        if (IN_SET(fd, -ELOOP, -ENOTDIR))
175,142✔
1076
                return -EEXIST;
1077
        if (fd < 0)
175,141✔
1078
                return fd;
4,799✔
1079

1080
        return TAKE_FD(fd);
1081
}
1082

1083
int openat_report_new(int dirfd, const char *pathname, int flags, mode_t mode, bool *ret_newly_created) {
32,341,142✔
1084
        int fd;
32,341,142✔
1085

1086
        /* Just like openat(), but adds one thing: optionally returns whether we created the file anew or if
1087
         * it already existed before. This is only relevant if O_CREAT is set without O_EXCL, and thus will
1088
         * shortcut to openat() otherwise.
1089
         *
1090
         * Note that this routine is a bit more strict with symlinks than regular openat() is. If O_NOFOLLOW
1091
         * is not specified, then we'll follow the symlink when opening an existing file but we will *not*
1092
         * follow it when creating a new one (because that's a terrible UNIX misfeature and generally a
1093
         * security hole). */
1094

1095
        if (!FLAGS_SET(flags, O_CREAT) || FLAGS_SET(flags, O_EXCL)) {
32,341,142✔
1096
                fd = openat(dirfd, pathname, flags, mode);
30,528,630✔
1097
                if (fd < 0)
30,528,630✔
1098
                        return -errno;
3,808,464✔
1099

1100
                if (ret_newly_created)
26,720,166✔
1101
                        *ret_newly_created = FLAGS_SET(flags, O_CREAT);
26,720,166✔
1102
                return fd;
1103
        }
1104

1105
        for (unsigned attempts = 7;;) {
1106
                /* First, attempt to open without O_CREAT/O_EXCL, i.e. open existing file */
1107
                fd = openat(dirfd, pathname, flags & ~(O_CREAT | O_EXCL), mode);
1,812,533✔
1108
                if (fd >= 0) {
1,812,533✔
1109
                        if (ret_newly_created)
1,641,989✔
1110
                                *ret_newly_created = false;
1,641,989✔
1111
                        return fd;
1112
                }
1113
                if (errno != ENOENT)
170,544✔
1114
                        return -errno;
×
1115

1116
                /* So the file didn't exist yet, hence create it with O_CREAT/O_EXCL/O_NOFOLLOW. */
1117
                fd = openat(dirfd, pathname, flags | O_CREAT | O_EXCL | O_NOFOLLOW, mode);
170,544✔
1118
                if (fd >= 0) {
170,544✔
1119
                        if (ret_newly_created)
170,521✔
1120
                                *ret_newly_created = true;
170,520✔
1121
                        return fd;
1122
                }
1123
                if (errno != EEXIST)
23✔
1124
                        return -errno;
×
1125

1126
                /* Hmm, so now we got EEXIST? Then someone might have created the file between the first and
1127
                 * second call to openat(). Let's try again but with a limit so we don't spin forever. */
1128

1129
                if (--attempts == 0) /* Give up eventually, somebody is playing with us */
23✔
1130
                        return -EEXIST;
1131
        }
1132
}
1133

1134
static int openat_with_automount(int dir_fd, const char *path, int open_flags, mode_t mode) {
148,615✔
1135
        /* When XO_TRIGGER_AUTOMOUNT is set we want to trigger automounts on the path. open() with O_PATH
1136
         * does not do that, so we use open_tree() without OPEN_TREE_CLONE which is equivalent to open() with
1137
         * O_PATH except that it does trigger automounts. Some sandboxes reject open_tree() with EPERM or
1138
         * ENOSYS, in which case we fall back to plain openat(): autofs wouldn't work inside a restricted
1139
         * mount namespace anyway. open_tree() only ever returns O_PATH fds, so this helper is for O_PATH
1140
         * acquisition only. */
1141

1142
        static bool can_open_tree = true;
148,615✔
1143

1144
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
148,615✔
1145
        assert(path);
148,615✔
1146
        assert(FLAGS_SET(open_flags, O_PATH));
148,615✔
1147

1148
        if (can_open_tree) {
148,615✔
1149
                int fd = RET_NERRNO(open_tree(dir_fd, path,
148,615✔
1150
                                              OPEN_TREE_CLOEXEC |
148,615✔
1151
                                              (FLAGS_SET(open_flags, O_NOFOLLOW) ? AT_SYMLINK_NOFOLLOW : 0)));
148,615✔
1152
                if (fd >= 0) {
3,293✔
1153
                        /* open_tree() doesn't honor O_DIRECTORY, so enforce it ourselves to match
1154
                         * the openat() fallback's behavior. */
1155
                        if (FLAGS_SET(open_flags, O_DIRECTORY)) {
145,322✔
1156
                                int q = fd_verify_directory(fd);
11✔
1157
                                if (q < 0) {
11✔
1158
                                        safe_close(fd);
×
1159
                                        return q;
×
1160
                                }
1161
                        }
1162

1163
                        return fd;
1164
                }
1165
                if (fd != -EPERM && !ERRNO_IS_NEG_NOT_SUPPORTED(fd))
3,293✔
1166
                        return fd;
1167

1168
                can_open_tree = false;
×
1169
        }
1170

1171
        return RET_NERRNO(openat(dir_fd, path, open_flags, mode));
×
1172
}
1173

1174
int xopenat_full(int dir_fd, const char *path, int open_flags, XOpenFlags xopen_flags, mode_t mode) {
37,221,556✔
1175
        _cleanup_close_ int fd = -EBADF;
37,221,556✔
1176
        bool made_dir = false, made_file = false;
37,221,556✔
1177
        int r;
37,221,556✔
1178

1179
        assert(wildcard_fd_is_valid(dir_fd));
37,221,556✔
1180

1181
        /* An inode can only be one of a directory, a regular file or a socket at the same time. */
1182
        assert(FLAGS_SET(open_flags, O_DIRECTORY) + FLAGS_SET(xopen_flags, XO_REGULAR) + FLAGS_SET(xopen_flags, XO_SOCKET) <= 1);
37,221,556✔
1183
        /* Sockets cannot be open()ed, only pinned via O_PATH. */
1184
        assert(!FLAGS_SET(xopen_flags, XO_SOCKET) || FLAGS_SET(open_flags, O_PATH));
37,221,556✔
1185
        /* XO_TRIGGER_AUTOMOUNT requires O_PATH and does not support creating inodes. XO_SUBVOLUME
1186
         * requires O_CREAT, and XO_COW/XO_NOCOW need a writable fd for their chattr ioctl, so none are
1187
         * compatible with XO_TRIGGER_AUTOMOUNT. */
1188
        assert(!FLAGS_SET(xopen_flags, XO_TRIGGER_AUTOMOUNT) ||
37,221,556✔
1189
               (FLAGS_SET(open_flags, O_PATH) && !FLAGS_SET(open_flags, O_CREAT)));
1190
        assert(!(FLAGS_SET(xopen_flags, XO_TRIGGER_AUTOMOUNT) && FLAGS_SET(xopen_flags, XO_SUBVOLUME)));
37,221,556✔
1191
        assert(!(FLAGS_SET(xopen_flags, XO_TRIGGER_AUTOMOUNT) && (xopen_flags & (XO_COW|XO_NOCOW))));
37,221,556✔
1192
        assert((xopen_flags & (XO_COW|XO_NOCOW)) != (XO_COW|XO_NOCOW));
37,221,556✔
1193

1194
        /* Don't specify an access mode if you want auto mode. */
1195
        assert(!FLAGS_SET(xopen_flags, XO_AUTO_RW_RO) || (open_flags & O_ACCMODE_STRICT) == 0);
37,221,556✔
1196

1197
        /* This is like openat(), but has a few tricks up its sleeves, extending behaviour:
1198
         *
1199
         *   • O_DIRECTORY|O_CREAT is supported, which causes a directory to be created, and immediately
1200
         *     opened. When used with the XO_SUBVOLUME flag this will even create a btrfs subvolume.
1201
         *
1202
         *   • If O_CREAT is used with XO_LABEL, any created file will be immediately relabelled.
1203
         *
1204
         *   • If the path is specified NULL or empty, behaves like fd_reopen().
1205
         *
1206
         *   • If XO_COW or XO_NOCOW is specified will turn off or on the NOCOW btrfs flag on the file, if
1207
         *     available.
1208
         *
1209
         *   • if XO_REGULAR is specified will return an error if inode is not a regular file.
1210
         *
1211
         *   • if XO_SOCKET is specified will return an error if inode is not a socket.
1212
         *
1213
         *   • if XO_TRIGGER_AUTOMOUNT is specified O_PATH fds will trigger automounts.
1214
         *
1215
         *   • If mode is specified as MODE_INVALID, we'll use 0755 for dirs, and 0644 for regular files.
1216
         *
1217
         *   • The dir fd can be passed as XAT_FDROOT, in which case any relative paths will be taken relative to the root fs.
1218
         *
1219
         *   • If XO_AUTO_RW_RO is specified and the file cannot be opened in O_RDWR mode due to EACCES/EROFS or similar, retry in O_RDONLY mode.
1220
         */
1221

1222
        if (mode == MODE_INVALID)
37,221,556✔
1223
                mode = (open_flags & O_DIRECTORY) ? 0755 : 0644;
36,458,289✔
1224

1225
        if (FLAGS_SET(xopen_flags, XO_AUTO_RW_RO)) {
37,221,556✔
1226
                if (open_flags & O_DIRECTORY) {
5✔
1227
                        /* Directories can only be opened in read-only mode */
1228
                        xopen_flags &= ~XO_AUTO_RW_RO;
2✔
1229
                        open_flags |= O_RDONLY;
2✔
1230
                } else if (open_flags & O_PATH)
3✔
1231
                        /* O_PATH is incompatible with O_RDONLY/O_RDWR → fail */
1232
                        return -EINVAL;
1233
        }
1234

1235
        if (isempty(path)) {
37,221,556✔
1236
                assert(!FLAGS_SET(open_flags, O_CREAT|O_EXCL));
6,102,408✔
1237
                open_flags &= ~O_NOFOLLOW;
6,102,408✔
1238

1239
                if (FLAGS_SET(xopen_flags, XO_REGULAR)) {
6,102,408✔
1240
                        r = fd_verify_regular(dir_fd);
63✔
1241
                        if (r < 0)
63✔
1242
                                return r;
1243
                }
1244

1245
                if (FLAGS_SET(xopen_flags, XO_SOCKET)) {
6,102,408✔
1246
                        r = fd_verify_socket(dir_fd);
3✔
1247
                        if (r < 0)
3✔
1248
                                return r;
1249
                }
1250

1251
                if (FLAGS_SET(xopen_flags, XO_AUTO_RW_RO)) {
6,102,406✔
1252
                        /* First try: in r/w mode */
1253
                        fd = fd_reopen(dir_fd, open_flags|O_RDWR);
1✔
1254
                        if (!ERRNO_IS_NEG_FS_WRITE_REFUSED(fd) && fd != -EISDIR)
2✔
1255
                                return TAKE_FD(fd);
37,221,556✔
1256

1257
                        open_flags |= O_RDONLY;
1258
                }
1259

1260
                return fd_reopen(dir_fd, open_flags);
6,102,405✔
1261
        }
1262

1263
        _cleanup_close_ int _dir_fd = -EBADF;
37,221,556✔
1264
        if (dir_fd == XAT_FDROOT) {
31,119,148✔
1265
                if (path_is_absolute(path))
5,029✔
1266
                        dir_fd = AT_FDCWD;
1267
                else {
1268
                        _dir_fd = open("/", O_CLOEXEC|O_DIRECTORY|O_PATH);
75✔
1269
                        if (_dir_fd < 0)
75✔
UNCOV
1270
                                return -errno;
×
1271

1272
                        dir_fd = _dir_fd;
1273
                }
1274
        }
1275

1276
        bool call_label_ops_post = false;
31,119,148✔
1277

1278
        if (FLAGS_SET(open_flags, O_CREAT) && FLAGS_SET(xopen_flags, XO_LABEL)) {
31,119,148✔
1279
                r = label_ops_pre(dir_fd, path, FLAGS_SET(open_flags, O_DIRECTORY) ? S_IFDIR : S_IFREG);
651✔
1280
                if (r < 0)
651✔
1281
                        return r;
1282

1283
                call_label_ops_post = true;
1284
        }
1285

1286
        if (FLAGS_SET(open_flags, O_DIRECTORY|O_CREAT)) {
31,119,148✔
1287
                if (FLAGS_SET(xopen_flags, XO_SUBVOLUME))
214,221✔
1288
                        r = btrfs_subvol_make_fallback(dir_fd, path, mode);
5✔
1289
                else
1290
                        r = RET_NERRNO(mkdirat(dir_fd, path, mode));
214,216✔
1291
                if (r == -EEXIST) {
130,844✔
1292
                        if (FLAGS_SET(open_flags, O_EXCL))
130,836✔
1293
                                return -EEXIST;
1294
                } else if (r < 0)
83,385✔
1295
                        return r;
1296
                else
1297
                        made_dir = true;
1298

1299
                open_flags &= ~(O_EXCL|O_CREAT);
209,347✔
1300
        }
1301

1302
        if (FLAGS_SET(xopen_flags, XO_REGULAR)) {
31,114,274✔
1303
                /* Guarantee we return a regular fd only, and don't open the file unless we verified it
1304
                 * first */
1305

1306
                if (FLAGS_SET(open_flags, O_PATH)) {
435,984✔
1307
                        fd = FLAGS_SET(xopen_flags, XO_TRIGGER_AUTOMOUNT) ?
2✔
1308
                                openat_with_automount(dir_fd, path, open_flags, mode) :
1✔
1309
                                RET_NERRNO(openat(dir_fd, path, open_flags, mode));
1✔
1310
                        if (fd < 0) {
×
UNCOV
1311
                                r = fd;
×
UNCOV
1312
                                goto error;
×
1313
                        }
1314

1315
                        r = fd_verify_regular(fd);
1✔
1316
                        if (r < 0)
1✔
UNCOV
1317
                                goto error;
×
1318

1319
                } else if (FLAGS_SET(open_flags, O_CREAT|O_EXCL)) {
435,983✔
1320
                        /* In O_EXCL mode we can just create the thing, everything is dealt with for us */
1321

1322
                        if (FLAGS_SET(xopen_flags, XO_AUTO_RW_RO)) {
3✔
1323
                                fd = RET_NERRNO(openat(dir_fd, path, open_flags|O_RDWR, mode));
1✔
1324
                                if (ERRNO_IS_NEG_FS_WRITE_REFUSED(fd))
1✔
1325
                                        open_flags |= O_RDONLY;
1326
                                else if (fd < 0) {
1✔
UNCOV
1327
                                        r = fd;
×
UNCOV
1328
                                        goto error;
×
1329
                                }
1330
                        }
1331

UNCOV
1332
                        if (fd < 0) {
×
1333
                                fd = openat(dir_fd, path, open_flags, mode);
2✔
1334
                                if (fd < 0) {
2✔
1335
                                        r = -errno;
1✔
1336
                                        goto error;
1✔
1337
                                }
1338
                        }
1339

1340
                        made_file = true;
2✔
1341
                } else {
1342
                        /* Otherwise pin the inode first via O_PATH */
1343
                        _cleanup_close_ int inode_fd = openat(dir_fd, path, O_PATH|O_CLOEXEC|(open_flags & O_NOFOLLOW));
435,980✔
1344
                        if (inode_fd < 0) {
435,980✔
1345
                                if (errno != ENOENT || !FLAGS_SET(open_flags, O_CREAT)) {
5,612✔
1346
                                        r = -errno;
5,586✔
1347
                                        goto error;
5,586✔
1348
                                }
1349

1350
                                /* Doesn't exist yet, then try to create it */
1351
                                open_flags |= O_EXCL;
26✔
1352

1353
                                if (FLAGS_SET(xopen_flags, XO_AUTO_RW_RO)) {
26✔
UNCOV
1354
                                        fd = RET_NERRNO(openat(dir_fd, path, open_flags|O_RDWR, mode));
×
1355
                                        if (ERRNO_IS_NEG_FS_WRITE_REFUSED(fd))
×
1356
                                                open_flags |= O_RDONLY;
1357
                                        else if (fd < 0) {
×
UNCOV
1358
                                                r = fd;
×
UNCOV
1359
                                                goto error;
×
1360
                                        }
1361
                                }
1362

UNCOV
1363
                                if (fd < 0) {
×
1364
                                        fd = openat(dir_fd, path, open_flags, mode);
26✔
1365
                                        if (fd < 0) {
26✔
UNCOV
1366
                                                r = -errno;
×
UNCOV
1367
                                                goto error;
×
1368
                                        }
1369
                                }
1370

1371
                                made_file = true;
26✔
1372
                        } else {
1373
                                /* OK, we pinned it. Now verify it's actually a regular file, and then reopen it */
1374
                                r = fd_verify_regular(inode_fd);
430,368✔
1375
                                if (r < 0)
430,368✔
1376
                                        goto error;
49✔
1377

1378
                                open_flags &= ~(O_NOFOLLOW|O_CREAT);
430,319✔
1379

1380
                                if (FLAGS_SET(xopen_flags, XO_AUTO_RW_RO)) {
430,319✔
UNCOV
1381
                                        fd = fd_reopen(inode_fd, open_flags|O_RDWR);
×
1382
                                        if (ERRNO_IS_NEG_FS_WRITE_REFUSED(fd))
×
1383
                                                open_flags |= O_RDONLY;
1384
                                        else if (fd < 0) {
×
UNCOV
1385
                                                r = fd;
×
UNCOV
1386
                                                goto error;
×
1387
                                        }
1388
                                }
1389

UNCOV
1390
                                if (fd < 0) {
×
1391
                                        fd = fd_reopen(inode_fd, open_flags);
430,319✔
1392
                                        if (fd < 0) {
430,319✔
UNCOV
1393
                                                r = fd;
×
UNCOV
1394
                                                goto error;
×
1395
                                        }
1396
                                }
1397
                        }
1398
                }
1399
        } else if (FLAGS_SET(xopen_flags, XO_TRIGGER_AUTOMOUNT)) {
30,678,290✔
1400
                fd = openat_with_automount(dir_fd, path, open_flags, mode);
148,615✔
1401
                if (fd < 0) {
148,615✔
1402
                        r = fd;
3,293✔
1403
                        goto error;
3,293✔
1404
                }
1405
        } else {
1406
                /* XO_SOCKET also lands here: it requires O_PATH (see asserts above) so openat() pins
1407
                 * the inode without connecting, and fd_verify_socket() below enforces the type. */
1408
                if (FLAGS_SET(xopen_flags, XO_AUTO_RW_RO)) {
30,529,675✔
1409
                        fd = openat_report_new(dir_fd, path, O_RDWR|open_flags, mode, &made_file);
1✔
1410
                        if (ERRNO_IS_NEG_FS_WRITE_REFUSED(fd) || fd == -EISDIR)
2✔
1411
                                open_flags |= O_RDONLY;
1412
                        else if (fd < 0) {
1✔
UNCOV
1413
                                r = fd;
×
UNCOV
1414
                                goto error;
×
1415
                        }
1416
                }
1417

UNCOV
1418
                if (fd < 0) {
×
1419
                        fd = openat_report_new(dir_fd, path, open_flags, mode, &made_file);
30,529,674✔
1420
                        if (fd < 0) {
30,529,674✔
1421
                                r = fd;
3,807,177✔
1422
                                goto error;
3,807,177✔
1423
                        }
1424
                }
1425
        }
1426

1427
        if (FLAGS_SET(xopen_flags, XO_SOCKET)) {
27,298,168✔
1428
                r = fd_verify_socket(fd);
3✔
1429
                if (r < 0)
3✔
1430
                        goto error;
2✔
1431
        }
1432

1433
        if (call_label_ops_post) {
27,298,166✔
1434
                call_label_ops_post = false;
651✔
1435

1436
                r = label_ops_post(fd, /* path= */ NULL, made_file || made_dir);
651✔
1437
                if (r < 0)
651✔
UNCOV
1438
                        goto error;
×
1439
        }
1440

1441
        if (xopen_flags & (XO_COW|XO_NOCOW)) {
27,298,166✔
1442
                r = chattr_fd(fd, FLAGS_SET(xopen_flags, XO_NOCOW) ? FS_NOCOW_FL : 0, FS_NOCOW_FL);
×
UNCOV
1443
                if (r < 0 && !ERRNO_IS_IOCTL_NOT_SUPPORTED(r))
×
UNCOV
1444
                        goto error;
×
1445
        }
1446

1447
        return TAKE_FD(fd);
1448

1449
error:
3,816,108✔
1450
        if (call_label_ops_post)
3,816,108✔
UNCOV
1451
                (void) label_ops_post(fd >= 0 ? fd : dir_fd, fd >= 0 ? NULL : path, made_dir || made_file);
×
1452

1453
        if (made_dir || made_file)
3,816,108✔
UNCOV
1454
                (void) unlinkat(dir_fd, path, made_dir ? AT_REMOVEDIR : 0);
×
1455

1456
        return r;
1457
}
1458

1459
int xopenat_lock_full(
204,732✔
1460
                int dir_fd,
1461
                const char *path,
1462
                int open_flags,
1463
                XOpenFlags xopen_flags,
1464
                mode_t mode,
1465
                LockType locktype,
1466
                int operation) {
1467

1468
        _cleanup_close_ int fd = -EBADF;
204,732✔
1469
        int r;
204,732✔
1470

1471
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
204,732✔
1472
        assert(IN_SET(operation & ~LOCK_NB, LOCK_EX, LOCK_SH));
204,732✔
1473

1474
        /* POSIX/UNPOSIX locks don't work on directories (errno is set to -EBADF so let's return early with
1475
         * the same error here). */
1476
        if (FLAGS_SET(open_flags, O_DIRECTORY) && !IN_SET(locktype, LOCK_BSD, LOCK_NONE))
204,732✔
1477
                return -EBADF;
1478

1479
        for (;;) {
205,761✔
1480
                struct stat st;
205,246✔
1481

1482
                fd = xopenat_full(dir_fd, path, open_flags, xopen_flags, mode);
205,246✔
1483
                if (fd < 0)
205,246✔
1484
                        return fd;
8✔
1485

1486
                r = lock_generic(fd, locktype, operation);
205,246✔
1487
                if (r < 0)
205,246✔
1488
                        return r;
1489

1490
                /* If we acquired the lock, let's check if the file/directory still exists in the file
1491
                 * system. If not, then the previous exclusive owner removed it and then closed it. In such a
1492
                 * case our acquired lock is worthless, hence try again. */
1493

1494
                if (fstat(fd, &st) < 0)
205,238✔
UNCOV
1495
                        return -errno;
×
1496
                if (st.st_nlink > 0)
205,238✔
1497
                        break;
1498

1499
                fd = safe_close(fd);
515✔
1500
        }
1501

1502
        return TAKE_FD(fd);
204,723✔
1503
}
1504

1505
int link_fd(int fd, int newdirfd, const char *newpath) {
30,733✔
1506
        int r;
30,733✔
1507

1508
        assert(fd >= 0);
30,733✔
1509
        assert(newdirfd >= 0 || newdirfd == AT_FDCWD);
30,733✔
1510
        assert(newpath);
30,733✔
1511

1512
        /* Try to link via AT_EMPTY_PATH first. This fails with ENOENT if we don't have CAP_DAC_READ_SEARCH
1513
         * on kernels < 6.10, in which case we'd then resort to /proc/self/fd/ dance.
1514
         *
1515
         * See also: https://github.com/torvalds/linux/commit/42bd2af5950456d46fdaa91c3a8fb02e680f19f5 */
1516
        r = RET_NERRNO(linkat(fd, "", newdirfd, newpath, AT_EMPTY_PATH));
30,733✔
1517
        if (r == -ENOENT) {
5,989✔
UNCOV
1518
                r = RET_NERRNO(linkat(AT_FDCWD, FORMAT_PROC_FD_PATH(fd), newdirfd, newpath, AT_SYMLINK_FOLLOW));
×
1519
                if (r == -ENOENT && proc_mounted() == 0) /* No proc_fd_enoent_errno() here because we don't
×
1520
                                                            know if it's the target path that's missing. */
UNCOV
1521
                        return -ENOSYS;
×
1522
        }
1523

1524
        return r;
1525
}
1526

1527
int linkat_replace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath) {
23,928✔
1528
        _cleanup_close_ int old_fd = -EBADF;
23,928✔
1529
        int r;
23,928✔
1530

1531
        assert(olddirfd >= 0 || olddirfd == AT_FDCWD);
23,928✔
1532
        assert(newdirfd >= 0 || newdirfd == AT_FDCWD);
23,928✔
1533
        assert(!isempty(newpath)); /* source path is optional, but the target path is not */
23,928✔
1534

1535
        /* Like linkat() but replaces the target if needed. Is a NOP if source and target already share the
1536
         * same inode. */
1537

1538
        if (olddirfd == AT_FDCWD && isempty(oldpath)) /* Refuse operating on the cwd (which is a dir, and dirs can't be hardlinked) */
23,928✔
1539
                return -EISDIR;
1540

1541
        if (path_implies_directory(oldpath)) /* Refuse these definite directories early */
23,928✔
1542
                return -EISDIR;
1543

1544
        if (path_implies_directory(newpath))
23,928✔
1545
                return -EISDIR;
1546

1547
        /* First, try to link this directly */
1548
        if (oldpath)
23,928✔
1549
                r = RET_NERRNO(linkat(olddirfd, oldpath, newdirfd, newpath, 0));
23,978✔
1550
        else
1551
                r = link_fd(olddirfd, newdirfd, newpath);
23,632✔
1552
        if (r >= 0)
23,682✔
1553
                return 0;
1554
        if (r != -EEXIST)
6,036✔
1555
                return r;
1556

1557
        old_fd = xopenat(olddirfd, oldpath, O_PATH|O_CLOEXEC);
6,036✔
1558
        if (old_fd < 0)
6,036✔
1559
                return old_fd;
1560

1561
        struct stat old_st;
6,036✔
1562
        if (fstat(old_fd, &old_st) < 0)
6,036✔
UNCOV
1563
                return -errno;
×
1564

1565
        if (S_ISDIR(old_st.st_mode)) /* Don't bother if we are operating on a directory */
6,036✔
1566
                return -EISDIR;
1567

1568
        struct stat new_st;
6,036✔
1569
        if (fstatat(newdirfd, newpath, &new_st, AT_SYMLINK_NOFOLLOW) < 0)
6,036✔
UNCOV
1570
                return -errno;
×
1571

1572
        if (S_ISDIR(new_st.st_mode)) /* Refuse replacing directories */
6,036✔
1573
                return -EEXIST;
1574

1575
        if (stat_inode_same(&old_st, &new_st)) /* Already the same inode? Then shortcut this */
6,035✔
1576
                return 0;
1577

1578
        _cleanup_free_ char *tmp_path = NULL;
6,034✔
1579
        r = tempfn_random(newpath, /* extra= */ NULL, &tmp_path);
6,034✔
1580
        if (r < 0)
6,034✔
1581
                return r;
1582

1583
        r = link_fd(old_fd, newdirfd, tmp_path);
6,034✔
1584
        if (r < 0) {
6,034✔
UNCOV
1585
                if (!ERRNO_IS_PRIVILEGE(r))
×
1586
                        return r;
1587

1588
                /* If that didn't work due to permissions then go via the path of the dentry */
UNCOV
1589
                r = RET_NERRNO(linkat(olddirfd, oldpath, newdirfd, tmp_path, 0));
×
UNCOV
1590
                if (r < 0)
×
1591
                        return r;
1592
        }
1593

1594
        r = RET_NERRNO(renameat(newdirfd, tmp_path, newdirfd, newpath));
6,034✔
1595
        if (r < 0) {
×
UNCOV
1596
                (void) unlinkat(newdirfd, tmp_path, /* flags= */ 0);
×
UNCOV
1597
                return r;
×
1598
        }
1599

1600
        return 0;
1601
}
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