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 node_symlink(const char *node, const char *slink)
93 char target[PATH_SIZE] = "";
99 /* use relative link */
100 while (node[i] && (node[i] == slink[i])) {
105 while (slink[i] != '\0') {
107 strlcat(target, "../", sizeof(target));
110 strlcat(target, &node[tail], sizeof(target));
112 /* look if symlink already exists */
113 len = readlink(slink, buf, sizeof(buf));
116 if (strcmp(target, buf) == 0) {
117 info("preserving symlink '%s' to '%s'", slink, target);
118 selinux_setfilecon(slink, NULL, S_IFLNK);
121 info("link '%s' points to different target '%s', delete it", slink, buf);
126 info("creating symlink '%s' to '%s'", slink, target);
127 selinux_setfscreatecon(slink, NULL, S_IFLNK);
128 if (symlink(target, slink) != 0)
129 err("symlink(%s, %s) failed: %s", target, slink, strerror(errno));
130 selinux_resetfscreatecon();
136 int udev_node_add(struct udevice *udev, struct udevice *udev_old)
138 char filename[PATH_SIZE];
144 snprintf(filename, sizeof(filename), "%s/%s", udev_root, udev->name);
145 filename[sizeof(filename)-1] = '\0';
147 /* create parent directories if needed */
148 create_path(filename);
150 if (strcmp(udev->owner, "root") == 0)
156 id = strtoul(udev->owner, &endptr, 10);
157 if (endptr[0] == '\0')
160 uid = lookup_user(udev->owner);
163 if (strcmp(udev->group, "root") == 0)
169 id = strtoul(udev->group, &endptr, 10);
170 if (endptr[0] == '\0')
173 gid = lookup_group(udev->group);
176 info("creating device node '%s', major = '%d', minor = '%d', " "mode = '%#o', uid = '%d', gid = '%d'",
177 filename, major(udev->devt), minor(udev->devt), udev->mode, uid, gid);
180 if (udev_node_mknod(udev, filename, udev->devt, udev->mode, uid, gid) != 0) {
185 setenv("DEVNAME", filename, 1);
187 /* create all_partitions if requested */
188 if (udev->partitions) {
189 char partitionname[PATH_SIZE];
193 /* take the maximum registered minor range */
194 attr = sysfs_attr_get_value(udev->dev->devpath, "range");
198 udev->partitions = range-1;
200 info("creating device partition nodes '%s[1-%i]'", filename, udev->partitions);
201 if (!udev->test_run) {
202 for (i = 1; i <= udev->partitions; i++) {
205 snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
206 partitionname[sizeof(partitionname)-1] = '\0';
207 part_devt = makedev(major(udev->devt), minor(udev->devt) + i);
208 udev_node_mknod(udev, partitionname, part_devt, udev->mode, uid, gid);
213 /* create symlink(s) if requested */
214 if (!list_empty(&udev->symlink_list)) {
215 struct name_entry *name_loop;
216 char symlinks[PATH_SIZE] = "";
218 list_for_each_entry(name_loop, &udev->symlink_list, node) {
219 char slink[PATH_SIZE];
221 strlcpy(slink, udev_root, sizeof(slink));
222 strlcat(slink, "/", sizeof(slink));
223 strlcat(slink, name_loop->name, sizeof(slink));
225 info("creating symlink '%s' to node '%s'", slink, filename);
226 if (!udev->test_run) {
228 node_symlink(filename, slink);
231 strlcat(symlinks, slink, sizeof(symlinks));
232 strlcat(symlinks, " ", sizeof(symlinks));
235 remove_trailing_chars(symlinks, ' ');
236 setenv("DEVLINKS", symlinks, 1);
243 void udev_node_remove_symlinks(struct udevice *udev)
245 char filename[PATH_SIZE];
246 struct name_entry *name_loop;
249 if (!list_empty(&udev->symlink_list)) {
250 char symlinks[PATH_SIZE] = "";
252 list_for_each_entry(name_loop, &udev->symlink_list, node) {
253 char devpath[PATH_SIZE];
255 snprintf(filename, sizeof(filename), "%s/%s", udev_root, name_loop->name);
256 filename[sizeof(filename)-1] = '\0';
258 if (stat(filename, &stats) != 0) {
259 dbg("symlink '%s' not found", filename);
262 if (udev->devt && stats.st_rdev != udev->devt) {
263 info("symlink '%s' points to a different device, skip removal", filename);
267 info("removing symlink '%s'", filename);
268 if (!udev->test_run) {
270 delete_path(filename);
273 /* see if another device wants this symlink */
274 if (udev_db_lookup_name(name_loop->name, devpath, sizeof(devpath)) == 0) {
277 info("found overwritten symlink '%s' of '%s'", name_loop->name, devpath);
278 old = udev_device_init();
280 if (udev_db_get_device(old, devpath) == 0) {
281 char slink[PATH_SIZE];
282 char node[PATH_SIZE];
284 strlcpy(slink, udev_root, sizeof(slink));
285 strlcat(slink, "/", sizeof(slink));
286 strlcat(slink, name_loop->name, sizeof(slink));
287 strlcpy(node, udev_root, sizeof(node));
288 strlcat(node, "/", sizeof(node));
289 strlcat(node, old->name, sizeof(node));
290 info("restore symlink '%s' to '%s'", slink, node);
292 node_symlink(node, slink);
294 udev_device_cleanup(old);
298 strlcat(symlinks, filename, sizeof(symlinks));
299 strlcat(symlinks, " ", sizeof(symlinks));
302 remove_trailing_chars(symlinks, ' ');
303 if (symlinks[0] != '\0')
304 setenv("DEVLINKS", symlinks, 1);
308 int udev_node_remove(struct udevice *udev)
310 char filename[PATH_SIZE];
311 char partitionname[PATH_SIZE];
316 udev_node_remove_symlinks(udev);
318 snprintf(filename, sizeof(filename), "%s/%s", udev_root, udev->name);
319 filename[sizeof(filename)-1] = '\0';
320 if (stat(filename, &stats) != 0) {
321 dbg("device node '%s' not found", filename);
324 if (udev->devt && stats.st_rdev != udev->devt) {
325 info("device node '%s' points to a different device, skip removal", filename);
329 info("removing device node '%s'", filename);
330 retval = unlink_secure(filename);
334 setenv("DEVNAME", filename, 1);
335 num = udev->partitions;
339 info("removing all_partitions '%s[1-%i]'", filename, num);
341 info("garbage from udev database, skip all_partitions removal");
344 for (i = 1; i <= num; i++) {
345 snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
346 partitionname[sizeof(partitionname)-1] = '\0';
347 unlink_secure(partitionname);
350 delete_path(filename);