chiark / gitweb /
[PATCH] udev - safer string handling all over the place
[elogind.git] / namedev.c
index 942788b2a2a83519ec785f209eaf589d84b597a4..219cb8a4b84406b63f7cfcf8917eb08fcb9eee66 100644 (file)
--- a/namedev.c
+++ b/namedev.c
 #include <sys/wait.h>
 #include <sys/stat.h>
 
+#include "libsysfs/sysfs/libsysfs.h"
 #include "list.h"
 #include "udev.h"
 #include "udev_version.h"
 #include "logging.h"
 #include "namedev.h"
-#include "libsysfs/libsysfs.h"
 #include "klibc_fixups.h"
 
 static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr);
@@ -157,7 +157,7 @@ static mode_t get_default_mode(void)
 static char *get_default_owner(void)
 {
        if (strlen(default_owner_str) == 0)
-               strncpy(default_owner_str, "root", OWNER_SIZE);
+               strfieldcpy(default_owner_str, "root");
 
        return default_owner_str;
 }
@@ -165,7 +165,7 @@ static char *get_default_owner(void)
 static char *get_default_group(void)
 {
        if (strlen(default_group_str) == 0)
-               strncpy(default_group_str, "root", GROUP_SIZE);
+               strfieldcpy(default_group_str, "root");
 
        return default_group_str;
 }
@@ -219,6 +219,7 @@ static void apply_format(struct udevice *udev, unsigned char *string, struct sys
        char *pos3;
        char *attr;
        int num;
+       int i;
        char c;
        struct sysfs_attribute *tmpattr;
 
@@ -270,11 +271,15 @@ static void apply_format(struct udevice *udev, unsigned char *string, struct sys
                case 'c':
                        if (strlen(udev->program_result) == 0)
                                break;
-                       if (num > 0) {
-                               strncpy(temp1, udev->program_result, sizeof(temp1));
+                       /* get part part of the result string */
+                       i = num; /* num syntax is deprecated and will be removed  */
+                       if (attr != NULL)
+                               i = atoi(attr);
+                       if (i > 0) {
+                               strfieldcpy(temp1, udev->program_result);
                                pos2 = temp1;
-                               while (num) {
-                                       num--;
+                               while (i) {
+                                       i--;
                                        pos3 = strsep(&pos2, " ");
                                        if (pos3 == NULL) {
                                                dbg("requested part of result string not found");
@@ -618,7 +623,8 @@ static struct sysfs_device *get_sysfs_device(struct sysfs_class_device *class_de
        tspec.tv_nsec = 10000000;  /* sleep 10 millisec */
        loop = 10;
        while (loop--) {
-               nanosleep(&tspec, NULL);
+               if (udev_sleep)
+                       nanosleep(&tspec, NULL);
                if (class_dev_parent)
                        sysfs_device = sysfs_get_classdev_device(class_dev_parent);
                else
@@ -645,7 +651,8 @@ device_found:
                loop = 10;
                tspec.tv_nsec = 10000000;
                while (loop--) {
-                       nanosleep(&tspec, NULL);
+                       if (udev_sleep)
+                               nanosleep(&tspec, NULL);
                        sysfs_get_device_bus(sysfs_device);
                        
                        if (sysfs_device->bus[0] != '\0')
@@ -830,8 +837,8 @@ done:
        } else {
                /* no matching perms found :( */
                udev->mode = get_default_mode();
-               strncpy(udev->owner, get_default_owner(), OWNER_SIZE);
-               strncpy(udev->group, get_default_group(), GROUP_SIZE);
+               strfieldcpy(udev->owner, get_default_owner());
+               strfieldcpy(udev->group, get_default_group());
        }
        dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o",
            udev->name, udev->owner, udev->group, udev->mode);