2 * udev-node.c - device node handling
4 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation version 2 of the License.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 675 Mass Ave, Cambridge, MA 02139, USA.
31 #include <sys/types.h>
34 #include "udev_rules.h"
35 #include "udev_selinux.h"
38 int udev_node_mknod(struct udevice *udev, const char *file, dev_t devt, mode_t mode, uid_t uid, gid_t gid)
43 if (major(devt) != 0 && strcmp(udev->dev->subsystem, "block") == 0)
48 if (stat(file, &stats) != 0)
51 /* preserve node with already correct numbers, to prevent changing the inode number */
52 if ((stats.st_mode & S_IFMT) == (mode & S_IFMT) && (stats.st_rdev == devt)) {
53 info("preserve file '%s', because it has correct dev_t", file);
54 selinux_setfilecon(file, udev->dev->kernel_name, stats.st_mode);
58 if (unlink(file) != 0)
59 err("unlink(%s) failed: %s", file, strerror(errno));
61 dbg("already present file '%s' unlinked", file);
64 selinux_setfscreatecon(file, udev->dev->kernel_name, mode);
65 retval = mknod(file, mode, devt);
66 selinux_resetfscreatecon();
68 err("mknod(%s, %#o, %u, %u) failed: %s",
69 file, mode, major(devt), minor(devt), strerror(errno));
74 dbg("chmod(%s, %#o)", file, mode);
75 if (chmod(file, mode) != 0) {
76 err("chmod(%s, %#o) failed: %s", file, mode, strerror(errno));
80 if (uid != 0 || gid != 0) {
81 dbg("chown(%s, %u, %u)", file, uid, gid);
82 if (chown(file, uid, gid) != 0) {
83 err("chown(%s, %u, %u) failed: %s",
84 file, uid, gid, strerror(errno));
93 static int udev_node_symlink(struct udevice *udev, const char *linktarget, const char *filename)
95 char target[PATH_SIZE];
98 /* look if symlink already exists */
99 len = readlink(filename, target, sizeof(target));
102 if (strcmp(linktarget, target) == 0) {
103 info("preserving symlink '%s' to '%s'", filename, linktarget);
104 selinux_setfilecon(filename, NULL, S_IFLNK);
107 info("link '%s' points to different target '%s', delete it", filename, target);
113 info("creating symlink '%s' to '%s'", filename, linktarget);
114 selinux_setfscreatecon(filename, NULL, S_IFLNK);
115 if (symlink(linktarget, filename) != 0)
116 err("symlink(%s, %s) failed: %s", linktarget, filename, strerror(errno));
117 selinux_resetfscreatecon();
123 int udev_node_add(struct udevice *udev, struct udevice *udev_old)
125 char filename[PATH_SIZE];
126 struct name_entry *name_loop;
135 snprintf(filename, sizeof(filename), "%s/%s", udev_root, udev->name);
136 filename[sizeof(filename)-1] = '\0';
138 /* create parent directories if needed */
139 if (strchr(udev->name, '/'))
140 create_path(filename);
142 if (strcmp(udev->owner, "root") == 0)
148 id = strtoul(udev->owner, &endptr, 10);
149 if (endptr[0] == '\0')
152 uid = lookup_user(udev->owner);
155 if (strcmp(udev->group, "root") == 0)
161 id = strtoul(udev->group, &endptr, 10);
162 if (endptr[0] == '\0')
165 gid = lookup_group(udev->group);
168 info("creating device node '%s', major = '%d', minor = '%d', " "mode = '%#o', uid = '%d', gid = '%d'",
169 filename, major(udev->devt), minor(udev->devt), udev->mode, uid, gid);
172 if (udev_node_mknod(udev, filename, udev->devt, udev->mode, uid, gid) != 0) {
177 setenv("DEVNAME", filename, 1);
179 /* create all_partitions if requested */
180 if (udev->partitions) {
181 char partitionname[PATH_SIZE];
185 /* take the maximum registered minor range */
186 attr = sysfs_attr_get_value(udev->dev->devpath, "range");
190 udev->partitions = range-1;
192 info("creating device partition nodes '%s[1-%i]'", filename, udev->partitions);
193 if (!udev->test_run) {
194 for (i = 1; i <= udev->partitions; i++) {
197 snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
198 partitionname[sizeof(partitionname)-1] = '\0';
199 part_devt = makedev(major(udev->devt), minor(udev->devt) + i);
200 udev_node_mknod(udev, partitionname, part_devt, udev->mode, uid, gid);
205 /* create symlink(s) if requested */
206 if (!list_empty(&udev->symlink_list)) {
207 char symlinks[512] = "";
209 list_for_each_entry(name_loop, &udev->symlink_list, node) {
210 char linktarget[PATH_SIZE];
212 snprintf(filename, sizeof(filename), "%s/%s", udev_root, name_loop->name);
213 filename[sizeof(filename)-1] = '\0';
215 dbg("symlink '%s' to node '%s' requested", filename, udev->name);
217 if (strchr(filename, '/'))
218 create_path(filename);
220 /* optimize relative link */
221 linktarget[0] = '\0';
224 while (udev->name[i] && (udev->name[i] == name_loop->name[i])) {
225 if (udev->name[i] == '/')
229 while (name_loop->name[i] != '\0') {
230 if (name_loop->name[i] == '/')
231 strlcat(linktarget, "../", sizeof(linktarget));
235 strlcat(linktarget, &udev->name[tail], sizeof(linktarget));
237 info("creating symlink '%s' to '%s'", filename, linktarget);
239 udev_node_symlink(udev, linktarget, filename);
241 strlcat(symlinks, filename, sizeof(symlinks));
242 strlcat(symlinks, " ", sizeof(symlinks));
245 remove_trailing_chars(symlinks, ' ');
246 setenv("DEVLINKS", symlinks, 1);
254 void udev_node_remove_symlinks(struct udevice *udev)
256 char filename[PATH_SIZE];
257 struct name_entry *name_loop;
260 if (!list_empty(&udev->symlink_list)) {
261 char symlinks[512] = "";
263 list_for_each_entry(name_loop, &udev->symlink_list, node) {
264 snprintf(filename, sizeof(filename), "%s/%s", udev_root, name_loop->name);
265 filename[sizeof(filename)-1] = '\0';
267 if (stat(filename, &stats) != 0) {
268 dbg("symlink '%s' not found", filename);
271 if (udev->devt && stats.st_rdev != udev->devt) {
272 info("symlink '%s' points to a different device, skip removal", filename);
276 info("removing symlink '%s'", filename);
277 if (!udev->test_run) {
280 if (strchr(filename, '/'))
281 delete_path(filename);
284 strlcat(symlinks, filename, sizeof(symlinks));
285 strlcat(symlinks, " ", sizeof(symlinks));
288 remove_trailing_chars(symlinks, ' ');
289 if (symlinks[0] != '\0')
290 setenv("DEVLINKS", symlinks, 1);
294 int udev_node_remove(struct udevice *udev)
296 char filename[PATH_SIZE];
297 char partitionname[PATH_SIZE];
302 udev_node_remove_symlinks(udev);
304 snprintf(filename, sizeof(filename), "%s/%s", udev_root, udev->name);
305 filename[sizeof(filename)-1] = '\0';
307 if (stat(filename, &stats) != 0) {
308 dbg("device node '%s' not found", filename);
311 if (udev->devt && stats.st_rdev != udev->devt) {
312 info("device node '%s' points to a different device, skip removal", filename);
316 info("removing device node '%s'", filename);
317 retval = unlink_secure(filename);
321 setenv("DEVNAME", filename, 1);
323 num = udev->partitions;
327 info("removing all_partitions '%s[1-%i]'", filename, num);
329 info("garbage from udev database, skip all_partitions removal");
332 for (i = 1; i <= num; i++) {
333 snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
334 partitionname[sizeof(partitionname)-1] = '\0';
335 unlink_secure(partitionname);
339 if (strchr(udev->name, '/'))
340 delete_path(filename);