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

systemd / systemd / 17253546277

26 Aug 2025 06:24PM UTC coverage: 72.232% (-0.005%) from 72.237%
17253546277

push

github

bluca
shell-completion: support -i option for journalctl

Follow-up for dde54b8a8.

302507 of 418798 relevant lines covered (72.23%)

653328.97 hits per line

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

89.55
/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) {
34,595✔
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 */
34,595✔
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) {
6✔
76
        _cleanup_free_ char *n1 = NULL;
6✔
77

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

80
        if (!FLAGS_SET(flags, CHASE_WARN))
6✔
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 not 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) {
15,989,008✔
91
        static bool can_open_tree = true;
15,989,008✔
92
        int r;
15,989,008✔
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);
15,989,008✔
105
        assert(path);
15,989,008✔
106

107
        if (automount && can_open_tree) {
15,989,008✔
108
                r = RET_NERRNO(open_tree(dir_fd, path, AT_SYMLINK_NOFOLLOW|OPEN_TREE_CLOEXEC));
80,320✔
109
                if (r >= 0 || (r != -EPERM && !ERRNO_IS_NEG_NOT_SUPPORTED(r)))
2,910✔
110
                        return r;
80,320✔
111

112
                can_open_tree = false;
×
113
        }
114

115
        return RET_NERRNO(openat(dir_fd, path, O_PATH|O_NOFOLLOW|O_CLOEXEC));
15,908,688✔
116
}
117

118
static int chaseat_needs_absolute(int dir_fd, const char *path) {
2,871,690✔
119
        if (dir_fd < 0)
2,871,690✔
120
                return path_is_absolute(path);
15,338✔
121

122
        return dir_fd_is_root(dir_fd);
2,864,021✔
123
}
124

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

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

140
        if (FLAGS_SET(flags, CHASE_STEP))
2,872,570✔
141
                assert(!ret_fd);
24,310✔
142

143
        if (isempty(path))
2,873,676✔
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)) {
2,872,570✔
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);
781,045✔
226
                if (r < 0)
773,445✔
227
                        return r;
228
                if (r > 0)
781,045✔
229
                        flags &= ~CHASE_AT_RESOLVE_IN_ROOT;
699,837✔
230
        }
231

232
        if (!ret_path && ret_fd && (flags & (CHASE_AT_RESOLVE_IN_ROOT|CHASE_NO_SHORTCUT_MASK)) == 0) {
2,872,570✔
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));
880✔
236
                if (r < 0)
880✔
237
                        return -errno;
19✔
238

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

243
        buffer = strdup(path);
2,871,690✔
244
        if (!buffer)
2,871,690✔
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);
2,871,690✔
251
        if (r < 0)
2,871,690✔
252
                return r;
253

254
        bool need_absolute = r;
2,871,690✔
255
        if (need_absolute) {
2,871,690✔
256
                done = strdup("/");
2,789,577✔
257
                if (!done)
2,789,577✔
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);
2,953,803✔
266
        if (fd < 0)
2,871,690✔
267
                return -errno;
×
268

269
        if (fstat(fd, &st) < 0)
2,871,690✔
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))
2,871,690✔
276
                root_fd = openat(dir_fd, ".", O_CLOEXEC|O_DIRECTORY|O_PATH);
81,208✔
277
        else
278
                root_fd = open("/", O_CLOEXEC|O_DIRECTORY|O_PATH);
2,790,482✔
279
        if (root_fd < 0)
2,871,690✔
280
                return -errno;
×
281

282
        if (ENDSWITH_SET(buffer, "/", "/.")) {
2,871,690✔
283
                flags |= CHASE_MUST_BE_DIRECTORY;
6,103✔
284
                if (FLAGS_SET(flags, CHASE_TRAIL_SLASH))
6,103✔
285
                        append_trail_slash = true;
10✔
286
        } else if (dot_or_dot_dot(buffer) || endswith(buffer, "/.."))
2,865,587✔
287
                flags |= CHASE_MUST_BE_DIRECTORY;
1,115✔
288

289
        if (FLAGS_SET(flags, CHASE_PARENT))
2,871,690✔
290
                flags |= CHASE_MUST_BE_DIRECTORY;
36,651✔
291

