Blame SOURCES/0016-Revert-udev-remove-WAIT_FOR-key.patch

a3e2b5
From 1bb734a44952a51285057409ba7b1c3e7a162cea Mon Sep 17 00:00:00 2001
a3e2b5
From: Michal Sekletar <msekleta@redhat.com>
a3e2b5
Date: Thu, 2 Aug 2018 13:16:49 +0200
a3e2b5
Subject: [PATCH] Revert "udev: remove WAIT_FOR key"
a3e2b5
a3e2b5
This reverts commit f2b8052fb648b788936dd3e85be6a9aca90fbb2f.
a3e2b5
a3e2b5
Resolves: #1523213
a3e2b5
---
a3e2b5
 man/udev.xml              |  9 +++++++
a3e2b5
 src/udev/udev-rules.c     | 50 +++++++++++++++++++++++++++++++++++++++
a3e2b5
 test/rule-syntax-check.py |  2 +-
a3e2b5
 3 files changed, 60 insertions(+), 1 deletion(-)
a3e2b5
a3e2b5
diff --git a/man/udev.xml b/man/udev.xml
a3e2b5
index 15e6d8eae1..bdf901a8f0 100644
a3e2b5
--- a/man/udev.xml
a3e2b5
+++ b/man/udev.xml
a3e2b5
@@ -515,6 +515,15 @@
a3e2b5
           </listitem>
a3e2b5
         </varlistentry>
a3e2b5
 
a3e2b5
+        <varlistentry>
a3e2b5
+          <term><varname>WAIT_FOR</varname></term>
a3e2b5
+          <listitem>
a3e2b5
+            <para>Wait for a file to become available or until a timeout of
a3e2b5
+            10 seconds expires. The path is relative to the sysfs device;
a3e2b5
+            if no path is specified, this waits for an attribute to appear.</para>
a3e2b5
+          </listitem>
a3e2b5
+        </varlistentry>
a3e2b5
+
a3e2b5
         <varlistentry>
a3e2b5
           <term><varname>OPTIONS</varname></term>
a3e2b5
           <listitem>
a3e2b5
diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c
a3e2b5
index f029395884..58af863f3d 100644
a3e2b5
--- a/src/udev/udev-rules.c
a3e2b5
+++ b/src/udev/udev-rules.c
a3e2b5
@@ -676,6 +676,41 @@ static int import_parent_into_properties(struct udev_device *dev, const char *fi
a3e2b5
         return 0;
a3e2b5
 }
a3e2b5
 
