chiark / gitweb /
elogind.git
6 years agologind: cast away return value we don't care about
Lennart Poettering [Fri, 2 Mar 2018 10:55:33 +0000 (11:55 +0100)]
logind: cast away return value we don't care about

6 years agologind: voidify a function we never check the return value of
Lennart Poettering [Fri, 2 Mar 2018 10:55:16 +0000 (11:55 +0100)]
logind: voidify a function we never check the return value of

6 years agomount-setup: change bpf mount mode to 0700 (#8334)
Lennart Poettering [Fri, 2 Mar 2018 11:55:24 +0000 (12:55 +0100)]
mount-setup: change bpf mount mode to 0700 (#8334)

After discussing with the kernel folks, we agreed to default to 0700 for
this. Better safe than sorry.

6 years agoRemove /sbin from paths if split-bin is false (#8324)
Zbigniew Jędrzejewski-Szmek [Thu, 1 Mar 2018 20:48:36 +0000 (21:48 +0100)]
Remove /sbin from paths if split-bin is false (#8324)

Follow-up for 157baa87e4.

6 years agomeson: support both separate and merged sbin-bin directories
Zbigniew Jędrzejewski-Szmek [Thu, 1 Mar 2018 09:28:29 +0000 (10:28 +0100)]
meson: support both separate and merged sbin-bin directories

Follow-up for ba7f4ae6178309dc937e10cf7dce0eca9dafb8de.

By default, we detect if the real root has a separate /usr/sbin directory, but
this can be overrides with -Dsplit-bin=true|false. The check assumes that
/usr/sbin is split if it is not a symlink, so it'll return a false negative
with some more complicated setups. But that's OK, in those cases this should be
configured explicitly.

This will copy the structure of the directories in the root file system to
$DESTDIR. If a directory is a directory in $DESTDIR but a symlink in the root
file system, this script will fail. This means that it's not possible to reuse
a $DESTDIR from between ba7f4ae61 and this patch.

6 years agomeson: autodetect split-usr
Zbigniew Jędrzejewski-Szmek [Thu, 1 Mar 2018 10:49:42 +0000 (11:49 +0100)]
meson: autodetect split-usr

Also move the status from "features" to the paths section. This is more of an
anti-feature.

6 years agoprocfs-util: drop unnecessary zero initializations (#8321)
Lennart Poettering [Thu, 1 Mar 2018 10:27:06 +0000 (11:27 +0100)]
procfs-util: drop unnecessary zero initializations (#8321)

Follow-up for #8149.

6 years agobasic/cgroup-util: simplify cg_get_keyed_attribute(), add test
Zbigniew Jędrzejewski-Szmek [Thu, 1 Mar 2018 08:30:55 +0000 (09:30 +0100)]
basic/cgroup-util: simplify cg_get_keyed_attribute(), add test

I didn't like the nested loop where we'd count what we have acquired already,
since we should always know that.

6 years agoupdate TODO
Lennart Poettering [Fri, 9 Feb 2018 18:43:35 +0000 (19:43 +0100)]
update TODO

6 years agocore: turn on memory/cpu/tasks accounting by default for the root slice
Lennart Poettering [Fri, 9 Feb 2018 18:07:01 +0000 (19:07 +0100)]
core: turn on memory/cpu/tasks accounting by default for the root slice

The kernel exposes the necessary data in /proc anyway, let's expose it
hence by default.

With this in place "systemctl status -- -.slice" will show accounting
data out-of-the-box now.

6 years agocore: hook up /proc queries for the root slice, too
Lennart Poettering [Fri, 9 Feb 2018 18:05:59 +0000 (19:05 +0100)]
core: hook up /proc queries for the root slice, too

Do what we already prepped in cgtop for the root slice in PID 1 too:
consult /proc for the data we need.

6 years agocgroup-util: rework cg_get_keyed_attribute() a bit
Lennart Poettering [Fri, 9 Feb 2018 17:35:52 +0000 (18:35 +0100)]
cgroup-util: rework cg_get_keyed_attribute() a bit

Let's make sure we don't clobber the return parameter on failure, to
follow our coding style. Also, break the loop early if we have all
attributes we need.

This also changes the keys parameter to a simple char**, so that we can
use STRV_MAKE() for passing the list of attributes to read.

This also makes it possible to distuingish the case when the whole
attribute file doesn't exist from one key in it missing. In the former
case we return -ENOENT, in the latter we now return -ENXIO.

6 years agoprocfs-util: add APIs to get consumed CPU time and used memory from /proc
Lennart Poettering [Fri, 9 Feb 2018 16:32:26 +0000 (17:32 +0100)]
procfs-util: add APIs to get consumed CPU time and used memory from /proc

This is preparation for emulating the "usage_usec" keyed attribute of
the "cpu.stat" property of the root cgroup from data in /proc. Similar,
for emulating the "memory.current" attribute.

6 years agocore: don't process dbus unit and job queue when there are already too many messages...
Lennart Poettering [Tue, 13 Feb 2018 17:30:34 +0000 (18:30 +0100)]
core: don't process dbus unit and job queue when there are already too many messages pending

We maintain a queue of units and jobs that we are supposed to generate
change/new notifications for because they were either just created or
some of their property has changed. Let's throttle processing of this
queue a bit: as soon as > 1K of bus messages are queued for writing
let's skip processing the queue, and then recheck on the next
iteration again.

Moreover, never process more than 100 units in one go, return to the
event loop after that. Both limits together should put effective limits
on both space and time usage of the function, delaying further
operations until a later moment, when the queue is empty or the the
event loop is sufficiently idle again.

This should keep the number of generated messages much lower than
before on busy systems or where some client is hanging.

Note that this also means a bad client can slow down message dispatching
substantially for up to 90s if it likes to, for all clients. But that
should be acceptable as we only allow trusted bus clients, anyway.

Fixes: #8166
6 years agosd-bus: add APIs to query the current read and write queue size
Lennart Poettering [Tue, 13 Feb 2018 17:27:05 +0000 (18:27 +0100)]
sd-bus: add APIs to query the current read and write queue size

6 years agoprocess-util: don't install atfork() handler more than once
Lennart Poettering [Mon, 26 Feb 2018 19:50:57 +0000 (20:50 +0100)]
process-util: don't install atfork() handler more than once

6 years agoutil: add new safe_close_above_stdio() wrapper
Lennart Poettering [Mon, 26 Feb 2018 14:41:38 +0000 (15:41 +0100)]
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.

6 years agomeson: install compat symlinks for systemctl and systemd (#8300)
Zbigniew Jędrzejewski-Szmek [Wed, 28 Feb 2018 09:20:48 +0000 (10:20 +0100)]
meson: install compat symlinks for systemctl and systemd (#8300)

v2:
- init is a symlink to systemd, not systemctl!

6 years agodoc: add a new doc/ directory, and move two markdown docs into them
Lennart Poettering [Mon, 26 Feb 2018 10:48:46 +0000 (11:48 +0100)]
doc: add a new doc/ directory, and move two markdown docs into them

I figure sooneror later we'll have more of these docs, hence let's give
them a clean place to be.

This leaves NEWS and README/README.md as well as the LICENSE texts in
the root directory of the project since that appears to be customary for
Free Software projects.

6 years agorules: add a missing comma in 70-uaccess.rules since it improves readability
Franck Bui [Fri, 23 Feb 2018 15:49:17 +0000 (16:49 +0100)]
rules: add a missing comma in 70-uaccess.rules since it improves readability

rule-syntax-check.py failed with the following error:

$ ./test/rule-syntax-check.py ./src/login/70-uaccess.rules
Invalid line ./src/login/70-uaccess.rules:31: SUBSYSTEM=="sound", TAG+="uaccess"   OPTIONS+="static_node=snd/timer", OPTIONS+="static_node=snd/seq"
  clause: TAG+="uaccess"   OPTIONS+="static_node=snd/timer"

The comma is actually optional but the script makes it mandatory which seems a
good thing since it improves readability.

6 years agomissing_syscall: add pkey_mprotect for ppc (#8292)
Zbigniew Jędrzejewski-Szmek [Tue, 27 Feb 2018 12:33:00 +0000 (13:33 +0100)]
missing_syscall: add pkey_mprotect for ppc (#8292)

Accurate for both ppc and ppc64 according to https://fedora.juszkiewicz.com.pl/syscalls.html.

6 years agokhash: try to detect broken AF_ALG support in centos kernels
Lennart Poettering [Mon, 26 Feb 2018 12:46:58 +0000 (13:46 +0100)]
khash: try to detect broken AF_ALG support in centos kernels

Fixes: #8278
6 years agosd-login: make use of _cleanup_close_ where possible
Lennart Poettering [Mon, 26 Feb 2018 17:45:45 +0000 (18:45 +0100)]
sd-login: make use of _cleanup_close_ where possible

6 years agologind: make sure we don't trip up on half-initialized session devices
Lennart Poettering [Mon, 26 Feb 2018 17:34:49 +0000 (18:34 +0100)]
logind: make sure we don't trip up on half-initialized session devices

Fixes: #8035
6 years agologind: check file is device node before using .st_rdev
Lennart Poettering [Mon, 26 Feb 2018 17:34:43 +0000 (18:34 +0100)]
logind: check file is device node before using .st_rdev

6 years agologind: let's pack a few struct fields we can pack
Lennart Poettering [Mon, 26 Feb 2018 17:34:13 +0000 (18:34 +0100)]
logind: let's pack a few struct fields we can pack

6 years agologind: fd 0 is a valid fd
Lennart Poettering [Mon, 26 Feb 2018 17:33:51 +0000 (18:33 +0100)]
logind: fd 0 is a valid fd

6 years agologind: let's reduce one level of indentation
Lennart Poettering [Mon, 26 Feb 2018 17:33:20 +0000 (18:33 +0100)]
logind: let's reduce one level of indentation

6 years agologind: propagate the right error, don't make up ENOMEM
Lennart Poettering [Mon, 26 Feb 2018 17:33:05 +0000 (18:33 +0100)]
logind: propagate the right error, don't make up ENOMEM

6 years agologind: rework sd_eviocrevoke()
Lennart Poettering [Mon, 26 Feb 2018 17:32:07 +0000 (18:32 +0100)]
logind: rework sd_eviocrevoke()

Let's initialize static variables properly and get rid of redundant
variables.

6 years agologind: trivial improvements
Lennart Poettering [Mon, 26 Feb 2018 17:31:06 +0000 (18:31 +0100)]
logind: trivial improvements

Just some addition whitespace, some additional assert()s, and removal of
redundant variables.

6 years agobasic/xattr-util: do not cast ssize_t to int
Zbigniew Jędrzejewski-Szmek [Sun, 25 Feb 2018 20:25:33 +0000 (21:25 +0100)]
basic/xattr-util: do not cast ssize_t to int

gcc warns about unitialized memory access because it notices that ssize_t which
is < 0 could be cast to positive int value. We know that this can't really
happen because only -1 can be returned, but OTOH, in principle a large
*positive* value cannot be cast properly. This is unlikely too, since xattrs
cannot be too large, but it seems cleaner to just use a size_t to return the
value and avoid the cast altoghter. This makes the code simpler and gcc is
happy too.

The following warning goes away:
[113/1502] Compiling C object 'src/basic/basic@sta/xattr-util.c.o'.
In file included from ../src/basic/alloc-util.h:28:0,
                 from ../src/basic/xattr-util.c:30:
../src/basic/xattr-util.c: In function ‘fd_getcrtime_at’:
../src/basic/macro.h:207:60: warning: ‘b’ may be used uninitialized in this function [-Wmaybe-uninitialized]
                 UNIQ_T(A,aq) < UNIQ_T(B,bq) ? UNIQ_T(A,aq) : UNIQ_T(B,bq); \
                                                            ^
../src/basic/xattr-util.c:155:19: note: ‘b’ was declared here
         usec_t a, b;
                   ^

6 years agobasic/exec-util: use _exit() to return from child
Zbigniew Jędrzejewski-Szmek [Sun, 25 Feb 2018 20:07:18 +0000 (21:07 +0100)]
basic/exec-util: use _exit() to return from child

6 years agobasic: shorten the code a bit in two places
Zbigniew Jędrzejewski-Szmek [Sun, 25 Feb 2018 16:26:22 +0000 (17:26 +0100)]
basic: shorten the code a bit in two places

gcc complains that len might be used unitialized, but afaict, this is not true.

6 years agotree-wide: use reallocarray instead of our home-grown realloc_multiply (#8279)
Zbigniew Jędrzejewski-Szmek [Mon, 26 Feb 2018 20:20:00 +0000 (21:20 +0100)]
tree-wide: use reallocarray instead of our home-grown realloc_multiply (#8279)

There isn't much difference, but in general we prefer to use the standard
functions. glibc provides reallocarray since version 2.26.

I moved explicit_bzero is configure test to the bottom, so that the two stdlib
functions are at the bottom.

6 years agobasic/virt: provide a nicer message is /proc/cpuinfo is not available
Zbigniew Jędrzejewski-Szmek [Fri, 23 Feb 2018 11:49:15 +0000 (12:49 +0100)]
basic/virt: provide a nicer message is /proc/cpuinfo is not available

$ sudo systemd-run -p RootDirectory=/usr -E LD_LIBRARY_PATH=/lib/systemd/ -E SYSTEMD_LOG_LEVEL=debug /bin/systemd-detect-virt

Before
systemd-detect-virt[18498]: No virtualization found in DMI
systemd-detect-virt[18498]: No virtualization found in CPUID
systemd-detect-virt[18498]: Virtualization XEN not found, /proc/xen does not exist
systemd-detect-virt[18498]: This platform does not support /proc/device-tree
systemd-detect-virt[18498]: Failed to check for virtualization: No such file or directory

The first four lines are at debug level, so the user would only see that last
one usually, which is not very enlightening.

This now becomes:
systemd-detect-virt[21172]: No virtualization found in DMI
systemd-detect-virt[21172]: No virtualization found in CPUID
systemd-detect-virt[21172]: Virtualization XEN not found, /proc/xen does not exist
systemd-detect-virt[21172]: This platform does not support /proc/device-tree
systemd-detect-virt[21172]: /proc/cpuinfo not found, assuming no UML virtualization.
systemd-detect-virt[21172]: This platform does not support /proc/sysinfo
systemd-detect-virt[21172]: Found VM virtualization none
systemd-detect-virt[21172]: none

We do more checks, which is good too.

6 years agobasic/log: add an assert that does not recurse into logging functions
Zbigniew Jędrzejewski-Szmek [Fri, 23 Feb 2018 12:29:03 +0000 (13:29 +0100)]
basic/log: add an assert that does not recurse into logging functions

Then it can be used in the asserts in logging functions without causing
infinite recursion. The error is just printed to stderr, it should be
good enough for the common case.

6 years agoudev/net-id: check all snprintf return values
Zbigniew Jędrzejewski-Szmek [Fri, 23 Feb 2018 10:12:19 +0000 (11:12 +0100)]
udev/net-id: check all snprintf return values

gcc-8 throws an error if it knows snprintf might truncate output and the
return value is ignored:
../src/udev/udev-builtin-net_id.c: In function 'dev_pci_slot':
../src/udev/udev-builtin-net_id.c:297:47: error: '%s' directive output may be truncated writing up to 255 bytes into a region of size between 0 and 4095 [-Werror=format-truncation=]
                 snprintf(str, sizeof str, "%s/%s/address", slots, dent->d_name);
                                               ^~
../src/udev/udev-builtin-net_id.c:297:17: note: 'snprintf' output between 10 and 4360 bytes into a destination of size 4096
                 snprintf(str, sizeof str, "%s/%s/address", slots, dent->d_name);
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1: some warnings being treated as errors

Let's check all return values. This actually makes the code better, because there's
no point in trying to open a file when the name has been truncated, etc.

6 years agobasic/log: make sure header is printed correctly, add test
Zbigniew Jędrzejewski-Szmek [Thu, 22 Feb 2018 22:55:14 +0000 (23:55 +0100)]
basic/log: make sure header is printed correctly, add test

If log_do_header() was called with overly long parameters, it'd generate
improper output. Essentially, it'd be truncated at random point, in particular
missing a newline at the end, so it'd run with the next field, usually MESSAGE=.

log_do_header is called with parameters from compiled code (file name, lien
nubmer, etc), so in practice this was unlikely to ever be a problem, but it is
possible. In particular, if systemd was compiled from sources in some deeply
nested directory (which happens for example in mock and other build roots), the
filename could be very long.

As a safety measure, let's truncate all parameters to 256 bytes. So we have
5 fields which are 256 bytes (plus the field name prefix), and a few other
fields with fixed width. This must always fit in the 2048 byte buffer.
I don't think there's much gain in calculating the required length precisely,
since it's a lot of fields and a few bytes allocated on the stack don't matter.

6 years agobasic/log: fix confusion with parameters to log_dispatch_internal
Zbigniew Jędrzejewski-Szmek [Thu, 22 Feb 2018 22:45:27 +0000 (23:45 +0100)]
basic/log: fix confusion with parameters to log_dispatch_internal

log_dispatch_internal has only one caller where the extra_field/extra
params are not null: log_unit_full. When log_unit_full() was called,
when we got to log_dispatch_internal, our header would look like this:
PRIORITY=7
SYSLOG_FACILITY=3
CODE_FILE=../src/core/manager.c
CODE_LINE=2145
CODE_FUNC=manager_invoke_sigchld_event
USER_UNIT=gnome-terminal-server.service
65dffa7a3b984a6d9a46f0b8fb57710bUSER_INVOCATION_ID=
SYSLOG_IDENTIFIER=systemd

It took me a while to understand why I'm not seeing mangled messages in the
journal (after all, "" is a valid rvalue for log messages). The answer is that
journald rejects any field name which starts with a digit, and the MESSAGE_ID
that was used here starts with a digit. Hence, those lines would be silently
filtered out.

6 years agobasic/log: make log_object_internalv static
Zbigniew Jędrzejewski-Szmek [Thu, 22 Feb 2018 22:07:58 +0000 (23:07 +0100)]
basic/log: make log_object_internalv static

It makes the code easier to read, because it's obvious that the function
cannot be called from elsewhere.

6 years agobasic/log: voidify snprintf statements
Zbigniew Jędrzejewski-Szmek [Thu, 22 Feb 2018 21:09:16 +0000 (22:09 +0100)]
basic/log: voidify snprintf statements

The buffers are fixed size, so the message may not fit, but we don't
particularly care.

6 years agoRevert "Replace use of snprintf with xsprintf"
Zbigniew Jędrzejewski-Szmek [Thu, 22 Feb 2018 21:03:15 +0000 (22:03 +0100)]
Revert "Replace use of snprintf with xsprintf"

This reverts commit a7419dbc59da5c8cc9e90b3d96bc947cad91ae16.

_All_ changes in that commit were wrong.

Fixes #8211.

6 years agologin: fix user@.service case, so we don't allow nested sessions (#8051)
Alan Jenkins [Thu, 22 Feb 2018 20:38:44 +0000 (20:38 +0000)]
login: fix user@.service case, so we don't allow nested sessions (#8051)

> logind sessions are mostly bound to the audit session concept, and audit
> sessions remain unaffected by "su", in fact they are defined to be
> "sealed off", i.e. in a way that if a process entered a session once, it
> will always stay with it, and so will its children, i.e. the only way to
> get a new session is by forking off something off PID 1 (or something
> similar) that never has been part of a session.

The code had a gap.  user@.service is a special case PAM session which does
not create a logind session.  Let's remember to check for it.

Fixes #8021

6 years agoFix format-truncation compile failure by typecasting USB IDs (#8250)
Patrick Uiterwijk [Thu, 22 Feb 2018 18:41:30 +0000 (19:41 +0100)]
Fix format-truncation compile failure by typecasting USB IDs (#8250)

This patch adds safe_atoux16 for parsing an unsigned hexadecimal 16bit int, and
uses that for parsing USB device and vendor IDs.

This fixes a compile error with gcc-8 because while we know that USB IDs are 2 bytes,
the compiler does not know that.

../src/udev/udev-builtin-hwdb.c:80:38: error: '%04X' directive output may be
truncated writing between 4 and 8 bytes into a region of size between 2 and 6
[-Werror=format-truncation=]

Signed-off-by: Adam Williamson <awilliam@redhat.com>
Signed-off-by: Patrick Uiterwijk <puiterwijk@redhat.com>
6 years agovirt: detect QNX hypervisor Detect QNX hypervisor based on the CPUID.
Shuang Liu [Thu, 22 Feb 2018 13:35:47 +0000 (14:35 +0100)]
virt: detect QNX hypervisor Detect QNX hypervisor based on the CPUID.

Fixes: #7239
6 years agomount-setup: always use the same source as fstype for the API VFS we mount
Lennart Poettering [Fri, 16 Feb 2018 14:49:32 +0000 (15:49 +0100)]
mount-setup: always use the same source as fstype for the API VFS we mount

So far, for all our API VFS mounts we used the fstype also as mount
source, let's do that for the cgroupsv2 mounts too. The kernel doesn't
really care about the source for API VFS, but it's visible to the user,
hence let's clean this up and follow the rule we otherwise follow.

6 years agobpf: use BPF_F_ALLOW_MULTI flag if it is available
Lennart Poettering [Fri, 16 Feb 2018 14:35:49 +0000 (15:35 +0100)]
bpf: use BPF_F_ALLOW_MULTI flag if it is available

This new kernel 4.15 flag permits that multiple BPF programs can be
executed for each packet processed: multiple per cgroup plus all
programs defined up the tree on all parent cgroups.

We can use this for two features:

1. Finally provide per-slice IP accounting (which was previously
   unavailable)

2. Permit delegation of BPF programs to services (i.e. leaf nodes).

This patch beefs up PID1's handling of BPF to enable both.

Note two special items to keep in mind:

a. Our inner-node BPF programs (i.e. the ones we attach to slices) do
   not enforce IP access lists, that's done exclsuively in the leaf-node
   BPF programs. That's a good thing, since that way rules in leaf nodes
   can cancel out rules further up (i.e. for example to implement a
   logic of "disallow everything except httpd.service"). Inner node BPF
   programs to accounting however if that's requested. This is
   beneficial for performance reasons: it means in order to provide
   per-slice IP accounting we don't have to add up all child unit's
   data.

b. When this code is run on pre-4.15 kernel (i.e. where
   BPF_F_ALLOW_MULTI is not available) we'll make IP acocunting on slice
   units unavailable (i.e. revert to behaviour from before this commit).
   For leaf nodes we'll fallback to non-ALLOW_MULTI mode however, which
   means that BPF delegation is not available there at all, if IP
   fw/acct is turned on for the unit. This is a change from earlier
   behaviour, where we use the BPF_F_ALLOW_OVERRIDE flag, so that our
   fw/acct would lose its effect as soon as delegation was turned on and
   some client made use of that. I think the new behaviour is the safer
   choice in this case, as silent bypassing of our fw rules is not
   possible anymore. And if people want proper delegation then the way
   out is a more modern kernel or turning off IP firewalling/acct for
   the unit algother.

6 years agobpf: mount bpffs by default on boot
Lennart Poettering [Fri, 16 Feb 2018 13:59:26 +0000 (14:59 +0100)]
bpf: mount bpffs by default on boot

We make heavy use of BPF functionality these days, hence expose the BPF
file system too by default now. (Note however, that we don't actually
make use bpf file systems object yet, but we might later on too.)

6 years agonologin: extend the /run/nologin descriptions a bit (#8244)
Lennart Poettering [Thu, 22 Feb 2018 13:21:30 +0000 (14:21 +0100)]
nologin: extend the /run/nologin descriptions a bit (#8244)

This is an attempt to improve #8228 a bit, by extending the /run/nologin
a bit, but still keeping it somewhat brief.

On purpose I used the vague wording "unprivileged user" rather than
"non-root user" so that pam_nologin can be updated to disable its
behaviour for members of the "wheel" group one day, and our messages
would still make sense.

See #8228.

6 years agobasic: split out update_reboot_parameter_and_warn() into its own .c/.h files
Lennart Poettering [Wed, 21 Feb 2018 16:54:35 +0000 (17:54 +0100)]
basic: split out update_reboot_parameter_and_warn() into its own .c/.h files

This is primarily preparation for a follow-up commit that adds a common
implementation of the other side of the reboot parameter file, i.e. the
code that reads the file and issues reboot() for it.

6 years agobasic: add a common syscall wrapper around reboot()
Lennart Poettering [Wed, 21 Feb 2018 16:42:59 +0000 (17:42 +0100)]
basic: add a common syscall wrapper around reboot()

This mimics the raw_clone() call we have in place already and
establishes a new syscall wrapper raw_reboot() that wraps the kernel's
reboot() system call in a bit more low-level fashion that glibc's
reboot() wrapper. The main difference is that the extra "arg" argument
is supported.

Ultimately this just replaces the syscall wrapper implementation we
currently have at three places in our codebase by a single one.

With this change this means that all our syscall() invocations are
neatly separated out in static inline system call wrappers in our header
functions.

6 years agomissing: always use __NR_ as prefix for syscall numbers
Lennart Poettering [Wed, 21 Feb 2018 16:27:34 +0000 (17:27 +0100)]
missing: always use __NR_ as prefix for syscall numbers

Apparently, both __NR_ and SYS_ are useful, but we mostly use __NR_
hence use it for these two cases too, so that we settle on __NR_
exclusively.

6 years agomissing: Fix statx syscall ifdeffery
Lennart Poettering [Wed, 21 Feb 2018 16:25:01 +0000 (17:25 +0100)]
missing: Fix statx syscall ifdeffery

Fix a copy/paste mistake.

Fixes: #8238
6 years agotree-wide: reopen log when we need to log in FORK_CLOSE_ALL_FDS children
Lennart Poettering [Wed, 21 Feb 2018 23:35:00 +0000 (00:35 +0100)]
tree-wide: reopen log when we need to log in FORK_CLOSE_ALL_FDS children

In a number of occasions we use FORK_CLOSE_ALL_FDS when forking off a
child, since we don't want to pass fds to the processes spawned (either
because we later want to execve() some other process there, or because
our child might hang around for longer than expected, in which case it
shouldn't keep our fd pinned). This also closes any logging fds, and
thus means logging is turned off in the child. If we want to do proper
logging, explicitly reopen the logs hence in the child at the right
time.

This is particularly crucial in the umount/remount children we fork off
the shutdown binary, as otherwise the children can't log, which is
why #8155 is harder to debug than necessary: the log messages we
generate about failing mount() system calls aren't actually visible on
screen, as they done in the child processes where the log fds are
closed.

6 years agolog: only open kmsg on fallback if we actually want to use it
Lennart Poettering [Wed, 21 Feb 2018 23:31:36 +0000 (00:31 +0100)]
log: only open kmsg on fallback if we actually want to use it

Previously, we'd try to open kmsg on failure of the journal/syslog even
if no automatic fallback to kmsg was requested — and we wouldn't even
use the open connection afterwards...

6 years agomissing_syscall: when adding syscall replacements, use different names (#8229)
Zbigniew Jędrzejewski-Szmek [Wed, 21 Feb 2018 13:04:50 +0000 (14:04 +0100)]
missing_syscall: when adding syscall replacements, use different names (#8229)

In meson.build we check that functions are available using:
    meson.get_compiler('c').has_function('foo')
which checks the following:
- if __stub_foo or __stub___foo are defined, return false
- if foo is declared (a pointer to the function can be taken), return true
- otherwise check for __builtin_memfd_create

_stub is documented by glibc as
   It defines a symbol '__stub_FUNCTION' for each function
   in the C library which is a stub, meaning it will fail
   every time called, usually setting errno to ENOSYS.

So if __stub is defined, we know we don't want to use the glibc version, but
this doesn't tell us if the name itself is defined or not. If it _is_ defined,
and we define our replacement as an inline static function, we get an error:

In file included from ../src/basic/missing.h:1358:0,
                 from ../src/basic/util.h:47,
                 from ../src/basic/calendarspec.h:29,
                 from ../src/basic/calendarspec.c:34:
../src/basic/missing_syscall.h:65:19: error: static declaration of 'memfd_create' follows non-static declaration
 static inline int memfd_create(const char *name, unsigned int flags) {
                   ^~~~~~~~~~~~
.../usr/include/bits/mman-shared.h:46:5: note: previous declaration of 'memfd_create' was here
 int memfd_create (const char *__name, unsigned int __flags) __THROW;
     ^~~~~~~~~~~~

To avoid this problem, call our inline functions different than glibc,
and use a #define to map the official name to our replacement.

Fixes #8099.

v2:
- use "missing_" as the prefix instead of "_"

v3:
- rebase and update for statx()

  Unfortunately "statx" is also present in "struct statx", so the define
  causes issues. Work around this by using a typedef.

I checked that systemd compiles with current glibc
(glibc-devel-2.26-24.fc27.x86_64) if HAVE_MEMFD_CREATE, HAVE_GETTID,
HAVE_PIVOT_ROOT, HAVE_SETNS, HAVE_RENAMEAT2, HAVE_KCMP, HAVE_KEYCTL,
HAVE_COPY_FILE_RANGE, HAVE_BPF, HAVE_STATX are forced to 0.

Setting HAVE_NAME_TO_HANDLE_AT to 0 causes an issue, but it's not because of
the define, but because of struct file_handle.

6 years agotest-user-util: skip most tests for nobody if synthentization is off
Zbigniew Jędrzejewski-Szmek [Tue, 20 Feb 2018 16:13:41 +0000 (17:13 +0100)]
test-user-util: skip most tests for nobody if synthentization is off

When synthetisation is turned off, there's just too many ways those tests can
go wrong. We are not interested in verifying that the db on disk is correct,
let's just skip all checks.

In the first version of this patch, I recorded if we detected a mismatch during
configuration and only skipped tests in that case, but actually it is possible
to change the host configuration between our configuration phase and running
of the tests. It's just more robust to skip always. (This is particularly true
if tests are installed.)

6 years agotest-user-util: print function delimiters
Zbigniew Jędrzejewski-Szmek [Tue, 20 Feb 2018 21:10:45 +0000 (22:10 +0100)]
test-user-util: print function delimiters

This makes it easier to see what is going on. Crashes may happen in a
nested test_{uid,gid}_to_name_one() function, and the default backtrace
doesn't show the actual string being tested.

6 years agoTODO: drop one item
Zbigniew Jędrzejewski-Szmek [Tue, 20 Feb 2018 13:53:09 +0000 (14:53 +0100)]
TODO: drop one item

C.f. 7cb609115c532c3591d43a604d67d72e508ba5d9.

6 years agomount-util: call mount_option_mangle() in mount_verbose()
Yu Watanabe [Thu, 15 Feb 2018 00:43:02 +0000 (09:43 +0900)]
mount-util: call mount_option_mangle() in mount_verbose()

6 years agomount-util: add mount_option_mangle()
Yu Watanabe [Thu, 15 Feb 2018 00:32:04 +0000 (09:32 +0900)]
mount-util: add mount_option_mangle()

This is used in the later commits.

6 years agoxattr-util: use crtime/btime if statx() is available for implementation of fd_setcrti...
Lennart Poettering [Tue, 20 Feb 2018 11:48:33 +0000 (12:48 +0100)]
xattr-util: use crtime/btime if statx() is available for implementation of fd_setcrtime() and friends

The Linux kernel exposes the birth time now for files through statx()
hence make use of it where available. We keep the xattr logic in place
for this however, since only a subset of file systems on Linux currently
expose the birth time. NFS and tmpfs for example do not support it. OTOH
there are other file systems that do support the birth time but might
not support xattrs (smb…), hence make the best of the two, in particular
in order to deal with journal files copied between file system types and
to maintain compatibility with older file systems that are updated to
newer version of the file system.

6 years agoxattr-util: support AT_EMPTY_PATH in fgetxattrat_fake()
Lennart Poettering [Tue, 20 Feb 2018 11:46:14 +0000 (12:46 +0100)]
xattr-util: support AT_EMPTY_PATH in fgetxattrat_fake()

Let's expose fstatat() like behaviour if AT_EMPTY_PATH is defined.

Also, check the specified flags returning EINVAL on the flags we don't
emulate.

6 years agoio-util: add an unlikely decorator for a test that should never hold
Lennart Poettering [Mon, 19 Feb 2018 17:37:50 +0000 (18:37 +0100)]
io-util: add an unlikely decorator for a test that should never hold

6 years agotree-wide: make use of fsync_directory_of_file() all over the place
Lennart Poettering [Mon, 19 Feb 2018 17:24:36 +0000 (18:24 +0100)]
tree-wide: make use of fsync_directory_of_file() all over the place

Let's make use this at various places we call fsync(), to make things
fully reliable, as the kernel devs suggest to first fsync() files and
then fsync() the directories they are located in.

6 years agofs-util: move fsync_directory_of_file() into generic code
Lennart Poettering [Mon, 19 Feb 2018 17:23:38 +0000 (18:23 +0100)]
fs-util: move fsync_directory_of_file() into generic code

This function used by the journal code is pretty useful generically,
let's move it to fs-util.c to make it useful for other code too.

6 years agostat-util: unify code that checks whether something is a regular file
Lennart Poettering [Mon, 19 Feb 2018 17:01:05 +0000 (18:01 +0100)]
stat-util: unify code that checks whether something is a regular file

Let's add a common implementation for regular file checks, that are
careful to return the right error code (EISDIR/EISLNK/EBADFD) when we
are encountering a wrong file node.

6 years agotests: add a simple test for utf8_n_codepoints()
Zbigniew Jędrzejewski-Szmek [Fri, 16 Feb 2018 09:34:00 +0000 (10:34 +0100)]
tests: add a simple test for utf8_n_codepoints()

6 years agoSome comment adjustments
Zbigniew Jędrzejewski-Szmek [Fri, 16 Feb 2018 09:27:47 +0000 (10:27 +0100)]
Some comment adjustments

Follow up for review of #8184.

6 years agopid1: do not initialize join_controllers by default
Zbigniew Jędrzejewski-Szmek [Fri, 16 Feb 2018 09:17:46 +0000 (10:17 +0100)]
pid1: do not initialize join_controllers by default

We're moving towards unified cgroup hierarchy where this is not necessary.
This makes main.c a bit simpler.

6 years agoMove config_parse_join_controllers to shared, add test
Zbigniew Jędrzejewski-Szmek [Fri, 16 Feb 2018 08:56:29 +0000 (09:56 +0100)]
Move config_parse_join_controllers to shared, add test

config_parse_join_controllers would free the destination argument on failure,
which is contrary to our normal style, where failed parsing has no effect.
Moving it to shared also allows a test to be added.

6 years agoshared/conf-parser: define a macro for the repeating argument set
Zbigniew Jędrzejewski-Szmek [Fri, 16 Feb 2018 08:29:57 +0000 (09:29 +0100)]
shared/conf-parser: define a macro for the repeating argument set

The arguments have to be indentical everywhere, so let's use a macro to
make things more readable. But only in the headers, in the .c files let's
keep them verbose so that it's easy to see the argument list.

6 years agotest-process-util: one more (void)
Zbigniew Jędrzejewski-Szmek [Fri, 16 Feb 2018 06:03:13 +0000 (07:03 +0100)]
test-process-util: one more (void)

6 years agoAdd some handling to remaining unlinkat calls
Zbigniew Jędrzejewski-Szmek [Fri, 16 Feb 2018 06:00:22 +0000 (07:00 +0100)]
Add some handling to remaining unlinkat calls

Coverity now started warning about this ("Calling unlinkat without checking
return value (as is done elsewhere 12 out of 15 times).", and it is right:
most of the time we should at list print a log message so people can figure
out something is wrong when this happens.

v2:
- use warning level in journald too (this is unlikely to happen ever, so it
  should be safe to something that is visible by default).

6 years agologin,user-sessions: always warn when we fail to remove nologin file
Zbigniew Jędrzejewski-Szmek [Fri, 16 Feb 2018 05:58:33 +0000 (06:58 +0100)]
login,user-sessions: always warn when we fail to remove nologin file

This usually is very annoying to users who then cannot log in, so
make sure we always warn if that happens (selinux, or whatever other reason).

This reverts a790812cb349c5cef95d1b4a20fc80ca08d3a145.

6 years agomeson: apply defaults if /etc/login.defs doesn't exist
Caio Marcelo de Oliveira Filho [Mon, 19 Feb 2018 02:33:16 +0000 (18:33 -0800)]
meson: apply defaults if /etc/login.defs doesn't exist

Apply defaults for system_{uid,gid}_max even if the /etc/login.defs file
doesn't exist (e.g. in Clear Linux with no changes).

awk returns an empty string in case the file doesn't exist, causing meson to
fail in to_int(). So set the default if output is empty. This makes the BEGIN{}
blocks unnecessary, so remove them.

6 years agomeson: use multiline string syntax (#8214)
Caio Marcelo de Oliveira Filho [Mon, 19 Feb 2018 09:37:19 +0000 (01:37 -0800)]
meson: use multiline string syntax (#8214)

The single quote working with multiple lines is likely to be unintended. With
current versions of meson, it also causes error messages after it to report the
wrong line number. Use the documented syntax instead.

6 years agomeson: drop unnecessary "transformation" of policy files
Zbigniew Jędrzejewski-Szmek [Fri, 16 Feb 2018 11:48:55 +0000 (12:48 +0100)]
meson: drop unnecessary "transformation" of policy files

Those files don't contain any @variables@, so the configuration step was just
copying them to build/. Let's avoid that, and fix their suffixes while at it.

6 years agomeson: drop double .in suffix for o.fd.systemd1.policy file
Zbigniew Jędrzejewski-Szmek [Fri, 16 Feb 2018 11:37:42 +0000 (12:37 +0100)]
meson: drop double .in suffix for o.fd.systemd1.policy file

This file is now undergoing just one transformation, so drop the unnecessary
suffix.

6 years agoGettextize policy files
Gunnar Hjalmarsson [Thu, 15 Feb 2018 20:21:58 +0000 (21:21 +0100)]
Gettextize policy files

* Don't merge translations into the files
* Add gettext-domain="systemd" to description and message

Closes #8162, replaces #8118.

6 years agologind: change check_gc to may_gc everywhere
Zbigniew Jędrzejewski-Szmek [Thu, 15 Feb 2018 12:14:35 +0000 (13:14 +0100)]
logind: change check_gc to may_gc everywhere

6 years agomeson: add -Dmemory-accounting-default=true|false
Zbigniew Jędrzejewski-Szmek [Thu, 15 Feb 2018 10:43:08 +0000 (11:43 +0100)]
meson: add -Dmemory-accounting-default=true|false

This makes it easy to set the default for distributions and users which want to
default to off because they primarily use older kernels.

6 years agopid1: turn memory accounting on by default now
Lennart Poettering [Fri, 9 Feb 2018 18:59:53 +0000 (19:59 +0100)]
pid1: turn memory accounting on by default now

After discussions with @htejun it appears it's OK now to enable memory
accounting by default for all units without affecting system performance
too badly. facebook has made good experiences with deploying memory
accounting across their infrastructure.

This hence turns MemoryAccounting= from opt-in to opt-out, similar to
how TasksAccounting= is already handled. The other accounting options
remain off, their performance impact is too big still.

6 years agoutf8: add utf8_n_codepoints() for counting complete utf8 codepoints in a string
Lennart Poettering [Wed, 14 Feb 2018 17:41:03 +0000 (18:41 +0100)]
utf8: add utf8_n_codepoints() for counting complete utf8 codepoints in a string

6 years agoterminal-util: make resolve_dev_console() less weird
Lennart Poettering [Wed, 14 Feb 2018 16:30:37 +0000 (17:30 +0100)]
terminal-util: make resolve_dev_console() less weird

Let's normalize the behaviour: return a negative errno style error code,
and return the resolved string directly as argument.

6 years agoterminal-util: modernize get_kernel_consoles() a bit
Lennart Poettering [Wed, 14 Feb 2018 13:56:17 +0000 (14:56 +0100)]
terminal-util: modernize get_kernel_consoles() a bit

Also, make sure when we run in a container, we don't use the data from
/sys at all, but immediately fall back to /dev/console itself.

6 years agotty-ask-password-agent: reenable color for boot-time password prompt
Lennart Poettering [Wed, 14 Feb 2018 13:30:30 +0000 (14:30 +0100)]
tty-ask-password-agent: reenable color for boot-time password prompt

The password prompt used to be highlighted, and that was a good thing.
Let's fix things to make the prompt highlighted again.

Fixes: #3853
6 years agoterminal-util: add some explanatory comments
Lennart Poettering [Wed, 14 Feb 2018 13:28:17 +0000 (14:28 +0100)]
terminal-util: add some explanatory comments

6 years agoupdate TODO
Lennart Poettering [Wed, 14 Feb 2018 13:28:00 +0000 (14:28 +0100)]
update TODO

6 years agoprocess: shortcut getenv_for_pid() for our own process
Lennart Poettering [Wed, 14 Feb 2018 13:27:31 +0000 (14:27 +0100)]
process: shortcut getenv_for_pid() for our own process

6 years agoterminal-util: minor, trivial fixes and improvements
Lennart Poettering [Tue, 13 Feb 2018 22:53:34 +0000 (23:53 +0100)]
terminal-util: minor, trivial fixes and improvements

6 years agoterminal-util: when making /dev/null or the console stdio, forget cached terminal...
Lennart Poettering [Tue, 13 Feb 2018 22:50:26 +0000 (23:50 +0100)]
terminal-util: when making /dev/null or the console stdio, forget cached terminal features

Let's forget all relevant terminal features we learnt when we make a
console or /dev/null stdin/stdout/stderr.

Also, while we are at it, let's drop the various _unlikely_ and
_likely_ annotiations around the terminal feature caches. In many cases
we call the relevant functions only once in which cases the annotations
are likely to do just harm and no good. After all we can't know if the
specific code will call us just once or many times...

6 years agoterminal-util: rework acquire_terminal()
Lennart Poettering [Tue, 13 Feb 2018 20:24:37 +0000 (21:24 +0100)]
terminal-util: rework acquire_terminal()

This modernizes acquire_terminal() in a couple of ways:

1. The three boolean arguments are replaced by a flags parameter, that
   should be more descriptive in what it does.

2. We now properly handle inotify queue overruns

3. We use _cleanup_ for closing the fds now, to shorten the code quite a
   bit.

Behaviour should not be altered by this.

6 years agoupdate TODO
Lennart Poettering [Wed, 7 Feb 2018 22:03:13 +0000 (23:03 +0100)]
update TODO

6 years agocore: add new new bus call for migrating foreign processes to scope/service units
Lennart Poettering [Wed, 7 Feb 2018 21:52:52 +0000 (22:52 +0100)]
core: add new new bus call for migrating foreign processes to scope/service units

This adds a new bus call to service and scope units called
AttachProcesses() that moves arbitrary processes into the cgroup of the
unit. The primary user for this new API is systemd itself: the systemd
--user instance uses this call of the systemd --system instance to
migrate processes if itself gets the request to migrate processes and
the kernel refuses this due to access restrictions.

The primary use-case of this is to make "systemd-run --scope --user …"
invoked from user session scopes work correctly on pure cgroupsv2
environments. There, the kernel refuses to migrate processes between two
unprivileged-owned cgroups unless the requestor as well as the ownership
of the closest parent cgroup all match. This however is not the case
between the session-XYZ.scope unit of a login session and the
user@ABC.service of the systemd --user instance.

The new logic always tries to move the processes on its own, but if
that doesn't work when being the user manager, then the system manager
is asked to do it instead.

The new operation is relatively restrictive: it will only allow to move
the processes like this if the caller is root, or the UID of the target
unit, caller and process all match. Note that this means that
unprivileged users cannot attach processes to scope units, as those do
not have "owning" users (i.e. they have now User= field).

Fixes: #3388
6 years agosd-bus: synthesize a description for user/system bus if otherwise unset
Lennart Poettering [Wed, 7 Feb 2018 21:28:42 +0000 (22:28 +0100)]
sd-bus: synthesize a description for user/system bus if otherwise unset

Let's make debugging easier, by synthesizing a name when we have some
indication what kind of bus this is.

6 years agouser-util: also consider /bin/false and /bin/true as non-shell
Lennart Poettering [Wed, 7 Feb 2018 17:11:18 +0000 (18:11 +0100)]
user-util: also consider /bin/false and /bin/true as non-shell

6 years agoprocess-util: be more careful in is_kernel_thread()
Lennart Poettering [Tue, 6 Feb 2018 14:59:55 +0000 (15:59 +0100)]
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.

6 years agocgroup: add a new "can_delegate" flag to the unit vtable, and set it for scope and...
Lennart Poettering [Tue, 6 Feb 2018 10:57:35 +0000 (11:57 +0100)]
cgroup: add a new "can_delegate" flag to the unit vtable, and set it for scope and service units only

Currently we allowed delegation for alluntis with cgroup backing
except for slices. Let's make this a bit more strict for now, and only
allow this in service and scope units.

Let's also add a generic accessor unit_cgroup_delegate() for checking
whether a unit has delegation turned on that checks the new bool first.

Also, when doing transient units, let's explcitly refuse turning on
delegation for unit types that don#t support it. This is mostly
cosmetical as we wouldn't act on the delegation request anyway, but
certainly helpful for debugging.