292
        for (todo = buffer;;) {
2,871,690✔
293
                _cleanup_free_ char *first = NULL;
16,563,916✔
294
                _cleanup_close_ int child = -EBADF;
18,323,960✔
295
                struct stat st_child;
18,323,960✔
296
                const char *e;
18,323,960✔
297

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

304
                first = strndup(e, r);
16,613,789✔
305
                if (!first)
16,613,789✔
306
                        return -ENOMEM;
307

308
                /* Two dots? Then chop off the last bit of what we already found out. */
309
                if (streq(first, "..")) {
16,613,789✔
310
                        _cleanup_free_ char *parent = NULL;
624,781✔
311
                        _cleanup_close_ int fd_parent = -EBADF;
624,781✔
312
                        struct stat st_parent;
624,781✔
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)) {
624,781✔
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);
624,660✔
323
                        if (fd_parent < 0)
624,660✔
324
                                return -errno;
×
325

326
                        if (fstat(fd_parent, &st_parent) < 0)
624,660✔
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)) {
624,660✔
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);
624,618✔
344
                        if (r >= 0) {
624,618✔
345
                                assert(!need_absolute || path_is_absolute(parent));
623,383✔
346
                                free_and_replace(done, parent);
623,383✔
347
                        } else if (r == -EDESTADDRREQ) {
1,235✔
348
                                /* 'done' contains filename only (i.e. no slash). */
349
                                assert(!need_absolute);
1,235✔
350
                                done = mfree(done);
1,235✔
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))
624,618✔
370
                                goto chased_one;
6✔
371

372
                        if (FLAGS_SET(flags, CHASE_SAFE) &&
624,612✔
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)) {
624,612✔
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;
624,610✔
401
                        close_and_replace(fd, fd_parent);
624,610✔
402
                        continue;
624,610✔
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));
15,989,008✔
407
                if (r < 0) {
15,989,008✔
408
                        if (r != -ENOENT)
1,131,316✔
409
                                return r;
410

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

414
                        if (FLAGS_SET(flags, CHASE_MKDIR_0755) && (!isempty(todo) || !(flags & (CHASE_PARENT|CHASE_NONEXISTENT)))) {
1,131,969✔
415
                                child = xopenat_full(fd,
196✔
416
                                                     first,
417
                                                     O_DIRECTORY|O_CREAT|O_EXCL|O_NOFOLLOW|O_PATH|O_CLOEXEC,
418
                                                     /* xopen_flags = */ 0,
419
                                                     0755);
420
                                if (child < 0)
196✔
421
                                        return child;
422
                        } else if (FLAGS_SET(flags, CHASE_PARENT) && isempty(todo)) {
1,131,116✔
423
                                if (!path_extend(&done, first))
5,568✔
424
                                        return -ENOMEM;
425

426
                                break;
427
                        } else if (FLAGS_SET(flags, CHASE_NONEXISTENT)) {
1,125,548✔
428
                                if (!path_extend(&done, first, todo))
13,923✔
429
                                        return -ENOMEM;
430

431
                                exists = false;
432
                                break;
433
                        } else
434
                                return r;
435
                }
436

437
                /* ... and then check what it actually is. */
438
                if (fstat(child, &st_child) < 0)
14,857,888✔
439
                        return -errno;
×
440

441
                if (FLAGS_SET(flags, CHASE_SAFE) &&
14,892,478✔
442
                    unsafe_transition(&st, &st_child))
34,590✔
443
                        return log_unsafe_transition(fd, child, path, flags);
5✔
444

445
                if (FLAGS_SET(flags, CHASE_NO_AUTOFS) &&
14,857,883✔
446
                    fd_is_fs_type(child, AUTOFS_SUPER_MAGIC) > 0)
6,362,857✔
447
                        return log_autofs_mount_point(child, path, flags);
×
448

449
                if (S_ISLNK(st_child.st_mode) && !(FLAGS_SET(flags, CHASE_NOFOLLOW) && isempty(todo))) {
14,857,883✔
450
                        _cleanup_free_ char *destination = NULL;
12✔
451

452
                        if (FLAGS_SET(flags, CHASE_PROHIBIT_SYMLINKS))
296,438✔
453
                                return log_prohibited_symlink(child, flags);
6✔
454

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

460
                        r = readlinkat_malloc(fd, first, &destination);
296,431✔
461
                        if (r < 0)
296,431✔
462
                                return r;
463
                        if (isempty(destination))
296,441✔
464
                                return -EINVAL;
465

466
                        if (path_is_absolute(destination)) {
296,429✔
467

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

471
                                safe_close(fd);
6,853✔
472
                                fd = fd_reopen(root_fd, O_CLOEXEC|O_PATH|O_DIRECTORY);
6,853✔
473
                                if (fd < 0)
6,853✔
474
                                        return fd;
475

476
                                if (fstat(fd, &st) < 0)
6,853✔
477
                                        return -errno;
×
478

479
                                if (FLAGS_SET(flags, CHASE_SAFE) &&
6,858✔
480
                                    unsafe_transition(&st_child, &st))
5✔
481
                                        return log_unsafe_transition(child, fd, path, flags);
3✔
482

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

488
                                r = free_and_strdup(&done, need_absolute ? "/" : NULL);
7,149✔
489
                                if (r < 0)
6,850✔
490
                                        return r;
491
                        }
492

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

498
                        free_and_replace(buffer, destination);
296,426✔
499
                        todo = buffer;
296,426✔
500

501
                        if (FLAGS_SET(flags, CHASE_STEP))
296,426✔
502
                                goto chased_one;
8✔
503

504
                        continue;
296,418✔
505
                }
