Commit Graph
113 Commits
Author SHA1 Message Date
Yu Watanabe e6a25c2fc7 stat-util: make proc_mounted() not update errno
Typically, proc_mounted() is used in error handling. Hence, it is better
to make it not update the original errno.

Currently, there are two places that returns wrong error code:
- pidref_get_capability() in src/basic/capability-util.c
```c
        _cleanup_fclose_ FILE *f = fopen(path, "re");
        if (!f) {
                if (errno == ENOENT && proc_mounted() == 0)
                        return -ENOSYS;

                return -errno;
        }
```
- fdset_new_fill() in src/shared/fdset.c
```c
        d = opendir("/proc/self/fd");
        if (!d) {
                if (errno == ENOENT && proc_mounted() == 0)
                        return -ENOSYS;

                return -errno;
        }
```

Rather than fixing them, let's make proc_mounted() not update errno,
otherwise we may make a similar failure in a future.
2026-01-20 22:37:04 +09:00
Mike Yuan 5f90e4e4bc stat-util: O_NOCTTY is nop with O_PATH 2026-01-19 13:33:27 +01:00
Mike Yuan b0906b1216 stat-util: do not use RET_NERRNO for our own function
Follow-up for 95abe4beff
2026-01-19 13:33:27 +01:00
Lennart Poettering 95abe4beff stat-util: write up XAT_FDROOT in stat-util.[ch] 2026-01-17 12:37:39 +01:00
Yu Watanabe d377cc5244 stat-util: STATX_TYPE and STATX_INO should be always set 2026-01-13 10:21:05 +09:00
Yu Watanabe 7ecfa87c56 fd-util,mountpoint-util: STATX_MNT_ID is supported since kernel 5.10
Our baseline on kernel is 5.10, hence we can assume it works.
2026-01-13 10:21:05 +09:00
Yu Watanabe a98a6eb95c tree-wide: statx() supports STATX_ATTR_MOUNT_ROOT since kernel 5.8
Our baseline on kernel is 5.10, hence we can unconditionally use it.
2026-01-13 10:21:04 +09:00
Lennart Poettering 8be204df2b stat-util: add fd_verify_symlink() helper 2025-10-14 11:21:46 +02:00
Zbigniew Jędrzejewski-Szmek c136be3f04 basic/stat-util: avoid access syscall
I was looking at strace for systemd-getty-generator and noticed the call to
faccessat2(3</sys>, "", W_OK, AT_EMPTY_PATH), even though we already did
fstatfs(3</sys>), which should give us all the necessary information. Let's
only do this additional check when it's likely to yield something useful, i.e.
for network fses and otherwise skip the syscall.

The call to statvfs is replaced by statfs because that gives us the .f_type
field and allows is_network_fs() to be called.

I'm a bit worried that the is_network_fs() is somewhat costly. This will be
improved in later commits.
2025-07-27 13:13:43 +02:00
Yu Watanabe 3fc2a44043 include: move trivial kernel header wrappers to src/include/override/ 2025-07-11 13:05:46 +09:00
Yu Watanabe 9d0fed7b44 stat-util: split null_or_empty() into two 2025-06-28 03:54:48 +09:00
Yu Watanabe 43aacae83a dirent-util: use getdents64() as is
This partially reverts e86a492ff0.

The function getdents64() was introduced in glibc-2.30, and our baseline
on glibc is 2.31. Hence, we can assume the function always exists.
The posix_getdents() wrapper was introduced for compatibility with musl.
However, even the latest release of musl does not provide posix_getdents()
yet. Also, even with musl, by defining _LARGEFILE64_SOURCE, we can get
getdents64() and struct dirent64. Hence, the wrapper is anyway not
necessary.
2025-06-18 10:45:49 +02:00
Daan De Meyer 0c15577abe basic + fundamental: Clean up includes
Split out of #37344.
2025-05-25 10:06:07 +02:00
Daan De Meyer 93a1f7921a basic: Stop including log.h in macro.h
Now that the necessary functions from log.h have been moved to macro.h,
we can stop including log.h in macro.h. This requires modifying source
files all over the tree to include log.h instead.
2025-04-18 14:19:15 +02:00
Yu Watanabe 99d704c6e8 stat-util: drop unnecessary call of xopenat() in xstatfsat() 2025-04-05 00:13:01 +09:00
Yu Watanabe e86a492ff0 dirent-util: introduce simple wrapper of posix_getdents()
glibc exports getdents64 syscall as is, but musl exports it as
posix_getdents(). Let's introduce a simple wrapper of posix_getdents().

