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
37 #include "libsysfs/sysfs/libsysfs.h"
38 #include "udev_version.h"
45 static TDB_CONTEXT *udevdb;
48 int udevdb_add_dev(const char *path, const struct udevice *dev)
51 char keystr[SYSFS_PATH_MAX];
53 if ((path == NULL) || (dev == NULL))
56 memset(keystr, 0, NAME_SIZE);
57 strfieldcpy(keystr, path);
59 key.dsize = strlen(keystr) + 1;
61 data.dptr = (void *)dev;
62 data.dsize = UDEVICE_LEN;
64 return tdb_store(udevdb, key, data, TDB_REPLACE);
67 int udevdb_get_dev(const char *path, struct udevice *dev)
74 key.dptr = (void *)path;
75 key.dsize = strlen(path) + 1;
77 data = tdb_fetch(udevdb, key);
78 if (data.dptr == NULL || data.dsize == 0)
81 memset(dev, 0, sizeof(struct udevice));
82 memcpy(dev, data.dptr, UDEVICE_LEN);
86 int udevdb_delete_dev(const char *path)
89 char keystr[SYSFS_PATH_MAX];
94 memset(keystr, 0, sizeof(keystr));
95 strfieldcpy(keystr, path);
98 key.dsize = strlen(keystr) + 1;
100 return tdb_delete(udevdb, key);
104 * udevdb_exit: closes database
106 void udevdb_exit(void)
108 if (udevdb != NULL) {
115 * udevdb_init: initializes database
116 * @init_flag: UDEVDB_INTERNAL - database stays in memory
117 * UDEVDB_DEFAULT - database is written to a file
119 int udevdb_init(int init_flag)
121 if (init_flag != UDEVDB_DEFAULT && init_flag != UDEVDB_INTERNAL)
124 udevdb = tdb_open(udev_db_filename, 0, init_flag, O_RDWR | O_CREAT, 0644);
125 if (udevdb == NULL) {
126 if (init_flag == UDEVDB_INTERNAL)
127 dbg("unable to initialize in-memory database");
129 dbg("unable to initialize database at '%s'", udev_db_filename);
136 * udevdb_open_ro: open database for reading
138 int udevdb_open_ro(void)
140 udevdb = tdb_open(udev_db_filename, 0, 0, O_RDONLY, 0);
141 if (udevdb == NULL) {
142 dbg("unable to open database at '%s'", udev_db_filename);
148 static int (*user_record_callback) (char *path, struct udevice *dev);
150 static int traverse_callback(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
152 return user_record_callback((char*) key.dptr, (struct udevice*) dbuf.dptr);
156 * udevdb_call_foreach: dumps whole database by passing record data to user function
157 * @user_record_handler: user function called for every record in the database
159 int udevdb_call_foreach(int (*user_record_handler) (char *path, struct udevice *dev))
163 if (user_record_handler == NULL) {
164 dbg("invalid user record handling function");
167 user_record_callback = user_record_handler;
168 retval = tdb_traverse(udevdb, traverse_callback, NULL);
175 static struct udevice *find_dev;
176 static char *find_path;
177 static const char *find_name;
178 static int find_found;
180 static int find_device_by_name(char *path, struct udevice *dev)
185 if (strncmp(dev->name, find_name, sizeof(dev->name)) == 0) {
186 memcpy(find_dev, dev, sizeof(struct udevice));
187 strfieldcpymax(find_path, path, NAME_SIZE);
192 /* look for matching symlink*/
193 foreach_strpart(dev->symlink, " ", pos, len) {
194 if (strncmp(pos, find_name, len) != 0)
197 if (len != strlen(find_name))
200 memcpy(find_dev, dev, sizeof(struct udevice));
201 strfieldcpymax(find_path, path, NAME_SIZE);
209 * udevdb_get_dev_byname: search device with given name by traversing the whole database
211 int udevdb_get_dev_byname(const char *name, char *path, struct udevice *dev)
217 udevdb_call_foreach(find_device_by_name);