a3e2b5
+#define WAIT_LOOP_PER_SECOND                50
a3e2b5
+static int wait_for_file(struct udev_device *dev, const char *file, int timeout) {
a3e2b5
+        char filepath[UTIL_PATH_SIZE];
a3e2b5
+        char devicepath[UTIL_PATH_SIZE];
a3e2b5
+        struct stat stats;
a3e2b5
+        int loop = timeout * WAIT_LOOP_PER_SECOND;
a3e2b5
+
a3e2b5
+        /* a relative path is a device attribute */
a3e2b5
+        devicepath[0] = '\0';
a3e2b5
+        if (file[0] != '/') {
a3e2b5
+                strscpyl(devicepath, sizeof(devicepath), udev_device_get_syspath(dev), NULL);
a3e2b5
+                strscpyl(filepath, sizeof(filepath), devicepath, "/", file, NULL);
a3e2b5
+                file = filepath;
a3e2b5
+        }
a3e2b5
+
a3e2b5
+        while (--loop) {
a3e2b5
+                const struct timespec duration = { 0, 1000 * 1000 * 1000 / WAIT_LOOP_PER_SECOND };
a3e2b5
+
a3e2b5
+                /* lookup file */
a3e2b5
+                if (stat(file, &stats) == 0) {
a3e2b5
+                        log_debug("file '%s' appeared after %i loops", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
a3e2b5
+                        return 0;
a3e2b5
+                }
a3e2b5
+                /* make sure, the device did not disappear in the meantime */
a3e2b5
+                if (devicepath[0] != '\0' && stat(devicepath, &stats) != 0) {
a3e2b5
+                        log_debug("device disappeared while waiting for '%s'", file);
a3e2b5
+                        return -2;
a3e2b5
+                }
a3e2b5
+                log_debug("wait for '%s' for %i mseconds", file, 1000 / WAIT_LOOP_PER_SECOND);
a3e2b5
+                nanosleep(&duration, NULL);
a3e2b5
+        }
a3e2b5
+        log_debug("waiting for '%s' failed", file);
a3e2b5
+        return -1;
a3e2b5
+}
a3e2b5
+
a3e2b5
 static void attr_subst_subdir(char *attr, size_t len) {
a3e2b5
         const char *pos, *tail, *path;
a3e2b5
         _cleanup_closedir_ DIR *dir = NULL;
a3e2b5
@@ -1284,7 +1319,12 @@ static void add_rule(struct udev_rules *rules, char *line,
a3e2b5
                                 rule_add_key(&rule_tmp, TK_A_RUN_PROGRAM, op, value, &cmd);
a3e2b5
                         } else
a3e2b5
                                 LOG_RULE_ERROR("ignoring unknown %s{} type '%s'", "RUN", attr);
a3e2b5
+                } else if (streq(key, "WAIT_FOR") || streq(key, "WAIT_FOR_SYSFS")) {
a3e2b5
+                        if (op == OP_REMOVE)
a3e2b5
+                                LOG_AND_RETURN("invalid %s operation", key);
a3e2b5
 
a3e2b5
+                        rule_add_key(&rule_tmp, TK_M_WAITFOR, 0, value, NULL);
a3e2b5
+                        continue;
a3e2b5
                 } else if (streq(key, "LABEL")) {
a3e2b5
                         if (op == OP_REMOVE)
a3e2b5
                                 LOG_AND_RETURN("invalid %s operation", key);
a3e2b5
@@ -1838,6 +1878,16 @@ void udev_rules_apply_to_event(struct udev_rules *rules,
a3e2b5
                         if (match_key(rules, cur, udev_device_get_driver(event->dev)) != 0)
a3e2b5
                                 goto nomatch;
a3e2b5
                         break;
a3e2b5
+                case TK_M_WAITFOR: {
a3e2b5
+                        char filename[UTIL_PATH_SIZE];
a3e2b5
+                        int found;
a3e2b5
+
a3e2b5
+                        udev_event_apply_format(event, rules_str(rules, cur->key.value_off), filename, sizeof(filename), false);
a3e2b5
+                        found = (wait_for_file(event->dev, filename, 10) == 0);
a3e2b5
+                        if (!found && (cur->key.op != OP_NOMATCH))
a3e2b5
+                                goto nomatch;
a3e2b5
+                        break;
a3e2b5
+                }
a3e2b5
                 case TK_M_ATTR:
a3e2b5
                         if (match_attr(rules, event->dev, event, cur) != 0)
a3e2b5
                                 goto nomatch;
a3e2b5
diff --git a/test/rule-syntax-check.py b/test/rule-syntax-check.py
a3e2b5
index dfb06d9ed9..706d93632e 100755
a3e2b5
--- a/test/rule-syntax-check.py
a3e2b5
+++ b/test/rule-syntax-check.py
a3e2b5
@@ -18,7 +18,7 @@ if not rules_files:
a3e2b5
 quoted_string_re = r'"(?:[^\\"]|\\.)*"'
a3e2b5
 no_args_tests = re.compile(r'(ACTION|DEVPATH|KERNELS?|NAME|SYMLINK|SUBSYSTEMS?|DRIVERS?|TAG|PROGRAM|RESULT|TEST)\s*(?:=|!)=\s*' + quoted_string_re + '$')
a3e2b5
 args_tests = re.compile(r'(ATTRS?|ENV|TEST){([a-zA-Z0-9/_.*%-]+)}\s*(?:=|!)=\s*' + quoted_string_re + '$')
a3e2b5
-no_args_assign = re.compile(r'(NAME|SYMLINK|OWNER|GROUP|MODE|TAG|RUN|LABEL|GOTO|OPTIONS|IMPORT)\s*(?:\+=|:=|=)\s*' + quoted_string_re + '$')
a3e2b5
+no_args_assign = re.compile(r'(NAME|SYMLINK|OWNER|GROUP|MODE|TAG|RUN|LABEL|GOTO|WAIT_FOR|OPTIONS|IMPORT)\s*(?:\+=|:=|=)\s*' + quoted_string_re + '$')
a3e2b5
 args_assign = re.compile(r'(ATTR|ENV|IMPORT|RUN){([a-zA-Z0-9/_.*%-]+)}\s*(=|\+=)\s*' + quoted_string_re + '$')
a3e2b5
 # Find comma-separated groups, but allow commas that are inside quoted strings.
a3e2b5
 # Using quoted_string_re + '?' so that strings missing the last double quote