chiark / gitweb /
autogen.sh: add debug
[elogind.git] / udev / udev_utils_file.c
1 /*
2  * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
3  *
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.
7  * 
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.
12  * 
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.
16  *
17  */
18
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <stddef.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <fcntl.h>
26 #include <errno.h>
27 #include <ctype.h>
28 #include <dirent.h>
29 #include <sys/stat.h>
30 #include <sys/mman.h>
31
32 #include "udev.h"
33 #include "udev_selinux.h"
34
35 int create_path(const char *path)
36 {
37         char p[PATH_SIZE];
38         char *pos;
39         struct stat stats;
40         int ret;
41
42         strlcpy(p, path, sizeof(p));
43         pos = strrchr(p, '/');
44         if (pos == p || pos == NULL)
45                 return 0;
46
47         while (pos[-1] == '/')
48                 pos--;
49         pos[0] = '\0';
50
51         dbg("stat '%s'\n", p);
52         if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
53                 return 0;
54
55         if (create_path(p) != 0)
56                 return -1;
57
58         dbg("mkdir '%s'\n", p);
59         selinux_setfscreatecon(p, NULL, S_IFDIR|0755);
60         ret = mkdir(p, 0755);
61         selinux_resetfscreatecon();
62         if (ret == 0)
63                 return 0;
64
65         if (errno == EEXIST)
66                 if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
67                         return 0;
68         return -1;
69 }
70
71 int delete_path(const char *path)
72 {
73         char p[PATH_SIZE];
74         char *pos;
75         int retval;
76
77         strcpy (p, path);
78         pos = strrchr(p, '/');
79         if (pos == p || pos == NULL)
80                 return 0;
81
82         while (1) {
83                 *pos = '\0';
84                 pos = strrchr(p, '/');
85
86                 /* don't remove the last one */
87                 if ((pos == p) || (pos == NULL))
88                         break;
89
90                 /* remove if empty */
91                 retval = rmdir(p);
92                 if (errno == ENOENT)
93                         retval = 0;
94                 if (retval) {
95                         if (errno == ENOTEMPTY)
96                                 return 0;
97                         err("rmdir(%s) failed: %s\n", p, strerror(errno));
98                         break;
99                 }
100                 dbg("removed '%s'\n", p);
101         }
102         return 0;
103 }
104
105 /* Reset permissions on the device node, before unlinking it to make sure,
106  * that permisions of possible hard links will be removed too.
107  */
108 int unlink_secure(const char *filename)
109 {
110         int retval;
111
112         retval = chown(filename, 0, 0);
113         if (retval)
114                 err("chown(%s, 0, 0) failed: %s\n", filename, strerror(errno));
115
116         retval = chmod(filename, 0000);
117         if (retval)
118                 err("chmod(%s, 0000) failed: %s\n", filename, strerror(errno));
119
120         retval = unlink(filename);
121         if (errno == ENOENT)
122                 retval = 0;
123
124         if (retval)
125                 err("unlink(%s) failed: %s\n", filename, strerror(errno));
126
127         return retval;
128 }
129
130 int file_map(const char *filename, char **buf, size_t *bufsize)
131 {
132         struct stat stats;
133         int fd;
134
135         fd = open(filename, O_RDONLY);
136         if (fd < 0) {
137                 return -1;
138         }
139
140         if (fstat(fd, &stats) < 0) {
141                 close(fd);
142                 return -1;
143         }
144
145         *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
146         if (*buf == MAP_FAILED) {
147                 close(fd);
148                 return -1;
149         }
150         *bufsize = stats.st_size;
151
152         close(fd);
153
154         return 0;
155 }
156
157 void file_unmap(void *buf, size_t bufsize)
158 {
159         munmap(buf, bufsize);
160 }
161
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)
164 {
165         int escape = 0;
166         size_t count;
167
168         for (count = cur; count < buflen; count++) {
169                 if (!escape && buf[count] == '\n')
170                         break;
171
172                 if (buf[count] == '\\')
173                         escape = 1;
174                 else
175                         escape = 0;
176         }
177
178         return count - cur;
179 }