2 * Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
3 * Copyright (C) 2009 Canonical Ltd.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <sys/types.h>
29 #include <sys/inotify.h>
36 /* inotify descriptor, will be shared with rules directory;
37 * set to cloexec since we need our children to be able to add
40 void udev_watch_init(struct udev *udev)
42 inotify_fd = inotify_init();
43 if (inotify_fd >= 0) {
46 flags = fcntl(inotify_fd, F_GETFD);
51 fcntl(inotify_fd, F_SETFD, flags);
52 } else if (errno == ENOSYS)
53 info(udev, "unable to use inotify, udevd will not monitor rule files changes\n");
55 err(udev, "inotify_init failed: %m\n");
58 /* move any old watches directory out of the way, and then restore
61 void udev_watch_restore(struct udev *udev)
63 char filename[UTIL_PATH_SIZE], oldname[UTIL_PATH_SIZE];
68 util_strlcpy(oldname, udev_get_dev_path(udev), sizeof(oldname));
69 util_strlcat(oldname, "/.udev/watch.old", sizeof(oldname));
71 util_strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
72 util_strlcat(filename, "/.udev/watch", sizeof(filename));
74 if (rename(filename, oldname) == 0) {
78 dir = opendir(oldname);
80 err(udev, "unable to open old watches dir '%s', old watches will not be restored: %m", oldname);
84 while ((ent = readdir(dir)) != NULL) {
85 char path[UTIL_PATH_SIZE];
86 char buf[UTIL_PATH_SIZE];
88 struct udev_device *dev;
90 if (ent->d_name[0] < '0' || ent->d_name[0] > '9')
93 util_strlcpy(path, oldname, sizeof(path));
94 util_strlcat(path, "/", sizeof(path));
95 util_strlcat(path, ent->d_name, sizeof(path));
97 len = readlink(path, buf, sizeof(buf));
104 dbg(udev, "old watch to '%s' found\n", buf);
105 dev = udev_device_new_from_syspath(udev, buf);
111 info(udev, "restoring old watch on '%s'\n", udev_device_get_devnode(dev));
112 udev_watch_begin(udev, dev);
114 udev_device_unref(dev);
121 } else if (errno != ENOENT) {
122 err(udev, "unable to move watches dir '%s', old watches will not be restored: %m", filename);
126 static const char *udev_watch_filename(struct udev *udev, int wd)
128 static char filename[UTIL_PATH_SIZE];
131 sprintf(str, "%d", wd);
132 util_strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
133 util_strlcat(filename, "/.udev/watch/", sizeof(filename));
134 util_strlcat(filename, str, sizeof(filename));
139 void udev_watch_begin(struct udev *udev, struct udev_device *dev)
141 const char *filename;
147 wd = inotify_add_watch(inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE);
149 err(udev, "inotify_add_watch(%d, %s, %o) failed: %m\n",
150 inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE);
153 filename = udev_watch_filename(udev, wd);
154 util_create_path(udev, filename);
156 symlink(udev_device_get_syspath(dev), filename);
159 void udev_watch_clear(struct udev *udev, struct udev_device *dev)
161 static char filename[UTIL_PATH_SIZE];
165 util_strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
166 util_strlcat(filename, "/.udev/watch", sizeof(filename));
168 dir = opendir(filename);
172 while ((ent = readdir(dir)) != NULL) {
173 char path[UTIL_PATH_SIZE];
174 char buf[UTIL_PATH_SIZE];
177 if (ent->d_name[0] < '0' || ent->d_name[0] > '9')
180 util_strlcpy(path, filename, sizeof(path));
181 util_strlcat(path, "/", sizeof(path));
182 util_strlcat(path, ent->d_name, sizeof(path));
184 len = readlink(path, buf, sizeof(buf));
189 if (strcmp(buf, udev_device_get_syspath(dev)))
192 /* this is the watch we're looking for */
193 info(udev, "clearing existing watch on '%s'\n", udev_device_get_devnode(dev));
194 udev_watch_end(udev, atoi(ent->d_name));
200 void udev_watch_end(struct udev *udev, int wd)
202 const char *filename;
204 if (inotify_fd < 0 || wd < 0)
207 inotify_rm_watch(inotify_fd, wd);
209 filename = udev_watch_filename(udev, wd);
213 const char *udev_watch_lookup(struct udev *udev, int wd)
215 const char *filename;
216 static char buf[UTIL_PATH_SIZE];
219 if (inotify_fd < 0 || wd < 0)
222 filename = udev_watch_filename(udev, wd);
223 len = readlink(filename, buf, sizeof(buf));