Commit Graph

33 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
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 309f631d41 libudev: add missing errno initialization/error propagation (#6781)
In libudev (which es much older code than the rest of systemd), we
propagate errors in functions not returning an int, via (positive) errno
(i.e. libc-style), and as negative Exyz values in those returning an int
(much preferred, i.e. Linux kernel style). Let's fix up a few place,
where this was incorrectly done, or not done at all.

Fixes: #6613
2017-09-09 22:31:09 +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
Thomas Hindoe Paaboel Andersen b4bbcaa9c4 tree-wide: group include of libudev.h with sd-* 2015-11-17 07:06:08 +01:00
Thomas Hindoe Paaboel Andersen cf0fbc49e6 tree-wide: sort includes
Sort the includes accoding to the new coding style.
2015-11-16 22:09: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 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
Tom Gundersen 54f0b4d9a3 libudev: enumerate - accept NULL parameters in add_match()
This was a regression introduced when moving to sd-device.
2015-06-03 22:08:46 +02:00
Tom Gundersen dee5e0b6c2 sd-device: enumerator - match only on initialized devices by default
It is still possible to include uninitialized ones, but now that is opt-in. In most
cases people only want initialized devices. Exception is if you want to work without
udev running.

Suggested by David Herrmann.
2015-04-17 14:16:16 +02:00
Tom Gundersen 19c9df4492 sd-device: enumerator - don't expose add_device()
This is rarely, if ever, used. Drop it from the new public API and only keep it for
the legacy API.

Suggested by David Herrmann.
2015-04-17 14:15:47 +02:00
Tom Gundersen c32eb440ba libudev: make libudev-enumerate a thin wrapper around sd-device 2015-04-16 16:47:23 +01:00
Thomas Hindoe Paaboel Andersen 2eec67acbb remove unused includes
This patch removes includes that are not used. The removals were found with
include-what-you-use which checks if any of the symbols from a header is
in use.
2015-02-23 23:53:42 +01:00
Lennart Poettering 955d98c9c1 everywhere: make use of new0() and macro() macros, and stop using perror() 2014-02-13 14:45:51 +01:00
Lennart Poettering 725d7e6ceb libudev: always return NULL in _unref() APIs
Returning anything else but NULL would suggest the caller's reference
might still be valid, but it isn't, because the caller just invoked
_unref() after all.

This turns the return value into a typesafe shortcut that allows
unreffing and resetting a reference in one line. In contrast to
solutions for this which take a pointer to a pointer to accomplish the
same this solution is just syntactic sugar the developer can make use of
but doesn't have to, and this is particularly useful when immediately
unreffing objects returned by function calls.
2013-11-20 19:36:14 +01:00
Zbigniew Jędrzejewski-Szmek 7ff7394d9e Never call qsort on potentially NULL arrays
This extends 62678ded 'efi: never call qsort on potentially
NULL arrays' to all other places where qsort is used and it
is not obvious that the count is non-zero.
2013-10-13 17:56:54 -04:00
Lukas Nykryn 6faa3dcbb8 libudev: fix move_later comparison
At the beginning move_later is set to -1, but it is set to different
value only if expression !move_later is true.
2013-09-13 15:36:08 +02:00
Kay Sievers 756c9a2499 libudev: enumerate - do not try to match against an empty subsystem 2013-09-04 17:59:14 +02:00
David Herrmann f9e84da678 libudev: enumerate: fix NULL-deref for subsystem-matches
udev_device_get_subsystem() may return NULL if no subsystem could be
figured out by libudev. This might be due to OOM or if the device
disconnected between the udev_device_new() call and
udev_device_get_subsystem(). Therefore, we need to handle subsystem==NULL
safely.

Instead of testing for it in each helper, we treat subsystem==NULL as
empty subsystem in match_subsystem().

Backtrace of udev_enumerate with an input-device disconnecting in exactly
this time-frame:
 (gdb) bt
 #0  0x00007ffff569dc24 in strnlen () from /usr/lib/libc.so.6
 #1  0x00007ffff56d9e04 in fnmatch@@GLIBC_2.2.5 () from /usr/lib/libc.so.6
 #2  0x00007ffff5beb83d in match_subsystem (udev_enumerate=0x7a05f0, subsystem=0x0) at src/libudev/libudev-enumerate.c:727
 #3  0x00007ffff5bebb30 in parent_add_child (enumerate=enumerate@entry=0x7a05f0, path=<optimized out>) at src/libudev/libudev-enumerate.c:834
 #4  0x00007ffff5bebc3f in parent_crawl_children (enumerate=enumerate@entry=0x7a05f0, path=0x7a56b0 "/sys/devices/<shortened>/input/input97", maxdepth=maxdepth@entry=254) at src/libudev/libudev-enumerate.c:866
 #5  0x00007ffff5bebc54 in parent_crawl_children (enumerate=enumerate@entry=0x7a05f0, path=0x79e8c0 "/sys/devices/<shortened>/input", maxdepth=maxdepth@entry=255) at src/libudev/libudev-enumerate.c:868
 #6  0x00007ffff5bebc54 in parent_crawl_children (enumerate=enumerate@entry=0x7a05f0, path=path@entry=0x753190 "/sys/devices/<shortened>", maxdepth=maxdepth@entry=256) at src/libudev/libudev-enumerate.c:868
 #7  0x00007ffff5bec7df in scan_devices_children (enumerate=0x7a05f0) at src/libudev/libudev-enumerate.c:882
 #8  udev_enumerate_scan_devices (udev_enumerate=udev_enumerate@entry=0x7a05f0) at src/libudev/libudev-enumerate.c:919
 #9  0x00007ffff5df8777 in <random_caller> () at some/file.c:181
2013-09-04 17:58:56 +02:00
David Herrmann 51cc07576e libudev: fix memleak when enumerating childs
We need to free udev-devices again if they don't match. Funny that no-one
noticed it yet since valgrind is quite verbose about it.
Fix it and free non-matching devices.
2013-09-04 14:56:06 +02:00
Harald Hoyer 763a24a3b6 libudev-enumerate.c:udev_enumerate_get_list_entry() fixed possible stale pointer
If a realloc() happens in syspath_add(), the move_later pointer could
point to an invalid memory region.

Let move_later store the array index, instead of the pointer to the
entry.
2013-08-19 15:21:49 +02:00
Thomas Hindoe Paaboel Andersen 641906e936 use strneq instead of strncmp 2013-02-13 00:56:13 +01:00
Kay Sievers d5a89d7dc1 udev: move string copy functions to shared/ 2013-01-09 19:06:46 +01:00
Michael Terry e688930750 libudev: validate 'udev' argument to udev_enumerate_new()
https://bugs.freedesktop.org/show_bug.cgi?id=58289
2012-12-14 15:36:47 +01:00
Kay Sievers 88a6477ef3 libudev: update copyright headers 2012-11-12 17:50:33 +01:00
Kay Sievers dbf61afb29 udev: export udev_device_new_from_device_id() 2012-08-10 19:56:57 +02:00
Kay Sievers c195956988 udev: update docs, NEWS 2012-05-15 23:44:28 +02:00
Kay Sievers 20bbd54f60 udev: libudev - bump soname, remove deprecated functions, introduce symbol versions 2012-05-15 23:06:26 +02:00
Kay Sievers 21dbe43aec docs: hook-up gtk-doc to 'make check' 2012-04-20 03:25:36 +02:00
Kay Sievers 33502ffe2e udev: use startswith() and streq() 2012-04-16 20:54:17 +02:00
Kay Sievers 6ada823a9a udev: remove configuration options for /dev, /sys, /run directories 2012-04-16 19:20:57 +02:00
Kay Sievers bb061708d5 udev: move libudev, gudev to src/; move gudev/docs/, udev/docs/ to to docs/ 2012-04-13 19:22:06 +02:00
Renamed from src/udev/libudev-enumerate.c (Browse further)