1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-10-29 21:55:25 +03:00

handle dynamic rules created in /dev/.udev/rules.d/

This commit is contained in:
Kay Sievers 2007-07-15 19:10:06 +02:00
parent 9aeded07a0
commit 254d6d3ca2
4 changed files with 67 additions and 28 deletions

1
udev.h
View File

@ -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;

View 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,29 +709,61 @@ 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)
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);
@ -740,7 +772,6 @@ int udev_rules_init(struct udev_rules *rules, int resolve_names)
list_del(&name_loop->node);
free(name_loop);
}
}
return retval;
}

View 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);

17
udevd.c
View File

@ -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;