chiark / gitweb /
[PATCH] DEVPATH for netdev
[elogind.git] / udev-add.c
index ed0286d1fc2e9175ab688abb8ab8121e27ed88a6..09c2ac6d3efd2634ceb1eacac37791e1409c658d 100644 (file)
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <grp.h>
+#include <net/if.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <linux/sockios.h>
 #ifndef __KLIBC__
 #include <pwd.h>
 #include <utmp.h>
@@ -37,9 +41,8 @@
 
 #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"
  */
 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)
@@ -111,28 +113,27 @@ static int make_node(char *filename, int major, int minor, unsigned int mode, ui
        if (retval != 0) {
                dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
                    filename, mode, major, minor, strerror(errno));
-               return retval;
+               goto exit;
        }
 
        dbg("chmod(%s, %#o)", filename, mode);
-       retval = chmod(filename, mode);
-       if (retval != 0) {
+       if (chmod(filename, mode) != 0) {
                dbg("chmod(%s, %#o) failed with error '%s'",
                    filename, mode, strerror(errno));
-               return retval;
+               goto exit;
        }
 
        if (uid != 0 || gid != 0) {
                dbg("chown(%s, %u, %u)", filename, uid, gid);
-               retval = chown(filename, uid, gid);
-               if (retval != 0) {
+               if (chown(filename, uid, gid) != 0) {
                        dbg("chown(%s, %u, %u) failed with error '%s'",
                            filename, uid, gid, strerror(errno));
-                       return retval;
+                       goto exit;
                }
        }
 
-       return 0;
+exit:
+       return retval;
 }
 
 /* get the local logged in user */
@@ -141,7 +142,7 @@ static void set_to_local_user(char *user)
        struct utmp *u;
        time_t recent = 0;
 
-       strnfieldcpy(user, default_owner_str, OWNER_SIZE);
+       strfieldcpymax(user, default_owner_str, OWNER_SIZE);
        setutent();
        while (1) {
                u = getutent();
@@ -158,7 +159,7 @@ static void set_to_local_user(char *user)
 
                if (u->ut_time > recent) {
                        recent = u->ut_time;
-                       strnfieldcpy(user, u->ut_user, OWNER_SIZE);
+                       strfieldcpymax(user, u->ut_user, OWNER_SIZE);
                        dbg("local user is '%s'", user);
                        break;
                }
@@ -166,7 +167,6 @@ 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;
@@ -186,16 +186,16 @@ static int unlink_entry(char *filename)
 
 static int create_node(struct udevice *dev, int fake)
 {
-       char filename[255];
-       char linktarget[255];
-       char partitionname[255];
-       char *linkname;
-       char *symlinks;
-       int retval = 0;
+       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;
 
        strfieldcpy(filename, udev_root);
        strfieldcat(filename, dev->name);
@@ -226,10 +226,11 @@ static int create_node(struct udevice *dev, int fake)
                if (endptr[0] == '\0')
                        uid = (uid_t) id;
                else {
+                       struct passwd *pw;
                        if (strncmp(dev->owner, LOCAL_USER, sizeof(LOCAL_USER)) == 0)
                                set_to_local_user(dev->owner);
 
-                       struct passwd *pw = getpwnam(dev->owner);
+                       pw = getpwnam(dev->owner);
                        if (pw == NULL)
                                dbg("specified user unknown '%s'", dev->owner);
                        else
@@ -254,14 +255,15 @@ 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) {
@@ -275,55 +277,47 @@ static int create_node(struct udevice *dev, int fake)
                }
        }
 
-       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;
-
-                       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++;
-                       }
+       /* create symlink(s) if requested */
+       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++;
+               }
 
-                       strfieldcat(linktarget, &dev->name[tail]);
+               strfieldcat(linktarget, &dev->name[tail]);
 
-                       if (!fake)
-                               unlink_entry(filename);
+               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));
-                       }
+               dbg("symlink(%s, %s)", linktarget, filename);
+               if (!fake) {
+                       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)
@@ -350,16 +344,16 @@ exit:
 /* wait for the "dev" file to show up in the directory in sysfs.
  * If it doesn't happen in about 10 seconds, give up.
  */
