2 * Copyright (C) 2004-2008 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/>.
28 #include <sys/types.h>
32 #define TMP_FILE_EXT ".udev-tmp"
34 /* reverse mapping from the device file name to the devpath */
35 static int name_index(struct udev *udev, const char *devpath, const char *name, int add)
37 char devpath_enc[UTIL_PATH_SIZE];
38 char name_enc[UTIL_PATH_SIZE];
39 char filename[UTIL_PATH_SIZE * 2];
42 util_path_encode(&name[strlen(udev_get_dev_path(udev))+1], name_enc, sizeof(name_enc));
43 util_path_encode(devpath, devpath_enc, sizeof(devpath_enc));
44 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev),
45 "/.udev/names/", name_enc, "/", devpath_enc, NULL);
48 dbg(udev, "creating index: '%s'\n", filename);
49 util_create_path(udev, filename);
50 fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644);
54 dbg(udev, "removing index: '%s'\n", filename);
56 util_delete_path(udev, filename);
61 int udev_node_mknod(struct udev_device *dev, const char *file, dev_t devnum, mode_t mode, uid_t uid, gid_t gid)
63 struct udev *udev = udev_device_get_udev(dev);
68 if (major(devnum) == 0)
69 devnum = udev_device_get_devnum(dev);
71 if (strcmp(udev_device_get_subsystem(dev), "block") == 0)
77 file = udev_device_get_devnode(dev);
79 if (lstat(file, &stats) == 0) {
80 if (((stats.st_mode & S_IFMT) == (mode & S_IFMT)) && (stats.st_rdev == devnum)) {
81 info(udev, "preserve file '%s', because it has correct dev_t\n", file);
83 udev_selinux_lsetfilecon(udev, file, mode);
85 char file_tmp[UTIL_PATH_SIZE + sizeof(TMP_FILE_EXT)];
87 info(udev, "atomically replace existing file '%s'\n", file);
88 util_strscpyl(file_tmp, sizeof(file_tmp), file, TMP_FILE_EXT, NULL);
90 udev_selinux_setfscreatecon(udev, file_tmp, mode);
91 err = mknod(file_tmp, mode, devnum);
92 udev_selinux_resetfscreatecon(udev);
94 err(udev, "mknod(%s, %#o, %u, %u) failed: %m\n",
95 file_tmp, mode, major(devnum), minor(devnum));
98 err = rename(file_tmp, file);
100 err(udev, "rename(%s, %s) failed: %m\n", file_tmp, file);
105 info(udev, "mknod(%s, %#o, (%u,%u))\n", file, mode, major(devnum), minor(devnum));
106 udev_selinux_setfscreatecon(udev, file, mode);
107 err = mknod(file, mode, devnum);
108 udev_selinux_resetfscreatecon(udev);
110 err(udev, "mknod(%s, %#o, (%u,%u) failed: %m\n", file, mode, major(devnum), minor(devnum));
115 if (!preserve || stats.st_mode != mode) {
116 info(udev, "chmod(%s, %#o)\n", file, mode);
117 err = chmod(file, mode);
119 err(udev, "chmod(%s, %#o) failed: %m\n", file, mode);
124 if (!preserve || stats.st_uid != uid || stats.st_gid != gid) {
125 info(udev, "chown(%s, %u, %u)\n", file, uid, gid);
126 err = chown(file, uid, gid);
128 err(udev, "chown(%s, %u, %u) failed: %m\n", file, uid, gid);
136 static int node_symlink(struct udev *udev, const char *node, const char *slink)
139 char target[UTIL_PATH_SIZE];
142 char slink_tmp[UTIL_PATH_SIZE + sizeof(TMP_FILE_EXT)];
147 /* use relative link */
149 while (node[i] && (node[i] == slink[i])) {
156 while (slink[i] != '\0') {
158 l = util_strpcpy(&s, l, "../");
161 l = util_strscpy(s, l, &node[tail]);
167 /* preserve link with correct target, do not replace node of other device */
168 if (lstat(slink, &stats) == 0) {
169 if (S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode)) {
172 info(udev, "found existing node instead of symlink '%s'\n", slink);
173 if (lstat(node, &stats2) == 0) {
174 if ((stats.st_mode & S_IFMT) == (stats2.st_mode & S_IFMT) &&
175 stats.st_rdev == stats2.st_rdev) {
176 info(udev, "replace device node '%s' with symlink to our node '%s'\n",
179 err(udev, "device node '%s' already exists, "
180 "link to '%s' will not overwrite it\n",
185 } else if (S_ISLNK(stats.st_mode)) {
186 char buf[UTIL_PATH_SIZE];
189 dbg(udev, "found existing symlink '%s'\n", slink);
190 len = readlink(slink, buf, sizeof(buf));
193 if (strcmp(target, buf) == 0) {
194 info(udev, "preserve already existing symlink '%s' to '%s'\n",
196 udev_selinux_lsetfilecon(udev, slink, S_IFLNK);
202 info(udev, "creating symlink '%s' to '%s'\n", slink, target);
203 udev_selinux_setfscreatecon(udev, slink, S_IFLNK);
204 err = symlink(target, slink);
205 udev_selinux_resetfscreatecon(udev);
210 info(udev, "atomically replace '%s'\n", slink);
211 util_strscpyl(slink_tmp, sizeof(slink_tmp), slink, TMP_FILE_EXT, NULL);
213 udev_selinux_setfscreatecon(udev, slink, S_IFLNK);
214 err = symlink(target, slink_tmp);
215 udev_selinux_resetfscreatecon(udev);
217 err(udev, "symlink(%s, %s) failed: %m\n", target, slink_tmp);
220 err = rename(slink_tmp, slink);
222 err(udev, "rename(%s, %s) failed: %m\n", slink_tmp, slink);
229 static int name_index_get_devices(struct udev *udev, const char *name, struct udev_list_node *dev_list)
231 char dirname[UTIL_PATH_SIZE];
238 l = util_strpcpyl(&s, sizeof(dirname), udev_get_dev_path(udev),
239 "/.udev/names/", NULL);
240 util_path_encode(&name[strlen(udev_get_dev_path(udev))+1], s, l);
241 dir = opendir(dirname);
243 dbg(udev, "no index directory '%s': %m\n", dirname);
247 dbg(udev, "found index directory '%s'\n", dirname);
251 char device[UTIL_PATH_SIZE];
254 if (ent == NULL || ent->d_name[0] == '\0')
256 if (ent->d_name[0] == '.')
259 util_strscpyl(device, sizeof(device), udev_get_sys_path(udev), ent->d_name, NULL);
260 util_path_decode(device);
261 udev_list_entry_add(udev, dev_list, device, NULL, 1, 0);
269 static int update_link(struct udev_device *dev, const char *slink)
271 struct udev *udev = udev_device_get_udev(dev);
272 struct udev_list_node dev_list;
273 struct udev_list_entry *dev_entry;
274 char target[UTIL_PATH_SIZE];
279 dbg(udev, "update symlink '%s' of '%s'\n", slink, udev_device_get_syspath(dev));
281 udev_list_init(&dev_list);
282 count = name_index_get_devices(udev, slink, &dev_list);
284 info(udev, "found %i devices with name '%s'\n", count, slink);
286 /* if we don't have a reference, delete it */
288 info(udev, "no reference left, remove '%s'\n", slink);
290 util_delete_path(udev, slink);
294 /* find the device with the highest priority */
296 udev_list_entry_foreach(dev_entry, udev_list_get_entry(&dev_list)) {
298 struct udev_device *dev_db;
301 syspath = udev_list_entry_get_name(dev_entry);
302 dbg(udev, "found '%s' for '%s'\n", syspath, slink);
304 /* did we find ourself? we win, if we have the same priority */
305 if (strcmp(udev_device_get_syspath(dev), syspath) == 0) {
306 dbg(udev, "compare (our own) priority of '%s' %i >= %i\n",
307 udev_device_get_devpath(dev), udev_device_get_devlink_priority(dev), priority);
308 if (strcmp(udev_device_get_devnode(dev), slink) == 0) {
309 info(udev, "'%s' is our device node, database inconsistent, skip link update\n",
310 udev_device_get_devnode(dev));
311 } else if (target[0] == '\0' || udev_device_get_devlink_priority(dev) >= priority) {
312 priority = udev_device_get_devlink_priority(dev);
313 util_strscpy(target, sizeof(target), udev_device_get_devnode(dev));
318 /* another device, read priority from database */
319 dev_db = udev_device_new_from_syspath(udev, syspath);
322 devnode = udev_device_get_devnode(dev_db);
323 if (devnode != NULL) {
324 if (strcmp(devnode, slink) == 0) {
325 info(udev, "'%s' is a device node of '%s', skip link update\n",
328 dbg(udev, "compare priority of '%s' %i > %i\n",
329 udev_device_get_devpath(dev_db),
330 udev_device_get_devlink_priority(dev_db),
332 if (target[0] == '\0' || udev_device_get_devlink_priority(dev_db) > priority) {
333 priority = udev_device_get_devlink_priority(dev_db);
334 util_strscpy(target, sizeof(target), devnode);
338 udev_device_unref(dev_db);
340 udev_list_cleanup_entries(udev, &dev_list);
342 if (target[0] == '\0') {
343 info(udev, "no current target for '%s' found\n", slink);
348 /* create symlink to the target with the highest priority */
349 info(udev, "'%s' with target '%s' has the highest priority %i, create it\n", slink, target, priority);
350 util_create_path(udev, slink);
351 node_symlink(udev, target, slink);
356 void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old)
358 struct udev *udev = udev_device_get_udev(dev);
359 struct udev_list_entry *list_entry;
360 const char *devnode_old;
362 /* update possible left-over symlinks */
363 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev_old)) {
364 const char *name = udev_list_entry_get_name(list_entry);
365 struct udev_list_entry *list_entry_current;
368 /* check if old link name is now our node name */
369 if (strcmp(name, udev_device_get_devnode(dev)) == 0)
372 /* check if old link name still belongs to this device */
374 udev_list_entry_foreach(list_entry_current, udev_device_get_devlinks_list_entry(dev)) {
375 const char *name_current = udev_list_entry_get_name(list_entry_current);
377 if (strcmp(name, name_current) == 0) {
385 info(udev, "update old name, '%s' no longer belonging to '%s'\n",
386 name, udev_device_get_devpath(dev));
387 name_index(udev, udev_device_get_devpath(dev), name, 0);
388 update_link(dev, name);
392 * if the node name has changed, delete the node,
393 * and possibly restore a symlink of a different device
395 devnode_old = udev_device_get_devnode(dev_old);
396 if (devnode_old != NULL) {
397 const char *devnode = udev_device_get_devnode(dev);
399 if (devnode != NULL && strcmp(devnode_old, devnode) != 0) {
400 info(udev, "node has changed from '%s' to '%s'\n", devnode_old, devnode);
401 name_index(udev, udev_device_get_devpath(dev), devnode_old, 0);
402 update_link(dev, devnode_old);
407 int udev_node_add(struct udev_device *dev, mode_t mode, uid_t uid, gid_t gid)
409 struct udev *udev = udev_device_get_udev(dev);
412 struct udev_list_entry *list_entry;
415 info(udev, "creating device node '%s', devnum=%d:%d, mode=%#o, uid=%d, gid=%d\n",
416 udev_device_get_devnode(dev),
417 major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)),
420 util_create_path(udev, udev_device_get_devnode(dev));
421 if (udev_node_mknod(dev, NULL, makedev(0,0), mode, uid, gid) != 0) {
426 /* create all_partitions if requested */
427 num = udev_device_get_num_fake_partitions(dev);
429 info(udev, "creating device partition nodes '%s[1-%i]'\n", udev_device_get_devnode(dev), num);
430 for (i = 1; i <= num; i++) {
431 char partitionname[UTIL_PATH_SIZE];
434 snprintf(partitionname, sizeof(partitionname), "%s%d",
435 udev_device_get_devnode(dev), i);
436 partitionname[sizeof(partitionname)-1] = '\0';
437 part_devnum = makedev(major(udev_device_get_devnum(dev)),
438 minor(udev_device_get_devnum(dev)) + i);
439 udev_node_mknod(dev, partitionname, part_devnum, mode, uid, gid);
443 /* add node to name index */
444 name_index(udev, udev_device_get_devpath(dev), udev_device_get_devnode(dev), 1);
446 /* create/update symlinks, add symlinks to name index */
447 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev)) {
448 name_index(udev, udev_device_get_devpath(dev), udev_list_entry_get_name(list_entry), 1);
449 update_link(dev, udev_list_entry_get_name(list_entry));
455 int udev_node_remove(struct udev_device *dev)
457 struct udev *udev = udev_device_get_udev(dev);
458 struct udev_list_entry *list_entry;
460 char partitionname[UTIL_PATH_SIZE];
465 /* remove node from name index */
466 name_index(udev, udev_device_get_devpath(dev), udev_device_get_devnode(dev), 0);
468 /* remove,update symlinks, remove symlinks from name index */
469 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev)) {
470 name_index(udev, udev_device_get_devpath(dev), udev_list_entry_get_name(list_entry), 0);
471 update_link(dev, udev_list_entry_get_name(list_entry));
474 devnode = udev_device_get_devnode(dev);
477 if (stat(devnode, &stats) != 0) {
478 info(udev, "device node '%s' not found\n", devnode);
481 if (stats.st_rdev != udev_device_get_devnum(dev)) {
482 info(udev, "device node '%s' points to a different device, skip removal\n", devnode);
486 info(udev, "removing device node '%s'\n", devnode);
487 err = util_unlink_secure(udev, devnode);
491 num = udev_device_get_num_fake_partitions(dev);
495 info(udev, "removing all_partitions '%s[1-%i]'\n", devnode, num);
498 for (i = 1; i <= num; i++) {
499 snprintf(partitionname, sizeof(partitionname), "%s%d", devnode, i);
500 partitionname[sizeof(partitionname)-1] = '\0';
501 util_unlink_secure(udev, partitionname);
504 util_delete_path(udev, devnode);