chiark / gitweb /
add flag for reading of precompiled rules
[elogind.git] / udev_db.c
1 /*
2  * udev_db.c
3  *
4  * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
5  * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
6  *
7  *      This program is free software; you can redistribute it and/or modify it
8  *      under the terms of the GNU General Public License as published by the
9  *      Free Software Foundation version 2 of the License.
10  * 
11  *      This program is distributed in the hope that it will be useful, but
12  *      WITHOUT ANY WARRANTY; without even the implied warranty of
13  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *      General Public License for more details.
15  * 
16  *      You should have received a copy of the GNU General Public License along
17  *      with this program; if not, write to the Free Software Foundation, Inc.,
18  *      675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */
21
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stddef.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <dirent.h>
32
33 #include "libsysfs/sysfs/libsysfs.h"
34 #include "udev_libc_wrapper.h"
35 #include "udev.h"
36 #include "udev_utils.h"
37 #include "logging.h"
38 #include "udev_db.h"
39
40 #define PATH_TO_NAME_CHAR               '@'
41
42 static int get_db_filename(const char *devpath, char *filename, int len)
43 {
44         char temp[PATH_SIZE];
45         char *pos;
46
47         /* replace '/' to transform path into a filename */
48         strlcpy(temp, devpath, sizeof(temp));
49         pos = strchr(&temp[1], '/');
50         while (pos) {
51                 pos[0] = PATH_TO_NAME_CHAR;
52                 pos = strchr(&pos[1], '/');
53         }
54         snprintf(filename, len, "%s%s", udev_db_path, temp);
55         filename[len-1] = '\0';
56
57         return 0;
58 }
59
60 int udev_db_add_device(struct udevice *udev)
61 {
62         char filename[PATH_SIZE];
63         struct name_entry *name_loop;
64         FILE *f;
65
66         if (udev->test_run)
67                 return 0;
68
69         /* don't write anything if udev created only the node with the
70          * kernel name without any interesting data to remember
71          */
72         if (strcmp(udev->name, udev->kernel_name) == 0 &&
73             list_empty(&udev->symlink_list) && list_empty(&udev->env_list) &&
74             !udev->partitions && !udev->ignore_remove) {
75                 dbg("nothing interesting to store in udevdb, skip");
76                 goto exit;
77         }
78
79         get_db_filename(udev->devpath, filename, sizeof(filename));
80         create_path(filename);
81         f = fopen(filename, "w");
82         if (f == NULL) {
83                 err("unable to create db file '%s'", filename);
84                 return -1;
85         }
86         dbg("storing data for device '%s' in '%s'", udev->devpath, filename);
87
88         fprintf(f, "P:%s\n", udev->devpath);
89         fprintf(f, "N:%s\n", udev->name);
90         list_for_each_entry(name_loop, &udev->symlink_list, node)
91                 fprintf(f, "S:%s\n", name_loop->name);
92         fprintf(f, "M:%u:%u\n", major(udev->devt), minor(udev->devt));
93         if (udev->partitions)
94                 fprintf(f, "A:%u\n", udev->partitions);
95         if (udev->ignore_remove)
96                 fprintf(f, "R:%u\n", udev->ignore_remove);
97         list_for_each_entry(name_loop, &udev->env_list, node)
98                 fprintf(f, "E:%s\n", name_loop->name);
99         fclose(f);
100
101 exit:
102         return 0;
103 }
104
105 static int parse_db_file(struct udevice *udev, const char *filename)
106 {
107         char line[PATH_SIZE];
108         unsigned int major, minor;
109         char *bufline;
110         char *buf;
111         size_t bufsize;
112         size_t cur;
113         size_t count;
114
115         if (file_map(filename, &buf, &bufsize) != 0) {
116                 err("unable to read db file '%s'", filename);
117                 return -1;
118         }
119
120         cur = 0;
121         while (cur < bufsize) {
122                 count = buf_get_line(buf, bufsize, cur);
123                 bufline = &buf[cur];
124                 cur += count+1;
125
126                 switch(bufline[0]) {
127                 case 'P':
128                         if (count > sizeof(udev->devpath))
129                                 count = sizeof(udev->devpath);
130                         memcpy(udev->devpath, &bufline[2], count-2);
131                         udev->devpath[count-2] = '\0';
132                         break;
133                 case 'N':
134                         if (count > sizeof(udev->name))
135                                 count = sizeof(udev->name);
136                         memcpy(udev->name, &bufline[2], count-2);
137                         udev->name[count-2] = '\0';
138                         break;
139                 case 'M':
140                         if (count > sizeof(line))
141                                 count = sizeof(line);
142                         memcpy(line, &bufline[2], count-2);
143                         line[count-2] = '\0';
144                         sscanf(line, "%u:%u", &major, &minor);
145                         udev->devt = makedev(major, minor);
146                         break;
147                 case 'S':
148                         if (count > sizeof(line))
149                                 count =  sizeof(line);
150                         memcpy(line, &bufline[2], count-2);
151                         line[count-2] = '\0';
152                         name_list_add(&udev->symlink_list, line, 0);
153                         break;
154                 case 'A':
155                         if (count > sizeof(line))
156                                 count =  sizeof(line);
157                         memcpy(line, &bufline[2], count-2);
158                         line[count-2] = '\0';
159                         udev->partitions = atoi(line);
160                         break;
161                 case 'R':
162                         if (count > sizeof(line))
163                                 count =  sizeof(line);
164                         memcpy(line, &bufline[2], count-2);
165                         line[count-2] = '\0';
166                         udev->ignore_remove = atoi(line);
167                         break;
168                 case 'E':
169                         if (count > sizeof(line))
170                                 count =  sizeof(line);
171                         memcpy(line, &bufline[2], count-2);
172                         line[count-2] = '\0';
173                         name_list_add(&udev->env_list, line, 0);
174                         break;
175                 }
176         }
177         file_unmap(buf, bufsize);
178
179         if (udev->name[0] == '\0')
180                 return -1;
181
182         return 0;
183 }
184
185 int udev_db_delete_device(struct udevice *udev)
186 {
187         char filename[PATH_SIZE];
188
189         get_db_filename(udev->devpath, filename, sizeof(filename));
190         unlink(filename);
191
192         return 0;
193 }
194
195 int udev_db_get_device(struct udevice *udev, const char *devpath)
196 {
197         char filename[PATH_SIZE];
198
199         get_db_filename(devpath, filename, sizeof(filename));
200
201         if (parse_db_file(udev, filename) != 0)
202                 return -1;
203
204         return 0;
205 }
206
207 int udev_db_search_name(char *devpath, size_t len, const char *name)
208 {
209         DIR *dir;
210
211         dir = opendir(udev_db_path);
212         if (dir == NULL) {
213                 err("unable to open udev_db '%s'", udev_db_path);
214                 return -1;
215         }
216
217         while (1) {
218                 struct dirent *ent;
219                 char filename[PATH_SIZE];
220                 char path[PATH_SIZE];
221                 char nodename[PATH_SIZE];
222                 char *bufline;
223                 char *buf;
224                 size_t bufsize;
225                 size_t cur;
226                 size_t count;
227
228                 ent = readdir(dir);
229                 if (ent == NULL || ent->d_name[0] == '\0')
230                         break;
231
232                 if (ent->d_name[0] == '.')
233                         continue;
234
235                 snprintf(filename, sizeof(filename), "%s/%s", udev_db_path, ent->d_name);
236                 filename[sizeof(filename)-1] = '\0';
237                 dbg("looking at '%s'", filename);
238
239                 if (file_map(filename, &buf, &bufsize) != 0) {
240                         err("unable to read db file '%s'", filename);
241                         continue;
242                 }
243
244                 cur = 0;
245                 while (cur < bufsize) {
246                         count = buf_get_line(buf, bufsize, cur);
247                         bufline = &buf[cur];
248                         cur += count+1;
249
250                         switch(bufline[0]) {
251                         case 'P':
252                                 if (count > sizeof(path))
253                                         count = sizeof(path);
254                                 memcpy(path, &bufline[2], count-2);
255                                 path[count-2] = '\0';
256                                 break;
257                         case 'N':
258                         case 'S':
259                                 if (count > sizeof(nodename))
260                                         count = sizeof(nodename);
261                                 memcpy(nodename, &bufline[2], count-2);
262                                 nodename[count-2] = '\0';
263                                 dbg("compare '%s' '%s'", nodename, name);
264                                 if (strcmp(nodename, name) == 0) {
265                                         memcpy(devpath, &bufline[2], count-2);
266                                         devpath[count-2] = '\0';
267                                         file_unmap(buf, bufsize);
268                                         closedir(dir);
269                                         return 0;
270                                 }
271                                 break;
272                         default:
273                                 continue;
274                         }
275                 }
276                 file_unmap(buf, bufsize);
277         }
278
279         closedir(dir);
280         return -1;
281 }
282
283 int udev_db_dump_names(int (*handler_function)(const char *path, const char *name))
284 {
285         DIR *dir;
286
287         dir = opendir(udev_db_path);
288         if (dir == NULL) {
289                 err("unable to open udev_db '%s'", udev_db_path);
290                 return -1;
291         }
292
293         while (1) {
294                 struct dirent *ent;
295                 char filename[PATH_SIZE];
296                 char path[PATH_SIZE];
297                 char nodename[PATH_SIZE];
298                 char *bufline;
299                 char *buf;
300                 size_t bufsize;
301                 size_t cur;
302                 size_t count;
303
304                 ent = readdir(dir);
305                 if (ent == NULL || ent->d_name[0] == '\0')
306                         break;
307
308                 if (ent->d_name[0] == '.')
309                         continue;
310
311                 snprintf(filename, sizeof(filename), "%s/%s", udev_db_path, ent->d_name);
312                 filename[sizeof(filename)-1] = '\0';
313                 dbg("looking at '%s'", filename);
314
315                 if (file_map(filename, &buf, &bufsize) != 0) {
316                         err("unable to read db file '%s'", filename);
317                         continue;
318                 }
319
320                 path[0] = '\0';
321                 nodename[0] = '\0';
322                 cur = 0;
323                 while (cur < bufsize) {
324                         count = buf_get_line(buf, bufsize, cur);
325                         bufline = &buf[cur];
326                         cur += count+1;
327
328                         switch(bufline[0]) {
329                         case 'P':
330                                 if (count > sizeof(path))
331                                         count = sizeof(path);
332                                 memcpy(path, &bufline[2], count-2);
333                                 path[count-2] = '\0';
334                                 break;
335                         case 'N':
336                                 if (count > sizeof(nodename))
337                                         count = sizeof(nodename);
338                                 memcpy(nodename, &bufline[2], count-2);
339                                 nodename[count-2] = '\0';
340                                 break;
341                         default:
342                                 continue;
343                         }
344                 }
345                 file_unmap(buf, bufsize);
346
347                 if (path[0] == '\0' || nodename[0] == '\0')
348                         continue;
349
350                 if (handler_function(path, nodename) != 0)
351                         break;
352         }
353
354         closedir(dir);
355         return 0;
356 }