chiark / gitweb /
[PATCH] fix up segfaulting binaries with new klibc
[elogind.git] / namedev.c
index 41f0bc0f161743a4ab971be63f99a8c91df66e18..49f9e4b1f48e30f21068179162d7b1015944eaed 100644 (file)
--- a/namedev.c
+++ b/namedev.c
@@ -144,15 +144,14 @@ static int get_format_len(char **str)
  */
 static int find_free_number(struct udevice *udev, const char *name)
 {
+       char devpath[NAME_SIZE];
        char filename[NAME_SIZE];
        int num = 0;
-       struct udevice db_udev;
 
        strfieldcpy(filename, name);
        while (1) {
                dbg("look for existing node '%s'", filename);
-               memset(&db_udev, 0x00, sizeof(struct udevice));
-               if (udev_db_get_device_by_name(&db_udev, filename) != 0) {
+               if (udev_db_search_name(devpath, DEVPATH_SIZE, filename) != 0) {
                        dbg("free num=%d", num);
                        return num;
                }
@@ -172,15 +171,10 @@ static void apply_format(struct udevice *udev, char *string, size_t maxsize,
 {
        char temp[NAME_SIZE];
        char temp2[NAME_SIZE];
-       char *tail;
-       char *pos;
-       char *attr;
+       char *tail, *pos, *cpos, *attr, *rest;
        int len;
        int i;
        char c;
-       char *spos;
-       char *rest;
-       int slen;
        struct sysfs_attribute *tmpattr;
        unsigned int next_free_number;
        struct sysfs_class_device *class_dev_parent;
@@ -218,12 +212,14 @@ static void apply_format(struct udevice *udev, char *string, size_t maxsize,
                        dbg("substitute kernel number '%s'", udev->kernel_number);
                                break;
                case 'm':
-                       strintcatmax(string, minor(udev->devt), maxsize);
-                       dbg("substitute minor number '%u'", minor(udev->devt));
+                       sprintf(temp2, "%d", minor(udev->devt));
+                       strfieldcatmax(string, temp2, maxsize);
+                       dbg("substitute minor number '%s'", temp2);
                        break;
                case 'M':
-                       strintcatmax(string, major(udev->devt), maxsize);
-                       dbg("substitute major number '%u'", major(udev->devt));
+                       sprintf(temp2, "%d", major(udev->devt));
+                       strfieldcatmax(string, temp2, maxsize);
+                       dbg("substitute major number '%s'", temp2);
                        break;
                case 'c':
                        if (udev->program_result[0] == '\0')
@@ -233,19 +229,25 @@ static void apply_format(struct udevice *udev, char *string, size_t maxsize,
                        if (attr != NULL)
                                i = strtoul(attr, &rest, 10);
                        if (i > 0) {
-                               foreach_strpart(udev->program_result, " \n\r", spos, slen) {
-                                       i--;
-                                       if (i == 0)
-                                               break;
+                               dbg("request part #%d of result string", i);
+                               cpos = udev->program_result;
+                               while (--i) {
+                                       while (cpos[0] != '\0' && !isspace(cpos[0]))
+                                               cpos++;
+                                       while (isspace(cpos[0]))
+                                               cpos++;
                                }
                                if (i > 0) {
                                        dbg("requested part of result string not found");
                                        break;
                                }
-                               if (rest[0] == '+')
-                                       strfieldcpy(temp2, spos);
-                               else
-                                       strfieldcpymax(temp2, spos, slen+1);
+                               strfieldcpy(temp2, cpos);
+                               /* %{2+}c copies the whole string from the second part on */
+                               if (rest[0] != '+') {
+                                       cpos = strchr(temp2, ' ');
+                                       if (cpos)
+                                               cpos[0] = '\0';
+                               }
                                strfieldcatmax(string, temp2, maxsize);
                                dbg("substitute part of result string '%s'", temp2);
                        } else {
@@ -298,13 +300,14 @@ static void apply_format(struct udevice *udev, char *string, size_t maxsize,
                                struct udevice udev_parent;
 
                                dbg("found parent '%s', get the node name", class_dev_parent->path);
-                               memset(&udev_parent, 0x00, sizeof(struct udevice));
+                               udev_init_device(&udev_parent, NULL, NULL);
                                /* lookup the name in the udev_db with the DEVPATH of the parent */
-                               if (udev_db_get_device_by_devpath(&udev_parent, &class_dev_parent->path[strlen(sysfs_path)]) == 0) {
+                               if (udev_db_get_device(&udev_parent, &class_dev_parent->path[strlen(sysfs_path)]) == 0) {
                                        strfieldcatmax(string, udev_parent.name, maxsize);
                                        dbg("substitute parent node name'%s'", udev_parent.name);
                                } else
                                        dbg("parent not found in database");
+                               udev_cleanup_device(&udev_parent);
                        }
                        break;
                case 'N':
@@ -749,14 +752,25 @@ int namedev_name_device(struct udevice *udev, struct sysfs_class_device *class_d
                        /* collect symlinks */
                        if (dev->symlink[0] != '\0') {
                                char temp[NAME_SIZE];
+                               char *pos, *next;
 
                                info("configured rule in '%s[%i]' applied, added symlink '%s'",
                                     dev->config_file, dev->config_line, dev->symlink);
                                strfieldcpy(temp, dev->symlink);
                                apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device);
-                               if (udev->symlink[0] != '\0')
-                                       strfieldcat(udev->symlink, " ");
-                               strfieldcat(udev->symlink, temp);
+
+                               /* add multiple symlinks separated by spaces */
+                               pos = temp;
+                               next = strchr(temp, ' ');
+                               while (next) {
+                                       next[0] = '\0';
+                                       dbg("add symlink '%s'", pos);
+                                       name_list_add(&udev->symlink_list, pos, 0);
+                                       pos = &next[1];
+                                       next = strchr(pos, ' ');
+                               }
+                               dbg("add symlink '%s'", pos);
+                               name_list_add(&udev->symlink_list, pos, 0);
                        }
 
                        /* rule matches */