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;
45 int udevdb_add_dev(const char *path, const struct udevice *dev)
48 char keystr[SYSFS_PATH_MAX];
50 if ((path == NULL) || (dev == NULL))
53 memset(keystr, 0, NAME_SIZE);
56 key.dsize = strlen(keystr) + 1;
58 data.dptr = (void *)dev;
59 data.dsize = sizeof(*dev);
61 return tdb_store(udevdb, key, data, TDB_REPLACE);
64 struct udevice *udevdb_get_dev(const char *path)
72 key.dptr = (void *)path;
73 key.dsize = strlen(path) + 1;
75 data = tdb_fetch(udevdb, key);
76 if (data.dptr == NULL || data.dsize == 0)
79 dev = malloc(sizeof(*dev));
83 memcpy(dev, data.dptr, sizeof(*dev));
89 int udevdb_delete_dev(const char *path)
92 char keystr[SYSFS_PATH_MAX];
97 memset(keystr, 0, sizeof(keystr));
101 key.dsize = strlen(keystr) + 1;
103 return tdb_delete(udevdb, key);
107 * udevdb_exit: closes database
109 void udevdb_exit(void)
111 if (udevdb != NULL) {
118 * udevdb_init: initializes database
119 * @init_flag: database can either be in memory - UDEVDB_INTERNAL - or
120 * written to a file with UDEVDB_DEFAULT.
122 int udevdb_init(int init_flag)
124 if (init_flag != UDEVDB_DEFAULT && init_flag != UDEVDB_INTERNAL)
127 udevdb = tdb_open(udev_db_filename, 0, init_flag, O_RDWR | O_CREAT, 0644);
128 if (udevdb == NULL) {
129 if (init_flag == UDEVDB_INTERNAL)
130 dbg("Unable to initialize in-memory database");
132 dbg("Unable to initialize database at %s", udev_db_filename);