6 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
7 * Copyright (C) 2003 IBM Corp.
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation version 2 of the License.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 675 Mass Ave, Cambridge, MA 02139, USA.
25 * udev database library
35 #include "udev_version.h"
40 #include "libsysfs/libsysfs.h"
42 static TDB_CONTEXT *udevdb;
44 struct sysfsdb_record {
49 * namedb_record - device name is key, remaining udevice info stored here.
51 struct namedb_record {
52 char sysfs_dev_path[PATH_SIZE];
53 char class_dev_name[NAME_SIZE];
54 char class_name[NAME_SIZE];
63 * udevdb_close: close udev database
65 static void udevdb_close(void)
74 * udevdb_open: opens udev's database
75 * @method: database can either be in memory - UDEVDB_INTERNAL - or
76 * written to a file with UDEVDB_DEFAULT.
78 static int udevdb_open(int method)
80 udevdb = tdb_open(UDEV_CONFIG_DIR UDEV_DB, 0, method, O_RDWR | O_CREAT, 0644);
82 if (method == UDEVDB_INTERNAL)
83 dbg("Unable to initialize in-memory database");
85 dbg("Unable to initialize database at %s", UDEV_CONFIG_DIR UDEV_DB);
91 static struct sysfsdb_record *sysfsdb_fetch(const char *path)
94 char keystr[PATH_SIZE+2];
95 struct sysfsdb_record *rec = NULL;
97 if (strlen(path) >= PATH_SIZE)
100 memset(keystr, 0, sizeof(keystr));
101 strcpy(keystr, path);
103 dbg("keystr = %s", keystr);
105 key.dptr = (void *)keystr;
106 key.dsize = strlen(keystr) + 1;
108 data = tdb_fetch(udevdb, key);
109 if (data.dptr == NULL || data.dsize == 0) {
110 dbg("tdb_fetch did not work :(");
114 rec = (struct sysfsdb_record *)malloc(sizeof(struct sysfsdb_record));
120 memcpy(rec, data.dptr, sizeof(struct sysfsdb_record));
129 static struct namedb_record *namedb_fetch(const char *name)
132 char nm_keystr[NAME_SIZE];
133 struct namedb_record *nrec = NULL;
137 if (strlen(name) >= NAME_SIZE)
140 memset(nm_keystr, 0, NAME_SIZE);
141 strcpy(nm_keystr, name);
143 key.dptr = (void *)nm_keystr;
144 key.dsize = strlen(nm_keystr) + 1;
146 data = tdb_fetch(udevdb, key);
147 if (data.dptr == NULL || data.dsize == 0)
150 nrec = (struct namedb_record *)malloc(sizeof(struct namedb_record));
156 memcpy(nrec, data.dptr, sizeof(struct namedb_record));
162 static int sysfs_store(const char *path, const struct udevice *dev)
165 char keystr[PATH_SIZE+2];
166 struct sysfsdb_record rec;
172 memset(keystr, 0, sizeof(keystr));
173 strcpy(keystr, path);
175 key.dptr = (void *)keystr;
176 key.dsize = strlen(keystr) + 1;
178 strcpy(rec.name, dev->name);
180 data.dptr = (void *) &rec;
181 data.dsize = sizeof(rec);
183 retval = tdb_store(udevdb, key, data, TDB_REPLACE);
190 static int namedb_store(const struct udevice *dev)
193 char keystr[NAME_SIZE];
194 struct namedb_record rec;
200 memset(keystr, 0, NAME_SIZE);
201 strcpy(keystr, dev->name);
203 key.dptr = (void *)keystr;
204 key.dsize = strlen(keystr) + 1;
206 strcpy(rec.sysfs_dev_path, dev->sysfs_dev_path);
207 strcpy(rec.id, dev->bus_id);
208 strcpy(rec.class_dev_name, dev->class_dev_name);
209 strcpy(rec.class_name, dev->class_name);
210 rec.type = dev->type;
211 rec.major = dev->major;
212 rec.minor = dev->minor;
213 rec.mode = dev->mode;
215 data.dptr = (void *) &rec;
216 data.dsize = sizeof(rec);
218 retval = tdb_store(udevdb, key, data, TDB_REPLACE);
225 static int namedb_delete(const char *name)
228 char keystr[NAME_SIZE];
233 if (strlen(name) >= NAME_SIZE)
236 memset(keystr, 0, NAME_SIZE);
237 strcpy(keystr, name);
239 key.dptr = (void *)keystr;
240 key.dsize = strlen(keystr) + 1;
242 retval = tdb_delete(udevdb, key);
246 static int sysfs_delete(const char *path)
249 char keystr[PATH_SIZE];
251 memset(keystr, 0, sizeof(keystr));
252 strcpy(keystr, path);
255 key.dsize = strlen(keystr) + 1;
257 return tdb_delete(udevdb, key);
261 * udevdb_delete_udevice
263 int udevdb_delete_udevice(const char *name)
265 struct namedb_record *nrec = NULL;
270 nrec = namedb_fetch(name);
281 * udevdb_add_device: adds class device to database
283 int udevdb_add_device(const char *device, const struct sysfs_class_device *class_dev, const char *name, char type, int major, int minor, int mode)
285 struct udevice dbdev;
287 if (class_dev == NULL)
290 memset(&dbdev, 0, sizeof(dbdev));
291 strncpy(dbdev.name, name, NAME_SIZE);
292 if (class_dev->sysdevice) {
293 strncpy(dbdev.sysfs_dev_path, class_dev->sysdevice->directory->path, PATH_SIZE);
294 strncpy(dbdev.bus_id, class_dev->sysdevice->bus_id, ID_SIZE);
296 strncpy(dbdev.class_dev_name, class_dev->name, NAME_SIZE);
297 // if ((sysfs_get_name_from_path(subsystem, dbdev.class_name, NAME_SIZE)) != 0)
298 // strcpy(dbdev.class_name, "unknown");
304 if ((sysfs_store(device, &dbdev)) != 0)
306 // if ((namedb_store(&dbdev)) != 0)
313 * udevdb_get_device: grab's device by name
315 struct udevice *udevdb_get_udevice(const char *name)
317 struct namedb_record *nrec = NULL;
318 struct udevice *dev = NULL;
323 nrec = namedb_fetch(name);
327 dev = (struct udevice *)malloc(sizeof(struct udevice));
333 strcpy(dev->name, name);
334 strcpy(dev->sysfs_dev_path, nrec->sysfs_dev_path);
335 strcpy(dev->class_dev_name, nrec->class_dev_name);
336 strcpy(dev->class_name, nrec->class_name);
337 strcpy(dev->bus_id, nrec->id);
338 dev->type = nrec->type;
339 dev->major = nrec->major;
340 dev->minor = nrec->minor;
341 dev->mode = nrec->mode;
348 int udevdb_get_dev(const char *path, char *name, size_t name_size)
350 struct sysfsdb_record *rec = NULL;
352 if ((path == NULL) || (name == NULL) || (name_size < 1))
355 rec = sysfsdb_fetch(path);
359 if (strlen(rec->name) >= name_size)
362 strncpy(name, rec->name, name_size);
366 int udevdb_delete_dev(const char *path)
371 return sysfs_delete(path);
375 * udevdb_exit: closes database
377 void udevdb_exit(void)
383 * udevdb_init: initializes database
385 int udevdb_init(int init_flag)
387 if (init_flag != UDEVDB_DEFAULT && init_flag != UDEVDB_INTERNAL)
390 return udevdb_open(init_flag);