diff options
-rw-r--r-- | man/udevadm.xml | 9 | ||||
-rw-r--r-- | shell-completion/bash/udevadm | 13 | ||||
-rw-r--r-- | shell-completion/zsh/_udevadm | 1 | ||||
-rw-r--r-- | src/import/pull-common.c | 15 | ||||
-rw-r--r-- | src/nspawn/nspawn-cgroup.c | 10 | ||||
-rw-r--r-- | src/nspawn/nspawn-mount.c | 72 | ||||
-rw-r--r-- | src/nspawn/nspawn.c | 2 | ||||
-rw-r--r-- | src/udev/udev-event.h | 1 | ||||
-rw-r--r-- | src/udev/udev-rules.c | 1202 | ||||
-rw-r--r-- | src/udev/udevadm-test.c | 25 | ||||
-rwxr-xr-x | test/units/TEST-17-UDEV.10.sh | 1 |
11 files changed, 761 insertions, 590 deletions
diff --git a/man/udevadm.xml b/man/udevadm.xml index 13cf106d87..07169f2e50 100644 --- a/man/udevadm.xml +++ b/man/udevadm.xml @@ -880,6 +880,15 @@ <xi:include href="version-info.xml" xpointer="v209"/> </listitem> </varlistentry> + <varlistentry> + <term><option>-v</option></term> + <term><option>--verbose</option></term> + <listitem> + <para>Shows verbose logs in processing udev rules.</para> + + <xi:include href="version-info.xml" xpointer="v258"/> + </listitem> + </varlistentry> <xi:include href="standard-options.xml" xpointer="help" /> </variablelist> diff --git a/shell-completion/bash/udevadm b/shell-completion/bash/udevadm index e5626c9301..c4aea7039c 100644 --- a/shell-completion/bash/udevadm +++ b/shell-completion/bash/udevadm @@ -69,7 +69,8 @@ _udevadm() { [CONTROL_ARG]='-l --log-priority -p --property -m --children-max -t --timeout' [MONITOR_STANDALONE]='-k --kernel -u --udev -p --property' [MONITOR_ARG]='-s --subsystem-match -t --tag-match' - [TEST]='-a --action -N --resolve-names' + [TEST_STANDALONE]='-v --verbose' + [TEST_ARG]='-a --action -N --resolve-names' [TEST_BUILTIN]='-a --action' [VERIFY]='-N --resolve-names --root --no-summary --no-style' [WAIT]='-t --timeout --initialized=no --removed --settle' @@ -216,7 +217,7 @@ _udevadm() { ;; 'test') - if __contains_word "$prev" ${OPTS[TEST]}; then + if __contains_word "$prev" ${OPTS[TEST_ARG]}; then case $prev in -a|--action) comps=$( udevadm test --action help ) @@ -225,12 +226,8 @@ _udevadm() { comps='early late never' ;; esac - COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) - return 0 - fi - - if [[ $cur = -* ]]; then - comps="${OPTS[COMMON]} ${OPTS[TEST]}" + elif [[ $cur = -* ]]; then + comps="${OPTS[COMMON]} ${OPTS[TEST_ARG]} ${OPTS[TEST_STANDALONE]}" else comps=$( __get_all_devices ) local IFS=$'\n' diff --git a/shell-completion/zsh/_udevadm b/shell-completion/zsh/_udevadm index 5f5761cbfa..c0141e5cd3 100644 --- a/shell-completion/zsh/_udevadm +++ b/shell-completion/zsh/_udevadm @@ -88,6 +88,7 @@ _udevadm_test(){ '(-)'{-V,--version}'[Show package version]' \ '--action=[The action string.]:actions:(add change remove move online offline bind unbind)' \ '--subsystem=[The subsystem string.]' \ + '(-v --verbose)'{-v,--verbose}'[Show verbose logs.]' \ '*::devpath:_files -P /sys/ -W /sys' } diff --git a/src/import/pull-common.c b/src/import/pull-common.c index 9a2ced002b..b566e52b56 100644 --- a/src/import/pull-common.c +++ b/src/import/pull-common.c @@ -22,6 +22,7 @@ #include "siphash24.h" #include "string-util.h" #include "strv.h" +#include "tmpfile-util.h" #include "web-util.h" #define FILENAME_ESCAPE "/.#\"\'" @@ -378,9 +379,9 @@ static int verify_gpg( const void *signature, size_t signature_size) { _cleanup_close_pair_ int gpg_pipe[2] = EBADF_PAIR; - char sig_file_path[] = "/tmp/sigXXXXXX", gpg_home[] = "/tmp/gpghomeXXXXXX"; + _cleanup_(rm_rf_physical_and_freep) char *gpg_home = NULL; + char sig_file_path[] = "/tmp/sigXXXXXX"; _cleanup_(sigkill_waitp) pid_t pid = 0; - bool gpg_home_created = false; int r; assert(payload || payload_size == 0); @@ -404,13 +405,12 @@ static int verify_gpg( } } - if (!mkdtemp(gpg_home)) { - r = log_error_errno(errno, "Failed to create temporary home for gpg: %m"); + r = mkdtemp_malloc("/tmp/gpghomeXXXXXX", &gpg_home); + if (r < 0) { + log_error_errno(r, "Failed to create temporary home for gpg: %m"); goto finish; } - gpg_home_created = true; - r = safe_fork_full("(gpg)", (int[]) { gpg_pipe[0], -EBADF, STDERR_FILENO }, NULL, 0, @@ -485,9 +485,6 @@ finish: if (signature_size > 0) (void) unlink(sig_file_path); - if (gpg_home_created) - (void) rm_rf(gpg_home, REMOVE_ROOT|REMOVE_PHYSICAL); - return r; } diff --git a/src/nspawn/nspawn-cgroup.c b/src/nspawn/nspawn-cgroup.c index 4f28b4a225..6d6a8a814b 100644 --- a/src/nspawn/nspawn-cgroup.c +++ b/src/nspawn/nspawn-cgroup.c @@ -18,6 +18,7 @@ #include "rm-rf.h" #include "string-util.h" #include "strv.h" +#include "tmpfile-util.h" #include "user-util.h" static int chown_cgroup_path(const char *path, uid_t uid_shift) { @@ -48,8 +49,9 @@ static int chown_cgroup_path(const char *path, uid_t uid_shift) { } int sync_cgroup(pid_t pid, CGroupUnified unified_requested, uid_t uid_shift) { + _cleanup_(rmdir_and_freep) char *tree = NULL; _cleanup_free_ char *cgroup = NULL; - char tree[] = "/tmp/unifiedXXXXXX", pid_string[DECIMAL_STR_MAX(pid) + 1]; + char pid_string[DECIMAL_STR_MAX(pid) + 1]; bool undo_mount = false; const char *fn; int r, unified_controller; @@ -70,8 +72,9 @@ int sync_cgroup(pid_t pid, CGroupUnified unified_requested, uid_t uid_shift) { return log_error_errno(r, "Failed to get control group of " PID_FMT ": %m", pid); /* In order to access the unified hierarchy we need to mount it */ - if (!mkdtemp(tree)) - return log_error_errno(errno, "Failed to generate temporary mount point for unified hierarchy: %m"); + r = mkdtemp_malloc("/tmp/unifiedXXXXXX", &tree); + if (r < 0) + return log_error_errno(r, "Failed to generate temporary mount point for unified hierarchy: %m"); if (unified_controller > 0) r = mount_nofollow_verbose(LOG_ERR, "cgroup", tree, "cgroup", @@ -107,7 +110,6 @@ finish: if (undo_mount) (void) umount_verbose(LOG_ERR, tree, UMOUNT_NOFOLLOW); - (void) rmdir(tree); return r; } diff --git a/src/nspawn/nspawn-mount.c b/src/nspawn/nspawn-mount.c index d5370c22b9..f64d6b48a6 100644 --- a/src/nspawn/nspawn-mount.c +++ b/src/nspawn/nspawn-mount.c @@ -127,18 +127,15 @@ static char *resolve_source_path(const char *dest, const char *source) { } static int allocate_temporary_source(CustomMount *m) { + int r; + assert(m); assert(!m->source); assert(!m->rm_rf_tmpdir); - m->rm_rf_tmpdir = strdup("/var/tmp/nspawn-temp-XXXXXX"); - if (!m->rm_rf_tmpdir) - return log_oom(); - - if (!mkdtemp(m->rm_rf_tmpdir)) { - m->rm_rf_tmpdir = mfree(m->rm_rf_tmpdir); - return log_error_errno(errno, "Failed to acquire temporary directory: %m"); - } + r = mkdtemp_malloc("/var/tmp/nspawn-temp-XXXXXX", &m->rm_rf_tmpdir); + if (r < 0) + return log_error_errno(r, "Failed to acquire temporary directory: %m"); m->source = path_join(m->rm_rf_tmpdir, "src"); if (!m->source) @@ -153,7 +150,7 @@ static int allocate_temporary_source(CustomMount *m) { int custom_mount_prepare_all(const char *dest, CustomMount *l, size_t n) { int r; - /* Prepare all custom mounts. This will make source we know all temporary directories. This is called in the + /* Prepare all custom mounts. This will make sure we know all temporary directories. This is called in the * parent process, so that we know the temporary directories to remove on exit before we fork off the * children. */ @@ -162,9 +159,7 @@ int custom_mount_prepare_all(const char *dest, CustomMount *l, size_t n) { /* Order the custom mounts, and make sure we have a working directory */ typesafe_qsort(l, n, custom_mount_compare); - for (size_t i = 0; i < n; i++) { - CustomMount *m = l + i; - + FOREACH_ARRAY(m, l, n) { /* /proc we mount in the inner child, i.e. when we acquired CLONE_NEWPID. All other mounts we mount * already in the outer child, so that the mounts are already established before CLONE_NEWPID and in * particular CLONE_NEWUSER. This also means any custom mounts below /proc also need to be mounted in @@ -1101,7 +1096,7 @@ static int setup_volatile_state_after_remount_idmap(const char *directory, uid_t static int setup_volatile_yes(const char *directory, uid_t uid_shift, const char *selinux_apifs_context) { bool tmpfs_mounted = false, bind_mounted = false; - char template[] = "/tmp/nspawn-volatile-XXXXXX"; + _cleanup_(rmdir_and_freep) char *template = NULL; _cleanup_free_ char *buf = NULL, *bindir = NULL; const char *f, *t, *options; struct stat st; @@ -1130,8 +1125,9 @@ static int setup_volatile_yes(const char *directory, uid_t uid_shift, const char return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Error starting image: if --volatile=yes is used /bin must be a symlink (for merged /usr support) or non-existent (in which case a symlink is created automatically)."); - if (!mkdtemp(template)) - return log_error_errno(errno, "Failed to create temporary directory: %m"); + r = mkdtemp_malloc("/tmp/nspawn-volatile-XXXXXX", &template); + if (r < 0) + return log_error_errno(r, "Failed to create temporary directory: %m"); options = "mode=0755" TMPFS_LIMITS_ROOTFS; r = tmpfs_patch_options(options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf); @@ -1182,13 +1178,12 @@ fail: if (tmpfs_mounted) (void) umount_verbose(LOG_ERR, template, UMOUNT_NOFOLLOW); - (void) rmdir(template); return r; } static int setup_volatile_overlay(const char *directory, uid_t uid_shift, const char *selinux_apifs_context) { _cleanup_free_ char *buf = NULL, *escaped_directory = NULL, *escaped_upper = NULL, *escaped_work = NULL; - char template[] = "/tmp/nspawn-volatile-XXXXXX"; + _cleanup_(rmdir_and_freep) char *template = NULL; const char *upper, *work, *options; bool tmpfs_mounted = false; int r; @@ -1197,8 +1192,9 @@ static int setup_volatile_overlay(const char *directory, uid_t uid_shift, const /* --volatile=overlay means we mount an overlayfs to the root dir. */ - if (!mkdtemp(template)) - return log_error_errno(errno, "Failed to create temporary directory: %m"); + r = mkdtemp_malloc("/tmp/nspawn-volatile-XXXXXX", &template); + if (r < 0) + return log_error_errno(r, "Failed to create temporary directory: %m"); options = "mode=0755" TMPFS_LIMITS_ROOTFS; r = tmpfs_patch_options(options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf); @@ -1243,7 +1239,6 @@ finish: if (tmpfs_mounted) (void) umount_verbose(LOG_ERR, template, UMOUNT_NOFOLLOW); - (void) rmdir(template); return r; } @@ -1322,8 +1317,7 @@ int pivot_root_parse(char **pivot_root_new, char **pivot_root_old, const char *s int setup_pivot_root(const char *directory, const char *pivot_root_new, const char *pivot_root_old) { _cleanup_free_ char *directory_pivot_root_new = NULL; _cleanup_free_ char *pivot_tmp_pivot_root_old = NULL; - char pivot_tmp[] = "/tmp/nspawn-pivot-XXXXXX"; - bool remove_pivot_tmp = false; + _cleanup_(rmdir_and_freep) char *pivot_tmp = NULL; int r; assert(directory); @@ -1364,43 +1358,33 @@ int setup_pivot_root(const char *directory, const char *pivot_root_new, const ch /* Remount directory_pivot_root_new to make it movable. */ r = mount_nofollow_verbose(LOG_ERR, directory_pivot_root_new, directory_pivot_root_new, NULL, MS_BIND, NULL); if (r < 0) - goto done; + return r; if (pivot_root_old) { - if (!mkdtemp(pivot_tmp)) { - r = log_error_errno(errno, "Failed to create temporary directory: %m"); - goto done; - } + r = mkdtemp_malloc("/tmp/nspawn-pivot-XXXXXX", &pivot_tmp); + if (r < 0) + return log_error_errno(r, "Failed to create temporary directory: %m"); - remove_pivot_tmp = true; pivot_tmp_pivot_root_old = path_join(pivot_tmp, pivot_root_old); - if (!pivot_tmp_pivot_root_old) { - r = log_oom(); - goto done; - } + if (!pivot_tmp_pivot_root_old) + return log_oom(); r = mount_nofollow_verbose(LOG_ERR, directory_pivot_root_new, pivot_tmp, NULL, MS_MOVE, NULL); if (r < 0) - goto done; + return r; r = mount_nofollow_verbose(LOG_ERR, directory, pivot_tmp_pivot_root_old, NULL, MS_MOVE, NULL); if (r < 0) - goto done; + return r; r = mount_nofollow_verbose(LOG_ERR, pivot_tmp, directory, NULL, MS_MOVE, NULL); - if (r < 0) - goto done; - } else { + } else r = mount_nofollow_verbose(LOG_ERR, directory_pivot_root_new, directory, NULL, MS_MOVE, NULL); - if (r < 0) - goto done; - } -done: - if (remove_pivot_tmp) - (void) rmdir(pivot_tmp); + if (r < 0) + return r; - return r; + return 0; } #define NSPAWN_PRIVATE_FULLY_VISIBLE_PROCFS "/run/host/proc" diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index 7ceda0b30c..5ab04b121a 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -3437,7 +3437,7 @@ static int inner_child( return r; } - r = mount_all(NULL, + r = mount_all(/* dest= */ NULL, arg_mount_settings | MOUNT_IN_USERNS, arg_uid_shift, arg_selinux_apifs_context); diff --git a/src/udev/udev-event.h b/src/udev/udev-event.h index b1ad2bf0c2..11e2c700e6 100644 --- a/src/udev/udev-event.h +++ b/src/udev/udev-event.h @@ -48,6 +48,7 @@ typedef struct UdevEvent { bool name_final; bool devlink_final; bool run_final; + bool trace; bool log_level_was_debug; int default_log_level; EventMode event_mode; diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c index 3687748993..041c642aa9 100644 --- a/src/udev/udev-rules.c +++ b/src/udev/udev-rules.c @@ -166,12 +166,15 @@ struct UdevRuleToken { const char *value; void *data; + const char *token_str; /* original token string for logging */ + UdevRuleLine *rule_line; LIST_FIELDS(UdevRuleToken, tokens); }; struct UdevRuleLine { char *line; + char *line_for_logging; unsigned line_number; UdevRuleLineType type; @@ -204,83 +207,172 @@ struct UdevRules { #define LINE_GET_RULES(line) \ ASSERT_PTR(ASSERT_PTR(ASSERT_PTR(line)->rule_file)->rules) +static bool token_is_for_parents(UdevRuleToken *token) { + return token->type >= TK_M_PARENTS_KERNEL && token->type <= TK_M_PARENTS_TAG; +} + /*** Logging helpers ***/ -#define log_udev_rule_internal(device, file, line_nr, level, error, fmt, ...) \ +#define _log_udev_rule_file_full(device, device_u, file, file_u, line_nr, level, level_u, error, fmt, ...) \ ({ \ - int _lv = (level); \ - sd_device *_dev = (device); \ - UdevRuleFile *_f = (file); \ - const char *_n = _f ? _f->filename : NULL; \ + int level_u = (level); \ + sd_device *device_u = (device); \ + UdevRuleFile *file_u = (file); \ \ - if (!_dev && _f) \ - _f->issues |= (1U << _lv); \ + if (!device_u && file_u) \ + file_u->issues |= (1U << level_u); \ \ log_device_full_errno_zerook( \ - _dev, _lv, error, "%s:%u " fmt, \ - strna(_n), line_nr, \ - ##__VA_ARGS__); \ + device_u, level_u, error, "%s:%u " fmt, \ + strna(file_u ? file_u->filename : NULL), \ + line_nr, ##__VA_ARGS__); \ }) -/* Mainly used when applying tokens to the event device. */ -#define log_event_full_errno_zerook(device, token, ...) \ +#define log_udev_rule_file_full(device, file, line_nr, level, error, fmt, ...) \ + _log_udev_rule_file_full(device, UNIQ_T(d, UNIQ), file, UNIQ_T(f, UNIQ), line_nr, level, UNIQ_T(l, UNIQ), error, fmt, ##__VA_ARGS__) + +#define _log_udev_rule_line_full(device, line, line_u, ...) \ ({ \ - UdevRuleToken *_t = (token); \ - UdevRuleLine *_l = _t ? _t->rule_line : NULL; \ + UdevRuleLine *line_u = ASSERT_PTR(line); \ \ - log_udev_rule_internal( \ + log_udev_rule_file_full( \ device, \ - _l ? _l->rule_file : NULL, \ - _l ? _l->line_number : 0, \ + line_u->rule_file, line_u->line_number, \ __VA_ARGS__); \ }) -#define log_event_full_errno(device, token, level, error, ...) \ +#define log_udev_rule_line_full(device, line, ...) \ + _log_udev_rule_line_full(device, line, UNIQ_T(l, UNIQ), __VA_ARGS__) + +/* Mainly used when applying tokens to the event device. */ +#define _log_event_full_errno_zerook(event, event_u, token, token_u, level, error, fmt, ...) \ + ({ \ + UdevEvent *event_u = ASSERT_PTR(event); \ + UdevRuleToken *token_u = ASSERT_PTR(token); \ + \ + log_udev_rule_line_full( \ + token_is_for_parents(token_u) ? event_u->dev_parent : event_u->dev, \ + token_u->rule_line, \ + level, error, "%s: " fmt, \ + token_u->token_str, ##__VA_ARGS__); \ + }) + +#define log_event_full_errno_zerook(event, token, ...) \ + _log_event_full_errno_zerook(event, UNIQ_T(e, UNIQ), token, UNIQ_T(t, UNIQ), __VA_ARGS__) + +#define _log_event_full_errno(event, token, level, error, error_u, ...) \ ({ \ - int _error = (error); \ - ASSERT_NON_ZERO(_error); \ + int error_u = (error); \ + ASSERT_NON_ZERO(error_u); \ log_event_full_errno_zerook( \ - device, token, level, _error, ##__VA_ARGS__); \ + event, token, level, error_u, \ + __VA_ARGS__); \ }) -#define log_event_full(device, token, level, ...) (void) log_event_full_errno_zerook(device, token, level, 0, __VA_ARGS__) +#define log_event_full_errno(event, token, level, error, ...) \ + _log_event_full_errno(event, token, level, error, UNIQ_T(e, UNIQ), __VA_ARGS__) -#define log_event_debug(device, token, ...) log_event_full(device, token, LOG_DEBUG, __VA_ARGS__) -#define log_event_info(device, token, ...) log_event_full(device, token, LOG_INFO, __VA_ARGS__) -#define log_event_notice(device, token, ...) log_event_full(device, token, LOG_NOTICE, __VA_ARGS__) -#define log_event_warning(device, token, ...) log_event_full(device, token, LOG_WARNING, __VA_ARGS__) -#define log_event_error(device, token, ...) log_event_full(device, token, LOG_ERR, __VA_ARGS__) +#define log_event_full(event, token, level, ...) (void) log_event_full_errno_zerook(event, token, level, 0, __VA_ARGS__) -#define log_event_debug_errno(device, token, error, ...) log_event_full_errno(device, token, LOG_DEBUG, error, __VA_ARGS__) -#define log_event_info_errno(device, token, error, ...) log_event_full_errno(device, token, LOG_INFO, error, __VA_ARGS__) -#define log_event_notice_errno(device, token, error, ...) log_event_full_errno(device, token, LOG_NOTICE, error, __VA_ARGS__) -#define log_event_warning_errno(device, token, error, ...) log_event_full_errno(device, token, LOG_WARNING, error, __VA_ARGS__) -#define log_event_error_errno(device, token, error, ...) log_event_full_errno(device, token, LOG_ERR, error, __VA_ARGS__) +#define log_event_debug(event, token, ...) log_event_full(event, token, LOG_DEBUG, __VA_ARGS__) +#define log_event_info(event, token, ...) log_event_full(event, token, LOG_INFO, __VA_ARGS__) +#define log_event_notice(event, token, ...) log_event_full(event, token, LOG_NOTICE, __VA_ARGS__) +#define log_event_warning(event, token, ...) log_event_full(event, token, LOG_WARNING, __VA_ARGS__) +#define log_event_error(event, token, ...) log_event_full(event, token, LOG_ERR, __VA_ARGS__) -/* Mainly used when parsing .rules files. */ -#define log_file_full_errno_zerook(...) \ - log_udev_rule_internal(NULL, __VA_ARGS__) +#define log_event_debug_errno(event, token, error, ...) log_event_full_errno(event, token, LOG_DEBUG, error, __VA_ARGS__) +#define log_event_info_errno(event, token, error, ...) log_event_full_errno(event, token, LOG_INFO, error, __VA_ARGS__) +#define log_event_notice_errno(event, token, error, ...) log_event_full_errno(event, token, LOG_NOTICE, error, __VA_ARGS__) +#define log_event_warning_errno(event, token, error, ...) log_event_full_errno(event, token, LOG_WARNING, error, __VA_ARGS__) +#define log_event_error_errno(event, token, error, ...) log_event_full_errno(event, token, LOG_ERR, error, __VA_ARGS__) -#define log_file_error(file, line_nr, ...) \ - log_file_full_errno_zerook(file, line_nr, LOG_ERR, 0, __VA_ARGS__) +#define _log_event_trace(event, event_u, ...) \ + ({ \ + UdevEvent *event_u = ASSERT_PTR(event); \ + \ + event_u->trace ? \ + log_event_debug(event_u, __VA_ARGS__) : \ + (void) 0; \ + }) -#define log_line_full_errno_zerook(line, ...) \ +#define log_event_trace(event, ...) \ + _log_event_trace(event, UNIQ_T(e, UNIQ), __VA_ARGS__) + +#define _log_event_result(event, token, result, result_u) \ ({ \ - UdevRuleLine *_l = (line); \ - log_file_full_errno_zerook( \ - _l ? _l->rule_file : NULL, \ - _l ? _l->line_number : 0, \ - __VA_ARGS__); \ + bool result_u = (result); \ + \ + log_event_trace(event, token, "%s", \ + result_u ? "PASS" : "FAIL"); \ + result_u; \ }) -#define log_line_full_errno(line, level, error, ...) \ +#define log_event_result(event, token, result) \ + _log_event_result(event, token, result, UNIQ_T(r, UNIQ)) + +#define log_event_done(event, token) \ + ({ \ + log_event_trace(event, token, "DONE"); \ + true; \ + }) + +#define _log_event_final_set(event, token, token_u) \ + ({ \ + UdevRuleToken *token_u = ASSERT_PTR(token); \ + \ + log_event_trace(event, token_u, \ + "Already assigned final value, ignoring further %s.", \ + token_u->op == OP_REMOVE ? "modification" : "assignment"); \ + true; \ + }) + +#define log_event_final_set(event, token) \ + _log_event_final_set(event, token, UNIQ_T(t, UNIQ)) + +#define _log_event_truncated(event, token, token_u, what, format) \ ({ \ - int _error = (error); \ - ASSERT_NON_ZERO(_error); \ - log_line_full_errno_zerook( \ - line, level, _error, ##__VA_ARGS__); \ + UdevRuleToken *token_u = ASSERT_PTR(token); \ + \ + token_u->type < _TK_M_MAX ? \ + log_event_debug(event, token_u, \ + "The %s is truncated while substituting into \"%s\", assuming the token fails.", \ + what, (const char*) format) : \ + log_event_warning( \ + event, token_u, \ + "The %s is truncated while substituting into \"%s\", refusing to apply the token.", \ + what, (const char*) format); \ }) +#define log_event_truncated(event, token, what, format) \ + _log_event_truncated(event, token, UNIQ_T(t, UNIQ), what, format) + +#define _log_event_line(event, event_u, line, ...) \ + ({ \ + UdevEvent *event_u = ASSERT_PTR(event); \ + \ + event_u->trace ? \ + log_udev_rule_line_full( \ + event_u->dev, line, \ + LOG_DEBUG, 0, __VA_ARGS__) : \ + 0; \ + }) + +#define log_event_line(event, line, ...) \ + _log_event_line(event, UNIQ_T(e, UNIQ), line, __VA_ARGS__) + +/* Mainly used when parsing .rules files. */ +#define log_file_full_errno_zerook(...) \ + log_udev_rule_file_full(NULL, __VA_ARGS__) + +#define log_file_error(file, line_nr, ...) \ + log_file_full_errno_zerook(file, line_nr, LOG_ERR, 0, __VA_ARGS__) + +#define log_line_full_errno_zerook(...) \ + log_udev_rule_line_full(NULL, __VA_ARGS__) + +#define log_line_full_errno(line, level, error, ...) \ + log_udev_rule_line_full(NULL, line, level, error, __VA_ARGS__) + #define log_line_full(line, level, ...) (void) log_line_full_errno_zerook(line, level, 0, __VA_ARGS__) #define log_line_debug(line, ...) log_line_full(line, LOG_DEBUG, __VA_ARGS__) @@ -312,38 +404,6 @@ struct UdevRules { "Invalid value \"%s\" for %s (char %zu: %s), ignoring.", \ value, key, offset, hint) -static void log_unknown_owner(sd_device *dev, UdevRuleLine *line, int error, const char *entity, const char *name) { - assert(line); - ASSERT_NON_ZERO(error); - - if (IN_SET(abs(error), ENOENT, ESRCH)) - log_udev_rule_internal(dev, line->rule_file, line->line_number, LOG_ERR, error, - "Unknown %s '%s', ignoring.", entity, name); - else - log_udev_rule_internal(dev, line->rule_file, line->line_number, LOG_ERR, error, - "Failed to resolve %s '%s', ignoring: %m", entity, name); -} - -static void log_event_truncated( - sd_device *dev, - UdevRuleToken *token, - const char *what, - const char *format, - const char *key, - bool is_match) { - - if (is_match) - log_event_debug(dev, token, - "The %s is truncated while substituting into '%s', " - "assuming the %s key does not match.", - what, format, key); - else - log_event_warning(dev, token, - "The %s is truncated while substituting into '%s', " - "refusing to apply the %s key.", - what, format, key); -} - /*** Other functions ***/ static UdevRuleToken* udev_rule_token_free(UdevRuleToken *token) { @@ -375,6 +435,7 @@ static UdevRuleLine* udev_rule_line_free(UdevRuleLine *rule_line) { LIST_REMOVE(rule_lines, rule_line->rule_file->rule_lines, rule_line); free(rule_line->line); + free(rule_line->line_for_logging); return mfree(rule_line); } @@ -427,7 +488,11 @@ static int rule_resolve_user(UdevRuleLine *rule_line, const char *name, uid_t *r r = get_user_creds(&name, &uid, NULL, NULL, NULL, USER_CREDS_ALLOW_MISSING); if (r < 0) { - log_unknown_owner(NULL, rule_line, r, "user", name); + if (IN_SET(r, -ENOENT, -ESRCH)) + log_line_error_errno(rule_line, r, "Unknown user '%s', ignoring.", name); + else + log_line_error_errno(rule_line, r, "Failed to resolve user '%s', ignoring: %m", name); + *ret = UID_INVALID; return 0; } @@ -463,7 +528,11 @@ static int rule_resolve_group(UdevRuleLine *rule_line, const char *name, gid_t * r = get_group_creds(&name, &gid, USER_CREDS_ALLOW_MISSING); if (r < 0) { - log_unknown_owner(NULL, rule_line, r, "group", name); + if (IN_SET(r, -ENOENT, -ESRCH)) + log_line_error_errno(rule_line, r, "Unknown group '%s', ignoring.", name); + else + log_line_error_errno(rule_line, r, "Failed to resolve group '%s', ignoring: %m", name); + *ret = GID_INVALID; return 0; } @@ -495,7 +564,15 @@ static bool type_has_nulstr_value(UdevRuleTokenType type) { return type < TK_M_TEST || type == TK_M_RESULT; } -static int rule_line_add_token(UdevRuleLine *rule_line, UdevRuleTokenType type, UdevRuleOperatorType op, char *value, void *data, bool is_case_insensitive) { +static int rule_line_add_token( + UdevRuleLine *rule_line, + UdevRuleTokenType type, + UdevRuleOperatorType op, + char *value, + void *data, + bool is_case_insensitive, + const char *token_str) { + _cleanup_(udev_rule_token_freep) UdevRuleToken *token = NULL; UdevRuleMatchType match_type = _MATCH_TYPE_INVALID; UdevRuleSubstituteType subst_type = _SUBST_TYPE_INVALID; @@ -583,6 +660,7 @@ static int rule_line_add_token(UdevRuleLine *rule_line, UdevRuleTokenType type, .data = data, .match_type = match_type, .attr_subst_type = subst_type, + .token_str = token_str, .rule_line = rule_line, }; @@ -630,7 +708,15 @@ static int check_attr_format_and_warn(UdevRuleLine *line, const char *key, const return 0; } -static int parse_token(UdevRuleLine *rule_line, const char *key, char *attr, UdevRuleOperatorType op, char *value, bool is_case_insensitive) { +static int parse_token( + UdevRuleLine *rule_line, + const char *key, + char *attr, + UdevRuleOperatorType op, + char *value, + bool is_case_insensitive, + const char *token_str) { + ResolveNameTiming resolve_name_timing = LINE_GET_RULES(rule_line)->resolve_name_timing; bool is_match = IN_SET(op, OP_MATCH, OP_NOMATCH); int r; @@ -648,29 +734,29 @@ static int parse_token(UdevRuleLine *rule_line, const char *key, char *attr, Ude if (!is_match) return log_line_invalid_op(rule_line, key); - r = rule_line_add_token(rule_line, TK_M_ACTION, op, value, NULL, is_case_insensitive); + r = rule_line_add_token(rule_line, TK_M_ACTION, op, value, NULL, is_case_insensitive, token_str); } else if (streq(key, "DEVPATH")) { if (attr) return log_line_invalid_attr(rule_line, key); if (!is_match) return log_line_invalid_op(rule_line, key); - r = rule_line_add_token(rule_line, TK_M_DEVPATH, op, value, NULL, is_case_insensitive); + r = rule_line_add_token(rule_line, TK_M_DEVPATH, op, value, NULL, is_case_insensitive, token_str); } else if (streq(key, "KERNEL")) { if (attr) return log_line_invalid_attr(rule_line, key); if (!is_match) return log_line_invalid_op(rule_line, key); - r = rule_line_add_token(rule_line, TK_M_KERNEL, op, value, NULL, is_case_insensitive); + r = rule_line_add_token(rule_line, TK_M_KERNEL, op, value, NULL, is_case_insensitive, token_str); } else if (streq(key, "SYMLINK")) { if (attr) return log_line_invalid_attr(rule_line, key); if (!is_match) { check_value_format_and_warn(rule_line, key, value, false); - r = rule_line_add_token(rule_line, TK_A_DEVLINK, op, value, NULL, /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_A_DEVLINK, op, value, NULL, /* is_case_insensitive = */ false, token_str); } else - r = rule_line_add_token(rule_line, TK_M_DEVLINK, op, value, NULL, is_case_insensitive); + r = rule_line_add_token(rule_line, TK_M_DEVLINK, op, value, NULL, is_case_insensitive, token_str); } else if (streq(key, "NAME")) { if (attr) return log_line_invalid_attr(rule_line, key); @@ -690,9 +776,9 @@ static int parse_token(UdevRuleLine *rule_line, const char *key, char *attr, Ude "Ignoring NAME=\"\", as udev will not delete any network interfaces."); check_value_format_and_warn(rule_line, key, value, false); - r = rule_line_add_token(rule_line, TK_A_NAME, op, value, NULL, /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_A_NAME, op, value, NULL, /* is_case_insensitive = */ false, token_str); } else - r = rule_line_add_token(rule_line, TK_M_NAME, op, value, NULL, is_case_insensitive); + r = rule_line_add_token(rule_line, TK_M_NAME, op, value, NULL, is_case_insensitive, token_str); } else if (streq(key, "ENV")) { if (isempty(attr)) return log_line_invalid_attr(rule_line, key); @@ -710,15 +796,15 @@ static int parse_token(UdevRuleLine *rule_line, const char *key, char *attr, Ude check_value_format_and_warn(rule_line, key, value, false); - r = rule_line_add_token(rule_line, TK_A_ENV, op, value, attr, /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_A_ENV, op, value, attr, /* is_case_insensitive = */ false, token_str); } else - r = rule_line_add_token(rule_line, TK_M_ENV, op, value, attr, is_case_insensitive); + r = rule_line_add_token(rule_line, TK_M_ENV, op, value, attr, is_case_insensitive, token_str); } else if (streq(key, "CONST")) { if (isempty(attr) || !STR_IN_SET(attr, "arch", "virt")) return log_line_invalid_attr(rule_line, key); if (!is_match) return log_line_invalid_op(rule_line, key); - r = rule_line_add_token(rule_line, TK_M_CONST, op, value, attr, is_case_insensitive); + r = rule_line_add_token(rule_line, TK_M_CONST, op, value, attr, is_case_insensitive, token_str); } else if (streq(key, "TAG")) { if (attr) return log_line_invalid_attr(rule_line, key); @@ -730,9 +816,9 @@ static int parse_token(UdevRuleLine *rule_line, const char *key, char *attr, Ude if (!is_match) { check_value_format_and_warn(rule_line, key, value, true); - r = rule_line_add_token(rule_line, TK_A_TAG, op, value, NULL, /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_A_TAG, op, value, NULL, /* is_case_insensitive = */ false, token_str); } else - r = rule_line_add_token(rule_line, TK_M_TAG, op, value, NULL, is_case_insensitive); + r = rule_line_add_token(rule_line, TK_M_TAG, op, value, NULL, is_case_insensitive, token_str); } else if (streq(key, "SUBSYSTEM")) { if (attr) return log_line_invalid_attr(rule_line, key); @@ -742,14 +828,14 @@ static int parse_token(UdevRuleLine *rule_line, const char *key, char *attr, Ude if (STR_IN_SET(value, "bus", "class")) log_line_warning(rule_line, "\"%s\" must be specified as \"subsystem\".", value); - r = rule_line_add_token(rule_line, TK_M_SUBSYSTEM, op, value, NULL, is_case_insensitive); + r = rule_line_add_token(rule_line, TK_M_SUBSYSTEM, op, value, NULL, is_case_insensitive, token_str); } else if (streq(key, "DRIVER")) { if (attr) return log_line_invalid_attr(rule_line, key); if (!is_match) return log_line_invalid_op(rule_line, key); - r = rule_line_add_token(rule_line, TK_M_DRIVER, op, value, NULL, is_case_insensitive); + r = rule_line_add_token(rule_line, TK_M_DRIVER, op, value, NULL, is_case_insensitive, token_str); } else if (streq(key, "ATTR")) { r = check_attr_format_and_warn(rule_line, key, attr); if (r < 0) @@ -763,9 +849,9 @@ static int parse_token(UdevRuleLine *rule_line, const char *key, char *attr, Ude if (!is_match) { check_value_format_and_warn(rule_line, key, value, false); - r = rule_line_add_token(rule_line, TK_A_ATTR, op, value, attr, /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_A_ATTR, op, value, attr, /* is_case_insensitive = */ false, token_str); } else - r = rule_line_add_token(rule_line, TK_M_ATTR, op, value, attr, is_case_insensitive); + r = rule_line_add_token(rule_line, TK_M_ATTR, op, value, attr, is_case_insensitive, token_str); } else if (streq(key, "SYSCTL")) { r = check_attr_format_and_warn(rule_line, key, attr); if (r < 0) @@ -779,30 +865,30 @@ static int parse_token(UdevRuleLine *rule_line, const char *key, char *attr, Ude if (!is_match) { check_value_format_and_warn(rule_line, key, value, false); - r = rule_line_add_token(rule_line, TK_A_SYSCTL, op, value, attr, /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_A_SYSCTL, op, value, attr, /* is_case_insensitive = */ false, token_str); } else - r = rule_line_add_token(rule_line, TK_M_SYSCTL, op, value, attr, is_case_insensitive); + r = rule_line_add_token(rule_line, TK_M_SYSCTL, op, value, attr, is_case_insensitive, token_str); } else if (streq(key, "KERNELS")) { if (attr) return log_line_invalid_attr(rule_line, key); if (!is_match) return log_line_invalid_op(rule_line, key); - r = rule_line_add_token(rule_line, TK_M_PARENTS_KERNEL, op, value, NULL, is_case_insensitive); + r = rule_line_add_token(rule_line, TK_M_PARENTS_KERNEL, op, value, NULL, is_case_insensitive, token_str); } else if (streq(key, "SUBSYSTEMS")) { if (attr) return log_line_invalid_attr(rule_line, key); if (!is_match) return log_line_invalid_op(rule_line, key); - r = rule_line_add_token(rule_line, TK_M_PARENTS_SUBSYSTEM, op, value, NULL, is_case_insensitive); + r = rule_line_add_token(rule_line, TK_M_PARENTS_SUBSYSTEM, op, value, NULL, is_case_insensitive, token_str); } else if (streq(key, "DRIVERS")) { if (attr) return log_line_invalid_attr(rule_line, key); if (!is_match) return log_line_invalid_op(rule_line, key); - r = rule_line_add_token(rule_line, TK_M_PARENTS_DRIVER, op, value, NULL, is_case_insensitive); + r = rule_line_add_token(rule_line, TK_M_PARENTS_DRIVER, op, value, NULL, is_case_insensitive, token_str); } else if (streq(key, "ATTRS")) { r = check_attr_format_and_warn(rule_line, key, attr); if (r < 0) @@ -815,14 +901,14 @@ static int parse_token(UdevRuleLine *rule_line, const char *key, char *attr, Ude if (strstr(attr, "../")) log_line_warning(rule_line, "Direct reference to parent sysfs directory, may break in future kernels."); - r = rule_line_add_token(rule_line, TK_M_PARENTS_ATTR, op, value, attr, is_case_insensitive); + r = rule_line_add_token(rule_line, TK_M_PARENTS_ATTR, op, value, attr, is_case_insensitive, token_str); } else if (streq(key, "TAGS")) { if (attr) return log_line_invalid_attr(rule_line, key); if (!is_match) return log_line_invalid_op(rule_line, key); - r = rule_line_add_token(rule_line, TK_M_PARENTS_TAG, op, value, NULL, is_case_insensitive); + r = rule_line_add_token(rule_line, TK_M_PARENTS_TAG, op, value, NULL, is_case_insensitive, token_str); } else if (streq(key, "TEST")) { mode_t mode = MODE_INVALID; @@ -837,7 +923,7 @@ static int parse_token(UdevRuleLine *rule_line, const char *key, char *attr, Ude if (is_case_insensitive) return log_line_invalid_prefix(rule_line, key); - r = rule_line_add_token(rule_line, TK_M_TEST, op, value, MODE_TO_PTR(mode), is_case_insensitive); + r = rule_line_add_token(rule_line, TK_M_TEST, op, value, MODE_TO_PTR(mode), is_case_insensitive, token_str); } else if (streq(key, "PROGRAM")) { if (attr) return log_line_invalid_attr(rule_line, key); @@ -849,7 +935,7 @@ static int parse_token(UdevRuleLine *rule_line, const char *key, char *attr, Ude if (is_case_insensitive) return log_line_invalid_prefix(rule_line, key); - r = rule_line_add_token(rule_line, TK_M_PROGRAM, op, value, NULL, /* is_case_insensitive */ false); + r = rule_line_add_token(rule_line, TK_M_PROGRAM, op, value, NULL, /* is_case_insensitive */ false, token_str); } else if (streq(key, "IMPORT")) { if (isempty(attr)) return log_line_invalid_attr(rule_line, key); @@ -862,16 +948,16 @@ static int parse_token(UdevRuleLine *rule_line, const char *key, char *attr, Ude return log_line_invalid_prefix(rule_line, key); if (streq(attr, "file")) - r = rule_line_add_token(rule_line, TK_M_IMPORT_FILE, op, value, NULL, /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_M_IMPORT_FILE, op, value, NULL, /* is_case_insensitive = */ false, token_str); else if (streq(attr, "program")) { UdevBuiltinCommand cmd; cmd = udev_builtin_lookup(value); if (cmd >= 0) { log_line_debug(rule_line, "Found builtin command '%s' for %s, replacing attribute.", value, key); - r = rule_line_add_token(rule_line, TK_M_IMPORT_BUILTIN, op, value, UDEV_BUILTIN_CMD_TO_PTR(cmd), /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_M_IMPORT_BUILTIN, op, value, UDEV_BUILTIN_CMD_TO_PTR(cmd), /* is_case_insensitive = */ false, token_str); } else - r = rule_line_add_token(rule_line, TK_M_IMPORT_PROGRAM, op, value, NULL, /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_M_IMPORT_PROGRAM, op, value, NULL, /* is_case_insensitive = */ false, token_str); } else if (streq(attr, "builtin")) { UdevBuiltinCommand cmd; @@ -879,13 +965,13 @@ static int parse_token(UdevRuleLine *rule_line, const char *key, char *attr, Ude if (cmd < 0) return log_line_error_errno(rule_line, SYNTHETIC_ERRNO(EINVAL), "Unknown builtin command: %s", value); - r = rule_line_add_token(rule_line, TK_M_IMPORT_BUILTIN, op, value, UDEV_BUILTIN_CMD_TO_PTR(cmd), /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_M_IMPORT_BUILTIN, op, value, UDEV_BUILTIN_CMD_TO_PTR(cmd), /* is_case_insensitive = */ false, token_str); } else if (streq(attr, "db")) - r = rule_line_add_token(rule_line, TK_M_IMPORT_DB, op, value, NULL, /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_M_IMPORT_DB, op, value, NULL, /* is_case_insensitive = */ false, token_str); else if (streq(attr, "cmdline")) - r = rule_line_add_token(rule_line, TK_M_IMPORT_CMDLINE, op, value, NULL, /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_M_IMPORT_CMDLINE, op, value, NULL, /* is_case_insensitive = */ false, token_str); else if (streq(attr, "parent")) - r = rule_line_add_token(rule_line, TK_M_IMPORT_PARENT, op, value, NULL, /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_M_IMPORT_PARENT, op, value, NULL, /* is_case_insensitive = */ false, token_str); else return log_line_invalid_attr(rule_line, key); } else if (streq(key, "RESULT")) { @@ -894,7 +980,7 @@ static int parse_token(UdevRuleLine *rule_line, const char *key, char *attr, Ude if (!is_match) return log_line_invalid_op(rule_line, key); - r = rule_line_add_token(rule_line, TK_M_RESULT, op, value, NULL, is_case_insensitive); + r = rule_line_add_token(rule_line, TK_M_RESULT, op, value, NULL, is_case_insensitive, token_str); } else if (streq(key, "OPTIONS")) { char *tmp; @@ -906,24 +992,24 @@ static int parse_token(UdevRuleLine *rule_line, const char *key, char *attr, Ude op = OP_ASSIGN; if (streq(value, "string_escape=none")) - r = rule_line_add_token(rule_line, TK_A_OPTIONS_STRING_ESCAPE_NONE, op, NULL, NULL, /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_A_OPTIONS_STRING_ESCAPE_NONE, op, NULL, NULL, /* is_case_insensitive = */ false, token_str); else if (streq(value, "string_escape=replace")) - r = rule_line_add_token(rule_line, TK_A_OPTIONS_STRING_ESCAPE_REPLACE, op, NULL, NULL, /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_A_OPTIONS_STRING_ESCAPE_REPLACE, op, NULL, NULL, /* is_case_insensitive = */ false, token_str); else if (streq(value, "db_persist")) - r = rule_line_add_token(rule_line, TK_A_OPTIONS_DB_PERSIST, op, NULL, NULL, /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_A_OPTIONS_DB_PERSIST, op, NULL, NULL, /* is_case_insensitive = */ false, token_str); else if (streq(value, "watch")) - r = rule_line_add_token(rule_line, TK_A_OPTIONS_INOTIFY_WATCH, op, NULL, INT_TO_PTR(1), /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_A_OPTIONS_INOTIFY_WATCH, op, NULL, INT_TO_PTR(1), /* is_case_insensitive = */ false, token_str); else if (streq(value, "nowatch")) - r = rule_line_add_token(rule_line, TK_A_OPTIONS_INOTIFY_WATCH, op, NULL, INT_TO_PTR(0), /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_A_OPTIONS_INOTIFY_WATCH, op, NULL, INT_TO_PTR(0), /* is_case_insensitive = */ false, token_str); else if ((tmp = startswith(value, "static_node="))) - r = rule_line_add_token(rule_line, TK_A_OPTIONS_STATIC_NODE, op, tmp, NULL, /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_A_OPTIONS_STATIC_NODE, op, tmp, NULL, /* is_case_insensitive = */ false, token_str); else if ((tmp = startswith(value, "link_priority="))) { int prio; r = safe_atoi(tmp, &prio); if (r < 0) return log_line_error_errno(rule_line, r, "Failed to parse link priority '%s': %m", tmp); - r = rule_line_add_token(rule_line, TK_A_OPTIONS_DEVLINK_PRIORITY, op, NULL, INT_TO_PTR(prio), /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_A_OPTIONS_DEVLINK_PRIORITY, op, NULL, INT_TO_PTR(prio), /* is_case_insensitive = */ false, token_str); } else if ((tmp = startswith(value, "log_level="))) { int level; @@ -934,7 +1020,7 @@ static int parse_token(UdevRuleLine *rule_line, const char *key, char *attr, Ude if (level < 0) return log_line_error_errno(rule_line, level, "Failed to parse log level '%s': %m", tmp); } - r = rule_line_add_token(rule_line, TK_A_OPTIONS_LOG_LEVEL, op, NULL, INT_TO_PTR(level), /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_A_OPTIONS_LOG_LEVEL, op, NULL, INT_TO_PTR(level), /* is_case_insensitive = */ false, token_str); } else { log_line_warning(rule_line, "Invalid value for OPTIONS key, ignoring: '%s'", value); return 0; @@ -952,17 +1038,17 @@ static int parse_token(UdevRuleLine *rule_line, const char *key, char *attr, Ude } if (parse_uid(value, &uid) >= 0) - r = rule_line_add_token(rule_line, TK_A_OWNER_ID, op, NULL, UID_TO_PTR(uid), /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_A_OWNER_ID, op, NULL, UID_TO_PTR(uid), /* is_case_insensitive = */ false, token_str); else if (resolve_name_timing == RESOLVE_NAME_EARLY && rule_get_substitution_type(value) == SUBST_TYPE_PLAIN) { r = rule_resolve_user(rule_line, value, &uid); if (r < 0) return log_line_error_errno(rule_line, r, "Failed to resolve user name '%s': %m", value); - r = rule_line_add_token(rule_line, TK_A_OWNER_ID, op, NULL, UID_TO_PTR(uid), /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_A_OWNER_ID, op, NULL, UID_TO_PTR(uid), /* is_case_insensitive = */ false, token_str); } else if (resolve_name_timing != RESOLVE_NAME_NEVER) { check_value_format_and_warn(rule_line, key, value, true); - r = rule_line_add_token(rule_line, TK_A_OWNER, op, value, NULL, /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_A_OWNER, op, value, NULL, /* is_case_insensitive = */ false, token_str); } else { log_line_debug(rule_line, "User name resolution is disabled, ignoring %s=\"%s\".", key, value); return 0; @@ -980,17 +1066,17 @@ static int parse_token(UdevRuleLine *rule_line, const char *key, char *attr, Ude } if (parse_gid(value, &gid) >= 0) - r = rule_line_add_token(rule_line, TK_A_GROUP_ID, op, NULL, GID_TO_PTR(gid), /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_A_GROUP_ID, op, NULL, GID_TO_PTR(gid), /* is_case_insensitive = */ false, token_str); else if (resolve_name_timing == RESOLVE_NAME_EARLY && rule_get_substitution_type(value) == SUBST_TYPE_PLAIN) { r = rule_resolve_group(rule_line, value, &gid); if (r < 0) return log_line_error_errno(rule_line, r, "Failed to resolve group name '%s': %m", value); - r = rule_line_add_token(rule_line, TK_A_GROUP_ID, op, NULL, GID_TO_PTR(gid), /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_A_GROUP_ID, op, NULL, GID_TO_PTR(gid), /* is_case_insensitive = */ false, token_str); } else if (resolve_name_timing != RESOLVE_NAME_NEVER) { check_value_format_and_warn(rule_line, key, value, true); - r = rule_line_add_token(rule_line, TK_A_GROUP, op, value, NULL, /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_A_GROUP, op, value, NULL, /* is_case_insensitive = */ false, token_str); } else { log_line_debug(rule_line, "Resolving group name is disabled, ignoring GROUP=\"%s\".", value); return 0; @@ -1008,10 +1094,10 @@ static int parse_token(UdevRuleLine *rule_line, const char *key, char *attr, Ude } if (parse_mode(value, &mode) >= 0) - r = rule_line_add_token(rule_line, TK_A_MODE_ID, op, NULL, MODE_TO_PTR(mode), /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_A_MODE_ID, op, NULL, MODE_TO_PTR(mode), /* is_case_insensitive = */ false, token_str); else { check_value_format_and_warn(rule_line, key, value, true); - r = rule_line_add_token(rule_line, TK_A_MODE, op, value, NULL, /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_A_MODE, op, value, NULL, /* is_case_insensitive = */ false, token_str); } } else if (streq(key, "SECLABEL")) { if (isempty(attr)) @@ -1024,13 +1110,13 @@ static int parse_token(UdevRuleLine *rule_line, const char *key, char *attr, Ude op = OP_ASSIGN; } - r = rule_line_add_token(rule_line, TK_A_SECLABEL, op, value, attr, /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_A_SECLABEL, op, value, attr, /* is_case_insensitive = */ false, token_str); } else if (streq(key, "RUN")) { if (is_match || op == OP_REMOVE) return log_line_invalid_op(rule_line, key); check_value_format_and_warn(rule_line, key, value, true); if (!attr || streq(attr, "program")) - r = rule_line_add_token(rule_line, TK_A_RUN_PROGRAM, op, value, NULL, /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_A_RUN_PROGRAM, op, value, NULL, /* is_case_insensitive = */ false, token_str); else if (streq(attr, "builtin")) { UdevBuiltinCommand cmd; @@ -1038,7 +1124,7 @@ static int parse_token(UdevRuleLine *rule_line, const char *key, char *attr, Ude if (cmd < 0) return log_line_error_errno(rule_line, SYNTHETIC_ERRNO(EINVAL), "Unknown builtin command '%s', ignoring.", value); - r = rule_line_add_token(rule_line, TK_A_RUN_BUILTIN, op, value, UDEV_BUILTIN_CMD_TO_PTR(cmd), /* is_case_insensitive = */ false); + r = rule_line_add_token(rule_line, TK_A_RUN_BUILTIN, op, value, UDEV_BUILTIN_CMD_TO_PTR(cmd), /* is_case_insensitive = */ false, token_str); } else return log_line_invalid_attr(rule_line, key); } else if (streq(key, "GOTO")) { @@ -1298,32 +1384,30 @@ static void sort_tokens(UdevRuleLine *rule_line) { } } -static int rule_add_line(UdevRuleFile *rule_file, const char *line_str, unsigned line_nr, bool extra_checks) { +static int rule_add_line(UdevRuleFile *rule_file, const char *line, unsigned line_nr, bool extra_checks) { _cleanup_(udev_rule_line_freep) UdevRuleLine *rule_line = NULL; - _cleanup_free_ char *line = NULL; char *p; int r; assert(rule_file); - assert(line_str); + assert(line); - if (isempty(line_str)) + if (isempty(line)) return 0; - line = strdup(line_str); - if (!line) - return log_oom(); - rule_line = new(UdevRuleLine, 1); if (!rule_line) return log_oom(); *rule_line = (UdevRuleLine) { - .line = TAKE_PTR(line), + .line = strdup(line), + .line_for_logging = strdup(line), .line_number = line_nr, - .rule_file = rule_file, }; + if (!rule_line->line || !rule_line->line_for_logging) + return log_oom(); + rule_line->rule_file = rule_file; LIST_APPEND(rule_lines, rule_file->rule_lines, rule_line); for (p = rule_line->line; !isempty(p); ) { @@ -1340,7 +1424,9 @@ static int rule_add_line(UdevRuleFile *rule_file, const char *line_str, unsigned if (r == 0) break; - r = parse_token(rule_line, key, attr, op, value, is_case_insensitive); + char *token_str = rule_line->line_for_logging + (key - rule_line->line); + token_str[p - key] = '\0'; + r = parse_token(rule_line, key, attr, op, value, is_case_insensitive, token_str); if (r < 0) return r; } @@ -1731,10 +1817,59 @@ bool udev_rules_should_reload(UdevRules *rules) { return false; } -static bool token_match_string(UdevRuleToken *token, const char *str) { +static bool apply_format_full( + UdevEvent *event, + UdevRuleToken *token, + const char *format, + char *result, + size_t result_size, + bool replace_whitespace, + const char *what) { + + assert(event); + assert(token); + assert(format); + assert(result); + assert(what); + + bool truncated = false; + (void) udev_event_apply_format(event, format, result, result_size, replace_whitespace, &truncated); + if (truncated) { + log_event_truncated(event, token, what, format); + return false; + } + + if (event->trace && !streq(format, result)) + log_event_trace(event, token, "Format substitution: \"%s\" -> \"%s\"", format, result); + + return true; +} + +static bool apply_format_value( + UdevEvent *event, + UdevRuleToken *token, + char *result, + size_t result_size, + const char *what) { + + return apply_format_full(event, token, token->value, result, result_size, /* replace_whitespace = */ false, what); +} + +static bool apply_format_attr( + UdevEvent *event, + UdevRuleToken *token, + char *result, + size_t result_size, + const char *what) { + + return apply_format_full(event, token, token->data, result, result_size, /* replace_whitespace = */ false, what); +} + +static bool token_match_string(UdevEvent *event, UdevRuleToken *token, const char *str, bool log_result) { const char *value; bool match = false, case_insensitive; + assert(event); assert(token); assert(token->value); assert(token->type < _TK_M_MAX); @@ -1783,13 +1918,56 @@ static bool token_match_string(UdevRuleToken *token, const char *str) { assert_not_reached(); } - return token->op == (match ? OP_MATCH : OP_NOMATCH); + bool result = token->op == (match ? OP_MATCH : OP_NOMATCH); + + if (event->trace) + switch (token->match_type & _MATCH_TYPE_MASK) { + case MATCH_TYPE_EMPTY: + log_event_trace(event, token, "String \"%s\" is%s empty%s", + strempty(str), match ? "" : " not", + log_result ? result ? ": PASS" : ": FAIL" : "."); + break; + case MATCH_TYPE_SUBSYSTEM: + log_event_trace(event, token, + "String \"%s\" matches %s of \"subsystem\", \"class\", or \"bus\"%s", + strempty(str), match ? "one" : "neither", + log_result ? result ? ": PASS" : ": FAIL" : "."); + break; + case MATCH_TYPE_PLAIN_WITH_EMPTY: + case MATCH_TYPE_PLAIN: + case MATCH_TYPE_GLOB_WITH_EMPTY: + case MATCH_TYPE_GLOB: { + _cleanup_free_ char *joined = NULL; + unsigned c = 0; + + if (IN_SET(token->match_type & _MATCH_TYPE_MASK, MATCH_TYPE_PLAIN_WITH_EMPTY, MATCH_TYPE_GLOB_WITH_EMPTY)) { + (void) strextend_with_separator(&joined, ", ", "\"\""); + c++; + } + + NULSTR_FOREACH(i, value) { + (void) strextendf_with_separator(&joined, ", ", "\"%s\"", i); + c++; + } + + assert(c > 0); + log_event_trace(event, token, "String \"%s\" %s %s%s", + strempty(str), + match ? (c > 1 ? "matches one of" : "matches") : (c > 1 ? "matches neither of" : "does not match"), + strempty(joined), + log_result ? result ? ": PASS" : ": FAIL" : "."); + break; + } + default: + assert_not_reached(); + } + + return result; } static bool token_match_attr(UdevRuleToken *token, sd_device *dev, UdevEvent *event) { char nbuf[UDEV_NAME_SIZE], vbuf[UDEV_NAME_SIZE]; const char *name, *value; - bool truncated; assert(token); assert(IN_SET(token->type, TK_M_ATTR, TK_M_PARENTS_ATTR)); @@ -1800,12 +1978,8 @@ static bool token_match_attr(UdevRuleToken *token, sd_device *dev, UdevEvent *ev switch (token->attr_subst_type) { case SUBST_TYPE_FORMAT: - (void) udev_event_apply_format(event, name, nbuf, sizeof(nbuf), false, &truncated); - if (truncated) { - log_event_truncated(dev, token, "sysfs attribute name", name, - token->type == TK_M_ATTR ? "ATTR" : "ATTRS", /* is_match = */ true); + if (!apply_format_attr(event, token, nbuf, sizeof(nbuf), "sysfs attribute name")) return false; - } name = nbuf; _fallthrough_; @@ -1819,7 +1993,7 @@ static bool token_match_attr(UdevRuleToken *token, sd_device *dev, UdevEvent *ev value = delete_trailing_chars(vbuf, NULL); } - return token_match_string(token, value); + return token_match_string(event, token, value, /* log_result = */ true); case SUBST_TYPE_SUBSYS: if (udev_resolve_subsys_kernel(name, vbuf, sizeof(vbuf), true) < 0) @@ -1829,7 +2003,7 @@ static bool token_match_attr(UdevRuleToken *token, sd_device *dev, UdevEvent *ev if (FLAGS_SET(token->match_type, MATCH_REMOVE_TRAILING_WHITESPACE)) delete_trailing_chars(vbuf, NULL); - return token_match_string(token, vbuf); + return token_match_string(event, token, vbuf, /* log_result = */ true); default: assert_not_reached(); @@ -1881,30 +2055,6 @@ static int get_property_from_string(char *line, char **ret_key, char **ret_value return 1; } -static int import_parent_into_properties(sd_device *dev, const char *filter) { - sd_device *parent; - int r; - - assert(dev); - assert(filter); - - r = sd_device_get_parent(dev, &parent); - if (r == -ENOENT) - return 0; - if (r < 0) - return r; - - FOREACH_DEVICE_PROPERTY(parent, key, val) { - if (fnmatch(filter, key, 0) != 0) - continue; - r = device_add_property(dev, key, val); - if (r < 0) - return r; - } - - return 1; -} - static int attr_subst_subdir(char attr[static UDEV_PATH_SIZE]) { _cleanup_closedir_ DIR *dir = NULL; char buf[UDEV_PATH_SIZE], *p; @@ -1949,7 +2099,7 @@ static int attr_subst_subdir(char attr[static UDEV_PATH_SIZE]) { return -ENOENT; } -static size_t udev_replace_ifname(char *str) { +static size_t udev_replace_ifname_strict(char *str) { size_t replaced = 0; assert(str); @@ -1965,6 +2115,35 @@ static size_t udev_replace_ifname(char *str) { return replaced; } +static void udev_replace_ifname(UdevEvent *event, UdevRuleToken *token, char *buf) { + assert(event); + assert(token); + assert(buf); + + size_t count; + if (naming_scheme_has(NAMING_REPLACE_STRICTLY)) + count = udev_replace_ifname_strict(buf); + else + count = udev_replace_chars(buf, "/"); + if (count > 0) + log_event_trace(event, token, + "Replaced %zu character(s) from network interface name, results to \"%s\"", + count, buf); +} + +static void udev_replace_chars_and_log(UdevEvent *event, UdevRuleToken *token, char *buf, const char *allow, const char *what) { + assert(event); + assert(token); + assert(buf); + assert(what); + + size_t count = udev_replace_chars(buf, allow); + if (count > 0) + log_event_trace(event, token, + "Replaced %zu character(s) from %s, results to \"%s\"", + count, what, buf); +} + static int udev_rule_apply_token_to_event( UdevRuleToken *token, sd_device *dev, @@ -1987,18 +2166,18 @@ static int udev_rule_apply_token_to_event( r = sd_device_get_action(dev, &a); if (r < 0) - return log_event_error_errno(dev, token, r, "Failed to get uevent action type: %m"); + return log_event_error_errno(event, token, r, "Failed to get uevent action type: %m"); - return token_match_string(token, device_action_to_string(a)); + return token_match_string(event, token, device_action_to_string(a), /* log_result = */ true); } case TK_M_DEVPATH: { const char *val; r = sd_device_get_devpath(dev, &val); if (r < 0) - return log_event_error_errno(dev, token, r, "Failed to get devpath: %m"); + return log_event_error_errno(event, token, r, "Failed to get devpath: %m"); - return token_match_string(token, val); + return token_match_string(event, token, val, /* log_result = */ true); } case TK_M_KERNEL: case TK_M_PARENTS_KERNEL: { @@ -2006,23 +2185,25 @@ static int udev_rule_apply_token_to_event( r = sd_device_get_sysname(dev, &val); if (r < 0) - return log_event_error_errno(dev, token, r, "Failed to get sysname: %m"); + return log_event_error_errno(event, token, r, "Failed to get sysname: %m"); - return token_match_string(token, val); + return token_match_string(event, token, val, /* log_result = */ true); } case TK_M_DEVLINK: FOREACH_DEVICE_DEVLINK(dev, val) - if (token_match_string(token, strempty(startswith(val, "/dev/"))) == (token->op == OP_MATCH)) - return token->op == OP_MATCH; - return token->op == OP_NOMATCH; + if (token_match_string(event, token, strempty(startswith(val, "/dev/")), /* log_result = */ false) == (token->op == OP_MATCH)) + return log_event_result(event, token, token->op == OP_MATCH); + return log_event_result(event, token, token->op == OP_NOMATCH); + case TK_M_NAME: - return token_match_string(token, event->name); + return token_match_string(event, token, event->name, /* log_result = */ true); + case TK_M_ENV: { const char *val = NULL; (void) device_get_property_value_with_fallback(dev, token->data, event->worker ? event->worker->properties : NULL, &val); - return token_match_string(token, val); + return token_match_string(event, token, val, /* log_result = */ true); } case TK_M_CONST: { const char *val, *k = token->data; @@ -2035,14 +2216,15 @@ static int udev_rule_apply_token_to_event( val = confidential_virtualization_to_string(detect_confidential_virtualization()); else assert_not_reached(); - return token_match_string(token, val); + return token_match_string(event, token, val, /* log_result = */ true); } case TK_M_TAG: case TK_M_PARENTS_TAG: FOREACH_DEVICE_CURRENT_TAG(dev, val) - if (token_match_string(token, val) == (token->op == OP_MATCH)) - return token->op == OP_MATCH; - return token->op == OP_NOMATCH; + if (token_match_string(event, token, val, /* log_result = */ false) == (token->op == OP_MATCH)) + return log_event_result(event, token, token->op == OP_MATCH); + return log_event_result(event, token, token->op == OP_NOMATCH); + case TK_M_SUBSYSTEM: case TK_M_PARENTS_SUBSYSTEM: { const char *val; @@ -2051,9 +2233,9 @@ static int udev_rule_apply_token_to_event( if (r == -ENOENT) val = NULL; else if (r < 0) - return log_event_error_errno(dev, token, r, "Failed to get subsystem: %m"); + return log_event_error_errno(event, token, r, "Failed to get subsystem: %m"); - return token_match_string(token, val); + return token_match_string(event, token, val, /* log_result = */ true); } case TK_M_DRIVER: case TK_M_PARENTS_DRIVER: { @@ -2063,41 +2245,35 @@ static int udev_rule_apply_token_to_event( if (r == -ENOENT) val = NULL; else if (r < 0) - return log_event_error_errno(dev, token, r, "Failed to get driver: %m"); + return log_event_error_errno(event, token, r, "Failed to get driver: %m"); - return token_match_string(token, val); + return token_match_string(event, token, val, /* log_result = */ true); } case TK_M_ATTR: case TK_M_PARENTS_ATTR: return token_match_attr(token, dev, event); + case TK_M_SYSCTL: { _cleanup_free_ char *value = NULL; char buf[UDEV_PATH_SIZE]; - bool truncated; - (void) udev_event_apply_format(event, token->data, buf, sizeof(buf), false, &truncated); - if (truncated) { - log_event_truncated(dev, token, "sysctl entry name", token->data, "SYSCTL", /* is_match = */ true); + if (!apply_format_attr(event, token, buf, sizeof(buf), "sysctl entry name")) return false; - } r = sysctl_read(sysctl_normalize(buf), &value); if (r < 0 && r != -ENOENT) - return log_event_error_errno(dev, token, r, "Failed to read sysctl '%s': %m", buf); + return log_event_error_errno(event, token, r, "Failed to read sysctl \"%s\": %m", buf); - return token_match_string(token, strstrip(value)); + return token_match_string(event, token, strstrip(value), /* log_result = */ true); } case TK_M_TEST: { mode_t mode = PTR_TO_MODE(token->data); char buf[UDEV_PATH_SIZE]; struct stat statbuf; - bool match, truncated; + bool match; - (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false, &truncated); - if (truncated) { - log_event_truncated(dev, token, "file name", token->value, "TEST", /* is_match = */ true); + if (!apply_format_value(event, token, buf, sizeof(buf), "file name")) return false; - } if (!path_is_absolute(buf) && udev_resolve_subsys_kernel(buf, buf, sizeof(buf), false) < 0) { @@ -2106,81 +2282,73 @@ static int udev_rule_apply_token_to_event( r = sd_device_get_syspath(dev, &val); if (r < 0) - return log_event_error_errno(dev, token, r, "Failed to get syspath: %m"); + return log_event_error_errno(event, token, r, "Failed to get syspath: %m"); + bool truncated; strscpy_full(tmp, sizeof(tmp), buf, &truncated); assert(!truncated); strscpyl_full(buf, sizeof(buf), &truncated, val, "/", tmp, NULL); if (truncated) - return false; + return log_event_result(event, token, false); } r = attr_subst_subdir(buf); if (r == -ENOENT) - return token->op == OP_NOMATCH; + return log_event_result(event, token, token->op == OP_NOMATCH); if (r < 0) - return log_event_error_errno(dev, token, r, "Failed to test for the existence of '%s': %m", buf); + return log_event_error_errno(event, token, r, "Failed to test for the existence of \"%s\": %m", buf); - if (stat(buf, &statbuf) < 0) - return token->op == OP_NOMATCH; + if (stat(buf, &statbuf) < 0) { + if (errno != ENOENT) + log_event_warning_errno(event, token, errno, "Failed to stat \"%s\", ignoring: %m", buf); + return log_event_result(event, token, token->op == OP_NOMATCH); + } if (mode == MODE_INVALID) - return token->op == OP_MATCH; + return log_event_result(event, token, token->op == OP_MATCH); match = (statbuf.st_mode & mode) > 0; - return token->op == (match ? OP_MATCH : OP_NOMATCH); + return log_event_result(event, token, token->op == (match ? OP_MATCH : OP_NOMATCH)); } case TK_M_PROGRAM: { char buf[UDEV_LINE_SIZE], result[UDEV_LINE_SIZE]; - bool truncated; - size_t count; event->program_result = mfree(event->program_result); - (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false, &truncated); - if (truncated) { - log_event_truncated(dev, token, "command", token->value, "PROGRAM", /* is_match = */ true); + + if (!apply_format_value(event, token, buf, sizeof(buf), "command")) return false; - } - log_event_debug(dev, token, "Running PROGRAM=\"%s\"", buf); + log_event_debug(event, token, "Running command \"%s\"", buf); r = udev_event_spawn(event, /* accept_failure = */ true, buf, result, sizeof(result), NULL); if (r != 0) { if (r < 0) - log_event_warning_errno(dev, token, r, "Failed to execute \"%s\": %m", buf); + log_event_warning_errno(event, token, r, "Failed to execute \"%s\": %m", buf); else /* returned value is positive when program fails */ - log_event_debug(dev, token, "Command \"%s\" returned %d (error)", buf, r); - return token->op == OP_NOMATCH; + log_event_debug(event, token, "Command \"%s\" returned %d (error)", buf, r); + return log_event_result(event, token, token->op == OP_NOMATCH); } delete_trailing_chars(result, "\n"); - count = udev_replace_chars(result, UDEV_ALLOWED_CHARS_INPUT); - if (count > 0) - log_event_debug(dev, token, - "Replaced %zu character(s) in result of \"%s\"", - count, buf); + udev_replace_chars_and_log(event, token, result, UDEV_ALLOWED_CHARS_INPUT, "command output"); event->program_result = strdup(result); - return token->op == OP_MATCH; + return log_event_result(event, token, token->op == OP_MATCH); } case TK_M_IMPORT_FILE: { _cleanup_fclose_ FILE *f = NULL; char buf[UDEV_PATH_SIZE]; - bool truncated; - (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false, &truncated); - if (truncated) { - log_event_truncated(dev, token, "file name to be imported", token->value, "IMPORT", /* is_match = */ true); + if (!apply_format_value(event, token, buf, sizeof(buf), "file name to be imported")) return false; - } - log_event_debug(dev, token, "Importing properties from '%s'", buf); + log_event_debug(event, token, "Importing properties from \"%s\"", buf); f = fopen(buf, "re"); if (!f) { if (errno != ENOENT) - return log_event_error_errno(dev, token, errno, "Failed to open '%s': %m", buf); - return token->op == OP_NOMATCH; + return log_event_error_errno(event, token, errno, "Failed to open \"%s\": %m", buf); + return log_event_result(event, token, token->op == OP_NOMATCH); } for (;;) { @@ -2189,16 +2357,16 @@ static int udev_rule_apply_token_to_event( r = read_line(f, LONG_LINE_MAX, &line); if (r < 0) { - log_event_debug_errno(dev, token, r, "Failed to read '%s', ignoring: %m", buf); - return token->op == OP_NOMATCH; + log_event_debug_errno(event, token, r, "Failed to read \"%s\", ignoring: %m", buf); + return log_event_result(event, token, token->op == OP_NOMATCH); } if (r == 0) - break; + return log_event_result(event, token, token->op == OP_MATCH); r = get_property_from_string(line, &key, &value); if (r < 0) { - log_event_debug_errno(dev, token, r, - "Failed to parse key and value from '%s', ignoring: %m", + log_event_debug_errno(event, token, r, + "Failed to parse key and value from \"%s\", ignoring: %m", line); continue; } @@ -2207,54 +2375,55 @@ static int udev_rule_apply_token_to_event( r = device_add_property(dev, key, value); if (r < 0) - return log_event_error_errno(dev, token, r, + return log_event_error_errno(event, token, r, "Failed to add property %s=%s: %m", key, value); + log_event_trace(event, token, "Imported property \"%s=%s\".", key, value); } - return token->op == OP_MATCH; + assert_not_reached(); } case TK_M_IMPORT_PROGRAM: { _cleanup_strv_free_ char **lines = NULL; char buf[UDEV_LINE_SIZE], result[UDEV_LINE_SIZE]; bool truncated; - (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false, &truncated); - if (truncated) { - log_event_truncated(dev, token, "command", token->value, "IMPORT", /* is_match = */ true); + if (!apply_format_value(event, token, buf, sizeof(buf), "command")) return false; - } - log_event_debug(dev, token, "Importing properties from results of '%s'", buf); + log_event_debug(event, token, "Importing properties from results of \"%s\"", buf); r = udev_event_spawn(event, /* accept_failure = */ true, buf, result, sizeof result, &truncated); if (r != 0) { if (r < 0) - log_event_warning_errno(dev, token, r, "Failed to execute '%s', ignoring: %m", buf); + log_event_warning_errno(event, token, r, "Failed to execute \"%s\", ignoring: %m", buf); else /* returned value is positive when program fails */ - log_event_debug(dev, token, "Command \"%s\" returned %d (error), ignoring", buf, r); - return token->op == OP_NOMATCH; + log_event_debug(event, token, "Command \"%s\" returned %d (error), ignoring", buf, r); + return log_event_result(event, token, token->op == OP_NOMATCH); } if (truncated) { - bool found = false; + log_event_debug(event, token, "Result of \"%s\" is too long and truncated, ignoring the last line of the result.", buf); /* Drop the last line. */ + bool found = false; for (char *p = PTR_SUB1(buf + strlen(buf), buf); p; p = PTR_SUB1(p, buf)) if (strchr(NEWLINE, *p)) { *p = '\0'; found = true; - } else if (found) break; + } + if (!found) + buf[0] = '\0'; } r = strv_split_newlines_full(&lines, result, EXTRACT_RETAIN_ESCAPE); if (r == -ENOMEM) return log_oom(); if (r < 0) { - log_event_warning_errno(dev, token, r, + log_event_warning_errno(event, token, r, "Failed to extract lines from result of command \"%s\", ignoring: %m", buf); - return false; + return log_event_result(event, token, false); } STRV_FOREACH(line, lines) { @@ -2262,8 +2431,8 @@ static int udev_rule_apply_token_to_event( r = get_property_from_string(*line, &key, &value); if (r < 0) { - log_event_debug_errno(dev, token, r, - "Failed to parse key and value from '%s', ignoring: %m", + log_event_debug_errno(event, token, r, + "Failed to parse key and value from \"%s\", ignoring: %m", *line); continue; } @@ -2272,123 +2441,142 @@ static int udev_rule_apply_token_to_event( r = device_add_property(dev, key, value); if (r < 0) - return log_event_error_errno(dev, token, r, + return log_event_error_errno(event, token, r, "Failed to add property %s=%s: %m", key, value); + log_event_trace(event, token, "Imported property \"%s=%s\".", key, value); } - return token->op == OP_MATCH; + return log_event_result(event, token, token->op == OP_MATCH); } case TK_M_IMPORT_BUILTIN: { UdevBuiltinCommand cmd = PTR_TO_UDEV_BUILTIN_CMD(token->data); assert(cmd >= 0 && cmd < _UDEV_BUILTIN_MAX); unsigned mask = 1U << (int) cmd; char buf[UDEV_LINE_SIZE]; - bool truncated; if (udev_builtin_run_once(cmd)) { /* check if we ran already */ if (event->builtin_run & mask) { - log_event_debug(dev, token, "Skipping builtin '%s' in IMPORT key", + log_event_debug(event, token, "Builtin command \"%s\" has already run, skipping.", udev_builtin_name(cmd)); /* return the result from earlier run */ - return token->op == (event->builtin_ret & mask ? OP_NOMATCH : OP_MATCH); + return log_event_result(event, token, token->op == (event->builtin_ret & mask ? OP_NOMATCH : OP_MATCH)); } /* mark as ran */ event->builtin_run |= mask; } - (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false, &truncated); - if (truncated) { - log_event_truncated(dev, token, "builtin command", token->value, "IMPORT", /* is_match = */ true); + if (!apply_format_value(event, token, buf, sizeof(buf), "builtin command")) return false; - } - log_event_debug(dev, token, "Importing properties from results of builtin command '%s'", buf); + log_event_debug(event, token, "Importing properties from results of builtin command \"%s\".", buf); r = udev_builtin_run(event, cmd, buf); if (r < 0) { /* remember failure */ - log_event_debug_errno(dev, token, r, "Failed to run builtin '%s': %m", buf); + log_event_debug_errno(event, token, r, "Failed to run builtin \"%s\": %m", buf); event->builtin_ret |= mask; } - return token->op == (r >= 0 ? OP_MATCH : OP_NOMATCH); + return log_event_result(event, token, token->op == (r >= 0 ? OP_MATCH : OP_NOMATCH)); } case TK_M_IMPORT_DB: { const char *val; if (!event->dev_db_clone) - return token->op == OP_NOMATCH; + return log_event_result(event, token, token->op == OP_NOMATCH); r = sd_device_get_property_value(event->dev_db_clone, token->value, &val); if (r == -ENOENT) - return token->op == OP_NOMATCH; + return log_event_result(event, token, token->op == OP_NOMATCH); if (r < 0) - return log_event_error_errno(dev, token, r, - "Failed to get property '%s' from database: %m", + return log_event_error_errno(event, token, r, + "Failed to get property \"%s\" from database: %m", token->value); r = device_add_property(dev, token->value, val); if (r < 0) - return log_event_error_errno(dev, token, r, "Failed to add property '%s=%s': %m", + return log_event_error_errno(event, token, r, "Failed to add property \"%s=%s\": %m", token->value, val); - return token->op == OP_MATCH; + log_event_trace(event, token, "Imported property \"%s=%s\".", token->value, val); + + return log_event_result(event, token, token->op == OP_MATCH); } case TK_M_IMPORT_CMDLINE: { _cleanup_free_ char *value = NULL; r = proc_cmdline_get_key(token->value, PROC_CMDLINE_VALUE_OPTIONAL|PROC_CMDLINE_IGNORE_EFI_OPTIONS, &value); if (r < 0) - return log_event_error_errno(dev, token, r, - "Failed to read '%s' option from /proc/cmdline: %m", + return log_event_error_errno(event, token, r, + "Failed to read \"%s\" option from /proc/cmdline: %m", token->value); if (r == 0) - return token->op == OP_NOMATCH; + return log_event_result(event, token, token->op == OP_NOMATCH); - r = device_add_property(dev, token->value, value ?: "1"); + const char *val = value ?: "1"; + r = device_add_property(dev, token->value, val); if (r < 0) - return log_event_error_errno(dev, token, r, "Failed to add property '%s=%s': %m", - token->value, value ?: "1"); - return token->op == OP_MATCH; + return log_event_error_errno(event, token, r, "Failed to add property \"%s=%s\": %m", + token->value, val); + log_event_trace(event, token, "Imported property \"%s=%s\".", token->value, val); + + return log_event_result(event, token, token->op == OP_MATCH); } case TK_M_IMPORT_PARENT: { char buf[UDEV_PATH_SIZE]; - bool truncated; - (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false, &truncated); - if (truncated) { - log_event_truncated(dev, token, "property name", token->value, "IMPORT", /* is_match = */ true); + if (!apply_format_value(event, token, buf, sizeof(buf), "property name")) return false; - } - r = import_parent_into_properties(dev, buf); + sd_device *parent; + r = sd_device_get_parent(dev, &parent); + if (ERRNO_IS_NEG_DEVICE_ABSENT(r)) + return log_event_result(event, token, token->op == OP_NOMATCH); if (r < 0) - return log_event_error_errno(dev, token, r, - "Failed to import properties '%s' from parent: %m", - buf); - return token->op == (r > 0 ? OP_MATCH : OP_NOMATCH); + return log_event_error_errno(event, token, r, "Failed to get parent device: %m"); + + bool have = false; + FOREACH_DEVICE_PROPERTY(parent, key, val) { + if (fnmatch(buf, key, 0) != 0) + continue; + + r = device_add_property(dev, key, val); + if (r < 0) + return log_event_error_errno(event, token, r, "Failed to add property \"%s=%s\": %m", key, val); + log_event_trace(event, token, "Imported property \"%s=%s\".", key, val); + have = true; + } + + return log_event_result(event, token, token->op == (have ? OP_MATCH : OP_NOMATCH)); } case TK_M_RESULT: - return token_match_string(token, event->program_result); + return token_match_string(event, token, event->program_result, /* log_result = */ true); + case TK_A_OPTIONS_STRING_ESCAPE_NONE: event->esc = ESCAPE_NONE; - break; + return log_event_done(event, token); + case TK_A_OPTIONS_STRING_ESCAPE_REPLACE: event->esc = ESCAPE_REPLACE; - break; + return log_event_done(event, token); + case TK_A_OPTIONS_DB_PERSIST: device_set_db_persist(dev); - break; + return log_event_done(event, token); + case TK_A_OPTIONS_INOTIFY_WATCH: if (event->inotify_watch_final) - break; + return log_event_final_set(event, token); + if (token->op == OP_ASSIGN_FINAL) event->inotify_watch_final = true; event->inotify_watch = token->data; - break; + return log_event_done(event, token); + case TK_A_OPTIONS_DEVLINK_PRIORITY: device_set_devlink_priority(dev, PTR_TO_INT(token->data)); - break; + return log_event_done(event, token); + case TK_A_OPTIONS_LOG_LEVEL: { int level = PTR_TO_INT(token->data); @@ -2400,7 +2588,7 @@ static int udev_rule_apply_token_to_event( else { _cleanup_free_ char *level_str = NULL; (void) log_level_to_string_alloc(level, &level_str); - log_event_debug(dev, token, "Running in test mode, skipping changing maximum log level to %s.", strna(level_str)); + log_event_debug(event, token, "Running in test mode, skipping changing maximum log level to %s.", strna(level_str)); } if (level == LOG_DEBUG && !event->log_level_was_debug) { @@ -2410,120 +2598,114 @@ static int udev_rule_apply_token_to_event( event->log_level_was_debug = true; } - break; + return log_event_done(event, token); } case TK_A_OWNER: { char owner[UDEV_NAME_SIZE]; const char *ow = owner; - bool truncated; if (event->owner_final) - break; + return log_event_final_set(event, token); + if (token->op == OP_ASSIGN_FINAL) event->owner_final = true; - (void) udev_event_apply_format(event, token->value, owner, sizeof(owner), false, &truncated); - if (truncated) { - log_event_truncated(dev, token, "user name", token->value, "OWNER", /* is_match = */ false); - break; - } + if (!apply_format_value(event, token, owner, sizeof(owner), "user name")) + return true; r = get_user_creds(&ow, &event->uid, NULL, NULL, NULL, USER_CREDS_ALLOW_MISSING); - if (r < 0) - log_unknown_owner(dev, token->rule_line, r, "user", owner); + if (IN_SET(r, -ENOENT, -ESRCH)) + log_event_error_errno(event, token, r, "Unknown user \"%s\", ignoring.", owner); + else if (r < 0) + log_event_error_errno(event, token, r, "Failed to resolve user \"%s\", ignoring: %m", owner); else - log_event_debug(dev, token, "OWNER %s(%u)", owner, event->uid); - break; + log_event_debug(event, token, "Set owner: %s(%u)", owner, event->uid); + return true; } case TK_A_GROUP: { char group[UDEV_NAME_SIZE]; const char *gr = group; - bool truncated; if (event->group_final) - break; + return log_event_final_set(event, token); + if (token->op == OP_ASSIGN_FINAL) event->group_final = true; - (void) udev_event_apply_format(event, token->value, group, sizeof(group), false, &truncated); - if (truncated) { - log_event_truncated(dev, token, "group name", token->value, "GROUP", /* is_match = */ false); - break; - } + if (!apply_format_value(event, token, group, sizeof(group), "group name")) + return true; r = get_group_creds(&gr, &event->gid, USER_CREDS_ALLOW_MISSING); - if (r < 0) - log_unknown_owner(dev, token->rule_line, r, "group", group); + if (IN_SET(r, -ENOENT, -ESRCH)) + log_event_error_errno(event, token, r, "Unknown group \"%s\", ignoring.", group); + else if (r < 0) + log_event_error_errno(event, token, r, "Failed to resolve group \"%s\", ignoring: %m", group); else - log_event_debug(dev, token, "GROUP %s(%u)", group, event->gid); - break; + log_event_debug(event, token, "Set group: %s(%u)", group, event->gid); + return true; } case TK_A_MODE: { char mode_str[UDEV_NAME_SIZE]; - bool truncated; if (event->mode_final) - break; + return log_event_final_set(event, token); + if (token->op == OP_ASSIGN_FINAL) event->mode_final = true; - (void) udev_event_apply_format(event, token->value, mode_str, sizeof(mode_str), false, &truncated); - if (truncated) { - log_event_truncated(dev, token, "mode", token->value, "MODE", /* is_match = */ false); - break; - } + if (!apply_format_value(event, token, mode_str, sizeof(mode_str), "mode")) + return true; r = parse_mode(mode_str, &event->mode); if (r < 0) - log_event_error_errno(dev, token, r, "Failed to parse mode '%s', ignoring: %m", mode_str); + log_event_error_errno(event, token, r, "Failed to parse mode \"%s\", ignoring: %m", mode_str); else - log_event_debug(dev, token, "MODE %#o", event->mode); - break; + log_event_debug(event, token, "Set mode: %#o", event->mode); + return true; } case TK_A_OWNER_ID: if (event->owner_final) - break; + return log_event_final_set(event, token); + if (token->op == OP_ASSIGN_FINAL) event->owner_final = true; - if (!token->data) - break; - event->uid = PTR_TO_UID(token->data); - log_event_debug(dev, token, "OWNER %u", event->uid); - break; + + event->uid = PTR_TO_UID(ASSERT_PTR(token->data)); + log_event_debug(event, token, "Set owner ID: %u", event->uid); + return true; + case TK_A_GROUP_ID: if (event->group_final) - break; + return log_event_final_set(event, token); + if (token->op == OP_ASSIGN_FINAL) event->group_final = true; - if (!token->data) - break; - event->gid = PTR_TO_GID(token->data); - log_event_debug(dev, token, "GROUP %u", event->gid); - break; + + event->gid = PTR_TO_GID(ASSERT_PTR(token->data)); + log_event_debug(event, token, "Set group ID: %u", event->gid); + return true; + case TK_A_MODE_ID: if (event->mode_final) - break; + return log_event_final_set(event, token); + if (token->op == OP_ASSIGN_FINAL) event->mode_final = true; - if (!token->data) - break; - event->mode = PTR_TO_MODE(token->data); - log_event_debug(dev, token, "MODE %#o", event->mode); - break; + + event->mode = PTR_TO_MODE(ASSERT_PTR(token->data)); + log_event_debug(event, token, "Set mode: %#o", event->mode); + return true; + case TK_A_SECLABEL: { _cleanup_free_ char *name = NULL, *label = NULL; char label_str[UDEV_LINE_SIZE] = {}; - bool truncated; name = strdup(token->data); if (!name) return log_oom(); - (void) udev_event_apply_format(event, token->value, label_str, sizeof(label_str), false, &truncated); - if (truncated) { - log_event_truncated(dev, token, "security label", token->value, "SECLABEL", /* is_match = */ false); - break; - } + if (!apply_format_value(event, token, label_str, sizeof(label_str), "security label")) + return true; if (!isempty(label_str)) label = strdup(label_str); @@ -2539,157 +2721,135 @@ static int udev_rule_apply_token_to_event( if (r == -ENOMEM) return log_oom(); if (r < 0) - return log_event_error_errno(dev, token, r, "Failed to store SECLABEL{%s}='%s': %m", name, label); + return log_event_error_errno(event, token, r, "Failed to store security label \"%s=%s\": %m", name, label); - log_event_debug(dev, token, "SECLABEL{%s}='%s'", name, label); + log_event_debug(event, token, "Set security label: %s=%s", name, label); TAKE_PTR(name); TAKE_PTR(label); - break; + return true; } case TK_A_ENV: { const char *val, *name = token->data; char value_new[UDEV_NAME_SIZE], *p = value_new; - size_t count, l = sizeof(value_new); - bool truncated; + size_t l = sizeof(value_new); if (isempty(token->value)) { if (token->op == OP_ADD) - break; + return log_event_done(event, token); + r = device_add_property(dev, name, NULL); if (r < 0) - return log_event_error_errno(dev, token, r, "Failed to remove property '%s': %m", name); - break; + return log_event_error_errno(event, token, r, "Failed to remove property \"%s\": %m", name); + log_event_trace(event, token, "Removed property \"%s\".", name); + return true; } if (token->op == OP_ADD && device_get_property_value_with_fallback(dev, name, event->worker ? event->worker->properties : NULL, &val) >= 0) { + bool truncated; l = strpcpyl_full(&p, l, &truncated, val, " ", NULL); if (truncated) { - log_event_warning(dev, token, - "The buffer for the property '%s' is full, " - "refusing to append the new value '%s'.", name, token->value); - break; + log_event_warning(event, token, + "The buffer for the property is full, refusing to append new property \"%s=%s\".", + name, token->value); + return true; } } - (void) udev_event_apply_format(event, token->value, p, l, false, &truncated); - if (truncated) { - _cleanup_free_ char *key_with_name = strjoin("ENV{", name, "}"); - log_event_truncated(dev, token, "property value", token->value, - key_with_name ?: "ENV", /* is_match = */ false); - break; - } + if (!apply_format_value(event, token, p, l, "property value")) + return true; - if (event->esc == ESCAPE_REPLACE) { - count = udev_replace_chars(p, NULL); - if (count > 0) - log_event_debug(dev, token, - "Replaced %zu slash(es) from result of ENV{%s}%s=\"%s\"", - count, name, token->op == OP_ADD ? "+" : "", token->value); - } + if (event->esc == ESCAPE_REPLACE) + udev_replace_chars_and_log(event, token, p, /* allow = */ NULL, "property value"); r = device_add_property(dev, name, value_new); if (r < 0) - return log_event_error_errno(dev, token, r, "Failed to add property '%s=%s': %m", name, value_new); - break; + return log_event_error_errno(event, token, r, "Failed to set property \"%s=%s\": %m", name, value_new); + log_event_trace(event, token, "Set property \"%s=%s\".", name, value_new); + return true; } case TK_A_TAG: { char buf[UDEV_PATH_SIZE]; - bool truncated; - (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false, &truncated); - if (truncated) { - log_event_truncated(dev, token, "tag name", token->value, "TAG", /* is_match = */ false); - break; + if (!apply_format_value(event, token, buf, sizeof(buf), "tag name")) + return true; + + if (token->op == OP_REMOVE) { + device_remove_tag(dev, buf); + log_event_trace(event, token, "Removed tag \"%s\".", buf); + return true; } + assert(IN_SET(token->op, OP_ASSIGN, OP_ADD)); + if (token->op == OP_ASSIGN) device_cleanup_tags(dev); - if (token->op == OP_REMOVE) - device_remove_tag(dev, buf); - else { - r = device_add_tag(dev, buf, true); - if (r == -ENOMEM) - return log_oom(); - if (r < 0) - log_event_warning_errno(dev, token, r, "Failed to add tag '%s', ignoring: %m", buf); - } - break; + r = device_add_tag(dev, buf, /* both = */ true); + if (r == -ENOMEM) + return log_oom(); + if (r < 0) + log_event_warning_errno(event, token, r, "Failed to %s tag \"%s\", ignoring: %m", + token->op == OP_ASSIGN ? "set" : "add", buf); + else + log_event_trace(event, token, "%s tag \"%s\".", token->op == OP_ASSIGN ? "Set" : "Added", buf); + return true; } case TK_A_NAME: { char buf[UDEV_PATH_SIZE]; - bool truncated; - size_t count; if (event->name_final) - break; + return log_event_final_set(event, token); + if (token->op == OP_ASSIGN_FINAL) event->name_final = true; if (sd_device_get_ifindex(dev, NULL) < 0) { - log_event_error(dev, token, - "Only network interfaces can be renamed, ignoring NAME=\"%s\".", - token->value); - break; + log_event_warning(event, token, "Only network interfaces can be renamed, ignoring."); + return true; } - (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false, &truncated); - if (truncated) { - log_event_truncated(dev, token, "network interface name", token->value, "NAME", /* is_match = */ false); - break; - } + if (!apply_format_value(event, token, buf, sizeof(buf), "network interface name")) + return true; + + if (IN_SET(event->esc, ESCAPE_UNSET, ESCAPE_REPLACE)) + udev_replace_ifname(event, token, buf); - if (IN_SET(event->esc, ESCAPE_UNSET, ESCAPE_REPLACE)) { - if (naming_scheme_has(NAMING_REPLACE_STRICTLY)) - count = udev_replace_ifname(buf); - else - count = udev_replace_chars(buf, "/"); - if (count > 0) - log_event_debug(dev, token, - "Replaced %zu character(s) from result of NAME=\"%s\"", - count, token->value); - } r = free_and_strdup_warn(&event->name, buf); if (r < 0) return r; - log_event_debug(dev, token, "NAME '%s'", event->name); - break; + log_event_debug(event, token, "Set network interface name: %s", event->name); + return true; } case TK_A_DEVLINK: { char buf[UDEV_PATH_SIZE]; - bool truncated; - size_t count; if (event->devlink_final) - break; - if (sd_device_get_devnum(dev, NULL) < 0) - break; + return log_event_final_set(event, token); + + if (sd_device_get_devnum(dev, NULL) < 0) { + log_event_debug(event, token, "Device does not have device node, ignoring to manage device node symlink."); + return true; + } + if (token->op == OP_ASSIGN_FINAL) event->devlink_final = true; + if (IN_SET(token->op, OP_ASSIGN, OP_ASSIGN_FINAL)) device_cleanup_devlinks(dev); - (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), - /* replace_whitespace = */ event->esc != ESCAPE_NONE, &truncated); - if (truncated) { - log_event_truncated(dev, token, "symbolic link path", token->value, "SYMLINK", /* is_match = */ false); - break; - } + if (!apply_format_full(event, token, token->value, buf, sizeof(buf), + /* replace_whitespace = */ event->esc != ESCAPE_NONE, + "symbolic link path")) + return true; /* By default or string_escape=none, allow multiple symlinks separated by spaces. */ if (event->esc == ESCAPE_UNSET) - count = udev_replace_chars(buf, /* allow = */ "/ "); + udev_replace_chars_and_log(event, token, buf, /* allow = */ "/ ", "device node symlink"); else if (event->esc == ESCAPE_REPLACE) - count = udev_replace_chars(buf, /* allow = */ "/"); - else - count = 0; - if (count > 0) - log_event_debug(dev, token, - "Replaced %zu character(s) from result of SYMLINK=\"%s\"", - count, token->value); + udev_replace_chars_and_log(event, token, buf, /* allow = */ "/", "device node symlink"); for (const char *p = buf;;) { _cleanup_free_ char *path = NULL; @@ -2698,166 +2858,164 @@ static int udev_rule_apply_token_to_event( if (r == -ENOMEM) return log_oom(); if (r < 0) { - log_warning_errno(r, "Failed to extract first path in SYMLINK=, ignoring: %m"); - break; + log_warning_errno(r, "Failed to extract first path in device node symlinks, ignoring: %m"); + return true; } if (r == 0) - break; + return true; if (token->op == OP_REMOVE) { r = device_remove_devlink(dev, path); if (r == -ENOMEM) return log_oom(); if (r < 0) - log_event_warning_errno(dev, token, r, "Failed to remove devlink '%s', ignoring: %m", path); + log_event_warning_errno(event, token, r, "Failed to remove device node symlink \"%s\", ignoring: %m", path); else if (r > 0) - log_event_debug(dev, token, "Dropped SYMLINK '%s'", path); + log_event_debug(event, token, "Removed device node symlink \"%s\"", path); } else { r = device_add_devlink(dev, path); if (r == -ENOMEM) return log_oom(); if (r < 0) - log_event_warning_errno(dev, token, r, "Failed to add devlink '%s', ignoring: %m", path); + log_event_warning_errno(event, token, r, "Failed to add device node symlink \"%s\", ignoring: %m", path); else if (r > 0) - log_event_debug(dev, token, "Added SYMLINK '%s'", path); + log_event_debug(event, token, "Added device node symlink \"%s\".", path); } } - break; + assert_not_reached(); } case TK_A_ATTR: { char buf[UDEV_PATH_SIZE], value[UDEV_NAME_SIZE]; const char *val, *key_name = token->data; - bool truncated; if (udev_resolve_subsys_kernel(key_name, buf, sizeof(buf), false) < 0 && sd_device_get_syspath(dev, &val) >= 0) { + bool truncated; strscpyl_full(buf, sizeof(buf), &truncated, val, "/", key_name, NULL); if (truncated) { - log_event_warning(dev, token, - "The path to the attribute '%s/%s' is too long, refusing to set the attribute.", + log_event_warning(event, token, + "The path to the attribute \"%s/%s\" is too long, refusing to set the attribute.", val, key_name); - break; + return true; } } r = attr_subst_subdir(buf); if (r < 0) { - log_event_error_errno(dev, token, r, "Could not find file matches '%s', ignoring: %m", buf); - break; - } - (void) udev_event_apply_format(event, token->value, value, sizeof(value), false, &truncated); - if (truncated) { - log_event_truncated(dev, token, "attribute value", token->value, "ATTR", /* is_match = */ false); - break; + log_event_error_errno(event, token, r, "Could not find file matches \"%s\", ignoring: %m", buf); + return true; } + if (!apply_format_value(event, token, value, sizeof(value), "attribute value")) + return true; + if (EVENT_MODE_DESTRUCTIVE(event)) { - log_event_debug(dev, token, "Writing ATTR{'%s'}=\"%s\".", buf, value); + log_event_debug(event, token, "Writing \"%s\" to sysfs attribute \"%s\".", value, buf); r = write_string_file(buf, value, WRITE_STRING_FILE_VERIFY_ON_FAILURE | WRITE_STRING_FILE_DISABLE_BUFFER | WRITE_STRING_FILE_AVOID_NEWLINE | WRITE_STRING_FILE_VERIFY_IGNORE_NEWLINE); if (r < 0) - log_event_error_errno(dev, token, r, "Failed to write ATTR{%s}=\"%s\", ignoring: %m", buf, value); + log_event_error_errno(event, token, r, "Failed to write \"%s\" to sysfs attribute \"%s\", ignoring: %m", value, buf); + else + log_event_done(event, token); } else - log_event_debug(dev, token, "Running in test mode, skipping writing ATTR{%s}=\"%s\".", buf, value); + log_event_debug(event, token, "Running in test mode, skipping writing \"%s\" to sysfs attribute \"%s\".", value, buf); - break; + return true; } case TK_A_SYSCTL: { char buf[UDEV_PATH_SIZE], value[UDEV_NAME_SIZE]; - bool truncated; - (void) udev_event_apply_format(event, token->data, buf, sizeof(buf), false, &truncated); - if (truncated) { - log_event_truncated(dev, token, "sysctl entry name", token->data, "SYSCTL", /* is_match = */ false); - break; - } + if (!apply_format_attr(event, token, buf, sizeof(buf), "sysctl entry name")) + return true; - (void) udev_event_apply_format(event, token->value, value, sizeof(value), false, &truncated); - if (truncated) { - _cleanup_free_ char *key_with_name = strjoin("SYSCTL{", buf, "}"); - log_event_truncated(dev, token, "sysctl value", token->value, - key_with_name ?: "SYSCTL", /* is_match = */ false); - break; - } + if (!apply_format_value(event, token, value, sizeof(value), "sysctl value")) + return true; sysctl_normalize(buf); if (EVENT_MODE_DESTRUCTIVE(event)) { - log_event_debug(dev, token, "Writing SYSCTL{%s}=\"%s\".", buf, value); + log_event_debug(event, token, "Writing \"%s\" to sysctl entry \"%s\".", value, buf); r = sysctl_write(buf, value); if (r < 0) - log_event_error_errno(dev, token, r, "Failed to write SYSCTL{%s}=\"%s\", ignoring: %m", buf, value); + log_event_error_errno(event, token, r, "Failed to write \"%s\" to sysctl entry \"%s\", ignoring: %m", value, buf); + else + log_event_done(event, token); } else - log_event_debug(dev, token, "Running in test mode, skipping writing SYSCTL{%s}=\"%s\".", buf, value); + log_event_debug(event, token, "Running in test mode, skipping writing \"%s\" to sysctl entry \"%s\".", value, buf); - break; + return true; } case TK_A_RUN_BUILTIN: case TK_A_RUN_PROGRAM: { _cleanup_free_ char *cmd = NULL; char buf[UDEV_LINE_SIZE]; - bool truncated; if (event->run_final) - break; + return log_event_final_set(event, token); + if (token->op == OP_ASSIGN_FINAL) event->run_final = true; if (IN_SET(token->op, OP_ASSIGN, OP_ASSIGN_FINAL)) ordered_hashmap_clear_free_key(event->run_list); - (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false, &truncated); - if (truncated) { - log_event_truncated(dev, token, "command", token->value, - token->type == TK_A_RUN_BUILTIN ? "RUN{builtin}" : "RUN{program}", - /* is_match = */ false); - break; - } + if (!apply_format_value(event, token, buf, sizeof(buf), "command")) + return true; cmd = strdup(buf); if (!cmd) return log_oom(); r = ordered_hashmap_ensure_put(&event->run_list, NULL, cmd, token->data); - if (r == -ENOMEM) - return log_oom(); if (r < 0) - return log_event_error_errno(dev, token, r, "Failed to store command '%s': %m", cmd); + return log_event_error_errno(event, token, r, "Failed to store command \"%s\": %m", cmd); + log_event_debug(event, token, "Set command: %s", cmd); TAKE_PTR(cmd); - - log_event_debug(dev, token, "RUN '%s'", token->value); - break; + return true; } case TK_A_OPTIONS_STATIC_NODE: /* do nothing for events. */ - break; + return true; + default: assert_not_reached(); } - return true; -} - -static bool token_is_for_parents(UdevRuleToken *token) { - return token->type >= TK_M_PARENTS_KERNEL && token->type <= TK_M_PARENTS_TAG; + assert_not_reached(); } static int udev_rule_apply_parent_token_to_event(UdevRuleToken *head_token, UdevEvent *event) { int r; assert(head_token); + assert(token_is_for_parents(head_token)); assert(event); + UdevRuleLine *line = head_token->rule_line; + if (event->trace) { + _cleanup_free_ char *joined = NULL; + + LIST_FOREACH(tokens, token, head_token) + if (token_is_for_parents(token)) + (void) strextend_with_separator(&joined, ", ", token->token_str); + else + break; + + log_event_line(event, line, "Checking conditions for parent devices: %s", strna(joined)); + } + event->dev_parent = ASSERT_PTR(event->dev); for (;;) { LIST_FOREACH(tokens, token, head_token) { - if (!token_is_for_parents(token)) - return true; /* All parent tokens match. */ + if (!token_is_for_parents(token)) { + r = 1; /* avoid false maybe-uninitialized warning */ + break; /* All parent tokens match. */ + } r = udev_rule_apply_token_to_event(token, event->dev_parent, event); if (r < 0) @@ -2865,12 +3023,18 @@ static int udev_rule_apply_parent_token_to_event(UdevRuleToken *head_token, Udev if (r == 0) break; } - if (r > 0) - /* All parent tokens match, and no more token (except for GOTO) in the line. */ + if (r > 0) { + if (event->trace) { + const char *s = NULL; + (void) sd_device_get_syspath(event->dev_parent, &s); + log_event_line(event, line, "Parent device \"%s\" passed all parent conditions.", strna(s)); + } return true; + } if (sd_device_get_parent(event->dev_parent, &event->dev_parent) < 0) { event->dev_parent = NULL; + log_event_line(event, line, "No parent device passed parent conditions."); return false; } } @@ -2927,8 +3091,10 @@ static int udev_rule_apply_line_to_event( return r; } - if (line->goto_line) + if (line->goto_line) { + log_event_line(event, line, "GOTO=%s", strna(line->goto_label)); *next_line = line->goto_line; /* update next_line only when the line has GOTO token. */ + } return 0; } diff --git a/src/udev/udevadm-test.c b/src/udev/udevadm-test.c index 5db991b8a6..088522b915 100644 --- a/src/udev/udevadm-test.c +++ b/src/udev/udevadm-test.c @@ -34,6 +34,7 @@ static sd_device_action_t arg_action = SD_DEVICE_ADD; static ResolveNameTiming arg_resolve_name_timing = RESOLVE_NAME_EARLY; static const char *arg_syspath = NULL; +static bool arg_verbose = false; static int help(void) { @@ -42,7 +43,8 @@ static int help(void) { " -h --help Show this help\n" " -V --version Show package version\n" " -a --action=ACTION|help Set action string\n" - " -N --resolve-names=early|late|never When to resolve names\n", + " -N --resolve-names=early|late|never When to resolve names\n" + " -v --verbose Show verbose logs\n", program_invocation_short_name); return 0; @@ -52,6 +54,7 @@ static int parse_argv(int argc, char *argv[]) { static const struct option options[] = { { "action", required_argument, NULL, 'a' }, { "resolve-names", required_argument, NULL, 'N' }, + { "verbose", no_argument, NULL, 'v' }, { "version", no_argument, NULL, 'V' }, { "help", no_argument, NULL, 'h' }, {} @@ -59,7 +62,7 @@ static int parse_argv(int argc, char *argv[]) { int r, c; - while ((c = getopt_long(argc, argv, "a:N:Vh", options, NULL)) >= 0) + while ((c = getopt_long(argc, argv, "a:N:vVh", options, NULL)) >= 0) switch (c) { case 'a': r = parse_device_action(optarg, &arg_action); @@ -74,6 +77,9 @@ static int parse_argv(int argc, char *argv[]) { return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--resolve-names= must be early, late or never"); break; + case 'v': + arg_verbose = true; + break; case 'V': return print_version(); case 'h': @@ -105,20 +111,23 @@ int test_main(int argc, char *argv[], void *userdata) { if (r <= 0) return r; - printf("This program is for debugging only, it does not run any program\n" - "specified by a RUN key. It may show incorrect results, because\n" - "some values may be different, or not available at a simulation run.\n" - "\n"); + puts("This program is for debugging only, it does not run any program\n" + "specified by a RUN key. It may show incorrect results, because\n" + "some values may be different, or not available at a simulation run."); assert_se(sigprocmask(SIG_SETMASK, NULL, &sigmask_orig) >= 0); + puts("\nLoading builtins..."); udev_builtin_init(); + puts("Loading builtins done."); + puts("\nLoading udev rules files..."); r = udev_rules_load(&rules, arg_resolve_name_timing); if (r < 0) { log_error_errno(r, "Failed to read udev rules: %m"); goto out; } + puts("Loading udev rules files done."); r = find_device_with_action(arg_syspath, arg_action, &dev); if (r < 0) { @@ -134,12 +143,16 @@ int test_main(int argc, char *argv[], void *userdata) { log_oom(); goto out; } + event->trace = arg_verbose; assert_se(sigfillset(&mask) >= 0); assert_se(sigprocmask(SIG_SETMASK, &mask, &sigmask_orig) >= 0); + printf("\nProcessing udev rules%s...\n", arg_verbose ? "" : " (verbose logs can be shown by -v/--verbose)"); udev_event_execute_rules(event, rules); + puts("Processing udev rules done."); + puts(""); printf("%sProperties:%s\n", ansi_highlight(), ansi_normal()); FOREACH_DEVICE_PROPERTY(dev, key, value) printf(" %s=%s\n", key, value); diff --git a/test/units/TEST-17-UDEV.10.sh b/test/units/TEST-17-UDEV.10.sh index e38291bdde..5ee1cd3e42 100755 --- a/test/units/TEST-17-UDEV.10.sh +++ b/test/units/TEST-17-UDEV.10.sh @@ -141,6 +141,7 @@ udevadm test -N early /sys/class/net/$netdev udevadm test -N late /sys/class/net/$netdev udevadm test --resolve-names never /sys/class/net/$netdev (! udevadm test -N hello /sys/class/net/$netdev) +udevadm test -v /sys/class/net/$netdev udevadm test -h # udevadm test-builtin path_id "$loopdev" |