506

507
                /* If this is not a symlink, then let's just add the name we read to what we already verified. */
508
                if (!path_extend(&done, first))
14,561,445✔
509
                        return -ENOMEM;
510

511
                if (FLAGS_SET(flags, CHASE_PARENT) && isempty(todo))
16,321,475✔
512
                        break;
513

514
                /* And iterate again, but go one directory further down. */
515
                st = st_child;
14,531,079✔
516
                close_and_replace(fd, child);
14,531,079✔
517
        }
518

519
        if (exists) {
1,760,030✔
520
                if (FLAGS_SET(flags, CHASE_MUST_BE_DIRECTORY)) {
1,746,107✔
521
                        r = stat_verify_directory(&st);
43,471✔
522
                        if (r < 0)
43,471✔
523
                                return r;
524
                }
525

526
                if (FLAGS_SET(flags, CHASE_MUST_BE_REGULAR)) {
1,746,107✔
527
                        r = stat_verify_regular(&st);
623✔
528
                        if (r < 0)
623✔
529
                                return r;
530
                }
531
        }
532

533
        if (ret_path) {
1,760,023✔
534
                if (FLAGS_SET(flags, CHASE_EXTRACT_FILENAME) && done) {
1,691,844✔
535
                        _cleanup_free_ char *f = NULL;
×
536

537
                        r = path_extract_filename(done, &f);
32,938✔
538
                        if (r < 0 && r != -EADDRNOTAVAIL)
32,938✔
539
                                return r;
×
540

541
                        /* If we get EADDRNOTAVAIL we clear done and it will get reinitialized by the next block. */
542
                        free_and_replace(done, f);
32,938✔
543
                }
544

545
                if (!done) {
1,691,844✔
546
                        assert(!need_absolute || FLAGS_SET(flags, CHASE_EXTRACT_FILENAME));
248✔
547
                        done = strdup(".");
248✔
548
                        if (!done)
248✔
549
                                return -ENOMEM;
550
                }
551

552
                if (append_trail_slash)
1,691,844✔
553
                        if (!strextend(&done, "/"))
10✔
554
                                return -ENOMEM;
555

556
                *ret_path = TAKE_PTR(done);
1,691,844✔
557
        }
558

559
        if (ret_fd) {
1,760,023✔
560
                if (exists) {
1,605,658✔
561
                        /* Return the O_PATH fd we currently are looking to the caller. It can translate it
562
                         * to a proper fd by opening /proc/self/fd/xyz. */
563
                        assert(fd >= 0);
1,605,522✔
564
                        *ret_fd = TAKE_FD(fd);
1,605,522✔
565
                } else
566
                        *ret_fd = -EBADF;
136✔
567
        }
568

569
        if (FLAGS_SET(flags, CHASE_STEP))
1,760,023✔
570
                return 1;
571

572
        return exists;
1,735,727✔
573

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

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

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

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

598
                        *ret_path = c;
14✔
599
                }
600
        }
601

602
        return 0;
603
}
604

