chiark / gitweb /
[PATCH] fix UDEV_NO_SLEEP
[elogind.git] / udev-add.c
index 1f17f50bc3de8a372b6a80d9595518fad05a9e4d..1f693297882430fa9f88c86ec958eff95b203022 100644 (file)
@@ -105,29 +105,46 @@ static int create_path(char *file)
        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;
+
+       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);
+               goto perms;
+       }
 
-       retval = mknod(filename, mode, makedev(major, minor));
+       if (unlink(file) != 0)
+               dbg("unlink(%s) failed with error '%s'", file, strerror(errno));
+       else
+               dbg("already present file '%s' unlinked", file);
+
+create:
+       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));
+                   file, mode, major, minor, strerror(errno));
                goto exit;
        }
 
-       dbg("chmod(%s, %#o)", filename, mode);
-       if (chmod(filename, mode) != 0) {
-               dbg("chmod(%s, %#o) failed with error '%s'",
-                   filename, mode, strerror(errno));
+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);
-               if (chown(filename, uid, gid) != 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));
+                           file, uid, gid, strerror(errno));
                        goto exit;
                }
        }
@@ -167,23 +184,6 @@ static void set_to_local_user(char *user)
        endutent();
 }
 
-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];
@@ -253,7 +253,6 @@ static int create_node(struct udevice *dev, int fake)
        }
 
        if (!fake) {
-               unlink_entry(filename);
                info("creating device node '%s'", filename);
                if (make_node(filename, dev->major, dev->minor, dev->mode, uid, gid) != 0)
                        goto error;
@@ -270,7 +269,6 @@ static int create_node(struct udevice *dev, int 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);
                        }
@@ -304,11 +302,9 @@ 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) {
+                       unlink(filename);
                        if (symlink(linktarget, filename) != 0)
                                dbg("symlink(%s, %s) failed with error '%s'",
                                    linktarget, filename, strerror(errno));
@@ -320,7 +316,7 @@ 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;
@@ -345,7 +341,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;
@@ -399,11 +395,11 @@ static int rename_net_if(struct udevice *dev, int fake)
        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;
        struct udevice dev;
-       char key[DEVPATH_SIZE];
+       char devpath[DEVPATH_SIZE];
        char *pos;
        int retval;
 
@@ -446,37 +442,38 @@ int udev_add_device(char *path, char *subsystem, int fake)
        case 'b':
        case 'c':
                retval = create_node(&dev, fake);
-               if (fake || retval != 0)
+               if (retval != 0)
                        goto exit;
-               if (udevdb_add_dev(path, &dev) != 0)
+               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':
-               strfieldcpy(key, path);
+               strfieldcpy(devpath, path);
                if (strcmp(dev.name, dev.kernel_name) != 0) {
                        retval = rename_net_if(&dev, fake);
-                       if (fake || retval != 0)
+                       if (retval != 0)
                                goto exit;
                        /* netif's are keyed with the configured name, cause
                         * the original kernel name sleeps with the fishes
                         */
-                       pos = strrchr(key, '/');
+                       pos = strrchr(devpath, '/');
                        if (pos != NULL) {
                                pos[1] = '\0';
-                               strfieldcat(key, dev.name);
+                               strfieldcat(devpath, dev.name);
                        }
                }
-               if (udevdb_add_dev(key, &dev) != 0)
+               if ((!fake) && (udevdb_add_dev(devpath, &dev) != 0))
                        dbg("udevdb_add_dev failed");
+
+               dev_d_send(&dev, subsystem, devpath);
                break;
        }
 
-       /* execute programs in dev.d/ with the name in the environment */
-       dev_d_send(&dev, subsystem);
-
 exit:
        sysfs_close_class_device(class_dev);