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;
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);
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);
#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;