2003-10-17 12:40:02 +04:00
/*
* udevdb . c
*
* Userspace devfs
*
* Copyright ( C ) 2003 Greg Kroah - Hartman < greg @ kroah . com >
* Copyright ( C ) 2003 IBM Corp .
*
* This program is free software ; you can redistribute it and / or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation version 2 of the License .
*
* This program is distributed in the hope that it will be useful , but
* WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
* General Public License for more details .
*
* You should have received a copy of the GNU General Public License along
* with this program ; if not , write to the Free Software Foundation , Inc . ,
* 675 Mass Ave , Cambridge , MA 0213 9 , USA .
*
*/
2003-08-06 10:57:23 +04:00
/*
* udev database library
*/
# include <stdlib.h>
# include <stdio.h>
# include <fcntl.h>
# include <string.h>
# include <sys/stat.h>
# include <errno.h>
2003-08-06 11:03:30 +04:00
# include <signal.h>
2003-08-06 10:57:23 +04:00
2003-10-16 10:50:13 +04:00
# include "udev_version.h"
# include "udev.h"
# include "namedev.h"
2003-08-06 10:57:23 +04:00
# include "udevdb.h"
2003-08-06 11:03:30 +04:00
# include "tdb/tdb.h"
2003-10-18 10:32:17 +04:00
# include "libsysfs/libsysfs.h"
2003-08-06 10:57:23 +04:00
2003-10-15 10:20:53 +04:00
static TDB_CONTEXT * udevdb ;
2003-08-06 10:57:23 +04:00
2003-10-18 10:32:17 +04:00
2003-10-21 09:48:44 +04:00
int udevdb_add_dev ( const char * path , const struct udevice * dev )
2003-10-18 10:32:17 +04:00
{
TDB_DATA key , data ;
2003-10-21 09:48:44 +04:00
char keystr [ SYSFS_PATH_MAX ] ;
2003-10-18 10:32:17 +04:00
2003-10-21 09:48:44 +04:00
if ( ( path = = NULL ) | | ( dev = = NULL ) )
return - ENODEV ;
2003-10-18 10:32:17 +04:00
2003-10-21 09:48:44 +04:00
memset ( keystr , 0 , NAME_SIZE ) ;
2003-10-18 10:32:17 +04:00
strcpy ( keystr , path ) ;
2003-10-21 09:48:44 +04:00
key . dptr = keystr ;
2003-10-18 10:32:17 +04:00
key . dsize = strlen ( keystr ) + 1 ;
2003-11-25 09:27:17 +03:00
2003-10-21 09:48:44 +04:00
data . dptr = ( void * ) dev ;
data . dsize = sizeof ( * dev ) ;
2003-10-18 10:32:17 +04:00
2003-10-21 09:48:44 +04:00
return tdb_store ( udevdb , key , data , TDB_REPLACE ) ;
2003-10-18 10:32:17 +04:00
}
2003-10-21 09:48:44 +04:00
struct udevice * udevdb_get_dev ( const char * path )
2003-08-06 10:57:23 +04:00
{
TDB_DATA key , data ;
2003-10-21 09:48:44 +04:00
struct udevice * dev ;
2003-08-06 10:57:23 +04:00
2003-10-21 09:48:44 +04:00
if ( path = = NULL )
2003-08-06 10:57:23 +04:00
return NULL ;
2003-10-21 09:48:44 +04:00
key . dptr = ( void * ) path ;
key . dsize = strlen ( path ) + 1 ;
2003-08-06 10:57:23 +04:00
2003-10-15 10:20:53 +04:00
data = tdb_fetch ( udevdb , key ) ;
2003-08-06 10:57:23 +04:00
if ( data . dptr = = NULL | | data . dsize = = 0 )
return NULL ;
2003-10-21 09:48:44 +04:00
dev = malloc ( sizeof ( * dev ) ) ;
2003-08-06 10:57:23 +04:00
if ( dev = = NULL )
2003-10-21 09:48:44 +04:00
goto exit ;
2003-11-25 09:27:17 +03:00
2003-10-21 09:48:44 +04:00
memcpy ( dev , data . dptr , sizeof ( * dev ) ) ;
exit :
free ( data . dptr ) ;
return dev ;
2003-08-06 10:57:23 +04:00
}
2003-10-21 09:48:44 +04:00
int udevdb_delete_dev ( const char * path )
2003-08-06 10:57:23 +04:00
{
TDB_DATA key ;
2003-10-21 09:48:44 +04:00
char keystr [ SYSFS_PATH_MAX ] ;
2003-08-06 10:57:23 +04:00
2003-10-21 09:48:44 +04:00
if ( path = = NULL )
return - EINVAL ;
2003-10-21 07:28:42 +04:00
memset ( keystr , 0 , sizeof ( keystr ) ) ;
strcpy ( keystr , path ) ;
key . dptr = keystr ;
key . dsize = strlen ( keystr ) + 1 ;
2003-11-25 09:27:17 +03:00
2003-10-21 07:28:42 +04:00
return tdb_delete ( udevdb , key ) ;
}
2003-10-15 10:20:53 +04:00
/**
* udevdb_exit : closes database
*/
void udevdb_exit ( void )
{
2003-10-21 09:48:44 +04:00
if ( udevdb ! = NULL ) {
tdb_close ( udevdb ) ;
udevdb = NULL ;
}
2003-10-15 10:20:53 +04:00
}
/**
* udevdb_init : initializes database
2003-11-25 09:27:17 +03:00
* @ init_flag : UDEVDB_INTERNAL - database stays in memory
* UDEVDB_DEFAULT - database is written to a file
2003-10-15 10:20:53 +04:00
*/
int udevdb_init ( int init_flag )
{
if ( init_flag ! = UDEVDB_DEFAULT & & init_flag ! = UDEVDB_INTERNAL )
2003-10-21 09:48:44 +04:00
return - EINVAL ;
2003-10-15 10:20:53 +04:00
2003-10-22 07:19:09 +04:00
udevdb = tdb_open ( udev_db_filename , 0 , init_flag , O_RDWR | O_CREAT , 0644 ) ;
2003-10-21 09:48:44 +04:00
if ( udevdb = = NULL ) {
if ( init_flag = = UDEVDB_INTERNAL )
2003-11-25 09:27:17 +03:00
dbg ( " unable to initialize in-memory database " ) ;
2003-10-21 09:48:44 +04:00
else
2003-11-25 09:27:17 +03:00
dbg ( " unable to initialize database at '%s' " , udev_db_filename ) ;
2003-10-21 09:48:44 +04:00
return - EINVAL ;
}
return 0 ;
2003-10-15 10:20:53 +04:00
}