valeriyvdovin / rpms / systemd

Forked from rpms/systemd 4 years ago
Clone
4fbe94
From d6210c3d053d70175d72ed1d1719497eed76000b Mon Sep 17 00:00:00 2001
4fbe94
From: Jan Synacek <jsynacek@redhat.com>
4fbe94
Date: Thu, 17 Oct 2019 09:37:35 +0200
4fbe94
Subject: [PATCH] udev: introduce CONST key name
4fbe94
4fbe94
Currently, there is no way to match against system-wide constants, such
4fbe94
as architecture or virtualization type, without forking helper binaries.
4fbe94
That potentially results in a huge number of spawned processes which
4fbe94
output always the same answer.
4fbe94
4fbe94
This patch introduces a special CONST keyword which takes a hard-coded
4fbe94
string as its key and returns a value assigned to that key. Currently
4fbe94
implemented are CONST{arch} and CONST{virt}, which can be used to match
4fbe94
against the system's architecture and virtualization type.
4fbe94
4fbe94
(based on commit 4801d8afe2ff1c1c075c9f0bc5631612172e0bb7)
4fbe94
4fbe94
Resolves: #1762679
4fbe94
---
4fbe94
 man/udev.xml              | 26 ++++++++++++++++++++++++++
4fbe94
 rules/40-redhat.rules     |  6 +++---
4fbe94
 src/udev/udev-rules.c     | 32 ++++++++++++++++++++++++++++++++
4fbe94
 test/rule-syntax-check.py |  2 +-
4fbe94
 4 files changed, 62 insertions(+), 4 deletions(-)
4fbe94
4fbe94
diff --git a/man/udev.xml b/man/udev.xml
4fbe94
index bdf901a8f0..8c1eb41787 100644
4fbe94
--- a/man/udev.xml
4fbe94
+++ b/man/udev.xml
4fbe94
@@ -236,6 +236,32 @@
4fbe94
           </listitem>
4fbe94
         </varlistentry>
4fbe94
 
4fbe94
+        <varlistentry>
4fbe94
+          <term><varname>CONST{<replaceable>key</replaceable>}</varname></term>
4fbe94
+          <listitem>
4fbe94
+            <para>Match against a system-wide constant. Supported keys are:</para>
4fbe94
+            <variablelist>
4fbe94
+              <varlistentry>
4fbe94
+                <term><literal>arch</literal></term>
4fbe94
+                <listitem>
4fbe94
+                  <para>System's architecture. See <option>ConditionArchitecture=</option> in
4fbe94
+                  <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>
4fbe94
+                  for possible values.</para>
4fbe94
+                </listitem>
4fbe94
+              </varlistentry>
4fbe94
+              <varlistentry>
4fbe94
+                <term><literal>virt</literal></term>
4fbe94
+                <listitem>
4fbe94
+                  <para>System's virtualization environment. See
4fbe94
+                  <citerefentry><refentrytitle>systemd-detect-virt</refentrytitle><manvolnum>1</manvolnum></citerefentry>
4fbe94
+                  for possible values.</para>
4fbe94
+                </listitem>
4fbe94
+              </varlistentry>
4fbe94
+            </variablelist>
4fbe94
+            <para>Unknown keys will never match.</para>
4fbe94
+          </listitem>
4fbe94
+        </varlistentry>
4fbe94
+
4fbe94
         <varlistentry>
4fbe94
           <term><varname>TAG</varname></term>
4fbe94
           <listitem>
4fbe94
diff --git a/rules/40-redhat.rules b/rules/40-redhat.rules
4fbe94
index fadc6e59f1..3c95cd2df0 100644
4fbe94
--- a/rules/40-redhat.rules
4fbe94
+++ b/rules/40-redhat.rules
4fbe94
@@ -6,11 +6,11 @@ SUBSYSTEM=="cpu", ACTION=="add", TEST=="online", ATTR{online}=="0", ATTR{online}
4fbe94
 # Memory hotadd request
4fbe94
 SUBSYSTEM!="memory", GOTO="memory_hotplug_end"
