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

systemd / systemd / 19448983682

17 Nov 2025 11:32PM UTC coverage: 72.503% (-0.2%) from 72.719%
19448983682

push

github

web-flow
core/unit: unit_process_job() tweaks (#39753)

6 of 8 new or added lines in 1 file covered. (75.0%)

3363 existing lines in 68 files now uncovered.

308308 of 425234 relevant lines covered (72.5%)

1141671.45 hits per line

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

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

3
#include <linux/magic.h>
4
#include <sys/mount.h>
5
#include <unistd.h>
6

7
#include "alloc-util.h"
8
#include "chase.h"
9
#include "errno-util.h"
10
#include "fd-util.h"
11
#include "fileio.h"
12
#include "fs-util.h"
13
#include "glyph-util.h"
14
#include "log.h"
15
#include "path-util.h"
16
#include "stat-util.h"
17
#include "string-util.h"
18
#include "strv.h"
19
#include "user-util.h"
20

21
#define CHASE_NO_SHORTCUT_MASK                          \
22
        (CHASE_NONEXISTENT |                            \
23
         CHASE_NO_AUTOFS |                              \
24
         CHASE_TRIGGER_AUTOFS |                         \
25
         CHASE_SAFE |                                   \
26
         CHASE_STEP |                                   \
27
         CHASE_PROHIBIT_SYMLINKS |                      \
28
         CHASE_PARENT |                                 \
29
         CHASE_MKDIR_0755)
30

31
bool unsafe_transition(const struct stat *a, const struct stat *b) {
33,128✔
32
        /* Returns true if the transition from a to b is safe, i.e. that we never transition from unprivileged to
33
         * privileged files or directories. Why bother? So that unprivileged code can't symlink to privileged files
34
         * making us believe we read something safe even though it isn't safe in the specific context we open it in. */
35

36
        if (a->st_uid == 0) /* Transitioning from privileged to unprivileged is always fine */
33,128✔
37
                return false;
38

39
        return a->st_uid != b->st_uid; /* Otherwise we need to stay within the same UID */
21✔
40
}
41

42
static int log_unsafe_transition(int a, int b, const char *path, ChaseFlags flags) {
8✔
43
        _cleanup_free_ char *n1 = NULL, *n2 = NULL, *user_a = NULL, *user_b = NULL;
8✔
44
        struct stat st;
8✔
45

46
        if (!FLAGS_SET(flags, CHASE_WARN))
8✔
47
                return -ENOLINK;
48

49
        (void) fd_get_path(a, &n1);
5✔
50
        (void) fd_get_path(b, &n2);
5✔
51

52
        if (fstat(a, &st) == 0)
5✔
53
                user_a = uid_to_name(st.st_uid);
5✔
54
        if (fstat(b, &st) == 0)
5✔
55
                user_b = uid_to_name(st.st_uid);
5✔
56

57
        return log_warning_errno(SYNTHETIC_ERRNO(ENOLINK),
5✔
58
                                 "Detected unsafe path transition %s (owned by %s) %s %s (owned by %s) during canonicalization of %s.",
59
                                 strna(n1), strna(user_a), glyph(GLYPH_ARROW_RIGHT), strna(n2), strna(user_b), path);
60
}
61

62
static int log_autofs_mount_point(int fd, const char *path, ChaseFlags flags) {
×
63
        _cleanup_free_ char *n1 = NULL;
×
64

65
        if (!FLAGS_SET(flags, CHASE_WARN))
×
66
                return -EREMOTE;
67

68
        (void) fd_get_path(fd, &n1);
×
69

70
        return log_warning_errno(SYNTHETIC_ERRNO(EREMOTE),
×
71
                                 "Detected autofs mount point '%s' during canonicalization of '%s'.",
72
                                 strna(n1), path);
73
}
74

75
static int log_prohibited_symlink(int fd, ChaseFlags flags) {
7✔
76
        _cleanup_free_ char *n1 = NULL;
7✔
77

78
        assert(fd >= 0);
7✔
79

80
        if (!FLAGS_SET(flags, CHASE_WARN))
7✔
81
                return -EREMCHG;
82

83
        (void) fd_get_path(fd, &n1);
3✔
84

85
        return log_warning_errno(SYNTHETIC_ERRNO(EREMCHG),
3✔
86
                                 "Detected symlink where no symlink is allowed at '%s', refusing.",
87
                                 strna(n1));
88
}
89

90
static int openat_opath_with_automount(int dir_fd, const char *path, bool automount) {
19,573,160✔
91
        static bool can_open_tree = true;
19,573,160✔
92
        int r;
19,573,160✔
93

94
        /* Pin an inode via O_PATH semantics. Sounds pretty obvious to do this, right? You just do open()
95
         * with O_PATH, and there you go. But uh, it's not that easy. open() via O_PATH does not trigger
96
         * automounts, but we may want that when CHASE_TRIGGER_AUTOFS is set. But thankfully there's
97
         * a way out: the newer open_tree() call, when specified without OPEN_TREE_CLONE actually is fully
98
         * equivalent to open() with O_PATH – except for one thing: it triggers automounts.
99
         *
100
         * As it turns out some sandboxes prohibit open_tree(), and return EPERM or ENOSYS if we call it.
101
         * But since autofs does not work inside of mount namespace anyway, let's simply handle this
102
         * as gracefully as we can, and fall back to classic openat() if we see EPERM/ENOSYS. */
103

104
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
19,573,160✔
105
        assert(path);
19,573,160✔
106

107
        if (automount && can_open_tree) {
19,573,160✔
108
                r = RET_NERRNO(open_tree(dir_fd, path, AT_SYMLINK_NOFOLLOW|OPEN_TREE_CLOEXEC));
68,322✔
109
                if (r >= 0 || (r != -EPERM && !ERRNO_IS_NEG_NOT_SUPPORTED(r)))
2,920✔
110
                        return r;
68,322✔
111

112
                can_open_tree = false;
×
113
        }
114

115
        return RET_NERRNO(openat(dir_fd, path, O_PATH|O_NOFOLLOW|O_CLOEXEC));
19,504,838✔
116
}
117

118
static int chaseat_needs_absolute(int dir_fd, const char *path) {
3,377,805✔
119
        if (dir_fd < 0)
3,377,805✔
120
                return path_is_absolute(path);
15,364✔
121

122
        return dir_fd_is_root(dir_fd);
3,370,123✔
123
}
124

125
int chaseat(int dir_fd, const char *path, ChaseFlags flags, char **ret_path, int *ret_fd) {
3,378,710✔
126
        _cleanup_free_ char *buffer = NULL, *done = NULL;
3,378,710✔
127
        _cleanup_close_ int fd = -EBADF, root_fd = -EBADF;
6,757,420✔
128
        unsigned max_follow = CHASE_MAX; /* how many symlinks to follow before giving up and returning ELOOP */
3,378,710✔
129
        bool exists = true, append_trail_slash = false;
3,378,710✔
130
        struct stat st; /* stat obtained from fd */
3,378,710✔
131
        const char *todo;
3,378,710✔
132
        int r;
3,378,710✔
133

134
        assert(!FLAGS_SET(flags, CHASE_PREFIX_ROOT));
3,378,710✔
135
        assert(!FLAGS_SET(flags, CHASE_MUST_BE_DIRECTORY|CHASE_MUST_BE_REGULAR));
3,378,710✔
136
        assert(!FLAGS_SET(flags, CHASE_STEP|CHASE_EXTRACT_FILENAME));
3,378,710✔
137
        assert(!FLAGS_SET(flags, CHASE_NO_AUTOFS|CHASE_TRIGGER_AUTOFS));
3,378,710✔
138
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
3,378,710✔
139

140
        if (FLAGS_SET(flags, CHASE_STEP))
3,378,710✔
141
                assert(!ret_fd);
22,929✔
142

143
        if (isempty(path))
3,379,836✔
144
                path = ".";
145

146
        /* This function resolves symlinks of the path relative to the given directory file descriptor. If
147
         * CHASE_AT_RESOLVE_IN_ROOT is specified and a directory file descriptor is provided, symlinks
148
         * are resolved relative to the given directory file descriptor. Otherwise, they are resolved
149
         * relative to the root directory of the host.
150
         *
151
         * Note that when a positive directory file descriptor is provided and CHASE_AT_RESOLVE_IN_ROOT is
152
         * specified and we find an absolute symlink, it is resolved relative to given directory file
153
         * descriptor and not the root of the host. Also, when following relative symlinks, this functions
154
         * ensures they cannot be used to "escape" the given directory file descriptor. If a positive
155
         * directory file descriptor is provided, the "path" parameter is always interpreted relative to the
156
         * given directory file descriptor, even if it is absolute. If the given directory file descriptor is
157
         * AT_FDCWD and "path" is absolute, it is interpreted relative to the root directory of the host.
158
         *
159
         * When "dir_fd" points to a non-root directory and CHASE_AT_RESOLVE_IN_ROOT is set, this function
160
         * always returns a relative path in "ret_path", even if "path" is an absolute path, because openat()
161
         * like functions generally ignore the directory fd if they are provided with an absolute path. When
162
         * CHASE_AT_RESOLVE_IN_ROOT is not set, then this returns relative path to the specified file
163
         * descriptor if all resolved symlinks are relative, otherwise absolute path will be returned. When
164
         * "dir_fd" is AT_FDCWD and "path" is an absolute path, we return an absolute path in "ret_path"
165
         * because otherwise, if the caller passes the returned relative path to another openat() like
166
         * function, it would be resolved relative to the current working directory instead of to "/".
167
         *
168
         * Summary about the result path:
169
         * - "dir_fd" points to the root directory
170
         *    → result will be absolute
171
         * - "dir_fd" points to a non-root directory, and CHASE_AT_RESOLVE_IN_ROOT is set
172
         *    → relative
173
         * - "dir_fd" points to a non-root directory, and CHASE_AT_RESOLVE_IN_ROOT is not set
174
         *    → relative when all resolved symlinks are relative, otherwise absolute
175
         * - "dir_fd" is AT_FDCWD, and "path" is absolute
176
         *    → absolute
177
         * - "dir_fd" is AT_FDCWD, and "path" is relative
178
         *    → relative when all resolved symlinks are relative, otherwise absolute
179
         *
180
         * Algorithmically this operates on two path buffers: "done" are the components of the path we
181
         * already processed and resolved symlinks, "." and ".." of. "todo" are the components of the path we
182
         * still need to process. On each iteration, we move one component from "todo" to "done", processing
183
         * its special meaning each time. We always keep an O_PATH fd to the component we are currently
184
         * processing, thus keeping lookup races to a minimum.
185
         *
186
         * Suggested usage: whenever you want to canonicalize a path, use this function. Pass the absolute
187
         * path you got as-is: fully qualified and relative to your host's root. Optionally, specify the
188
         * "dir_fd" parameter to tell this function what to do when encountering a symlink with an absolute
189
         * path as directory: resolve it relative to the given directory file descriptor.
190
         *
191
         * There are five ways to invoke this function:
192
         *
193
         * 1. Without CHASE_STEP or ret_fd: in this case the path is resolved and the normalized path is
194
         *    returned in `ret_path`. The return value is < 0 on error. If CHASE_NONEXISTENT is also set, 0
195
         *    is returned if the file doesn't exist, > 0 otherwise. If CHASE_NONEXISTENT is not set, >= 0 is
196
         *    returned if the destination was found, -ENOENT if it wasn't.
197
         *
198
         * 2. With ret_fd: in this case the destination is opened after chasing it as O_PATH and this file
199
         *    descriptor is returned as return value. This is useful to open files relative to some root
200
         *    directory. Note that the returned O_PATH file descriptors must be converted into a regular one
201
         *    (using fd_reopen() or such) before it can be used for reading/writing. ret_fd may not be
202
         *    combined with CHASE_NONEXISTENT.
203
         *
204
         * 3. With CHASE_STEP: in this case only a single step of the normalization is executed, i.e. only
205
         *    the first symlink or ".." component of the path is resolved, and the resulting path is
206
         *    returned. This is useful if a caller wants to trace the path through the file system verbosely.
207
         *    Returns < 0 on error, > 0 if the path is fully normalized, and == 0 for each normalization
208
         *    step. This may be combined with CHASE_NONEXISTENT, in which case 1 is returned when a component
209
         *    is not found.
210
         *
211
         * 4. With CHASE_SAFE: in this case the path must not contain unsafe transitions, i.e. transitions
212
         *    from unprivileged to privileged files or directories. In such cases the return value is
213
         *    -ENOLINK. If CHASE_WARN is also set, a warning describing the unsafe transition is emitted.
214
         *    CHASE_WARN cannot be used in PID 1.
215
         *
216
         * 5. With CHASE_NO_AUTOFS: in this case if an autofs mount point is encountered, path normalization
217
         *    is aborted and -EREMOTE is returned. If CHASE_WARN is also set, a warning showing the path of
218
         *    the mount point is emitted. CHASE_WARN cannot be used in PID 1.
219
         */
220

221
        if (FLAGS_SET(flags, CHASE_AT_RESOLVE_IN_ROOT)) {
3,378,710✔
222
                /* If we get AT_FDCWD or dir_fd points to "/", then we always resolve symlinks relative to
223
                 * the host's root. Hence, CHASE_AT_RESOLVE_IN_ROOT is meaningless. */
224

225
                r = dir_fd_is_root_or_cwd(dir_fd);
812,961✔
226
                if (r < 0)
805,335✔
227
                        return r;
228
                if (r > 0)
812,961✔
229
                        flags &= ~CHASE_AT_RESOLVE_IN_ROOT;
730,825✔
230
        }
231

232
        if (!ret_path && ret_fd && (flags & (CHASE_AT_RESOLVE_IN_ROOT|CHASE_NO_SHORTCUT_MASK)) == 0) {
3,378,710✔
233
                /* Shortcut the ret_fd case if the caller isn't interested in the actual path and has no root
234
                 * set and doesn't care about any of the other special features we provide either. */
235
                r = openat(dir_fd, path, O_PATH|O_CLOEXEC|(FLAGS_SET(flags, CHASE_NOFOLLOW) ? O_NOFOLLOW : 0));
905✔
236
                if (r < 0)
905✔
237
                        return -errno;
19✔
238

239
                *ret_fd = r;
886✔
240
                return 0;
886✔
241
        }
242

243
        buffer = strdup(path);
3,377,805✔
244
        if (!buffer)
3,377,805✔
245
                return -ENOMEM;
246

247
        /* If we receive an absolute path together with AT_FDCWD, we need to return an absolute path, because
248
         * a relative path would be interpreted relative to the current working directory. Also, let's make
249
         * the result absolute when the file descriptor of the root directory is specified. */
250
        r = chaseat_needs_absolute(dir_fd, path);
3,377,805✔
251
        if (r < 0)
3,377,805✔
252
                return r;
253

254
        bool need_absolute = r;
3,377,805✔
255
        if (need_absolute) {
3,377,805✔
256
                done = strdup("/");
3,294,764✔
257
                if (!done)
3,294,764✔
258
                        return -ENOMEM;
259
        }
260

261
        /* If a positive directory file descriptor is provided, always resolve the given path relative to it,
262
         * regardless of whether it is absolute or not. If we get AT_FDCWD, follow regular openat()
263
         * semantics, if the path is relative, resolve against the current working directory. Otherwise,
264
         * resolve against root. */
265
        fd = openat(dir_fd, done ?: ".", O_CLOEXEC|O_DIRECTORY|O_PATH);
3,460,846✔
266
        if (fd < 0)
3,377,805✔
267
                return -errno;
×
268

269
        if (fstat(fd, &st) < 0)
3,377,805✔
270
                return -errno;
×
271

272
        /* If we get AT_FDCWD, we always resolve symlinks relative to the host's root. Only if a positive
273
         * directory file descriptor is provided we will look at CHASE_AT_RESOLVE_IN_ROOT to determine
274
         * whether to resolve symlinks in it or not. */
275
        if (dir_fd >= 0 && FLAGS_SET(flags, CHASE_AT_RESOLVE_IN_ROOT))
3,377,805✔
276
                root_fd = openat(dir_fd, ".", O_CLOEXEC|O_DIRECTORY|O_PATH);
82,136✔
277
        else
278
                root_fd = open("/", O_CLOEXEC|O_DIRECTORY|O_PATH);
3,295,669✔
279
        if (root_fd < 0)
3,377,805✔
280
                return -errno;
×
281

282
        if (ENDSWITH_SET(buffer, "/", "/.")) {
3,377,805✔
283
                flags |= CHASE_MUST_BE_DIRECTORY;
5,937✔
284
                if (FLAGS_SET(flags, CHASE_TRAIL_SLASH))
5,937✔
285
                        append_trail_slash = true;
10✔
286
        } else if (dot_or_dot_dot(buffer) || endswith(buffer, "/.."))
3,371,868✔
287
                flags |= CHASE_MUST_BE_DIRECTORY;
1,135✔
288

289
        if (FLAGS_SET(flags, CHASE_PARENT))
3,377,805✔
290
                flags |= CHASE_MUST_BE_DIRECTORY;
37,858✔
291

292
        for (todo = buffer;;) {
3,377,805✔
293
                _cleanup_free_ char *first = NULL;
20,417,281✔
294
                _cleanup_close_ int child = -EBADF;
22,514,153✔
295
                struct stat st_child;
22,514,153✔
296
                const char *e;
22,514,153✔
297

298
                r = path_find_first_component(&todo, /* accept_dot_dot= */ true, &e);
22,514,153✔
299
                if (r < 0)
22,514,153✔
300
                        return r;
301
                if (r == 0) /* We reached the end. */
22,514,153✔
302
                        break;
303

304
                first = strndup(e, r);
20,468,391✔
305
                if (!first)
20,468,391✔
306
                        return -ENOMEM;
307

308
                /* Two dots? Then chop off the last bit of what we already found out. */
309
                if (streq(first, "..")) {
20,468,391✔
310
                        _cleanup_free_ char *parent = NULL;
895,231✔
311
                        _cleanup_close_ int fd_parent = -EBADF;
895,231✔
312
                        struct stat st_parent;
895,231✔
313

314
                        /* If we already are at the top, then going up will not change anything. This is
315
                         * in-line with how the kernel handles this. */
316
                        if (empty_or_root(done) && FLAGS_SET(flags, CHASE_AT_RESOLVE_IN_ROOT)) {
895,231✔
317
                                if (FLAGS_SET(flags, CHASE_STEP))
121✔
318
                                        goto chased_one;
×
319
                                continue;
121✔
320
                        }
321

322
                        fd_parent = openat(fd, "..", O_CLOEXEC|O_NOFOLLOW|O_PATH|O_DIRECTORY);
895,110✔
323
                        if (fd_parent < 0)
895,110✔
324
                                return -errno;
×
325

326
                        if (fstat(fd_parent, &st_parent) < 0)
895,110✔
327
                                return -errno;
×
328

329
                        /* If we opened the same directory, that _may_ indicate that we're at the host root
330
                         * directory. Let's confirm that in more detail with dir_fd_is_root(). And if so,
331
                         * going up won't change anything. */
332
                        if (stat_inode_same(&st_parent, &st)) {
895,110✔
333
                                r = dir_fd_is_root(fd);
42✔
334
                                if (r < 0)
42✔
335
                                        return r;
336
                                if (r > 0) {
42✔
337
                                        if (FLAGS_SET(flags, CHASE_STEP))
42✔
338
                                                goto chased_one;
×
339
                                        continue;
42✔
340
                                }
341
                        }
342

343
                        r = path_extract_directory(done, &parent);
895,068✔
344
                        if (r >= 0) {
895,068✔
345
                                assert(!need_absolute || path_is_absolute(parent));
893,800✔
346
                                free_and_replace(done, parent);
893,800✔
347
                        } else if (r == -EDESTADDRREQ) {
1,268✔
348
                                /* 'done' contains filename only (i.e. no slash). */
349
                                assert(!need_absolute);
1,268✔
350
                                done = mfree(done);
1,268✔
351
                        } else if (r == -EADDRNOTAVAIL) {
×
352
                                /* 'done' is "/". This branch should be already handled in the above. */
353
                                assert(!FLAGS_SET(flags, CHASE_AT_RESOLVE_IN_ROOT));
×
354
                                assert_not_reached();
×
355
                        } else if (r == -EINVAL) {
×
356
                                /* 'done' is an empty string, ends with '..', or an invalid path. */
357
                                assert(!need_absolute);
×
358
                                assert(!FLAGS_SET(flags, CHASE_AT_RESOLVE_IN_ROOT));
×
359

360
                                if (!path_is_valid(done))
×
361
                                        return -EINVAL;
362

363
                                /* If we're at the top of "dir_fd", start appending ".." to "done". */
364
                                if (!path_extend(&done, ".."))
×
365
                                        return -ENOMEM;
366
                        } else
367
                                return r;
368

369
                        if (FLAGS_SET(flags, CHASE_STEP))
895,068✔
370
                                goto chased_one;
6✔
371

372
                        if (FLAGS_SET(flags, CHASE_SAFE) &&
895,062✔
373
                            unsafe_transition(&st, &st_parent))
×
374
                                return log_unsafe_transition(fd, fd_parent, path, flags);
×
375

376
                        /* If the path ends on a "..", and CHASE_PARENT is specified then our current 'fd' is
377
                         * the child of the returned normalized path, not the parent as requested. To correct
378
                         * this we have to go *two* levels up. */
379
                        if (FLAGS_SET(flags, CHASE_PARENT) && isempty(todo)) {
895,062✔
380
                                _cleanup_close_ int fd_grandparent = -EBADF;
2✔
381
                                struct stat st_grandparent;
2✔
382

383
                                fd_grandparent = openat(fd_parent, "..", O_CLOEXEC|O_NOFOLLOW|O_PATH|O_DIRECTORY);
2✔
384
                                if (fd_grandparent < 0)
2✔
385
                                        return -errno;
×
386

387
                                if (fstat(fd_grandparent, &st_grandparent) < 0)
2✔
388
                                        return -errno;
×
389

390
                                if (FLAGS_SET(flags, CHASE_SAFE) &&
2✔
391
                                    unsafe_transition(&st_parent, &st_grandparent))
×
392
                                        return log_unsafe_transition(fd_parent, fd_grandparent, path, flags);
×
393

394
                                st = st_grandparent;
2✔
395
                                close_and_replace(fd, fd_grandparent);
2✔
396
                                break;
2✔
397
                        }
398

399
                        /* update fd and stat */
400
                        st = st_parent;
895,060✔
401
                        close_and_replace(fd, fd_parent);
895,060✔
402
                        continue;
895,060✔
403
                }
404

405
                /* Otherwise let's pin it by file descriptor, via O_PATH. */
406
                child = r = openat_opath_with_automount(fd, first, /* automount = */ FLAGS_SET(flags, CHASE_TRIGGER_AUTOFS));
19,573,160✔
407
                if (r < 0) {
19,573,160✔
408
                        if (r != -ENOENT)
1,300,906✔
409
                                return r;
410

411
                        if (!isempty(todo) && !path_is_safe(todo)) /* Refuse parent/mkdir handling if suffix contains ".." or something weird */
1,740,928✔
412
                                return r;
413

414
                        if (FLAGS_SET(flags, CHASE_MKDIR_0755) && (!isempty(todo) || !(flags & (CHASE_PARENT|CHASE_NONEXISTENT)))) {
1,301,579✔
415
                                child = xopenat(fd,
199✔
416
                                                first,
417
                                                O_DIRECTORY|O_CREAT|O_EXCL|O_NOFOLLOW|O_PATH|O_CLOEXEC);
418
                                if (child < 0)
199✔
419
                                        return child;
420
                        } else if (FLAGS_SET(flags, CHASE_PARENT) && isempty(todo)) {
1,300,703✔
421
                                if (!path_extend(&done, first))
5,804✔
422
                                        return -ENOMEM;
423

424
                                break;
425
                        } else if (FLAGS_SET(flags, CHASE_NONEXISTENT)) {
1,294,899✔
426
                                if (!path_extend(&done, first, todo))
13,988✔
427
                                        return -ENOMEM;
428

429
                                exists = false;
430
                                break;
431
                        } else
432
                                return r;
433
                }
434

435
                /* ... and then check what it actually is. */
436
                if (fstat(child, &st_child) < 0)
18,272,453✔
UNCOV
437
                        return -errno;
×
438

439
                if (FLAGS_SET(flags, CHASE_SAFE) &&
18,305,576✔
440
                    unsafe_transition(&st, &st_child))
33,123✔
441
                        return log_unsafe_transition(fd, child, path, flags);
5✔
442

443
                if (FLAGS_SET(flags, CHASE_NO_AUTOFS) &&
18,272,448✔
444
                    fd_is_fs_type(child, AUTOFS_SUPER_MAGIC) > 0)
7,679,742✔
UNCOV
445
                        return log_autofs_mount_point(child, path, flags);
×
446

447
                if (S_ISLNK(st_child.st_mode) && !(FLAGS_SET(flags, CHASE_NOFOLLOW) && isempty(todo))) {
18,272,448✔
448
                        _cleanup_free_ char *destination = NULL;
13✔
449

450
                        if (FLAGS_SET(flags, CHASE_PROHIBIT_SYMLINKS))
412,329✔
451
                                return log_prohibited_symlink(child, flags);
7✔
452

453
                        /* This is a symlink, in this case read the destination. But let's make sure we
454
                         * don't follow symlinks without bounds. */
455
                        if (--max_follow <= 0)
412,322✔
456
                                return -ELOOP;
457

458
                        r = readlinkat_malloc(fd, first, &destination);
412,321✔
459
                        if (r < 0)
412,321✔
460
                                return r;
461
                        if (isempty(destination))
412,332✔
462
                                return -EINVAL;
463

464
                        if (path_is_absolute(destination)) {
412,319✔
465

466
                                /* An absolute destination. Start the loop from the beginning, but use the
467
                                 * root file descriptor as base. */
468

469
                                safe_close(fd);
7,368✔
470
                                fd = fd_reopen(root_fd, O_CLOEXEC|O_PATH|O_DIRECTORY);
7,368✔
471
                                if (fd < 0)
7,368✔
472
                                        return fd;
473

474
                                if (fstat(fd, &st) < 0)
7,368✔
UNCOV
475
                                        return -errno;
×
476

477
                                if (FLAGS_SET(flags, CHASE_SAFE) &&
7,373✔
478
                                    unsafe_transition(&st_child, &st))
5✔
479
                                        return log_unsafe_transition(child, fd, path, flags);
3✔
480

481
                                /* When CHASE_AT_RESOLVE_IN_ROOT is not set, now the chased path may be
482
                                 * outside of the specified dir_fd. Let's make the result absolute. */
483
                                if (!FLAGS_SET(flags, CHASE_AT_RESOLVE_IN_ROOT))
7,365✔
484
                                        need_absolute = true;
485

486
                                r = free_and_strdup(&done, need_absolute ? "/" : NULL);
7,664✔
487
                                if (r < 0)
7,365✔
488
                                        return r;
489
                        }
490

491
                        /* Prefix what's left to do with what we just read, and start the loop again, but
492
                         * remain in the current directory. */
493
                        if (!path_extend(&destination, todo))
412,316✔
494
                                return -ENOMEM;
495

496
                        free_and_replace(buffer, destination);
412,316✔
497
                        todo = buffer;
412,316✔
498

499
                        if (FLAGS_SET(flags, CHASE_STEP))
412,316✔
500
                                goto chased_one;
8✔
501

502
                        continue;
412,308✔
503
                }
504

505
                /* If this is not a symlink, then let's just add the name we read to what we already verified. */
506
                if (!path_extend(&done, first))
17,860,119✔
507
                        return -ENOMEM;
508

509
                if (FLAGS_SET(flags, CHASE_PARENT) && isempty(todo))
19,956,977✔
510
                        break;
511

512
                /* And iterate again, but go one directory further down. */
513
                st = st_child;
17,828,817✔
514
                close_and_replace(fd, child);
17,828,817✔
515
        }
516

517
        if (exists) {
2,096,858✔
518
                if (FLAGS_SET(flags, CHASE_MUST_BE_DIRECTORY)) {
2,082,870✔
519
                        r = stat_verify_directory(&st);
52,445✔
520
                        if (r < 0)
52,445✔
521
                                return r;
522
                }
523

524
                if (FLAGS_SET(flags, CHASE_MUST_BE_REGULAR)) {
2,082,870✔
525
                        r = stat_verify_regular(&st);
628✔
526
                        if (r < 0)
628✔
527
                                return r;
528
                }
529
        }
530

531
        if (ret_path) {
2,096,851✔
532
                if (FLAGS_SET(flags, CHASE_EXTRACT_FILENAME) && done) {
2,026,904✔
UNCOV
533
                        _cleanup_free_ char *f = NULL;
×
534

535
                        r = path_extract_filename(done, &f);
33,923✔
536
                        if (r < 0 && r != -EADDRNOTAVAIL)
33,923✔
UNCOV
537
                                return r;
×
538

539
                        /* If we get EADDRNOTAVAIL we clear done and it will get reinitialized by the next block. */
540
                        free_and_replace(done, f);
33,923✔
541
                }
542

543
                if (!done) {
2,026,904✔
544
                        assert(!need_absolute || FLAGS_SET(flags, CHASE_EXTRACT_FILENAME));
261✔
545
                        done = strdup(".");
261✔
546
                        if (!done)
261✔
547
                                return -ENOMEM;
548
                }
549

550
                if (append_trail_slash)
2,026,904✔
551
                        if (!strextend(&done, "/"))
10✔
552
                                return -ENOMEM;
553

554
                *ret_path = TAKE_PTR(done);
2,026,904✔
555
        }
556

557
        if (ret_fd) {
2,096,851✔
558
                if (exists) {
1,954,535✔
559
                        /* Return the O_PATH fd we currently are looking to the caller. It can translate it
560
                         * to a proper fd by opening /proc/self/fd/xyz. */
561
                        assert(fd >= 0);
1,954,399✔
562
                        *ret_fd = TAKE_FD(fd);
1,954,399✔
563
                } else
564
                        *ret_fd = -EBADF;
136✔
565
        }
566

567
        if (FLAGS_SET(flags, CHASE_STEP))
2,096,851✔
568
                return 1;
569

570
        return exists;
2,073,936✔
571

572
chased_one:
14✔
573
        if (ret_path) {
14✔
574
                const char *e;
14✔
575

576
                if (!done) {
14✔
577
                        assert(!need_absolute);
1✔
578
                        done = strdup(append_trail_slash ? "./" : ".");
1✔
579
                        if (!done)
1✔
UNCOV
580
                                return -ENOMEM;
×
581
                }
582

583
                /* todo may contain slashes at the beginning. */
584
                r = path_find_first_component(&todo, /* accept_dot_dot= */ true, &e);
14✔
585
                if (r < 0)
14✔
586
                        return r;
587
                if (r == 0)
14✔
UNCOV
588
                        *ret_path = TAKE_PTR(done);
×
589
                else {
590
                        char *c;
14✔
591

592
                        c = path_join(done, e);
14✔
593
                        if (!c)
14✔
594
                                return -ENOMEM;
595

596
                        *ret_path = c;
14✔
597
                }
598
        }
599

600
        return 0;
601
}
602

603
int chase(const char *path, const char *root, ChaseFlags flags, char **ret_path, int *ret_fd) {
2,639,952✔
604
        _cleanup_free_ char *root_abs = NULL, *absolute = NULL, *p = NULL;
2,639,952✔
605
        _cleanup_close_ int fd = -EBADF, pfd = -EBADF;
5,279,904✔
606
        int r;
2,639,952✔
607

608
        assert(path);
2,639,952✔
609

610
        if (isempty(path))
5,279,903✔
611
                return -EINVAL;
612

613
        r = empty_or_root_harder_to_null(&root);
2,639,951✔
614
        if (r < 0)
2,639,951✔
615
                return r;
616

617
        /* A root directory of "/" or "" is identical to "/". */
618
        if (empty_or_root(root)) {
2,639,948✔
619
                root = "/";
2,564,926✔
620

621
                /* When the root directory is "/", we will drop CHASE_AT_RESOLVE_IN_ROOT in chaseat(),
622
                 * hence below is not necessary, but let's shortcut. */
623
                flags &= ~CHASE_AT_RESOLVE_IN_ROOT;
2,564,926✔
624

625
        } else {
626
                r = path_make_absolute_cwd(root, &root_abs);
75,022✔
627
                if (r < 0)
75,022✔
628
                        return r;
629

630
                /* Simplify the root directory, so that it has no duplicate slashes and nothing at the
631
                 * end. While we won't resolve the root path we still simplify it. */
632
                root = path_simplify(root_abs);
75,022✔
633

634
                assert(path_is_absolute(root));
75,022✔
635
                assert(!empty_or_root(root));
75,022✔
636

637
                if (FLAGS_SET(flags, CHASE_PREFIX_ROOT)) {
75,022✔
638
                        absolute = path_join(root, path);
14,049✔
639
                        if (!absolute)
14,049✔
640
                                return -ENOMEM;
641
                }
642

643
                flags |= CHASE_AT_RESOLVE_IN_ROOT;
75,022✔
644
        }
645

646
        if (!absolute) {
2,639,948✔
647
                r = path_make_absolute_cwd(path, &absolute);
2,625,899✔
648
                if (r < 0)
2,625,899✔
649
                        return r;
650
        }
651

652
        path = path_startswith(absolute, root);
2,639,948✔
653
        if (!path)
2,639,948✔
UNCOV
654
                return log_full_errno(FLAGS_SET(flags, CHASE_WARN) ? LOG_WARNING : LOG_DEBUG,
×
655
                                      SYNTHETIC_ERRNO(ECHRNG),
656
                                      "Specified path '%s' is outside of specified root directory '%s', refusing to resolve.",
657
                                      absolute, root);
658

659
        fd = open(root, O_CLOEXEC|O_DIRECTORY|O_PATH);
2,639,948✔
660
        if (fd < 0)
2,639,948✔
661
                return -errno;
1✔
662

663
        r = chaseat(fd, path, flags & ~CHASE_PREFIX_ROOT, ret_path ? &p : NULL, ret_fd ? &pfd : NULL);
3,129,780✔
664
        if (r < 0)
2,639,947✔
665
                return r;
666

667
        if (ret_path) {
1,919,724✔
668
                if (!FLAGS_SET(flags, CHASE_EXTRACT_FILENAME)) {
1,858,392✔
669

670
                        /* When "root" points to the root directory, the result of chaseat() is always
671
                         * absolute, hence it is not necessary to prefix with the root. When "root" points to
672
                         * a non-root directory, the result path is always normalized and relative, hence
673
                         * we can simply call path_join() and not necessary to call path_simplify().
674
                         * As a special case, chaseat() may return "." or "./", which are normalized too,
675
                         * but we need to drop "." before merging with root. */
676

677
                        if (empty_or_root(root))
1,826,069✔
678
                                assert(path_is_absolute(p));
1,791,706✔
679
                        else {
680
                                char *q;
34,363✔
681

682
                                assert(!path_is_absolute(p));
34,363✔
683

684
                                q = path_join(root, p + STR_IN_SET(p, ".", "./"));
34,363✔
685
                                if (!q)
34,363✔
UNCOV
686
                                        return -ENOMEM;
×
687

688
                                free_and_replace(p, q);
34,363✔
689
                        }
690
                }
691

692
                *ret_path = TAKE_PTR(p);
1,858,392✔
693
        }
694

695
        if (ret_fd)
1,919,724✔
696
                *ret_fd = TAKE_FD(pfd);
1,786,161✔
697

698
        return r;
699
}
700

701
int chaseat_prefix_root(const char *path, const char *root, char **ret) {
127,889✔
702
        char *q;
127,889✔
703
        int r;
127,889✔
704

705
        assert(path);
127,889✔
706
        assert(ret);
127,889✔
707

708
        /* This is mostly for prefixing the result of chaseat(). */
709

710
        if (!path_is_absolute(path)) {
127,889✔
711
                _cleanup_free_ char *root_abs = NULL;
2,987✔
712

713
                r = empty_or_root_harder_to_null(&root);
2,987✔
714
                if (r < 0 && r != -ENOENT)
2,987✔
715
                        return r;
716

717
                /* If the dir_fd points to the root directory, chaseat() always returns an absolute path. */
718
                if (empty_or_root(root))
2,987✔
719
                        return -EINVAL;
720

721
                r = path_make_absolute_cwd(root, &root_abs);
2,987✔
722
                if (r < 0)
2,987✔
723
                        return r;
724

725
                root = path_simplify(root_abs);
2,987✔
726

727
                q = path_join(root, path + (path[0] == '.' && IN_SET(path[1], '/', '\0')));
2,987✔
728
        } else
729
                q = strdup(path);
124,902✔
730
        if (!q)
127,889✔
731
                return -ENOMEM;
732

733
        *ret = q;
127,889✔
734
        return 0;
127,889✔
735
}
736

737
int chase_extract_filename(const char *path, const char *root, char **ret) {
2,327✔
738
        int r;
2,327✔
739

740
        /* This is similar to path_extract_filename(), but takes root directory.
741
         * The result should be consistent with chase() with CHASE_EXTRACT_FILENAME. */
742

743
        assert(path);
2,327✔
744
        assert(ret);
2,327✔
745

746
        if (isempty(path))
2,327✔
747
                return -EINVAL;
748

749
        if (!path_is_absolute(path))
4,654✔
750
                return -EINVAL;
751

752
        r = empty_or_root_harder_to_null(&root);
2,327✔
753
        if (r < 0 && r != -ENOENT)
2,327✔
754
                return r;
755

756
        if (!empty_or_root(root)) {
2,327✔
757
                _cleanup_free_ char *root_abs = NULL;
1,177✔
758

759
                r = path_make_absolute_cwd(root, &root_abs);
1,177✔
760
                if (r < 0)
1,177✔
761
                        return r;
762

763
                path = path_startswith(path, root_abs);
1,177✔
764
                if (!path)
1,177✔
765
                        return -EINVAL;
766
        }
767

768
        if (!isempty(path)) {
2,327✔
769
                r = path_extract_filename(path, ret);
2,178✔
770
                if (r != -EADDRNOTAVAIL)
2,178✔
771
                        return r;
772
        }
773

774
        return strdup_to(ret, ".");
156✔
775
}
776

777
int chase_and_open(
43,640✔
778
                const char *path,
779
                const char *root,
780
                ChaseFlags chase_flags,
781
                int open_flags,
782
                char **ret_path) {
783

784
        _cleanup_close_ int path_fd = -EBADF;
43,640✔
785
        _cleanup_free_ char *p = NULL, *fname = NULL;
43,640✔
786
        int r;
43,640✔
787

788
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP)));
43,640✔
789

