udev-node.c revision e7f32890335886cfa0f2f835413aed5af8ac2b53
1c1b291a3d146eeb4e7e66e73e9fa891e075b509Till Mossakowski * Copyright (C) 2003-2010 Kay Sievers <kay.sievers@vrfy.org>
1c1b291a3d146eeb4e7e66e73e9fa891e075b509Till Mossakowski * This program is free software: you can redistribute it and/or modify
1c1b291a3d146eeb4e7e66e73e9fa891e075b509Till Mossakowski * it under the terms of the GNU General Public License as published by
1c1b291a3d146eeb4e7e66e73e9fa891e075b509Till Mossakowski * the Free Software Foundation, either version 2 of the License, or
1c1b291a3d146eeb4e7e66e73e9fa891e075b509Till Mossakowski * (at your option) any later version.
1c1b291a3d146eeb4e7e66e73e9fa891e075b509Till Mossakowski * This program is distributed in the hope that it will be useful,
1c1b291a3d146eeb4e7e66e73e9fa891e075b509Till Mossakowski * but WITHOUT ANY WARRANTY; without even the implied warranty of
1c1b291a3d146eeb4e7e66e73e9fa891e075b509Till Mossakowski * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1c1b291a3d146eeb4e7e66e73e9fa891e075b509Till Mossakowski * GNU General Public License for more details.
1c1b291a3d146eeb4e7e66e73e9fa891e075b509Till Mossakowski * You should have received a copy of the GNU General Public License
1c1b291a3d146eeb4e7e66e73e9fa891e075b509Till Mossakowski * along with this program. If not, see <http://www.gnu.org/licenses/>.
1c1b291a3d146eeb4e7e66e73e9fa891e075b509Till Mossakowskistatic int node_symlink(struct udev *udev, const char *node, const char *slink)
1c1b291a3d146eeb4e7e66e73e9fa891e075b509Till Mossakowski char slink_tmp[UTIL_PATH_SIZE + sizeof(TMP_FILE_EXT)];
1c1b291a3d146eeb4e7e66e73e9fa891e075b509Till Mossakowski /* use relative link */
1c1b291a3d146eeb4e7e66e73e9fa891e075b509Till Mossakowski if (l == 0) {
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski /* preserve link with correct target, do not replace node of other device */
83a9571a3df107123eb25f59ac52c576468f2952Till Mossakowski if (S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode)) {
f69658e57cba7ecb37c0d84181f4c563215c2534Till Mossakowski info(udev, "found existing node instead of symlink '%s'\n", slink);
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski if ((stats.st_mode & S_IFMT) == (stats2.st_mode & S_IFMT) &&
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski stats.st_rdev == stats2.st_rdev && stats.st_ino != stats2.st_ino) {
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski info(udev, "replace device node '%s' with symlink to our node '%s'\n",
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski err(udev, "device node '%s' already exists, "
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski "link to '%s' will not overwrite it\n",
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski dbg(udev, "found existing symlink '%s'\n", slink);
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski info(udev, "preserve already existing symlink '%s' to '%s'\n",
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski udev_selinux_lsetfilecon(udev, slink, S_IFLNK);
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski utimensat(AT_FDCWD, slink, NULL, AT_SYMLINK_NOFOLLOW);
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski info(udev, "creating symlink '%s' to '%s'\n", slink, target);
67d5e49547d78aa56a8f9ba5e64a950b730eba66Till Mossakowski err = util_create_path_selinux(udev, slink);
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski udev_selinux_setfscreatecon(udev, slink, S_IFLNK);
83a9571a3df107123eb25f59ac52c576468f2952Till Mossakowski info(udev, "atomically replace '%s'\n", slink);
83a9571a3df107123eb25f59ac52c576468f2952Till Mossakowski util_strscpyl(slink_tmp, sizeof(slink_tmp), slink, TMP_FILE_EXT, NULL);
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski err = util_create_path_selinux(udev, slink_tmp);
968edf72c9abb1e35ad5f41419d0399c6d9acf32Till Mossakowski udev_selinux_setfscreatecon(udev, slink_tmp, S_IFLNK);
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski err(udev, "symlink '%s' '%s' failed: %m\n", target, slink_tmp);
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski err(udev, "rename '%s' '%s' failed: %m\n", slink_tmp, slink);
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski/* find device node of device with highest priority */
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowskistatic const char *link_find_prioritized(struct udev_device *dev, bool add, const char *stackdir, char *buf, size_t bufsize)
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski struct udev *udev = udev_device_get_udev(dev);
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski priority = udev_device_get_devlink_priority(dev);
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski util_strscpy(buf, bufsize, udev_device_get_devnode(dev));
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski if (dent == NULL || dent->d_name[0] == '\0')
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski info(udev, "found '%s' claiming '%s'\n", dent->d_name, stackdir);
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski /* did we find ourself? */
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski if (strcmp(dent->d_name, udev_device_get_id_filename(dev)) == 0)
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski dev_db = udev_device_new_from_id_filename(udev, dent->d_name);
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski dbg(udev, "compare priority of '%s'(%i) > '%s'(%i)\n", target, priority,
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski udev_device_get_devnode(dev_db), udev_device_get_devlink_priority(dev_db));
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski if (target == NULL || udev_device_get_devlink_priority(dev_db) > priority) {
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski info(udev, "'%s' claims priority %i for '%s'\n",
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski udev_device_get_syspath(dev_db), udev_device_get_devlink_priority(dev_db), stackdir);
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski priority = udev_device_get_devlink_priority(dev_db);
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski/* manage "stack of names" with possibly specified device priorities */
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowskistatic void link_update(struct udev_device *dev, const char *slink, bool add)
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski struct udev *udev = udev_device_get_udev(dev);
144d4893ba5a3815bd1639d498ee4a20ed13a211Till Mossakowski dbg(udev, "update symlink '%s' of '%s'\n", slink, udev_device_get_syspath(dev));
968edf72c9abb1e35ad5f41419d0399c6d9acf32Till Mossakowski util_path_encode(&slink[strlen(udev_get_dev_path(udev))+1], name_enc, sizeof(name_enc));
968edf72c9abb1e35ad5f41419d0399c6d9acf32Till Mossakowski util_strscpyl(dirname, sizeof(dirname), udev_get_run_path(udev), "/links/", name_enc, NULL);
968edf72c9abb1e35ad5f41419d0399c6d9acf32Till Mossakowski util_strscpyl(filename, sizeof(filename), dirname, "/", udev_device_get_id_filename(dev), NULL);
968edf72c9abb1e35ad5f41419d0399c6d9acf32Till Mossakowski dbg(udev, "removing index: '%s'\n", filename);
968edf72c9abb1e35ad5f41419d0399c6d9acf32Till Mossakowski target = link_find_prioritized(dev, add, dirname, buf, sizeof(buf));
f3a84cc409ed345569be6673d05072dcb4291ebeTill Mossakowski info(udev, "no reference left, remove '%s'\n", slink);
f3a84cc409ed345569be6673d05072dcb4291ebeTill Mossakowski info(udev, "creating link '%s' to '%s'\n", slink, target);
if (add) {
int err;
int fd;
if (fd >= 0)
int found;
found = 0;
if (found)
int err = 0;
goto out;
goto out;
out:
return err;
int err = 0;
const char *devnode;