Commit Graph

264 Commits

Author SHA1 Message Date
Lennart Poettering 4b3b5bc71b tree-wide: port various places over to use chmod_and_chown()
Doing this properly is hard, hence let's unify the code.
2019-05-24 15:07:55 +02:00
Lennart Poettering 6cdc62aa97 udev: whitespace fix 2019-04-18 15:18:12 +02:00
Florian Dollinger 5a1ee07302 Check for final assignments in RUN keys (#12309)
As described in #12291
2019-04-15 15:59:36 +09:00
Lennart Poettering 568ee8fc46 udev: use strempty() where appropriate 2019-04-02 14:54:42 +02:00
Lennart Poettering b82f71c7ff tree-wide: constify a few static string tables 2019-03-25 14:04:34 +01:00
Yu Watanabe 7a2093236b udev: refuse to modify SEQNUM by udev rules 2019-03-12 03:49:53 +09:00
Yu Watanabe d4d690facd udevd: use device_get_action() and device_get_seqnum() 2019-03-12 03:49:53 +09:00
Franck Bui 3c37dadf62 udev: restore debug level when logging a failure in the external prog called by IMPORT{program}
It was already the case before commit a75211421f,
which upgraded the log to warning.

This seems an unintended side effect as the commit message doesn't mention it
and the old behavior looks more appropriate.
2019-03-05 13:53:25 +01:00
Yu Watanabe 39a15c8a8d udev: run programs in the specified order
This fixes bugs introduced by 29448498c7
and d838e14515.

Previously, RUN and SECLABEL keys are stored in udev_list with its unique
flag is false. If the flag is false, then udev_list is just a linked
list and new entries are always added in the last.
So, we should use OrderedHashmap instead of Hashmap.

Fixes #11368.
2019-03-05 09:27:29 +01:00
Yu Watanabe e8b2737f20 udev-rules: do not ignore short lines
Otherwise, a short line continues the previous continuation.

This fixes a bug introduced by f10aa08e3e.
2019-02-22 06:50:12 +09:00
Lennart Poettering bde06abd4f
Merge pull request #10408 from keszybz/analyze-cat-presets
systemd-analyze cat-presets
2019-02-18 16:02:45 +01:00
Zbigniew Jędrzejewski-Szmek 116b91e8cc udev: use the usual set of load paths for udev rules
This adds /usr/local/lib/udev/rules.d to the search path on non-split-usr systems.
On split-usr systems, the paths with /usr/-prefixes are added too.
In the past, on split-usr systems, it made sense to only load rules from
/lib/udev/rules.d, because /usr could be mounted late. But we don't support running
without /usr since 80758717a6, so in practice it doesn't matter whether the
rules files are in /lib/udev/rules.d or /usr/lib/udev/rules.d. Distributions
that maintain the illusion of functional split-usr are welcome to simply not put any
files in /usr/lib/udev/rules.d/.

In practice this doesn't change much, but it makes udev more consistent with the
rest of the systemd suite.
2019-02-18 10:29:33 +01:00
Yu Watanabe 72ca8f71c1 udev-rules: use parse_uid() or parse_gid() 2019-02-18 12:22:54 +09:00
Yu Watanabe 145e020ac3 udev-rules: use new() macro instead of malloc_multiply() 2019-02-18 12:22:54 +09:00
Yu Watanabe f10aa08e3e udev-rules: use read_line() and drop fgets() 2019-02-18 12:22:49 +09:00
Yu Watanabe 759fb3a904 udev-rules: use size_t for array index 2019-02-18 09:24:03 +09:00
Yu Watanabe 530727aed8 udev-rules: use GREEDY_REALLOC() macro where it applicable
This also changes types of several variables e.g. token_max to size_t.
2019-02-18 09:22:25 +09:00
Yu Watanabe 57f08d5cdd udev-rules: update log messages about OWNER= or GROUP= settings on --resolve=names=never
This also set lower log level for the messages.

6e2efb6c73 introduces the log messages.
But udevd may be started with --resolve-names=never, and the behavior
is expected.

Fixes #11720.
2019-02-16 07:21:28 +09:00
Lennart Poettering 683a1e7fd9
Merge pull request #11594 from yuwata/udev-rule-cleanups
udev-rule, ethtool: several coding style cleanups
2019-02-15 11:37:11 +01:00
Yu Watanabe 6e2efb6c73 udev-rule: make rule_add_key() return negative errno when too much tokens
As OPTIONS= rule introduce multiple tokens.

Fixes oss-fuzz#12980.
2019-02-13 04:14:21 +09:00
Yu Watanabe 76b9bdd96f udev-rule: check function retun value is negative or not, instead of non-zero
One exception is udev_event_spawn(), which returns negative or positive
value on failure.
2019-02-12 11:03:40 +09:00
Yu Watanabe 605aa52f83 udev-rule: make match_key() and match_attr() return boolean value 2019-02-12 11:03:40 +09:00
Yu Watanabe bb175a0338 udev-rule: drop unnecessary parentheses 2019-02-12 11:03:40 +09:00
Yu Watanabe 704dbfb279 udev-rule: make get_key() return negative errno 2019-02-12 11:03:40 +09:00
Yu Watanabe 1f362ff185 udev-rule: drop unnecessary assignments 2019-02-12 11:03:39 +09:00
Yu Watanabe ef660d072f udev-rule: propagate error cause in add_token() 2019-02-12 11:03:39 +09:00
Zbigniew Jędrzejewski-Szmek a6ca3c1921 udev: do logging before setting variables to NULL
gcc-9 diagnoses this as an error.
Reported by Jeff Law.
2019-01-15 13:58:45 +01:00
Topi Miettinen a1e92eee3e Remove 'inline' attributes from static functions in .c files (#11426)
Let the compiler perform inlining (see #11397).
2019-01-15 08:12:28 +01:00
Zbigniew Jędrzejewski-Szmek a75211421f udev: rework how we handle the return value from spawned programs
When running PROGRAM="...", we would log
systemd-udevd[447]: Failed to wait spawned command '...': Input/output error
no matter why the program actually failed, at error level.

The code wouldn't distinguish between an internal failure and a failure in the
program being called and run sd_event_exit(..., -EIO) on any kind of error. EIO
is rather misleading here, becuase it suggests a serious error.

on_spawn_sigchld is updated to set the return code to distinguish failure to
spawn, including the program being killed by a signal (a negative return value),
and the program failing (positive return value).

The logging levels are adjusted, so that for PROGRAM= calls, which are
essentially "if" statements, we only log at debug level (unless we get a
timeout or segfault or another unexpected error).
2019-01-07 18:36:04 +01:00
Zbigniew Jędrzejewski-Szmek 2e08871534 udev: use typedef for struct udev_event 2018-12-17 09:27:24 +01:00
Zbigniew Jędrzejewski-Szmek 9a07157dd5 udev: use typedef for struct udev_rules 2018-12-17 09:27:21 +01:00
Zbigniew Jędrzejewski-Szmek 1d79128121 udev: make udev_rules_new() return a proper error code 2018-12-14 10:20:43 +01:00
Yu Watanabe e924c60f69 udev: use hashmap_clear_free_key() and hashmap_free_free_key() 2018-12-02 12:18:54 +01:00
Yu Watanabe 49c603bd5e udev: drop duplicate whitespaces 2018-11-26 11:53:21 +09:00
Yu Watanabe b4ba2fe3f1 udev-rules: include device sysname in log messages 2018-11-26 11:37:29 +09:00
Yu Watanabe 8c19dc54d3 udev-rules: update log messages 2018-11-23 01:15:42 +09:00
Yu Watanabe 20e97dd3de meson: add option for debugging udev 2018-11-23 00:28:27 +09:00
Yu Watanabe 67e4b38563 udev-rules: trivial coding style cleanups 2018-11-23 00:22:09 +09:00
Yu Watanabe 981fae90df udev: rename udev_rules_unref() to udev_rules_free()
As udev_rules do not have a reference counter.
2018-11-23 00:07:40 +09:00
Zbigniew Jędrzejewski-Szmek baaa35ad70 coccinelle: make use of SYNTHETIC_ERRNO
Ideally, coccinelle would strip unnecessary braces too. But I do not see any
option in coccinelle for this, so instead, I edited the patch text using
search&replace to remove the braces. Unfortunately this is not fully automatic,
in particular it didn't deal well with if-else-if-else blocks and ifdefs, so
there is an increased likelikehood be some bugs in such spots.

I also removed part of the patch that coccinelle generated for udev, where we
returns -1 for failure. This should be fixed independently.
2018-11-22 10:54:38 +01:00
Yu Watanabe 3839535a53 libudev: make util_resolve_subsys_kernel() return negative errno
This also replaces udev_device by sd_device in the function.
2018-11-21 17:31:16 +09:00
Yu Watanabe 5ea78a39e5 libudev-list: move libudev-list related definitions to libudev-list-internal.h
This also rename libudev-private.h to libudev-util.h, and cleanups
several unnecessary headers from udev.h and libudev-util.h
2018-11-20 14:38:35 +09:00
Yu Watanabe a6da77b71b udev-rules: fix possible assertion
On 'remove' action, event->dev_db_clone is NULL. So, `IMPORT{db}` rule
on 'remove' action triggers assertion. This fixes the issue.
2018-11-15 15:50:27 +01:00
Lennart Poettering 042cad5737
Merge pull request #10753 from keszybz/pager-no-interrupt
Add mode in journalctl where ^C is handled by the pager
2018-11-14 20:09:39 +01:00
Zbigniew Jędrzejewski-Szmek fde32028a4 Move LONG_LINE_MAX definition to fileio.h
All users of the macro (except for one, in serialize.c), use the macro in
connection with read_line(), so they must include fileio.h.  Let's not play
libc games and require multiple header file to be included for the most common
use of a function.

The removal of def.h includes is not exact. I mostly went over the commits that
switch over to use read_line() and add def.h at the same time and reverted the
addition of def.h in those files.
2018-11-14 16:25:32 +01:00
Zbigniew Jędrzejewski-Szmek bc768f0475 udev: move ResolveNameTiming definition and parsers to udev-util.h
Follow-up for c4d44cba4d. No functional change,
but the parser is moved to libsystemd-shared.so.
2018-11-13 14:04:29 +01:00
Zbigniew Jędrzejewski-Szmek 66f737b415 udev: do not pass timeout_warn_usec around, calculate it on demand
It was always set to one third of timeout_usec, so let's simplify things by
calculating it using a helper function right before it is used.

Before 9d9264ba39, udevd.c would avoid setting
timeout_warn_usec to 0, using 1 instead. This wasn't necessary, because when
timeout_warn_usec is finally used in spawn_wait(), it is ignored if
timeout_usec is 0 or timeout_warn_usec is 0. So there was no need to handle
this case specially.
2018-11-13 14:00:32 +01:00
Yu Watanabe c4d44cba4d udev: introduce enum ResolveNameTiming for --resolve-names argument 2018-11-12 23:20:53 +09:00
Yu Watanabe cf28ad4689 udev-event: make udev_event_new() take sd_device instead of udev_device 2018-11-07 13:35:03 +09:00
Yu Watanabe cf697ec00e udev-rules: replace udev_device by sd_device in udev_rules_apply_to_event() 2018-11-07 13:35:03 +09:00
Yu Watanabe dbea7f24d2 udev-rules: replace two udev_list_entry_foreach() by corresponding FOREACH_DEVICE_*() macros 2018-11-07 13:35:03 +09:00
Yu Watanabe 1017d66bf5 udev-rules: use structured initializer 2018-11-07 13:35:03 +09:00
Yu Watanabe 92b80c6baa udev-rules: drop unused member file_list in struct udev_rules 2018-11-07 13:35:03 +09:00
Yu Watanabe 1ce7fecb4f udev-rules: make import_parent_into_properties() take sd_device 2018-11-07 13:35:03 +09:00
Yu Watanabe 13c7b75f2f udev-rules: make import_file_into_properties() take sd_device 2018-11-07 13:35:03 +09:00
Yu Watanabe 29b5eb5adf udev-rules: make import_property_from_string() take sd_device
Also, this makes the function return negative errno, though its
return value is always ignored.
2018-11-07 13:35:03 +09:00
Yu Watanabe f3d241feb2 udev: use sd_device for udev_event.dev_parent 2018-11-07 13:35:03 +09:00
Yu Watanabe 5ba7e79885 udev-rules: make match_attr() take sd_device instead of udev_device 2018-11-07 13:35:03 +09:00
Yu Watanabe 480ecb7d28 udev: use sd_device for udev_event.dev_db
Also, this adds sd_device_unref for the object in udev_event_free()
for safety and readability of code.
2018-11-07 13:35:03 +09:00
Yu Watanabe d354690e7d udev-builtin: propagate negative errno 2018-10-23 18:29:25 +09:00
Yu Watanabe c45b369dd1 udev-builtin: modernize code a bit
- introduce _UDEV_BUILTIN_INVALID,
- use _UDEV_BUILTIN_MAX where applicable,
- add several assertions.
2018-10-23 18:28:38 +09:00
Yu Watanabe 3d6194e83e udev: make udev_builtin_run take sd_device 2018-10-23 18:28:38 +09:00
Yu Watanabe 14cb109d45 tree-wide: replace 'unsigned int' with 'unsigned' 2018-10-19 22:19:12 +02:00
Yu Watanabe 218b4609d0
Merge pull request #10447 from poettering/fgets-excorcism
let's get rid of fgets()
2018-10-19 08:36:01 +09:00
Lennart Poettering b33fa02b2d util: drop const_int_one/const_int_zero again
There's only a single user remaining now that we have setsockopt_int(),
let's define those variables locally.

This more or less reverts 6d5e65f645.
2018-10-18 19:50:29 +02:00
Lennart Poettering fae0f8a047 udev-rules: fgets() excorcism 2018-10-18 13:41:09 +02:00
Yu Watanabe 9b5150b63e udev: use Hashmap for storing global properties 2018-10-17 06:50:12 +09:00
Yu Watanabe 29448498c7 udev: use Hashmap for storing PROGRAM or BUILTIN 2018-10-17 06:49:56 +09:00
Yu Watanabe d838e14515 udev: use Hashmap for storing SECLABEL 2018-10-17 06:48:53 +09:00
Lennart Poettering 6d5e65f645 tree-wide: add a single version of "static const int one = 1"
All over the place we define local variables for the various sockopts
that take a bool-like "int" value. Sometimes they are const, sometimes
static, sometimes both, sometimes neither.

Let's clean this up, introduce a common const variable "const_int_one"
(as well as one matching "const_int_zero") and use it everywhere, all
acorss the codebase.
2018-10-15 19:40:51 +02:00
Yu Watanabe 07a26e42da udev-builtin: move definitions related to builtin commands to udev-builtin.h 2018-10-11 04:21:14 +09:00
Yu Watanabe 2024ed616e udev: drop unused udev struct 2018-09-10 18:27:36 +09:00
Yu Watanabe 755c3fe9a7 libudev-util: drop unnecessary argument 'struct udev *udev' from util_resolve_subsys_kernel() 2018-09-10 18:27:36 +09:00
Lennart Poettering fafff8f1ff user-util: rework get_user_creds()
Let's fold get_user_creds_clean() into get_user_creds(), and introduce a
flags argument for it to select "clean" behaviour. This flags parameter
also learns to other new flags:

- USER_CREDS_SYNTHESIZE_FALLBACK: in this mode the user records for
  root/nobody are only synthesized as fallback. Normally, the synthesized
  records take precedence over what is in the user database.  With this
  flag set this is reversed, and the user database takes precedence, and
  the synthesized records are only used if they are missing there. This
  flag should be set in cases where doing NSS is deemed safe, and where
  there's interest in knowing the correct shell, for example if the
  admin changed root's shell to zsh or suchlike.

- USER_CREDS_ALLOW_MISSING: if set, and a UID/GID is specified by
  numeric value, and there's no user/group record for it accept it
  anyway. This allows us to fix #9767

This then also ports all users to set the most appropriate flags.

Fixes: #9767

[zj: remove one isempty() call]
2018-08-20 15:58:21 +02:00
Yu Watanabe f330408d62 tree-wide: drop empty lines in comments 2018-07-23 08:44:24 +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
Zbigniew Jędrzejewski-Szmek 50d1760d26 Drop more license boilerplate
$ git grep -e 'This program is free software' -l |grep -v LICENSE | \
  xargs perl -i -0pe 's/ \* This program.*?for more details.\s*\*\n( \* You should have.*licenses.>.\n)?//gms'

For some reason they were missed previously. All those files seem to
have proper SDPX tags.
2018-06-14 13:05:41 +02:00
Lennart Poettering 810adae9e9 tree-wide: use proper unicode © instead of (C) where we can
Let's use a proper unicode copyright symbol where we can, it's prettier.

This important patch is very important.
2018-06-14 10:20:20 +02:00
Lennart Poettering 8419d45776 coccinelle: similar to reallocarray() let's also systematically use malloc_multiply() 2018-03-02 12:39:07 +01:00
Lennart Poettering 62d74c78b5 coccinelle: add reallocarray() coccinelle script
Let's systematically make use of reallocarray() whereever we invoke
realloc() with a product of two values.
2018-03-02 12:39:07 +01: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
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 e7145211c7 Add SPDX license identifiers to source files under the GPL 2017-11-19 19:08:15 +01:00
Lennart Poettering 7546145e26 string-util: add delete_trailing_chars() and skip_leading_chars() helpers
And let's port over a couple of users to the new APIs.
2017-11-13 10:47:15 +01:00
Yu Watanabe 4c70109600 tree-wide: use IN_SET macro (#6977) 2017-10-04 16:01:32 +02:00
Franck Bui 7e760b79ad udev-rules: all values can contain escaped double quotes now (#6890)
This is primarly useful to support escaped double quotes in PROGRAM or
IMPORT{program} directives.

The only possibilty before this patch was to use an external shell script but
this seems too cumbersome for trivial logics such as

 PROGRAM=="/bin/sh -c 'FOO=\"%s{model}\"; echo ${FOO:0:4}'"

or any similar shell constructs that needs to deals with patterns including
whitespaces.

As it's the case for single quote and for directives running a program, words
within escaped double quotes will be considered as a single argument.

Fixes: #6835
2017-09-28 08:53:46 +02:00
Lennart Poettering 88b013b26d udev: proc_cmdline_get_key() FTW! (#6925)
Let's use proc_cmdline_get_key() instead of some strstr() logic to find
a kernel command line key. Using strstr() gets confused by similarly
named keys, and we should reuse our own code as much as we can anyway...

Fixes: #6330
2017-09-27 09:25:25 +02:00
Lennart Poettering b50846055e exec-util,conf-files: skip non-executable files in execute_directories()
Fixes: #6787
2017-09-13 11:42:31 +02:00
Zbigniew Jędrzejewski-Szmek 2da03cbf9d udev-rules: add helper function for logging
Avoid repeating the same conditional four times. Error messages are
capitalized.
2017-05-19 15:03:14 -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
Dan Streetman 0a10235ed4 udev-rules: perform whitespace replacement for symlink subst values
If the string_escape option is either unset or 'replace' (i.e. if it is
not 'none'), then enable whitespace replacement in SYMLINK variable
substitution values, as added in the last patch.

This will keep any whitespace that is directly contained in a SYMLINK
value, but will replace any whitespace that is added to the SYMLINK
value as a result of variable substitution (except $result/%c).

This fixes bug 4833.
2017-01-03 17:24:52 -05:00
Dan Streetman e20a917105 udev-event: add replace_whitespace param to udev_event_apply_format
If replace_whitespace is true, each substitution value has all its
whitespace removed/replaced by util_replace_whitespace (except the
SUBST_RESULT substitution - $result{} or %c{} - which handles spaces
itself as field separators).  All existing callers are updated to
pass false, so no functional change is made by this patch.

This is needed so the SYMLINK assignment can replace any spaces
introduced through variable substitution, becuase the SYMLINK value is
a space-separated list of symlinks to create.  Any variables that
contain spaces will thus unexpectedly change the symlink value from
a single symlink to multiple incorrectly-named symlinks.

This is used in the next patch, which enables the whitespace
replacement for SYMLINK variable substitution.
2017-01-03 17:12:00 -05:00
Reverend Homer 8fb3f00997 tree-wide: replace all readdir cycles with FOREACH_DIRENT{,_ALL} (#4853) 2016-12-09 10:04:30 +01:00
Tobias Stoeckmann cd05bb8baf Spot inconsistent quoting (just one single quote) (#4732)
It is possible to specify only one quote in udev rules, which is not
detected as an invalid quoting (" instead of "" for empty string).

Technically this doesn't lead to a bug, because the string ends in two
terminating nul characters at this position, but a user should still be
reminded that his configuration is invalid.
2016-12-01 18:46:36 -05:00
Michal Sekletar 4f985bd802 udev: allow substitutions for SECLABEL key (#4505) 2016-10-28 12:09:14 +02:00
Zbigniew Jędrzejewski-Szmek 6b430fdb7c tree-wide: use mfree more 2016-10-16 23:35:39 -04:00
Lubomir Rintel fa394301e2 udev: fix build with DEBUG=1
Alternatively, this could perhaps be removed since it was broken for a
long time and noone seemed to care.

But it was helpful for me today.
2016-05-03 22:35:27 +02:00
Zbigniew Jędrzejewski-Szmek 916a8d4341 udev-rules: fix querying of attributes
faccessat returns 0 on success.

A cosmetic fix is also included: the slash was doubled unnecessarily.
2016-02-22 13:21:32 -05:00
Zbigniew Jędrzejewski-Szmek fdd21be6f5 udev-rules: use _cleanup_ for fclose 2016-02-21 11:05:13 -05:00
Zbigniew Jędrzejewski-Szmek 19a8e656a9 udev-rules: make error messages about rules more uniform
Also downgrade non-fatal warnings to log_warning.

Previously rule_add_key() would check the output array and log a cryptic
error and return -1. Most of the time the return value was ignored. This
does not seems right, because the buffer can overflow with enough rules.
It would also check if we have enough space for the *next* rule, even if
there might be not next rule, i.e. off-by-one.

Replace this with a check that we have enough space for a next rule before
we start parsing.

Normally using macros to alter flow is not allowed, but in this case I
think it is worth it, because it allows lots of boilerplate code to be
removed and hides repeated boring parameters, making function logic much
easier to follow.
2016-02-21 11:05:11 -05:00