udev: use readlink_malloc() or its friend

Follow-up for a2554acec6 and
7006860271.
This commit is contained in:
Yu Watanabe 2018-10-13 23:28:02 +09:00 committed by Lennart Poettering
parent 18094bdcf4
commit e7aa9512e4
2 changed files with 19 additions and 28 deletions

View file

@ -49,18 +49,14 @@ static int node_symlink(sd_device *dev, const char *node, const char *slink) {
log_error("Conflicting device node '%s' found, link to '%s' will not be created.", slink, node); log_error("Conflicting device node '%s' found, link to '%s' will not be created.", slink, node);
return -EOPNOTSUPP; return -EOPNOTSUPP;
} else if (S_ISLNK(stats.st_mode)) { } else if (S_ISLNK(stats.st_mode)) {
char buf[PATH_MAX]; _cleanup_free_ char *buf = NULL;
ssize_t len;
len = readlink(slink, buf, sizeof(buf)); if (readlink_malloc(slink, &buf) >= 0 &&
if (len > 0 && len < (ssize_t) sizeof(buf)) { streq(target, buf)) {
buf[len] = '\0'; log_debug("Preserve already existing symlink '%s' to '%s'", slink, target);
if (streq(target, buf)) { (void) label_fix(slink, LABEL_IGNORE_ENOENT);
log_debug("Preserve already existing symlink '%s' to '%s'", slink, target); (void) utimensat(AT_FDCWD, slink, NULL, AT_SYMLINK_NOFOLLOW);
(void) label_fix(slink, LABEL_IGNORE_ENOENT); return 0;
(void) utimensat(AT_FDCWD, slink, NULL, AT_SYMLINK_NOFOLLOW);
return 0;
}
} }
} }
} else { } else {

View file

@ -7,8 +7,10 @@
#include <sys/inotify.h> #include <sys/inotify.h>
#include <unistd.h> #include <unistd.h>
#include "alloc-util.h"
#include "device-private.h" #include "device-private.h"
#include "dirent-util.h" #include "dirent-util.h"
#include "fs-util.h"
#include "mkdir.h" #include "mkdir.h"
#include "stdio-util.h" #include "stdio-util.h"
#include "udev-watch.h" #include "udev-watch.h"
@ -48,21 +50,16 @@ int udev_watch_restore(void) {
FOREACH_DIRENT_ALL(ent, dir, break) { FOREACH_DIRENT_ALL(ent, dir, break) {
_cleanup_(sd_device_unrefp) sd_device *dev = NULL; _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
char device[PATH_MAX]; _cleanup_free_ char *device = NULL;
ssize_t len;
if (ent->d_name[0] == '.') if (ent->d_name[0] == '.')
continue; continue;
len = readlinkat(dirfd(dir), ent->d_name, device, sizeof(device)); r = readlinkat_malloc(dirfd(dir), ent->d_name, &device);
if (len <= 0) { if (r < 0) {
log_error_errno(errno, "Failed to read link '/run/udev/watch.old/%s', ignoring: %m", ent->d_name); log_error_errno(r, "Failed to read link '/run/udev/watch.old/%s', ignoring: %m", ent->d_name);
goto unlink;
} else if (len >= (ssize_t) sizeof(device)) {
log_error("Path specified by link '/run/udev/watch.old/%s' is truncated, ignoring.", ent->d_name);
goto unlink; goto unlink;
} }
device[len] = '\0';
r = sd_device_new_from_device_id(&dev, device); r = sd_device_new_from_device_id(&dev, device);
if (r < 0) { if (r < 0) {
@ -152,8 +149,8 @@ int udev_watch_end(sd_device *dev) {
} }
int udev_watch_lookup(int wd, sd_device **ret) { int udev_watch_lookup(int wd, sd_device **ret) {
char filename[STRLEN("/run/udev/watch/") + DECIMAL_STR_MAX(int)], device[PATH_MAX]; char filename[STRLEN("/run/udev/watch/") + DECIMAL_STR_MAX(int)];
ssize_t len; _cleanup_free_ char *device = NULL;
int r; int r;
assert(ret); assert(ret);
@ -165,14 +162,12 @@ int udev_watch_lookup(int wd, sd_device **ret) {
return log_error_errno(EINVAL, "Invalid watch handle."); return log_error_errno(EINVAL, "Invalid watch handle.");
xsprintf(filename, "/run/udev/watch/%d", wd); xsprintf(filename, "/run/udev/watch/%d", wd);
len = readlink(filename, device, sizeof(device)); r = readlink_malloc(filename, &device);
if (len <= 0) { if (r < 0) {
if (errno != ENOENT) if (r != -ENOENT)
return log_error_errno(errno, "Failed to read link '%s': %m", filename); return log_error_errno(errno, "Failed to read link '%s': %m", filename);
return 0; return 0;
} else if (len >= (ssize_t) sizeof(device)) }
return log_error_errno(ENAMETOOLONG, "Path specified by link '%s' is truncated.", filename);
device[len] = '\0';
r = sd_device_new_from_device_id(ret, device); r = sd_device_new_from_device_id(ret, device);
if (r < 0) if (r < 0)