Commit Graph

124 Commits

Author SHA1 Message Date
Daan De Meyer
e6724664c3 fd-util: Return 1 from fd_nonblock() if we actually change the mode 2024-04-22 15:28:26 +02:00
Lars Ellenberg
b8e25bff38 fs-utils: new wrapper fd_reopen_propagate_append_and_position()
We may want to propagate O_APPEND, or (try to) keep the current file position,
even if we use fd_reopen() to re-initialize (and "unshare") other file
description status.

For now, used only with --pty to keep/propagate O_APPEND (and/or) position
if set on stdin/stdout.

If we re-open stdout and "drop" the O_APPEND,
we get rather "unexpected" behavior,
for example with repeated "systemd-run --pty >> some-log".

If someone carefully pre-positioned the passed in original file descriptors,
we avoid surprises if we do not reset file postition to zero.

fcntl F_GETFL first, and propagate O_APPEND if present in the existing flags.

Then use lseek to propagate the file position.
2024-03-12 19:01:00 +01:00
Lennart Poettering
9f65355b85 fd-util: beef up fd_verify_safe_flags() features
Let's make fd_verify_safe_flags() even more useful:

1. let's return the cleaned up flags (i.e. just the access mode) after
   validation, hiding all the noise, such as O_NOFOLLOW, O_LARGEFILE and
   similar.

2. let's add a "full" version of the call that allows passing additional
   flags that are OK to be set.
2024-03-11 14:49:51 +01:00
Lennart Poettering
4a5aa6842d fd-util: O_DIRECTORY is fine in fd_verify_safe_flags() too 2024-03-01 22:25:42 +01:00
Mike Yuan
14f38d179d fd-util: introduce fd_verify_safe_flags
As per https://github.com/systemd/systemd/pull/31419#discussion_r1496921074
2024-02-22 06:17:54 +08:00
Adrian Vovk
85f660d46b fd-util: Expose helper to pack fds into 3,4,5,...
This is useful for situations where an array of FDs is to be passed into
a child process (i.e. by passing it through safe_fork). This function
can be called in the child (before calling exec) to pack the FDs to all
be next to each-other starting from SD_LISTEN_FDS_START (i.e. 3)
2024-02-19 11:18:11 +00:00
Lennart Poettering
39d69836ad missing: change our close_range() syscall wrapper to map glibc's
So glibc exposes a close_range() syscall wrapper now, but they decided
to use "unsigned" as type for the fds. Which is a bit weird, because fds
are universally understood to be "int". The kernel internally uses
"unsigned", both for close() and for close_range(), but weirdly,
userspace didn't fix that for close_range() unlike what they did for
close()... Weird.

But anyway, let's follow suit, and make our wrapper match glibc's.

Fixes #31270
2024-02-12 14:07:19 +01:00
Lennart Poettering
3b1e80f7cb process-util: turn off O_NONBLOCK on stdio fds when rearranging fds
We often create our fds O_NONBLOCK, but when we want to invoke some
program with them as stdin/stdout/stderr we really should turn it off
again.
2024-01-08 23:23:42 +01:00
Mike Yuan
6b9cac874c fd-util: don't eat up errors in fd_cloexec_many
Follow-up for ed18c22c98

Before this commit, a successful fd_cloexec() call would
discard all previously gathered errors.
2023-12-29 14:26:40 +01:00
Mike Yuan
1276e63370 fd-util: modernization 2023-12-28 18:26:52 +08:00
Luca Boccassi
5134e54625 basic: add fds_are_same_mount() helper 2023-12-05 17:20:27 +00:00
Mike Yuan
05314c9c87 fd-util: refuse O_CREAT in fd_reopen
O_CREAT doesn't make sense for fd_reopen, since we're
working on an already opened fd. Also, in fd_reopen
we don't handle the mode parameter of open(2), which
means we may get runtime error like #29938.
2023-11-09 03:35:56 +08:00
Lennart Poettering
61c062f896 fd-util: add new FORMAT_PROC_PID_FD_PATH() helper
This is just like FORMAT_PROC_FD_PATH() but goes via the PID number
rather than the "self" symlink.

