chiark / gitweb /
8bc8775735a5d36f408763932327ae9967ed6657
[elogind.git] / udev / udev-watch.c
1 /*
2  * Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
3  * Copyright (C) 2009 Canonical Ltd.
4  * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <sys/types.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <stdio.h>
24 #include <dirent.h>
25 #include <stddef.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #ifdef HAVE_INOTIFY
30 #include <sys/inotify.h>
31 #endif
32
33 #include "udev.h"
34
35 int inotify_fd = -1;
36
37 /* inotify descriptor, will be shared with rules directory;
38  * set to cloexec since we need our children to be able to add
39  * watches for us
40  */
41 void udev_watch_init(struct udev *udev)
42 {
43         inotify_fd = inotify_init();
44         if (inotify_fd >= 0)
45                 util_set_fd_cloexec(inotify_fd);
46         else if (errno == ENOSYS)
47                 info(udev, "unable to use inotify, udevd will not monitor rule files changes\n");
48         else
49                 err(udev, "inotify_init failed: %m\n");
50 }
51
52 /* move any old watches directory out of the way, and then restore
53  * the watches
54  */
55 void udev_watch_restore(struct udev *udev)
56 {
57         char filename[UTIL_PATH_SIZE], oldname[UTIL_PATH_SIZE];
58
59         if (inotify_fd < 0)
60                 return;
61
62         util_strscpyl(oldname, sizeof(oldname), udev_get_dev_path(udev), "/.udev/watch.old", NULL);
63         util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/watch", NULL);
64         if (rename(filename, oldname) == 0) {
65                 DIR *dir;
66                 struct dirent *ent;
67
68                 dir = opendir(oldname);
69                 if (dir == NULL) {
70                         err(udev, "unable to open old watches dir '%s', old watches will not be restored: %m", oldname);
71                         return;
72                 }
73
74                 for (ent = readdir(dir); ent != NULL; ent = readdir(dir)) {
75                         char path[UTIL_PATH_SIZE];
76                         char buf[UTIL_PATH_SIZE];
77                         char *s;
78                         size_t l;
79                         ssize_t len;
80                         struct udev_device *dev;
81
82                         if (ent->d_name[0] < '0' || ent->d_name[0] > '9')
83                                 continue;
84
85                         util_strscpyl(path, sizeof(path), oldname, "/", ent->d_name, NULL);
86                         s = buf;
87                         l = util_strpcpy(&s, sizeof(buf), udev_get_sys_path(udev));
88                         len = readlink(path, s, l);
89                         if (len <= 0 || len >= (ssize_t)l) {
90                                 unlink(path);
91                                 continue;
92                         }
93                         s[len] = '\0';
94                         dbg(udev, "old watch to '%s' found\n", buf);
95                         dev = udev_device_new_from_syspath(udev, buf);
96                         if (dev == NULL) {
97                                 unlink(path);
98                                 continue;
99                         }
100
101                         info(udev, "restoring old watch on '%s'\n", udev_device_get_devnode(dev));
102                         udev_watch_begin(udev, dev);
103
104                         udev_device_unref(dev);
105                         unlink(path);
106                 }
107
108                 closedir(dir);
109                 rmdir(oldname);
110
111         } else if (errno != ENOENT) {
112                 err(udev, "unable to move watches dir '%s', old watches will not be restored: %m", filename);
113         }
114 }
115
116 void udev_watch_begin(struct udev *udev, struct udev_device *dev)
117 {
118         char filename[UTIL_PATH_SIZE];
119         int wd;
120
121         if (inotify_fd < 0)
122                 return;
123
124         info(udev, "adding watch on '%s'\n", udev_device_get_devnode(dev));
125         wd = inotify_add_watch(inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE);
126         if (wd < 0) {
127                 err(udev, "inotify_add_watch(%d, %s, %o) failed: %m\n",
128                     inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE);
129         }
130
131         snprintf(filename, sizeof(filename), "%s/.udev/watch/%d", udev_get_dev_path(udev), wd);
132         util_create_path(udev, filename);
133         unlink(filename);
134         symlink(udev_device_get_devpath(dev), filename);
135
136         udev_device_set_watch_handle(dev, wd);
137 }
138
139 void udev_watch_end(struct udev *udev, struct udev_device *dev)
140 {
141         int wd;
142         char filename[UTIL_PATH_SIZE];
143
144         if (inotify_fd < 0)
145                 return;
146
147         wd = udev_device_get_watch_handle(dev);
148         if (wd < 0)
149                 return;
150
151         info(udev, "removing watch on '%s'\n", udev_device_get_devnode(dev));
152         inotify_rm_watch(inotify_fd, wd);
153
154         snprintf(filename, sizeof(filename), "%s/.udev/watch/%d", udev_get_dev_path(udev), wd);
155         unlink(filename);
156
157         udev_device_set_watch_handle(dev, -1);
158 }
159
160 struct udev_device *udev_watch_lookup(struct udev *udev, int wd)
161 {
162         char filename[UTIL_PATH_SIZE];
163         char syspath[UTIL_PATH_SIZE];
164         char *s;
165         size_t l;
166         ssize_t len;
167
168         if (inotify_fd < 0 || wd < 0)
169                 return NULL;
170
171         snprintf(filename, sizeof(filename), "%s/.udev/watch/%d", udev_get_dev_path(udev), wd);
172         s = syspath;
173         l = util_strpcpy(&s, sizeof(syspath), udev_get_sys_path(udev));
174         len = readlink(filename, s, l);
175         if (len < 0 || (size_t)len >= l)
176                 return NULL;
177         s[len] = '\0';
178         return udev_device_new_from_syspath(udev, syspath);
179 }