From: Yu Watanabe Date: Wed, 2 Jun 2021 15:44:39 +0000 (+0900) Subject: udev: refuse to create device symlink when a non-symlink file already exists X-Git-Tag: v249-rc1~91^2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f3b393e951450a5983ec91f47451dc561a0d977d;p=thirdparty%2Fsystemd.git udev: refuse to create device symlink when a non-symlink file already exists --- diff --git a/src/udev/udev-node.c b/src/udev/udev-node.c index 2232b1dc8c1..d5a341baaac 100644 --- a/src/udev/udev-node.c +++ b/src/udev/udev-node.c @@ -52,21 +52,22 @@ static int node_symlink(sd_device *dev, const char *node, const char *slink) { if (r < 0) return log_device_error_errno(dev, r, "Failed to get relative path from '%s' to '%s': %m", slink, node); - /* preserve link with correct target, do not replace node of other device */ - if (lstat(slink, &stats) == 0) { - if (S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode)) - return log_device_error_errno(dev, SYNTHETIC_ERRNO(EOPNOTSUPP), - "Conflicting device node '%s' found, link to '%s' will not be created.", slink, node); - else if (S_ISLNK(stats.st_mode)) { - _cleanup_free_ char *buf = NULL; - - if (readlink_malloc(slink, &buf) >= 0 && - streq(target, buf)) { - log_device_debug(dev, "Preserve already existing symlink '%s' to '%s'", slink, target); - (void) label_fix(slink, LABEL_IGNORE_ENOENT); - (void) utimensat(AT_FDCWD, slink, NULL, AT_SYMLINK_NOFOLLOW); - return 0; - } + if (lstat(slink, &stats) >= 0) { + _cleanup_free_ char *buf = NULL; + + if (!S_ISLNK(stats.st_mode)) + return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EEXIST), + "Conflicting inode '%s' found, link to '%s' will not be created.", slink, node); + + if (readlink_malloc(slink, &buf) >= 0 && + streq(target, buf)) { + /* preserve link with correct target, do not replace node of other device */ + log_device_debug(dev, "Preserve already existing symlink '%s' to '%s'", slink, target); + + (void) label_fix(slink, LABEL_IGNORE_ENOENT); + (void) utimensat(AT_FDCWD, slink, NULL, AT_SYMLINK_NOFOLLOW); + + return 0; } } else { log_device_debug(dev, "Creating symlink '%s' to '%s'", slink, target);