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.
29 #include <sys/types.h>
32 #include "udev_rules.h"
33 #include "udev_selinux.h"
36 int udev_node_mknod(struct udevice *udev, const char *file, dev_t devt, mode_t mode, uid_t uid, gid_t gid)
41 if (major(devt) != 0 && strcmp(udev->dev->subsystem, "block") == 0)
46 if (stat(file, &stats) != 0)
49 /* preserve node with already correct numbers, to prevent changing the inode number */
50 if ((stats.st_mode & S_IFMT) == (mode & S_IFMT) && (stats.st_rdev == devt)) {
51 info("preserve file '%s', because it has correct dev_t", file);
52 selinux_setfilecon(file, udev->dev->kernel, stats.st_mode);
56 if (unlink(file) != 0)
57 err("unlink(%s) failed: %s", file, strerror(errno));
59 dbg("already present file '%s' unlinked", file);
62 selinux_setfscreatecon(file, udev->dev->kernel, mode);
63 retval = mknod(file, mode, devt);
64 selinux_resetfscreatecon();
66 err("mknod(%s, %#o, %u, %u) failed: %s",
67 file, mode, major(devt), minor(devt), strerror(errno));
72 dbg("chmod(%s, %#o)", file, mode);
73 if (chmod(file, mode) != 0) {
74 err("chmod(%s, %#o) failed: %s", file, mode, strerror(errno));
78 if (uid != 0 || gid != 0) {
79 dbg("chown(%s, %u, %u)", file, uid, gid);
80 if (chown(file, uid, gid) != 0) {
81 err("chown(%s, %u, %u) failed: %s",
82 file, uid, gid, strerror(errno));
91 static int udev_node_symlink(struct udevice *udev, const char *linktarget, const char *filename)
93 char target[PATH_SIZE];
96 /* look if symlink already exists */
97 len = readlink(filename, target, sizeof(target));
100 if (strcmp(linktarget, target) == 0) {
101 info("preserving symlink '%s' to '%s'", filename, linktarget);
102 selinux_setfilecon(filename, NULL, S_IFLNK);
105 info("link '%s' points to different target '%s', delete it", filename, target);
111 info("creating symlink '%s' to '%s'", filename, linktarget);
112 selinux_setfscreatecon(filename, NULL, S_IFLNK);
113 if (symlink(linktarget, filename) != 0)
114 err("symlink(%s, %s) failed: %s", linktarget, filename, strerror(errno));
115 selinux_resetfscreatecon();
121 int udev_node_add(struct udevice *udev, struct udevice *udev_old)
123 char filename[PATH_SIZE];
124 struct name_entry *name_loop;
131 snprintf(filename, sizeof(filename), "%s/%s", udev_root, udev->name);
132 filename[sizeof(filename)-1] = '\0';
134 /* create parent directories if needed */
135 if (strchr(udev->name, '/'))
136 create_path(filename);
138 if (strcmp(udev->owner, "root") == 0)
144 id = strtoul(udev->owner, &endptr, 10);
145 if (endptr[0] == '\0')
148 uid = lookup_user(udev->owner);
151 if (strcmp(udev->group, "root") == 0)
157 id = strtoul(udev->group, &endptr, 10);
158 if (endptr[0] == '\0')
161 gid = lookup_group(udev->group);
164 info("creating device node '%s', major = '%d', minor = '%d', " "mode = '%#o', uid = '%d', gid = '%d'",
165 filename, major(udev->devt), minor(udev->devt), udev->mode, uid, gid);
168 if (udev_node_mknod(udev, filename, udev->devt, udev->mode, uid, gid) != 0) {
173 setenv("DEVNAME", filename, 1);
175 /* create all_partitions if requested */
176 if (udev->partitions) {
177 char partitionname[PATH_SIZE];
181 /* take the maximum registered minor range */
182 attr = sysfs_attr_get_value(udev->dev->devpath, "range");
186 udev->partitions = range-1;
188 info("creating device partition nodes '%s[1-%i]'", filename, udev->partitions);
189 if (!udev->test_run) {
190 for (i = 1; i <= udev->partitions; i++) {
193 snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
194 partitionname[sizeof(partitionname)-1] = '\0';
195 part_devt = makedev(major(udev->devt), minor(udev->devt) + i);
196 udev_node_mknod(udev, partitionname, part_devt, udev->mode, uid, gid);
201 /* create symlink(s) if requested */
202 if (!list_empty(&udev->symlink_list)) {
203 char symlinks[512] = "";
205 list_for_each_entry(name_loop, &udev->symlink_list, node) {
206 char linktarget[PATH_SIZE];
208 snprintf(filename, sizeof(filename), "%s/%s", udev_root, name_loop->name);
209 filename[sizeof(filename)-1] = '\0';
211 dbg("symlink '%s' to node '%s' requested", filename, udev->name);
213 if (strchr(filename, '/'))
214 create_path(filename);
216 /* optimize relative link */
217 linktarget[0] = '\0';
220 while (udev->name[i] && (udev->name[i] == name_loop->name[i])) {
221 if (udev->name[i] == '/')
225 while (name_loop->name[i] != '\0') {
226 if (name_loop->name[i] == '/')
227 strlcat(linktarget, "../", sizeof(linktarget));
231 strlcat(linktarget, &udev->name[tail], sizeof(linktarget));
233 info("creating symlink '%s' to '%s'", filename, linktarget);
235 udev_node_symlink(udev, linktarget, filename);
237 strlcat(symlinks, filename, sizeof(symlinks));
238 strlcat(symlinks, " ", sizeof(symlinks));
241 remove_trailing_chars(symlinks, ' ');
242 setenv("DEVLINKS", symlinks, 1);
250 void udev_node_remove_symlinks(struct udevice *udev)
252 char filename[PATH_SIZE];
253 struct name_entry *name_loop;
256 if (!list_empty(&udev->symlink_list)) {
257 char symlinks[512] = "";
259 list_for_each_entry(name_loop, &udev->symlink_list, node) {
260 snprintf(filename, sizeof(filename), "%s/%s", udev_root, name_loop->name);
261 filename[sizeof(filename)-1] = '\0';
263 if (stat(filename, &stats) != 0) {
264 dbg("symlink '%s' not found", filename);
267 if (udev->devt && stats.st_rdev != udev->devt) {
268 info("symlink '%s' points to a different device, skip removal", filename);
272 info("removing symlink '%s'", filename);
273 if (!udev->test_run) {
276 if (strchr(filename, '/'))
277 delete_path(filename);
280 strlcat(symlinks, filename, sizeof(symlinks));
281 strlcat(symlinks, " ", sizeof(symlinks));
284 remove_trailing_chars(symlinks, ' ');
285 if (symlinks[0] != '\0')
286 setenv("DEVLINKS", symlinks, 1);
290 int udev_node_remove(struct udevice *udev)
292 char filename[PATH_SIZE];
293 char partitionname[PATH_SIZE];
298 udev_node_remove_symlinks(udev);
300 snprintf(filename, sizeof(filename), "%s/%s", udev_root, udev->name);
301 filename[sizeof(filename)-1] = '\0';
303 if (stat(filename, &stats) != 0) {
304 dbg("device node '%s' not found", filename);
307 if (udev->devt && stats.st_rdev != udev->devt) {
308 info("device node '%s' points to a different device, skip removal", filename);
312 info("removing device node '%s'", filename);
313 retval = unlink_secure(filename);
317 setenv("DEVNAME", filename, 1);
319 num = udev->partitions;
323 info("removing all_partitions '%s[1-%i]'", filename, num);
325 info("garbage from udev database, skip all_partitions removal");
328 for (i = 1; i <= num; i++) {
329 snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
330 partitionname[sizeof(partitionname)-1] = '\0';
331 unlink_secure(partitionname);
335 if (strchr(udev->name, '/'))
336 delete_path(filename);