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, size_t len)
46 start = strlcpy(filename, udev_db_path, len);
47 end = strlcat(filename, devpath, len);
51 /* replace '/' to transform path into a filename */
52 for (i = start+1; i < end; i++)
53 if (filename[i] == '/')
54 filename[i] = PATH_TO_NAME_CHAR;
59 int udev_db_add_device(struct udevice *udev)
61 char filename[PATH_SIZE];
62 struct name_entry *name_loop;
68 /* don't write anything if udev created only the node with the
69 * kernel name without any interesting data to remember
71 if (strcmp(udev->name, udev->kernel_name) == 0 &&
72 list_empty(&udev->symlink_list) && list_empty(&udev->env_list) &&
73 !udev->partitions && !udev->ignore_remove) {
74 dbg("nothing interesting to store in udevdb, skip");
78 get_db_filename(udev->devpath, filename, sizeof(filename));
79 create_path(filename);
80 f = fopen(filename, "w");
82 err("unable to create db file '%s'", filename);
85 dbg("storing data for device '%s' in '%s'", udev->devpath, filename);
87 fprintf(f, "P:%s\n", udev->devpath);
88 fprintf(f, "N:%s\n", udev->name);
89 list_for_each_entry(name_loop, &udev->symlink_list, node)
90 fprintf(f, "S:%s\n", name_loop->name);
91 fprintf(f, "M:%u:%u\n", major(udev->devt), minor(udev->devt));
93 fprintf(f, "A:%u\n", udev->partitions);
94 if (udev->ignore_remove)
95 fprintf(f, "R:%u\n", udev->ignore_remove);
96 list_for_each_entry(name_loop, &udev->env_list, node)
97 fprintf(f, "E:%s\n", name_loop->name);
104 static int parse_db_file(struct udevice *udev, const char *filename)
106 char line[PATH_SIZE];
107 unsigned int major, minor;
114 if (file_map(filename, &buf, &bufsize) != 0) {
115 dbg("no db file to read '%s'", filename);
120 while (cur < bufsize) {
121 count = buf_get_line(buf, bufsize, cur);
127 if (count > sizeof(udev->devpath))
128 count = sizeof(udev->devpath);
129 memcpy(udev->devpath, &bufline[2], count-2);
130 udev->devpath[count-2] = '\0';
133 if (count > sizeof(udev->name))
134 count = sizeof(udev->name);
135 memcpy(udev->name, &bufline[2], count-2);
136 udev->name[count-2] = '\0';
139 if (count > sizeof(line))
140 count = sizeof(line);
141 memcpy(line, &bufline[2], count-2);
142 line[count-2] = '\0';
143 sscanf(line, "%u:%u", &major, &minor);
144 udev->devt = makedev(major, minor);
147 if (count > sizeof(line))
148 count = sizeof(line);
149 memcpy(line, &bufline[2], count-2);
150 line[count-2] = '\0';
151 name_list_add(&udev->symlink_list, line, 0);
154 if (count > sizeof(line))
155 count = sizeof(line);
156 memcpy(line, &bufline[2], count-2);
157 line[count-2] = '\0';
158 udev->partitions = atoi(line);
161 if (count > sizeof(line))
162 count = sizeof(line);
163 memcpy(line, &bufline[2], count-2);
164 line[count-2] = '\0';
165 udev->ignore_remove = atoi(line);
168 if (count > sizeof(line))
169 count = sizeof(line);
170 memcpy(line, &bufline[2], count-2);
171 line[count-2] = '\0';
172 name_list_add(&udev->env_list, line, 0);
176 file_unmap(buf, bufsize);
178 if (udev->name[0] == '\0')
184 int udev_db_delete_device(struct udevice *udev)
186 char filename[PATH_SIZE];
188 get_db_filename(udev->devpath, filename, sizeof(filename));
194 int udev_db_get_device(struct udevice *udev, const char *devpath)
196 char filename[PATH_SIZE];
198 get_db_filename(devpath, filename, sizeof(filename));
199 if (parse_db_file(udev, filename) != 0)
205 int udev_db_search_name(const char *name, char *devpath, size_t len)
208 char path[PATH_SIZE];
211 dir = opendir(udev_db_path);
213 err("unable to open udev_db '%s'", udev_db_path);
219 char filename[PATH_SIZE];
220 char nodename[PATH_SIZE];
228 if (ent == NULL || ent->d_name[0] == '\0')
231 if (ent->d_name[0] == '.')
234 snprintf(filename, sizeof(filename), "%s/%s", udev_db_path, ent->d_name);
235 filename[sizeof(filename)-1] = '\0';
236 dbg("looking at '%s'", filename);
238 if (file_map(filename, &buf, &bufsize) != 0) {
239 err("unable to read db file '%s'", filename);
244 while (cur < bufsize && !found) {
245 count = buf_get_line(buf, bufsize, cur);
251 if (count > sizeof(path))
252 count = sizeof(path);
253 memcpy(path, &bufline[2], count-2);
254 path[count-2] = '\0';
258 if (count > sizeof(nodename))
259 count = sizeof(nodename);
260 memcpy(nodename, &bufline[2], count-2);
261 nodename[count-2] = '\0';
262 dbg("compare '%s' '%s'", nodename, name);
263 if (strcmp(nodename, name) == 0) {
272 file_unmap(buf, bufsize);
277 strlcpy(devpath, path, len);
283 int udev_db_get_all_entries(struct list_head *name_list)
287 dir = opendir(udev_db_path);
289 err("unable to open udev_db '%s'", udev_db_path);
295 char filename[PATH_SIZE] = "/";
299 if (ent == NULL || ent->d_name[0] == '\0')
302 if (ent->d_name[0] == '.')
305 end = strlcat(filename, ent->d_name, sizeof(filename));
306 for (i = 1; i < end; i++)
307 if (filename[i] == PATH_TO_NAME_CHAR)
309 name_list_add(name_list, filename, 1);
310 dbg("added '%s'", filename);