790
        XOpenFlags xopen_flags = 0;
43,640✔
791
        if (FLAGS_SET(chase_flags, CHASE_MUST_BE_DIRECTORY))
43,640✔
792
                open_flags |= O_DIRECTORY;
14✔
793
        if (FLAGS_SET(chase_flags, CHASE_MUST_BE_REGULAR))
43,640✔
794
                xopen_flags |= XO_REGULAR;
505✔
795

796
        if (empty_or_root(root) && !ret_path && (chase_flags & CHASE_NO_SHORTCUT_MASK) == 0)
43,640✔
797
                /* Shortcut this call if none of the special features of this call are requested */
798
                return xopenat_full(AT_FDCWD, path,
40,902✔
799
                                    open_flags | (FLAGS_SET(chase_flags, CHASE_NOFOLLOW) ? O_NOFOLLOW : 0),
40,902✔
800
                                    xopen_flags,
801
                                    MODE_INVALID);
802

803
        r = chase(path, root, (CHASE_PARENT|chase_flags)&~CHASE_MUST_BE_REGULAR, &p, &path_fd);
2,738✔
804
        if (r < 0)
2,738✔
805
                return r;
806
        assert(path_fd >= 0);
2,322✔
807

808
        if (!FLAGS_SET(chase_flags, CHASE_PARENT) &&
2,322✔
809
            !FLAGS_SET(chase_flags, CHASE_EXTRACT_FILENAME)) {
2,321✔
810
                r = chase_extract_filename(p, root, &fname);
2,321✔
811
                if (r < 0)
2,321✔
812
                        return r;
813
        }
