2000-09-11 11:02:43 +04:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
2000-09-11 11:02:43 +04:00
Samba internal messaging functions
Copyright ( C ) Andrew Tridgell 2000
2001-12-21 03:37:49 +03:00
Copyright ( C ) 2001 by Martin Pool
2003-01-10 23:17:02 +03:00
Copyright ( C ) 2002 by Jeremy Allison
2000-09-11 11:02:43 +04:00
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 .
*/
2001-12-19 10:49:25 +03:00
/**
2003-03-18 01:42:56 +03:00
@ defgroup messages Internal messaging framework
@ {
@ file messages . c
@ brief Module for internal messaging between Samba daemons .
2000-09-12 04:47:11 +04:00
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 .
2001-12-19 10:49:25 +03:00
The dispatch function is given the pid of the sender , and it can
use that to reply by message_send_pid ( ) . See ping_message ( ) for a
simple example .
2003-03-18 01:42:56 +03:00
@ caution Dispatch functions must be able to cope with incoming
2003-01-10 23:17:02 +03:00
messages on an * odd * byte boundary .
2000-09-12 04:47:11 +04:00
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
/****************************************************************************
2001-11-21 01:55:46 +03:00
Notifications come in as signals .
2000-09-11 11:02:43 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-11-21 01:55:46 +03:00
2000-09-11 11:02:43 +04:00
static void sig_usr1 ( void )
{
received_signal = 1 ;
2005-06-09 19:20:11 +04:00
sys_select_signal ( SIGUSR1 ) ;
2000-09-11 11:02:43 +04:00
}
2000-09-12 10:57:25 +04:00
/****************************************************************************
2001-11-21 01:55:46 +03:00
A useful function for testing the message system .
2000-09-12 10:57:25 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-11-21 01:55:46 +03:00
2002-07-15 14:35:28 +04:00
static void ping_message ( int msg_type , pid_t src , void * buf , size_t len )
2000-09-12 10:57:25 +04:00
{
2003-01-03 11:28:12 +03:00
const char * msg = buf ? buf : " none " ;
2001-11-21 01:55:46 +03:00
DEBUG ( 1 , ( " INFO: Received PING message from PID %u [%s] \n " , ( unsigned int ) src , msg ) ) ;
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
}
2001-11-21 01:55:46 +03:00
2000-09-11 11:02:43 +04:00
/****************************************************************************
Initialise the messaging functions .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-11-21 01:55:46 +03:00
2000-09-11 11:02:43 +04:00
BOOL message_init ( void )
{
if ( tdb ) return True ;
2001-06-04 09:13:59 +04:00
tdb = tdb_open_log ( lock_path ( " messages.tdb " ) ,
2001-11-21 01:55:46 +03:00
0 , TDB_CLEAR_IF_FIRST | TDB_DEFAULT ,
2000-09-11 11:02:43 +04:00
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 ) ;
2003-04-14 06:19:10 +04:00
/* Register some debugging related messages */
register_msg_pool_usage ( ) ;
register_dmalloc_msgs ( ) ;
2000-09-11 11:02:43 +04:00
return True ;
}
/*******************************************************************
2001-11-21 01:55:46 +03:00
Form a static tdb key from a pid .
2000-09-11 11:02:43 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-11-21 01:55:46 +03:00
2000-09-11 11:02:43 +04:00
static TDB_DATA message_key_pid ( pid_t pid )
{
static char key [ 20 ] ;
TDB_DATA kbuf ;
2001-04-09 00:22:39 +04:00
slprintf ( key , sizeof ( key ) - 1 , " PID/%d " , ( int ) pid ) ;
2000-12-02 03:51:50 +03:00
2000-09-11 11:02:43 +04:00
kbuf . dptr = ( char * ) key ;
2000-12-02 03:51:50 +03:00
kbuf . dsize = strlen ( key ) + 1 ;
2000-09-11 11:02:43 +04:00
return kbuf ;
}
/****************************************************************************
2001-11-21 01:55:46 +03:00
Notify a process that it has a message . If the process doesn ' t exist
then delete its record in the database .
2000-09-11 11:02:43 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-11-21 01:55:46 +03:00
2000-09-11 11:02:43 +04:00
static BOOL message_notify ( pid_t pid )
{
2003-01-10 23:17:02 +03:00
/*
* Doing kill with a non - positive pid causes messages to be
* sent to places we don ' t want .
*/
2002-03-20 09:57:03 +03:00
SMB_ASSERT ( pid > 0 ) ;
2003-01-10 23:17:02 +03:00
2000-09-11 11:02:43 +04:00
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 ;
}
/****************************************************************************
2001-11-21 01:55:46 +03:00
Send a message to a particular pid .
2000-09-11 11:02:43 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-11-21 01:55:46 +03:00
2003-01-31 02:55:58 +03:00
static BOOL message_send_pid_internal ( pid_t pid , int msg_type , const void * buf , size_t len ,
BOOL duplicates_allowed , unsigned int timeout )
2000-09-11 11:02:43 +04:00
{
TDB_DATA kbuf ;
TDB_DATA dbuf ;
2003-01-11 03:17:41 +03:00
TDB_DATA old_dbuf ;
2000-09-11 11:02:43 +04:00
struct message_rec rec ;
2003-01-11 03:17:41 +03:00
char * ptr ;
struct message_rec prec ;
2000-09-11 11:02:43 +04:00
2003-01-10 23:17:02 +03:00
/*
* Doing kill with a non - positive pid causes messages to be
* sent to places we don ' t want .
*/
SMB_ASSERT ( pid > 0 ) ;
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 ) ;
2004-12-07 21:25:53 +03:00
dbuf . dptr = ( void * ) SMB_MALLOC ( len + sizeof ( rec ) ) ;
2003-01-11 03:17:41 +03:00
if ( ! dbuf . dptr )
return False ;
memcpy ( dbuf . dptr , & rec , sizeof ( rec ) ) ;
if ( len > 0 )
memcpy ( ( void * ) ( ( char * ) dbuf . dptr + sizeof ( rec ) ) , buf , len ) ;
dbuf . dsize = len + sizeof ( rec ) ;
if ( duplicates_allowed ) {
/* If duplicates are allowed we can just append the message and return. */
/* lock the record for the destination */
2003-01-31 02:55:58 +03:00
if ( timeout ) {
if ( tdb_chainlock_with_timeout ( tdb , kbuf , timeout ) = = - 1 ) {
DEBUG ( 0 , ( " message_send_pid_internal: failed to get chainlock with timeout %ul. \n " , timeout ) ) ;
return False ;
}
} else {
if ( tdb_chainlock ( tdb , kbuf ) = = - 1 ) {
DEBUG ( 0 , ( " message_send_pid_internal: failed to get chainlock. \n " ) ) ;
return False ;
}
}
2003-01-11 03:17:41 +03:00
tdb_append ( tdb , kbuf , dbuf ) ;
tdb_chainunlock ( tdb , kbuf ) ;
SAFE_FREE ( dbuf . dptr ) ;
errno = 0 ; /* paranoia */
return message_notify ( pid ) ;
}
2000-09-11 11:02:43 +04:00
/* lock the record for the destination */
2003-01-31 02:55:58 +03:00
if ( timeout ) {
if ( tdb_chainlock_with_timeout ( tdb , kbuf , timeout ) = = - 1 ) {
DEBUG ( 0 , ( " message_send_pid_internal: failed to get chainlock with timeout %ul. \n " , timeout ) ) ;
return False ;
}
} else {
if ( tdb_chainlock ( tdb , kbuf ) = = - 1 ) {
DEBUG ( 0 , ( " message_send_pid_internal: failed to get chainlock. \n " ) ) ;
return False ;
}
}
2003-01-11 03:17:41 +03:00
old_dbuf = tdb_fetch ( tdb , kbuf ) ;
2000-09-11 11:02:43 +04:00
2003-01-11 03:17:41 +03:00
if ( ! old_dbuf . dptr ) {
2000-09-11 11:02:43 +04:00
/* its a new record */
tdb_store ( tdb , kbuf , dbuf , TDB_REPLACE ) ;
2003-01-11 03:17:41 +03:00
tdb_chainunlock ( tdb , kbuf ) ;
SAFE_FREE ( dbuf . dptr ) ;
errno = 0 ; /* paranoia */
return message_notify ( pid ) ;
2000-09-11 11:02:43 +04:00
}
2003-01-11 03:17:41 +03:00
/* Not a new record. Check for duplicates. */
for ( ptr = ( char * ) old_dbuf . dptr ; ptr < old_dbuf . dptr + old_dbuf . dsize ; ) {
/*
* 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 ) , buf , len ) ) ) {
tdb_chainunlock ( tdb , kbuf ) ;
2003-01-31 02:55:58 +03:00
DEBUG ( 10 , ( " message_send_pid_internal: discarding duplicate message. \n " ) ) ;
2003-01-11 03:17:41 +03:00
SAFE_FREE ( dbuf . dptr ) ;
SAFE_FREE ( old_dbuf . dptr ) ;
return True ;
2000-11-17 00:38:24 +03:00
}
}
2003-01-11 03:17:41 +03:00
memcpy ( & prec , ptr , sizeof ( prec ) ) ;
ptr + = sizeof ( rec ) + prec . len ;
2000-11-17 00:38:24 +03:00
}
2000-09-11 11:02:43 +04:00
/* we're adding to an existing entry */
2003-01-11 03:17:41 +03:00
tdb_append ( tdb , kbuf , dbuf ) ;
tdb_chainunlock ( tdb , kbuf ) ;
2000-09-11 11:02:43 +04:00
2003-01-11 03:17:41 +03:00
SAFE_FREE ( old_dbuf . dptr ) ;
2001-09-17 06:19:44 +04:00
SAFE_FREE ( dbuf . dptr ) ;
2000-09-11 11:02:43 +04:00
2001-02-03 20:19:10 +03:00
errno = 0 ; /* paranoia */
2000-09-11 11:02:43 +04:00
return message_notify ( pid ) ;
}
2003-01-31 02:55:58 +03:00
/****************************************************************************
Send a message to a particular pid - no timeout .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
BOOL message_send_pid ( pid_t pid , int msg_type , const void * buf , size_t len , BOOL duplicates_allowed )
{
return message_send_pid_internal ( pid , msg_type , buf , len , duplicates_allowed , 0 ) ;
}
/****************************************************************************
Send a message to a particular pid , with timeout in seconds .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
BOOL message_send_pid_with_timeout ( pid_t pid , int msg_type , const void * buf , size_t len ,
BOOL duplicates_allowed , unsigned int timeout )
{
return message_send_pid_internal ( pid , msg_type , buf , len , duplicates_allowed , timeout ) ;
}
2003-04-23 04:19:30 +04:00
/****************************************************************************
Count the messages pending for a particular pid . Expensive . . . .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
unsigned int messages_pending_for_pid ( pid_t pid )
{
TDB_DATA kbuf ;
TDB_DATA dbuf ;
char * buf ;
unsigned int message_count = 0 ;
2005-08-25 21:46:13 +04:00
kbuf = message_key_pid ( pid ) ;
2003-04-23 04:19:30 +04:00
dbuf = tdb_fetch ( tdb , kbuf ) ;
if ( dbuf . dptr = = NULL | | dbuf . dsize = = 0 ) {
SAFE_FREE ( dbuf . dptr ) ;
return 0 ;
}
for ( buf = dbuf . dptr ; dbuf . dsize > sizeof ( struct message_rec ) ; ) {
struct message_rec rec ;
memcpy ( & rec , buf , sizeof ( rec ) ) ;
buf + = ( sizeof ( rec ) + rec . len ) ;
dbuf . dsize - = ( sizeof ( rec ) + rec . len ) ;
message_count + + ;
}
SAFE_FREE ( dbuf . dptr ) ;
return message_count ;
}
2000-09-11 11:02:43 +04:00
/****************************************************************************
2003-01-10 23:17:02 +03:00
Retrieve all messages for the current process .
2000-09-11 11:02:43 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-11-21 01:55:46 +03:00
2003-01-10 23:17:02 +03:00
static BOOL retrieve_all_messages ( char * * msgs_buf , size_t * total_len )
2000-09-11 11:02:43 +04:00
{
TDB_DATA kbuf ;
TDB_DATA dbuf ;
2003-01-10 23:17:02 +03:00
TDB_DATA null_dbuf ;
ZERO_STRUCT ( null_dbuf ) ;
* msgs_buf = NULL ;
* total_len = 0 ;
2000-09-11 11:02:43 +04:00
kbuf = message_key_pid ( sys_getpid ( ) ) ;
2003-03-20 03:52:37 +03:00
if ( tdb_chainlock ( tdb , kbuf ) = = - 1 )
return False ;
2000-09-11 11:02:43 +04:00
dbuf = tdb_fetch ( tdb , kbuf ) ;
2003-01-10 23:17:02 +03:00
/*
* Replace with an empty record to keep the allocated
* space in the tdb .
*/
tdb_store ( tdb , kbuf , null_dbuf , TDB_REPLACE ) ;
tdb_chainunlock ( tdb , kbuf ) ;
2000-09-11 11:02:43 +04:00
2003-01-10 23:17:02 +03:00
if ( dbuf . dptr = = NULL | | dbuf . dsize = = 0 ) {
SAFE_FREE ( dbuf . dptr ) ;
return False ;
}
* msgs_buf = dbuf . dptr ;
* total_len = dbuf . dsize ;
return True ;
}
/****************************************************************************
Parse out the next message for the current process .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static BOOL message_recv ( char * msgs_buf , size_t total_len , int * msg_type , pid_t * src , char * * buf , size_t * len )
{
struct message_rec rec ;
char * ret_buf = * buf ;
* buf = NULL ;
* len = 0 ;
if ( total_len - ( ret_buf - msgs_buf ) < sizeof ( rec ) )
return False ;
memcpy ( & rec , ret_buf , sizeof ( rec ) ) ;
ret_buf + = sizeof ( rec ) ;
2000-09-11 11:02:43 +04:00
if ( rec . msg_version ! = MESSAGE_VERSION ) {
DEBUG ( 0 , ( " message version %d received (expected %d) \n " , rec . msg_version , MESSAGE_VERSION ) ) ;
2003-01-10 23:17:02 +03:00
return False ;
2000-09-11 11:02:43 +04:00
}
2000-09-12 10:13:25 +04:00
if ( rec . len > 0 ) {
2003-01-10 23:17:02 +03:00
if ( total_len - ( ret_buf - msgs_buf ) < rec . len )
return False ;
2000-09-12 10:13:25 +04:00
}
2000-09-11 11:02:43 +04:00
* len = rec . len ;
* msg_type = rec . msg_type ;
* src = rec . src ;
2003-01-10 23:17:02 +03:00
* buf = ret_buf ;
2000-09-11 11:02:43 +04:00
return True ;
}
/****************************************************************************
2001-11-21 01:55:46 +03:00
Receive and dispatch any messages pending for this process .
Notice that all dispatch handlers for a particular msg_type get called ,
so you can register multiple handlers for a message .
2003-01-10 23:17:02 +03:00
* NOTE * : Dispatch functions must be able to cope with incoming
messages on an * odd * byte boundary .
2000-09-11 11:02:43 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-11-21 01:55:46 +03:00
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 ;
2003-01-10 23:17:02 +03:00
char * buf ;
char * msgs_buf ;
size_t len , total_len ;
2000-09-12 04:47:11 +04:00
struct dispatch_fns * dfn ;
2002-01-03 06:42:33 +03:00
int n_handled ;
2000-09-11 11:02:43 +04:00
2003-01-10 23:17:02 +03:00
if ( ! received_signal )
return ;
2001-09-25 04:50:37 +04:00
DEBUG ( 10 , ( " message_dispatch: received_signal = %d \n " , received_signal ) ) ;
2001-11-21 01:55:46 +03:00
2000-09-11 11:02:43 +04:00
received_signal = 0 ;
2003-01-10 23:17:02 +03:00
if ( ! retrieve_all_messages ( & msgs_buf , & total_len ) )
return ;
for ( buf = msgs_buf ; message_recv ( msgs_buf , total_len , & msg_type , & src , & buf , & len ) ; buf + = len ) {
DEBUG ( 10 , ( " message_dispatch: received msg_type=%d src_pid=%u \n " ,
msg_type , ( unsigned int ) src ) ) ;
2002-01-03 06:42:33 +03:00
n_handled = 0 ;
2000-09-12 04:47:11 +04:00
for ( dfn = dispatch_fns ; dfn ; dfn = dfn - > next ) {
if ( dfn - > msg_type = = msg_type ) {
2001-09-25 04:50:37 +04:00
DEBUG ( 10 , ( " message_dispatch: processing message of type %d. \n " , msg_type ) ) ;
2003-01-10 23:17:02 +03:00
dfn - > fn ( msg_type , src , len ? ( void * ) buf : NULL , len ) ;
2002-01-03 06:42:33 +03:00
n_handled + + ;
2000-09-12 04:47:11 +04:00
}
2000-09-11 11:02:43 +04:00
}
2002-01-03 06:42:33 +03:00
if ( ! n_handled ) {
2003-01-10 23:17:02 +03:00
DEBUG ( 5 , ( " message_dispatch: warning: no handlers registed for "
" msg_type %d in pid %u \n " ,
msg_type , ( unsigned int ) sys_getpid ( ) ) ) ;
2002-01-03 06:42:33 +03:00
}
2000-09-11 11:02:43 +04:00
}
2003-01-10 23:17:02 +03:00
SAFE_FREE ( msgs_buf ) ;
2000-09-11 11:02:43 +04:00
}
2000-09-12 04:47:11 +04:00
/****************************************************************************
2001-11-21 01:55:46 +03:00
Register a dispatch function for a particular message type .
2003-01-10 23:17:02 +03:00
* NOTE * : Dispatch functions must be able to cope with incoming
messages on an * odd * byte boundary .
2000-09-12 04:47:11 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-11-21 01:55:46 +03:00
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 ;
2004-12-07 21:25:53 +03:00
dfn = SMB_MALLOC_P ( struct dispatch_fns ) ;
2000-09-12 04:47:11 +04:00
2001-08-05 14:10:16 +04:00
if ( dfn ! = NULL ) {
2000-09-12 04:47:11 +04:00
2001-08-05 14:10:16 +04:00
ZERO_STRUCTPN ( dfn ) ;
2000-09-12 04:47:11 +04:00
2001-08-05 14:10:16 +04:00
dfn - > msg_type = msg_type ;
dfn - > fn = fn ;
DLIST_ADD ( dispatch_fns , dfn ) ;
}
else {
DEBUG ( 0 , ( " message_register: Not enough memory. malloc failed! \n " ) ) ;
}
2000-09-12 04:47:11 +04:00
}
2000-09-12 10:57:25 +04:00
/****************************************************************************
2001-11-21 01:55:46 +03:00
De - register the function for a particular message type .
2000-09-12 10:57:25 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-11-21 01:55:46 +03:00
2000-09-12 10:57:25 +04:00
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 ) ;
2001-09-17 06:19:44 +04:00
SAFE_FREE ( dfn ) ;
2000-09-12 10:57:25 +04:00
}
}
}
2000-09-13 11:07:17 +04:00
2001-01-23 23:25:25 +03:00
struct msg_all {
2000-09-13 11:07:17 +04:00
int msg_type ;
2002-09-25 19:19:00 +04:00
uint32 msg_flag ;
2001-12-21 03:37:49 +03:00
const void * buf ;
2000-09-13 11:07:17 +04:00
size_t len ;
2000-11-17 00:38:24 +03:00
BOOL duplicates ;
2002-09-25 19:19:00 +04:00
int n_sent ;
2001-01-23 23:25:25 +03:00
} ;
2000-09-13 11:07:17 +04:00
/****************************************************************************
2001-11-21 01:55:46 +03:00
Send one of the messages for the broadcast .
2000-09-13 11:07:17 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-11-21 01:55:46 +03:00
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 ;
2001-01-23 23:25:25 +03:00
struct msg_all * msg_all = ( struct msg_all * ) state ;
2000-09-13 11:07:17 +04:00
2001-05-15 22:12:02 +04:00
if ( dbuf . dsize ! = sizeof ( crec ) )
return 0 ;
2000-09-13 11:07:17 +04:00
memcpy ( & crec , dbuf . dptr , sizeof ( crec ) ) ;
2001-05-15 22:12:02 +04:00
if ( crec . cnum ! = - 1 )
return 0 ;
2001-02-03 20:19:10 +03:00
2002-09-25 19:19:00 +04:00
/* Don't send if the receiver hasn't registered an interest. */
if ( ! ( crec . bcast_msg_flags & msg_all - > msg_flag ) )
return 0 ;
/* If the msg send fails because the pid was not found (i.e. smbd died),
2001-02-03 20:19:10 +03:00
* the msg has already been deleted from the messages . tdb . */
2002-09-25 19:19:00 +04:00
2001-12-21 03:37:49 +03:00
if ( ! message_send_pid ( crec . pid , msg_all - > msg_type ,
msg_all - > buf , msg_all - > len ,
msg_all - > duplicates ) ) {
2001-02-03 20:19:10 +03:00
2002-09-25 19:19:00 +04:00
/* If the pid was not found delete the entry from connections.tdb */
2001-02-03 20:19:10 +03:00
if ( errno = = ESRCH ) {
2001-04-28 02:17:10 +04:00
DEBUG ( 2 , ( " pid %u doesn't exist - deleting connections %d [%s] \n " ,
( unsigned int ) crec . pid , crec . cnum , crec . name ) ) ;
2001-02-03 20:19:10 +03:00
tdb_delete ( the_tdb , kbuf ) ;
}
}
2001-12-21 03:37:49 +03:00
msg_all - > n_sent + + ;
2000-09-13 11:07:17 +04:00
return 0 ;
}
2001-12-21 03:37:49 +03:00
/**
* Send a message 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 .
*
* @ param n_sent Set to the number of messages sent . This should be
* equal to the number of processes , but be careful for races .
*
2003-03-18 01:42:56 +03:00
* @ retval True for success .
2001-12-21 03:37:49 +03:00
* */
BOOL message_send_all ( TDB_CONTEXT * conn_tdb , int msg_type ,
const void * buf , size_t len ,
BOOL duplicates_allowed ,
int * n_sent )
2000-09-13 11:07:17 +04:00
{
2001-01-23 23:25:25 +03:00
struct msg_all msg_all ;
2000-09-13 11:07:17 +04:00
msg_all . msg_type = msg_type ;
2002-09-25 19:19:00 +04:00
if ( msg_type < 1000 )
msg_all . msg_flag = FLAG_MSG_GENERAL ;
else if ( msg_type > 1000 & & msg_type < 2000 )
msg_all . msg_flag = FLAG_MSG_NMBD ;
2004-08-18 17:55:58 +04:00
else if ( msg_type > 2000 & & msg_type < 2100 )
msg_all . msg_flag = FLAG_MSG_PRINT_NOTIFY ;
else if ( msg_type > 2100 & & msg_type < 3000 )
msg_all . msg_flag = FLAG_MSG_PRINT_GENERAL ;
2002-09-25 19:19:00 +04:00
else if ( msg_type > 3000 & & msg_type < 4000 )
msg_all . msg_flag = FLAG_MSG_SMBD ;
else
return False ;
2000-09-13 11:07:17 +04:00
msg_all . buf = buf ;
msg_all . len = len ;
2000-11-17 00:38:24 +03:00
msg_all . duplicates = duplicates_allowed ;
2001-12-21 03:37:49 +03:00
msg_all . n_sent = 0 ;
2000-09-13 11:07:17 +04:00
2001-01-23 23:25:25 +03:00
tdb_traverse ( conn_tdb , traverse_fn , & msg_all ) ;
2001-12-21 03:37:49 +03:00
if ( n_sent )
* n_sent = msg_all . n_sent ;
2000-09-13 11:07:17 +04:00
return True ;
}
2002-03-09 12:48:35 +03:00
/** @} **/