chiark / gitweb /
udevd: use dev_t or netif ifindex as database key
[elogind.git] / udev / udev-watch.c
1 /*
2  * Copyright (C) 2004-2010 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 #include <sys/inotify.h>
30
31 #include "udev.h"
32
33 static int inotify_fd = -1;
34
35 /* inotify descriptor, will be shared with rules directory;
36  * set to cloexec since we need our children to be able to add
37  * watches for us
38  */
39 int udev_watch_init(struct udev *udev)
40 {
41         inotify_fd = inotify_init1(IN_CLOEXEC);
42         if (inotify_fd < 0)
43                 err(udev, "inotify_init failed: %m\n");
44         return inotify_fd;
45 }
46
47 /* move any old watches directory out of the way, and then restore
48  * the watches
49  */
50 void udev_watch_restore(struct udev *udev)
51 {
52         char filename[UTIL_PATH_SIZE], oldname[UTIL_PATH_SIZE];
53
54         if (inotify_fd < 0)
55                 return;
56
57         util_strscpyl(oldname, sizeof(oldname), udev_get_dev_path(udev), "/.udev/watch.old", NULL);
58         util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/watch", NULL);
59         if (rename(filename, oldname) == 0) {
60                 DIR *dir;
61                 struct dirent *ent;
62
63                 dir = opendir(oldname);
64                 if (dir == NULL) {
65                         err(udev, "unable to open old watches dir '%s', old watches will not be restored: %m", oldname);
66                         return;
67                 }
68
69                 for (ent = readdir(dir); ent != NULL; ent = readdir(dir)) {
70                         char device[UTIL_PATH_SIZE];
71                         char *s;
72                         size_t l;
73                         ssize_t len;
74                         struct udev_device *dev;
75                         int maj, min;
76                         char type;
77
78                         if (ent->d_name[0] == '.')
79                                 continue;
80
81                         s = device;
82                         l = util_strpcpy(&s, sizeof(device), udev_get_sys_path(udev));
83                         len = readlinkat(dirfd(dir), ent->d_name, s, l);
84                         if (len <= 0 || len == (ssize_t)l)
85                                 goto unlink;
86                         s[len] = '\0';
87
88                         if (sscanf(s, "%c%i:%i", &type, &maj, &min) != 3)
89                                 goto unlink;
90                         dev = udev_device_new_from_devnum(udev, type, makedev(maj, min));
91                         if (dev == NULL)
92                                 goto unlink;
93
94                         info(udev, "restoring old watch on '%s'\n", udev_device_get_devnode(dev));
95                         udev_watch_begin(udev, dev);
96                         udev_device_unref(dev);
97 unlink:
98                         unlinkat(dirfd(dir), ent->d_name, 0);
99                 }
100
101                 closedir(dir);
102                 rmdir(oldname);
103
104         } else if (errno != ENOENT) {
105                 err(udev, "unable to move watches dir '%s', old watches will not be restored: %m", filename);
106         }
107 }
108
109 void udev_watch_begin(struct udev *udev, struct udev_device *dev)
110 {
111         char filename[UTIL_PATH_SIZE];
112         int wd;
113
114         if (inotify_fd < 0)
115                 return;
116
117         info(udev, "adding watch on '%s'\n", udev_device_get_devnode(dev));
118         wd = inotify_add_watch(inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE);
119         if (wd < 0) {
120                 err(udev, "inotify_add_watch(%d, %s, %o) failed: %m\n",
121                     inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE);
122                 return;
123         }
124
125         snprintf(filename, sizeof(filename), "%s/.udev/watch/%d", udev_get_dev_path(udev), wd);
126         util_create_path(udev, filename);
127         unlink(filename);
128         symlink(udev_device_get_id_filename(dev), filename);
129
130         udev_device_set_watch_handle(dev, wd);
131 }
132
133 void udev_watch_end(struct udev *udev, struct udev_device *dev)
134 {
135         int wd;
136         char filename[UTIL_PATH_SIZE];
137
138         if (inotify_fd < 0)
139                 return;
140
141         wd = udev_device_get_watch_handle(dev);
142         if (wd < 0)
143                 return;
144
145         info(udev, "removing watch on '%s'\n", udev_device_get_devnode(dev));
146         inotify_rm_watch(inotify_fd, wd);
147
148         snprintf(filename, sizeof(filename), "%s/.udev/watch/%d", udev_get_dev_path(udev), wd);
149         unlink(filename);
150
151         udev_device_set_watch_handle(dev, -1);
152 }
153
154 struct udev_device *udev_watch_lookup(struct udev *udev, int wd)
155 {
156         char filename[UTIL_PATH_SIZE];
157         char majmin[UTIL_PATH_SIZE];
158         char *s;
159         size_t l;
160         ssize_t len;
161         int maj, min;
162         char type;
163         dev_t devnum;
164
165         if (inotify_fd < 0 || wd < 0)
166                 return NULL;
167
168         snprintf(filename, sizeof(filename), "%s/.udev/watch/%d", udev_get_dev_path(udev), wd);
169         s = majmin;
170         l = util_strpcpy(&s, sizeof(majmin), udev_get_sys_path(udev));
171         len = readlink(filename, s, l);
172         if (len <= 0 || (size_t)len == l)
173                 return NULL;
174         s[len] = '\0';
175
176         if (sscanf(s, "%c%i:%i", &type, &maj, &min) != 3)
177                 return NULL;
178         devnum = makedev(maj, min);
179         return udev_device_new_from_devnum(udev, type, devnum);
180 }