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
27 #define _KLIBC_HAS_ARCH_SIG_ATOMIC_T
36 #include "udev_version.h"
42 #include "libsysfs/libsysfs.h"
44 static TDB_CONTEXT *udevdb;
47 int udevdb_add_dev(const char *path, const struct udevice *dev)
50 char keystr[SYSFS_PATH_MAX];
52 if ((path == NULL) || (dev == NULL))
55 memset(keystr, 0, NAME_SIZE);
58 key.dsize = strlen(keystr) + 1;
60 data.dptr = (void *)dev;
61 data.dsize = sizeof(*dev);
63 return tdb_store(udevdb, key, data, TDB_REPLACE);
66 int udevdb_get_dev(const char *path, struct udevice *dev)
73 key.dptr = (void *)path;
74 key.dsize = strlen(path) + 1;
76 data = tdb_fetch(udevdb, key);
77 if (data.dptr == NULL || data.dsize == 0)
80 memcpy(dev, data.dptr, sizeof(*dev));
84 int udevdb_delete_dev(const char *path)
87 char keystr[SYSFS_PATH_MAX];
92 memset(keystr, 0, sizeof(keystr));
96 key.dsize = strlen(keystr) + 1;
98 return tdb_delete(udevdb, key);
102 * udevdb_exit: closes database
104 void udevdb_exit(void)
106 if (udevdb != NULL) {
113 * udevdb_init: initializes database
114 * @init_flag: UDEVDB_INTERNAL - database stays in memory
115 * UDEVDB_DEFAULT - database is written to a file
117 int udevdb_init(int init_flag)
119 if (init_flag != UDEVDB_DEFAULT && init_flag != UDEVDB_INTERNAL)
122 udevdb = tdb_open(udev_db_filename, 0, init_flag, O_RDWR | O_CREAT, 0644);
123 if (udevdb == NULL) {
124 if (init_flag == UDEVDB_INTERNAL)
125 dbg("unable to initialize in-memory database");
127 dbg("unable to initialize database at '%s'", udev_db_filename);
134 * udevdb_open_ro: open database for reading
136 int udevdb_open_ro(void)
138 udevdb = tdb_open(udev_db_filename, 0, 0, O_RDONLY, 0);
139 if (udevdb == NULL) {
140 dbg("unable to open database at '%s'", udev_db_filename);
146 void (*user_record_callback) (char *path, struct udevice *dev);
148 static int traverse_callback(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
150 user_record_callback((char*) key.dptr, (struct udevice*) dbuf.dptr);
155 * udevdb_dump: dumps whole database by passing record data to user function
156 * @user_record_handler: user function called for every record in the database
158 int udevdb_dump(void (*user_record_handler) (char *path, struct udevice *dev))
160 if (user_record_handler == NULL) {
161 dbg("invalid user record handling function");
164 user_record_callback = user_record_handler;
165 tdb_traverse(udevdb, traverse_callback, NULL);