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 size_t 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);
55 /* reverse mapping from the device file name to the devpath */
56 static int name_index(const char *devpath, const char *name, int add)
58 char device[PATH_SIZE];
59 char filename[PATH_SIZE * 2];
63 /* directory with device name */
64 strlcpy(filename, udev_root, sizeof(filename));
65 start = strlcat(filename, "/"DB_NAME_INDEX_DIR"/", sizeof(filename));
66 strlcat(filename, name, sizeof(filename));
67 path_encode(&filename[start+1], sizeof(filename) - (start+1));
68 /* entry with the devpath */
69 strlcpy(device, devpath, sizeof(device));
70 path_encode(&device[1], sizeof(device)-1);
71 strlcat(filename, device, sizeof(filename));
74 dbg("creating: '%s'", filename);
75 create_path(filename);
76 fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644);
80 dbg("removing: '%s'", filename);
82 delete_path(filename);
87 int udev_db_add_device(struct udevice *udev)
89 char filename[PATH_SIZE];
94 devpath_to_db_path(udev->dev->devpath, filename, sizeof(filename));
95 create_path(filename);
96 name_index(udev->dev->devpath, udev->name, 1);
99 * create only a symlink with the name as the target
100 * if we don't have any interesting data to remember
102 if (list_empty(&udev->symlink_list) && list_empty(&udev->env_list) &&
103 !udev->partitions && !udev->ignore_remove) {
104 dbg("nothing interesting to store, create symlink");
106 if (symlink(udev->name, filename) != 0) {
107 err("unable to create db link '%s': %s", filename, strerror(errno));
111 struct name_entry *name_loop;
115 f = fopen(filename, "w");
117 err("unable to create db file '%s': %s", filename, strerror(errno));
120 dbg("storing data for device '%s' in '%s'", udev->dev->devpath, filename);
122 fprintf(f, "N:%s\n", udev->name);
123 list_for_each_entry(name_loop, &udev->symlink_list, node) {
124 fprintf(f, "S:%s\n", name_loop->name);
125 name_index(udev->dev->devpath, name_loop->name, 1);
127 fprintf(f, "M:%u:%u\n", major(udev->devt), minor(udev->devt));
128 if (udev->partitions)
129 fprintf(f, "A:%u\n", udev->partitions);
130 if (udev->ignore_remove)
131 fprintf(f, "R:%u\n", udev->ignore_remove);
132 list_for_each_entry(name_loop, &udev->env_list, node)
133 fprintf(f, "E:%s\n", name_loop->name);
139 int udev_db_get_device(struct udevice *udev, const char *devpath)
142 char filename[PATH_SIZE];
143 char line[PATH_SIZE];
144 unsigned int maj, min;
151 strlcpy(udev->dev->devpath, devpath, sizeof(udev->dev->devpath));
152 devpath_to_db_path(devpath, filename, sizeof(filename));
154 if (lstat(filename, &stats) != 0) {
155 info("no db file to read %s: %s", filename, strerror(errno));
158 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
159 char target[NAME_SIZE];
162 info("found a symlink as db file");
163 target_len = readlink(filename, target, sizeof(target));
165 target[target_len] = '\0';
167 info("error reading db link %s: %s", filename, strerror(errno));
170 dbg("db link points to '%s'", target);
171 strlcpy(udev->name, target, sizeof(udev->name));
175 if (file_map(filename, &buf, &bufsize) != 0) {
176 info("error reading db file %s: %s", filename, strerror(errno));
181 while (cur < bufsize) {
182 count = buf_get_line(buf, bufsize, cur);
188 if (count > sizeof(udev->name))
189 count = sizeof(udev->name);
190 memcpy(udev->name, &bufline[2], count-2);
191 udev->name[count-2] = '\0';
194 if (count > sizeof(line))
195 count = sizeof(line);
196 memcpy(line, &bufline[2], count-2);
197 line[count-2] = '\0';
198 sscanf(line, "%u:%u", &maj, &min);
199 udev->devt = makedev(maj, min);
202 if (count > sizeof(line))
203 count = sizeof(line);
204 memcpy(line, &bufline[2], count-2);
205 line[count-2] = '\0';
206 name_list_add(&udev->symlink_list, line, 0);
209 if (count > sizeof(line))
210 count = sizeof(line);
211 memcpy(line, &bufline[2], count-2);
212 line[count-2] = '\0';
213 udev->partitions = atoi(line);
216 if (count > sizeof(line))
217 count = sizeof(line);
218 memcpy(line, &bufline[2], count-2);
219 line[count-2] = '\0';
220 udev->ignore_remove = atoi(line);
223 if (count > sizeof(line))
224 count = sizeof(line);
225 memcpy(line, &bufline[2], count-2);
226 line[count-2] = '\0';
227 name_list_add(&udev->env_list, line, 0);
231 file_unmap(buf, bufsize);
233 if (udev->name[0] == '\0')
239 int udev_db_delete_device(struct udevice *udev)
241 char filename[PATH_SIZE];
242 struct name_entry *name_loop;
244 devpath_to_db_path(udev->dev->devpath, filename, sizeof(filename));
247 name_index(udev->dev->devpath, udev->name, 0);
248 list_for_each_entry(name_loop, &udev->symlink_list, node)
249 name_index(udev->dev->devpath, name_loop->name, 0);
254 int udev_db_lookup_name(const char *name, char *devpath, size_t len)
256 char dirname[PATH_MAX];
261 strlcpy(dirname, udev_root, sizeof(dirname));
262 start = strlcat(dirname, "/"DB_NAME_INDEX_DIR"/", sizeof(dirname));
263 strlcat(dirname, name, sizeof(dirname));
264 path_encode(&dirname[start+1], sizeof(dirname) - (start+1));
266 dir = opendir(dirname);
268 info("no index directory '%s': %s", dirname, strerror(errno));
272 info("found index directory '%s'", dirname);
275 char device[PATH_SIZE];
276 char filename[PATH_SIZE];
280 if (ent == NULL || ent->d_name[0] == '\0')
282 if (ent->d_name[0] == '.')
285 strlcpy(device, "/", len);
286 strlcat(device, ent->d_name, sizeof(device));
289 dbg("looking at '%s'", device);
290 strlcpy(filename, sysfs_path, sizeof(filename));
291 strlcat(filename, device, sizeof(filename));
292 if (stat(filename, &statbuf) == 0) {
293 strlcpy(devpath, device, len);
306 int udev_db_get_all_entries(struct list_head *name_list)
308 char dbpath[PATH_MAX];
311 strlcpy(dbpath, udev_root, sizeof(dbpath));
312 strlcat(dbpath, "/"DB_DIR, sizeof(dbpath));
313 dir = opendir(dbpath);
315 info("no udev_db available '%s': %s", dbpath, strerror(errno));
321 char device[PATH_SIZE];
324 if (ent == NULL || ent->d_name[0] == '\0')
326 if (ent->d_name[0] == '.')
329 db_file_to_devpath(ent->d_name, device, sizeof(device));
330 name_list_add(name_list, device, 1);
331 dbg("added '%s'", device);