X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=udev_add.c;h=be62e130ab4cc33bf2f2bd111bbc4681a21ced3d;hp=d4cccfc085882f1fd09330484d4614b51dcd2c44;hb=6a24dc747788b6049ef35607ff252e3965af2ce5;hpb=85a953c0eeb66260f77bd69a255034af18cdbba0 diff --git a/udev_add.c b/udev_add.c index d4cccfc08..be62e130a 100644 --- a/udev_add.c +++ b/udev_add.c @@ -43,8 +43,7 @@ #include "logging.h" #include "namedev.h" #include "udev_db.h" - -#include "selinux.h" +#include "udev_selinux.h" /* * the major/minor of a device is stored in a file called "dev" @@ -68,7 +67,7 @@ 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, int major, int minor, mode_t mode, uid_t uid, gid_t gid) { struct stat stats; int retval = 0; @@ -80,7 +79,7 @@ static int make_node(char *file, int major, int minor, unsigned int mode, uid_t if (((stats.st_mode & S_IFMT) == S_IFBLK || (stats.st_mode & S_IFMT) == S_IFCHR) && (stats.st_rdev == makedev(major, minor))) { 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; } @@ -90,7 +89,23 @@ static int make_node(char *file, int major, int minor, unsigned int mode, uid_t dbg("already present file '%s' unlinked", file); create: - selinux_setfscreatecon(file, mode); + 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, makedev(major, minor)); if (retval != 0) { dbg("mknod(%s, %#o, %u, %u) failed with error '%s'", @@ -118,43 +133,28 @@ exit: 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; - int i; int tail; char *pos; int len; + int i; 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 */ - 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); + if (endptr[0] == '\0') uid = (uid_t) id; else { @@ -171,6 +171,7 @@ static int create_node(struct udevice *udev) if (udev->group[0] != '\0') { char *endptr; unsigned long id = strtoul(udev->group, &endptr, 10); + if (endptr[0] == '\0') gid = (gid_t) id; else { @@ -184,7 +185,7 @@ static int create_node(struct udevice *udev) 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->major, udev->minor, udev->mode, uid, gid) != 0) goto error; } else { info("creating device node '%s', major = '%d', minor = '%d', " @@ -194,12 +195,22 @@ static int create_node(struct udevice *udev) /* create all_partitions if requested */ if (udev->partitions > 0) { + 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++) { strfieldcpy(partitionname, filename); strintcat(partitionname, i); - make_node(partitionname, udev->major, udev->minor + i, udev->mode, uid, gid); + udev_make_node(udev, partitionname, udev->major, udev->minor + i, udev->mode, uid, gid); } } } @@ -237,7 +248,7 @@ static int create_node(struct udevice *udev) 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'", @@ -299,7 +310,7 @@ int udev_add_device(struct udevice *udev, struct sysfs_class_device *class_dev) selinux_init(); if (udev->type == 'b' || udev->type == 'c') { - retval = create_node(udev); + retval = create_node(udev, class_dev); if (retval != 0) goto exit;