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 <sys/types.h>
38 static int devpath_to_db_path(const char *devpath, char *filename, size_t len)
42 /* add location of db files */
43 strlcpy(filename, udev_root, len);
44 start = strlcat(filename, "/"DB_DIR, len);
45 end = strlcat(filename, devpath, len);
49 /* replace '/' to transform path into a filename */
50 for (i = start+1; i < end; i++)
51 if (filename[i] == '/')
52 filename[i] = PATH_TO_NAME_CHAR;
57 static int db_file_to_devpath(const char *filename, char *devpath, size_t len)
61 strlcpy(devpath, "/", len);
62 end = strlcat(devpath, filename, len);
64 /* replace PATH_TO_NAME_CHAR to transform name into devpath */
65 for (i = 1; i < end; i++)
66 if (devpath[i] == PATH_TO_NAME_CHAR)
72 int udev_db_add_device(struct udevice *udev)
74 char filename[PATH_SIZE];
79 devpath_to_db_path(udev->dev->devpath, filename, sizeof(filename));
80 create_path(filename);
83 * create only a symlink with the name as the target
84 * if we don't have any interesting data to remember
86 if (list_empty(&udev->symlink_list) && list_empty(&udev->env_list) &&
87 !udev->partitions && !udev->ignore_remove) {
88 dbg("nothing interesting to store, create symlink");
90 if (symlink(udev->name, filename) != 0) {
91 err("unable to create db link '%s': %s", filename, strerror(errno));
95 struct name_entry *name_loop;
98 f = fopen(filename, "w");
100 err("unable to create db file '%s': %s", filename, strerror(errno));
103 dbg("storing data for device '%s' in '%s'", udev->dev->devpath, filename);
105 fprintf(f, "N:%s\n", udev->name);
106 list_for_each_entry(name_loop, &udev->symlink_list, node)
107 fprintf(f, "S:%s\n", name_loop->name);
108 fprintf(f, "M:%u:%u\n", major(udev->devt), minor(udev->devt));
109 if (udev->partitions)
110 fprintf(f, "A:%u\n", udev->partitions);
111 if (udev->ignore_remove)
112 fprintf(f, "R:%u\n", udev->ignore_remove);
113 list_for_each_entry(name_loop, &udev->env_list, node)
114 fprintf(f, "E:%s\n", name_loop->name);
120 int udev_db_get_device(struct udevice *udev, const char *devpath)
123 char filename[PATH_SIZE];
124 char line[PATH_SIZE];
125 unsigned int maj, min;
132 strlcpy(udev->dev->devpath, devpath, sizeof(udev->dev->devpath));
133 devpath_to_db_path(devpath, filename, sizeof(filename));
135 if (lstat(filename, &stats) != 0) {
136 info("no db file to read %s: %s", filename, strerror(errno));
139 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
140 char target[NAME_SIZE];
143 info("found a symlink as db file");
144 target_len = readlink(filename, target, sizeof(target));
146 target[target_len] = '\0';
148 info("error reading db link %s: %s", filename, strerror(errno));
151 dbg("db link points to '%s'", target);
152 strlcpy(udev->name, target, sizeof(udev->name));
156 if (file_map(filename, &buf, &bufsize) != 0) {
157 info("error reading db file %s: %s", filename, strerror(errno));
162 while (cur < bufsize) {
163 count = buf_get_line(buf, bufsize, cur);
169 if (count > sizeof(udev->name))
170 count = sizeof(udev->name);
171 memcpy(udev->name, &bufline[2], count-2);
172 udev->name[count-2] = '\0';
175 if (count > sizeof(line))
176 count = sizeof(line);
177 memcpy(line, &bufline[2], count-2);
178 line[count-2] = '\0';
179 sscanf(line, "%u:%u", &maj, &min);
180 udev->devt = makedev(maj, min);
183 if (count > sizeof(line))
184 count = sizeof(line);
185 memcpy(line, &bufline[2], count-2);
186 line[count-2] = '\0';
187 name_list_add(&udev->symlink_list, line, 0);
190 if (count > sizeof(line))
191 count = sizeof(line);
192 memcpy(line, &bufline[2], count-2);
193 line[count-2] = '\0';
194 udev->partitions = atoi(line);
197 if (count > sizeof(line))
198 count = sizeof(line);
199 memcpy(line, &bufline[2], count-2);
200 line[count-2] = '\0';
201 udev->ignore_remove = atoi(line);
204 if (count > sizeof(line))
205 count = sizeof(line);
206 memcpy(line, &bufline[2], count-2);
207 line[count-2] = '\0';
208 name_list_add(&udev->env_list, line, 0);
212 file_unmap(buf, bufsize);
214 if (udev->name[0] == '\0')
220 int udev_db_delete_device(struct udevice *udev)
222 char filename[PATH_SIZE];
224 devpath_to_db_path(udev->dev->devpath, filename, sizeof(filename));
230 int udev_db_lookup_name(const char *name, char *devpath, size_t len)
232 char dbpath[PATH_MAX];
236 strlcpy(dbpath, udev_root, sizeof(dbpath));
237 strlcat(dbpath, "/"DB_DIR, sizeof(dbpath));
238 dir = opendir(dbpath);
240 info("no udev_db available '%s': %s", dbpath, strerror(errno));
246 char filename[PATH_SIZE];
247 char nodename[PATH_SIZE];
256 if (ent == NULL || ent->d_name[0] == '\0')
258 if (ent->d_name[0] == '.')
261 snprintf(filename, sizeof(filename), "%s/%s", dbpath, ent->d_name);
262 filename[sizeof(filename)-1] = '\0';
263 dbg("looking at '%s'", filename);
265 if (lstat(filename, &stats) != 0) {
266 info("unable to read %s: %s", filename, strerror(errno));
269 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
270 char target[NAME_SIZE];
273 info("found a symlink as db file");
274 target_len = readlink(filename, target, sizeof(target));
276 target[target_len] = '\0';
278 info("error reading db link %s: %s", filename, strerror(errno));
281 dbg("db link points to '%s'", target);
282 if (strcmp(name, target) == 0) {
283 db_file_to_devpath(ent->d_name, devpath, len);
289 if (file_map(filename, &buf, &bufsize) != 0) {
290 info("unable to read db file '%s': %s", filename, strerror(errno));
295 while (cur < bufsize && !found) {
296 count = buf_get_line(buf, bufsize, cur);
303 if (count > sizeof(nodename))
304 count = sizeof(nodename);
305 memcpy(nodename, &bufline[2], count-2);
306 nodename[count-2] = '\0';
307 dbg("compare '%s' '%s'", nodename, name);
308 if (strcmp(nodename, name) == 0) {
309 db_file_to_devpath(ent->d_name, devpath, len);
317 file_unmap(buf, bufsize);
327 int udev_db_get_all_entries(struct list_head *name_list)
329 char dbpath[PATH_MAX];
332 strlcpy(dbpath, udev_root, sizeof(dbpath));
333 strlcat(dbpath, "/"DB_DIR, sizeof(dbpath));
334 dir = opendir(dbpath);
336 info("no udev_db available '%s': %s", dbpath, strerror(errno));
342 char filename[PATH_SIZE] = "/";
346 if (ent == NULL || ent->d_name[0] == '\0')
348 if (ent->d_name[0] == '.')
351 end = strlcat(filename, ent->d_name, sizeof(filename));
352 for (i = 1; i < end; i++)
353 if (filename[i] == PATH_TO_NAME_CHAR)
355 name_list_add(name_list, filename, 1);
356 dbg("added '%s'", filename);