Note, our baseline for glibc is 2.31. Hence, we can assume getdents64()
always defined when building with glibc.
2025-03-04 04:23:57 +09:00
Yu Watanabe d5ddc0e0d3 stat-util: drop statx_fallback()
Now both our required baseline of glibc and kernel support statx.
2025-03-01 00:33:32 +09:00
Yu Watanabe 4424e6c811 tree-wide: drop workarounds for statx()
struct statx in glibc header was introduced in glibc-2.28
(fd70af45528d59a00eb3190ef6706cb299488fcd), but at that time,
sys/stat.h conflicts with linux/stat.h. Since glibc-2.30
(5dad6ffbb2b76215cfcd38c3001778536ada8e8a), sys/stat.h includes
linux/stat.h if exists.

Since now our baseline of glibc is 2.31. Hence, we can drop workarounds
for struct statx by importing linux/stat.h from newer kernel (v6.14-rc4).
2025-03-01 00:33:28 +09:00
Lennart Poettering 4b11087b37 fs-util: also add an at_flags_normalize_follow() helper
This is the reverse of at_flags_normalize_follow()
2025-01-22 17:46:50 +01:00
Mike Yuan e472238525 basic/stat-util: use xopenat() where appropriate 2024-10-15 01:16:57 +02:00
Lennart Poettering f663a15aa5 stat-util: port inode_same_at() to use FID file handle data to determine inode identity
Let's be good boys, and use FID for this.
2024-07-01 16:02:05 +02:00
Zbigniew Jędrzejewski-Szmek 1934242b72 Drop unnecessary path_equal_ptr() wrapper
path_equal already works with NULL pointers.
2024-03-23 13:21:06 +01:00
Yu Watanabe d30d44883f stat-util: expose fd_is_read_only_fs()
Currently it is not used, but the fucntion is already quite generic and
useful.
2024-03-14 23:45:27 +08:00
Yu Watanabe f03caa0d3e stat-util: fix typo
Follow-up for 7cff2b79f0.
2024-03-14 17:37:50 +09:00
Lennart Poettering 7cff2b79f0 stat-util: make sure inode_type_to_string() handles anonymous inodes in a reasonable way 2024-03-12 10:45:24 +01:00
Lennart Poettering 1c248d7fb7 stat-util: add explicit helpers for checking if stat/statx is initialized
This is a follow-up for 945a8210c7 and
makes the st_dev check generic, so that we can reuse it some other
places. It also incorporates the non-NULL check now, to be a
comprehensive one-stop solution.

The helper is static inline so that compilers can optimize the redundant
checks away in case it is combined with other checks.
2024-03-12 10:25:48 +01:00
Mike Yuan 945a8210c7 stat-util: struct stat could be initialized with (st_mode & S_IFMT == 0)
For anonymous inodes, the result would be 0, but
the struct stat is initialized obviously.
So let's switch to st_dev for the check, which
is guaranteed to be non-zero.

Also this is completely unnecessary for statx(),
since we check stx_mask first and that on its own
denotes that the struct is initialized.
2024-03-11 22:57:50 +08:00
Mike Yuan 2560dcbfe6 stat-util: generalize is_* and verify_* handling 2024-03-04 19:53:51 +00:00
Yu Watanabe a6d0cf939c stat-util: introduce {stat,fd}_verify_linked() 2024-02-16 03:48:18 +09:00
Yu Watanabe dd7fa015a6 stat-util: rebreak comment 2024-02-16 03:48:18 +09:00
Yu Watanabe e40b11be04 fs-util: rename xopenat() -> xopanat_full() 2024-02-16 00:25:37 +09:00
Lennart Poettering cc03788086 stat-util: add inode_type_from_string() helper 2024-01-03 18:38:46 +01:00
Yu Watanabe c01a5c0527 siphash24: introduce siphash24_compress_typesafe() macro
To prevent copy-and-paste mistake.

This also introduce in_addr_hash_func().