605
int chase(const char *path, const char *root, ChaseFlags flags, char **ret_path, int *ret_fd) {
2,166,086✔
606
        _cleanup_free_ char *root_abs = NULL, *absolute = NULL, *p = NULL;
2,166,086✔
607
        _cleanup_close_ int fd = -EBADF, pfd = -EBADF;
4,332,172✔
608
        int r;
2,166,086✔
609

610
        assert(path);
2,166,086✔
611

612
        if (isempty(path))
4,332,171✔
613
                return -EINVAL;
614

615
        r = empty_or_root_harder_to_null(&root);
2,166,085✔
616
        if (r < 0)
2,166,085✔
617
                return r;
618

619
        /* A root directory of "/" or "" is identical to "/". */
620
        if (empty_or_root(root)) {
2,166,082✔
621
                root = "/";
2,090,714✔
622

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

627
        } else {
628
                r = path_make_absolute_cwd(root, &root_abs);
75,368✔
629
                if (r < 0)
75,368✔
630
                        return r;
631

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

636
                assert(path_is_absolute(root));
75,368✔
637
                assert(!empty_or_root(root));
75,368✔
638

639
                if (FLAGS_SET(flags, CHASE_PREFIX_ROOT)) {
75,368✔
640
                        absolute = path_join(root, path);
13,327✔
641
                        if (!absolute)
13,327✔
642
                                return -ENOMEM;
643
                }
644

645
                flags |= CHASE_AT_RESOLVE_IN_ROOT;
75,368✔
646
        }
647

648
        if (!absolute) {
2,166,082✔
649
                r = path_make_absolute_cwd(path, &absolute);
2,152,755✔
650
                if (r < 0)
2,152,755✔
651
                        return r;
652
        }
653

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

661
        fd = open(root, O_CLOEXEC|O_DIRECTORY|O_PATH);
2,166,082✔
662
        if (fd < 0)
2,166,082✔
663
                return -errno;
1✔
664

665
        r = chaseat(fd, path, flags & ~CHASE_PREFIX_ROOT, ret_path ? &p : NULL, ret_fd ? &pfd : NULL);
2,543,660✔
666
        if (r < 0)
2,166,081✔
667
                return r;
668

669
        if (ret_path) {
1,590,966✔
670
                if (!FLAGS_SET(flags, CHASE_EXTRACT_FILENAME)) {
1,530,153✔
671

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

679
                        if (empty_or_root(root))
1,498,799✔
680
                                assert(path_is_absolute(p));
1,463,311✔
681
                        else {
682
                                char *q;
35,488✔
683

684
                                assert(!path_is_absolute(p));
35,488✔
685

686
                                q = path_join(root, p + STR_IN_SET(p, ".", "./"));
35,488✔
687
                                if (!q)
35,488✔
688
                                        return -ENOMEM;
×
689

690
                                free_and_replace(p, q);
35,488✔
691
                        }
692
                }
693

694
                *ret_path = TAKE_PTR(p);
1,530,153✔
695
        }
696

697
        if (ret_fd)
1,590,966✔
698
                *ret_fd = TAKE_FD(pfd);
1,445,177✔
699

700
        return r;
701
}
702

703
int chaseat_prefix_root(const char *path, const char *root, char **ret) {
117,930✔
704
        char *q;
117,930✔
705
        int r;
117,930✔
706

707
        assert(path);
117,930✔
708
        assert(ret);
117,930✔
709

710
        /* This is mostly for prefixing the result of chaseat(). */
711

712
        if (!path_is_absolute(path)) {
117,930✔
713
                _cleanup_free_ char *root_abs = NULL;
2,953✔
714

715
                r = empty_or_root_harder_to_null(&root);
2,953✔
716
                if (r < 0 && r != -ENOENT)
2,953✔
717
                        return r;
718

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

723
                r = path_make_absolute_cwd(root, &root_abs);
2,953✔
724
                if (r < 0)
2,953✔
725
                        return r;
726

727
                root = path_simplify(root_abs);
2,953✔
728

729
                q = path_join(root, path + (path[0] == '.' && IN_SET(path[1], '/', '\0')));
2,953✔
730
        } else
731
                q = strdup(path);
114,977✔
732
        if (!q)
117,930✔
733
                return -ENOMEM;
734

735
        *ret = q;
117,930✔
736
        return 0;
117,930✔
737
}
738

739
int chase_extract_filename(const char *path, const char *root, char **ret) {
2,187✔
740
        int r;
2,187✔
741

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

745
        assert(path);
2,187✔
746
        assert(ret);
2,187✔
747

748
        if (isempty(path))
2,187✔
749
                return -EINVAL;
750

751
        if (!path_is_absolute(path))
4,374✔
752
                return -EINVAL;
753

754
        r = empty_or_root_harder_to_null(&root);
2,187✔
755
        if (r < 0 && r != -ENOENT)
2,187✔
756
                return r;
757

758
        if (!empty_or_root(root)) {
2,187✔
759
                _cleanup_free_ char *root_abs = NULL;
1,139✔
760

761
                r = path_make_absolute_cwd(root, &root_abs);
1,139✔
762
                if (r < 0)
1,139✔
763
                        return r;
764

765
                path = path_startswith(path, root_abs);
1,139✔
766
                if (!path)
1,139✔
767
                        return -EINVAL;
768
        }
769

770
        if (!isempty(path)) {
2,187✔
771
                r = path_extract_filename(path, ret);
2,047✔
772
                if (r != -EADDRNOTAVAIL)
2,047✔
773
                        return r;
774
        }
775

776
        return strdup_to(ret, ".");
147✔
777
}
778