814

815
        r = xopenat_full(path_fd, strempty(fname), open_flags|O_NOFOLLOW, xopen_flags, MODE_INVALID);
2,323✔
816
        if (r < 0)
2,322✔
817
                return r;
818

819
        if (ret_path)
1,952✔
820
                *ret_path = TAKE_PTR(p);
768✔
821

822
        return r;
823
}
824

825
int chase_and_opendir(const char *path, const char *root, ChaseFlags chase_flags, char **ret_path, DIR **ret_dir) {
36,705✔
826
        _cleanup_close_ int path_fd = -EBADF;
36,705✔
827
        _cleanup_free_ char *p = NULL;
36,705✔
828
        DIR *d;
36,705✔
829
        int r;
36,705✔
830

831
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP|CHASE_MUST_BE_REGULAR)));
36,705✔
832
        assert(ret_dir);
36,705✔
833

834
        if (empty_or_root(root) && !ret_path && (chase_flags & CHASE_NO_SHORTCUT_MASK) == 0) {
36,705✔
835
                /* Shortcut this call if none of the special features of this call are requested */
UNCOV
836
                d = opendir(path);
×
UNCOV
837
                if (!d)
×
838
                        return -errno;
×
839

840
                *ret_dir = d;
×
UNCOV
841
                return 0;
×
842
        }
