chiark / gitweb /
9b40a5904e751d531490e8cc2398a8c499b2ef2f
[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                 dbg("no db file to read '%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         char path[PATH_SIZE];
211         int found = 0;
212
213         dir = opendir(udev_db_path);
214         if (dir == NULL) {
215                 err("unable to open udev_db '%s'", udev_db_path);
216                 return -1;
217         }
218
219         while (!found) {
220                 struct dirent *ent;
221                 char filename[PATH_SIZE];
222                 char nodename[PATH_SIZE];
223                 char *bufline;
224                 char *buf;
225                 size_t bufsize;
226                 size_t cur;
227                 size_t count;
228
229                 ent = readdir(dir);
230                 if (ent == NULL || ent->d_name[0] == '\0')
231                         break;
232
233                 if (ent->d_name[0] == '.')
234                         continue;
235
236                 snprintf(filename, sizeof(filename), "%s/%s", udev_db_path, ent->d_name);
237                 filename[sizeof(filename)-1] = '\0';
238                 dbg("looking at '%s'", filename);
239
240                 if (file_map(filename, &buf, &bufsize) != 0) {
241                         err("unable to read db file '%s'", filename);
242                         continue;
243                 }
244
245                 cur = 0;
246                 while (cur < bufsize && !found) {
247                         count = buf_get_line(buf, bufsize, cur);
248                         bufline = &buf[cur];
249                         cur += count+1;
250
251                         switch(bufline[0]) {
252                         case 'P':
253                                 if (count > sizeof(path))
254                                         count = sizeof(path);
255                                 memcpy(path, &bufline[2], count-2);
256                                 path[count-2] = '\0';
257                                 break;
258                         case 'N':
259                         case 'S':
260                                 if (count > sizeof(nodename))
261                                         count = sizeof(nodename);
262                                 memcpy(nodename, &bufline[2], count-2);
263                                 nodename[count-2] = '\0';
264                                 dbg("compare '%s' '%s'", nodename, name);
265                                 if (strcmp(nodename, name) == 0) {
266                                         found = 1;
267                                         break;
268                                 }
269                                 break;
270                         default:
271                                 continue;
272                         }
273                 }
274                 file_unmap(buf, bufsize);
275         }
276
277         closedir(dir);
278         if (found) {
279                 strlcpy(devpath, path, len);
280                 return 0;
281         } else
282                 return -1;
283 }
284
285 int udev_db_dump_names(int (*handler_function)(const char *path, const char *name))
286 {
287         DIR *dir;
288
289         dir = opendir(udev_db_path);
290         if (dir == NULL) {
291                 err("unable to open udev_db '%s'", udev_db_path);
292                 return -1;
293         }
294
295         while (1) {
296                 struct dirent *ent;
297                 char filename[PATH_SIZE];
298                 char path[PATH_SIZE];
299                 char nodename[PATH_SIZE];
300                 char *bufline;
301                 char *buf;
302                 size_t bufsize;
303                 size_t cur;
304                 size_t count;
305
306                 ent = readdir(dir);
307                 if (ent == NULL || ent->d_name[0] == '\0')
308                         break;
309
310                 if (ent->d_name[0] == '.')
311                         continue;
312
313                 snprintf(filename, sizeof(filename), "%s/%s", udev_db_path, ent->d_name);
314                 filename[sizeof(filename)-1] = '\0';
315                 dbg("looking at '%s'", filename);
316
317                 if (file_map(filename, &buf, &bufsize) != 0) {
318                         err("unable to read db file '%s'", filename);
319                         continue;
320                 }
321
322                 path[0] = '\0';
323                 nodename[0] = '\0';
324                 cur = 0;
325                 while (cur < bufsize) {
326                         count = buf_get_line(buf, bufsize, cur);
327                         bufline = &buf[cur];
328                         cur += count+1;
329
330                         switch(bufline[0]) {
331                         case 'P':
332                                 if (count > sizeof(path))
333                                         count = sizeof(path);
334                                 memcpy(path, &bufline[2], count-2);
335                                 path[count-2] = '\0';
336                                 break;
337                         case 'N':
338                                 if (count > sizeof(nodename))
339                                         count = sizeof(nodename);
340                                 memcpy(nodename, &bufline[2], count-2);
341                                 nodename[count-2] = '\0';
342                                 break;
343                         default:
344                                 continue;
345                         }
346                 }
347                 file_unmap(buf, bufsize);
348
349                 if (path[0] == '\0' || nodename[0] == '\0')
350                         continue;
351
352                 if (handler_function(path, nodename) != 0)
353                         break;
354         }
355
356         closedir(dir);
357         return 0;
358 }