summaryrefslogtreecommitdiff
path: root/src/basic/process-util.c
Commit message (Collapse)AuthorAgeFilesLines
* process-util: be more careful with pidfd_get_pid() special casesLennart Poettering2023-04-171-1/+15
| | | | | | | | | | | | | | Let's be more careful with generating error codes for (expected) error causes. This does not introduce new error conditions, it just changes what we return under specific cases, to make things nicely recognizable in each case. Most importantly this detects if fdinfo reports a pid of "-1" for pidfds with processes that are already reaped (and thus have no PID anymore) None of our current users care about these error codes, but let's get this right for the future.
* process-util: make safe_fork() unset $NOTIFY_SOCKETYu Watanabe2023-04-171-0/+8
| | | | | | | | Propagating $NOTIFY_SOCKET is typically dangerous. Let's unset it unless explicitly requested to keep it. Fixes #27288. Replaces #27291.
* process-util: introduce get_process_cmdline_strv()Yu Watanabe2023-03-281-0/+22
| | | | | | | | | | | | The reason why get_process_cmdline() is so complicated is that we need to escape and quote arguments for building a single result string. That's necessary when we want to log or print the command line. However, when we want to parse the command line, it is not necessary that the result is a single string, but can be strv. This will be used when we parse the command line.
* tree-wide: port various places to find_line_startswith()Lennart Poettering2023-03-241-8/+3
|
* process-util: drop trailing NUls before parsing the nulstrYu Watanabe2023-03-241-9/+3
| | | | No functional changes, just refactoring.
* 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.
* Include <threads.h> if possible to get thread_local definitionCristian Rodríguez2023-03-061-0/+1
| | | | | | | | | | | | IN C23, thread_local is a reserved keyword and we shall therefore do nothing to redefine it. glibc has it defined for older standard version with the right conditions. v2 by Yu Watanabe: Move the definition to missing_threads.h like the way we define e.g. missing syscalls or missing definitions, and include it by the users. Co-authored-by: Yu Watanabe <watanabe.yu+github@gmail.com>
* process-util: rename FORK_NULL_STDIO -> FORK_REARRANGE_STDIOYu Watanabe2023-02-211-16/+30
| | | | And make safe_fork_full() takes fds to be assigned to stdio.
* process-util: show requested process name in the logYu Watanabe2023-02-191-1/+1
| | | | This is useful for debugging issues like #26474.
* process-util: add helper get_process_threads()Lennart Poettering2023-02-171-0/+25
| | | | | Let's add a proper helper for querying the number of threads in a process.
* process-util: add missing error checkLennart Poettering2023-02-081-0/+2
|
* process-util: add helper to verify a pid via its pidfdLuca Boccassi2023-01-201-0/+14
|
* shutdown: propagate mount() failures from child to parentLennart Poettering2023-01-091-0/+17
| | | | | | Let's propagate the actual error code up, as we usual do. Inspired by: #25168
* shared: add new safe_fork flag FORK_PRIVATE_TMPZbigniew Jędrzejewski-Szmek2022-12-141-2/+14
| | | | | | | | | | | | | If the flag is set, we mount /tmp/ in a way that is suitable for generators and other quick jobs. Unfortunately I had to move some code from shared/mount-util.c to basic/mountpoint-util.c. The functions that are moved are very thin wrappers around mount(2), so this doesn't actually change much in the code split between libbasic and libshared. Implications for the host would be weird if a private mount namespace is not used, so assert on FORK_NEW_MOUNTNS when the flag is used.
* strv: move nulstr utilities to nulstr-util.[ch]Lennart Poettering2022-11-131-0/+1
| | | | | | | Let's move them out of the generic, already very long strv.[ch] module into the more specific nulst-util.[ch] No code changes.
* basic: move a bunch of cmdline-related funcs to new argv-util.c+hZbigniew Jędrzejewski-Szmek2022-11-081-205/+1
| | | | | | | | | | | | | I wanted to move saved_arg[cv] to process-util.c+h, but this causes problems: process-util.h includes format-util.h which includes net/if.h, which conflicts with linux/if.h. So we can't include process-util.h in some files. But process-util.c is very long anyway, so it seems nice to create a new file. rename_process(), invoked_as(), invoked_by_systemd(), and argv_looks_like_help() which lived in process-util.c refer to saved_argc and saved_argv, so it seems reasonable to move them to the new file too. util.c is now empty, so it is removed. util.h remains.
* basic: move container_get_leader() to process-util.[ch]Zbigniew Jędrzejewski-Szmek2022-11-081-0/+44
| | | | | basic/util.[ch] is a grab-bag of unrelated functions. Let's move a few of the remaning functions to better locations.
* process-util: add new FORK_CLOEXEC_OFF flag for disabling O_CLOEXEC on ↵Lennart Poettering2022-11-041-0/+8
| | | | | | | | remaining fds Often the fds that shall stay around in the child shall be passed to a process over execve(), hence add an option to explicitly disable O_CLOEXEC on them in the child.
* tree-wide: drop () around the first argument of a ternary opZbigniew Jędrzejewski-Szmek2022-10-111-1/+1
| | | | https://github.com/systemd/systemd/pull/24933#discussion_r991242789
* Merge pull request #23963 from matoro/mainLennart Poettering2022-07-211-2/+8
|\ | | | | Replace __sync intrinsics with __atomic
| * process-util: replace __sync intrinsics to __atomicmatoro2022-07-141-2/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This one is rather tricky, but changing the initialization of current_val should give this the same effect. Based on ffmpeg's change here: https://ffmpeg.org/pipermail/ffmpeg-devel/2014-October/164556.html Quoting from them: > The second reason is __atomic_compare_exchange_n(), and how it differs from > __sync_val_compare_and_swap(). > While the latter returns *ptr as it was before the operation, the former > doesn't and instead copies *ptr to oldval if the result of the > comparison is false. This means that returning oldval will match the old behavoir > without having to change the wrapper. > A disassemble example from libavutil/buffer.o however hints that the > __atomic function may be slower because of it writting oldval.
* | basic/log: split out invoked_by_systemd() utility functionZbigniew Jędrzejewski-Szmek2022-07-151-0/+24
|/
* tree-wide: use 'Architecture' type consistently across the treeLennart Poettering2022-04-051-2/+2
| | | | | | | Some parts of our tree used 'Architecture' for storing architectures, others used ints. Let's unify on the former. Inspired by #22952's rework of the 'Virtualization' enum.
* process-util: refactor APIs for reading /proc/self/xyz symlinksLennart Poettering2022-04-011-28/+13
| | | | | | | | | | | | | The three functions for reading cwd, exe and root symlinks of processes already share a common core: get_process_link_contents(). Let's refactor that a bit, and move formatting of the /proc/self/ path into this helper function instead of doing that in the caller, thus sharing more code. While we are at it, make the return parameters optional, in case the information if the links are readable is interesting, but the contents is not. (This also means safe_getcwd() and readlinkat_malloc() are updated to make the return parameter optional, as these are called by the relevant three functions)
* tree-wide: unify some code that looks for --help in the command lineLennart Poettering2022-03-311-0/+24
|
* strv: make iterator in STRV_FOREACH() declaread in the loopYu Watanabe2022-03-191-1/+0
| | | | This also avoids multiple evaluations in STRV_FOREACH_BACKWARDS()
* time-util: add macros around timespec_store() that operates on compund ↵Lennart Poettering2022-03-181-2/+1
| | | | | | | | literal allocated timespec struct This way we can convert usec_t to timespec on-the-fly, without a buffer. No actual behaviour change just some shortening of code.
* do not call __register_atfork directlyMatija Skala2022-02-141-7/+2
| | | this way it is cleaner and more portable and systemd links against libpthread anyway
* tree-wide: move `unsigned` to the start of type declarationFrantisek Sumsal2022-02-101-2/+2
| | | | | | | | | | | | | | | | | Even though ISO C11 doesn't mandate in which order the type specifiers should appear, having `unsigned` at the beginning of each type declaration feels more natural and, more importantly, it unbreaks Coccinelle, which has a hard time parsing `long unsigned` and others: ``` init_defs_builtins: /usr/lib64/coccinelle/standard.h init_defs: /home/mrc0mmand/repos/systemd/coccinelle/macros.h HANDLING: src/shared/mount-util.c : 1: strange type1, maybe because of weird order: long unsigned ``` Most of the codebase already "complies", so let's fix the remaining "offenders".
* process-util: Fix memory leakDaan De Meyer2021-12-091-3/+6
|
* process-util: handle double NUL as the end of command lineYu Watanabe2021-12-061-0/+6
| | | | Fixes #21186.
* process-util: rename function arguments for storing resultsYu Watanabe2021-12-061-32/+35
|
* Merge pull request #21487 from DaanDeMeyer/dissect-image-other-archLennart Poettering2021-11-251-4/+4
|\ | | | | Allow dissect_image() to dissect images from architectures other than the native one
| * basic: Rename SECONDARY_ARCHITECTURE to ARCHITECTURE_SECONDARYDaan De Meyer2021-11-241-4/+4
| | | | | | | | | | For easier integration with the _GPT_ALL_ARCHES macro in a future commit.
* | shared: split out ioprio related stuff into ioprio-util.[ch]Lennart Poettering2021-11-241-25/+0
|/ | | | No actual code changes, just some splitting out.
* process-util: add missing NULL initialization for _cleanup_ variableLennart Poettering2021-11-231-1/+1
| | | | This is will otherwise trigger a bad memory access in the error path
* tree-wide: use new RET_NERRNO() helper at various placesLennart Poettering2021-11-161-2/+2
|
* process-util: use quote_command_line() at one more placeLennart Poettering2021-11-111-12/+1
|
* process-util: wait for processes we killed even if killing failedLennart Poettering2021-11-031-4/+4
| | | | | The processes might be zombies in which case killing will fail, but reaping them still matters.
* exec-util: use close_all_fds_without_malloc() from freeze()Lennart Poettering2021-10-271-2/+4
|
* Revert "basic/fd-util: sort the 'except' array in place"Lennart Poettering2021-10-271-4/+3
| | | | This reverts commit 9c46228b7deb53d6384545535b37b2844a102b2b.
* Revert "Add variant of close_all_fds() that does not allocate and use it in ↵Lennart Poettering2021-10-271-1/+1
| | | | | | freeze()" This reverts commit cbcf371abc328167fa869721c1add4850c793240.
* process-util: move sync() out of freeze()Lennart Poettering2021-10-221-2/+0
| | | | | | | | We are using this for creating userns namespaces, and we really shouldn't try to sync there. Moreover the use of free() in shutdown code doesn't need it anyway, since it just sync()ed right before anyway. Only the third user of freeze() we have actually needs the syc(), hence do it there and nowhere else.
* basic: move freeze() from shared/exec-util.h to basic/process-util.hLennart Poettering2021-10-221-0/+21
| | | | | 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.
* process-util: add helper for querying oom score adjustment valueLennart Poettering2021-10-041-0/+18
|
* Get rid of ioprio.h and add a minimalistic reimplementation of the apiZbigniew Jędrzejewski-Szmek2021-09-221-1/+0
|
* tree-wide: mark set-but-not-used variables as unused to make LLVM happyFrantisek Sumsal2021-09-151-1/+1
| | | | | | | | | | | | | | LLVM 13 introduced `-Wunused-but-set-variable` diagnostic flag, which trips over some intentionally set-but-not-used variables or variables attached to cleanup handlers with side effects (`_cleanup_umask_`, `_cleanup_(notify_on_cleanup)`, `_cleanup_(restore_sigsetp)`, etc.): ``` ../src/basic/process-util.c:1257:46: error: variable 'saved_ssp' set but not used [-Werror,-Wunused-but-set-variable] _cleanup_(restore_sigsetp) sigset_t *saved_ssp = NULL; ^ 1 error generated. ```
* Revert "Make oom_score_adjust_is_valid() static"Lennart Poettering2021-07-281-0/+4
| | | | This reverts commit 6bf3c6c9007ca87376d5dff1e029186a38736cdc.
* Move freeze() into shared/Zbigniew Jędrzejewski-Szmek2021-07-231-24/+0
| | | | | | | | 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-76/+0
| | | | | | | 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/.