chiark / gitweb /
replace spaces in dm and md name symlinks
[elogind.git] / udev / udev_rules.c
index 72c859348e215e28bbadb89fe94179b2550cf321..d06f536d1e0e24aa518ffe656d5d23289cacca4e 100644 (file)
@@ -123,8 +123,8 @@ static int run_program(struct udev *udev, const char *command, const char *subsy
        int outpipe[2] = {-1, -1};
        int errpipe[2] = {-1, -1};
        pid_t pid;
-       char arg[PATH_SIZE];
-       char program[PATH_SIZE];
+       char arg[UTIL_PATH_SIZE];
+       char program[UTIL_PATH_SIZE];
        char *argv[(sizeof(arg) / 2) + 1];
        int devnull;
        int i;
@@ -159,13 +159,13 @@ static int run_program(struct udev *udev, const char *command, const char *subsy
        /* prepare pipes from child to parent */
        if (result != NULL || udev_get_log_priority(udev) >= LOG_INFO) {
                if (pipe(outpipe) != 0) {
-                       err(udev, "pipe failed: %s\n", strerror(errno));
+                       err(udev, "pipe failed: %m\n");
                        return -1;
                }
        }
        if (udev_get_log_priority(udev) >= LOG_INFO) {
                if (pipe(errpipe) != 0) {
-                       err(udev, "pipe failed: %s\n", strerror(errno));
+                       err(udev, "pipe failed: %m\n");
                        return -1;
                }
        }
@@ -196,7 +196,7 @@ static int run_program(struct udev *udev, const char *command, const char *subsy
                                dup2(devnull, STDERR_FILENO);
                        close(devnull);
                } else
-                       err(udev, "open /dev/null failed: %s\n", strerror(errno));
+                       err(udev, "open /dev/null failed: %m\n");
                if (outpipe[WRITE_END] > 0) {
                        dup2(outpipe[WRITE_END], STDOUT_FILENO);
                        close(outpipe[WRITE_END]);
@@ -215,7 +215,7 @@ static int run_program(struct udev *udev, const char *command, const char *subsy
                }
                _exit(1);
        case -1:
-               err(udev, "fork of '%s' failed: %s\n", argv[0], strerror(errno));
+               err(udev, "fork of '%s' failed: %m\n", argv[0]);
                return -1;
        default:
                /* read from child if requested */
@@ -258,7 +258,7 @@ static int run_program(struct udev *udev, const char *command, const char *subsy
                                                close(outpipe[READ_END]);
                                                outpipe[READ_END] = -1;
                                                if (count < 0) {
-                                                       err(udev, "stdin read failed: %s\n", strerror(errno));
+                                                       err(udev, "stdin read failed: %m\n");
                                                        retval = -1;
                                                }
                                                continue;
@@ -292,7 +292,7 @@ static int run_program(struct udev *udev, const char *command, const char *subsy
                                                close(errpipe[READ_END]);
                                                errpipe[READ_END] = -1;
                                                if (count < 0)
-                                                       err(udev, "stderr read failed: %s\n", strerror(errno));
+                                                       err(udev, "stderr read failed: %m\n");
                                                continue;
                                        }
                                        errbuf[count] = '\0';
@@ -331,7 +331,7 @@ static int run_program(struct udev *udev, const char *command, const char *subsy
 
 static int import_keys_into_env(struct udevice *udevice, const char *buf, size_t bufsize)
 {
-       char line[LINE_SIZE];
+       char line[UTIL_LINE_SIZE];
        const char *bufline;
        char *linepos;
        char *variable;
@@ -358,7 +358,7 @@ static int import_keys_into_env(struct udevice *udevice, const char *buf, size_t
                        continue;
 
                /* see if this is a comment */
-               if (bufline[0] == COMMENT_CHARACTER)
+               if (bufline[0] == '#')
                        continue;
 
                if (count >= sizeof(line)) {
@@ -392,7 +392,7 @@ static int import_file_into_env(struct udevice *udevice, const char *filename)
        size_t bufsize;
 
        if (file_map(filename, &buf, &bufsize) != 0) {
-               err(udevice->udev, "can't open '%s': %s\n", filename, strerror(errno));
+               err(udevice->udev, "can't open '%s': %m\n", filename);
                return -1;
        }
        import_keys_into_env(udevice, buf, bufsize);
@@ -429,7 +429,7 @@ static int import_parent_into_env(struct udevice *udevice, const char *filter)
                if (udev_db_get_device(udev_parent, dev_parent->devpath) == 0) {
                        dbg(udevice->udev, "import stored parent env '%s'\n", udev_parent->name);
                        list_for_each_entry(name_loop, &udev_parent->env_list, node) {
-                               char name[NAME_SIZE];
+                               char name[UTIL_NAME_SIZE];
                                char *pos;
 
                                util_strlcpy(name, name_loop->name, sizeof(name));
@@ -512,7 +512,7 @@ int udev_rules_run(struct udevice *udevice)
                if (strncmp(name_loop->name, "socket:", strlen("socket:")) == 0) {
                        pass_env_to_socket(udevice->udev, &name_loop->name[strlen("socket:")], udevice->dev->devpath, udevice->action);
                } else {
-                       char program[PATH_SIZE];
+                       char program[UTIL_PATH_SIZE];
 
                        util_strlcpy(program, name_loop->name, sizeof(program));
                        udev_rules_apply_format(udevice, program, sizeof(program));
@@ -528,8 +528,8 @@ int udev_rules_run(struct udevice *udevice)
 #define WAIT_LOOP_PER_SECOND           50
 static int wait_for_file(struct udevice *udevice, const char *file, int timeout)
 {
-       char filepath[PATH_SIZE];
-       char devicepath[PATH_SIZE] = "";
+       char filepath[UTIL_PATH_SIZE];
+       char devicepath[UTIL_PATH_SIZE] = "";
        struct stat stats;
        int loop = timeout * WAIT_LOOP_PER_SECOND;
 
@@ -566,7 +566,7 @@ static int wait_for_file(struct udevice *udevice, const char *file, int timeout)
 /* handle "[$SUBSYSTEM/$KERNEL]<attribute>" lookup */
 static int attr_get_by_subsys_id(struct udev *udev, const char *attrstr, char *devpath, size_t len, char **attr)
 {
-       char subsys[NAME_SIZE];
+       char subsys[UTIL_NAME_SIZE];
        char *pos;
        char *id;
        char *attrib;
@@ -610,7 +610,7 @@ static int attr_subst_subdir(char *attr, size_t len)
 
        pos = strstr(attr, "/*/");
        if (pos != NULL) {
-               char str[PATH_SIZE];
+               char str[UTIL_PATH_SIZE];
                DIR *dir;
 
                pos[1] = '\0';
@@ -643,8 +643,8 @@ static int attr_subst_subdir(char *attr, size_t len)
 
 void udev_rules_apply_format(struct udevice *udevice, char *string, size_t maxsize)
 {
-       char temp[PATH_SIZE];
-       char temp2[PATH_SIZE];
+       char temp[UTIL_PATH_SIZE];
+       char temp2[UTIL_PATH_SIZE];
        char *head, *tail, *pos, *cpos, *attr, *rest;
        int len;
        int i;
@@ -825,7 +825,7 @@ found:
                        if (attr == NULL)
                                err(udevice->udev, "missing file parameter for attr\n");
                        else {
-                               char devpath[PATH_SIZE];
+                               char devpath[UTIL_PATH_SIZE];
                                char *attrib;
                                const char *value = NULL;
                                size_t size;
@@ -915,7 +915,7 @@ found:
                case SUBST_LINKS:
                        if (!list_empty(&udevice->symlink_list)) {
                                struct name_entry *name_loop;
-                               char symlinks[PATH_SIZE] = "";
+                               char symlinks[UTIL_PATH_SIZE] = "";
 
                                list_for_each_entry(name_loop, &udevice->symlink_list, node) {
                                        util_strlcat(symlinks, name_loop->name, sizeof(symlinks));
@@ -971,7 +971,7 @@ static char *key_pair_name(struct udev_rule *rule, struct key_pair *pair)
 
 static int match_key(struct udev *udev, const char *key_name, struct udev_rule *rule, struct key *key, const char *val)
 {
-       char value[PATH_SIZE];
+       char value[UTIL_PATH_SIZE];
        char *key_value;
        char *pos;
        int match = 0;
@@ -1070,8 +1070,8 @@ static int match_rule(struct udevice *udevice, struct udev_rule *rule)
 
        if (rule->test.operation == KEY_OP_MATCH ||
            rule->test.operation == KEY_OP_NOMATCH) {
-               char filename[PATH_SIZE];
-               char devpath[PATH_SIZE];
+               char filename[UTIL_PATH_SIZE];
+               char devpath[UTIL_PATH_SIZE];
                char *attr;
                struct stat statbuf;
                int match;
@@ -1087,7 +1087,7 @@ static int match_rule(struct udevice *udevice, struct udev_rule *rule)
                                util_strlcat(filename, attr, sizeof(filename));
                        }
                } else if (filename[0] != '/') {
-                       char tmp[PATH_SIZE];
+                       char tmp[UTIL_PATH_SIZE];
 
                        util_strlcpy(tmp, udev_get_sys_path(udevice->udev), sizeof(tmp));
                        util_strlcat(tmp, udevice->dev->devpath, sizeof(tmp));
@@ -1114,7 +1114,7 @@ static int match_rule(struct udevice *udevice, struct udev_rule *rule)
        }
 
        if (rule->wait_for.operation != KEY_OP_UNSET) {
-               char filename[PATH_SIZE];
+               char filename[UTIL_PATH_SIZE];
                int found;
 
                util_strlcpy(filename, key_val(rule, &rule->wait_for), sizeof(filename));
@@ -1132,10 +1132,10 @@ static int match_rule(struct udevice *udevice, struct udev_rule *rule)
                    pair->key.operation == KEY_OP_NOMATCH) {
                        const char *key_name = key_pair_name(rule, pair);
                        const char *key_value = key_val(rule, &pair->key);
-                       char devpath[PATH_SIZE];
+                       char devpath[UTIL_PATH_SIZE];
                        char *attrib;
                        const char *value = NULL;
-                       char val[VALUE_SIZE];
+                       char val[UTIL_NAME_SIZE];
                        size_t len;
 
                        if (attr_get_by_subsys_id(udevice->udev, key_name, devpath, sizeof(devpath), &attrib)) {
@@ -1188,7 +1188,7 @@ static int match_rule(struct udevice *udevice, struct udev_rule *rule)
                                const char *key_name = key_pair_name(rule, pair);
                                const char *key_value = key_val(rule, &pair->key);
                                const char *value;
-                               char val[VALUE_SIZE];
+                               char val[UTIL_NAME_SIZE];
                                size_t len;
 
                                value = sysfs_attr_get_value(udevice->udev, udevice->dev_parent->devpath, key_name);
@@ -1226,8 +1226,8 @@ try_parent:
 
        /* execute external program */
        if (rule->program.operation != KEY_OP_UNSET) {
-               char program[PATH_SIZE];
-               char result[PATH_SIZE];
+               char program[UTIL_PATH_SIZE];
+               char result[UTIL_PATH_SIZE];
 
                util_strlcpy(program, key_val(rule, &rule->program), sizeof(program));
                udev_rules_apply_format(udevice, program, sizeof(program));
@@ -1262,7 +1262,7 @@ try_parent:
 
        /* import variables returned from program or or file into environment */
        if (rule->import.operation != KEY_OP_UNSET) {
-               char import[PATH_SIZE];
+               char import[UTIL_PATH_SIZE];
                int rc = -1;
 
                util_strlcpy(import, key_val(rule, &rule->import), sizeof(import));
@@ -1291,13 +1291,13 @@ try_parent:
                struct key_pair *pair = &rule->env.keys[i];
 
                if (pair->key.operation == KEY_OP_ASSIGN) {
-                       char temp_value[NAME_SIZE];
+                       char temp_value[UTIL_NAME_SIZE];
                        const char *key_name = key_pair_name(rule, pair);
                        const char *value = key_val(rule, &pair->key);
 
                        /* make sure we don't write to the same string we possibly read from */
                        util_strlcpy(temp_value, value, sizeof(temp_value));
-                       udev_rules_apply_format(udevice, temp_value, NAME_SIZE);
+                       udev_rules_apply_format(udevice, temp_value, sizeof(temp_value));
 
                        if (temp_value[0] == '\0') {
                                name_list_key_remove(udevice->udev, &udevice->env_list, key_name);
@@ -1321,10 +1321,10 @@ try_parent:
 
                if (pair->key.operation == KEY_OP_ASSIGN) {
                        const char *key_name = key_pair_name(rule, pair);
-                       char devpath[PATH_SIZE];
+                       char devpath[UTIL_PATH_SIZE];
                        char *attrib;
-                       char attr[PATH_SIZE] = "";
-                       char value[NAME_SIZE];
+                       char attr[UTIL_PATH_SIZE] = "";
+                       char value[UTIL_NAME_SIZE];
                        FILE *f;
 
                        if (attr_get_by_subsys_id(udevice->udev, key_name, devpath, sizeof(devpath), &attrib)) {
@@ -1352,10 +1352,10 @@ try_parent:
                        if (f != NULL) {
                                if (!udevice->test_run)
                                        if (fprintf(f, "%s", value) <= 0)
-                                               err(udevice->udev, "error writing ATTR{%s}: %s\n", attr, strerror(errno));
+                                               err(udevice->udev, "error writing ATTR{%s}: %m\n", attr);
                                fclose(f);
                        } else
-                               err(udevice->udev, "error opening ATTR{%s} for writing: %s\n", attr, strerror(errno));
+                               err(udevice->udev, "error opening ATTR{%s} for writing: %m\n", attr);
                }
        }
        return 0;
@@ -1445,9 +1445,9 @@ int udev_rules_get_name(struct udev_rules *rules, struct udevice *udevice)
                            (rule->symlink.operation == KEY_OP_ASSIGN ||
                             rule->symlink.operation == KEY_OP_ASSIGN_FINAL ||
                             rule->symlink.operation == KEY_OP_ADD)) {
-                               char temp[PATH_SIZE];
+                               char temp[UTIL_PATH_SIZE];
                                char *pos, *next;
-                               int count;
+                               int count = 0;
 
                                if (rule->symlink.operation == KEY_OP_ASSIGN_FINAL)
                                        udevice->symlink_final = 1;
@@ -1459,12 +1459,12 @@ int udev_rules_get_name(struct udev_rules *rules, struct udevice *udevice)
                                /* allow  multiple symlinks separated by spaces */
                                util_strlcpy(temp, key_val(rule, &rule->symlink), sizeof(temp));
                                udev_rules_apply_format(udevice, temp, sizeof(temp));
-                               if (rule->string_escape == ESCAPE_UNSET ||
-                                   rule->string_escape == ESCAPE_REPLACE) {
+                               if (rule->string_escape == ESCAPE_UNSET)
                                        count = util_replace_chars(temp, ALLOWED_CHARS_FILE " ");
-                                       if (count > 0)
-                                               info(udevice->udev, "%i character(s) replaced\n" , count);
-                               }
+                               else if (rule->string_escape == ESCAPE_REPLACE)
+                                       count = util_replace_chars(temp, ALLOWED_CHARS_FILE);
+                               if (count > 0)
+                                       info(udevice->udev, "%i character(s) replaced\n" , count);
                                dbg(udevice->udev, "rule applied, added symlink(s) '%s'\n", temp);
                                pos = temp;
                                while (isspace(pos[0]))
@@ -1529,7 +1529,7 @@ int udev_rules_get_name(struct udev_rules *rules, struct udevice *udevice)
 
                        if (rule->goto_label.operation != KEY_OP_UNSET) {
                                dbg(udevice->udev, "moving forward to label '%s'\n", key_val(rule, &rule->goto_label));
-                               udev_rules_iter_label(&iter, key_val(rule, &rule->goto_label));
+                               udev_rules_iter_goto(&iter, rule->goto_rule_off);
                        }
                }
        }
@@ -1609,7 +1609,7 @@ int udev_rules_get_run(struct udev_rules *rules, struct udevice *udevice)
 
                        if (rule->goto_label.operation != KEY_OP_UNSET) {
                                dbg(udevice->udev, "moving forward to label '%s'\n", key_val(rule, &rule->goto_label));
-                               udev_rules_iter_label(&iter, key_val(rule, &rule->goto_label));
+                               udev_rules_iter_goto(&iter, rule->goto_rule_off);
                        }
                }
        }