Commit graph

1478 commits

Author SHA1 Message Date
Lennart Poettering 665dfe9318 io-util: make flush_fd() return how many bytes where flushed
This is useful so that callers know whether anything at all and how much
was flushed.

This patches through users of this functions to ensure that the return
values > 0 which may be returned now are not propagated in public APIs.

Also, users that ignore the return value are changed to do so explicitly
now.
2018-01-05 13:55:08 +01:00
Zbigniew Jędrzejewski-Szmek bbc79ae5c9
Merge pull request #7767 from poettering/fork-wait
A variety of smaller improvements all over the place
2018-01-04 15:25:07 +01:00
Zbigniew Jędrzejewski-Szmek 963c6c90af coverity: don't use (void)0 under coverity
I'm not sure why this is needed, but apparrently coverity doesn't like
(void)0. With this change, coverity can (almost) build systemd:

CFLAGS='-D_Float128="long double"' meson cov-build -Dman=false && \
  CCACHE_DISABLE=1 COVERITY_UNSUPPORTED=1 cov-build --dir cov-int ninja -C cov-build

Patch originially by Marek Cermak <macermak@redhat.com>.
2018-01-04 13:44:39 +01:00
Lennart Poettering 99c61f6b00 fileio: minor tweak to executable_is_script()
If read_line() returns ENOBFUS this means the line was overly long. When
we use this for checking whether an executable is a script, then this
shouldn't be propagated as-is, but simply as "this is not a script".
2018-01-04 13:28:24 +01:00
Lennart Poettering 91dc2bf74d fileio: tweak write_string_stream_ts() to write out trailing \n in one go even if buffering is off
This tweaks write_string_stream_ts() in one minor way: when stdio
buffering has been turned off, let's append the newline we shall append
to the buffer we write ourselves so that the kernel only gets one
syscall for the result. When buffering is enabled stdio will take care
of that anyway.

Follow-up for #7750.
2018-01-04 13:28:24 +01:00
Lennart Poettering 217d896782 socket-util: slight rework of getpeersec()
Let's call getsockopt() in a loop, so that we can deal correctly with
the label changing while we are trying to read it.

(also, while we are at it, let's make sure that there's always one
trailing NUL byte at the end of the buffer, after all SO_PEERSEC has
zero documentation, and multiple implementing backends, hence let's
better be safe than sorry)
2018-01-04 13:28:24 +01:00
Lennart Poettering bbcc701ee7 tree-wide: use {pid,uid,gid}_is_valid() where appropriate
Also, drop UID/GID validity checks from getpeercred() as the kernel will
never pass us invalid UID/GID on userns, but the overflow UID/GID
instead. Add a comment about this.
2018-01-04 13:27:54 +01:00
Lennart Poettering 10e859a2f3 calenderspec: when normalizing, flush out UTC timezone 2018-01-04 13:27:27 +01:00
Lennart Poettering 2dc894541f user-util: minor updates of in_gid()
Let's use newa() and use the right type for storing sysconf() return
values.
2018-01-04 13:27:27 +01:00
Lennart Poettering 43f2c88df0 socket-util: add new getpeergroups() call
It's a wrapper around the new SO_PEERGROUPS sockopt, similar in style as
getpeersec() and getpeercred().
2018-01-04 13:27:27 +01:00
Lennart Poettering 5e9f01e8a6 tree-wide: in all threads we fork off in library code, block all signals
This ensures that in all threads we fork off in the background in our
code we mask out all signals, so that our thread won't end up getting
signals delivered the main process should be getting.

We always set the signal mask before forking off the thread, so that the
thread has the right mask set from its earliest existance on.
2018-01-04 13:27:27 +01: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 2e87a1fde9 tree-wide: make use of wait_for_terminate_and_check() at various places
Using wait_for_terminate_and_check() instead of wait_for_terminate()
let's us simplify, shorten and unify the return value checking and
logging of waitid().  Hence, let's use it all over the place.
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 66fb11423b exec-util: drop redundant log message in do_spawn()
safe_fork() logs that anyway, hence no need to do this twice.
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 fb92330ccc
Merge pull request #7759 from yuwata/dbus-api
DBus-API: add more options in transient units
2018-01-03 18:41:32 +01:00
Lennart Poettering 7f9ac71c76
Merge pull request #7705 from keszybz/redo-linking
Redo linking
2018-01-03 18:37:00 +01:00
Zbigniew Jędrzejewski-Szmek 34e221a58b Move gcrypt-util to basic/
We were including gcrypt-util.[ch] by hand in the few places where it
was used. Create a convenience library to avoid compiling the same
files multiple times.

v2:
- use a separate static library instead of mergin into libbasic
2018-01-03 12:09:45 +01:00
Zbigniew Jędrzejewski-Szmek 2d4ceca8a5 meson: link libbasic and libshared_static into libshared
gcrypt_util_sources had to be moved because otherwise they appeared twice
in libshared.so halfproducts, causing an error.

-fvisibility=default is added to libbasic, libshared_static so that the symbols
appear properly in the exported symbol list in libshared.

The advantage is that files are not compiled twice. When configured with -Dman=false,
the ninja target list is reduced from 1588 to 1347 targets. The difference in compilation
time is small (<10%). I think this is because of -O0 and ccache and multiple cores, and
in different settings the compilation time could be reduced. The main advantage is that
errors and warnings are not reported twice.
2018-01-03 12:09:14 +01:00
Mike Gilbert 8481e3e71e basic: detect_vm_cpuid: fix hypervisor detection
The __get_cpuid() function only calls __cpuid() if __get_cpuid_max()
returns a value that is less than or equal to the leaf value.