779
int chase_and_open(
40,806✔
780
                const char *path,
781
                const char *root,
782
                ChaseFlags chase_flags,
783
                int open_flags,
784
                char **ret_path) {
785

786
        _cleanup_close_ int path_fd = -EBADF;
40,806✔
787
        _cleanup_free_ char *p = NULL, *fname = NULL;
40,806✔
788
        int r;
40,806✔
789

790
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP)));
40,806✔
791

792
        if (empty_or_root(root) && !ret_path && (chase_flags & CHASE_NO_SHORTCUT_MASK) == 0)
40,806✔
793
                /* Shortcut this call if none of the special features of this call are requested */
794
                return xopenat_full(AT_FDCWD, path,
38,223✔
795
                                    open_flags | (FLAGS_SET(chase_flags, CHASE_NOFOLLOW) ? O_NOFOLLOW : 0),
38,223✔
796
                                    /* xopen_flags = */ 0,
797
                                    MODE_INVALID);
798

799
        r = chase(path, root, CHASE_PARENT|chase_flags, &p, &path_fd);
2,583✔
800
        if (r < 0)
2,583✔
801
                return r;
802
        assert(path_fd >= 0);
2,182✔
803

804
        if (!FLAGS_SET(chase_flags, CHASE_PARENT) &&
2,182✔
805
            !FLAGS_SET(chase_flags, CHASE_EXTRACT_FILENAME)) {
2,181✔
806
                r = chase_extract_filename(p, root, &fname);
2,181✔
807
                if (r < 0)
2,181✔
808
                        return r;
809
        }
810

811
        r = xopenat_full(path_fd, strempty(fname), open_flags|O_NOFOLLOW, /* xopen_flags = */ 0, MODE_INVALID);
2,183✔
812
        if (r < 0)
2,182✔
813
                return r;
814

815
        if (ret_path)
1,814✔
816
                *ret_path = TAKE_PTR(p);
754✔
817

818
        return r;
819
}
820

821
int chase_and_opendir(const char *path, const char *root, ChaseFlags chase_flags, char **ret_path, DIR **ret_dir) {
36,677✔
822
        _cleanup_close_ int path_fd = -EBADF;
36,677✔
823
        _cleanup_free_ char *p = NULL;
36,677✔
824
        DIR *d;
36,677✔
825
        int r;
36,677✔
826

827
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP)));
36,677✔
828
        assert(ret_dir);
36,677✔
829

830
        if (empty_or_root(root) && !ret_path && (chase_flags & CHASE_NO_SHORTCUT_MASK) == 0) {
36,677✔
831
                /* Shortcut this call if none of the special features of this call are requested */
832
                d = opendir(path);
×
833
                if (!d)
×
834
                        return -errno;
×
835

836
                *ret_dir = d;
×
837
                return 0;
×
838
        }
839

840
        r = chase(path, root, chase_flags, ret_path ? &p : NULL, &path_fd);
36,724✔
841
        if (r < 0)
36,677✔
842
                return r;
843
        assert(path_fd >= 0);
8,022✔
844

845
        d = xopendirat(path_fd, ".", O_NOFOLLOW);
8,022✔
846
        if (!d)
8,022✔
847
                return -errno;
×
848

849
        if (ret_path)
8,022✔
850
                *ret_path = TAKE_PTR(p);
7,975✔
851

852
        *ret_dir = d;
8,022✔
853
        return 0;
8,022✔
854
}
855

856
int chase_and_stat(const char *path, const char *root, ChaseFlags chase_flags, char **ret_path, struct stat *ret_stat) {
488,520✔
857
        _cleanup_close_ int path_fd = -EBADF;
488,520✔
858
        _cleanup_free_ char *p = NULL;
488,520✔
859
        int r;
488,520✔
860

861
        assert(path);
488,520✔
862
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP)));
488,520✔
863
        assert(ret_stat);
488,520✔
864

865
        if (empty_or_root(root) && !ret_path && (chase_flags & CHASE_NO_SHORTCUT_MASK) == 0)
488,520✔
866
                /* Shortcut this call if none of the special features of this call are requested */
867
                return RET_NERRNO(fstatat(AT_FDCWD, path, ret_stat,
296,990✔
868
                                          FLAGS_SET(chase_flags, CHASE_NOFOLLOW) ? AT_SYMLINK_NOFOLLOW : 0));
296,932✔
869

870
        r = chase(path, root, chase_flags, ret_path ? &p : NULL, &path_fd);
191,807✔
871
        if (r < 0)
191,588✔
872
                return r;
