udev_node.c revision aa8734ffcb8a895fc8d66ff383cbcf8f4b78f562
0N/A/*
2362N/A * Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
0N/A *
0N/A * This program is free software: you can redistribute it and/or modify
0N/A * it under the terms of the GNU General Public License as published by
0N/A * the Free Software Foundation, either version 2 of the License, or
2362N/A * (at your option) any later version.
0N/A *
2362N/A * This program is distributed in the hope that it will be useful,
0N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
0N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0N/A * GNU General Public License for more details.
0N/A *
0N/A * You should have received a copy of the GNU General Public License
0N/A * along with this program. If not, see <http://www.gnu.org/licenses/>.
0N/A */
0N/A
0N/A#include <stdlib.h>
0N/A#include <string.h>
0N/A#include <stdio.h>
2362N/A#include <stddef.h>
2362N/A#include <fcntl.h>
2362N/A#include <unistd.h>
0N/A#include <errno.h>
0N/A#include <grp.h>
0N/A#include <dirent.h>
0N/A#include <sys/stat.h>
0N/A#include <sys/types.h>
0N/A
0N/A#include "udev.h"
0N/A#include "udev_rules.h"
0N/A
0N/A#define TMP_FILE_EXT ".udev-tmp"
0N/A
0N/A/* reverse mapping from the device file name to the devpath */
0N/Astatic int name_index(struct udev *udev, const char *devpath, const char *name, int add, int test)
0N/A{
0N/A char device[UTIL_PATH_SIZE];
0N/A char filename[UTIL_PATH_SIZE * 2];
0N/A size_t devlen = strlen(udev_get_dev_path(udev))+1;
0N/A size_t start;
0N/A int fd;
0N/A
0N/A /* directory with device name */
0N/A util_strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
0N/A start = util_strlcat(filename, "/.udev/names/", sizeof(filename));
0N/A util_strlcat(filename, &name[devlen], sizeof(filename));
0N/A util_path_encode(&filename[start], sizeof(filename) - start);
0N/A /* entry with the devpath */
0N/A util_strlcpy(device, devpath, sizeof(device));
0N/A util_path_encode(device, sizeof(device));
0N/A util_strlcat(filename, "/", sizeof(filename));
0N/A util_strlcat(filename, device, sizeof(filename));
0N/A
0N/A if (add) {
0N/A info(udev, "creating index: '%s'\n", filename);
0N/A create_path(udev, filename);
0N/A fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644);
0N/A if (fd > 0)
0N/A close(fd);
0N/A } else {
0N/A info(udev, "removing index: '%s'\n", filename);
0N/A unlink(filename);
0N/A delete_path(udev, filename);
0N/A }
0N/A return 0;
0N/A}
0N/A
0N/Aint udev_node_mknod(struct udev_device *dev, const char *file, dev_t devnum, mode_t mode, uid_t uid, gid_t gid)
0N/A{
0N/A struct udev *udev = udev_device_get_udev(dev);
0N/A char file_tmp[UTIL_PATH_SIZE + sizeof(TMP_FILE_EXT)];
0N/A struct stat stats;
0N/A int preserve = 0;
0N/A int err = 0;
0N/A
0N/A if (major(devnum) == 0)
0N/A devnum = udev_device_get_devnum(dev);
0N/A
0N/A if (strcmp(udev_device_get_subsystem(dev), "block") == 0)
0N/A mode |= S_IFBLK;
0N/A else
0N/A mode |= S_IFCHR;
0N/A
0N/A if (file == NULL)
0N/A file = udev_device_get_devnode(dev);
0N/A
0N/A if (lstat(file, &stats) == 0) {
0N/A if (((stats.st_mode & S_IFMT) == (mode & S_IFMT)) && (stats.st_rdev == devnum)) {
0N/A info(udev, "preserve file '%s', because it has correct dev_t\n", file);
0N/A preserve = 1;
0N/A udev_selinux_lsetfilecon(udev, file, mode);
0N/A } else {
0N/A info(udev, "atomically replace existing file '%s'\n", file);
0N/A util_strlcpy(file_tmp, file, sizeof(file_tmp));
0N/A util_strlcat(file_tmp, TMP_FILE_EXT, sizeof(file_tmp));
0N/A unlink(file_tmp);
0N/A udev_selinux_setfscreatecon(udev, file_tmp, mode);
0N/A err = mknod(file_tmp, mode, devnum);
0N/A udev_selinux_resetfscreatecon(udev);
0N/A if (err != 0) {
0N/A err(udev, "mknod(%s, %#o, %u, %u) failed: %m\n",
0N/A file_tmp, mode, major(devnum), minor(devnum));
0N/A goto exit;
0N/A }
0N/A err = rename(file_tmp, file);
0N/A if (err != 0) {
0N/A err(udev, "rename(%s, %s) failed: %m\n", file_tmp, file);
0N/A unlink(file_tmp);
0N/A }
0N/A }
0N/A } else {
0N/A info(udev, "mknod(%s, %#o, (%u,%u))\n", file, mode, major(devnum), minor(devnum));
0N/A udev_selinux_setfscreatecon(udev, file, mode);
0N/A err = mknod(file, mode, devnum);
0N/A udev_selinux_resetfscreatecon(udev);
0N/A if (err != 0) {
0N/A err(udev, "mknod(%s, %#o, (%u,%u) failed: %m\n", file, mode, major(devnum), minor(devnum));
0N/A goto exit;
0N/A }
0N/A }
0N/A
0N/A if (!preserve || stats.st_mode != mode) {
0N/A info(udev, "chmod(%s, %#o)\n", file, mode);
0N/A err = chmod(file, mode);
0N/A if (err != 0) {
0N/A err(udev, "chmod(%s, %#o) failed: %m\n", file, mode);
0N/A goto exit;
0N/A }
0N/A }
0N/A
0N/A if (!preserve || stats.st_uid != uid || stats.st_gid != gid) {
0N/A info(udev, "chown(%s, %u, %u)\n", file, uid, gid);
0N/A err = chown(file, uid, gid);
0N/A if (err != 0) {
0N/A err(udev, "chown(%s, %u, %u) failed: %m\n", file, uid, gid);
0N/A goto exit;
0N/A }
0N/A }
0N/Aexit:
0N/A return err;
0N/A}
0N/A
0N/Astatic int node_symlink(struct udev *udev, const char *node, const char *slink)
0N/A{
0N/A struct stat stats;
0N/A char target[UTIL_PATH_SIZE] = "";
0N/A char slink_tmp[UTIL_PATH_SIZE + sizeof(TMP_FILE_EXT)];
0N/A int i = 0;
0N/A int tail = 0;
0N/A int len;
0N/A int err = 0;
0N/A
0N/A /* use relative link */
0N/A while (node[i] && (node[i] == slink[i])) {
0N/A if (node[i] == '/')
0N/A tail = i+1;
0N/A i++;
0N/A }
0N/A while (slink[i] != '\0') {
0N/A if (slink[i] == '/')
0N/A util_strlcat(target, "../", sizeof(target));
0N/A i++;
0N/A }
0N/A util_strlcat(target, &node[tail], sizeof(target));
0N/A
0N/A /* preserve link with correct target, do not replace node of other device */
0N/A if (lstat(slink, &stats) == 0) {
0N/A if (S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode)) {
0N/A struct stat stats2;
0N/A
0N/A info(udev, "found existing node instead of symlink '%s'\n", slink);
0N/A if (lstat(node, &stats2) == 0) {
0N/A if ((stats.st_mode & S_IFMT) == (stats2.st_mode & S_IFMT) &&
0N/A stats.st_rdev == stats2.st_rdev) {
0N/A info(udev, "replace device node '%s' with symlink to our node '%s'\n",
0N/A slink, node);
0N/A } else {
0N/A err(udev, "device node '%s' already exists, "
0N/A "link to '%s' will not overwrite it\n",
0N/A slink, node);
0N/A goto exit;
0N/A }
0N/A }
0N/A } else if (S_ISLNK(stats.st_mode)) {
0N/A char buf[UTIL_PATH_SIZE];
0N/A
0N/A info(udev, "found existing symlink '%s'\n", slink);
0N/A len = readlink(slink, buf, sizeof(buf));
0N/A if (len > 0) {
0N/A buf[len] = '\0';
0N/A if (strcmp(target, buf) == 0) {
0N/A info(udev, "preserve already existing symlink '%s' to '%s'\n",
0N/A slink, target);
0N/A udev_selinux_lsetfilecon(udev, slink, S_IFLNK);
0N/A goto exit;
0N/A }
0N/A }
0N/A }
0N/A } else {
0N/A info(udev, "creating symlink '%s' to '%s'\n", slink, target);
0N/A udev_selinux_setfscreatecon(udev, slink, S_IFLNK);
0N/A err = symlink(target, slink);
0N/A udev_selinux_resetfscreatecon(udev);
0N/A if (err == 0)
0N/A goto exit;
0N/A }
0N/A
0N/A info(udev, "atomically replace '%s'\n", slink);
0N/A util_strlcpy(slink_tmp, slink, sizeof(slink_tmp));
0N/A util_strlcat(slink_tmp, TMP_FILE_EXT, sizeof(slink_tmp));
0N/A unlink(slink_tmp);
0N/A udev_selinux_setfscreatecon(udev, slink, S_IFLNK);
0N/A err = symlink(target, slink_tmp);
0N/A udev_selinux_resetfscreatecon(udev);
0N/A if (err != 0) {
0N/A err(udev, "symlink(%s, %s) failed: %m\n", target, slink_tmp);
0N/A goto exit;
0N/A }
0N/A err = rename(slink_tmp, slink);
0N/A if (err != 0) {
0N/A err(udev, "rename(%s, %s) failed: %m\n", slink_tmp, slink);
0N/A unlink(slink_tmp);
0N/A goto exit;
0N/A }
0N/Aexit:
0N/A return err;
0N/A}
0N/A
0N/Astatic int get_devices_by_name(struct udev *udev, const char *name, struct list_head *name_list)
0N/A{
0N/A char dirname[PATH_MAX];
0N/A size_t devlen = strlen(udev_get_dev_path(udev))+1;
0N/A size_t start;
0N/A DIR *dir;
0N/A int count = 0;
0N/A
0N/A util_strlcpy(dirname, udev_get_dev_path(udev), sizeof(dirname));
0N/A start = util_strlcat(dirname, "/.udev/names/", sizeof(dirname));
0N/A util_strlcat(dirname, &name[devlen], sizeof(dirname));
0N/A util_path_encode(&dirname[start], sizeof(dirname) - start);
0N/A
0N/A dir = opendir(dirname);
0N/A if (dir == NULL) {
0N/A info(udev, "no index directory '%s': %m\n", dirname);
0N/A count = -1;
0N/A goto out;
0N/A }
0N/A
0N/A info(udev, "found index directory '%s'\n", dirname);
0N/A while (1) {
0N/A struct dirent *ent;
0N/A char device[UTIL_PATH_SIZE];
0N/A
0N/A ent = readdir(dir);
0N/A if (ent == NULL || ent->d_name[0] == '\0')
0N/A break;
0N/A if (ent->d_name[0] == '.')
0N/A continue;
0N/A
0N/A util_strlcpy(device, udev_get_sys_path(udev), sizeof(device));
0N/A util_strlcat(device, ent->d_name, sizeof(device));
0N/A util_path_decode(device);
0N/A name_list_add(udev, name_list, device, 0);
0N/A count++;
0N/A }
0N/A closedir(dir);
0N/Aout:
0N/A return count;
0N/A}
0N/A
0N/Astatic int update_link(struct udev_device *dev, const char *slink, int test)
0N/A{
0N/A struct udev *udev = udev_device_get_udev(dev);
0N/A LIST_HEAD(name_list);
0N/A struct name_entry *device;
0N/A char target[UTIL_PATH_SIZE] = "";
0N/A int count;
0N/A int priority = 0;
0N/A int rc = 0;
0N/A
0N/A info(udev, "update symlink '%s' of '%s'\n", slink, udev_device_get_syspath(dev));
0N/A
0N/A count = get_devices_by_name(udev, slink, &name_list);
0N/A info(udev, "found %i devices with name '%s'\n", count, slink);
0N/A
0N/A /* if we don't have a reference, delete it */
0N/A if (count <= 0) {
0N/A info(udev, "no reference left, remove '%s'\n", slink);
0N/A if (!test) {
0N/A unlink(slink);
0N/A delete_path(udev, slink);
0N/A }
0N/A goto out;
0N/A }
0N/A
0N/A /* find the device with the highest priority */
0N/A list_for_each_entry(device, &name_list, node) {
0N/A struct udev_device *dev_db;
0N/A const char *devnode;
0N/A
0N/A info(udev, "found '%s' for '%s'\n", device->name, slink);
0N/A
0N/A /* did we find ourself? we win, if we have the same priority */
0N/A if (strcmp(udev_device_get_syspath(dev), device->name) == 0) {
0N/A info(udev, "compare (our own) priority of '%s' %i >= %i\n",
0N/A udev_device_get_devpath(dev), udev_device_get_devlink_priority(dev), priority);
0N/A if (strcmp(udev_device_get_devnode(dev), slink) == 0) {
0N/A info(udev, "'%s' is our device node, database inconsistent, skip link update\n",
0N/A udev_device_get_devnode(dev));
0N/A } else if (target[0] == '\0' || udev_device_get_devlink_priority(dev) >= priority) {
0N/A priority = udev_device_get_devlink_priority(dev);
0N/A util_strlcpy(target, udev_device_get_devnode(dev), sizeof(target));
0N/A }
0N/A continue;
0N/A }
0N/A
0N/A /* another device, read priority from database */
0N/A dev_db = udev_device_new_from_syspath(udev, device->name);
0N/A if (dev_db == NULL)
0N/A continue;
0N/A devnode = udev_device_get_devnode(dev_db);
0N/A if (devnode != NULL) {
0N/A if (strcmp(devnode, slink) == 0) {
0N/A info(udev, "'%s' is a device node of '%s', skip link update\n",
0N/A devnode, device->name);
0N/A } else {
0N/A info(udev, "compare priority of '%s' %i > %i\n",
0N/A udev_device_get_devpath(dev_db),
0N/A udev_device_get_devlink_priority(dev_db),
0N/A priority);
0N/A if (target[0] == '\0' || udev_device_get_devlink_priority(dev_db) > priority) {
0N/A priority = udev_device_get_devlink_priority(dev_db);
0N/A util_strlcpy(target, devnode, sizeof(target));
0N/A }
0N/A }
0N/A }
0N/A udev_device_unref(dev_db);
0N/A }
0N/A name_list_cleanup(udev, &name_list);
0N/A
0N/A if (target[0] == '\0') {
0N/A info(udev, "no current target for '%s' found\n", slink);
0N/A rc = 1;
0N/A goto out;
0N/A }
0N/A
0N/A /* create symlink to the target with the highest priority */
0N/A info(udev, "'%s' with target '%s' has the highest priority %i, create it\n", slink, target, priority);
0N/A if (!test) {
0N/A create_path(udev, slink);
0N/A node_symlink(udev, target, slink);
0N/A }
0N/Aout:
0N/A return rc;
0N/A}
0N/A
0N/Avoid udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old, int test)
0N/A{
0N/A struct udev *udev = udev_device_get_udev(dev);
0N/A struct udev_list_entry *list_entry;
0N/A const char *devnode_old;
0N/A
0N/A /* update possible left-over symlinks */
0N/A udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev_old)) {
0N/A struct udev_list_entry *list_entry_current;
0N/A
0N/A udev_list_entry_foreach(list_entry_current, udev_device_get_devlinks_list_entry(dev)) {
0N/A if (strcmp(udev_list_entry_get_name(list_entry_current),
0N/A udev_list_entry_get_name(list_entry)) == 0)
0N/A continue;
0N/A }
0N/A /* link does no longer belong to this device */
0N/A info(udev, "update old symlink '%s' no longer belonging to '%s'\n",
0N/A udev_list_entry_get_name(list_entry), udev_device_get_devpath(dev));
0N/A update_link(dev, udev_list_entry_get_name(list_entry), test);
0N/A }
0N/A
0N/A /*
0N/A * if the node name has changed, delete the node,
0N/A * and possibly restore a symlink of another device
0N/A */
0N/A devnode_old = udev_device_get_devnode(dev_old);
0N/A if (devnode_old != NULL) {
0N/A const char *devnode = udev_device_get_devnode(dev);
0N/A
0N/A if (devnode != NULL && strcmp(devnode_old, devnode) != 0)
0N/A update_link(dev, devnode_old, test);
0N/A }
0N/A}
0N/A
0N/Aint udev_node_add(struct udev_device *dev, mode_t mode, const char *owner, const char *group, int test)
0N/A{
0N/A struct udev *udev = udev_device_get_udev(dev);
0N/A uid_t uid;
0N/A gid_t gid;
0N/A int i;
0N/A int num;
0N/A struct udev_list_entry *list_entry;
0N/A int err = 0;
0N/A
0N/A create_path(udev, udev_device_get_devnode(dev));
0N/A
0N/A if (strcmp(owner, "root") == 0)
0N/A uid = 0;
0N/A else {
0N/A char *endptr;
0N/A unsigned long id;
0N/A
0N/A id = strtoul(owner, &endptr, 10);
0N/A if (endptr[0] == '\0')
0N/A uid = (uid_t) id;
0N/A else
0N/A uid = lookup_user(udev, owner);
0N/A }
0N/A
0N/A if (strcmp(group, "root") == 0)
0N/A gid = 0;
0N/A else {
0N/A char *endptr;
0N/A unsigned long id;
0N/A
0N/A id = strtoul(group, &endptr, 10);
0N/A if (endptr[0] == '\0')
0N/A gid = (gid_t) id;
0N/A else
0N/A gid = lookup_group(udev, group);
0N/A }
0N/A
0N/A info(udev, "creating device node '%s', devnum=%d:%d, mode=%#o, uid=%d, gid=%d\n",
0N/A udev_device_get_devnode(dev),
0N/A major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)),
0N/A mode, uid, gid);
0N/A
0N/A if (!test)
0N/A if (udev_node_mknod(dev, NULL, makedev(0,0), mode, uid, gid) != 0) {
0N/A err = -1;
0N/A goto exit;
0N/A }
0N/A
0N/A /* create all_partitions if requested */
0N/A num = udev_device_get_num_fake_partitions(dev);
0N/A if (num > 0) {
0N/A info(udev, "creating device partition nodes '%s[1-%i]'\n", udev_device_get_devnode(dev), num);
0N/A if (!test) {
0N/A for (i = 1; i <= num; i++) {
0N/A char partitionname[UTIL_PATH_SIZE];
0N/A dev_t part_devnum;
0N/A
0N/A snprintf(partitionname, sizeof(partitionname), "%s%d",
0N/A udev_device_get_devnode(dev), i);
0N/A partitionname[sizeof(partitionname)-1] = '\0';
0N/A part_devnum = makedev(major(udev_device_get_devnum(dev)),
0N/A minor(udev_device_get_devnum(dev)) + i);
0N/A udev_node_mknod(dev, partitionname, part_devnum, mode, uid, gid);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /* add node and to name index */
0N/A name_index(udev, udev_device_get_devpath(dev), udev_device_get_devnode(dev), 1, test);
0N/A
0N/A /* create/update symlinks, add symlinks to name index */
0N/A udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev)) {
0N/A name_index(udev, udev_device_get_devpath(dev), udev_list_entry_get_name(list_entry), 1, test);
0N/A update_link(dev, udev_list_entry_get_name(list_entry), test);
0N/A }
0N/Aexit:
0N/A return err;
0N/A}
0N/A
0N/Aextern int udev_node_remove(struct udev_device *dev, int test)
0N/A{
0N/A struct udev *udev = udev_device_get_udev(dev);
0N/A struct udev_list_entry *list_entry;
0N/A const char *devnode;
0N/A char partitionname[UTIL_PATH_SIZE];
0N/A struct stat stats;
0N/A int err = 0;
0N/A int num;
0N/A
0N/A /* remove node from name index */
0N/A name_index(udev, udev_device_get_devpath(dev), udev_device_get_devnode(dev), 0, test);
0N/A
0N/A /* remove,update symlinks, remove symlinks from name index */
0N/A udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev)) {
0N/A name_index(udev, udev_device_get_devpath(dev), udev_list_entry_get_name(list_entry), 0, test);
0N/A update_link(dev, udev_list_entry_get_name(list_entry), test);
0N/A }
0N/A
0N/A devnode = udev_device_get_devnode(dev);
0N/A if (devnode == NULL)
0N/A return 0;
0N/A if (stat(devnode, &stats) != 0) {
0N/A info(udev, "device node '%s' not found\n", devnode);
0N/A return 0;
0N/A }
0N/A if (stats.st_rdev != udev_device_get_devnum(dev)) {
0N/A info(udev, "device node '%s' points to a different device, skip removal\n", devnode);
0N/A return -1;
0N/A }
0N/A
0N/A info(udev, "removing device node '%s'\n", devnode);
0N/A if (!test)
0N/A err = unlink_secure(udev, devnode);
0N/A if (err)
0N/A return err;
0N/A
0N/A num = udev_device_get_num_fake_partitions(dev);
0N/A if (num > 0) {
0N/A int i;
0N/A
0N/A info(udev, "removing all_partitions '%s[1-%i]'\n", devnode, num);
0N/A if (num > 255)
0N/A return -1;
0N/A for (i = 1; i <= num; i++) {
0N/A snprintf(partitionname, sizeof(partitionname), "%s%d", devnode, i);
0N/A partitionname[sizeof(partitionname)-1] = '\0';
0N/A if (!test)
0N/A unlink_secure(udev, partitionname);
0N/A }
0N/A }
0N/A delete_path(udev, devnode);
0N/A return err;
0N/A}
0N/A