udev-node.c revision 2be7287b0eb88ec7ebc3117e59554c2f6e7ac9e4
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering * Copyright (C) 2003-2010 Kay Sievers <kay.sievers@vrfy.org>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering * This program is free software: you can redistribute it and/or modify
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering * it under the terms of the GNU General Public License as published by
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering * the Free Software Foundation, either version 2 of the License, or
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers * (at your option) any later version.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering * This program is distributed in the hope that it will be useful,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering * but WITHOUT ANY WARRANTY; without even the implied warranty of
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering * GNU General Public License for more details.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering * You should have received a copy of the GNU General Public License
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering * along with this program. If not, see <http://www.gnu.org/licenses/>.
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sieversstatic int node_symlink(struct udev *udev, const char *node, const char *slink)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering char slink_tmp[UTIL_PATH_SIZE + sizeof(TMP_FILE_EXT)];
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering /* use relative link */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering while (node[i] && (node[i] == slink[i])) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering /* preserve link with correct target, do not replace node of other device */
46e65dcc3a522b5e992e165b5e61d14254026859Lennart Poettering if (S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode)) {
46e65dcc3a522b5e992e165b5e61d14254026859Lennart Poettering log_error("conflicting device node '%s' found, link to '%s' will not be created\n", slink, node);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering log_debug("preserve already existing symlink '%s' to '%s'\n", slink, target);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering utimensat(AT_FDCWD, slink, NULL, AT_SYMLINK_NOFOLLOW);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering log_debug("creating symlink '%s' to '%s'\n", slink, target);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering log_debug("atomically replace '%s'\n", slink);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering util_strscpyl(slink_tmp, sizeof(slink_tmp), slink, TMP_FILE_EXT, NULL);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering err = mkdir_parents_label(slink_tmp, 0755);
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek err = symlink(target, slink_tmp);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers log_error("symlink '%s' '%s' failed: %m\n", target, slink_tmp);
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers log_error("rename '%s' '%s' failed: %m\n", slink_tmp, slink);
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers/* find device node of device with highest priority */
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sieversstatic const char *link_find_prioritized(struct udev_device *dev, bool add, const char *stackdir, char *buf, size_t bufsize)
6046278f405b3ebaf84fcf17f6e834c4c4bd6a42Djalal Harouni struct udev *udev = udev_device_get_udev(dev);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers priority = udev_device_get_devlink_priority(dev);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers util_strscpy(buf, bufsize, udev_device_get_devnode(dev));
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (dent == NULL || dent->d_name[0] == '\0')
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering log_debug("found '%s' claiming '%s'\n", dent->d_name, stackdir);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering /* did we find ourself? */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (strcmp(dent->d_name, udev_device_get_id_filename(dev)) == 0)
94676f3e9352cbf1f72e0a512ee0d2ed83ff676dLennart Poettering dev_db = udev_device_new_from_id_filename(udev, dent->d_name);
94676f3e9352cbf1f72e0a512ee0d2ed83ff676dLennart Poettering devnode = udev_device_get_devnode(dev_db);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers if (target == NULL || udev_device_get_devlink_priority(dev_db) > priority) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering log_debug("'%s' claims priority %i for '%s'\n",
94676f3e9352cbf1f72e0a512ee0d2ed83ff676dLennart Poettering udev_device_get_syspath(dev_db), udev_device_get_devlink_priority(dev_db), stackdir);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering priority = udev_device_get_devlink_priority(dev_db);
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna/* manage "stack of names" with possibly specified device priorities */
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagnastatic void link_update(struct udev_device *dev, const char *slink, bool add)
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna struct udev *udev = udev_device_get_udev(dev);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering util_path_encode(slink + strlen(TEST_PREFIX "/dev"), name_enc, sizeof(name_enc));
7c2d80944afb4196f2eff614e8da1450dffcbeaaThomas Hindoe Paaboel Andersen util_strscpyl(dirname, sizeof(dirname), TEST_PREFIX "/run/udev/links/", name_enc, NULL);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering util_strscpyl(filename, sizeof(filename), dirname, "/", udev_device_get_id_filename(dev), NULL);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers target = link_find_prioritized(dev, add, dirname, buf, sizeof(buf));
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering log_debug("no reference left, remove '%s'\n", slink);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering log_debug("creating link '%s' to '%s'\n", slink, target);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering fd = open(filename, O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444);
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poetteringvoid udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering /* update possible left-over symlinks */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev_old)) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering const char *name = udev_list_entry_get_name(list_entry);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering struct udev_list_entry *list_entry_current;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering /* check if old link name still belongs to this device */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering udev_list_entry_foreach(list_entry_current, udev_device_get_devlinks_list_entry(dev)) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering const char *name_current = udev_list_entry_get_name(list_entry_current);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering log_debug("update old name, '%s' no longer belonging to '%s'\n",
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringstatic int node_fixup(struct udev_device *dev, mode_t mode, uid_t uid, gid_t gid)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering const char *devnode = udev_device_get_devnode(dev);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering dev_t devnum = udev_device_get_devnum(dev);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (strcmp(udev_device_get_subsystem(dev), "block") == 0)
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers log_debug("can not stat() node '%s' (%m)\n", devnode);
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt if (((stats.st_mode & S_IFMT) != (mode & S_IFMT)) || (stats.st_rdev != devnum)) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering log_debug("found node '%s' with non-matching devnum %s, skip handling\n",
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering udev_device_get_devnode(dev), udev_device_get_id_filename(dev));
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if ((stats.st_mode & 0777) != (mode & 0777) || stats.st_uid != uid || stats.st_gid != gid) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering log_debug("set permissions %s, %#o, uid=%u, gid=%u\n", devnode, mode, uid, gid);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering log_debug("preserve permissions %s, %#o, uid=%u, gid=%u\n", devnode, mode, uid, gid);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering * Set initial selinux file context only on add events.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering * We set the proper context on bootup (triger) or for newly
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering * added devices, but we don't change it later, in case
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering * something else has set a custom context in the meantime.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (strcmp(udev_device_get_action(dev), "add") == 0)
7c2d80944afb4196f2eff614e8da1450dffcbeaaThomas Hindoe Paaboel Andersen /* always update timestamp when we re-use the node, like on media change events */
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sieversvoid udev_node_add(struct udev_device *dev, mode_t mode, uid_t uid, gid_t gid)
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering struct udev *udev = udev_device_get_udev(dev);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering log_debug("handling device node '%s', devnum=%s, mode=%#o, uid=%d, gid=%d\n",
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering udev_device_get_devnode(dev), udev_device_get_id_filename(dev), mode, uid, gid);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering /* always add /dev/{block,char}/$major:$minor */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering snprintf(filename, sizeof(filename), TEST_PREFIX "/dev/%s/%u:%u",
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering strcmp(udev_device_get_subsystem(dev), "block") == 0 ? "block" : "char",
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering node_symlink(udev, udev_device_get_devnode(dev), filename);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering /* create/update symlinks, add symlinks to name index */
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev)) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering /* simple unmanaged link name */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering node_symlink(udev, udev_device_get_devnode(dev), udev_list_entry_get_name(list_entry));
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering link_update(dev, udev_list_entry_get_name(list_entry), 1);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering /* remove/update symlinks, remove symlinks from name index */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers link_update(dev, udev_list_entry_get_name(list_entry), 0);
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer /* remove /dev/{block,char}/$major:$minor */
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering snprintf(filename, sizeof(filename), TEST_PREFIX "/dev/%s/%u:%u",
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering strcmp(udev_device_get_subsystem(dev), "block") == 0 ? "block" : "char",