naccyde / rpms / systemd

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