]> git.ipfire.org Git - thirdparty/systemd.git/blame - udev/udev-watch.c
use global "reload_config" flag
[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);
bd284db1
SJR
105 dev = udev_device_new_from_syspath(udev, buf);
106 if (dev == NULL) {
107 unlink(path);
108 continue;
109 }
110
bd284db1
SJR
111 info(udev, "restoring old watch on '%s'\n", udev_device_get_devnode(dev));
112 udev_watch_begin(udev, dev);
113
114 udev_device_unref(dev);
bd284db1
SJR
115 unlink(path);
116 }
117
118 closedir(dir);
119 rmdir(oldname);
120
121 } else if (errno != ENOENT) {
122 err(udev, "unable to move watches dir '%s', old watches will not be restored: %m", filename);
123 }
124}
125
126static const char *udev_watch_filename(struct udev *udev, int wd)
127{
128 static char filename[UTIL_PATH_SIZE];
129 char str[32];
130
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));
135
136 return filename;
137}
138
139void udev_watch_begin(struct udev *udev, struct udev_device *dev)
140{
141 const char *filename;
142 int wd;
143
144 if (inotify_fd < 0)
145 return;
146
4aca304e 147 wd = inotify_add_watch(inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE);
bd284db1
SJR
148 if (wd < 0) {
149 err(udev, "inotify_add_watch(%d, %s, %o) failed: %m\n",
150 inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE);
151 }
152
153 filename = udev_watch_filename(udev, wd);
154 util_create_path(udev, filename);
155 unlink(filename);
156 symlink(udev_device_get_syspath(dev), filename);
157}
158
159void udev_watch_clear(struct udev *udev, struct udev_device *dev)
160{
161 static char filename[UTIL_PATH_SIZE];
162 DIR *dir;
163 struct dirent *ent;
164
165 util_strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
166 util_strlcat(filename, "/.udev/watch", sizeof(filename));
167
168 dir = opendir(filename);
169 if (dir == NULL)
170 return;
171
172 while ((ent = readdir(dir)) != NULL) {
173 char path[UTIL_PATH_SIZE];
174 char buf[UTIL_PATH_SIZE];
175 ssize_t len;
176
177 if (ent->d_name[0] < '0' || ent->d_name[0] > '9')
178 continue;
179
180 util_strlcpy(path, filename, sizeof(path));
181 util_strlcat(path, "/", sizeof(path));
182 util_strlcat(path, ent->d_name, sizeof(path));
183
184 len = readlink(path, buf, sizeof(buf));
185 if (len <= 0)
186 continue;
187
188 buf[len] = '\0';
189 if (strcmp(buf, udev_device_get_syspath(dev)))
190 continue;
191
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));
195 }
196
197 closedir(dir);
198}
199
200void udev_watch_end(struct udev *udev, int wd)
201{
202 const char *filename;
203
204 if (inotify_fd < 0 || wd < 0)
205 return;
206
207 inotify_rm_watch(inotify_fd, wd);
208
209 filename = udev_watch_filename(udev, wd);
210 unlink(filename);
211}
212
213const char *udev_watch_lookup(struct udev *udev, int wd)
214{
215 const char *filename;
216 static char buf[UTIL_PATH_SIZE];
217 ssize_t len;
218
219 if (inotify_fd < 0 || wd < 0)
220 return NULL;
221
222 filename = udev_watch_filename(udev, wd);
223 len = readlink(filename, buf, sizeof(buf));
224 if (len > 0) {
225 buf[len] = '\0';
226
227 return buf;
228 }
229
230 return NULL;
231}