From: Kay Sievers Date: Thu, 16 Oct 2008 15:16:58 +0000 (+0200) Subject: udevd: use libudev X-Git-Tag: 174~1446 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=commitdiff_plain;h=aa8734ffcb8a895fc8d66ff383cbcf8f4b78f562 udevd: use libudev --- diff --git a/udev/Makefile.am b/udev/Makefile.am index c0df391d6..ff8b27555 100644 --- a/udev/Makefile.am +++ b/udev/Makefile.am @@ -17,12 +17,10 @@ common_files = \ udev_rules.h \ udev_sysdeps.h \ udev_db.c \ - udev_device.c \ udev_device_event.c \ udev_node.c \ udev_rules.c \ udev_rules_parse.c \ - udev_sysfs.c \ udev_utils.c \ udev_utils_file.c \ list.h \ diff --git a/udev/lib/libudev-device.c b/udev/lib/libudev-device.c index daf221776..65ba6e30d 100644 --- a/udev/lib/libudev-device.c +++ b/udev/lib/libudev-device.c @@ -75,7 +75,7 @@ static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *f return util_path_encode(&filename[start], len - start); } -static int device_read_db(struct udev_device *udev_device) +int udev_device_read_db(struct udev_device *udev_device) { struct stat stats; char filename[UTIL_PATH_SIZE]; @@ -220,7 +220,7 @@ static void device_load_info(struct udev_device *device) { device->info_loaded = 1; udev_device_read_uevent_file(device); - device_read_db(device); + udev_device_read_db(device); } void udev_device_set_info_loaded(struct udev_device *device) diff --git a/udev/lib/libudev-private.h b/udev/lib/libudev-private.h index 60d995aa7..5290bc801 100644 --- a/udev/lib/libudev-private.h +++ b/udev/lib/libudev-private.h @@ -62,6 +62,7 @@ extern void udev_device_cleanup_devlinks_list(struct udev_device *udev_device); extern struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value); extern struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property); extern char **udev_device_get_properties_envp(struct udev_device *udev_device); +extern int udev_device_read_db(struct udev_device *udev_device); extern int udev_device_read_uevent_file(struct udev_device *udev_device); extern int udev_device_set_action(struct udev_device *udev_device, const char *action); extern int udev_device_set_driver(struct udev_device *udev_device, const char *driver); diff --git a/udev/test-udev.c b/udev/test-udev.c index 508f9f140..6c327a6e0 100644 --- a/udev/test-udev.c +++ b/udev/test-udev.c @@ -45,20 +45,20 @@ static void asmlinkage sig_handler(int signum) int main(int argc, char *argv[]) { struct udev *udev; - struct sysfs_device *dev; - struct udevice *udevice; - const char *maj, *min; + struct udev_event *event; + struct udev_device *dev; struct udev_rules rules; - const char *action; + char syspath[UTIL_PATH_SIZE]; const char *devpath; + const char *action; const char *subsystem; struct sigaction act; - int retval = -EINVAL; + int err = -EINVAL; udev = udev_new(); if (udev == NULL) exit(1); - dbg(udev, "version %s\n", VERSION); + info(udev, "version %s\n", VERSION); selinux_init(udev); /* set signal handlers */ @@ -76,65 +76,45 @@ int main(int argc, char *argv[]) action = getenv("ACTION"); devpath = getenv("DEVPATH"); subsystem = getenv("SUBSYSTEM"); - /* older kernels passed the SUBSYSTEM only as argument */ - if (subsystem == NULL && argc == 2) - subsystem = argv[1]; if (action == NULL || subsystem == NULL || devpath == NULL) { err(udev, "action, subsystem or devpath missing\n"); goto exit; } - /* export log_priority , as called programs may want to do the same as udev */ - if (udev_get_log_priority(udev) > 0) { - char priority[32]; - - sprintf(priority, "%i", udev_get_log_priority(udev)); - setenv("UDEV_LOG", priority, 1); - } - - sysfs_init(); udev_rules_init(udev, &rules, 0); - dev = sysfs_device_get(udev, devpath); + util_strlcpy(syspath, udev_get_sys_path(udev), sizeof(syspath)); + util_strlcat(syspath, devpath, sizeof(syspath)); + dev = udev_device_new_from_syspath(udev, syspath); if (dev == NULL) { - info(udev, "unable to open '%s'\n", devpath); + info(udev, "unknown device '%s'\n", devpath); goto fail; } - udevice = udev_device_init(udev); - if (udevice == NULL) - goto fail; - - /* override built-in sysfs device */ - udevice->dev = dev; - util_strlcpy(udevice->action, action, sizeof(udevice->action)); - - /* get dev_t from environment, which is needed for "remove" to work, "add" works also from sysfs */ - maj = getenv("MAJOR"); - min = getenv("MINOR"); - if (maj != NULL && min != NULL) - udevice->devt = makedev(atoi(maj), atoi(min)); - else - udevice->devt = udev_device_get_devt(udevice); + /* skip reading of db, but read kernel parameters */ + udev_device_set_info_loaded(dev); + udev_device_read_uevent_file(dev); - retval = udev_device_event(&rules, udevice); + udev_device_set_action(dev, action); + event = udev_event_new(dev); + err = udev_event_run(event, &rules); /* rules may change/disable the timeout */ - if (udevice->event_timeout >= 0) - alarm(udevice->event_timeout); + if (udev_device_get_event_timeout(dev) >= 0) + alarm(udev_device_get_event_timeout(dev)); - if (retval == 0 && !udevice->ignore_device && udev_get_run(udev)) - udev_rules_run(udevice); + if (err == 0 && !event->ignore_device && udev_get_run(udev)) + udev_rules_run(event); - udev_device_cleanup(udevice); + udev_event_unref(event); + udev_device_unref(dev); fail: udev_rules_cleanup(&rules); - sysfs_cleanup(); exit: selinux_exit(udev); udev_unref(udev); - if (retval != 0) + if (err != 0) return 1; return 0; } diff --git a/udev/udev.h b/udev/udev.h index 6a685e675..896a46676 100644 --- a/udev/udev.h +++ b/udev/udev.h @@ -23,7 +23,6 @@ #include #include "udev_sysdeps.h" -#define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE 1 #include "lib/libudev.h" #include "lib/libudev-private.h" #include "list.h" @@ -49,52 +48,6 @@ struct udev_rules; -struct sysfs_device { - struct list_head node; /* for device cache */ - struct sysfs_device *parent; /* already cached parent*/ - char devpath[UTIL_PATH_SIZE]; - char subsystem[UTIL_NAME_SIZE]; - char kernel[UTIL_NAME_SIZE]; /* device instance name */ - char kernel_number[UTIL_NAME_SIZE]; - char driver[UTIL_NAME_SIZE]; -}; - -struct udevice { - struct udev *udev; - - /* device event */ - struct sysfs_device *dev; /* points to dev_local by default */ - struct sysfs_device dev_local; - struct sysfs_device *dev_parent; /* current parent device used for matching */ - char action[UTIL_NAME_SIZE]; - char *devpath_old; - - /* node */ - char name[UTIL_PATH_SIZE]; - struct list_head symlink_list; - int symlink_final; - char owner[UTIL_NAME_SIZE]; - int owner_final; - char group[UTIL_NAME_SIZE]; - int group_final; - mode_t mode; - int mode_final; - dev_t devt; - - /* event processing */ - struct list_head run_list; - int run_final; - struct list_head env_list; - char tmp_node[UTIL_PATH_SIZE]; - int partitions; - int ignore_device; - int ignore_remove; - char program_result[UTIL_PATH_SIZE]; - int link_priority; - int event_timeout; - int test_run; -}; - static inline void logging_init(const char *program_name) { openlog(program_name, LOG_PID | LOG_CONS, LOG_DAEMON); @@ -112,40 +65,48 @@ static inline void logging_close(void) closelog(); } -/* udev_device.c */ -extern struct udevice *udev_device_init(struct udev *udev); -extern void udev_device_cleanup(struct udevice *udevice); -extern dev_t udev_device_get_devt(struct udevice *udevice); - -/* udev_device_event.c */ -extern int udev_device_event(struct udev_rules *rules, struct udevice *udevice); - -/* udev_sysfs.c */ -extern int sysfs_init(void); -extern void sysfs_cleanup(void); -extern void sysfs_device_set_values(struct udev *udev, - struct sysfs_device *dev, const char *devpath, - const char *subsystem, const char *driver); -extern struct sysfs_device *sysfs_device_get(struct udev *udev, const char *devpath); -extern struct sysfs_device *sysfs_device_get_parent(struct udev *udev, struct sysfs_device *dev); -extern struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct udev *udev, struct sysfs_device *dev, const char *subsystem); -extern char *sysfs_attr_get_value(struct udev *udev, const char *devpath, const char *attr_name); -extern int sysfs_lookup_devpath_by_subsys_id(struct udev *udev, char *devpath, size_t len, const char *subsystem, const char *id); - -/* udev_node.c */ -extern int udev_node_mknod(struct udevice *udevice, const char *file, dev_t devt, mode_t mode, uid_t uid, gid_t gid); -extern void udev_node_update_symlinks(struct udevice *udevice, struct udevice *udev_old); -extern int udev_node_add(struct udevice *udevice); -extern int udev_node_remove(struct udevice *udevice); - -/* udev_db.c */ -extern int udev_db_add_device(struct udevice *udevice); -extern int udev_db_delete_device(struct udevice *udevice); -extern int udev_db_rename(struct udev *udev, const char *devpath_old, const char *devpath); -extern int udev_db_get_device(struct udevice *udevice, const char *devpath); -extern int udev_db_get_devices_by_name(struct udev *udev, const char *name, struct list_head *name_list); - -/* udev_utils.c */ +/* udev-event.c */ +struct udev_event { + struct udev *udev; + struct udev_device *dev; + struct udev_device *dev_parent; + int devlink_final; + int owner_final; + int group_final; + int mode_final; + char tmp_node[UTIL_PATH_SIZE]; + char program_result[UTIL_PATH_SIZE]; + int run_final; + + char name[UTIL_PATH_SIZE]; + mode_t mode; + char owner[UTIL_NAME_SIZE]; + char group[UTIL_NAME_SIZE]; + struct udev_list_node run_list; + int ignore_device; + int test; + + struct list_head node; + pid_t pid; + int exitstatus; + time_t queue_time; +}; +extern struct udev_event *udev_event_new(struct udev_device *dev); +extern void udev_event_unref(struct udev_event *event); +extern int udev_event_run(struct udev_event *event, struct udev_rules *rules); + +/* udev-node.c */ +extern int udev_node_mknod(struct udev_device *dev, const char *file, dev_t devnum, mode_t mode, uid_t uid, gid_t gid); +extern int udev_node_add(struct udev_device *dev, mode_t mode, const char *owner, const char *group, int test); +extern int udev_node_remove(struct udev_device *dev, int test); +extern void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old, int test); + +/* udev-device-db.c */ +extern int udev_device_update_db(struct udev_device *udev_device); +extern int udev_device_delete_db(struct udev_device *udev_device); +extern int udev_device_rename_db(struct udev_device *udev_device, const char *devpath); + +/* udev-util.c */ struct name_entry { struct list_head node; char name[UTIL_PATH_SIZE]; @@ -167,7 +128,7 @@ extern int file_map(const char *filename, char **buf, size_t *bufsize); extern void file_unmap(void *buf, size_t bufsize); extern size_t buf_get_line(const char *buf, size_t buflen, size_t cur); -/* udev_selinux */ +/* udev-selinux.c */ #ifndef USE_SELINUX static inline void selinux_init(struct udev *udev) {} static inline void selinux_exit(struct udev *udev) {} @@ -189,5 +150,4 @@ extern int udevadm_control(struct udev *udev, int argc, char *argv[]); extern int udevadm_trigger(struct udev *udev, int argc, char *argv[]); extern int udevadm_settle(struct udev *udev, int argc, char *argv[]); extern int udevadm_test(struct udev *udev, int argc, char *argv[]); - #endif diff --git a/udev/udev_db.c b/udev/udev_db.c index e03c2782a..17e989310 100644 --- a/udev/udev_db.c +++ b/udev/udev_db.c @@ -1,6 +1,5 @@ /* - * Copyright (C) 2003 Greg Kroah-Hartman - * Copyright (C) 2004-2008 Kay Sievers + * Copyright (C) 2008 Kay Sievers * * 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 @@ -23,10 +22,7 @@ #include #include #include -#include -#include #include -#include #include "udev.h" @@ -41,282 +37,99 @@ static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *f return util_path_encode(&filename[start], len - start); } -/* reverse mapping from the device file name to the devpath */ -static int name_index(struct udev *udev, const char *devpath, const char *name, int add) -{ - char device[UTIL_PATH_SIZE]; - char filename[UTIL_PATH_SIZE * 2]; - size_t start; - int fd; - - /* directory with device name */ - util_strlcpy(filename, udev_get_dev_path(udev), sizeof(filename)); - start = util_strlcat(filename, "/.udev/names/", sizeof(filename)); - util_strlcat(filename, name, sizeof(filename)); - util_path_encode(&filename[start], sizeof(filename) - start); - /* entry with the devpath */ - util_strlcpy(device, devpath, sizeof(device)); - util_path_encode(device, sizeof(device)); - util_strlcat(filename, "/", sizeof(filename)); - util_strlcat(filename, device, sizeof(filename)); - - if (add) { - info(udev, "creating index: '%s'\n", filename); - create_path(udev, filename); - fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644); - if (fd > 0) - close(fd); - } else { - info(udev, "removing index: '%s'\n", filename); - unlink(filename); - delete_path(udev, filename); - } - return 0; -} - -int udev_db_get_devices_by_name(struct udev *udev, const char *name, struct list_head *name_list) -{ - char dirname[PATH_MAX]; - size_t start; - DIR *dir; - int rc = 0; - - util_strlcpy(dirname, udev_get_dev_path(udev), sizeof(dirname)); - start = util_strlcat(dirname, "/.udev/names/", sizeof(dirname)); - util_strlcat(dirname, name, sizeof(dirname)); - util_path_encode(&dirname[start], sizeof(dirname) - start); - - dir = opendir(dirname); - if (dir == NULL) { - info(udev, "no index directory '%s': %m\n", dirname); - rc = -1; - goto out; - } - - info(udev, "found index directory '%s'\n", dirname); - while (1) { - struct dirent *ent; - char device[UTIL_PATH_SIZE]; - - ent = readdir(dir); - if (ent == NULL || ent->d_name[0] == '\0') - break; - if (ent->d_name[0] == '.') - continue; - - util_strlcpy(device, ent->d_name, sizeof(device)); - util_path_decode(device); - name_list_add(udev, name_list, device, 0); - rc++; - } - closedir(dir); -out: - return rc; -} - -int udev_db_rename(struct udev *udev, const char *devpath_old, const char *devpath) -{ - char filename[UTIL_PATH_SIZE]; - char filename_old[UTIL_PATH_SIZE]; - - devpath_to_db_path(udev, devpath_old, filename_old, sizeof(filename_old)); - devpath_to_db_path(udev, devpath, filename, sizeof(filename)); - return rename(filename_old, filename); -} - -int udev_db_add_device(struct udevice *udevice) +int udev_device_update_db(struct udev_device *udev_device) { + struct udev *udev = udev_device_get_udev(udev_device); char filename[UTIL_PATH_SIZE]; FILE *f; - struct name_entry *name_loop; char target[232]; /* on 64bit, tmpfs inlines up to 239 bytes */ + size_t devlen = strlen(udev_get_dev_path(udev))+1; + struct udev_list_entry *list_entry; int ret; - if (udevice->test_run) - return 0; - - devpath_to_db_path(udevice->udev, udevice->dev->devpath, filename, sizeof(filename)); - create_path(udevice->udev, filename); + devpath_to_db_path(udev, + udev_device_get_devpath(udev_device), + filename, sizeof(filename)); + create_path(udev, filename); unlink(filename); - if (!list_empty(&udevice->env_list)) + udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) + if (udev_list_entry_get_flag(list_entry)) + goto file; + if (udev_device_get_num_fake_partitions(udev_device)) goto file; - if (udevice->partitions || udevice->ignore_remove) + if (udev_device_get_ignore_remove(udev_device)) goto file; - /* try not to waste tmpfs memory, store values, if they fit, in a symlink target */ - util_strlcpy(target, udevice->name, sizeof(target)); - list_for_each_entry(name_loop, &udevice->symlink_list, node) { + /* try not to waste tmpfs memory; store values, if they fit, in a symlink target */ + util_strlcpy(target, &udev_device_get_devnode(udev_device)[devlen], sizeof(target)); + udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device)) { size_t len; util_strlcat(target, " ", sizeof(target)); - len = util_strlcat(target, name_loop->name, sizeof(target)); + len = util_strlcat(target, &udev_list_entry_get_name(list_entry)[devlen], sizeof(target)); if (len >= sizeof(target)) { - info(udevice->udev, "size of links too large, create file\n"); + info(udev, "size of links too large, create file\n"); goto file; } } - /* add symlink names to index */ - list_for_each_entry(name_loop, &udevice->symlink_list, node) { - name_index(udevice->udev, udevice->dev->devpath, name_loop->name, 1); - } - info(udevice->udev, "create db link (%s)\n", target); - udev_selinux_setfscreatecon(udevice->udev, filename, S_IFLNK); + info(udev, "create db link (%s)\n", target); + udev_selinux_setfscreatecon(udev, filename, S_IFLNK); ret = symlink(target, filename); - udev_selinux_resetfscreatecon(udevice->udev); + udev_selinux_resetfscreatecon(udev); if (ret == 0) goto out; file: f = fopen(filename, "w"); if (f == NULL) { - err(udevice->udev, "unable to create db file '%s': %m\n", filename); + err(udev, "unable to create db file '%s': %m\n", filename); return -1; } - info(udevice->udev, "created db file for '%s' in '%s'\n", udevice->dev->devpath, filename); - - fprintf(f, "N:%s\n", udevice->name); - list_for_each_entry(name_loop, &udevice->symlink_list, node) { - fprintf(f, "S:%s\n", name_loop->name); - /* add symlink names to index */ - name_index(udevice->udev, udevice->dev->devpath, name_loop->name, 1); + info(udev, "created db file for '%s' in '%s'\n", udev_device_get_devpath(udev_device), filename); + + fprintf(f, "N:%s\n", &udev_device_get_devnode(udev_device)[devlen]); + udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device)) + fprintf(f, "S:%s\n", &udev_list_entry_get_name(list_entry)[devlen]); + if (udev_device_get_devlink_priority(udev_device) != 0) + fprintf(f, "L:%u\n", udev_device_get_devlink_priority(udev_device)); + if (udev_device_get_event_timeout(udev_device) >= 0) + fprintf(f, "T:%u\n", udev_device_get_event_timeout(udev_device)); + if (udev_device_get_num_fake_partitions(udev_device) != 0) + fprintf(f, "A:%u\n", udev_device_get_num_fake_partitions(udev_device)); + if (udev_device_get_ignore_remove(udev_device)) + fprintf(f, "R:%u\n", udev_device_get_ignore_remove(udev_device)); + udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) { + if (!udev_list_entry_get_flag(list_entry)) + continue; + fprintf(f, "E:%s=%s\n", + udev_list_entry_get_name(list_entry), + udev_list_entry_get_value(list_entry)); } - fprintf(f, "M:%u:%u\n", major(udevice->devt), minor(udevice->devt)); - if (udevice->link_priority != 0) - fprintf(f, "L:%u\n", udevice->link_priority); - if (udevice->event_timeout >= 0) - fprintf(f, "T:%u\n", udevice->event_timeout); - if (udevice->partitions != 0) - fprintf(f, "A:%u\n", udevice->partitions); - if (udevice->ignore_remove) - fprintf(f, "R:%u\n", udevice->ignore_remove); - list_for_each_entry(name_loop, &udevice->env_list, node) - fprintf(f, "E:%s\n", name_loop->name); fclose(f); out: - /* add name to index */ - name_index(udevice->udev, udevice->dev->devpath, udevice->name, 1); return 0; } -int udev_db_get_device(struct udevice *udevice, const char *devpath) +int udev_device_delete_db(struct udev_device *udev_device) { - struct stat stats; char filename[UTIL_PATH_SIZE]; - char line[UTIL_PATH_SIZE]; - unsigned int maj, min; - char *bufline; - char *buf; - size_t bufsize; - size_t cur; - size_t count; - - sysfs_device_set_values(udevice->udev, udevice->dev, devpath, NULL, NULL); - devpath_to_db_path(udevice->udev, devpath, filename, sizeof(filename)); - - if (lstat(filename, &stats) != 0) { - info(udevice->udev, "no db file to read %s: %m\n", filename); - return -1; - } - if ((stats.st_mode & S_IFMT) == S_IFLNK) { - char target[UTIL_NAME_SIZE]; - int target_len; - char *next; - - info(udevice->udev, "found db symlink\n"); - target_len = readlink(filename, target, sizeof(target)); - if (target_len > 0) - target[target_len] = '\0'; - else { - info(udevice->udev, "error reading db link %s: %m\n", filename); - return -1; - } - next = strchr(target, ' '); - if (next != NULL) { - next[0] = '\0'; - next = &next[1]; - } - info(udevice->udev, "got db link node: '%s'\n", target); - util_strlcpy(udevice->name, target, sizeof(udevice->name)); - while (next != NULL) { - char *lnk; - - lnk = next; - next = strchr(next, ' '); - if (next != NULL) { - next[0] = '\0'; - next = &next[1]; - } - info(udevice->udev, "got db link link: '%s'\n", lnk); - name_list_add(udevice->udev, &udevice->symlink_list, lnk, 0); - } - return 0; - } - - if (file_map(filename, &buf, &bufsize) != 0) { - info(udevice->udev, "error reading db file %s: %m\n", filename); - return -1; - } - - cur = 0; - while (cur < bufsize) { - count = buf_get_line(buf, bufsize, cur); - bufline = &buf[cur]; - cur += count+1; - - if (count > sizeof(line)) - count = sizeof(line); - memcpy(line, &bufline[2], count-2); - line[count-2] = '\0'; - - switch(bufline[0]) { - case 'N': - util_strlcpy(udevice->name, line, sizeof(udevice->name)); - break; - case 'M': - sscanf(line, "%u:%u", &maj, &min); - udevice->devt = makedev(maj, min); - break; - case 'S': - name_list_add(udevice->udev, &udevice->symlink_list, line, 0); - break; - case 'L': - udevice->link_priority = atoi(line); - break; - case 'T': - udevice->event_timeout = atoi(line); - break; - case 'A': - udevice->partitions = atoi(line); - break; - case 'R': - udevice->ignore_remove = atoi(line); - break; - case 'E': - name_list_add(udevice->udev, &udevice->env_list, line, 0); - break; - } - } - file_unmap(buf, bufsize); - - if (udevice->name[0] == '\0') - return -1; + devpath_to_db_path(udev_device_get_udev(udev_device), + udev_device_get_devpath(udev_device), + filename, sizeof(filename)); + unlink(filename); return 0; } -int udev_db_delete_device(struct udevice *udevice) +int udev_device_rename_db(struct udev_device *udev_device, const char *devpath_old) { + char filename_old[UTIL_PATH_SIZE]; char filename[UTIL_PATH_SIZE]; - struct name_entry *name_loop; - - if (udevice->test_run) - return 0; - - devpath_to_db_path(udevice->udev, udevice->dev->devpath, filename, sizeof(filename)); - unlink(filename); - name_index(udevice->udev, udevice->dev->devpath, udevice->name, 0); - list_for_each_entry(name_loop, &udevice->symlink_list, node) - name_index(udevice->udev, udevice->dev->devpath, name_loop->name, 0); - - return 0; + devpath_to_db_path(udev_device_get_udev(udev_device), + devpath_old, + filename_old, sizeof(filename_old)); + devpath_to_db_path(udev_device_get_udev(udev_device), + udev_device_get_devpath(udev_device), + filename, sizeof(filename)); + return rename(filename_old, filename); } diff --git a/udev/udev_device.c b/udev/udev_device.c deleted file mode 100644 index 751b7cd91..000000000 --- a/udev/udev_device.c +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright (C) 2004-2008 Kay Sievers - * - * 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 . - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "udev.h" -#include "udev_rules.h" - - -struct udevice *udev_device_init(struct udev *udev) -{ - struct udevice *udevice; - - udevice = malloc(sizeof(struct udevice)); - if (udevice == NULL) - return NULL; - memset(udevice, 0x00, sizeof(struct udevice)); - - udevice->udev = udev; - - INIT_LIST_HEAD(&udevice->symlink_list); - INIT_LIST_HEAD(&udevice->run_list); - INIT_LIST_HEAD(&udevice->env_list); - - /* set sysfs device to local storage, can be overridden if needed */ - udevice->dev = &udevice->dev_local; - - /* default node permissions */ - udevice->mode = 0660; - strcpy(udevice->owner, "root"); - strcpy(udevice->group, "root"); - - udevice->event_timeout = -1; - return udevice; -} - -void udev_device_cleanup(struct udevice *udevice) -{ - if (udevice == NULL) - return; - name_list_cleanup(udevice->udev, &udevice->symlink_list); - name_list_cleanup(udevice->udev, &udevice->run_list); - name_list_cleanup(udevice->udev, &udevice->env_list); - free(udevice); -} - -dev_t udev_device_get_devt(struct udevice *udevice) -{ - const char *attr; - unsigned int maj, min; - - /* read it from sysfs */ - attr = sysfs_attr_get_value(udevice->udev, udevice->dev->devpath, "dev"); - if (attr != NULL) { - if (sscanf(attr, "%u:%u", &maj, &min) == 2) - return makedev(maj, min); - } - return makedev(0, 0); -} diff --git a/udev/udev_device_event.c b/udev/udev_device_event.c index 4408400ff..e5d716085 100644 --- a/udev/udev_device_event.c +++ b/udev/udev_device_event.c @@ -31,6 +31,33 @@ #include "udev.h" #include "udev_rules.h" +struct udev_event *udev_event_new(struct udev_device *dev) +{ + struct udev_event *event; + + event = malloc(sizeof(struct udev_event)); + if (event == NULL) + return NULL; + memset(event, 0x00, sizeof(struct udev_event)); + + event->dev = dev; + event->udev = udev_device_get_udev(dev); + udev_list_init(&event->run_list); + event->mode = 0660; + util_strlcpy(event->owner, "0", sizeof(event->owner)); + util_strlcpy(event->group, "0", sizeof(event->group)); + + dbg(event->udev, "allocated event %p\n", event); + return event; +} + +void udev_event_unref(struct udev_event *event) +{ + udev_list_cleanup(event->udev, &event->run_list); + dbg(event->udev, "free event %p\n", event); + free(event); +} + static void kernel_log(struct ifreq ifr) { int klog; @@ -51,209 +78,209 @@ static void kernel_log(struct ifreq ifr) fclose(f); } -static int rename_netif(struct udevice *udevice) +static int rename_netif(struct udev_event *event) { + struct udev_device *dev = event->dev; int sk; struct ifreq ifr; - int retval; + int err; - info(udevice->udev, "changing net interface name from '%s' to '%s'\n", udevice->dev->kernel, udevice->name); - if (udevice->test_run) + info(event->udev, "changing net interface name from '%s' to '%s'\n", + udev_device_get_sysname(dev), event->name); + if (event->test) return 0; sk = socket(PF_INET, SOCK_DGRAM, 0); if (sk < 0) { - err(udevice->udev, "error opening socket: %m\n"); + err(event->udev, "error opening socket: %m\n"); return -1; } memset(&ifr, 0x00, sizeof(struct ifreq)); - util_strlcpy(ifr.ifr_name, udevice->dev->kernel, IFNAMSIZ); - util_strlcpy(ifr.ifr_newname, udevice->name, IFNAMSIZ); - retval = ioctl(sk, SIOCSIFNAME, &ifr); - if (retval == 0) + util_strlcpy(ifr.ifr_name, udev_device_get_sysname(dev), IFNAMSIZ); + util_strlcpy(ifr.ifr_newname, event->name, IFNAMSIZ); + err = ioctl(sk, SIOCSIFNAME, &ifr); + if (err == 0) kernel_log(ifr); else { int loop; /* see if the destination interface name already exists */ if (errno != EEXIST) { - err(udevice->udev, "error changing netif name %s to %s: %m\n", + err(event->udev, "error changing netif name %s to %s: %m\n", ifr.ifr_name, ifr.ifr_newname); goto exit; } /* free our own name, another process may wait for us */ - util_strlcpy(ifr.ifr_newname, udevice->dev->kernel, IFNAMSIZ); + util_strlcpy(ifr.ifr_newname, udev_device_get_sysname(dev), IFNAMSIZ); util_strlcat(ifr.ifr_newname, "_rename", IFNAMSIZ); - retval = ioctl(sk, SIOCSIFNAME, &ifr); - if (retval != 0) { - err(udevice->udev, "error changing netif name %s to %s: %m\n", + err = ioctl(sk, SIOCSIFNAME, &ifr); + if (err != 0) { + err(event->udev, "error changing netif name %s to %s: %m\n", ifr.ifr_name, ifr.ifr_newname); goto exit; } /* wait 30 seconds for our target to become available */ util_strlcpy(ifr.ifr_name, ifr.ifr_newname, IFNAMSIZ); - util_strlcpy(ifr.ifr_newname, udevice->name, IFNAMSIZ); + util_strlcpy(ifr.ifr_newname, udev_device_get_devnode(dev), IFNAMSIZ); loop = 30 * 20; while (loop--) { - retval = ioctl(sk, SIOCSIFNAME, &ifr); - if (retval == 0) { + err = ioctl(sk, SIOCSIFNAME, &ifr); + if (err == 0) { kernel_log(ifr); break; } if (errno != EEXIST) { - err(udevice->udev, "error changing net interface name %s to %s: %m\n", + err(event->udev, "error changing net interface name %s to %s: %m\n", ifr.ifr_name, ifr.ifr_newname); break; } - dbg(udevice->udev, "wait for netif '%s' to become free, loop=%i\n", - udevice->name, (30 * 20) - loop); + dbg(event->udev, "wait for netif '%s' to become free, loop=%i\n", + udev_device_get_devnode(dev), (30 * 20) - loop); usleep(1000 * 1000 / 20); } } exit: close(sk); - return retval; + return err; } -int udev_device_event(struct udev_rules *rules, struct udevice *udevice) +int udev_event_run(struct udev_event *event, struct udev_rules *rules) { - int retval = 0; + struct udev_device *dev = event->dev; + int err = 0; - if (udevice->devpath_old != NULL) - if (udev_db_rename(udevice->udev, udevice->devpath_old, udevice->dev->devpath) == 0) - info(udevice->udev, "moved database from '%s' to '%s'\n", udevice->devpath_old, udevice->dev->devpath); + if (udev_device_get_devpath_old(dev) != NULL) { + if (udev_device_rename_db(dev, udev_device_get_devpath(dev)) == 0) + info(event->udev, "moved database from '%s' to '%s'\n", + udev_device_get_devpath_old(dev), udev_device_get_devpath(dev)); + } /* add device node */ - if (major(udevice->devt) != 0 && - (strcmp(udevice->action, "add") == 0 || strcmp(udevice->action, "change") == 0)) { - struct udevice *udevice_old; + if (major(udev_device_get_devnum(dev)) != 0 && + (strcmp(udev_device_get_action(dev), "add") == 0 || strcmp(udev_device_get_action(dev), "change") == 0)) { + char filename[UTIL_PATH_SIZE]; + struct udev_device *dev_old; - dbg(udevice->udev, "device node add '%s'\n", udevice->dev->devpath); + dbg(event->udev, "device node add '%s'\n", udev_device_get_devpath(dev)); - udev_rules_get_name(rules, udevice); - if (udevice->ignore_device) { - info(udevice->udev, "device event will be ignored\n"); + udev_rules_get_name(rules, event); + if (event->ignore_device) { + info(event->udev, "device event will be ignored\n"); goto exit; } - if (udevice->name[0] == '\0') { - info(udevice->udev, "device node creation supressed\n"); + + if (event->name[0] == '\0') { + info(event->udev, "device node creation supressed\n"); goto exit; } + /* set device node name */ + util_strlcpy(filename, udev_get_dev_path(event->udev), sizeof(filename)); + util_strlcat(filename, "/", sizeof(filename)); + util_strlcat(filename, event->name, sizeof(filename)); + udev_device_set_devnode(dev, filename); + /* read current database entry; cleanup, if it is known device */ - udevice_old = udev_device_init(udevice->udev); - if (udevice_old != NULL) { - udevice_old->test_run = udevice->test_run; - if (udev_db_get_device(udevice_old, udevice->dev->devpath) == 0) { - info(udevice->udev, "device '%s' already in database, cleanup\n", udevice->dev->devpath); - udev_db_delete_device(udevice_old); - } else { - udev_device_cleanup(udevice_old); - udevice_old = NULL; - } + dev_old = udev_device_new_from_syspath(event->udev, udev_device_get_syspath(dev)); + if (dev_old != NULL) { + info(event->udev, "device '%s' already in database, updating\n", + udev_device_get_devpath(dev)); + udev_node_update_old_links(dev, dev_old, event->test); + udev_device_unref(dev_old); } - /* create node */ - retval = udev_node_add(udevice); - if (retval != 0) - goto exit; - - /* store in database */ - udev_db_add_device(udevice); + udev_device_update_db(dev); - /* create, replace, delete symlinks according to priority */ - udev_node_update_symlinks(udevice, udevice_old); + err = udev_node_add(dev, event->mode, event->owner, event->group, event->test); + if (err != 0) + goto exit; - if (udevice_old != NULL) - udev_device_cleanup(udevice_old); goto exit; } /* add netif */ - if (strcmp(udevice->dev->subsystem, "net") == 0 && strcmp(udevice->action, "add") == 0) { - dbg(udevice->udev, "netif add '%s'\n", udevice->dev->devpath); - udev_rules_get_name(rules, udevice); - if (udevice->ignore_device) { - info(udevice->udev, "device event will be ignored\n"); + if (strcmp(udev_device_get_subsystem(dev), "net") == 0 && strcmp(udev_device_get_action(dev), "add") == 0) { + dbg(event->udev, "netif add '%s'\n", udev_device_get_devpath(dev)); + + udev_rules_get_name(rules, event); + if (event->ignore_device) { + info(event->udev, "device event will be ignored\n"); goto exit; } - if (udevice->name[0] == '\0') { - info(udevice->udev, "device renaming supressed\n"); + if (event->name[0] == '\0') { + info(event->udev, "device renaming supressed\n"); goto exit; } /* look if we want to change the name of the netif */ - if (strcmp(udevice->name, udevice->dev->kernel) != 0) { - char devpath[PATH_MAX]; + if (strcmp(event->name, udev_device_get_sysname(dev)) != 0) { + char syspath[UTIL_PATH_SIZE]; char *pos; - retval = rename_netif(udevice); - if (retval != 0) + err = rename_netif(event); + if (err != 0) goto exit; - info(udevice->udev, "renamed netif to '%s'\n", udevice->name); + info(event->udev, "renamed netif to '%s'\n", event->name); - /* export old name */ - setenv("INTERFACE_OLD", udevice->dev->kernel, 1); + /* remember old name */ + udev_device_add_property(dev, "INTERFACE_OLD", udev_device_get_sysname(dev)); /* now change the devpath, because the kernel device name has changed */ - util_strlcpy(devpath, udevice->dev->devpath, sizeof(devpath)); - pos = strrchr(devpath, '/'); + util_strlcpy(syspath, udev_device_get_syspath(dev), sizeof(syspath)); + pos = strrchr(syspath, '/'); if (pos != NULL) { pos[1] = '\0'; - util_strlcat(devpath, udevice->name, sizeof(devpath)); - sysfs_device_set_values(udevice->udev, udevice->dev, devpath, NULL, NULL); - setenv("DEVPATH", udevice->dev->devpath, 1); - setenv("INTERFACE", udevice->name, 1); - info(udevice->udev, "changed devpath to '%s'\n", udevice->dev->devpath); + util_strlcat(syspath, event->name, sizeof(syspath)); + udev_device_set_syspath(event->dev, syspath); + udev_device_add_property(dev, "INTERFACE", udev_device_get_sysname(dev)); + info(event->udev, "changed devpath to '%s'\n", udev_device_get_devpath(dev)); } } goto exit; } /* remove device node */ - if (major(udevice->devt) != 0 && strcmp(udevice->action, "remove") == 0) { - struct name_entry *name_loop; - - /* import database entry, and delete it */ - if (udev_db_get_device(udevice, udevice->dev->devpath) == 0) { - udev_db_delete_device(udevice); - /* restore stored persistent data */ - list_for_each_entry(name_loop, &udevice->env_list, node) - putenv(name_loop->name); - } else { - dbg(udevice->udev, "'%s' not found in database, using kernel name '%s'\n", - udevice->dev->devpath, udevice->dev->kernel); - util_strlcpy(udevice->name, udevice->dev->kernel, sizeof(udevice->name)); + if (major(udev_device_get_devnum(dev)) != 0 && strcmp(udev_device_get_action(dev), "remove") == 0) { + /* import database entry and delete it */ + udev_device_read_db(dev); + if (!event->test) + udev_device_delete_db(dev); + + if (udev_device_get_devnode(dev) == NULL) { + char devnode[UTIL_PATH_SIZE]; + + info(event->udev, "'%s' not found in database, using kernel name '%s'\n", + udev_device_get_syspath(dev), udev_device_get_sysname(dev)); + util_strlcpy(devnode, udev_get_dev_path(event->udev), sizeof(devnode)); + util_strlcat(devnode, "/", sizeof(devnode)); + util_strlcat(devnode, udev_device_get_sysname(dev), sizeof(devnode)); + udev_device_set_devnode(dev, devnode); } - udev_rules_get_run(rules, udevice); - if (udevice->ignore_device) { - info(udevice->udev, "device event will be ignored\n"); + udev_rules_get_run(rules, event); + if (event->ignore_device) { + info(event->udev, "device event will be ignored\n"); goto exit; } - if (udevice->ignore_remove) { - info(udevice->udev, "ignore_remove for '%s'\n", udevice->name); + if (udev_device_get_ignore_remove(dev)) { + info(event->udev, "ignore_remove for '%s'\n", udev_device_get_devnode(dev)); goto exit; } - /* remove the node */ - retval = udev_node_remove(udevice); - /* delete or restore symlinks according to priority */ - udev_node_update_symlinks(udevice, NULL); + err = udev_node_remove(dev, event->test); goto exit; } /* default devices */ - udev_rules_get_run(rules, udevice); - if (udevice->ignore_device) - info(udevice->udev, "device event will be ignored\n"); - + udev_rules_get_run(rules, event); + if (event->ignore_device) + info(event->udev, "device event will be ignored\n"); exit: - return retval; + return err; } diff --git a/udev/udev_node.c b/udev/udev_node.c index a43e5bd63..425764242 100644 --- a/udev/udev_node.c +++ b/udev/udev_node.c @@ -32,68 +32,108 @@ #define TMP_FILE_EXT ".udev-tmp" -int udev_node_mknod(struct udevice *udevice, const char *file, dev_t devt, mode_t mode, uid_t uid, gid_t gid) +/* reverse mapping from the device file name to the devpath */ +static int name_index(struct udev *udev, const char *devpath, const char *name, int add, int test) { + char device[UTIL_PATH_SIZE]; + char filename[UTIL_PATH_SIZE * 2]; + size_t devlen = strlen(udev_get_dev_path(udev))+1; + size_t start; + int fd; + + /* directory with device name */ + util_strlcpy(filename, udev_get_dev_path(udev), sizeof(filename)); + start = util_strlcat(filename, "/.udev/names/", sizeof(filename)); + util_strlcat(filename, &name[devlen], sizeof(filename)); + util_path_encode(&filename[start], sizeof(filename) - start); + /* entry with the devpath */ + util_strlcpy(device, devpath, sizeof(device)); + util_path_encode(device, sizeof(device)); + util_strlcat(filename, "/", sizeof(filename)); + util_strlcat(filename, device, sizeof(filename)); + + if (add) { + info(udev, "creating index: '%s'\n", filename); + create_path(udev, filename); + fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644); + if (fd > 0) + close(fd); + } else { + info(udev, "removing index: '%s'\n", filename); + unlink(filename); + delete_path(udev, filename); + } + return 0; +} + +int udev_node_mknod(struct udev_device *dev, const char *file, dev_t devnum, mode_t mode, uid_t uid, gid_t gid) +{ + struct udev *udev = udev_device_get_udev(dev); char file_tmp[UTIL_PATH_SIZE + sizeof(TMP_FILE_EXT)]; struct stat stats; int preserve = 0; int err = 0; - if (major(devt) != 0 && strcmp(udevice->dev->subsystem, "block") == 0) + if (major(devnum) == 0) + devnum = udev_device_get_devnum(dev); + + if (strcmp(udev_device_get_subsystem(dev), "block") == 0) mode |= S_IFBLK; else mode |= S_IFCHR; + if (file == NULL) + file = udev_device_get_devnode(dev); + if (lstat(file, &stats) == 0) { - if (((stats.st_mode & S_IFMT) == (mode & S_IFMT)) && (stats.st_rdev == devt)) { - info(udevice->udev, "preserve file '%s', because it has correct dev_t\n", file); + if (((stats.st_mode & S_IFMT) == (mode & S_IFMT)) && (stats.st_rdev == devnum)) { + info(udev, "preserve file '%s', because it has correct dev_t\n", file); preserve = 1; - udev_selinux_lsetfilecon(udevice->udev, file, mode); + udev_selinux_lsetfilecon(udev, file, mode); } else { - info(udevice->udev, "atomically replace existing file '%s'\n", file); + info(udev, "atomically replace existing file '%s'\n", file); util_strlcpy(file_tmp, file, sizeof(file_tmp)); util_strlcat(file_tmp, TMP_FILE_EXT, sizeof(file_tmp)); unlink(file_tmp); - udev_selinux_setfscreatecon(udevice->udev, file_tmp, mode); - err = mknod(file_tmp, mode, devt); - udev_selinux_resetfscreatecon(udevice->udev); + udev_selinux_setfscreatecon(udev, file_tmp, mode); + err = mknod(file_tmp, mode, devnum); + udev_selinux_resetfscreatecon(udev); if (err != 0) { - err(udevice->udev, "mknod(%s, %#o, %u, %u) failed: %m\n", - file_tmp, mode, major(devt), minor(devt)); + err(udev, "mknod(%s, %#o, %u, %u) failed: %m\n", + file_tmp, mode, major(devnum), minor(devnum)); goto exit; } err = rename(file_tmp, file); if (err != 0) { - err(udevice->udev, "rename(%s, %s) failed: %m\n", file_tmp, file); + err(udev, "rename(%s, %s) failed: %m\n", file_tmp, file); unlink(file_tmp); } } } else { - info(udevice->udev, "mknod(%s, %#o, (%u,%u))\n", file, mode, major(devt), minor(devt)); - udev_selinux_setfscreatecon(udevice->udev, file, mode); - err = mknod(file, mode, devt); - udev_selinux_resetfscreatecon(udevice->udev); + info(udev, "mknod(%s, %#o, (%u,%u))\n", file, mode, major(devnum), minor(devnum)); + udev_selinux_setfscreatecon(udev, file, mode); + err = mknod(file, mode, devnum); + udev_selinux_resetfscreatecon(udev); if (err != 0) { - err(udevice->udev, "mknod(%s, %#o, (%u,%u) failed: %m\n", - file, mode, major(devt), minor(devt)); + err(udev, "mknod(%s, %#o, (%u,%u) failed: %m\n", file, mode, major(devnum), minor(devnum)); goto exit; } } if (!preserve || stats.st_mode != mode) { - info(udevice->udev, "chmod(%s, %#o)\n", file, mode); + info(udev, "chmod(%s, %#o)\n", file, mode); err = chmod(file, mode); if (err != 0) { - err(udevice->udev, "chmod(%s, %#o) failed: %m\n", file, mode); + err(udev, "chmod(%s, %#o) failed: %m\n", file, mode); goto exit; } } if (!preserve || stats.st_uid != uid || stats.st_gid != gid) { - info(udevice->udev, "chown(%s, %u, %u)\n", file, uid, gid); + info(udev, "chown(%s, %u, %u)\n", file, uid, gid); err = chown(file, uid, gid); if (err != 0) { - err(udevice->udev, "chown(%s, %u, %u) failed: %m\n", file, uid, gid); + err(udev, "chown(%s, %u, %u) failed: %m\n", file, uid, gid); goto exit; } } @@ -101,7 +141,7 @@ exit: return err; } -static int node_symlink(struct udevice *udevice, const char *node, const char *slink) +static int node_symlink(struct udev *udev, const char *node, const char *slink) { struct stat stats; char target[UTIL_PATH_SIZE] = ""; @@ -109,7 +149,7 @@ static int node_symlink(struct udevice *udevice, const char *node, const char *s int i = 0; int tail = 0; int len; - int retval = 0; + int err = 0; /* use relative link */ while (node[i] && (node[i] == slink[i])) { @@ -129,316 +169,357 @@ static int node_symlink(struct udevice *udevice, const char *node, const char *s if (S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode)) { struct stat stats2; - info(udevice->udev, "found existing node instead of symlink '%s'\n", slink); + info(udev, "found existing node instead of symlink '%s'\n", slink); if (lstat(node, &stats2) == 0) { if ((stats.st_mode & S_IFMT) == (stats2.st_mode & S_IFMT) && stats.st_rdev == stats2.st_rdev) { - info(udevice->udev, "replace device node '%s' with symlink to our node '%s'\n", slink, node); + info(udev, "replace device node '%s' with symlink to our node '%s'\n", + slink, node); } else { - err(udevice->udev, "device node '%s' already exists, link to '%s' will not overwrite it\n", slink, node); + err(udev, "device node '%s' already exists, " + "link to '%s' will not overwrite it\n", + slink, node); goto exit; } } } else if (S_ISLNK(stats.st_mode)) { char buf[UTIL_PATH_SIZE]; - info(udevice->udev, "found existing symlink '%s'\n", slink); + info(udev, "found existing symlink '%s'\n", slink); len = readlink(slink, buf, sizeof(buf)); if (len > 0) { buf[len] = '\0'; if (strcmp(target, buf) == 0) { - info(udevice->udev, "preserve already existing symlink '%s' to '%s'\n", slink, target); - udev_selinux_lsetfilecon(udevice->udev, slink, S_IFLNK); + info(udev, "preserve already existing symlink '%s' to '%s'\n", + slink, target); + udev_selinux_lsetfilecon(udev, slink, S_IFLNK); goto exit; } } } } else { - info(udevice->udev, "creating symlink '%s' to '%s'\n", slink, target); - udev_selinux_setfscreatecon(udevice->udev, slink, S_IFLNK); - retval = symlink(target, slink); - udev_selinux_resetfscreatecon(udevice->udev); - if (retval == 0) + info(udev, "creating symlink '%s' to '%s'\n", slink, target); + udev_selinux_setfscreatecon(udev, slink, S_IFLNK); + err = symlink(target, slink); + udev_selinux_resetfscreatecon(udev); + if (err == 0) goto exit; } - info(udevice->udev, "atomically replace '%s'\n", slink); + info(udev, "atomically replace '%s'\n", slink); util_strlcpy(slink_tmp, slink, sizeof(slink_tmp)); util_strlcat(slink_tmp, TMP_FILE_EXT, sizeof(slink_tmp)); unlink(slink_tmp); - udev_selinux_setfscreatecon(udevice->udev, slink, S_IFLNK); - retval = symlink(target, slink_tmp); - udev_selinux_resetfscreatecon(udevice->udev); - if (retval != 0) { - err(udevice->udev, "symlink(%s, %s) failed: %m\n", target, slink_tmp); + udev_selinux_setfscreatecon(udev, slink, S_IFLNK); + err = symlink(target, slink_tmp); + udev_selinux_resetfscreatecon(udev); + if (err != 0) { + err(udev, "symlink(%s, %s) failed: %m\n", target, slink_tmp); goto exit; } - retval = rename(slink_tmp, slink); - if (retval != 0) { - err(udevice->udev, "rename(%s, %s) failed: %m\n", slink_tmp, slink); + err = rename(slink_tmp, slink); + if (err != 0) { + err(udev, "rename(%s, %s) failed: %m\n", slink_tmp, slink); unlink(slink_tmp); goto exit; } exit: - return retval; + return err; } -static int update_link(struct udevice *udevice, const char *name) +static int get_devices_by_name(struct udev *udev, const char *name, struct list_head *name_list) { + char dirname[PATH_MAX]; + size_t devlen = strlen(udev_get_dev_path(udev))+1; + size_t start; + DIR *dir; + int count = 0; + + util_strlcpy(dirname, udev_get_dev_path(udev), sizeof(dirname)); + start = util_strlcat(dirname, "/.udev/names/", sizeof(dirname)); + util_strlcat(dirname, &name[devlen], sizeof(dirname)); + util_path_encode(&dirname[start], sizeof(dirname) - start); + + dir = opendir(dirname); + if (dir == NULL) { + info(udev, "no index directory '%s': %m\n", dirname); + count = -1; + goto out; + } + + info(udev, "found index directory '%s'\n", dirname); + while (1) { + struct dirent *ent; + char device[UTIL_PATH_SIZE]; + + ent = readdir(dir); + if (ent == NULL || ent->d_name[0] == '\0') + break; + if (ent->d_name[0] == '.') + continue; + + util_strlcpy(device, udev_get_sys_path(udev), sizeof(device)); + util_strlcat(device, ent->d_name, sizeof(device)); + util_path_decode(device); + name_list_add(udev, name_list, device, 0); + count++; + } + closedir(dir); +out: + return count; +} + +static int update_link(struct udev_device *dev, const char *slink, int test) +{ + struct udev *udev = udev_device_get_udev(dev); LIST_HEAD(name_list); - char slink[UTIL_PATH_SIZE]; - char node[UTIL_PATH_SIZE]; - struct udevice *udevice_db; struct name_entry *device; char target[UTIL_PATH_SIZE] = ""; int count; int priority = 0; int rc = 0; - util_strlcpy(slink, udev_get_dev_path(udevice->udev), sizeof(slink)); - util_strlcat(slink, "/", sizeof(slink)); - util_strlcat(slink, name, sizeof(slink)); + info(udev, "update symlink '%s' of '%s'\n", slink, udev_device_get_syspath(dev)); - count = udev_db_get_devices_by_name(udevice->udev, name, &name_list); - info(udevice->udev, "found %i devices with name '%s'\n", count, name); + count = get_devices_by_name(udev, slink, &name_list); + info(udev, "found %i devices with name '%s'\n", count, slink); /* if we don't have a reference, delete it */ if (count <= 0) { - info(udevice->udev, "no reference left, remove '%s'\n", name); - if (!udevice->test_run) { + info(udev, "no reference left, remove '%s'\n", slink); + if (!test) { unlink(slink); - delete_path(udevice->udev, slink); + delete_path(udev, slink); } goto out; } /* find the device with the highest priority */ list_for_each_entry(device, &name_list, node) { - info(udevice->udev, "found '%s' for '%s'\n", device->name, name); + struct udev_device *dev_db; + const char *devnode; + + info(udev, "found '%s' for '%s'\n", device->name, slink); /* did we find ourself? we win, if we have the same priority */ - if (strcmp(udevice->dev->devpath, device->name) == 0) { - info(udevice->udev, "compare (our own) priority of '%s' %i >= %i\n", - udevice->dev->devpath, udevice->link_priority, priority); - if (strcmp(udevice->name, name) == 0) { - info(udevice->udev, "'%s' is our device node, database inconsistent, skip link update\n", udevice->name); - } else if (target[0] == '\0' || udevice->link_priority >= priority) { - priority = udevice->link_priority; - util_strlcpy(target, udevice->name, sizeof(target)); + if (strcmp(udev_device_get_syspath(dev), device->name) == 0) { + info(udev, "compare (our own) priority of '%s' %i >= %i\n", + udev_device_get_devpath(dev), udev_device_get_devlink_priority(dev), priority); + if (strcmp(udev_device_get_devnode(dev), slink) == 0) { + info(udev, "'%s' is our device node, database inconsistent, skip link update\n", + udev_device_get_devnode(dev)); + } else if (target[0] == '\0' || udev_device_get_devlink_priority(dev) >= priority) { + priority = udev_device_get_devlink_priority(dev); + util_strlcpy(target, udev_device_get_devnode(dev), sizeof(target)); } continue; } /* another device, read priority from database */ - udevice_db = udev_device_init(udevice->udev); - if (udevice_db == NULL) + dev_db = udev_device_new_from_syspath(udev, device->name); + if (dev_db == NULL) continue; - if (udev_db_get_device(udevice_db, device->name) == 0) { - if (strcmp(udevice_db->name, name) == 0) { - info(udevice->udev, "'%s' is a device node of '%s', skip link update\n", udevice_db->name, device->name); + devnode = udev_device_get_devnode(dev_db); + if (devnode != NULL) { + if (strcmp(devnode, slink) == 0) { + info(udev, "'%s' is a device node of '%s', skip link update\n", + devnode, device->name); } else { - info(udevice->udev, "compare priority of '%s' %i > %i\n", - udevice_db->dev->devpath, udevice_db->link_priority, priority); - if (target[0] == '\0' || udevice_db->link_priority > priority) { - priority = udevice_db->link_priority; - util_strlcpy(target, udevice_db->name, sizeof(target)); + info(udev, "compare priority of '%s' %i > %i\n", + udev_device_get_devpath(dev_db), + udev_device_get_devlink_priority(dev_db), + priority); + if (target[0] == '\0' || udev_device_get_devlink_priority(dev_db) > priority) { + priority = udev_device_get_devlink_priority(dev_db); + util_strlcpy(target, devnode, sizeof(target)); } } } - udev_device_cleanup(udevice_db); + udev_device_unref(dev_db); } - name_list_cleanup(udevice->udev, &name_list); + name_list_cleanup(udev, &name_list); if (target[0] == '\0') { - info(udevice->udev, "no current target for '%s' found\n", name); + info(udev, "no current target for '%s' found\n", slink); rc = 1; goto out; } /* create symlink to the target with the highest priority */ - util_strlcpy(node, udev_get_dev_path(udevice->udev), sizeof(node)); - util_strlcat(node, "/", sizeof(node)); - util_strlcat(node, target, sizeof(node)); - info(udevice->udev, "'%s' with target '%s' has the highest priority %i, create it\n", name, target, priority); - if (!udevice->test_run) { - create_path(udevice->udev, slink); - node_symlink(udevice, node, slink); + info(udev, "'%s' with target '%s' has the highest priority %i, create it\n", slink, target, priority); + if (!test) { + create_path(udev, slink); + node_symlink(udev, target, slink); } out: return rc; } -void udev_node_update_symlinks(struct udevice *udevice, struct udevice *udevice_old) +void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old, int test) { - struct name_entry *name_loop; - char symlinks[UTIL_PATH_SIZE] = ""; - - list_for_each_entry(name_loop, &udevice->symlink_list, node) { - info(udevice->udev, "update symlink '%s' of '%s'\n", name_loop->name, udevice->dev->devpath); - update_link(udevice, name_loop->name); - util_strlcat(symlinks, udev_get_dev_path(udevice->udev), sizeof(symlinks)); - util_strlcat(symlinks, "/", sizeof(symlinks)); - util_strlcat(symlinks, name_loop->name, sizeof(symlinks)); - util_strlcat(symlinks, " ", sizeof(symlinks)); + struct udev *udev = udev_device_get_udev(dev); + struct udev_list_entry *list_entry; + const char *devnode_old; + + /* update possible left-over symlinks */ + udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev_old)) { + struct udev_list_entry *list_entry_current; + + udev_list_entry_foreach(list_entry_current, udev_device_get_devlinks_list_entry(dev)) { + if (strcmp(udev_list_entry_get_name(list_entry_current), + udev_list_entry_get_name(list_entry)) == 0) + continue; + } + /* link does no longer belong to this device */ + info(udev, "update old symlink '%s' no longer belonging to '%s'\n", + udev_list_entry_get_name(list_entry), udev_device_get_devpath(dev)); + update_link(dev, udev_list_entry_get_name(list_entry), test); } - /* export symlinks to environment */ - util_remove_trailing_chars(symlinks, ' '); - if (symlinks[0] != '\0') - setenv("DEVLINKS", symlinks, 1); - - /* update possible left-over symlinks (device metadata changed) */ - if (udevice_old != NULL) { - struct name_entry *link_loop; - struct name_entry *link_old_loop; - int found; - - /* remove current symlinks from old list */ - list_for_each_entry(link_old_loop, &udevice_old->symlink_list, node) { - found = 0; - list_for_each_entry(link_loop, &udevice->symlink_list, node) { - if (strcmp(link_old_loop->name, link_loop->name) == 0) { - found = 1; - break; - } - } - if (!found) { - /* link does no longer belong to this device */ - info(udevice->udev, "update old symlink '%s' no longer belonging to '%s'\n", - link_old_loop->name, udevice->dev->devpath); - update_link(udevice, link_old_loop->name); - } - } + /* + * if the node name has changed, delete the node, + * and possibly restore a symlink of another device + */ + devnode_old = udev_device_get_devnode(dev_old); + if (devnode_old != NULL) { + const char *devnode = udev_device_get_devnode(dev); - /* - * if the node name has changed, delete the node, - * or possibly restore a symlink of another device - */ - if (strcmp(udevice->name, udevice_old->name) != 0) - update_link(udevice, udevice_old->name); + if (devnode != NULL && strcmp(devnode_old, devnode) != 0) + update_link(dev, devnode_old, test); } } -int udev_node_add(struct udevice *udevice) +int udev_node_add(struct udev_device *dev, mode_t mode, const char *owner, const char *group, int test) { - char filename[UTIL_PATH_SIZE]; + struct udev *udev = udev_device_get_udev(dev); uid_t uid; gid_t gid; int i; - int retval = 0; + int num; + struct udev_list_entry *list_entry; + int err = 0; - util_strlcpy(filename, udev_get_dev_path(udevice->udev), sizeof(filename)); - util_strlcat(filename, "/", sizeof(filename)); - util_strlcat(filename, udevice->name, sizeof(filename)); - create_path(udevice->udev, filename); + create_path(udev, udev_device_get_devnode(dev)); - if (strcmp(udevice->owner, "root") == 0) + if (strcmp(owner, "root") == 0) uid = 0; else { char *endptr; unsigned long id; - id = strtoul(udevice->owner, &endptr, 10); + id = strtoul(owner, &endptr, 10); if (endptr[0] == '\0') uid = (uid_t) id; else - uid = lookup_user(udevice->udev, udevice->owner); + uid = lookup_user(udev, owner); } - if (strcmp(udevice->group, "root") == 0) + if (strcmp(group, "root") == 0) gid = 0; else { char *endptr; unsigned long id; - id = strtoul(udevice->group, &endptr, 10); + id = strtoul(group, &endptr, 10); if (endptr[0] == '\0') gid = (gid_t) id; else - gid = lookup_group(udevice->udev, udevice->group); + gid = lookup_group(udev, group); } - info(udevice->udev, "creating device node '%s', major=%d, minor=%d, mode=%#o, uid=%d, gid=%d\n", - filename, major(udevice->devt), minor(udevice->devt), udevice->mode, uid, gid); + info(udev, "creating device node '%s', devnum=%d:%d, mode=%#o, uid=%d, gid=%d\n", + udev_device_get_devnode(dev), + major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)), + mode, uid, gid); - if (!udevice->test_run) - if (udev_node_mknod(udevice, filename, udevice->devt, udevice->mode, uid, gid) != 0) { - retval = -1; + if (!test) + if (udev_node_mknod(dev, NULL, makedev(0,0), mode, uid, gid) != 0) { + err = -1; goto exit; } - setenv("DEVNAME", filename, 1); - /* create all_partitions if requested */ - if (udevice->partitions) { - char partitionname[UTIL_PATH_SIZE]; - char *attr; - int range; - - /* take the maximum registered minor range */ - attr = sysfs_attr_get_value(udevice->udev, udevice->dev->devpath, "range"); - if (attr != NULL) { - range = atoi(attr); - if (range > 1) - udevice->partitions = range-1; - } - info(udevice->udev, "creating device partition nodes '%s[1-%i]'\n", filename, udevice->partitions); - if (!udevice->test_run) { - for (i = 1; i <= udevice->partitions; i++) { - dev_t part_devt; - - snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i); + num = udev_device_get_num_fake_partitions(dev); + if (num > 0) { + info(udev, "creating device partition nodes '%s[1-%i]'\n", udev_device_get_devnode(dev), num); + if (!test) { + for (i = 1; i <= num; i++) { + char partitionname[UTIL_PATH_SIZE]; + dev_t part_devnum; + + snprintf(partitionname, sizeof(partitionname), "%s%d", + udev_device_get_devnode(dev), i); partitionname[sizeof(partitionname)-1] = '\0'; - part_devt = makedev(major(udevice->devt), minor(udevice->devt) + i); - udev_node_mknod(udevice, partitionname, part_devt, udevice->mode, uid, gid); + part_devnum = makedev(major(udev_device_get_devnum(dev)), + minor(udev_device_get_devnum(dev)) + i); + udev_node_mknod(dev, partitionname, part_devnum, mode, uid, gid); } } } + + /* add node and to name index */ + name_index(udev, udev_device_get_devpath(dev), udev_device_get_devnode(dev), 1, test); + + /* create/update symlinks, add symlinks to name index */ + udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev)) { + name_index(udev, udev_device_get_devpath(dev), udev_list_entry_get_name(list_entry), 1, test); + update_link(dev, udev_list_entry_get_name(list_entry), test); + } exit: - return retval; + return err; } -int udev_node_remove(struct udevice *udevice) +extern int udev_node_remove(struct udev_device *dev, int test) { - char filename[UTIL_PATH_SIZE]; + struct udev *udev = udev_device_get_udev(dev); + struct udev_list_entry *list_entry; + const char *devnode; char partitionname[UTIL_PATH_SIZE]; struct stat stats; - int retval = 0; + int err = 0; int num; - util_strlcpy(filename, udev_get_dev_path(udevice->udev), sizeof(filename)); - util_strlcat(filename, "/", sizeof(filename)); - util_strlcat(filename, udevice->name, sizeof(filename)); - if (stat(filename, &stats) != 0) { - info(udevice->udev, "device node '%s' not found\n", filename); + /* remove node from name index */ + name_index(udev, udev_device_get_devpath(dev), udev_device_get_devnode(dev), 0, test); + + /* remove,update symlinks, remove symlinks from name index */ + udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev)) { + name_index(udev, udev_device_get_devpath(dev), udev_list_entry_get_name(list_entry), 0, test); + update_link(dev, udev_list_entry_get_name(list_entry), test); + } + + devnode = udev_device_get_devnode(dev); + if (devnode == NULL) + return 0; + if (stat(devnode, &stats) != 0) { + info(udev, "device node '%s' not found\n", devnode); return 0; } - if (udevice->devt && stats.st_rdev != udevice->devt) { - info(udevice->udev, "device node '%s' points to a different device, skip removal\n", filename); + if (stats.st_rdev != udev_device_get_devnum(dev)) { + info(udev, "device node '%s' points to a different device, skip removal\n", devnode); return -1; } - info(udevice->udev, "removing device node '%s'\n", filename); - if (!udevice->test_run) - retval = unlink_secure(udevice->udev, filename); - if (retval) - return retval; + info(udev, "removing device node '%s'\n", devnode); + if (!test) + err = unlink_secure(udev, devnode); + if (err) + return err; - setenv("DEVNAME", filename, 1); - num = udevice->partitions; + num = udev_device_get_num_fake_partitions(dev); if (num > 0) { int i; - info(udevice->udev, "removing all_partitions '%s[1-%i]'\n", filename, num); + info(udev, "removing all_partitions '%s[1-%i]'\n", devnode, num); if (num > 255) return -1; for (i = 1; i <= num; i++) { - snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i); + snprintf(partitionname, sizeof(partitionname), "%s%d", devnode, i); partitionname[sizeof(partitionname)-1] = '\0'; - if (!udevice->test_run) - unlink_secure(udevice->udev, partitionname); + if (!test) + unlink_secure(udev, partitionname); } } - delete_path(udevice->udev, filename); - return retval; + delete_path(udev, devnode); + return err; } diff --git a/udev/udev_rules.c b/udev/udev_rules.c index d06f536d1..dfcd87539 100644 --- a/udev/udev_rules.c +++ b/udev/udev_rules.c @@ -116,10 +116,12 @@ out: return 0; } -static int run_program(struct udev *udev, const char *command, const char *subsystem, +static int run_program(struct udev_device *dev, const char *command, char *result, size_t ressize, size_t *reslen) { + struct udev *udev = udev_device_get_udev(dev); int status; + char **envp; int outpipe[2] = {-1, -1}; int errpipe[2] = {-1, -1}; pid_t pid; @@ -128,9 +130,9 @@ static int run_program(struct udev *udev, const char *command, const char *subsy char *argv[(sizeof(arg) / 2) + 1]; int devnull; int i; - int retval = 0; + int err = 0; - /* build argv from comand */ + /* build argv from command */ util_strlcpy(arg, command, sizeof(arg)); i = 0; if (strchr(arg, ' ') != NULL) { @@ -170,13 +172,15 @@ static int run_program(struct udev *udev, const char *command, const char *subsy } } - /* allow programs in /lib/udev called without the path */ + /* allow programs in /lib/udev/ to be called without the path */ if (strchr(argv[0], '/') == NULL) { util_strlcpy(program, UDEV_PREFIX "/lib/udev/", sizeof(program)); util_strlcat(program, argv[0], sizeof(program)); argv[0] = program; } + envp = udev_device_get_properties_envp(dev); + pid = fork(); switch(pid) { case 0: @@ -205,7 +209,7 @@ static int run_program(struct udev *udev, const char *command, const char *subsy dup2(errpipe[WRITE_END], STDERR_FILENO); close(errpipe[WRITE_END]); } - execv(argv[0], argv); + execve(argv[0], argv, envp); if (errno == ENOENT || errno == ENOTDIR) { /* may be on a filesytem which is not mounted right now */ info(udev, "program '%s' not found\n", argv[0]); @@ -243,7 +247,7 @@ static int run_program(struct udev *udev, const char *command, const char *subsy if (fdcount < 0) { if (errno == EINTR) continue; - retval = -1; + err = -1; break; } @@ -259,7 +263,7 @@ static int run_program(struct udev *udev, const char *command, const char *subsy outpipe[READ_END] = -1; if (count < 0) { err(udev, "stdin read failed: %m\n"); - retval = -1; + err = -1; } continue; } @@ -272,7 +276,7 @@ static int run_program(struct udev *udev, const char *command, const char *subsy respos += count; } else { err(udev, "ressize %ld too short\n", (long)ressize); - retval = -1; + err = -1; } } pos = inbuf; @@ -319,18 +323,19 @@ static int run_program(struct udev *udev, const char *command, const char *subsy if (WIFEXITED(status)) { info(udev, "'%s' returned with status %i\n", argv[0], WEXITSTATUS(status)); if (WEXITSTATUS(status) != 0) - retval = -1; + err = -1; } else { err(udev, "'%s' abnormal exit\n", argv[0]); - retval = -1; + err = -1; } } - return retval; + return err; } -static int import_keys_into_env(struct udevice *udevice, const char *buf, size_t bufsize) +static int import_keys_into_env(struct udev_event *event, const char *buf, size_t bufsize) { + struct udev_device *dev = event->dev; char line[UTIL_LINE_SIZE]; const char *bufline; char *linepos; @@ -362,7 +367,7 @@ static int import_keys_into_env(struct udevice *udevice, const char *buf, size_t continue; if (count >= sizeof(line)) { - err(udevice->udev, "line too long, skipped\n"); + err(event->udev, "line too long, skipped\n"); continue; } @@ -371,162 +376,112 @@ static int import_keys_into_env(struct udevice *udevice, const char *buf, size_t linepos = line; if (get_key(&linepos, &variable, &value) == 0) { - dbg(udevice->udev, "import '%s=%s'\n", variable, value); + char syspath[UTIL_PATH_SIZE]; + dbg(event->udev, "import '%s=%s'\n", variable, value); /* handle device, renamed by external tool, returning new path */ if (strcmp(variable, "DEVPATH") == 0) { - info(udevice->udev, "updating devpath from '%s' to '%s'\n", udevice->dev->devpath, value); - sysfs_device_set_values(udevice->udev, udevice->dev, value, NULL, NULL); - } else - name_list_key_add(udevice->udev, &udevice->env_list, variable, value); - setenv(variable, value, 1); + info(event->udev, "updating devpath from '%s' to '%s'\n", + udev_device_get_devpath(dev), value); + util_strlcpy(syspath, udev_get_sys_path(event->udev), sizeof(syspath)); + util_strlcat(syspath, value, sizeof(syspath)); + udev_device_set_syspath(dev, syspath); + } else { + struct udev_list_entry *entry; + + entry = udev_device_add_property(dev, variable, value); + /* store in db */ + udev_list_entry_set_flag(entry, 1); + } } } - return 0; } -static int import_file_into_env(struct udevice *udevice, const char *filename) +static int import_file_into_env(struct udev_event *event, const char *filename) { char *buf; size_t bufsize; if (file_map(filename, &buf, &bufsize) != 0) { - err(udevice->udev, "can't open '%s': %m\n", filename); + err(event->udev, "can't open '%s': %m\n", filename); return -1; } - import_keys_into_env(udevice, buf, bufsize); + import_keys_into_env(event, buf, bufsize); file_unmap(buf, bufsize); return 0; } -static int import_program_into_env(struct udevice *udevice, const char *program) +static int import_program_into_env(struct udev_event *event, const char *program) { char result[2048]; size_t reslen; - if (run_program(udevice->udev, program, udevice->dev->subsystem, result, sizeof(result), &reslen) != 0) + if (run_program(event->dev, program, result, sizeof(result), &reslen) != 0) return -1; - return import_keys_into_env(udevice, result, reslen); + return import_keys_into_env(event, result, reslen); } -static int import_parent_into_env(struct udevice *udevice, const char *filter) +static int import_parent_into_env(struct udev_event *event, const char *filter) { - struct sysfs_device *dev_parent; - int rc = -1; + struct udev_device *dev_parent; + struct udev_list_entry *list_entry; - dev_parent = sysfs_device_get_parent(udevice->udev, udevice->dev); - if (dev_parent != NULL) { - struct udevice *udev_parent; - struct name_entry *name_loop; - - dbg(udevice->udev, "found parent '%s', get the node name\n", dev_parent->devpath); - udev_parent = udev_device_init(udevice->udev); - if (udev_parent == NULL) - return -1; - /* import the udev_db of the parent */ - 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[UTIL_NAME_SIZE]; - char *pos; - - util_strlcpy(name, name_loop->name, sizeof(name)); - pos = strchr(name, '='); - if (pos) { - pos[0] = '\0'; - pos++; - if (fnmatch(filter, name, 0) == 0) { - dbg(udevice->udev, "import key '%s'\n", name_loop->name); - name_list_add(udevice->udev, &udevice->env_list, name_loop->name, 0); - setenv(name, pos, 1); - } else - dbg(udevice->udev, "skip key '%s'\n", name_loop->name); - } - } - rc = 0; - } else - dbg(udevice->udev, "parent not found in database\n"); - udev_device_cleanup(udev_parent); - } + dev_parent = udev_device_get_parent(event->dev); + if (dev_parent == NULL) + return -1; - return rc; -} + dbg(event->udev, "found parent '%s', get the node name\n", udev_device_get_syspath(dev_parent)); + udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev_parent)) { + const char *key = udev_list_entry_get_name(list_entry); + const char *val = udev_list_entry_get_value(list_entry); -static int pass_env_to_socket(struct udev *udev, const char *sockpath, const char *devpath, const char *action) -{ - int sock; - struct sockaddr_un saddr; - socklen_t saddrlen; - struct stat stats; - char buf[2048]; - size_t bufpos = 0; - int i; - ssize_t count; - int retval = 0; - - dbg(udev, "pass environment to socket '%s'\n", sockpath); - sock = socket(AF_LOCAL, SOCK_DGRAM, 0); - memset(&saddr, 0x00, sizeof(struct sockaddr_un)); - saddr.sun_family = AF_LOCAL; - if (sockpath[0] == '@') { - /* abstract namespace socket requested */ - util_strlcpy(&saddr.sun_path[1], &sockpath[1], sizeof(saddr.sun_path)-1); - saddrlen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&saddr.sun_path[1]); - } else if (stat(sockpath, &stats) == 0 && S_ISSOCK(stats.st_mode)) { - /* existing socket file */ - util_strlcpy(saddr.sun_path, sockpath, sizeof(saddr.sun_path)); - saddrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path); - } else { - /* no socket file, assume abstract namespace socket */ - util_strlcpy(&saddr.sun_path[1], sockpath, sizeof(saddr.sun_path)-1); - saddrlen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&saddr.sun_path[1]); - } + if (fnmatch(filter, key, 0) == 0) { + struct udev_list_entry *entry; - bufpos = snprintf(buf, sizeof(buf), "%s@%s", action, devpath); - bufpos++; - for (i = 0; environ[i] != NULL && bufpos < (sizeof(buf)); i++) { - bufpos += util_strlcpy(&buf[bufpos], environ[i], sizeof(buf) - bufpos); - bufpos++; + dbg(event->udev, "import key '%s=%s'\n", key, val); + entry = udev_device_add_property(event->dev, key, val); + /* store in db */ + udev_list_entry_set_flag(entry, 1); + } } - if (bufpos > sizeof(buf)) - bufpos = sizeof(buf); - - count = sendto(sock, &buf, bufpos, 0, (struct sockaddr *)&saddr, saddrlen); - if (count < 0) - retval = -1; - info(udev, "passed %zi bytes to socket '%s', \n", count, sockpath); - - close(sock); - return retval; + return 0; } -int udev_rules_run(struct udevice *udevice) +int udev_rules_run(struct udev_event *event) { - struct name_entry *name_loop; - int retval = 0; + struct udev_list_entry *list_entry; + int err = 0; + + dbg(event->udev, "executing run list\n"); + udev_list_entry_foreach(list_entry, udev_list_get_entry(&event->run_list)) { + const char *cmd = udev_list_entry_get_name(list_entry); + + if (strncmp(cmd, "socket:", strlen("socket:")) == 0) { + struct udev_monitor *monitor; - dbg(udevice->udev, "executing run list\n"); - list_for_each_entry(name_loop, &udevice->run_list, node) { - 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); + monitor = udev_monitor_new_from_socket(event->udev, &cmd[strlen("socket:")]); + if (monitor == NULL) + continue; + udev_monitor_send_device(monitor, event->dev); + udev_monitor_unref(monitor); } else { char program[UTIL_PATH_SIZE]; - util_strlcpy(program, name_loop->name, sizeof(program)); - udev_rules_apply_format(udevice, program, sizeof(program)); - if (run_program(udevice->udev, program, udevice->dev->subsystem, NULL, 0, NULL) != 0) - if (!name_loop->ignore_error) - retval = -1; + util_strlcpy(program, cmd, sizeof(program)); + udev_rules_apply_format(event, program, sizeof(program)); + if (run_program(event->dev, program, NULL, 0, NULL) != 0) { + if (!udev_list_entry_get_flag(list_entry)) + err = -1; + } } } - - return retval; + return err; } #define WAIT_LOOP_PER_SECOND 50 -static int wait_for_file(struct udevice *udevice, const char *file, int timeout) +static int wait_for_file(struct udev_event *event, const char *file, int timeout) { char filepath[UTIL_PATH_SIZE]; char devicepath[UTIL_PATH_SIZE] = ""; @@ -535,8 +490,8 @@ static int wait_for_file(struct udevice *udevice, const char *file, int timeout) /* a relative path is a device attribute */ if (file[0] != '/') { - util_strlcpy(devicepath, udev_get_sys_path(udevice->udev), sizeof(devicepath)); - util_strlcat(devicepath, udevice->dev->devpath, sizeof(devicepath)); + util_strlcpy(devicepath, udev_get_sys_path(event->udev), sizeof(devicepath)); + util_strlcat(devicepath, udev_device_get_devpath(event->dev), sizeof(devicepath)); util_strlcpy(filepath, devicepath, sizeof(filepath)); util_strlcat(filepath, "/", sizeof(filepath)); @@ -544,63 +499,53 @@ static int wait_for_file(struct udevice *udevice, const char *file, int timeout) file = filepath; } - dbg(udevice->udev, "will wait %i sec for '%s'\n", timeout, file); + dbg(event->udev, "will wait %i sec for '%s'\n", timeout, file); while (--loop) { /* lookup file */ if (stat(file, &stats) == 0) { - info(udevice->udev, "file '%s' appeared after %i loops\n", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1); + info(event->udev, "file '%s' appeared after %i loops\n", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1); return 0; } /* make sure, the device did not disappear in the meantime */ if (devicepath[0] != '\0' && stat(devicepath, &stats) != 0) { - info(udevice->udev, "device disappeared while waiting for '%s'\n", file); + info(event->udev, "device disappeared while waiting for '%s'\n", file); return -2; } - info(udevice->udev, "wait for '%s' for %i mseconds\n", file, 1000 / WAIT_LOOP_PER_SECOND); + info(event->udev, "wait for '%s' for %i mseconds\n", file, 1000 / WAIT_LOOP_PER_SECOND); usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND); } - info(udevice->udev, "waiting for '%s' failed\n", file); + info(event->udev, "waiting for '%s' failed\n", file); return -1; } /* handle "[$SUBSYSTEM/$KERNEL]" lookup */ -static int attr_get_by_subsys_id(struct udev *udev, const char *attrstr, char *devpath, size_t len, char **attr) +static int split_subsys_sysname(struct udev *udev, char *attrstr, char **subsys, char **sysname, char **attr) { - char subsys[UTIL_NAME_SIZE]; char *pos; - char *id; - char *attrib; - int found = 0; if (attrstr[0] != '[') - goto out; + return -1; + + *subsys = &attrstr[1]; + pos = strchr(*subsys, ']'); + if (pos == NULL) + return -1; + pos[0] = '\0'; + pos = &pos[1]; - attrib = strchr(&attrstr[1], ']'); - if (attrib == NULL) - goto out; - attrib = &attrib[1]; + if (pos[0] == '/') + pos = &pos[1]; + if (pos[0] != '\0') + *attr = pos; + else + *attr = NULL; - util_strlcpy(subsys, &attrstr[1], sizeof(subsys)); - pos = strchr(subsys, ']'); + pos = strchr(*subsys, '/'); if (pos == NULL) - goto out; + return -1; pos[0] = '\0'; - id = strchr(subsys, '/'); - if (id == NULL) - goto out; - id[0] = '\0'; - id = &id[1]; - if (sysfs_lookup_devpath_by_subsys_id(udev, devpath, len, subsys, id)) { - if (attr != NULL) { - if (attrib[0] != '\0') - *attr = attrib; - else - *attr = NULL; - } - found = 1; - } -out: - return found; + *sysname = &pos[1]; + return 0; } static int attr_subst_subdir(char *attr, size_t len) @@ -641,11 +586,12 @@ static int attr_subst_subdir(char *attr, size_t len) return found; } -void udev_rules_apply_format(struct udevice *udevice, char *string, size_t maxsize) +void udev_rules_apply_format(struct udev_event *event, char *string, size_t maxsize) { + struct udev_device *dev = event->dev; char temp[UTIL_PATH_SIZE]; char temp2[UTIL_PATH_SIZE]; - char *head, *tail, *pos, *cpos, *attr, *rest; + char *head, *tail, *cpos, *attr, *rest; int len; int i; int count; @@ -714,12 +660,12 @@ void udev_rules_apply_format(struct udevice *udevice, char *string, size_t maxsi if (strncasecmp(&head[1], subst->name, strlen(subst->name)) == 0) { type = subst->type; tail = head + strlen(subst->name)+1; - dbg(udevice->udev, "will substitute format name '%s'\n", subst->name); + dbg(event->udev, "will substitute format name '%s'\n", subst->name); goto found; } } head[0] = '$'; - err(udevice->udev, "unknown format variable '%s'\n", head); + err(event->udev, "unknown format variable '%s'\n", head); } else if (head[0] == '%') { /* substitute format char */ if (head[1] == '\0') @@ -732,71 +678,77 @@ void udev_rules_apply_format(struct udevice *udevice, char *string, size_t maxsi } head[0] = '\0'; tail = head+1; - len = get_format_len(udevice->udev, &tail); + len = get_format_len(event->udev, &tail); for (subst = map; subst->name; subst++) { if (tail[0] == subst->fmt) { type = subst->type; tail++; - dbg(udevice->udev, "will substitute format char '%c'\n", subst->fmt); + dbg(event->udev, "will substitute format char '%c'\n", subst->fmt); goto found; } } head[0] = '%'; - err(udevice->udev, "unknown format char '%c'\n", tail[0]); + err(event->udev, "unknown format char '%c'\n", tail[0]); } head++; } break; found: - attr = get_format_attribute(udevice->udev, &tail); + attr = get_format_attribute(event->udev, &tail); util_strlcpy(temp, tail, sizeof(temp)); - dbg(udevice->udev, "format=%i, string='%s', tail='%s'\n", type ,string, tail); + dbg(event->udev, "format=%i, string='%s', tail='%s'\n", type ,string, tail); switch (type) { case SUBST_DEVPATH: - util_strlcat(string, udevice->dev->devpath, maxsize); - dbg(udevice->udev, "substitute devpath '%s'\n", udevice->dev->devpath); + util_strlcat(string, udev_device_get_devpath(dev), maxsize); + dbg(event->udev, "substitute devpath '%s'\n", udev_device_get_devpath(dev)); break; case SUBST_KERNEL: - util_strlcat(string, udevice->dev->kernel, maxsize); - dbg(udevice->udev, "substitute kernel name '%s'\n", udevice->dev->kernel); + util_strlcat(string, udev_device_get_sysname(dev), maxsize); + dbg(event->udev, "substitute kernel name '%s'\n", udev_device_get_sysname(dev)); break; case SUBST_KERNEL_NUMBER: - util_strlcat(string, udevice->dev->kernel_number, maxsize); - dbg(udevice->udev, "substitute kernel number '%s'\n", udevice->dev->kernel_number); + if (udev_device_get_sysnum(dev) == NULL) + break; + util_strlcat(string, udev_device_get_sysnum(dev), maxsize); + dbg(event->udev, "substitute kernel number '%s'\n", udev_device_get_sysnum(dev)); break; case SUBST_ID: - if (udevice->dev_parent != NULL) { - util_strlcat(string, udevice->dev_parent->kernel, maxsize); - dbg(udevice->udev, "substitute id '%s'\n", udevice->dev_parent->kernel); + if (event->dev_parent != NULL) { + util_strlcat(string, udev_device_get_sysname(event->dev_parent), maxsize); + dbg(event->udev, "substitute id '%s'\n", udev_device_get_sysname(event->dev_parent)); } break; case SUBST_DRIVER: - if (udevice->dev_parent != NULL) { - util_strlcat(string, udevice->dev_parent->driver, maxsize); - dbg(udevice->udev, "substitute driver '%s'\n", udevice->dev_parent->driver); + if (event->dev_parent != NULL) { + const char *driver = udev_device_get_driver(event->dev_parent); + + if (driver == NULL) + break; + util_strlcat(string, driver, maxsize); + dbg(event->udev, "substitute driver '%s'\n", driver); } break; case SUBST_MAJOR: - sprintf(temp2, "%d", major(udevice->devt)); + sprintf(temp2, "%d", major(udev_device_get_devnum(dev))); util_strlcat(string, temp2, maxsize); - dbg(udevice->udev, "substitute major number '%s'\n", temp2); + dbg(event->udev, "substitute major number '%s'\n", temp2); break; case SUBST_MINOR: - sprintf(temp2, "%d", minor(udevice->devt)); + sprintf(temp2, "%d", minor(udev_device_get_devnum(dev))); util_strlcat(string, temp2, maxsize); - dbg(udevice->udev, "substitute minor number '%s'\n", temp2); + dbg(event->udev, "substitute minor number '%s'\n", temp2); break; case SUBST_RESULT: - if (udevice->program_result[0] == '\0') + if (event->program_result[0] == '\0') break; /* get part part of the result string */ i = 0; if (attr != NULL) i = strtoul(attr, &rest, 10); if (i > 0) { - dbg(udevice->udev, "request part #%d of result string\n", i); - cpos = udevice->program_result; + dbg(event->udev, "request part #%d of result string\n", i); + cpos = event->program_result; while (--i) { while (cpos[0] != '\0' && !isspace(cpos[0])) cpos++; @@ -804,7 +756,7 @@ found: cpos++; } if (i > 0) { - err(udevice->udev, "requested part of result string not found\n"); + err(event->udev, "requested part of result string not found\n"); break; } util_strlcpy(temp2, cpos, sizeof(temp2)); @@ -815,145 +767,159 @@ found: cpos[0] = '\0'; } util_strlcat(string, temp2, maxsize); - dbg(udevice->udev, "substitute part of result string '%s'\n", temp2); + dbg(event->udev, "substitute part of result string '%s'\n", temp2); } else { - util_strlcat(string, udevice->program_result, maxsize); - dbg(udevice->udev, "substitute result string '%s'\n", udevice->program_result); + util_strlcat(string, event->program_result, maxsize); + dbg(event->udev, "substitute result string '%s'\n", event->program_result); } break; case SUBST_ATTR: if (attr == NULL) - err(udevice->udev, "missing file parameter for attr\n"); + err(event->udev, "missing file parameter for attr\n"); else { - char devpath[UTIL_PATH_SIZE]; + char *subsys; + char *sysname; char *attrib; - const char *value = NULL; + char value[UTIL_NAME_SIZE] = ""; size_t size; - if (attr_get_by_subsys_id(udevice->udev, attr, devpath, sizeof(devpath), &attrib)) { - if (attrib != NULL) - value = sysfs_attr_get_value(udevice->udev, devpath, attrib); - else + if (split_subsys_sysname(event->udev, attr, &subsys, &sysname, &attrib) == 0) { + struct udev_device *d; + const char *val; + + if (attrib == NULL) break; + d = udev_device_new_from_subsystem_sysname(event->udev, subsys, sysname); + if (d == NULL) + break; + val = udev_device_get_attr_value(d, attrib); + if (val != NULL) + util_strlcpy(value, val, sizeof(value)); + udev_device_unref(d); } /* try the current device, other matches may have selected */ - if (value == NULL && udevice->dev_parent != NULL && udevice->dev_parent != udevice->dev) - value = sysfs_attr_get_value(udevice->udev, udevice->dev_parent->devpath, attr); + if (value[0]=='\0' && event->dev_parent != NULL && event->dev_parent != event->dev) { + const char *val; + + val = udev_device_get_attr_value(event->dev_parent, attr); + if (val != NULL) + util_strlcpy(value, val, sizeof(value)); + } /* look at all devices along the chain of parents */ - if (value == NULL) { - struct sysfs_device *dev_parent = udevice->dev; + if (value[0]=='\0') { + struct udev_device *dev_parent = dev; + const char *val; do { - dbg(udevice->udev, "looking at '%s'\n", dev_parent->devpath); - value = sysfs_attr_get_value(udevice->udev, dev_parent->devpath, attr); - if (value != NULL) + dbg(event->udev, "looking at '%s'\n", udev_device_get_syspath(dev_parent)); + val = udev_device_get_attr_value(dev_parent, attr); + if (val != NULL) { + util_strlcpy(value, val, sizeof(value)); break; - dev_parent = sysfs_device_get_parent(udevice->udev, dev_parent); + } + dev_parent = udev_device_get_parent(dev_parent); } while (dev_parent != NULL); } - if (value == NULL) + if (value[0]=='\0') break; /* strip trailing whitespace, and replace unwanted characters */ - size = util_strlcpy(temp2, value, sizeof(temp2)); - if (size >= sizeof(temp2)) - size = sizeof(temp2)-1; - while (size > 0 && isspace(temp2[size-1])) - temp2[--size] = '\0'; - count = util_replace_chars(temp2, ALLOWED_CHARS_INPUT); + size = strlen(value); + while (size > 0 && isspace(value[--size])) + value[size] = '\0'; + count = util_replace_chars(value, ALLOWED_CHARS_INPUT); if (count > 0) - info(udevice->udev, "%i character(s) replaced\n" , count); - util_strlcat(string, temp2, maxsize); - dbg(udevice->udev, "substitute sysfs value '%s'\n", temp2); + info(event->udev, "%i character(s) replaced\n" , count); + util_strlcat(string, value, maxsize); + dbg(event->udev, "substitute sysfs value '%s'\n", value); } break; case SUBST_PARENT: { - struct sysfs_device *dev_parent; - - dev_parent = sysfs_device_get_parent(udevice->udev, udevice->dev); - if (dev_parent != NULL) { - struct udevice *udev_parent; - - dbg(udevice->udev, "found parent '%s', get the node name\n", dev_parent->devpath); - udev_parent = udev_device_init(udevice->udev); - if (udev_parent != NULL) { - /* lookup the name in the udev_db with the DEVPATH of the parent */ - if (udev_db_get_device(udev_parent, dev_parent->devpath) == 0) { - util_strlcat(string, udev_parent->name, maxsize); - dbg(udevice->udev, "substitute parent node name'%s'\n", udev_parent->name); - } else - dbg(udevice->udev, "parent not found in database\n"); - udev_device_cleanup(udev_parent); - } + struct udev_device *dev_parent; + const char *devnode; + + dev_parent = udev_device_get_parent(event->dev); + if (dev_parent == NULL) + break; + devnode = udev_device_get_devnode(dev_parent); + if (devnode != NULL) { + size_t devlen = strlen(udev_get_dev_path(event->udev))+1; + + util_strlcat(string, &devnode[devlen], maxsize); + dbg(event->udev, "found parent '%s', got node name '%s'\n", + udev_device_get_syspath(dev_parent), &devnode[devlen]); } } break; case SUBST_TEMP_NODE: - if (udevice->tmp_node[0] == '\0' && major(udevice->devt) > 0) { - dbg(udevice->udev, "create temporary device node for callout\n"); - snprintf(udevice->tmp_node, sizeof(udevice->tmp_node), "%s/.tmp-%u-%u", - udev_get_dev_path(udevice->udev), major(udevice->devt), minor(udevice->devt)); - udevice->tmp_node[sizeof(udevice->tmp_node)-1] = '\0'; - udev_node_mknod(udevice, udevice->tmp_node, udevice->devt, 0600, 0, 0); + if (event->tmp_node[0] == '\0' && major(udev_device_get_devnum(dev)) > 0) { + dbg(event->udev, "create temporary device node for callout\n"); + snprintf(event->tmp_node, sizeof(event->tmp_node), "%s/.tmp-%u-%u", + udev_get_dev_path(event->udev), + major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev))); + udev_node_mknod(dev, event->tmp_node, makedev(0,0), 0600, 0, 0); } - util_strlcat(string, udevice->tmp_node, maxsize); - dbg(udevice->udev, "substitute temporary device node name '%s'\n", udevice->tmp_node); + util_strlcat(string, event->tmp_node, maxsize); + dbg(event->udev, "substitute temporary device node name '%s'\n", event->tmp_node); break; case SUBST_NAME: - if (udevice->name[0] == '\0') { - util_strlcat(string, udevice->dev->kernel, maxsize); - dbg(udevice->udev, "substitute udevice->kernel '%s'\n", udevice->name); + if (event->name != NULL) { + util_strlcat(string, event->name, maxsize); + dbg(event->udev, "substitute name '%s'\n", event->name); } else { - util_strlcat(string, udevice->name, maxsize); - dbg(udevice->udev, "substitute udevice->name '%s'\n", udevice->name); + util_strlcat(string, udev_device_get_sysname(dev), maxsize); + dbg(event->udev, "substitute sysname '%s'\n", udev_device_get_sysname(dev)); } break; case SUBST_LINKS: - if (!list_empty(&udevice->symlink_list)) { - struct name_entry *name_loop; - char symlinks[UTIL_PATH_SIZE] = ""; + { + struct udev_list_entry *list_entry; - list_for_each_entry(name_loop, &udevice->symlink_list, node) { - util_strlcat(symlinks, name_loop->name, sizeof(symlinks)); - util_strlcat(symlinks, " ", sizeof(symlinks)); + list_entry = udev_device_get_properties_list_entry(dev); + util_strlcpy(string, udev_list_entry_get_name(list_entry), maxsize); + udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry)) { + util_strlcat(string, " ", maxsize); + util_strlcat(string, udev_list_entry_get_name(list_entry), maxsize); } - util_remove_trailing_chars(symlinks, ' '); - util_strlcat(string, symlinks, maxsize); } break; case SUBST_ROOT: - util_strlcat(string, udev_get_dev_path(udevice->udev), maxsize); - dbg(udevice->udev, "substitute udev_root '%s'\n", udev_get_dev_path(udevice->udev)); + util_strlcat(string, udev_get_dev_path(event->udev), maxsize); + dbg(event->udev, "substitute udev_root '%s'\n", udev_get_dev_path(event->udev)); break; case SUBST_SYS: - util_strlcat(string, udev_get_sys_path(udevice->udev), maxsize); - dbg(udevice->udev, "substitute sys_path '%s'\n", udev_get_sys_path(udevice->udev)); + util_strlcat(string, udev_get_sys_path(event->udev), maxsize); + dbg(event->udev, "substitute sys_path '%s'\n", udev_get_sys_path(event->udev)); break; case SUBST_ENV: if (attr == NULL) { - dbg(udevice->udev, "missing attribute\n"); + dbg(event->udev, "missing attribute\n"); break; - } - pos = getenv(attr); - if (pos == NULL) { - dbg(udevice->udev, "env '%s' not available\n", attr); + } else { + struct udev_list_entry *list_entry; + const char *value; + + list_entry = udev_device_get_properties_list_entry(event->dev); + list_entry = udev_list_entry_get_by_name(list_entry, attr); + if (list_entry == NULL) + break; + value = udev_list_entry_get_value(list_entry); + dbg(event->udev, "substitute env '%s=%s'\n", attr, value); + util_strlcat(string, value, maxsize); break; } - dbg(udevice->udev, "substitute env '%s=%s'\n", attr, pos); - util_strlcat(string, pos, maxsize); - break; default: - err(udevice->udev, "unknown substitution type=%i\n", type); + err(event->udev, "unknown substitution type=%i\n", type); break; } /* possibly truncate to format-char specified length */ if (len >= 0 && len < (int)strlen(head)) { head[len] = '\0'; - dbg(udevice->udev, "truncate to %i chars, subtitution string becomes '%s'\n", len, head); + dbg(event->udev, "truncate to %i chars, subtitution string becomes '%s'\n", len, head); } util_strlcat(string, temp, maxsize); } @@ -980,15 +946,18 @@ static int match_key(struct udev *udev, const char *key_name, struct udev_rule * key->operation != KEY_OP_NOMATCH) return 0; + if (val == NULL) + val = ""; + /* look for a matching string, parts are separated by '|' */ util_strlcpy(value, rule->buf + key->val_off, sizeof(value)); key_value = value; dbg(udev, "key %s value='%s'\n", key_name, key_value); - while (key_value) { + while (key_value != NULL) { pos = strchr(key_value, '|'); - if (pos) { + if (pos != NULL) { pos[0] = '\0'; - pos++; + pos = &pos[1]; } dbg(udev, "match %s '%s' <-> '%s'\n", key_name, key_value, val); @@ -1011,37 +980,42 @@ static int match_key(struct udev *udev, const char *key_name, struct udev_rule * } /* match a single rule against a given device and possibly its parent devices */ -static int match_rule(struct udevice *udevice, struct udev_rule *rule) +static int match_rule(struct udev_event *event, struct udev_rule *rule) { + struct udev_device *dev = event->dev; int i; - if (match_key(udevice->udev, "ACTION", rule, &rule->action, udevice->action)) + if (match_key(event->udev, "ACTION", rule, &rule->action, udev_device_get_action(dev))) goto nomatch; - if (match_key(udevice->udev, "KERNEL", rule, &rule->kernel, udevice->dev->kernel)) + if (match_key(event->udev, "KERNEL", rule, &rule->kernel, udev_device_get_sysname(dev))) goto nomatch; - if (match_key(udevice->udev, "SUBSYSTEM", rule, &rule->subsystem, udevice->dev->subsystem)) + if (match_key(event->udev, "SUBSYSTEM", rule, &rule->subsystem, udev_device_get_subsystem(dev))) goto nomatch; - if (match_key(udevice->udev, "DEVPATH", rule, &rule->devpath, udevice->dev->devpath)) + if (match_key(event->udev, "DEVPATH", rule, &rule->devpath, udev_device_get_devpath(dev))) goto nomatch; - if (match_key(udevice->udev, "DRIVER", rule, &rule->driver, udevice->dev->driver)) + if (match_key(event->udev, "DRIVER", rule, &rule->driver, udev_device_get_driver(dev))) goto nomatch; /* match NAME against a value assigned by an earlier rule */ - if (match_key(udevice->udev, "NAME", rule, &rule->name, udevice->name)) + if (match_key(event->udev, "NAME", rule, &rule->name, event->name)) goto nomatch; /* match against current list of symlinks */ if (rule->symlink_match.operation == KEY_OP_MATCH || rule->symlink_match.operation == KEY_OP_NOMATCH) { - struct name_entry *name_loop; + size_t devlen = strlen(udev_get_dev_path(event->udev))+1; + struct udev_list_entry *list_entry; int match = 0; - list_for_each_entry(name_loop, &udevice->symlink_list, node) { - if (match_key(udevice->udev, "SYMLINK", rule, &rule->symlink_match, name_loop->name) == 0) { + udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev)) { + const char *devlink; + + devlink = &udev_list_entry_get_name(list_entry)[devlen]; + if (match_key(event->udev, "SYMLINK", rule, &rule->symlink_match, devlink) == 0) { match = 1; break; } @@ -1056,14 +1030,18 @@ static int match_rule(struct udevice *udevice, struct udev_rule *rule) /* we only check for matches, assignments will be handled later */ if (pair->key.operation == KEY_OP_MATCH || pair->key.operation == KEY_OP_NOMATCH) { + struct udev_list_entry *list_entry; const char *key_name = key_pair_name(rule, pair); - const char *value = getenv(key_name); + const char *value; - if (!value) { - dbg(udevice->udev, "ENV{'%s'} is not set, treat as empty\n", key_name); + list_entry = udev_device_get_properties_list_entry(event->dev); + list_entry = udev_list_entry_get_by_name(list_entry, key_name); + value = udev_list_entry_get_value(list_entry); + if (value == NULL) { + dbg(event->udev, "ENV{%s} is not set, treat as empty\n", key_name); value = ""; } - if (match_key(udevice->udev, "ENV", rule, &pair->key, value)) + if (match_key(event->udev, "ENV", rule, &pair->key, value)) goto nomatch; } } @@ -1071,26 +1049,30 @@ 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[UTIL_PATH_SIZE]; - char devpath[UTIL_PATH_SIZE]; - char *attr; + char *subsys; + char *sysname; + char *attrib; struct stat statbuf; int match; util_strlcpy(filename, key_val(rule, &rule->test), sizeof(filename)); - udev_rules_apply_format(udevice, filename, sizeof(filename)); - - if (attr_get_by_subsys_id(udevice->udev, filename, devpath, sizeof(devpath), &attr)) { - util_strlcpy(filename, udev_get_sys_path(udevice->udev), sizeof(filename)); - util_strlcat(filename, devpath, sizeof(filename)); - if (attr != NULL) { - util_strlcat(filename, "/", sizeof(filename)); - util_strlcat(filename, attr, sizeof(filename)); + udev_rules_apply_format(event, filename, sizeof(filename)); + + if (split_subsys_sysname(event->udev, filename, &subsys, &sysname, &attrib) == 0) { + struct udev_device *d; + d = udev_device_new_from_subsystem_sysname(event->udev, subsys, sysname); + if (d != NULL) { + util_strlcpy(filename, udev_device_get_syspath(d), sizeof(filename)); + if (attrib != NULL) { + util_strlcat(filename, "/", sizeof(filename)); + util_strlcat(filename, attrib, sizeof(filename)); + } + udev_device_unref(d); } } else if (filename[0] != '/') { char tmp[UTIL_PATH_SIZE]; - util_strlcpy(tmp, udev_get_sys_path(udevice->udev), sizeof(tmp)); - util_strlcat(tmp, udevice->dev->devpath, sizeof(tmp)); + util_strlcpy(tmp, udev_device_get_syspath(dev), sizeof(tmp)); util_strlcat(tmp, "/", sizeof(tmp)); util_strlcat(tmp, filename, sizeof(tmp)); util_strlcpy(filename, tmp, sizeof(filename)); @@ -1099,10 +1081,10 @@ static int match_rule(struct udevice *udevice, struct udev_rule *rule) attr_subst_subdir(filename, sizeof(filename)); match = (stat(filename, &statbuf) == 0); - info(udevice->udev, "'%s' %s", filename, match ? "exists\n" : "does not exist\n"); + info(event->udev, "'%s' %s", filename, match ? "exists\n" : "does not exist\n"); if (match && rule->test_mode_mask > 0) { match = ((statbuf.st_mode & rule->test_mode_mask) > 0); - info(udevice->udev, "'%s' has mode=%#o and %s %#o\n", filename, statbuf.st_mode, + info(event->udev, "'%s' has mode=%#o and %s %#o\n", filename, statbuf.st_mode, match ? "matches" : "does not match", rule->test_mode_mask); } @@ -1110,7 +1092,7 @@ static int match_rule(struct udevice *udevice, struct udev_rule *rule) goto nomatch; if (!match && rule->test.operation == KEY_OP_MATCH) goto nomatch; - dbg(udevice->udev, "TEST key is true\n"); + dbg(event->udev, "TEST key is true\n"); } if (rule->wait_for.operation != KEY_OP_UNSET) { @@ -1118,8 +1100,8 @@ static int match_rule(struct udevice *udevice, struct udev_rule *rule) int found; util_strlcpy(filename, key_val(rule, &rule->wait_for), sizeof(filename)); - udev_rules_apply_format(udevice, filename, sizeof(filename)); - found = (wait_for_file(udevice, filename, 10) == 0); + udev_rules_apply_format(event, filename, sizeof(filename)); + found = (wait_for_file(event, filename, 10) == 0); if (!found && (rule->wait_for.operation != KEY_OP_NOMATCH)) goto nomatch; } @@ -1130,53 +1112,72 @@ static int match_rule(struct udevice *udevice, struct udev_rule *rule) if (pair->key.operation == KEY_OP_MATCH || pair->key.operation == KEY_OP_NOMATCH) { + char attr[UTIL_PATH_SIZE]; const char *key_name = key_pair_name(rule, pair); const char *key_value = key_val(rule, &pair->key); - char devpath[UTIL_PATH_SIZE]; + char *subsys; + char *sysname; char *attrib; - const char *value = NULL; - char val[UTIL_NAME_SIZE]; + char value[UTIL_NAME_SIZE] = ""; size_t len; - if (attr_get_by_subsys_id(udevice->udev, key_name, devpath, sizeof(devpath), &attrib)) { - if (attrib != NULL) - value = sysfs_attr_get_value(udevice->udev, devpath, attrib); - else + util_strlcpy(attr, key_name, sizeof(attr)); + if (split_subsys_sysname(event->udev, attr, &subsys, &sysname, &attrib) == 0) { + struct udev_device *d; + const char *val; + + if (attrib == NULL) goto nomatch; + d = udev_device_new_from_subsystem_sysname(event->udev, subsys, sysname); + if (d == NULL) + goto nomatch; + val = udev_device_get_attr_value(d, attrib); + if (val != NULL) + util_strlcpy(value, val, sizeof(value)); + udev_device_unref(d); } - if (value == NULL) - value = sysfs_attr_get_value(udevice->udev, udevice->dev->devpath, key_name); - if (value == NULL) + + if (value[0]=='\0') { + const char *val; + + val = udev_device_get_attr_value(dev, key_name); + if (val != NULL) + util_strlcpy(value, val, sizeof(value)); + } + + if (value[0]=='\0') goto nomatch; - util_strlcpy(val, value, sizeof(val)); /* strip trailing whitespace of value, if not asked to match for it */ len = strlen(key_value); if (len > 0 && !isspace(key_value[len-1])) { - len = strlen(val); - while (len > 0 && isspace(val[len-1])) - val[--len] = '\0'; - dbg(udevice->udev, "removed %zi trailing whitespace chars from '%s'\n", strlen(val)-len, val); + len = strlen(value); + while (len > 0 && isspace(value[--len])) + value[len] = '\0'; + dbg(event->udev, "removed trailing whitespace from '%s'\n", value); } - if (match_key(udevice->udev, "ATTR", rule, &pair->key, val)) + if (match_key(event->udev, "ATTR", rule, &pair->key, value)) goto nomatch; } } /* walk up the chain of parent devices and find a match */ - udevice->dev_parent = udevice->dev; + event->dev_parent = dev; while (1) { /* check for matching kernel device name */ - if (match_key(udevice->udev, "KERNELS", rule, &rule->kernels, udevice->dev_parent->kernel)) + if (match_key(event->udev, "KERNELS", rule, + &rule->kernels, udev_device_get_sysname(event->dev_parent))) goto try_parent; /* check for matching subsystem value */ - if (match_key(udevice->udev, "SUBSYSTEMS", rule, &rule->subsystems, udevice->dev_parent->subsystem)) + if (match_key(event->udev, "SUBSYSTEMS", rule, + &rule->subsystems, udev_device_get_subsystem(event->dev_parent))) goto try_parent; /* check for matching driver */ - if (match_key(udevice->udev, "DRIVERS", rule, &rule->drivers, udevice->dev_parent->driver)) + if (match_key(event->udev, "DRIVERS", rule, + &rule->drivers, udev_device_get_driver(event->dev_parent))) goto try_parent; /* check for matching sysfs attribute pairs */ @@ -1187,27 +1188,27 @@ 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); - const char *value; - char val[UTIL_NAME_SIZE]; + const char *val; + char value[UTIL_NAME_SIZE]; size_t len; - value = sysfs_attr_get_value(udevice->udev, udevice->dev_parent->devpath, key_name); - if (value == NULL) - value = sysfs_attr_get_value(udevice->udev, udevice->dev->devpath, key_name); - if (value == NULL) + val = udev_device_get_attr_value(event->dev_parent, key_name); + if (val == NULL) + val = udev_device_get_attr_value(dev, key_name); + if (val == NULL) goto try_parent; - util_strlcpy(val, value, sizeof(val)); + util_strlcpy(value, val, sizeof(value)); /* strip trailing whitespace of value, if not asked to match for it */ len = strlen(key_value); if (len > 0 && !isspace(key_value[len-1])) { - len = strlen(val); - while (len > 0 && isspace(val[len-1])) - val[--len] = '\0'; - dbg(udevice->udev, "removed %zi trailing whitespace chars from '%s'\n", strlen(val)-len, val); + len = strlen(value); + while (len > 0 && isspace(value[--len])) + value[len] = '\0'; + dbg(event->udev, "removed trailing whitespace from '%s'\n", value); } - if (match_key(udevice->udev, "ATTRS", rule, &pair->key, val)) + if (match_key(event->udev, "ATTRS", rule, &pair->key, value)) goto try_parent; } } @@ -1216,12 +1217,12 @@ static int match_rule(struct udevice *udevice, struct udev_rule *rule) break; try_parent: /* move to parent device */ - dbg(udevice->udev, "try parent sysfs device\n"); - udevice->dev_parent = sysfs_device_get_parent(udevice->udev, udevice->dev_parent); - if (udevice->dev_parent == NULL) + dbg(event->udev, "try parent sysfs device\n"); + event->dev_parent = udev_device_get_parent(event->dev_parent); + if (event->dev_parent == NULL) goto nomatch; - dbg(udevice->udev, "looking at dev_parent->devpath='%s'\n", udevice->dev_parent->devpath); - dbg(udevice->udev, "looking at dev_parent->kernel='%s'\n", udevice->dev_parent->kernel); + dbg(event->udev, "looking at dev_parent->devpath='%s'\n", + udev_device_get_syspath(event->dev_parent)); } /* execute external program */ @@ -1230,34 +1231,34 @@ try_parent: char result[UTIL_PATH_SIZE]; util_strlcpy(program, key_val(rule, &rule->program), sizeof(program)); - udev_rules_apply_format(udevice, program, sizeof(program)); - if (run_program(udevice->udev, program, udevice->dev->subsystem, result, sizeof(result), NULL) != 0) { - dbg(udevice->udev, "PROGRAM is false\n"); - udevice->program_result[0] = '\0'; + udev_rules_apply_format(event, program, sizeof(program)); + if (run_program(event->dev, program, result, sizeof(result), NULL) != 0) { + dbg(event->udev, "PROGRAM is false\n"); + event->program_result[0] = '\0'; if (rule->program.operation != KEY_OP_NOMATCH) goto nomatch; } else { int count; - dbg(udevice->udev, "PROGRAM matches\n"); + dbg(event->udev, "PROGRAM matches\n"); util_remove_trailing_chars(result, '\n'); if (rule->string_escape == ESCAPE_UNSET || rule->string_escape == ESCAPE_REPLACE) { count = util_replace_chars(result, ALLOWED_CHARS_INPUT); if (count > 0) - info(udevice->udev, "%i character(s) replaced\n" , count); + info(event->udev, "%i character(s) replaced\n" , count); } - dbg(udevice->udev, "result is '%s'\n", result); - util_strlcpy(udevice->program_result, result, sizeof(udevice->program_result)); - dbg(udevice->udev, "PROGRAM returned successful\n"); + dbg(event->udev, "result is '%s'\n", result); + util_strlcpy(event->program_result, result, sizeof(event->program_result)); + dbg(event->udev, "PROGRAM returned successful\n"); if (rule->program.operation == KEY_OP_NOMATCH) goto nomatch; } - dbg(udevice->udev, "PROGRAM key is true\n"); + dbg(event->udev, "PROGRAM key is true\n"); } /* check for matching result of external program */ - if (match_key(udevice->udev, "RESULT", rule, &rule->result, udevice->program_result)) + if (match_key(event->udev, "RESULT", rule, &rule->result, event->program_result)) goto nomatch; /* import variables returned from program or or file into environment */ @@ -1266,24 +1267,24 @@ try_parent: int rc = -1; util_strlcpy(import, key_val(rule, &rule->import), sizeof(import)); - udev_rules_apply_format(udevice, import, sizeof(import)); - dbg(udevice->udev, "check for IMPORT import='%s'\n", import); + udev_rules_apply_format(event, import, sizeof(import)); + dbg(event->udev, "check for IMPORT import='%s'\n", import); if (rule->import_type == IMPORT_PROGRAM) { - rc = import_program_into_env(udevice, import); + rc = import_program_into_env(event, import); } else if (rule->import_type == IMPORT_FILE) { - dbg(udevice->udev, "import file import='%s'\n", import); - rc = import_file_into_env(udevice, import); + dbg(event->udev, "import file import='%s'\n", import); + rc = import_file_into_env(event, import); } else if (rule->import_type == IMPORT_PARENT) { - dbg(udevice->udev, "import parent import='%s'\n", import); - rc = import_parent_into_env(udevice, import); + dbg(event->udev, "import parent import='%s'\n", import); + rc = import_parent_into_env(event, import); } if (rc != 0) { - dbg(udevice->udev, "IMPORT failed\n"); + dbg(event->udev, "IMPORT failed\n"); if (rule->import.operation != KEY_OP_NOMATCH) goto nomatch; } else - dbg(udevice->udev, "IMPORT '%s' imported\n", key_val(rule, &rule->import)); - dbg(udevice->udev, "IMPORT key is true\n"); + dbg(event->udev, "IMPORT '%s' imported\n", key_val(rule, &rule->import)); + dbg(event->udev, "IMPORT key is true\n"); } /* rule matches, if we have ENV assignments export it */ @@ -1297,20 +1298,15 @@ try_parent: /* 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, sizeof(temp_value)); + udev_rules_apply_format(event, temp_value, sizeof(temp_value)); - if (temp_value[0] == '\0') { - name_list_key_remove(udevice->udev, &udevice->env_list, key_name); - unsetenv(key_name); - info(udevice->udev, "unset ENV '%s'\n", key_name); - } else { - struct name_entry *entry; + if (temp_value[0] != '\0') { + struct udev_list_entry *entry; - entry = name_list_key_add(udevice->udev, &udevice->env_list, key_name, temp_value); - if (entry == NULL) - break; - putenv(entry->name); - info(udevice->udev, "set ENV '%s'\n", entry->name); + info(event->udev, "set ENV '%s=%s'\n", key_name, temp_value); + entry = udev_device_add_property(dev, key_name, temp_value); + /* store in db */ + udev_list_entry_set_flag(entry, 1); } } } @@ -1321,24 +1317,28 @@ try_parent: if (pair->key.operation == KEY_OP_ASSIGN) { const char *key_name = key_pair_name(rule, pair); - char devpath[UTIL_PATH_SIZE]; + char *subsys; + char *sysname; char *attrib; - char attr[UTIL_PATH_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)) { - if (attrib != NULL) { - util_strlcpy(attr, udev_get_sys_path(udevice->udev), sizeof(attr)); - util_strlcat(attr, devpath, sizeof(attr)); - util_strlcat(attr, "/", sizeof(attr)); - util_strlcat(attr, attrib, sizeof(attr)); - } - } + util_strlcpy(attr, key_name, sizeof(attr)); + if (split_subsys_sysname(event->udev, attr, &subsys, &sysname, &attrib) == 0) { + struct udev_device *d; - if (attr[0] == '\0') { - util_strlcpy(attr, udev_get_sys_path(udevice->udev), sizeof(attr)); - util_strlcat(attr, udevice->dev->devpath, sizeof(attr)); + d = udev_device_new_from_subsystem_sysname(event->udev, subsys, sysname); + if (d != NULL) { + util_strlcpy(attr, udev_device_get_syspath(d), sizeof(attr)); + if (attrib != NULL) { + util_strlcat(attr, "/", sizeof(attr)); + util_strlcat(attr, attrib, sizeof(attr)); + } + udev_device_unref(d); + } + } else { + util_strlcpy(attr, udev_device_get_syspath(dev), sizeof(attr)); util_strlcat(attr, "/", sizeof(attr)); util_strlcat(attr, key_name, sizeof(attr)); } @@ -1346,16 +1346,16 @@ try_parent: attr_subst_subdir(attr, sizeof(attr)); util_strlcpy(value, key_val(rule, &pair->key), sizeof(value)); - udev_rules_apply_format(udevice, value, sizeof(value)); - info(udevice->udev, "writing '%s' to sysfs file '%s'\n", value, attr); + udev_rules_apply_format(event, value, sizeof(value)); + info(event->udev, "writing '%s' to sysfs file '%s'\n", value, attr); f = fopen(attr, "w"); if (f != NULL) { - if (!udevice->test_run) + if (!event->test) if (fprintf(f, "%s", value) <= 0) - err(udevice->udev, "error writing ATTR{%s}: %m\n", attr); + err(event->udev, "error writing ATTR{%s}: %m\n", attr); fclose(f); } else - err(udevice->udev, "error opening ATTR{%s} for writing: %m\n", attr); + err(event->udev, "error opening ATTR{%s} for writing: %m\n", attr); } } return 0; @@ -1364,14 +1364,14 @@ nomatch: return -1; } -int udev_rules_get_name(struct udev_rules *rules, struct udevice *udevice) +int udev_rules_get_name(struct udev_rules *rules, struct udev_event *event) { + struct udev_device *dev = event->dev; struct udev_rules_iter iter; struct udev_rule *rule; int name_set = 0; - dbg(udevice->udev, "udevice->dev->devpath='%s'\n", udevice->dev->devpath); - dbg(udevice->udev, "udevice->dev->kernel='%s'\n", udevice->dev->kernel); + dbg(event->udev, "device: '%s'\n", udev_device_get_syspath(dev)); /* look for a matching rule to apply */ udev_rules_iter_init(&iter, rules); @@ -1384,104 +1384,115 @@ int udev_rules_get_name(struct udev_rules *rules, struct udevice *udevice) (rule->name.operation == KEY_OP_ASSIGN || rule->name.operation == KEY_OP_ASSIGN_FINAL || rule->name.operation == KEY_OP_ADD)) { - dbg(udevice->udev, "node name already set, rule ignored\n"); + dbg(event->udev, "node name already set, rule ignored\n"); continue; } - dbg(udevice->udev, "process rule\n"); - if (match_rule(udevice, rule) == 0) { + dbg(event->udev, "process rule\n"); + if (match_rule(event, rule) == 0) { /* apply options */ if (rule->ignore_device) { - info(udevice->udev, "rule applied, '%s' is ignored\n", udevice->dev->kernel); - udevice->ignore_device = 1; + info(event->udev, "rule applied, '%s' is ignored\n", udev_device_get_sysname(dev)); + event->ignore_device = 1; return 0; } if (rule->ignore_remove) { - udevice->ignore_remove = 1; - dbg(udevice->udev, "remove event should be ignored\n"); + udev_device_set_ignore_remove(dev, 1); + dbg(event->udev, "remove event should be ignored\n"); } if (rule->link_priority != 0) { - udevice->link_priority = rule->link_priority; - info(udevice->udev, "link_priority=%i\n", udevice->link_priority); + udev_device_set_devlink_priority(dev, rule->link_priority); + info(event->udev, "devlink_priority=%i\n", rule->link_priority); } if (rule->event_timeout >= 0) { - udevice->event_timeout = rule->event_timeout; - info(udevice->udev, "event_timeout=%i\n", udevice->event_timeout); + udev_device_set_event_timeout(dev, rule->event_timeout); + info(event->udev, "event_timeout=%i\n", rule->event_timeout); } - /* apply all_partitions option only at a main block device */ - if (rule->partitions && - strcmp(udevice->dev->subsystem, "block") == 0 && udevice->dev->kernel_number[0] == '\0') { - udevice->partitions = rule->partitions; - dbg(udevice->udev, "creation of partition nodes requested\n"); + /* apply all_partitions option only at a disk device */ + if (rule->partitions > 0 && + strcmp(udev_device_get_subsystem(dev), "block") == 0 && + udev_device_get_sysnum(dev) == NULL) { + udev_device_set_num_fake_partitions(dev, rule->partitions); + dbg(event->udev, "creation of partition nodes requested\n"); } /* apply permissions */ - if (!udevice->mode_final && rule->mode.operation != KEY_OP_UNSET) { + if (!event->mode_final && rule->mode.operation != KEY_OP_UNSET) { if (rule->mode.operation == KEY_OP_ASSIGN_FINAL) - udevice->mode_final = 1; + event->mode_final = 1; char buf[20]; util_strlcpy(buf, key_val(rule, &rule->mode), sizeof(buf)); - udev_rules_apply_format(udevice, buf, sizeof(buf)); - udevice->mode = strtol(buf, NULL, 8); - dbg(udevice->udev, "applied mode=%#o to '%s'\n", udevice->mode, udevice->dev->kernel); + udev_rules_apply_format(event, buf, sizeof(buf)); + event->mode = strtol(buf, NULL, 8); + dbg(event->udev, "applied mode=%#o to '%s'\n", + event->mode, udev_device_get_sysname(dev)); } - if (!udevice->owner_final && rule->owner.operation != KEY_OP_UNSET) { + if (!event->owner_final && rule->owner.operation != KEY_OP_UNSET) { if (rule->owner.operation == KEY_OP_ASSIGN_FINAL) - udevice->owner_final = 1; - util_strlcpy(udevice->owner, key_val(rule, &rule->owner), sizeof(udevice->owner)); - udev_rules_apply_format(udevice, udevice->owner, sizeof(udevice->owner)); - dbg(udevice->udev, "applied owner='%s' to '%s'\n", udevice->owner, udevice->dev->kernel); + event->owner_final = 1; + util_strlcpy(event->owner, key_val(rule, &rule->owner), sizeof(event->owner)); + udev_rules_apply_format(event, event->owner, sizeof(event->owner)); + dbg(event->udev, "applied owner='%s' to '%s'\n", + event->owner, udev_device_get_sysname(dev)); } - if (!udevice->group_final && rule->group.operation != KEY_OP_UNSET) { + if (!event->group_final && rule->group.operation != KEY_OP_UNSET) { if (rule->group.operation == KEY_OP_ASSIGN_FINAL) - udevice->group_final = 1; - util_strlcpy(udevice->group, key_val(rule, &rule->group), sizeof(udevice->group)); - udev_rules_apply_format(udevice, udevice->group, sizeof(udevice->group)); - dbg(udevice->udev, "applied group='%s' to '%s'\n", udevice->group, udevice->dev->kernel); + event->group_final = 1; + util_strlcpy(event->group, key_val(rule, &rule->group), sizeof(event->group)); + udev_rules_apply_format(event, event->group, sizeof(event->group)); + dbg(event->udev, "applied group='%s' to '%s'\n", + event->group, udev_device_get_sysname(dev)); } /* collect symlinks */ - if (!udevice->symlink_final && + if (!event->devlink_final && (rule->symlink.operation == KEY_OP_ASSIGN || rule->symlink.operation == KEY_OP_ASSIGN_FINAL || rule->symlink.operation == KEY_OP_ADD)) { char temp[UTIL_PATH_SIZE]; + char filename[UTIL_PATH_SIZE]; char *pos, *next; int count = 0; if (rule->symlink.operation == KEY_OP_ASSIGN_FINAL) - udevice->symlink_final = 1; + event->devlink_final = 1; if (rule->symlink.operation == KEY_OP_ASSIGN || rule->symlink.operation == KEY_OP_ASSIGN_FINAL) { - info(udevice->udev, "reset symlink list\n"); - name_list_cleanup(udevice->udev, &udevice->symlink_list); + info(event->udev, "reset symlink list\n"); + udev_device_cleanup_devlinks_list(dev); } /* allow multiple symlinks separated by spaces */ util_strlcpy(temp, key_val(rule, &rule->symlink), sizeof(temp)); - udev_rules_apply_format(udevice, temp, sizeof(temp)); + udev_rules_apply_format(event, temp, sizeof(temp)); if (rule->string_escape == ESCAPE_UNSET) count = util_replace_chars(temp, ALLOWED_CHARS_FILE " "); 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); + info(event->udev, "%i character(s) replaced\n" , count); + dbg(event->udev, "rule applied, added symlink(s) '%s'\n", temp); pos = temp; while (isspace(pos[0])) pos++; next = strchr(pos, ' '); while (next) { next[0] = '\0'; - info(udevice->udev, "add symlink '%s'\n", pos); - name_list_add(udevice->udev, &udevice->symlink_list, pos, 0); + info(event->udev, "add symlink '%s'\n", pos); + util_strlcpy(filename, udev_get_dev_path(event->udev), sizeof(filename)); + util_strlcat(filename, "/", sizeof(filename)); + util_strlcat(filename, pos, sizeof(filename)); + udev_device_add_devlink(dev, filename); while (isspace(next[1])) next++; pos = &next[1]; next = strchr(pos, ' '); } if (pos[0] != '\0') { - info(udevice->udev, "add symlink '%s'\n", pos); - name_list_add(udevice->udev, &udevice->symlink_list, pos, 0); + info(event->udev, "add symlink '%s'\n", pos); + util_strlcpy(filename, udev_get_dev_path(event->udev), sizeof(filename)); + util_strlcat(filename, "/", sizeof(filename)); + util_strlcat(filename, pos, sizeof(filename)); + udev_device_add_devlink(dev, filename); } } @@ -1492,68 +1503,72 @@ int udev_rules_get_name(struct udev_rules *rules, struct udevice *udevice) int count; name_set = 1; - util_strlcpy(udevice->name, key_val(rule, &rule->name), sizeof(udevice->name)); - udev_rules_apply_format(udevice, udevice->name, sizeof(udevice->name)); + util_strlcpy(event->name, key_val(rule, &rule->name), sizeof(event->name)); + udev_rules_apply_format(event, event->name, sizeof(event->name)); if (rule->string_escape == ESCAPE_UNSET || rule->string_escape == ESCAPE_REPLACE) { - count = util_replace_chars(udevice->name, ALLOWED_CHARS_FILE); + count = util_replace_chars(event->name, ALLOWED_CHARS_FILE); if (count > 0) - info(udevice->udev, "%i character(s) replaced\n", count); + info(event->udev, "%i character(s) replaced\n", count); } - info(udevice->udev, "rule applied, '%s' becomes '%s'\n", udevice->dev->kernel, udevice->name); - if (strcmp(udevice->dev->subsystem, "net") != 0) - dbg(udevice->udev, "name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i\n", - udevice->name, udevice->owner, udevice->group, udevice->mode, udevice->partitions); + info(event->udev, "rule applied, '%s' becomes '%s'\n", + udev_device_get_sysname(dev), event->name); + if (strcmp(udev_device_get_subsystem(dev), "net") != 0) + dbg(event->udev, "'%s' owner='%s', group='%s', mode=%#o partitions=%i\n", + event->name, event->owner, event->group, event->mode, + udev_device_get_num_fake_partitions(dev)); } - if (!udevice->run_final && rule->run.operation != KEY_OP_UNSET) { - struct name_entry *entry; + if (!event->run_final && rule->run.operation != KEY_OP_UNSET) { + struct udev_list_entry *list_entry; if (rule->run.operation == KEY_OP_ASSIGN_FINAL) - udevice->run_final = 1; + event->run_final = 1; if (rule->run.operation == KEY_OP_ASSIGN || rule->run.operation == KEY_OP_ASSIGN_FINAL) { - info(udevice->udev, "reset run list\n"); - name_list_cleanup(udevice->udev, &udevice->run_list); + info(event->udev, "reset run list\n"); + udev_list_cleanup(event->udev, &event->run_list); } - dbg(udevice->udev, "add run '%s'\n", key_val(rule, &rule->run)); - entry = name_list_add(udevice->udev, &udevice->run_list, key_val(rule, &rule->run), 0); - if (rule->run_ignore_error) - entry->ignore_error = 1; + dbg(event->udev, "add run '%s'\n", key_val(rule, &rule->run)); + list_entry = udev_list_entry_add(event->udev, &event->run_list, + key_val(rule, &rule->run), NULL, 1, 0); + if (rule->run_ignore_error && list_entry != NULL) + udev_list_entry_set_flag(list_entry, 1); } if (rule->last_rule) { - dbg(udevice->udev, "last rule to be applied\n"); + dbg(event->udev, "last rule to be applied\n"); break; } if (rule->goto_label.operation != KEY_OP_UNSET) { - dbg(udevice->udev, "moving forward to label '%s'\n", key_val(rule, &rule->goto_label)); + dbg(event->udev, "moving forward to label '%s'\n", key_val(rule, &rule->goto_label)); udev_rules_iter_goto(&iter, rule->goto_rule_off); } } } if (!name_set) { - info(udevice->udev, "no node name set, will use kernel name '%s'\n", udevice->dev->kernel); - util_strlcpy(udevice->name, udevice->dev->kernel, sizeof(udevice->name)); + info(event->udev, "no node name set, will use kernel name '%s'\n", + udev_device_get_sysname(dev)); + util_strlcpy(event->name, udev_device_get_sysname(dev), sizeof(event->name)); } - if (udevice->tmp_node[0] != '\0') { - dbg(udevice->udev, "removing temporary device node\n"); - unlink_secure(udevice->udev, udevice->tmp_node); - udevice->tmp_node[0] = '\0'; + if (event->tmp_node[0] != '\0') { + dbg(event->udev, "removing temporary device node\n"); + unlink_secure(event->udev, event->tmp_node); + event->tmp_node[0] = '\0'; } - return 0; } -int udev_rules_get_run(struct udev_rules *rules, struct udevice *udevice) +int udev_rules_get_run(struct udev_rules *rules, struct udev_event *event) { + struct udev_device *dev = event->dev; struct udev_rules_iter iter; struct udev_rule *rule; - dbg(udevice->udev, "udevice->kernel='%s'\n", udevice->dev->kernel); + dbg(event->udev, "sysname: '%s'\n", udev_device_get_sysname(dev)); /* look for a matching rule to apply */ udev_rules_iter_init(&iter, rules); @@ -1562,7 +1577,7 @@ int udev_rules_get_run(struct udev_rules *rules, struct udevice *udevice) if (rule == NULL) break; - dbg(udevice->udev, "process rule\n"); + dbg(event->udev, "process rule\n"); if (rule->name.operation == KEY_OP_ASSIGN || rule->name.operation == KEY_OP_ASSIGN_FINAL || rule->name.operation == KEY_OP_ADD || @@ -1571,44 +1586,45 @@ int udev_rules_get_run(struct udev_rules *rules, struct udevice *udevice) rule->symlink.operation == KEY_OP_ADD || rule->mode.operation != KEY_OP_UNSET || rule->owner.operation != KEY_OP_UNSET || rule->group.operation != KEY_OP_UNSET) { - dbg(udevice->udev, "skip rule that names a device\n"); + dbg(event->udev, "skip rule that names a device\n"); continue; } - if (match_rule(udevice, rule) == 0) { + if (match_rule(event, rule) == 0) { if (rule->ignore_device) { - info(udevice->udev, "rule applied, '%s' is ignored\n", udevice->dev->kernel); - udevice->ignore_device = 1; + info(event->udev, "rule applied, '%s' is ignored\n", udev_device_get_sysname(dev)); + event->ignore_device = 1; return 0; } if (rule->ignore_remove) { - udevice->ignore_remove = 1; - dbg(udevice->udev, "remove event should be ignored\n"); + udev_device_set_ignore_remove(dev, 1); + dbg(event->udev, "remove event should be ignored\n"); } - if (!udevice->run_final && rule->run.operation != KEY_OP_UNSET) { - struct name_entry *entry; + if (!event->run_final && rule->run.operation != KEY_OP_UNSET) { + struct udev_list_entry *list_entry; if (rule->run.operation == KEY_OP_ASSIGN || rule->run.operation == KEY_OP_ASSIGN_FINAL) { - info(udevice->udev, "reset run list\n"); - name_list_cleanup(udevice->udev, &udevice->run_list); + info(event->udev, "reset run list\n"); + udev_list_cleanup(event->udev, &event->run_list); } - dbg(udevice->udev, "add run '%s'\n", key_val(rule, &rule->run)); - entry = name_list_add(udevice->udev, &udevice->run_list, key_val(rule, &rule->run), 0); - if (rule->run_ignore_error) - entry->ignore_error = 1; + dbg(event->udev, "add run '%s'\n", key_val(rule, &rule->run)); + list_entry = udev_list_entry_add(event->udev, &event->run_list, + key_val(rule, &rule->run), NULL, 1, 0); + if (rule->run_ignore_error && list_entry != NULL) + udev_list_entry_set_flag(list_entry, 1); if (rule->run.operation == KEY_OP_ASSIGN_FINAL) break; } if (rule->last_rule) { - dbg(udevice->udev, "last rule to be applied\n"); + dbg(event->udev, "last rule to be applied\n"); break; } if (rule->goto_label.operation != KEY_OP_UNSET) { - dbg(udevice->udev, "moving forward to label '%s'\n", key_val(rule, &rule->goto_label)); + dbg(event->udev, "moving forward to label '%s'\n", key_val(rule, &rule->goto_label)); udev_rules_iter_goto(&iter, rule->goto_rule_off); } } diff --git a/udev/udev_rules.h b/udev/udev_rules.h index af96b8119..d8f2f367c 100644 --- a/udev/udev_rules.h +++ b/udev/udev_rules.h @@ -125,10 +125,10 @@ extern void udev_rules_iter_init(struct udev_rules_iter *iter, struct udev_rules extern struct udev_rule *udev_rules_iter_next(struct udev_rules_iter *iter); extern struct udev_rule *udev_rules_iter_goto(struct udev_rules_iter *iter, size_t rule_off); -extern int udev_rules_get_name(struct udev_rules *rules, struct udevice *udev); -extern int udev_rules_get_run(struct udev_rules *rules, struct udevice *udev); -extern int udev_rules_run(struct udevice *udev); +extern int udev_rules_get_name(struct udev_rules *rules, struct udev_event *event); +extern int udev_rules_get_run(struct udev_rules *rules, struct udev_event *event); +extern int udev_rules_run(struct udev_event *event); -extern void udev_rules_apply_format(struct udevice *udev, char *string, size_t maxsize); +extern void udev_rules_apply_format(struct udev_event *event, char *string, size_t maxsize); #endif diff --git a/udev/udev_sysfs.c b/udev/udev_sysfs.c deleted file mode 100644 index 2ea724a78..000000000 --- a/udev/udev_sysfs.c +++ /dev/null @@ -1,499 +0,0 @@ -/* - * Copyright (C) 2005-2008 Kay Sievers - * - * 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 . - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "udev.h" - -/* device cache */ -static LIST_HEAD(dev_list); - -/* attribute value cache */ -static LIST_HEAD(attr_list); - -struct sysfs_attr { - struct list_head node; - char path[UTIL_PATH_SIZE]; - char *value; /* points to value_local if value is cached */ - char value_local[UTIL_NAME_SIZE]; -}; - -static int resolve_sys_link(struct udev *udev, char *path, size_t size) -{ - char link_path[UTIL_PATH_SIZE]; - char link_target[UTIL_PATH_SIZE]; - - int len; - int i; - int back; - - util_strlcpy(link_path, udev_get_sys_path(udev), sizeof(link_path)); - util_strlcat(link_path, path, sizeof(link_path)); - len = readlink(link_path, link_target, sizeof(link_target)); - if (len <= 0) - return -1; - link_target[len] = '\0'; - dbg(udev, "path link '%s' points to '%s'\n", path, link_target); - - for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++) - ; - dbg(udev, "base '%s', tail '%s', back %i\n", path, &link_target[back * 3], back); - for (i = 0; i <= back; i++) { - char *pos = strrchr(path, '/'); - - if (pos == NULL) - return -1; - pos[0] = '\0'; - } - dbg(udev, "after moving back '%s'\n", path); - util_strlcat(path, "/", size); - util_strlcat(path, &link_target[back * 3], size); - return 0; -} - -int sysfs_init(void) -{ - INIT_LIST_HEAD(&dev_list); - INIT_LIST_HEAD(&attr_list); - return 0; -} - -void sysfs_cleanup(void) -{ - struct sysfs_attr *attr_loop; - struct sysfs_attr *attr_temp; - struct sysfs_device *dev_loop; - struct sysfs_device *dev_temp; - - list_for_each_entry_safe(attr_loop, attr_temp, &attr_list, node) { - list_del(&attr_loop->node); - free(attr_loop); - } - - list_for_each_entry_safe(dev_loop, dev_temp, &dev_list, node) { - list_del(&dev_loop->node); - free(dev_loop); - } -} - -void sysfs_device_set_values(struct udev *udev, - struct sysfs_device *dev, const char *devpath, - const char *subsystem, const char *driver) -{ - char *pos; - - util_strlcpy(dev->devpath, devpath, sizeof(dev->devpath)); - if (subsystem != NULL) - util_strlcpy(dev->subsystem, subsystem, sizeof(dev->subsystem)); - if (driver != NULL) - util_strlcpy(dev->driver, driver, sizeof(dev->driver)); - - /* set kernel name */ - pos = strrchr(dev->devpath, '/'); - if (pos == NULL) - return; - util_strlcpy(dev->kernel, &pos[1], sizeof(dev->kernel)); - dbg(udev, "kernel='%s'\n", dev->kernel); - - /* some devices have '!' in their name, change that to '/' */ - pos = dev->kernel; - while (pos[0] != '\0') { - if (pos[0] == '!') - pos[0] = '/'; - pos++; - } - - /* get kernel number */ - pos = &dev->kernel[strlen(dev->kernel)]; - while (isdigit(pos[-1])) - pos--; - util_strlcpy(dev->kernel_number, pos, sizeof(dev->kernel_number)); - dbg(udev, "kernel_number='%s'\n", dev->kernel_number); -} - -struct sysfs_device *sysfs_device_get(struct udev *udev, const char *devpath) -{ - char path[UTIL_PATH_SIZE]; - char devpath_real[UTIL_PATH_SIZE]; - struct sysfs_device *dev; - struct sysfs_device *dev_loop; - struct stat statbuf; - char link_path[UTIL_PATH_SIZE]; - char link_target[UTIL_PATH_SIZE]; - int len; - char *pos; - - /* we handle only these devpathes */ - if (devpath != NULL && - strncmp(devpath, "/devices/", 9) != 0 && - strncmp(devpath, "/subsystem/", 11) != 0 && - strncmp(devpath, "/module/", 8) != 0 && - strncmp(devpath, "/bus/", 5) != 0 && - strncmp(devpath, "/class/", 7) != 0 && - strncmp(devpath, "/block/", 7) != 0) - return NULL; - - dbg(udev, "open '%s'\n", devpath); - util_strlcpy(devpath_real, devpath, sizeof(devpath_real)); - util_remove_trailing_chars(devpath_real, '/'); - if (devpath[0] == '\0' ) - return NULL; - - /* look for device already in cache (we never put an untranslated path in the cache) */ - list_for_each_entry(dev_loop, &dev_list, node) { - if (strcmp(dev_loop->devpath, devpath_real) == 0) { - dbg(udev, "found in cache '%s'\n", dev_loop->devpath); - return dev_loop; - } - } - - /* if we got a link, resolve it to the real device */ - util_strlcpy(path, udev_get_sys_path(udev), sizeof(path)); - util_strlcat(path, devpath_real, sizeof(path)); - if (lstat(path, &statbuf) != 0) { - dbg(udev, "stat '%s' failed: %m\n", path); - return NULL; - } - if (S_ISLNK(statbuf.st_mode)) { - if (resolve_sys_link(udev, devpath_real, sizeof(devpath_real)) != 0) - return NULL; - - /* now look for device in cache after path translation */ - list_for_each_entry(dev_loop, &dev_list, node) { - if (strcmp(dev_loop->devpath, devpath_real) == 0) { - dbg(udev, "found in cache '%s'\n", dev_loop->devpath); - return dev_loop; - } - } - } - - /* it is a new device */ - dbg(udev, "new uncached device '%s'\n", devpath_real); - dev = malloc(sizeof(struct sysfs_device)); - if (dev == NULL) - return NULL; - memset(dev, 0x00, sizeof(struct sysfs_device)); - - sysfs_device_set_values(udev, dev, devpath_real, NULL, NULL); - - /* get subsystem name */ - util_strlcpy(link_path, udev_get_sys_path(udev), sizeof(link_path)); - util_strlcat(link_path, dev->devpath, sizeof(link_path)); - util_strlcat(link_path, "/subsystem", sizeof(link_path)); - len = readlink(link_path, link_target, sizeof(link_target)); - if (len > 0) { - /* get subsystem from "subsystem" link */ - link_target[len] = '\0'; - dbg(udev, "subsystem link '%s' points to '%s'\n", link_path, link_target); - pos = strrchr(link_target, '/'); - if (pos != NULL) - util_strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem)); - } else if (strstr(dev->devpath, "/drivers/") != NULL) { - util_strlcpy(dev->subsystem, "drivers", sizeof(dev->subsystem)); - } else if (strncmp(dev->devpath, "/module/", 8) == 0) { - util_strlcpy(dev->subsystem, "module", sizeof(dev->subsystem)); - } else if (strncmp(dev->devpath, "/subsystem/", 11) == 0) { - pos = strrchr(dev->devpath, '/'); - if (pos == &dev->devpath[10]) - util_strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem)); - } else if (strncmp(dev->devpath, "/class/", 7) == 0) { - pos = strrchr(dev->devpath, '/'); - if (pos == &dev->devpath[6]) - util_strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem)); - } else if (strncmp(dev->devpath, "/bus/", 5) == 0) { - pos = strrchr(dev->devpath, '/'); - if (pos == &dev->devpath[4]) - util_strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem)); - } - - /* get driver name */ - util_strlcpy(link_path, udev_get_sys_path(udev), sizeof(link_path)); - util_strlcat(link_path, dev->devpath, sizeof(link_path)); - util_strlcat(link_path, "/driver", sizeof(link_path)); - len = readlink(link_path, link_target, sizeof(link_target)); - if (len > 0) { - link_target[len] = '\0'; - dbg(udev, "driver link '%s' points to '%s'\n", link_path, link_target); - pos = strrchr(link_target, '/'); - if (pos != NULL) - util_strlcpy(dev->driver, &pos[1], sizeof(dev->driver)); - } - - dbg(udev, "add to cache 'devpath=%s', subsystem='%s', driver='%s'\n", dev->devpath, dev->subsystem, dev->driver); - list_add(&dev->node, &dev_list); - - return dev; -} - -struct sysfs_device *sysfs_device_get_parent(struct udev *udev, struct sysfs_device *dev) -{ - char parent_devpath[UTIL_PATH_SIZE]; - char *pos; - - dbg(udev, "open '%s'\n", dev->devpath); - - /* look if we already know the parent */ - if (dev->parent != NULL) - return dev->parent; - - util_strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath)); - dbg(udev, "'%s'\n", parent_devpath); - - /* strip last element */ - pos = strrchr(parent_devpath, '/'); - if (pos == NULL || pos == parent_devpath) - return NULL; - pos[0] = '\0'; - - if (strncmp(parent_devpath, "/class", 6) == 0) { - pos = strrchr(parent_devpath, '/'); - if (pos == &parent_devpath[6] || pos == parent_devpath) { - dbg(udev, "/class top level, look for device link\n"); - goto device_link; - } - } - if (strcmp(parent_devpath, "/block") == 0) { - dbg(udev, "/block top level, look for device link\n"); - goto device_link; - } - - /* are we at the top level? */ - pos = strrchr(parent_devpath, '/'); - if (pos == NULL || pos == parent_devpath) - return NULL; - - /* get parent and remember it */ - dev->parent = sysfs_device_get(udev, parent_devpath); - return dev->parent; - -device_link: - util_strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath)); - util_strlcat(parent_devpath, "/device", sizeof(parent_devpath)); - if (resolve_sys_link(udev, parent_devpath, sizeof(parent_devpath)) != 0) - return NULL; - - /* get parent and remember it */ - dev->parent = sysfs_device_get(udev, parent_devpath); - return dev->parent; -} - -struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct udev *udev, struct sysfs_device *dev, const char *subsystem) -{ - struct sysfs_device *dev_parent; - - dev_parent = sysfs_device_get_parent(udev, dev); - while (dev_parent != NULL) { - if (strcmp(dev_parent->subsystem, subsystem) == 0) - return dev_parent; - dev_parent = sysfs_device_get_parent(udev, dev_parent); - } - return NULL; -} - -char *sysfs_attr_get_value(struct udev *udev, const char *devpath, const char *attr_name) -{ - char path_full[UTIL_PATH_SIZE]; - const char *path; - char value[UTIL_NAME_SIZE]; - struct sysfs_attr *attr_loop; - struct sysfs_attr *attr; - struct stat statbuf; - int fd; - ssize_t size; - size_t sysfs_len; - - dbg(udev, "open '%s'/'%s'\n", devpath, attr_name); - sysfs_len = util_strlcpy(path_full, udev_get_sys_path(udev), sizeof(path_full)); - if(sysfs_len >= sizeof(path_full)) - sysfs_len = sizeof(path_full) - 1; - path = &path_full[sysfs_len]; - util_strlcat(path_full, devpath, sizeof(path_full)); - util_strlcat(path_full, "/", sizeof(path_full)); - util_strlcat(path_full, attr_name, sizeof(path_full)); - - /* look for attribute in cache */ - list_for_each_entry(attr_loop, &attr_list, node) { - if (strcmp(attr_loop->path, path) == 0) { - dbg(udev, "found in cache '%s'\n", attr_loop->path); - return attr_loop->value; - } - } - - /* store attribute in cache (also negatives are kept in cache) */ - dbg(udev, "new uncached attribute '%s'\n", path_full); - attr = malloc(sizeof(struct sysfs_attr)); - if (attr == NULL) - return NULL; - memset(attr, 0x00, sizeof(struct sysfs_attr)); - util_strlcpy(attr->path, path, sizeof(attr->path)); - dbg(udev, "add to cache '%s'\n", path_full); - list_add(&attr->node, &attr_list); - - if (lstat(path_full, &statbuf) != 0) { - dbg(udev, "stat '%s' failed: %m\n", path_full); - goto out; - } - - if (S_ISLNK(statbuf.st_mode)) { - /* links return the last element of the target path */ - char link_target[UTIL_PATH_SIZE]; - int len; - const char *pos; - - len = readlink(path_full, link_target, sizeof(link_target)); - if (len > 0) { - link_target[len] = '\0'; - pos = strrchr(link_target, '/'); - if (pos != NULL) { - dbg(udev, "cache '%s' with link value '%s'\n", path_full, value); - util_strlcpy(attr->value_local, &pos[1], sizeof(attr->value_local)); - attr->value = attr->value_local; - } - } - goto out; - } - - /* skip directories */ - if (S_ISDIR(statbuf.st_mode)) - goto out; - - /* skip non-readable files */ - if ((statbuf.st_mode & S_IRUSR) == 0) - goto out; - - /* read attribute value */ - fd = open(path_full, O_RDONLY); - if (fd < 0) { - dbg(udev, "attribute '%s' can not be opened\n", path_full); - goto out; - } - size = read(fd, value, sizeof(value)); - close(fd); - if (size < 0) - goto out; - if (size == sizeof(value)) - goto out; - - /* got a valid value, store and return it */ - value[size] = '\0'; - util_remove_trailing_chars(value, '\n'); - dbg(udev, "cache '%s' with attribute value '%s'\n", path_full, value); - util_strlcpy(attr->value_local, value, sizeof(attr->value_local)); - attr->value = attr->value_local; - -out: - return attr->value; -} - -int sysfs_lookup_devpath_by_subsys_id(struct udev *udev, char *devpath_full, size_t len, const char *subsystem, const char *id) -{ - size_t sysfs_len; - char path_full[UTIL_PATH_SIZE]; - char *path; - struct stat statbuf; - - sysfs_len = util_strlcpy(path_full, udev_get_sys_path(udev), sizeof(path_full)); - path = &path_full[sysfs_len]; - - if (strcmp(subsystem, "subsystem") == 0) { - util_strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len); - util_strlcat(path, id, sizeof(path_full) - sysfs_len); - if (stat(path_full, &statbuf) == 0) - goto found; - - util_strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len); - util_strlcat(path, id, sizeof(path_full) - sysfs_len); - if (stat(path_full, &statbuf) == 0) - goto found; - goto out; - - util_strlcpy(path, "/class/", sizeof(path_full) - sysfs_len); - util_strlcat(path, id, sizeof(path_full) - sysfs_len); - if (stat(path_full, &statbuf) == 0) - goto found; - } - - if (strcmp(subsystem, "module") == 0) { - util_strlcpy(path, "/module/", sizeof(path_full) - sysfs_len); - util_strlcat(path, id, sizeof(path_full) - sysfs_len); - if (stat(path_full, &statbuf) == 0) - goto found; - goto out; - } - - if (strcmp(subsystem, "drivers") == 0) { - char subsys[UTIL_NAME_SIZE]; - char *driver; - - util_strlcpy(subsys, id, sizeof(subsys)); - driver = strchr(subsys, ':'); - if (driver != NULL) { - driver[0] = '\0'; - driver = &driver[1]; - util_strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len); - util_strlcat(path, subsys, sizeof(path_full) - sysfs_len); - util_strlcat(path, "/drivers/", sizeof(path_full) - sysfs_len); - util_strlcat(path, driver, sizeof(path_full) - sysfs_len); - if (stat(path_full, &statbuf) == 0) - goto found; - - util_strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len); - util_strlcat(path, subsys, sizeof(path_full) - sysfs_len); - util_strlcat(path, "/drivers/", sizeof(path_full) - sysfs_len); - util_strlcat(path, driver, sizeof(path_full) - sysfs_len); - if (stat(path_full, &statbuf) == 0) - goto found; - } - goto out; - } - - util_strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len); - util_strlcat(path, subsystem, sizeof(path_full) - sysfs_len); - util_strlcat(path, "/devices/", sizeof(path_full) - sysfs_len); - util_strlcat(path, id, sizeof(path_full) - sysfs_len); - if (stat(path_full, &statbuf) == 0) - goto found; - - util_strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len); - util_strlcat(path, subsystem, sizeof(path_full) - sysfs_len); - util_strlcat(path, "/devices/", sizeof(path_full) - sysfs_len); - util_strlcat(path, id, sizeof(path_full) - sysfs_len); - if (stat(path_full, &statbuf) == 0) - goto found; - - util_strlcpy(path, "/class/", sizeof(path_full) - sysfs_len); - util_strlcat(path, subsystem, sizeof(path_full) - sysfs_len); - util_strlcat(path, "/", sizeof(path_full) - sysfs_len); - util_strlcat(path, id, sizeof(path_full) - sysfs_len); - if (stat(path_full, &statbuf) == 0) - goto found; -out: - return 0; -found: - if (S_ISLNK(statbuf.st_mode)) - resolve_sys_link(udev, path, sizeof(path_full) - sysfs_len); - util_strlcpy(devpath_full, path, len); - return 1; -} diff --git a/udev/udevadm-info.c b/udev/udevadm-info.c index b18f24949..78c218eef 100644 --- a/udev/udevadm-info.c +++ b/udev/udevadm-info.c @@ -141,15 +141,15 @@ static void print_record(struct udev_device *device) if (str != NULL) printf("N: %s\n", &str[len+1]); - i = device_get_devlink_priority(device); + i = udev_device_get_devlink_priority(device); if (i != 0) printf("L: %i\n", i); - i = device_get_num_fake_partitions(device); + i = udev_device_get_num_fake_partitions(device); if (i != 0) printf("A:%u\n", i); - i = device_get_ignore_remove(device); + i = udev_device_get_ignore_remove(device); if (i != 0) printf("R:%u\n", i); diff --git a/udev/udevadm-test.c b/udev/udevadm-test.c index f4b0125d3..70da9ebf6 100644 --- a/udev/udevadm-test.c +++ b/udev/udevadm-test.c @@ -31,60 +31,20 @@ #include "udev.h" #include "udev_rules.h" -static int import_uevent_var(struct udev *udev, const char *devpath) -{ - char path[UTIL_PATH_SIZE]; - static char value[4096]; /* must stay, used with putenv */ - ssize_t size; - int fd; - char *key; - char *next; - int rc = -1; - - /* read uevent file */ - util_strlcpy(path, udev_get_sys_path(udev), sizeof(path)); - util_strlcat(path, devpath, sizeof(path)); - util_strlcat(path, "/uevent", sizeof(path)); - fd = open(path, O_RDONLY); - if (fd < 0) - goto out; - size = read(fd, value, sizeof(value)); - close(fd); - if (size < 0) - goto out; - value[size] = '\0'; - - /* import keys into environment */ - key = value; - while (key[0] != '\0') { - next = strchr(key, '\n'); - if (next == NULL) - goto out; - next[0] = '\0'; - info(udev, "import into environment: '%s'\n", key); - putenv(key); - key = &next[1]; - } - rc = 0; -out: - return rc; -} - int udevadm_test(struct udev *udev, int argc, char *argv[]) { + char filename[UTIL_PATH_SIZE]; int force = 0; const char *action = "add"; - const char *subsystem = NULL; - const char *devpath = NULL; - struct udevice *udevice; - struct sysfs_device *dev; + const char *syspath = NULL; + struct udev_event *event; + struct udev_device *dev; struct udev_rules rules = {}; - int retval; + int err; int rc = 0; static const struct option options[] = { { "action", required_argument, NULL, 'a' }, - { "subsystem", required_argument, NULL, 's' }, { "force", no_argument, NULL, 'f' }, { "help", no_argument, NULL, 'h' }, {} @@ -92,14 +52,6 @@ int udevadm_test(struct udev *udev, int argc, char *argv[]) info(udev, "version %s\n", VERSION); - /* export log priority to executed programs */ - if (udev_get_log_priority(udev) > 0) { - char priority[32]; - - sprintf(priority, "%i", udev_get_log_priority(udev)); - setenv("UDEV_LOG", priority, 1); - } - while (1) { int option; @@ -112,16 +64,12 @@ int udevadm_test(struct udev *udev, int argc, char *argv[]) case 'a': action = optarg; break; - case 's': - subsystem = optarg; - break; case 'f': force = 1; break; case 'h': - printf("Usage: udevadm test OPTIONS \n" + printf("Usage: udevadm test OPTIONS \n" " --action= set action string\n" - " --subsystem= set subsystem string\n" " --force don't skip node/link creation\n" " --help print this help text\n\n"); exit(0); @@ -129,10 +77,10 @@ int udevadm_test(struct udev *udev, int argc, char *argv[]) exit(1); } } - devpath = argv[optind]; + syspath = argv[optind]; - if (devpath == NULL) { - fprintf(stderr, "devpath parameter missing\n"); + if (syspath == NULL) { + fprintf(stderr, "syspath parameter missing\n"); rc = 1; goto exit; } @@ -144,60 +92,49 @@ int udevadm_test(struct udev *udev, int argc, char *argv[]) udev_rules_init(udev, &rules, 0); - /* remove /sys if given */ - if (strncmp(devpath, udev_get_sys_path(udev), strlen(udev_get_sys_path(udev))) == 0) - devpath = &devpath[strlen(udev_get_sys_path(udev))]; + /* add /sys if needed */ + if (strncmp(syspath, udev_get_sys_path(udev), strlen(udev_get_sys_path(udev))) != 0) { + util_strlcpy(filename, udev_get_sys_path(udev), sizeof(filename)); + util_strlcat(filename, syspath, sizeof(filename)); + syspath = filename; + } - dev = sysfs_device_get(udev, devpath); + dev = udev_device_new_from_syspath(udev, syspath); if (dev == NULL) { - fprintf(stderr, "unable to open device '%s'\n", devpath); + fprintf(stderr, "unable to open device '%s'\n", syspath); rc = 2; goto exit; } - udevice = udev_device_init(udev); - if (udevice == NULL) { - fprintf(stderr, "error initializing device\n"); - rc = 3; - goto exit; - } + /* skip reading of db, but read kernel parameters */ + udev_device_set_info_loaded(dev); + udev_device_read_uevent_file(dev); - if (subsystem != NULL) - util_strlcpy(dev->subsystem, subsystem, sizeof(dev->subsystem)); - - /* override built-in sysfs device */ - udevice->dev = dev; - util_strlcpy(udevice->action, action, sizeof(udevice->action)); - udevice->devt = udev_device_get_devt(udevice); + udev_device_set_action(dev, action); + event = udev_event_new(dev); /* simulate node creation with test flag */ if (!force) - udevice->test_run = 1; - - setenv("DEVPATH", udevice->dev->devpath, 1); - setenv("SUBSYSTEM", udevice->dev->subsystem, 1); - setenv("ACTION", udevice->action, 1); - import_uevent_var(udev, udevice->dev->devpath); + event->test = 1; - info(udev, "looking at device '%s' from subsystem '%s'\n", udevice->dev->devpath, udevice->dev->subsystem); - retval = udev_device_event(&rules, udevice); + err = udev_event_run(event, &rules); - if (udevice->event_timeout >= 0) - info(udev, "custom event timeout: %i\n", udevice->event_timeout); + if (udev_device_get_event_timeout(dev) >= 0) + info(udev, "custom event timeout: %i\n", udev_device_get_event_timeout(dev)); - if (retval == 0 && !udevice->ignore_device && udev_get_run(udev)) { - struct name_entry *name_loop; + if (err == 0 && !event->ignore_device && udev_get_run(udev)) { + struct udev_list_entry *entry; - list_for_each_entry(name_loop, &udevice->run_list, node) { + udev_list_entry_foreach(entry, udev_list_get_entry(&event->run_list)) { char program[UTIL_PATH_SIZE]; - util_strlcpy(program, name_loop->name, sizeof(program)); - udev_rules_apply_format(udevice, program, sizeof(program)); + util_strlcpy(program, udev_list_entry_get_name(entry), sizeof(program)); + udev_rules_apply_format(event, program, sizeof(program)); info(udev, "run: '%s'\n", program); } } - udev_device_cleanup(udevice); - + udev_event_unref(event); + udev_device_unref(dev); exit: udev_rules_cleanup(&rules); return rc; diff --git a/udev/udevadm.c b/udev/udevadm.c index 3671b9ee5..8fd976a6d 100644 --- a/udev/udevadm.c +++ b/udev/udevadm.c @@ -141,7 +141,6 @@ int main(int argc, char *argv[]) logging_init("udevadm"); udev_set_log_fn(udev, log_fn); selinux_init(udev); - sysfs_init(); /* see if we are a compat link, this will be removed in a future release */ command = argv[0]; @@ -211,7 +210,6 @@ int main(int argc, char *argv[]) help(udev, argc, argv); rc = 2; out: - sysfs_cleanup(); selinux_exit(udev); udev_unref(udev); logging_close(); diff --git a/udev/udevd.c b/udev/udevd.c index ee686c6eb..15ec0fbb0 100644 --- a/udev/udevd.c +++ b/udev/udevd.c @@ -33,11 +33,8 @@ #include #include #include -#include #include #include -#include -#include #ifdef HAVE_INOTIFY #include #endif @@ -65,29 +62,10 @@ static void log_fn(struct udev *udev, int priority, } } -struct udevd_uevent_msg { - struct udev *udev; - struct list_head node; - pid_t pid; - int exitstatus; - time_t queue_time; - char *action; - char *devpath; - char *subsystem; - char *driver; - dev_t devt; - unsigned long long seqnum; - char *devpath_old; - char *physdevpath; - unsigned int timeout; - char *envp[UEVENT_NUM_ENVP+1]; - char envbuf[]; -}; - static int debug_trace; static struct udev_rules rules; static struct udev_ctrl *udev_ctrl; -static int uevent_netlink_sock = -1; +static struct udev_monitor *kernel_monitor; static int inotify_fd = -1; static int signal_pipe[2] = {-1, -1}; @@ -97,156 +75,108 @@ static volatile int reload_config; static int run_exec_q; static int stop_exec_q; static int max_childs; -static char udev_log_env[32]; static LIST_HEAD(exec_list); static LIST_HEAD(running_list); -static void asmlinkage udev_event_sig_handler(int signum) -{ - if (signum == SIGALRM) - exit(1); -} - -static int udev_event_process(struct udevd_uevent_msg *msg) -{ - struct sigaction act; - struct udevice *udevice; - int i; - int retval; - - /* set signal handlers */ - memset(&act, 0x00, sizeof(act)); - act.sa_handler = (void (*)(int)) udev_event_sig_handler; - sigemptyset (&act.sa_mask); - act.sa_flags = 0; - sigaction(SIGALRM, &act, NULL); - - /* reset to default */ - act.sa_handler = SIG_DFL; - sigaction(SIGINT, &act, NULL); - sigaction(SIGTERM, &act, NULL); - sigaction(SIGCHLD, &act, NULL); - sigaction(SIGHUP, &act, NULL); - - /* trigger timeout to prevent hanging processes */ - alarm(UDEV_EVENT_TIMEOUT); - - /* reconstruct event environment from message */ - for (i = 0; msg->envp[i]; i++) - putenv(msg->envp[i]); - - udevice = udev_device_init(msg->udev); - if (udevice == NULL) - return -1; - util_strlcpy(udevice->action, msg->action, sizeof(udevice->action)); - sysfs_device_set_values(udevice->udev, udevice->dev, msg->devpath, msg->subsystem, msg->driver); - udevice->devpath_old = msg->devpath_old; - udevice->devt = msg->devt; - - retval = udev_device_event(&rules, udevice); - - /* rules may change/disable the timeout */ - if (udevice->event_timeout >= 0) - alarm(udevice->event_timeout); - - /* run programs collected by RUN-key*/ - if (retval == 0 && !udevice->ignore_device && udev_get_run(msg->udev)) - retval = udev_rules_run(udevice); - - udev_device_cleanup(udevice); - return retval; -} - enum event_state { EVENT_QUEUED, EVENT_FINISHED, EVENT_FAILED, }; -static void export_event_state(struct udevd_uevent_msg *msg, enum event_state state) +static void export_event_state(struct udev_event *event, enum event_state state) { char filename[UTIL_PATH_SIZE]; char filename_failed[UTIL_PATH_SIZE]; size_t start; /* location of queue file */ - snprintf(filename, sizeof(filename), "%s/.udev/queue/%llu", udev_get_dev_path(msg->udev), msg->seqnum); + snprintf(filename, sizeof(filename), "%s/.udev/queue/%llu", + udev_get_dev_path(event->udev), udev_device_get_seqnum(event->dev)); /* location of failed file */ - util_strlcpy(filename_failed, udev_get_dev_path(msg->udev), sizeof(filename_failed)); + util_strlcpy(filename_failed, udev_get_dev_path(event->udev), sizeof(filename_failed)); util_strlcat(filename_failed, "/", sizeof(filename_failed)); start = util_strlcat(filename_failed, ".udev/failed/", sizeof(filename_failed)); - util_strlcat(filename_failed, msg->devpath, sizeof(filename_failed)); + util_strlcat(filename_failed, udev_device_get_devpath(event->dev), sizeof(filename_failed)); util_path_encode(&filename_failed[start], sizeof(filename_failed) - start); switch (state) { case EVENT_QUEUED: unlink(filename_failed); - delete_path(msg->udev, filename_failed); - create_path(msg->udev, filename); - udev_selinux_setfscreatecon(msg->udev, filename, S_IFLNK); - symlink(msg->devpath, filename); - udev_selinux_resetfscreatecon(msg->udev); + delete_path(event->udev, filename_failed); + create_path(event->udev, filename); + udev_selinux_setfscreatecon(event->udev, filename, S_IFLNK); + symlink(udev_device_get_devpath(event->dev), filename); + udev_selinux_resetfscreatecon(event->udev); break; case EVENT_FINISHED: - if (msg->devpath_old != NULL) { + if (udev_device_get_devpath_old(event->dev) != NULL) { /* "move" event - rename failed file to current name, do not delete failed */ char filename_failed_old[UTIL_PATH_SIZE]; - util_strlcpy(filename_failed_old, udev_get_dev_path(msg->udev), sizeof(filename_failed_old)); + util_strlcpy(filename_failed_old, udev_get_dev_path(event->udev), sizeof(filename_failed_old)); util_strlcat(filename_failed_old, "/", sizeof(filename_failed_old)); start = util_strlcat(filename_failed_old, ".udev/failed/", sizeof(filename_failed_old)); - util_strlcat(filename_failed_old, msg->devpath_old, sizeof(filename_failed_old)); + util_strlcat(filename_failed_old, udev_device_get_devpath_old(event->dev), sizeof(filename_failed_old)); util_path_encode(&filename_failed_old[start], sizeof(filename) - start); if (rename(filename_failed_old, filename_failed) == 0) - info(msg->udev, "renamed devpath, moved failed state of '%s' to %s'\n", - msg->devpath_old, msg->devpath); + info(event->udev, "renamed devpath, moved failed state of '%s' to %s'\n", + udev_device_get_devpath_old(event->dev), udev_device_get_devpath(event->dev)); } else { unlink(filename_failed); - delete_path(msg->udev, filename_failed); + delete_path(event->udev, filename_failed); } unlink(filename); - delete_path(msg->udev, filename); + delete_path(event->udev, filename); break; case EVENT_FAILED: /* move failed event to the failed directory */ - create_path(msg->udev, filename_failed); + create_path(event->udev, filename_failed); rename(filename, filename_failed); /* clean up possibly empty queue directory */ - delete_path(msg->udev, filename); + delete_path(event->udev, filename); break; } return; } -static void msg_queue_delete(struct udevd_uevent_msg *msg) +static void event_queue_delete(struct udev_event *event) { - list_del(&msg->node); + list_del(&event->node); /* mark as failed, if "add" event returns non-zero */ - if (msg->exitstatus && strcmp(msg->action, "add") == 0) - export_event_state(msg, EVENT_FAILED); + if (event->exitstatus && strcmp(udev_device_get_action(event->dev), "add") == 0) + export_event_state(event, EVENT_FAILED); else - export_event_state(msg, EVENT_FINISHED); + export_event_state(event, EVENT_FINISHED); + + udev_device_unref(event->dev); + udev_event_unref(event); +} - free(msg); +static void asmlinkage event_sig_handler(int signum) +{ + if (signum == SIGALRM) + exit(1); } -static void udev_event_run(struct udevd_uevent_msg *msg) +static void event_fork(struct udev_event *event) { pid_t pid; - int retval; + struct sigaction act; + int err; pid = fork(); switch (pid) { case 0: /* child */ - close(uevent_netlink_sock); + udev_monitor_unref(kernel_monitor); udev_ctrl_unref(udev_ctrl); if (inotify_fd >= 0) close(inotify_fd); @@ -256,64 +186,93 @@ static void udev_event_run(struct udevd_uevent_msg *msg) logging_init("udevd-event"); setpriority(PRIO_PROCESS, 0, UDEV_PRIORITY); - retval = udev_event_process(msg); - info(msg->udev, "seq %llu finished with %i\n", msg->seqnum, retval); - + /* set signal handlers */ + memset(&act, 0x00, sizeof(act)); + act.sa_handler = (void (*)(int)) event_sig_handler; + sigemptyset (&act.sa_mask); + act.sa_flags = 0; + sigaction(SIGALRM, &act, NULL); + + /* reset to default */ + act.sa_handler = SIG_DFL; + sigaction(SIGINT, &act, NULL); + sigaction(SIGTERM, &act, NULL); + sigaction(SIGCHLD, &act, NULL); + sigaction(SIGHUP, &act, NULL); + + /* set timeout to prevent hanging processes */ + alarm(UDEV_EVENT_TIMEOUT); + + /* apply rules, create node, symlinks */ + err = udev_event_run(event, &rules); + + /* rules may change/disable the timeout */ + if (udev_device_get_event_timeout(event->dev) >= 0) + alarm(udev_device_get_event_timeout(event->dev)); + + /* execute RUN= */ + if (err == 0 && !event->ignore_device && udev_get_run(event->udev)) + udev_rules_run(event); + info(event->udev, "seq %llu finished with %i\n", udev_device_get_seqnum(event->dev), err); logging_close(); - if (retval) + if (err != 0) exit(1); exit(0); case -1: - err(msg->udev, "fork of child failed: %m\n"); - msg_queue_delete(msg); + err(event->udev, "fork of child failed: %m\n"); + event_queue_delete(event); break; default: /* get SIGCHLD in main loop */ - info(msg->udev, "seq %llu forked, pid [%d], '%s' '%s', %ld seconds old\n", - msg->seqnum, pid, msg->action, msg->subsystem, time(NULL) - msg->queue_time); - msg->pid = pid; + info(event->udev, "seq %llu forked, pid [%d], '%s' '%s', %ld seconds old\n", + udev_device_get_seqnum(event->dev), + pid, + udev_device_get_action(event->dev), + udev_device_get_subsystem(event->dev), + time(NULL) - event->queue_time); + event->pid = pid; } } -static void msg_queue_insert(struct udevd_uevent_msg *msg) +static void event_queue_insert(struct udev_event *event) { char filename[UTIL_PATH_SIZE]; int fd; - msg->queue_time = time(NULL); + event->queue_time = time(NULL); - export_event_state(msg, EVENT_QUEUED); - info(msg->udev, "seq %llu queued, '%s' '%s'\n", msg->seqnum, msg->action, msg->subsystem); + export_event_state(event, EVENT_QUEUED); + info(event->udev, "seq %llu queued, '%s' '%s'\n", udev_device_get_seqnum(event->dev), udev_device_get_action(event->dev), udev_device_get_subsystem(event->dev)); - util_strlcpy(filename, udev_get_dev_path(msg->udev), sizeof(filename)); + util_strlcpy(filename, udev_get_dev_path(event->udev), sizeof(filename)); util_strlcat(filename, "/.udev/uevent_seqnum", sizeof(filename)); fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644); if (fd >= 0) { char str[32]; int len; - len = sprintf(str, "%llu\n", msg->seqnum); + len = sprintf(str, "%llu\n", udev_device_get_seqnum(event->dev)); write(fd, str, len); close(fd); } /* run one event after the other in debug mode */ if (debug_trace) { - list_add_tail(&msg->node, &running_list); - udev_event_run(msg); - waitpid(msg->pid, NULL, 0); - msg_queue_delete(msg); + list_add_tail(&event->node, &running_list); + event_fork(event); + waitpid(event->pid, NULL, 0); + event_queue_delete(event); return; } /* run all events with a timeout set immediately */ - if (msg->timeout != 0) { - list_add_tail(&msg->node, &running_list); - udev_event_run(msg); + if (udev_device_get_timeout(event->dev) > 0) { + list_add_tail(&event->node, &running_list); + event_fork(event); return; } - list_add_tail(&msg->node, &exec_list); + list_add_tail(&event->node, &exec_list); run_exec_q = 1; } @@ -366,80 +325,99 @@ static int compare_devpath(const char *running, const char *waiting) } /* lookup event for identical, parent, child, or physical device */ -static int devpath_busy(struct udevd_uevent_msg *msg, int limit) +static int devpath_busy(struct udev_event *event, int limit) { - struct udevd_uevent_msg *loop_msg; + struct udev_event *loop_event; int childs_count = 0; /* check exec-queue which may still contain delayed events we depend on */ - list_for_each_entry(loop_msg, &exec_list, node) { + list_for_each_entry(loop_event, &exec_list, node) { /* skip ourself and all later events */ - if (loop_msg->seqnum >= msg->seqnum) + if (udev_device_get_seqnum(loop_event->dev) >= udev_device_get_seqnum(event->dev)) break; /* check our old name */ - if (msg->devpath_old != NULL) - if (strcmp(loop_msg->devpath , msg->devpath_old) == 0) + if (udev_device_get_devpath_old(event->dev) != NULL) + if (strcmp(udev_device_get_devpath(loop_event->dev), udev_device_get_devpath_old(event->dev)) == 0) return 2; /* check identical, parent, or child device event */ - if (compare_devpath(loop_msg->devpath, msg->devpath) != 0) { - dbg(msg->udev, "%llu, device event still pending %llu (%s)\n", - msg->seqnum, loop_msg->seqnum, loop_msg->devpath); + if (compare_devpath(udev_device_get_devpath(loop_event->dev), udev_device_get_devpath(event->dev)) != 0) { + dbg(event->udev, "%llu, device event still pending %llu (%s)\n", + udev_device_get_seqnum(event->dev), + udev_device_get_seqnum(loop_event->dev), + udev_device_get_devpath(loop_event->dev)); return 3; } /* check for our major:minor number */ - if (msg->devt && loop_msg->devt == msg->devt && - strcmp(msg->subsystem, loop_msg->subsystem) == 0) { - dbg(msg->udev, "%llu, device event still pending %llu (%d:%d)\n", msg->seqnum, - loop_msg->seqnum, major(loop_msg->devt), minor(loop_msg->devt)); + if (major(udev_device_get_devnum(event->dev)) > 0 && + udev_device_get_devnum(loop_event->dev) == udev_device_get_devnum(event->dev) && + strcmp(udev_device_get_subsystem(event->dev), udev_device_get_subsystem(loop_event->dev)) == 0) { + dbg(event->udev, "%llu, device event still pending %llu (%d:%d)\n", + udev_device_get_seqnum(event->dev), + udev_device_get_seqnum(loop_event->dev), + major(udev_device_get_devnum(loop_event->dev)), minor(udev_device_get_devnum(loop_event->dev))); return 4; } /* check physical device event (special case of parent) */ - if (msg->physdevpath && msg->action && strcmp(msg->action, "add") == 0) - if (compare_devpath(loop_msg->devpath, msg->physdevpath) != 0) { - dbg(msg->udev, "%llu, physical device event still pending %llu (%s)\n", - msg->seqnum, loop_msg->seqnum, loop_msg->devpath); + if (udev_device_get_physdevpath(event->dev) != NULL && + strcmp(udev_device_get_action(event->dev), "add") == 0) + if (compare_devpath(udev_device_get_devpath(loop_event->dev), + udev_device_get_physdevpath(event->dev)) != 0) { + dbg(event->udev, "%llu, physical device event still pending %llu (%s)\n", + udev_device_get_seqnum(event->dev), + udev_device_get_seqnum(loop_event->dev), + udev_device_get_devpath(loop_event->dev)); return 5; } } /* check run queue for still running events */ - list_for_each_entry(loop_msg, &running_list, node) { + list_for_each_entry(loop_event, &running_list, node) { childs_count++; if (childs_count++ >= limit) { - info(msg->udev, "%llu, maximum number (%i) of childs reached\n", msg->seqnum, childs_count); + info(event->udev, "%llu, maximum number (%i) of childs reached\n", + udev_device_get_seqnum(event->dev), childs_count); return 1; } /* check our old name */ - if (msg->devpath_old != NULL) - if (strcmp(loop_msg->devpath , msg->devpath_old) == 0) + if (udev_device_get_devpath_old(event->dev) != NULL) + if (strcmp(udev_device_get_devpath(loop_event->dev), udev_device_get_devpath_old(event->dev)) == 0) return 2; /* check identical, parent, or child device event */ - if (compare_devpath(loop_msg->devpath, msg->devpath) != 0) { - dbg(msg->udev, "%llu, device event still running %llu (%s)\n", - msg->seqnum, loop_msg->seqnum, loop_msg->devpath); + if (compare_devpath(udev_device_get_devpath(loop_event->dev), udev_device_get_devpath(event->dev)) != 0) { + dbg(event->udev, "%llu, device event still running %llu (%s)\n", + udev_device_get_seqnum(event->dev), + udev_device_get_seqnum(loop_event->dev), + udev_device_get_devpath(loop_event->dev)); return 3; } /* check for our major:minor number */ - if (msg->devt && loop_msg->devt == msg->devt && - strcmp(msg->subsystem, loop_msg->subsystem) == 0) { - dbg(msg->udev, "%llu, device event still running %llu (%d:%d)\n", msg->seqnum, - loop_msg->seqnum, major(loop_msg->devt), minor(loop_msg->devt)); + if (major(udev_device_get_devnum(event->dev)) > 0 && + udev_device_get_devnum(loop_event->dev) == udev_device_get_devnum(event->dev) && + strcmp(udev_device_get_subsystem(event->dev), udev_device_get_subsystem(loop_event->dev)) == 0) { + dbg(event->udev, "%llu, device event still pending %llu (%d:%d)\n", + udev_device_get_seqnum(event->dev), + udev_device_get_seqnum(loop_event->dev), + major(udev_device_get_devnum(loop_event->dev)), minor(udev_device_get_devnum(loop_event->dev))); return 4; } /* check physical device event (special case of parent) */ - if (msg->physdevpath && msg->action && strcmp(msg->action, "add") == 0) - if (compare_devpath(loop_msg->devpath, msg->physdevpath) != 0) { - dbg(msg->udev, "%llu, physical device event still running %llu (%s)\n", - msg->seqnum, loop_msg->seqnum, loop_msg->devpath); + if (udev_device_get_physdevpath(event->dev) != NULL && + strcmp(udev_device_get_action(event->dev), "add") == 0) + if (compare_devpath(udev_device_get_devpath(loop_event->dev), + udev_device_get_physdevpath(event->dev)) != 0) { + dbg(event->udev, "%llu, physical device event still pending %llu (%s)\n", + udev_device_get_seqnum(event->dev), + udev_device_get_seqnum(loop_event->dev), + udev_device_get_devpath(loop_event->dev)); return 5; } } @@ -447,97 +425,28 @@ static int devpath_busy(struct udevd_uevent_msg *msg, int limit) } /* serializes events for the identical and parent and child devices */ -static void msg_queue_manager(struct udev *udev) +static void event_queue_manager(struct udev *udev) { - struct udevd_uevent_msg *loop_msg; - struct udevd_uevent_msg *tmp_msg; + struct udev_event *loop_event; + struct udev_event *tmp_event; if (list_empty(&exec_list)) return; - list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, node) { + list_for_each_entry_safe(loop_event, tmp_event, &exec_list, node) { /* serialize and wait for parent or child events */ - if (devpath_busy(loop_msg, max_childs) != 0) { - dbg(udev, "delay seq %llu (%s)\n", loop_msg->seqnum, loop_msg->devpath); + if (devpath_busy(loop_event, max_childs) != 0) { + dbg(udev, "delay seq %llu (%s)\n", + udev_device_get_seqnum(loop_event->dev), + udev_device_get_devpath(loop_event->dev)); continue; } /* move event to run list */ - list_move_tail(&loop_msg->node, &running_list); - udev_event_run(loop_msg); - dbg(udev, "moved seq %llu to running list\n", loop_msg->seqnum); - } -} - -static struct udevd_uevent_msg *get_msg_from_envbuf(struct udev *udev, const char *buf, int buf_size) -{ - int bufpos; - int i; - struct udevd_uevent_msg *msg; - char *physdevdriver_key = NULL; - int maj = 0; - int min = 0; - - msg = malloc(sizeof(struct udevd_uevent_msg) + buf_size); - if (msg == NULL) - return NULL; - memset(msg, 0x00, sizeof(struct udevd_uevent_msg) + buf_size); - msg->udev = udev; - - /* copy environment buffer and reconstruct envp */ - memcpy(msg->envbuf, buf, buf_size); - bufpos = 0; - for (i = 0; (bufpos < buf_size) && (i < UEVENT_NUM_ENVP-2); i++) { - int keylen; - char *key; - - key = &msg->envbuf[bufpos]; - keylen = strlen(key); - msg->envp[i] = key; - bufpos += keylen + 1; - dbg(udev, "add '%s' to msg.envp[%i]\n", msg->envp[i], i); - - /* remember some keys for further processing */ - if (strncmp(key, "ACTION=", 7) == 0) - msg->action = &key[7]; - else if (strncmp(key, "DEVPATH=", 8) == 0) - msg->devpath = &key[8]; - else if (strncmp(key, "SUBSYSTEM=", 10) == 0) - msg->subsystem = &key[10]; - else if (strncmp(key, "DRIVER=", 7) == 0) - msg->driver = &key[7]; - else if (strncmp(key, "SEQNUM=", 7) == 0) - msg->seqnum = strtoull(&key[7], NULL, 10); - else if (strncmp(key, "DEVPATH_OLD=", 12) == 0) - msg->devpath_old = &key[12]; - else if (strncmp(key, "PHYSDEVPATH=", 12) == 0) - msg->physdevpath = &key[12]; - else if (strncmp(key, "PHYSDEVDRIVER=", 14) == 0) - physdevdriver_key = key; - else if (strncmp(key, "MAJOR=", 6) == 0) - maj = strtoull(&key[6], NULL, 10); - else if (strncmp(key, "MINOR=", 6) == 0) - min = strtoull(&key[6], NULL, 10); - else if (strncmp(key, "TIMEOUT=", 8) == 0) - msg->timeout = strtoull(&key[8], NULL, 10); - } - msg->devt = makedev(maj, min); - msg->envp[i++] = "UDEVD_EVENT=1"; - - if (msg->driver == NULL && msg->physdevpath == NULL && physdevdriver_key != NULL) { - /* for older kernels DRIVER is empty for a bus device, export PHYSDEVDRIVER as DRIVER */ - msg->envp[i++] = &physdevdriver_key[7]; - msg->driver = &physdevdriver_key[14]; - } - - msg->envp[i] = NULL; - - if (msg->devpath == NULL || msg->action == NULL) { - info(udev, "DEVPATH or ACTION missing, ignore message\n"); - free(msg); - return NULL; + list_move_tail(&loop_event->node, &running_list); + event_fork(loop_event); + dbg(udev, "moved seq %llu to running list\n", udev_device_get_seqnum(loop_event->dev)); } - return msg; } /* receive the udevd message from userspace */ @@ -556,8 +465,6 @@ static void handle_ctrl_msg(struct udev_ctrl *uctrl) if (i >= 0) { info(udev, "udevd message (SET_LOG_PRIORITY) received, log_priority=%i\n", i); udev_set_log_priority(udev, i); - sprintf(udev_log_env, "UDEV_LOG=%i", i); - putenv(udev_log_env); } if (udev_ctrl_get_stop_exec_queue(ctrl_msg) > 0) { @@ -568,7 +475,7 @@ static void handle_ctrl_msg(struct udev_ctrl *uctrl) if (udev_ctrl_get_start_exec_queue(ctrl_msg) > 0) { info(udev, "udevd message (START_EXEC_QUEUE) received\n"); stop_exec_q = 0; - msg_queue_manager(udev); + event_queue_manager(udev); } if (udev_ctrl_get_reload_rules(ctrl_msg) > 0) { @@ -578,24 +485,28 @@ static void handle_ctrl_msg(struct udev_ctrl *uctrl) str = udev_ctrl_get_set_env(ctrl_msg); if (str != NULL) { - char *key = strdup(str); - char *val; - - val = strchr(str, '='); - if (val != NULL) { - val[0] = '\0'; - val = &val[1]; - if (val[0] == '\0') { - info(udev, "udevd message (ENV) received, unset '%s'\n", key); - unsetenv(str); + char *key; + + key = strdup(str); + if (key != NULL) { + char *val; + + val = strchr(key, '='); + if (val != NULL) { + val[0] = '\0'; + val = &val[1]; + if (val[0] == '\0') { + info(udev, "udevd message (ENV) received, unset '%s'\n", key); + udev_add_property(udev, key, NULL); + } else { + info(udev, "udevd message (ENV) received, set '%s=%s'\n", key, val); + udev_add_property(udev, key, val); + } } else { - info(udev, "udevd message (ENV) received, set '%s=%s'\n", key, val); - setenv(key, val, 1); + err(udev, "wrong key format '%s'\n", key); } - } else { - err(udev, "wrong key format '%s'\n", key); + free(key); } - free(key); } i = udev_ctrl_get_set_max_childs(ctrl_msg); @@ -607,57 +518,6 @@ static void handle_ctrl_msg(struct udev_ctrl *uctrl) udev_ctrl_msg_unref(ctrl_msg); } -/* receive the kernel user event message and do some sanity checks */ -static struct udevd_uevent_msg *get_netlink_msg(struct udev *udev) -{ - struct udevd_uevent_msg *msg; - int bufpos; - ssize_t size; - static char buffer[UEVENT_BUFFER_SIZE+512]; - char *pos; - - size = recv(uevent_netlink_sock, &buffer, sizeof(buffer), 0); - if (size < 0) { - if (errno != EINTR) - err(udev, "unable to receive kernel netlink message: %m\n"); - return NULL; - } - - if ((size_t)size > sizeof(buffer)-1) - size = sizeof(buffer)-1; - buffer[size] = '\0'; - dbg(udev, "uevent_size=%zi\n", size); - - /* start of event payload */ - bufpos = strlen(buffer)+1; - msg = get_msg_from_envbuf(udev, &buffer[bufpos], size-bufpos); - if (msg == NULL) - return NULL; - - /* validate message */ - pos = strchr(buffer, '@'); - if (pos == NULL) { - err(udev, "invalid uevent '%s'\n", buffer); - free(msg); - return NULL; - } - pos[0] = '\0'; - - if (msg->action == NULL) { - info(udev, "no ACTION in payload found, skip event '%s'\n", buffer); - free(msg); - return NULL; - } - - if (strcmp(msg->action, buffer) != 0) { - err(udev, "ACTION in payload does not match uevent, skip event '%s'\n", buffer); - free(msg); - return NULL; - } - - return msg; -} - static void asmlinkage sig_handler(int signum) { switch (signum) { @@ -680,15 +540,16 @@ static void asmlinkage sig_handler(int signum) static void udev_done(int pid, int exitstatus) { - /* find msg associated with pid and delete it */ - struct udevd_uevent_msg *msg; + /* find event associated with pid and delete it */ + struct udev_event *event; - list_for_each_entry(msg, &running_list, node) { - if (msg->pid == pid) { - info(msg->udev, "seq %llu, pid [%d] exit with %i, %ld seconds old\n", msg->seqnum, msg->pid, - exitstatus, time(NULL) - msg->queue_time); - msg->exitstatus = exitstatus; - msg_queue_delete(msg); + list_for_each_entry(event, &running_list, node) { + if (event->pid == pid) { + info(event->udev, "seq %llu, pid [%d] exit with %i, %ld seconds old\n", + udev_device_get_seqnum(event->dev), event->pid, + exitstatus, time(NULL) - event->queue_time); + event->exitstatus = exitstatus; + event_queue_delete(event); /* there may be events waiting with the same devpath */ run_exec_q = 1; @@ -716,36 +577,6 @@ static void reap_sigchilds(void) } } -static int init_uevent_netlink_sock(struct udev *udev) -{ - struct sockaddr_nl snl; - const int buffersize = 16 * 1024 * 1024; - int retval; - - memset(&snl, 0x00, sizeof(struct sockaddr_nl)); - snl.nl_family = AF_NETLINK; - snl.nl_pid = getpid(); - snl.nl_groups = 1; - - uevent_netlink_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT); - if (uevent_netlink_sock == -1) { - err(udev, "error getting socket: %m\n"); - return -1; - } - - /* set receive buffersize */ - setsockopt(uevent_netlink_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize)); - - retval = bind(uevent_netlink_sock, (struct sockaddr *) &snl, sizeof(struct sockaddr_nl)); - if (retval < 0) { - err(udev, "bind failed: %m\n"); - close(uevent_netlink_sock); - uevent_netlink_sock = -1; - return -1; - } - return 0; -} - static void export_initial_seqnum(struct udev *udev) { char filename[UTIL_PATH_SIZE]; @@ -777,7 +608,7 @@ static void export_initial_seqnum(struct udev *udev) int main(int argc, char *argv[]) { struct udev *udev; - int retval; + int err; int fd; struct sigaction act; fd_set readfds; @@ -800,7 +631,7 @@ int main(int argc, char *argv[]) logging_init("udevd"); udev_set_log_fn(udev, log_fn); - dbg(udev, "version %s\n", VERSION); + info(udev, "version %s\n", VERSION); selinux_init(udev); while (1) { @@ -868,45 +699,49 @@ int main(int argc, char *argv[]) goto exit; } - if (init_uevent_netlink_sock(udev) < 0) { + kernel_monitor = udev_monitor_new_from_netlink(udev); + if (kernel_monitor == NULL || udev_monitor_enable_receiving(kernel_monitor) < 0) { fprintf(stderr, "error initializing netlink socket\n"); err(udev, "error initializing netlink socket\n"); rc = 3; goto exit; + } else { + /* set receive buffersize */ + const int buffersize = 32 * 1024 * 1024; + + setsockopt(udev_monitor_get_fd(kernel_monitor), + SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize)); } - retval = pipe(signal_pipe); - if (retval < 0) { + err = pipe(signal_pipe); + if (err < 0) { err(udev, "error getting pipes: %m\n"); goto exit; } - retval = fcntl(signal_pipe[READ_END], F_GETFL, 0); - if (retval < 0) { + err = fcntl(signal_pipe[READ_END], F_GETFL, 0); + if (err < 0) { err(udev, "error fcntl on read pipe: %m\n"); goto exit; } - retval = fcntl(signal_pipe[READ_END], F_SETFL, retval | O_NONBLOCK); - if (retval < 0) { + err = fcntl(signal_pipe[READ_END], F_SETFL, err | O_NONBLOCK); + if (err < 0) { err(udev, "error fcntl on read pipe: %m\n"); goto exit; } - retval = fcntl(signal_pipe[WRITE_END], F_GETFL, 0); - if (retval < 0) { + err = fcntl(signal_pipe[WRITE_END], F_GETFL, 0); + if (err < 0) { err(udev, "error fcntl on write pipe: %m\n"); goto exit; } - retval = fcntl(signal_pipe[WRITE_END], F_SETFL, retval | O_NONBLOCK); - if (retval < 0) { + err = fcntl(signal_pipe[WRITE_END], F_SETFL, err | O_NONBLOCK); + if (err < 0) { err(udev, "error fcntl on write pipe: %m\n"); goto exit; } - /* parse the rules and keep them in memory */ - sysfs_init(); udev_rules_init(udev, &rules, 1); - export_initial_seqnum(udev); if (daemonize) { @@ -1021,28 +856,18 @@ int main(int argc, char *argv[]) } info(udev, "initialize max_childs to %u\n", max_childs); - /* clear environment for forked event processes */ - clearenv(); - - /* export log_priority , as called programs may want to follow that setting */ - sprintf(udev_log_env, "UDEV_LOG=%i", udev_get_log_priority(udev)); - putenv(udev_log_env); - if (debug_trace) - putenv("DEBUG=1"); - maxfd = udev_ctrl_get_fd(udev_ctrl); - maxfd = UDEV_MAX(maxfd, uevent_netlink_sock); + maxfd = UDEV_MAX(maxfd, udev_monitor_get_fd(kernel_monitor)); maxfd = UDEV_MAX(maxfd, signal_pipe[READ_END]); maxfd = UDEV_MAX(maxfd, inotify_fd); while (!udev_exit) { - struct udevd_uevent_msg *msg; int fdcount; FD_ZERO(&readfds); FD_SET(signal_pipe[READ_END], &readfds); FD_SET(udev_ctrl_get_fd(udev_ctrl), &readfds); - FD_SET(uevent_netlink_sock, &readfds); + FD_SET(udev_monitor_get_fd(kernel_monitor), &readfds); if (inotify_fd >= 0) FD_SET(inotify_fd, &readfds); @@ -1057,11 +882,20 @@ int main(int argc, char *argv[]) if (FD_ISSET(udev_ctrl_get_fd(udev_ctrl), &readfds)) handle_ctrl_msg(udev_ctrl); - /* get netlink message */ - if (FD_ISSET(uevent_netlink_sock, &readfds)) { - msg = get_netlink_msg(udev); - if (msg) - msg_queue_insert(msg); + /* get kernel uevent */ + if (FD_ISSET(udev_monitor_get_fd(kernel_monitor), &readfds)) { + struct udev_device *dev; + + dev = udev_monitor_receive_device(kernel_monitor); + if (dev != NULL) { + struct udev_event *event; + + event = udev_event_new(dev); + if (event != NULL) + event_queue_insert(event); + else + udev_device_unref(dev); + } } /* received a signal, clear our notification pipe */ @@ -1098,7 +932,6 @@ int main(int argc, char *argv[]) udev_rules_init(udev, &rules, 1); } - /* forked child has returned */ if (sigchilds_waiting) { sigchilds_waiting = 0; reap_sigchilds(); @@ -1107,14 +940,13 @@ int main(int argc, char *argv[]) if (run_exec_q) { run_exec_q = 0; if (!stop_exec_q) - msg_queue_manager(udev); + event_queue_manager(udev); } } rc = 0; exit: udev_rules_cleanup(&rules); - sysfs_cleanup(); if (signal_pipe[READ_END] >= 0) close(signal_pipe[READ_END]); @@ -1124,8 +956,7 @@ exit: udev_ctrl_unref(udev_ctrl); if (inotify_fd >= 0) close(inotify_fd); - if (uevent_netlink_sock >= 0) - close(uevent_netlink_sock); + udev_monitor_unref(kernel_monitor); selinux_exit(udev); udev_unref(udev);