873
        assert(path_fd >= 0);
95,392✔
874

875
        if (fstat(path_fd, ret_stat) < 0)
95,392✔
876
                return -errno;
×
877

878
        if (ret_path)
95,392✔
879
                *ret_path = TAKE_PTR(p);
95,324✔
880

881
        return 0;
882
}
883

884
int chase_and_access(const char *path, const char *root, ChaseFlags chase_flags, int access_mode, char **ret_path) {
285✔
885
        _cleanup_close_ int path_fd = -EBADF;
285✔
886
        _cleanup_free_ char *p = NULL;
285✔
887
        int r;
285✔
888

889
        assert(path);
285✔
890
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP)));
285✔
891

892
        if (empty_or_root(root) && !ret_path && (chase_flags & CHASE_NO_SHORTCUT_MASK) == 0)
285✔
893
                /* Shortcut this call if none of the special features of this call are requested */
894
                return RET_NERRNO(faccessat(AT_FDCWD, path, access_mode,
468✔
895
                                            FLAGS_SET(chase_flags, CHASE_NOFOLLOW) ? AT_SYMLINK_NOFOLLOW : 0));
234✔
896

897
        r = chase(path, root, chase_flags, ret_path ? &p : NULL, &path_fd);
92✔
898
        if (r < 0)
51✔
899
                return r;
900
        assert(path_fd >= 0);
43✔
901

902
        r = access_fd(path_fd, access_mode);
43✔
903
        if (r < 0)
43✔
904
                return r;
905

906
        if (ret_path)
43✔
907
                *ret_path = TAKE_PTR(p);
10✔
908

909
        return 0;
910
}
911

912
int chase_and_fopen_unlocked(
36,846✔
913
                const char *path,
914
                const char *root,
915
                ChaseFlags chase_flags,
916
                const char *open_flags,
917
                char **ret_path,
918
                FILE **ret_file) {
919

920
        _cleanup_free_ char *final_path = NULL;
36,846✔
921
        _cleanup_close_ int fd = -EBADF;
36,846✔
922
        int mode_flags, r;
36,846✔
923

924
        assert(path);
36,846✔
925
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP|CHASE_PARENT)));
36,846✔
926
        assert(open_flags);
36,846✔
927
        assert(ret_file);
36,846✔
928

929
        mode_flags = fopen_mode_to_flags(open_flags);
36,846✔
930
        if (mode_flags < 0)
36,846✔
931
                return mode_flags;
932

933
        fd = chase_and_open(path, root, chase_flags, mode_flags, ret_path ? &final_path : NULL);
72,907✔
934
        if (fd < 0)
36,846✔
935
                return fd;
936

937
        r = take_fdopen_unlocked(&fd, open_flags, ret_file);
36,011✔
938
        if (r < 0)
36,011✔
939
                return r;
940

941
        if (ret_path)
36,011✔
942
                *ret_path = TAKE_PTR(final_path);
631✔
943

944
        return 0;
945
}
946

947
int chase_and_unlink(const char *path, const char *root, ChaseFlags chase_flags, int unlink_flags, char **ret_path) {
1✔
948
        _cleanup_free_ char *p = NULL, *fname = NULL;
1✔
949
        _cleanup_close_ int fd = -EBADF;
1✔
950
        int r;
1✔
951

952
        assert(path);
1✔
953
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP|CHASE_PARENT)));
1✔
954

955
        fd = chase_and_open(path, root, chase_flags|CHASE_PARENT|CHASE_NOFOLLOW, O_PATH|O_DIRECTORY|O_CLOEXEC, &p);
1✔
956
        if (fd < 0)
1✔
957
                return fd;
958

959
        r = path_extract_filename(p, &fname);
1✔
960
        if (r < 0)
1✔
961
                return r;
962

963
        if (unlinkat(fd, fname, unlink_flags) < 0)
1✔
964
                return -errno;
×
965

966
        if (ret_path)
1✔
967
                *ret_path = TAKE_PTR(p);
1✔
968

969
        return 0;
970
}
971

972
int chase_and_open_parent(const char *path, const char *root, ChaseFlags chase_flags, char **ret_filename) {
26,181✔
973
        int pfd, r;
26,181✔
974

975
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP)));
26,181✔
976

977
        r = chase(path, root, CHASE_PARENT|CHASE_EXTRACT_FILENAME|chase_flags, ret_filename, &pfd);
26,181✔
978
        if (r < 0)
26,181✔
979
                return r;
26,181✔
980

981
        return pfd;
26,067✔
982
}
983

