Commit Graph

88 Commits

Author SHA1 Message Date
Lennart Poettering ae2a15bc14 macro: introduce TAKE_PTR() macro
This macro will read a pointer of any type, return it, and set the
pointer to NULL. This is useful as an explicit concept of passing
ownership of a memory area between pointers.

This takes inspiration from Rust:

https://doc.rust-lang.org/std/option/enum.Option.html#method.take

and was suggested by Alan Jenkins (@sourcejedi).

It drops ~160 lines of code from our codebase, which makes me like it.
Also, I think it clarifies passing of ownership, and thus helps
readability a bit (at least for the initiated who know the new macro)
2018-03-22 20:21:42 +01:00
Evgeny Vereshchagin e4711004d6
Merge pull request #8461 from keszybz/oss-fuzz-fixes
Oss fuzz fixes
2018-03-19 00:06:44 +03:00
Zbigniew Jędrzejewski-Szmek 10062bbc35 unit-file: do not allow bogus IOSchedulingClass values
We have only three bits of space, i.e. 8 possible classes. Immediately reject
anything outside of that range. Add the fuzzer test case and an additional
unit test.

oss-fuzz #6908.
2018-03-17 09:01:53 +01:00
Franck Bui 848e863acc basic/macros: rename noreturn into _noreturn_ (#8456)
"noreturn" is reserved and can be used in other header files we include:

  [   16s] In file included from /usr/include/gcrypt.h:30:0,
  [   16s]                  from ../src/journal/journal-file.h:26,
  [   16s]                  from ../src/journal/journal-vacuum.c:31:
  [   16s] /usr/include/gpg-error.h:1544:46: error: expected ‘,’ or ‘;’ before ‘)’ token
  [   16s]  void gpgrt_log_bug (const char *fmt, ...)    GPGRT_ATTR_NR_PRINTF(1,2);

Here we include grcrypt.h (which in turns include gpg-error.h) *after* we
"noreturn" was defined in macro.h.
2018-03-15 14:23:46 +09:00
Lennart Poettering 5d71bac3b9 process-util: don't install atfork() handler more than once 2018-02-28 10:01:16 +01:00
Lennart Poettering e7685a77b4 util: add new safe_close_above_stdio() wrapper
At various places we only want to close fds if they are not
stdin/stdout/stderr, i.e. fds 0, 1, 2. Let's add a unified helper call
for that, and port everything over.
2018-02-28 10:00:50 +01:00
Zbigniew Jędrzejewski-Szmek 730f40eb57 Merge pull request #8184 from poettering/color-ask-pw
Trivial merge conflict resolved locally.
2018-02-15 17:14:59 +01:00
Lennart Poettering e70f445306 process: shortcut getenv_for_pid() for our own process 2018-02-14 14:27:31 +01:00
Lennart Poettering 36b5119a0c process-util: be more careful in is_kernel_thread()
This reworks is_kernel_thread() a bit. Instead of checking whether
/proc/$pid/cmdline is entirely empty we now parse the 'flags' field from
/proc/$pid/stat and check the PF_KTHREAD flag, which directly encodes
whether something is a kernel thread.

