2 * Copyright (C) 2004-2012 Kay Sievers <kay@vrfy.org>
3 * Copyright (C) 2009 Canonical Ltd.
4 * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <sys/types.h>
29 #include <sys/inotify.h>
33 static int inotify_fd = -1;
35 /* inotify descriptor, will be shared with rules directory;
36 * set to cloexec since we need our children to be able to add
39 int udev_watch_init(struct udev *udev)
41 inotify_fd = inotify_init1(IN_CLOEXEC);
43 log_error("inotify_init failed: %m\n");
47 /* move any old watches directory out of the way, and then restore
50 void udev_watch_restore(struct udev *udev)
55 if (rename("/run/udev/watch", "/run/udev/watch.old") == 0) {
59 dir = opendir("/run/udev/watch.old");
61 log_error("unable to open old watches dir /run/udev/watch.old; old watches will not be restored: %m");
65 for (ent = readdir(dir); ent != NULL; ent = readdir(dir)) {
66 char device[UTIL_PATH_SIZE];
68 struct udev_device *dev;
70 if (ent->d_name[0] == '.')
73 len = readlinkat(dirfd(dir), ent->d_name, device, sizeof(device));
74 if (len <= 0 || len == (ssize_t)sizeof(device))
78 dev = udev_device_new_from_device_id(udev, device);
82 log_debug("restoring old watch on '%s'\n", udev_device_get_devnode(dev));
83 udev_watch_begin(udev, dev);
84 udev_device_unref(dev);
86 unlinkat(dirfd(dir), ent->d_name, 0);
90 rmdir("/run/udev/watch.old");
92 } else if (errno != ENOENT) {
93 log_error("unable to move watches dir /run/udev/watch; old watches will not be restored: %m");
97 void udev_watch_begin(struct udev *udev, struct udev_device *dev)
99 char filename[UTIL_PATH_SIZE];
106 log_debug("adding watch on '%s'\n", udev_device_get_devnode(dev));
107 wd = inotify_add_watch(inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE);
109 log_error("inotify_add_watch(%d, %s, %o) failed: %m\n",
110 inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE);
114 snprintf(filename, sizeof(filename), "/run/udev/watch/%d", wd);
115 mkdir_parents(filename, 0755);
117 r = symlink(udev_device_get_id_filename(dev), filename);
119 log_error("Failed to create symlink %s: %m", filename);
121 udev_device_set_watch_handle(dev, wd);
124 void udev_watch_end(struct udev *udev, struct udev_device *dev)
127 char filename[UTIL_PATH_SIZE];
132 wd = udev_device_get_watch_handle(dev);
136 log_debug("removing watch on '%s'\n", udev_device_get_devnode(dev));
137 inotify_rm_watch(inotify_fd, wd);
139 snprintf(filename, sizeof(filename), "/run/udev/watch/%d", wd);
142 udev_device_set_watch_handle(dev, -1);
145 struct udev_device *udev_watch_lookup(struct udev *udev, int wd)
147 char filename[UTIL_PATH_SIZE];
148 char device[UTIL_NAME_SIZE];
151 if (inotify_fd < 0 || wd < 0)
154 snprintf(filename, sizeof(filename), "/run/udev/watch/%d", wd);
155 len = readlink(filename, device, sizeof(device));
156 if (len <= 0 || (size_t)len == sizeof(device))
160 return udev_device_new_from_device_id(udev, device);