No functional change, just refactoring.
2023-12-25 15:38:59 +09:00
Lennart Poettering 6a7e015429 stat-util: make file name arguments optional in inode_same_at() 2023-11-24 11:00:52 +01:00
Yu Watanabe d35ff4b60e stat-util: fix error handling of statx()
When newer glibc is used, but kernel does not support statx(), then
glibc try to fallback with fstatat(). That's quite similar to our
implementation, but the supported flags are different, and if
unsupported flags are specified, it returns EINVAL.

Let's handle the case more gracefully.
2023-07-22 02:14:54 +09:00
Lennart Poettering e83fbf4b2a stat-util: add missing S_IFLNK entry in inode_type_to_string()
No idea what happened here, but it's a glaring omission.
2023-06-20 19:10:32 +01:00
Daan De Meyer b196e17ed4 stat-util: Add is_fs_type_at() 2023-06-06 14:42:03 +02:00
Daan De Meyer f90c1959db stat-util: Follow coding style in xstatfsat()
Allow passing NULL to indicate the empty path per coding style.
2023-06-06 14:42:03 +02:00
Daan De Meyer 420d2e3136 fs-util: Add XOpenFlags with XO_LABEL flag to have xopenat() MAC label files/dirs 2023-05-31 13:15:56 +02:00
Lennart Poettering 563e684689 stat-util: rename files_same() → inode_same()
Let's be more accurate about what this function does: it checks whether
the underlying reported inode is the same. Internally, this already uses
a better named stat_inode_same() call, hence let's similarly name the
wrapping function following the same logic.

Similar for files_same_at() and path_equal_or_same_files().

No code changes, just some renaming.
2023-05-19 17:42:41 +02:00
Lennart Poettering 676ade310a stat-util: add _at() variant for files_same() 2023-05-19 17:41:08 +02:00
Zbigniew Jędrzejewski-Szmek 08c2f9c626 detect-virt: add message at debug level
Normal users do not have permissions to access /proc/1/root, so
'systemd-detect-virt -r' fails, but the output, even at debug level
is cryptic:

$ SYSTEMD_LOG_LEVEL=debug build/systemd-detect-virt -r
Failed to check for chroot() environment: Permission denied

Let's make this a bit easier to figure out:

$ SYSTEMD_LOG_LEVEL=debug build/systemd-detect-virt -r
Cannot stat /proc/1/root: Permission denied
Failed to check for chroot() environment: Permission denied

I looked over other users of files_same(), and I think in general the message
at debug level is OK for them too.
2023-04-21 03:20:24 +08:00
Daan De Meyer 6eec59f9cc stat-util: Add xstatfsat() 2023-04-01 14:25:30 +02:00
Lennart Poettering d83ce13636 stat-util: add inode_type_to_string() helper for showing mode_t inode type as string 2023-03-29 18:27:04 +02:00
Zbigniew Jędrzejewski-Szmek 3e2d735328 basic/stat-util: remove unused null_or_empty_fd() 2023-03-28 18:44:40 +02:00
Daan De Meyer f461a28da7 chase-symlinks: Rename chase_symlinks() to chase()
Chasing symlinks is a core function that's used in a lot of places
so it deservers a less verbose names so let's rename it to chase()
and chaseat().

We also slightly change the pattern used for the chaseat() helpers
so we get chase_and_openat() and similar.
2023-03-24 13:43:51 +01:00
Daan De Meyer bd7a79528c stat-util: Add verify_regular_at() 2023-03-22 12:17:14 +01:00
Daan De Meyer 06344330a1 chase-symlinks: Remove unused ret_fd arguments 2023-03-14 13:46:58 +01:00
Zbigniew Jędrzejewski-Szmek a5937dcf39 tree-wide: change initialization to use EBADF instead of EBADFD
Those fds never were, so it's not fair to say that they are in "bad state".
Let's use the shorter and more direct errno.
2022-12-19 15:00:59 +01:00
Zbigniew Jędrzejewski-Szmek 254d1313ae tree-wide: use -EBADF for fd initialization
-1 was used everywhere, but -EBADF or -EBADFD started being used in various
places. Let's make things consistent in the new style.

Note that there are two candidates:
EBADF 9 Bad file descriptor
EBADFD 77 File descriptor in bad state

Since we're initializating the fd, we're just assigning a value that means
"no fd yet", so it's just a bad file descriptor, and the first errno fits
better. If instead we had a valid file descriptor that became invalid because
of some operation or state change, the other errno would fit better.

In some places, initialization is dropped if unnecessary.
2022-12-19 15:00:57 +01:00