chiark / gitweb /
059 release
[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_libc_wrapper.h"
37 #include "udev.h"
38 #include "udev_utils.h"
39 #include "logging.h"
40 #include "udev_db.h"
41
42 #define PATH_TO_NAME_CHAR               '@'
43
44 static int get_db_filename(const char *devpath, char *filename, int len)
45 {
46         char temp[PATH_SIZE];
47         char *pos;
48
49         /* replace '/' to transform path into a filename */
50         strlcpy(temp, devpath, sizeof(temp));
51         pos = strchr(&temp[1], '/');
52         while (pos) {
53                 pos[0] = PATH_TO_NAME_CHAR;
54                 pos = strchr(&pos[1], '/');
55         }
56         snprintf(filename, len, "%s%s", udev_db_path, temp);
57         filename[len-1] = '\0';
58
59         return 0;
60 }
61
62 int udev_db_add_device(struct udevice *udev)
63 {
64         char filename[PATH_SIZE];
65         struct name_entry *name_loop;
66         FILE *f;
67
68         if (udev->test_run)
69                 return 0;
70
71         get_db_filename(udev->devpath, filename, sizeof(filename));
72
73         create_path(filename);
74
75         f = fopen(filename, "w");
76         if (f == NULL) {
77                 err("unable to create db file '%s'", filename);
78                 return -1;
79         }
80         dbg("storing data for device '%s' in '%s'", udev->devpath, filename);
81
82         fprintf(f, "P:%s\n", udev->devpath);
83         fprintf(f, "N:%s\n", udev->name);
84         list_for_each_entry(name_loop, &udev->symlink_list, node)
85                 fprintf(f, "S:%s\n", name_loop->name);
86         fprintf(f, "M:%u:%u\n", major(udev->devt), minor(udev->devt));
87         fprintf(f, "A:%u\n", udev->partitions);
88         fprintf(f, "R:%u\n", udev->ignore_remove);
89         list_for_each_entry(name_loop, &udev->env_list, node)
90                 fprintf(f, "E:%s\n", name_loop->name);
91         fclose(f);
92
93         return 0;
94 }
95
96 static int parse_db_file(struct udevice *udev, const char *filename)
97 {
98         char line[PATH_SIZE];
99         unsigned int major, minor;
100         char *bufline;
101         char *buf;
102         size_t bufsize;
103         size_t cur;
104         size_t count;
105
106         if (file_map(filename, &buf, &bufsize) != 0) {
107                 err("unable to read db file '%s'", filename);
108                 return -1;
109         }
110
111         cur = 0;
112         while (cur < bufsize) {
113                 count = buf_get_line(buf, bufsize, cur);
114                 bufline = &buf[cur];
115                 cur += count+1;
116
117                 switch(bufline[0]) {
118                 case 'P':
119                         if (count > sizeof(udev->devpath))
120                                 count = sizeof(udev->devpath);
121                         strlcpy(udev->devpath, &bufline[2], count-1);
122                         break;
123                 case 'N':
124                         if (count > sizeof(udev->name))
125                                 count = sizeof(udev->name);
126                         strlcpy(udev->name, &bufline[2], count-1);
127                         break;
128                 case 'M':
129                         if (count > sizeof(line))
130                                 count = sizeof(line);
131                         strlcpy(line, &bufline[2], count-1);
132                         sscanf(line, "%u:%u", &major, &minor);
133                         udev->devt = makedev(major, minor);
134                         break;
135                 case 'S':
136                         if (count > sizeof(line))
137                                 count =  sizeof(line);
138                         strlcpy(line, &bufline[2], count-1);
139                         name_list_add(&udev->symlink_list, line, 0);
140                         break;
141                 case 'A':
142                         if (count > sizeof(line))
143                                 count =  sizeof(line);
144                         strlcpy(line, &bufline[2], count-1);
145                         udev->partitions = atoi(line);
146                         break;
147                 case 'R':
148                         if (count > sizeof(line))
149                                 count =  sizeof(line);
150                         strlcpy(line, &bufline[2], count-1);
151                         udev->ignore_remove = atoi(line);
152                         break;
153                 case 'E':
154                         if (count > sizeof(line))
155                                 count =  sizeof(line);
156                         strlcpy(line, &bufline[2], count-1);
157                         name_list_add(&udev->env_list, line, 0);
158                         break;
159                 }
160         }
161         file_unmap(buf, bufsize);
162
163         if (udev->name[0] == '\0')
164                 return -1;
165
166         return 0;
167 }
168
169 int udev_db_delete_device(struct udevice *udev)
170 {
171         char filename[PATH_SIZE];
172
173         get_db_filename(udev->devpath, filename, sizeof(filename));
174         unlink(filename);
175
176         return 0;
177 }
178
179 int udev_db_get_device(struct udevice *udev, const char *devpath)
180 {
181         char filename[PATH_SIZE];
182
183         get_db_filename(devpath, filename, sizeof(filename));
184
185         if (parse_db_file(udev, filename) != 0)
186                 return -1;
187
188         return 0;
189 }
190
191 int udev_db_search_name(char *devpath, size_t len, const char *name)
192 {
193         DIR *dir;
194
195         dir = opendir(udev_db_path);
196         if (dir == NULL) {
197                 err("unable to open udev_db '%s'", udev_db_path);
198                 return -1;
199         }
200
201         while (1) {
202                 struct dirent *ent;
203                 char filename[PATH_SIZE];
204                 char path[PATH_SIZE];
205                 char nodename[PATH_SIZE];
206                 char *bufline;
207                 char *buf;
208                 size_t bufsize;
209                 size_t cur;
210                 size_t count;
211
212                 ent = readdir(dir);
213                 if (ent == NULL || ent->d_name[0] == '\0')
214                         break;
215
216                 if (ent->d_name[0] == '.')
217                         continue;
218
219                 snprintf(filename, sizeof(filename), "%s/%s", udev_db_path, ent->d_name);
220                 filename[sizeof(filename)-1] = '\0';
221                 dbg("looking at '%s'", filename);
222
223                 if (file_map(filename, &buf, &bufsize) != 0) {
224                         err("unable to read db file '%s'", filename);
225                         continue;
226                 }
227
228                 cur = 0;
229                 while (cur < bufsize) {
230                         count = buf_get_line(buf, bufsize, cur);
231                         bufline = &buf[cur];
232                         cur += count+1;
233
234                         switch(bufline[0]) {
235                         case 'P':
236                                 if (count > sizeof(path))
237                                         count = sizeof(path);
238                                 strlcpy(path, &bufline[2], count-1);
239                                 break;
240                         case 'N':
241                         case 'S':
242                                 if (count > sizeof(nodename))
243                                         count = sizeof(nodename);
244                                 strlcpy(nodename, &bufline[2], count-1);
245                                 dbg("compare '%s' '%s'", nodename, name);
246                                 if (strcmp(nodename, name) == 0) {
247                                         strlcpy(devpath, path, len);
248                                         file_unmap(buf, bufsize);
249                                         closedir(dir);
250                                         return 0;
251                                 }
252                                 break;
253                         default:
254                                 continue;
255                         }
256                 }
257                 file_unmap(buf, bufsize);
258         }
259
260         closedir(dir);
261         return -1;
262 }
263
264 int udev_db_dump_names(int (*handler_function)(const char *path, const char *name))
265 {
266         DIR *dir;
267
268         dir = opendir(udev_db_path);
269         if (dir == NULL) {
270                 err("unable to open udev_db '%s'", udev_db_path);
271                 return -1;
272         }
273
274         while (1) {
275                 struct dirent *ent;
276                 char filename[PATH_SIZE];
277                 char path[PATH_SIZE];
278                 char nodename[PATH_SIZE];
279                 char *bufline;
280                 char *buf;
281                 size_t bufsize;
282                 size_t cur;
283                 size_t count;
284
285                 ent = readdir(dir);
286                 if (ent == NULL || ent->d_name[0] == '\0')
287                         break;
288
289                 if (ent->d_name[0] == '.')
290                         continue;
291
292                 snprintf(filename, sizeof(filename), "%s/%s", udev_db_path, ent->d_name);
293                 filename[sizeof(filename)-1] = '\0';
294                 dbg("looking at '%s'", filename);
295
296                 if (file_map(filename, &buf, &bufsize) != 0) {
297                         err("unable to read db file '%s'", filename);
298                         continue;
299                 }
300
301                 path[0] = '\0';
302                 nodename[0] = '\0';
303                 cur = 0;
304                 while (cur < bufsize) {
305                         count = buf_get_line(buf, bufsize, cur);
306                         bufline = &buf[cur];
307                         cur += count+1;
308
309                         switch(bufline[0]) {
310                         case 'P':
311                                 if (count > sizeof(path))
312                                         count = sizeof(path);
313                                 strlcpy(path, &bufline[2], count-1);
314                                 break;
315                         case 'N':
316                                 if (count > sizeof(nodename))
317                                         count = sizeof(nodename);
318                                 strlcpy(nodename, &bufline[2], count-1);
319                                 break;
320                         default:
321                                 continue;
322                         }
323                 }
324                 file_unmap(buf, bufsize);
325
326                 if (path[0] == '\0' || nodename[0] == '\0')
327                         continue;
328
329                 if (handler_function(path, nodename) != 0)
330                         break;
331         }
332
333         closedir(dir);
334         return 0;
335 }