chiark / gitweb /
[PATCH] klibc: version 0.211
[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 <fcntl.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <dirent.h>
33
34 #include "libsysfs/sysfs/libsysfs.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[SYSFS_PATH_MAX];
45         char *pos;
46
47         /* replace '/' to transform path into a filename */
48         strfieldcpy(temp, devpath);
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[SYSFS_PATH_MAX];
63         struct name_entry *name_loop;
64         FILE *f;
65
66         if (udev->test_run)
67                 return 0;
68
69         get_db_filename(udev->devpath, filename, SYSFS_PATH_MAX);
70
71         create_path(filename);
72
73         f = fopen(filename, "w");
74         if (f == NULL) {
75                 dbg("unable to create db file '%s'", filename);
76                 return -1;
77         }
78         dbg("storing data for device '%s' in '%s'", udev->devpath, filename);
79
80         fprintf(f, "P:%s\n", udev->devpath);
81         fprintf(f, "N:%s\n", udev->name);
82         list_for_each_entry(name_loop, &udev->symlink_list, node)
83                 fprintf(f, "S:%s\n", name_loop->name);
84         fprintf(f, "M:%u:%u\n", major(udev->devt), minor(udev->devt));
85         fprintf(f, "A:%u\n", udev->partitions);
86         fprintf(f, "R:%u\n", udev->ignore_remove);
87
88         fclose(f);
89
90         return 0;
91 }
92
93 static int parse_db_file(struct udevice *udev, const char *filename)
94 {
95         char line[NAME_SIZE];
96         char temp[NAME_SIZE];
97         unsigned int major, minor;
98         char *bufline;
99         char *buf;
100         size_t bufsize;
101         size_t cur;
102         size_t count;
103
104         if (file_map(filename, &buf, &bufsize) != 0) {
105                 dbg("unable to read db file '%s'", filename);
106                 return -1;
107         }
108
109         cur = 0;
110         while (cur < bufsize) {
111                 count = buf_get_line(buf, bufsize, cur);
112                 bufline = &buf[cur];
113                 cur += count+1;
114
115                 switch(bufline[0]) {
116                 case 'P':
117                         if (count > DEVPATH_SIZE)
118                                 count = DEVPATH_SIZE-1;
119                         strncpy(udev->devpath, &bufline[2], count-2);
120                         udev->devpath[count-2] = '\0';
121                         break;
122                 case 'N':
123                         if (count > NAME_SIZE)
124                                 count = NAME_SIZE-1;
125                         strncpy(udev->name, &bufline[2], count-2);
126                         udev->name[count-2] = '\0';
127                         break;
128                 case 'M':
129                         if (count > NAME_SIZE)
130                                 count = NAME_SIZE-1;
131                         strncpy(temp, &bufline[2], count-2);
132                         temp[count-2] = '\0';
133                         sscanf(temp, "%u:%u", &major, &minor);
134                         udev->devt = makedev(major, minor);
135                         break;
136                 case 'S':
137                         if (count > NAME_SIZE)
138                                 count = NAME_SIZE-1;
139                         strncpy(temp, &bufline[2], count-2);
140                         temp[count-2] = '\0';
141                         name_list_add(&udev->symlink_list, temp, 0);
142                         break;
143                 case 'A':
144                         if (count > NAME_SIZE)
145                                 count = NAME_SIZE-1;
146                         strncpy(line, &bufline[2], count-2);
147                         line[count-2] = '\0';
148                         udev->partitions = atoi(line);
149                         break;
150                 case 'R':
151                         if (count > NAME_SIZE)
152                                 count = NAME_SIZE-1;
153                         strncpy(line, &bufline[2], count-2);
154                         line[count-2] = '\0';
155                         udev->ignore_remove = atoi(line);
156                         break;
157                 }
158         }
159         file_unmap(buf, bufsize);
160
161         if (udev->name[0] == '\0')
162                 return -1;
163
164         return 0;
165 }
166
167 int udev_db_delete_device(struct udevice *udev)
168 {
169         char filename[SYSFS_PATH_MAX];
170
171         get_db_filename(udev->devpath, filename, SYSFS_PATH_MAX);
172         unlink(filename);
173
174         return 0;
175 }
176
177 int udev_db_get_device(struct udevice *udev, const char *devpath)
178 {
179         char filename[SYSFS_PATH_MAX];
180
181         get_db_filename(devpath, filename, SYSFS_PATH_MAX);
182
183         if (parse_db_file(udev, filename) != 0)
184                 return -1;
185
186         return 0;
187 }
188
189 int udev_db_search_name(char *devpath, size_t len, const char *name)
190 {
191         struct dirent *ent;
192         DIR *dir;
193         char filename[NAME_SIZE];
194
195         dir = opendir(udev_db_path);
196         if (dir == NULL) {
197                 dbg("unable to udev db '%s'", udev_db_path);
198                 return -1;
199         }
200
201         while (1) {
202                 char path[DEVPATH_SIZE];
203                 char nodename[NAME_SIZE];
204                 char *bufline;
205                 char *buf;
206                 size_t bufsize;
207                 size_t cur;
208                 size_t count;
209
210                 ent = readdir(dir);
211                 if (ent == NULL || ent->d_name[0] == '\0')
212                         break;
213
214                 if (ent->d_name[0] == '.')
215                         continue;
216
217                 snprintf(filename, NAME_SIZE, "%s/%s", udev_db_path, ent->d_name);
218                 filename[NAME_SIZE-1] = '\0';
219                 dbg("looking at '%s'", filename);
220
221                 if (file_map(filename, &buf, &bufsize) != 0) {
222                         dbg("unable to read db file '%s'", filename);
223                         continue;
224                 }
225
226                 cur = 0;
227                 while (cur < bufsize) {
228                         count = buf_get_line(buf, bufsize, cur);
229                         bufline = &buf[cur];
230                         cur += count+1;
231
232                         switch(bufline[0]) {
233                         case 'P':
234                                 if (count > DEVPATH_SIZE)
235                                         count = DEVPATH_SIZE-1;
236                                 strncpy(path, &bufline[2], count-2);
237                                 path[count-2] = '\0';
238                                 break;
239                         case 'N':
240                         case 'S':
241                                 if (count > NAME_SIZE)
242                                 count = NAME_SIZE-1;
243                                 strncpy(nodename, &bufline[2], count-2);
244                                 nodename[count-2] = '\0';
245                                 dbg("compare '%s' '%s'", nodename, name);
246                                 if (strcmp(nodename, name) == 0) {
247                                         strncpy(devpath, path, len);
248                                         devpath[len] = '\0';
249                                         file_unmap(buf, bufsize);
250                                         closedir(dir);
251                                         return 0;
252                                 }
253                                 break;
254                         default:
255                                 continue;
256                         }
257                 }
258                 file_unmap(buf, bufsize);
259         }
260
261         closedir(dir);
262         return -1;
263 }
264
265 int udev_db_call_foreach(int (*handler_function)(struct udevice *udev))
266 {
267         struct dirent *ent;
268         DIR *dir;
269         char filename[NAME_SIZE];
270         struct udevice db_udev;
271
272         dir = opendir(udev_db_path);
273         if (dir == NULL) {
274                 dbg("unable to udev db '%s'", udev_db_path);
275                 return -1;
276         }
277
278         while (1) {
279                 ent = readdir(dir);
280                 if (ent == NULL || ent->d_name[0] == '\0')
281                         break;
282
283                 if (ent->d_name[0] == '.')
284                         continue;
285
286                 snprintf(filename, NAME_SIZE, "%s/%s", udev_db_path, ent->d_name);
287                 filename[NAME_SIZE-1] = '\0';
288
289                 dbg("found '%s'", filename);
290
291                 udev_init_device(&db_udev, NULL, NULL);
292                 if (parse_db_file(&db_udev, filename) == 0) {
293                         if (handler_function(&db_udev) != 0)
294                                 break;
295                 }
296         }
297
298         closedir(dir);
299         return 0;
300 }