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>
31 #include "udev_rules.h"
33 #define TMP_FILE_EXT ".udev-tmp"
35 int udev_node_mknod(struct udevice *udevice, const char *file, dev_t devt, mode_t mode, uid_t uid, gid_t gid)
37 char file_tmp[PATH_SIZE + sizeof(TMP_FILE_EXT)];
42 if (major(devt) != 0 && strcmp(udevice->dev->subsystem, "block") == 0)
47 if (lstat(file, &stats) == 0) {
48 if (((stats.st_mode & S_IFMT) == (mode & S_IFMT)) && (stats.st_rdev == devt)) {
49 info(udevice->udev, "preserve file '%s', because it has correct dev_t\n", file);
51 udev_selinux_lsetfilecon(udevice->udev, file, mode);
53 info(udevice->udev, "atomically replace existing file '%s'\n", file);
54 util_strlcpy(file_tmp, file, sizeof(file_tmp));
55 util_strlcat(file_tmp, TMP_FILE_EXT, sizeof(file_tmp));
57 udev_selinux_setfscreatecon(udevice->udev, file_tmp, mode);
58 err = mknod(file_tmp, mode, devt);
59 udev_selinux_resetfscreatecon(udevice->udev);
61 err(udevice->udev, "mknod(%s, %#o, %u, %u) failed: %s\n",
62 file_tmp, mode, major(devt), minor(devt), strerror(errno));
65 err = rename(file_tmp, file);
67 err(udevice->udev, "rename(%s, %s) failed: %s\n", file_tmp, file, strerror(errno));
72 info(udevice->udev, "mknod(%s, %#o, (%u,%u))\n", file, mode, major(devt), minor(devt));
73 udev_selinux_setfscreatecon(udevice->udev, file, mode);
74 err = mknod(file, mode, devt);
75 udev_selinux_resetfscreatecon(udevice->udev);
77 err(udevice->udev, "mknod(%s, %#o, (%u,%u) failed: %s\n",
78 file, mode, major(devt), minor(devt), strerror(errno));
83 if (!preserve || stats.st_mode != mode) {
84 info(udevice->udev, "chmod(%s, %#o)\n", file, mode);
85 err = chmod(file, mode);
87 err(udevice->udev, "chmod(%s, %#o) failed: %s\n", file, mode, strerror(errno));
92 if (!preserve || stats.st_uid != uid || stats.st_gid != gid) {
93 info(udevice->udev, "chown(%s, %u, %u)\n", file, uid, gid);
94 err = chown(file, uid, gid);
96 err(udevice->udev, "chown(%s, %u, %u) failed: %s\n", file, uid, gid, strerror(errno));
104 static int node_symlink(struct udevice *udevice, const char *node, const char *slink)
107 char target[PATH_SIZE] = "";
108 char slink_tmp[PATH_SIZE + sizeof(TMP_FILE_EXT)];
114 /* use relative link */
115 while (node[i] && (node[i] == slink[i])) {
120 while (slink[i] != '\0') {
122 util_strlcat(target, "../", sizeof(target));
125 util_strlcat(target, &node[tail], sizeof(target));
127 /* preserve link with correct target, do not replace node of other device */
128 if (lstat(slink, &stats) == 0) {
129 if (S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode)) {
132 info(udevice->udev, "found existing node instead of symlink '%s'\n", slink);
133 if (lstat(node, &stats2) == 0) {
134 if ((stats.st_mode & S_IFMT) == (stats2.st_mode & S_IFMT) &&
135 stats.st_rdev == stats2.st_rdev) {
136 info(udevice->udev, "replace device node '%s' with symlink to our node '%s'\n", slink, node);
138 err(udevice->udev, "device node '%s' already exists, link to '%s' will not overwrite it\n", slink, node);
142 } else if (S_ISLNK(stats.st_mode)) {
145 info(udevice->udev, "found existing symlink '%s'\n", slink);
146 len = readlink(slink, buf, sizeof(buf));
149 if (strcmp(target, buf) == 0) {
150 info(udevice->udev, "preserve already existing symlink '%s' to '%s'\n", slink, target);
151 udev_selinux_lsetfilecon(udevice->udev, slink, S_IFLNK);
157 info(udevice->udev, "creating symlink '%s' to '%s'\n", slink, target);
158 udev_selinux_setfscreatecon(udevice->udev, slink, S_IFLNK);
159 retval = symlink(target, slink);
160 udev_selinux_resetfscreatecon(udevice->udev);
165 info(udevice->udev, "atomically replace '%s'\n", slink);
166 util_strlcpy(slink_tmp, slink, sizeof(slink_tmp));
167 util_strlcat(slink_tmp, TMP_FILE_EXT, sizeof(slink_tmp));
169 udev_selinux_setfscreatecon(udevice->udev, slink, S_IFLNK);
170 retval = symlink(target, slink_tmp);
171 udev_selinux_resetfscreatecon(udevice->udev);
173 err(udevice->udev, "symlink(%s, %s) failed: %s\n", target, slink_tmp, strerror(errno));
176 retval = rename(slink_tmp, slink);
178 err(udevice->udev, "rename(%s, %s) failed: %s\n", slink_tmp, slink, strerror(errno));
186 static int update_link(struct udevice *udevice, const char *name)
188 LIST_HEAD(name_list);
189 char slink[PATH_SIZE];
190 char node[PATH_SIZE];
191 struct udevice *udevice_db;
192 struct name_entry *device;
193 char target[PATH_MAX] = "";
198 util_strlcpy(slink, udev_get_dev_path(udevice->udev), sizeof(slink));
199 util_strlcat(slink, "/", sizeof(slink));
200 util_strlcat(slink, name, sizeof(slink));
202 count = udev_db_get_devices_by_name(udevice->udev, name, &name_list);
203 info(udevice->udev, "found %i devices with name '%s'\n", count, name);
205 /* if we don't have a reference, delete it */
207 info(udevice->udev, "no reference left, remove '%s'\n", name);
208 if (!udevice->test_run) {
210 delete_path(udevice->udev, slink);
215 /* find the device with the highest priority */
216 list_for_each_entry(device, &name_list, node) {
217 info(udevice->udev, "found '%s' for '%s'\n", device->name, name);
219 /* did we find ourself? we win, if we have the same priority */
220 if (strcmp(udevice->dev->devpath, device->name) == 0) {
221 info(udevice->udev, "compare (our own) priority of '%s' %i >= %i\n",
222 udevice->dev->devpath, udevice->link_priority, priority);
223 if (strcmp(udevice->name, name) == 0) {
224 info(udevice->udev, "'%s' is our device node, database inconsistent, skip link update\n", udevice->name);
225 } else if (target[0] == '\0' || udevice->link_priority >= priority) {
226 priority = udevice->link_priority;
227 util_strlcpy(target, udevice->name, sizeof(target));
232 /* another device, read priority from database */
233 udevice_db = udev_device_init(udevice->udev);
234 if (udevice_db == NULL)
236 if (udev_db_get_device(udevice_db, device->name) == 0) {
237 if (strcmp(udevice_db->name, name) == 0) {
238 info(udevice->udev, "'%s' is a device node of '%s', skip link update\n", udevice_db->name, device->name);
240 info(udevice->udev, "compare priority of '%s' %i > %i\n",
241 udevice_db->dev->devpath, udevice_db->link_priority, priority);
242 if (target[0] == '\0' || udevice_db->link_priority > priority) {
243 priority = udevice_db->link_priority;
244 util_strlcpy(target, udevice_db->name, sizeof(target));
248 udev_device_cleanup(udevice_db);
250 name_list_cleanup(udevice->udev, &name_list);
252 if (target[0] == '\0') {
253 info(udevice->udev, "no current target for '%s' found\n", name);
258 /* create symlink to the target with the highest priority */
259 util_strlcpy(node, udev_get_dev_path(udevice->udev), sizeof(node));
260 util_strlcat(node, "/", sizeof(node));
261 util_strlcat(node, target, sizeof(node));
262 info(udevice->udev, "'%s' with target '%s' has the highest priority %i, create it\n", name, target, priority);
263 if (!udevice->test_run) {
264 create_path(udevice->udev, slink);
265 node_symlink(udevice, node, slink);
271 void udev_node_update_symlinks(struct udevice *udevice, struct udevice *udevice_old)
273 struct name_entry *name_loop;
274 char symlinks[PATH_SIZE] = "";
276 list_for_each_entry(name_loop, &udevice->symlink_list, node) {
277 info(udevice->udev, "update symlink '%s' of '%s'\n", name_loop->name, udevice->dev->devpath);
278 update_link(udevice, name_loop->name);
279 util_strlcat(symlinks, udev_get_dev_path(udevice->udev), sizeof(symlinks));
280 util_strlcat(symlinks, "/", sizeof(symlinks));
281 util_strlcat(symlinks, name_loop->name, sizeof(symlinks));
282 util_strlcat(symlinks, " ", sizeof(symlinks));
285 /* export symlinks to environment */
286 util_remove_trailing_chars(symlinks, ' ');
287 if (symlinks[0] != '\0')
288 setenv("DEVLINKS", symlinks, 1);
290 /* update possible left-over symlinks (device metadata changed) */
291 if (udevice_old != NULL) {
292 struct name_entry *link_loop;
293 struct name_entry *link_old_loop;
296 /* remove current symlinks from old list */
297 list_for_each_entry(link_old_loop, &udevice_old->symlink_list, node) {
299 list_for_each_entry(link_loop, &udevice->symlink_list, node) {
300 if (strcmp(link_old_loop->name, link_loop->name) == 0) {
306 /* link does no longer belong to this device */
307 info(udevice->udev, "update old symlink '%s' no longer belonging to '%s'\n",
308 link_old_loop->name, udevice->dev->devpath);
309 update_link(udevice, link_old_loop->name);
314 * if the node name has changed, delete the node,
315 * or possibly restore a symlink of another device
317 if (strcmp(udevice->name, udevice_old->name) != 0)
318 update_link(udevice, udevice_old->name);
322 int udev_node_add(struct udevice *udevice)
324 char filename[PATH_SIZE];
330 util_strlcpy(filename, udev_get_dev_path(udevice->udev), sizeof(filename));
331 util_strlcat(filename, "/", sizeof(filename));
332 util_strlcat(filename, udevice->name, sizeof(filename));
333 create_path(udevice->udev, filename);
335 if (strcmp(udevice->owner, "root") == 0)
341 id = strtoul(udevice->owner, &endptr, 10);
342 if (endptr[0] == '\0')
345 uid = lookup_user(udevice->udev, udevice->owner);
348 if (strcmp(udevice->group, "root") == 0)
354 id = strtoul(udevice->group, &endptr, 10);
355 if (endptr[0] == '\0')
358 gid = lookup_group(udevice->udev, udevice->group);
361 info(udevice->udev, "creating device node '%s', major=%d, minor=%d, mode=%#o, uid=%d, gid=%d\n",
362 filename, major(udevice->devt), minor(udevice->devt), udevice->mode, uid, gid);
364 if (!udevice->test_run)
365 if (udev_node_mknod(udevice, filename, udevice->devt, udevice->mode, uid, gid) != 0) {
370 setenv("DEVNAME", filename, 1);
372 /* create all_partitions if requested */
373 if (udevice->partitions) {
374 char partitionname[PATH_SIZE];
378 /* take the maximum registered minor range */
379 attr = sysfs_attr_get_value(udevice->udev, udevice->dev->devpath, "range");
383 udevice->partitions = range-1;
385 info(udevice->udev, "creating device partition nodes '%s[1-%i]'\n", filename, udevice->partitions);
386 if (!udevice->test_run) {
387 for (i = 1; i <= udevice->partitions; i++) {
390 snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
391 partitionname[sizeof(partitionname)-1] = '\0';
392 part_devt = makedev(major(udevice->devt), minor(udevice->devt) + i);
393 udev_node_mknod(udevice, partitionname, part_devt, udevice->mode, uid, gid);
401 int udev_node_remove(struct udevice *udevice)
403 char filename[PATH_SIZE];
404 char partitionname[PATH_SIZE];
409 util_strlcpy(filename, udev_get_dev_path(udevice->udev), sizeof(filename));
410 util_strlcat(filename, "/", sizeof(filename));
411 util_strlcat(filename, udevice->name, sizeof(filename));
412 if (stat(filename, &stats) != 0) {
413 info(udevice->udev, "device node '%s' not found\n", filename);
416 if (udevice->devt && stats.st_rdev != udevice->devt) {
417 info(udevice->udev, "device node '%s' points to a different device, skip removal\n", filename);
421 info(udevice->udev, "removing device node '%s'\n", filename);
422 if (!udevice->test_run)
423 retval = unlink_secure(udevice->udev, filename);
427 setenv("DEVNAME", filename, 1);
428 num = udevice->partitions;
432 info(udevice->udev, "removing all_partitions '%s[1-%i]'\n", filename, num);
435 for (i = 1; i <= num; i++) {
436 snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
437 partitionname[sizeof(partitionname)-1] = '\0';
438 if (!udevice->test_run)
439 unlink_secure(udevice->udev, partitionname);
442 delete_path(udevice->udev, filename);