chiark / gitweb /
[PATCH] replace fgets() with mmap() and introduce udev_lib.[hc]
[elogind.git] / udev-add.c
index a3bc0df579270abaa619e2b1b300c4928c39756b..2bd16014f5835a749660e60df0bdee9c7295b61c 100644 (file)
 #include <grp.h>
 #ifndef __KLIBC__
 #include <pwd.h>
+#include <utmp.h>
 #endif
 
+#include "libsysfs/sysfs/libsysfs.h"
 #include "udev.h"
+#include "udev_lib.h"
 #include "udev_version.h"
 #include "udev_dbus.h"
+#include "udev_selinux.h"
 #include "logging.h"
 #include "namedev.h"
 #include "udevdb.h"
-#include "libsysfs/libsysfs.h"
 #include "klibc_fixups.h"
 
+#define LOCAL_USER "$local"
+
 /* 
  * Right now the major/minor of a device is stored in a file called
  * "dev" in sysfs.
@@ -78,7 +83,7 @@ static int create_path(char *file)
        int retval;
        struct stat stats;
        
-       strncpy(p, file, sizeof(p));
+       strfieldcpy(p, file);
        pos = strchr(p+1, '/');
        while (1) {
                pos = strchr(pos+1, '/');
@@ -131,22 +136,71 @@ static int make_node(char *filename, int major, int minor, unsigned int mode, ui
        return 0;
 }
 
-static int create_node(struct udevice *dev, int fake)
+/* 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();
+}
+
+/* Used to unlink existing files to ensure that our new file/symlink is created */
+static int unlink_entry(char *filename)
 {
        struct stat stats;
-       char filename[255];
-       char linktarget[255];
-       char partitionname[255];
-       char *linkname;
-       char *symlinks;
+       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;
        int tail;
+       char *pos;
+       int len;
 
-       strncpy(filename, udev_root, sizeof(filename));
-       strncat(filename, dev->name, sizeof(filename));
+       strfieldcpy(filename, udev_root);
+       strfieldcat(filename, dev->name);
 
        switch (dev->type) {
        case 'b':
@@ -174,7 +228,11 @@ static int create_node(struct udevice *dev, int fake)
                if (endptr[0] == '\0')
                        uid = (uid_t) id;
                else {
-                       struct passwd *pw = getpwnam(dev->owner);
+                       struct passwd *pw;
+                       if (strncmp(dev->owner, LOCAL_USER, sizeof(LOCAL_USER)) == 0)
+                               set_to_local_user(dev->owner);
+
+                       pw = getpwnam(dev->owner);
                        if (pw == NULL)
                                dbg("specified user unknown '%s'", dev->owner);
                        else
@@ -197,6 +255,7 @@ 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);
        } else {
@@ -210,63 +269,54 @@ static int create_node(struct udevice *dev, int fake)
                info("creating device partition nodes '%s[1-%i]'", filename, dev->partitions);
                if (!fake) {
                        for (i = 1; i <= dev->partitions; i++) {
-                               sprintf(partitionname, "%s%i", filename, i);
+                               strfieldcpy(partitionname, filename);
+                               strintcat(partitionname, i);
+                               unlink_entry(partitionname);
                                make_node(partitionname, dev->major,
                                          dev->minor + i, dev->mode, uid, gid);
                        }
                }
        }
 
+       if (!fake)
+               selinux_add_node(filename);
+
        /* create symlink if requested */
-       if (dev->symlink[0] != '\0') {
-               symlinks = dev->symlink;
-               while (1) {
-                       linkname = strsep(&symlinks, " ");
-                       if (linkname == NULL || linkname[0] == '\0')
-                               break;
-
-                       strncpy(filename, udev_root, sizeof(filename));
-                       strncat(filename, linkname, sizeof(filename));
-                       dbg("symlink '%s' to node '%s' requested", filename, dev->name);
-                       if (!fake)
-                               if (strrchr(linkname, '/'))
-                                       create_path(filename);
-
-                       /* optimize relative link */
-                       linktarget[0] = '\0';
-                       i = 0;
-                       tail = 0;
-                       while ((dev->name[i] == linkname[i]) && dev->name[i]) {
-                               if (dev->name[i] == '/')
-                                       tail = i+1;
-                               i++;
-                       }
-                       while (linkname[i] != '\0') {
-                               if (linkname[i] == '/')
-                                       strcat(linktarget, "../");
-                               i++;
-                       }
+       foreach_strpart(dev->symlink, " ", pos, len) {
+               strfieldcpymax(linkname, pos, len+1);
+               strfieldcpy(filename, udev_root);
+               strfieldcat(filename, linkname);
+               dbg("symlink '%s' to node '%s' requested", filename, dev->name);
+               if (!fake)
+                       if (strrchr(linkname, '/'))
+                               create_path(filename);
+
+               /* optimize relative link */
+               linktarget[0] = '\0';
+               i = 0;
+               tail = 0;
+               while ((dev->name[i] == linkname[i]) && dev->name[i]) {
+                       if (dev->name[i] == '/')
+                               tail = i+1;
+                       i++;
+               }
+               while (linkname[i] != '\0') {
+                       if (linkname[i] == '/')
+                               strfieldcat(linktarget, "../");
+                       i++;
+               }
 
-                       if (linktarget[0] == '\0')
-                               strcpy(linktarget, "./");
-                       strcat(linktarget, &dev->name[tail]);
-
-                       /* unlink existing files to ensure that our symlink is created */
-                       if (!fake && (lstat(filename, &stats) == 0)) {
-                               if ((stats.st_mode & S_IFMT) != S_IFDIR) {
-                                       if (unlink(filename))
-                                               dbg("unlink(%s) failed with error '%s'",
-                                                   filename, strerror(errno));
-                               }
-                       }
+               strfieldcat(linktarget, &dev->name[tail]);
 
-                       dbg("symlink(%s, %s)", linktarget, filename);
-                       if (!fake) {
-                               retval = symlink(linktarget, filename);
-                               if (retval != 0)
-                                       dbg("symlink(%s, %s) failed with error '%s'",
-                                           linktarget, filename, strerror(errno));
-                       }
+               if (!fake)
+                       unlink_entry(filename);
+
+               dbg("symlink(%s, %s)", linktarget, filename);
+               if (!fake) {
+                       retval = symlink(linktarget, filename);
+                       if (retval != 0)
+                               dbg("symlink(%s, %s) failed with error '%s'",
+                                   linktarget, filename, strerror(errno));
                }
        }
 
@@ -278,8 +328,8 @@ static struct sysfs_class_device *get_class_dev(char *device_name)
        char dev_path[SYSFS_PATH_MAX];
        struct sysfs_class_device *class_dev = NULL;
 
-       strcpy(dev_path, sysfs_path);
-       strcat(dev_path, device_name);
+       strfieldcpy(dev_path, sysfs_path);
+       strfieldcat(dev_path, device_name);
        dbg("looking at '%s'", dev_path);
 
        /* open up the sysfs class device for this thing... */
@@ -304,9 +354,9 @@ static int sleep_for_dev(char *path)
        int loop = SECONDS_TO_WAIT_FOR_DEV;
        int retval;
 
-       strcpy(filename, sysfs_path);
-       strcat(filename, path);
-       strcat(filename, "/dev");
+       strfieldcpy(filename, sysfs_path);
+       strfieldcat(filename, path);
+       strfieldcat(filename, "/dev");
 
        while (loop--) {
                struct stat buf;