Commit Graph

213 Commits

Author SHA1 Message Date
Yu Watanabe 1403fe26d6 udev: drop unnecessary libudev-util.h inclusions 2020-12-16 02:31:44 +09:00
Yu Watanabe c8eaaf69ac udev: rename UTIL_LINE_SIZE -> UDEV_LINE_SIZE and friends
This also moves them to udev-util.h
2020-12-16 02:22:58 +09:00
Yu Watanabe 1223227f95 udev: move util_resolve_subsys_kernel() to udev-util.c 2020-12-16 02:14:47 +09:00
Yu Watanabe 393fcaf733 udev: move util_replace_chars() to udev-util.c 2020-12-16 02:14:47 +09:00
Yu Watanabe 5953d8b910 udev: move util_replace_whitespace() to udev-util.c 2020-12-16 02:14:47 +09:00
Yu Watanabe 1a0bd01529 udev: introduce new OPTIONS="log_level=" udev rule 2020-12-10 12:31:45 +09:00
Yu Watanabe 5b90b906c7 udev: use FOREACH_DEVICE_TAG() macro at one more place 2020-11-17 09:47:14 +01:00
Zbigniew Jędrzejewski-Szmek b8aac5014c
Merge pull request #17431 from msekletar/udev-link-update-race
udev: make algorithm that selects highest priority devlink less susceptible to race conditions
2020-11-10 09:41:57 +01:00
Yu Watanabe f13467ec23 license: GPL-2.0+ -> GPL-2.0-or-later 2020-11-09 13:25:50 +09:00
Michal Sekletár 30f6dce62c udev: make algorithm that selects highest priority devlink less susceptible to race conditions
Previously it was very likely, when multiple contenders for the symlink
appear in parallel, that algorithm would select wrong symlink (i.e. one
with lower-priority).

Now the algorithm is much more defensive and when we detect change in
set of contenders for the symlink we reevaluate the selection. Same
happens when new symlink replaces already existing symlink that points
to different device node.
2020-11-06 13:35:05 +01:00
Yu Watanabe 51d9aec0ff Revert "udev: import the full db on MOVE events for devices without dev_t"
This reverts commit b081b27e14.

If a network interface get a 'move' event, then previously SYSTEMD_ALIAS=
property still contains an old alias, and the old alias .device unit
will not be removed.

This makes all properties cleared on 'move' event, and then old alias
.device unit will be removed by pid1.

Fixes #16967.
2020-09-15 11:06:25 +09:00
Lennart Poettering 12ce0f4173
Merge pull request #16635 from keszybz/do-not-for-each-word
Drop FOREACH_WORD
2020-09-09 17:43:38 +02:00
Zbigniew Jędrzejewski-Szmek 90e30d767a Rename strv_split_extract() to strv_split_full()
Now that _full() is gone, we can rename _extract() to have the usual suffix
we use for the more featureful version.
2020-09-09 09:34:55 +02:00
Zbigniew Jędrzejewski-Szmek 0645b83a40 tree-wide: replace strv_split_full() with strv_split_extract() everywhere
Behaviour is not identical, as shown by the tests in test-strv.
The combination of EXTRACT_UNQUOTE without EXTRACT_RELAX only appears in
the test, so it doesn't seem particularly important. OTOH, the difference
in handling of squished parameters could make a difference. New behaviour
is what both bash and python do, so I think we can ignore this corner case.

This change has the following advantages:
- the duplication of code paths that do a very similar thing is removed
- extract_one_word() / strv_split_extract() return a proper error code.
2020-09-09 09:34:55 +02:00
Yu Watanabe ab54f12b78 sd-device: make log_device_error() or friends return void 2020-09-09 02:34:38 +09:00
Zbigniew Jędrzejewski-Szmek 90e74a66e6 tree-wide: define iterator inside of the macro 2020-09-08 12:14:05 +02:00
Lennart Poettering e77b146f82 udev: make tags "sticky"
This tries to address the "bind"/"unbind" uevent kernel API breakage, by
changing the semantics of device tags.

