chiark / gitweb /
[PATCH] switch major/minor to dev_t
[elogind.git] / udev_add.c
index f537f567ccdfb36c034af288cc26841f9191d9d7..66264f06e0771b3b85e0a05d6a7d8540719db504 100644 (file)
 #include "libsysfs/sysfs/libsysfs.h"
 #include "udev.h"
 #include "udev_utils.h"
 #include "libsysfs/sysfs/libsysfs.h"
 #include "udev.h"
 #include "udev_utils.h"
+#include "udev_sysfs.h"
 #include "udev_version.h"
 #include "logging.h"
 #include "namedev.h"
 #include "udev_db.h"
 #include "udev_version.h"
 #include "logging.h"
 #include "namedev.h"
 #include "udev_db.h"
+#include "udev_selinux.h"
 
 
-#include "selinux.h"
 
 
-/*
- * the major/minor of a device is stored in a file called "dev"
- * The number is stored in decimal values in the format: M:m
- */
-static int get_major_minor(struct sysfs_class_device *class_dev, struct udevice *udev)
-{
-       struct sysfs_attribute *attr = NULL;
-
-       attr = sysfs_get_classdev_attr(class_dev, "dev");
-       if (attr == NULL)
-               goto error;
-       dbg("dev='%s'", attr->value);
-
-       if (sscanf(attr->value, "%u:%u", &udev->major, &udev->minor) != 2)
-               goto error;
-       dbg("found major=%d, minor=%d", udev->major, udev->minor);
-
-       return 0;
-error:
-       return -1;
-}
-
-static int make_node(char *file, int major, int minor, unsigned int mode, uid_t uid, gid_t gid)
+int udev_make_node(struct udevice *udev, const char *file, dev_t devt, mode_t mode, uid_t uid, gid_t gid)
 {
        struct stat stats;
        int retval = 0;
 {
        struct stat stats;
        int retval = 0;
@@ -78,9 +57,9 @@ static int make_node(char *file, int major, int minor, unsigned int mode, uid_t
 
        /* preserve node with already correct numbers, to not change the inode number */
        if (((stats.st_mode & S_IFMT) == S_IFBLK || (stats.st_mode & S_IFMT) == S_IFCHR) &&
 
        /* preserve node with already correct numbers, to not change the inode number */
        if (((stats.st_mode & S_IFMT) == S_IFBLK || (stats.st_mode & S_IFMT) == S_IFCHR) &&
-           (stats.st_rdev == makedev(major, minor))) {
+           (stats.st_rdev == devt)) {
                dbg("preserve file '%s', cause it has correct dev_t", file);
                dbg("preserve file '%s', cause it has correct dev_t", file);
-               selinux_setfilecon(file,stats.st_mode);
+               selinux_setfilecon(file, udev->kernel_name, stats.st_mode);
                goto perms;
        }
 
                goto perms;
        }
 
@@ -90,11 +69,27 @@ static int make_node(char *file, int major, int minor, unsigned int mode, uid_t
                dbg("already present file '%s' unlinked", file);
 
 create:
                dbg("already present file '%s' unlinked", file);
 
 create:
-       selinux_setfscreatecon(file, mode);
-       retval = mknod(file, mode, makedev(major, minor));
+       switch (udev->type) {
+       case 'b':
+               mode |= S_IFBLK;
+               break;
+       case 'c':
+       case 'u':
+               mode |= S_IFCHR;
+               break;
+       case 'p':
+               mode |= S_IFIFO;
+               break;
+       default:
+               dbg("unknown node type %c\n", udev->type);
+               return -EINVAL;
+       }
+
+       selinux_setfscreatecon(file, udev->kernel_name, mode);
+       retval = mknod(file, mode, devt);
        if (retval != 0) {
                dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
        if (retval != 0) {
                dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
-                   file, mode, major, minor, strerror(errno));
+                   file, mode, major(devt), minor(devt), strerror(errno));
                goto exit;
        }
 
                goto exit;
        }
 
@@ -118,43 +113,28 @@ exit:
        return retval;
 }
 
        return retval;
 }
 
