]>
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 | ||
bd284db1 | 20 | #include <errno.h> |
bd284db1 SJR |
21 | #include <stdio.h> |
22 | #include <dirent.h> | |
23 | #include <stddef.h> | |
bd284db1 | 24 | #include <unistd.h> |
bd284db1 | 25 | #include <sys/inotify.h> |
bd284db1 SJR |
26 | |
27 | #include "udev.h" | |
28 | ||
1e03b754 | 29 | static int inotify_fd = -1; |
bd284db1 SJR |
30 | |
31 | /* inotify descriptor, will be shared with rules directory; | |
32 | * set to cloexec since we need our children to be able to add | |
33 | * watches for us | |
34 | */ | |
9ec6e95b | 35 | int udev_watch_init(struct udev *udev) { |
912541b0 KS |
36 | inotify_fd = inotify_init1(IN_CLOEXEC); |
37 | if (inotify_fd < 0) | |
56f64d95 | 38 | log_error_errno(errno, "inotify_init failed: %m"); |
912541b0 | 39 | return inotify_fd; |
bd284db1 SJR |
40 | } |
41 | ||
42 | /* move any old watches directory out of the way, and then restore | |
43 | * the watches | |
44 | */ | |
9ec6e95b | 45 | void udev_watch_restore(struct udev *udev) { |
912541b0 KS |
46 | if (inotify_fd < 0) |
47 | return; | |
48 | ||
6ada823a | 49 | if (rename("/run/udev/watch", "/run/udev/watch.old") == 0) { |
912541b0 KS |
50 | DIR *dir; |
51 | struct dirent *ent; | |
52 | ||
6ada823a | 53 | dir = opendir("/run/udev/watch.old"); |
912541b0 | 54 | if (dir == NULL) { |
56f64d95 | 55 | log_error_errno(errno, "unable to open old watches dir /run/udev/watch.old; old watches will not be restored: %m"); |
912541b0 KS |
56 | return; |
57 | } | |
58 | ||
59 | for (ent = readdir(dir); ent != NULL; ent = readdir(dir)) { | |
60 | char device[UTIL_PATH_SIZE]; | |
912541b0 KS |
61 | ssize_t len; |
62 | struct udev_device *dev; | |
63 | ||
64 | if (ent->d_name[0] == '.') | |
65 | continue; | |
66 | ||
6ada823a KS |
67 | len = readlinkat(dirfd(dir), ent->d_name, device, sizeof(device)); |
68 | if (len <= 0 || len == (ssize_t)sizeof(device)) | |
912541b0 | 69 | goto unlink; |
6ada823a | 70 | device[len] = '\0'; |
912541b0 | 71 | |
dbf61afb | 72 | dev = udev_device_new_from_device_id(udev, device); |
912541b0 KS |
73 | if (dev == NULL) |
74 | goto unlink; | |
75 | ||
9f6445e3 | 76 | log_debug("restoring old watch on '%s'", udev_device_get_devnode(dev)); |
912541b0 KS |
77 | udev_watch_begin(udev, dev); |
78 | udev_device_unref(dev); | |
777239cb | 79 | unlink: |
912541b0 KS |
80 | unlinkat(dirfd(dir), ent->d_name, 0); |
81 | } | |
bd284db1 | 82 | |
912541b0 | 83 | closedir(dir); |
6ada823a | 84 | rmdir("/run/udev/watch.old"); |
bd284db1 | 85 | |
1f6b4113 | 86 | } else if (errno != ENOENT) |
56f64d95 | 87 | log_error_errno(errno, "unable to move watches dir /run/udev/watch; old watches will not be restored: %m"); |
bd284db1 SJR |
88 | } |
89 | ||
9ec6e95b | 90 | void udev_watch_begin(struct udev *udev, struct udev_device *dev) { |
912541b0 KS |
91 | char filename[UTIL_PATH_SIZE]; |
92 | int wd; | |
6bb2f0a0 | 93 | int r; |
912541b0 KS |
94 | |
95 | if (inotify_fd < 0) | |
96 | return; | |
97 | ||
9f6445e3 | 98 | log_debug("adding watch on '%s'", udev_device_get_devnode(dev)); |
912541b0 KS |
99 | wd = inotify_add_watch(inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE); |
100 | if (wd < 0) { | |
56f64d95 | 101 | log_error_errno(errno, "inotify_add_watch(%d, %s, %o) failed: %m", |
1f6b4113 | 102 | inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE); |
912541b0 KS |
103 | return; |
104 | } | |
105 | ||
6ada823a | 106 | snprintf(filename, sizeof(filename), "/run/udev/watch/%d", wd); |
667e3924 | 107 | mkdir_parents(filename, 0755); |
912541b0 | 108 | unlink(filename); |
6bb2f0a0 VP |
109 | r = symlink(udev_device_get_id_filename(dev), filename); |
110 | if (r < 0) | |
56f64d95 | 111 | log_error_errno(errno, "Failed to create symlink %s: %m", filename); |
912541b0 KS |
112 | |
113 | udev_device_set_watch_handle(dev, wd); | |
bd284db1 SJR |
114 | } |
115 | ||
9ec6e95b | 116 | void udev_watch_end(struct udev *udev, struct udev_device *dev) { |
912541b0 KS |
117 | int wd; |
118 | char filename[UTIL_PATH_SIZE]; | |
bd284db1 | 119 | |
912541b0 KS |
120 | if (inotify_fd < 0) |
121 | return; | |
03e0170d | 122 | |
912541b0 KS |
123 | wd = udev_device_get_watch_handle(dev); |
124 | if (wd < 0) | |
125 | return; | |
bd284db1 | 126 | |
9f6445e3 | 127 | log_debug("removing watch on '%s'", udev_device_get_devnode(dev)); |
912541b0 | 128 | inotify_rm_watch(inotify_fd, wd); |
bd284db1 | 129 | |
6ada823a | 130 | snprintf(filename, sizeof(filename), "/run/udev/watch/%d", wd); |
912541b0 | 131 | unlink(filename); |
047f88bc | 132 | |
912541b0 | 133 | udev_device_set_watch_handle(dev, -1); |
bd284db1 SJR |
134 | } |
135 | ||
9ec6e95b | 136 | struct udev_device *udev_watch_lookup(struct udev *udev, int wd) { |
912541b0 | 137 | char filename[UTIL_PATH_SIZE]; |
6ada823a | 138 | char device[UTIL_NAME_SIZE]; |
912541b0 KS |
139 | ssize_t len; |
140 | ||
141 | if (inotify_fd < 0 || wd < 0) | |
142 | return NULL; | |
143 | ||
6ada823a KS |
144 | snprintf(filename, sizeof(filename), "/run/udev/watch/%d", wd); |
145 | len = readlink(filename, device, sizeof(device)); | |
146 | if (len <= 0 || (size_t)len == sizeof(device)) | |
912541b0 | 147 | return NULL; |
6ada823a | 148 | device[len] = '\0'; |
912541b0 | 149 | |
dbf61afb | 150 | return udev_device_new_from_device_id(udev, device); |
bd284db1 | 151 | } |