In QEMU/KVM, I found that the special hypervisor leaf value (0x40000000U)
is always larger than the value retured by __get_cpuid_max().

Avoid this problem by calling the __cpuid() macro directly once we have
checked the hypervisor bit from leaf 1.

Fixes: d31b0033b7
2018-01-02 12:12:57 +01:00
Yu Watanabe 33d12153da basic: introduce *_to_string_with_check() functions
They are used in later commits.
2018-01-02 02:23:24 +09:00
Yu Watanabe 6f90844fe2 socket-util: introduce parse_socket_address_bind_ipv6_only_or_bool() 2018-01-02 02:23:21 +09:00
Lennart Poettering b954c051c2
Merge pull request #7755 from floppym/fileio-error
fileio: write_string_stream_ts: return errors from fputs and fputc
2017-12-30 12:21:53 +01:00
Mike Gilbert d31b0033b7 basic: detect_vm_cpuid: use gcc's __get_cpuid() function (#7758)
The __get_cpuid() function includes a safety check to ensure that
executing the cpuid instruction is valid/safe.

This method also works with clang.

https://lists.freedesktop.org/archives/systemd-devel/2017-December/040054.html
2017-12-29 19:30:38 +01:00
Yu Watanabe 5543b2b2c9
Merge pull request #7745 from poettering/sockaddr-size
mostly systemd-analyze fixes
2017-12-29 23:13:53 +09:00
Mike Gilbert ba8b8c9e40 fileio: write_string_stream_ts: check for file errors immediately 2017-12-29 08:45:30 -05:00
Mike Gilbert 94d3b60ff6 fileio: write_string_stream_ts: return errors from fputs and fputc
Ignoring errors from these functions may mask errors returned by the
kernel.

Fixes: https://github.com/systemd/systemd/issues/7744
2017-12-29 08:45:30 -05:00
Lennart Poettering 68c58c67b5 condition: extend ConditionKernelVersion= with relative version checks
Now that we have str_verscmp() in our source tree anyway, let's make it
generic and reuse it for ConditionKernelVersion=.
2017-12-26 17:43:29 +01:00
Yu Watanabe 95f7f85d39
Merge pull request #7728 from poettering/fork-rework
some fork() reworking
2017-12-27 01:32:46 +09:00
Lennart Poettering f6aac5bf1b socket-util: clarify why sockaddr_port returns unsigned rather than uint16_t 2017-12-26 13:35:52 +01:00
bleep_blop 7629744a3d separate flags from shebang 2017-12-25 19:48:49 +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 18c528e99f basic: split out blockdev-util.[ch] from util.h
With three functions it makes sense to split this out now.
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 fa7ff4cf03 tree-wide: properly name all threads we fork off 2017-12-25 11:48:21 +01:00
Lennart Poettering d00c263143 shutdown: unify shutdown.c's and async.c's sync() helper process
The helper processes are pretty much the same now, let's unify them
hence.
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 d8caff6db6 terminal-util: open /dev/null with O_CLOEXEC in make_stdio_null()
Ultimately, O_CLOEXEC should be off in fd 0, 1, 2, but when we open
/dev/null here it's unlikely to be < 0, and after dupping the fd to 0,
1, 2 we turn off O_CLOEXEC explicitly anyway.

Unless we know that what we are about to open will return 0, 1 or 2 we
should always set O_CLOEXEC in order to be safe to other threads forking
of subprocesses at the wrong moment.
2017-12-25 11:48:21 +01:00
Lennart Poettering 395195bb42 terminal-util: return first error, not last in make_stdio()
Just a minor tweak, making sure we execute as much as we can of the
funciton, but return the first error instead of the last we encounter.

This is usuelly how we do things when we have functions that continue on
the first error, so let's do it like that here too.
2017-12-25 11:48:21 +01:00
Lennart Poettering e43bc9f526 fd-util: use close_nointr() return value instead of errno
Our own calls return errors in their return values, hence use that
rather than errno when checking errors.
2017-12-25 11:48:21 +01:00
Lennart Poettering 61ccf77238 fd-util: add some (void) casts 2017-12-25 11:48:21 +01:00
Lennart Poettering 41bd3379ef sync: fork off sync() in a process instead of a thread
Let's fork off sync() ina process instead of a thread, as a safety
measure. This is beneficial to ensure that the original process can exit
without having to wait for the sync() to finish (note that the kernel
will delay process termination until all threads finished their
syscalls). In case of hanging NFS this increases the chance that PID 1
can safely transition to the "systemd-shutdown" process as the sync() is
initiated early on but definitely not waited for.
2017-12-25 11:48:21 +01:00
Lennart Poettering ebe6ff658d
Merge pull request #7663 from keszybz/mkdir-return-value
util-lib: fix return value in mkdir_parents()
2017-12-24 11:59:58 +01:00
Lennart Poettering 7785da68e6
Merge pull request #7695 from yuwata/transient-socket
DBus-API: implement transient socket unit
2017-12-23 19:20:29 +01:00
Susant Sahani d384826f69 networkd: Add support for ipvlan L3s and flags (#7726)
This works supports to configure L3S mode and flags
such as bridge, private and vepa
2017-12-23 18:55:03 +01:00