843

844
        r = chase(path, root, chase_flags|CHASE_MUST_BE_DIRECTORY, ret_path ? &p : NULL, &path_fd);
36,752✔
845
        if (r < 0)
36,705✔
846
                return r;
847
        assert(path_fd >= 0);
8,024✔
848

849
        d = xopendirat(path_fd, /* path= */ NULL, /* flags= */ 0);
8,024✔
850
        if (!d)
8,024✔
UNCOV
851
                return -errno;
×
852

853
        if (ret_path)
8,024✔
854
                *ret_path = TAKE_PTR(p);
7,977✔
855

856
        *ret_dir = d;
8,024✔
857
        return 0;
8,024✔
858
}
859

860
int chase_and_stat(const char *path, const char *root, ChaseFlags chase_flags, char **ret_path, struct stat *ret_stat) {
532,619✔
861
        _cleanup_close_ int path_fd = -EBADF;
532,619✔
862
        _cleanup_free_ char *p = NULL;
532,619✔
863
        int r;
532,619✔
864

865
        assert(path);
532,619✔
866
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP)));
532,619✔
867
        assert(ret_stat);
532,619✔
868

869
        if (empty_or_root(root) && !ret_path && (chase_flags & CHASE_NO_SHORTCUT_MASK) == 0)
532,619✔
870
                /* Shortcut this call if none of the special features of this call are requested */
