]> err.no Git - systemd/commitdiff
libudev: handle ! in sysname, add sysnum, return allocated list_entry on add
authorKay Sievers <kay.sievers@vrfy.org>
Tue, 14 Oct 2008 17:53:47 +0000 (19:53 +0200)
committerKay Sievers <kay.sievers@vrfy.org>
Tue, 14 Oct 2008 17:53:47 +0000 (19:53 +0200)
udev/lib/exported_symbols
udev/lib/libudev-device.c
udev/lib/libudev-list.c
udev/lib/libudev-private.h
udev/lib/libudev.h

index 8e0be1ab8d0f29576cf5631eb510e73a9cb00b00..1fe0cc30befb1843afe55e17ec215c0bfa300991 100644 (file)
@@ -24,6 +24,7 @@ udev_device_get_syspath
 udev_device_get_devpath
 udev_device_get_devnode
 udev_device_get_sysname
+udev_device_get_sysnum
 udev_device_get_subsystem
 udev_device_get_devlinks_list_entry
 udev_device_get_properties_list_entry
index 943e104bf6fa1086400d7e624213f5f56e637f92..2f685bd8e893849d1b93c48f1c5e16476d41ff23 100644 (file)
@@ -25,6 +25,7 @@
 #include <string.h>
 #include <dirent.h>
 #include <fcntl.h>
+#include <ctype.h>
 #include <sys/stat.h>
 
 #include "libudev.h"
@@ -36,10 +37,11 @@ struct udev_device {
        struct udev_device *parent_device;
        char *syspath;
        const char *devpath;
-       const char *sysname;
+       char *sysname;
+       const char *sysnum;
        char *devnode;
        char *subsystem;
-       struct udev_list_node devlink_list;
+       struct udev_list_node devlinks_list;
        struct udev_list_node properties_list;
        char *action;
        int event_timeout;
@@ -82,6 +84,7 @@ static int device_read_db(struct udev_device *udev_device)
        }
        if ((stats.st_mode & S_IFMT) == S_IFLNK) {
                char target[UTIL_PATH_SIZE];
+               char devnode[UTIL_PATH_SIZE];
                int target_len;
                char *next;
 
@@ -98,10 +101,12 @@ static int device_read_db(struct udev_device *udev_device)
                        next[0] = '\0';
                        next = &next[1];
                }
-               if (asprintf(&udev_device->devnode, "%s/%s", udev_get_dev_path(udev_device->udev), target) < 0)
-                       return -ENOMEM;
+               util_strlcpy(devnode, udev_get_dev_path(udev_device->udev), sizeof(devnode));
+               util_strlcat(devnode, "/", sizeof(devnode));
+               util_strlcat(devnode, target, sizeof(devnode));
+               udev_device_set_devnode(udev_device, devnode);
                while (next != NULL) {
-                       char linkname[UTIL_PATH_SIZE];
+                       char devlink[UTIL_PATH_SIZE];
                        const char *lnk;
 
                        lnk = next;
@@ -110,10 +115,10 @@ static int device_read_db(struct udev_device *udev_device)
                                next[0] = '\0';
                                next = &next[1];
                        }
-                       util_strlcpy(linkname, udev_get_dev_path(udev_device->udev), sizeof(linkname));
-                       util_strlcat(linkname, "/", sizeof(linkname));
-                       util_strlcat(linkname, lnk, sizeof(linkname));
-                       udev_device_add_devlink(udev_device, linkname);
+                       util_strlcpy(devlink, udev_get_dev_path(udev_device->udev), sizeof(devlink));
+                       util_strlcat(devlink, "/", sizeof(devlink));
+                       util_strlcat(devlink, lnk, sizeof(devlink));
+                       udev_device_add_devlink(udev_device, devlink);
                }
                info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devnode);
                return 0;
@@ -133,10 +138,12 @@ static int device_read_db(struct udev_device *udev_device)
                        break;
                line[len-1] = '\0';
                val = &line[2];
