chiark / gitweb /
handle dynamic rules created in /dev/.udev/rules.d/
authorKay Sievers <kay.sievers@vrfy.org>
Sun, 15 Jul 2007 17:10:06 +0000 (19:10 +0200)
committerKay Sievers <kay.sievers@vrfy.org>
Sun, 15 Jul 2007 17:10:06 +0000 (19:10 +0200)
udev.h
udev_rules_parse.c
udev_utils_file.c
udevd.c

diff --git a/udev.h b/udev.h
index 9b22c42b8c3074ea96c18b6b3e370a63799d45e7..f13eb8366eaa6f5707974b2abc22535efb4413a5 100644 (file)
--- a/udev.h
+++ b/udev.h
@@ -49,6 +49,7 @@
 
 #define DB_DIR                                 ".udev/db"
 #define DB_NAME_INDEX_DIR                      ".udev/names"
+#define RULES_DYN_DIR                          ".udev/rules.d"
 
 struct udev_rules;
 
index 638ea159fbdf71e15f9a65b9cd6efb84a894a39b..124410a3402d8b7a36f9c4b5ed9274529f9ceb2b 100644 (file)
@@ -438,7 +438,7 @@ static int add_to_rules(struct udev_rules *rules, char *line, const char *filena
                                /* figure it out if it is executable */
                                char file[PATH_SIZE];
                                char *pos;
-                               struct stat stats;
+                               struct stat statbuf;
 
                                strlcpy(file, value, sizeof(file));
                                pos = strchr(file, ' ');
@@ -455,7 +455,7 @@ static int add_to_rules(struct udev_rules *rules, char *line, const char *filena
                                }
 
                                dbg("IMPORT auto mode for '%s'", file);
-                               if (!lstat(file, &stats) && (stats.st_mode & S_IXUSR)) {
+                               if (!lstat(file, &statbuf) && (statbuf.st_mode & S_IXUSR)) {
                                        dbg("IMPORT is executable, will be executed (autotype)");
                                        rule->import_type  = IMPORT_PROGRAM;
                                } else {
@@ -593,7 +593,7 @@ static int add_to_rules(struct udev_rules *rules, char *line, const char *filena
                        pos = strstr(value, "link_priority=");
                        if (pos != NULL) {
                                rule->link_priority = atoi(&pos[strlen("link_priority=")]);
-                               info("link priority=%i", rule->link_priority);
+                               dbg("link priority=%i", rule->link_priority);
                        }
                        pos = strstr(value, "string_escape=");
                        if (pos != NULL) {
@@ -709,39 +709,70 @@ static int parse_file(struct udev_rules *rules, const char *filename)
 
 int udev_rules_init(struct udev_rules *rules, int resolve_names)
 {
-       struct stat stats;
-       int retval;
+       struct stat statbuf;
+       char filename[PATH_MAX];
+       LIST_HEAD(name_list);
+       LIST_HEAD(dyn_list);
+       struct name_entry *name_loop, *name_tmp;
+       struct name_entry *dyn_loop, *dyn_tmp;
+       int retval = 0;
 
        memset(rules, 0x00, sizeof(struct udev_rules));
        rules->resolve_names = resolve_names;
 
-       /* parse rules file or all matching files in directory */
-       if (stat(udev_rules_dir, &stats) != 0)
+       /* read main config from single file or all files in a directory */
+       if (stat(udev_rules_dir, &statbuf) != 0)
                return -1;
-
-       if ((stats.st_mode & S_IFMT) != S_IFDIR) {
+       if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
                dbg("parse single rules file '%s'", udev_rules_dir);
-               retval = parse_file(rules, udev_rules_dir);
+               name_list_add(&name_list, udev_rules_dir, 1);
        } else {
-               struct name_entry *name_loop, *name_tmp;
-               LIST_HEAD(name_list);
-
                dbg("parse rules directory '%s'", udev_rules_dir);
                retval = add_matching_files(&name_list, udev_rules_dir, RULESFILE_SUFFIX);
+       }
+
+       /* read dynamic rules directory */
+       strlcpy(filename, udev_root, sizeof(filename));
+       strlcat(filename, "/"RULES_DYN_DIR, sizeof(filename));
+       if (stat(filename, &statbuf) != 0) {
+               create_path(filename);
+               mkdir(filename, 0755);
+       }
+       add_matching_files(&dyn_list, filename, RULESFILE_SUFFIX);
+
+       /* sort dynamic rules files by basename into list of files */
+       list_for_each_entry_safe(dyn_loop, dyn_tmp, &dyn_list, node) {
+               const char *dyn_base = strrchr(dyn_loop->name, '/');
+
+               if (dyn_base == NULL)
+                       continue;
 
                list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
-                       if (stat(name_loop->name, &stats) == 0) {
-                               if (stats.st_size)
-                                       parse_file(rules, name_loop->name);
-                               else
-                                       dbg("empty rules file '%s'", name_loop->name);
-                       } else
-                               err("could not read '%s': %s", name_loop->name, strerror(errno));
-                       list_del(&name_loop->node);
-                       free(name_loop);
+                       const char *name_base = strrchr(name_loop->name, '/');
+
+                       if (name_base == NULL)
+                               continue;
+
+                       if (strcmp(name_base, dyn_base) > 0) {
+                               list_move_tail(&dyn_loop->node, &name_loop->node);
+                               break;
+                       }
                }
        }
 
+       /* parse list of files */
+       list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
+               if (stat(name_loop->name, &statbuf) == 0) {
+                       if (statbuf.st_size)
+                               parse_file(rules, name_loop->name);
+                       else
+                               dbg("empty rules file '%s'", name_loop->name);
+               } else
+                       err("could not read '%s': %s", name_loop->name, strerror(errno));
+               list_del(&name_loop->node);
+               free(name_loop);
+       }
+
        return retval;
 }
 
index 44c36863617466803dcdbefe192fe4c11dce3d46..0ceefe17200c68bab8e08a20b0d3be2dc60834ee 100644 (file)
@@ -49,7 +49,7 @@ int create_path(const char *path)
        if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
                return 0;
 
-       if (create_path (p) != 0)
+       if (create_path(p) != 0)
                return -1;
 
        dbg("mkdir '%s'", p);
diff --git a/udevd.c b/udevd.c
index 7dedf078f3ef91ebea9963830c0845e95e0eedae..69572c6268789a896603f3b302d65439219fe2f8 100644 (file)
--- a/udevd.c
+++ b/udevd.c
@@ -1102,10 +1102,17 @@ int main(int argc, char *argv[], char *envp[])
 
        /* watch rules directory */
        inotify_fd = inotify_init();
-       if (inotify_fd >= 0)
+       if (inotify_fd >= 0) {
+               char filename[PATH_MAX];
+
                inotify_add_watch(inotify_fd, udev_rules_dir, IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
-       else if (errno == ENOSYS)
-               err("the kernel does not support inotify, udevd can't monitor configuration file changes");
+
+               /* watch dynamic rules directory */
+               strlcpy(filename, udev_root, sizeof(filename));
+               strlcat(filename, "/"RULES_DYN_DIR, sizeof(filename));
+               inotify_add_watch(inotify_fd, filename, IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
+       } else if (errno == ENOSYS)
+               err("the kernel does not support inotify, udevd can't monitor rules file changes");
        else
                err("inotify_init failed: %s", strerror(errno));
 
@@ -1190,12 +1197,12 @@ int main(int argc, char *argv[], char *envp[])
                        int nbytes;
 
                        /* discard all possible events, we can just reload the config */
-                       if ((ioctl(inotify_fd, FIONREAD, &nbytes) == 0) && nbytes) {
+                       if ((ioctl(inotify_fd, FIONREAD, &nbytes) == 0) && nbytes > 0) {
                                char *buf;
 
                                reload_config = 1;
                                buf = malloc(nbytes);
-                               if (!buf) {
+                               if (buf != NULL) {
                                        err("error getting buffer for inotify, disable watching");
                                        close(inotify_fd);
                                        inotify_fd = -1;