udev-node.c revision 2be7287b0eb88ec7ebc3117e59554c2f6e7ac9e4
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering/*
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering * Copyright (C) 2003-2010 Kay Sievers <kay.sievers@vrfy.org>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering *
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 *
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 *
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/>.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <stdlib.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <string.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <stdio.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <stddef.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <stdbool.h>
a9cdc94f7ff40f22a3cf9472f612a80730a1b010Dave Reisner#include <fcntl.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <unistd.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <errno.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <grp.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <dirent.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <sys/time.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <sys/stat.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <sys/types.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include "udev.h"
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers#define TMP_FILE_EXT ".udev-tmp"
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sieversstatic int node_symlink(struct udev *udev, const char *node, const char *slink)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering{
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering struct stat stats;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering char target[UTIL_PATH_SIZE];
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering char *s;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering size_t l;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering char slink_tmp[UTIL_PATH_SIZE + sizeof(TMP_FILE_EXT)];
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering int i = 0;
0affed79d2e30013f07cb94e6f07e3fcb81c02faLennart Poettering int tail = 0;
0732ef7acf37473847992888bcb6446726d9d877Zbigniew Jędrzejewski-Szmek int err = 0;
7568345034f2890af745747783c5abfbf6eccf0fLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering /* use relative link */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering target[0] = '\0';
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering while (node[i] && (node[i] == slink[i])) {
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers if (node[i] == '/')
7085053a437456ab87d726f3697002dd811fdf7aDaniel Wallace tail = i+1;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering i++;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering s = target;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering l = sizeof(target);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering while (slink[i] != '\0') {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (slink[i] == '/')
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering l = util_strpcpy(&s, l, "../");
1b12a7b5896f94bdf33b3a6661ebabd761ea6adcHarald Hoyer i++;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering l = util_strscpy(s, l, &node[tail]);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (l == 0) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering err = -EINVAL;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering goto exit;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering /* preserve link with correct target, do not replace node of other device */
46e65dcc3a522b5e992e165b5e61d14254026859Lennart Poettering if (lstat(slink, &stats) == 0) {
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 goto exit;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering } else if (S_ISLNK(stats.st_mode)) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering char buf[UTIL_PATH_SIZE];
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering int len;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering len = readlink(slink, buf, sizeof(buf));
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (len > 0 && len < (int)sizeof(buf)) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering buf[len] = '\0';
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (strcmp(target, buf) == 0) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering log_debug("preserve already existing symlink '%s' to '%s'\n", slink, target);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering label_fix(slink, true, false);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering utimensat(AT_FDCWD, slink, NULL, AT_SYMLINK_NOFOLLOW);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering goto exit;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering } else {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering log_debug("creating symlink '%s' to '%s'\n", slink, target);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering do {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering err = mkdir_parents_label(slink, 0755);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (err != 0 && err != -ENOENT)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering break;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering label_context_set(slink, S_IFLNK);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering err = symlink(target, slink);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (err != 0)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering err = -errno;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering label_context_clear();
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering } while (err == -ENOENT);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (err == 0)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering goto exit;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
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 unlink(slink_tmp);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering do {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering err = mkdir_parents_label(slink_tmp, 0755);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (err != 0 && err != -ENOENT)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering break;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers label_context_set(slink_tmp, S_IFLNK);
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek err = symlink(target, slink_tmp);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers if (err != 0)
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers err = -errno;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers label_context_clear();
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers } while (err == -ENOENT);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers if (err != 0) {
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers log_error("symlink '%s' '%s' failed: %m\n", target, slink_tmp);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers goto exit;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers }
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers err = rename(slink_tmp, slink);
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers if (err != 0) {
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers log_error("rename '%s' '%s' failed: %m\n", slink_tmp, slink);
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers unlink(slink_tmp);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sieversexit:
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return err;
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers}
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers
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)
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers{
6046278f405b3ebaf84fcf17f6e834c4c4bd6a42Djalal Harouni struct udev *udev = udev_device_get_udev(dev);
6046278f405b3ebaf84fcf17f6e834c4c4bd6a42Djalal Harouni DIR *dir;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers int priority = 0;
6046278f405b3ebaf84fcf17f6e834c4c4bd6a42Djalal Harouni const char *target = NULL;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (add) {
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers priority = udev_device_get_devlink_priority(dev);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers util_strscpy(buf, bufsize, udev_device_get_devnode(dev));
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering target = buf;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering dir = opendir(stackdir);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers if (dir == NULL)
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers return target;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers for (;;) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering struct udev_device *dev_db;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering struct dirent *dent;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering dent = readdir(dir);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (dent == NULL || dent->d_name[0] == '\0')
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering break;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (dent->d_name[0] == '.')
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering continue;
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering log_debug("found '%s' claiming '%s'\n", dent->d_name, stackdir);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering /* did we find ourself? */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (strcmp(dent->d_name, udev_device_get_id_filename(dev)) == 0)
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering continue;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
94676f3e9352cbf1f72e0a512ee0d2ed83ff676dLennart Poettering dev_db = udev_device_new_from_id_filename(udev, dent->d_name);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (dev_db != NULL) {
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers const char *devnode;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
94676f3e9352cbf1f72e0a512ee0d2ed83ff676dLennart Poettering devnode = udev_device_get_devnode(dev_db);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (devnode != NULL) {
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);
c49b30a23583ff39daaa26696bcab478d2fee0bbLennart Poettering util_strscpy(buf, bufsize, devnode);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers target = buf;
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering }
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering udev_device_unref(dev_db);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering closedir(dir);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return target;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers}
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
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)
7568345034f2890af745747783c5abfbf6eccf0fLennart Poettering{
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna struct udev *udev = udev_device_get_udev(dev);
7568345034f2890af745747783c5abfbf6eccf0fLennart Poettering char name_enc[UTIL_PATH_SIZE];
7568345034f2890af745747783c5abfbf6eccf0fLennart Poettering char filename[UTIL_PATH_SIZE * 2];
7568345034f2890af745747783c5abfbf6eccf0fLennart Poettering char dirname[UTIL_PATH_SIZE];
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek const char *target;
7568345034f2890af745747783c5abfbf6eccf0fLennart Poettering char buf[UTIL_PATH_SIZE];
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
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);
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (!add && unlink(filename) == 0)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering rmdir(dirname);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers target = link_find_prioritized(dev, add, dirname, buf, sizeof(buf));
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (target == NULL) {
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering log_debug("no reference left, remove '%s'\n", slink);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (unlink(slink) == 0)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering util_delete_path(udev, slink);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering } else {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering log_debug("creating link '%s' to '%s'\n", slink, target);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering node_symlink(udev, target, slink);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (add) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering int err;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering do {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering int fd;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering err = mkdir_parents(filename, 0755);
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering if (err != 0 && err != -ENOENT)
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering break;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering fd = open(filename, O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (fd >= 0)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering close(fd);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering else
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers err = -errno;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering } while (err == -ENOENT);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers }
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering}
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poetteringvoid udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old)
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering{
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering struct udev_list_entry *list_entry;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
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 int found;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering /* check if old link name still belongs to this device */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering found = 0;
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
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (strcmp(name, name_current) == 0) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering found = 1;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering break;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (found)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering continue;
2b6bf07dd23bb467099d213c97b3875c5e453491Zbigniew Jędrzejewski-Szmek
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering log_debug("update old name, '%s' no longer belonging to '%s'\n",
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering name, udev_device_get_devpath(dev));
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering link_update(dev, name, 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringstatic int node_fixup(struct udev_device *dev, mode_t mode, uid_t uid, gid_t gid)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering{
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering const char *devnode = udev_device_get_devnode(dev);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering dev_t devnum = udev_device_get_devnum(dev);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering struct stat stats;
ef42202ac8ed27e7ff1fc90ef8bc2590046dff25Zbigniew Jędrzejewski-Szmek int err = 0;
ef42202ac8ed27e7ff1fc90ef8bc2590046dff25Zbigniew Jędrzejewski-Szmek
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (strcmp(udev_device_get_subsystem(dev), "block") == 0)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering mode |= S_IFBLK;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering else
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering mode |= S_IFCHR;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (lstat(devnode, &stats) != 0) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering err = -errno;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers log_debug("can not stat() node '%s' (%m)\n", devnode);
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer goto out;
0732ef7acf37473847992888bcb6446726d9d877Zbigniew Jędrzejewski-Szmek }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt if (((stats.st_mode & S_IFMT) != (mode & S_IFMT)) || (stats.st_rdev != devnum)) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering err = -EEXIST;
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));
0732ef7acf37473847992888bcb6446726d9d877Zbigniew Jędrzejewski-Szmek goto out;
0732ef7acf37473847992888bcb6446726d9d877Zbigniew Jędrzejewski-Szmek }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
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 chmod(devnode, mode);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering chown(devnode, uid, gid);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering } else {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering log_debug("preserve permissions %s, %#o, uid=%u, gid=%u\n", devnode, mode, uid, gid);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering /*
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 */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (strcmp(udev_device_get_action(dev), "add") == 0)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering label_fix(devnode, true, false);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
7c2d80944afb4196f2eff614e8da1450dffcbeaaThomas Hindoe Paaboel Andersen /* always update timestamp when we re-use the node, like on media change events */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering utimensat(AT_FDCWD, devnode, NULL, 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringout:
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return err;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sieversvoid udev_node_add(struct udev_device *dev, mode_t mode, uid_t uid, gid_t gid)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering{
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering struct udev *udev = udev_device_get_udev(dev);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering char filename[UTIL_PATH_SIZE];
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering struct udev_list_entry *list_entry;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
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
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (node_fixup(dev, mode, uid, gid) < 0)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
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);
6b2b6f30e38d67b032d6bdc6b47ae05e143e96c5Michal Schmidt
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)) {
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering if (udev_list_entry_get_num(list_entry))
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 else
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering link_update(dev, udev_list_entry_get_name(list_entry), 1);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sieversvoid udev_node_remove(struct udev_device *dev)
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering{
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering struct udev_list_entry *list_entry;
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering char filename[UTIL_PATH_SIZE];
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering
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);
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering
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",
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering unlink(filename);
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering}
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering