Commit graph

86 commits

Author SHA1 Message Date
Lennart Poettering ae2a15bc14 macro: introduce TAKE_PTR() macro
This macro will read a pointer of any type, return it, and set the
pointer to NULL. This is useful as an explicit concept of passing
ownership of a memory area between pointers.

This takes inspiration from Rust:

https://doc.rust-lang.org/std/option/enum.Option.html#method.take

and was suggested by Alan Jenkins (@sourcejedi).

It drops ~160 lines of code from our codebase, which makes me like it.
Also, I think it clarifies passing of ownership, and thus helps
readability a bit (at least for the initiated who know the new macro)
2018-03-22 20:21:42 +01:00
Yu Watanabe 2e1ec12ec3 sd-device: support the case that /sys is a symlink
When /sys is a symlink to the sysfs mountpoint, e.g. /path/to/sysfs.
Then, device->syspath was set to like /path/to/sysfs/devices/foo/baz.
This converts the path to /sys/devices/foo/baz.

Fixes #7676.
2018-01-08 01:36:08 +09:00
Yu Watanabe 5bbe8eab34 sd-device: use chase_symlinks() 2018-01-08 01:35:44 +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
Shawn Landden 4831981d89 tree-wide: adjust fall through comments so that gcc is happy
Distcc removes comments, making the comment silencing
not work.

