ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
661545
From be88b819ecec075187ac313b8ab15d8ed8004b54 Mon Sep 17 00:00:00 2001
661545
From: Jan Synacek <jsynacek@redhat.com>
661545
Date: Wed, 11 Sep 2019 12:35:18 +0200
661545
Subject: [PATCH] udev: introduce CONST key name
661545
661545
Currently, there is no way to match against system-wide constants, such
661545
as architecture or virtualization type, without forking helper binaries.
661545
That potentially results in a huge number of spawned processes which
661545
output always the same answer.
661545
661545
This patch introduces a special CONST keyword which takes a hard-coded
661545
string as its key and returns a value assigned to that key. Currently
661545
implemented are CONST{arch} and CONST{virt}, which can be used to match
661545
against the system's architecture and virtualization type.
661545
661545
(based on commit 4801d8afe2ff1c1c075c9f0bc5631612172e0bb7)
661545
661545
Resolves: #1748051
661545
---
661545
 man/udev.xml              | 26 ++++++++++++++++++++++++++
661545
 rules/40-redhat.rules     |  4 ++--
661545
 src/udev/udev-rules.c     | 39 +++++++++++++++++++++++++++++++++++++++
661545
 test/rule-syntax-check.py |  2 +-
661545
 4 files changed, 68 insertions(+), 3 deletions(-)
661545
661545
diff --git a/man/udev.xml b/man/udev.xml
661545
index a948ea79a9..fc02edd214 100644
661545
--- a/man/udev.xml
661545
+++ b/man/udev.xml
661545
@@ -240,6 +240,32 @@
661545
           </listitem>
661545
         </varlistentry>
661545
 
661545
+        <varlistentry>
661545
+          <term><varname>CONST{<replaceable>key</replaceable>}</varname></term>
661545
+          <listitem>
661545
+            <para>Match against a system-wide constant. Supported keys are:</para>
661545
+            <variablelist>
661545
+              <varlistentry>
661545
+                <term><literal>arch</literal></term>
661545
+                <listitem>
661545
+                  <para>System's architecture. See <option>ConditionArchitecture=</option> in
661545
+                  <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>
661545
+                  for possible values.</para>
661545
+                </listitem>
661545
+              </varlistentry>
661545
+              <varlistentry>
661545
+                <term><literal>virt</literal></term>
661545
+                <listitem>
661545
+                  <para>System's virtualization environment. See
661545
+                  <citerefentry><refentrytitle>systemd-detect-virt</refentrytitle><manvolnum>1</manvolnum></citerefentry>
661545
+                  for possible values.</para>
661545
+                </listitem>
661545
+              </varlistentry>
661545
+            </variablelist>
661545
+            <para>Unknown keys will never match.</para>
661545
+          </listitem>
661545
+        </varlistentry>
661545
+
661545
         <varlistentry>
661545
           <term><varname>TAG</varname></term>
661545
           <listitem>
661545
diff --git a/rules/40-redhat.rules b/rules/40-redhat.rules
661545
index 2c690e522e..83d823e172 100644
661545
--- a/rules/40-redhat.rules
661545
+++ b/rules/40-redhat.rules
661545
@@ -6,10 +6,10 @@ SUBSYSTEM=="cpu", ACTION=="add", TEST=="online", ATTR{online}=="0", ATTR{online}
661545
 # Memory hotadd request
661545
 SUBSYSTEM!="memory", GOTO="memory_hotplug_end"
661545
 ACTION!="add", GOTO="memory_hotplug_end"
661545
-PROGRAM="/bin/uname -p", RESULT=="s390*", GOTO="memory_hotplug_end"
661545
+CONST{arch}=="s390*", GOTO="memory_hotplug_end"
661545
 
661545
 ENV{.state}="online"
661545
-PROGRAM="/bin/systemd-detect-virt", RESULT=="none", ENV{.state}="online_movable"
661545
+CONST{virt}=="none", ENV{.state}="online_movable"
661545
 ATTR{state}=="offline", ATTR{state}="$env{.state}"
661545
 
661545
 LABEL="memory_hotplug_end"
661545
diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c
661545
index c9a0197534..9211f563b1 100644
661545
--- a/src/udev/udev-rules.c
661545
+++ b/src/udev/udev-rules.c
661545
@@ -29,12 +29,14 @@
661545
 #include <fnmatch.h>
661545
 #include <time.h>
661545
 
661545
+#include "architecture.h"
661545
 #include "udev.h"
661545
 #include "path-util.h"
661545
 #include "conf-files.h"
661545
 #include "strbuf.h"
661545
 #include "strv.h"
661545
 #include "util.h"
661545
+#include "virt.h"
661545
 
661545
 #define PREALLOC_TOKEN          2048
661545
 
