]>
Commit | Line | Data |
---|---|---|
e7145211 | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
bd284db1 | 2 | /* |
1298001e | 3 | * Copyright (C) 2004-2012 Kay Sievers <kay@vrfy.org> |
bd284db1 | 4 | * Copyright (C) 2009 Canonical Ltd. |
64746532 | 5 | * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com> |
bd284db1 SJR |
6 | * |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
cf0fbc49 | 21 | #include <errno.h> |
bd284db1 | 22 | #include <stddef.h> |
cf0fbc49 | 23 | #include <stdio.h> |
bd284db1 | 24 | #include <sys/inotify.h> |
cf0fbc49 | 25 | #include <unistd.h> |
bd284db1 | 26 | |
8fb3f009 | 27 | #include "dirent-util.h" |
d054f0a4 | 28 | #include "stdio-util.h" |
bd284db1 SJR |
29 | #include "udev.h" |
30 | ||
1e03b754 | 31 | static int inotify_fd = -1; |
bd284db1 SJR |
32 | |
33 | /* inotify descriptor, will be shared with rules directory; | |
34 | * set to cloexec since we need our children to be able to add | |
35 | * watches for us | |
36 | */ | |
9ec6e95b | 37 | int udev_watch_init(struct udev *udev) { |
912541b0 KS |
38 | inotify_fd = inotify_init1(IN_CLOEXEC); |
39 | if (inotify_fd < 0) | |
56f64d95 | 40 | log_error_errno(errno, "inotify_init failed: %m"); |
912541b0 | 41 | return inotify_fd; |
bd284db1 SJR |
42 | } |
43 | ||
44 | /* move any old watches directory out of the way, and then restore | |
45 | * the watches | |
46 | */ | |
9ec6e95b | 47 | void udev_watch_restore(struct udev *udev) { |
912541b0 KS |
48 | if (inotify_fd < 0) |
49 | return; | |
50 | ||
6ada823a | 51 | if (rename("/run/udev/watch", "/run/udev/watch.old") == 0) { |
912541b0 KS |
52 | DIR *dir; |
53 | struct dirent *ent; | |
54 | ||
6ada823a | 55 | dir = opendir("/run/udev/watch.old"); |
912541b0 | 56 | if (dir == NULL) { |
56f64d95 | 57 | log_error_errno(errno, "unable to open old watches dir /run/udev/watch.old; old watches will not be restored: %m"); |
912541b0 KS |
58 | return; |
59 | } | |
60 | ||
8fb3f009 | 61 | FOREACH_DIRENT_ALL(ent, dir, break) { |
912541b0 | 62 | char device[UTIL_PATH_SIZE]; |
912541b0 KS |
63 | ssize_t len; |
64 | struct udev_device *dev; | |
65 | ||
66 | if (ent->d_name[0] == '.') | |
67 | continue; | |
68 | ||
6ada823a KS |
69 | len = readlinkat(dirfd(dir), ent->d_name, device, sizeof(device)); |
70 | if (len <= 0 || len == (ssize_t)sizeof(device)) | |
912541b0 | 71 | goto unlink; |
6ada823a | 72 | device[len] = '\0'; |
912541b0 | 73 | |
dbf61afb | 74 | dev = udev_device_new_from_device_id(udev, device); |
912541b0 KS |
75 | if (dev == NULL) |
76 | goto unlink; | |
77 | ||
9f6445e3 | 78 | log_debug("restoring old watch on '%s'", udev_device_get_devnode(dev)); |
912541b0 KS |
79 | udev_watch_begin(udev, dev); |
80 | udev_device_unref(dev); | |
777239cb | 81 | unlink: |
cb51ee7a | 82 | (void) unlinkat(dirfd(dir), ent->d_name, 0); |
912541b0 | 83 | } |
bd284db1 | 84 | |
912541b0 | 85 | closedir(dir); |
6ada823a | 86 | rmdir("/run/udev/watch.old"); |
bd284db1 | 87 | |
1f6b4113 | 88 | } else if (errno != ENOENT) |
56f64d95 | 89 | log_error_errno(errno, "unable to move watches dir /run/udev/watch; old watches will not be restored: %m"); |
bd284db1 SJR |
90 | } |
91 | ||
9ec6e95b | 92 | void udev_watch_begin(struct udev *udev, struct udev_device *dev) { |
e02c6135 | 93 | char filename[sizeof("/run/udev/watch/") + DECIMAL_STR_MAX(int)]; |
912541b0 | 94 | int wd; |
6bb2f0a0 | 95 | int r; |
912541b0 KS |
96 | |
97 | if (inotify_fd < 0) | |
98 | return; | |
99 | ||
9f6445e3 | 100 | log_debug("adding watch on '%s'", udev_device_get_devnode(dev)); |
912541b0 KS |
101 | wd = inotify_add_watch(inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE); |
102 | if (wd < 0) { | |
56f64d95 | 103 | log_error_errno(errno, "inotify_add_watch(%d, %s, %o) failed: %m", |
1f6b4113 | 104 | inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE); |
912541b0 KS |
105 | return; |
106 | } | |
107 | ||
d054f0a4 | 108 | xsprintf(filename, "/run/udev/watch/%d", wd); |
667e3924 | 109 | mkdir_parents(filename, 0755); |
912541b0 | 110 | unlink(filename); |
6bb2f0a0 VP |
111 | r = symlink(udev_device_get_id_filename(dev), filename); |
112 | if (r < 0) | |
56f64d95 | 113 | log_error_errno(errno, "Failed to create symlink %s: %m", filename); |
912541b0 KS |
114 | |
115 | udev_device_set_watch_handle(dev, wd); | |
bd284db1 SJR |
116 | } |
117 | ||
9ec6e95b | 118 | void udev_watch_end(struct udev *udev, struct udev_device *dev) { |
912541b0 | 119 | int wd; |
e02c6135 | 120 | char filename[sizeof("/run/udev/watch/") + DECIMAL_STR_MAX(int)]; |
bd284db1 | 121 | |
912541b0 KS |
122 | if (inotify_fd < 0) |
123 | return; | |
03e0170d | 124 | |
912541b0 KS |
125 | wd = udev_device_get_watch_handle(dev); |
126 | if (wd < 0) | |
127 | return; | |
bd284db1 | 128 | |
9f6445e3 | 129 | log_debug("removing watch on '%s'", udev_device_get_devnode(dev)); |
912541b0 | 130 | inotify_rm_watch(inotify_fd, wd); |
bd284db1 | 131 | |
d054f0a4 | 132 | xsprintf(filename, "/run/udev/watch/%d", wd); |
912541b0 | 133 | unlink(filename); |
047f88bc | 134 | |
912541b0 | 135 | udev_device_set_watch_handle(dev, -1); |
bd284db1 SJR |
136 | } |
137 | ||
9ec6e95b | 138 | struct udev_device *udev_watch_lookup(struct udev *udev, int wd) { |
e02c6135 | 139 | char filename[sizeof("/run/udev/watch/") + DECIMAL_STR_MAX(int)]; |
6ada823a | 140 | char device[UTIL_NAME_SIZE]; |
912541b0 KS |
141 | ssize_t len; |
142 | ||
143 | if (inotify_fd < 0 || wd < 0) | |
144 | return NULL; | |
145 | ||
d054f0a4 | 146 | xsprintf(filename, "/run/udev/watch/%d", wd); |
6ada823a KS |
147 | len = readlink(filename, device, sizeof(device)); |
148 | if (len <= 0 || (size_t)len == sizeof(device)) | |
912541b0 | 149 | return NULL; |
6ada823a | 150 | device[len] = '\0'; |
912541b0 | 151 | |
dbf61afb | 152 | return udev_device_new_from_device_id(udev, device); |
bd284db1 | 153 | } |