1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-06 17:18:12 +03:00

udev: let stack_directory_open() convert a slink into a dirname itself

We likely always want to open the directory via a slink.

There's currently only one caller so it doesn't make any difference in practice
but I think it's still nicer.

No functional change.
This commit is contained in:
Franck Bui 2022-12-14 19:04:16 +01:00 committed by Yu Watanabe
parent c9032f910c
commit 72a459adc4

View File

@ -11,6 +11,7 @@
#include "dirent-util.h"
#include "escape.h"
#include "fd-util.h"
#include "fileio.h"
#include "format-util.h"
#include "fs-util.h"
#include "hexdecoct.h"
@ -175,14 +176,14 @@ static int stack_directory_read_one(int dirfd, const char *id, bool is_symlink,
return 1; /* Updated */
}
static int stack_directory_find_prioritized_devnode(sd_device *dev, const char *dirname, bool add, char **ret) {
static int stack_directory_find_prioritized_devnode(sd_device *dev, int dirfd, bool add, char **ret) {
_cleanup_closedir_ DIR *dir = NULL;
_cleanup_free_ char *devnode = NULL;
int r, priority = 0;
const char *id;
assert(dev);
assert(dirname);
assert(dirfd >= 0);
assert(ret);
/* Find device node of device with highest priority. This returns 1 if a device found, 0 if no
@ -204,7 +205,7 @@ static int stack_directory_find_prioritized_devnode(sd_device *dev, const char *
return -ENOMEM;
}
dir = opendir(dirname);
dir = xopendirat(dirfd, ".", O_NOFOLLOW);
if (!dir)
return -errno;
@ -223,9 +224,9 @@ static int stack_directory_find_prioritized_devnode(sd_device *dev, const char *
if (!IN_SET(de->d_type, DT_LNK, DT_REG))
continue;
r = stack_directory_read_one(dirfd(dir), de->d_name, /* is_symlink = */ de->d_type == DT_LNK, &devnode, &priority);
r = stack_directory_read_one(dirfd, de->d_name, /* is_symlink = */ de->d_type == DT_LNK, &devnode, &priority);
if (r < 0) {
log_debug_errno(r, "Failed to read '%s/%s', ignoring: %m", dirname, de->d_name);
log_debug_errno(r, "Failed to read '%s', ignoring: %m", de->d_name);
continue;
}
}
@ -353,15 +354,20 @@ static int stack_directory_get_name(const char *slink, char **ret) {
return 0;
}
static int stack_directory_open(sd_device *dev, const char *dirname, int *ret_dirfd, int *ret_lockfd) {
static int stack_directory_open(sd_device *dev, const char *slink, int *ret_dirfd, int *ret_lockfd) {
_cleanup_close_ int dirfd = -EBADF, lockfd = -EBADF;
_cleanup_free_ char *dirname = NULL;
int r;
assert(dev);
assert(dirname);
assert(slink);
assert(ret_dirfd);
assert(ret_lockfd);
r = stack_directory_get_name(slink, &dirname);
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to build stack directory name for '%s': %m", slink);
r = mkdir_parents(dirname, 0755);
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to create stack directory '%s': %m", dirname);
@ -375,7 +381,7 @@ static int stack_directory_open(sd_device *dev, const char *dirname, int *ret_di
return log_device_debug_errno(dev, errno, "Failed to create lock file for stack directory '%s': %m", dirname);
if (flock(lockfd, LOCK_EX) < 0)
return log_device_debug_errno(dev, errno, "Failed to place a lock on lock file for '%s': %m", dirname);
return log_device_debug_errno(dev, errno, "Failed to place a lock on lock file for %s: %m", dirname);
*ret_dirfd = TAKE_FD(dirfd);
*ret_lockfd = TAKE_FD(lockfd);
@ -383,26 +389,22 @@ static int stack_directory_open(sd_device *dev, const char *dirname, int *ret_di
}
static int link_update(sd_device *dev, const char *slink, bool add) {
_cleanup_free_ char *dirname = NULL, *devnode = NULL;
_cleanup_close_ int dirfd = -EBADF, lockfd = -EBADF;
_cleanup_free_ char *devnode = NULL;
int r;
assert(dev);
assert(slink);
r = stack_directory_get_name(slink, &dirname);
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to build stack directory name for '%s': %m", slink);
r = stack_directory_open(dev, dirname, &dirfd, &lockfd);
r = stack_directory_open(dev, slink, &dirfd, &lockfd);
if (r < 0)
return r;
r = stack_directory_update(dev, dirfd, add);
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to update stack directory '%s': %m", dirname);
return log_device_debug_errno(dev, r, "Failed to update stack directory for '%s': %m", slink);
r = stack_directory_find_prioritized_devnode(dev, dirname, add, &devnode);
r = stack_directory_find_prioritized_devnode(dev, dirfd, add, &devnode);
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to determine device node with the highest priority for '%s': %m", slink);
if (r > 0)