]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-watch.c
1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright © 2009 Canonical Ltd.
4 * Copyright © 2009 Scott James Remnant <scott@netsplit.com>
10 #include <sys/inotify.h>
13 #include "dirent-util.h"
14 #include "stdio-util.h"
17 static int inotify_fd
= -1;
19 /* inotify descriptor, will be shared with rules directory;
20 * set to cloexec since we need our children to be able to add
23 int udev_watch_init(struct udev
*udev
) {
24 inotify_fd
= inotify_init1(IN_CLOEXEC
);
26 log_error_errno(errno
, "inotify_init failed: %m");
30 /* move any old watches directory out of the way, and then restore
33 void udev_watch_restore(struct udev
*udev
) {
37 if (rename("/run/udev/watch", "/run/udev/watch.old") == 0) {
41 dir
= opendir("/run/udev/watch.old");
43 log_error_errno(errno
, "unable to open old watches dir /run/udev/watch.old; old watches will not be restored: %m");
47 FOREACH_DIRENT_ALL(ent
, dir
, break) {
48 char device
[UTIL_PATH_SIZE
];
50 struct udev_device
*dev
;
52 if (ent
->d_name
[0] == '.')
55 len
= readlinkat(dirfd(dir
), ent
->d_name
, device
, sizeof(device
));
56 if (len
<= 0 || len
== (ssize_t
)sizeof(device
))
60 dev
= udev_device_new_from_device_id(udev
, device
);
64 log_debug("restoring old watch on '%s'", udev_device_get_devnode(dev
));
65 udev_watch_begin(udev
, dev
);
66 udev_device_unref(dev
);
68 (void) unlinkat(dirfd(dir
), ent
->d_name
, 0);
72 rmdir("/run/udev/watch.old");
74 } else if (errno
!= ENOENT
)
75 log_error_errno(errno
, "unable to move watches dir /run/udev/watch; old watches will not be restored: %m");
78 void udev_watch_begin(struct udev
*udev
, struct udev_device
*dev
) {
79 char filename
[sizeof("/run/udev/watch/") + DECIMAL_STR_MAX(int)];
86 log_debug("adding watch on '%s'", udev_device_get_devnode(dev
));
87 wd
= inotify_add_watch(inotify_fd
, udev_device_get_devnode(dev
), IN_CLOSE_WRITE
);
89 log_error_errno(errno
, "inotify_add_watch(%d, %s, %o) failed: %m",
90 inotify_fd
, udev_device_get_devnode(dev
), IN_CLOSE_WRITE
);
94 xsprintf(filename
, "/run/udev/watch/%d", wd
);
95 mkdir_parents(filename
, 0755);
97 r
= symlink(udev_device_get_id_filename(dev
), filename
);
99 log_error_errno(errno
, "Failed to create symlink %s: %m", filename
);
101 udev_device_set_watch_handle(dev
, wd
);
104 void udev_watch_end(struct udev
*udev
, struct udev_device
*dev
) {
106 char filename
[sizeof("/run/udev/watch/") + DECIMAL_STR_MAX(int)];
111 wd
= udev_device_get_watch_handle(dev
);
115 log_debug("removing watch on '%s'", udev_device_get_devnode(dev
));
116 inotify_rm_watch(inotify_fd
, wd
);
118 xsprintf(filename
, "/run/udev/watch/%d", wd
);
121 udev_device_set_watch_handle(dev
, -1);
124 struct udev_device
*udev_watch_lookup(struct udev
*udev
, int wd
) {
125 char filename
[sizeof("/run/udev/watch/") + DECIMAL_STR_MAX(int)];
126 char device
[UTIL_NAME_SIZE
];
129 if (inotify_fd
< 0 || wd
< 0)
132 xsprintf(filename
, "/run/udev/watch/%d", wd
);
133 len
= readlink(filename
, device
, sizeof(device
));
134 if (len
<= 0 || (size_t)len
== sizeof(device
))
138 return udev_device_new_from_device_id(udev
, device
);