2007-05-08 17:44:36 +04:00
/*
Unix SMB / CIFS implementation .
Low - level connections . tdb access functions
Copyright ( C ) Volker Lendecke 2007
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 ; either version 2 of the License , or
( at your option ) any later version .
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 .
*/
# include "includes.h"
2007-05-28 15:38:42 +04:00
static struct db_context * connections_db_ctx ( BOOL rw )
2007-05-08 17:44:36 +04:00
{
2007-05-28 15:38:42 +04:00
static struct db_context * db_ctx ;
2007-05-08 17:44:36 +04:00
2007-05-28 15:38:42 +04:00
if ( db_ctx ! = NULL ) {
return db_ctx ;
2007-05-08 17:44:36 +04:00
}
if ( rw ) {
2007-05-28 15:38:42 +04:00
db_ctx = db_open ( NULL , lock_path ( " connections.tdb " ) , 0 ,
TDB_CLEAR_IF_FIRST | TDB_DEFAULT ,
O_RDWR | O_CREAT , 0644 ) ;
2007-05-08 17:44:36 +04:00
}
else {
2007-05-28 15:38:42 +04:00
db_ctx = db_open ( NULL , lock_path ( " connections.tdb " ) , 0 ,
TDB_DEFAULT , O_RDONLY , 0 ) ;
2007-05-08 17:44:36 +04:00
}
2007-05-28 15:38:42 +04:00
return db_ctx ;
}
struct db_record * connections_fetch_record ( TALLOC_CTX * mem_ctx ,
TDB_DATA key )
{
struct db_context * ctx = connections_db_ctx ( True ) ;
if ( ctx = = NULL ) {
return NULL ;
2007-05-08 17:44:36 +04:00
}
2007-05-28 15:38:42 +04:00
return ctx - > fetch_locked ( ctx , mem_ctx , key ) ;
}
struct db_record * connections_fetch_entry ( TALLOC_CTX * mem_ctx ,
connection_struct * conn ,
const char * name )
{
struct connections_key ckey ;
TDB_DATA key ;
ZERO_STRUCT ( ckey ) ;
ckey . pid = procid_self ( ) ;
ckey . cnum = conn ? conn - > cnum : - 1 ;
strlcpy ( ckey . name , name , sizeof ( ckey . name ) ) ;
key . dsize = sizeof ( ckey ) ;
key . dptr = ( uint8 * ) & ckey ;
return connections_fetch_record ( mem_ctx , key ) ;
2007-05-08 17:44:36 +04:00
}
struct conn_traverse_state {
2007-05-28 15:38:42 +04:00
int ( * fn ) ( struct db_record * rec ,
2007-05-08 17:44:36 +04:00
const struct connections_key * key ,
const struct connections_data * data ,
void * private_data ) ;
void * private_data ;
} ;
2007-05-28 15:38:42 +04:00
static int conn_traverse_fn ( struct db_record * rec , void * private_data )
2007-05-08 17:44:36 +04:00
{
struct conn_traverse_state * state =
( struct conn_traverse_state * ) private_data ;
2007-05-28 15:38:42 +04:00
if ( ( rec - > key . dsize ! = sizeof ( struct connections_key ) )
| | ( rec - > value . dsize ! = sizeof ( struct connections_data ) ) ) {
2007-05-08 17:44:36 +04:00
return 0 ;
}
2007-05-28 15:38:42 +04:00
return state - > fn ( rec , ( const struct connections_key * ) rec - > key . dptr ,
( const struct connections_data * ) rec - > value . dptr ,
state - > private_data ) ;
2007-05-08 17:44:36 +04:00
}
2007-05-28 15:38:42 +04:00
int connections_traverse ( int ( * fn ) ( struct db_record * rec ,
void * private_data ) ,
2007-05-08 17:44:36 +04:00
void * private_data )
{
2007-05-28 15:38:42 +04:00
struct db_context * ctx = connections_db_ctx ( False ) ;
2007-05-08 17:44:36 +04:00
2007-05-28 15:38:42 +04:00
if ( ctx = = NULL ) {
2007-05-08 17:44:36 +04:00
return - 1 ;
}
2007-05-28 15:38:42 +04:00
return ctx - > traverse ( ctx , fn , private_data ) ;
2007-05-08 17:44:36 +04:00
}
2007-05-28 15:38:42 +04:00
int connections_forall ( int ( * fn ) ( struct db_record * rec ,
2007-05-08 17:44:36 +04:00
const struct connections_key * key ,
const struct connections_data * data ,
void * private_data ) ,
void * private_data )
{
struct conn_traverse_state state ;
state . fn = fn ;
state . private_data = private_data ;
return connections_traverse ( conn_traverse_fn , ( void * ) & state ) ;
}
BOOL connections_init ( BOOL rw )
{
2007-05-28 15:38:42 +04:00
return ( connections_db_ctx ( rw ) ! = NULL ) ;
2007-05-08 17:44:36 +04:00
}