valeriyvdovin / rpms / systemd

Forked from rpms/systemd 4 years ago
Clone

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

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