daandemeyer / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
4295f9
From ba508dc60d5f62d8821242eebf50efcfbddd1428 Mon Sep 17 00:00:00 2001
4295f9
From: David Tardon <dtardon@redhat.com>
4295f9
Date: Tue, 10 Aug 2021 14:46:16 +0200
4295f9
Subject: [PATCH] Revert "udev: remove WAIT_FOR key"
4295f9
4295f9
This reverts commit f2b8052fb648b788936dd3e85be6a9aca90fbb2f.
4295f9
4295f9
RHEL-only
4295f9
4295f9
Resolves: #1982666
4295f9
---
4295f9
 man/udev.xml              |  9 +++++++
4295f9
 src/udev/udev-rules.c     | 56 +++++++++++++++++++++++++++++++++++++++
4295f9
 test/rule-syntax-check.py |  2 +-
4295f9
 3 files changed, 66 insertions(+), 1 deletion(-)
4295f9
4295f9
diff --git a/man/udev.xml b/man/udev.xml
4295f9
index f6ea2abc12..ce96e201e4 100644
4295f9
--- a/man/udev.xml
4295f9
+++ b/man/udev.xml
4295f9
@@ -592,6 +592,15 @@
4295f9
             </listitem>
4295f9
           </varlistentry>
4295f9
 
4295f9
+          <varlistentry>
4295f9
+            <term><varname>WAIT_FOR</varname></term>
4295f9
+            <listitem>
4295f9
+              <para>Wait for a file to become available or until a timeout of
4295f9
+              10 seconds expires. The path is relative to the sysfs device;
4295f9
+              if no path is specified, this waits for an attribute to appear.</para>
4295f9
+            </listitem>
4295f9
+          </varlistentry>
4295f9
+
4295f9
           <varlistentry>
4295f9
             <term><varname>OPTIONS</varname></term>
4295f9
             <listitem>