4fbe94
 ACTION!="add", GOTO="memory_hotplug_end"
4fbe94
-PROGRAM="/bin/uname -p", RESULT=="s390*", GOTO="memory_hotplug_end"
4fbe94
-PROGRAM="/bin/uname -p", RESULT=="ppc64*", GOTO="memory_hotplug_end"
4fbe94
+CONST{arch}=="s390*", GOTO="memory_hotplug_end"
4fbe94
+CONST{arch}=="ppc64*", GOTO="memory_hotplug_end"
4fbe94
 
4fbe94
 ENV{.state}="online"
4fbe94
-PROGRAM="/bin/systemd-detect-virt", RESULT=="none", ENV{.state}="online_movable"
4fbe94
+CONST{virt}=="none", ENV{.state}="online_movable"
4fbe94
 ATTR{state}=="offline", ATTR{state}="$env{.state}"
4fbe94
 
4fbe94
 LABEL="memory_hotplug_end"
4fbe94
diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c
4fbe94
index 58af863f3d..a246cbe67e 100644
4fbe94
--- a/src/udev/udev-rules.c
4fbe94
+++ b/src/udev/udev-rules.c
4fbe94
@@ -17,6 +17,7 @@
4fbe94
 #include <unistd.h>
4fbe94
 
4fbe94
 #include "alloc-util.h"
4fbe94
+#include "architecture.h"
4fbe94
 #include "conf-files.h"
4fbe94
 #include "dirent-util.h"
4fbe94
 #include "escape.h"
4fbe94
@@ -34,6 +35,7 @@
4fbe94
 #include "udev.h"
4fbe94
 #include "user-util.h"
4fbe94
 #include "util.h"
4fbe94
+#include "virt.h"
4fbe94
 
4fbe94
 #define PREALLOC_TOKEN          2048
4fbe94
 
