chiark / gitweb /
[PATCH] fix special file mode mask for temporary device node
[elogind.git] / udev_add.c
index 809a33cedb664f7f4dafab1dde562982e85ccdef..be62e130ab4cc33bf2f2bd111bbc4681a21ced3d 100644 (file)
 #include <sys/socket.h>
 #include <sys/ioctl.h>
 #include <linux/sockios.h>
-#ifndef __KLIBC__
 #include <pwd.h>
-#include <utmp.h>
-#endif
 
 #include "libsysfs/sysfs/libsysfs.h"
 #include "udev.h"
-#include "udev_lib.h"
+#include "udev_utils.h"
 #include "udev_version.h"
 #include "logging.h"
 #include "namedev.h"
-#include "udevdb.h"
-#include "klibc_fixups.h"
-
-#define LOCAL_USER "$local"
-
-#include "selinux.h"
+#include "udev_db.h"
+#include "udev_selinux.h"
 
 /*
  * the major/minor of a device is stored in a file called "dev"
@@ -74,38 +67,7 @@ error:
        return -1;
 }
 
-static int create_path(char *file)
-{
-       char p[NAME_SIZE];
-       char *pos;
-       int retval;
-       struct stat stats;
-       
-       strfieldcpy(p, file);
-       pos = strchr(p+1, '/');
-       while (1) {
-               pos = strchr(pos+1, '/');
-               if (pos == NULL)
-                       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'",
-                                   p, strerror(errno));
-                               return retval;
-                       }
-                       dbg("created '%s'", p);
-               } else {
-                       selinux_setfilecon(p, S_IFDIR);
-               }
-               *pos = '/';
-       }
-       return 0;
-}
-
-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;
@@ -117,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;
        }
 
@@ -127,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'",
@@ -155,82 +133,32 @@ exit:
        return retval;
 }
 
-/* get the local logged in user */
-static void set_to_local_user(char *user)
-{
-       struct utmp *u;
-       time_t recent = 0;
-
-       strfieldcpymax(user, default_owner_str, OWNER_SIZE);
-       setutent();
-       while (1) {
-               u = getutent();
-               if (u == NULL)
-                       break;
-
-               /* is this a user login ? */
-               if (u->ut_type != USER_PROCESS)
-                       continue;
-
-               /* is this a local login ? */
-               if (strcmp(u->ut_host, ""))
-                       continue;
-
-               if (u->ut_time > recent) {
-                       recent = u->ut_time;
-                       strfieldcpymax(user, u->ut_user, OWNER_SIZE);
-                       dbg("local user is '%s'", user);
-                       break;
-               }
-       }
-       endutent();
-}
-
-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 linkname[NAME_SIZE];
-       char linktarget[NAME_SIZE];
        char partitionname[NAME_SIZE];
        uid_t uid = 0;
        gid_t gid = 0;
-       int i;
        int tail;
        char *pos;
        int len;
+               int i;
 
-       strfieldcpy(filename, udev_root);
-       strfieldcat(filename, udev->name);
-
-       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;
-       }
+       snprintf(filename, NAME_SIZE, "%s/%s", udev_root, udev->name);
+       filename[NAME_SIZE-1] = '\0';
 
        /* 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 {
                        struct passwd *pw;
-                       if (strncmp(udev->owner, LOCAL_USER, sizeof(LOCAL_USER)) == 0)
-                               set_to_local_user(udev->owner);
 
                        pw = getpwnam(udev->owner);
                        if (pw == NULL)
@@ -243,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 {
@@ -256,31 +185,45 @@ 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', "
                     "mode = '%#o', uid = '%d', gid = '%d'", filename,
-                    udev->major, udev->minor, (mode_t)udev->mode, uid, gid);
+                    udev->major, udev->minor, udev->mode, uid, gid);
        }
 
        /* 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);
                        }
                }
        }
 
        /* create symlink(s) if requested */
        foreach_strpart(udev->symlink, " ", pos, len) {
+               char linkname[NAME_SIZE];
+               char linktarget[NAME_SIZE];
+
                strfieldcpymax(linkname, pos, len+1);
-               strfieldcpy(filename, udev_root);
-               strfieldcat(filename, linkname);
+               snprintf(filename, NAME_SIZE, "%s/%s", udev_root, linkname);
+               filename[NAME_SIZE-1] = '\0';
+
                dbg("symlink '%s' to node '%s' requested", filename, udev->name);
                if (!udev->test_run)
                        if (strrchr(linkname, '/'))
@@ -305,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'",
@@ -360,23 +303,24 @@ int udev_add_device(struct udevice *udev, struct sysfs_class_device *class_dev)
        }
 
        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') {
-               retval = create_node(udev);
+               retval = create_node(udev, class_dev);
                if (retval != 0)
                        goto exit;
 
-               if (udevdb_add_dev(udev) != 0)
-                       dbg("udevdb_add_dev failed, but we create the node anyway, "
+               if (udev_db_add_device(udev) != 0)
+                       dbg("udev_db_add_dev failed, but we create the node anyway, "
                            "remove might not work for custom names");
 
                /* use full path to the environment */
-               snprintf(udev->devname, NAME_SIZE-1, "%s%s", udev_root, udev->name);
+               snprintf(udev->devname, NAME_SIZE, "%s/%s", udev_root, udev->name);
+               udev->devname[NAME_SIZE-1] = '\0';
 
        } else if (udev->type == 'n') {
                /* look if we want to change the name of the netif */
@@ -385,15 +329,16 @@ int udev_add_device(struct udevice *udev, struct sysfs_class_device *class_dev)
                        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);
+                               setenv("INTERFACE", udev->name, 1);
                        }
 
                        /* use netif name for the environment */