chiark / gitweb /
do not skip RUN execution if device node removal fails
[elogind.git] / udev_node.c
index 8c0287405c537505bc80a32423c44090fabcffc4..bd58a6c485d9ddc6128bd4d31079ec54ba63fc6e 100644 (file)
@@ -46,24 +46,24 @@ int udev_node_mknod(struct udevice *udev, const char *file, dev_t devt, mode_t m
        else
                mode |= S_IFCHR;
 
-       /* preserve node with already correct numbers, to prevent changing the inode number */
        if (lstat(file, &stats) == 0) {
                if ((stats.st_mode & S_IFMT) == (mode & S_IFMT) && (stats.st_rdev == devt)) {
                        info("preserve file '%s', because it has correct dev_t", file);
                        selinux_setfilecon(file, udev->dev->kernel, stats.st_mode);
                        goto perms;
                }
+       } else {
+               selinux_setfscreatecon(file, udev->dev->kernel, mode);
+               retval = mknod(file, mode, devt);
+               selinux_resetfscreatecon();
+               if (retval == 0)
+                       goto perms;
        }
 
-       selinux_setfscreatecon(file, udev->dev->kernel, mode);
-       retval = mknod(file, mode, devt);
-       selinux_resetfscreatecon();
-       if (retval == 0)
-               goto perms;
-
        info("atomically replace '%s'", file);
        strlcpy(file_tmp, file, sizeof(file_tmp));
        strlcat(file_tmp, TMP_FILE_EXT, sizeof(file_tmp));
+       unlink(file_tmp);
        selinux_setfscreatecon(file_tmp, udev->dev->kernel, mode);
        retval = mknod(file_tmp, mode, devt);
        selinux_resetfscreatecon();
@@ -133,7 +133,7 @@ static int node_symlink(const char *node, const char *slink)
                                    stats.st_rdev == stats2.st_rdev) {
                                        info("replace device node '%s' with symlink to our node '%s'", slink, node);
                                } else {
-                                       err("device node '%s' already exists, link '%s' will not overwrite it", node, slink);
+                                       err("device node '%s' already exists, link to '%s' will not overwrite it", slink, node);
                                        goto exit;
                                }
                        }
@@ -151,18 +151,19 @@ static int node_symlink(const char *node, const char *slink)
                                }
                        }
                }
+       } else {
+               info("creating symlink '%s' to '%s'", slink, target);
+               selinux_setfscreatecon(slink, NULL, S_IFLNK);
+               retval = symlink(target, slink);
+               selinux_resetfscreatecon();
+               if (retval == 0)
+                       goto exit;
        }
 
-       info("creating symlink '%s' to '%s'", slink, target);
-       selinux_setfscreatecon(slink, NULL, S_IFLNK);
-       retval = symlink(target, slink);
-       selinux_resetfscreatecon();
-       if (retval == 0)
-               goto exit;
-
        info("atomically replace '%s'", slink);
        strlcpy(slink_tmp, slink, sizeof(slink_tmp));
        strlcat(slink_tmp, TMP_FILE_EXT, sizeof(slink_tmp));
+       unlink(slink_tmp);
        selinux_setfscreatecon(slink_tmp, NULL, S_IFLNK);
        retval = symlink(target, slink_tmp);
        selinux_resetfscreatecon();
@@ -217,23 +218,29 @@ static int update_link(struct udevice *udev, const char *name)
                if (strcmp(udev->dev->devpath, device->name) == 0) {
                        info("compare (our own) priority of '%s' %i >= %i",
                             udev->dev->devpath, udev->link_priority, priority);
-                       if (target[0] == '\0' || udev->link_priority >= priority) {
+                       if (strcmp(udev->name, name) == 0) {
+                               info("'%s' is our device node, database inconsistent, skip link update", udev->name);
+                       } else if (target[0] == '\0' || udev->link_priority >= priority) {
                                priority = udev->link_priority;
                                strlcpy(target, udev->name, sizeof(target));
                        }
                        continue;
                }
 
-               /* or something else, then read priority from database */
+               /* another device, read priority from database */
                udev_db = udev_device_init(NULL);
                if (udev_db == NULL)
                        continue;
                if (udev_db_get_device(udev_db, device->name) == 0) {
-                       info("compare priority of '%s' %i > %i",
-                            udev_db->dev->devpath, udev_db->link_priority, priority);
-                       if (target[0] == '\0' || udev_db->link_priority > priority) {
-                               priority = udev_db->link_priority;
-                               strlcpy(target, udev_db->name, sizeof(target));
+                       if (strcmp(udev_db->name, name) == 0) {
+                               info("'%s' is a device node of '%s', skip link update", udev_db->name, device->name);
+                       } else {
+                               info("compare priority of '%s' %i > %i",
+                                    udev_db->dev->devpath, udev_db->link_priority, priority);
+                               if (target[0] == '\0' || udev_db->link_priority > priority) {
+                                       priority = udev_db->link_priority;
+                                       strlcpy(target, udev_db->name, sizeof(target));
+                               }
                        }
                }
                udev_device_cleanup(udev_db);
@@ -241,8 +248,8 @@ static int update_link(struct udevice *udev, const char *name)
        name_list_cleanup(&name_list);
 
        if (target[0] == '\0') {
-               err("missing target for '%s'", name);
-               rc = -1;
+               info("no current target for '%s' found", name);
+               rc = 1;
                goto out;
        }
 
@@ -401,8 +408,8 @@ int udev_node_remove(struct udevice *udev)
        strlcat(filename, "/", sizeof(filename));
        strlcat(filename, udev->name, sizeof(filename));
        if (stat(filename, &stats) != 0) {
-               dbg("device node '%s' not found", filename);
-               return -1;
+               info("device node '%s' not found", filename);
+               return 0;
        }
        if (udev->devt && stats.st_rdev != udev->devt) {
                info("device node '%s' points to a different device, skip removal", filename);