6 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
7 * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
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.
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.
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.
35 #include "libsysfs/sysfs/libsysfs.h"
36 #include "udev_libc_wrapper.h"
38 #include "udev_utils.h"
42 #define PATH_TO_NAME_CHAR '@'
44 static int get_db_filename(const char *devpath, char *filename, int len)
49 /* replace '/' to transform path into a filename */
50 strlcpy(temp, devpath, sizeof(temp));
51 pos = strchr(&temp[1], '/');
53 pos[0] = PATH_TO_NAME_CHAR;
54 pos = strchr(&pos[1], '/');
56 snprintf(filename, len, "%s%s", udev_db_path, temp);
57 filename[len-1] = '\0';
62 int udev_db_add_device(struct udevice *udev)
64 char filename[PATH_SIZE];
65 struct name_entry *name_loop;
71 /* don't write anything if udev created only the node with the
72 * kernel name without any interesting data to remember
74 if (strcmp(udev->name, udev->kernel_name) == 0 &&
75 list_empty(&udev->symlink_list) && list_empty(&udev->env_list) &&
76 !udev->partitions && !udev->ignore_remove) {
77 dbg("nothing interesting to store in udevdb, skip");
81 get_db_filename(udev->devpath, filename, sizeof(filename));
82 create_path(filename);
83 f = fopen(filename, "w");
85 err("unable to create db file '%s'", filename);
88 dbg("storing data for device '%s' in '%s'", udev->devpath, filename);
90 fprintf(f, "P:%s\n", udev->devpath);
91 fprintf(f, "N:%s\n", udev->name);
92 list_for_each_entry(name_loop, &udev->symlink_list, node)
93 fprintf(f, "S:%s\n", name_loop->name);
94 fprintf(f, "M:%u:%u\n", major(udev->devt), minor(udev->devt));
96 fprintf(f, "A:%u\n", udev->partitions);
97 if (udev->ignore_remove)
98 fprintf(f, "R:%u\n", udev->ignore_remove);
99 list_for_each_entry(name_loop, &udev->env_list, node)
100 fprintf(f, "E:%s\n", name_loop->name);
107 static int parse_db_file(struct udevice *udev, const char *filename)
109 char line[PATH_SIZE];
110 unsigned int major, minor;
117 if (file_map(filename, &buf, &bufsize) != 0) {
118 err("unable to read db file '%s'", filename);
123 while (cur < bufsize) {
124 count = buf_get_line(buf, bufsize, cur);
130 if (count > sizeof(udev->devpath))
131 count = sizeof(udev->devpath);
132 strlcpy(udev->devpath, &bufline[2], count-1);
135 if (count > sizeof(udev->name))
136 count = sizeof(udev->name);
137 strlcpy(udev->name, &bufline[2], count-1);
140 if (count > sizeof(line))
141 count = sizeof(line);
142 strlcpy(line, &bufline[2], count-1);
143 sscanf(line, "%u:%u", &major, &minor);
144 udev->devt = makedev(major, minor);
147 if (count > sizeof(line))
148 count = sizeof(line);
149 strlcpy(line, &bufline[2], count-1);
150 name_list_add(&udev->symlink_list, line, 0);
153 if (count > sizeof(line))
154 count = sizeof(line);
155 strlcpy(line, &bufline[2], count-1);
156 udev->partitions = atoi(line);
159 if (count > sizeof(line))
160 count = sizeof(line);
161 strlcpy(line, &bufline[2], count-1);
162 udev->ignore_remove = atoi(line);
165 if (count > sizeof(line))
166 count = sizeof(line);
167 strlcpy(line, &bufline[2], count-1);
168 name_list_add(&udev->env_list, line, 0);
172 file_unmap(buf, bufsize);
174 if (udev->name[0] == '\0')
180 int udev_db_delete_device(struct udevice *udev)
182 char filename[PATH_SIZE];
184 get_db_filename(udev->devpath, filename, sizeof(filename));
190 int udev_db_get_device(struct udevice *udev, const char *devpath)
192 char filename[PATH_SIZE];
194 get_db_filename(devpath, filename, sizeof(filename));
196 if (parse_db_file(udev, filename) != 0)
202 int udev_db_search_name(char *devpath, size_t len, const char *name)
206 dir = opendir(udev_db_path);
208 err("unable to open udev_db '%s'", udev_db_path);
214 char filename[PATH_SIZE];
215 char path[PATH_SIZE];
216 char nodename[PATH_SIZE];
224 if (ent == NULL || ent->d_name[0] == '\0')
227 if (ent->d_name[0] == '.')
230 snprintf(filename, sizeof(filename), "%s/%s", udev_db_path, ent->d_name);
231 filename[sizeof(filename)-1] = '\0';
232 dbg("looking at '%s'", filename);
234 if (file_map(filename, &buf, &bufsize) != 0) {
235 err("unable to read db file '%s'", filename);
240 while (cur < bufsize) {
241 count = buf_get_line(buf, bufsize, cur);
247 if (count > sizeof(path))
248 count = sizeof(path);
249 strlcpy(path, &bufline[2], count-1);
253 if (count > sizeof(nodename))
254 count = sizeof(nodename);
255 strlcpy(nodename, &bufline[2], count-1);
256 dbg("compare '%s' '%s'", nodename, name);
257 if (strcmp(nodename, name) == 0) {
258 strlcpy(devpath, path, len);
259 file_unmap(buf, bufsize);
268 file_unmap(buf, bufsize);
275 int udev_db_dump_names(int (*handler_function)(const char *path, const char *name))
279 dir = opendir(udev_db_path);
281 err("unable to open udev_db '%s'", udev_db_path);
287 char filename[PATH_SIZE];
288 char path[PATH_SIZE];
289 char nodename[PATH_SIZE];
297 if (ent == NULL || ent->d_name[0] == '\0')
300 if (ent->d_name[0] == '.')
303 snprintf(filename, sizeof(filename), "%s/%s", udev_db_path, ent->d_name);
304 filename[sizeof(filename)-1] = '\0';
305 dbg("looking at '%s'", filename);
307 if (file_map(filename, &buf, &bufsize) != 0) {
308 err("unable to read db file '%s'", filename);
315 while (cur < bufsize) {
316 count = buf_get_line(buf, bufsize, cur);
322 if (count > sizeof(path))
323 count = sizeof(path);
324 strlcpy(path, &bufline[2], count-1);
327 if (count > sizeof(nodename))
328 count = sizeof(nodename);
329 strlcpy(nodename, &bufline[2], count-1);
335 file_unmap(buf, bufsize);
337 if (path[0] == '\0' || nodename[0] == '\0')
340 if (handler_function(path, nodename) != 0)