-
                switch(line[0]) {
                case 'N':
-                       asprintf(&udev_device->devnode, "%s/%s", udev_get_dev_path(udev_device->udev), val);
+                       util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
+                       util_strlcat(filename, "/", sizeof(filename));
+                       util_strlcat(filename, val, sizeof(filename));
+                       udev_device_set_devnode(udev_device, filename);
                        break;
                case 'S':
                        util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
@@ -205,9 +212,9 @@ static int device_read_uevent_file(struct udev_device *udev_device)
 
 static void device_load_info(struct udev_device *device)
 {
+       device->info_loaded = 1;
        device_read_uevent_file(device);
        device_read_db(device);
-       device->info_loaded = 1;
 }
 
 void udev_device_set_info_loaded(struct udev_device *device)
@@ -228,7 +235,7 @@ struct udev_device *device_new(struct udev *udev)
        memset(udev_device, 0x00, sizeof(struct udev_device));
        udev_device->refcount = 1;
        udev_device->udev = udev;
-       udev_list_init(&udev_device->devlink_list);
+       udev_list_init(&udev_device->devlinks_list);
        udev_list_init(&udev_device->properties_list);
        udev_list_init(&udev_device->attr_list);
        info(udev_device->udev, "udev_device: %p created\n", udev_device);
@@ -579,9 +586,10 @@ void udev_device_unref(struct udev_device *udev_device)
        if (udev_device->parent_device != NULL)
                udev_device_unref(udev_device->parent_device);
        free(udev_device->syspath);
+       free(udev_device->sysname);
        free(udev_device->devnode);
        free(udev_device->subsystem);
-       udev_list_cleanup(udev_device->udev, &udev_device->devlink_list);
+       udev_list_cleanup(udev_device->udev, &udev_device->devlinks_list);
        udev_list_cleanup(udev_device->udev, &udev_device->properties_list);
        free(udev_device->action);
        free(udev_device->driver);
@@ -631,6 +639,13 @@ const char *udev_device_get_sysname(struct udev_device *udev_device)
        return udev_device->sysname;
 }
 
+const char *udev_device_get_sysnum(struct udev_device *udev_device)
+{
+       if (udev_device == NULL)
+               return NULL;
+       return udev_device->sysnum;
+}
+
 /**
  * udev_device_get_devnode:
  * @udev_device: udev device
@@ -710,7 +725,7 @@ struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *
                return NULL;
        if (!udev_device->info_loaded)
                device_load_info(udev_device);
-       return udev_list_get_entry(&udev_device->devlink_list);
+       return udev_list_get_entry(&udev_device->devlinks_list);
 }
 
 /**
@@ -781,10 +796,16 @@ const char *udev_device_get_attr_value(struct udev_device *udev_device, const ch
        ssize_t size;
        const char *val = NULL;
 
+       if (udev_device == NULL)
+               return NULL;
+       if (attr == NULL)
+               return NULL;
+
        /* look for possibly already cached result */
        udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->attr_list)) {
                if (strcmp(udev_list_entry_get_name(list_entry), attr) == 0) {
-                       info(udev_device->udev, "got '%s' (%s) from cache\n", attr, udev_list_entry_get_value(list_entry));
+                       info(udev_device->udev, "got '%s' (%s) from cache\n",
+                            attr, udev_list_entry_get_value(list_entry));
                        return udev_list_entry_get_value(list_entry);
                }
        }
@@ -848,9 +869,11 @@ const char *udev_device_get_attr_value(struct udev_device *udev_device, const ch
 out:
        return val;
 }
+
 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
 {
        const char *pos;
+       size_t len;
 
        free(udev_device->syspath);
        udev_device->syspath = strdup(syspath);
@@ -858,10 +881,25 @@ int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath
                return -ENOMEM;
        udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
        udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
+
        pos = strrchr(udev_device->syspath, '/');
        if (pos == NULL)
                return -EINVAL;
-       udev_device->sysname = &pos[1];
+       udev_device->sysname = strdup(&pos[1]);
+       if (udev_device->sysname == NULL)
+               return -ENOMEM;
+
+       /* some devices have '!' in their name, change that to '/' */
+       len = 0;
+       while (udev_device->sysname[len] != '\0') {
+               if (udev_device->sysname[len] == '!')
+                       udev_device->sysname[len] = '/';
+               len++;
+       }
+
+       /* trailing number */
+       while (isdigit(udev_device->sysname[--len]))
+               udev_device->sysnum = &udev_device->sysname[len];
        return 0;
 }
 