I know there was a decision against a macro in commit
ec251fe7d5
2017-11-20 13:06:25 -08: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
Yu Watanabe 945c2931bb libsystemd: use IN_SET macro 2017-09-28 17:37:59 +09: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
Zbigniew Jędrzejewski-Szmek 72cdb3e783 build-sys: drop automake support
v2:
- also mention m4
2017-07-18 10:04:44 -04: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
Evgeny Vereshchagin 3bd82598a1 udev: stop freeing value after using it for setting sysattr (#6094)
This prevents udev from double-freeing and crashing.

See https://github.com/systemd/systemd/issues/6040#issuecomment-306589836
==351== Invalid free() / delete / delete[] / realloc()
==351==    at 0x4C2C14B: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==351==    by 0x13CBE8: hashmap_clear_free_free (hashmap.c:900)
==351==    by 0x13CBE8: hashmap_free_free_free (hashmap.c:852)
==351==    by 0x147F4F: sd_device_unref (sd-device.c:88)
==351==    by 0x130CCC: udev_device_unref (libudev-device.c:552)
==351==    by 0x130CD5: udev_device_unref (libudev-device.c:553)
==351==    by 0x11FBBB: worker_spawn (udevd.c:488)
==351==    by 0x1216E5: event_run (udevd.c:584)
==351==    by 0x1216E5: event_queue_start (udevd.c:823)
==351==    by 0x122213: on_uevent (udevd.c:927)
==351==    by 0x141F2F: source_dispatch (sd-event.c:2272)
==351==    by 0x142D52: sd_event_dispatch (sd-event.c:2631)
==351==    by 0x142D52: sd_event_run (sd-event.c:2690)
==351==    by 0x142D52: sd_event_loop (sd-event.c:2710)
==351==    by 0x1159CB: run (udevd.c:1643)
==351==    by 0x1159CB: main (udevd.c:1772)
==351==  Address 0x81745b0 is 0 bytes inside a block of size 1 free'd
==351==    at 0x4C2C14B: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==351==    by 0x1447F0: freep (alloc-util.h:57)
==351==    by 0x1447F0: sd_device_set_sysattr_value (sd-device.c:1859)
==351==    by 0x132081: udev_device_set_sysattr_value (libudev-device.c:849)
==351==    by 0x12E777: set_trackpoint_sensitivity (udev-builtin-keyboard.c:180)
==351==    by 0x12E777: builtin_keyboard.lto_priv.170 (udev-builtin-keyboard.c:263)
==351==    by 0x14D03F: udev_builtin_run.constprop.75 (udev-builtin.c:133)
==351==    by 0x11FAEB: udev_event_execute_run (udev-event.c:957)
==351==    by 0x11FAEB: worker_spawn (udevd.c:461)
==351==    by 0x1216E5: event_run (udevd.c:584)
==351==    by 0x1216E5: event_queue_start (udevd.c:823)
==351==    by 0x122213: on_uevent (udevd.c:927)
==351==    by 0x141F2F: source_dispatch (sd-event.c:2272)
==351==    by 0x142D52: sd_event_dispatch (sd-event.c:2631)
==351==    by 0x142D52: sd_event_run (sd-event.c:2690)
==351==    by 0x142D52: sd_event_loop (sd-event.c:2710)
==351==    by 0x1159CB: run (udevd.c:1643)
==351==    by 0x1159CB: main (udevd.c:1772)
==351==  Block was alloc'd at
==351==    at 0x4C2CF35: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==351==    by 0x144853: sd_device_set_sysattr_value (sd-device.c:1888)
==351==    by 0x132081: udev_device_set_sysattr_value (libudev-device.c:849)
==351==    by 0x12E777: set_trackpoint_sensitivity (udev-builtin-keyboard.c:180)
==351==    by 0x12E777: builtin_keyboard.lto_priv.170 (udev-builtin-keyboard.c:263)
==351==    by 0x14D03F: udev_builtin_run.constprop.75 (udev-builtin.c:133)
==351==    by 0x11FAEB: udev_event_execute_run (udev-event.c:957)
==351==    by 0x11FAEB: worker_spawn (udevd.c:461)
==351==    by 0x1216E5: event_run (udevd.c:584)
==351==    by 0x1216E5: event_queue_start (udevd.c:823)
==351==    by 0x122213: on_uevent (udevd.c:927)
==351==    by 0x141F2F: source_dispatch (sd-event.c:2272)
==351==    by 0x142D52: sd_event_dispatch (sd-event.c:2631)
==351==    by 0x142D52: sd_event_run (sd-event.c:2690)
==351==    by 0x142D52: sd_event_loop (sd-event.c:2710)
==351==    by 0x1159CB: run (udevd.c:1643)
==351==    by 0x1159CB: main (udevd.c:1772)
2017-06-06 21:47:47 -04:00
Paul Kocialkowski a918b6738a sd-device: Try /sys/firmware for sysname to allow device-tree (#5837)
This adds /sys/firmware lookup for sysname when creating a new device,
which allows device-tree properties lookup. This look-up can then be
used in udev rules, allowing device-tree-based model detection.
2017-06-06 11:27:30 -04:00
Zbigniew Jędrzejewski-Szmek 25f027c5ef tree-wide: when %m is used in log_*, always specify errno explicitly
All those uses were correct, but I think it's better to be explicit.
Using implicit errno is too error prone, and with this change we can require
(in the sense of a style guideline) that the code is always specified.

Helpful query: git grep -n -P 'log_[^s][a-z]+\(.*%m'
2017-05-19 14:24:03 -04:00
AsciiWolf 13e785f7a0 Fix missing space in comments (#5439) 2017-02-24 18:14:02 +01:00
Zbigniew Jędrzejewski-Szmek 2fa4861ad5 sd-device: replace lstat() + open() with open(O_NOFOLLOW)
Coverity was complaining about TOCTOU (CID #745806). Indeed, it seems better
to open the file and avoid the stat altogether:

- O_NOFOLLOW means we'll get ELOOP, which we can translate to EINVAL as before,
- similarly, open(O_WRONLY) on a directory will fail with EISDIR,
- and finally, it makes no sense to check access mode ourselves: just let
  the kernel do it and propagate the error.

v2:
- fix memleak, don't clober input arg
2017-02-20 16:03:42 -05:00
Zbigniew Jędrzejewski-Szmek ec251fe7d5 tree-wide: adjust fall through comments so that gcc is happy
gcc 7 adds -Wimplicit-fallthrough=3 to -Wextra. There are a few ways
we could deal with that. After we take into account the need to stay compatible
with older versions of the compiler (and other compilers), I don't think adding
__attribute__((fallthrough)), even as a macro, is worth the trouble. It sticks
out too much, a comment is just as good. But gcc has some very specific
requiremnts how the comment should look. Adjust it the specific form that it
likes. I don't think the extra stuff we had in those comments was adding much
value.

(Note: the documentation seems to be wrong, and seems to describe a different
pattern from the one that is actually used. I guess either the docs or the code
will have to change before gcc 7 is finalized.)
2017-01-31 14:04:55 -05:00
Reverend Homer 8fb3f00997 tree-wide: replace all readdir cycles with FOREACH_DIRENT{,_ALL} (#4853) 2016-12-09 10:04:30 +01:00
Lennart Poettering e187369587 tree-wide: stop using canonicalize_file_name(), use chase_symlinks() instead
Let's use chase_symlinks() everywhere, and stop using GNU
canonicalize_file_name() everywhere. For most cases this should not change
behaviour, however increase exposure of our function to get better tested. Most
importantly in a few cases (most notably nspawn) it can take the correct root
directory into account when chasing symlinks.
2016-12-01 00:25:51 +01:00
Zbigniew Jędrzejewski-Szmek 605405c6cc tree-wide: drop NULL sentinel from strjoin
This makes strjoin and strjoina more similar and avoids the useless final
argument.

spatch -I . -I ./src -I ./src/basic -I ./src/basic -I ./src/shared -I ./src/shared -I ./src/network -I ./src/locale -I ./src/login -I ./src/journal -I ./src/journal -I ./src/timedate -I ./src/timesync -I ./src/nspawn -I ./src/resolve -I ./src/resolve -I ./src/systemd -I ./src/core -I ./src/core -I ./src/libudev -I ./src/udev -I ./src/udev/net -I ./src/udev -I ./src/libsystemd/sd-bus -I ./src/libsystemd/sd-event -I ./src/libsystemd/sd-login -I ./src/libsystemd/sd-netlink -I ./src/libsystemd/sd-network -I ./src/libsystemd/sd-hwdb -I ./src/libsystemd/sd-device -I ./src/libsystemd/sd-id128 -I ./src/libsystemd-network --sp-file coccinelle/strjoin.cocci --in-place $(git ls-files src/*.c)

git grep -e '\bstrjoin\b.*NULL' -l|xargs sed -i -r 's/strjoin\((.*), NULL\)/strjoin(\1)/'

This might have missed a few cases (spatch has a really hard time dealing
with _cleanup_ macros), but that's no big issue, they can always be fixed
later.
2016-10-23 11:43:27 -04:00
Lennart Poettering 429b435026 sd-device/networkd: unify code to get a socket for issuing netdev ioctls on
As suggested here:

https://github.com/systemd/systemd/pull/4296#issuecomment-251911349

Let's try AF_INET first as socket, but let's fall back to AF_NETLINK, so that
we can use a protocol-independent socket here if possible. This has the benefit
that our code will still work even if AF_INET/AF_INET6 is made unavailable (for
exmple via seccomp), at least on current kernels.
2016-10-06 19:04:01 +02:00
Torstein Husebø 61233823aa treewide: fix typos and remove accidental repetition of words 2016-07-11 16:18:43 +02:00
Tom Gundersen 21d6220fe0 sd-device: new_from_subsystem_sysnam - support a real subsystem called 'drivers'
We support writing out tags and db files in case a real subsystem called
'drivers' exists, so there is no reason to refuse parsing it.
2016-06-27 09:58:59 +02:00
Tom Gundersen de7e983ea1 sd-device: device_id - set correctly for 'drivers'
The 'drivers' pseudo-subsystem needs special treatment. These pseudo-devices are
found under /sys/bus/drivers/, so needs the real subsystem encoded
in the device_id in order to be resolved.

The reader side already assumed this to be the case.
2016-06-27 09:54:20 +02:00
Tom Gundersen 3172836b2f sd-device: enumerator - do not abort enumeration if a device fails
Collect the errors and return to the caller, but continue enumerating all devices.
2016-06-26 23:03:31 +02:00
Zbigniew Jędrzejewski-Szmek 755700bbd4 Udevadm trivial cleanups (#3331)
* udevadm-info: use _cleanup_

* udevadm-info: propagate return value from export_devices()

* sd-device: add comment and remove unnecessary braces
2016-05-24 12:07:42 +02:00
Tom Gundersen bee26651fc sd-device: udev-db - handle properties with empty value (#3330)
The statemachine was unable to parse properties with empty values,
reported in [0].

When reaching the start of the KEY, we would unconditionally read
one more character before starting to look for the end-of-line.
Simply look for the end-of-line from the first character.

[0]: <https://bugzilla.redhat.com/show_bug.cgi?id=1338823>
2016-05-23 19:34:29 -04:00
Zbigniew Jędrzejewski-Szmek ccddd104fc tree-wide: use mdash instead of a two minuses 2016-04-21 23:00:13 -04:00
Martin Pitt 817ec8cc81 sd-device: Allocate enough room for tags string
Fix commit 1d88a2: We need to allocate another byte for building the "tags"
string, as we append an extra ':' and still need the NUL terminator.
2016-04-06 23:50:39 +02:00
Martin Pitt 1d88a271a6 sd-device: fix crash if a device has many tags or devlinks
strjoina() is unsafe to be used in an unbounded loop as alloca() has no error
reporting. Thus devices with a large number of tags or devlinks trigger a
segfault in device_properties_prepare() due to overflowing the stack.

Rewrite the building of the "tags" and "devlinks" strings using
GREEDY_REALLOC() and strpcpy() to work with arbitrarily long strings. This also
avoids re-copying the entire string in each loop iteration.

Before this commit we always appended one final ":" to "tags". Change this to
start with an iniital ":" and for each tag append instead of prepend a ":".
This unifies what happens for the first and all subsequent tags so that we can
use a for loop.

Fixes #2954
2016-04-05 08:55:34 +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
Tom Gundersen 7c7c0cbe64 Merge pull request #2685 from poettering/lldp-fixes2
lldp fixes, second iteration
2016-02-22 17:38:34 +01:00
Zbigniew Jędrzejewski-Szmek bccfe92e46 sd-device: use (void) before set_iterate calls
set_iterate sets the output argument to NULL on error, and the return
value is not used in this case.

CID #1306804-09.
2016-02-20 19:37:10 -05:00
Lennart Poettering 0ef6f45425 tree-wide: place #pragma once at the same place everywhere
Usually, we place the #pragma once before the copyright blurb in header files,
but in a few cases we didn't. Move those around, so that we do the same thing
everywhere.
2016-02-20 22:42:29 +01:00
Daniel Mack b26fa1a2fb tree-wide: remove Emacs lines from all files
This should be handled fine now by .dir-locals.el, so need to carry that
stuff in every file.
2016-02-10 13:41:57 +01:00
Zbigniew Jędrzejewski-Szmek 97c94b9865 sd-device: initialize variables to avoid warning
The code is correct, assuming that the kernel does not feed
us garbled data. Let's initialize those variables to avoid the
warning anyway.
2016-01-18 15:21:27 -05: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 6ad623a3f7 parse-util: introduce parse_ifindex() and make use of it everywhere
We have enough places where we parse an ifindex, hence introduce a
proper parsing function for it, that verifies all parameters.
2015-11-03 00:02:00 +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 8fcde01280 util-lib: split stat()/statfs()/stavfs() related calls into stat-util.[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 a09561746f util-lib: introduce dirent-util.[ch] for directory entry calls
Also, move a couple of more path-related functions to path-util.c.
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 58ce77339c build-sys: add missing makefile symlink 2015-10-26 01:24:39 +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
Kay Sievers a9ec9f2942 libsystemd: sd-device - translate / vs. ! in sysname
The kernel replaces '/' in device names with '!', we translate that back
to '/' in sysname, when taking sysname as input, we should translate it
back again.
2015-10-06 00:41:32 +02:00
Michal Schmidt 19c2985380 sd-device: fix format strings after conversion to log_*_errno()
Use %m where previously %s was used together with strerrno().

Fixes: e53fc357a9 "tree-wide: remove a number of invocations of
       strerror() and replace by %m"
2015-10-01 11:18:05 +02:00