2 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
3 * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation version 2 of the License.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 #include <sys/types.h>
36 static size_t 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 strlcat(filename, devpath, len);
44 return path_encode(&filename[start+1], len - (start+1));
47 static int db_file_to_devpath(const char *filename, char *devpath, size_t len)
49 strlcpy(devpath, "/", len);
50 strlcat(devpath, filename, len);
51 return path_decode(devpath);
54 int udev_db_add_device(struct udevice *udev)
56 char filename[PATH_SIZE];
61 devpath_to_db_path(udev->dev->devpath, filename, sizeof(filename));
62 create_path(filename);
65 * create only a symlink with the name as the target
66 * if we don't have any interesting data to remember
68 if (list_empty(&udev->symlink_list) && list_empty(&udev->env_list) &&
69 !udev->partitions && !udev->ignore_remove) {
70 dbg("nothing interesting to store, create symlink");
72 if (symlink(udev->name, filename) != 0) {
73 err("unable to create db link '%s': %s", filename, strerror(errno));
77 struct name_entry *name_loop;
81 f = fopen(filename, "w");
83 err("unable to create db file '%s': %s", filename, strerror(errno));
86 dbg("storing data for device '%s' in '%s'", udev->dev->devpath, filename);
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);
103 int udev_db_get_device(struct udevice *udev, const char *devpath)
106 char filename[PATH_SIZE];
107 char line[PATH_SIZE];
108 unsigned int maj, min;
115 strlcpy(udev->dev->devpath, devpath, sizeof(udev->dev->devpath));
116 devpath_to_db_path(devpath, filename, sizeof(filename));
118 if (lstat(filename, &stats) != 0) {
119 info("no db file to read %s: %s", filename, strerror(errno));
122 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
123 char target[NAME_SIZE];
126 info("found a symlink as db file");
127 target_len = readlink(filename, target, sizeof(target));
129 target[target_len] = '\0';
131 info("error reading db link %s: %s", filename, strerror(errno));
134 dbg("db link points to '%s'", target);
135 strlcpy(udev->name, target, sizeof(udev->name));
139 if (file_map(filename, &buf, &bufsize) != 0) {
140 info("error reading db file %s: %s", filename, strerror(errno));
145 while (cur < bufsize) {
146 count = buf_get_line(buf, bufsize, cur);
152 if (count > sizeof(udev->name))
153 count = sizeof(udev->name);
154 memcpy(udev->name, &bufline[2], count-2);
155 udev->name[count-2] = '\0';
158 if (count > sizeof(line))
159 count = sizeof(line);
160 memcpy(line, &bufline[2], count-2);
161 line[count-2] = '\0';
162 sscanf(line, "%u:%u", &maj, &min);
163 udev->devt = makedev(maj, min);
166 if (count > sizeof(line))
167 count = sizeof(line);
168 memcpy(line, &bufline[2], count-2);
169 line[count-2] = '\0';
170 name_list_add(&udev->symlink_list, line, 0);
173 if (count > sizeof(line))
174 count = sizeof(line);
175 memcpy(line, &bufline[2], count-2);
176 line[count-2] = '\0';
177 udev->partitions = atoi(line);
180 if (count > sizeof(line))
181 count = sizeof(line);
182 memcpy(line, &bufline[2], count-2);
183 line[count-2] = '\0';
184 udev->ignore_remove = atoi(line);
187 if (count > sizeof(line))
188 count = sizeof(line);
189 memcpy(line, &bufline[2], count-2);
190 line[count-2] = '\0';
191 name_list_add(&udev->env_list, line, 0);
195 file_unmap(buf, bufsize);
197 if (udev->name[0] == '\0')
203 int udev_db_delete_device(struct udevice *udev)
205 char filename[PATH_SIZE];
207 devpath_to_db_path(udev->dev->devpath, filename, sizeof(filename));
213 int udev_db_lookup_name(const char *name, char *devpath, size_t len)
215 char dbpath[PATH_MAX];
219 strlcpy(dbpath, udev_root, sizeof(dbpath));
220 strlcat(dbpath, "/"DB_DIR, sizeof(dbpath));
221 dir = opendir(dbpath);
223 info("no udev_db available '%s': %s", dbpath, strerror(errno));
229 char filename[PATH_SIZE];
230 char nodename[PATH_SIZE];
239 if (ent == NULL || ent->d_name[0] == '\0')
241 if (ent->d_name[0] == '.')
244 snprintf(filename, sizeof(filename), "%s/%s", dbpath, ent->d_name);
245 filename[sizeof(filename)-1] = '\0';
246 dbg("looking at '%s'", filename);
248 if (lstat(filename, &stats) != 0) {
249 info("unable to read %s: %s", filename, strerror(errno));
252 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
253 char target[NAME_SIZE];
256 info("found a symlink as db file");
257 target_len = readlink(filename, target, sizeof(target));
259 target[target_len] = '\0';
261 info("error reading db link %s: %s", filename, strerror(errno));
264 dbg("db link points to '%s'", target);
265 if (strcmp(name, target) == 0) {
266 db_file_to_devpath(ent->d_name, devpath, len);
272 if (file_map(filename, &buf, &bufsize) != 0) {
273 info("unable to read db file '%s': %s", filename, strerror(errno));
278 while (cur < bufsize && !found) {
279 count = buf_get_line(buf, bufsize, cur);
286 if (count > sizeof(nodename))
287 count = sizeof(nodename);
288 memcpy(nodename, &bufline[2], count-2);
289 nodename[count-2] = '\0';
290 dbg("compare '%s' '%s'", nodename, name);
291 if (strcmp(nodename, name) == 0) {
292 db_file_to_devpath(ent->d_name, devpath, len);
300 file_unmap(buf, bufsize);
310 int udev_db_get_all_entries(struct list_head *name_list)
312 char dbpath[PATH_MAX];
315 strlcpy(dbpath, udev_root, sizeof(dbpath));
316 strlcat(dbpath, "/"DB_DIR, sizeof(dbpath));
317 dir = opendir(dbpath);
319 info("no udev_db available '%s': %s", dbpath, strerror(errno));
325 char device[PATH_SIZE];
328 if (ent == NULL || ent->d_name[0] == '\0')
330 if (ent->d_name[0] == '.')
333 db_file_to_devpath(ent->d_name, device, sizeof(device));
334 name_list_add(name_list, device, 1);
335 dbg("added '%s'", device);