Commit Graph
95 Commits
Author SHA1 Message Date
Daan De Meyer 69a283c5f2 shared: Clean up includes
Split out of #37344.
2025-05-24 14:00:44 +02:00
Daan De Meyer e548ca38b9 device-util: Reduce transitive includes 2025-05-07 22:04:46 +09:00
Yu Watanabe 90670111a9 udev: make udevadm and friends not warn about unknown settings
Without this change, when e.g. event_timeout= is specified in udev.conf,
udevadm and friends which loads udev.conf warn about unknown key:
===
$ udevadm info /sys/class/net/lo
/run/udev/udev.conf.d/test-17.conf:1: Unknown key 'event_timeout', ignoring.
/run/udev/udev.conf.d/test-17.conf:2: Unknown key 'timeout_signal', ignoring.
===

Follow-up for 07f5e35fe7 (v256).
2025-03-21 12:51:39 +00:00
Yu Watanabe df7cef0940 udev-util: drop unnecessary inclusion of missing_threads.h
Follow-up for a3df693799.
2025-02-26 18:07:51 +09:00
Yu Watanabe 54e6172550 tree-wide: use device_is_processed() more 2024-04-05 01:49:20 +09:00
Yu Watanabe 3df2b4557a udev-util: rename device_is_processing() -> device_is_processed()
And make it also check the existence of the udev database.
2024-04-05 01:48:59 +09:00
Zbigniew Jędrzejewski-Szmek 6378f257e7 various: use new config loader instead of config_parse_config_file()
This means the main config file is loaded also from /run and /usr.

We should load the main config file from all the places where we load drop-ins.

I realize I had a giant blind spot: I always assumed that we load config files
from /etc, /run, /usr/local/lib, /usr/lib. But it turns out that we only used
those paths for drop-ins. For the main config file, we only looked in /etc. The
docs actually partially described this behaviour, i.e. most SYNOPSIS sections
and some parts of the text, but not others.

This is strange, because 6495361c7d was completely
bogus with the behaviour before this patch. We had a huge discussion before it
was merged, and clearly nobody noticed this. Similarly, in the previous version
of the current pull request, we had a long discussion about the appropriate
order of directories, and apparently nobody noticed that there was no order,
because only looked in one directory. So the blind spot seems to have been
shared.

Also, systemd-analyze cat-config behaved incorrectly, i.e. its behaviour matches
the new behaviour.

Possibly, in the future it'll make it easier to add support for --root.
2024-03-07 19:14:36 +01:00
Zbigniew Jędrzejewski-Szmek e5abff372d shared/conf-parser: collapse pkgdir and conf_file args into one
This essentially reverts 5656cdfeea. I find it
much easier to understand what is going on when the
path-relative-to-the-search-path is passed in full, instead of being constructed
from two parts, with one of the parts being implicit in some places.

Also, we call 'systemd-analyze cat-config <path>' with <path> with the same
meaning, so this makes the internal and external APIs more consistent.
2024-03-07 18:49:44 +01:00
Yu Watanabe f30a47c45e udev-util: introduce device_is_processing() helper function 2024-02-12 01:14:48 +09:00
Yu Watanabe 1e7c8fe494 udev-util: use device_get_property_bool() at one more place 2024-02-12 01:13:50 +09:00
Yu Watanabe a3df693799 udev-util: introduce reset_cached_udev_availability() 2024-01-19 15:06:08 +09:00
David Tardon 09dd8e77fc udev: factor out config parser call into function
... which is then called from both places. This makes sure that the
configuration is parsed by udevd and other tools in exactly the same
way.
2024-01-12 05:12:42 +09:00
David Tardon a1c7dc7d16 udev-util: drop unused function 2024-01-12 05:11:18 +09:00
David Tardon 07f5e35fe7 udev-util: use config. parser to parse udev.conf 2024-01-12 05:11:14 +09:00
David Tardon f20ae7dbdf udev-util: add wrapper for sd_device_get_property_value()
... that allows to pass additional properties to fall back to.
2023-11-13 19:18:23 +01:00
Lennart Poettering 3cc7a9fd25 udev-util: add generic device_get_{vendor,model}_string() helpers
We chck the same props in various places, add a single implementation of
a call to inquire this.
2023-11-13 16:24:17 +01:00
Yu Watanabe c0af9dee0d udev: move declaration of ResolveNameTiming to udev-rules.c
It is only used by udevd and udevadm, not necessary in src/shared.

