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 *udev, 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(udev->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("preserve file '%s', because it has correct dev_t\n", file);
54 selinux_setfilecon(file, udev->dev->kernel, mode);
56 info("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(file_tmp, udev->dev->kernel, mode);
61 err = mknod(file_tmp, mode, devt);
62 selinux_resetfscreatecon();
64 err("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("rename(%s, %s) failed: %s\n",
71 file_tmp, file, strerror(errno));
76 info("mknod(%s, %#o, (%u,%u))\n", file, mode, major(devt), minor(devt));
77 selinux_setfscreatecon(file, udev->dev->kernel, mode);
78 err = mknod(file, mode, devt);
79 selinux_resetfscreatecon();
81 err("mknod(%s, %#o, (%u,%u) failed: %s\n",
82 file, mode, major(devt), minor(devt), strerror(errno));
87 if (!preserve || stats.st_mode != mode) {
88 info("chmod(%s, %#o)\n", file, mode);
89 err = chmod(file, mode);
91 err("chmod(%s, %#o) failed: %s\n", file, mode, strerror(errno));
96 if (!preserve || stats.st_uid != uid || stats.st_gid != gid) {
97 info("chown(%s, %u, %u)\n", file, uid, gid);
98 err = chown(file, uid, gid);
100 err("chown(%s, %u, %u) failed: %s\n", file, uid, gid, strerror(errno));
108 static int node_symlink(const char *node, const char *slink)
111 char target[PATH_SIZE] = "";
112 char slink_tmp[PATH_SIZE + sizeof(TMP_FILE_EXT)];
118 /* use relative link */
119 while (node[i] && (node[i] == slink[i])) {
124 while (slink[i] != '\0') {
126 strlcat(target, "../", sizeof(target));
129 strlcat(target, &node[tail], sizeof(target));
131 /* preserve link with correct target, do not replace node of other device */
132 if (lstat(slink, &stats) == 0) {
133 if (S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode)) {
136 info("found existing node instead of symlink '%s'\n", slink);
137 if (lstat(node, &stats2) == 0) {
138 if ((stats.st_mode & S_IFMT) == (stats2.st_mode & S_IFMT) &&
139 stats.st_rdev == stats2.st_rdev) {
140 info("replace device node '%s' with symlink to our node '%s'\n", slink, node);
142 err("device node '%s' already exists, link to '%s' will not overwrite it\n", slink, node);
146 } else if (S_ISLNK(stats.st_mode)) {
149 info("found existing symlink '%s'\n", slink);
150 len = readlink(slink, buf, sizeof(buf));
153 if (strcmp(target, buf) == 0) {
154 info("preserve already existing symlink '%s' to '%s'\n", slink, target);
155 selinux_setfilecon(slink, NULL, S_IFLNK);
161 info("creating symlink '%s' to '%s'\n", slink, target);
162 selinux_setfscreatecon(slink, NULL, S_IFLNK);
163 retval = symlink(target, slink);
164 selinux_resetfscreatecon();
169 info("atomically replace '%s'\n", slink);
170 strlcpy(slink_tmp, slink, sizeof(slink_tmp));
171 strlcat(slink_tmp, TMP_FILE_EXT, sizeof(slink_tmp));
173 selinux_setfscreatecon(slink, NULL, S_IFLNK);
174 retval = symlink(target, slink_tmp);
175 selinux_resetfscreatecon();
177 err("symlink(%s, %s) failed: %s\n", target, slink_tmp, strerror(errno));
180 retval = rename(slink_tmp, slink);
182 err("rename(%s, %s) failed: %s\n", slink_tmp, slink, strerror(errno));
190 static int update_link(struct udevice *udev, const char *name)
192 LIST_HEAD(name_list);
193 char slink[PATH_SIZE];
194 char node[PATH_SIZE];
195 struct udevice *udev_db;
196 struct name_entry *device;
197 char target[PATH_MAX] = "";
202 strlcpy(slink, udev_root, sizeof(slink));
203 strlcat(slink, "/", sizeof(slink));
204 strlcat(slink, name, sizeof(slink));
206 count = udev_db_get_devices_by_name(name, &name_list);
207 info("found %i devices with name '%s'\n", count, name);
209 /* if we don't have a reference, delete it */
211 info("no reference left, remove '%s'\n", name);
212 if (!udev->test_run) {
219 /* find the device with the highest priority */
220 list_for_each_entry(device, &name_list, node) {
221 info("found '%s' for '%s'\n", device->name, name);
223 /* did we find ourself? we win, if we have the same priority */
224 if (strcmp(udev->dev->devpath, device->name) == 0) {
225 info("compare (our own) priority of '%s' %i >= %i\n",
226 udev->dev->devpath, udev->link_priority, priority);
227 if (strcmp(udev->name, name) == 0) {
228 info("'%s' is our device node, database inconsistent, skip link update\n", udev->name);
229 } else if (target[0] == '\0' || udev->link_priority >= priority) {
230 priority = udev->link_priority;
231 strlcpy(target, udev->name, sizeof(target));
236 /* another device, read priority from database */
237 udev_db = udev_device_init(NULL);
240 if (udev_db_get_device(udev_db, device->name) == 0) {
241 if (strcmp(udev_db->name, name) == 0) {
242 info("'%s' is a device node of '%s', skip link update\n", udev_db->name, device->name);
244 info("compare priority of '%s' %i > %i\n",
245 udev_db->dev->devpath, udev_db->link_priority, priority);
246 if (target[0] == '\0' || udev_db->link_priority > priority) {
247 priority = udev_db->link_priority;
248 strlcpy(target, udev_db->name, sizeof(target));
252 udev_device_cleanup(udev_db);
254 name_list_cleanup(&name_list);
256 if (target[0] == '\0') {
257 info("no current target for '%s' found\n", name);
262 /* create symlink to the target with the highest priority */
263 strlcpy(node, udev_root, sizeof(node));
264 strlcat(node, "/", sizeof(node));
265 strlcat(node, target, sizeof(node));
266 info("'%s' with target '%s' has the highest priority %i, create it\n", name, target, priority);
267 if (!udev->test_run) {
269 node_symlink(node, slink);
275 void udev_node_update_symlinks(struct udevice *udev, struct udevice *udev_old)
277 struct name_entry *name_loop;
278 char symlinks[PATH_SIZE] = "";
280 list_for_each_entry(name_loop, &udev->symlink_list, node) {
281 info("update symlink '%s' of '%s'\n", name_loop->name, udev->dev->devpath);
282 update_link(udev, name_loop->name);
283 strlcat(symlinks, udev_root, sizeof(symlinks));
284 strlcat(symlinks, "/", sizeof(symlinks));
285 strlcat(symlinks, name_loop->name, sizeof(symlinks));
286 strlcat(symlinks, " ", sizeof(symlinks));
289 /* export symlinks to environment */
290 remove_trailing_chars(symlinks, ' ');
291 if (symlinks[0] != '\0')
292 setenv("DEVLINKS", symlinks, 1);
294 /* update possible left-over symlinks (device metadata changed) */
295 if (udev_old != NULL) {
296 struct name_entry *link_loop;
297 struct name_entry *link_old_loop;
300 /* remove current symlinks from old list */
301 list_for_each_entry(link_old_loop, &udev_old->symlink_list, node) {
303 list_for_each_entry(link_loop, &udev->symlink_list, node) {
304 if (strcmp(link_old_loop->name, link_loop->name) == 0) {
310 /* link does no longer belong to this device */
311 info("update old symlink '%s' no longer belonging to '%s'\n",
312 link_old_loop->name, udev->dev->devpath);
313 update_link(udev, link_old_loop->name);
318 * if the node name has changed, delete the node,
319 * or possibly restore a symlink of another device
321 if (strcmp(udev->name, udev_old->name) != 0)
322 update_link(udev, udev_old->name);
326 int udev_node_add(struct udevice *udev)
328 char filename[PATH_SIZE];
334 strlcpy(filename, udev_root, sizeof(filename));
335 strlcat(filename, "/", sizeof(filename));
336 strlcat(filename, udev->name, sizeof(filename));
337 create_path(filename);
339 if (strcmp(udev->owner, "root") == 0)
345 id = strtoul(udev->owner, &endptr, 10);
346 if (endptr[0] == '\0')
349 uid = lookup_user(udev->owner);
352 if (strcmp(udev->group, "root") == 0)
358 id = strtoul(udev->group, &endptr, 10);
359 if (endptr[0] == '\0')
362 gid = lookup_group(udev->group);
365 info("creating device node '%s', major=%d, minor=%d, mode=%#o, uid=%d, gid=%d\n",
366 filename, major(udev->devt), minor(udev->devt), udev->mode, uid, gid);
369 if (udev_node_mknod(udev, filename, udev->devt, udev->mode, uid, gid) != 0) {
374 setenv("DEVNAME", filename, 1);
376 /* create all_partitions if requested */
377 if (udev->partitions) {
378 char partitionname[PATH_SIZE];
382 /* take the maximum registered minor range */
383 attr = sysfs_attr_get_value(udev->dev->devpath, "range");
387 udev->partitions = range-1;
389 info("creating device partition nodes '%s[1-%i]'\n", filename, udev->partitions);
390 if (!udev->test_run) {
391 for (i = 1; i <= udev->partitions; i++) {
394 snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
395 partitionname[sizeof(partitionname)-1] = '\0';
396 part_devt = makedev(major(udev->devt), minor(udev->devt) + i);
397 udev_node_mknod(udev, partitionname, part_devt, udev->mode, uid, gid);
405 int udev_node_remove(struct udevice *udev)
407 char filename[PATH_SIZE];
408 char partitionname[PATH_SIZE];
413 strlcpy(filename, udev_root, sizeof(filename));
414 strlcat(filename, "/", sizeof(filename));
415 strlcat(filename, udev->name, sizeof(filename));
416 if (stat(filename, &stats) != 0) {
417 info("device node '%s' not found\n", filename);
420 if (udev->devt && stats.st_rdev != udev->devt) {
421 info("device node '%s' points to a different device, skip removal\n", filename);
425 info("removing device node '%s'\n", filename);
427 retval = unlink_secure(filename);
431 setenv("DEVNAME", filename, 1);
432 num = udev->partitions;
436 info("removing all_partitions '%s[1-%i]'\n", filename, num);
439 for (i = 1; i <= num; i++) {
440 snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
441 partitionname[sizeof(partitionname)-1] = '\0';
443 unlink_secure(partitionname);
446 delete_path(filename);