871
                return RET_NERRNO(fstatat(AT_FDCWD, path, ret_stat,
305,699✔
872
                                          FLAGS_SET(chase_flags, CHASE_NOFOLLOW) ? AT_SYMLINK_NOFOLLOW : 0));
305,640✔
873

874
        r = chase(path, root, chase_flags, ret_path ? &p : NULL, &path_fd);
227,198✔
875
        if (r < 0)
226,979✔
876
                return r;
877
        assert(path_fd >= 0);
147,298✔
878

879
        if (fstat(path_fd, ret_stat) < 0)
147,298✔
UNCOV
880
                return -errno;
×
881

882
        if (ret_path)
147,298✔
883
                *ret_path = TAKE_PTR(p);
147,230✔
884

885
        return 0;
886
}
887

888
int chase_and_access(const char *path, const char *root, ChaseFlags chase_flags, int access_mode, char **ret_path) {
6,700✔
889
        _cleanup_close_ int path_fd = -EBADF;
6,700✔
890
        _cleanup_free_ char *p = NULL;
6,700✔
891
        int r;
6,700✔
892

893
        assert(path);
6,700✔
894
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP)));
6,700✔
895

896
        if (empty_or_root(root) && !ret_path && (chase_flags & CHASE_NO_SHORTCUT_MASK) == 0)
