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