chiark / gitweb /
don not print error if GOTO jumps just to next rule
[elogind.git] / udev / udev_rules_parse.c
index 9513ac9cf3dab5c4e3be81dc19c3e3028bc17014..c42bd08c51f1d2490da2f5fce7a126976a57c58a 100644 (file)
@@ -1,24 +1,21 @@
 /*
  * Copyright (C) 2003,2004 Greg Kroah-Hartman <greg@kroah.com>
- * Copyright (C) 2003-2006 Kay Sievers <kay.sievers@vrfy.org>
+ * Copyright (C) 2003-2008 Kay Sievers <kay.sievers@vrfy.org>
  *
- *     This program is free software; you can redistribute it and/or modify it
- *     under the terms of the GNU General Public License as published by the
- *     Free Software Foundation version 2 of the License.
- * 
- *     This program is distributed in the hope that it will be useful, but
- *     WITHOUT ANY WARRANTY; without even the implied warranty of
- *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- *     General Public License for more details.
- * 
- *     You should have received a copy of the GNU General Public License along
- *     with this program; if not, write to the Free Software Foundation, Inc.,
- *     51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
  *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "config.h"
-
 #include <stddef.h>
 #include <stdlib.h>
 #include <string.h>
@@ -34,7 +31,7 @@
 
 void udev_rules_iter_init(struct udev_rules_iter *iter, struct udev_rules *rules)
 {
-       dbg(iter->rules->udev, "bufsize=%zi\n", rules->bufsize);
+       dbg(rules->udev, "bufsize=%zi\n", rules->bufsize);
        iter->rules = rules;
        iter->current = 0;
 }
@@ -48,9 +45,9 @@ struct udev_rule *udev_rules_iter_next(struct udev_rules_iter *iter)
        if (!rules)
                return NULL;
 
-       dbg(iter->rules->udev, "current=%zi\n", iter->current);
+       dbg(rules->udev, "current=%zi\n", iter->current);
        if (iter->current >= rules->bufsize) {
-               dbg(iter->rules->udev, "no more rules\n");
+               dbg(rules->udev, "no more rules\n");
                return NULL;
        }
 
@@ -61,29 +58,40 @@ struct udev_rule *udev_rules_iter_next(struct udev_rules_iter *iter)
        return rule;
 }
 
-struct udev_rule *udev_rules_iter_label(struct udev_rules_iter *iter, const char *label)
+struct udev_rule *udev_rules_iter_goto(struct udev_rules_iter *iter, size_t rule_off)
+{
+       struct udev_rules *rules = iter->rules;
+       struct udev_rule *rule;
+
+       dbg(rules->udev, "current=%zi\n", iter->current);
+       iter->current = rule_off;
+       rule = (struct udev_rule *) (rules->buf + iter->current);
+
+       return rule;
+}
+
+static size_t find_label(const struct udev_rules_iter *iter, const char *label)
 {
        struct udev_rule *rule;
        struct udev_rules *rules = iter->rules;
-       size_t start = iter->current;
+       size_t current = iter->current;
 
 next:
-       dbg(iter->rules->udev, "current=%zi\n", iter->current);
-       if (iter->current >= rules->bufsize) {
-               err(rules->udev, "LABEL='%s' not found, GOTO will be ignored\n", label);
-               iter->current = start;
-               return NULL;
+       dbg(rules->udev, "current=%zi\n", current);
+       if (current >= rules->bufsize) {
+               dbg(rules->udev, "LABEL='%s' not found\n", label);
+               return 0;
        }
-       rule = (struct udev_rule *) (rules->buf + iter->current);
+       rule = (struct udev_rule *) (rules->buf + current);
 
        if (strcmp(&rule->buf[rule->label.val_off], label) != 0) {
                dbg(rules->udev, "moving forward, looking for label '%s'\n", label);
-               iter->current += sizeof(struct udev_rule) + rule->bufsize;
+               current += sizeof(struct udev_rule) + rule->bufsize;
                goto next;
        }
 
        dbg(rules->udev, "found label '%s'\n", label);
-       return rule;
+       return current;
 }
 
 static int get_key(struct udev_rules *rules, char **line, char **key, enum key_operation *operation, char **value)
@@ -205,12 +213,12 @@ static char *get_key_attribute(struct udev_rules *rules, char *str)
 static int add_rule_key(struct udev_rule *rule, struct key *key,
                        enum key_operation operation, const char *value)
 {
-       size_t val_len = strnlen(value, PATH_SIZE);
+       size_t val_len = strnlen(value, UTIL_PATH_SIZE);
 
        key->operation = operation;
 
        key->val_off = rule->bufsize;
-       strlcpy(rule->buf + rule->bufsize, value, val_len+1);
+       util_strlcpy(rule->buf + rule->bufsize, value, val_len+1);
        rule->bufsize += val_len+1;
 
        return 0;
@@ -219,7 +227,7 @@ static int add_rule_key(struct udev_rule *rule, struct key *key,
 static int add_rule_key_pair(struct udev_rules *rules, struct udev_rule *rule, struct key_pairs *pairs,
                             enum key_operation operation, const char *key, const char *value)
 {
-       size_t key_len = strnlen(key, PATH_SIZE);
+       size_t key_len = strnlen(key, UTIL_PATH_SIZE);
 
        if (pairs->count >= PAIRS_MAX) {
                err(rules->udev, "skip, too many keys of the same type in a single rule\n");
@@ -230,7 +238,7 @@ static int add_rule_key_pair(struct udev_rules *rules, struct udev_rule *rule, s
 
        /* add the key-name of the pair */
        pairs->keys[pairs->count].key_name_off = rule->bufsize;
