]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-watch.c
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Copyright © 2009 Canonical Ltd.
4 * Copyright © 2009 Scott James Remnant <scott@netsplit.com>
7 #include <sys/inotify.h>
9 #include "alloc-util.h"
10 #include "device-private.h"
11 #include "device-util.h"
12 #include "dirent-util.h"
16 #include "parse-util.h"
18 #include "stdio-util.h"
19 #include "string-util.h"
20 #include "udev-util.h"
21 #include "udev-watch.h"
23 int device_new_from_watch_handle_at(sd_device
**ret
, int dirfd
, int wd
) {
24 char path_wd
[STRLEN("/run/udev/watch/") + DECIMAL_STR_MAX(int)];
25 _cleanup_free_
char *id
= NULL
;
34 xsprintf(path_wd
, "%d", wd
);
35 r
= readlinkat_malloc(dirfd
, path_wd
, &id
);
37 xsprintf(path_wd
, "/run/udev/watch/%d", wd
);
38 r
= readlink_malloc(path_wd
, &id
);
43 return sd_device_new_from_device_id(ret
, id
);
46 int udev_watch_restore(int inotify_fd
) {
47 _cleanup_closedir_
DIR *dir
= NULL
;
50 /* Move any old watches directory out of the way, and then restore the watches. */
52 assert(inotify_fd
>= 0);
54 (void) rm_rf("/run/udev/watch.old", REMOVE_ROOT
);
56 if (rename("/run/udev/watch", "/run/udev/watch.old") < 0) {
60 r
= log_warning_errno(errno
,
61 "Failed to move watches directory '/run/udev/watch/'. "
62 "Old watches will not be restored: %m");
66 dir
= opendir("/run/udev/watch.old");
68 r
= log_warning_errno(errno
,
69 "Failed to open old watches directory '/run/udev/watch.old/'. "
70 "Old watches will not be restored: %m");
74 FOREACH_DIRENT_ALL(de
, dir
, break) {
75 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
78 /* For backward compatibility, read symlink from watch handle to device ID. This is necessary
79 * when udevd is restarted after upgrading from v248 or older. The new format (ID -> wd) was
80 * introduced by e7f781e473f5119bf9246208a6de9f6b76a39c5d (v249). */
82 if (dot_or_dot_dot(de
->d_name
))
85 if (safe_atoi(de
->d_name
, &wd
) < 0)
88 r
= device_new_from_watch_handle_at(&dev
, dirfd(dir
), wd
);
90 log_full_errno(r
== -ENODEV
? LOG_DEBUG
: LOG_WARNING
, r
,
91 "Failed to create sd_device object from saved watch handle '%i', ignoring: %m",
96 (void) udev_watch_begin(inotify_fd
, dev
);
102 (void) rm_rf("/run/udev/watch.old", REMOVE_ROOT
);
106 static int udev_watch_clear(sd_device
*dev
, int dirfd
, int *ret_wd
) {
107 _cleanup_free_
char *wd_str
= NULL
, *buf
= NULL
;
114 r
= device_get_device_id(dev
, &id
);
116 return log_device_debug_errno(dev
, r
, "Failed to get device ID: %m");
118 /* 1. read symlink ID -> wd */
119 r
= readlinkat_malloc(dirfd
, id
, &wd_str
);
126 log_device_debug_errno(dev
, r
, "Failed to read symlink '/run/udev/watch/%s': %m", id
);
130 r
= safe_atoi(wd_str
, &wd
);
132 log_device_debug_errno(dev
, r
, "Failed to parse watch handle from symlink '/run/udev/watch/%s': %m", id
);
137 r
= log_device_debug_errno(dev
, SYNTHETIC_ERRNO(EBADF
), "Invalid watch handle %i.", wd
);
141 /* 2. read symlink wd -> ID */
142 r
= readlinkat_malloc(dirfd
, wd_str
, &buf
);
144 log_device_debug_errno(dev
, r
, "Failed to read symlink '/run/udev/watch/%s': %m", wd_str
);
148 /* 3. check if the symlink wd -> ID is owned by the device. */
149 if (!streq(buf
, id
)) {
150 r
= log_device_debug_errno(dev
, SYNTHETIC_ERRNO(ENOENT
),
151 "Symlink '/run/udev/watch/%s' is owned by another device '%s'.", wd_str
, buf
);
155 /* 4. remove symlink wd -> ID.
156 * In the above, we already confirmed that the symlink is owned by us. Hence, no other workers remove
157 * the symlink and cannot create a new symlink with the same filename but to a different ID. Hence,
158 * the removal below is safe even the steps in this function are not atomic. */
159 if (unlinkat(dirfd
, wd_str
, 0) < 0 && errno
!= ENOENT
)
160 log_device_debug_errno(dev
, errno
, "Failed to remove '/run/udev/watch/%s', ignoring: %m", wd_str
);
167 /* 5. remove symlink ID -> wd.
168 * The file is always owned by the device. Hence, it is safe to remove it unconditionally. */
169 if (unlinkat(dirfd
, id
, 0) < 0 && errno
!= ENOENT
)
170 log_device_debug_errno(dev
, errno
, "Failed to remove '/run/udev/watch/%s': %m", id
);
175 int udev_watch_begin(int inotify_fd
, sd_device
*dev
) {
176 char wd_str
[DECIMAL_STR_MAX(int)];
177 _cleanup_close_
int dirfd
= -EBADF
;
178 const char *devnode
, *id
;
181 assert(inotify_fd
>= 0);
184 if (device_for_action(dev
, SD_DEVICE_REMOVE
))
187 r
= sd_device_get_devname(dev
, &devnode
);
189 return log_device_debug_errno(dev
, r
, "Failed to get device node: %m");
191 r
= device_get_device_id(dev
, &id
);
193 return log_device_debug_errno(dev
, r
, "Failed to get device ID: %m");
195 r
= dirfd
= open_mkdir_at(AT_FDCWD
, "/run/udev/watch", O_CLOEXEC
| O_RDONLY
, 0755);
197 return log_device_debug_errno(dev
, r
, "Failed to create and open '/run/udev/watch/': %m");
199 /* 1. Clear old symlinks */
200 (void) udev_watch_clear(dev
, dirfd
, NULL
);
202 /* 2. Add inotify watch */
203 log_device_debug(dev
, "Adding watch on '%s'", devnode
);
204 wd
= inotify_add_watch(inotify_fd
, devnode
, IN_CLOSE_WRITE
);
206 return log_device_debug_errno(dev
, errno
, "Failed to watch device node '%s': %m", devnode
);
208 xsprintf(wd_str
, "%d", wd
);
210 /* 3. Create new symlinks */
211 if (symlinkat(wd_str
, dirfd
, id
) < 0) {
212 r
= log_device_debug_errno(dev
, errno
, "Failed to create symlink '/run/udev/watch/%s' to '%s': %m", id
, wd_str
);
216 if (symlinkat(id
, dirfd
, wd_str
) < 0) {
217 /* Possibly, the watch handle is previously assigned to another device, and udev_watch_end()
218 * is not called for the device yet. */
219 r
= log_device_debug_errno(dev
, errno
, "Failed to create symlink '/run/udev/watch/%s' to '%s': %m", wd_str
, id
);
226 (void) unlinkat(dirfd
, id
, 0);
227 (void) inotify_rm_watch(inotify_fd
, wd
);
231 int udev_watch_end(int inotify_fd
, sd_device
*dev
) {
232 _cleanup_close_
int dirfd
= -EBADF
;
235 assert(inotify_fd
>= 0);
238 if (sd_device_get_devname(dev
, NULL
) < 0)
241 dirfd
= RET_NERRNO(open("/run/udev/watch", O_CLOEXEC
| O_DIRECTORY
| O_NOFOLLOW
| O_RDONLY
));
242 if (dirfd
== -ENOENT
)
245 return log_device_debug_errno(dev
, dirfd
, "Failed to open '/run/udev/watch/': %m");
247 /* First, clear symlinks. */
248 r
= udev_watch_clear(dev
, dirfd
, &wd
);
252 /* Then, remove inotify watch. */
253 log_device_debug(dev
, "Removing watch handle %i.", wd
);
254 (void) inotify_rm_watch(inotify_fd
, wd
);