984
int chase_and_openat(
977✔
985
                int dir_fd,
986
                const char *path,
987
                ChaseFlags chase_flags,
988
                int open_flags,
989
                char **ret_path) {
990

991
        _cleanup_close_ int path_fd = -EBADF;
977✔
992
        _cleanup_free_ char *p = NULL, *fname = NULL;
977✔
993
        int r;
977✔
994

995
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP)));
977✔
996

997
        if (dir_fd == AT_FDCWD && !ret_path && (chase_flags & CHASE_NO_SHORTCUT_MASK) == 0)
977✔
998
                /* Shortcut this call if none of the special features of this call are requested */
999
                return xopenat_full(dir_fd, path,
×
1000
                                    open_flags | (FLAGS_SET(chase_flags, CHASE_NOFOLLOW) ? O_NOFOLLOW : 0),
×
1001
                                    /* xopen_flags = */ 0,
1002
                                    MODE_INVALID);
1003

1004
        r = chaseat(dir_fd, path, chase_flags|CHASE_PARENT, &p, &path_fd);
977✔
1005
        if (r < 0)
977✔
1006
                return r;
1007

1008
        if (!FLAGS_SET(chase_flags, CHASE_PARENT)) {
973✔
1009
                r = path_extract_filename(p, &fname);
178✔
1010
                if (r < 0 && r != -EADDRNOTAVAIL)
178✔
1011
                        return r;
1012
        }
1013

1014
        r = xopenat_full(path_fd, strempty(fname), open_flags|O_NOFOLLOW, /* xopen_flags= */ 0, MODE_INVALID);
1,768✔
1015
        if (r < 0)
973✔
1016
                return r;
1017

1018
        if (ret_path)
973✔
1019
                *ret_path = TAKE_PTR(p);
796✔
1020

1021
        return r;
1022
}
1023

1024
int chase_and_opendirat(int dir_fd, const char *path, ChaseFlags chase_flags, char **ret_path, DIR **ret_dir) {
577,596✔
1025
        _cleanup_close_ int path_fd = -EBADF;
577,596✔
1026
        _cleanup_free_ char *p = NULL;
577,596✔
1027
        DIR *d;
577,596✔
1028
        int r;
577,596✔
1029

1030
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP)));
577,596✔
1031
        assert(ret_dir);
577,596✔
1032

1033
        if (dir_fd == AT_FDCWD && !ret_path && (chase_flags & CHASE_NO_SHORTCUT_MASK) == 0) {
577,596✔
1034
                /* Shortcut this call if none of the special features of this call are requested */
1035
                d = opendir(path);
×
1036
                if (!d)
×
1037
                        return -errno;
×
1038

1039
                *ret_dir = d;
×
1040
                return 0;
×
1041
        }
1042

1043
        r = chaseat(dir_fd, path, chase_flags, ret_path ? &p : NULL, &path_fd);
577,596✔
1044
        if (r < 0)
577,596✔
1045
                return r;
1046
        assert(path_fd >= 0);
44,041✔
1047

1048
        d = xopendirat(path_fd, ".", O_NOFOLLOW);
44,041✔
1049
        if (!d)
44,041✔
1050
                return -errno;
×
1051

1052
        if (ret_path)
44,041✔
1053
                *ret_path = TAKE_PTR(p);
44,041✔
1054

1055
        *ret_dir = d;
44,041✔
1056
        return 0;
44,041✔
1057
}
1058

1059
int chase_and_statat(int dir_fd, const char *path, ChaseFlags chase_flags, char **ret_path, struct stat *ret_stat) {
1✔
1060
        _cleanup_close_ int path_fd = -EBADF;
1✔
1061
        _cleanup_free_ char *p = NULL;
1✔
1062
        int r;
1✔
1063

1064
        assert(path);
1✔
1065
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP)));
1✔
1066
        assert(ret_stat);
1✔
1067

1068
        if (dir_fd == AT_FDCWD && !ret_path && (chase_flags & CHASE_NO_SHORTCUT_MASK) == 0)
1✔
1069
                /* Shortcut this call if none of the special features of this call are requested */
1070
                return RET_NERRNO(fstatat(AT_FDCWD, path, ret_stat,
×
1071
                                          FLAGS_SET(chase_flags, CHASE_NOFOLLOW) ? AT_SYMLINK_NOFOLLOW : 0));
×
1072

1073
        r = chaseat(dir_fd, path, chase_flags, ret_path ? &p : NULL, &path_fd);
1✔
1074
        if (r < 0)
1✔
1075
                return r;
1076
        assert(path_fd >= 0);
1✔
1077

1078
        if (fstat(path_fd, ret_stat) < 0)
1✔
1079
                return -errno;
