2 * Copyright (C) 2003-2009 Kay Sievers <kay.sievers@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/>.
29 #include <sys/types.h>
33 #define TMP_FILE_EXT ".udev-tmp"
35 int udev_node_mknod(struct udev_device *dev, const char *file, dev_t devnum, mode_t mode, uid_t uid, gid_t gid)
37 struct udev *udev = udev_device_get_udev(dev);
42 if (major(devnum) == 0)
43 devnum = udev_device_get_devnum(dev);
45 if (strcmp(udev_device_get_subsystem(dev), "block") == 0)
51 file = udev_device_get_devnode(dev);
53 if (lstat(file, &stats) == 0) {
54 if (((stats.st_mode & S_IFMT) == (mode & S_IFMT)) && (stats.st_rdev == devnum)) {
55 info(udev, "preserve file '%s', because it has correct dev_t\n", file);
57 udev_selinux_lsetfilecon(udev, file, mode);
59 char file_tmp[UTIL_PATH_SIZE + sizeof(TMP_FILE_EXT)];
61 info(udev, "atomically replace existing file '%s'\n", file);
62 util_strscpyl(file_tmp, sizeof(file_tmp), file, TMP_FILE_EXT, NULL);
64 udev_selinux_setfscreatecon(udev, file_tmp, mode);
65 err = mknod(file_tmp, mode, devnum);
66 udev_selinux_resetfscreatecon(udev);
68 err(udev, "mknod(%s, %#o, %u, %u) failed: %m\n",
69 file_tmp, mode, major(devnum), minor(devnum));
72 err = rename(file_tmp, file);
74 err(udev, "rename(%s, %s) failed: %m\n", file_tmp, file);
79 info(udev, "mknod(%s, %#o, (%u,%u))\n", file, mode, major(devnum), minor(devnum));
81 err = util_create_path(udev, file);
82 if (err != 0 && err != -ENOENT)
84 udev_selinux_setfscreatecon(udev, file, mode);
85 err = mknod(file, mode, devnum);
88 udev_selinux_resetfscreatecon(udev);
89 } while (err == -ENOENT);
91 err(udev, "mknod(%s, %#o, (%u,%u) failed: %m\n", file, mode, major(devnum), minor(devnum));
96 if (!preserve || stats.st_mode != mode) {
97 info(udev, "chmod(%s, %#o)\n", file, mode);
98 err = chmod(file, mode);
100 err(udev, "chmod(%s, %#o) failed: %m\n", file, mode);
105 if (!preserve || stats.st_uid != uid || stats.st_gid != gid) {
106 info(udev, "chown(%s, %u, %u)\n", file, uid, gid);
107 err = chown(file, uid, gid);
109 err(udev, "chown(%s, %u, %u) failed: %m\n", file, uid, gid);
117 static int node_symlink(struct udev *udev, const char *node, const char *slink)
120 char target[UTIL_PATH_SIZE];
123 char slink_tmp[UTIL_PATH_SIZE + sizeof(TMP_FILE_EXT)];
128 /* use relative link */
130 while (node[i] && (node[i] == slink[i])) {
137 while (slink[i] != '\0') {
139 l = util_strpcpy(&s, l, "../");
142 l = util_strscpy(s, l, &node[tail]);
148 /* preserve link with correct target, do not replace node of other device */
149 if (lstat(slink, &stats) == 0) {
150 if (S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode)) {
153 info(udev, "found existing node instead of symlink '%s'\n", slink);
154 if (lstat(node, &stats2) == 0) {
155 if ((stats.st_mode & S_IFMT) == (stats2.st_mode & S_IFMT) &&
156 stats.st_rdev == stats2.st_rdev && stats.st_ino != stats2.st_ino) {
157 info(udev, "replace device node '%s' with symlink to our node '%s'\n",
160 err(udev, "device node '%s' already exists, "
161 "link to '%s' will not overwrite it\n",
166 } else if (S_ISLNK(stats.st_mode)) {
167 char buf[UTIL_PATH_SIZE];
170 dbg(udev, "found existing symlink '%s'\n", slink);
171 len = readlink(slink, buf, sizeof(buf));
174 if (strcmp(target, buf) == 0) {
175 info(udev, "preserve already existing symlink '%s' to '%s'\n",
177 udev_selinux_lsetfilecon(udev, slink, S_IFLNK);
183 info(udev, "creating symlink '%s' to '%s'\n", slink, target);
185 err = util_create_path(udev, slink);
186 if (err != 0 && err != -ENOENT)
188 udev_selinux_setfscreatecon(udev, slink, S_IFLNK);
189 err = symlink(target, slink);
192 udev_selinux_resetfscreatecon(udev);
193 } while (err == -ENOENT);
198 info(udev, "atomically replace '%s'\n", slink);
199 util_strscpyl(slink_tmp, sizeof(slink_tmp), slink, TMP_FILE_EXT, NULL);
202 err = util_create_path(udev, slink_tmp);
203 if (err != 0 && err != -ENOENT)
205 udev_selinux_setfscreatecon(udev, slink_tmp, S_IFLNK);
206 err = symlink(target, slink_tmp);
209 udev_selinux_resetfscreatecon(udev);
210 } while (err == -ENOENT);
212 err(udev, "symlink(%s, %s) failed: %m\n", target, slink_tmp);
215 err = rename(slink_tmp, slink);
217 err(udev, "rename(%s, %s) failed: %m\n", slink_tmp, slink);
224 /* find device node of device with highest priority */
225 static const char *link_find_prioritized(struct udev_device *dev, bool add, const char *stackdir, char *buf, size_t bufsize)
227 struct udev *udev = udev_device_get_udev(dev);
230 const char *target = NULL;
233 priority = udev_device_get_devlink_priority(dev);
234 util_strscpy(buf, bufsize, udev_device_get_devnode(dev));
238 dir = opendir(stackdir);
242 struct udev_device *dev_db;
244 char devpath[UTIL_PATH_SIZE];
245 char syspath[UTIL_PATH_SIZE];
249 if (dent == NULL || dent->d_name[0] == '\0')
251 if (dent->d_name[0] == '.')
253 dbg(udev, "found '%s/%s'\n", stackdir, dent->d_name);
254 len = readlinkat(dirfd(dir), dent->d_name, devpath, sizeof(devpath));
255 if (len <= 0 || len == (ssize_t)sizeof(devpath))
258 util_strscpyl(syspath, sizeof(syspath), udev_get_sys_path(udev), devpath, NULL);
259 info(udev, "found '%s' claiming '%s'\n", syspath, stackdir);
261 /* did we find ourself? */
262 if (strcmp(udev_device_get_syspath(dev), syspath) == 0)
265 dev_db = udev_device_new_from_syspath(udev, syspath);
266 if (dev_db != NULL) {
269 devnode = udev_device_get_devnode(dev_db);
270 if (devnode != NULL) {
271 dbg(udev, "compare priority of '%s'(%i) > '%s'(%i)\n", target, priority,
272 udev_device_get_devnode(dev_db), udev_device_get_devlink_priority(dev_db));
273 if (target == NULL || udev_device_get_devlink_priority(dev_db) > priority) {
274 info(udev, "'%s' claims priority %i for '%s'\n",
275 syspath, udev_device_get_devlink_priority(dev_db), stackdir);
276 priority = udev_device_get_devlink_priority(dev_db);
277 util_strscpy(buf, bufsize, devnode);
281 udev_device_unref(dev_db);
288 /* manage "stack of names" with possibly specified device priorities */
289 static void link_update(struct udev_device *dev, const char *slink, bool add)
291 struct udev *udev = udev_device_get_udev(dev);
292 char name_enc[UTIL_PATH_SIZE];
293 char filename[UTIL_PATH_SIZE * 2];
294 char dirname[UTIL_PATH_SIZE];
296 char buf[UTIL_PATH_SIZE];
298 dbg(udev, "update symlink '%s' of '%s'\n", slink, udev_device_get_syspath(dev));
300 util_path_encode(&slink[strlen(udev_get_dev_path(udev))+1], name_enc, sizeof(name_enc));
301 snprintf(dirname, sizeof(dirname), "%s/.udev/links/%s", udev_get_dev_path(udev), name_enc);
302 snprintf(filename, sizeof(filename), "%s/%c%u:%u", dirname,
303 strcmp(udev_device_get_subsystem(dev), "block") == 0 ? 'b' : 'c',
304 major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));
307 dbg(udev, "removing index: '%s'\n", filename);
309 util_delete_path(udev, filename);
312 target = link_find_prioritized(dev, add, dirname, buf, sizeof(buf));
313 if (target == NULL) {
314 info(udev, "no reference left, remove '%s'\n", slink);
316 util_delete_path(udev, slink);
318 info(udev, "creating link '%s' to '%s'\n", slink, target);
319 node_symlink(udev, target, slink);
325 dbg(udev, "creating index: '%s'\n", filename);
327 err = util_create_path(udev, filename);
328 if (err != 0 && err != -ENOENT)
330 err = symlink(udev_device_get_devpath(dev), filename);
333 } while (err == -ENOENT);
337 void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old)
339 struct udev *udev = udev_device_get_udev(dev);
340 struct udev_list_entry *list_entry;
342 /* update possible left-over symlinks */
343 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev_old)) {
344 const char *name = udev_list_entry_get_name(list_entry);
345 struct udev_list_entry *list_entry_current;
348 /* check if old link name still belongs to this device */
350 udev_list_entry_foreach(list_entry_current, udev_device_get_devlinks_list_entry(dev)) {
351 const char *name_current = udev_list_entry_get_name(list_entry_current);
353 if (strcmp(name, name_current) == 0) {
361 info(udev, "update old name, '%s' no longer belonging to '%s'\n",
362 name, udev_device_get_devpath(dev));
363 link_update(dev, name, 0);
367 int udev_node_add(struct udev_device *dev, mode_t mode, uid_t uid, gid_t gid)
369 struct udev *udev = udev_device_get_udev(dev);
372 struct udev_list_entry *list_entry;
375 info(udev, "creating device node '%s', devnum=%d:%d, mode=%#o, uid=%d, gid=%d\n",
376 udev_device_get_devnode(dev),
377 major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)),
380 if (udev_node_mknod(dev, NULL, makedev(0,0), mode, uid, gid) != 0) {
385 /* create all_partitions if requested */
386 num = udev_device_get_num_fake_partitions(dev);
388 info(udev, "creating device partition nodes '%s[1-%i]'\n", udev_device_get_devnode(dev), num);
389 for (i = 1; i <= num; i++) {
390 char partitionname[UTIL_PATH_SIZE];
393 snprintf(partitionname, sizeof(partitionname), "%s%d",
394 udev_device_get_devnode(dev), i);
395 partitionname[sizeof(partitionname)-1] = '\0';
396 part_devnum = makedev(major(udev_device_get_devnum(dev)),
397 minor(udev_device_get_devnum(dev)) + i);
398 udev_node_mknod(dev, partitionname, part_devnum, mode, uid, gid);
402 /* create/update symlinks, add symlinks to name index */
403 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev)) {
404 if (udev_list_entry_get_flag(list_entry))
405 /* simple unmanaged link name */
406 node_symlink(udev, udev_device_get_devnode(dev), udev_list_entry_get_name(list_entry));
408 link_update(dev, udev_list_entry_get_name(list_entry), 1);
414 int udev_node_remove(struct udev_device *dev)
416 struct udev *udev = udev_device_get_udev(dev);
417 struct udev_list_entry *list_entry;
419 char partitionname[UTIL_PATH_SIZE];
424 /* remove,update symlinks, remove symlinks from name index */
425 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
426 link_update(dev, udev_list_entry_get_name(list_entry), 0);
428 devnode = udev_device_get_devnode(dev);
431 if (stat(devnode, &stats) != 0) {
432 info(udev, "device node '%s' not found\n", devnode);
435 if (stats.st_rdev != udev_device_get_devnum(dev)) {
436 info(udev, "device node '%s' points to a different device, skip removal\n", devnode);
440 info(udev, "removing device node '%s'\n", devnode);
441 err = util_unlink_secure(udev, devnode);
445 num = udev_device_get_num_fake_partitions(dev);
449 info(udev, "removing all_partitions '%s[1-%i]'\n", devnode, num);
452 for (i = 1; i <= num; i++) {
453 snprintf(partitionname, sizeof(partitionname), "%s%d", devnode, i);
454 partitionname[sizeof(partitionname)-1] = '\0';
455 util_unlink_secure(udev, partitionname);
458 util_delete_path(udev, devnode);