]> git.proxmox.com Git - systemd.git/blob - src/udev/udev-node.c
Imported Upstream version 220
[systemd.git] / src / udev / udev-node.c
1 /*
2 * Copyright (C) 2003-2013 Kay Sievers <kay@vrfy.org>
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <string.h>
19 #include <stdio.h>
20 #include <stddef.h>
21 #include <stdbool.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <dirent.h>
26 #include <sys/stat.h>
27
28 #include "udev.h"
29 #include "smack-util.h"
30 #include "selinux-util.h"
31 #include "formats-util.h"
32
33 static int node_symlink(struct udev_device *dev, const char *node, const char *slink) {
34 struct stat stats;
35 char target[UTIL_PATH_SIZE];
36 char *s;
37 size_t l;
38 char slink_tmp[UTIL_PATH_SIZE + 32];
39 int i = 0;
40 int tail = 0;
41 int err = 0;
42
43 /* use relative link */
44 target[0] = '\0';
45 while (node[i] && (node[i] == slink[i])) {
46 if (node[i] == '/')
47 tail = i+1;
48 i++;
49 }
50 s = target;
51 l = sizeof(target);
52 while (slink[i] != '\0') {
53 if (slink[i] == '/')
54 l = strpcpy(&s, l, "../");
55 i++;
56 }
57 l = strscpy(s, l, &node[tail]);
58 if (l == 0) {
59 err = -EINVAL;
60 goto exit;
61 }
62
63 /* preserve link with correct target, do not replace node of other device */
64 if (lstat(slink, &stats) == 0) {
65 if (S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode)) {
66 log_error("conflicting device node '%s' found, link to '%s' will not be created", slink, node);
67 goto exit;
68 } else if (S_ISLNK(stats.st_mode)) {
69 char buf[UTIL_PATH_SIZE];
70 int len;
71
72 len = readlink(slink, buf, sizeof(buf));
73 if (len > 0 && len < (int)sizeof(buf)) {
74 buf[len] = '\0';
75 if (streq(target, buf)) {
76 log_debug("preserve already existing symlink '%s' to '%s'", slink, target);
77 label_fix(slink, true, false);
78 utimensat(AT_FDCWD, slink, NULL, AT_SYMLINK_NOFOLLOW);
79 goto exit;
80 }
81 }
82 }
83 } else {
84 log_debug("creating symlink '%s' to '%s'", slink, target);
85 do {
86 err = mkdir_parents_label(slink, 0755);
87 if (err != 0 && err != -ENOENT)
88 break;
89 mac_selinux_create_file_prepare(slink, S_IFLNK);
90 err = symlink(target, slink);
91 if (err != 0)
92 err = -errno;
93 mac_selinux_create_file_clear();
94 } while (err == -ENOENT);
95 if (err == 0)
96 goto exit;
97 }
98
99 log_debug("atomically replace '%s'", slink);
100 strscpyl(slink_tmp, sizeof(slink_tmp), slink, ".tmp-", udev_device_get_id_filename(dev), NULL);
101 unlink(slink_tmp);
102 do {
103 err = mkdir_parents_label(slink_tmp, 0755);
104 if (err != 0 && err != -ENOENT)
105 break;
106 mac_selinux_create_file_prepare(slink_tmp, S_IFLNK);
107 err = symlink(target, slink_tmp);
108 if (err != 0)
109 err = -errno;
110 mac_selinux_create_file_clear();
111 } while (err == -ENOENT);
112 if (err != 0) {
113 log_error_errno(errno, "symlink '%s' '%s' failed: %m", target, slink_tmp);
114 goto exit;
115 }
116 err = rename(slink_tmp, slink);
117 if (err != 0) {
118 log_error_errno(errno, "rename '%s' '%s' failed: %m", slink_tmp, slink);
119 unlink(slink_tmp);
120 }
121 exit:
122 return err;
123 }
124
125 /* find device node of device with highest priority */
126 static const char *link_find_prioritized(struct udev_device *dev, bool add, const char *stackdir, char *buf, size_t bufsize) {
127 struct udev *udev = udev_device_get_udev(dev);
128 DIR *dir;
129 int priority = 0;
130 const char *target = NULL;
131
132 if (add) {
133 priority = udev_device_get_devlink_priority(dev);
134 strscpy(buf, bufsize, udev_device_get_devnode(dev));
135 target = buf;
136 }
137
138 dir = opendir(stackdir);
139 if (dir == NULL)
140 return target;
141 for (;;) {
142 struct udev_device *dev_db;
143 struct dirent *dent;
144
145 dent = readdir(dir);
146 if (dent == NULL || dent->d_name[0] == '\0')
147 break;
148 if (dent->d_name[0] == '.')
149 continue;
150
151 log_debug("found '%s' claiming '%s'", dent->d_name, stackdir);
152
153 /* did we find ourself? */
154 if (streq(dent->d_name, udev_device_get_id_filename(dev)))
155 continue;
156
157 dev_db = udev_device_new_from_device_id(udev, dent->d_name);
158 if (dev_db != NULL) {
159 const char *devnode;
160
161 devnode = udev_device_get_devnode(dev_db);
162 if (devnode != NULL) {
163 if (target == NULL || udev_device_get_devlink_priority(dev_db) > priority) {
164 log_debug("'%s' claims priority %i for '%s'",
165 udev_device_get_syspath(dev_db), udev_device_get_devlink_priority(dev_db), stackdir);
166 priority = udev_device_get_devlink_priority(dev_db);
167 strscpy(buf, bufsize, devnode);
168 target = buf;
169 }
170 }
171 udev_device_unref(dev_db);
172 }
173 }
174 closedir(dir);
175 return target;
176 }
177
178 /* manage "stack of names" with possibly specified device priorities */
179 static void link_update(struct udev_device *dev, const char *slink, bool add) {
180 char name_enc[UTIL_PATH_SIZE];
181 char filename[UTIL_PATH_SIZE * 2];
182 char dirname[UTIL_PATH_SIZE];
183 const char *target;
184 char buf[UTIL_PATH_SIZE];
185
186 util_path_encode(slink + strlen("/dev"), name_enc, sizeof(name_enc));
187 strscpyl(dirname, sizeof(dirname), "/run/udev/links/", name_enc, NULL);
188 strscpyl(filename, sizeof(filename), dirname, "/", udev_device_get_id_filename(dev), NULL);
189
190 if (!add && unlink(filename) == 0)
191 rmdir(dirname);
192
193 target = link_find_prioritized(dev, add, dirname, buf, sizeof(buf));
194 if (target == NULL) {
195 log_debug("no reference left, remove '%s'", slink);
196 if (unlink(slink) == 0)
197 rmdir_parents(slink, "/");
198 } else {
199 log_debug("creating link '%s' to '%s'", slink, target);
200 node_symlink(dev, target, slink);
201 }
202
203 if (add) {
204 int err;
205
206 do {
207 int fd;
208
209 err = mkdir_parents(filename, 0755);
210 if (err != 0 && err != -ENOENT)
211 break;
212 fd = open(filename, O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444);
213 if (fd >= 0)
214 close(fd);
215 else
216 err = -errno;
217 } while (err == -ENOENT);
218 }
219 }
220
221 void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old) {
222 struct udev_list_entry *list_entry;
223
224 /* update possible left-over symlinks */
225 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev_old)) {
226 const char *name = udev_list_entry_get_name(list_entry);
227 struct udev_list_entry *list_entry_current;
228 int found;
229
230 /* check if old link name still belongs to this device */
231 found = 0;
232 udev_list_entry_foreach(list_entry_current, udev_device_get_devlinks_list_entry(dev)) {
233 const char *name_current = udev_list_entry_get_name(list_entry_current);
234
235 if (streq(name, name_current)) {
236 found = 1;
237 break;
238 }
239 }
240 if (found)
241 continue;
242
243 log_debug("update old name, '%s' no longer belonging to '%s'",
244 name, udev_device_get_devpath(dev));
245 link_update(dev, name, false);
246 }
247 }
248
249 static int node_permissions_apply(struct udev_device *dev, bool apply,
250 mode_t mode, uid_t uid, gid_t gid,
251 struct udev_list *seclabel_list) {
252 const char *devnode = udev_device_get_devnode(dev);
253 dev_t devnum = udev_device_get_devnum(dev);
254 struct stat stats;
255 struct udev_list_entry *entry;
256 int err = 0;
257
258 if (streq(udev_device_get_subsystem(dev), "block"))
259 mode |= S_IFBLK;
260 else
261 mode |= S_IFCHR;
262
263 if (lstat(devnode, &stats) != 0) {
264 err = -errno;
265 log_debug_errno(errno, "can not stat() node '%s' (%m)", devnode);
266 goto out;
267 }
268
269 if (((stats.st_mode & S_IFMT) != (mode & S_IFMT)) || (stats.st_rdev != devnum)) {
270 err = -EEXIST;
271 log_debug("found node '%s' with non-matching devnum %s, skip handling",
272 udev_device_get_devnode(dev), udev_device_get_id_filename(dev));
273 goto out;
274 }
275
276 if (apply) {
277 bool selinux = false;
278 bool smack = false;
279
280 if ((stats.st_mode & 0777) != (mode & 0777) || stats.st_uid != uid || stats.st_gid != gid) {
281 log_debug("set permissions %s, %#o, uid=%u, gid=%u", devnode, mode, uid, gid);
282 err = chmod(devnode, mode);
283 if (err < 0)
284 log_warning_errno(errno, "setting mode of %s to %#o failed: %m", devnode, mode);
285 err = chown(devnode, uid, gid);
286 if (err < 0)
287 log_warning_errno(errno, "setting owner of %s to uid=%u, gid=%u failed: %m", devnode, uid, gid);
288 } else {
289 log_debug("preserve permissions %s, %#o, uid=%u, gid=%u", devnode, mode, uid, gid);
290 }
291
292 /* apply SECLABEL{$module}=$label */
293 udev_list_entry_foreach(entry, udev_list_get_entry(seclabel_list)) {
294 const char *name, *label;
295 int r;
296
297 name = udev_list_entry_get_name(entry);
298 label = udev_list_entry_get_value(entry);
299
300 if (streq(name, "selinux")) {
301 selinux = true;
302
303 r = mac_selinux_apply(devnode, label);
304 if (r < 0)
305 log_error_errno(r, "SECLABEL: failed to set SELinux label '%s': %m", label);
306 else
307 log_debug("SECLABEL: set SELinux label '%s'", label);
308
309 } else if (streq(name, "smack")) {
310 smack = true;
311
312 r = mac_smack_apply(devnode, label);
313 if (r < 0)
314 log_error_errno(r, "SECLABEL: failed to set SMACK label '%s': %m", label);
315 else
316 log_debug("SECLABEL: set SMACK label '%s'", label);
317
318 } else
319 log_error("SECLABEL: unknown subsystem, ignoring '%s'='%s'", name, label);
320 }
321
322 /* set the defaults */
323 if (!selinux)
324 mac_selinux_fix(devnode, true, false);
325 if (!smack)
326 mac_smack_apply(devnode, NULL);
327 }
328
329 /* always update timestamp when we re-use the node, like on media change events */
330 utimensat(AT_FDCWD, devnode, NULL, 0);
331 out:
332 return err;
333 }
334
335 void udev_node_add(struct udev_device *dev, bool apply,
336 mode_t mode, uid_t uid, gid_t gid,
337 struct udev_list *seclabel_list) {
338 char filename[UTIL_PATH_SIZE];
339 struct udev_list_entry *list_entry;
340
341 log_debug("handling device node '%s', devnum=%s, mode=%#o, uid="UID_FMT", gid="GID_FMT,
342 udev_device_get_devnode(dev), udev_device_get_id_filename(dev), mode, uid, gid);
343
344 if (node_permissions_apply(dev, apply, mode, uid, gid, seclabel_list) < 0)
345 return;
346
347 /* always add /dev/{block,char}/$major:$minor */
348 snprintf(filename, sizeof(filename), "/dev/%s/%u:%u",
349 streq(udev_device_get_subsystem(dev), "block") ? "block" : "char",
350 major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));
351 node_symlink(dev, udev_device_get_devnode(dev), filename);
352
353 /* create/update symlinks, add symlinks to name index */
354 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
355 link_update(dev, udev_list_entry_get_name(list_entry), true);
356 }
357
358 void udev_node_remove(struct udev_device *dev) {
359 struct udev_list_entry *list_entry;
360 char filename[UTIL_PATH_SIZE];
361
362 /* remove/update symlinks, remove symlinks from name index */
363 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
364 link_update(dev, udev_list_entry_get_name(list_entry), false);
365
366 /* remove /dev/{block,char}/$major:$minor */
367 snprintf(filename, sizeof(filename), "/dev/%s/%u:%u",
368 streq(udev_device_get_subsystem(dev), "block") ? "block" : "char",
369 major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));
370 unlink(filename);
371 }