]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-watch.c
udev: replace util_create_path() with mkdir_parents()
[thirdparty/systemd.git] / src / udev / udev-watch.c
1 /*
2 * Copyright (C) 2004-2012 Kay Sievers <kay.sievers@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
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>
29 #include <sys/inotify.h>
30
31 #include "udev.h"
32
33 static int inotify_fd = -1;
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 */
39 int udev_watch_init(struct udev *udev)
40 {
41 inotify_fd = inotify_init1(IN_CLOEXEC);
42 if (inotify_fd < 0)
43 log_error("inotify_init failed: %m\n");
44 return inotify_fd;
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 if (inotify_fd < 0)
53 return;
54
55 if (rename("/run/udev/watch", "/run/udev/watch.old") == 0) {
56 DIR *dir;
57 struct dirent *ent;
58
59 dir = opendir("/run/udev/watch.old");
60 if (dir == NULL) {
61 log_error("unable to open old watches dir /run/udev/watch.old; old watches will not be restored: %m");
62 return;
63 }
64
65 for (ent = readdir(dir); ent != NULL; ent = readdir(dir)) {
66 char device[UTIL_PATH_SIZE];
67 ssize_t len;
68 struct udev_device *dev;
69
70 if (ent->d_name[0] == '.')
71 continue;
72
73 len = readlinkat(dirfd(dir), ent->d_name, device, sizeof(device));
74 if (len <= 0 || len == (ssize_t)sizeof(device))
75 goto unlink;
76 device[len] = '\0';
77
78 dev = udev_device_new_from_id_filename(udev, device);
79 if (dev == NULL)
80 goto unlink;
81
82 log_debug("restoring old watch on '%s'\n", udev_device_get_devnode(dev));
83 udev_watch_begin(udev, dev);
84 udev_device_unref(dev);
85 unlink:
86 unlinkat(dirfd(dir), ent->d_name, 0);
87 }
88
89 closedir(dir);
90 rmdir("/run/udev/watch.old");
91
92 } else if (errno != ENOENT) {
93 log_error("unable to move watches dir /run/udev/watch; old watches will not be restored: %m");
94 }
95 }
96
97 void udev_watch_begin(struct udev *udev, struct udev_device *dev)
98 {
99 char filename[UTIL_PATH_SIZE];
100 int wd;
101
102 if (inotify_fd < 0)
103 return;
104
105 log_debug("adding watch on '%s'\n", udev_device_get_devnode(dev));
106 wd = inotify_add_watch(inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE);
107 if (wd < 0) {
108 log_error("inotify_add_watch(%d, %s, %o) failed: %m\n",
109 inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE);
110 return;
111 }
112
113 snprintf(filename, sizeof(filename), "/run/udev/watch/%d", wd);
114 mkdir_parents(filename, 0755);
115 unlink(filename);
116 symlink(udev_device_get_id_filename(dev), filename);
117
118 udev_device_set_watch_handle(dev, wd);
119 }
120
121 void udev_watch_end(struct udev *udev, struct udev_device *dev)
122 {
123 int wd;
124 char filename[UTIL_PATH_SIZE];
125
126 if (inotify_fd < 0)
127 return;
128
129 wd = udev_device_get_watch_handle(dev);
130 if (wd < 0)
131 return;
132
133 log_debug("removing watch on '%s'\n", udev_device_get_devnode(dev));
134 inotify_rm_watch(inotify_fd, wd);
135
136 snprintf(filename, sizeof(filename), "/run/udev/watch/%d", wd);
137 unlink(filename);
138
139 udev_device_set_watch_handle(dev, -1);
140 }
141
142 struct udev_device *udev_watch_lookup(struct udev *udev, int wd)
143 {
144 char filename[UTIL_PATH_SIZE];
145 char device[UTIL_NAME_SIZE];
146 ssize_t len;
147
148 if (inotify_fd < 0 || wd < 0)
149 return NULL;
150
151 snprintf(filename, sizeof(filename), "/run/udev/watch/%d", wd);
152 len = readlink(filename, device, sizeof(device));
153 if (len <= 0 || (size_t)len == sizeof(device))
154 return NULL;
155 device[len] = '\0';
156
157 return udev_device_new_from_id_filename(udev, device);
158 }