summaryrefslogtreecommitdiffstats
path: root/src/test/test-fd-util.c (follow)
Commit message (Collapse)AuthorAgeFilesLines
* fd-util: use F_DUPFD_QUERY for same_fd()Lennart Poettering2024-10-081-1/+17
| | | | | | | | | | | | | | Catch up with the nice little toys the kernel fs developers have added for us. Preferably, let's make use of the new F_DUPFD_QUERY fcntl() call that checks whether two fds are just duplicates of each other (duplicates as in dup(), not as in open() of the same inode, i.e. whether they share a single file offset and so on). This API is much nicer, since it is a core kernel feature, unlike the kcmp() call we so far used, which is part of the (optional) checkpoint/restore stuff. F_DUPFD_QUERY is available since kernel 6.10.
* fd-util: introduce fd_validate() helperLennart Poettering2024-10-081-5/+18
| | | | | It just uses F_GETFD to validate an fd. it's a bit easier to read though, and handles the < 0 case internally.
* tree-wide: Use log_setup() everywhereDaan De Meyer2024-04-251-1/+1
| | | | | Otherwise the default log target is the console and we won't use the journal socket even if it is available.
* ASSERT_STREQ for simple casesIvan Kruglov2024-04-151-17/+17
|
* Introduce ASSERT_OK_ERRNO()Daan De Meyer2024-04-101-8/+8
| | | | | | ASSERT_OK() is for functions that return negative errno. Let's introduce ASSERT_OK_ERRNO() for syscalls that return -1 and store the error in errno.
* test: make test-fd-util more lenient when using fd_move_above_stdio()Frantisek Sumsal2024-04-051-9/+13
| | | | | | | | | | | | | | | | | | | | | | On s390x this test fails when the SUT uses the z90crypt kernel module, as it's an another FD the test doesn't account for: /* test_rearrange_stdio */ Successfully forked off 'rearrange' as PID 57293. test_rearrange_stdio: r=0 /proc/57293/fd: total 0 lrwx------. 1 root root 64 Apr 5 06:18 0 -> /dev/pts/0 lrwx------. 1 root root 64 Apr 5 06:18 1 -> /dev/pts/0 lrwx------. 1 root root 64 Apr 5 06:18 2 -> /dev/pts/0 lrwx------. 1 root root 64 Apr 5 06:18 3 -> /dev/z90crypt rearrange terminated by signal ABRT. Debugging this was pain, since the child process didn't log anything once we closed stdout/stderr (for obvious reasons). Let's fix both issues by switching logging to kmsg once we close stdin/stdout/stderr, and also by making the test work fine when there are some extra FDs in the child's environment.
* data-fd-util: accept size == SIZE_MAX and translate that to strlenMike Yuan2024-03-121-1/+1
| | | | Like what we already do at other places
* basic: add fds_are_same_mount() helperLuca Boccassi2023-12-051-0/+18
|
* process-util: add new FORK_DEATHSIG_SIGKILL flag, rename FORK_DEATHSIG → ↵Lennart Poettering2023-11-021-4/+4
| | | | | | | | | | | | | | | | | | FORK_DEATHSIG_SIGTERM Sometimes it makes sense to hard kill a client if we die. Let's hence add a third FORK_DEATHSIG flag for this purpose: FORK_DEATHSIG_SIGKILL. To make things less confusing this also renames FORK_DEATHSIG to FORK_DEATHSIG_SIGTERM to make clear it sends SIGTERM. We already had FORK_DEATHSIG_SIGINT, hence this makes things nicely symmetric. A bunch of users are switched over for FORK_DEATHSIG_SIGKILL where we know it's safe to abort things abruptly. This should make some kernel cases more robust, since we cannot get confused by signal masks or such. While we are at it, also fix a bunch of bugs where we didn't take FORK_DEATHSIG_SIGINT into account in safe_fork()
* fd-uitl: rename PIPE_EBADF → EBADF_PAIR, and add EBADF_TRIPLETLennart Poettering2023-10-261-1/+1
| | | | | | | | 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.
* serialize: add open_serialization_file()Luca Boccassi2023-10-121-0/+11
|
* Revert "tree-wide: Mount file descriptors via /proc/<pid>/fd"Lennart Poettering2023-09-081-10/+5
|
* fd-util: Use /proc/pid/fd instead of /proc/self/fdDaan De Meyer2023-08-141-5/+10
| | | | | | | | | | | | | | | | | | | | | Currently, we mount via file descriptors using /proc/self/fd. This works, but it means that in /proc/mounts and various other files, the source of the mount will be listed as /proc/self/fd/xxx. For other software that parses these files, /proc/self/fd/xxx doesn't mean anything, or worse, it means the completely wrong thing, as it will refer to one of their own file descriptors instead. Let's improve the situation by using /proc/pid/fd instead. This allows processes parsing /proc/mounts to do the right thing more often than not. One scenario where even this doesn't work if when containers are involved, as with the pid namespace unshared, even /proc/pid/fd will mean the wrong thing, but it's no worse than /proc/self/fd which will always means the wrong thing. This also doesn't work if we mount via file descriptor and then exit, as the pid will be gone, but it does work as long as the process that did the mount is alive, which makes it useful for systemd-dissect --with for example if the program we run in the image wants to parse /proc/mounts.
* test: use _cleanup_ for temp. filesDavid Tardon2023-04-211-10/+4
|
* test: add tests for dir_fd_is_root_or_cwd()Yu Watanabe2023-04-051-0/+7
| | | | Follow-up for e212f422796da9e626030289faf083407c8955df.
* fd-util: make fd_get_path() support AT_FDCWDYu Watanabe2023-03-301-0/+5
|
* test: add tests for fd_get_path()Yu Watanabe2023-03-301-0/+112
|
* test-fd-util: use stat_inode_same() where appropriateLennart Poettering2023-03-271-8/+4
|
* fd-util: cover some corner cases with fd_reopen() on symlinksLennart Poettering2023-03-271-0/+22
| | | | | | | 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.
* core: Settle log target if we're going to be closing all fdsDaan De Meyer2023-03-221-0/+1
| | | | | | | | | | | Whenever we're going to close all file descriptors, we tend to close the log and set it into open when needed mode. When this is done with the logging target set to LOG_TARGET_AUTO, we run into issues because for every logging call, we'll check if stderr is connected to the journal to determine where to send the logging message. This check obviously stops working when we close stderr, so we settle the log target before we do that so that we keep using the same logging target even after stderr is closed.
* test: add a testcase that dir_fd_is_root() is not confused by bind mountYu Watanabe2023-03-171-0/+33
| | | | | See the comment in dir_fd_is_root() and https://github.com/systemd/systemd/pull/26820#issuecomment-1469554966.
* fd-util: Add dir_fd_is_root()Daan De Meyer2023-03-151-0/+12
|
* tree-wide: use -EBADF moreYu Watanabe2022-12-201-2/+2
|
* tree-wide: introduce PIPE_EBADF macroYu Watanabe2022-12-201-1/+1
|
* tree-wide: use -EBADF also in pipe initializersZbigniew Jędrzejewski-Szmek2022-12-191-1/+1
| | | | In some places, initialization is dropped when unnecesary.
* tree-wide: use -EBADF for fd initializationZbigniew Jędrzejewski-Szmek2022-12-191-19/+19
| | | | | | | | | | | | | | | | -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.
* fd-util: add new helper fd_reopen_conditional()Lennart Poettering2022-11-291-0/+47
| | | | | | | | | 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.
* test-fd-util: fix typos and use log_tests_skipped()Zbigniew Jędrzejewski-Szmek2022-11-091-12/+8
|
* tree-wide: fix typoYu Watanabe2021-11-301-1/+1
|
* test: Use TEST macroJan Janssen2021-11-251-31/+12
| | | | | | | | | This converts to TEST macro where it is trivial. Some additional notable changes: - simplify HAVE_LIBIDN #ifdef in test-dns-domain.c - use saved_argc/saved_argv in test-copy.c, test-path-util.c, test-tmpfiles.c and test-unit-file.c
* build: fix build without seccompDominique Martinet2021-11-251-1/+1
| | | | | | | | | | - execute.c: bpf functions were in the middle of an #if HAVE_SECCOMP block for no reason - test-fd-util.c: make seccomp-util.h includable without depending on <seccomp.h>, and make is_seccomp_available() hardcoded to returning false in this case. Also fix a stray DEFINED() -- HAVE_SECCOMP is defined as 0, so normal #if should be used like everywhere else.
* test: add test that ensures TAKE_FD() works as it shouldLennart Poettering2021-11-031-0/+43
|
* test-fd-util: extend close_all_fds() test to trigger all fallback codepathsLennart Poettering2021-10-271-1/+106
| | | | | | | | | This extends the close_all_fds() logic to overmount /proc with an empty tmpfs, and/or to block close_range() via seccomp, so that we run the test case for the function with the fallback paths. This should make sure that we don't regress in limited environments or older kernels.
* fd-util: export get_max_fd() so that we can use it in testsLennart Poettering2021-10-271-4/+4
|
* test-fd-util: add test case for fd_reopen()Lennart Poettering2021-10-221-0/+96
|
* test: add test for FORMAT_PROC_FD_PATH()Lennart Poettering2021-08-191-0/+9
|
* basic: move acquire_data_fd() and fd_duplicate_data_fd() to new data-fd-util.cZbigniew Jędrzejewski-Szmek2021-06-241-137/+1
| | | | | | | | | | | fd_duplicate_data_fd() is renamed to copy_data_fd(). This makes the two functions have nicely similar names. Now fd-util.[ch] is again about low-level file descriptor manipulations. copy_data_fd() is a complex function that internally wraps the other functions in copy.c. I want to move copy.c and the whole cluster of related code from basic/ to shared/ later on, and this is a preparatory step for that.
* license: LGPL-2.1+ -> LGPL-2.1-or-laterYu Watanabe2020-11-091-1/+1
|
* test-fd-util: add test case for close_all_fd()Lennart Poettering2020-10-141-0/+96
|
* test: Use assert_se() where variables are only checked by assertBenjamin Robin2020-05-061-4/+4
| | | | Allow to build without any warning with NDEBUG defined
* util: split out memcmp()/memset() related calls into memory-util.[ch]Lennart Poettering2019-03-131-1/+1
| | | | Just some source rearranging.
* util-lib: move open_serialization_fd() to serialize.cLennart Poettering2018-12-021-0/+1
| | | | | | It definitely fits better there. No code changes, just some rearranging.
* util-lib: split out all temporary file related calls into tmpfiles-util.cLennart Poettering2018-12-021-1/+2
| | | | | | | | This splits out a bunch of functions from fileio.c that have to do with temporary files. Simply to make the header files a bit shorter, and to group things more nicely. No code changes, just some rearranging of source files.
* tests: use a helper function to parse environment and open loggingZbigniew Jędrzejewski-Szmek2018-09-141-1/+2
| | | | | The advantages are that we save a few lines, and that we can override logging using environment variables in more test executables.
* tree-wide: remove Lennart's copyright linesLennart Poettering2018-06-141-3/+0
| | | | | | | | | | | These lines are generally out-of-date, incomplete and unnecessary. With SPDX and git repository much more accurate and fine grained information about licensing and authorship is available, hence let's drop the per-file copyright notice. Of course, removing copyright lines of others is problematic, hence this commit only removes my own lines and leaves all others untouched. It might be nicer if sooner or later those could go away too, making git the only and accurate source of authorship information.
* tree-wide: drop 'This file is part of systemd' blurbLennart Poettering2018-06-141-2/+0
| | | | | | | | | | | | | | | | This part of the copyright blurb stems from the GPL use recommendations: https://www.gnu.org/licenses/gpl-howto.en.html The concept appears to originate in times where version control was per file, instead of per tree, and was a way to glue the files together. Ultimately, we nowadays don't live in that world anymore, and this information is entirely useless anyway, as people are very welcome to copy these files into any projects they like, and they shouldn't have to change bits that are part of our copyright header for that. hence, let's just get rid of this old cruft, and shorten our codebase a bit.
* main: split out reading of /proc/sys/fs/nr_open into its own functionLennart Poettering2018-06-061-0/+8
| | | | | This doesn't really reduce the code size over all, but it does make main.c shorter and more readable, and that's always a good thing.
* fd-util: add new helper call fd_duplicate_data_fd()Lennart Poettering2018-05-241-0/+88
| | | | | | | | | | | | This call creates an fd from another fd containing the same data. Specifically, repeated read() on the returned fd should return the same data as the original fd. This call is useful when we want to copy data out of disk images and suchlike, and want to be pass fds with the data around without having to keep the disk image continously mounted. The implementation tries to be somewhat smart and tries to prefer memfds/pipes over files in /tmp or /var/tmp based on the size of the data, but has appropropriate fallbacks in place.
* tree-wide: drop license boilerplateZbigniew Jędrzejewski-Szmek2018-04-061-13/+0
| | | | | | | | | | Files which are installed as-is (any .service and other unit files, .conf files, .policy files, etc), are left as is. My assumption is that SPDX identifiers are not yet that well known, so it's better to retain the extended header to avoid any doubt. I also kept any copyright lines. We can probably remove them, but it'd nice to obtain explicit acks from all involved authors before doing that.
* coccinelle: always use fcntl(fd, FD_DUPFD, 3) instead of dup(fd)Lennart Poettering2018-03-201-2/+2
| | | | Let's avoid fds 0…2 for safety reasons.