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