Commit Graph

70 Commits

Author SHA1 Message Date
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
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
Yu Watanabe 858d36c1ec path-util: introduce path_simplify()
The function is similar to path_kill_slashes() but also removes
initial './', trailing '/.', and '/./' in the path.
When the second argument of path_simplify() is false, then it
behaves as the same as path_kill_slashes(). Hence, this also
replaces path_kill_slashes() with path_simplify().
2018-06-03 23:39:26 +09: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
Zbigniew Jędrzejewski-Szmek ffb3c2bd70
Merge pull request #8554 from poettering/chase-trail-slash
fs-util: add new CHASE_TRAIL_SLASH flag for chase_symlinks()
2018-03-26 18:00:08 +02:00
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
Lennart Poettering d89e0dc81a sd-device: make use of the fact that log_debug_errno() returns the error passed in 2018-03-22 19:59:49 +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