-       strlcpy(rule->buf + rule->bufsize, key, key_len+1);
+       util_strlcpy(rule->buf + rule->bufsize, key, key_len+1);
        rule->bufsize += key_len+1;
 
        pairs->count++;
@@ -240,7 +248,7 @@ static int add_rule_key_pair(struct udev_rules *rules, struct udev_rule *rule, s
 
 static int add_to_rules(struct udev_rules *rules, char *line, const char *filename, unsigned int lineno)
 {
-       char buf[sizeof(struct udev_rule) + LINE_SIZE];
+       char buf[sizeof(struct udev_rule) + UTIL_LINE_SIZE];
        struct udev_rule *rule;
        size_t rule_size;
        int valid;
@@ -445,19 +453,19 @@ static int add_to_rules(struct udev_rules *rules, char *line, const char *filena
                                rule->import_type = IMPORT_PARENT;
                        } else {
                                /* figure it out if it is executable */
-                               char file[PATH_SIZE];
+                               char file[UTIL_PATH_SIZE];
                                char *pos;
                                struct stat statbuf;
 
-                               strlcpy(file, value, sizeof(file));
+                               util_strlcpy(file, value, sizeof(file));
                                pos = strchr(file, ' ');
                                if (pos)
                                        pos[0] = '\0';
 
                                /* allow programs in /lib/udev called without the path */
                                if (strchr(file, '/') == NULL) {
-                                       strlcpy(file, UDEV_PREFIX "/lib/udev/", sizeof(file));
-                                       strlcat(file, value, sizeof(file));
+                                       util_strlcpy(file, UDEV_PREFIX "/lib/udev/", sizeof(file));
+                                       util_strlcat(file, value, sizeof(file));
                                        pos = strchr(file, ' ');
                                        if (pos)
                                                pos[0] = '\0';
@@ -669,7 +677,7 @@ invalid:
 
 static int parse_file(struct udev_rules *rules, const char *filename)
 {
-       char line[LINE_SIZE];
+       char line[UTIL_LINE_SIZE];
        char *bufline;
        unsigned int lineno;
        char *buf;
@@ -677,9 +685,14 @@ static int parse_file(struct udev_rules *rules, const char *filename)
        size_t cur;
        size_t count;
        int retval = 0;
+       size_t start;
+       struct udev_rule *rule;
+       struct udev_rules_iter iter;
+
+       start = rules->bufsize;
 
        if (file_map(filename, &buf, &bufsize) != 0) {
-               err(rules->udev, "can't open '%s' as rules file: %s\n", filename, strerror(errno));
+               err(rules->udev, "can't open '%s' as rules file: %m\n", filename);
                return -1;
        }
        info(rules->udev, "reading '%s' as rules file\n", filename);
@@ -704,7 +717,7 @@ static int parse_file(struct udev_rules *rules, const char *filename)
                        continue;
 
                /* see if this is a comment */
-               if (bufline[0] == COMMENT_CHARACTER)
+               if (bufline[0] == '#')
                        continue;
 
                if (count >= sizeof(line)) {
@@ -725,6 +738,23 @@ static int parse_file(struct udev_rules *rules, const char *filename)
                add_to_rules(rules, line, filename, lineno);
        }
 
+       /* Compute all goto targets within this file */
+       udev_rules_iter_init(&iter, rules);
+       udev_rules_iter_goto(&iter, start);
+       while((rule = udev_rules_iter_next(&iter))) {
+               if (rule->goto_label.operation != KEY_OP_UNSET) {
+                       char *goto_label = &rule->buf[rule->goto_label.val_off];
+
+                       dbg(rules->udev, "resolving goto label '%s'\n", goto_label);
+                       rule->goto_rule_off = find_label(&iter, goto_label);
+                       if (rule->goto_rule_off == 0) {
+                               err(rules->udev, "ignore goto to nonexistent label '%s' in '%s'\n",
+                                   goto_label, filename);
+                               rule->goto_rule_off = iter.current;
+                       }
+               }
+       }
+
        file_unmap(buf, bufsize);
        return retval;
 }
@@ -751,8 +781,8 @@ int udev_rules_init(struct udev *udev, struct udev_rules *rules, int resolve_nam
                add_matching_files(udev, &name_list, SYSCONFDIR "/udev/rules.d", ".rules");
 
                /* read dynamic/temporary rules */
-               strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
-               strlcat(filename, "/.udev/rules.d", sizeof(filename));
+               util_strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
+               util_strlcat(filename, "/.udev/rules.d", sizeof(filename));
                if (stat(filename, &statbuf) != 0) {
                        create_path(udev, filename);
                        udev_selinux_setfscreatecon(udev, filename, S_IFDIR|0755);
@@ -802,7 +832,7 @@ int udev_rules_init(struct udev *udev, struct udev_rules *rules, int resolve_nam
                        else
                                dbg(udev, "empty rules file '%s'\n", name_loop->name);
                } else
-                       err(udev, "could not read '%s': %s\n", name_loop->name, strerror(errno));
+                       err(udev, "could not read '%s': %m\n", name_loop->name);
                list_del(&name_loop->node);
                free(name_loop);
        }