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