2000-09-11 11:02:43 +04:00
/*
Unix SMB / Netbios implementation .
Version 3.0
Samba internal messaging functions
Copyright ( C ) Andrew Tridgell 2000
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 .
*/
2000-09-12 04:47:11 +04:00
/* this module is used for internal messaging between Samba daemons.
The idea is that if a part of Samba wants to do communication with
another Samba process then it will do a message_register ( ) of a
dispatch function , and use message_send_pid ( ) to send messages to
that process .
This system doesn ' t have any inherent size limitations but is not
very efficient for large messages or when messages are sent in very
quick succession .
*/
2000-09-11 11:02:43 +04:00
# include "includes.h"
/* the locking database handle */
static TDB_CONTEXT * tdb ;
static int received_signal ;
/* change the message version with any incompatible changes in the protocol */
# define MESSAGE_VERSION 1
struct message_rec {
int msg_version ;
2000-09-12 10:13:25 +04:00
int msg_type ;
2000-09-11 11:02:43 +04:00
pid_t dest ;
pid_t src ;
size_t len ;
} ;
2000-09-12 04:47:11 +04:00
/* we have a linked list of dispatch handlers */
static struct dispatch_fns {
struct dispatch_fns * next , * prev ;
2000-09-12 10:13:25 +04:00
int msg_type ;
void ( * fn ) ( int msg_type , pid_t pid , void * buf , size_t len ) ;
2000-09-12 04:47:11 +04:00
} * dispatch_fns ;
2000-09-11 11:02:43 +04:00
/****************************************************************************
notifications come in as signals
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static void sig_usr1 ( void )
{
received_signal = 1 ;
sys_select_signal ( ) ;
}
2000-09-12 10:57:25 +04:00
/****************************************************************************
a useful function for testing the message system
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void ping_message ( int msg_type , pid_t src , void * buf , size_t len )
{
2000-10-12 21:58:40 +04:00
DEBUG ( 1 , ( " INFO: Received PING message from PID %d \n " , src ) ) ;
2000-11-17 00:38:24 +03:00
message_send_pid ( src , MSG_PONG , buf , len , True ) ;
2000-09-12 10:57:25 +04:00
}
2000-10-11 09:31:39 +04:00
/****************************************************************************
return current debug level
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void debuglevel_message ( int msg_type , pid_t src , void * buf , size_t len )
{
int level ;
2000-10-12 21:58:40 +04:00
DEBUG ( 1 , ( " INFO: Received REQ_DEBUGLEVEL message from PID %d \n " , src ) ) ;
2000-10-11 09:31:39 +04:00
level = DEBUGLEVEL ;
2000-11-17 00:38:24 +03:00
message_send_pid ( src , MSG_DEBUGLEVEL , & level , sizeof ( int ) , True ) ;
2000-10-11 09:31:39 +04:00
}
2000-09-11 11:02:43 +04:00
/****************************************************************************
Initialise the messaging functions .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
BOOL message_init ( void )
{
if ( tdb ) return True ;
tdb = tdb_open ( lock_path ( " messages.tdb " ) ,
0 , TDB_CLEAR_IF_FIRST ,
O_RDWR | O_CREAT , 0600 ) ;
if ( ! tdb ) {
DEBUG ( 0 , ( " ERROR: Failed to initialise messages database \n " ) ) ;
return False ;
}
2000-10-04 05:03:23 +04:00
CatchSignal ( SIGUSR1 , SIGNAL_CAST sig_usr1 ) ;
2000-09-11 11:02:43 +04:00
2000-09-12 10:57:25 +04:00
message_register ( MSG_PING , ping_message ) ;
2000-10-11 09:31:39 +04:00
message_register ( MSG_REQ_DEBUGLEVEL , debuglevel_message ) ;
2000-09-12 10:57:25 +04:00
2000-09-11 11:02:43 +04:00
return True ;
}
/*******************************************************************
form a static tdb key from a pid
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static TDB_DATA message_key_pid ( pid_t pid )
{
static char key [ 20 ] ;
TDB_DATA kbuf ;
slprintf ( key , sizeof ( key ) , " PID/%d " , ( int ) pid ) ;
kbuf . dptr = ( char * ) key ;
kbuf . dsize = sizeof ( key ) ;
return kbuf ;
}
/****************************************************************************
notify a process that it has a message . If the process doesn ' t exist
then delete its record in the database
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static BOOL message_notify ( pid_t pid )
{
if ( kill ( pid , SIGUSR1 ) = = - 1 ) {
if ( errno = = ESRCH ) {
DEBUG ( 2 , ( " pid %d doesn't exist - deleting messages record \n " , ( int ) pid ) ) ;
tdb_delete ( tdb , message_key_pid ( pid ) ) ;
} else {
DEBUG ( 2 , ( " message to process %d failed - %s \n " , ( int ) pid , strerror ( errno ) ) ) ;
}
return False ;
}
return True ;
}
/****************************************************************************
send a message to a particular pid
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-11-17 00:38:24 +03:00
BOOL message_send_pid ( pid_t pid , int msg_type , void * buf , size_t len , BOOL duplicates_allowed )
2000-09-11 11:02:43 +04:00
{
TDB_DATA kbuf ;
TDB_DATA dbuf ;
struct message_rec rec ;
void * p ;
2000-11-17 00:38:24 +03:00
/*
* Do an early check for process exists - saves adding into a tdb
* and deleting again if the target is not present . JRA .
*/
2000-11-17 03:02:16 +03:00
if ( ! process_exists ( pid ) ) {
2000-11-17 00:38:24 +03:00
DEBUG ( 2 , ( " message_send_pid: pid %d doesn't exist \n " , ( int ) pid ) ) ;
return False ;
}
2000-09-11 11:02:43 +04:00
rec . msg_version = MESSAGE_VERSION ;
rec . msg_type = msg_type ;
rec . dest = pid ;
rec . src = sys_getpid ( ) ;
rec . len = len ;
kbuf = message_key_pid ( pid ) ;
/* lock the record for the destination */
tdb_lockchain ( tdb , kbuf ) ;
dbuf = tdb_fetch ( tdb , kbuf ) ;
if ( ! dbuf . dptr ) {
/* its a new record */
p = ( void * ) malloc ( len + sizeof ( rec ) ) ;
if ( ! p ) goto failed ;
memcpy ( p , & rec , sizeof ( rec ) ) ;
2000-11-07 02:14:59 +03:00
if ( len > 0 ) memcpy ( ( void * ) ( ( char * ) p + sizeof ( rec ) ) , buf , len ) ;
2000-09-11 11:02:43 +04:00
dbuf . dptr = p ;
dbuf . dsize = len + sizeof ( rec ) ;
tdb_store ( tdb , kbuf , dbuf , TDB_REPLACE ) ;
free ( p ) ;
goto ok ;
}
2000-11-17 00:38:24 +03:00
if ( ! duplicates_allowed ) {
char * ptr ;
struct message_rec * prec ;
for ( ptr = ( char * ) dbuf . dptr , prec = ( struct message_rec * ) ptr ; ptr < dbuf . dptr + dbuf . dsize ;
ptr + = ( sizeof ( rec ) + prec - > len ) , prec = ( struct message_rec * ) ptr ) {
/*
* First check if the message header matches , then , if it ' s a non - zero
* sized message , check if the data matches . If so it ' s a duplicate and
* we can discard it . JRA .
*/
if ( ! memcmp ( ptr , & rec , sizeof ( rec ) ) ) {
if ( ! len | | ( len & & ! memcmp ( ptr + sizeof ( rec ) , ( char * ) buf , len ) ) ) {
DEBUG ( 10 , ( " message_send_pid: discarding duplicate message. \n " ) ) ;
free ( dbuf . dptr ) ;
tdb_unlockchain ( tdb , kbuf ) ;
return True ;
}
}
}
}
2000-09-11 11:02:43 +04:00
/* we're adding to an existing entry */
p = ( void * ) malloc ( dbuf . dsize + len + sizeof ( rec ) ) ;
if ( ! p ) goto failed ;
memcpy ( p , dbuf . dptr , dbuf . dsize ) ;
2000-11-07 02:14:59 +03:00
memcpy ( ( void * ) ( ( char * ) p + dbuf . dsize ) , & rec , sizeof ( rec ) ) ;
if ( len > 0 ) memcpy ( ( void * ) ( ( char * ) p + dbuf . dsize + sizeof ( rec ) ) , buf , len ) ;
2000-09-11 11:02:43 +04:00
2000-09-12 10:57:25 +04:00
free ( dbuf . dptr ) ;
2000-09-11 11:02:43 +04:00
dbuf . dptr = p ;
dbuf . dsize + = len + sizeof ( rec ) ;
tdb_store ( tdb , kbuf , dbuf , TDB_REPLACE ) ;
free ( dbuf . dptr ) ;
ok :
tdb_unlockchain ( tdb , kbuf ) ;
return message_notify ( pid ) ;
failed :
tdb_unlockchain ( tdb , kbuf ) ;
return False ;
}
/****************************************************************************
retrieve the next message for the current process
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-09-12 10:13:25 +04:00
static BOOL message_recv ( int * msg_type , pid_t * src , void * * buf , size_t * len )
2000-09-11 11:02:43 +04:00
{
TDB_DATA kbuf ;
TDB_DATA dbuf ;
struct message_rec rec ;
kbuf = message_key_pid ( sys_getpid ( ) ) ;
tdb_lockchain ( tdb , kbuf ) ;
dbuf = tdb_fetch ( tdb , kbuf ) ;
if ( dbuf . dptr = = NULL | | dbuf . dsize = = 0 ) goto failed ;
memcpy ( & rec , dbuf . dptr , sizeof ( rec ) ) ;
if ( rec . msg_version ! = MESSAGE_VERSION ) {
DEBUG ( 0 , ( " message version %d received (expected %d) \n " , rec . msg_version , MESSAGE_VERSION ) ) ;
goto failed ;
}
2000-09-12 10:13:25 +04:00
if ( rec . len > 0 ) {
( * buf ) = ( void * ) malloc ( rec . len ) ;
if ( ! ( * buf ) ) goto failed ;
memcpy ( * buf , dbuf . dptr + sizeof ( rec ) , rec . len ) ;
} else {
* buf = NULL ;
}
2000-09-11 11:02:43 +04:00
* len = rec . len ;
* msg_type = rec . msg_type ;
* src = rec . src ;
memmove ( dbuf . dptr , dbuf . dptr + sizeof ( rec ) + rec . len , dbuf . dsize - ( sizeof ( rec ) + rec . len ) ) ;
dbuf . dsize - = sizeof ( rec ) + rec . len ;
tdb_store ( tdb , kbuf , dbuf , TDB_REPLACE ) ;
free ( dbuf . dptr ) ;
tdb_unlockchain ( tdb , kbuf ) ;
return True ;
failed :
tdb_unlockchain ( tdb , kbuf ) ;
return False ;
}
/****************************************************************************
receive and dispatch any messages pending for this process
2000-09-12 04:47:11 +04:00
notice that all dispatch handlers for a particular msg_type get called ,
so you can register multiple handlers for a message
2000-09-11 11:02:43 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void message_dispatch ( void )
{
2000-09-12 10:13:25 +04:00
int msg_type ;
2000-09-11 11:02:43 +04:00
pid_t src ;
void * buf ;
size_t len ;
2000-09-12 04:47:11 +04:00
struct dispatch_fns * dfn ;
2000-09-11 11:02:43 +04:00
if ( ! received_signal ) return ;
received_signal = 0 ;
while ( message_recv ( & msg_type , & src , & buf , & len ) ) {
2000-09-12 04:47:11 +04:00
for ( dfn = dispatch_fns ; dfn ; dfn = dfn - > next ) {
if ( dfn - > msg_type = = msg_type ) {
dfn - > fn ( msg_type , src , buf , len ) ;
}
2000-09-11 11:02:43 +04:00
}
2000-09-12 10:57:25 +04:00
if ( buf ) free ( buf ) ;
2000-09-11 11:02:43 +04:00
}
}
2000-09-12 04:47:11 +04:00
/****************************************************************************
register a dispatch function for a particular message type
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-09-12 10:13:25 +04:00
void message_register ( int msg_type ,
void ( * fn ) ( int msg_type , pid_t pid , void * buf , size_t len ) )
2000-09-12 04:47:11 +04:00
{
struct dispatch_fns * dfn ;
dfn = ( struct dispatch_fns * ) malloc ( sizeof ( * dfn ) ) ;
ZERO_STRUCTP ( dfn ) ;
dfn - > msg_type = msg_type ;
dfn - > fn = fn ;
DLIST_ADD ( dispatch_fns , dfn ) ;
}
2000-09-12 10:57:25 +04:00
/****************************************************************************
de - register the function for a particular message type
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void message_deregister ( int msg_type )
{
struct dispatch_fns * dfn , * next ;
for ( dfn = dispatch_fns ; dfn ; dfn = next ) {
next = dfn - > next ;
if ( dfn - > msg_type = = msg_type ) {
DLIST_REMOVE ( dispatch_fns , dfn ) ;
free ( dfn ) ;
}
}
}
2000-09-13 11:07:17 +04:00
static struct {
int msg_type ;
void * buf ;
size_t len ;
2000-11-17 00:38:24 +03:00
BOOL duplicates ;
2000-09-13 11:07:17 +04:00
} msg_all ;
/****************************************************************************
send one of the messages for the broadcast
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-11-04 21:44:34 +03:00
static int traverse_fn ( TDB_CONTEXT * the_tdb , TDB_DATA kbuf , TDB_DATA dbuf , void * state )
2000-09-13 11:07:17 +04:00
{
struct connections_data crec ;
memcpy ( & crec , dbuf . dptr , sizeof ( crec ) ) ;
2000-11-11 03:33:33 +03:00
if ( crec . cnum = = - 1 ) return 0 ;
2000-11-17 00:38:24 +03:00
message_send_pid ( crec . pid , msg_all . msg_type , msg_all . buf , msg_all . len , msg_all . duplicates ) ;
2000-09-13 11:07:17 +04:00
return 0 ;
}
/****************************************************************************
this is a useful function for sending messages to all smbd processes .
It isn ' t very efficient , but should be OK for the sorts of applications that
use it . When we need efficient broadcast we can add it .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-11-17 00:38:24 +03:00
BOOL message_send_all ( int msg_type , void * buf , size_t len , BOOL duplicates_allowed )
2000-09-13 11:07:17 +04:00
{
2000-11-04 21:44:34 +03:00
TDB_CONTEXT * the_tdb ;
2000-09-13 11:07:17 +04:00
2000-11-04 21:44:34 +03:00
the_tdb = tdb_open ( lock_path ( " connections.tdb " ) , 0 , 0 , O_RDONLY , 0 ) ;
if ( ! the_tdb ) {
2000-09-13 11:07:17 +04:00
DEBUG ( 2 , ( " Failed to open connections database in message_send_all \n " ) ) ;
return False ;
}
msg_all . msg_type = msg_type ;
msg_all . buf = buf ;
msg_all . len = len ;
2000-11-17 00:38:24 +03:00
msg_all . duplicates = duplicates_allowed ;
2000-09-13 11:07:17 +04:00
2000-11-04 21:44:34 +03:00
tdb_traverse ( the_tdb , traverse_fn , NULL ) ;
tdb_close ( the_tdb ) ;
2000-09-13 11:07:17 +04:00
return True ;
}