]> git.ipfire.org Git - thirdparty/systemd.git/blame - udev/udev-watch.c
udevd: optionally watch device nodes with inotify
[thirdparty/systemd.git] / udev / udev-watch.c
CommitLineData
bd284db1
SJR
1/*
2 * Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
3 * Copyright (C) 2009 Canonical Ltd.
4 *
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.
9 *
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.
14 *
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/>.
17 */
18
19#include <sys/types.h>
20#include <errno.h>
21#include <fcntl.h>
22#include <stdio.h>
23#include <dirent.h>
24#include <stddef.h>
25#include <stdlib.h>
26#include <string.h>
27#include <unistd.h>
28#ifdef HAVE_INOTIFY
29#include <sys/inotify.h>
30#endif
31
32#include "udev.h"
33
34int inotify_fd = -1;
35
36/* inotify descriptor, will be shared with rules directory;
37 * set to cloexec since we need our children to be able to add
38 * watches for us
39 */
40void udev_watch_init(struct udev *udev)
41{
42 inotify_fd = inotify_init();
43 if (inotify_fd >= 0) {
44 int flags;
45
46 flags = fcntl(inotify_fd, F_GETFD);
47 if (flags < 0)
48 flags = FD_CLOEXEC;
49 else
50 flags |= FD_CLOEXEC;
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");
54 else
55 err(udev, "inotify_init failed: %m\n");
56}
57
58/* move any old watches directory out of the way, and then restore
59 * the watches
60 */
61void udev_watch_restore(struct udev *udev)
62{
63 char filename[UTIL_PATH_SIZE], oldname[UTIL_PATH_SIZE];
64
65 if (inotify_fd < 0)
66 return;
67
68 util_strlcpy(oldname, udev_get_dev_path(udev), sizeof(oldname));
69 util_strlcat(oldname, "/.udev/watch.old", sizeof(oldname));
70
71 util_strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
72 util_strlcat(filename, "/.udev/watch", sizeof(filename));
73
74 if (rename(filename, oldname) == 0) {
75 DIR *dir;
76 struct dirent *ent;
77
78 dir = opendir(oldname);
79 if (dir == NULL) {
80 err(udev, "unable to open old watches dir '%s', old watches will not be restored: %m", oldname);
81 return;
82 }
83
84 while ((ent = readdir(dir)) != NULL) {
85 char path[UTIL_PATH_SIZE];
86 char buf[UTIL_PATH_SIZE];
87 ssize_t len;
88 struct udev_device *dev;
89
90 if (ent->d_name[0] < '0' || ent->d_name[0] > '9')
91 continue;
92
93 util_strlcpy(path, oldname, sizeof(path));
94 util_strlcat(path, "/", sizeof(path));
95 util_strlcat(path, ent->d_name, sizeof(path));
96
97 len = readlink(path, buf, sizeof(buf));
98 if (len <= 0) {
99 unlink(path);
100 continue;
101 }
102
103 buf[len] = '\0';
104 dbg(udev, "old watch to '%s' found\n", buf);
105
106 dev = udev_device_new_from_syspath(udev, buf);
107 if (dev == NULL) {
108 unlink(path);
109 continue;
110 }
111
112 udev_device_read_db(dev);
113 udev_device_set_info_loaded(dev);
114
115 info(udev, "restoring old watch on '%s'\n", udev_device_get_devnode(dev));
116 udev_watch_begin(udev, dev);
117
118 udev_device_unref(dev);
119
120 unlink(path);
121 }
122
123 closedir(dir);
124 rmdir(oldname);
125
126 } else if (errno != ENOENT) {
127 err(udev, "unable to move watches dir '%s', old watches will not be restored: %m", filename);
128 }
129}
130
131static const char *udev_watch_filename(struct udev *udev, int wd)
132{
133 static char filename[UTIL_PATH_SIZE];
134 char str[32];
135
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));
140
141 return filename;
142}
143
144void udev_watch_begin(struct udev *udev, struct udev_device *dev)
145{
146 const char *filename;
147 int wd;
148
149 if (inotify_fd < 0)
150 return;
151
152 wd = inotify_add_watch (inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE);
153 if (wd < 0) {
154 err(udev, "inotify_add_watch(%d, %s, %o) failed: %m\n",
155 inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE);
156 }
157
158 filename = udev_watch_filename(udev, wd);
159 util_create_path(udev, filename);
160 unlink(filename);
161 symlink(udev_device_get_syspath(dev), filename);
162}
163
164void udev_watch_clear(struct udev *udev, struct udev_device *dev)
165{
166 static char filename[UTIL_PATH_SIZE];
167 DIR *dir;
168 struct dirent *ent;
169
170 util_strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
171 util_strlcat(filename, "/.udev/watch", sizeof(filename));
172
173 dir = opendir(filename);
174 if (dir == NULL)
175 return;
176
177 while ((ent = readdir(dir)) != NULL) {
178 char path[UTIL_PATH_SIZE];
179 char buf[UTIL_PATH_SIZE];
180 ssize_t len;
181
182 if (ent->d_name[0] < '0' || ent->d_name[0] > '9')
183 continue;
184
185 util_strlcpy(path, filename, sizeof(path));
186 util_strlcat(path, "/", sizeof(path));
187 util_strlcat(path, ent->d_name, sizeof(path));
188
189 len = readlink(path, buf, sizeof(buf));
190 if (len <= 0)
191 continue;
192
193 buf[len] = '\0';
194 if (strcmp(buf, udev_device_get_syspath(dev)))
195 continue;
196
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));
200 }
201
202 closedir(dir);
203}
204
205void udev_watch_end(struct udev *udev, int wd)
206{
207 const char *filename;
208
209 if (inotify_fd < 0 || wd < 0)
210 return;
211
212 inotify_rm_watch(inotify_fd, wd);
213
214 filename = udev_watch_filename(udev, wd);
215 unlink(filename);
216}
217
218const char *udev_watch_lookup(struct udev *udev, int wd)
219{
220 const char *filename;
221 static char buf[UTIL_PATH_SIZE];
222 ssize_t len;
223
224 if (inotify_fd < 0 || wd < 0)
225 return NULL;
226
227 filename = udev_watch_filename(udev, wd);
228 len = readlink(filename, buf, sizeof(buf));
229 if (len > 0) {
230 buf[len] = '\0';
231
232 return buf;
233 }
234
235 return NULL;
236}