chiark / gitweb /
prepare RELEASE-NOTES
[elogind.git] / udev_rules.c
index aa66c7ad30a858bcdfc3b739cb7ddda2af3e5f09..9606f04d3df64f88b0eb2cbce40d87379ce32ac8 100644 (file)
@@ -174,21 +174,26 @@ static int get_key(char **line, char **key, char **value)
                linepos++;
 
        /* get the value*/
-       if (linepos[0] == '\0')
-               return -1;
-
        if (linepos[0] == '"') {
                linepos++;
                temp = strchr(linepos, '"');
-               if (!temp)
+               if (!temp) {
+                       dbg("missing closing quote");
                        return -1;
+               }
+               dbg("value is quoted");
                temp[0] = '\0';
        } else if (linepos[0] == '\'') {
                linepos++;
                temp = strchr(linepos, '\'');
-               if (!temp)
+               if (!temp) {
+                       dbg("missing closing quote");
                        return -1;
+               }
+               dbg("value is quoted");
                temp[0] = '\0';
+       } else if (linepos[0] == '\0') {
+               dbg("value is empty");
        } else {
                temp = linepos;
                while (temp[0] && !isspace(temp[0]))
@@ -200,7 +205,7 @@ static int get_key(char **line, char **key, char **value)
        return 0;
 }
 
-static int import_keys_into_env(const char *buf, size_t bufsize)
+static int import_keys_into_env(struct udevice *udev, const char *buf, size_t bufsize)
 {
        char line[LINE_SIZE];
        const char *bufline;
@@ -211,7 +216,7 @@ static int import_keys_into_env(const char *buf, size_t bufsize)
        size_t count;
        int lineno;
 
-       /* loop through the whole file */
+       /* loop through the whole buffer */
        lineno = 0;
        cur = 0;
        while (cur < bufsize) {
@@ -241,15 +246,16 @@ static int import_keys_into_env(const char *buf, size_t bufsize)
 
                linepos = line;
                if (get_key(&linepos, &variable, &value) == 0) {
-                       dbg("import %s=%s", variable, value);
-                       setenv(variable, value, 0);
+                       dbg("import '%s=%s'", variable, value);
+                       name_list_key_add(&udev->env_list, variable, value);
+                       setenv(variable, value, 1);
                }
        }
 
        return 0;
 }
 
-static int import_file_into_env(const char *filename)
+static int import_file_into_env(struct udevice *udev, const char *filename)
 {
        char *buf;
        size_t bufsize;
@@ -258,17 +264,24 @@ static int import_file_into_env(const char *filename)
                err("can't open '%s'", filename);
                return -1;
        }
-       import_keys_into_env(buf, bufsize);
+       import_keys_into_env(udev, buf, bufsize);
        file_unmap(buf, bufsize);
 
        return 0;
 }
 
-/** Finds the lowest positive N such that <name>N isn't present in 
- *  $(udevroot) either as a file or a symlink.
- *
- *  @param  name                Name to check for
- *  @return                     0 if <name> didn't exist and N otherwise.
+static int import_program_into_env(struct udevice *udev, const char *program)
+{
+       char result[1024];
+       size_t reslen;
+
+       if (execute_program(program, udev->subsystem, result, sizeof(result), &reslen) != 0)
+               return -1;
+       return import_keys_into_env(udev, result, reslen);
+}
+
+/* finds the lowest positive N such that <name>N isn't present in the udevdb
+ * if <name> doesn't exist, 0 is returned, N otherwise
  */
 static int find_free_number(struct udevice *udev, const char *name)
 {
@@ -780,7 +793,7 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule,
                                        len = strlen(value);
                                        while (len > 0 && isspace(value[len-1]))
                                                value[--len] = '\0';
-                                       dbg("removed %i trailing whitespace chars from '%s'", strlen(value)-len, value);
+                                       dbg("removed %zi trailing whitespace chars from '%s'", strlen(value)-len, value);
                                }
 
                                dbg("compare attribute '%s' value '%s' with '%s'", pair->name, value, pair->value);
@@ -811,16 +824,24 @@ try_parent:
        /* import variables from file into environment */
        if (rule->import_operation != KEY_OP_UNSET) {
                char import[PATH_SIZE];
+               int rc = -1;
 
                strlcpy(import, rule->import, sizeof(import));
                apply_format(udev, import, sizeof(import), class_dev, sysfs_device);
-               dbg("check for " KEY_IMPORT " import='%s", import);
-               if (import_file_into_env(import) != 0) {
+               dbg("check for " KEY_IMPORT " import='%s'", import);
+               if (rule->import_exec) {
+                       dbg("run executable file import='%s'", import);
+                       rc = import_program_into_env(udev, import);
+               } else {
+                       dbg("import file import='%s'", import);
+                       rc = import_file_into_env(udev, import);
+               }
+               if (rc) {
                        dbg(KEY_IMPORT " failed");
                        if (rule->import_operation != KEY_OP_NOMATCH)
                                goto exit;
                } else
-                       dbg(KEY_IMPORT " file '%s' imported", rule->import);
+                       dbg(KEY_IMPORT " '%s' imported", rule->import);
                dbg(KEY_IMPORT " key is true");
        }