chiark / gitweb /
libudev: add udev_device_get_syspath()
authorKay Sievers <kay.sievers@vrfy.org>
Mon, 1 Sep 2008 16:52:22 +0000 (18:52 +0200)
committerKay Sievers <kay.sievers@vrfy.org>
Mon, 1 Sep 2008 16:52:22 +0000 (18:52 +0200)
udev/lib/exported_symbols
udev/lib/libudev-device.c
udev/lib/libudev-monitor.c
udev/lib/libudev-private.h
udev/lib/libudev.h
udev/lib/test-libudev.c

index 0826e6f6f1a03aecd5481f111d6488fab8c3f390..4db23b2251244d990da4066217bcb2243f4f3b0a 100644 (file)
@@ -10,6 +10,7 @@ udev_device_ref
 udev_device_unref
 udev_device_get_udev
 udev_device_get_devpath
+udev_device_get_syspath
 udev_device_get_devname
 udev_device_get_subsystem
 udev_device_get_devlinks
index 6465157fbd49ca8036eace793d423b821ca4f379..c4d65e9c49243468619c52bca6d63a698e300a30 100644 (file)
 #include "libudev-private.h"
 #include "../udev.h"
 
+struct udev_device {
+       int refcount;
+       struct udev *udev;
+       char *devpath;
+       char *syspath;
+       char *devname;
+       char *subsystem;
+       struct list_head link_list;
+       struct list_head env_list;
+};
+
 struct udev_device *device_init(struct udev *udev)
 {
        struct udev_device *udev_device;
@@ -99,7 +110,7 @@ struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *
        /* resolve possible symlink to real path */
        strlcpy(path, devpath, sizeof(path));
        sysfs_resolve_link(path, sizeof(path));
-       udev_device->devpath = strdup(path);
+       device_set_devpath(udev_device, devpath);
        log_info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
 
        err = udev_db_get_device(udevice, path);
@@ -171,7 +182,7 @@ void udev_device_unref(struct udev_device *udev_device)
        udev_device->refcount--;
        if (udev_device->refcount > 0)
                return;
-       free(udev_device->devpath);
+       free(udev_device->syspath);
        free(udev_device->devname);
        free(udev_device->subsystem);
        name_list_cleanup(&udev_device->link_list);
@@ -184,10 +195,10 @@ void udev_device_unref(struct udev_device *udev_device)
  * udev_device_get_devpath:
  * @udev_device: udev device
  *
- * Retrieve the sys path of the udev device. The path does not contain
- * the sys mount point.
+ * Retrieve the kernel devpath value of the udev device. The path
+ * does not contain the sys mount point, and starts with a '/'.
  *
- * Returns: the sys path of the udev device
+ * Returns: the devpath of the udev device
  **/
 const char *udev_device_get_devpath(struct udev_device *udev_device)
 {
@@ -196,6 +207,22 @@ const char *udev_device_get_devpath(struct udev_device *udev_device)
        return udev_device->devpath;
 }
 
+/**
+ * udev_device_get_syspath:
+ * @udev_device: udev device
+ *
+ * Retrieve the sys path of the udev device. The path is an
+ * absolute path and starts with the sys mount point.
+ *
+ * Returns: the sys path of the udev device
+ **/
+const char *udev_device_get_syspath(struct udev_device *udev_device)
+{
+       if (udev_device == NULL)
+               return NULL;
+       return udev_device->syspath;
+}
+
 /**
  * udev_device_get_devname:
  * @udev_device: udev device
@@ -305,3 +332,41 @@ int udev_device_get_properties(struct udev_device *udev_device,
        }
        return count;
 }
+
+int device_set_devpath(struct udev_device *udev_device, const char *devpath)
+{
+       if (asprintf(&udev_device->syspath, "%s%s", udev_get_sys_path(udev_device->udev), devpath) < 0)
+               return -ENOMEM;
+       udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
+       return 0;
+}
+
+int device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
+{
+       udev_device->subsystem = strdup(subsystem);
+       if (udev_device->subsystem == NULL)
+               return -1;
+       return 0;
+}
+
+int device_set_devname(struct udev_device *udev_device, const char *devname)
+{
+       udev_device->devname = strdup(devname);
+       if (udev_device->devname == NULL)
+               return -ENOMEM;
+       return 0;
+}
+
+int device_add_devlink(struct udev_device *udev_device, const char *devlink)
+{
+       if (name_list_add(&udev_device->link_list, devlink, 0) == NULL)
+               return -ENOMEM;
+       return 0;
+}
+
+int device_add_property(struct udev_device *udev_device, const char *property)
+{
+       if (name_list_add(&udev_device->env_list, property, 0) == NULL)
+               return -ENOMEM;
+       return 0;
+}
index c8f07eae8f88aac3d3f43de813872f4ed3e20e6d..92617e8888b3a12d583c2d920bc3824689b69eaa 100644 (file)
@@ -257,25 +257,25 @@ struct udev_device *udev_monitor_get_device(struct udev_monitor *udev_monitor)
                bufpos += keylen + 1;
 
                if (strncmp(key, "DEVPATH=", 8) == 0) {
-                       udev_device->devpath = strdup(&key[8]);
+                       device_set_devpath(udev_device, &key[8]);
                } else if (strncmp(key, "SUBSYSTEM=", 10) == 0) {
-                       udev_device->subsystem = strdup(&key[10]);
+                       device_set_subsystem(udev_device, &key[10]);
                } else if (strncmp(key, "DEVNAME=", 8) == 0) {
-                       udev_device->devname = strdup(&key[8]);
+                       device_set_devname(udev_device, &key[8]);
                } else if (strncmp(key, "DEVLINKS=", 9) == 0) {
                        char *slink = &key[9];
                        char *next = strchr(slink, ' ');
 
                        while (next != NULL) {
                                next[0] = '\0';
-                               name_list_add(&udev_device->link_list, slink, 0);
+                               device_add_devlink(udev_device, slink);
                                slink = &next[1];
                                next = strchr(slink, ' ');
                        }
                        if (slink[0] != '\0')
-                               name_list_add(&udev_device->link_list, slink, 0);
+                               device_add_devlink(udev_device, slink);
                }
-               name_list_add(&udev_device->env_list, key, 0);
+               device_add_property(udev_device, key);
        }
 
        return udev_device;
index 6331f99d5bad5ce5d8e6d50d00449f73e8758cda..ffdf5715136f638d27d2331d682715ea100714a6 100644 (file)
@@ -43,16 +43,16 @@ void udev_log(struct udev *udev,
 #define log_err(format, arg...) do { } while (0)
 #endif
 
-struct udev_device {
-       int refcount;
-       struct udev *udev;
-       char *devpath;
-       char *devname;
-       char *subsystem;
-       struct list_head link_list;
-       struct list_head env_list;
-};
-
+/* libudev */
 extern struct udev_device *device_init(struct udev *udev);
+
+/* libudev-device */
+extern int device_set_devpath(struct udev_device *udev_device, const char *devpath);
+extern int device_set_subsystem(struct udev_device *udev_device, const char *subsystem);
+extern int device_set_devname(struct udev_device *udev_device, const char *devname);
+extern int device_add_devlink(struct udev_device *udev_device, const char *devlink);
+extern int device_add_property(struct udev_device *udev_device, const char *property);
+
+/* libudev-utils */
 extern ssize_t util_get_sys_subsystem(struct udev *udev, const char *devpath, char *subsystem, size_t size);
 #endif
index 3a1c1bf3ac404a0f8456bedb7897310ff267ce1f..30753a19a782255327962c4308738ef8874ec02c 100644 (file)
@@ -44,8 +44,9 @@ 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);
 extern const char *udev_device_get_devpath(struct udev_device *udev_device);
-extern const char *udev_device_get_devname(struct udev_device *udev_device);
 extern const char *udev_device_get_subsystem(struct udev_device *udev_device);
+extern const char *udev_device_get_syspath(struct udev_device *udev_device);
+extern const char *udev_device_get_devname(struct udev_device *udev_device);
 extern int udev_device_get_devlinks(struct udev_device *udev_device,
                                    int (*cb)(struct udev_device *udev_device,
                                              const char *value, void *data),
index 3c38600192150e8e3d1b448596afe7e5ff488496..d0095f15fc5828cc9cab1a1971b671be93eb678f 100644 (file)
@@ -58,6 +58,8 @@ static void print_device(struct udev_device *device)
        printf("devpath:   '%s'\n", str);
        str = udev_device_get_subsystem(device);
        printf("subsystem: '%s'\n", str);
+       str = udev_device_get_syspath(device);
+       printf("syspath:   '%s'\n", str);
        str = udev_device_get_devname(device);
        printf("devname:   '%s'\n", str);
        count = udev_device_get_devlinks(device, print_devlinks_cb, NULL);