chiark / gitweb /
[PATCH] libsysfs update for refresh + namedev.c changes
[elogind.git] / libsysfs / sysfs_device.c
index 82b54719ff7ef679bbce3dec09365e5f09ba125b..bfd761b885c37d3a8aa7e19fc1afb9cf43048dae 100644 (file)
 #include "sysfs.h"
 
 /**
- * get_device_bus: retrieves the bus name the device is on, checks path to
- *     bus' link to make sure it has correct device.
+ * sysfs_get_device_bus: retrieves the bus name the device is on, checks path 
+ *     to bus' link to make sure it has correct device.
  * @dev: device to get busname.
  * returns 0 with success and -1 with error.
  */
-static int get_device_bus(struct sysfs_device *dev)
+int sysfs_get_device_bus(struct sysfs_device *dev)
 {
        unsigned char subsys[SYSFS_NAME_LEN], path[SYSFS_PATH_MAX];
        unsigned char target[SYSFS_PATH_MAX], *bus = NULL, *c = NULL;
@@ -104,14 +104,6 @@ static void sysfs_close_device_tree(struct sysfs_device *devroot)
        }
 }
 
-/**
- * sysfs_del_device: routine for dlist integration
- */
-static void sysfs_del_device(void *dev)
-{
-       sysfs_close_device((struct sysfs_device *)dev);
-}
-
 /**
  * sysfs_close_dev_tree: routine for dlist integration
  */
@@ -127,6 +119,8 @@ static void sysfs_close_dev_tree(void *dev)
 void sysfs_close_device(struct sysfs_device *dev)
 {
        if (dev != NULL) {
+               if (dev->parent != NULL)
+                       sysfs_close_device(dev->parent);
                if (dev->directory != NULL)
                        sysfs_close_directory(dev->directory);
                if (dev->children != NULL && dev->children->count == 0)
@@ -164,7 +158,7 @@ static struct sysfs_directory *open_device_dir(const unsigned char *path)
                dprintf ("Device %s not supported on this system\n", path);
                return NULL;
        }
-       if ((sysfs_read_directory(rdir)) != 0) {
+       if ((sysfs_read_dir_subdirs(rdir)) != 0) {
                dprintf ("Error reading device at dir %s\n", path);
                sysfs_close_directory(rdir);
                return NULL;
@@ -174,11 +168,11 @@ static struct sysfs_directory *open_device_dir(const unsigned char *path)
 }
 
 /**
- * sysfs_open_device: opens and populates device structure
+ * sysfs_open_device_path: opens and populates device structure
  * @path: path to device, this is the /sys/devices/ path
  * returns sysfs_device structure with success or NULL with error
  */
-struct sysfs_device *sysfs_open_device(const unsigned char *path)
+struct sysfs_device *sysfs_open_device_path(const unsigned char *path)
 {
        struct sysfs_device *dev = NULL;
 
@@ -203,6 +197,11 @@ struct sysfs_device *sysfs_open_device(const unsigned char *path)
                return NULL;
        }
        strcpy(dev->path, path);
+       if ((sysfs_remove_trailing_slash(dev->path)) != 0) {
+               dprintf("Invalid path to device %s\n", dev->path);
+               sysfs_close_device(dev);
+               return NULL;
+       }
        /* 
         * The "name" attribute no longer exists... return the device's
         * sysfs representation instead, in the "dev->name" field, which
@@ -210,8 +209,8 @@ struct sysfs_device *sysfs_open_device(const unsigned char *path)
         */
        strncpy(dev->name, dev->bus_id, SYSFS_NAME_LEN);
        
-       if (get_device_bus(dev) != 0)
-               strcpy(dev->bus, SYSFS_UNKNOWN);
+       if (sysfs_get_device_bus(dev) != 0)
+               dprintf("Could not get device bus\n");
 
        return dev;
 }
@@ -232,7 +231,7 @@ static struct sysfs_device *sysfs_open_device_tree(const unsigned char *path)
                errno = EINVAL;
                return NULL;
        }