-static int create_node(struct udevice *udev)
+static int create_node(struct udevice *udev, struct sysfs_class_device *class_dev)
 {
        char filename[NAME_SIZE];
        char partitionname[NAME_SIZE];
        uid_t uid = 0;
        gid_t gid = 0;
 {
        char filename[NAME_SIZE];
        char partitionname[NAME_SIZE];
        uid_t uid = 0;
        gid_t gid = 0;
-       int i;
        int tail;
        char *pos;
        int len;
        int tail;
        char *pos;
        int len;
+               int i;
 
        snprintf(filename, NAME_SIZE, "%s/%s", udev_root, udev->name);
        filename[NAME_SIZE-1] = '\0';
 
 
        snprintf(filename, NAME_SIZE, "%s/%s", udev_root, udev->name);
        filename[NAME_SIZE-1] = '\0';
 
-       switch (udev->type) {
-       case 'b':
-               udev->mode |= S_IFBLK;
-               break;
-       case 'c':
-       case 'u':
-               udev->mode |= S_IFCHR;
-               break;
-       case 'p':
-               udev->mode |= S_IFIFO;
-               break;
-       default:
-               dbg("unknown node type %c\n", udev->type);
-               return -EINVAL;
-       }
-
        /* create parent directories if needed */
        /* create parent directories if needed */
-       if (strrchr(udev->name, '/'))
+       if (strchr(udev->name, '/'))
                create_path(filename);
 
        if (udev->owner[0] != '\0') {
                char *endptr;
                unsigned long id = strtoul(udev->owner, &endptr, 10);
                create_path(filename);
 
        if (udev->owner[0] != '\0') {
                char *endptr;
                unsigned long id = strtoul(udev->owner, &endptr, 10);
+
                if (endptr[0] == '\0')
                        uid = (uid_t) id;
                else {
                if (endptr[0] == '\0')
                        uid = (uid_t) id;
                else {
@@ -171,6 +151,7 @@ static int create_node(struct udevice *udev)
        if (udev->group[0] != '\0') {
                char *endptr;
                unsigned long id = strtoul(udev->group, &endptr, 10);
        if (udev->group[0] != '\0') {
                char *endptr;
                unsigned long id = strtoul(udev->group, &endptr, 10);
+
                if (endptr[0] == '\0')
                        gid = (gid_t) id;
                else {
                if (endptr[0] == '\0')
                        gid = (gid_t) id;
                else {
@@ -184,22 +165,35 @@ static int create_node(struct udevice *udev)
 
        if (!udev->test_run) {
                info("creating device node '%s'", filename);
 
        if (!udev->test_run) {
                info("creating device node '%s'", filename);
-               if (make_node(filename, udev->major, udev->minor, udev->mode, uid, gid) != 0)
+               if (udev_make_node(udev, filename, udev->devt, udev->mode, uid, gid) != 0)
                        goto error;
        } else {
                info("creating device node '%s', major = '%d', minor = '%d', "
                     "mode = '%#o', uid = '%d', gid = '%d'", filename,
                        goto error;
        } else {
                info("creating device node '%s', major = '%d', minor = '%d', "
                     "mode = '%#o', uid = '%d', gid = '%d'", filename,
-                    udev->major, udev->minor, (mode_t)udev->mode, uid, gid);
+                    major(udev->devt), minor(udev->devt), udev->mode, uid, gid);
        }
 
        /* create all_partitions if requested */
        }
 
        /* create all_partitions if requested */
-       if (udev->partitions > 0) {
+       if (udev->partitions) {
+               struct sysfs_attribute *attr;
+               int range;
+
+               /* take the maximum registered minor range */
+               attr = sysfs_get_classdev_attr(class_dev, "range");
+               if (attr) {
+                       range = atoi(attr->value);
+                       if (range > 1)
+                               udev->partitions = range-1;
+               }
                info("creating device partition nodes '%s[1-%i]'", filename, udev->partitions);
                if (!udev->test_run) {
                        for (i = 1; i <= udev->partitions; i++) {
                info("creating device partition nodes '%s[1-%i]'", filename, udev->partitions);
                if (!udev->test_run) {
                        for (i = 1; i <= udev->partitions; i++) {
+                               dev_t part_devt;
+
                                strfieldcpy(partitionname, filename);
                                strintcat(partitionname, i);
                                strfieldcpy(partitionname, filename);
                                strintcat(partitionname, i);
-                               make_node(partitionname, udev->major, udev->minor + i, udev->mode, uid, gid);
+                               part_devt = makedev(major(udev->devt), minor(udev->devt)+1);
+                               udev_make_node(udev, partitionname, part_devt, udev->mode, uid, gid);
                        }
                }
        }
                        }
                }
        }
@@ -237,7 +231,7 @@ static int create_node(struct udevice *udev)
 
                dbg("symlink(%s, %s)", linktarget, filename);
                if (!udev->test_run) {
 
                dbg("symlink(%s, %s)", linktarget, filename);
                if (!udev->test_run) {
-                       selinux_setfscreatecon(filename, S_IFLNK);
+                       selinux_setfscreatecon(filename, udev->kernel_name, S_IFLNK);
                        unlink(filename);
                        if (symlink(linktarget, filename) != 0)
                                dbg("symlink(%s, %s) failed with error '%s'",
                        unlink(filename);
                        if (symlink(linktarget, filename) != 0)
                                dbg("symlink(%s, %s) failed with error '%s'",
@@ -284,22 +278,22 @@ int udev_add_device(struct udevice *udev, struct sysfs_class_device *class_dev)
        int retval = 0;
 
        if (udev->type == 'b' || udev->type == 'c') {
        int retval = 0;
 
        if (udev->type == 'b' || udev->type == 'c') {
-               retval = get_major_minor(class_dev, udev);
-               if (retval != 0) {
+               udev->devt = get_devt(class_dev);
+               if (!udev->devt) {
                        dbg("no dev-file found, do nothing");
                        return 0;
                }
        }
 
        if (namedev_name_device(udev, class_dev) != 0)
                        dbg("no dev-file found, do nothing");
                        return 0;
                }
        }
 
        if (namedev_name_device(udev, class_dev) != 0)
-               goto exit;
+               return 0;
 
        dbg("adding name='%s'", udev->name);
 
        selinux_init();
 
        if (udev->type == 'b' || udev->type == 'c') {
 
        dbg("adding name='%s'", udev->name);
 
        selinux_init();
 
        if (udev->type == 'b' || udev->type == 'c') {
-               retval = create_node(udev);
+               retval = create_node(udev, class_dev);
                if (retval != 0)
                        goto exit;
 
                if (retval != 0)
                        goto exit;
 
@@ -318,15 +312,16 @@ int udev_add_device(struct udevice *udev, struct sysfs_class_device *class_dev)
                        if (retval != 0)
                                goto exit;
 
                        if (retval != 0)
                                goto exit;
 
-                       /* we've changed the name, now fake the devpath,
-                        * cause original kernel name sleeps with the fishes
-                        * and we don't get any event from the kernel now
+                       /* we've changed the name, now fake the devpath, cause the
+                        * original kernel name sleeps with the fishes and we don't
+                        * get an event from the kernel with the new name
                         */
                        pos = strrchr(udev->devpath, '/');
                        if (pos != NULL) {
                                pos[1] = '\0';
                                strfieldcat(udev->devpath, udev->name);
                                setenv("DEVPATH", udev->devpath, 1);
                         */
                        pos = strrchr(udev->devpath, '/');
                        if (pos != NULL) {
                                pos[1] = '\0';
                                strfieldcat(udev->devpath, udev->name);
                                setenv("DEVPATH", udev->devpath, 1);
+                               setenv("INTERFACE", udev->name, 1);
                        }
 
                        /* use netif name for the environment */
                        }
 
                        /* use netif name for the environment */