1
0
mirror of https://github.com/systemd/systemd.git synced 2025-03-09 12:58:26 +03:00

sd-device: move device_new_from_watch_handle_at() to udev-watch.c

And drop unused watch handle related functions.
This commit is contained in:
Yu Watanabe 2022-04-15 06:38:33 +09:00
parent 790da548b0
commit 3fb94c7062
6 changed files with 28 additions and 147 deletions

View File

@ -21,8 +21,6 @@ struct sd_device {
*/
unsigned database_version;
int watch_handle;
sd_device *parent;
OrderedHashmap *properties;

View File

@ -619,144 +619,6 @@ int device_get_devlink_priority(sd_device *device, int *ret) {
return 0;
}
int device_get_watch_handle(sd_device *device) {
char path_wd[STRLEN("/run/udev/watch/") + DECIMAL_STR_MAX(int)];
_cleanup_free_ char *buf = NULL;
const char *id, *path_id;
int wd, r;
assert(device);
if (device->watch_handle >= 0)
return device->watch_handle;
r = device_get_device_id(device, &id);
if (r < 0)
return r;
path_id = strjoina("/run/udev/watch/", id);
r = readlink_malloc(path_id, &buf);
if (r < 0)
return r;
r = safe_atoi(buf, &wd);
if (r < 0)
return r;
if (wd < 0)
return -EBADF;
buf = mfree(buf);
xsprintf(path_wd, "/run/udev/watch/%d", wd);
r = readlink_malloc(path_wd, &buf);
if (r < 0)
return r;
if (!streq(buf, id))
return -EBADF;
return device->watch_handle = wd;
}
static void device_remove_watch_handle(sd_device *device) {
const char *id;
int wd;
assert(device);
/* First, remove the symlink from handle to device id. */
wd = device_get_watch_handle(device);
if (wd >= 0) {
char path_wd[STRLEN("/run/udev/watch/") + DECIMAL_STR_MAX(int)];
xsprintf(path_wd, "/run/udev/watch/%d", wd);
if (unlink(path_wd) < 0 && errno != ENOENT)
log_device_debug_errno(device, errno,
"sd-device: failed to remove %s, ignoring: %m",
path_wd);
}
/* Next, remove the symlink from device id to handle. */
if (device_get_device_id(device, &id) >= 0) {
const char *path_id;
path_id = strjoina("/run/udev/watch/", id);
if (unlink(path_id) < 0 && errno != ENOENT)
log_device_debug_errno(device, errno,
"sd-device: failed to remove %s, ignoring: %m",
path_id);
}
device->watch_handle = -1;
}
int device_set_watch_handle(sd_device *device, int wd) {
char path_wd[STRLEN("/run/udev/watch/") + DECIMAL_STR_MAX(int)];
const char *id, *path_id;
int r;
assert(device);
if (wd >= 0 && wd == device_get_watch_handle(device))
return 0;
device_remove_watch_handle(device);
if (wd < 0)
/* negative wd means that the caller requests to clear saved watch handle. */
return 0;
r = device_get_device_id(device, &id);
if (r < 0)
return r;
path_id = strjoina("/run/udev/watch/", id);
xsprintf(path_wd, "/run/udev/watch/%d", wd);
r = mkdir_parents(path_wd, 0755);
if (r < 0)
return r;
if (symlink(id, path_wd) < 0)
return -errno;
if (symlink(path_wd + STRLEN("/run/udev/watch/"), path_id) < 0) {
r = -errno;
if (unlink(path_wd) < 0 && errno != ENOENT)
log_device_debug_errno(device, errno,
"sd-device: failed to remove %s, ignoring: %m",
path_wd);
return r;
}
device->watch_handle = wd;
return 0;
}
int device_new_from_watch_handle_at(sd_device **ret, int dirfd, int wd) {
char path_wd[STRLEN("/run/udev/watch/") + DECIMAL_STR_MAX(int)];
_cleanup_free_ char *id = NULL;
int r;
assert(ret);
if (wd < 0)
return -EBADF;
if (dirfd >= 0) {
xsprintf(path_wd, "%d", wd);
r = readlinkat_malloc(dirfd, path_wd, &id);
} else {
xsprintf(path_wd, "/run/udev/watch/%d", wd);
r = readlink_malloc(path_wd, &id);
}
if (r < 0)
return r;
return sd_device_new_from_device_id(ret, id);
}
int device_rename(sd_device *device, const char *name) {
_cleanup_free_ char *new_syspath = NULL;
const char *interface;

View File

@ -13,17 +13,12 @@
int device_new_from_mode_and_devnum(sd_device **ret, mode_t mode, dev_t devnum);
int device_new_from_nulstr(sd_device **ret, char *nulstr, size_t len);
int device_new_from_strv(sd_device **ret, char **strv);
int device_new_from_watch_handle_at(sd_device **ret, int dirfd, int wd);
static inline int device_new_from_watch_handle(sd_device **ret, int wd) {
return device_new_from_watch_handle_at(ret, -1, wd);
}
int device_get_property_bool(sd_device *device, const char *key);
int device_get_sysattr_unsigned(sd_device *device, const char *sysattr, unsigned *ret_value);
int device_get_sysattr_bool(sd_device *device, const char *sysattr);
int device_get_device_id(sd_device *device, const char **ret);
int device_get_devlink_priority(sd_device *device, int *ret);
int device_get_watch_handle(sd_device *device);
int device_get_devnode_mode(sd_device *device, mode_t *ret);
int device_get_devnode_uid(sd_device *device, uid_t *ret);
int device_get_devnode_gid(sd_device *device, gid_t *ret);
@ -34,7 +29,6 @@ int device_get_cached_sysattr_value(sd_device *device, const char *key, const ch
void device_seal(sd_device *device);
void device_set_is_initialized(sd_device *device);
int device_set_watch_handle(sd_device *device, int wd);
void device_set_db_persist(sd_device *device);
void device_set_devlink_priority(sd_device *device, int priority);
int device_ensure_usec_initialized(sd_device *device, sd_device *device_old);

View File

@ -46,7 +46,6 @@ int device_new_aux(sd_device **ret) {
*device = (sd_device) {
.n_ref = 1,
.watch_handle = -1,
.devmode = MODE_INVALID,
.devuid = UID_INVALID,
.devgid = GID_INVALID,

View File

@ -19,6 +19,29 @@
#include "string-util.h"
#include "udev-watch.h"
int device_new_from_watch_handle_at(sd_device **ret, int dirfd, int wd) {
char path_wd[STRLEN("/run/udev/watch/") + DECIMAL_STR_MAX(int)];
_cleanup_free_ char *id = NULL;
int r;
assert(ret);
if (wd < 0)
return -EBADF;
if (dirfd >= 0) {
xsprintf(path_wd, "%d", wd);
r = readlinkat_malloc(dirfd, path_wd, &id);
} else {
xsprintf(path_wd, "/run/udev/watch/%d", wd);
r = readlink_malloc(path_wd, &id);
}
if (r < 0)
return r;
return sd_device_new_from_device_id(ret, id);
}
int udev_watch_restore(int inotify_fd) {
_cleanup_closedir_ DIR *dir = NULL;
int r;

View File

@ -3,6 +3,11 @@
#include "sd-device.h"
int device_new_from_watch_handle_at(sd_device **ret, int dirfd, int wd);
static inline int device_new_from_watch_handle(sd_device **ret, int wd) {
return device_new_from_watch_handle_at(ret, -1, wd);
}
int udev_watch_restore(int inotify_fd);
int udev_watch_begin(int inotify_fd, sd_device *dev);
int udev_watch_end(int inotify_fd, sd_device *dev);