Commit Graph

188 Commits

Author SHA1 Message Date
Lennart Poettering
7c248223eb tree-wide: use new RET_NERRNO() helper at various places 2021-11-16 08:04:09 +01:00
Lennart Poettering
5e659ffcb0 process-util: use quote_command_line() at one more place 2021-11-11 10:06:26 +01:00
Lennart Poettering
2c1612100d process-util: wait for processes we killed even if killing failed
The processes might be zombies in which case killing will fail, but
reaping them still matters.
2021-11-03 15:57:02 +01:00
Lennart Poettering
ab27b2fe56 exec-util: use close_all_fds_without_malloc() from freeze() 2021-10-27 17:56:36 +02:00
Lennart Poettering
c85cb3bc7f Revert "basic/fd-util: sort the 'except' array in place"
This reverts commit 9c46228b7d.
2021-10-27 17:56:36 +02:00
Lennart Poettering
e7e7c07c50 Revert "Add variant of close_all_fds() that does not allocate and use it in freeze()"
This reverts commit cbcf371abc.
2021-10-27 17:56:36 +02:00
Lennart Poettering
43cac49092 process-util: move sync() out of freeze()
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.
2021-10-22 16:37:10 +02:00
Lennart Poettering
8ddefb8eef basic: move freeze() from shared/exec-util.h to basic/process-util.h
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.
2021-10-22 16:34:30 +02:00
Lennart Poettering
2c37c613a7 process-util: add helper for querying oom score adjustment value 2021-10-04 16:27:10 +02:00
Zbigniew Jędrzejewski-Szmek
5bead76e46 Get rid of ioprio.h and add a minimalistic reimplementation of the api 2021-09-22 12:58:47 +02:00
Frantisek Sumsal
d7ac09520b tree-wide: mark set-but-not-used variables as unused to make LLVM happy
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.
```
2021-09-15 13:09:45 +02:00
Lennart Poettering
c4412d4d33 Revert "Make oom_score_adjust_is_valid() static"
This reverts commit 6bf3c6c900.
2021-07-28 18:39:41 +02:00
Zbigniew Jędrzejewski-Szmek
45a68ed307 Move freeze() into shared/
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.
2021-07-23 11:39:45 +02:00
Zbigniew Jędrzejewski-Szmek
3e24e8cd64 Move fork_agent() into shared/
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/.
2021-07-23 11:39:45 +02:00
Zbigniew Jędrzejewski-Szmek
3bd6a01c01 basic/process-util: use xsprintf() in one more place 2021-07-23 11:39:45 +02:00
Zbigniew Jędrzejewski-Szmek
6bf3c6c900 Make oom_score_adjust_is_valid() static
It has only one user and we don't need to put it in basic/.
2021-07-23 11:39:45 +02:00
Zbigniew Jędrzejewski-Szmek
9c46228b7d basic/fd-util: sort the 'except' array in place
We need a sorted list of fds to skip over when closing. We would allocate a
copy of the passed array to do the sort. But all callers construct a temporary
array to pass to us, so it is pointless to copy it again.

close_all_fds/safe_fork_full/namespace_fork/fork_agent are changed to pass
a non-const int array. I checked all users, and all callers are fine with
the array being sorted.

The function was returning some number (sometimes 1, sometimes the extent
of the range passed over to close_range(), ???). Anyway, all callers only
check for error, so let's return 0 on success.
2021-07-23 11:37:44 +02:00
Lennart Poettering
0c4d1e6d96 process-util: explicitly handle processes lacking parents in get_process_ppid()
Let's make sure we signal out-of-band via an error message if a process
doesn't have a parent process whose PID we could return. Otherwise we'll
too likely hide errors, as we return an invalid PID 0, which in other
contexts has special meaning (i.e. usually "myself").

Replaces: #20153

This is based on work by @dtardon, but goes a different route, by
ensuring we propagate a proper error in this case.

This modernizes the function in question a bit in other ways, i.e.
renames stuff and makes the return parameter optional.
2021-07-07 18:41:08 +01:00
Yu Watanabe
7802194ac0 tree-wide: add missing whitespace at the end of comments 2021-06-15 14:09:29 +09:00
Lennart Poettering
319a4f4bc4 alloc-util: simplify GREEDY_REALLOC() logic by relying on malloc_usable_size()
We recently started making more use of malloc_usable_size() and rely on
it (see the string_erase() story). Given that we don't really support
sytems where malloc_usable_size() cannot be trusted beyond statistics
anyway, let's go fully in and rework GREEDY_REALLOC() on top of it:
instead of passing around and maintaining the currenly allocated size
everywhere, let's just derive it automatically from
malloc_usable_size().

I am mostly after this for the simplicity this brings. It also brings
minor efficiency improvements I guess, but things become so much nicer
to look at if we can avoid these allocation size variables everywhere.

Note that the malloc_usable_size() man page says relying on it wasn't
"good programming practice", but I think it does this for reasons that
don't apply here: the greedy realloc logic specifically doesn't rely on
the returned extra size, beyond the fact that it is equal or larger than
what was requested.

(This commit was supposed to be a quick patch btw, but apparently we use
the greedy realloc stuff quite a bit across the codebase, so this ends
up touching *a*lot* of code.)
2021-05-19 16:42:37 +02:00
Yu Watanabe
19cc6d5e54 tree-wide: fix typo 2021-05-09 14:36:19 +09:00
Lennart Poettering
5f9687363a process-util: add option for cloning with CLONE_NEWUSER
This is useful for allocating a userns fd later on for use in idmapped
mounts.
2021-05-07 22:43:42 +02:00
Zbigniew Jędrzejewski-Szmek
99009ed0f4 basic/process-util: add mode where posix shell escape is used for quoting
The new flag is not used, except in tests, so no functional change yet.

This way, the command as shown can be copied-and-pasted into the shell
in more cases. For simple cases, shell quoting with "" is enough. But
$'' is needed when there are control characters in the command.
2021-05-05 13:59:23 +02:00
Zbigniew Jędrzejewski-Szmek
61977664e9 basic/process-util: allow quoting of commandlines
Since the new functionality is controlled by an option, this causes no change
in output yet, except tests.

The login in the old branch of !(flags & PROCESS_CMDLINE_QUOTE) is essentially
unmodified. But there is an important difference in behaviour: instead of
unconditionally reading the whole virtual file, we now read only 'max_columns'
bytes. This makes out code to write process lists quite a bit more efficient
when there are processes with long command lines.
2021-05-05 13:59:23 +02:00
Zbigniew Jędrzejewski-Szmek
b19f211698 basic/escape: flagsify xescape_full() 2021-05-05 13:59:23 +02:00