6,700✔
897
                /* Shortcut this call if none of the special features of this call are requested */
898
                return RET_NERRNO(faccessat(AT_FDCWD, path, access_mode,
13,067✔
899
                                            FLAGS_SET(chase_flags, CHASE_NOFOLLOW) ? AT_SYMLINK_NOFOLLOW : 0));
6,535✔
900

901
        r = chase(path, root, chase_flags, ret_path ? &p : NULL, &path_fd);
320✔
902
        if (r < 0)
165✔
903
                return r;
904
        assert(path_fd >= 0);
46✔
905

906
        r = access_fd(path_fd, access_mode);
46✔
907
        if (r < 0)
46✔
908
                return r;
909

910
        if (ret_path)
46✔
911
                *ret_path = TAKE_PTR(p);
10✔
912

913
        return 0;
914
}
915

916
int chase_and_fopen_unlocked(
39,502✔
917
                const char *path,
918
                const char *root,
919
                ChaseFlags chase_flags,
920
                const char *open_flags,
921
                char **ret_path,
922
                FILE **ret_file) {
923

924
        _cleanup_free_ char *final_path = NULL;
39,502✔
925
        _cleanup_close_ int fd = -EBADF;
39,502✔
926
        int mode_flags, r;
39,502✔
927

928
        assert(path);
39,502✔
929
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP|CHASE_PARENT|CHASE_MUST_BE_DIRECTORY)));
39,502✔
930
        assert(open_flags);
