chiark / gitweb /
[PATCH] handle renamed network interfaces properly if we manage hotplug.d/
[elogind.git] / udev_add.c
index d07120d..acd678f 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 "udev_db.h"
 
 #include "selinux.h"
 
@@ -74,37 +68,6 @@ 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)
 {
        struct stat stats;
@@ -155,42 +118,9 @@ 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)
 {
        char filename[NAME_SIZE];
-       char linkname[NAME_SIZE];
-       char linktarget[NAME_SIZE];
        char partitionname[NAME_SIZE];
        uid_t uid = 0;
        gid_t gid = 0;
@@ -199,8 +129,8 @@ static int create_node(struct udevice *udev)
        char *pos;
        int len;
 
-       strfieldcpy(filename, udev_root);
-       strfieldcat(filename, udev->name);
+       snprintf(filename, NAME_SIZE, "%s/%s", udev_root, udev->name);
+       filename[NAME_SIZE-1] = '\0';
 
        switch (udev->type) {
        case 'b':
@@ -229,8 +159,6 @@ static int create_node(struct udevice *udev)
                        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)
@@ -261,7 +189,7 @@ static int create_node(struct udevice *udev)
        } 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 */
@@ -278,9 +206,13 @@ static int create_node(struct udevice *udev)
 
        /* 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, '/'))
@@ -355,7 +287,7 @@ int udev_add_device(struct udevice *udev, struct sysfs_class_device *class_dev)
                retval = get_major_minor(class_dev, udev);
                if (retval != 0) {
                        dbg("no dev-file found, do nothing");
-                       goto close;
+                       return 0;
                }
        }
 
@@ -365,45 +297,46 @@ int udev_add_device(struct udevice *udev, struct sysfs_class_device *class_dev)
        dbg("adding name='%s'", udev->name);
 
        selinux_init();
-       switch (udev->type) {
-       case 'b':
-       case 'c':
+
+       if (udev->type == 'b' || udev->type == 'c') {
                retval = create_node(udev);
                if (retval != 0)
                        goto exit;
-               if ((!udev->test_run) && (udevdb_add_dev(udev) != 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(udev);
-               break;
+               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");
 
-       case 'n':
+               /* use full path to the environment */
+               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 */
                if (strcmp(udev->name, udev->kernel_name) != 0) {
                        retval = rename_net_if(udev);
                        if (retval != 0)
                                goto exit;
-                       /* netif's are keyed with the configured name, cause
-                        * the original kernel name sleeps with the fishes
+
+                       /* 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);
                        }
-               }
-               if ((!udev->test_run) && (udevdb_add_dev(udev) != 0))
-                       dbg("udevdb_add_dev failed");
 
-               dev_d_send(udev);
-               break;
+                       /* use netif name for the environment */
+                       strfieldcpy(udev->devname, udev->name);
+               }
        }
 
 exit:
        selinux_restore();
-close:
-       sysfs_close_class_device(class_dev);
 
        return retval;
 }