X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=udev_add.c;h=e05710ce81f820638d90b1a3caa01c9938b3f385;hp=f384ea96e50043405fb37abfb70705f721024a8a;hb=57e1a2770c4d85edd377488906c437f6d1e9b369;hpb=fd9efc00a2e477423185c993b8ec4570ef3ee07d diff --git a/udev_add.c b/udev_add.c index f384ea96e..e05710ce8 100644 --- a/udev_add.c +++ b/udev_add.c @@ -34,40 +34,20 @@ #include #include #include -#include #include "libsysfs/sysfs/libsysfs.h" +#include "udev_libc_wrapper.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_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; -} - -int udev_make_node(struct udevice *udev, const char *file, int major, int minor, mode_t 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; @@ -77,7 +57,7 @@ int udev_make_node(struct udevice *udev, const char *file, int major, int minor, /* 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); selinux_setfilecon(file, udev->kernel_name, stats.st_mode); goto perms; @@ -90,26 +70,22 @@ int udev_make_node(struct udevice *udev, const char *file, int major, int minor, create: switch (udev->type) { - case 'b': + case BLOCK: mode |= S_IFBLK; break; - case 'c': - case 'u': + case CLASS: 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)); + retval = mknod(file, mode, devt); 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; } @@ -137,12 +113,11 @@ static int create_node(struct udevice *udev, struct sysfs_class_device *class_de { char filename[NAME_SIZE]; char partitionname[NAME_SIZE]; + struct name_entry *name_loop; uid_t uid = 0; gid_t gid = 0; int tail; - char *pos; - int len; - int i; + int i; snprintf(filename, NAME_SIZE, "%s/%s", udev_root, udev->name); filename[NAME_SIZE-1] = '\0'; @@ -157,15 +132,8 @@ static int create_node(struct udevice *udev, struct sysfs_class_device *class_de if (endptr[0] == '\0') uid = (uid_t) id; - else { - struct passwd *pw; - - pw = getpwnam(udev->owner); - if (pw == NULL) - dbg("specified user unknown '%s'", udev->owner); - else - uid = pw->pw_uid; - } + else + uid = lookup_user(udev->owner); } if (udev->group[0] != '\0') { @@ -174,23 +142,18 @@ static int create_node(struct udevice *udev, struct sysfs_class_device *class_de if (endptr[0] == '\0') gid = (gid_t) id; - else { - struct group *gr = getgrnam(udev->group); - if (gr == NULL) - dbg("specified group unknown '%s'", udev->group); - else - gid = gr->gr_gid; - } + else + gid = lookup_group(udev->group); } if (!udev->test_run) { info("creating device node '%s'", filename); - if (udev_make_node(udev, 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, - udev->major, udev->minor, udev->mode, uid, gid); + major(udev->devt), minor(udev->devt), udev->mode, uid, gid); } /* create all_partitions if requested */ @@ -208,38 +171,39 @@ static int create_node(struct udevice *udev, struct sysfs_class_device *class_de 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); - udev_make_node(udev, partitionname, udev->major, udev->minor + i, udev->mode, uid, gid); + dev_t part_devt; + + snprintf(partitionname, NAME_SIZE, "%s%d", filename, i); + partitionname[NAME_SIZE-1] = '\0'; + part_devt = makedev(major(udev->devt), minor(udev->devt)+1); + udev_make_node(udev, partitionname, part_devt, udev->mode, uid, gid); } } } /* create symlink(s) if requested */ - foreach_strpart(udev->symlink, " ", pos, len) { - char linkname[NAME_SIZE]; + list_for_each_entry(name_loop, &udev->symlink_list, node) { char linktarget[NAME_SIZE]; - strfieldcpymax(linkname, pos, len+1); - snprintf(filename, NAME_SIZE, "%s/%s", udev_root, linkname); + snprintf(filename, NAME_SIZE, "%s/%s", udev_root, name_loop->name); filename[NAME_SIZE-1] = '\0'; dbg("symlink '%s' to node '%s' requested", filename, udev->name); if (!udev->test_run) - if (strrchr(linkname, '/')) + if (strchr(filename, '/')) create_path(filename); /* optimize relative link */ linktarget[0] = '\0'; i = 0; tail = 0; - while ((udev->name[i] == linkname[i]) && udev->name[i]) { + while (udev->name[i] && (udev->name[i] == name_loop->name[i])) { if (udev->name[i] == '/') tail = i+1; i++; } - while (linkname[i] != '\0') { - if (linkname[i] == '/') + while (name_loop->name[i] != '\0') { + if (name_loop->name[i] == '/') strfieldcat(linktarget, "../"); i++; } @@ -294,9 +258,9 @@ int udev_add_device(struct udevice *udev, struct sysfs_class_device *class_dev) char *pos; int retval = 0; - if (udev->type == 'b' || udev->type == 'c') { - retval = get_major_minor(class_dev, udev); - if (retval != 0) { + if (udev->type == BLOCK || udev->type == CLASS) { + udev->devt = get_devt(class_dev); + if (!udev->devt) { dbg("no dev-file found, do nothing"); return 0; } @@ -309,7 +273,7 @@ int udev_add_device(struct udevice *udev, struct sysfs_class_device *class_dev) selinux_init(); - if (udev->type == 'b' || udev->type == 'c') { + if (udev->type == BLOCK || udev->type == CLASS) { retval = create_node(udev, class_dev); if (retval != 0) goto exit; @@ -322,7 +286,7 @@ int udev_add_device(struct udevice *udev, struct sysfs_class_device *class_dev) snprintf(udev->devname, NAME_SIZE, "%s/%s", udev_root, udev->name); udev->devname[NAME_SIZE-1] = '\0'; - } else if (udev->type == 'n') { + } else if (udev->type == NET) { /* look if we want to change the name of the netif */ if (strcmp(udev->name, udev->kernel_name) != 0) { retval = rename_net_if(udev);