×
1080

1081
        if (ret_path)
1✔
1082
                *ret_path = TAKE_PTR(p);
1✔
1083

1084
        return 0;
1085
}
1086

1087
int chase_and_accessat(int dir_fd, const char *path, ChaseFlags chase_flags, int access_mode, char **ret_path) {
1✔
1088
        _cleanup_close_ int path_fd = -EBADF;
1✔
1089
        _cleanup_free_ char *p = NULL;
1✔
1090
        int r;
1✔
1091

1092
        assert(path);
1✔
1093
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP)));
1✔
1094

1095
        if (dir_fd == AT_FDCWD && !ret_path && (chase_flags & CHASE_NO_SHORTCUT_MASK) == 0)
1✔
1096
                /* Shortcut this call if none of the special features of this call are requested */
1097
                return RET_NERRNO(faccessat(AT_FDCWD, path, access_mode,
×
1098
                                            FLAGS_SET(chase_flags, CHASE_NOFOLLOW) ? AT_SYMLINK_NOFOLLOW : 0));
×
1099

1100
        r = chaseat(dir_fd, path, chase_flags, ret_path ? &p : NULL, &path_fd);
1✔
1101
        if (r < 0)
1✔
1102
                return r;
1103
        assert(path_fd >= 0);
1✔
1104

1105
        r = access_fd(path_fd, access_mode);
1✔
1106
        if (r < 0)
1✔
1107
                return r;
1108

1109
        if (ret_path)
1✔
1110
                *ret_path = TAKE_PTR(p);
1✔
1111

1112
        return 0;
1113
}
1114

1115
int chase_and_fopenat_unlocked(
179✔
1116
                int dir_fd,
1117
                const char *path,
1118
                ChaseFlags chase_flags,
1119
                const char *open_flags,
1120
                char **ret_path,
1121
                FILE **ret_file) {
1122

1123
        _cleanup_free_ char *final_path = NULL;
179✔
1124
        _cleanup_close_ int fd = -EBADF;
179✔
1125
        int mode_flags, r;
179✔
1126

1127
        assert(path);
179✔
1128
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP|CHASE_PARENT)));
179✔
1129
        assert(open_flags);
179✔
1130
        assert(ret_file);
179✔
1131

1132
        mode_flags = fopen_mode_to_flags(open_flags);
179✔
1133
        if (mode_flags < 0)
179✔
1134
                return mode_flags;
1135

1136
        fd = chase_and_openat(dir_fd, path, chase_flags, mode_flags, ret_path ? &final_path : NULL);
357✔
1137
        if (fd < 0)
179✔
1138
                return fd;
1139

1140
        r = take_fdopen_unlocked(&fd, open_flags, ret_file);
175✔
1141
        if (r < 0)
175✔
1142
                return r;
1143

1144
        if (ret_path)
175✔
1145
                *ret_path = TAKE_PTR(final_path);
1✔
1146

1147
        return 0;
1148
}
1149

1150
int chase_and_unlinkat(int dir_fd, const char *path, ChaseFlags chase_flags, int unlink_flags, char **ret_path) {
1✔
1151
        _cleanup_free_ char *p = NULL, *fname = NULL;
1✔
1152
        _cleanup_close_ int fd = -EBADF;
1✔
1153
        int r;
1✔
1154

1155
        assert(path);
1✔
1156
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP|CHASE_PARENT)));
1✔
1157

1158
        fd = chase_and_openat(dir_fd, path, chase_flags|CHASE_PARENT|CHASE_NOFOLLOW, O_PATH|O_DIRECTORY|O_CLOEXEC, &p);
1✔
1159
        if (fd < 0)
1✔
1160
                return fd;
1161

1162
        r = path_extract_filename(p, &fname);
1✔
1163
        if (r < 0)
1✔
1164
                return r;
1165

1166
        if (unlinkat(fd, fname, unlink_flags) < 0)
1✔
1167
                return -errno;
×
1168

1169
        if (ret_path)
1✔
1170
                *ret_path = TAKE_PTR(p);
1✔
1171

1172
        return 0;
1173
}
1174

1175
int chase_and_open_parent_at(int dir_fd, const char *path, ChaseFlags chase_flags, char **ret_filename) {
976✔
1176
        int pfd, r;
976✔
1177

1178
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP)));
976✔
1179

1180
        r = chaseat(dir_fd, path, CHASE_PARENT|CHASE_EXTRACT_FILENAME|chase_flags, ret_filename, &pfd);
976✔
1181
        if (r < 0)
976✔
1182
                return r;
976✔
1183

1184
        return pfd;
976✔
1185
}
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