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