X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=udev%2Flib%2Flibudev-device.c;h=893ec080a6159b5dc31e4f4f745b5fcd6855d952;hp=6da6cd93e474cfaa3893bf21c416a8db86f749ef;hb=e0083e8e69856f1b4e1e643cebb08906775cca8a;hpb=e345e2670a8c17f5e1145cc554b7a7646e271032 diff --git a/udev/lib/libudev-device.c b/udev/lib/libudev-device.c index 6da6cd93e..893ec080a 100644 --- a/udev/lib/libudev-device.c +++ b/udev/lib/libudev-device.c @@ -77,7 +77,7 @@ static int device_read_db(struct udev_device *udev_device) syspath_to_db_path(udev_device, filename, sizeof(filename)); if (lstat(filename, &stats) != 0) { - info(udev_device->udev, "no db file to read %s: %s\n", filename, strerror(errno)); + info(udev_device->udev, "no db file to read %s: %m\n", filename); return -1; } if ((stats.st_mode & S_IFMT) == S_IFLNK) { @@ -88,7 +88,7 @@ static int device_read_db(struct udev_device *udev_device) if (target_len > 0) target[target_len] = '\0'; else { - info(udev_device->udev, "error reading db link %s: %s\n", filename, strerror(errno)); + info(udev_device->udev, "error reading db link %s: %m\n", filename); return -1; } if (asprintf(&udev_device->devnode, "%s/%s", udev_get_dev_path(udev_device->udev), target) < 0) @@ -99,7 +99,7 @@ static int device_read_db(struct udev_device *udev_device) f = fopen(filename, "r"); if (f == NULL) { - info(udev_device->udev, "error reading db file %s: %s\n", filename, strerror(errno)); + info(udev_device->udev, "error reading db file %s: %m\n", filename); return -1; } while (fgets(line, sizeof(line), f)) { @@ -193,7 +193,7 @@ void device_set_info_loaded(struct udev_device *device) device->info_loaded = 1; } -struct udev_device *device_init(struct udev *udev) +struct udev_device *device_new(struct udev *udev) { struct udev_device *udev_device; @@ -229,6 +229,9 @@ struct udev_device *device_init(struct udev *udev) **/ struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath) { + size_t len; + const char *subdir; + const char *pos; char path[UTIL_PATH_SIZE]; struct stat statbuf; struct udev_device *udev_device; @@ -238,20 +241,50 @@ struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char * if (syspath == NULL) return NULL; - util_strlcpy(path, syspath, sizeof(path)); - util_strlcat(path, "/uevent", sizeof(path)); - if (stat(path, &statbuf) != 0) { - info(udev, "not a device :%s\n", syspath); + /* path starts in sys */ + len = strlen(udev_get_sys_path(udev)); + if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) { + info(udev, "not in sys :%s\n", syspath); return NULL; } - udev_device = device_init(udev); - if (udev_device == NULL) + /* path is not a root directory */ + subdir = &syspath[len+1]; + pos = strrchr(subdir, '/'); + if (pos == NULL || pos < &subdir[2]) { + info(udev, "not a subdir :%s\n", syspath); return NULL; + } /* resolve possible symlink to real path */ util_strlcpy(path, syspath, sizeof(path)); util_resolve_sys_link(udev, path, sizeof(path)); + + /* path exists in sys */ + if (strncmp(&syspath[len], "/devices/", 9) == 0 || + strncmp(&syspath[len], "/class/", 7) == 0 || + strncmp(&syspath[len], "/block/", 7) == 0) { + char file[UTIL_PATH_SIZE]; + + /* all "devices" require a "uevent" file */ + util_strlcpy(file, path, sizeof(file)); + util_strlcat(file, "/uevent", sizeof(file)); + if (stat(file, &statbuf) != 0) { + info(udev, "not a device: %s\n", syspath); + return NULL; + } + } else { + /* everything else just needs to be a directory */ + if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) { + info(udev, "directory not found: %s\n", syspath); + return NULL; + } + } + + udev_device = device_new(udev); + if (udev_device == NULL) + return NULL; + device_set_syspath(udev_device, path); info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device)); @@ -262,8 +295,8 @@ struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, de { char path[UTIL_PATH_SIZE]; const char *type_str; - struct udev_enumerate *enumerate; - struct udev_list *list; + struct udev_enumerate *udev_enumerate; + struct udev_list_entry *list_entry; struct udev_device *device = NULL; if (type == 'b') @@ -273,31 +306,39 @@ struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, de else return NULL; - /* /sys/dev/{block,char}/: links */ + /* /sys/dev/{block,char}/: link */ snprintf(path, sizeof(path), "%s/dev/%s/%u:%u", udev_get_sys_path(udev), type_str, major(devnum), minor(devnum)); if (util_resolve_sys_link(udev, path, sizeof(path)) == 0) return udev_device_new_from_syspath(udev, path); - /* fallback to search all sys devices for the major/minor */ - enumerate = udev_enumerate_new_from_subsystems(udev, NULL); - if (enumerate == NULL) + udev_enumerate = udev_enumerate_new(udev); + if (udev_enumerate == NULL) return NULL; - list = udev_enumerate_get_list(enumerate); - while (list != NULL) { + + /* fallback to search sys devices for the major/minor */ + if (type == 'b') + udev_enumerate_add_match_subsystem(udev_enumerate, "block"); + else if (type == 'c') + udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block"); + udev_enumerate_scan_devices(udev_enumerate); + udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(udev_enumerate)) { struct udev_device *device_loop; - device_loop = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list)); + device_loop = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry)); if (device_loop != NULL) { if (udev_device_get_devnum(device_loop) == devnum) { + if (type == 'b' && strcmp(udev_device_get_subsystem(device_loop), "block") != 0) + continue; + if (type == 'c' && strcmp(udev_device_get_subsystem(device_loop), "block") == 0) + continue; device = device_loop; break; } udev_device_unref(device_loop); } - list = udev_list_entry_get_next(list); } - udev_enumerate_unref(enumerate); + udev_enumerate_unref(udev_enumerate); return device; } @@ -305,37 +346,39 @@ static struct udev_device *device_new_from_parent(struct udev_device *udev_devic { struct udev_device *udev_device_parent = NULL; char path[UTIL_PATH_SIZE]; - char *pos; + const char *subdir; - if (udev_device == NULL) - return NULL; + /* follow "device" link in deprecated sys layout */ + if (strncmp(udev_device->devpath, "/class/", 7) == 0 || + strncmp(udev_device->devpath, "/block/", 7) == 0) { + util_strlcpy(path, udev_device->syspath, sizeof(path)); + util_strlcat(path, "/device", sizeof(path)); + if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0) + udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path); + return udev_device_parent; + } util_strlcpy(path, udev_device->syspath, sizeof(path)); + subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1]; while (1) { - pos = strrchr(path, '/'); - if (pos == path || pos == NULL) + char *pos; + + pos = strrchr(subdir, '/'); + if (pos == NULL || pos < &subdir[2]) break; pos[0] = '\0'; udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path); if (udev_device_parent != NULL) return udev_device_parent; } - - /* follow "device" link in deprecated sys /sys/class/ layout */ - if (strncmp(udev_device->devpath, "/class/", 7) == 0) { - util_strlcpy(path, udev_device->syspath, sizeof(path)); - util_strlcat(path, "/device", sizeof(path)); - if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0) { - udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path); - if (udev_device_parent != NULL) - return udev_device_parent; - } - } return NULL; } struct udev_device *udev_device_get_parent(struct udev_device *udev_device) { + if (udev_device == NULL) + return NULL; + if (udev_device->parent_device != NULL) { info(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device); return udev_device->parent_device; @@ -506,7 +549,7 @@ const char *udev_device_get_subsystem(struct udev_device *udev_device) } /** - * udev_device_get_devlinks_list: + * udev_device_get_devlinks_list_entry: * @udev_device: udev device * * Retrieve the list of device links pointing to the device file of @@ -518,7 +561,7 @@ const char *udev_device_get_subsystem(struct udev_device *udev_device) * * Returns: the first entry of the device node link list **/ -struct udev_list *udev_device_get_devlinks_list(struct udev_device *udev_device) +struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device) { if (udev_device == NULL) return NULL; @@ -528,7 +571,7 @@ struct udev_list *udev_device_get_devlinks_list(struct udev_device *udev_device) } /** - * udev_device_get_properties_list: + * udev_device_get_properties_list_entry: * @udev_device: udev device * * Retrieve the list of key/value device properties of the udev @@ -539,7 +582,7 @@ struct udev_list *udev_device_get_devlinks_list(struct udev_device *udev_device) * * Returns: the first entry of the property list **/ -struct udev_list *udev_device_get_properties_list(struct udev_device *udev_device) +struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device) { if (udev_device == NULL) return NULL; @@ -587,7 +630,7 @@ unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device) const char *udev_device_get_attr_value(struct udev_device *udev_device, const char *attr) { - struct udev_list *list; + struct udev_list_entry *list_entry; char path[UTIL_PATH_SIZE]; char value[UTIL_NAME_SIZE]; struct stat statbuf; @@ -596,13 +639,11 @@ const char *udev_device_get_attr_value(struct udev_device *udev_device, const ch const char *val = NULL; /* look for possibly already cached result */ - list = list_get_entry(&udev_device->attr_list); - while (list != NULL) { - if (strcmp(udev_list_entry_get_name(list), attr) == 0) { - info(udev_device->udev, "got '%s' (%s) from cache\n", attr, udev_list_entry_get_value(list)); - return udev_list_entry_get_value(list); + udev_list_entry_foreach(list_entry, list_get_entry(&udev_device->attr_list)) { + if (strcmp(udev_list_entry_get_name(list_entry), attr) == 0) { + info(udev_device->udev, "got '%s' (%s) from cache\n", attr, udev_list_entry_get_value(list_entry)); + return udev_list_entry_get_value(list_entry); } - list = udev_list_entry_get_next(list); } util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path)); @@ -610,7 +651,7 @@ const char *udev_device_get_attr_value(struct udev_device *udev_device, const ch util_strlcat(path, attr, sizeof(path)); if (lstat(path, &statbuf) != 0) { - info(udev_device->udev, "stat '%s' failed: %s\n", path, strerror(errno)); + info(udev_device->udev, "stat '%s' failed: %m\n", path); goto out; } @@ -627,8 +668,8 @@ const char *udev_device_get_attr_value(struct udev_device *udev_device, const ch if (pos != NULL) { pos = &pos[1]; info(udev_device->udev, "cache '%s' with link value '%s'\n", attr, pos); - list = list_insert_entry(udev_device->udev, &udev_device->attr_list, attr, pos, 0); - val = udev_list_entry_get_value(list); + list_entry = list_entry_add(udev_device->udev, &udev_device->attr_list, attr, pos, 0, 0); + val = udev_list_entry_get_value(list_entry); } } goto out; @@ -659,8 +700,8 @@ const char *udev_device_get_attr_value(struct udev_device *udev_device, const ch value[size] = '\0'; util_remove_trailing_chars(value, '\n'); info(udev_device->udev, "'%s' has attribute value '%s'\n", path, value); - list = list_insert_entry(udev_device->udev, &udev_device->attr_list, attr, value, 0); - val = udev_list_entry_get_value(list); + list_entry = list_entry_add(udev_device->udev, &udev_device->attr_list, attr, value, 0, 0); + val = udev_list_entry_get_value(list_entry); out: return val; } @@ -697,14 +738,14 @@ int device_set_devnode(struct udev_device *udev_device, const char *devnode) int device_add_devlink(struct udev_device *udev_device, const char *devlink) { - if (list_insert_entry(udev_device->udev, &udev_device->devlink_list, devlink, NULL, 0) == NULL) + if (list_entry_add(udev_device->udev, &udev_device->devlink_list, devlink, NULL, 1, 0) == NULL) return -ENOMEM; return 0; } int device_add_property(struct udev_device *udev_device, const char *key, const char *value) { - if (list_insert_entry(udev_device->udev, &udev_device->properties_list, key, value, 0) == NULL) + if (list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0) == NULL) return -ENOMEM; return 0; }