chiark / gitweb /
libudev: switch to "udev_device_get_parent"
authorKay Sievers <kay.sievers@vrfy.org>
Thu, 11 Sep 2008 22:58:40 +0000 (00:58 +0200)
committerKay Sievers <kay.sievers@vrfy.org>
Thu, 11 Sep 2008 22:58:40 +0000 (00:58 +0200)
Makefile.am
udev/lib/exported_symbols
udev/lib/libudev-device.c
udev/lib/libudev.h
udev/lib/test-libudev.c
udev/udevadm-info.c

index d4afefd..31c0fca 100644 (file)
@@ -47,6 +47,4 @@ git-release:
        head -1 NEWS | grep -q "udev $(VERSION)"
        git commit -a -m "release $(VERSION)"
        cat .git/refs/heads/master > .git/refs/tags/$(VERSION)
-       git gc
-       git prune
-       git-update-server-info
+       git gc --prune
index 058b79a..f53165d 100644 (file)
@@ -8,7 +8,7 @@ udev_set_log_priority
 udev_get_sys_path
 udev_get_dev_path
 udev_device_new_from_devpath
-udev_device_new_from_parent
+udev_device_get_parent
 udev_device_ref
 udev_device_unref
 udev_device_get_udev
index f3f20cd..f400b62 100644 (file)
@@ -34,6 +34,7 @@
 struct udev_device {
        int refcount;
        struct udev *udev;
+       struct udev_device *parent_device;
        char *syspath;
        const char *devpath;
        const char *sysname;
@@ -196,7 +197,7 @@ struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *
        util_strlcat(path, devpath, sizeof(path));
        util_strlcat(path, "/uevent", sizeof(path));
        if (stat(path, &statbuf) != 0) {
-               info(udev, "not a device :%s\n", path);
+               info(udev, "not a device :%s\n", devpath);
                return NULL;
        }
 
@@ -212,11 +213,10 @@ struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *
 
        if (device_read_db(udev_device) >= 0)
                info(udev, "device %p filled with udev database data\n", udev_device);
-
        return udev_device;
 }
 