Previously, tags would be applied on uevents (and the database entries
they result in) only depending on the immediate context. This means that
if one uevent causes the tag to be set and the next to be unset, this
would immediately effect what apps would see and the database entries
would contain each time. This is problematic however, as tags are a
filtering concept, and if tags vanish then clients won't hence notice
when a device stops being relevant to them since not only the tags
disappear but immediately also the uevents for it are filtered including
the one necessary for the app to notice that the device lost its tag and
hence relevance.

With this change tags become "sticky". If a tag is applied is once
applied to a device it will stay in place forever, until the device is
removed. Tags can never be removed again. This means that an app
watching a specific set of devices by filtering for a tag is guaranteed
to not only see the events where the tag is set but also all follow-up
events where the tags might be removed again.

This change of behaviour is unfortunate, but is required due to the
kernel introducing new "bind" and "unbind" uevents that generally have
the effect that tags and properties disappear and apps hence don't
notice when a device looses relevance to it. "bind"/"unbind" events were
introduced in kernel 4.12, and are now used in more and more subsystems.
The introduction broke userspace widely, and this commit is an attempt
to provide a way for apps to deal with it.

While tags are now "sticky" a new automatic device property
CURRENT_TAGS is introduced (matching the existing TAGS property) that
always reflects the precise set of tags applied on the most recent
events. Thus, when subscribing to devices through tags, all devices that
ever had the tag put on them will be be seen, and by CURRENT_TAGS it may
be checked whether the device right at the moment matches the tag
requirements.

See: #7587 #7018 #8221
2020-09-01 17:40:12 +02:00
Yu Watanabe 28266c446a udev: drop unnecessary checks
Also, drop one unnecessary sd_device_unref(), as dev_db_clone will be
unref()ed in udev_event_free().
2020-07-21 11:55:28 +09:00
Yu Watanabe ae353ec2f6 udev: save ID_RENAMING= property to database before renaming network interface 2020-07-21 11:55:28 +09:00
Yu Watanabe 5eb6ef8b45 udev: udev_event_apply_format() always make buf NUL terminated
The return value of udev_event_apply_format() is always ignored.
So, the destination buffer must be always NUL terminated.
2020-06-18 13:53:08 +09:00
Yu Watanabe bc568a7a35 udev: fix error handling of sd_device_get_parent()
sd_device_get_parent() returns -EINVAL or -ENOENT on error, not -ENODEV.

Fixes #16207.
2020-06-18 13:52:38 +09:00
Michal Sekletár e209926778 udev: make signal that we use to kill workers on timeout configurable 2020-06-05 11:09:17 +02:00
Paul Davey 9f537ae310 udev: Ensure udev_event_spawn reads stdout
When running the program with udev_event_spawn it is possible to miss
output in stdout when the program exits causing the result to be empty
which can cause rules using the result to not function correctly.

This is due to the on_spawn_sigchld callback being processed while IO is
still pending and causing the event loop to exit.

To correct this the sigchld event source is made a lower priority than
the other event sources to ensure it is processed after IO.  This
requires changing the IO event source to oneshot and re-enabling it when
valid data is read but not for EOF, this prevents the empty pipes
constantly generating IO events.
2019-12-04 10:31:37 +01:00
Yu Watanabe 08de195825 udev: do not propagate error in executing PROGRAM and IMPORT{program}
Also, this adds more logs.

Fixes #14027.
2019-11-19 20:20:46 +01:00
Yu Watanabe 1405cb653a tree-wide: drop stdio.h when stdio-util.h is included 2019-11-04 00:30:32 +09:00
Yu Watanabe 88d566aa23 udev-event: log device name on spawning commands 2019-08-03 05:38:27 +09:00
Lennart Poettering 2caa38e99f tree-wide: some more [static] related fixes
let's add [static] where it was missing so far

Drop [static] on parameters that can be NULL.

Add an assert() around parameters that have [static] and can't be NULL
hence.

Add some "const" where it was forgotten.
2019-07-12 16:40:10 +02:00
Zbigniew Jędrzejewski-Szmek 3708c0f455 udev: don't force device ownership and mode on every event
This partially reverts 25de7aa7b9. I don't think the
change was intended there.

The problem I'm trying to solve: for /dev/kvm we get first an ADD uevent, and
then CHANGE whenever something connects or disconnects to the character device.
The rules in 50-default-udev.rules set UID, GID, and MODE on ADD, but not on
CHANGE. When the change event happens, we would reset the ownership and
permissions.

