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>
33 #include "smack-util.h"
35 static int node_symlink(struct udev_device *dev, const char *node, const char *slink)
38 char target[UTIL_PATH_SIZE];
41 char slink_tmp[UTIL_PATH_SIZE + 32];
46 /* use relative link */
48 while (node[i] && (node[i] == slink[i])) {
55 while (slink[i] != '\0') {
57 l = strpcpy(&s, l, "../");
60 l = strscpy(s, l, &node[tail]);
66 /* preserve link with correct target, do not replace node of other device */
67 if (lstat(slink, &stats) == 0) {
68 if (S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode)) {
69 log_error("conflicting device node '%s' found, link to '%s' will not be created", slink, node);
71 } else if (S_ISLNK(stats.st_mode)) {
72 char buf[UTIL_PATH_SIZE];
75 len = readlink(slink, buf, sizeof(buf));
76 if (len > 0 && len < (int)sizeof(buf)) {
78 if (streq(target, buf)) {
79 log_debug("preserve already existing symlink '%s' to '%s'", slink, target);
80 label_fix(slink, true, false);
81 utimensat(AT_FDCWD, slink, NULL, AT_SYMLINK_NOFOLLOW);
87 log_debug("creating symlink '%s' to '%s'", slink, target);
89 err = mkdir_parents_label(slink, 0755);
90 if (err != 0 && err != -ENOENT)
92 label_context_set(slink, S_IFLNK);
93 err = symlink(target, slink);
96 label_context_clear();
97 } while (err == -ENOENT);
102 log_debug("atomically replace '%s'", slink);
103 strscpyl(slink_tmp, sizeof(slink_tmp), slink, ".tmp-", udev_device_get_id_filename(dev), NULL);
106 err = mkdir_parents_label(slink_tmp, 0755);
107 if (err != 0 && err != -ENOENT)
109 label_context_set(slink_tmp, S_IFLNK);
110 err = symlink(target, slink_tmp);
113 label_context_clear();
114 } while (err == -ENOENT);
116 log_error("symlink '%s' '%s' failed: %m", target, slink_tmp);
119 err = rename(slink_tmp, slink);
121 log_error("rename '%s' '%s' failed: %m", slink_tmp, slink);
128 /* find device node of device with highest priority */
129 static const char *link_find_prioritized(struct udev_device *dev, bool add, const char *stackdir, char *buf, size_t bufsize)
131 struct udev *udev = udev_device_get_udev(dev);
134 const char *target = NULL;
137 priority = udev_device_get_devlink_priority(dev);
138 strscpy(buf, bufsize, udev_device_get_devnode(dev));
142 dir = opendir(stackdir);
146 struct udev_device *dev_db;
150 if (dent == NULL || dent->d_name[0] == '\0')
152 if (dent->d_name[0] == '.')
155 log_debug("found '%s' claiming '%s'", dent->d_name, stackdir);
157 /* did we find ourself? */
158 if (streq(dent->d_name, udev_device_get_id_filename(dev)))
161 dev_db = udev_device_new_from_device_id(udev, dent->d_name);
162 if (dev_db != NULL) {
165 devnode = udev_device_get_devnode(dev_db);
166 if (devnode != NULL) {
167 if (target == NULL || udev_device_get_devlink_priority(dev_db) > priority) {
168 log_debug("'%s' claims priority %i for '%s'",
169 udev_device_get_syspath(dev_db), udev_device_get_devlink_priority(dev_db), stackdir);
170 priority = udev_device_get_devlink_priority(dev_db);
171 strscpy(buf, bufsize, devnode);
175 udev_device_unref(dev_db);
182 /* manage "stack of names" with possibly specified device priorities */
183 static void link_update(struct udev_device *dev, const char *slink, bool add)
185 struct udev *udev = udev_device_get_udev(dev);
186 char name_enc[UTIL_PATH_SIZE];
187 char filename[UTIL_PATH_SIZE * 2];
188 char dirname[UTIL_PATH_SIZE];
190 char buf[UTIL_PATH_SIZE];
192 util_path_encode(slink + strlen("/dev"), name_enc, sizeof(name_enc));
193 strscpyl(dirname, sizeof(dirname), "/run/udev/links/", name_enc, NULL);
194 strscpyl(filename, sizeof(filename), dirname, "/", udev_device_get_id_filename(dev), NULL);
196 if (!add && unlink(filename) == 0)
199 target = link_find_prioritized(dev, add, dirname, buf, sizeof(buf));
200 if (target == NULL) {
201 log_debug("no reference left, remove '%s'", slink);
202 if (unlink(slink) == 0)
203 util_delete_path(udev, slink);
205 log_debug("creating link '%s' to '%s'", slink, target);
206 node_symlink(dev, target, slink);
215 err = mkdir_parents(filename, 0755);
216 if (err != 0 && err != -ENOENT)
218 fd = open(filename, O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444);
223 } while (err == -ENOENT);
227 void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old)
229 struct udev_list_entry *list_entry;
231 /* update possible left-over symlinks */
232 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev_old)) {
233 const char *name = udev_list_entry_get_name(list_entry);
234 struct udev_list_entry *list_entry_current;
237 /* check if old link name still belongs to this device */
239 udev_list_entry_foreach(list_entry_current, udev_device_get_devlinks_list_entry(dev)) {
240 const char *name_current = udev_list_entry_get_name(list_entry_current);
242 if (streq(name, name_current)) {
250 log_debug("update old name, '%s' no longer belonging to '%s'",
251 name, udev_device_get_devpath(dev));
252 link_update(dev, name, false);
256 static int node_permissions_apply(struct udev_device *dev, bool apply,
257 mode_t mode, uid_t uid, gid_t gid,
258 struct udev_list *seclabel_list) {
259 const char *devnode = udev_device_get_devnode(dev);
260 dev_t devnum = udev_device_get_devnum(dev);
262 struct udev_list_entry *entry;
265 if (streq(udev_device_get_subsystem(dev), "block"))
270 if (lstat(devnode, &stats) != 0) {
272 log_debug("can not stat() node '%s' (%m)", devnode);
276 if (((stats.st_mode & S_IFMT) != (mode & S_IFMT)) || (stats.st_rdev != devnum)) {
278 log_debug("found node '%s' with non-matching devnum %s, skip handling",
279 udev_device_get_devnode(dev), udev_device_get_id_filename(dev));
284 bool selinux = false;
287 if ((stats.st_mode & 0777) != (mode & 0777) || stats.st_uid != uid || stats.st_gid != gid) {
288 log_debug("set permissions %s, %#o, uid=%u, gid=%u", devnode, mode, uid, gid);
289 chmod(devnode, mode);
290 chown(devnode, uid, gid);
292 log_debug("preserve permissions %s, %#o, uid=%u, gid=%u", devnode, mode, uid, gid);
295 /* apply SECLABEL{$module}=$label */
296 udev_list_entry_foreach(entry, udev_list_get_entry(seclabel_list)) {
297 const char *name, *label;
299 name = udev_list_entry_get_name(entry);
300 label = udev_list_entry_get_value(entry);
302 if (streq(name, "selinux")) {
304 if (label_apply(devnode, label) < 0)
305 log_error("SECLABEL: failed to set SELinux label '%s'", label);
307 log_debug("SECLABEL: set SELinux label '%s'", label);
309 } else if (streq(name, "smack")) {
311 if (smack_label_path(devnode, label) < 0)
312 log_error("SECLABEL: failed to set SMACK label '%s'", label);
314 log_debug("SECLABEL: set SMACK label '%s'", label);
317 log_error("SECLABEL: unknown subsystem, ignoring '%s'='%s'", name, label);
320 /* set the defaults */
322 label_fix(devnode, true, false);
324 smack_label_path(devnode, NULL);
327 /* always update timestamp when we re-use the node, like on media change events */
328 utimensat(AT_FDCWD, devnode, NULL, 0);
333 void udev_node_add(struct udev_device *dev, bool apply,
334 mode_t mode, uid_t uid, gid_t gid,
335 struct udev_list *seclabel_list) {
336 char filename[UTIL_PATH_SIZE];
337 struct udev_list_entry *list_entry;
339 log_debug("handling device node '%s', devnum=%s, mode=%#o, uid=%d, gid=%d",
340 udev_device_get_devnode(dev), udev_device_get_id_filename(dev), mode, uid, gid);
342 if (node_permissions_apply(dev, apply, mode, uid, gid, seclabel_list) < 0)
345 /* always add /dev/{block,char}/$major:$minor */
346 snprintf(filename, sizeof(filename), "/dev/%s/%u:%u",
347 streq(udev_device_get_subsystem(dev), "block") ? "block" : "char",
348 major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));
349 node_symlink(dev, udev_device_get_devnode(dev), filename);
351 /* create/update symlinks, add symlinks to name index */
352 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
353 link_update(dev, udev_list_entry_get_name(list_entry), true);
356 void udev_node_remove(struct udev_device *dev)
358 struct udev_list_entry *list_entry;
359 char filename[UTIL_PATH_SIZE];
361 /* remove/update symlinks, remove symlinks from name index */
362 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
363 link_update(dev, udev_list_entry_get_name(list_entry), false);
365 /* remove /dev/{block,char}/$major:$minor */
366 snprintf(filename, sizeof(filename), "/dev/%s/%u:%u",
367 streq(udev_device_get_subsystem(dev), "block") ? "block" : "char",
368 major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));