-#define SECONDS_TO_WAIT_FOR_DEV                10
-static int sleep_for_dev(char *path)
+#define SECONDS_TO_WAIT_FOR_FILE       10
+static int sleep_for_file(char *path, char* file)
 {
        char filename[SYSFS_PATH_MAX + 6];
-       int loop = SECONDS_TO_WAIT_FOR_DEV;
+       int loop = SECONDS_TO_WAIT_FOR_FILE;
        int retval;
 
        strfieldcpy(filename, sysfs_path);
        strfieldcat(filename, path);
-       strfieldcat(filename, "/dev");
+       strfieldcat(filename, file);
 
        while (loop--) {
                struct stat buf;
@@ -377,55 +371,115 @@ exit:
        return retval;
 }
 
+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");
+               return -1;
+       }
+
+       memset(&ifr, 0x00, sizeof(struct ifreq));
+       strfieldcpy(ifr.ifr_name, dev->kernel_name);
+       strfieldcpy(ifr.ifr_newname, 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)
 {
-       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 */
-       if (strcmp(subsystem, "block") == 0)
-               dev.type = 'b';
-       else
-               dev.type = 'c';
+       dev.type = get_device_type(path, subsystem);
+       switch (dev.type) {
+       case 'b':
+       case 'c':
+               retval = sleep_for_file(path, "/dev");
+               break;
 
-       retval = sleep_for_dev(path);
-       if (retval != 0)
-               goto exit;
+       case 'n':
+               retval = sleep_for_file(path, "/address");
+               break;
+
+       default:
+               dbg("unknown device type '%c'", dev.type);
+               return -1;
+       }
 
        class_dev = get_class_dev(path);
        if (class_dev == NULL)
-               goto exit;
+               return -1;
 
-       retval = get_major_minor(class_dev, &dev);
-       if (retval != 0) {
-               dbg("get_major_minor failed");
-               goto exit;
+       if (dev.type == 'b' || dev.type == 'c') {
+               retval = get_major_minor(class_dev, &dev);
+               if (retval != 0) {
+                       dbg("get_major_minor failed");
+                       goto exit;
+               }
        }
 
-       retval = namedev_name_device(class_dev, &dev);
-       if (retval != 0)
+       if (namedev_name_device(class_dev, &dev) != 0)
                goto exit;
 
-       if (!fake) {
-               retval = udevdb_add_dev(path, &dev);
-               if (retval != 0)
+       dbg("name='%s'", dev.name);
+
+       switch (dev.type) {
+       case 'b':
+       case 'c':
+               retval = create_node(&dev, fake);
+               if (fake || retval != 0)
+                       goto exit;
+               if (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.");
 
-       }
-       dbg("name='%s'", dev.name);
-       retval = create_node(&dev, fake);
+               dev_d_send(&dev, subsystem, path);
+               break;
+
+       case 'n':
+               if (strcmp(dev.name, dev.kernel_name) != 0) {
+                       retval = rename_net_if(&dev, fake);
+                       if (fake || retval != 0)
+                               goto exit;
+                       /* netif's are keyed with the configured name, cause
+                        * the original kernel name sleeps with the fishes
+                        */
+                       strfieldcpy(devpath, path);
+                       pos = strrchr(devpath, '/');
+                       if (pos != NULL) {
+                               pos[1] = '\0';
+                               strfieldcat(devpath, dev.name);
+                       }
+               }
+               if (udevdb_add_dev(devpath, &dev) != 0)
+                       dbg("udevdb_add_dev failed");
 
-       if ((retval == 0) && (!fake))
-               sysbus_send_create(&dev, path);
+               dev_d_send(&dev, subsystem, devpath);
+               break;
+       }
 
 exit:
-       if (class_dev)
-               sysfs_close_class_device(class_dev);
+       sysfs_close_class_device(class_dev);
 
        return retval;
 }