-       rootdev = sysfs_open_device(path);
+       rootdev = sysfs_open_device_path(path);
        if (rootdev == NULL) {
                dprintf("Error opening root device at %s\n", path);
                return NULL;
@@ -255,7 +254,7 @@ static struct sysfs_device *sysfs_open_device_tree(const unsigned char *path)
                        if (rootdev->children == NULL)
                                rootdev->children = dlist_new_with_delete
                                        (sizeof(struct sysfs_device),
-                                       sysfs_del_device);
+                                       sysfs_close_dev_tree);
                        dlist_unshift(rootdev->children, new);
                }
        }
@@ -340,9 +339,7 @@ struct sysfs_root_device *sysfs_open_root_device(const unsigned char *name)
                return NULL;
        }
 
-       if (sysfs_trailing_slash(rootpath) == 0)
-               strcat(rootpath, "/");
-
+       strcat(rootpath, "/");
        strcat(rootpath, SYSFS_DEVICES_NAME);
        strcat(rootpath, "/");
        strcat(rootpath, name);
@@ -357,7 +354,13 @@ struct sysfs_root_device *sysfs_open_root_device(const unsigned char *name)
                dprintf("calloc failure\n");
                return NULL;
        }
+       strcpy(root->name, name);
        strcpy(root->path, rootpath);
+       if ((sysfs_remove_trailing_slash(root->path)) != 0) {
+               dprintf("Invalid path to root device %s\n", root->path);
+               sysfs_close_root_device(root);
+               return NULL;
+       }
        return root;
 }
 
@@ -379,20 +382,37 @@ struct dlist *sysfs_get_device_attributes(struct sysfs_device *device)
        if (device->directory->attributes == NULL) {
                if ((sysfs_read_dir_attributes(device->directory)) != 0)
                        return NULL;
-       } else {
-               if ((sysfs_path_is_dir(device->path)) != 0) {
-                       dprintf("Device at %s no longer exists", device->path);
-                       return NULL;
-               }
-               if ((sysfs_refresh_attributes
-                               (device->directory->attributes)) != 0) {
-                       dprintf("Error refreshing device attributes\n");
-                       return NULL;
-               }
        }
        return (device->directory->attributes);
 }
 
+/**
+ * sysfs_refresh_device_attributes: refreshes the device's list of attributes
+ * @device: sysfs_device whose attributes to refresh
+ *  
+ * NOTE: Upon return, prior references to sysfs_attributes for this device
+ *             _may_ not be valid
+ *
+ * Returns list of attributes on success and NULL on failure
+ */
+struct dlist *sysfs_refresh_device_attributes(struct sysfs_device *device)
+{
+       if (device == NULL) {
+               errno = EINVAL;
+               return NULL;
+       }
+
+       if (device->directory == NULL)
+               return (sysfs_get_device_attributes(device));
+
+       if ((sysfs_refresh_dir_attributes(device->directory)) != 0) {
+               dprintf("Error refreshing device attributes\n");
+               return NULL;
+       }
+
+       return (device->directory->attributes);
+}
+
 /**
  * sysfs_get_device_attr: searches dev's attributes by name
  * @dev: device to look through
@@ -402,24 +422,19 @@ struct dlist *sysfs_get_device_attributes(struct sysfs_device *device)
 struct sysfs_attribute *sysfs_get_device_attr(struct sysfs_device *dev,
                                                const unsigned char *name)
 {
-       struct sysfs_attribute *cur = NULL;
        struct dlist *attrlist = NULL;
 
        if (dev == NULL || name == NULL) {
                errno = EINVAL;
                return NULL;
        }
-
+       
        attrlist = sysfs_get_device_attributes(dev);
        if (attrlist == NULL)
                return NULL;
 
-       cur = sysfs_get_directory_attribute(dev->directory, 
-                       (unsigned char *)name);
-       if (cur != NULL)
-               return cur;
-
-       return NULL;
+       return sysfs_get_directory_attribute(dev->directory, 
+                                       (unsigned char *)name);
 }
 
 /**
@@ -445,10 +460,7 @@ static int get_device_absolute_path(const unsigned char *device,
                dprintf ("Sysfs not supported on this system\n");
                return -1;
        }
-
-       if (sysfs_trailing_slash(bus_path) == 0)
-               strcat(bus_path, "/");
-
+       strcat(bus_path, "/");
        strcat(bus_path, SYSFS_BUS_NAME);
        strcat(bus_path, "/");
        strcat(bus_path, bus);
@@ -468,7 +480,7 @@ static int get_device_absolute_path(const unsigned char *device,
 }
 
 /**
- * sysfs_open_device_by_id: open a device by id (use the "bus" subsystem)
+ * sysfs_open_device: open a device by id (use the "bus" subsystem)
  * @bus_id: bus_id of the device to open - has to be the "bus_id" in 
  *             /sys/bus/xxx/devices
  * @bus: bus the device belongs to
@@ -478,7 +490,7 @@ static int get_device_absolute_path(const unsigned char *device,
  * 2. Bus the device is on must be supplied
  *     Use sysfs_find_device_bus to get the bus name
  */