This also moves UDEV_NAME_SIZE and friends.
2023-07-31 23:23:00 +09:00
Yu Watanabe 04a1ee5867 udev: move udev_parse_config_full() to udevd.c
Then, rename it to manager_parse_udev_config().

No functional change, just refactoring.
2023-07-31 23:23:00 +09:00
Yu Watanabe 0226729181 udev: move several functions from udev-util.c to relevant udevd source files
The functions are only used by udevd (and relevant tests), hence it is
not necessary to be in src/shared.
2023-07-31 23:23:00 +09:00
Yu Watanabe 9daec33938 udev-util: drop udev_queue_init() from shared
It is only used in libudev, let's move it.
2023-07-31 23:23:00 +09:00
Daan De Meyer a1af83728f device-util: Declare iterator variables inline 2023-07-12 20:05:18 +02:00
Lennart Poettering af4e8e86ec battery-util: split out code that checks AC power state into its own .c/.h pair
No code change, just some splitting out of the relevant code from
udev-util.[ch].

This makes sense on its own, but is also prepartion to move the code
that checks for low battery state into battery-util.[ch], too.
2023-06-05 12:18:21 +02:00
Dmitry V. Levin c43ff248f9 udev-rules: fix matching of token types that support alternative patterns
For those token types that support matching of alternative patterns,
their token values are interpreted as nulstr, so make sure the parser
does the right thing and makes these token values terminated by two
subsequent NULs so they could be safely interpreted as nulstr.

Before this fix, the following rules would result to "echo foo" invocation:
  ENV{foo}=", RUN"
  ENV{foo}=="bar", RUN+="echo foo"
because the value of `ENV{foo}` is treated as nulstr, and it used to match
against alternative patterns, in this case `bar`, `, RUN`, and `="echo foo`.

Fixes: 25de7aa7b9 ("udev: modernize udev-rules.c")
2023-03-19 11:32:09 +00:00
Zbigniew Jędrzejewski-Szmek 254d1313ae tree-wide: use -EBADF for fd initialization
-1 was used everywhere, but -EBADF or -EBADFD started being used in various
places. Let's make things consistent in the new style.

Note that there are two candidates:
EBADF 9 Bad file descriptor
EBADFD 77 File descriptor in bad state

Since we're initializating the fd, we're just assigning a value that means
"no fd yet", so it's just a bad file descriptor, and the first errno fits
better. If instead we had a valid file descriptor that became invalid because
of some operation or state change, the other errno would fit better.

In some places, initialization is dropped if unnecessary.
2022-12-19 15:00:57 +01:00
Yu Watanabe 1c03f7f4ba ac-power: check battery existence and status
If a battery is not present or its status is not discharging, then
the battery should not be used as a power source.
Let's count batteries currently discharging.

Fixes #25316.
2022-11-13 17:35:27 +09:00
Yu Watanabe 17bf3c550d tree-wide: set description for device manager 2022-10-19 04:46:24 +09:00
David Tardon 995340074e tree-wide: use ASSERT_PTR more 2022-09-13 08:13:27 +02:00
Yu Watanabe 4f89ce0c91 udev-util: make device_wait_for_initialization() take relative timeout
Also make the timer event source floating.
2022-08-31 23:12:46 +09:00
Yu Watanabe f81b3e90e4 udev-util: replace device_new_from_dev_path() with sd_device_new_from_devname() 2022-08-31 23:12:46 +09:00
Yu Watanabe 8676bdb708 udev-util: minor cleanups for on_ac_power()
Follow-ups for #24420.
2022-08-28 21:46:45 +01:00
Zbigniew Jędrzejewski-Szmek 3c69e94a5c on-ac-power: ignore devices with scope==Device
My mouse is reported as:

