+
+struct udev_device {
+ int refcount;
+ struct udev *udev;
+ struct udev_device *parent_device;
+ char *syspath;
+ const char *devpath;
+ const char *sysname;
+ char *devname;
+ char *subsystem;
+ struct list_head link_list;
+ struct list_head properties_list;
+ char *action;
+ char *driver;
+ char *devpath_old;
+ char *physdevpath;
+ int timeout;
+ dev_t devnum;
+ unsigned long long int seqnum;
+ int num_fake_partitions;
+ int devlink_priority;
+ int ignore_remove;
+ struct list_head attr_list;
+};
+
+static size_t syspath_to_db_path(struct udev_device *udev_device, char *filename, size_t len)
+{
+ size_t start;
+
+ /* translate to location of db file */
+ util_strlcpy(filename, udev_get_dev_path(udev_device->udev), len);
+ start = util_strlcat(filename, "/.udev/db/", len);
+ util_strlcat(filename, udev_device->devpath, len);
+ return util_path_encode(&filename[start], len - start);
+}
+
+static int device_read_uevent_file(struct udev_device *udev_device)
+{
+ char filename[UTIL_PATH_SIZE];
+ FILE *f;
+ char line[UTIL_LINE_SIZE];
+ int maj = 0;
+ int min = 0;
+
+ util_strlcpy(filename, udev_device->syspath, sizeof(filename));
+ util_strlcat(filename, "/uevent", sizeof(filename));
+ f = fopen(filename, "r");
+ if (f == NULL)
+ return -1;
+
+ while (fgets(line, sizeof(line), f)) {
+ char *pos;
+
+ pos = strchr(line, '\n');
+ if (pos == NULL)
+ continue;
+ pos[0] = '\0';
+
+ if (strncmp(line, "MAJOR=", 6) == 0)
+ maj = strtoull(&line[6], NULL, 10);
+ else if (strncmp(line, "MINOR=", 6) == 0)
+ min = strtoull(&line[6], NULL, 10);
+
+ device_add_property_from_string(udev_device, line);
+ }
+
+ udev_device->devnum = makedev(maj, min);
+
+ fclose(f);
+ return 0;
+}
+
+static int device_read_db(struct udev_device *udev_device)
+{
+ struct stat stats;
+ char filename[UTIL_PATH_SIZE];
+ char line[UTIL_LINE_SIZE];
+ FILE *f;
+
+ 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));
+ return -1;
+ }
+ if ((stats.st_mode & S_IFMT) == S_IFLNK) {
+ char target[UTIL_PATH_SIZE];
+ int target_len;
+
+ target_len = readlink(filename, target, sizeof(target));
+ if (target_len > 0)
+ target[target_len] = '\0';
+ else {
+ info(udev_device->udev, "error reading db link %s: %s\n", filename, strerror(errno));
+ return -1;
+ }
+ if (asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev_device->udev), target) < 0)
+ return -ENOMEM;
+ info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devname);
+ return 0;
+ }
+
+ f = fopen(filename, "r");
+ if (f == NULL) {
+ info(udev_device->udev, "error reading db file %s: %s\n", filename, strerror(errno));
+ return -1;
+ }
+ while (fgets(line, sizeof(line), f)) {
+ ssize_t len;
+ const char *val;
+
+ len = strlen(line);
+ if (len < 4)
+ break;
+ line[len-1] = '\0';
+ val = &line[2];
+
+ switch(line[0]) {
+ case 'N':
+ asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev_device->udev), val);
+ break;
+ case 'S':
+ util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
+ util_strlcat(filename, "/", sizeof(filename));
+ util_strlcat(filename, val, sizeof(filename));
+ device_add_devlink(udev_device, filename);
+ break;
+ case 'L':
+ device_set_devlink_priority(udev_device, atoi(val));
+ break;
+ case 'T':
+ device_set_timeout(udev_device, atoi(val));
+ break;
+ case 'A':
+ device_set_num_fake_partitions(udev_device, atoi(val));
+ break;
+ case 'R':
+ device_set_ignore_remove(udev_device, atoi(val));
+ break;
+ case 'E':
+ device_add_property_from_string(udev_device, val);
+ break;
+ }
+ }
+ fclose(f);
+
+ info(udev_device->udev, "device %p filled with db file data\n", udev_device);
+ return 0;
+}