use openat(), unlinkat(), fstatat()

This commit is contained in:
Kay Sievers 2009-05-20 18:58:52 +02:00
parent eba87f55f6
commit e6c1a2bde7
6 changed files with 16 additions and 26 deletions

1
TODO
View file

@ -2,7 +2,6 @@
o drop modprobe floppy alias (SUSE), it will be in the module (2.6.30)
o remove MMC rules, they got a modalias now (2.6.30)
o add scsi:t-0x09* to "ch" and remove modprobe rule (2.6.30)
o convert readdir loops to unlinkat(), fstatat()
o implement path_id in C with libudev (?)
o convert firmware.sh to C (?)

View file

@ -201,7 +201,6 @@ struct udev_list_entry *udev_queue_get_queued_list_entry(struct udev_queue *udev
if (dir == NULL)
return NULL;
for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
char filename[UTIL_PATH_SIZE];
char syspath[UTIL_PATH_SIZE];
char *s;
size_t l;
@ -209,10 +208,9 @@ struct udev_list_entry *udev_queue_get_queued_list_entry(struct udev_queue *udev
if (dent->d_name[0] == '.')
continue;
util_strscpyl(filename, sizeof(filename), path, "/", dent->d_name, NULL);
s = syspath;
l = util_strpcpyl(&s, sizeof(syspath), udev_get_sys_path(udev_queue->udev), NULL);
len = readlink(filename, s, l);
len = readlinkat(dirfd(dir), dent->d_name, s, l);
if (len < 0 || (size_t)len >= l)
continue;
s[len] = '\0';
@ -246,10 +244,9 @@ struct udev_list_entry *udev_queue_get_failed_list_entry(struct udev_queue *udev
if (dent->d_name[0] == '.')
continue;
util_strscpyl(filename, sizeof(filename), path, "/", dent->d_name, NULL);
s = syspath;
l = util_strpcpyl(&s, sizeof(syspath), udev_get_sys_path(udev_queue->udev), NULL);
len = readlink(filename, s, l);
len = readlinkat(dirfd(dir), dent->d_name, s, l);
if (len < 0 || (size_t)len >= l)
continue;
s[len] = '\0';

View file

@ -1641,10 +1641,8 @@ static int add_matching_files(struct udev *udev, struct udev_list_node *file_lis
if (strcmp(ext, suffix) != 0)
continue;
}
dbg(udev, "put file '%s/%s' into list\n", dirname, dent->d_name);
snprintf(filename, sizeof(filename), "%s/%s", dirname, dent->d_name);
filename[sizeof(filename)-1] = '\0';
util_strscpyl(filename, sizeof(filename), dirname, "/", dent->d_name, NULL);
dbg(udev, "put file '%s' into list\n", filename);
udev_list_entry_add(udev, file_list, filename, NULL, 1, 1);
}

View file

@ -72,8 +72,7 @@ void udev_watch_restore(struct udev *udev)
}
for (ent = readdir(dir); ent != NULL; ent = readdir(dir)) {
char path[UTIL_PATH_SIZE];
char buf[UTIL_PATH_SIZE];
char device[UTIL_PATH_SIZE];
char *s;
size_t l;
ssize_t len;
@ -82,19 +81,18 @@ void udev_watch_restore(struct udev *udev)
if (ent->d_name[0] < '0' || ent->d_name[0] > '9')
continue;
util_strscpyl(path, sizeof(path), oldname, "/", ent->d_name, NULL);
s = buf;
l = util_strpcpy(&s, sizeof(buf), udev_get_sys_path(udev));
len = readlink(path, s, l);
s = device;
l = util_strpcpy(&s, sizeof(device), udev_get_sys_path(udev));
len = readlinkat(dirfd(dir), ent->d_name, s, l);
if (len <= 0 || len >= (ssize_t)l) {
unlink(path);
unlinkat(dirfd(dir), ent->d_name, 0);
continue;
}
s[len] = '\0';
dbg(udev, "old watch to '%s' found\n", buf);
dev = udev_device_new_from_syspath(udev, buf);
dbg(udev, "old watch to '%s' found\n", device);
dev = udev_device_new_from_syspath(udev, device);
if (dev == NULL) {
unlink(path);
unlinkat(dirfd(dir), ent->d_name, 0);
continue;
}
@ -102,7 +100,7 @@ void udev_watch_restore(struct udev *udev)
udev_watch_begin(udev, dev);
udev_device_unref(dev);
unlink(path);
unlinkat(dirfd(dir), ent->d_name, 0);
}
closedir(dir);

View file

@ -25,6 +25,7 @@
#include <dirent.h>
#include <errno.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
@ -40,7 +41,6 @@ static void print_all_attributes(struct udev_device *device, const char *key)
if (dir != NULL) {
for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
struct stat statbuf;
char filename[UTIL_PATH_SIZE];
const char *value;
size_t len;
@ -52,8 +52,7 @@ static void print_all_attributes(struct udev_device *device, const char *key)
if (strcmp(dent->d_name, "dev") == 0)
continue;
util_strscpyl(filename, sizeof(filename), udev_device_get_syspath(device), "/", dent->d_name, NULL);
if (lstat(filename, &statbuf) != 0)
if (fstatat(dirfd(dir), dent->d_name, &statbuf, AT_SYMLINK_NOFOLLOW) != 0)
continue;
if (S_ISLNK(statbuf.st_mode))
continue;

View file

@ -649,8 +649,7 @@ static void cleanup_queue_dir(struct udev *udev)
break;
if (dent->d_name[0] == '.')
continue;
util_strscpyl(filename, sizeof(filename), dirname, "/", dent->d_name, NULL);
unlink(filename);
unlinkat(dirfd(dir), dent->d_name, 0);
}
closedir(dir);
rmdir(dirname);