chiark / gitweb /
[PATCH] selinux: cleanup udev integration
[elogind.git] / udev_utils.c
1 /*
2  * udev_lib - generic stuff used by udev
3  *
4  * Copyright (C) 2004 Kay Sievers <kay@vrfy.org>
5  *
6  *      This program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  * 
10  *      This program is distributed in the hope that it will be useful, but
11  *      WITHOUT ANY WARRANTY; without even the implied warranty of
12  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *      General Public License for more details.
14  * 
15  *      You should have received a copy of the GNU General Public License along
16  *      with this program; if not, write to the Free Software Foundation, Inc.,
17  *      675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  */
20
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <stddef.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <ctype.h>
29 #include <dirent.h>
30 #include <sys/stat.h>
31 #include <sys/mman.h>
32 #include <sys/utsname.h>
33
34 #include "udev.h"
35 #include "logging.h"
36 #include "udev_utils.h"
37 #include "list.h"
38
39
40 void udev_init_device(struct udevice *udev, const char* devpath, const char *subsystem)
41 {
42         memset(udev, 0x00, sizeof(struct udevice));
43
44         if (devpath)
45                 strfieldcpy(udev->devpath, devpath);
46         if (subsystem)
47                 strfieldcpy(udev->subsystem, subsystem);
48
49         if (strcmp(udev->subsystem, "block") == 0)
50                 udev->type = 'b';
51         else if (strcmp(udev->subsystem, "net") == 0)
52                 udev->type = 'n';
53         else if (strncmp(udev->devpath, "/block/", 7) == 0)
54                 udev->type = 'b';
55         else if (strncmp(udev->devpath, "/class/net/", 11) == 0)
56                 udev->type = 'n';
57         else if (strncmp(udev->devpath, "/class/", 7) == 0)
58                 udev->type = 'c';
59
60         udev->mode = 0660;
61         strcpy(udev->owner, "root");
62         strcpy(udev->group, "root");
63 }
64
65 int kernel_release_satisfactory(int version, int patchlevel, int sublevel)
66 {
67         static int kversion = 0;
68         static int kpatchlevel;
69         static int ksublevel;
70
71         if (kversion == 0) {
72                 struct utsname uts;
73                 if (uname(&uts) != 0)
74                         return -1;
75
76                 if (sscanf (uts.release, "%u.%u.%u", &kversion, &kpatchlevel, &ksublevel) != 3) {
77                         kversion = 0;
78                         return -1;
79                 }
80         }
81
82         if (kversion >= version && kpatchlevel >= patchlevel && ksublevel >= sublevel)
83                 return 1;
84         else
85                 return 0;
86 }
87
88 int create_path(const char *path)
89 {
90         char p[NAME_SIZE];
91         char *pos;
92         struct stat stats;
93
94         strcpy (p, path);
95         pos = strrchr(p, '/');
96         if (pos == p || pos == NULL)
97                 return 0;
98
99         while (pos[-1] == '/')
100                 pos--;
101
102         pos[0] = '\0';
103
104         dbg("stat '%s'\n", p);
105         if (stat (p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
106                 return 0;
107
108         if (create_path (p) != 0)
109                 return -1;
110
111         dbg("mkdir '%s'\n", p);
112         return mkdir(p, 0755);
113 }
114
115 int parse_get_pair(char **orig_string, char **left, char **right)
116 {
117         char *temp;
118         char *string = *orig_string;
119
120         if (!string)
121                 return -ENODEV;
122
123         /* eat any whitespace */
124         while (isspace(*string) || *string == ',')
125                 ++string;
126
127         /* split based on '=' */
128         temp = strsep(&string, "=");
129         *left = temp;
130         if (!string)
131                 return -ENODEV;
132
133         /* take the right side and strip off the '"' */
134         while (isspace(*string))
135                 ++string;
136         if (*string == '"')
137                 ++string;
138         else
139                 return -ENODEV;
140
141         temp = strsep(&string, "\"");
142         if (!string || *temp == '\0')
143                 return -ENODEV;
144         *right = temp;
145         *orig_string = string;
146         
147         return 0;
148 }
149
150 int file_map(const char *filename, char **buf, size_t *bufsize)
151 {
152         struct stat stats;
153         int fd;
154
155         fd = open(filename, O_RDONLY);
156         if (fd < 0) {
157                 return -1;
158         }
159
160         if (fstat(fd, &stats) < 0) {
161                 close(fd);
162                 return -1;
163         }
164
165         *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
166         if (*buf == MAP_FAILED) {
167                 close(fd);
168                 return -1;
169         }
170         *bufsize = stats.st_size;
171
172         close(fd);
173
174         return 0;
175 }
176
177 void file_unmap(char *buf, size_t bufsize)
178 {
179         munmap(buf, bufsize);
180 }
181
182 /* return number of chars until the next newline, skip escaped newline */
183 size_t buf_get_line(const char *buf, size_t buflen, size_t cur)
184 {
185         int escape = 0;
186         size_t count;
187
188         for (count = cur; count < buflen; count++) {
189                 if (!escape && buf[count] == '\n')
190                         break;
191
192                 if (buf[count] == '\\')
193                         escape = 1;
194                 else
195                         escape = 0;
196         }
197
198         return count - cur;
199 }
200
201 void no_trailing_slash(char *path)
202 {
203         int len;
204
205         len = strlen(path);
206         if (len > 0 && path[len-1] == '/')
207                 path[len-1] = '\0';
208 }
209
210 struct files {
211         struct list_head list;
212         char name[NAME_SIZE];
213 };
214
215 /* sort files in lexical order */
216 static int file_list_insert(char *filename, struct list_head *file_list)
217 {
218         struct files *loop_file;
219         struct files *new_file;
220
221         list_for_each_entry(loop_file, file_list, list) {
222                 if (strcmp(loop_file->name, filename) > 0) {
223                         break;
224                 }
225         }
226
227         new_file = malloc(sizeof(struct files));
228         if (new_file == NULL) {
229                 dbg("error malloc");
230                 return -ENOMEM;
231         }
232
233         strfieldcpy(new_file->name, filename);
234         list_add_tail(&new_file->list, &loop_file->list);
235         return 0;
236 }
237
238 /* calls function for every file found in specified directory */
239 int call_foreach_file(file_fnct_t fnct, const char *dirname,
240                       const char *suffix, void *data)
241 {
242         struct dirent *ent;
243         DIR *dir;
244         char *ext;
245         struct files *loop_file;
246         struct files *tmp_file;
247         LIST_HEAD(file_list);
248
249         dbg("open directory '%s'", dirname);
250         dir = opendir(dirname);
251         if (dir == NULL) {
252                 dbg("unable to open '%s'", dirname);
253                 return -1;
254         }
255
256         while (1) {
257                 ent = readdir(dir);
258                 if (ent == NULL || ent->d_name[0] == '\0')
259                         break;
260
261                 if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER))
262                         continue;
263
264                 /* look for file with specified suffix */
265                 ext = strrchr(ent->d_name, '.');
266                 if (ext == NULL)
267                         continue;
268
269                 if (strcmp(ext, suffix) != 0)
270                         continue;
271
272                 dbg("put file '%s/%s' in list", dirname, ent->d_name);
273                 file_list_insert(ent->d_name, &file_list);
274         }
275
276         /* call function for every file in the list */
277         list_for_each_entry_safe(loop_file, tmp_file, &file_list, list) {
278                 char filename[NAME_SIZE];
279
280                 snprintf(filename, NAME_SIZE, "%s/%s", dirname, loop_file->name);
281                 filename[NAME_SIZE-1] = '\0';
282
283                 fnct(filename, data);
284
285                 list_del(&loop_file->list);
286                 free(loop_file);
287         }
288
289         closedir(dir);
290         return 0;
291 }