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"
34 #include "udev_selinux.h"
36 #define TMP_FILE_EXT ".udev-tmp"
38 int udev_node_mknod(struct udevice *udevice, const char *file, dev_t devt, mode_t mode, uid_t uid, gid_t gid)
40 char file_tmp[PATH_SIZE + sizeof(TMP_FILE_EXT)];
45 if (major(devt) != 0 && strcmp(udevice->dev->subsystem, "block") == 0)
50 if (lstat(file, &stats) == 0) {
51 if (((stats.st_mode & S_IFMT) == (mode & S_IFMT)) && (stats.st_rdev == devt)) {
52 info(udevice->udev, "preserve file '%s', because it has correct dev_t\n", file);
54 selinux_setfilecon(udevice->udev, file, udevice->dev->kernel, mode);
56 info(udevice->udev, "atomically replace existing file '%s'\n", file);
57 strlcpy(file_tmp, file, sizeof(file_tmp));
58 strlcat(file_tmp, TMP_FILE_EXT, sizeof(file_tmp));
60 selinux_setfscreatecon(udevice->udev, file_tmp, udevice->dev->kernel, mode);
61 err = mknod(file_tmp, mode, devt);
62 selinux_resetfscreatecon(udevice->udev);
64 err(udevice->udev, "mknod(%s, %#o, %u, %u) failed: %s\n",
65 file_tmp, mode, major(devt), minor(devt), strerror(errno));
68 err = rename(file_tmp, file);
70 err(udevice->udev, "rename(%s, %s) failed: %s\n", file_tmp, file, strerror(errno));
75 info(udevice->udev, "mknod(%s, %#o, (%u,%u))\n", file, mode, major(devt), minor(devt));
76 selinux_setfscreatecon(udevice->udev, file, udevice->dev->kernel, mode);
77 err = mknod(file, mode, devt);
78 selinux_resetfscreatecon(udevice->udev);
80 err(udevice->udev, "mknod(%s, %#o, (%u,%u) failed: %s\n",
81 file, mode, major(devt), minor(devt), strerror(errno));
86 if (!preserve || stats.st_mode != mode) {
87 info(udevice->udev, "chmod(%s, %#o)\n", file, mode);
88 err = chmod(file, mode);
90 err(udevice->udev, "chmod(%s, %#o) failed: %s\n", file, mode, strerror(errno));
95 if (!preserve || stats.st_uid != uid || stats.st_gid != gid) {
96 info(udevice->udev, "chown(%s, %u, %u)\n", file, uid, gid);
97 err = chown(file, uid, gid);
99 err(udevice->udev, "chown(%s, %u, %u) failed: %s\n", file, uid, gid, strerror(errno));
107 static int node_symlink(struct udevice *udevice, const char *node, const char *slink)
110 char target[PATH_SIZE] = "";
111 char slink_tmp[PATH_SIZE + sizeof(TMP_FILE_EXT)];
117 /* use relative link */
118 while (node[i] && (node[i] == slink[i])) {
123 while (slink[i] != '\0') {
125 strlcat(target, "../", sizeof(target));
128 strlcat(target, &node[tail], sizeof(target));
130 /* preserve link with correct target, do not replace node of other device */
131 if (lstat(slink, &stats) == 0) {
132 if (S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode)) {
135 info(udevice->udev, "found existing node instead of symlink '%s'\n", slink);
136 if (lstat(node, &stats2) == 0) {
137 if ((stats.st_mode & S_IFMT) == (stats2.st_mode & S_IFMT) &&
138 stats.st_rdev == stats2.st_rdev) {
139 info(udevice->udev, "replace device node '%s' with symlink to our node '%s'\n", slink, node);
141 err(udevice->udev, "device node '%s' already exists, link to '%s' will not overwrite it\n", slink, node);
145 } else if (S_ISLNK(stats.st_mode)) {
148 info(udevice->udev, "found existing symlink '%s'\n", slink);
149 len = readlink(slink, buf, sizeof(buf));
152 if (strcmp(target, buf) == 0) {
153 info(udevice->udev, "preserve already existing symlink '%s' to '%s'\n", slink, target);
154 selinux_setfilecon(udevice->udev, slink, NULL, S_IFLNK);
160 info(udevice->udev, "creating symlink '%s' to '%s'\n", slink, target);
161 selinux_setfscreatecon(udevice->udev, slink, NULL, S_IFLNK);
162 retval = symlink(target, slink);
163 selinux_resetfscreatecon(udevice->udev);
168 info(udevice->udev, "atomically replace '%s'\n", slink);
169 strlcpy(slink_tmp, slink, sizeof(slink_tmp));
170 strlcat(slink_tmp, TMP_FILE_EXT, sizeof(slink_tmp));
172 selinux_setfscreatecon(udevice->udev, slink, NULL, S_IFLNK);
173 retval = symlink(target, slink_tmp);
174 selinux_resetfscreatecon(udevice->udev);
176 err(udevice->udev, "symlink(%s, %s) failed: %s\n", target, slink_tmp, strerror(errno));
179 retval = rename(slink_tmp, slink);
181 err(udevice->udev, "rename(%s, %s) failed: %s\n", slink_tmp, slink, strerror(errno));
189 static int update_link(struct udevice *udevice, const char *name)
191 LIST_HEAD(name_list);
192 char slink[PATH_SIZE];
193 char node[PATH_SIZE];
194 struct udevice *udevice_db;
195 struct name_entry *device;
196 char target[PATH_MAX] = "";
201 strlcpy(slink, udev_get_dev_path(udevice->udev), sizeof(slink));
202 strlcat(slink, "/", sizeof(slink));
203 strlcat(slink, name, sizeof(slink));
205 count = udev_db_get_devices_by_name(udevice->udev, name, &name_list);
206 info(udevice->udev, "found %i devices with name '%s'\n", count, name);
208 /* if we don't have a reference, delete it */
210 info(udevice->udev, "no reference left, remove '%s'\n", name);
211 if (!udevice->test_run) {
213 delete_path(udevice->udev, slink);
218 /* find the device with the highest priority */
219 list_for_each_entry(device, &name_list, node) {
220 info(udevice->udev, "found '%s' for '%s'\n", device->name, name);
222 /* did we find ourself? we win, if we have the same priority */
223 if (strcmp(udevice->dev->devpath, device->name) == 0) {
224 info(udevice->udev, "compare (our own) priority of '%s' %i >= %i\n",
225 udevice->dev->devpath, udevice->link_priority, priority);
226 if (strcmp(udevice->name, name) == 0) {
227 info(udevice->udev, "'%s' is our device node, database inconsistent, skip link update\n", udevice->name);
228 } else if (target[0] == '\0' || udevice->link_priority >= priority) {
229 priority = udevice->link_priority;
230 strlcpy(target, udevice->name, sizeof(target));
235 /* another device, read priority from database */
236 udevice_db = udev_device_init(udevice->udev);
237 if (udevice_db == NULL)
239 if (udev_db_get_device(udevice_db, device->name) == 0) {
240 if (strcmp(udevice_db->name, name) == 0) {
241 info(udevice->udev, "'%s' is a device node of '%s', skip link update\n", udevice_db->name, device->name);
243 info(udevice->udev, "compare priority of '%s' %i > %i\n",
244 udevice_db->dev->devpath, udevice_db->link_priority, priority);
245 if (target[0] == '\0' || udevice_db->link_priority > priority) {
246 priority = udevice_db->link_priority;
247 strlcpy(target, udevice_db->name, sizeof(target));
251 udev_device_cleanup(udevice_db);
253 name_list_cleanup(udevice->udev, &name_list);
255 if (target[0] == '\0') {
256 info(udevice->udev, "no current target for '%s' found\n", name);
261 /* create symlink to the target with the highest priority */
262 strlcpy(node, udev_get_dev_path(udevice->udev), sizeof(node));
263 strlcat(node, "/", sizeof(node));
264 strlcat(node, target, sizeof(node));
265 info(udevice->udev, "'%s' with target '%s' has the highest priority %i, create it\n", name, target, priority);
266 if (!udevice->test_run) {
267 create_path(udevice->udev, slink);
268 node_symlink(udevice, node, slink);
274 void udev_node_update_symlinks(struct udevice *udevice, struct udevice *udevice_old)
276 struct name_entry *name_loop;
277 char symlinks[PATH_SIZE] = "";
279 list_for_each_entry(name_loop, &udevice->symlink_list, node) {
280 info(udevice->udev, "update symlink '%s' of '%s'\n", name_loop->name, udevice->dev->devpath);
281 update_link(udevice, name_loop->name);
282 strlcat(symlinks, udev_get_dev_path(udevice->udev), sizeof(symlinks));
283 strlcat(symlinks, "/", sizeof(symlinks));
284 strlcat(symlinks, name_loop->name, sizeof(symlinks));
285 strlcat(symlinks, " ", sizeof(symlinks));
288 /* export symlinks to environment */
289 remove_trailing_chars(symlinks, ' ');
290 if (symlinks[0] != '\0')
291 setenv("DEVLINKS", symlinks, 1);
293 /* update possible left-over symlinks (device metadata changed) */
294 if (udevice_old != NULL) {
295 struct name_entry *link_loop;
296 struct name_entry *link_old_loop;
299 /* remove current symlinks from old list */
300 list_for_each_entry(link_old_loop, &udevice_old->symlink_list, node) {
302 list_for_each_entry(link_loop, &udevice->symlink_list, node) {
303 if (strcmp(link_old_loop->name, link_loop->name) == 0) {
309 /* link does no longer belong to this device */
310 info(udevice->udev, "update old symlink '%s' no longer belonging to '%s'\n",
311 link_old_loop->name, udevice->dev->devpath);
312 update_link(udevice, link_old_loop->name);
317 * if the node name has changed, delete the node,
318 * or possibly restore a symlink of another device
320 if (strcmp(udevice->name, udevice_old->name) != 0)
321 update_link(udevice, udevice_old->name);
325 int udev_node_add(struct udevice *udevice)
327 char filename[PATH_SIZE];
333 strlcpy(filename, udev_get_dev_path(udevice->udev), sizeof(filename));
334 strlcat(filename, "/", sizeof(filename));
335 strlcat(filename, udevice->name, sizeof(filename));
336 create_path(udevice->udev, filename);
338 if (strcmp(udevice->owner, "root") == 0)
344 id = strtoul(udevice->owner, &endptr, 10);
345 if (endptr[0] == '\0')
348 uid = lookup_user(udevice->udev, udevice->owner);
351 if (strcmp(udevice->group, "root") == 0)
357 id = strtoul(udevice->group, &endptr, 10);
358 if (endptr[0] == '\0')
361 gid = lookup_group(udevice->udev, udevice->group);
364 info(udevice->udev, "creating device node '%s', major=%d, minor=%d, mode=%#o, uid=%d, gid=%d\n",
365 filename, major(udevice->devt), minor(udevice->devt), udevice->mode, uid, gid);
367 if (!udevice->test_run)
368 if (udev_node_mknod(udevice, filename, udevice->devt, udevice->mode, uid, gid) != 0) {
373 setenv("DEVNAME", filename, 1);
375 /* create all_partitions if requested */
376 if (udevice->partitions) {
377 char partitionname[PATH_SIZE];
381 /* take the maximum registered minor range */
382 attr = sysfs_attr_get_value(udevice->udev, udevice->dev->devpath, "range");
386 udevice->partitions = range-1;
388 info(udevice->udev, "creating device partition nodes '%s[1-%i]'\n", filename, udevice->partitions);
389 if (!udevice->test_run) {
390 for (i = 1; i <= udevice->partitions; i++) {
393 snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
394 partitionname[sizeof(partitionname)-1] = '\0';
395 part_devt = makedev(major(udevice->devt), minor(udevice->devt) + i);
396 udev_node_mknod(udevice, partitionname, part_devt, udevice->mode, uid, gid);
404 int udev_node_remove(struct udevice *udevice)
406 char filename[PATH_SIZE];
407 char partitionname[PATH_SIZE];
412 strlcpy(filename, udev_get_dev_path(udevice->udev), sizeof(filename));
413 strlcat(filename, "/", sizeof(filename));
414 strlcat(filename, udevice->name, sizeof(filename));
415 if (stat(filename, &stats) != 0) {
416 info(udevice->udev, "device node '%s' not found\n", filename);
419 if (udevice->devt && stats.st_rdev != udevice->devt) {
420 info(udevice->udev, "device node '%s' points to a different device, skip removal\n", filename);
424 info(udevice->udev, "removing device node '%s'\n", filename);
425 if (!udevice->test_run)
426 retval = unlink_secure(udevice->udev, filename);
430 setenv("DEVNAME", filename, 1);
431 num = udevice->partitions;
435 info(udevice->udev, "removing all_partitions '%s[1-%i]'\n", filename, num);
438 for (i = 1; i <= num; i++) {
439 snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
440 partitionname[sizeof(partitionname)-1] = '\0';
441 if (!udevice->test_run)
442 unlink_secure(udevice->udev, partitionname);
445 delete_path(udevice->udev, filename);