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"
37 int udev_node_mknod(struct udevice *udev, const char *file, dev_t devt, mode_t mode, uid_t uid, gid_t gid)
42 if (major(devt) != 0 && strcmp(udev->dev->subsystem, "block") == 0)
47 if (lstat(file, &stats) != 0)
50 /* preserve node with already correct numbers, to prevent changing the inode number */
51 if ((stats.st_mode & S_IFMT) == (mode & S_IFMT) && (stats.st_rdev == devt)) {
52 info("preserve file '%s', because it has correct dev_t", file);
53 selinux_setfilecon(file, udev->dev->kernel, stats.st_mode);
57 if (unlink(file) != 0)
58 err("unlink(%s) failed: %s", file, strerror(errno));
60 dbg("already present file '%s' unlinked", file);
63 selinux_setfscreatecon(file, udev->dev->kernel, mode);
64 retval = mknod(file, mode, devt);
65 selinux_resetfscreatecon();
67 err("mknod(%s, %#o, %u, %u) failed: %s",
68 file, mode, major(devt), minor(devt), strerror(errno));
73 dbg("chmod(%s, %#o)", file, mode);
74 if (chmod(file, mode) != 0) {
75 err("chmod(%s, %#o) failed: %s", file, mode, strerror(errno));
79 if (uid != 0 || gid != 0) {
80 dbg("chown(%s, %u, %u)", file, uid, gid);
81 if (chown(file, uid, gid) != 0) {
82 err("chown(%s, %u, %u) failed: %s",
83 file, uid, gid, strerror(errno));
92 static int node_symlink(const char *node, const char *slink)
94 char target[PATH_SIZE] = "";
100 /* use relative link */
101 while (node[i] && (node[i] == slink[i])) {
106 while (slink[i] != '\0') {
108 strlcat(target, "../", sizeof(target));
111 strlcat(target, &node[tail], sizeof(target));
113 /* look if symlink already exists */
114 len = readlink(slink, buf, sizeof(buf));
117 if (strcmp(target, buf) == 0) {
118 info("preserve already existing symlink '%s' to '%s'", slink, target);
119 selinux_setfilecon(slink, NULL, S_IFLNK);
122 info("link '%s' points to different target '%s', delete it", slink, buf);
127 info("creating symlink '%s' to '%s'", slink, target);
128 selinux_setfscreatecon(slink, NULL, S_IFLNK);
129 if (symlink(target, slink) != 0)
130 err("symlink(%s, %s) failed: %s", target, slink, strerror(errno));
131 selinux_resetfscreatecon();
137 static int update_link(struct udevice *udev, const char *name)
139 LIST_HEAD(name_list);
140 char slink[PATH_SIZE];
141 char node[PATH_SIZE];
142 struct udevice *udev_db;
143 struct name_entry *device;
144 char target[PATH_MAX] = "";
149 strlcpy(slink, udev_root, sizeof(slink));
150 strlcat(slink, "/", sizeof(slink));
151 strlcat(slink, name, sizeof(slink));
153 count = udev_db_get_devices_by_name(name, &name_list);
154 info("found %i devices with name '%s'", count, name);
156 /* if we don't have a reference, delete it */
158 info("no reference left, remove '%s'", name);
159 if (!udev->test_run) {
166 /* find the device with the highest priority */
167 list_for_each_entry(device, &name_list, node) {
168 info("found '%s' for '%s'", device->name, name);
170 /* did we find ourself? we win, if we have the same priority */
171 if (strcmp(udev->dev->devpath, device->name) == 0) {
172 info("compare (our own) priority of '%s' %i >= %i",
173 udev->dev->devpath, udev->link_priority, priority);
174 if (target[0] == '\0' || udev->link_priority >= priority) {
175 priority = udev->link_priority;
176 strlcpy(target, udev->name, sizeof(target));
181 /* or something else, then read priority from database */
182 udev_db = udev_device_init(NULL);
185 if (udev_db_get_device(udev_db, device->name) == 0) {
186 info("compare priority of '%s' %i > %i",
187 udev_db->dev->devpath, udev_db->link_priority, priority);
188 if (target[0] == '\0' || udev_db->link_priority > priority) {
189 priority = udev_db->link_priority;
190 strlcpy(target, udev_db->name, sizeof(target));
193 udev_device_cleanup(udev_db);
195 name_list_cleanup(&name_list);
197 if (target[0] == '\0') {
198 err("missing target for '%s'", name);
203 /* create symlink to the target with the highest priority */
204 strlcpy(node, udev_root, sizeof(node));
205 strlcat(node, "/", sizeof(node));
206 strlcat(node, target, sizeof(node));
207 info("'%s' with target '%s' has the highest priority %i, create it", name, target, priority);
208 if (!udev->test_run) {
210 node_symlink(node, slink);
216 void udev_node_update_symlinks(struct udevice *udev, struct udevice *udev_old)
218 struct name_entry *name_loop;
219 char symlinks[PATH_SIZE] = "";
221 list_for_each_entry(name_loop, &udev->symlink_list, node) {
222 info("update symlink '%s' of '%s'", name_loop->name, udev->dev->devpath);
223 update_link(udev, name_loop->name);
224 strlcat(symlinks, udev_root, sizeof(symlinks));
225 strlcat(symlinks, "/", sizeof(symlinks));
226 strlcat(symlinks, name_loop->name, sizeof(symlinks));
227 strlcat(symlinks, " ", sizeof(symlinks));
230 /* export symlinks to environment */
231 remove_trailing_chars(symlinks, ' ');
232 if (symlinks[0] != '\0')
233 setenv("DEVLINKS", symlinks, 1);
235 /* update possible left-over symlinks (device metadata changed) */
236 if (udev_old != NULL) {
237 struct name_entry *link_loop;
238 struct name_entry *link_old_loop;
241 /* remove current symlinks from old list */
242 list_for_each_entry(link_old_loop, &udev_old->symlink_list, node) {
244 list_for_each_entry(link_loop, &udev->symlink_list, node) {
245 if (strcmp(link_old_loop->name, link_loop->name) == 0) {
251 /* link does no longer belong to this device */
252 info("update old symlink '%s' no longer belonging to '%s'",
253 link_old_loop->name, udev->dev->devpath);
254 update_link(udev, link_old_loop->name);
259 * if the node name has changed, delete the node,
260 * or possibly restore a symlink of another device
262 if (strcmp(udev->name, udev_old->name) != 0)
263 update_link(udev, udev_old->name);
267 int udev_node_add(struct udevice *udev)
269 char filename[PATH_SIZE];
275 strlcpy(filename, udev_root, sizeof(filename));
276 strlcat(filename, "/", sizeof(filename));
277 strlcat(filename, udev->name, sizeof(filename));
278 create_path(filename);
280 if (strcmp(udev->owner, "root") == 0)
286 id = strtoul(udev->owner, &endptr, 10);
287 if (endptr[0] == '\0')
290 uid = lookup_user(udev->owner);
293 if (strcmp(udev->group, "root") == 0)
299 id = strtoul(udev->group, &endptr, 10);
300 if (endptr[0] == '\0')
303 gid = lookup_group(udev->group);
306 info("creating device node '%s', major = '%d', minor = '%d', " "mode = '%#o', uid = '%d', gid = '%d'",
307 filename, major(udev->devt), minor(udev->devt), udev->mode, uid, gid);
310 if (udev_node_mknod(udev, filename, udev->devt, udev->mode, uid, gid) != 0) {
315 setenv("DEVNAME", filename, 1);
317 /* create all_partitions if requested */
318 if (udev->partitions) {
319 char partitionname[PATH_SIZE];
323 /* take the maximum registered minor range */
324 attr = sysfs_attr_get_value(udev->dev->devpath, "range");
328 udev->partitions = range-1;
330 info("creating device partition nodes '%s[1-%i]'", filename, udev->partitions);
331 if (!udev->test_run) {
332 for (i = 1; i <= udev->partitions; i++) {
335 snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
336 partitionname[sizeof(partitionname)-1] = '\0';
337 part_devt = makedev(major(udev->devt), minor(udev->devt) + i);
338 udev_node_mknod(udev, partitionname, part_devt, udev->mode, uid, gid);
346 int udev_node_remove(struct udevice *udev)
348 char filename[PATH_SIZE];
349 char partitionname[PATH_SIZE];
354 strlcpy(filename, udev_root, sizeof(filename));
355 strlcat(filename, "/", sizeof(filename));
356 strlcat(filename, udev->name, sizeof(filename));
357 if (stat(filename, &stats) != 0) {
358 dbg("device node '%s' not found", filename);
361 if (udev->devt && stats.st_rdev != udev->devt) {
362 info("device node '%s' points to a different device, skip removal", filename);
366 info("removing device node '%s'", filename);
368 retval = unlink_secure(filename);
372 setenv("DEVNAME", filename, 1);
373 num = udev->partitions;
377 info("removing all_partitions '%s[1-%i]'", filename, num);
380 for (i = 1; i <= num; i++) {
381 snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
382 partitionname[sizeof(partitionname)-1] = '\0';
384 unlink_secure(partitionname);
387 delete_path(filename);