Commit Graph

57 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
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
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
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
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
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
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 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 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
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
Michal Schmidt 23446f0148 tree-wide: add missing errno arguments to log_*_errno()
A few of the recent conversions to log_*_errno() were missing the errno
value arguments.

Fixes: e53fc357a9 "tree-wide: remove a number of invocations of
       strerror() and replace by %m"
2015-10-01 11:04:08 +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
David Herrmann bf4c113e17 sd-device: never return NULL+0
It is highly confusing if a getter function returns 0, but the value is
set to NULL. This, right now, triggers assertions as code relies on the
returned values to be non-NULL.

Like with sd-bus-creds and friends, return 0 only if a value is actually
available.

Discussed with Tom, and actually fixes real bugs as in #512.
2015-07-15 12:31:21 +02:00
David Herrmann 8927b1dad2 hashmap: fix iterators to not skip entries
Currently, the HASHMAP iterators stop at the first NULL entry in a
hashmap. This is non-obvious and breaks users like sd-device, which
legitimately store NULL values in a hashmap.

Fix all the iterators by taking a pointer to the value storage, instead of
returning it. The iterators now return a boolean that tells whether the
end of the list was reached.

Current users of HASHMAP_FOREACH() are *NOT* changed to explicitly check
for NULL. If it turns out, there were users that inserted NULL into
hashmaps, but didn't properly check for it during iteration, then we
really want to find those and fix them.
2015-06-14 16:56:02 +02:00
Thomas Hindoe Paaboel Andersen 7e518afab9 fix double semicolon typo 2015-06-02 23:29:20 +02:00
Tom Gundersen 7283a80d10 sd-device: get_driver - remember if a device does not have a driver
Don't try to read it again.
2015-06-02 18:12:47 +02:00
Tom Gundersen 4189708ad0 sd-device: get_subsystem - don't complain if a device does not have a subsystem 2015-06-02 18:12:47 +02:00
Zbigniew Jędrzejewski-Szmek 53fae771bc sd-device: fix return codes on error
asprintf() does not set errno.
2015-05-20 23:44:46 -04:00
Tom Gundersen 7141e4f62c sd-device: don't retry loading uevent/db files more than once
If for whatever reason there was nothing to load or loading failed, don't keep trying.
2015-04-26 01:30:23 +02:00
Tom Gundersen 2a2137401b sd-device: don't complain if the uevent file is missing
Only 'real' devices are required to have an uevent file.
2015-04-26 01:30:23 +02:00
Tom Gundersen 107f2e2526 udevd: fix REMOVE handling
This reverts b67f944. Lazy loading of device properties does not work for devices
that are received over netlink, as these are sealed. Reinstate the unconditional
loading of the device db.

Reported by: Mantas Mikulėnas <grawity@gmail.com>.
2015-04-23 22:31:25 +02:00
Tom Gundersen 52d629010d sd-device: set_syspath - return ENODEV when passed something that is not a device 2015-04-21 14:41:13 +02:00
Tom Gundersen 08232a020b sd-device: uniformly handle missing devices
sd_device_new_from_* now returns -ENODEV when the device does not exist, and the enumerator
silently drops these errors as missing devices is exepected.
2015-04-17 14:55:40 +02:00