X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=udev-add.c;h=e1e145de8a532b3c42caa3275df166ba316f5f62;hp=4aff06e15f8b83f89004450ff1c2e5654ffa289c;hb=c8fa2d8b413f7cf1ab42d1c35865952649bfccad;hpb=f61d732a02c8a5e11c39651a70e3e3fd00529495 diff --git a/udev-add.c b/udev-add.c index 4aff06e15..e1e145de8 100644 --- a/udev-add.c +++ b/udev-add.c @@ -50,6 +50,8 @@ #define LOCAL_USER "$local" +#include "selinux.h" + /* * Right now the major/minor of a device is stored in a file called * "dev" in sysfs. @@ -61,21 +63,20 @@ */ static int get_major_minor(struct sysfs_class_device *class_dev, struct udevice *udev) { - int retval = -ENODEV; struct sysfs_attribute *attr = NULL; attr = sysfs_get_classdev_attr(class_dev, "dev"); if (attr == NULL) - goto exit; + goto error; dbg("dev='%s'", attr->value); if (sscanf(attr->value, "%u:%u", &udev->major, &udev->minor) != 2) - goto exit; + goto error; dbg("found major=%d, minor=%d", udev->major, udev->minor); - retval = 0; -exit: - return retval; + return 0; +error: + return -1; } static int create_path(char *file) @@ -93,6 +94,7 @@ static int create_path(char *file) break; *pos = 0x00; if (stat(p, &stats)) { + selinux_setfscreatecon(p, S_IFDIR); retval = mkdir(p, 0755); if (retval != 0) { dbg("mkdir(%s) failed with error '%s'", @@ -100,42 +102,62 @@ static int create_path(char *file) return retval; } dbg("created '%s'", p); + } else { + selinux_setfilecon(p, S_IFDIR); } *pos = '/'; } return 0; } -static int make_node(char *filename, int major, int minor, unsigned int mode, uid_t uid, gid_t gid) +static int make_node(char *file, int major, int minor, unsigned int mode, uid_t uid, gid_t gid) { - int retval; + struct stat stats; + int retval = 0; - retval = mknod(filename, mode, makedev(major, minor)); + if (stat(file, &stats) != 0) + goto create; + + /* 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))) { + dbg("preserve file '%s', cause it has correct dev_t", file); + selinux_setfilecon(file,stats.st_mode); + goto perms; + } + + if (unlink(file) != 0) + dbg("unlink(%s) failed with error '%s'", file, strerror(errno)); + else + dbg("already present file '%s' unlinked", file); + +create: + selinux_setfscreatecon(file, mode); + retval = mknod(file, mode, makedev(major, minor)); if (retval != 0) { dbg("mknod(%s, %#o, %u, %u) failed with error '%s'", - filename, mode, major, minor, strerror(errno)); - return retval; + file, mode, major, minor, strerror(errno)); + goto exit; } - dbg("chmod(%s, %#o)", filename, mode); - retval = chmod(filename, mode); - if (retval != 0) { - dbg("chmod(%s, %#o) failed with error '%s'", - filename, mode, strerror(errno)); - return retval; +perms: + dbg("chmod(%s, %#o)", file, mode); + if (chmod(file, mode) != 0) { + dbg("chmod(%s, %#o) failed with error '%s'", file, mode, strerror(errno)); + goto exit; } if (uid != 0 || gid != 0) { - dbg("chown(%s, %u, %u)", filename, uid, gid); - retval = chown(filename, uid, gid); - if (retval != 0) { + dbg("chown(%s, %u, %u)", file, uid, gid); + if (chown(file, uid, gid) != 0) { dbg("chown(%s, %u, %u) failed with error '%s'", - filename, uid, gid, strerror(errno)); - return retval; + file, uid, gid, strerror(errno)); + goto exit; } } - return 0; +exit: + return retval; } /* get the local logged in user */ @@ -169,31 +191,12 @@ static void set_to_local_user(char *user) endutent(); } -/* Used to unlink existing files to ensure that our new file/symlink is created */ -static int unlink_entry(char *filename) -{ - struct stat stats; - int retval = 0; - - if (lstat(filename, &stats) == 0) { - if ((stats.st_mode & S_IFMT) != S_IFDIR) { - retval = unlink(filename); - if (retval) { - dbg("unlink(%s) failed with error '%s'", - filename, strerror(errno)); - } - } - } - return retval; -} - static int create_node(struct udevice *dev, int fake) { char filename[NAME_SIZE]; char linkname[NAME_SIZE]; char linktarget[NAME_SIZE]; char partitionname[NAME_SIZE]; - int retval = 0; uid_t uid = 0; gid_t gid = 0; int i; @@ -257,30 +260,29 @@ static int create_node(struct udevice *dev, int fake) } if (!fake) { - unlink_entry(filename); info("creating device node '%s'", filename); - make_node(filename, dev->major, dev->minor, dev->mode, uid, gid); + if (make_node(filename, dev->major, dev->minor, dev->mode, uid, gid) != 0) + goto error; } else { info("creating device node '%s', major = '%d', minor = '%d', " "mode = '%#o', uid = '%d', gid = '%d'", filename, dev->major, dev->minor, (mode_t)dev->mode, uid, gid); } - /* create partitions if requested */ + /* create all_partitions if requested */ if (dev->partitions > 0) { info("creating device partition nodes '%s[1-%i]'", filename, dev->partitions); if (!fake) { for (i = 1; i <= dev->partitions; i++) { strfieldcpy(partitionname, filename); strintcat(partitionname, i); - unlink_entry(partitionname); make_node(partitionname, dev->major, dev->minor + i, dev->mode, uid, gid); } } } - /* create symlink if requested */ + /* create symlink(s) if requested */ foreach_strpart(dev->symlink, " ", pos, len) { strfieldcpymax(linkname, pos, len+1); strfieldcpy(filename, udev_root); @@ -307,22 +309,22 @@ static int create_node(struct udevice *dev, int fake) strfieldcat(linktarget, &dev->name[tail]); - if (!fake) - unlink_entry(filename); - dbg("symlink(%s, %s)", linktarget, filename); if (!fake) { - retval = symlink(linktarget, filename); - if (retval != 0) + selinux_setfscreatecon(filename, S_IFLNK); + unlink(filename); + if (symlink(linktarget, filename) != 0) dbg("symlink(%s, %s) failed with error '%s'", linktarget, filename, strerror(errno)); } } - return retval; + return 0; +error: + return -1; } -static struct sysfs_class_device *get_class_dev(char *device_name) +static struct sysfs_class_device *get_class_dev(const char *device_name) { char dev_path[SYSFS_PATH_MAX]; struct sysfs_class_device *class_dev = NULL; @@ -347,7 +349,7 @@ exit: * If it doesn't happen in about 10 seconds, give up. */ #define SECONDS_TO_WAIT_FOR_FILE 10 -static int sleep_for_file(char *path, char* file) +static int sleep_for_file(const char *path, char* file) { char filename[SYSFS_PATH_MAX + 6]; int loop = SECONDS_TO_WAIT_FOR_FILE; @@ -373,12 +375,16 @@ exit: return retval; } -static int rename_net_if(struct udevice *dev) +static int rename_net_if(struct udevice *dev, int fake) { int sk; struct ifreq ifr; int retval; + dbg("changing net interface name from '%s' to '%s'", dev->kernel_name, dev->name); + if (fake) + return 0; + sk = socket(PF_INET, SOCK_DGRAM, 0); if (sk < 0) { dbg("error opening socket"); @@ -389,26 +395,25 @@ static int rename_net_if(struct udevice *dev) strfieldcpy(ifr.ifr_name, dev->kernel_name); strfieldcpy(ifr.ifr_newname, dev->name); - dbg("changing net interface name from '%s' to '%s'", dev->kernel_name, dev->name); retval = ioctl(sk, SIOCSIFNAME, &ifr); if (retval != 0) dbg("error changing net interface name"); + close(sk); return retval; } -int udev_add_device(char *path, char *subsystem, int fake) +int udev_add_device(const char *path, const char *subsystem, int fake) { - struct sysfs_class_device *class_dev = NULL; + struct sysfs_class_device *class_dev; struct udevice dev; - int retval = -EINVAL; + char devpath[DEVPATH_SIZE]; + char *pos; + int retval; memset(&dev, 0x00, sizeof(dev)); - /* for now, the block layer is the only place where block devices are */ - dev.type = get_device_type(path, subsystem); - switch (dev.type) { case 'b': case 'c': @@ -421,12 +426,12 @@ int udev_add_device(char *path, char *subsystem, int fake) default: dbg("unknown device type '%c'", dev.type); - retval = -EINVAL; + return -1; } class_dev = get_class_dev(path); if (class_dev == NULL) - goto exit; + return -1; if (dev.type == 'b' || dev.type == 'c') { retval = get_major_minor(class_dev, &dev); @@ -436,38 +441,51 @@ int udev_add_device(char *path, char *subsystem, int fake) } } - retval = namedev_name_device(class_dev, &dev); - if (retval != 0) + if (namedev_name_device(class_dev, &dev) != 0) goto exit; - if (!fake && (dev.type == 'b' || dev.type == 'c')) { - retval = udevdb_add_dev(path, &dev); - if (retval != 0) - dbg("udevdb_add_dev failed, but we are going to try " - "to create the node anyway. But remove might not " - "work properly for this device."); - } - dbg("name='%s'", dev.name); + + selinux_init(); switch (dev.type) { case 'b': case 'c': retval = create_node(&dev, fake); + if (retval != 0) + goto exit; + if ((!fake) && (udevdb_add_dev(path, &dev) != 0)) + dbg("udevdb_add_dev failed, but we are going to try " + "to create the node anyway. But remove might not " + "work properly for this device."); + + dev_d_send(&dev, subsystem, path); break; case 'n': - retval = rename_net_if(&dev); - if (retval != 0) - dbg("net device naming failed"); + strfieldcpy(devpath, path); + if (strcmp(dev.name, dev.kernel_name) != 0) { + retval = rename_net_if(&dev, fake); + if (retval != 0) + goto exit; + /* netif's are keyed with the configured name, cause + * the original kernel name sleeps with the fishes + */ + pos = strrchr(devpath, '/'); + if (pos != NULL) { + pos[1] = '\0'; + strfieldcat(devpath, dev.name); + } + } + if ((!fake) && (udevdb_add_dev(devpath, &dev) != 0)) + dbg("udevdb_add_dev failed"); + + dev_d_send(&dev, subsystem, devpath); break; } - if ((retval == 0) && (!fake)) - dev_d_send(&dev, subsystem); - exit: - if (class_dev) - sysfs_close_class_device(class_dev); + selinux_restore(); + sysfs_close_class_device(class_dev); return retval; }