4fbe94
@@ -123,6 +125,7 @@ enum token_type {
4fbe94
         TK_M_DEVLINK,                   /* val */
4fbe94
         TK_M_NAME,                      /* val */
4fbe94
         TK_M_ENV,                       /* val, attr */
4fbe94
+        TK_M_CONST,                     /* val, attr */
4fbe94
         TK_M_TAG,                       /* val */
4fbe94
         TK_M_SUBSYSTEM,                 /* val */
4fbe94
         TK_M_DRIVER,                    /* val */
4fbe94
@@ -259,6 +262,7 @@ static const char *token_str(enum token_type type) {
4fbe94
                 [TK_M_DEVLINK] =                "M DEVLINK",
4fbe94
                 [TK_M_NAME] =                   "M NAME",
4fbe94
                 [TK_M_ENV] =                    "M ENV",
4fbe94
+                [TK_M_CONST] =                  "M CONST",
4fbe94
                 [TK_M_TAG] =                    "M TAG",
4fbe94
                 [TK_M_SUBSYSTEM] =              "M SUBSYSTEM",
4fbe94
                 [TK_M_DRIVER] =                 "M DRIVER",
4fbe94
@@ -370,6 +374,7 @@ static void dump_token(struct udev_rules *rules, struct token *token) {
4fbe94
         case TK_M_SYSCTL:
4fbe94
         case TK_M_ATTRS:
4fbe94
         case TK_M_ENV:
4fbe94
+        case TK_M_CONST:
4fbe94
         case TK_A_ATTR:
4fbe94
         case TK_A_SYSCTL:
4fbe94
         case TK_A_ENV:
4fbe94
@@ -903,6 +908,7 @@ static void rule_add_key(struct rule_tmp *rule_tmp, enum token_type type,
4fbe94
                 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
4fbe94
                 break;
4fbe94
         case TK_M_ENV:
4fbe94
+        case TK_M_CONST:
4fbe94
         case TK_M_ATTR:
4fbe94
         case TK_M_SYSCTL:
4fbe94
         case TK_M_ATTRS:
4fbe94
@@ -1226,6 +1232,17 @@ static void add_rule(struct udev_rules *rules, char *line,
4fbe94
                                 rule_add_key(&rule_tmp, TK_A_ENV, op, value, attr);
4fbe94
                         }
4fbe94
 
4fbe94
+                } else if (startswith(key, "CONST{")) {
4fbe94
+                        attr = get_key_attribute(rules->udev, key + STRLEN("CONST"));
4fbe94
+                        if (attr == NULL || !STR_IN_SET(attr, "arch", "virt"))
4fbe94
+                                LOG_AND_RETURN("error parsing %s attribute", "CONST");
4fbe94
+
4fbe94
+                        if (op == OP_REMOVE)
4fbe94
+                                LOG_AND_RETURN("invalid %s operation", "CONST");
4fbe94
+
4fbe94
+                        if (op < OP_MATCH_MAX)
4fbe94
+                                rule_add_key(&rule_tmp, TK_M_CONST, op, value, attr);
4fbe94
+
4fbe94
                 } else if (streq(key, "TAG")) {
4fbe94
                         if (op < OP_MATCH_MAX)
4fbe94
                                 rule_add_key(&rule_tmp, TK_M_TAG, op, value, NULL);
4fbe94
@@ -1855,6 +1872,21 @@ void udev_rules_apply_to_event(struct udev_rules *rules,
4fbe94
                                 goto nomatch;
4fbe94
                         break;
4fbe94
                 }
4fbe94
+                case TK_M_CONST: {
4fbe94
+                        const char *key_name = rules_str(rules, cur->key.attr_off);
4fbe94
+                        const char *value = NULL;
4fbe94
+
4fbe94
+                        if (streq(key_name, "arch")) {
4fbe94
+                                value = architecture_to_string(uname_architecture());
4fbe94
+                        } else if (streq(key_name, "virt")) {
4fbe94
+                                value = virtualization_to_string(detect_virtualization());
4fbe94
+                        } else
4fbe94
+                                assert_not_reached("Invalid CONST key");
4fbe94
+
4fbe94
+                        if (match_key(rules, cur, value))
4fbe94
+                                goto nomatch;
4fbe94
+                        break;
4fbe94
+                }
4fbe94
                 case TK_M_TAG: {
4fbe94
                         struct udev_list_entry *list_entry;
4fbe94
                         bool match = false;
4fbe94
diff --git a/test/rule-syntax-check.py b/test/rule-syntax-check.py
4fbe94
index c7c0a1a656..6e59f421f5 100755
4fbe94
--- a/test/rule-syntax-check.py
4fbe94
+++ b/test/rule-syntax-check.py
4fbe94
@@ -19,7 +19,7 @@ quoted_string_re = r'"(?:[^\\"]|\\.)*"'
4fbe94
 no_args_tests = re.compile(r'(ACTION|DEVPATH|KERNELS?|NAME|SYMLINK|SUBSYSTEMS?|DRIVERS?|TAG|PROGRAM|RESULT|TEST)\s*(?:=|!)=\s*' + quoted_string_re + '$')
4fbe94
 # PROGRAM can also be specified as an assignment.
4fbe94
 program_assign = re.compile(r'PROGRAM\s*=\s*' + quoted_string_re + '$')
4fbe94
-args_tests = re.compile(r'(ATTRS?|ENV|TEST){([a-zA-Z0-9/_.*%-]+)}\s*(?:=|!)=\s*' + quoted_string_re + '$')
4fbe94
+args_tests = re.compile(r'(ATTRS?|ENV|CONST|TEST){([a-zA-Z0-9/_.*%-]+)}\s*(?:=|!)=\s*' + quoted_string_re + '$')
4fbe94
 no_args_assign = re.compile(r'(NAME|SYMLINK|OWNER|GROUP|MODE|TAG|RUN|LABEL|GOTO|WAIT_FOR|OPTIONS|IMPORT)\s*(?:\+=|:=|=)\s*' + quoted_string_re + '$')
4fbe94
 args_assign = re.compile(r'(ATTR|ENV|IMPORT|RUN){([a-zA-Z0-9/_.*%-]+)}\s*(=|\+=)\s*' + quoted_string_re + '$')
4fbe94
 # Find comma-separated groups, but allow commas that are inside quoted strings.