39,502✔
931
        assert(ret_file);
39,502✔
932

933
        mode_flags = fopen_mode_to_flags(open_flags);
39,502✔
934
        if (mode_flags < 0)
39,502✔
935
                return mode_flags;
936

937
        fd = chase_and_open(path, root, chase_flags, mode_flags, ret_path ? &final_path : NULL);
78,217✔
938
        if (fd < 0)
39,502✔
939
                return fd;
940

941
        r = take_fdopen_unlocked(&fd, open_flags, ret_file);
37,241✔
942
        if (r < 0)
37,241✔
943
                return r;
944

945
        if (ret_path)
37,241✔
946
                *ret_path = TAKE_PTR(final_path);
633✔
947

948
        return 0;
949
}
950

951
int chase_and_unlink(const char *path, const char *root, ChaseFlags chase_flags, int unlink_flags, char **ret_path) {
1✔
952
        _cleanup_free_ char *p = NULL, *fname = NULL;
1✔
953
        _cleanup_close_ int fd = -EBADF;
1✔
954
        int r;
1✔
955

956
        assert(path);
1✔
957
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP|CHASE_PARENT)));
1✔
958

959
        fd = chase_and_open(path, root, chase_flags|CHASE_PARENT|CHASE_NOFOLLOW, O_PATH|O_DIRECTORY|O_CLOEXEC, &p);
1✔
960
        if (fd < 0)
1✔
961
                return fd;
962

963
        r = path_extract_filename(p, &fname);
1✔
964
        if (r < 0)
1✔
965
                return r;
966

967
        if (unlinkat(fd, fname, unlink_flags) < 0)
1✔
UNCOV
968
                return -errno;
×
969

970
        if (ret_path)
1✔
971
                *ret_path = TAKE_PTR(p);
1✔
972

973
        return 0;
974
}
975

976
int chase_and_open_parent(const char *path, const char *root, ChaseFlags chase_flags, char **ret_filename) {
26,927✔
977
        int pfd, r;
26,927✔
978

979
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP)));
26,927✔
980

981
        r = chase(path, root, CHASE_PARENT|CHASE_EXTRACT_FILENAME|chase_flags, ret_filename, &pfd);
26,927✔
982
        if (r < 0)
26,927✔
983
                return r;
26,927✔
984

985
        return pfd;
26,804✔
986
}
987

988
int chase_and_openat(
996✔
989
                int dir_fd,
990
                const char *path,
991
                ChaseFlags chase_flags,
992
                int open_flags,
993
                char **ret_path) {
994

995
        _cleanup_close_ int path_fd = -EBADF;
996✔
996
        _cleanup_free_ char *p = NULL, *fname = NULL;
996✔
997
        int r;
996✔
998

999
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP)));
996✔
1000

1001
        XOpenFlags xopen_flags = 0;
996✔
1002
        if (FLAGS_SET(chase_flags, CHASE_MUST_BE_DIRECTORY))
996✔
UNCOV
1003
                open_flags |= O_DIRECTORY;
×
1004
        if (FLAGS_SET(chase_flags, CHASE_MUST_BE_REGULAR))
996✔
1005
                xopen_flags |= XO_REGULAR;
1✔
1006

1007
        if (dir_fd == AT_FDCWD && !ret_path && (chase_flags & CHASE_NO_SHORTCUT_MASK) == 0)
996✔
1008
                /* Shortcut this call if none of the special features of this call are requested */
UNCOV
1009
                return xopenat_full(dir_fd, path,
×
UNCOV
1010
                                    open_flags | (FLAGS_SET(chase_flags, CHASE_NOFOLLOW) ? O_NOFOLLOW : 0),
×
1011
                                    xopen_flags,
1012
                                    MODE_INVALID);
1013

1014
        r = chaseat(dir_fd, path, (chase_flags|CHASE_PARENT)&~CHASE_MUST_BE_REGULAR, &p, &path_fd);
996✔
1015
        if (r < 0)
996✔
1016
                return r;
1017

1018
        if (!FLAGS_SET(chase_flags, CHASE_PARENT)) {
992✔
1019
                r = path_extract_filename(p, &fname);
185✔
1020
                if (r < 0 && r != -EADDRNOTAVAIL)
185✔
1021
                        return r;
1022
        }
1023

1024
        r = xopenat_full(
2,976✔
1025
                        path_fd,
1026
                        strempty(fname),
1,799✔
1027
                        open_flags|O_NOFOLLOW,
1028
                        xopen_flags,
1029
                        MODE_INVALID);
1030
        if (r < 0)
992✔
1031
                return r;
1032

1033
        if (ret_path)
992✔
1034
                *ret_path = TAKE_PTR(p);
808✔
1035

1036
        return r;
1037
}
1038

