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 err("unable to read db file '%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 strlcpy(udev->devpath, &bufline[2], count-1);
133 if (count > sizeof(udev->name))
134 count = sizeof(udev->name);
135 strlcpy(udev->name, &bufline[2], count-1);
138 if (count > sizeof(line))
139 count = sizeof(line);
140 strlcpy(line, &bufline[2], count-1);
141 sscanf(line, "%u:%u", &major, &minor);
142 udev->devt = makedev(major, minor);
145 if (count > sizeof(line))
146 count = sizeof(line);
147 strlcpy(line, &bufline[2], count-1);
148 name_list_add(&udev->symlink_list, line, 0);
151 if (count > sizeof(line))
152 count = sizeof(line);
153 strlcpy(line, &bufline[2], count-1);
154 udev->partitions = atoi(line);
157 if (count > sizeof(line))
158 count = sizeof(line);
159 strlcpy(line, &bufline[2], count-1);
160 udev->ignore_remove = atoi(line);
163 if (count > sizeof(line))
164 count = sizeof(line);
165 strlcpy(line, &bufline[2], count-1);
166 name_list_add(&udev->env_list, line, 0);
170 file_unmap(buf, bufsize);
172 if (udev->name[0] == '\0')
178 int udev_db_delete_device(struct udevice *udev)
180 char filename[PATH_SIZE];
182 get_db_filename(udev->devpath, filename, sizeof(filename));
188 int udev_db_get_device(struct udevice *udev, const char *devpath)
190 char filename[PATH_SIZE];
192 get_db_filename(devpath, filename, sizeof(filename));
194 if (parse_db_file(udev, filename) != 0)
200 int udev_db_search_name(char *devpath, size_t len, const char *name)
204 dir = opendir(udev_db_path);
206 err("unable to open udev_db '%s'", udev_db_path);
212 char filename[PATH_SIZE];
213 char path[PATH_SIZE];
214 char nodename[PATH_SIZE];
222 if (ent == NULL || ent->d_name[0] == '\0')
225 if (ent->d_name[0] == '.')
228 snprintf(filename, sizeof(filename), "%s/%s", udev_db_path, ent->d_name);
229 filename[sizeof(filename)-1] = '\0';
230 dbg("looking at '%s'", filename);
232 if (file_map(filename, &buf, &bufsize) != 0) {
233 err("unable to read db file '%s'", filename);
238 while (cur < bufsize) {
239 count = buf_get_line(buf, bufsize, cur);
245 if (count > sizeof(path))
246 count = sizeof(path);
247 strlcpy(path, &bufline[2], count-1);
251 if (count > sizeof(nodename))
252 count = sizeof(nodename);
253 strlcpy(nodename, &bufline[2], count-1);
254 dbg("compare '%s' '%s'", nodename, name);
255 if (strcmp(nodename, name) == 0) {
256 strlcpy(devpath, path, len);
257 file_unmap(buf, bufsize);
266 file_unmap(buf, bufsize);
273 int udev_db_dump_names(int (*handler_function)(const char *path, const char *name))
277 dir = opendir(udev_db_path);
279 err("unable to open udev_db '%s'", udev_db_path);
285 char filename[PATH_SIZE];
286 char path[PATH_SIZE];
287 char nodename[PATH_SIZE];
295 if (ent == NULL || ent->d_name[0] == '\0')
298 if (ent->d_name[0] == '.')
301 snprintf(filename, sizeof(filename), "%s/%s", udev_db_path, ent->d_name);
302 filename[sizeof(filename)-1] = '\0';
303 dbg("looking at '%s'", filename);
305 if (file_map(filename, &buf, &bufsize) != 0) {
306 err("unable to read db file '%s'", filename);
313 while (cur < bufsize) {
314 count = buf_get_line(buf, bufsize, cur);
320 if (count > sizeof(path))
321 count = sizeof(path);
322 strlcpy(path, &bufline[2], count-1);
325 if (count > sizeof(nodename))
326 count = sizeof(nodename);
327 strlcpy(nodename, &bufline[2], count-1);
333 file_unmap(buf, bufsize);
335 if (path[0] == '\0' || nodename[0] == '\0')
338 if (handler_function(path, nodename) != 0)