2 * Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 int create_path(struct udev *udev, const char *path)
33 char p[UTIL_PATH_SIZE];
38 util_strlcpy(p, path, sizeof(p));
39 pos = strrchr(p, '/');
40 if (pos == p || pos == NULL)
43 while (pos[-1] == '/')
47 dbg(udev, "stat '%s'\n", p);
48 if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
51 if (create_path(udev, p) != 0)
54 dbg(udev, "mkdir '%s'\n", p);
55 udev_selinux_setfscreatecon(udev, p, S_IFDIR|0755);
57 udev_selinux_resetfscreatecon(udev);
62 if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
67 int delete_path(struct udev *udev, const char *path)
69 char p[UTIL_PATH_SIZE];
74 pos = strrchr(p, '/');
75 if (pos == p || pos == NULL)
80 pos = strrchr(p, '/');
82 /* don't remove the last one */
83 if ((pos == p) || (pos == NULL))
91 if (errno == ENOTEMPTY)
93 err(udev, "rmdir(%s) failed: %m\n", p);
96 dbg(udev, "removed '%s'\n", p);
101 /* Reset permissions on the device node, before unlinking it to make sure,
102 * that permisions of possible hard links will be removed too.
104 int unlink_secure(struct udev *udev, const char *filename)
108 retval = chown(filename, 0, 0);
110 err(udev, "chown(%s, 0, 0) failed: %m\n", filename);
112 retval = chmod(filename, 0000);
114 err(udev, "chmod(%s, 0000) failed: %m\n", filename);
116 retval = unlink(filename);
121 err(udev, "unlink(%s) failed: %m\n", filename);
126 uid_t lookup_user(struct udev *udev, const char *user)
134 if (errno == 0 || errno == ENOENT || errno == ESRCH)
135 err(udev, "specified user '%s' unknown\n", user);
137 err(udev, "error resolving user '%s': %m\n", user);
144 extern gid_t lookup_group(struct udev *udev, const char *group)
150 gr = getgrnam(group);
152 if (errno == 0 || errno == ENOENT || errno == ESRCH)
153 err(udev, "specified group '%s' unknown\n", group);
155 err(udev, "error resolving group '%s': %m\n", group);