]>
Commit | Line | Data |
---|---|---|
bd284db1 | 1 | /* |
777239cb | 2 | * Copyright (C) 2004-2010 Kay Sievers <kay.sievers@vrfy.org> |
bd284db1 | 3 | * Copyright (C) 2009 Canonical Ltd. |
64746532 | 4 | * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com> |
bd284db1 SJR |
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> | |
bd284db1 | 29 | #include <sys/inotify.h> |
bd284db1 SJR |
30 | |
31 | #include "udev.h" | |
32 | ||
1e03b754 | 33 | static int inotify_fd = -1; |
bd284db1 SJR |
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 | */ | |
1e03b754 | 39 | int udev_watch_init(struct udev *udev) |
bd284db1 | 40 | { |
26347a4c KS |
41 | inotify_fd = inotify_init1(IN_CLOEXEC); |
42 | if (inotify_fd < 0) | |
bd284db1 | 43 | err(udev, "inotify_init failed: %m\n"); |
1e03b754 | 44 | return inotify_fd; |
bd284db1 SJR |
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 | ||
4ec9c3e7 KS |
57 | util_strscpyl(oldname, sizeof(oldname), udev_get_run_path(udev), "/watch.old", NULL); |
58 | util_strscpyl(filename, sizeof(filename), udev_get_run_path(udev), "/watch", NULL); | |
bd284db1 SJR |
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 | ||
065db052 | 69 | for (ent = readdir(dir); ent != NULL; ent = readdir(dir)) { |
e6c1a2bd | 70 | char device[UTIL_PATH_SIZE]; |
065db052 KS |
71 | char *s; |
72 | size_t l; | |
bd284db1 SJR |
73 | ssize_t len; |
74 | struct udev_device *dev; | |
75 | ||
777239cb | 76 | if (ent->d_name[0] == '.') |
bd284db1 SJR |
77 | continue; |
78 | ||
e6c1a2bd KS |
79 | s = device; |
80 | l = util_strpcpy(&s, sizeof(device), udev_get_sys_path(udev)); | |
81 | len = readlinkat(dirfd(dir), ent->d_name, s, l); | |
60067cc7 | 82 | if (len <= 0 || len == (ssize_t)l) |
777239cb | 83 | goto unlink; |
065db052 | 84 | s[len] = '\0'; |
777239cb | 85 | |
cad40a5f | 86 | dev = udev_device_new_from_id_filename(udev, s); |
777239cb KS |
87 | if (dev == NULL) |
88 | goto unlink; | |
bd284db1 | 89 | |
bd284db1 SJR |
90 | info(udev, "restoring old watch on '%s'\n", udev_device_get_devnode(dev)); |
91 | udev_watch_begin(udev, dev); | |
bd284db1 | 92 | udev_device_unref(dev); |
777239cb | 93 | unlink: |
e6c1a2bd | 94 | unlinkat(dirfd(dir), ent->d_name, 0); |
bd284db1 SJR |
95 | } |
96 | ||
97 | closedir(dir); | |
98 | rmdir(oldname); | |
99 | ||
100 | } else if (errno != ENOENT) { | |
101 | err(udev, "unable to move watches dir '%s', old watches will not be restored: %m", filename); | |
102 | } | |
103 | } | |
104 | ||
bd284db1 SJR |
105 | void udev_watch_begin(struct udev *udev, struct udev_device *dev) |
106 | { | |
d5d04d4f | 107 | char filename[UTIL_PATH_SIZE]; |
bd284db1 SJR |
108 | int wd; |
109 | ||
3d3a0a70 | 110 | if (inotify_fd < 0) |
bd284db1 SJR |
111 | return; |
112 | ||
9ce46272 | 113 | info(udev, "adding watch on '%s'\n", udev_device_get_devnode(dev)); |
4aca304e | 114 | wd = inotify_add_watch(inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE); |
bd284db1 SJR |
115 | if (wd < 0) { |
116 | err(udev, "inotify_add_watch(%d, %s, %o) failed: %m\n", | |
117 | inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE); | |
ebc1ba78 | 118 | return; |
bd284db1 SJR |
119 | } |
120 | ||
4ec9c3e7 | 121 | snprintf(filename, sizeof(filename), "%s/watch/%d", udev_get_run_path(udev), wd); |
bd284db1 SJR |
122 | util_create_path(udev, filename); |
123 | unlink(filename); | |
4281da1f | 124 | symlink(udev_device_get_id_filename(dev), filename); |
25bdd197 SJR |
125 | |
126 | udev_device_set_watch_handle(dev, wd); | |
bd284db1 SJR |
127 | } |
128 | ||
047f88bc | 129 | void udev_watch_end(struct udev *udev, struct udev_device *dev) |
bd284db1 | 130 | { |
047f88bc | 131 | int wd; |
d5d04d4f | 132 | char filename[UTIL_PATH_SIZE]; |
bd284db1 | 133 | |
3d3a0a70 | 134 | if (inotify_fd < 0) |
03e0170d SJR |
135 | return; |
136 | ||
047f88bc SJR |
137 | wd = udev_device_get_watch_handle(dev); |
138 | if (wd < 0) | |
bd284db1 SJR |
139 | return; |
140 | ||
047f88bc | 141 | info(udev, "removing watch on '%s'\n", udev_device_get_devnode(dev)); |
bd284db1 SJR |
142 | inotify_rm_watch(inotify_fd, wd); |
143 | ||
4ec9c3e7 | 144 | snprintf(filename, sizeof(filename), "%s/watch/%d", udev_get_run_path(udev), wd); |
bd284db1 | 145 | unlink(filename); |
047f88bc SJR |
146 | |
147 | udev_device_set_watch_handle(dev, -1); | |
bd284db1 SJR |
148 | } |
149 | ||
047f88bc | 150 | struct udev_device *udev_watch_lookup(struct udev *udev, int wd) |
bd284db1 | 151 | { |
d5d04d4f | 152 | char filename[UTIL_PATH_SIZE]; |
777239cb | 153 | char majmin[UTIL_PATH_SIZE]; |
065db052 KS |
154 | char *s; |
155 | size_t l; | |
bd284db1 SJR |
156 | ssize_t len; |
157 | ||
158 | if (inotify_fd < 0 || wd < 0) | |
159 | return NULL; | |
160 | ||
4ec9c3e7 | 161 | snprintf(filename, sizeof(filename), "%s/watch/%d", udev_get_run_path(udev), wd); |
777239cb KS |
162 | s = majmin; |
163 | l = util_strpcpy(&s, sizeof(majmin), udev_get_sys_path(udev)); | |
065db052 | 164 | len = readlink(filename, s, l); |
60067cc7 | 165 | if (len <= 0 || (size_t)len == l) |
065db052 KS |
166 | return NULL; |
167 | s[len] = '\0'; | |
777239cb | 168 | |
cad40a5f | 169 | return udev_device_new_from_id_filename(udev, s); |
bd284db1 | 170 | } |