2 * Copyright (C) 2003-2013 Kay Sievers <kay@vrfy.org>
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.
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.
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/>.
30 #include <sys/types.h>
32 #include <attr/xattr.h>
37 static int node_symlink(struct udev_device *dev, const char *node, const char *slink)
40 char target[UTIL_PATH_SIZE];
43 char slink_tmp[UTIL_PATH_SIZE + 32];
48 /* use relative link */
50 while (node[i] && (node[i] == slink[i])) {
57 while (slink[i] != '\0') {
59 l = strpcpy(&s, l, "../");
62 l = strscpy(s, l, &node[tail]);
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\n", slink, node);
73 } else if (S_ISLNK(stats.st_mode)) {
74 char buf[UTIL_PATH_SIZE];
77 len = readlink(slink, buf, sizeof(buf));
78 if (len > 0 && len < (int)sizeof(buf)) {
80 if (streq(target, buf)) {
81 log_debug("preserve already existing symlink '%s' to '%s'\n", slink, target);
82 label_fix(slink, true, false);
83 utimensat(AT_FDCWD, slink, NULL, AT_SYMLINK_NOFOLLOW);
89 log_debug("creating symlink '%s' to '%s'\n", slink, target);
91 err = mkdir_parents_label(slink, 0755);
92 if (err != 0 && err != -ENOENT)
94 label_context_set(slink, S_IFLNK);
95 err = symlink(target, slink);
98 label_context_clear();
99 } while (err == -ENOENT);
104 log_debug("atomically replace '%s'\n", slink);
105 strscpyl(slink_tmp, sizeof(slink_tmp), slink, ".tmp-", udev_device_get_id_filename(dev), NULL);
108 err = mkdir_parents_label(slink_tmp, 0755);
109 if (err != 0 && err != -ENOENT)
111 label_context_set(slink_tmp, S_IFLNK);
112 err = symlink(target, slink_tmp);
115 label_context_clear();
116 } while (err == -ENOENT);
118 log_error("symlink '%s' '%s' failed: %m\n", target, slink_tmp);
121 err = rename(slink_tmp, slink);
123 log_error("rename '%s' '%s' failed: %m\n", slink_tmp, slink);
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)
133 struct udev *udev = udev_device_get_udev(dev);
136 const char *target = NULL;
139 priority = udev_device_get_devlink_priority(dev);
140 strscpy(buf, bufsize, udev_device_get_devnode(dev));
144 dir = opendir(stackdir);
148 struct udev_device *dev_db;
152 if (dent == NULL || dent->d_name[0] == '\0')
154 if (dent->d_name[0] == '.')
157 log_debug("found '%s' claiming '%s'\n", dent->d_name, stackdir);
159 /* did we find ourself? */
160 if (streq(dent->d_name, udev_device_get_id_filename(dev)))
163 dev_db = udev_device_new_from_device_id(udev, dent->d_name);
164 if (dev_db != NULL) {
167 devnode = udev_device_get_devnode(dev_db);
168 if (devnode != NULL) {
169 if (target == NULL || udev_device_get_devlink_priority(dev_db) > priority) {
170 log_debug("'%s' claims priority %i for '%s'\n",
171 udev_device_get_syspath(dev_db), udev_device_get_devlink_priority(dev_db), stackdir);
172 priority = udev_device_get_devlink_priority(dev_db);
173 strscpy(buf, bufsize, devnode);
177 udev_device_unref(dev_db);
184 /* manage "stack of names" with possibly specified device priorities */
185 static void link_update(struct udev_device *dev, const char *slink, bool add)
187 struct udev *udev = udev_device_get_udev(dev);
188 char name_enc[UTIL_PATH_SIZE];
189 char filename[UTIL_PATH_SIZE * 2];
190 char dirname[UTIL_PATH_SIZE];
192 char buf[UTIL_PATH_SIZE];
194 util_path_encode(slink + strlen("/dev"), name_enc, sizeof(name_enc));
195 strscpyl(dirname, sizeof(dirname), "/run/udev/links/", name_enc, NULL);
196 strscpyl(filename, sizeof(filename), dirname, "/", udev_device_get_id_filename(dev), NULL);
198 if (!add && unlink(filename) == 0)
201 target = link_find_prioritized(dev, add, dirname, buf, sizeof(buf));
202 if (target == NULL) {
203 log_debug("no reference left, remove '%s'\n", slink);
204 if (unlink(slink) == 0)
205 util_delete_path(udev, slink);
207 log_debug("creating link '%s' to '%s'\n", slink, target);
208 node_symlink(dev, target, slink);
217 err = mkdir_parents(filename, 0755);
218 if (err != 0 && err != -ENOENT)
220 fd = open(filename, O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444);
225 } while (err == -ENOENT);
229 void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old)
231 struct udev_list_entry *list_entry;
233 /* update possible left-over symlinks */
234 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev_old)) {
235 const char *name = udev_list_entry_get_name(list_entry);
236 struct udev_list_entry *list_entry_current;
239 /* check if old link name still belongs to this device */
241 udev_list_entry_foreach(list_entry_current, udev_device_get_devlinks_list_entry(dev)) {
242 const char *name_current = udev_list_entry_get_name(list_entry_current);
244 if (streq(name, name_current)) {
252 log_debug("update old name, '%s' no longer belonging to '%s'\n",
253 name, udev_device_get_devpath(dev));
254 link_update(dev, name, false);
258 static int node_permissions_apply(struct udev_device *dev, bool apply,
259 mode_t mode, uid_t uid, gid_t gid,
260 struct udev_list *seclabel_list) {
261 const char *devnode = udev_device_get_devnode(dev);
262 dev_t devnum = udev_device_get_devnum(dev);
264 struct udev_list_entry *entry;
267 if (streq(udev_device_get_subsystem(dev), "block"))
272 if (lstat(devnode, &stats) != 0) {
274 log_debug("can not stat() node '%s' (%m)\n", devnode);
278 if (((stats.st_mode & S_IFMT) != (mode & S_IFMT)) || (stats.st_rdev != devnum)) {
280 log_debug("found node '%s' with non-matching devnum %s, skip handling\n",
281 udev_device_get_devnode(dev), udev_device_get_id_filename(dev));
286 bool selinux = false;
291 if ((stats.st_mode & 0777) != (mode & 0777) || stats.st_uid != uid || stats.st_gid != gid) {
292 log_debug("set permissions %s, %#o, uid=%u, gid=%u\n", devnode, mode, uid, gid);
293 chmod(devnode, mode);
294 chown(devnode, uid, gid);
296 log_debug("preserve permissions %s, %#o, uid=%u, gid=%u\n", devnode, mode, uid, gid);
299 /* apply SECLABEL{$module}=$label */
300 udev_list_entry_foreach(entry, udev_list_get_entry(seclabel_list)) {
301 const char *name, *label;
303 name = udev_list_entry_get_name(entry);
304 label = udev_list_entry_get_value(entry);
306 if (streq(name, "selinux")) {
308 if (label_apply(devnode, label) < 0)
309 log_error("SECLABEL: failed to set SELinux label '%s'", label);
311 log_debug("SECLABEL: set SELinux label '%s'", label);
314 } else if (streq(name, "smack")) {
316 if (lsetxattr(devnode, "security.SMACK64", label, strlen(label), 0) < 0)
317 log_error("SECLABEL: failed to set SMACK label '%s'", label);
319 log_debug("SECLABEL: set SMACK label '%s'", label);
323 log_error("SECLABEL: unknown subsystem, ignoring '%s'='%s'", name, label);
326 /* set the defaults */
328 label_fix(devnode, true, false);
331 lremovexattr(devnode, "security.SMACK64");
335 /* always update timestamp when we re-use the node, like on media change events */
336 utimensat(AT_FDCWD, devnode, NULL, 0);
341 void udev_node_add(struct udev_device *dev, bool apply,
342 mode_t mode, uid_t uid, gid_t gid,
343 struct udev_list *seclabel_list) {
344 char filename[UTIL_PATH_SIZE];
345 struct udev_list_entry *list_entry;
347 log_debug("handling device node '%s', devnum=%s, mode=%#o, uid=%d, gid=%d\n",
348 udev_device_get_devnode(dev), udev_device_get_id_filename(dev), mode, uid, gid);
350 if (node_permissions_apply(dev, apply, mode, uid, gid, seclabel_list) < 0)
353 /* always add /dev/{block,char}/$major:$minor */
354 snprintf(filename, sizeof(filename), "/dev/%s/%u:%u",
355 streq(udev_device_get_subsystem(dev), "block") ? "block" : "char",
356 major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));
357 node_symlink(dev, udev_device_get_devnode(dev), filename);
359 /* create/update symlinks, add symlinks to name index */
360 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
361 link_update(dev, udev_list_entry_get_name(list_entry), true);
364 void udev_node_remove(struct udev_device *dev)
366 struct udev_list_entry *list_entry;
367 char filename[UTIL_PATH_SIZE];
369 /* remove/update symlinks, remove symlinks from name index */
370 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
371 link_update(dev, udev_list_entry_get_name(list_entry), false);
373 /* remove /dev/{block,char}/$major:$minor */
374 snprintf(filename, sizeof(filename), "/dev/%s/%u:%u",
375 streq(udev_device_get_subsystem(dev), "block") ? "block" : "char",
376 major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));