This happens because node_permissions_apply() would (after 25de7aa7b9)
set uid=gid=0 if they weren't set by the rules.

So let's only pass uid/gid/mode to node_permissions_apply() if appropriately
configured. Also let node_permissions_apply() do the skip of uid/gid/mode if
not set, and rename "always_apply" to more closely reflect its meaning.
2019-06-29 16:10:49 +02:00
Zbigniew Jędrzejewski-Szmek f85cc54c4b udev-rules: add precise information to rule failure logs
It is pretty hard to figure out what the problem actually is, esp. when the rule
is long.

On my machine:
systemd[1]: Starting udev Kernel Device Manager...
systemd-udevd[217399]: /usr/lib/udev/rules.d/11-dm-lvm.rules:40 Invalid value for OPTIONS key, ignoring: 'event_timeout=180'
systemd-udevd[217399]: /usr/lib/udev/rules.d/11-dm-lvm.rules:40 The line takes no effect, ignoring.
systemd-udevd[217399]: /etc/udev/rules.d/60-ipath.rules:4 Invalid value "kcopy/%02n" for NAME (char 7: invalid substitution type), ignoring, but please fix it.
systemd-udevd[217399]: /usr/lib/udev/rules.d/65-md-incremental.rules:28 Invalid value "/sbin/mdadm -I $env{DEVNAME} --export $devnode --offroot ${DEVLINKS}" for IMPORT (char 58: invalid substitution type), ignoring, but please fix it.
systemd-udevd[217399]: /etc/udev/rules.d/73-special-net-names.rules:14 Invalid value "/bin/sh -ec 'D=${DEVPATH#*/vio/}; D=${D%%%%/*}; D=${D#????}; D=${D#0}; D=${D#0}; D=${D#0}; D=${D#0}; echo ${D:-0}'" for PROGRAM (char 16: invalid substitution type), ignoring, but please fix it.
systemd-udevd[217399]: /usr/lib/udev/rules.d/84-nm-drivers.rules:10 Invalid value "/bin/sh -c 'ethtool -i $1 | sed -n s/^driver:\ //p' -- $env{INTERFACE}" for PROGRAM (char 24: invalid substitution type), ignoring, but please fix it.
systemd-udevd[217399]: /usr/lib/udev/rules.d/90-libgpod.rules:19 IMPORT key takes '==' or '!=' operator, assuming '==', but please fix it.
systemd-udevd[217399]: /usr/lib/udev/rules.d/90-libgpod.rules:23 IMPORT key takes '==' or '!=' operator, assuming '==', but please fix it.
systemd-udevd[217399]: /usr/lib/udev/rules.d/99-vmware-scsi-udev.rules:5 Invalid value "/bin/sh -c 'echo 180 >/sys$DEVPATH/device/timeout'" for RUN (char 27: invalid substitution type), ignoring, but please fix it.
systemd-udevd[217399]: /usr/lib/udev/rules.d/99-vmware-scsi-udev.rules:6 Invalid value "/bin/sh -c 'echo 180 >/sys$DEVPATH/device/timeout'" for RUN (char 27: invalid substitution type), ignoring, but please fix it.
systemd[1]: Started udev Kernel Device Manager.
2019-06-28 16:20:48 +02:00
Zbigniew Jędrzejewski-Szmek 1824300add udev-event: use normal pointer ops where appropriate 2019-06-28 16:20:48 +02:00
Yu Watanabe 99058cd66a udev: propagate errors in udev_event_execute_rules()
And do not set initialized flag to the device.
2019-06-05 10:44:17 +02:00
Yu Watanabe d7aee41db3 udev: check formatting of attribute or value earlier 2019-06-03 08:35:33 +09:00
Yu Watanabe 1448820aad udev: evaluate formatting in RUN= key earlier
Closes #12291.
2019-06-03 08:35:33 +09:00
Yu Watanabe 7280153364 udev: use delete_trailing_chars() at one more place 2019-06-03 08:35:33 +09:00
Yu Watanabe 1b65f1ebc3 udev: replace xsprintf() + strpcpy() by strpcpyf() 2019-06-03 08:35:33 +09:00
Yu Watanabe ce4f94b8e9 udev: make parser for attribute of $result harder 2019-06-03 08:35:33 +09:00
Yu Watanabe 13cd553f91 udev: split udev_event_apply_format() into small pieces
Also, this makes the function refuse invalid formatting and
propagate error in substituing variable.
2019-06-03 08:35:08 +09:00
Yu Watanabe 3c209d6070 udev: comment that $tempnode and $sysfs formats are deprecated 2019-06-02 14:15:32 +09:00
Yu Watanabe ef315adaa4 udev: pass format type to udev_event_subst_format() 2019-06-02 14:15:32 +09:00
Yu Watanabe 7e9c23dd19 udev: rename enum subst_type -> FormatSubstitutionType 2019-06-02 14:15:32 +09:00
Yu Watanabe 25de7aa7b9 udev: modernize udev-rules.c
This does the following:
- rename enum udev_builtin_cmd -> UdevBuiltinCmd
- rename struct udev_builtin -> UdevBuiltin
- move type definitions to udev-rules.h
- move prototypes of functions defined in udev-rules.c to udev-rules.h
- drop to use strbuf
- propagate critical errors in applying rules,
- drop limitation for number of tokens per line.
2019-06-02 14:15:26 +09:00
Adrian Bunk 010f917cf2 src/udev/udev-event.c must #include <sys/wait.h>
Fixes the following build failure with musl:
../git/src/udev/udev-event.c: In function 'spawn_wait':
../git/src/udev/udev-event.c:600:53: error: 'WEXITED' undeclared (first use in this function); did you mean 'WIFEXITED'?
         r = sd_event_add_child(e, NULL, spawn->pid, WEXITED, on_spawn_sigchld, spawn);
                                                     ^~~~~~~

This looks like a bug in udev-event.c that could also have broken
the compilation after some future glibc header reshuffle.
2019-05-17 09:35:43 +03:00
Yu Watanabe d4d690facd udevd: use device_get_action() and device_get_seqnum() 2019-03-12 03:49:53 +09:00
Lennart Poettering 73622e02fb
Merge pull request #11881 from yuwata/networkd-vs-interface-renaming
Networkd vs interface renaming
2019-03-05 10:05:57 +01:00
Yu Watanabe 39a15c8a8d udev: run programs in the specified order
This fixes bugs introduced by 29448498c7
and d838e14515.

Previously, RUN and SECLABEL keys are stored in udev_list with its unique
flag is false. If the flag is false, then udev_list is just a linked
list and new entries are always added in the last.
So, we should use OrderedHashmap instead of Hashmap.

Fixes #11368.
2019-03-05 09:27:29 +01:00
Yu Watanabe a4055a608e udev: set ID_RENAMING property when interface renaming is requested
And drop the property on the corresponding 'move' uevent.
2019-03-05 10:31:35 +09:00
Yu Watanabe 6d0fdf4513 udev: do not read UdevEvent object before checking it is non-NULL 2019-03-05 10:31:20 +09:00
Yu Watanabe 589384be8d udev: drop unnecessary copy of new interface name 2019-03-05 10:02:09 +09:00
Yu Watanabe 380d19016e udev-event: make subst_format_var() always provide null-terminated string on success
Fixes #11731.
2019-02-18 12:54:53 +01:00
Zbigniew Jędrzejewski-Szmek a75211421f udev: rework how we handle the return value from spawned programs
When running PROGRAM="...", we would log
systemd-udevd[447]: Failed to wait spawned command '...': Input/output error
no matter why the program actually failed, at error level.

The code wouldn't distinguish between an internal failure and a failure in the
program being called and run sd_event_exit(..., -EIO) on any kind of error. EIO
is rather misleading here, becuase it suggests a serious error.

on_spawn_sigchld is updated to set the return code to distinguish failure to
spawn, including the program being killed by a signal (a negative return value),
and the program failing (positive return value).

The logging levels are adjusted, so that for PROGRAM= calls, which are
essentially "if" statements, we only log at debug level (unless we get a
timeout or segfault or another unexpected error).
2019-01-07 18:36:04 +01:00