P: /devices/pci0000:00/0000:00:14.0/usb1/1-4/1-4.4/1-4.4:1.2/0003:046D:C52B.001E/0003:046D:4051.001F/power_supply/hidpp_battery_4
M: hidpp_battery_4
R: 4
U: power_supply
E: DEVPATH=/devices/pci0000:00/0000:00:14.0/usb1/1-4/1-4.4/1-4.4:1.2/0003:046D:C52B.001E/0003:046D:4051.001F/power_supply/hidpp_battery_4
E: SUBSYSTEM=power_supply
E: POWER_SUPPLY_NAME=hidpp_battery_4
E: POWER_SUPPLY_TYPE=Battery
E: POWER_SUPPLY_ONLINE=1
E: POWER_SUPPLY_STATUS=Discharging
E: POWER_SUPPLY_SCOPE=Device
E: POWER_SUPPLY_MODEL_NAME=Wireless Mouse M510
E: POWER_SUPPLY_MANUFACTURER=Logitech
E: POWER_SUPPLY_SERIAL_NUMBER=4051-bc-cd-d2-5b
E: POWER_SUPPLY_CAPACITY_LEVEL=Normal

See https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=25a0bc2dfc2ea732f40af2dae52426ead66ae76e
Effectively, "System" and "Unkown" are passed through, "Device" is rejected.
2022-08-23 17:04:31 +02:00
Zbigniew Jędrzejewski-Szmek 4a52514b37 on-ac-power: rework logic
History of the function:
96788d2aa4 assume system is running on AC power when no battery found
795e86b4f1 ignore USB-C ports in power source mode when detecting system is running on AC power
c19a51bec4 invert ac_power() source type check
6d89003462 assume ac when /sys/class/power_supply is missing
240dbaa44f add ConditionACPower=

Interestingly, the return condition 'on_ac_power == found_online || !found_offline'
was there from the very beginning, and even Yu's latest change doesn't change this,
but only extends it to 'on_ac_power == found_online || !found_offline || !found_battery'.
This means that any system with no AC power supply will be unconditionally classified
as on_ac_power.

Let's change the logic: if we have an online AC supply, answer is "yes". If no
supplies, but we have a battery, answer is "no". Otherwise, assume "yes", based
on the assumption that presense of a battery would at least be always reported,
even if an AC power supply might not be.

Fixes #24407. It also shouldn't impact previous fixes: assume ac when
/sys/class/power_supply is missing, ignore USB-C ports in power source mode,
assume system is running on AC power when no battery found.
2022-08-23 17:04:31 +02:00
Zbigniew Jędrzejewski-Szmek 406fbeca32 shared/udev-util: say "ignoring device", not "ignoring"
The short form sounds like we're ignoring the error.
2022-08-23 16:55:30 +02:00
Yu Watanabe 96788d2aa4 udev-util: assume system is running on AC power when no battery found
Fixes #24214.
2022-08-06 03:42:32 +09:00
Yu Watanabe 1c6e17e5d1 udev-util: align string table 2022-08-06 03:35:04 +09:00
nl6720 0e68582323 tree-wide: link to docs.kernel.org for kernel documentation
https://www.kernel.org/ links to https://docs.kernel.org/ for the documentation.
See https://git.kernel.org/pub/scm/docs/kernel/website.git/commit/?id=ebc1c372850f249dd143c6d942e66c88ec610520

These URLs are shorter and nicer looking.
2022-07-04 19:56:53 +02:00
Yu Watanabe a1af9668ec udev: make newer event also blocked by DEVPATH_OLD
Previously, a device has DEVPATH_OLD is blocked by a previous event
whose devpath is equivalent to the DEVPATH_OLD.

