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/>.
30 #include <sys/types.h>
34 #define TMP_FILE_EXT ".udev-tmp"
36 int udev_node_mknod(struct udev_device *dev, const char *file, dev_t devnum, mode_t mode, uid_t uid, gid_t gid)
38 struct udev *udev = udev_device_get_udev(dev);
43 if (major(devnum) == 0)
44 devnum = udev_device_get_devnum(dev);
46 if (strcmp(udev_device_get_subsystem(dev), "block") == 0)
52 file = udev_device_get_devnode(dev);
54 if (lstat(file, &stats) == 0) {
55 if (((stats.st_mode & S_IFMT) == (mode & S_IFMT)) && (stats.st_rdev == devnum)) {
56 info(udev, "preserve file '%s', because it has correct dev_t\n", file);
58 udev_selinux_lsetfilecon(udev, file, mode);
59 /* update time stamp when we re-use the node, like on media change events */
62 char file_tmp[UTIL_PATH_SIZE + sizeof(TMP_FILE_EXT)];
64 info(udev, "atomically replace existing file '%s'\n", file);
65 util_strscpyl(file_tmp, sizeof(file_tmp), file, TMP_FILE_EXT, NULL);
67 udev_selinux_setfscreatecon(udev, file_tmp, mode);
68 err = mknod(file_tmp, mode, devnum);
69 udev_selinux_resetfscreatecon(udev);
71 err(udev, "mknod(%s, %#o, %u, %u) failed: %m\n",
72 file_tmp, mode, major(devnum), minor(devnum));
75 err = rename(file_tmp, file);
77 err(udev, "rename(%s, %s) failed: %m\n", file_tmp, file);
82 info(udev, "mknod(%s, %#o, (%u,%u))\n", file, mode, major(devnum), minor(devnum));
84 err = util_create_path(udev, file);
85 if (err != 0 && err != -ENOENT)
87 udev_selinux_setfscreatecon(udev, file, mode);
88 err = mknod(file, mode, devnum);
91 udev_selinux_resetfscreatecon(udev);
92 } while (err == -ENOENT);
94 err(udev, "mknod(%s, %#o, (%u,%u) failed: %m\n", file, mode, major(devnum), minor(devnum));
99 if (!preserve || stats.st_mode != mode) {
100 info(udev, "chmod(%s, %#o)\n", file, mode);
101 err = chmod(file, mode);
103 err(udev, "chmod(%s, %#o) failed: %m\n", file, mode);
108 if (!preserve || stats.st_uid != uid || stats.st_gid != gid) {
109 info(udev, "chown(%s, %u, %u)\n", file, uid, gid);
110 err = chown(file, uid, gid);
112 err(udev, "chown(%s, %u, %u) failed: %m\n", file, uid, gid);
120 static int node_symlink(struct udev *udev, const char *node, const char *slink)
123 char target[UTIL_PATH_SIZE];
126 char slink_tmp[UTIL_PATH_SIZE + sizeof(TMP_FILE_EXT)];
131 /* use relative link */
133 while (node[i] && (node[i] == slink[i])) {
140 while (slink[i] != '\0') {
142 l = util_strpcpy(&s, l, "../");
145 l = util_strscpy(s, l, &node[tail]);
151 /* preserve link with correct target, do not replace node of other device */
152 if (lstat(slink, &stats) == 0) {
153 if (S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode)) {
156 info(udev, "found existing node instead of symlink '%s'\n", slink);
157 if (lstat(node, &stats2) == 0) {
158 if ((stats.st_mode & S_IFMT) == (stats2.st_mode & S_IFMT) &&
159 stats.st_rdev == stats2.st_rdev && stats.st_ino != stats2.st_ino) {
160 info(udev, "replace device node '%s' with symlink to our node '%s'\n",
163 err(udev, "device node '%s' already exists, "
164 "link to '%s' will not overwrite it\n",
169 } else if (S_ISLNK(stats.st_mode)) {
170 char buf[UTIL_PATH_SIZE];
173 dbg(udev, "found existing symlink '%s'\n", slink);
174 len = readlink(slink, buf, sizeof(buf));
177 if (strcmp(target, buf) == 0) {
178 info(udev, "preserve already existing symlink '%s' to '%s'\n",
180 udev_selinux_lsetfilecon(udev, slink, S_IFLNK);
181 lutimes(slink, NULL);
187 info(udev, "creating symlink '%s' to '%s'\n", slink, target);
189 err = util_create_path(udev, slink);
190 if (err != 0 && err != -ENOENT)
192 udev_selinux_setfscreatecon(udev, slink, S_IFLNK);
193 err = symlink(target, slink);
196 udev_selinux_resetfscreatecon(udev);
197 } while (err == -ENOENT);
202 info(udev, "atomically replace '%s'\n", slink);
203 util_strscpyl(slink_tmp, sizeof(slink_tmp), slink, TMP_FILE_EXT, NULL);
206 err = util_create_path(udev, slink_tmp);
207 if (err != 0 && err != -ENOENT)
209 udev_selinux_setfscreatecon(udev, slink_tmp, S_IFLNK);
210 err = symlink(target, slink_tmp);
213 udev_selinux_resetfscreatecon(udev);
214 } while (err == -ENOENT);
216 err(udev, "symlink(%s, %s) failed: %m\n", target, slink_tmp);
219 err = rename(slink_tmp, slink);
221 err(udev, "rename(%s, %s) failed: %m\n", slink_tmp, slink);
228 /* find device node of device with highest priority */
229 static const char *link_find_prioritized(struct udev_device *dev, bool add, const char *stackdir, char *buf, size_t bufsize)
231 struct udev *udev = udev_device_get_udev(dev);
234 const char *target = NULL;
237 priority = udev_device_get_devlink_priority(dev);
238 util_strscpy(buf, bufsize, udev_device_get_devnode(dev));
242 dir = opendir(stackdir);
246 struct udev_device *dev_db;
248 char devpath[UTIL_PATH_SIZE];
249 char syspath[UTIL_PATH_SIZE];
253 if (dent == NULL || dent->d_name[0] == '\0')
255 if (dent->d_name[0] == '.')
257 dbg(udev, "found '%s/%s'\n", stackdir, dent->d_name);
258 len = readlinkat(dirfd(dir), dent->d_name, devpath, sizeof(devpath));
259 if (len <= 0 || len == (ssize_t)sizeof(devpath))
262 util_strscpyl(syspath, sizeof(syspath), udev_get_sys_path(udev), devpath, NULL);
263 info(udev, "found '%s' claiming '%s'\n", syspath, stackdir);
265 /* did we find ourself? */
266 if (strcmp(udev_device_get_syspath(dev), syspath) == 0)
269 dev_db = udev_device_new_from_syspath(udev, syspath);
270 if (dev_db != NULL) {
273 devnode = udev_device_get_devnode(dev_db);
274 if (devnode != NULL) {
275 dbg(udev, "compare priority of '%s'(%i) > '%s'(%i)\n", target, priority,
276 udev_device_get_devnode(dev_db), udev_device_get_devlink_priority(dev_db));
277 if (target == NULL || udev_device_get_devlink_priority(dev_db) > priority) {
278 info(udev, "'%s' claims priority %i for '%s'\n",
279 syspath, udev_device_get_devlink_priority(dev_db), stackdir);
280 priority = udev_device_get_devlink_priority(dev_db);
281 util_strscpy(buf, bufsize, devnode);
285 udev_device_unref(dev_db);
292 /* manage "stack of names" with possibly specified device priorities */
293 static void link_update(struct udev_device *dev, const char *slink, bool add)
295 struct udev *udev = udev_device_get_udev(dev);
296 char name_enc[UTIL_PATH_SIZE];
297 char filename[UTIL_PATH_SIZE * 2];
298 char dirname[UTIL_PATH_SIZE];
300 char buf[UTIL_PATH_SIZE];
302 dbg(udev, "update symlink '%s' of '%s'\n", slink, udev_device_get_syspath(dev));
304 util_path_encode(&slink[strlen(udev_get_dev_path(udev))+1], name_enc, sizeof(name_enc));
305 snprintf(dirname, sizeof(dirname), "%s/.udev/links/%s", udev_get_dev_path(udev), name_enc);
306 snprintf(filename, sizeof(filename), "%s/%c%u:%u", dirname,
307 strcmp(udev_device_get_subsystem(dev), "block") == 0 ? 'b' : 'c',
308 major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));
311 dbg(udev, "removing index: '%s'\n", filename);
313 util_delete_path(udev, filename);
316 target = link_find_prioritized(dev, add, dirname, buf, sizeof(buf));
317 if (target == NULL) {
318 info(udev, "no reference left, remove '%s'\n", slink);
320 util_delete_path(udev, slink);
322 info(udev, "creating link '%s' to '%s'\n", slink, target);
323 node_symlink(udev, target, slink);
329 dbg(udev, "creating index: '%s'\n", filename);
331 err = util_create_path(udev, filename);
332 if (err != 0 && err != -ENOENT)
334 err = symlink(udev_device_get_devpath(dev), filename);
337 } while (err == -ENOENT);
341 void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old)
343 struct udev *udev = udev_device_get_udev(dev);
344 struct udev_list_entry *list_entry;
346 /* update possible left-over symlinks */
347 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev_old)) {
348 const char *name = udev_list_entry_get_name(list_entry);
349 struct udev_list_entry *list_entry_current;
352 /* check if old link name still belongs to this device */
354 udev_list_entry_foreach(list_entry_current, udev_device_get_devlinks_list_entry(dev)) {
355 const char *name_current = udev_list_entry_get_name(list_entry_current);
357 if (strcmp(name, name_current) == 0) {
365 info(udev, "update old name, '%s' no longer belonging to '%s'\n",
366 name, udev_device_get_devpath(dev));
367 link_update(dev, name, 0);
371 int udev_node_add(struct udev_device *dev, mode_t mode, uid_t uid, gid_t gid)
373 struct udev *udev = udev_device_get_udev(dev);
376 struct udev_list_entry *list_entry;
379 info(udev, "creating device node '%s', devnum=%d:%d, mode=%#o, uid=%d, gid=%d\n",
380 udev_device_get_devnode(dev),
381 major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)),
384 if (udev_node_mknod(dev, NULL, makedev(0,0), mode, uid, gid) != 0) {
389 /* create all_partitions if requested */
390 num = udev_device_get_num_fake_partitions(dev);
392 info(udev, "creating device partition nodes '%s[1-%i]'\n", udev_device_get_devnode(dev), num);
393 for (i = 1; i <= num; i++) {
394 char partitionname[UTIL_PATH_SIZE];
397 snprintf(partitionname, sizeof(partitionname), "%s%d",
398 udev_device_get_devnode(dev), i);
399 partitionname[sizeof(partitionname)-1] = '\0';
400 part_devnum = makedev(major(udev_device_get_devnum(dev)),
401 minor(udev_device_get_devnum(dev)) + i);
402 udev_node_mknod(dev, partitionname, part_devnum, mode, uid, gid);
406 /* create/update symlinks, add symlinks to name index */
407 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev)) {
408 if (udev_list_entry_get_flag(list_entry))
409 /* simple unmanaged link name */
410 node_symlink(udev, udev_device_get_devnode(dev), udev_list_entry_get_name(list_entry));
412 link_update(dev, udev_list_entry_get_name(list_entry), 1);
418 int udev_node_remove(struct udev_device *dev)
420 struct udev *udev = udev_device_get_udev(dev);
421 struct udev_list_entry *list_entry;
423 char partitionname[UTIL_PATH_SIZE];
425 struct udev_device *dev_check;
429 /* remove/update symlinks, remove symlinks from name index */
430 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
431 link_update(dev, udev_list_entry_get_name(list_entry), 0);
433 devnode = udev_device_get_devnode(dev);
436 if (stat(devnode, &stats) != 0) {
437 info(udev, "device node '%s' not found\n", devnode);
440 if (stats.st_rdev != udev_device_get_devnum(dev)) {
441 info(udev, "device node '%s' points to a different device, skip removal\n", devnode);
445 dev_check = udev_device_new_from_syspath(udev, udev_device_get_syspath(dev));
446 if (dev_check != NULL && stats.st_rdev == udev_device_get_devnum(dev_check)) {
447 /* do not remove device node if the same sys-device is re-created in the meantime */
448 info(udev, "keeping device node '%s'\n", devnode);
450 info(udev, "removing device node '%s'\n", devnode);
451 err = util_unlink_secure(udev, devnode);
453 udev_device_unref(dev_check);
455 num = udev_device_get_num_fake_partitions(dev);
459 info(udev, "removing all_partitions '%s[1-%i]'\n", devnode, num);
462 for (i = 1; i <= num; i++) {
463 snprintf(partitionname, sizeof(partitionname), "%s%d", devnode, i);
464 partitionname[sizeof(partitionname)-1] = '\0';
465 util_unlink_secure(udev, partitionname);
468 util_delete_path(udev, devnode);