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.
36 static int devpath_to_db_path(const char *devpath, char *filename, size_t len)
40 /* add location of db files */
41 strlcpy(filename, udev_root, len);
42 start = strlcat(filename, "/"DB_DIR, len);
43 end = strlcat(filename, devpath, len);
47 /* replace '/' to transform path into a filename */
48 for (i = start+1; i < end; i++)
49 if (filename[i] == '/')
50 filename[i] = PATH_TO_NAME_CHAR;
55 static int db_file_to_devpath(const char *filename, char *devpath, size_t len)
59 strlcpy(devpath, "/", len);
60 end = strlcat(devpath, filename, len);
62 /* replace PATH_TO_NAME_CHAR to transform name into devpath */
63 for (i = 1; i < end; i++)
64 if (devpath[i] == PATH_TO_NAME_CHAR)
70 int udev_db_add_device(struct udevice *udev)
72 char filename[PATH_SIZE];
73 struct name_entry *name_loop;
79 /* don't write anything if udev created only the node with the
80 * kernel name without any interesting data to remember
82 if (strcmp(udev->name, udev->dev->kernel_name) == 0 &&
83 list_empty(&udev->symlink_list) && list_empty(&udev->env_list) &&
84 !udev->partitions && !udev->ignore_remove) {
85 dbg("nothing interesting to store in udevdb, skip");
89 devpath_to_db_path(udev->dev->devpath, filename, sizeof(filename));
90 create_path(filename);
91 f = fopen(filename, "w");
93 err("unable to create db file '%s': %s", filename, strerror(errno));
96 dbg("storing data for device '%s' in '%s'", udev->dev->devpath, filename);
98 fprintf(f, "N:%s\n", udev->name);
99 list_for_each_entry(name_loop, &udev->symlink_list, node)
100 fprintf(f, "S:%s\n", name_loop->name);
101 fprintf(f, "M:%u:%u\n", major(udev->devt), minor(udev->devt));
102 if (udev->partitions)
103 fprintf(f, "A:%u\n", udev->partitions);
104 if (udev->ignore_remove)
105 fprintf(f, "R:%u\n", udev->ignore_remove);
106 list_for_each_entry(name_loop, &udev->env_list, node)
107 fprintf(f, "E:%s\n", name_loop->name);
114 int udev_db_get_device(struct udevice *udev, const char *devpath)
116 char filename[PATH_SIZE];
117 char line[PATH_SIZE];
118 unsigned int major, minor;
125 devpath_to_db_path(devpath, filename, sizeof(filename));
126 if (file_map(filename, &buf, &bufsize) != 0) {
127 info("no db file to read %s: %s", filename, strerror(errno));
131 strlcpy(udev->dev->devpath, devpath, sizeof(udev->dev->devpath));
133 while (cur < bufsize) {
134 count = buf_get_line(buf, bufsize, cur);
140 if (count > sizeof(udev->name))
141 count = sizeof(udev->name);
142 memcpy(udev->name, &bufline[2], count-2);
143 udev->name[count-2] = '\0';
146 if (count > sizeof(line))
147 count = sizeof(line);
148 memcpy(line, &bufline[2], count-2);
149 line[count-2] = '\0';
150 sscanf(line, "%u:%u", &major, &minor);
151 udev->devt = makedev(major, minor);
154 if (count > sizeof(line))
155 count = sizeof(line);
156 memcpy(line, &bufline[2], count-2);
157 line[count-2] = '\0';
158 name_list_add(&udev->symlink_list, line, 0);
161 if (count > sizeof(line))
162 count = sizeof(line);
163 memcpy(line, &bufline[2], count-2);
164 line[count-2] = '\0';
165 udev->partitions = atoi(line);
168 if (count > sizeof(line))
169 count = sizeof(line);
170 memcpy(line, &bufline[2], count-2);
171 line[count-2] = '\0';
172 udev->ignore_remove = atoi(line);
175 if (count > sizeof(line))
176 count = sizeof(line);
177 memcpy(line, &bufline[2], count-2);
178 line[count-2] = '\0';
179 name_list_add(&udev->env_list, line, 0);
183 file_unmap(buf, bufsize);
185 if (udev->name[0] == '\0')
191 int udev_db_delete_device(struct udevice *udev)
193 char filename[PATH_SIZE];
195 devpath_to_db_path(udev->dev->devpath, filename, sizeof(filename));
201 int udev_db_lookup_name(const char *name, char *devpath, size_t len)
203 char dbpath[PATH_MAX];
207 strlcpy(dbpath, udev_root, sizeof(dbpath));
208 strlcat(dbpath, "/"DB_DIR, sizeof(dbpath));
209 dir = opendir(dbpath);
211 info("no udev_db available '%s': %s", dbpath, strerror(errno));
217 char filename[PATH_SIZE];
218 char nodename[PATH_SIZE];
226 if (ent == NULL || ent->d_name[0] == '\0')
228 if (ent->d_name[0] == '.')
231 snprintf(filename, sizeof(filename), "%s/%s", dbpath, ent->d_name);
232 filename[sizeof(filename)-1] = '\0';
233 dbg("looking at '%s'", filename);
235 if (file_map(filename, &buf, &bufsize) != 0) {
236 err("unable to read db file '%s': %s", filename, strerror(errno));
241 while (cur < bufsize && !found) {
242 count = buf_get_line(buf, bufsize, cur);
249 if (count > sizeof(nodename))
250 count = sizeof(nodename);
251 memcpy(nodename, &bufline[2], count-2);
252 nodename[count-2] = '\0';
253 dbg("compare '%s' '%s'", nodename, name);
254 if (strcmp(nodename, name) == 0) {
255 db_file_to_devpath(ent->d_name, devpath, len);
263 file_unmap(buf, bufsize);
273 int udev_db_get_all_entries(struct list_head *name_list)
275 char dbpath[PATH_MAX];
278 strlcpy(dbpath, udev_root, sizeof(dbpath));
279 strlcat(dbpath, "/"DB_DIR, sizeof(dbpath));
280 dir = opendir(dbpath);
282 info("no udev_db available '%s': %s", dbpath, strerror(errno));
288 char filename[PATH_SIZE] = "/";
292 if (ent == NULL || ent->d_name[0] == '\0')
294 if (ent->d_name[0] == '.')
297 end = strlcat(filename, ent->d_name, sizeof(filename));
298 for (i = 1; i < end; i++)
299 if (filename[i] == PATH_TO_NAME_CHAR)
301 name_list_add(name_list, filename, 1);
302 dbg("added '%s'", filename);