2 * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
33 #include "udev_selinux.h"
35 int create_path(struct udev *udev, const char *path)
42 strlcpy(p, path, sizeof(p));
43 pos = strrchr(p, '/');
44 if (pos == p || pos == NULL)
47 while (pos[-1] == '/')
51 dbg(udev, "stat '%s'\n", p);
52 if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
55 if (create_path(udev, p) != 0)
58 dbg(udev, "mkdir '%s'\n", p);
59 selinux_setfscreatecon(udev, p, NULL, S_IFDIR|0755);
61 selinux_resetfscreatecon(udev);
66 if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
71 int delete_path(struct udev *udev, const char *path)
78 pos = strrchr(p, '/');
79 if (pos == p || pos == NULL)
84 pos = strrchr(p, '/');
86 /* don't remove the last one */
87 if ((pos == p) || (pos == NULL))
95 if (errno == ENOTEMPTY)
97 err(udev, "rmdir(%s) failed: %s\n", p, strerror(errno));
100 dbg(udev, "removed '%s'\n", p);
105 /* Reset permissions on the device node, before unlinking it to make sure,
106 * that permisions of possible hard links will be removed too.
108 int unlink_secure(struct udev *udev, const char *filename)
112 retval = chown(filename, 0, 0);
114 err(udev, "chown(%s, 0, 0) failed: %s\n", filename, strerror(errno));
116 retval = chmod(filename, 0000);
118 err(udev, "chmod(%s, 0000) failed: %s\n", filename, strerror(errno));
120 retval = unlink(filename);
125 err(udev, "unlink(%s) failed: %s\n", filename, strerror(errno));
130 int file_map(const char *filename, char **buf, size_t *bufsize)
135 fd = open(filename, O_RDONLY);
140 if (fstat(fd, &stats) < 0) {
145 *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
146 if (*buf == MAP_FAILED) {
150 *bufsize = stats.st_size;
157 void file_unmap(void *buf, size_t bufsize)
159 munmap(buf, bufsize);
162 /* return number of chars until the next newline, skip escaped newline */
163 size_t buf_get_line(const char *buf, size_t buflen, size_t cur)
168 for (count = cur; count < buflen; count++) {
169 if (!escape && buf[count] == '\n')
172 if (buf[count] == '\\')