Commit Graph

57 Commits

Author SHA1 Message Date
Yu Watanabe 31063db0b4 sd-device: keep escaped strings in DEVLINK= property
This fixes a bug introduced by 87a4d416e5.

Fixes #17772.
2020-12-08 12:35:47 +09:00
Yu Watanabe 58b30ada0b sd-device: introduce database version and save it in udev database V field 2020-11-26 06:38:38 +09:00
Yu Watanabe b9cbb08e0a sd-device: drop unwanted newline in netlink message 2020-11-18 05:32:44 +09:00
Yu Watanabe db9ecf0501 license: LGPL-2.1+ -> LGPL-2.1-or-later 2020-11-09 13:23:58 +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 87a4d416e5 sd-device: use extract_first_word() 2020-09-09 09:34:54 +02: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
Zbigniew Jędrzejewski-Szmek 9e79123884 tree-wide: use SYNTHETIC_ERRNO with log_device_* in more places 2020-07-16 22:08:12 +02:00
Yu Watanabe 9e0196b1e2 sd-device: introduce dump_device_action_table()
Will be used in later commits.
2019-06-29 04:10:14 +09:00
Yu Watanabe 270384b2d4 tree-wide: replace strjoina() with prefix_roota() 2019-06-25 01:31:26 +09:00
Lennart Poettering d8b4d14df4 util: split out nulstr related stuff to nulstr-util.[ch] 2019-03-14 13:25:52 +01:00
Yu Watanabe 5ebd3fc3c1 sd-device: store parsed ACTION= and SEQNUM= udev properties 2019-03-12 03:49:53 +09:00
Zbigniew Jędrzejewski-Szmek 311b63fef8 Remove now-unused refcnt.h 2019-03-04 14:16:39 +01:00
Yu Watanabe 5ce41697bd sd-device: fix device_copy_properties()
This fixes a bug introduced by a3ce813697.

Fixes #11652.
2019-02-06 08:36:52 +01:00
Yu Watanabe a3ce813697 sd-device: do not save e.g., DEVPATH or INTERFACE properties to udev database
Previously, device_copy_properties() copies all properties to both
sd_device::properties and ::properties_db. Thus, on move uevent,
also tentative properties, e.g. DEVPATH or INTERFACE, are stored to
::properties_db, and saved to udev database.

This makes such tentative properties be copied to only ::properties,
and thus not saved to udev database.

Fixes #9426.
2019-01-22 14:51:02 +09:00
Yu Watanabe b261494128 Revert "sd-device: ignore bind/unbind events for now"
This reverts commit 56c886dc7e.

Fixes #11277 and #11299.
2019-01-02 04:00:14 +09:00
Dmitry Torokhov 56c886dc7e sd-device: ignore bind/unbind events for now
Until systemd/udev are ready for the new events and do not flush entire
device state on each new event received, we should ignore them.
2018-12-21 19:48:13 +01:00
Zbigniew Jędrzejewski-Szmek dc5042c0a3 sd-device: pass timestamp internally as usec_t not char* 2018-12-16 20:58:45 +01:00
Zbigniew Jędrzejewski-Szmek ebcc52fad6 sd-device: reduce the number of implementations of device_read_db() we keep around
We had two very similar functions: device_read_db_aux and device_read_db,
and a number of wrappers for them:

device_read_db_aux
  ← device_read_db (in sd-device.c)
    ← all functions in sd-device.c, including sd_device_is_initialized

  ← device_read_db_force
     ← event_execute_rules_on_remove (in udev-event.c)

device_read_db (in device-private.c)
  ← functions in device_private.c (but not device_read_db_force):
    device_get_devnode_{mode,uid,gid}
    device_get_devlink_priority
    device_get_watch_handle
    device_clone_with_db
    ← called from udevadm, udev-{node,event,watch}.c

Before 7141e4f62c (sd-device: don't retry loading
uevent/db files more than once), the two implementations were the same. In that
commit, device_read_db_aux was changed. Those changes were reverted in the parent
commit, so the two implementations are now again the same except for superficial
differences. This commit removes device_read_db (in sd-device.c), and renames
device_read_db_aux to device_read_db_internal and makes everyone use this one
implementation. There should be no functional change.
2018-12-16 20:17:39 +01:00
Lennart Poettering e4de72876e util-lib: split out all temporary file related calls into tmpfiles-util.c
This splits out a bunch of functions from fileio.c that have to do with
temporary files. Simply to make the header files a bit shorter, and to
group things more nicely.

No code changes, just some rearranging of source files.
2018-12-02 13:22:29 +01:00
Yu Watanabe 8d578a2e73 sd-device: fix possible use of uninitialized value 2018-11-10 11:59:32 +01:00
Yu Watanabe c7d54daef9 sd-device: normalize debug messages 2018-11-06 15:45:22 +09:00
Yu Watanabe 78ffb476f2 sd-device: make several sd_device_get_*() accepts NULL pointer for buffer of returned value
When only the existence of the value are important, then we can set
NULL now.
2018-10-29 17:18:00 +09:00
Yu Watanabe dcfbde3a43 sd-device: make sd_device_get_*() return -ENOENT if the values are not set 2018-09-23 17:18:19 +09:00
Yu Watanabe ad5944d71c sd-device: introduce device_new_from_stat_rdev() 2018-08-23 04:57:39 +09:00
Yu Watanabe db2f8a2e8a tree-wide: add a space after (void) 2018-07-03 10:44:28 +02:00
Zbigniew Jędrzejewski-Szmek d9b02e1697 tree-wide: drop copyright headers from frequent contributors
Fixes #9320.

for p in Shapovalov Chevalier Rozhkov Sievers Mack Herrmann Schmidt Rudenberg Sahani Landden Andersen Watanabe; do
  git grep -e 'Copyright.*'$p -l|xargs perl -i -0pe 's|/([*][*])?[*]\s+([*#]\s+)?Copyright[^\n]*'$p'[^\n]*\s*[*]([*][*])?/\n*|\n|gms; s|\s+([*#]\s+)?Copyright[^\n]*'$p'[^\n]*\n*|\n|gms'
done
2018-06-20 11:58:53 +02:00
Lennart Poettering 96b2fb93c5 tree-wide: beautify remaining copyright statements
Let's unify an beautify our remaining copyright statements, with a
unicode ©. This means our copyright statements are now always formatted
the same way. Yay.
2018-06-14 10:20:21 +02:00
Lennart Poettering 818bf54632 tree-wide: drop 'This file is part of systemd' blurb
This part of the copyright blurb stems from the GPL use recommendations:

https://www.gnu.org/licenses/gpl-howto.en.html

The concept appears to originate in times where version control was per
file, instead of per tree, and was a way to glue the files together.
Ultimately, we nowadays don't live in that world anymore, and this
information is entirely useless anyway, as people are very welcome to
copy these files into any projects they like, and they shouldn't have to
change bits that are part of our copyright header for that.

hence, let's just get rid of this old cruft, and shorten our codebase a
bit.
2018-06-14 10:20:20 +02:00
Zbigniew Jędrzejewski-Szmek 11a1589223 tree-wide: drop license boilerplate
Files which are installed as-is (any .service and other unit files, .conf
files, .policy files, etc), are left as is. My assumption is that SPDX
identifiers are not yet that well known, so it's better to retain the
extended header to avoid any doubt.

I also kept any copyright lines. We can probably remove them, but it'd nice to
obtain explicit acks from all involved authors before doing that.
2018-04-06 18:58:55 +02:00
Yu Watanabe 1cc6c93a95 tree-wide: use TAKE_PTR() and TAKE_FD() macros 2018-04-05 14:26:26 +09:00
Lennart Poettering fbd0b64f44
tree-wide: make use of new STRLEN() macro everywhere (#7639)
Let's employ coccinelle to do this for us.

Follow-up for #7625.
2017-12-14 19:02:29 +01:00
Daniel Lockyer f9ecfd3bbe Replace free and reassignment with free_and_replace 2017-11-24 10:33:41 +00:00
Zbigniew Jędrzejewski-Szmek 53e1b68390 Add SPDX license identifiers to source files under the LGPL
This follows what the kernel is doing, c.f.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=5fd54ace4721fc5ce2bb5aef6318fcf17f421460.
2017-11-19 19:08:15 +01:00
Lennart Poettering 9a39e1ce31 Add handling for bind/unbind actions (#6720)
Newer kernels will emit uevents with "bind" and "unbind" actions. These
uevents will be issued when driver is bound to or unbound from a device.
"Bind" events are helpful when device requires a firmware to operate
properly, and driver is unable to create a child device before firmware
is properly loaded.

For some reason systemd validates actions and drops the ones it does not
know, instead of passing them on through as old udev did, so we need to
explicitly teach it about them.
2017-09-04 16:59:17 +03:00
Evgeny Vereshchagin f7b1c8d1fc udev: use interface before the string that interface points to is freed by device_add_property_internal (#6105)
This prevents udev from reading the data after freeing it.

See https://github.com/systemd/systemd/issues/6040#issuecomment-306589836
==264== Invalid read of size 1
==264==    at 0x4C2E112: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==264==    by 0x5943EBD: strdup (in /usr/lib/libc-2.25.so)
==264==    by 0x13E263: device_add_property_aux (sd-device.c:122)
==264==    by 0x14788C: device_add_property_internal (sd-device.c:150)
==264==    by 0x14788C: device_rename (device-private.c:786)
==264==    by 0x120DB6: udev_device_rename (libudev-device-private.c:213)
==264==    by 0x120DB6: udev_event_execute_rules (udev-event.c:895)
==264==    by 0x120DB6: worker_spawn (udevd.c:456)
==264==    by 0x1216E5: event_run (udevd.c:584)
==264==    by 0x1216E5: event_queue_start (udevd.c:823)
==264==    by 0x122213: on_uevent (udevd.c:927)
==264==    by 0x141F2F: source_dispatch (sd-event.c:2272)
==264==    by 0x142D52: sd_event_dispatch (sd-event.c:2631)
==264==    by 0x142D52: sd_event_run (sd-event.c:2690)
==264==    by 0x142D52: sd_event_loop (sd-event.c:2710)
==264==    by 0x1159CB: run (udevd.c:1643)
==264==    by 0x1159CB: main (udevd.c:1772)
==264==  Address 0x7b251a0 is 0 bytes inside a block of size 5 free'd
==264==    at 0x4C2C14B: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==264==    by 0x13E2A2: freep (alloc-util.h:57)
==264==    by 0x13E2A2: device_add_property_aux (sd-device.c:111)
==264==    by 0x147873: device_add_property_internal (sd-device.c:150)
==264==    by 0x147873: device_rename (device-private.c:781)
==264==    by 0x120DB6: udev_device_rename (libudev-device-private.c:213)
==264==    by 0x120DB6: udev_event_execute_rules (udev-event.c:895)
==264==    by 0x120DB6: worker_spawn (udevd.c:456)
==264==    by 0x1216E5: event_run (udevd.c:584)
==264==    by 0x1216E5: event_queue_start (udevd.c:823)
==264==    by 0x122213: on_uevent (udevd.c:927)
==264==    by 0x141F2F: source_dispatch (sd-event.c:2272)
==264==    by 0x142D52: sd_event_dispatch (sd-event.c:2631)
==264==    by 0x142D52: sd_event_run (sd-event.c:2690)
==264==    by 0x142D52: sd_event_loop (sd-event.c:2710)
==264==    by 0x1159CB: run (udevd.c:1643)
==264==    by 0x1159CB: main (udevd.c:1772)
==264==  Block was alloc'd at
==264==    at 0x4C2AF1F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==264==    by 0x5943EC9: strdup (in /usr/lib/libc-2.25.so)
==264==    by 0x13E263: device_add_property_aux (sd-device.c:122)
==264==    by 0x143B45: device_add_property_internal (sd-device.c:150)
==264==    by 0x143B45: device_amend.lto_priv.235 (device-private.c:454)
==264==    by 0x1387B7: device_append (device-private.c:516)
==264==    by 0x1387B7: device_new_from_nulstr (device-private.c:620)
==264==    by 0x1387B7: udev_device_new_from_nulstr (libudev-device-private.c:268)
==264==    by 0x1387B7: udev_monitor_receive_device (libudev-monitor.c:682)
==264==    by 0x11FC69: worker_spawn (udevd.c:509)
==264==    by 0x1216E5: event_run (udevd.c:584)
==264==    by 0x1216E5: event_queue_start (udevd.c:823)
==264==    by 0x122213: on_uevent (udevd.c:927)
==264==    by 0x141F2F: source_dispatch (sd-event.c:2272)
==264==    by 0x142D52: sd_event_dispatch (sd-event.c:2631)
==264==    by 0x142D52: sd_event_run (sd-event.c:2690)
==264==    by 0x142D52: sd_event_loop (sd-event.c:2710)
==264==    by 0x1159CB: run (udevd.c:1643)
==264==    by 0x1159CB: main (udevd.c:1772)
==264==
2017-06-18 11:31:30 +02:00
Vito Caputo 313cefa1d9 tree-wide: make ++/-- usage consistent WRT spacing
Throughout the tree there's spurious use of spaces separating ++ and --
operators from their respective operands.  Make ++ and -- operator
consistent with the majority of existing uses; discard the spaces.
2016-02-22 20:32:04 -08:00
Lennart Poettering 4afd3348c7 tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.

With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.

The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).

This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.

Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:

       #define _cleanup_(function) __attribute__((cleanup(function)))

Or similar, to make the gcc feature easier to use.

Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.

See #2008.
2015-11-27 19:19:36 +01:00
Lennart Poettering b5efdb8af4 util-lib: split out allocation calls into alloc-util.[ch] 2015-10-27 13:45:53 +01:00
Lennart Poettering ee104e11e3 user-util: move UID/GID related macros from macro.h to user-util.h 2015-10-27 13:25:57 +01:00
Lennart Poettering 8b43440b7e util-lib: move string table stuff into its own string-table.[ch] 2015-10-27 13:25:56 +01:00
Lennart Poettering f4f15635ec util-lib: move a number of fs operations into fs-util.[ch] 2015-10-27 13:25:56 +01:00
Lennart Poettering 6bedfcbb29 util-lib: split string parsing related calls from util.[ch] into parse-util.[ch] 2015-10-27 13:25:55 +01:00
Lennart Poettering 3ffd4af220 util-lib: split out fd-related operations into fd-util.[ch]
There are more than enough to deserve their own .c file, hence move them
over.
2015-10-25 13:19:18 +01:00
Lennart Poettering 07630cea1f util-lib: split our string related calls from util.[ch] into its own file string-util.[ch]
There are more than enough calls doing string manipulations to deserve
its own files, hence do something about it.

This patch also sorts the #include blocks of all files that needed to be
updated, according to the sorting suggestions from CODING_STYLE. Since
pretty much every file needs our string manipulation functions this
effectively means that most files have sorted #include blocks now.

Also touches a few unrelated include files.
2015-10-24 23:05:02 +02:00
Lennart Poettering e53fc357a9 tree-wide: remove a number of invocations of strerror() and replace by %m
Let's clean up our tree a bit, and reduce invocations of the
thread-unsafe strerror() by replacing it with printf()'s %m specifier.
2015-09-30 22:26:16 +02:00
Lennart Poettering dacd6cee76 tree-wide: port everything over to fflush_and_check()
Some places invoked fflush() directly with their own manual error
checking, let's unify all that by using fflush_and_check().

This also unifies the general error paths of fflush()+rename() file
writers.
2015-07-29 20:31:07 +02:00
Tom Gundersen ccc1002a1c sd-device: ensure update_properties_buf() is a noop on failure
Don't clobber the sd_device struct, and don't leak memory when memory allocation fails.
2015-06-01 16:28:58 +02:00
Martin Pitt d854ba50a8 sd-device: fix invalid property strv pointers
In device_update_properties_bufs(), the strv is built from pointers into the
single nul-terminated buf_nulstr string, to avoid allocating the key=value
strings twice. However, we must not do that while building and
GREEDY_REALLOC0()'ing buf_nulstr, as each time when this actually reallocates
memory the pointers we wrote into buf_strv so far become invalid.

So change the logic to first completely build the new buf_nulstr, and then
iterate over it to pick out the pointers to the individual key=value strings
for properties_strv.

This fixes invalid environment for udev callouts.
2015-06-01 12:04:37 +02:00