chiark / gitweb /
elogind.git
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.

6 years agomeson: factor out daemon/event/id128 sources into variables (#8170)
Davide Cavalca [Wed, 14 Feb 2018 13:08:50 +0000 (05:08 -0800)]
meson: factor out daemon/event/id128 sources into variables (#8170)

6 years agorm-rf: make flags fields easier to read
Lennart Poettering [Fri, 9 Feb 2018 08:52:36 +0000 (09:52 +0100)]
rm-rf: make flags fields easier to read

6 years agofs-util: add new unlinkat_deallocate() helper
Lennart Poettering [Fri, 9 Feb 2018 08:50:31 +0000 (09:50 +0100)]
fs-util: add new unlinkat_deallocate() helper

This new helper not only removes a file from a directory but also
ensures its space on disk is deallocated, by either punching a hole over
the full file or truncating the file afterwards if the file's link
counter is 0. This is useful in "vacuuming" algorithms to ensure that
client's can't keep the disk space the vacuuming is supposed to recover
pinned simply by keeping an fd open to it.

6 years agocopy: wrap some unlink() calls in (void) casts
Lennart Poettering [Fri, 9 Feb 2018 08:50:13 +0000 (09:50 +0100)]
copy: wrap some unlink() calls in (void) casts

6 years agotree-wide: use path_hash_ops instead of string_hash_ops whenever we key by a path
Lennart Poettering [Thu, 8 Feb 2018 17:58:35 +0000 (18:58 +0100)]
tree-wide: use path_hash_ops instead of string_hash_ops whenever we key by a path

Let's make use of our new hash_ops!

6 years agohash-func: add generic hash_ops implementation for hashing paths
Lennart Poettering [Thu, 8 Feb 2018 17:31:15 +0000 (18:31 +0100)]
hash-func: add generic hash_ops implementation for hashing paths

This is similar to string_hash_ops but operates one file system paths
specifically. It will ensure that "/foo//bar" and "///foo/bar" are
considered to be the same path for hashmap purposes.

This makes use of the existing path_compare() API, and adds a matching
hashing function for it.

Note that relative and absolute paths will hash to different values,
however whether the path is suffixed with a slash or not is not
detected. This matches the existing path_compare() behaviour, and
follows the logic that on Linux there can't be two different objects at
path /foo/bar and /foo/bar/ either.

6 years agohash-funcs: remove redundant definition of devt_hash_ops
Lennart Poettering [Thu, 8 Feb 2018 16:37:56 +0000 (17:37 +0100)]
hash-funcs: remove redundant definition of devt_hash_ops

We should assign a value only in the .c file, not in both the .c and .h
file.

6 years agojournal: move code that checks for network fs to stat-util.[ch]
Lennart Poettering [Thu, 8 Feb 2018 16:14:37 +0000 (17:14 +0100)]
journal: move code that checks for network fs to stat-util.[ch]

We have similar code in stat-util.[ch] and managing this at a central
place almost definitely is the better choice.

6 years agomailmap: one more person
Zbigniew Jędrzejewski-Szmek [Wed, 14 Feb 2018 08:34:46 +0000 (09:34 +0100)]
mailmap: one more person

For a083b4875e8dec5ce5379d8bc437d750cd338c37.

6 years agosocket-util: drop getnameinfo_pretty()
Yu Watanabe [Fri, 9 Feb 2018 08:52:05 +0000 (17:52 +0900)]
socket-util: drop getnameinfo_pretty()

6 years agofs-util: drop readlink_and_make_absolute_root()
Yu Watanabe [Fri, 9 Feb 2018 07:58:56 +0000 (16:58 +0900)]
fs-util: drop readlink_and_make_absolute_root()

6 years agofs-util: drop readlink_and_canonicalize()
Yu Watanabe [Fri, 9 Feb 2018 07:49:54 +0000 (16:49 +0900)]
fs-util: drop readlink_and_canonicalize()

6 years agobasic/socket-util: drop use of NI_IDN_USE_STD3_ASCII_RULES
Zbigniew Jędrzejewski-Szmek [Fri, 9 Feb 2018 12:31:37 +0000 (13:31 +0100)]
basic/socket-util: drop use of NI_IDN_USE_STD3_ASCII_RULES

The only use of socknameinfo_pretty() is in src/journal-remote/journal-remote.c,
to determine the output filename.

Replaces #8120.

6 years agofd-util: move certain fds above fd #2 (#8129)
Lennart Poettering [Fri, 9 Feb 2018 16:53:28 +0000 (17:53 +0100)]
fd-util: move certain fds above fd #2 (#8129)

This adds some paranoia code that moves some of the fds we allocate for
longer periods of times to fds > 2 if they are allocated below this
boundary. This is a paranoid safety thing, in order to avoid that
external code might end up erroneously use our fds under the assumption
they were valid stdin/stdout/stderr. Think: some app closes
stdin/stdout/stderr and then invokes 'fprintf(stderr, …' which causes
writes on our fds.

This both adds the helper to do the moving as well as ports over a
number of users to this new logic. Since we don't want to litter all our
code with invocations of this I tried to strictly focus on fds we keep
open for long periods of times only and only in code that is frequently
loaded into foreign programs (under the assumptions that in our own
codebase we are smart enough to always keep stdin/stdout/stderr
allocated to avoid this pitfall). Specifically this means all code used
by NSS and our sd-xyz API:

1. our logging APIs
2. sd-event
3. sd-bus
4. sd-resolve
5. sd-netlink

This changed was inspired by this:

https://github.com/systemd/systemd/issues/8075#issuecomment-363689755

This shows that apparently IRL there are programs that do close
stdin/stdout/stderr, and we should accomodate for that.

Note that this won't fix any bugs, this just makes sure that buggy
programs are less likely to interfere with out own code.

6 years agoSuspend on lid close based on power status. (#8016)
Simon Fowler [Fri, 9 Feb 2018 16:37:39 +0000 (02:37 +1000)]
Suspend on lid close based on power status. (#8016)

This change adds support for controlling the suspend-on-lid-close
behaviour based on the power status as well as whether the machine is
docked or has an external monitor. For backwards compatibility the new
configuration file variable is ignored completely by default, and must
be set explicitly before being considered in any decisions.

6 years agosd-bus: cleanup ssh sessions (Closes: #8076)
Shawn Landden [Sat, 3 Feb 2018 18:16:33 +0000 (10:16 -0800)]
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()

6 years agosd-bus: explicitly convert int to bool
Yu Watanabe [Fri, 9 Feb 2018 07:19:11 +0000 (16:19 +0900)]
sd-bus: explicitly convert int to bool

6 years agosd-bus: use free_and_replace()
Yu Watanabe [Fri, 9 Feb 2018 07:21:51 +0000 (16:21 +0900)]
sd-bus: use free_and_replace()

6 years agosd-bus: avoid potential memory leaks
Yu Watanabe [Fri, 9 Feb 2018 07:21:29 +0000 (16:21 +0900)]
sd-bus: avoid potential memory leaks

6 years agotmpfiles: allow admin/runtime overrides to runtime config
Zbigniew Jędrzejewski-Szmek [Mon, 5 Feb 2018 13:53:11 +0000 (14:53 +0100)]
tmpfiles: allow admin/runtime overrides to runtime config

This is very similar to d16a1c1bb6. For tmpfiles this is much less useful
compared to sysusers, but let's add this anyway for consistency.

6 years agoprocess-util: use raw_getpid() in getpid_cache() internally (#8115)
Lennart Poettering [Wed, 7 Feb 2018 02:10:09 +0000 (03:10 +0100)]
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
6 years agocore/execute: make arguments constant if possible
Yu Watanabe [Tue, 6 Feb 2018 03:17:50 +0000 (12:17 +0900)]
core/execute: make arguments constant if possible

Also make functions static if possible.

6 years agotest: add a simple smoke test for string_hashsum()
Zbigniew Jędrzejewski-Szmek [Mon, 5 Feb 2018 08:48:38 +0000 (09:48 +0100)]
test: add a simple smoke test for string_hashsum()

This is enough to show memory leakages pointed out by Stef Bon <stefbon@gmail.com>.

6 years agosysusers: allow admin/runtime overrides to command-line config
Zbigniew Jędrzejewski-Szmek [Wed, 31 Jan 2018 14:37:02 +0000 (15:37 +0100)]
sysusers: allow admin/runtime overrides to command-line config

When used in a package installation script, we want to invoke systemd-sysusers
before that package is installed (so it can contain files owned by the newly
created user), so the configuration to use is specified on the command
line. This should be a copy of the configuration that will be installed as
/usr/lib/sysusers.d/package.conf. We still want to obey any overrides in
/etc/sysusers.d or /run/sysusers.d in the usual fashion. Otherwise, we'd get a
different result when systemd-sysusers is run with a copy of the new config on
the command line and when systemd-sysusers is run at boot after package
instalation. In the second case any files in /etc or /run have higher priority,
so the same should happen when the configuration is given on the command line.
More generally, we want the behaviour in this special case to be as close to
the case where the file is finally on disk as possible, so we have to read all
configuration files, since they all might contain overrides and additional
configuration that matters. Even files that have lower priority might specify
additional groups for the user we are creating. Thus, we need to read all
configuration, but insert our new configuration somewhere with the right
priority.

If --target=/path/to/file.conf is given on the command line, we gather the list
of files, and pretend that the command-line config is read from
/path/to/file.conf (doesn't matter if the file on disk actually exists or
not). All package scripts should use this option to obtain consistent and
idempotent behaviour.

The corner case when --target= is specified and there are no positional
arguments is disallowed.

v1:
- version with --config-name=
v2:
- disallow --config-name= and no positional args
v3:
- remove --config-name=
v4:
- add --target= and rework the code completely
v5:
- fix argcounting bug and add example in man page
v6:
- rename --target to --replace

6 years agobasic/strv: add function to insert items at position
Zbigniew Jędrzejewski-Szmek [Thu, 1 Feb 2018 11:50:18 +0000 (12:50 +0100)]
basic/strv: add function to insert items at position

6 years agosysusers: allow the shell to be specified
Zbigniew Jędrzejewski-Szmek [Tue, 30 Jan 2018 13:28:10 +0000 (14:28 +0100)]
sysusers: allow the shell to be specified

This is necessary for some system users where the "login shell" is
set to a specific binary.

6 years agosysusers: emit a bit more info at debug level when locking fails
Zbigniew Jędrzejewski-Szmek [Mon, 29 Jan 2018 13:23:31 +0000 (14:23 +0100)]
sysusers: emit a bit more info at debug level when locking fails

This is the first error message when running unprivileged, and the message is
unspecific, so let's at least add some logging at debug level to make this less
confusing.