@@ -876,6 +914,7 @@ int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsy
 
 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
 {
+       free(udev_device->devnode);
        udev_device->devnode = strdup(devnode);
        if (udev_device->devnode == NULL)
                return -ENOMEM;
@@ -888,9 +927,8 @@ int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink
        char symlinks[UTIL_PATH_SIZE];
        struct udev_list_entry *list_entry;
 
-       if (udev_list_entry_add(udev_device->udev, &udev_device->devlink_list, devlink, NULL, 1, 0) == NULL)
+       if (udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0) == NULL)
                return -ENOMEM;
-
        list_entry =  udev_device_get_devlinks_list_entry(udev_device);
        util_strlcpy(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks));
        udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry)) {
@@ -898,18 +936,15 @@ int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink
                util_strlcat(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks));
        }
        udev_device_add_property(udev_device, "DEVLINKS", symlinks);
-
        return 0;
 }
 
-int udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
+struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
 {
-       if (udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0) == NULL)
-               return -ENOMEM;
-       return 0;
+       return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
 }
 
-int udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
+struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
 {
        char name[UTIL_PATH_SIZE];
        char *val;
@@ -917,21 +952,21 @@ int udev_device_add_property_from_string(struct udev_device *udev_device, const
        strncpy(name, property, sizeof(name));
        val = strchr(name, '=');
        if (val == NULL)
-               return -1;
+               return NULL;
        val[0] = '\0';
        val = &val[1];
        if (val[0] == '\0')
                val = NULL;
-       udev_device_add_property(udev_device, name, val);
-       return 0;
+       return udev_device_add_property(udev_device, name, val);
 }
 
 int udev_device_set_action(struct udev_device *udev_device, const char *action)
 {
-       udev_device_add_property(udev_device, "ACTION", action);
+       free(udev_device->action);
        udev_device->action = strdup(action);
        if (udev_device->action == NULL)
                return -ENOMEM;
+       udev_device_add_property(udev_device, "ACTION", action);
        return 0;
 }
 
index 8545835078fbe93fd28e3bdb4049b552f42bb721..25e69cb3a3a621388d2f3ac5c2345a59f1768bbe 100644 (file)
@@ -197,9 +197,12 @@ struct udev_list_entry *udev_list_entry_get_by_name(struct udev_list_entry *list
 {
        struct udev_list_entry *entry;
 
-       udev_list_entry_foreach(entry, list_entry)
-               if (strcmp(udev_list_entry_get_name(entry), name) == 0)
+       udev_list_entry_foreach(entry, list_entry) {
+               if (strcmp(udev_list_entry_get_name(entry), name) == 0) {
+                       dbg(entry->udev, "found '%s=%s'\n", entry->name, entry->value);
                        return entry;
+               }
+       }
        return NULL;
 }
 
index 6e419dff6c6c3d9419216bf33c9ac5b88aa22394..4e31d6b4463f436828b0b9438fcf2d9a80a04193 100644 (file)
@@ -54,8 +54,8 @@ extern int udev_device_set_syspath(struct udev_device *udev_device, const char *
 extern int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem);
 extern int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode);
 extern int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink);
-extern int udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value);
-extern int udev_device_add_property_from_string(struct udev_device *udev_device, const char *property);
+extern struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value);
+extern struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property);
 extern int udev_device_set_action(struct udev_device *udev_device, const char *action);
 extern int udev_device_set_driver(struct udev_device *udev_device, const char *driver);
 extern const char *udev_device_get_devpath_old(struct udev_device *udev_device);
index 3f4cccf8c3da101e46cfbcf25f31df2184bdbfb3..8949724844997bdba54ff9d437372e550572b0af 100644 (file)
@@ -69,6 +69,7 @@ extern const char *udev_device_get_devpath(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_sysname(struct udev_device *udev_device);
+extern const char *udev_device_get_sysnum(struct udev_device *udev_device);
 extern const char *udev_device_get_devnode(struct udev_device *udev_device);
 extern struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device);
 extern struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device);