chiark / gitweb /
use libudev code, unify logging, pass udev context around everywhere
[elogind.git] / udev / lib / libudev-device.c
index f6ad541de4afa24e2b07526148cbceae5a6cf35d..43ed2f3dbfd215d6b1923d1b55022a317356bcce 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;
@@ -47,7 +58,7 @@ struct udev_device *device_init(struct udev *udev)
        udev_device->udev = udev;
        INIT_LIST_HEAD(&udev_device->link_list);
        INIT_LIST_HEAD(&udev_device->env_list);
-       log_info(udev_device->udev, "udev_device: %p created\n", udev_device);
+       info(udev_device->udev, "udev_device: %p created\n", udev_device);
        return udev_device;
 }
 
@@ -90,7 +101,7 @@ struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *
        if (udev_device == NULL)
                return NULL;
 
-       udevice = udev_device_init(NULL);
+       udevice = udev_device_init(udev);
        if (udevice == NULL) {
                free(udev_device);
                return NULL;
@@ -98,13 +109,13 @@ 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);
-       log_info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
+       sysfs_resolve_link(udev, path, sizeof(path));
+       device_set_devpath(udev_device, devpath);
+       info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
 
        err = udev_db_get_device(udevice, path);
        if (err >= 0)
-               log_info(udev, "device %p filled with udev database data\n", udev_device);
+               info(udev, "device %p filled with udev database data\n", udev_device);
 
        if (udevice->name[0] != '\0')
                asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev), udevice->name);
@@ -115,11 +126,11 @@ struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *
                strlcpy(name, udev_get_dev_path(udev), sizeof(name));
                strlcat(name, "/", sizeof(name));
                strlcat(name, name_loop->name, sizeof(name));
-               name_list_add(&udev_device->link_list, name, 0);
+               name_list_add(udev, &udev_device->link_list, name, 0);
        }
 
        list_for_each_entry(name_loop, &udevice->env_list, node)
-               name_list_add(&udev_device->env_list, name_loop->name, 0);
+               name_list_add(udev_device->udev, &udev_device->env_list, name_loop->name, 0);
 
        udev_device_cleanup(udevice);
        return udev_device;
@@ -127,6 +138,7 @@ struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *
 
 /**
  * udev_device_get_udev:
+ * @udev_device: udev device
  *
  * Retrieve the udev library context the device was created with.
  *
@@ -170,12 +182,12 @@ 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);
-       name_list_cleanup(&udev_device->env_list);
-       log_info(udev_device->udev, "udev_device: %p released\n", udev_device);
+       name_list_cleanup(udev_device->udev, &udev_device->link_list);
+       name_list_cleanup(udev_device->udev, &udev_device->env_list);
+       info(udev_device->udev, "udev_device: %p released\n", udev_device);
        free(udev_device);
 }
 
@@ -183,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)
 {
@@ -195,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
@@ -304,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->udev, &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->udev, &udev_device->env_list, property, 0) == NULL)
+               return -ENOMEM;
+       return 0;
+}