2 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
3 * Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation version 2 of the License.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 #include <sys/types.h>
33 #include "udev_rules.h"
35 #define TMP_FILE_EXT ".udev-tmp"
37 int udev_node_mknod(struct udevice *udevice, const char *file, dev_t devt, mode_t mode, uid_t uid, gid_t gid)
39 char file_tmp[PATH_SIZE + sizeof(TMP_FILE_EXT)];
44 if (major(devt) != 0 && strcmp(udevice->dev->subsystem, "block") == 0)
49 if (lstat(file, &stats) == 0) {
50 if (((stats.st_mode & S_IFMT) == (mode & S_IFMT)) && (stats.st_rdev == devt)) {
51 info(udevice->udev, "preserve file '%s', because it has correct dev_t\n", file);
53 udev_selinux_lsetfilecon(udevice->udev, file, mode);
55 info(udevice->udev, "atomically replace existing file '%s'\n", file);
56 strlcpy(file_tmp, file, sizeof(file_tmp));
57 strlcat(file_tmp, TMP_FILE_EXT, sizeof(file_tmp));
59 udev_selinux_setfscreatecon(udevice->udev, file_tmp, mode);
60 err = mknod(file_tmp, mode, devt);
61 udev_selinux_resetfscreatecon(udevice->udev);
63 err(udevice->udev, "mknod(%s, %#o, %u, %u) failed: %s\n",
64 file_tmp, mode, major(devt), minor(devt), strerror(errno));
67 err = rename(file_tmp, file);
69 err(udevice->udev, "rename(%s, %s) failed: %s\n", file_tmp, file, strerror(errno));
74 info(udevice->udev, "mknod(%s, %#o, (%u,%u))\n", file, mode, major(devt), minor(devt));
75 udev_selinux_setfscreatecon(udevice->udev, file, mode);
76 err = mknod(file, mode, devt);
77 udev_selinux_resetfscreatecon(udevice->udev);
79 err(udevice->udev, "mknod(%s, %#o, (%u,%u) failed: %s\n",
80 file, mode, major(devt), minor(devt), strerror(errno));
85 if (!preserve || stats.st_mode != mode) {
86 info(udevice->udev, "chmod(%s, %#o)\n", file, mode);
87 err = chmod(file, mode);
89 err(udevice->udev, "chmod(%s, %#o) failed: %s\n", file, mode, strerror(errno));
94 if (!preserve || stats.st_uid != uid || stats.st_gid != gid) {
95 info(udevice->udev, "chown(%s, %u, %u)\n", file, uid, gid);
96 err = chown(file, uid, gid);
98 err(udevice->udev, "chown(%s, %u, %u) failed: %s\n", file, uid, gid, strerror(errno));
106 static int node_symlink(struct udevice *udevice, const char *node, const char *slink)
109 char target[PATH_SIZE] = "";
110 char slink_tmp[PATH_SIZE + sizeof(TMP_FILE_EXT)];
116 /* use relative link */
117 while (node[i] && (node[i] == slink[i])) {
122 while (slink[i] != '\0') {
124 strlcat(target, "../", sizeof(target));
127 strlcat(target, &node[tail], sizeof(target));
129 /* preserve link with correct target, do not replace node of other device */
130 if (lstat(slink, &stats) == 0) {
131 if (S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode)) {
134 info(udevice->udev, "found existing node instead of symlink '%s'\n", slink);
135 if (lstat(node, &stats2) == 0) {
136 if ((stats.st_mode & S_IFMT) == (stats2.st_mode & S_IFMT) &&
137 stats.st_rdev == stats2.st_rdev) {
138 info(udevice->udev, "replace device node '%s' with symlink to our node '%s'\n", slink, node);
140 err(udevice->udev, "device node '%s' already exists, link to '%s' will not overwrite it\n", slink, node);
144 } else if (S_ISLNK(stats.st_mode)) {
147 info(udevice->udev, "found existing symlink '%s'\n", slink);
148 len = readlink(slink, buf, sizeof(buf));
151 if (strcmp(target, buf) == 0) {
152 info(udevice->udev, "preserve already existing symlink '%s' to '%s'\n", slink, target);
153 udev_selinux_lsetfilecon(udevice->udev, slink, S_IFLNK);
159 info(udevice->udev, "creating symlink '%s' to '%s'\n", slink, target);
160 udev_selinux_setfscreatecon(udevice->udev, slink, S_IFLNK);
161 retval = symlink(target, slink);
162 udev_selinux_resetfscreatecon(udevice->udev);
167 info(udevice->udev, "atomically replace '%s'\n", slink);
168 strlcpy(slink_tmp, slink, sizeof(slink_tmp));
169 strlcat(slink_tmp, TMP_FILE_EXT, sizeof(slink_tmp));
171 udev_selinux_setfscreatecon(udevice->udev, slink, S_IFLNK);
172 retval = symlink(target, slink_tmp);
173 udev_selinux_resetfscreatecon(udevice->udev);
175 err(udevice->udev, "symlink(%s, %s) failed: %s\n", target, slink_tmp, strerror(errno));
178 retval = rename(slink_tmp, slink);
180 err(udevice->udev, "rename(%s, %s) failed: %s\n", slink_tmp, slink, strerror(errno));
188 static int update_link(struct udevice *udevice, const char *name)
190 LIST_HEAD(name_list);
191 char slink[PATH_SIZE];
192 char node[PATH_SIZE];
193 struct udevice *udevice_db;
194 struct name_entry *device;
195 char target[PATH_MAX] = "";
200 strlcpy(slink, udev_get_dev_path(udevice->udev), sizeof(slink));
201 strlcat(slink, "/", sizeof(slink));
202 strlcat(slink, name, sizeof(slink));
204 count = udev_db_get_devices_by_name(udevice->udev, name, &name_list);
205 info(udevice->udev, "found %i devices with name '%s'\n", count, name);
207 /* if we don't have a reference, delete it */
209 info(udevice->udev, "no reference left, remove '%s'\n", name);
210 if (!udevice->test_run) {
212 delete_path(udevice->udev, slink);
217 /* find the device with the highest priority */
218 list_for_each_entry(device, &name_list, node) {
219 info(udevice->udev, "found '%s' for '%s'\n", device->name, name);
221 /* did we find ourself? we win, if we have the same priority */
222 if (strcmp(udevice->dev->devpath, device->name) == 0) {
223 info(udevice->udev, "compare (our own) priority of '%s' %i >= %i\n",
224 udevice->dev->devpath, udevice->link_priority, priority);
225 if (strcmp(udevice->name, name) == 0) {
226 info(udevice->udev, "'%s' is our device node, database inconsistent, skip link update\n", udevice->name);
227 } else if (target[0] == '\0' || udevice->link_priority >= priority) {
228 priority = udevice->link_priority;
229 strlcpy(target, udevice->name, sizeof(target));
234 /* another device, read priority from database */
235 udevice_db = udev_device_init(udevice->udev);
236 if (udevice_db == NULL)
238 if (udev_db_get_device(udevice_db, device->name) == 0) {
239 if (strcmp(udevice_db->name, name) == 0) {
240 info(udevice->udev, "'%s' is a device node of '%s', skip link update\n", udevice_db->name, device->name);
242 info(udevice->udev, "compare priority of '%s' %i > %i\n",
243 udevice_db->dev->devpath, udevice_db->link_priority, priority);
244 if (target[0] == '\0' || udevice_db->link_priority > priority) {
245 priority = udevice_db->link_priority;
246 strlcpy(target, udevice_db->name, sizeof(target));
250 udev_device_cleanup(udevice_db);
252 name_list_cleanup(udevice->udev, &name_list);
254 if (target[0] == '\0') {
255 info(udevice->udev, "no current target for '%s' found\n", name);
260 /* create symlink to the target with the highest priority */
261 strlcpy(node, udev_get_dev_path(udevice->udev), sizeof(node));
262 strlcat(node, "/", sizeof(node));
263 strlcat(node, target, sizeof(node));
264 info(udevice->udev, "'%s' with target '%s' has the highest priority %i, create it\n", name, target, priority);
265 if (!udevice->test_run) {
266 create_path(udevice->udev, slink);
267 node_symlink(udevice, node, slink);
273 void udev_node_update_symlinks(struct udevice *udevice, struct udevice *udevice_old)
275 struct name_entry *name_loop;
276 char symlinks[PATH_SIZE] = "";
278 list_for_each_entry(name_loop, &udevice->symlink_list, node) {
279 info(udevice->udev, "update symlink '%s' of '%s'\n", name_loop->name, udevice->dev->devpath);
280 update_link(udevice, name_loop->name);
281 strlcat(symlinks, udev_get_dev_path(udevice->udev), sizeof(symlinks));
282 strlcat(symlinks, "/", sizeof(symlinks));
283 strlcat(symlinks, name_loop->name, sizeof(symlinks));
284 strlcat(symlinks, " ", sizeof(symlinks));
287 /* export symlinks to environment */
288 remove_trailing_chars(symlinks, ' ');
289 if (symlinks[0] != '\0')
290 setenv("DEVLINKS", symlinks, 1);
292 /* update possible left-over symlinks (device metadata changed) */
293 if (udevice_old != NULL) {
294 struct name_entry *link_loop;
295 struct name_entry *link_old_loop;
298 /* remove current symlinks from old list */
299 list_for_each_entry(link_old_loop, &udevice_old->symlink_list, node) {
301 list_for_each_entry(link_loop, &udevice->symlink_list, node) {
302 if (strcmp(link_old_loop->name, link_loop->name) == 0) {
308 /* link does no longer belong to this device */
309 info(udevice->udev, "update old symlink '%s' no longer belonging to '%s'\n",
310 link_old_loop->name, udevice->dev->devpath);
311 update_link(udevice, link_old_loop->name);
316 * if the node name has changed, delete the node,
317 * or possibly restore a symlink of another device
319 if (strcmp(udevice->name, udevice_old->name) != 0)
320 update_link(udevice, udevice_old->name);
324 int udev_node_add(struct udevice *udevice)
326 char filename[PATH_SIZE];
332 strlcpy(filename, udev_get_dev_path(udevice->udev), sizeof(filename));
333 strlcat(filename, "/", sizeof(filename));
334 strlcat(filename, udevice->name, sizeof(filename));
335 create_path(udevice->udev, filename);
337 if (strcmp(udevice->owner, "root") == 0)
343 id = strtoul(udevice->owner, &endptr, 10);
344 if (endptr[0] == '\0')
347 uid = lookup_user(udevice->udev, udevice->owner);
350 if (strcmp(udevice->group, "root") == 0)
356 id = strtoul(udevice->group, &endptr, 10);
357 if (endptr[0] == '\0')
360 gid = lookup_group(udevice->udev, udevice->group);
363 info(udevice->udev, "creating device node '%s', major=%d, minor=%d, mode=%#o, uid=%d, gid=%d\n",
364 filename, major(udevice->devt), minor(udevice->devt), udevice->mode, uid, gid);
366 if (!udevice->test_run)
367 if (udev_node_mknod(udevice, filename, udevice->devt, udevice->mode, uid, gid) != 0) {
372 setenv("DEVNAME", filename, 1);
374 /* create all_partitions if requested */
375 if (udevice->partitions) {
376 char partitionname[PATH_SIZE];
380 /* take the maximum registered minor range */
381 attr = sysfs_attr_get_value(udevice->udev, udevice->dev->devpath, "range");
385 udevice->partitions = range-1;
387 info(udevice->udev, "creating device partition nodes '%s[1-%i]'\n", filename, udevice->partitions);
388 if (!udevice->test_run) {
389 for (i = 1; i <= udevice->partitions; i++) {
392 snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
393 partitionname[sizeof(partitionname)-1] = '\0';
394 part_devt = makedev(major(udevice->devt), minor(udevice->devt) + i);
395 udev_node_mknod(udevice, partitionname, part_devt, udevice->mode, uid, gid);
403 int udev_node_remove(struct udevice *udevice)
405 char filename[PATH_SIZE];
406 char partitionname[PATH_SIZE];
411 strlcpy(filename, udev_get_dev_path(udevice->udev), sizeof(filename));
412 strlcat(filename, "/", sizeof(filename));
413 strlcat(filename, udevice->name, sizeof(filename));
414 if (stat(filename, &stats) != 0) {
415 info(udevice->udev, "device node '%s' not found\n", filename);
418 if (udevice->devt && stats.st_rdev != udevice->devt) {
419 info(udevice->udev, "device node '%s' points to a different device, skip removal\n", filename);
423 info(udevice->udev, "removing device node '%s'\n", filename);
424 if (!udevice->test_run)
425 retval = unlink_secure(udevice->udev, filename);
429 setenv("DEVNAME", filename, 1);
430 num = udevice->partitions;
434 info(udevice->udev, "removing all_partitions '%s[1-%i]'\n", filename, num);
437 for (i = 1; i <= num; i++) {
438 snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
439 partitionname[sizeof(partitionname)-1] = '\0';
440 if (!udevice->test_run)
441 unlink_secure(udevice->udev, partitionname);
444 delete_path(udevice->udev, filename);