This is useful whenever we want to generate a path that is useful
outside of our local scope.
2023-11-02 14:09:23 +01:00
Lennart Poettering
7113640493 fd-uitl: rename PIPE_EBADF → EBADF_PAIR, and add EBADF_TRIPLET
We use it for more than just pipe() arrays. For example also for
socketpair(). Hence let's give it a generic name.

Also add EBADF_TRIPLET to mirror this for things like
stdin/stdout/stderr arrays, which we use a bunch of times.
2023-10-26 22:30:42 +02:00
Lennart Poettering
cf45db5598 fd-util: use close_many() where appropriate 2023-10-18 14:03:47 +02:00
Lennart Poettering
d3eb74f8f8 fd-util: add new helper close_many_unset() 2023-10-18 14:03:36 +02:00
Lennart Poettering
3b444970e6 serialize: add serialize_fd_many() helper 2023-10-18 13:51:02 +02:00
Zbigniew Jędrzejewski-Szmek
bb44fd0734 various: use _NEG_ macros to reduce indentation
No functional change intended.
2023-08-16 12:52:56 +02:00
Lennart Poettering
d1e8e8b5ae tree-wide: drop _pure_ + _const_ from local, static functions
This is supposed to be a help for compilers to apply optimizations on
functions where they can't determine whether they are const/pure on
their own. For static, local functions the compiler can do this on its
own easily however, hence the decoration with pure/const is just noise.
Let's drop it, and let the compiler to its thing better.

(Use it for exported functions, since compilers can't 'reach-over' into
other modules to determine if they are pure, except if LTO is used)
2023-08-10 00:01:04 +08:00
Yu Watanabe
68a4fc8b53 fd-util: do not call statx() twice when it does not provide mount ID
Previously, in path_is_root_at(), if statx() does not provide mount ID,
path_get_mnt_id_at() was called, but it also calls statx(). Let's avoid
the second trial.
2023-07-22 02:22:06 +09:00
Yu Watanabe
bd96111d5f fd-util: make path_is_root_at() not fail even when /proc is mounted
path_get_mnt_id_at() -> fd_fdinfo_mnt_id() may return -EOPNOTSUPP when
/proc is mounted, and -ENOSYS otherwise, when an old kernel is used.
2023-07-22 02:20:27 +09:00
Daan De Meyer
549a9a671a fd-util: Add extra shortcut to path_is_root_at() 2023-06-13 16:13:43 +02:00
Daan De Meyer
8a65b0b2c9 fd-util: Add path_is_root_at()
A generalization of dir_fd_is_root() that allows passing a path
component.
2023-06-06 14:42:03 +02:00
David Tardon
e652663a04 tree-wide: use parse_fd() 2023-05-05 09:10:56 +02:00
Yu Watanabe
8d3c49b168 fd-util: skip to check mount ID if kernel is too old and /proc is not mounted
Now, dir_fd_is_root() is heavily used in chaseat(), which is used at
various places. If the kernel is too old and /proc is not mounted, then
there is no way to get the mount ID of a directory. In that case, let's
silently skip the mount ID check.

Fixes https://github.com/systemd/systemd/pull/27299#issuecomment-1511403680.
2023-04-19 03:38:47 +09:00
Yu Watanabe
b4cb4c5cc6 fd-util: slightly optimize dir_fd_is_root()
When STATX_MNT_ID is not supported, we need to manually obtain mount id.
Let's compare inodes earlier.
2023-04-10 10:04:46 +09:00
Daan De Meyer
1b07fd3c22 Merge pull request #27041 from poettering/fdstore-dump
add ability to show contents of service fdstore + teach systemd-notify passing fds into the fdstore
2023-03-30 09:32:13 +02:00
Yu Watanabe
46693a7925 fd-util: make fd_get_path() support AT_FDCWD 2023-03-30 12:03:33 +09:00
Lennart Poettering
b2b84f4b23 fd-util: add helper for converting O_RDONLY/WRONLY/RDWR to strings 2023-03-29 18:27:05 +02:00
Lennart Poettering
fdb583e6a9 fd-util: cover some corner cases with fd_reopen() on symlinks
The /proc/self/fd/ interface cannot be used to follow symlinks pinned
via O_PATH. Add a comment + test for that. Moreover, using fd_reopen()
with O_NOFOLLOW cannot work. Add an explicit check and test for that, to
make behaviour uniform.
2023-03-27 12:28:48 +02:00
Lennart Poettering
42db4a8db7 fd-util: make sure fd_reopen() works with AT_FDCWD systematically
Prompted by:

https://github.com/systemd/systemd/pull/26827#pullrequestreview-1341171981
2023-03-21 20:51:55 +01:00
Daan De Meyer
af423b4ba9 fd-util: Add dir_fd_is_root() 2023-03-15 11:51:08 +01:00
Yu Watanabe
ea61e2e9bd fd-util: introduce a simple helper to check a file descriptor has O_PATH 2023-02-17 07:18:42 +09:00
Yu Watanabe
5bb1d7fbab tree-wide: use -EBADF more 2022-12-21 01:50:33 +09:00
Zbigniew Jędrzejewski-Szmek
351293b3bb basic/fd-util: rearrange variable declarations
Having two blocks of normal variable declarations was unnecessary.
Also 'i' can be narrower in scope.
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
Thomas Haller
6759b62720 all: avoid various "-Wcast-align=strict" warnings 2022-12-09 08:58:07 +01:00
Lennart Poettering
5f5865f0ad fd-util: add new helper fd_reopen_conditional()
This is a wrapper around fd_reopen() that will reopen an fd if the
F_GETFL flags indicate this is necessary, and otherwise not.

This is useful for various utility calls that shall be able to operate
on O_PATH and without it, and might need to convert between the two
depending on what's passed in.
2022-11-29 15:32:47 +01:00
Zbigniew Jędrzejewski-Szmek
3ae6b3bf72 basic: rename util.h to logarithm.h
util.h is now about logarithms only, so we can rename it. Many files included
util.h for no apparent reason… Those includes are dropped.
2022-11-08 18:21:10 +01:00
Lennart Poettering
ed18c22c98 fd-util: add new fd_cloexec_many() helper 2022-11-04 18:46:42 +01:00
Lennart Poettering
d11c14a981 fd-util: make fd_in_set() (and thus close_all_fds()) handle invalidated fds in the array
let's handle gracefully if fds in the specified array are already
invalidated (i.e. negative). This is handy when putting together arrays
on the fly.
2022-11-04 18:45:32 +01:00
Yu Watanabe
4117366a28 btrfs-util: move btrfs_defrag_fd() from fd-util.[ch]
After d71ece3f0b, the function is not used
in libbasic or libsystemd anymore. Let's move it to more appropriate
place.
2022-10-25 16:25:46 +09:00
Yu Watanabe
ee3455cf11 fd-util: rename CLOSE_AND_REPLACE() -> close_and_replace()
We have free_and_replace() and friends, they are all named with lower
letters, even they are macros, not functions.
For consistency, let's rename CLOSE_AND_REPLACE() with lower letters.

This also mekes the macro used more places.
2022-09-17 09:54:41 +02:00
Rudi Heitbaum
3657d3a01c glibc: Remove #include <linux/fs.h> to resolve fsconfig_command/mount_attr conflict with glibc 2.36 2022-07-24 20:52:35 +09:00
Yu Watanabe
7e93a65868 fd-util: rename loop_get_diskseq() -> fd_get_diskseq()
And move it from loop-util.[ch] -> fd-util.[ch]
2022-04-01 15:13:18 +09:00
Lennart Poettering
675e7fc22c fd-util: use ERRNO_IS_XYZ() macros where appropriate 2022-02-14 17:27:18 +01:00
Lennart Poettering
a9dac7a6dd tree-wide: port various places over to new stat_inode_same() helper 2022-02-14 17:27:14 +01:00
Zbigniew Jędrzejewski-Szmek
af3b864d76 Define FOREACH_DIRENT through FOREACH_DIRENT_ALL
As in the previous commit, 'de' is used as the iterator variable name.
2021-12-15 22:50:00 +01:00
Lennart Poettering
7c248223eb tree-wide: use new RET_NERRNO() helper at various places 2021-11-16 08:04:09 +01:00
Lennart Poettering
73fc0cbc87 fd-util: export get_max_fd() so that we can use it in tests 2021-10-27 17:56:36 +02:00