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);
106 dev = udev_device_new_from_syspath(udev, buf);
112 udev_device_read_db(dev);
113 udev_device_set_info_loaded(dev);
115 info(udev, "restoring old watch on '%s'\n", udev_device_get_devnode(dev));
116 udev_watch_begin(udev, dev);
118 udev_device_unref(dev);
126 } else if (errno != ENOENT) {
127 err(udev, "unable to move watches dir '%s', old watches will not be restored: %m", filename);
131 static const char *udev_watch_filename(struct udev *udev, int wd)
133 static char filename[UTIL_PATH_SIZE];
136 sprintf(str, "%d", wd);
137 util_strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
138 util_strlcat(filename, "/.udev/watch/", sizeof(filename));
139 util_strlcat(filename, str, sizeof(filename));
144 void udev_watch_begin(struct udev *udev, struct udev_device *dev)
146 const char *filename;
152 wd = inotify_add_watch (inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE);
154 err(udev, "inotify_add_watch(%d, %s, %o) failed: %m\n",
155 inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE);
158 filename = udev_watch_filename(udev, wd);
159 util_create_path(udev, filename);
161 symlink(udev_device_get_syspath(dev), filename);
164 void udev_watch_clear(struct udev *udev, struct udev_device *dev)
166 static char filename[UTIL_PATH_SIZE];
170 util_strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
171 util_strlcat(filename, "/.udev/watch", sizeof(filename));
173 dir = opendir(filename);
177 while ((ent = readdir(dir)) != NULL) {
178 char path[UTIL_PATH_SIZE];
179 char buf[UTIL_PATH_SIZE];
182 if (ent->d_name[0] < '0' || ent->d_name[0] > '9')
185 util_strlcpy(path, filename, sizeof(path));
186 util_strlcat(path, "/", sizeof(path));
187 util_strlcat(path, ent->d_name, sizeof(path));
189 len = readlink(path, buf, sizeof(buf));
194 if (strcmp(buf, udev_device_get_syspath(dev)))
197 /* this is the watch we're looking for */
198 info(udev, "clearing existing watch on '%s'\n", udev_device_get_devnode(dev));
199 udev_watch_end(udev, atoi(ent->d_name));
205 void udev_watch_end(struct udev *udev, int wd)
207 const char *filename;
209 if (inotify_fd < 0 || wd < 0)
212 inotify_rm_watch(inotify_fd, wd);
214 filename = udev_watch_filename(udev, wd);
218 const char *udev_watch_lookup(struct udev *udev, int wd)
220 const char *filename;
221 static char buf[UTIL_PATH_SIZE];
224 if (inotify_fd < 0 || wd < 0)
227 filename = udev_watch_filename(udev, wd);
228 len = readlink(filename, buf, sizeof(buf));