-struct sysfs_device *sysfs_open_device_by_id(const unsigned char *bus_id, 
+struct sysfs_device *sysfs_open_device(const unsigned char *bus_id, 
                                                const unsigned char *bus)
 {
        char sysfs_path[SYSFS_PATH_MAX];
@@ -495,7 +507,7 @@ struct sysfs_device *sysfs_open_device_by_id(const unsigned char *bus_id,
                return NULL;
        }
        
-       device = sysfs_open_device(sysfs_path);
+       device = sysfs_open_device_path(sysfs_path);
        if (device == NULL) {
                dprintf("Error opening device %s\n", bus_id);
                return NULL;
@@ -504,6 +516,60 @@ struct sysfs_device *sysfs_open_device_by_id(const unsigned char *bus_id,
        return device;
 }
 
+/**
+ * sysfs_get_device_parent: opens up given device's parent and returns a 
+ *     reference to its sysfs_device
+ * @dev: sysfs_device whose parent is requested
+ * Returns sysfs_device of the parent on success and NULL on failure
+ */
+struct sysfs_device *sysfs_get_device_parent(struct sysfs_device *dev)
+{
+       unsigned char ppath[SYSFS_PATH_MAX], *tmp = NULL;
+
+       if (dev == NULL) {
+               errno = EINVAL;
+               return NULL;
+       }
+
+       if (dev->parent != NULL)
+               return (dev->parent);
+
+       memset(ppath, 0, SYSFS_PATH_MAX);
+       strcpy(ppath, dev->path);
+       tmp = strrchr(ppath, '/');
+       if (tmp == NULL) {
+               dprintf("Invalid path to device %s\n", ppath);
+               return NULL;
+       }
+       if (*(tmp +1) == '\0') {
+               *tmp = '\0';
+               tmp = strrchr(tmp, '/');
+               if (tmp == NULL) {
+                       dprintf("Invalid path to device %s\n", ppath);
+                       return NULL;
+               }
+       }
+       *tmp = '\0';
+       
+       /*
+        * All "devices" have the "detach_state" attribute - validate here
+        */
+       strcat(ppath, "/detach_state");
+       if ((sysfs_path_is_file(ppath)) != 0) {
+               dprintf("Device at %s does not have a parent\n", dev->path);
+               return NULL;
+       }
+       tmp = strrchr(ppath, '/');
+       *tmp = '\0';
+       dev->parent = sysfs_open_device_path(ppath);
+       if (dev->parent == NULL) {
+               dprintf("Error opening device %s's parent at %s\n", 
+                                       dev->bus_id, ppath);
+               return NULL;
+       }
+       return (dev->parent);
+}
+
 /*
  * sysfs_open_device_attr: open the given device's attribute
  * @bus: Bus on which to look