4 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
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.
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.
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.
33 #include "libsysfs/sysfs/libsysfs.h"
34 #include "udev_libc_wrapper.h"
36 #include "udev_utils.h"
40 #define PATH_TO_NAME_CHAR '@'
42 static int get_db_filename(const char *devpath, char *filename, int len)
47 /* replace '/' to transform path into a filename */
48 strlcpy(temp, devpath, sizeof(temp));
49 pos = strchr(&temp[1], '/');
51 pos[0] = PATH_TO_NAME_CHAR;
52 pos = strchr(&pos[1], '/');
54 snprintf(filename, len, "%s%s", udev_db_path, temp);
55 filename[len-1] = '\0';
60 int udev_db_add_device(struct udevice *udev)
62 char filename[PATH_SIZE];
63 struct name_entry *name_loop;
69 /* don't write anything if udev created only the node with the
70 * kernel name without any interesting data to remember
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");
79 get_db_filename(udev->devpath, filename, sizeof(filename));
80 create_path(filename);
81 f = fopen(filename, "w");
83 err("unable to create db file '%s'", filename);
86 dbg("storing data for device '%s' in '%s'", udev->devpath, filename);
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));
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);
105 static int parse_db_file(struct udevice *udev, const char *filename)
107 char line[PATH_SIZE];
108 unsigned int major, minor;
115 if (file_map(filename, &buf, &bufsize) != 0) {
116 dbg("no db file to read '%s'", filename);
121 while (cur < bufsize) {
122 count = buf_get_line(buf, bufsize, cur);
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';
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';
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);
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);
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);
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);
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);
177 file_unmap(buf, bufsize);
179 if (udev->name[0] == '\0')
185 int udev_db_delete_device(struct udevice *udev)
187 char filename[PATH_SIZE];
189 get_db_filename(udev->devpath, filename, sizeof(filename));
195 int udev_db_get_device(struct udevice *udev, const char *devpath)
197 char filename[PATH_SIZE];
199 get_db_filename(devpath, filename, sizeof(filename));
201 if (parse_db_file(udev, filename) != 0)
207 int udev_db_search_name(char *devpath, size_t len, const char *name)
211 dir = opendir(udev_db_path);
213 err("unable to open udev_db '%s'", udev_db_path);
219 char filename[PATH_SIZE];
220 char path[PATH_SIZE];
221 char nodename[PATH_SIZE];
229 if (ent == NULL || ent->d_name[0] == '\0')
232 if (ent->d_name[0] == '.')
235 snprintf(filename, sizeof(filename), "%s/%s", udev_db_path, ent->d_name);
236 filename[sizeof(filename)-1] = '\0';
237 dbg("looking at '%s'", filename);
239 if (file_map(filename, &buf, &bufsize) != 0) {
240 err("unable to read db file '%s'", filename);
245 while (cur < bufsize) {
246 count = buf_get_line(buf, bufsize, cur);
252 if (count > sizeof(path))
253 count = sizeof(path);
254 memcpy(path, &bufline[2], count-2);
255 path[count-2] = '\0';
259 if (count > sizeof(nodename))
260 count = sizeof(nodename);
261 memcpy(nodename, &bufline[2], count-2);
262 nodename[count-2] = '\0';
263 dbg("compare '%s' '%s'", nodename, name);
264 if (strcmp(nodename, name) == 0) {
265 strlcpy(devpath, nodename, len);
266 devpath[count-2] = '\0';
267 file_unmap(buf, bufsize);
276 file_unmap(buf, bufsize);
283 int udev_db_dump_names(int (*handler_function)(const char *path, const char *name))
287 dir = opendir(udev_db_path);
289 err("unable to open udev_db '%s'", udev_db_path);
295 char filename[PATH_SIZE];
296 char path[PATH_SIZE];
297 char nodename[PATH_SIZE];
305 if (ent == NULL || ent->d_name[0] == '\0')
308 if (ent->d_name[0] == '.')
311 snprintf(filename, sizeof(filename), "%s/%s", udev_db_path, ent->d_name);
312 filename[sizeof(filename)-1] = '\0';
313 dbg("looking at '%s'", filename);
315 if (file_map(filename, &buf, &bufsize) != 0) {
316 err("unable to read db file '%s'", filename);
323 while (cur < bufsize) {
324 count = buf_get_line(buf, bufsize, cur);
330 if (count > sizeof(path))
331 count = sizeof(path);
332 memcpy(path, &bufline[2], count-2);
333 path[count-2] = '\0';
336 if (count > sizeof(nodename))
337 count = sizeof(nodename);
338 memcpy(nodename, &bufline[2], count-2);
339 nodename[count-2] = '\0';
345 file_unmap(buf, bufsize);
347 if (path[0] == '\0' || nodename[0] == '\0')
350 if (handler_function(path, nodename) != 0)