]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-watch.c
9d3e69bc9382fef13ec74c346e99b3f08b40e669
1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright © 2004-2012 Kay Sievers <kay@vrfy.org>
4 * Copyright © 2009 Canonical Ltd.
5 * Copyright © 2009 Scott James Remnant <scott@netsplit.com>
12 #include <sys/inotify.h>
15 #include "dirent-util.h"
16 #include "stdio-util.h"
19 static int inotify_fd
= -1;
21 /* inotify descriptor, will be shared with rules directory;
22 * set to cloexec since we need our children to be able to add
25 int udev_watch_init(struct udev
*udev
) {
26 inotify_fd
= inotify_init1(IN_CLOEXEC
);
28 log_error_errno(errno
, "inotify_init failed: %m");
32 /* move any old watches directory out of the way, and then restore
35 void udev_watch_restore(struct udev
*udev
) {
39 if (rename("/run/udev/watch", "/run/udev/watch.old") == 0) {
43 dir
= opendir("/run/udev/watch.old");
45 log_error_errno(errno
, "unable to open old watches dir /run/udev/watch.old; old watches will not be restored: %m");
49 FOREACH_DIRENT_ALL(ent
, dir
, break) {
50 char device
[UTIL_PATH_SIZE
];
52 struct udev_device
*dev
;
54 if (ent
->d_name
[0] == '.')
57 len
= readlinkat(dirfd(dir
), ent
->d_name
, device
, sizeof(device
));
58 if (len
<= 0 || len
== (ssize_t
)sizeof(device
))
62 dev
= udev_device_new_from_device_id(udev
, device
);
66 log_debug("restoring old watch on '%s'", udev_device_get_devnode(dev
));
67 udev_watch_begin(udev
, dev
);
68 udev_device_unref(dev
);
70 (void) unlinkat(dirfd(dir
), ent
->d_name
, 0);
74 rmdir("/run/udev/watch.old");
76 } else if (errno
!= ENOENT
)
77 log_error_errno(errno
, "unable to move watches dir /run/udev/watch; old watches will not be restored: %m");
80 void udev_watch_begin(struct udev
*udev
, struct udev_device
*dev
) {
81 char filename
[sizeof("/run/udev/watch/") + DECIMAL_STR_MAX(int)];
88 log_debug("adding watch on '%s'", udev_device_get_devnode(dev
));
89 wd
= inotify_add_watch(inotify_fd
, udev_device_get_devnode(dev
), IN_CLOSE_WRITE
);
91 log_error_errno(errno
, "inotify_add_watch(%d, %s, %o) failed: %m",
92 inotify_fd
, udev_device_get_devnode(dev
), IN_CLOSE_WRITE
);
96 xsprintf(filename
, "/run/udev/watch/%d", wd
);
97 mkdir_parents(filename
, 0755);
99 r
= symlink(udev_device_get_id_filename(dev
), filename
);
101 log_error_errno(errno
, "Failed to create symlink %s: %m", filename
);
103 udev_device_set_watch_handle(dev
, wd
);
106 void udev_watch_end(struct udev
*udev
, struct udev_device
*dev
) {
108 char filename
[sizeof("/run/udev/watch/") + DECIMAL_STR_MAX(int)];
113 wd
= udev_device_get_watch_handle(dev
);
117 log_debug("removing watch on '%s'", udev_device_get_devnode(dev
));
118 inotify_rm_watch(inotify_fd
, wd
);
120 xsprintf(filename
, "/run/udev/watch/%d", wd
);
123 udev_device_set_watch_handle(dev
, -1);
126 struct udev_device
*udev_watch_lookup(struct udev
*udev
, int wd
) {
127 char filename
[sizeof("/run/udev/watch/") + DECIMAL_STR_MAX(int)];
128 char device
[UTIL_NAME_SIZE
];
131 if (inotify_fd
< 0 || wd
< 0)
134 xsprintf(filename
, "/run/udev/watch/%d", wd
);
135 len
= readlink(filename
, device
, sizeof(device
));
136 if (len
<= 0 || (size_t)len
== sizeof(device
))
140 return udev_device_new_from_device_id(udev
, device
);