1039
int chase_and_opendirat(int dir_fd, const char *path, ChaseFlags chase_flags, char **ret_path, DIR **ret_dir) {
599,976✔
1040
        _cleanup_close_ int path_fd = -EBADF;
599,976✔
1041
        _cleanup_free_ char *p = NULL;
599,976✔
1042
        DIR *d;
599,976✔
1043
        int r;
599,976✔
1044

1045
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP|CHASE_MUST_BE_REGULAR)));
599,976✔
1046
        assert(ret_dir);
599,976✔
1047

1048
        if (dir_fd == AT_FDCWD && !ret_path && (chase_flags & CHASE_NO_SHORTCUT_MASK) == 0) {
599,976✔
1049
                /* Shortcut this call if none of the special features of this call are requested */
UNCOV
1050
                d = opendir(path);
×
UNCOV
1051
                if (!d)
×
1052
                        return -errno;
×
1053

1054
                *ret_dir = d;
×
UNCOV
1055
                return 0;
×
1056
        }
1057

1058
        r = chaseat(dir_fd, path, chase_flags, ret_path ? &p : NULL, &path_fd);
599,976✔
1059
        if (r < 0)
599,976✔
1060
                return r;
1061
        assert(path_fd >= 0);
42,331✔
1062

1063
        d = xopendirat(path_fd, /* path= */ NULL, /* flags= */ 0);
42,331✔
1064
        if (!d)
42,331✔
UNCOV
1065
                return -errno;
×
1066

1067
        if (ret_path)
42,331✔
1068
                *ret_path = TAKE_PTR(p);
42,331✔
1069

1070
        *ret_dir = d;
42,331✔
1071
        return 0;
42,331✔
1072
}
1073

1074
int chase_and_statat(int dir_fd, const char *path, ChaseFlags chase_flags, char **ret_path, struct stat *ret_stat) {
1✔
1075
        _cleanup_close_ int path_fd = -EBADF;
1✔
1076
        _cleanup_free_ char *p = NULL;
1✔
1077
        int r;
1✔
1078

1079
        assert(path);
1✔
1080
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP)));
1✔
1081
        assert(ret_stat);
1✔
1082

1083
        if (dir_fd == AT_FDCWD && !ret_path && (chase_flags & CHASE_NO_SHORTCUT_MASK) == 0)
1✔
1084
                /* Shortcut this call if none of the special features of this call are requested */
UNCOV
1085
                return RET_NERRNO(fstatat(AT_FDCWD, path, ret_stat,
×
UNCOV
1086
                                          FLAGS_SET(chase_flags, CHASE_NOFOLLOW) ? AT_SYMLINK_NOFOLLOW : 0));
×
1087

1088
        r = chaseat(dir_fd, path, chase_flags, ret_path ? &p : NULL, &path_fd);
1✔
1089
        if (r < 0)
1✔
1090
                return r;
1091
        assert(path_fd >= 0);
1✔
1092

1093
        if (fstat(path_fd, ret_stat) < 0)
1✔
UNCOV
1094
                return -errno;
×
1095

1096
        if (ret_path)
1✔
1097
                *ret_path = TAKE_PTR(p);
1✔
1098

1099
        return 0;
1100
}
1101

1102
int chase_and_accessat(int dir_fd, const char *path, ChaseFlags chase_flags, int access_mode, char **ret_path) {
1✔
1103
        _cleanup_close_ int path_fd = -EBADF;
1✔
1104
        _cleanup_free_ char *p = NULL;
1✔
1105
        int r;
1✔
1106

1107
        assert(path);
1✔
1108
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP)));
1✔
1109

1110
        if (dir_fd == AT_FDCWD && !ret_path && (chase_flags & CHASE_NO_SHORTCUT_MASK) == 0)
1✔
1111
                /* Shortcut this call if none of the special features of this call are requested */
UNCOV
1112
                return RET_NERRNO(faccessat(AT_FDCWD, path, access_mode,
×
UNCOV
1113
                                            FLAGS_SET(chase_flags, CHASE_NOFOLLOW) ? AT_SYMLINK_NOFOLLOW : 0));
×
1114

1115
        r = chaseat(dir_fd, path, chase_flags, ret_path ? &p : NULL, &path_fd);
1✔
1116
        if (r < 0)
1✔
1117
                return r;
1118
        assert(path_fd >= 0);
1✔
1119

1120
        r = access_fd(path_fd, access_mode);
1✔
1121
        if (r < 0)
1✔
1122
                return r;
1123

1124
        if (ret_path)
1✔
1125
                *ret_path = TAKE_PTR(p);
1✔
1126

1127
        return 0;
1128
}
1129

1130
int chase_and_fopenat_unlocked(
186✔
1131
                int dir_fd,
1132
                const char *path,
1133
                ChaseFlags chase_flags,
1134
                const char *open_flags,
1135
                char **ret_path,
1136
                FILE **ret_file) {
1137

1138
        _cleanup_free_ char *final_path = NULL;
186✔
1139
        _cleanup_close_ int fd = -EBADF;
186✔
1140
        int mode_flags, r;
186✔
1141

1142
        assert(path);
186✔
1143
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP|CHASE_PARENT)));
186✔
1144
        assert(open_flags);
186✔
1145
        assert(ret_file);
186✔
1146

1147
        mode_flags = fopen_mode_to_flags(open_flags);
186✔
1148
        if (mode_flags < 0)
186✔
1149
                return mode_flags;
1150

1151
        fd = chase_and_openat(dir_fd, path, chase_flags, mode_flags, ret_path ? &final_path : NULL);
371✔
1152
        if (fd < 0)
186✔
1153
                return fd;
1154

1155
        r = take_fdopen_unlocked(&fd, open_flags, ret_file);
182✔
1156
        if (r < 0)
182✔
1157
                return r;
1158

1159
        if (ret_path)
182✔
1160
                *ret_path = TAKE_PTR(final_path);
1✔
1161

1162
        return 0;
1163
}
1164

1165
int chase_and_unlinkat(int dir_fd, const char *path, ChaseFlags chase_flags, int unlink_flags, char **ret_path) {
1✔
1166
        _cleanup_free_ char *p = NULL, *fname = NULL;
1✔
1167
        _cleanup_close_ int fd = -EBADF;
1✔
1168
        int r;
1✔
1169

1170
        assert(path);
1✔
1171
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP|CHASE_PARENT)));
1✔
1172

1173
        fd = chase_and_openat(dir_fd, path, chase_flags|CHASE_PARENT|CHASE_NOFOLLOW, O_PATH|O_DIRECTORY|O_CLOEXEC, &p);
1✔
1174
        if (fd < 0)
1✔
1175
                return fd;
1176

1177
        r = path_extract_filename(p, &fname);
1✔
1178
        if (r < 0)
1✔
1179
                return r;
1180

1181
        if (unlinkat(fd, fname, unlink_flags) < 0)
1✔
UNCOV
1182
                return -errno;
×
1183

1184
        if (ret_path)
1✔
1185
                *ret_path = TAKE_PTR(p);
1✔
1186

1187
        return 0;
1188
}
1189

1190
int chase_and_open_parent_at(int dir_fd, const char *path, ChaseFlags chase_flags, char **ret_filename) {
983✔
1191
        int pfd, r;
983✔
1192

1193
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP)));
983✔
1194

1195
        r = chaseat(dir_fd, path, CHASE_PARENT|CHASE_EXTRACT_FILENAME|chase_flags, ret_filename, &pfd);
983✔
1196
        if (r < 0)
983✔
1197
                return r;
983✔
1198

1199
        return pfd;
983✔
1200
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc