Commit Graph

116 Commits

Author SHA1 Message Date
Yu Watanabe 6f3ac0d517 sd-device: use set_strjoin()
This slightly changes TAGS= and CURRENT_TAGS= properties:

Before
E: TAGS=:aaa:bbb:

After
E: TAGS=aaa:bbb
2020-12-08 12:28:58 +09:00
Yu Watanabe 6ece7cd2b7 sd-device: make sd_device_has_current_tag() and friends compatible with database version 0 2020-11-26 08:29:17 +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 9e25cca130 sd-device: make device_add_property_internal() inline 2020-11-26 05:58:32 +09:00
Yu Watanabe db9ecf0501 license: LGPL-2.1+ -> LGPL-2.1-or-later 2020-11-09 13:23:58 +09:00
Yu Watanabe ea2bc25762 sd-device: introduce sd_device_set_sysattr_valuef() 2020-09-11 17:41:10 +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
Zbigniew Jędrzejewski-Szmek fe20121a4c sd-device: use strjoin instead of asprintf
strjoin should be faster for string concatenation.
Also drop "_"-prefix from function prototypes.
2020-07-16 18:38:03 +02:00
Yu Watanabe 43b24a0716 sd-device: use log_device_debug_errno() 2020-07-08 12:11:19 +02:00
Zbigniew Jędrzejewski-Szmek 21c7fe6d12 sd-device: be more defensive in FOREACH_DEVICE_SYSATTR
Follow-up for fadcc12229.
2020-07-05 10:55:18 +02:00
Yu Watanabe fadcc12229 sd-device: make FOREACH_DEVICE_SYSATTR() list attributes in subdirectories
Then, `udevadm info -a` can show subdir attributes.

Closes #12191.
2020-07-03 22:21:44 +09:00
Zbigniew Jędrzejewski-Szmek be32732168 basic/set: let set_put_strdup() create the set with string hash ops
If we're using a set with _put_strdup(), most of the time we want to use
string hash ops on the set, and free the strings when done. This defines
the appropriate a new string_hash_ops_free structure to automatically free
the keys when removing the set, and makes set_put_strdup() and set_put_strdupv()
instantiate the set with those hash ops.

hashmap_put_strdup() was already doing something similar.

(It is OK to instantiate the set earlier, possibly with a different hash ops
structure. set_put_strdup() will then use the existing set. It is also OK
to call set_free_free() instead of set_free() on a set with
string_hash_ops_free, the effect is the same, we're just overriding the
override of the cleanup function.)

No functional change intended.
2020-05-06 16:54:06 +02:00
Zbigniew Jędrzejewski-Szmek 597da51bae tree-wide: make parse_ifindex simply return the index
We don't need a seperate output parameter that is of type int.  glibc() says
that the type is "unsigned", but the kernel thinks it's "int".  And the
"alternative names" interface also uses ints. So let's standarize on ints,
since it's clearly not realisitic to have interface numbers in the upper half
of unsigned int range.
2020-01-11 12:06:08 +01:00
Franck Bui 21b40f1662 fileio: introduce read_full_virtual_file() for reading virtual files in sysfs, procfs
Virtual filesystems such as sysfs or procfs use kernfs, and kernfs can work
with two sorts of virtual files.

One sort uses "seq_file", and the results of the first read are buffered for
the second read. The other sort uses "raw" reads which always go direct to the
device.

In the later case, the content of the virtual file must be retrieved with a
single read otherwise subsequent read might get the new value instead of
finding EOF immediately. That's the reason why the usage of fread(3) is
prohibited in this case as it always performs a second call to read(2) looking
for EOF which is subject to the race described previously.

Fixes: #13585.
2019-10-25 15:24:49 +02:00
Yu Watanabe f2106b1789
Merge pull request #13836 from systemd/assert-cleanups-and-constification
Assert cleanups and constification
2019-10-25 13:36:00 +09:00
Zbigniew Jędrzejewski-Szmek a5648b8094 basic/fs-util: change CHASE_OPEN flag into a separate output parameter
chase_symlinks() would return negative on error, and either a non-negative status
or a non-negative fd when CHASE_OPEN was given. This made the interface quite
complicated, because dependning on the flags used, we would get two different
"types" of return object. Coverity was always confused by this, and flagged
every use of chase_symlinks() without CHASE_OPEN as a resource leak (because it
would this that an fd is returned). This patch uses a saparate output parameter,
so there is no confusion.

(I think it is OK to have functions which return either an error or an fd. It's
only returning *either* an fd or a non-fd that is confusing.)
2019-10-24 22:44:24 +09:00
Zbigniew Jędrzejewski-Szmek 730b76bd2c sd-device: allow sd_device_get_devtype to be called with NULL arg and do not assert
We shouldn't call assert() on user-specified arguments in public functions.
While at it, let's return 1 if the type exists, and 0 otherwise.
2019-10-24 11:48:08 +02:00
Zbigniew Jędrzejewski-Szmek 7b9103a622 sd-device: voidify and simplify calls to ordered_hashmap_iterate()
Coverity CID#1402356 and CID#1402335.
2019-07-17 11:35:04 +02:00
Lennart Poettering 05b2ace147
Merge pull request #12870 from yuwata/tree-wide-further-path-join-cleanups
tree-wide: further path_join() and path_joina() cleanups
2019-06-25 09:27:01 +02:00
Lubomir Rintel c4f885e7e0 sd-device: don't accept /sys as a device path
Because it's not a device path and (slightly) bad things happen if it
gets confused with one:

  $ udevadm info /sys/
  Assertion 'device->devpath[0] == '/'' failed at
      ../src/libsystemd/sd-device/sd-device.c:958,
      function sd_device_get_devpath(). Aborting.
  Aborted (core dumped)
2019-06-25 10:22:18 +09:00
Yu Watanabe 270384b2d4 tree-wide: replace strjoina() with prefix_roota() 2019-06-25 01:31:26 +09:00
Lennart Poettering cee97d5768
Merge pull request #12836 from yuwata/tree-wide-replace-strjoin
tree-wide: replace strjoin() with path_join()
2019-06-22 20:02:46 +02:00
Yu Watanabe 657ee2d82b tree-wide: replace strjoin() with path_join() 2019-06-21 03:26:16 +09:00
Yu Watanabe 6d946490ba tree-wide: drop alloca() in loop 2019-06-20 06:29:19 +09:00
Lennart Poettering 39f2bc6e7e sd-device: use xsprintf() where appropriate 2019-04-02 14:54:42 +02:00
Yu Watanabe 5ebd3fc3c1 sd-device: store parsed ACTION= and SEQNUM= udev properties 2019-03-12 03:49:53 +09:00
Yu Watanabe b07d0f2a85 sd-device: split device_read_db_internal() into two part
The new device_read_db_internal_filename() will be used by a fuzzer.
2019-02-26 12:55:43 +09:00
Yu Watanabe 03dd7b7dde sd-device: also store properties read from udev database to sd_device::properties_db
Follow-up for a3ce813697 and
5ce41697bd.

Before a3ce813697, all properties in
src->properties and src->properties_db are mixed and copied to
dst->properties_db by device_copy_properties().
So, it is not necessary to store data from udev database file to
sd_device::properties_db before copying properties.

But now, properties are not mixed. So, the read data need to be
stored to also ::properties_db.

Fixes #11721.
2019-02-17 00:43:38 +09: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
Zbigniew Jędrzejewski-Szmek 2cfb197890 sd-device: modernize code a bit
Empty line between setting the output parameter and return is removed. I like
to think about both steps as part of returning from the function, and there's
no need to separate them.

Similarly, if we need to unset a pointer after successfully passing ownership,
use TAKE_PTR and do it immediately after the ownership change, without an empty
line inbetween.
2019-01-08 22:14:06 +01:00
Yu Watanabe 18fee12a2d sd-device: fix segfault when error occurs in device_new_from_{nulstr,strv}()
As devpath may not be set yet.

When debug logging is enabled, log_device_*() calls
sd_device_get_sysname(). So, we should not assume that devpath is always
set.

Fixes #11258.
2018-12-30 22:47:20 +09: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
Zbigniew Jędrzejewski-Szmek cd53c8f97d sd-device: attempt to read db again if it wasn't found
This mostly reverts "sd-device: don't retry loading uevent/db files more than
once", 7141e4f62c. We will retry if we couldn't
access the file, but not if parsing failed.

Not re-reading the database at all just doesn't seem like a good idea. We have
two implementations of device_read_db, and one does that, and the other retries
to read the db. Re-reading seems more useful, since we can create the object
and then access properties as some later time when we know that the device has
been initialized and we can get useful results. Otherwise, we force the user to
destroy this object and create a new one.

This changes device_read_uevent_file() and device_read_db_aux(). See next
commit for description of where those functions are used.
2018-12-16 19:52:58 +01:00
Yu Watanabe ef118d00eb util: drop missing.h from socket-util.h 2018-12-06 13:31:16 +01:00
Lennart Poettering 3a47c40d97 tree-wide: port various parts of the code to use parse_dev() 2018-11-29 20:03:56 +01:00
Zbigniew Jędrzejewski-Szmek e5e0a79623 pid1,sd-device: use PATH_STARTSWITH_SET more 2018-11-23 13:37:47 +01:00
Zbigniew Jędrzejewski-Szmek baaa35ad70 coccinelle: make use of SYNTHETIC_ERRNO
Ideally, coccinelle would strip unnecessary braces too. But I do not see any
option in coccinelle for this, so instead, I edited the patch text using
search&replace to remove the braces. Unfortunately this is not fully automatic,
in particular it didn't deal well with if-else-if-else blocks and ifdefs, so
there is an increased likelikehood be some bugs in such spots.

I also removed part of the patch that coccinelle generated for udev, where we
returns -1 for failure. This should be fixed independently.
2018-11-22 10:54:38 +01:00
Lennart Poettering 9edcbc4f44
Merge pull request #10602 from yuwata/sd-device-logs
update log messages
2018-11-06 19:33:51 +03:00
Yu Watanabe 7cadbe09e9 sd-device: use write_string_file() helper to write sysattr 2018-11-06 21:24:03 +09:00
Yu Watanabe c7d54daef9 sd-device: normalize debug messages 2018-11-06 15:45:22 +09:00
Yu Watanabe 5a937ea2f6 sd-device: make sd_device_get_is_initialized() returns is_initialized by return value 2018-10-29 17:33:33 +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 1dfa960786 sd-device: return positive ifindex when sd_device_get_ifindex() succeeds
As valid ifindex is positive value, not non-negative value.

Follow-up for dcfbde3a43.
2018-10-27 07:03:38 +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 8aae9a66fa sd-device,libudev: make an argument for *_set_sysattr_value() const 2018-09-11 12:45:21 +09:00
Yu Watanabe 6116d2b275 sd-device: use structured initializers 2018-08-28 05:09:40 +09:00
Yu Watanabe 8301aa0bf1 tree-wide: use DEFINE_TRIVIAL_REF_UNREF_FUNC() macro or friends where applicable 2018-08-27 14:01:46 +09:00
Yu Watanabe fc95c359f6 tree-wide: use returned value from log_*_errno() 2018-08-07 15:48:37 +09:00