Why all this? With current kernels userspace processes can set their
command line to empty too (through PR_SET_MM_ARG_START and friends), and
could potentially confuse us. Hence, let's use a more reliable way to
detect kernels like this.
2018-02-12 11:34:00 +01:00
Shawn Landden 392cf1d05d sd-bus: cleanup ssh sessions (Closes: #8076)
we still invoke ssh unnecessarily when there in incompatible or erreneous input
The fallow-up to finish that would make the code a bit more verbose,
as it would require repeating this bit:
```
        r = bus_connect_transport(arg_transport, arg_host, false, &bus);
        if (r < 0) {
                log_error_errno(r, "Failed to create bus connection: %m");
                goto finish;
        }

        sd_bus_set_allow_interactive_authorization(bus, arg_ask_password);
```
in every verb, after parsing.

v2: add waitpid() to avoid a zombie process, switch to SIGTERM from SIGKILL
v3: refactor, wait in bus_start_address()
2018-02-08 10:14:48 -08:00
Lennart Poettering 996def17f9 process-util: use raw_getpid() in getpid_cache() internally (#8115)
We have the raw_getpid() definition in place anyway, and it's certainly
beneficial to expose the same semantics on pre glibc 2.24 and after it
too, hence always bypass glibc for this, and always cache things on our
side.

Fixes: #8113
2018-02-07 11:10:09 +09:00
Michal Sekletar 8647283e45 process-util: make our freeze() routine do something useful
When we crash we freeze() our-self (or possibly we reboot the machine if
that is configured). However, calling pause() is very unhelpful thing to
do. We should at least continue to do what init systems being doing
since 70's and that is reaping zombies. Otherwise zombies start to
accumulate on the system which is a very bad thing. As that can prevent
admin from taking manual steps to reboot the machine in somewhat
graceful manner (e.g. manually stopping services, unmounting data
volumes  and calling reboot -f).

Fixes #7783
2018-01-12 17:50:02 +01:00
Lennart Poettering dfd14786b5 util-lib: save/restore errno in cleanup calls
We should be careful with errno in cleanup functions, and not alter it
under any circumstances. In the safe_close cleanup handlers we are
already safe in that regard, but let's add similar protections on other
cleanup handlers that invoke system calls.

Why bother? Cleanup handlers insert code at function return in
non-obvious ways. Hence, code that sets errno and returns should not be
confused by us overrding the errno from a cleanup handler.

This is a paranoia fix only, I am not aware where this actually mattered
in real-life situations.
2018-01-10 17:21:15 +01:00
Yu Watanabe a41f6217ba process-util: initialize block_signals
CID 1384240.
2018-01-10 20:52:46 +09:00
Lennart Poettering be39f6ee31 process-util: add new FORK_NEW_MOUNTNS flag to safe_fork()
That way we can move one more code location to use safe_fork()
2018-01-04 13:27:27 +01:00
Lennart Poettering 1f5d1e0247 process-spec: add another flag FORK_WAIT to safe_fork()
This new flag will cause safe_fork() to wait for the forked off child
before returning. This allows us to unify a number of cases where we
immediately wait on the forked off child, witout running any code in the
parent after the fork, and without direct interest in the precise exit
status of the process, except recgonizing EXIT_SUCCESS vs everything
else.
2018-01-04 13:27:27 +01:00
Lennart Poettering 7d4904fe7a process-util: rework wait_for_terminate_and_warn() to take a flags parameter
This renames wait_for_terminate_and_warn() to
wait_for_terminate_and_check(), and adds a flags parameter, that
controls how much to log: there's one flag that means we log about
abnormal stuff, and another one that controls whether we log about
non-zero exit codes. Finally, there's a shortcut flag value for logging
in both cases, as that's what we usually use.

All callers are accordingly updated. At three occasions duplicate logging
is removed, i.e. where the old function was called but logged in the
caller, too.
2018-01-04 13:27:27 +01:00
Lennart Poettering b6e1fff13d process-util: add another fork_safe() flag for enabling LOG_ERR/LOG_WARN logging 2018-01-04 13:27:26 +01:00
Lennart Poettering 799a960d1f raw-clone: beef up raw_clone() wrapper a bit
First of all, let's return pid_t, which appears to be the correct type
given that we return PIDs, and it#s what fork() uses too.

Most importantly though, flush out our PID cache, so that the call
becomes compatible with our getpid_cached() logic.
2018-01-04 13:27:26 +01:00
Shawn Landden 7ddc2dc5e6 fix machinectl shell (in machined) (#7785)
4c253ed broke machined
$machinectl shell arch
Failed to get shell PTY: Input/output error

Closes: #7779

v2: do not drop DEATHSIG flag
2018-01-04 12:37:15 +01:00
Lennart Poettering 5a8af74711 process-util: debug log if PR_SET_NAME fails. 2017-12-25 11:48:21 +01:00
Lennart Poettering 1096bb8a9e process-util: allow rename_process() only in the main thread
We make assumptions about the comm name we set via PR_SET_NAME: that it
would reflect the process name, but that's only the case for the main
thread. Moreover, we cache the mmap() region without locking.

Let's hence be safe rather than sorry and support all this only in the
main thread.
2017-12-25 11:48:21 +01:00
Lennart Poettering 78752f2eb4 process-util: move fork_agent() to process-util.[ch]
It's a relatively small wrapper around safe_fork() now, hence let's move
it over, and make its signature even more alike. Also, set a different
process name for the polkit and askpw agents.
2017-12-25 11:48:21 +01:00
Lennart Poettering 4c253ed1ca tree-wide: introduce new safe_fork() helper and port everything over
This adds a new safe_fork() wrapper around fork() and makes use of it
everywhere. The new wrapper does a couple of things we previously did
manually and separately in a safer, more correct and automatic way:

1. Optionally resets signal handlers/mask in the child

2. Sets a name on all processes we fork off right after forking off (and
   the patch assigns useful names for all processes we fork off now,
   following a systematic naming scheme: always enclosed in () – in order
   to indicate that these are not proper, exec()ed processes, but only
   forked off children, and if the process is long-running with only our
   own code, without execve()'ing something else, it gets am "sd-" prefix.)

3. Optionally closes all file descriptors in the child

4. Optionally sets a PR_SET_DEATHSIG to SIGTERM in the child, in a safe
   way so that the parent dying before this happens being handled
   safely.

5. Optionally reopens the logs

6. Optionally connects stdin/stdout/stderr to /dev/null

7. Debug logs about the forked off processes.
2017-12-25 11:48:21 +01:00
Lennart Poettering f78a88beca
Merge pull request #6598 from kyle-walker/shutdown-limit
core: Limit the time and attempts in shutdown remount/umount efforts
2017-12-14 22:57:57 +01:00
Kyle Walker d5641e0d7e core: Implement timeout based umount/remount limit
Remount, and subsequent umount, attempts can hang for inaccessible network
based mount points. This can leave a system in a hard hang state that
requires a hard reset in order to recover. This change moves the remount,
and umount attempts into separate child processes. The remount and umount
operations will block for up to 90 seconds (DEFAULT_TIMEOUT_USEC). Should
those waits fail, the parent will issue a SIGKILL to the child and continue
with the shutdown efforts.

In addition, instead of only reporting some additional errors on the final
attempt, failures are reported as they occur.
2017-12-14 08:58:56 -05:00
Lennart Poettering 35bbbf85e0 basic: turn off stdio locking for a couple of helper calls
These helper calls are potentially called often, and allocate FILE*
objects internally for a very short period of time, let's turn off
locking for them too.
2017-12-14 10:46:19 +01:00
Lennart Poettering fba868fa71 tree-wide: unify logging of "Must be root" message
Let's unify this in one call, generalizing must_be_root() from
bootctl.c.
2017-12-11 23:19:45 +01:00
Zbigniew Jędrzejewski-Szmek 53e1b68390 Add SPDX license identifiers to source files under the LGPL
This follows what the kernel is doing, c.f.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=5fd54ace4721fc5ce2bb5aef6318fcf17f421460.
2017-11-19 19:08:15 +01:00
Zbigniew Jędrzejewski-Szmek 03d4358277 Merge pull request #6975 from sourcejedi/logind_pid_0_v2
Selectively revert "tree-wide: use pid_is_valid() at more places"
2017-10-04 21:33:52 +02:00
Lennart Poettering 4aa1d31c89 Merge pull request #6974 from keszybz/clean-up-defines
Clean up define definitions
2017-10-04 19:25:30 +02:00
Yu Watanabe 4c70109600 tree-wide: use IN_SET macro (#6977) 2017-10-04 16:01:32 +02:00
Zbigniew Jędrzejewski-Szmek 349cc4a507 build-sys: use #if Y instead of #ifdef Y everywhere
The advantage is that is the name is mispellt, cpp will warn us.

$ git grep -Ee "conf.set\('(HAVE|ENABLE)_" -l|xargs sed -r -i "s/conf.set\('(HAVE|ENABLE)_/conf.set10('\1_/"
$ git grep -Ee '#ifn?def (HAVE|ENABLE)' -l|xargs sed -r -i 's/#ifdef (HAVE|ENABLE)/#if \1/; s/#ifndef (HAVE|ENABLE)/#if ! \1/;'
$ git grep -Ee 'if.*defined\(HAVE' -l|xargs sed -i -r 's/defined\((HAVE_[A-Z0-9_]*)\)/\1/g'
$ git grep -Ee 'if.*defined\(ENABLE' -l|xargs sed -i -r 's/defined\((ENABLE_[A-Z0-9_]*)\)/\1/g'
+ manual changes to meson.build

squash! build-sys: use #if Y instead of #ifdef Y everywhere

v2:
- fix incorrect setting of HAVE_LIBIDN2
2017-10-04 12:09:29 +02:00
Alan Jenkins 07b38ba51e Revert "tree-wide: use pid_is_valid() at more places"
This reverts commit ee043777be.

It broke almost everywhere it touched.  The places that
handn't been converted, were mostly followed by special
handling for the invalid PID `0`.  That explains why they
tested for `pid < 0` instead of `pid <= 0`.

I think that one was the first commit I reviewed, heh.
2017-10-03 12:43:24 +01:00
Andreas Rammhold 3742095b27
tree-wide: use IN_SET where possible
In addition to the changes from #6933 this handles cases that could be
matched with the included cocci file.
2017-10-02 13:09:54 +02:00
Zbigniew Jędrzejewski-Szmek a4041e4f68 Link to the right glibc commit in comment (#6884)
Reported by Marcos Mello.

Fixes #6882.
2017-09-21 20:54:16 +02:00
Lennart Poettering 21022b9dde util-lib: wrap personality() to fix up broken glibc error handling (#6766)
glibc appears to propagate different errors in different ways, let's fix
this up, so that our own code doesn't get confused by this.

See #6752 + #6737 for details.

Fixes: #6755
2017-09-08 17:16:29 +03:00
Lennart Poettering ee043777be tree-wide: use pid_is_valid() at more places 2017-08-31 15:45:04 +02:00
Lennart Poettering e8132d63fe seccomp: default to something resembling the current personality when locking it
Let's lock the personality to the currently set one, if nothing is
specifically specified. But do so with a grain of salt, and never
default to any exotic personality here, but only PER_LINUX or
PER_LINUX32.
2017-08-29 15:56:57 +02:00
Jouke Witteveen 01f989c662 process-util: update the end pointer of the process name on rename (#6492)
We only updated the end pointer when allocating new memory, i.e. on the first
call to rename_process.
2017-08-04 11:25:49 +02:00
Lennart Poettering 6f8cbcdb27 process-util: slightly optimize querying of our own process metadata
When we are checking our own data, we can optimize things a bit.
2017-07-31 18:20:28 +02:00
Lennart Poettering df0ff12775 tree-wide: make use of getpid_cached() wherever we can
This moves pretty much all uses of getpid() over to getpid_raw(). I
didn't specifically check whether the optimization is worth it for each
replacement, but in order to keep things simple and systematic I
switched over everything at once.
2017-07-20 20:27:24 +02:00
Lennart Poettering 5c30a6d2b8 process-util: add getpid_cached() as a caching wrapper for getpid()
Let's make getpid() fast again.
2017-07-20 20:27:24 +02:00
Lennart Poettering 7f452159b8 core: make IOSchedulingClass= and IOSchedulingPriority= settable for transient units
This patch is a bit more complex thant I hoped. In particular the single
IOScheduling= property exposed on the bus is split up into
IOSchedulingClass= and IOSchedulingPriority= (though compat is
retained). Otherwise the asymmetry between setting props and getting
them is a bit too nasty.

Fixes #5613
2017-06-26 17:43:18 +02:00
Zbigniew Jędrzejewski-Szmek e3f791a2b3 basic/path-util: allow flags for path_equal_or_files_same
No functional change, just a new parameters and the tests that
AT_SYMLINK_NOFOLLOW works as expected.
2017-06-17 12:37:16 -04:00
AsciiWolf 13e785f7a0 Fix missing space in comments (#5439) 2017-02-24 18:14:02 +01:00
stuart-mclaren a3d8d68cc1 Fix check for signal in set (#5416)
IN_SET(SIGCONT, SIGKILL) will always evaluate to false.
The signal needs to be included as the first argument.

Fixup for 26f417d3e8.
2017-02-21 15:39:52 -05:00
Zbigniew Jędrzejewski-Szmek c05347807f process-util: rename char *r to ans and add comment
Add a comment about the return value and rename r to ans. r is
nowadays reserved for the integer return value, and char *r is confusing.
2017-01-15 12:41:34 -05:00
Lennart Poettering 9bfaffd5a9 util-lib: rework rename_process() to be able to make use of PR_SET_MM_ARG_START
PR_SET_MM_ARG_START allows us to relatively cleanly implement process renaming.
However, it's only available with privileges. Hence, let's try to make use of
it, and if we can't fall back to the traditional way of overriding argv[0].

This removes size restrictions on the process name shown in argv[] at least for
privileged processes.
2016-12-14 18:29:30 +01:00
Zbigniew Jędrzejewski-Szmek 041b5ae170 basic/process-util: we need to take the shorter of two strings
==30496== Conditional jump or move depends on uninitialised value(s)
==30496==    at 0x489F654: memcmp (vg_replace_strmem.c:1091)
==30496==    by 0x49BF203: getenv_for_pid (process-util.c:678)
==30496==    by 0x4993ACB: detect_container (virt.c:442)
==30496==    by 0x182DFF: test_get_process_comm (test-process-util.c:98)
==30496==    by 0x185847: main (test-process-util.c:368)
==30496==
2016-11-17 20:57:22 -05:00