]>
Commit | Line | Data |
---|---|---|
bd284db1 | 1 | /* |
1298001e | 2 | * Copyright (C) 2004-2012 Kay Sievers <kay@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 | { |
912541b0 KS |
41 | inotify_fd = inotify_init1(IN_CLOEXEC); |
42 | if (inotify_fd < 0) | |
baa30fbc | 43 | log_error("inotify_init failed: %m\n"); |
912541b0 | 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 | { | |
912541b0 KS |
52 | if (inotify_fd < 0) |
53 | return; | |
54 | ||
6ada823a | 55 | if (rename("/run/udev/watch", "/run/udev/watch.old") == 0) { |
912541b0 KS |
56 | DIR *dir; |
57 | struct dirent *ent; | |
58 | ||
6ada823a | 59 | dir = opendir("/run/udev/watch.old"); |
912541b0 | 60 | if (dir == NULL) { |
6ada823a | 61 | log_error("unable to open old watches dir /run/udev/watch.old; old watches will not be restored: %m"); |
912541b0 KS |
62 | return; |
63 | } | |
64 | ||
65 | for (ent = readdir(dir); ent != NULL; ent = readdir(dir)) { | |
66 | char device[UTIL_PATH_SIZE]; | |
912541b0 KS |
67 | ssize_t len; |
68 | struct udev_device *dev; | |
69 | ||
70 | if (ent->d_name[0] == '.') | |
71 | continue; | |
72 | ||
6ada823a KS |
73 | len = readlinkat(dirfd(dir), ent->d_name, device, sizeof(device)); |
74 | if (len <= 0 || len == (ssize_t)sizeof(device)) | |
912541b0 | 75 | goto unlink; |
6ada823a | 76 | device[len] = '\0'; |
912541b0 | 77 | |
dbf61afb | 78 | dev = udev_device_new_from_device_id(udev, device); |
912541b0 KS |
79 | if (dev == NULL) |
80 | goto unlink; | |
81 | ||
baa30fbc | 82 | log_debug("restoring old watch on '%s'\n", udev_device_get_devnode(dev)); |
912541b0 KS |
83 | udev_watch_begin(udev, dev); |
84 | udev_device_unref(dev); | |
777239cb | 85 | unlink: |
912541b0 KS |
86 | unlinkat(dirfd(dir), ent->d_name, 0); |
87 | } | |
bd284db1 | 88 | |
912541b0 | 89 | closedir(dir); |
6ada823a | 90 | rmdir("/run/udev/watch.old"); |
bd284db1 | 91 | |
912541b0 | 92 | } else if (errno != ENOENT) { |
6ada823a | 93 | log_error("unable to move watches dir /run/udev/watch; old watches will not be restored: %m"); |
912541b0 | 94 | } |
bd284db1 SJR |
95 | } |
96 | ||
bd284db1 SJR |
97 | void udev_watch_begin(struct udev *udev, struct udev_device *dev) |
98 | { | |
912541b0 KS |
99 | char filename[UTIL_PATH_SIZE]; |
100 | int wd; | |
6bb2f0a0 | 101 | int r; |
912541b0 KS |
102 | |
103 | if (inotify_fd < 0) | |
104 | return; | |
105 | ||
baa30fbc | 106 | log_debug("adding watch on '%s'\n", udev_device_get_devnode(dev)); |
912541b0 KS |
107 | wd = inotify_add_watch(inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE); |
108 | if (wd < 0) { | |
baa30fbc | 109 | log_error("inotify_add_watch(%d, %s, %o) failed: %m\n", |
912541b0 KS |
110 | inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE); |
111 | return; | |
112 | } | |
113 | ||
6ada823a | 114 | snprintf(filename, sizeof(filename), "/run/udev/watch/%d", wd); |
667e3924 | 115 | mkdir_parents(filename, 0755); |
912541b0 | 116 | unlink(filename); |
6bb2f0a0 VP |
117 | r = symlink(udev_device_get_id_filename(dev), filename); |
118 | if (r < 0) | |
119 | log_error("Failed to create symlink: %m"); | |
912541b0 KS |
120 | |
121 | udev_device_set_watch_handle(dev, wd); | |
bd284db1 SJR |
122 | } |
123 | ||
047f88bc | 124 | void udev_watch_end(struct udev *udev, struct udev_device *dev) |
bd284db1 | 125 | { |
912541b0 KS |
126 | int wd; |
127 | char filename[UTIL_PATH_SIZE]; | |
bd284db1 | 128 | |
912541b0 KS |
129 | if (inotify_fd < 0) |
130 | return; | |
03e0170d | 131 | |
912541b0 KS |
132 | wd = udev_device_get_watch_handle(dev); |
133 | if (wd < 0) | |
134 | return; | |
bd284db1 | 135 | |
baa30fbc | 136 | log_debug("removing watch on '%s'\n", udev_device_get_devnode(dev)); |
912541b0 | 137 | inotify_rm_watch(inotify_fd, wd); |
bd284db1 | 138 | |
6ada823a | 139 | snprintf(filename, sizeof(filename), "/run/udev/watch/%d", wd); |
912541b0 | 140 | unlink(filename); |
047f88bc | 141 | |
912541b0 | 142 | udev_device_set_watch_handle(dev, -1); |
bd284db1 SJR |
143 | } |
144 | ||
047f88bc | 145 | struct udev_device *udev_watch_lookup(struct udev *udev, int wd) |
bd284db1 | 146 | { |
912541b0 | 147 | char filename[UTIL_PATH_SIZE]; |
6ada823a | 148 | char device[UTIL_NAME_SIZE]; |
912541b0 KS |
149 | ssize_t len; |
150 | ||
151 | if (inotify_fd < 0 || wd < 0) | |
152 | return NULL; | |
153 | ||
6ada823a KS |
154 | snprintf(filename, sizeof(filename), "/run/udev/watch/%d", wd); |
155 | len = readlink(filename, device, sizeof(device)); | |
156 | if (len <= 0 || (size_t)len == sizeof(device)) | |
912541b0 | 157 | return NULL; |
6ada823a | 158 | device[len] = '\0'; |
912541b0 | 159 | |
dbf61afb | 160 | return udev_device_new_from_device_id(udev, device); |
bd284db1 | 161 | } |