661545
@@ -123,6 +125,7 @@ enum token_type {
661545
         TK_M_DEVLINK,                   /* val */
661545
         TK_M_NAME,                      /* val */
661545
         TK_M_ENV,                       /* val, attr */
661545
+        TK_M_CONST,                     /* val, attr */
661545
         TK_M_TAG,                       /* val */
661545
         TK_M_SUBSYSTEM,                 /* val */
661545
         TK_M_DRIVER,                    /* val */
661545
@@ -257,6 +260,7 @@ static const char *token_str(enum token_type type) {
661545
                 [TK_M_DEVLINK] =                "M DEVLINK",
661545
                 [TK_M_NAME] =                   "M NAME",
661545
                 [TK_M_ENV] =                    "M ENV",
661545
+                [TK_M_CONST] =                  "M CONST",
661545
                 [TK_M_TAG] =                    "M TAG",
661545
                 [TK_M_SUBSYSTEM] =              "M SUBSYSTEM",
661545
                 [TK_M_DRIVER] =                 "M DRIVER",
661545
@@ -365,6 +369,7 @@ static void dump_token(struct udev_rules *rules, struct token *token) {
661545
         case TK_M_ATTR:
661545
         case TK_M_ATTRS:
661545
         case TK_M_ENV:
661545
+        case TK_M_CONST:
661545
         case TK_A_ATTR:
661545
         case TK_A_ENV:
661545
                 log_debug("%s %s '%s' '%s'(%s)",
661545
@@ -905,6 +910,7 @@ static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type,
661545
                 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
661545
                 break;
661545
         case TK_M_ENV:
661545
+        case TK_M_CONST:
661545
         case TK_M_ATTR:
661545
         case TK_M_ATTRS:
661545
         case TK_A_ATTR:
661545
@@ -1264,6 +1270,22 @@ static int add_rule(struct udev_rules *rules, char *line,
661545
                         continue;
661545
                 }
661545
 
661545
+                if (startswith(key, "CONST{")) {
661545
+                        attr = get_key_attribute(rules->udev, key + STRLEN("CONST"));
661545
+                        if (attr == NULL || !STR_IN_SET(attr, "arch", "virt")) {
661545
+                                log_error("error parsing CONST attribute");
661545
+                                goto invalid;
661545
+                        }
661545
+                        if (op == OP_REMOVE) {
661545
+                                log_error("invalid CONST operation");
661545
+                                goto invalid;
661545
+                        }
661545
+                        if (op < OP_MATCH_MAX) {
661545
+                                if (rule_add_key(&rule_tmp, TK_M_CONST, op, value, attr) != 0)
661545
+                                        goto invalid;
661545
+                        }
661545
+                        continue;
661545
+                }
661545
                 if (streq(key, "TAG")) {
661545
                         if (op < OP_MATCH_MAX)
661545
                                 rule_add_key(&rule_tmp, TK_M_TAG, op, value, NULL);
661545
@@ -1959,6 +1981,23 @@ int udev_rules_apply_to_event(struct udev_rules *rules,
661545
                                 goto nomatch;
661545
                         break;
661545
                 }
661545
+                case TK_M_CONST: {
661545
+                        const char *key_name = rules_str(rules, cur->key.attr_off);
661545
+                        const char *value = NULL;
661545
+                        int q;
661545
+
661545
+                        if (streq(key_name, "arch")) {
661545
+                                q = uname_architecture();
661545
+                                value = architecture_to_string(q);
661545
+                        } else if (streq(key_name, "virt")) {
661545
+                                q = detect_virtualization(&value);
661545
+                        } else
661545
+                                assert_not_reached("Invalid CONST key");
661545
+
661545
+                        if (match_key(rules, cur, value))
661545
+                                goto nomatch;
661545
+                        break;
661545
+                }
661545
                 case TK_M_TAG: {
661545
                         struct udev_list_entry *list_entry;
661545
                         bool match = false;
661545
diff --git a/test/rule-syntax-check.py b/test/rule-syntax-check.py
661545
index 80bbe65bea..c6d003b167 100644
661545
--- a/test/rule-syntax-check.py
661545
+++ b/test/rule-syntax-check.py
661545
@@ -34,7 +34,7 @@ else:
661545
     rules_files = glob(os.path.join(rules_dir, '*.rules'))
661545
 
661545
 no_args_tests = re.compile('(ACTION|DEVPATH|KERNELS?|NAME|SYMLINK|SUBSYSTEMS?|DRIVERS?|TAG|RESULT|TEST)\s*(?:=|!)=\s*"([^"]*)"$')
661545
-args_tests = re.compile('(ATTRS?|ENV|TEST){([a-zA-Z0-9/_.*%-]+)}\s*(?:=|!)=\s*"([^"]*)"$')
661545
+args_tests = re.compile('(ATTRS?|ENV|CONST|TEST){([a-zA-Z0-9/_.*%-]+)}\s*(?:=|!)=\s*"([^"]*)"$')
661545
 no_args_assign = re.compile('(NAME|SYMLINK|OWNER|GROUP|MODE|TAG|PROGRAM|RUN|LABEL|GOTO|WAIT_FOR|OPTIONS|IMPORT)\s*(?:\+=|:=|=)\s*"([^"]*)"$')
661545
 args_assign = re.compile('(ATTR|ENV|IMPORT|RUN){([a-zA-Z0-9/_.*%-]+)}\s*(=|\+=)\s*"([^"]*)"$')
661545