2 * udevdb.c - udev database library
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.
34 #include "libsysfs/sysfs/libsysfs.h"
37 #include "udev_version.h"
43 static TDB_CONTEXT *udevdb;
44 sig_atomic_t gotalarm;
46 int udevdb_add_dev(struct udevice *udev)
49 char keystr[SYSFS_PATH_MAX];
57 memset(keystr, 0x00, SYSFS_PATH_MAX);
58 strfieldcpy(keystr, udev->devpath);
60 key.dsize = strlen(keystr) + 1;
62 data.dptr = (void *) udev;
63 data.dsize = UDEVICE_DB_LEN;
64 dbg("store key '%s' for device '%s'", keystr, udev->name);
66 return tdb_store(udevdb, key, data, TDB_REPLACE);
69 int udevdb_get_dev(const char *path, struct udevice *udev)
79 key.dptr = (void *)path;
80 key.dsize = strlen(path) + 1;
82 data = tdb_fetch(udevdb, key);
83 if (data.dptr == NULL || data.dsize == 0)
86 memset(udev, 0x00, sizeof(struct udevice));
87 memcpy(udev, data.dptr, UDEVICE_DB_LEN);
92 int udevdb_delete_dev(const char *path)
95 char keystr[SYSFS_PATH_MAX];
103 memset(keystr, 0, sizeof(keystr));
104 strfieldcpy(keystr, path);
107 key.dsize = strlen(keystr) + 1;
109 return tdb_delete(udevdb, key);
113 * udevdb_exit: closes database
115 void udevdb_exit(void)
117 if (udevdb != NULL) {
124 * udevdb_init: initializes database
125 * @init_flag: UDEVDB_INTERNAL - database stays in memory
126 * UDEVDB_DEFAULT - database is written to a file
128 int udevdb_init(int init_flag)
130 if (init_flag != UDEVDB_DEFAULT && init_flag != UDEVDB_INTERNAL)
133 tdb_set_lock_alarm(&gotalarm);
135 udevdb = tdb_open(udev_db_filename, 0, init_flag, O_RDWR | O_CREAT, 0644);
136 if (udevdb == NULL) {
137 if (init_flag == UDEVDB_INTERNAL)
138 dbg("unable to initialize in-memory database");
140 dbg("unable to initialize database at '%s'", udev_db_filename);
147 * udevdb_open_ro: open database for reading
149 int udevdb_open_ro(void)
151 udevdb = tdb_open(udev_db_filename, 0, 0, O_RDONLY, 0);
152 if (udevdb == NULL) {
153 dbg("unable to open database at '%s'", udev_db_filename);
159 static int (*user_record_callback) (const char *path, struct udevice *dev);
161 static int traverse_callback(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
163 return user_record_callback((char*) key.dptr, (struct udevice*) dbuf.dptr);
167 * udevdb_call_foreach: dumps whole database by passing record data to user function
168 * @user_record_handler: user function called for every record in the database
170 int udevdb_call_foreach(int (*user_record_handler) (const char *path, struct udevice *dev))
177 if (user_record_handler == NULL) {
178 dbg("invalid user record handling function");
181 user_record_callback = user_record_handler;
182 retval = tdb_traverse(udevdb, traverse_callback, NULL);
189 static struct udevice *find_dev;
190 static char *find_path;
191 static const char *find_name;
192 static int find_found;
194 static int find_device_by_name(const char *path, struct udevice *udev)
199 if (strncmp(udev->name, find_name, sizeof(udev->name)) == 0) {
200 memcpy(find_dev, udev, sizeof(struct udevice));
201 strfieldcpymax(find_path, path, NAME_SIZE);
206 /* look for matching symlink*/
207 foreach_strpart(udev->symlink, " ", pos, len) {
208 if (strncmp(pos, find_name, len) != 0)
211 if (len != strlen(find_name))
214 memcpy(find_dev, udev, sizeof(struct udevice));
215 strfieldcpymax(find_path, path, NAME_SIZE);
223 * udevdb_get_dev_byname: search device with given name by traversing the whole database
225 int udevdb_get_dev_byname(const char *name, char *path, struct udevice *dev)
231 udevdb_call_foreach(find_device_by_name);