chiark / gitweb /
ef0e9a71ae8471131a1b39f2136f43c638e84bf7
[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                         strlcpy(udev->devpath, &bufline[2], count-1);
131                         break;
132                 case 'N':
133                         if (count > sizeof(udev->name))
134                                 count = sizeof(udev->name);
135                         strlcpy(udev->name, &bufline[2], count-1);
136                         break;
137                 case 'M':
138                         if (count > sizeof(line))
139                                 count = sizeof(line);
140                         strlcpy(line, &bufline[2], count-1);
141                         sscanf(line, "%u:%u", &major, &minor);
142                         udev->devt = makedev(major, minor);
143                         break;
144                 case 'S':
145                         if (count > sizeof(line))
146                                 count =  sizeof(line);
147                         strlcpy(line, &bufline[2], count-1);
148                         name_list_add(&udev->symlink_list, line, 0);
149                         break;
150                 case 'A':
151                         if (count > sizeof(line))
152                                 count =  sizeof(line);
153                         strlcpy(line, &bufline[2], count-1);
154                         udev->partitions = atoi(line);
155                         break;
156                 case 'R':
157                         if (count > sizeof(line))
158                                 count =  sizeof(line);
159                         strlcpy(line, &bufline[2], count-1);
160                         udev->ignore_remove = atoi(line);
161                         break;
162                 case 'E':
163                         if (count > sizeof(line))
164                                 count =  sizeof(line);
165                         strlcpy(line, &bufline[2], count-1);
166                         name_list_add(&udev->env_list, line, 0);
167                         break;
168                 }
169         }
170         file_unmap(buf, bufsize);
171
172         if (udev->name[0] == '\0')
173                 return -1;
174
175         return 0;
176 }
177
178 int udev_db_delete_device(struct udevice *udev)
179 {
180         char filename[PATH_SIZE];
181
182         get_db_filename(udev->devpath, filename, sizeof(filename));
183         unlink(filename);
184
185         return 0;
186 }
187
188 int udev_db_get_device(struct udevice *udev, const char *devpath)
189 {
190         char filename[PATH_SIZE];
191
192         get_db_filename(devpath, filename, sizeof(filename));
193
194         if (parse_db_file(udev, filename) != 0)
195                 return -1;
196
197         return 0;
198 }
199
200 int udev_db_search_name(char *devpath, size_t len, const char *name)
201 {
202         DIR *dir;
203
204         dir = opendir(udev_db_path);
205         if (dir == NULL) {
206                 err("unable to open udev_db '%s'", udev_db_path);
207                 return -1;
208         }
209
210         while (1) {
211                 struct dirent *ent;
212                 char filename[PATH_SIZE];
213                 char path[PATH_SIZE];
214                 char nodename[PATH_SIZE];
215                 char *bufline;
216                 char *buf;
217                 size_t bufsize;
218                 size_t cur;
219                 size_t count;
220
221                 ent = readdir(dir);
222                 if (ent == NULL || ent->d_name[0] == '\0')
223                         break;
224
225                 if (ent->d_name[0] == '.')
226                         continue;
227
228                 snprintf(filename, sizeof(filename), "%s/%s", udev_db_path, ent->d_name);
229                 filename[sizeof(filename)-1] = '\0';
230                 dbg("looking at '%s'", filename);
231
232                 if (file_map(filename, &buf, &bufsize) != 0) {
233                         err("unable to read db file '%s'", filename);
234                         continue;
235                 }
236
237                 cur = 0;
238                 while (cur < bufsize) {
239                         count = buf_get_line(buf, bufsize, cur);
240                         bufline = &buf[cur];
241                         cur += count+1;
242
243                         switch(bufline[0]) {
244                         case 'P':
245                                 if (count > sizeof(path))
246                                         count = sizeof(path);
247                                 strlcpy(path, &bufline[2], count-1);
248                                 break;
249                         case 'N':
250                         case 'S':
251                                 if (count > sizeof(nodename))
252                                         count = sizeof(nodename);
253                                 strlcpy(nodename, &bufline[2], count-1);
254                                 dbg("compare '%s' '%s'", nodename, name);
255                                 if (strcmp(nodename, name) == 0) {
256                                         strlcpy(devpath, path, len);
257                                         file_unmap(buf, bufsize);
258                                         closedir(dir);
259                                         return 0;
260                                 }
261                                 break;
262                         default:
263                                 continue;
264                         }
265                 }
266                 file_unmap(buf, bufsize);
267         }
268
269         closedir(dir);
270         return -1;
271 }
272
273 int udev_db_dump_names(int (*handler_function)(const char *path, const char *name))
274 {
275         DIR *dir;
276
277         dir = opendir(udev_db_path);
278         if (dir == NULL) {
279                 err("unable to open udev_db '%s'", udev_db_path);
280                 return -1;
281         }
282
283         while (1) {
284                 struct dirent *ent;
285                 char filename[PATH_SIZE];
286                 char path[PATH_SIZE];
287                 char nodename[PATH_SIZE];
288                 char *bufline;
289                 char *buf;
290                 size_t bufsize;
291                 size_t cur;
292                 size_t count;
293
294                 ent = readdir(dir);
295                 if (ent == NULL || ent->d_name[0] == '\0')
296                         break;
297
298                 if (ent->d_name[0] == '.')
299                         continue;
300
301                 snprintf(filename, sizeof(filename), "%s/%s", udev_db_path, ent->d_name);
302                 filename[sizeof(filename)-1] = '\0';
303                 dbg("looking at '%s'", filename);
304
305                 if (file_map(filename, &buf, &bufsize) != 0) {
306                         err("unable to read db file '%s'", filename);
307                         continue;
308                 }
309
310                 path[0] = '\0';
311                 nodename[0] = '\0';
312                 cur = 0;
313                 while (cur < bufsize) {
314                         count = buf_get_line(buf, bufsize, cur);
315                         bufline = &buf[cur];
316                         cur += count+1;
317
318                         switch(bufline[0]) {
319                         case 'P':
320                                 if (count > sizeof(path))
321                                         count = sizeof(path);
322                                 strlcpy(path, &bufline[2], count-1);
323                                 break;
324                         case 'N':
325                                 if (count > sizeof(nodename))
326                                         count = sizeof(nodename);
327                                 strlcpy(nodename, &bufline[2], count-1);
328                                 break;
329                         default:
330                                 continue;
331                         }
332                 }
333                 file_unmap(buf, bufsize);
334
335                 if (path[0] == '\0' || nodename[0] == '\0')
336                         continue;
337
338                 if (handler_function(path, nodename) != 0)
339                         break;
340         }
341
342         closedir(dir);
343         return 0;
344 }