-struct udev_device *udev_device_new_from_parent(struct udev_device *udev_device)
+static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
 {
        struct udev_device *udev_device_parent = NULL;
        char path[UTIL_PATH_SIZE];
@@ -228,7 +228,7 @@ struct udev_device *udev_device_new_from_parent(struct udev_device *udev_device)
        util_strlcpy(path, udev_device_get_devpath(udev_device), sizeof(path));
        while (1) {
                pos = strrchr(path, '/');
-               if (pos == NULL)
+               if (pos == path || pos == NULL)
                        break;
                pos[0] = '\0';
                udev_device_parent = udev_device_new_from_devpath(udev_device->udev, path);
@@ -238,6 +238,13 @@ struct udev_device *udev_device_new_from_parent(struct udev_device *udev_device)
        return udev_device_parent;
 }
 
+struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
+{
+       if (udev_device->parent_device == NULL)
+               udev_device->parent_device = device_new_from_parent(udev_device);
+       return udev_device->parent_device;
+}
+
 /**
  * udev_device_get_udev:
  * @udev_device: udev device
@@ -284,6 +291,8 @@ void udev_device_unref(struct udev_device *udev_device)
        udev_device->refcount--;
        if (udev_device->refcount > 0)
                return;
+       if (udev_device->parent_device != NULL)
+               udev_device_unref(udev_device->parent_device);
        free(udev_device->syspath);
        free(udev_device->devname);
        free(udev_device->subsystem);
index c3d0092..7033643 100644 (file)
@@ -46,7 +46,7 @@ extern void udev_selinux_lsetfilecon(struct udev *udev, const char *file, unsign
 
 struct udev_device;
 extern struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *devpath);
-extern struct udev_device *udev_device_new_from_parent(struct udev_device *udev_device);
+extern struct udev_device *udev_device_get_parent(struct udev_device *udev_device);
 extern struct udev_device *udev_device_ref(struct udev_device *udev_device);
 extern void udev_device_unref(struct udev_device *udev_device);
 extern struct udev *udev_device_get_udev(struct udev_device *udev_device);
index 7795f5f..b301176 100644 (file)
@@ -24,6 +24,8 @@
 #include <unistd.h>
 #include <errno.h>
 #include <string.h>
+#include <getopt.h>
+#include <syslog.h>
 #include <sys/select.h>
 
 #include "libudev.h"
@@ -89,17 +91,26 @@ static int test_device(struct udev *udev, const char *devpath)
 static int test_device_parents(struct udev *udev, const char *devpath)
 {
        struct udev_device *device;
+       struct udev_device *device_parent;
 
        printf("looking at device: %s\n", devpath);
        device = udev_device_new_from_devpath(udev, devpath);
-       while (device != NULL) {
-               struct udev_device *device_parent;
+       if (device == NULL)
+               return -1;
+
+       device_parent = device;
+       do {
+               print_device(device_parent);
+               device_parent = udev_device_get_parent(device_parent);
+       } while (device_parent != NULL);
+
+       device_parent = device;
+       do {
+               print_device(device_parent);
+               device_parent = udev_device_get_parent(device_parent);
+       } while (device_parent != NULL);
+       udev_device_unref(device);
 
-               print_device(device);
-               device_parent = udev_device_new_from_parent(device);
-               udev_device_unref(device);
-               device = device_parent;
-       }
        return 0;
 }
 
@@ -172,20 +183,21 @@ static int test_monitor(struct udev *udev, const char *socket_path)
 
 int main(int argc, char *argv[], char *envp[])
 {
-       struct udev *udev;
+       struct udev *udev = NULL;
+       static const struct option options[] = {
+               { "devpath", 1, NULL, 'p' },
+               { "subsystem", 1, NULL, 's' },
+               { "socket", 1, NULL, 'S' },
+               { "debug", 0, NULL, 'd' },
+               { "help", 0, NULL, 'h' },
+               { "version", 0, NULL, 'V' },
+               {}
+       };
        const char *devpath = "/devices/virtual/mem/null";
        const char *subsystem = NULL;
        const char *socket = "@/org/kernel/udev/monitor";
        const char *str;
 
-       if (argv[1] != NULL) {
-               devpath = argv[1];
-               if (argv[2] != NULL)
-                       subsystem = argv[2];
-                       if (argv[3] != NULL)
-                               socket = argv[3];
-       }
-
        udev = udev_new();
        printf("context: %p\n", udev);
        if (udev == NULL) {
@@ -195,6 +207,38 @@ int main(int argc, char *argv[], char *envp[])
        udev_set_log_fn(udev, log_fn);
        printf("set log: %p\n", log_fn);
 
+       while (1) {
+               int option;
+
+               option = getopt_long(argc, argv, "+dhV", options, NULL);
+               if (option == -1)
+                       break;
+
+               switch (option) {
+               case 'p':
+                       devpath = optarg;
+                       break;
+               case 's':
+                       subsystem = optarg;
+                       break;
+               case 'S':
+                       socket = optarg;
+                       break;
+               case 'd':
+                       if (udev_get_log_priority(udev) < LOG_INFO)
+                               udev_set_log_priority(udev, LOG_INFO);
+                       break;
+               case 'h':
+                       printf("--debug --devpath= --subsystem= --socket= --help\n");
+                       goto out;
+               case 'V':
+                       printf("%s\n", VERSION);
+                       goto out;
+               default:
+                       goto out;
+               }
+       }
+
        str = udev_get_sys_path(udev);
        printf("sys_path: '%s'\n", str);
        str = udev_get_dev_path(udev);
@@ -204,7 +248,7 @@ int main(int argc, char *argv[], char *envp[])
        test_device_parents(udev, devpath);
        test_enumerate(udev, subsystem);
        test_monitor(udev, socket);
-
+out:
        udev_unref(udev);
        return 0;
 }
index 3be5f31..6f0678e 100644 (file)
@@ -92,6 +92,7 @@ static void print_all_attributes(struct udev *udev, const char *devpath, const c
 static int print_device_chain(struct udev *udev, const char *devpath)
 {
        struct udev_device *device;
+       struct udev_device *device_parent;
        const char *str;
 
        device = udev_device_new_from_devpath(udev, devpath);
@@ -118,27 +119,25 @@ static int print_device_chain(struct udev *udev, const char *devpath)
        printf("    DRIVER==\"%s\"\n", str);
        print_all_attributes(udev, udev_device_get_devpath(device), "ATTR");
 
-       while (device != NULL) {
-               struct udev_device *device_parent;
-
-               device_parent = udev_device_new_from_parent(device);
-               udev_device_unref(device);
+       device_parent = device;
+       do {
+               device_parent = udev_device_get_parent(device_parent);
                if (device_parent == NULL)
                        break;
-               device = device_parent;
-               printf("  looking at parent device '%s':\n", udev_device_get_devpath(device));
-               printf("    KERNELS==\"%s\"\n", udev_device_get_sysname(device));
-               str = udev_device_get_subsystem(device);
+               printf("  looking at parent device '%s':\n", udev_device_get_devpath(device_parent));
+               printf("    KERNELS==\"%s\"\n", udev_device_get_sysname(device_parent));
+               str = udev_device_get_subsystem(device_parent);
                if (str == NULL)
                        str = "";
                printf("    SUBSYSTEMS==\"%s\"\n", str);
-               str = udev_device_get_driver(device);
+               str = udev_device_get_driver(device_parent);
                if (str == NULL)
                        str = "";
                printf("    DRIVERS==\"%s\"\n", str);
-               print_all_attributes(udev, udev_device_get_devpath(device), "ATTRS");
-       }
+               print_all_attributes(udev, udev_device_get_devpath(device_parent), "ATTRS");
+       } while (device_parent != NULL);
 
+       udev_device_unref(device);
        return 0;
 }