4295f9
diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c
4295f9
index bf997fc0ed..a02a7a1bc6 100644
4295f9
--- a/src/udev/udev-rules.c
4295f9
+++ b/src/udev/udev-rules.c
4295f9
@@ -78,6 +78,7 @@ typedef enum {
4295f9
         TK_M_TAG,                           /* strv, sd_device_get_tag_first(), sd_device_get_tag_next() */
4295f9
         TK_M_SUBSYSTEM,                     /* string, sd_device_get_subsystem() */
4295f9
         TK_M_DRIVER,                        /* string, sd_device_get_driver() */
4295f9
+        TK_M_WAITFOR,
4295f9
         TK_M_ATTR,                          /* string, takes filename through attribute, sd_device_get_sysattr_value(), udev_resolve_subsys_kernel(), etc. */
4295f9
         TK_M_SYSCTL,                        /* string, takes kernel parameter through attribute */
4295f9
 
4295f9
@@ -415,6 +416,47 @@ static void rule_line_append_token(UdevRuleLine *rule_line, UdevRuleToken *token
4295f9
         rule_line->current_token = token;
4295f9
 }
4295f9
 
4295f9
+#define WAIT_LOOP_PER_SECOND                50
4295f9
+static int wait_for_file(sd_device *dev, const char *file, int timeout) {
4295f9
+        char filepath[UDEV_PATH_SIZE];
4295f9
+        char devicepath[UDEV_PATH_SIZE];
4295f9
+        struct stat stats;
4295f9
+        int loop = timeout * WAIT_LOOP_PER_SECOND;
4295f9
+
4295f9
+        /* a relative path is a device attribute */
4295f9
+        devicepath[0] = '\0';
4295f9
+        if (file[0] != '/') {
4295f9
+                const char *val;
4295f9
+                int r;
4295f9
+
4295f9
+                r = sd_device_get_syspath(dev, &val;;
4295f9
+                if (r < 0)
4295f9
+                    return r;
4295f9
+                strscpyl(devicepath, sizeof(devicepath), val, NULL);
4295f9
+                strscpyl(filepath, sizeof(filepath), devicepath, "/", file, NULL);
4295f9
+                file = filepath;
4295f9
+        }
4295f9
+
4295f9
+        while (--loop) {
4295f9
+                const struct timespec duration = { 0, 1000 * 1000 * 1000 / WAIT_LOOP_PER_SECOND };
4295f9
+
4295f9
+                /* lookup file */
4295f9
+                if (stat(file, &stats) == 0) {
4295f9
+                        log_debug("file '%s' appeared after %i loops", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
4295f9
+                        return 0;
4295f9
+                }
4295f9
+                /* make sure, the device did not disappear in the meantime */
4295f9
+                if (devicepath[0] != '\0' && stat(devicepath, &stats) != 0) {
4295f9
+                        log_debug("device disappeared while waiting for '%s'", file);
4295f9
+                        return -2;
4295f9
+                }
4295f9
+                log_debug("wait for '%s' for %i mseconds", file, 1000 / WAIT_LOOP_PER_SECOND);
4295f9
+                nanosleep(&duration, NULL);
4295f9
+        }
4295f9
+        log_debug("waiting for '%s' failed", file);
4295f9
+        return -1;
4295f9
+}
4295f9
+
4295f9
 static int rule_line_add_token(UdevRuleLine *rule_line, UdevRuleTokenType type, UdevRuleOperatorType op, char *value, void *data) {
4295f9
         UdevRuleToken *token;
4295f9
         UdevRuleMatchType match_type = _MATCH_TYPE_INVALID;
4295f9
@@ -957,6 +999,12 @@ static int parse_token(UdevRules *rules, const char *key, char *attr, UdevRuleOp
4295f9
                         r = rule_line_add_token(rule_line, TK_A_RUN_BUILTIN, op, value, UDEV_BUILTIN_CMD_TO_PTR(cmd));
4295f9
                 } else
4295f9
                         return log_token_invalid_attr(rules, key);
4295f9
+        } else if (streq(key, "WAIT_FOR") || streq(key, "WAIT_FOR_SYSFS")) {
4295f9
+                if (op == OP_REMOVE)
4295f9
+                        return log_token_invalid_op(rules, key);
4295f9
+
4295f9
+                rule_line_add_token(rule_line, TK_M_WAITFOR, 0, value, NULL);
4295f9
+                return 1;
4295f9
         } else if (streq(key, "GOTO")) {
4295f9
                 if (attr)
4295f9
                         return log_token_invalid_attr(rules, key);
4295f9
@@ -1643,6 +1691,14 @@ static int udev_rule_apply_token_to_event(
4295f9
 
4295f9
                 return token_match_string(token, val);
4295f9
         }
4295f9
+        case TK_M_WAITFOR: {
4295f9
+                char filename[UDEV_PATH_SIZE];
4295f9
+                int found;
4295f9
+
4295f9
+                udev_event_apply_format(event, token->value, filename, sizeof(filename), false);
4295f9
+                found = (wait_for_file(event->dev, filename, 10) == 0);
4295f9
+                return found || (token->op == OP_NOMATCH);
4295f9
+        }
4295f9
         case TK_M_ATTR:
4295f9
         case TK_M_PARENTS_ATTR:
4295f9
                 return token_match_attr(token, dev, event);
4295f9
diff --git a/test/rule-syntax-check.py b/test/rule-syntax-check.py
4295f9
index 9a9e4d1658..0649bcf58e 100755
4295f9
--- a/test/rule-syntax-check.py
4295f9
+++ b/test/rule-syntax-check.py
4295f9
@@ -20,7 +20,7 @@ no_args_tests = re.compile(r'(ACTION|DEVPATH|KERNELS?|NAME|SYMLINK|SUBSYSTEMS?|D
4295f9
 # PROGRAM can also be specified as an assignment.
4295f9
 program_assign = re.compile(r'PROGRAM\s*=\s*' + quoted_string_re + '$')
4295f9
 args_tests = re.compile(r'(ATTRS?|ENV|CONST|TEST){([a-zA-Z0-9/_.*%-]+)}\s*(?:=|!)=\s*' + quoted_string_re + '$')
4295f9
-no_args_assign = re.compile(r'(NAME|SYMLINK|OWNER|GROUP|MODE|TAG|RUN|LABEL|GOTO|OPTIONS|IMPORT)\s*(?:\+=|:=|=)\s*' + quoted_string_re + '$')
4295f9
+no_args_assign = re.compile(r'(NAME|SYMLINK|OWNER|GROUP|MODE|TAG|RUN|LABEL|GOTO|WAIT_FOR|OPTIONS|IMPORT)\s*(?:\+=|:=|=)\s*' + quoted_string_re + '$')
4295f9
 args_assign = re.compile(r'(ATTR|ENV|IMPORT|RUN){([a-zA-Z0-9/_.*%-]+)}\s*(=|\+=)\s*' + quoted_string_re + '$')
4295f9
 # Find comma-separated groups, but allow commas that are inside quoted strings.
4295f9
 # Using quoted_string_re + '?' so that strings missing the last double quote