chiark / gitweb /
Cleanup a little.
[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                 int flags;
46
47                 flags = fcntl(inotify_fd, F_GETFD);
48                 if (flags < 0)
49                         flags = FD_CLOEXEC;
50                 else
51                         flags |= FD_CLOEXEC;
52                 fcntl(inotify_fd, F_SETFD, flags);
53         } else if (errno == ENOSYS)
54                 info(udev, "unable to use inotify, udevd will not monitor rule files changes\n");
55         else
56                 err(udev, "inotify_init failed: %m\n");
57 }
58
59 /* move any old watches directory out of the way, and then restore
60  * the watches
61  */
62 void udev_watch_restore(struct udev *udev)
63 {
64         char filename[UTIL_PATH_SIZE], oldname[UTIL_PATH_SIZE];
65
66         if (inotify_fd < 0)
67                 return;
68
69         util_strlcpy(oldname, udev_get_dev_path(udev), sizeof(oldname));
70         util_strlcat(oldname, "/.udev/watch.old", sizeof(oldname));
71
72         util_strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
73         util_strlcat(filename, "/.udev/watch", sizeof(filename));
74
75         if (rename(filename, oldname) == 0) {
76                 DIR *dir;
77                 struct dirent *ent;
78
79                 dir = opendir(oldname);
80                 if (dir == NULL) {
81                         err(udev, "unable to open old watches dir '%s', old watches will not be restored: %m", oldname);
82                         return;
83                 }
84
85                 while ((ent = readdir(dir)) != NULL) {
86                         char path[UTIL_PATH_SIZE];
87                         char buf[UTIL_PATH_SIZE];
88                         ssize_t len;
89                         struct udev_device *dev;
90
91                         if (ent->d_name[0] < '0' || ent->d_name[0] > '9')
92                                 continue;
93
94                         util_strlcpy(path, oldname, sizeof(path));
95                         util_strlcat(path, "/", sizeof(path));
96                         util_strlcat(path, ent->d_name, sizeof(path));
97
98                         len = readlink(path, buf, sizeof(buf));
99                         if (len <= 0) {
100                                 unlink(path);
101                                 continue;
102                         }
103
104                         buf[len] = '\0';
105                         dbg(udev, "old watch to '%s' found\n", buf);
106                         dev = udev_device_new_from_syspath(udev, buf);
107                         if (dev == NULL) {
108                                 unlink(path);
109                                 continue;
110                         }
111
112                         info(udev, "restoring old watch on '%s'\n", udev_device_get_devnode(dev));
113                         udev_watch_begin(udev, dev);
114
115                         udev_device_unref(dev);
116                         unlink(path);
117                 }
118
119                 closedir(dir);
120                 rmdir(oldname);
121
122         } else if (errno != ENOENT) {
123                 err(udev, "unable to move watches dir '%s', old watches will not be restored: %m", filename);
124         }
125 }
126
127 static const char *udev_watch_filename(struct udev *udev, int wd)
128 {
129         static char filename[UTIL_PATH_SIZE];
130         char str[32];
131
132         sprintf(str, "%d", wd);
133         util_strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
134         util_strlcat(filename, "/.udev/watch/", sizeof(filename));
135         util_strlcat(filename, str, sizeof(filename));
136
137         return filename;
138 }
139
140 void udev_watch_begin(struct udev *udev, struct udev_device *dev)
141 {
142         const char *filename;
143         int wd;
144
145         if (inotify_fd < 0 || major(udev_device_get_devnum(dev)) == 0)
146                 return;
147
148         wd = inotify_add_watch(inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE);
149         if (wd < 0) {
150                 err(udev, "inotify_add_watch(%d, %s, %o) failed: %m\n",
151                     inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE);
152         }
153
154         filename = udev_watch_filename(udev, wd);
155         util_create_path(udev, filename);
156         unlink(filename);
157         symlink(udev_device_get_syspath(dev), filename);
158 }
159
160 void udev_watch_clear(struct udev *udev, struct udev_device *dev)
161 {
162         static char filename[UTIL_PATH_SIZE];
163         DIR *dir;
164         struct dirent *ent;
165
166         if (inotify_fd < 0 || major(udev_device_get_devnum(dev)) == 0)
167                 return;
168
169         util_strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
170         util_strlcat(filename, "/.udev/watch", sizeof(filename));
171
172         dir = opendir(filename);
173         if (dir == NULL)
174                 return;
175
176         while ((ent = readdir(dir)) != NULL) {
177                 char path[UTIL_PATH_SIZE];
178                 char buf[UTIL_PATH_SIZE];
179                 ssize_t len;
180
181                 if (ent->d_name[0] < '0' || ent->d_name[0] > '9')
182                         continue;
183
184                 util_strlcpy(path, filename, sizeof(path));
185                 util_strlcat(path, "/", sizeof(path));
186                 util_strlcat(path, ent->d_name, sizeof(path));
187
188                 len = readlink(path, buf, sizeof(buf));
189                 if (len <= 0)
190                         continue;
191
192                 buf[len] = '\0';
193                 if (strcmp(buf, udev_device_get_syspath(dev)))
194                         continue;
195
196                 /* this is the watch we're looking for */
197                 info(udev, "clearing existing watch on '%s'\n", udev_device_get_devnode(dev));
198                 udev_watch_end(udev, atoi(ent->d_name));
199         }
200
201         closedir(dir);
202 }
203
204 void udev_watch_end(struct udev *udev, int wd)
205 {
206         const char *filename;
207
208         if (inotify_fd < 0 || wd < 0)
209                 return;
210
211         inotify_rm_watch(inotify_fd, wd);
212
213         filename = udev_watch_filename(udev, wd);
214         unlink(filename);
215 }
216
217 const char *udev_watch_lookup(struct udev *udev, int wd)
218 {
219         const char *filename;
220         static char buf[UTIL_PATH_SIZE];
221         ssize_t len;
222
223         if (inotify_fd < 0 || wd < 0)
224                 return NULL;
225
226         filename = udev_watch_filename(udev, wd);
227         len = readlink(filename, buf, sizeof(buf));
228         if (len > 0) {
229                 buf[len] = '\0';
230
231                 return buf;
232         }
233
234         return NULL;
235 }