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>
34 static int node_symlink(struct udev_device *dev, const char *node, const char *slink)
37 char target[UTIL_PATH_SIZE];
40 char slink_tmp[UTIL_PATH_SIZE + 32];
45 /* use relative link */
47 while (node[i] && (node[i] == slink[i])) {
54 while (slink[i] != '\0') {
56 l = strpcpy(&s, l, "../");
59 l = strscpy(s, l, &node[tail]);
65 /* preserve link with correct target, do not replace node of other device */
66 if (lstat(slink, &stats) == 0) {
67 if (S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode)) {
68 log_error("conflicting device node '%s' found, link to '%s' will not be created\n", slink, node);
70 } else if (S_ISLNK(stats.st_mode)) {
71 char buf[UTIL_PATH_SIZE];
74 len = readlink(slink, buf, sizeof(buf));
75 if (len > 0 && len < (int)sizeof(buf)) {
77 if (streq(target, buf)) {
78 log_debug("preserve already existing symlink '%s' to '%s'\n", slink, target);
79 label_fix(slink, true, false);
80 utimensat(AT_FDCWD, slink, NULL, AT_SYMLINK_NOFOLLOW);
86 log_debug("creating symlink '%s' to '%s'\n", slink, target);
88 err = mkdir_parents_label(slink, 0755);
89 if (err != 0 && err != -ENOENT)
91 label_context_set(slink, S_IFLNK);
92 err = symlink(target, slink);
95 label_context_clear();
96 } while (err == -ENOENT);
101 log_debug("atomically replace '%s'\n", slink);
102 strscpyl(slink_tmp, sizeof(slink_tmp), slink, ".tmp-", udev_device_get_id_filename(dev), NULL);
105 err = mkdir_parents_label(slink_tmp, 0755);
106 if (err != 0 && err != -ENOENT)
108 label_context_set(slink_tmp, S_IFLNK);
109 err = symlink(target, slink_tmp);
112 label_context_clear();
113 } while (err == -ENOENT);
115 log_error("symlink '%s' '%s' failed: %m\n", target, slink_tmp);
118 err = rename(slink_tmp, slink);
120 log_error("rename '%s' '%s' failed: %m\n", slink_tmp, slink);
127 /* find device node of device with highest priority */
128 static const char *link_find_prioritized(struct udev_device *dev, bool add, const char *stackdir, char *buf, size_t bufsize)
130 struct udev *udev = udev_device_get_udev(dev);
133 const char *target = NULL;
136 priority = udev_device_get_devlink_priority(dev);
137 strscpy(buf, bufsize, udev_device_get_devnode(dev));
141 dir = opendir(stackdir);
145 struct udev_device *dev_db;
149 if (dent == NULL || dent->d_name[0] == '\0')
151 if (dent->d_name[0] == '.')
154 log_debug("found '%s' claiming '%s'\n", dent->d_name, stackdir);
156 /* did we find ourself? */
157 if (streq(dent->d_name, udev_device_get_id_filename(dev)))
160 dev_db = udev_device_new_from_device_id(udev, dent->d_name);
161 if (dev_db != NULL) {
164 devnode = udev_device_get_devnode(dev_db);
165 if (devnode != NULL) {
166 if (target == NULL || udev_device_get_devlink_priority(dev_db) > priority) {
167 log_debug("'%s' claims priority %i for '%s'\n",
168 udev_device_get_syspath(dev_db), udev_device_get_devlink_priority(dev_db), stackdir);
169 priority = udev_device_get_devlink_priority(dev_db);
170 strscpy(buf, bufsize, devnode);
174 udev_device_unref(dev_db);
181 /* manage "stack of names" with possibly specified device priorities */
182 static void link_update(struct udev_device *dev, const char *slink, bool add)
184 struct udev *udev = udev_device_get_udev(dev);
185 char name_enc[UTIL_PATH_SIZE];
186 char filename[UTIL_PATH_SIZE * 2];
187 char dirname[UTIL_PATH_SIZE];
189 char buf[UTIL_PATH_SIZE];
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);
195 if (!add && unlink(filename) == 0)
198 target = link_find_prioritized(dev, add, dirname, buf, sizeof(buf));
199 if (target == NULL) {
200 log_debug("no reference left, remove '%s'\n", slink);
201 if (unlink(slink) == 0)
202 util_delete_path(udev, slink);
204 log_debug("creating link '%s' to '%s'\n", slink, target);
205 node_symlink(dev, target, slink);
214 err = mkdir_parents(filename, 0755);
215 if (err != 0 && err != -ENOENT)
217 fd = open(filename, O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444);
222 } while (err == -ENOENT);
226 void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old)
228 struct udev_list_entry *list_entry;
230 /* update possible left-over symlinks */
231 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev_old)) {
232 const char *name = udev_list_entry_get_name(list_entry);
233 struct udev_list_entry *list_entry_current;
236 /* check if old link name still belongs to this device */
238 udev_list_entry_foreach(list_entry_current, udev_device_get_devlinks_list_entry(dev)) {
239 const char *name_current = udev_list_entry_get_name(list_entry_current);
241 if (streq(name, name_current)) {
249 log_debug("update old name, '%s' no longer belonging to '%s'\n",
250 name, udev_device_get_devpath(dev));
251 link_update(dev, name, false);
255 static int node_permissions_apply(struct udev_device *dev, bool apply, mode_t mode, uid_t uid, gid_t gid)
257 const char *devnode = udev_device_get_devnode(dev);
258 dev_t devnum = udev_device_get_devnum(dev);
262 if (streq(udev_device_get_subsystem(dev), "block"))
267 if (lstat(devnode, &stats) != 0) {
269 log_debug("can not stat() node '%s' (%m)\n", devnode);
273 if (((stats.st_mode & S_IFMT) != (mode & S_IFMT)) || (stats.st_rdev != devnum)) {
275 log_debug("found node '%s' with non-matching devnum %s, skip handling\n",
276 udev_device_get_devnode(dev), udev_device_get_id_filename(dev));
281 if ((stats.st_mode & 0777) != (mode & 0777) || stats.st_uid != uid || stats.st_gid != gid) {
282 log_debug("set permissions %s, %#o, uid=%u, gid=%u\n", devnode, mode, uid, gid);
283 chmod(devnode, mode);
284 chown(devnode, uid, gid);
286 log_debug("preserve permissions %s, %#o, uid=%u, gid=%u\n", devnode, mode, uid, gid);
288 label_fix(devnode, true, false);
291 /* always update timestamp when we re-use the node, like on media change events */
292 utimensat(AT_FDCWD, devnode, NULL, 0);
297 void udev_node_add(struct udev_device *dev, bool apply, mode_t mode, uid_t uid, gid_t gid)
299 char filename[UTIL_PATH_SIZE];
300 struct udev_list_entry *list_entry;
302 log_debug("handling device node '%s', devnum=%s, mode=%#o, uid=%d, gid=%d\n",
303 udev_device_get_devnode(dev), udev_device_get_id_filename(dev), mode, uid, gid);
305 if (node_permissions_apply(dev, apply, mode, uid, gid) < 0)
308 /* always add /dev/{block,char}/$major:$minor */
309 snprintf(filename, sizeof(filename), "/dev/%s/%u:%u",
310 streq(udev_device_get_subsystem(dev), "block") ? "block" : "char",
311 major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));
312 node_symlink(dev, udev_device_get_devnode(dev), filename);
314 /* create/update symlinks, add symlinks to name index */
315 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
316 link_update(dev, udev_list_entry_get_name(list_entry), true);
319 void udev_node_remove(struct udev_device *dev)
321 struct udev_list_entry *list_entry;
322 char filename[UTIL_PATH_SIZE];
324 /* remove/update symlinks, remove symlinks from name index */
325 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
326 link_update(dev, udev_list_entry_get_name(list_entry), false);
328 /* remove /dev/{block,char}/$major:$minor */
329 snprintf(filename, sizeof(filename), "/dev/%s/%u:%u",
330 streq(udev_device_get_subsystem(dev), "block") ? "block" : "char",
331 major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));