chiark / gitweb /
use no_argument, required_argument, optional_argument in longopts
[elogind.git] / udev / udev_sysfs.c
index 0d4a97118861dd4ee758efa5dd3ff0b9f5ec844f..2ea724a78fb3a496541eaf932f4b63e034903cf6 100644 (file)
@@ -35,11 +35,44 @@ static LIST_HEAD(attr_list);
 
 struct sysfs_attr {
        struct list_head node;
-       char path[PATH_SIZE];
+       char path[UTIL_PATH_SIZE];
        char *value;                    /* points to value_local if value is cached */
-       char value_local[NAME_SIZE];
+       char value_local[UTIL_NAME_SIZE];
 };
 
+static int resolve_sys_link(struct udev *udev, char *path, size_t size)
+{
+       char link_path[UTIL_PATH_SIZE];
+       char link_target[UTIL_PATH_SIZE];
+
+       int len;
+       int i;
+       int back;
+
+       util_strlcpy(link_path, udev_get_sys_path(udev), sizeof(link_path));
+       util_strlcat(link_path, path, sizeof(link_path));
+       len = readlink(link_path, link_target, sizeof(link_target));
+       if (len <= 0)
+               return -1;
+       link_target[len] = '\0';
+       dbg(udev, "path link '%s' points to '%s'\n", path, link_target);
+
+       for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
+               ;
+       dbg(udev, "base '%s', tail '%s', back %i\n", path, &link_target[back * 3], back);
+       for (i = 0; i <= back; i++) {
+               char *pos = strrchr(path, '/');
+
+               if (pos == NULL)
+                       return -1;
+               pos[0] = '\0';
+       }
+       dbg(udev, "after moving back '%s'\n", path);
+       util_strlcat(path, "/", size);
+       util_strlcat(path, &link_target[back * 3], size);
+       return 0;
+}
+
 int sysfs_init(void)
 {
        INIT_LIST_HEAD(&dev_list);
@@ -100,47 +133,15 @@ void sysfs_device_set_values(struct udev *udev,
        dbg(udev, "kernel_number='%s'\n", dev->kernel_number);
 }
 
-int sysfs_resolve_link(struct udev *udev, char *devpath, size_t size)
-{
-       char link_path[PATH_SIZE];
-       char link_target[PATH_SIZE];
-       int len;
-       int i;
-       int back;
-
-       util_strlcpy(link_path, udev_get_sys_path(udev), sizeof(link_path));
-       util_strlcat(link_path, devpath, sizeof(link_path));
-       len = readlink(link_path, link_target, sizeof(link_target));
-       if (len <= 0)
-               return -1;
-       link_target[len] = '\0';
-       dbg(udev, "path link '%s' points to '%s'\n", devpath, link_target);
-
-       for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
-               ;
-       dbg(udev, "base '%s', tail '%s', back %i\n", devpath, &link_target[back * 3], back);
-       for (i = 0; i <= back; i++) {
-               char *pos = strrchr(devpath, '/');
-
-               if (pos == NULL)
-                       return -1;
-               pos[0] = '\0';
-       }
-       dbg(udev, "after moving back '%s'\n", devpath);
-       util_strlcat(devpath, "/", size);
-       util_strlcat(devpath, &link_target[back * 3], size);
-       return 0;
-}
-
 struct sysfs_device *sysfs_device_get(struct udev *udev, const char *devpath)
 {
-       char path[PATH_SIZE];
-       char devpath_real[PATH_SIZE];
+       char path[UTIL_PATH_SIZE];
+       char devpath_real[UTIL_PATH_SIZE];
        struct sysfs_device *dev;
        struct sysfs_device *dev_loop;
        struct stat statbuf;
-       char link_path[PATH_SIZE];
-       char link_target[PATH_SIZE];
+       char link_path[UTIL_PATH_SIZE];
+       char link_target[UTIL_PATH_SIZE];
        int len;
        char *pos;
 
@@ -172,11 +173,11 @@ struct sysfs_device *sysfs_device_get(struct udev *udev, const char *devpath)
        util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
        util_strlcat(path, devpath_real, sizeof(path));
        if (lstat(path, &statbuf) != 0) {
-               dbg(udev, "stat '%s' failed: %s\n", path, strerror(errno));
+               dbg(udev, "stat '%s' failed: %m\n", path);
                return NULL;
        }
        if (S_ISLNK(statbuf.st_mode)) {
-               if (sysfs_resolve_link(udev, devpath_real, sizeof(devpath_real)) != 0)
+               if (resolve_sys_link(udev, devpath_real, sizeof(devpath_real)) != 0)
                        return NULL;
 
                /* now look for device in cache after path translation */
@@ -248,7 +249,7 @@ struct sysfs_device *sysfs_device_get(struct udev *udev, const char *devpath)
 
 struct sysfs_device *sysfs_device_get_parent(struct udev *udev, struct sysfs_device *dev)
 {
-       char parent_devpath[PATH_SIZE];
+       char parent_devpath[UTIL_PATH_SIZE];
        char *pos;
 
        dbg(udev, "open '%s'\n", dev->devpath);
@@ -290,7 +291,7 @@ struct sysfs_device *sysfs_device_get_parent(struct udev *udev, struct sysfs_dev
 device_link:
        util_strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
        util_strlcat(parent_devpath, "/device", sizeof(parent_devpath));
-       if (sysfs_resolve_link(udev, parent_devpath, sizeof(parent_devpath)) != 0)
+       if (resolve_sys_link(udev, parent_devpath, sizeof(parent_devpath)) != 0)
                return NULL;
 
        /* get parent and remember it */
@@ -313,9 +314,9 @@ struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct udev *udev, s
 
 char *sysfs_attr_get_value(struct udev *udev, const char *devpath, const char *attr_name)
 {
-       char path_full[PATH_SIZE];
+       char path_full[UTIL_PATH_SIZE];
        const char *path;
-       char value[NAME_SIZE];
+       char value[UTIL_NAME_SIZE];
        struct sysfs_attr *attr_loop;
        struct sysfs_attr *attr;
        struct stat statbuf;
@@ -351,13 +352,13 @@ char *sysfs_attr_get_value(struct udev *udev, const char *devpath, const char *a
        list_add(&attr->node, &attr_list);
 
        if (lstat(path_full, &statbuf) != 0) {
-               dbg(udev, "stat '%s' failed: %s\n", path_full, strerror(errno));
+               dbg(udev, "stat '%s' failed: %m\n", path_full);
                goto out;
        }
 
        if (S_ISLNK(statbuf.st_mode)) {
                /* links return the last element of the target path */
-               char link_target[PATH_SIZE];
+               char link_target[UTIL_PATH_SIZE];
                int len;
                const char *pos;
 
@@ -409,7 +410,7 @@ out:
 int sysfs_lookup_devpath_by_subsys_id(struct udev *udev, char *devpath_full, size_t len, const char *subsystem, const char *id)
 {
        size_t sysfs_len;
-       char path_full[PATH_SIZE];
+       char path_full[UTIL_PATH_SIZE];
        char *path;
        struct stat statbuf;
 
@@ -443,7 +444,7 @@ int sysfs_lookup_devpath_by_subsys_id(struct udev *udev, char *devpath_full, siz
        }
 
        if (strcmp(subsystem, "drivers") == 0) {
-               char subsys[NAME_SIZE];
+               char subsys[UTIL_NAME_SIZE];
                char *driver;
 
                util_strlcpy(subsys, id, sizeof(subsys));
@@ -492,7 +493,7 @@ out:
        return 0;
 found:
        if (S_ISLNK(statbuf.st_mode))
-               sysfs_resolve_link(udev, path, sizeof(path_full) - sysfs_len);
+               resolve_sys_link(udev, path, sizeof(path_full) - sysfs_len);
        util_strlcpy(devpath_full, path, len);
        return 1;
 }