X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=udevdb.c;h=8d077ea94fc766b652cdd0af705210867e23218f;hb=b6864b4bf1945d6b950a77e96fd68e9952bbeb02;hp=ec67a079627df09cec9cdf5a1733d99e87139051;hpb=c056c5141b16fe95485eeb233fe8b90954686a60;p=elogind.git diff --git a/udevdb.c b/udevdb.c index ec67a0796..8d077ea94 100644 --- a/udevdb.c +++ b/udevdb.c @@ -24,6 +24,7 @@ /* * udev database library */ +#define _KLIBC_HAS_ARCH_SIG_ATOMIC_T #include #include #include @@ -54,36 +55,29 @@ int udevdb_add_dev(const char *path, const struct udevice *dev) strcpy(keystr, path); key.dptr = keystr; key.dsize = strlen(keystr) + 1; - + data.dptr = (void *)dev; data.dsize = sizeof(*dev); return tdb_store(udevdb, key, data, TDB_REPLACE); } -struct udevice *udevdb_get_dev(const char *path) +int udevdb_get_dev(const char *path, struct udevice *dev) { TDB_DATA key, data; - struct udevice *dev; if (path == NULL) - return NULL; + return -ENODEV; key.dptr = (void *)path; key.dsize = strlen(path) + 1; data = tdb_fetch(udevdb, key); if (data.dptr == NULL || data.dsize == 0) - return NULL; + return -ENODEV; - dev = malloc(sizeof(*dev)); - if (dev == NULL) - goto exit; - memcpy(dev, data.dptr, sizeof(*dev)); -exit: - free(data.dptr); - return dev; + return 0; } int udevdb_delete_dev(const char *path) @@ -99,7 +93,7 @@ int udevdb_delete_dev(const char *path) key.dptr = keystr; key.dsize = strlen(keystr) + 1; - + return tdb_delete(udevdb, key); } @@ -116,8 +110,8 @@ void udevdb_exit(void) /** * udevdb_init: initializes database - * @init_flag: database can either be in memory - UDEVDB_INTERNAL - or - * written to a file with UDEVDB_DEFAULT. + * @init_flag: UDEVDB_INTERNAL - database stays in memory + * UDEVDB_DEFAULT - database is written to a file */ int udevdb_init(int init_flag) { @@ -127,10 +121,46 @@ int udevdb_init(int init_flag) udevdb = tdb_open(udev_db_filename, 0, init_flag, O_RDWR | O_CREAT, 0644); if (udevdb == NULL) { if (init_flag == UDEVDB_INTERNAL) - dbg("Unable to initialize in-memory database"); + dbg("unable to initialize in-memory database"); else - dbg("Unable to initialize database at %s", udev_db_filename); + dbg("unable to initialize database at '%s'", udev_db_filename); + return -EACCES; + } + return 0; +} + +/** + * udevdb_open_ro: open database for reading + */ +int udevdb_open_ro(void) +{ + udevdb = tdb_open(udev_db_filename, 0, 0, O_RDONLY, 0); + if (udevdb == NULL) { + dbg("unable to open database at '%s'", udev_db_filename); + return -EACCES; + } + return 0; +} + +void (*user_record_callback) (char *path, struct udevice *dev); + +static int traverse_callback(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state) +{ + user_record_callback((char*) key.dptr, (struct udevice*) dbuf.dptr); + return 0; +} + +/** + * udevdb_dump: dumps whole database by passing record data to user function + * @user_record_handler: user function called for every record in the database + */ +int udevdb_dump(void (*user_record_handler) (char *path, struct udevice *dev)) +{ + if (user_record_handler == NULL) { + dbg("invalid user record handling function"); return -EINVAL; } + user_record_callback = user_record_handler; + tdb_traverse(udevdb, traverse_callback, NULL); return 0; }