This extends the condtion.

1. an event has DEVPATH_OLD is blocked by a previous event whose
   devpath is a parent of, child of, or equivalent to the DEVPATH_OLD.

2. an event is blocked by a previous event whose DEVPATH_OLD is a
   parent of, child of, or equivalent to the devpath of the new event.

I am not sure such check is really necessary. But, the cost of the check
is expected to be extremely small, as device renaming does not occur so
frequently. Hence, it should not introduce any significant performance
regression.
2022-05-04 17:16:14 +09:00
Yu Watanabe 9fa31df62d udev-util: add parentheses to make coverity silent
Fixes CID#1474365.
2022-02-18 08:35:35 +01:00
Yu Watanabe f92c5bb18c udev-util: introduce udev_available() helper function 2022-02-17 18:51:57 +09:00
Lennart Poettering b74163607b sd128: export sd_id128_to_uuid_string()
We expose various other forms of UUID helpers already, i.e.
SD_ID128_UUID_FORMAT_STR and SD_ID128_MAKE_UUID_STR(), and we parse
UUIDs, hence add a high-level helper for formatting UUIDs too.

This doesn't add any new code, it just moves some helpers
id128-util.[ch] → sd-id128.[ch], to make them public.
2022-02-14 15:13:23 +01:00
Yu Watanabe e916276016 udev-util: add event UUID to debugging logs 2022-01-19 20:48:36 +09:00
Yu Watanabe 795e86b4f1 udev-util: ignore USB-C ports in power source mode when detecting system is running on AC power
Fixes #21988.
2022-01-06 18:28:56 +09:00
Yu Watanabe 01d4ad3bde udev-util: re-implement on_ac_power() with sd-device 2022-01-06 18:28:50 +09:00
Yu Watanabe 06795b02e3 util: move on_ac_power() from util.c -> udev-util.c 2022-01-06 18:06:22 +09:00
Zbigniew Jędrzejewski-Szmek e437538f35 tree-wide: make cunescape*() functions return ssize_t
Strictly speaking, we are returning the size of a memory chunk of
arbitrary size, so ssize_t is more appropriate than int.
2021-07-09 15:07:40 +02:00
Yu Watanabe e1ecfef16f udev-util: introduce udev_replace_ifname() 2021-06-23 16:02:05 +09:00
Yu Watanabe f5fbe71d95 tree-wide: use UINT64_MAX or friends 2021-03-05 07:10:13 +09:00
Lennart Poettering 930aa88f04 tree-wide: use sd_device_new_from_stat_rdev() whereever appropriate 2021-02-22 17:38:57 +01:00
Yu Watanabe acfc2a1d15 sd-device: ignore error in device_cache_sysattr_value() and propagate original error code
There are three calls of device_cache_sysattr_value(). Two of them are
just caching the value. Hence, let's ignore its failure, and propagate
original error code.

One exception is the last call in sd_device_get_sysattr_value().
Unfortunately, it returns `const char *` instead of `char *`. So,
sd_device object must have the reference of the returned value.
Hence, error in updating the cache by device_cache_sysattr_value()
is critical, and we need to propagate the error in that case.
2021-02-21 04:40:17 +09:00
Lennart Poettering a113002214 sd-device: add sd_device_get_action() + sd_device_get_seqnum() + sd_device_new_from_stat_rdev()
To make sd-device properly usable for all programs we need to provide an
API for the "action" field of an event, it's one of the most relevant
ones, and it was so far missing.

This also adds sd_device_get_seqnum(), which isn't that interesting,
except for generating pretty debug output, which we use it ourselves
for.

This also makes device_new_from_stat_rdev() public, as it is truly
useful, as we can see in our own uses of it, and I think is fairly
generic to show up in the public APIs.
2021-02-18 18:20:56 +01:00