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.
32 #include "udev_selinux.h"
34 int create_path(const char *path)
41 strlcpy(p, path, sizeof(p));
42 pos = strrchr(p, '/');
43 if (pos == p || pos == NULL)
46 while (pos[-1] == '/')
50 dbg("stat '%s'\n", p);
51 if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
54 if (create_path(p) != 0)
57 dbg("mkdir '%s'\n", p);
58 selinux_setfscreatecon(p, NULL, S_IFDIR|0755);
60 selinux_resetfscreatecon();
65 if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
70 int delete_path(const char *path)
77 pos = strrchr(p, '/');
78 if (pos == p || pos == NULL)
83 pos = strrchr(p, '/');
85 /* don't remove the last one */
86 if ((pos == p) || (pos == NULL))
94 if (errno == ENOTEMPTY)
96 err("rmdir(%s) failed: %s\n", p, strerror(errno));
99 dbg("removed '%s'\n", p);
104 /* Reset permissions on the device node, before unlinking it to make sure,
105 * that permisions of possible hard links will be removed too.
107 int unlink_secure(const char *filename)
111 retval = chown(filename, 0, 0);
113 err("chown(%s, 0, 0) failed: %s\n", filename, strerror(errno));
115 retval = chmod(filename, 0000);
117 err("chmod(%s, 0000) failed: %s\n", filename, strerror(errno));
119 retval = unlink(filename);
124 err("unlink(%s) failed: %s\n", filename, strerror(errno));
129 int file_map(const char *filename, char **buf, size_t *bufsize)
134 fd = open(filename, O_RDONLY);
139 if (fstat(fd, &stats) < 0) {
144 *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
145 if (*buf == MAP_FAILED) {
149 *bufsize = stats.st_size;
156 void file_unmap(void *buf, size_t bufsize)
158 munmap(buf, bufsize);
161 /* return number of chars until the next newline, skip escaped newline */
162 size_t buf_get_line(const char *buf, size_t buflen, size_t cur)
167 for (count = cur; count < buflen; count++) {
168 if (!escape && buf[count] == '\n')
171 if (buf[count] == '\\')