chiark / gitweb /
libudev: get devnum from uevent file
authorKay Sievers <kay.sievers@vrfy.org>
Thu, 25 Sep 2008 21:26:22 +0000 (14:26 -0700)
committerKay Sievers <kay.sievers@vrfy.org>
Thu, 25 Sep 2008 21:26:22 +0000 (14:26 -0700)
TODO
udev/lib/libudev-device.c
udev/lib/test-libudev.c

diff --git a/TODO b/TODO
index d35da54..17baf63 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,6 +1,6 @@
   o enumerate() - add buses and drivers
   o increase ressize buffer
   o enumerate() - add buses and drivers
   o increase ressize buffer
-  o set dev_t from "uevent" MAJOR/MINOR
+  o lazy loading of "uevent" and db
 
   o add DVB variables to kernel, and drop shell script rule
   o add watershed extra
 
   o add DVB variables to kernel, and drop shell script rule
   o add watershed extra
index a772dbb..359ae44 100644 (file)
@@ -70,6 +70,8 @@ static int device_read_uevent_file(struct udev_device *udev_device)
        char filename[UTIL_PATH_SIZE];
        FILE *f;
        char line[UTIL_LINE_SIZE];
        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));
 
        util_strlcpy(filename, udev_device->syspath, sizeof(filename));
        util_strlcat(filename, "/uevent", sizeof(filename));
@@ -84,9 +86,17 @@ static int device_read_uevent_file(struct udev_device *udev_device)
                if (pos == NULL)
                        continue;
                pos[0] = '\0';
                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);
        }
 
                device_add_property_from_string(udev_device, line);
        }
 
+       udev_device->devnum = makedev(maj, min);
+
        fclose(f);
        return 0;
 }
        fclose(f);
        return 0;
 }
@@ -97,7 +107,6 @@ static int device_read_db(struct udev_device *udev_device)
        char filename[UTIL_PATH_SIZE];
        char line[UTIL_LINE_SIZE];
        FILE *f;
        char filename[UTIL_PATH_SIZE];
        char line[UTIL_LINE_SIZE];
        FILE *f;
-       int rc = 0;
 
        syspath_to_db_path(udev_device, filename, sizeof(filename));
 
 
        syspath_to_db_path(udev_device, filename, sizeof(filename));
 
@@ -131,7 +140,6 @@ static int device_read_db(struct udev_device *udev_device)
        while (fgets(line, sizeof(line), f)) {
                ssize_t len;
                const char *val;
        while (fgets(line, sizeof(line), f)) {
                ssize_t len;
                const char *val;
-               unsigned int maj, min;
 
                len = strlen(line);
                if (len < 4)
 
                len = strlen(line);
                if (len < 4)
@@ -143,10 +151,6 @@ static int device_read_db(struct udev_device *udev_device)
                case 'N':
                        asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev_device->udev), val);
                        break;
                case 'N':
                        asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev_device->udev), val);
                        break;
-               case 'M':
-                       sscanf(val, "%u:%u", &maj, &min);
-                       device_set_devnum(udev_device, makedev(maj, min));
-                       break;
                case 'S':
                        util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
                        util_strlcat(filename, "/", sizeof(filename));
                case 'S':
                        util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
                        util_strlcat(filename, "/", sizeof(filename));
@@ -157,7 +161,7 @@ static int device_read_db(struct udev_device *udev_device)
                        device_set_devlink_priority(udev_device, atoi(val));
                        break;
                case 'T':
                        device_set_devlink_priority(udev_device, atoi(val));
                        break;
                case 'T':
-                       device_set_timeout(udev_device,  atoi(val));
+                       device_set_timeout(udev_device, atoi(val));
                        break;
                case 'A':
                        device_set_num_fake_partitions(udev_device, atoi(val));
                        break;
                case 'A':
                        device_set_num_fake_partitions(udev_device, atoi(val));
@@ -172,7 +176,8 @@ static int device_read_db(struct udev_device *udev_device)
        }
        fclose(f);
 
        }
        fclose(f);
 
-       return rc;
+       info(udev_device->udev, "device %p filled with udev database data\n", udev_device);
+       return 0;
 }
 
 struct udev_device *device_init(struct udev *udev)
 }
 
 struct udev_device *device_init(struct udev *udev)
@@ -238,8 +243,7 @@ struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *
        info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
 
        device_read_uevent_file(udev_device);
        info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
 
        device_read_uevent_file(udev_device);
-       if (device_read_db(udev_device) >= 0)
-               info(udev, "device %p filled with udev database data\n", udev_device);
+       device_read_db(udev_device);
        return udev_device;
 }
 
        return udev_device;
 }
 
@@ -264,7 +268,7 @@ struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, de
        if (util_resolve_sys_link(udev, path, sizeof(path)) == 0)
                return udev_device_new_from_syspath(udev, path);
 
        if (util_resolve_sys_link(udev, path, sizeof(path)) == 0)
                return udev_device_new_from_syspath(udev, path);
 
-       /* search all sys devices for the major/minor */
+       /* fallback to search all sys devices for the major/minor */
        enumerate = udev_enumerate_new_from_subsystems(udev, NULL);
        if (enumerate == NULL)
                return NULL;
        enumerate = udev_enumerate_new_from_subsystems(udev, NULL);
        if (enumerate == NULL)
                return NULL;
index fed4350..421d051 100644 (file)
@@ -40,6 +40,7 @@ static void log_fn(struct udev *udev,
 static void print_device(struct udev_device *device)
 {
        const char *str;
 static void print_device(struct udev_device *device)
 {
        const char *str;
+       dev_t devnum;
        int count;
        struct udev_list *list;
 
        int count;
        struct udev_list *list;
 
@@ -56,6 +57,8 @@ static void print_device(struct udev_device *device)
        printf("driver:    '%s'\n", str);
        str = udev_device_get_devnode(device);
        printf("devname:   '%s'\n", str);
        printf("driver:    '%s'\n", str);
        str = udev_device_get_devnode(device);
        printf("devname:   '%s'\n", str);
+       devnum = udev_device_get_devnum(device);
+       printf("devnum:    %u:%u\n", major(devnum), minor(devnum));
 
        count = 0;
        list = udev_device_get_devlinks_list(device);
 
        count = 0;
        list = udev_device_get_devlinks_list(device);