summaryrefslogtreecommitdiff
path: root/src/shared/exec-util.c
Commit message (Collapse)AuthorAgeFilesLines
* exec-util: make execute_strv() optionally take root directoryYu Watanabe2023-04-161-3/+5
| | | | Preparation for rewriting kernel-install in C.
* shared/exec-util: reduce scope of iterator variablesZbigniew Jędrzejewski-Szmek2023-04-031-6/+3
|
* shared/exec-util: null_or_empty_path() does not return booleanZbigniew Jędrzejewski-Szmek2023-03-281-1/+1
| | | | | | We shouldn't report that the file is empty if the stating fails. Let's do the same as in other places, and just ignore the error and let the subsequent operation fail.
* exec-util: introduce EXEC_DIR_SKIP_REMAINING flagYu Watanabe2023-03-171-3/+21
| | | | Will be used in later commits.
* exec-util: extract the core logic of execute_directories() as execute_strv()Yu Watanabe2023-03-171-16/+38
| | | | | | Then, we can use it with a custom enumerator of executables. No functional change, preparation for later commits.
* exec-util: enumerate executables earlierYu Watanabe2023-03-171-10/+13
| | | | Then, return earlier if no executable found.
* exec-util: drop meaningless castsYu Watanabe2023-03-171-7/+6
|
* exec-util: tighten variable scope a bitYu Watanabe2023-03-171-5/+6
|
* process-util: rename FORK_NULL_STDIO -> FORK_REARRANGE_STDIOYu Watanabe2023-02-211-0/+1
| | | | And make safe_fork_full() takes fds to be assigned to stdio.
* exec-util: use TAKE_FD()Yu Watanabe2023-02-191-4/+2
|
* exec-util: propagate error in wait_for_terminate_and_check()Yu Watanabe2023-02-191-4/+5
| | | | | Then, the two error handlings becomes consistent with the one in execute_directories().
* tree-wide: set FORK_RLIMIT_NOFILE_SAFE flagYu Watanabe2023-02-071-7/+2
| | | | No functional changes, just refactoring.
* shared: port various shared helpers basename() → path_extract_filename()Lennart Poettering2022-12-231-4/+12
|
* tree-wide: use -EBADF for fd initializationZbigniew Jędrzejewski-Szmek2022-12-191-4/+4
| | | | | | | | | | | | | | | | -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.
* basic: rename util.h to logarithm.hZbigniew Jędrzejewski-Szmek2022-11-081-1/+0
| | | | | 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.
* tree-wide: use ASSERT_PTR moreDavid Tardon2022-09-131-9/+3
|
* strv: make iterator in STRV_FOREACH() declaread in the loopYu Watanabe2022-03-191-3/+1
| | | | This also avoids multiple evaluations in STRV_FOREACH_BACKWARDS()
* tree-wide: some additional checks to avoid CVE-2021-4034 style weaknessesLennart Poettering2022-01-311-0/+9
|
* tree-wide: always use TAKE_FD() when calling rearrange_stdio()Lennart Poettering2021-11-031-1/+1
| | | | | | | | | | | | rearrange_stdio() invalidates specified fds even on failure, which means we should always invalidate the fds we pass in no matter what. Let's make this explicit by using TAKE_FD() for that everywhere. Note that in many places we such invalidation doesnt get us much behaviour-wise, since we don't use the variables anymore later. But TAKE_FD() in a way is also documentation, it encodes explicitly that the fds are invalidated here, so I think it's a good thing to always make this explicit here.
* Revert "basic/fd-util: sort the 'except' array in place"Lennart Poettering2021-10-271-1/+1
| | | | This reverts commit 9c46228b7deb53d6384545535b37b2844a102b2b.
* basic: move freeze() from shared/exec-util.h to basic/process-util.hLennart Poettering2021-10-221-23/+0
| | | | | That way we can use it in other code from basic/. It fits into both headers equally well or badly, hence let's just move this one function.
* tree-wide: assorted Coccinelle fixesFrantisek Sumsal2021-10-081-1/+1
| | | | It's that time of year again.
* exec-util: handle gracefully if we want to fork an agent but have no ↵Lennart Poettering2021-08-301-12/+18
| | | | | | controlling tty Fixes: #20576
* Add variant of close_all_fds() that does not allocate and use it in freeze()Zbigniew Jędrzejewski-Szmek2021-07-231-1/+1
| | | | | | | | | | | | Even though it's just a fallback path, let's not be sloppy and allocate in the crash handler. > The deadlock happens because systemd crash in malloc() then in signal > handler, it calls malloc() (close_all_fds()-> opendir()-> __alloc_dir()) > again. malloc() is not a signal-safe function, maybe we should re-think > the logic here. Fixes #20266.
* Move freeze() into shared/Zbigniew Jędrzejewski-Szmek2021-07-231-0/+23
| | | | | | | | Library code should not call freeze(), this is something that should only be done by "application code", so moving it into shared/ is appropriate. The fallback to call _exit() is dropped: let's trust that the infinite loop is infinite.
* Move fork_agent() into shared/Zbigniew Jędrzejewski-Szmek2021-07-231-0/+73
| | | | | | | Currently it's only used in two places in src/shared/, so the function was already included just once in compiled code. But it seems appropriate to move it there anyway, because library code should have no need to fork agents, so it doesn't belong in basic/.
* Merge pull request #18641 from benjarobin/fix-enum-invalid-valZbigniew Jędrzejewski-Szmek2021-02-171-4/+6
|\ | | | | Various follow-up: Fix build and EINVAL for _INVALID enum value
| * shared: use -EINVAL for _EXEC_COMMAND_FLAGS_INVALIDBenjamin Robin2021-02-161-4/+6
| | | | | | | | Follow-up of #11484
* | shared/exec-util: fix minor memleakZbigniew Jędrzejewski-Szmek2021-02-161-8/+2
|/ | | | p was not freed on error.
* core: also set $SYSTEMD_EXEC_PID= for generatorsYu Watanabe2021-02-011-2/+8
|
* Merge pull request #17079 from keszybz/late-exec-resolutionLennart Poettering2020-12-031-1/+25
|\ | | | | Resolve executable paths before execution, use fexecve()
| * meson: add option for fexecve useZbigniew Jędrzejewski-Szmek2020-11-061-0/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | There are downsides to using fexecve: when fexecve is used (for normal executables), /proc/pid/status shows Name: 3, which means that ps -C foobar doesn't work. pidof works, because it checks /proc/self/cmdline. /proc/self/exe also shows the correct link, but requires privileges to read. /proc/self/comm also shows "3". I think this can be considered a kernel deficiency: when O_CLOEXEC is used, this "3" is completely meaningless. It could be any number. The kernel should use argv[0] instead, which at least has *some* meaning. I think the approach with fexecve/execveat is instersting, so let's provide it as opt-in.
| * shared/exec-util: fall back to execve() also on permission errorsZbigniew Jędrzejewski-Szmek2020-11-061-2/+4
| |
| * shared/exec-util: use our own execveat() wrapper instead of fexecve()Zbigniew Jędrzejewski-Szmek2020-11-061-3/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | For scripts, when we call fexecve(), on new kernels glibc calls execveat(), which fails with ENOENT, and then we fall back to execve() which succeeds: [pid 63039] execveat(3, "", ["/home/zbyszek/src/systemd/test/test-path-util/script.sh", "--version"], 0x7ffefa3633f0 /* 0 vars */, AT_EMPTY_PATH) = -1 ENOENT (No such file or directory) [pid 63039] execve("/home/zbyszek/src/systemd/test/test-path-util/script.sh", ["/home/zbyszek/src/systemd/test/test-path-util/script.sh", "--version"], 0x7ffefa3633f0 /* 0 vars */) = 0 But on older kernels glibc (some versions?) implement a fallback which falls into the same trap with bash $0: [pid 13534] execve("/proc/self/fd/3", ["/home/test/systemd/test/test-path-util/script.sh", "--version"], 0x7fff84995870 /* 0 vars */) = 0 We don't want that, so let's call execveat() ourselves. Then we can do the execve() fallback as we want.
| * core/execute: fall back to execve() for scriptsZbigniew Jędrzejewski-Szmek2020-11-061-0/+20
| | | | | | | | fexecve() fails with ENOENT and we need a fallback. Add appropriate test.
| * core: use fexecve() to spawn childrenZbigniew Jędrzejewski-Szmek2020-11-061-1/+0
| | | | | | | | | | We base the smack/selinux setup on the executable. Let's open the file once and use the same fd for that setup and the subsequent execve.
* | license: LGPL-2.1+ -> LGPL-2.1-or-laterYu Watanabe2020-11-091-1/+1
|/
* core: add ExecStartXYZEx= with dbus support for executable prefixesAnita Zhang2019-05-301-0/+74
| | | | Closes #11654
* codespell: fix spelling errorsBen Boeckel2019-04-291-1/+1
|
* core: Allow to configure execute_directories execution behaviorMatthias Klumpp2019-02-181-19/+38
| | | | | | | | | This adds a new bitfield to `execute_directories()` which allows to configure whether to ignore non-zero exit statuses of binaries run and whether to allow parallel execution of commands. In case errors are not ignored, the exit status of the failed script will now be returned for error reposrting purposes or other further future use.
* tree-wide: Remove O_CLOEXEC from fdopenChris Down2018-12-121-1/+1
| | | | | | | | | | | | fdopen doesn't accept "e", it's ignored. Let's not mislead people into believing that it actually sets O_CLOEXEC. From `man 3 fdopen`: > e (since glibc 2.7): > Open the file with the O_CLOEXEC flag. See open(2) for more information. This flag is ignored for fdopen() As mentioned by @jlebon in #11131.
* Merge pull request #10935 from poettering/rlimit-nofile-safeZbigniew Jędrzejewski-Szmek2018-12-061-0/+3
|\ | | | | | | Merged by hand to resolve a trivial conflict in TODO.
| * tree-wide: invoke rlimit_nofile_safe() before various exec{v,ve,l}() invocationsLennart Poettering2018-12-011-0/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Whenever we invoke external, foreign code from code that has RLIMIT_NOFILE's soft limit bumped to high values, revert it to 1024 first. This is a safety precaution for compatibility with programs using select() which cannot operate with fds > 1024. This commit adds the call to rlimit_nofile_safe() to all invocations of exec{v,ve,l}() and friends that either are in code that we know runs with RLIMIT_NOFILE bumped up (which is PID 1 and all journal code for starters) or that is part of shared code that might end up there. The calls are placed as early as we can in processes invoking a flavour of execve(), but after the last time we do fd manipulations, so that we can still take benefit of the high fd limits for that.
* | util-lib: split out env file parsing code into env-file.cLennart Poettering2018-12-021-0/+1
| | | | | | | | | | | | It's quite complex, let's split this out. No code changes, just some file rearranging.
* | util-lib: split out all temporary file related calls into tmpfiles-util.cLennart Poettering2018-12-021-0/+1
|/ | | | | | | | 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.
* Move various files that don't need to be in basic/ to shared/Zbigniew Jędrzejewski-Szmek2018-11-201-0/+348
This doesn't have much effect on the final build, because we link libbasic.a into libsystemd-shared.so, so in the end, all the object built from basic/ end up in libsystemd-shared. And when the static library is linked into binaries, any objects that are included in it but are not used are trimmed. Hence, the size of output artifacts doesn't change: $ du -sb /var/tmp/inst* 54181861 /var/tmp/inst1 (old) 54207441 /var/tmp/inst1s (old split-usr) 54182477 /var/tmp/inst2 (new) 54208041 /var/tmp/inst2s (new split-usr) (The negligible change in size is because libsystemd-shared.so is bigger by a few hundred bytes. I guess it's because symbols are named differently or something like that.) The effect is on the build process, in particular partial builds. This change effectively moves the requirements on some build steps toward the leaves of the dependency tree. Two effects: - when building items that do not depend on libsystemd-shared, we build less stuff for libbasic.a (which wouldn't be used anyway, so it's a net win). - when building items that do depend on libshared, we reduce libbasic.a as a synchronization point, possibly allowing better parallelism. Method: 1. copy list of .h files from src/basic/meson.build to /tmp/basic 2. $ for i in $(grep '.h$' /tmp/basic); do echo $i; git --no-pager grep "include \"$i\"" src/basic/ 'src/lib*' 'src/nss-*' 'src/journal/sd-journal.c' |grep -v "${i%.h}.c";echo ;done | less