udev-rules: check tokens order
authorDmitry V. Levin <ldv@strace.io>
Sun, 26 Mar 2023 08:00:00 +0000 (08:00 +0000)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 3 Apr 2023 14:44:26 +0000 (23:44 +0900)
When invoked by udevadm verify, warn about rules that have PROGRAM
assignments specified after RESULT checks.

src/udev/udev-rules.c
test/units/testsuite-17.11.sh

index bbdd498cd718f5fcf3e0e928b95e548d02907733..19bbf295c51acded9b10e045d23caf561723b742 100644 (file)
@@ -1168,6 +1168,20 @@ static int parse_line(char **line, char **ret_key, char **ret_attr, UdevRuleOper
         return 1;
 }
 
+static void check_tokens_order(UdevRuleLine *rule_line) {
+        bool has_result = false;
+
+        assert(rule_line);
+
+        LIST_FOREACH(tokens, t, rule_line->tokens)
+                if (t->type == TK_M_RESULT)
+                        has_result = true;
+                else if (has_result && t->type == TK_M_PROGRAM) {
+                        log_line_warning(rule_line, "Reordering RESULT check after PROGRAM assignment.");
+                        break;
+                }
+}
+
 static void sort_tokens(UdevRuleLine *rule_line) {
         assert(rule_line);
 
@@ -1236,6 +1250,9 @@ static int rule_add_line(UdevRuleFile *rule_file, const char *line_str, unsigned
                 return 0;
         }
 
+        if (extra_checks)
+                check_tokens_order(rule_line);
+
         sort_tokens(rule_line);
         TAKE_PTR(rule_line);
         return 0;
index e3a96e5cdb7e734cea1847bff63e84fad0625802..ce6f58077f2be73d738c8532299f0040f729d571 100755 (executable)
@@ -300,6 +300,9 @@ test_syntax_error 'ACTION=="a" NAME="b"' 'A comma between tokens is expected.'
 test_syntax_error 'ACTION=="a",, NAME="b"' 'More than one comma between tokens.'
 test_syntax_error 'ACTION=="a" , NAME="b"' 'Stray whitespace before comma.'
 test_syntax_error 'ACTION=="a",NAME="b"' 'Whitespace after comma is expected.'
+test_syntax_error 'RESULT=="a", PROGRAM="b"' 'Reordering RESULT check after PROGRAM assignment.'
+test_syntax_error 'RESULT=="a*", PROGRAM="b", RESULT=="*c", PROGRAM="d"' \
+        'Reordering RESULT check after PROGRAM assignment.'
 
 cat >"${rules}" <<'EOF'
 KERNEL=="a|b", KERNEL=="a|c", NAME="d"
@@ -308,6 +311,7 @@ KERNEL!="a", KERNEL!="b", NAME="c"
 KERNEL=="|a", KERNEL=="|b", NAME="c"
 KERNEL=="*", KERNEL=="a*", NAME="b"
 KERNEL=="a*", KERNEL=="c*|ab*", NAME="d"
+PROGRAM="a", RESULT=="b"
 EOF
 assert_0 "${rules}"