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