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
2007-05-21 00:11:23 +04:00
Copyright ( C ) 2007 by Volker Lendecke
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"
2007-05-22 02:17:13 +04:00
# include "librpc/gen_ndr/messaging.h"
# include "librpc/gen_ndr/ndr_messaging.h"
2000-09-11 11:02:43 +04:00
/* the locking database handle */
static int received_signal ;
/* change the message version with any incompatible changes in the protocol */
2007-05-22 02:17:13 +04:00
# define MESSAGE_VERSION 2
2000-09-11 11:02:43 +04:00
2007-05-22 02:17:13 +04:00
struct messaging_callback {
struct messaging_callback * prev , * next ;
uint32 msg_type ;
void ( * fn ) ( struct messaging_context * msg , void * private_data ,
uint32_t msg_type ,
struct server_id server_id , DATA_BLOB * data ) ;
r21064: The core of this patch is
void message_register(int msg_type,
void (*fn)(int msg_type, struct process_id pid,
- void *buf, size_t len))
+ void *buf, size_t len,
+ void *private_data),
+ void *private_data)
{
struct dispatch_fns *dfn;
So this adds a (so far unused) private pointer that is passed from
message_register to the message handler. A prerequisite to implement a tiny
samba4-API compatible wrapper around our messaging system. That itself is
necessary for the Samba4 notify system.
Yes, I know, I could import the whole Samba4 messaging system, but I want to
do it step by step and I think getting notify in is more important in this
step.
Volker
(This used to be commit c8ae60ed65dcce9660ee39c75488f2838cf9a28b)
2007-01-31 01:22:06 +03:00
void * private_data ;
2007-05-22 02:17:13 +04:00
} ;
2006-04-08 21:25:31 +04:00
2007-05-22 02:17:13 +04:00
struct messaging_context {
TDB_CONTEXT * tdb ;
struct server_id id ;
struct event_context * event_ctx ;
struct messaging_callback * callbacks ;
2007-05-24 15:09:37 +04:00
2007-05-22 02:17:13 +04:00
} ;
2006-04-08 21:25:31 +04:00
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
2007-05-22 02:17:13 +04:00
static void ping_message ( struct messaging_context * msg_ctx ,
void * private_data ,
uint32_t msg_type ,
struct server_id src ,
DATA_BLOB * data )
2000-09-12 10:57:25 +04:00
{
2007-05-22 02:17:13 +04:00
const char * msg = data - > data ? ( const char * ) data - > data : " none " ;
2006-06-20 05:20:38 +04:00
2005-09-30 21:13:37 +04:00
DEBUG ( 1 , ( " INFO: Received PING message from PID %s [%s] \n " ,
procid_str_static ( & src ) , msg ) ) ;
2007-05-22 02:17:13 +04:00
messaging_send ( msg_ctx , src , MSG_PONG , data ) ;
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
2007-05-24 15:09:37 +04:00
static BOOL message_tdb_init ( struct messaging_context * msg_ctx )
2000-09-11 11:02:43 +04:00
{
2006-07-29 02:42:39 +04:00
sec_init ( ) ;
2007-05-22 02:17:13 +04:00
msg_ctx - > tdb = tdb_open_log ( lock_path ( " messages.tdb " ) ,
0 , TDB_CLEAR_IF_FIRST | TDB_DEFAULT ,
O_RDWR | O_CREAT , 0600 ) ;
2000-09-11 11:02:43 +04:00
2007-05-22 02:17:13 +04:00
if ( ! msg_ctx - > tdb ) {
2000-09-11 11:02:43 +04:00
DEBUG ( 0 , ( " ERROR: Failed to initialise messages database \n " ) ) ;
return False ;
}
2007-03-06 13:40:18 +03:00
/* Activate the per-hashchain freelist */
2007-05-22 02:17:13 +04:00
tdb_set_max_dead ( msg_ctx - > tdb , 5 ) ;
2007-03-06 13:40:18 +03:00
2000-10-04 05:03:23 +04:00
CatchSignal ( SIGUSR1 , SIGNAL_CAST sig_usr1 ) ;
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
2007-05-07 13:35:35 +04:00
static TDB_DATA message_key_pid ( struct server_id pid )
2000-09-11 11:02:43 +04:00
{
static char key [ 20 ] ;
TDB_DATA kbuf ;
2005-09-30 21:13:37 +04:00
slprintf ( key , sizeof ( key ) - 1 , " PID/%s " , procid_str_static ( & pid ) ) ;
2000-12-02 03:51:50 +03:00
2007-03-29 13:35:51 +04:00
kbuf . dptr = ( uint8 * ) key ;
2000-12-02 03:51:50 +03:00
kbuf . dsize = strlen ( key ) + 1 ;
2000-09-11 11:02:43 +04:00
return kbuf ;
}
2007-05-22 02:17:13 +04:00
/*
Fetch the messaging array for a process
*/
static NTSTATUS messaging_tdb_fetch ( TDB_CONTEXT * msg_tdb ,
TDB_DATA key ,
TALLOC_CTX * mem_ctx ,
struct messaging_array * * presult )
{
struct messaging_array * result ;
TDB_DATA data ;
DATA_BLOB blob ;
NTSTATUS status ;
if ( ! ( result = TALLOC_ZERO_P ( mem_ctx , struct messaging_array ) ) ) {
return NT_STATUS_NO_MEMORY ;
}
data = tdb_fetch ( msg_tdb , key ) ;
if ( data . dptr = = NULL ) {
* presult = result ;
return NT_STATUS_OK ;
}
blob = data_blob_const ( data . dptr , data . dsize ) ;
status = ndr_pull_struct_blob (
& blob , result , result ,
( ndr_pull_flags_fn_t ) ndr_pull_messaging_array ) ;
SAFE_FREE ( data . dptr ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
TALLOC_FREE ( result ) ;
return status ;
}
if ( DEBUGLEVEL > = 10 ) {
DEBUG ( 10 , ( " messaging_tdb_fetch: \n " ) ) ;
NDR_PRINT_DEBUG ( messaging_array , result ) ;
}
* presult = result ;
return NT_STATUS_OK ;
}
/*
Store a messaging array for a pid
*/
static NTSTATUS messaging_tdb_store ( TDB_CONTEXT * msg_tdb ,
TDB_DATA key ,
struct messaging_array * array )
{
TDB_DATA data ;
DATA_BLOB blob ;
NTSTATUS status ;
TALLOC_CTX * mem_ctx ;
int ret ;
if ( array - > num_messages = = 0 ) {
tdb_delete ( msg_tdb , key ) ;
return NT_STATUS_OK ;
}
if ( ! ( mem_ctx = talloc_new ( array ) ) ) {
return NT_STATUS_NO_MEMORY ;
}
status = ndr_push_struct_blob (
& blob , mem_ctx , array ,
( ndr_push_flags_fn_t ) ndr_push_messaging_array ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
talloc_free ( mem_ctx ) ;
return status ;
}
if ( DEBUGLEVEL > = 10 ) {
DEBUG ( 10 , ( " messaging_tdb_store: \n " ) ) ;
NDR_PRINT_DEBUG ( messaging_array , array ) ;
}
data . dptr = blob . data ;
data . dsize = blob . length ;
ret = tdb_store ( msg_tdb , key , data , TDB_REPLACE ) ;
TALLOC_FREE ( mem_ctx ) ;
return ( ret = = 0 ) ? NT_STATUS_OK : NT_STATUS_INTERNAL_DB_CORRUPTION ;
}
2000-09-11 11:02:43 +04:00
/****************************************************************************
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
2007-05-07 13:35:35 +04:00
static NTSTATUS message_notify ( struct server_id procid )
2000-09-11 11:02:43 +04:00
{
2005-09-30 21:13:37 +04:00
pid_t pid = procid . pid ;
2006-07-29 02:42:39 +04:00
int ret ;
uid_t euid = geteuid ( ) ;
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
2006-07-29 02:42:39 +04:00
if ( euid ! = 0 ) {
2007-04-06 03:56:10 +04:00
/* If we're not root become so to send the message. */
save_re_uid ( ) ;
set_effective_uid ( 0 ) ;
2006-07-29 02:42:39 +04:00
}
ret = kill ( pid , SIGUSR1 ) ;
if ( euid ! = 0 ) {
2007-04-06 03:56:10 +04:00
/* Go back to who we were. */
int saved_errno = errno ;
restore_re_uid_fromroot ( ) ;
errno = saved_errno ;
2006-07-29 02:42:39 +04:00
}
2007-05-21 00:28:45 +04:00
if ( ret = = 0 ) {
return NT_STATUS_OK ;
}
2007-02-01 22:29:07 +03:00
2007-05-21 00:28:45 +04:00
/*
* Something has gone wrong
*/
2007-02-01 22:29:07 +03:00
2007-05-21 00:28:45 +04:00
DEBUG ( 2 , ( " message to process %d failed - %s \n " , ( int ) pid ,
strerror ( errno ) ) ) ;
/*
* No call to map_nt_error_from_unix - - don ' t want to link in
* errormap . o into lots of utils .
*/
2007-05-22 02:17:13 +04:00
if ( errno = = ESRCH ) return NT_STATUS_INVALID_HANDLE ;
2007-05-21 00:28:45 +04:00
if ( errno = = EINVAL ) return NT_STATUS_INVALID_PARAMETER ;
if ( errno = = EPERM ) return NT_STATUS_ACCESS_DENIED ;
return NT_STATUS_UNSUCCESSFUL ;
2000-09-11 11:02:43 +04:00
}
/****************************************************************************
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
2007-05-24 15:09:37 +04:00
static NTSTATUS messaging_tdb_send ( struct messaging_context * msg_ctx ,
2007-05-22 02:17:13 +04:00
struct server_id pid , int msg_type ,
2007-05-24 15:09:37 +04:00
const DATA_BLOB * data )
2000-09-11 11:02:43 +04:00
{
2007-05-22 02:17:13 +04:00
struct messaging_array * msg_array ;
struct messaging_rec * rec ;
TALLOC_CTX * mem_ctx ;
NTSTATUS status ;
TDB_DATA key = message_key_pid ( pid ) ;
2000-09-11 11:02:43 +04:00
2006-06-20 05:20:38 +04:00
/* NULL pointer means implicit length zero. */
2007-05-24 15:09:37 +04:00
if ( ! data - > data ) {
SMB_ASSERT ( data - > length = = 0 ) ;
2006-06-20 05:20:38 +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 .
*/
2005-09-30 21:13:37 +04:00
SMB_ASSERT ( procid_to_pid ( & pid ) > 0 ) ;
2003-01-10 23:17:02 +03:00
2007-05-22 02:17:13 +04:00
if ( ! ( mem_ctx = talloc_init ( " message_send_pid " ) ) ) {
return NT_STATUS_NO_MEMORY ;
}
2007-05-24 15:09:37 +04:00
if ( tdb_chainlock ( msg_ctx - > tdb , key ) = = - 1 ) {
TALLOC_FREE ( mem_ctx ) ;
2007-05-22 02:17:13 +04:00
return NT_STATUS_LOCK_NOT_GRANTED ;
}
2007-05-24 15:09:37 +04:00
status = messaging_tdb_fetch ( msg_ctx - > tdb , key , mem_ctx , & msg_array ) ;
2007-05-22 02:17:13 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2007-05-24 15:09:37 +04:00
goto done ;
}
if ( ( msg_type & MSG_FLAG_LOWPRIORITY )
& & ( msg_array - > num_messages > 1000 ) ) {
DEBUG ( 5 , ( " Dropping message for PID %s \n " ,
procid_str_static ( & pid ) ) ) ;
status = NT_STATUS_INSUFFICIENT_RESOURCES ;
goto done ;
2007-05-22 02:17:13 +04:00
}
2000-09-11 11:02:43 +04:00
2007-05-22 02:17:13 +04:00
if ( ! ( rec = TALLOC_REALLOC_ARRAY ( mem_ctx , msg_array - > messages ,
struct messaging_rec ,
msg_array - > num_messages + 1 ) ) ) {
2007-05-24 15:09:37 +04:00
status = NT_STATUS_NO_MEMORY ;
goto done ;
2007-02-01 22:29:07 +03:00
}
2003-01-11 03:17:41 +03:00
2007-05-22 02:17:13 +04:00
rec [ msg_array - > num_messages ] . msg_version = MESSAGE_VERSION ;
2007-05-24 15:09:37 +04:00
rec [ msg_array - > num_messages ] . msg_type = msg_type & MSG_TYPE_MASK ;
2007-05-22 02:17:13 +04:00
rec [ msg_array - > num_messages ] . dest = pid ;
rec [ msg_array - > num_messages ] . src = procid_self ( ) ;
2007-05-24 15:09:37 +04:00
rec [ msg_array - > num_messages ] . buf = * data ;
2003-01-11 03:17:41 +03:00
2007-05-22 02:17:13 +04:00
msg_array - > messages = rec ;
msg_array - > num_messages + = 1 ;
2003-01-11 03:17:41 +03:00
2007-05-24 15:09:37 +04:00
status = messaging_tdb_store ( msg_ctx - > tdb , key , msg_array ) ;
2007-05-22 02:17:13 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2007-05-24 15:09:37 +04:00
goto done ;
2007-05-22 02:17:13 +04:00
}
status = message_notify ( pid ) ;
2003-01-11 03:17:41 +03:00
2007-05-22 02:17:13 +04:00
if ( NT_STATUS_EQUAL ( status , NT_STATUS_INVALID_HANDLE ) ) {
DEBUG ( 2 , ( " pid %s doesn't exist - deleting messages record \n " ,
procid_str_static ( & pid ) ) ) ;
2007-05-24 15:09:37 +04:00
tdb_delete ( msg_ctx - > tdb , message_key_pid ( pid ) ) ;
2000-11-17 00:38:24 +03:00
}
2007-05-24 15:09:37 +04:00
done :
tdb_chainunlock ( msg_ctx - > tdb , key ) ;
TALLOC_FREE ( mem_ctx ) ;
2007-05-22 02:17:13 +04:00
return status ;
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
2007-05-22 02:17:13 +04:00
static NTSTATUS retrieve_all_messages ( TDB_CONTEXT * msg_tdb ,
TALLOC_CTX * mem_ctx ,
struct messaging_array * * presult )
2000-09-11 11:02:43 +04:00
{
2007-05-22 02:17:13 +04:00
struct messaging_array * result ;
TDB_DATA key = message_key_pid ( procid_self ( ) ) ;
NTSTATUS status ;
2000-09-11 11:02:43 +04:00
2007-05-22 02:17:13 +04:00
if ( tdb_chainlock ( msg_tdb , key ) = = - 1 ) {
return NT_STATUS_LOCK_NOT_GRANTED ;
}
2000-09-11 11:02:43 +04:00
2007-05-22 02:17:13 +04:00
status = messaging_tdb_fetch ( msg_tdb , key , mem_ctx , & result ) ;
2003-03-20 03:52:37 +03:00
2003-01-10 23:17:02 +03:00
/*
2007-05-22 02:17:13 +04:00
* We delete the record here , tdb_set_max_dead keeps it around
2003-01-10 23:17:02 +03:00
*/
2007-05-22 02:17:13 +04:00
tdb_delete ( msg_tdb , key ) ;
tdb_chainunlock ( msg_tdb , key ) ;
2000-09-11 11:02:43 +04:00
2007-05-22 02:17:13 +04:00
if ( NT_STATUS_IS_OK ( status ) ) {
* presult = result ;
2003-01-10 23:17:02 +03:00
}
2007-05-22 02:17:13 +04:00
return status ;
2003-01-10 23:17:02 +03:00
}
2007-05-22 02:17:13 +04:00
/*
Dispatch one messsaging_rec
*/
static void messaging_dispatch_rec ( struct messaging_context * msg_ctx ,
struct messaging_rec * rec )
2003-01-10 23:17:02 +03:00
{
2007-05-22 02:17:13 +04:00
struct messaging_callback * cb , * next ;
2000-09-11 11:02:43 +04:00
2007-05-22 02:17:13 +04:00
for ( cb = msg_ctx - > callbacks ; cb ! = NULL ; cb = next ) {
next = cb - > next ;
if ( cb - > msg_type = = rec - > msg_type ) {
cb - > fn ( msg_ctx , cb - > private_data , rec - > msg_type ,
rec - > src , & rec - > buf ) ;
return ;
}
2000-09-12 10:13:25 +04:00
}
2007-05-22 02:17:13 +04:00
return ;
2000-09-11 11:02:43 +04:00
}
/****************************************************************************
2001-11-21 01:55:46 +03:00
Receive and dispatch any messages pending for this process .
2006-12-14 04:00:16 +03:00
JRA changed Dec 13 2006. Only one message handler now permitted per 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-11 11:02:43 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-11-21 01:55:46 +03:00
2007-05-22 02:17:13 +04:00
void message_dispatch ( struct messaging_context * msg_ctx )
2000-09-11 11:02:43 +04:00
{
2007-05-22 02:17:13 +04:00
struct messaging_array * msg_array = NULL ;
uint32 i ;
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
2007-05-20 21:00:18 +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 ;
2007-05-22 02:17:13 +04:00
if ( ! NT_STATUS_IS_OK ( retrieve_all_messages ( msg_ctx - > tdb , NULL ,
& msg_array ) ) ) {
2003-01-10 23:17:02 +03:00
return ;
2007-05-22 02:17:13 +04:00
}
2003-01-10 23:17:02 +03:00
2007-05-22 02:17:13 +04:00
for ( i = 0 ; i < msg_array - > num_messages ; i + + ) {
messaging_dispatch_rec ( msg_ctx , & msg_array - > messages [ i ] ) ;
2000-09-11 11:02:43 +04:00
}
2007-05-22 02:17:13 +04:00
TALLOC_FREE ( msg_array ) ;
2000-09-11 11:02:43 +04:00
}
2000-09-12 04:47:11 +04:00
/****************************************************************************
2006-12-14 04:00:16 +03:00
Register / replace a dispatch function for a particular message type .
JRA changed Dec 13 2006. Only one message handler now permitted per 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
2001-01-23 23:25:25 +03:00
struct msg_all {
2007-05-22 02:17:13 +04:00
struct messaging_context * msg_ctx ;
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 ;
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
2007-05-22 02:17:13 +04:00
static int traverse_fn ( TDB_CONTEXT * the_tdb ,
const struct connections_key * ckey ,
const struct connections_data * crec ,
void * private_data )
2000-09-13 11:07:17 +04:00
{
2007-05-22 02:17:13 +04:00
struct msg_all * msg_all = ( struct msg_all * ) private_data ;
2007-02-01 22:29:07 +03:00
NTSTATUS status ;
2000-09-13 11:07:17 +04:00
2007-05-22 02:17:13 +04:00
if ( crec - > cnum ! = - 1 )
2001-05-15 22:12:02 +04:00
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. */
2007-05-22 02:17:13 +04:00
if ( ! ( crec - > bcast_msg_flags & msg_all - > msg_flag ) )
2002-09-25 19:19:00 +04:00
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
2007-05-22 02:17:13 +04:00
status = messaging_send_buf ( msg_all - > msg_ctx ,
crec - > pid , msg_all - > msg_type ,
( uint8 * ) msg_all - > buf , msg_all - > len ) ;
2007-02-01 22:29:07 +03:00
if ( NT_STATUS_EQUAL ( status , NT_STATUS_INVALID_HANDLE ) ) {
2007-05-22 02:17:13 +04:00
TDB_DATA key ;
2001-02-03 20:19:10 +03:00
2007-05-20 21:00:18 +04:00
/* If the pid was not found delete the entry from
* connections . tdb */
2002-09-25 19:19:00 +04:00
2007-05-20 21:00:18 +04:00
DEBUG ( 2 , ( " pid %s doesn't exist - deleting connections "
2007-05-22 02:17:13 +04:00
" %d [%s] \n " , procid_str_static ( & crec - > pid ) ,
crec - > cnum , crec - > servicename ) ) ;
key . dptr = ( uint8 * ) ckey ;
key . dsize = sizeof ( * ckey ) ;
tdb_delete ( the_tdb , key ) ;
2001-02-03 20:19:10 +03:00
}
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
* */
2007-05-15 19:49:55 +04:00
BOOL message_send_all ( struct messaging_context * msg_ctx ,
int msg_type ,
2001-12-21 03:37:49 +03:00
const void * buf , size_t len ,
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 ;
2001-12-21 03:37:49 +03:00
msg_all . n_sent = 0 ;
2007-05-22 02:17:13 +04:00
msg_all . msg_ctx = msg_ctx ;
2000-09-13 11:07:17 +04:00
2007-05-22 02:17:13 +04:00
connections_forall ( 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 ;
}
2006-02-04 01:19:41 +03:00
/*
* Block and unblock receiving of messages . Allows removal of race conditions
* when doing a fork and changing message disposition .
*/
void message_block ( void )
{
BlockSignals ( True , SIGUSR1 ) ;
}
void message_unblock ( void )
{
BlockSignals ( False , SIGUSR1 ) ;
}
2007-01-31 15:01:52 +03:00
2007-05-16 17:02:53 +04:00
struct event_context * messaging_event_context ( struct messaging_context * msg_ctx )
{
return msg_ctx - > event_ctx ;
}
2007-01-31 15:01:52 +03:00
struct messaging_context * messaging_init ( TALLOC_CTX * mem_ctx ,
struct server_id server_id ,
struct event_context * ev )
{
struct messaging_context * ctx ;
if ( ! ( ctx = TALLOC_ZERO_P ( mem_ctx , struct messaging_context ) ) ) {
return NULL ;
}
ctx - > id = server_id ;
2007-05-16 17:02:53 +04:00
ctx - > event_ctx = ev ;
2007-05-15 19:14:32 +04:00
2007-05-24 15:09:37 +04:00
if ( ! message_tdb_init ( ctx ) ) {
2007-05-15 19:14:32 +04:00
DEBUG ( 0 , ( " message_init failed: %s \n " , strerror ( errno ) ) ) ;
TALLOC_FREE ( ctx ) ;
}
2007-05-24 15:09:37 +04:00
messaging_register ( ctx , NULL , MSG_PING , ping_message ) ;
/* Register some debugging related messages */
register_msg_pool_usage ( ctx ) ;
register_dmalloc_msgs ( ctx ) ;
debug_register_msgs ( ctx ) ;
2007-01-31 15:01:52 +03:00
return ctx ;
}
/*
* Register a dispatch function for a particular message type . Allow multiple
* registrants
*/
2007-05-22 02:17:13 +04:00
NTSTATUS messaging_register ( struct messaging_context * msg_ctx ,
void * private_data ,
2007-01-31 15:01:52 +03:00
uint32_t msg_type ,
void ( * fn ) ( struct messaging_context * msg ,
void * private_data ,
uint32_t msg_type ,
struct server_id server_id ,
DATA_BLOB * data ) )
{
struct messaging_callback * cb ;
2007-05-22 02:17:13 +04:00
/*
* Only one callback per type
*/
for ( cb = msg_ctx - > callbacks ; cb ! = NULL ; cb = cb - > next ) {
if ( cb - > msg_type = = msg_type ) {
cb - > fn = fn ;
cb - > private_data = private_data ;
return NT_STATUS_OK ;
}
}
if ( ! ( cb = talloc ( msg_ctx , struct messaging_callback ) ) ) {
2007-01-31 15:01:52 +03:00
return NT_STATUS_NO_MEMORY ;
}
cb - > msg_type = msg_type ;
cb - > fn = fn ;
cb - > private_data = private_data ;
2007-05-22 02:17:13 +04:00
DLIST_ADD ( msg_ctx - > callbacks , cb ) ;
2007-01-31 15:01:52 +03:00
return NT_STATUS_OK ;
}
/*
De - register the function for a particular message type .
*/
void messaging_deregister ( struct messaging_context * ctx , uint32_t msg_type ,
void * private_data )
{
struct messaging_callback * cb , * next ;
for ( cb = ctx - > callbacks ; cb ; cb = next ) {
next = cb - > next ;
if ( ( cb - > msg_type = = msg_type )
& & ( cb - > private_data = = private_data ) ) {
DLIST_REMOVE ( ctx - > callbacks , cb ) ;
TALLOC_FREE ( cb ) ;
}
}
}
2007-01-31 16:05:36 +03:00
/*
Send a message to a particular server
*/
2007-05-15 00:31:28 +04:00
NTSTATUS messaging_send ( struct messaging_context * msg_ctx ,
2007-05-24 15:09:37 +04:00
struct server_id server , uint32_t msg_type ,
const DATA_BLOB * data )
2007-01-31 16:05:36 +03:00
{
2007-05-24 15:09:37 +04:00
return messaging_tdb_send ( msg_ctx , server , msg_type , data ) ;
2007-01-31 16:05:36 +03:00
}
2007-01-31 15:01:52 +03:00
2007-05-15 00:31:28 +04:00
NTSTATUS messaging_send_buf ( struct messaging_context * msg_ctx ,
struct server_id server , uint32_t msg_type ,
const uint8 * buf , size_t len )
{
DATA_BLOB blob = data_blob_const ( buf , len ) ;
return messaging_send ( msg_ctx , server , msg_type , & blob ) ;
}
2002-03-09 12:48:35 +03:00
/** @} **/