2014-10-04 13:15:12 +04:00
/*
* Unix SMB / CIFS implementation .
* Samba internal messaging functions
* Copyright ( C ) 2014 by Volker Lendecke
*
* 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 3 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 , see < http : //www.gnu.org/licenses/>.
*/
# include "replace.h"
2015-07-12 09:31:52 +03:00
# include <talloc.h>
2014-10-04 13:15:12 +04:00
# include "messages_dgm.h"
# include "messages_dgm_ref.h"
# include "lib/util/debug.h"
# include "lib/util/dlinklist.h"
struct msg_dgm_ref {
struct msg_dgm_ref * prev , * next ;
2016-10-01 07:53:44 +03:00
struct messaging_dgm_fde * fde ;
2016-09-24 04:36:15 +03:00
void ( * recv_cb ) ( struct tevent_context * ev ,
const uint8_t * msg , size_t msg_len ,
2014-10-04 13:15:12 +04:00
int * fds , size_t num_fds , void * private_data ) ;
void * recv_cb_private_data ;
} ;
static pid_t dgm_pid = 0 ;
static struct msg_dgm_ref * refs = NULL ;
2018-07-09 13:33:34 +03:00
static struct msg_dgm_ref * next_ref = NULL ;
2014-10-04 13:15:12 +04:00
static int msg_dgm_ref_destructor ( struct msg_dgm_ref * r ) ;
2016-09-24 04:36:15 +03:00
static void msg_dgm_ref_recv ( struct tevent_context * ev ,
const uint8_t * msg , size_t msg_len ,
2014-10-04 13:15:12 +04:00
int * fds , size_t num_fds , void * private_data ) ;
void * messaging_dgm_ref ( TALLOC_CTX * mem_ctx , struct tevent_context * ev ,
2015-10-12 22:30:30 +03:00
uint64_t * unique ,
2014-10-04 13:15:12 +04:00
const char * socket_dir ,
const char * lockfile_dir ,
2016-09-24 04:36:15 +03:00
void ( * recv_cb ) ( struct tevent_context * ev ,
const uint8_t * msg , size_t msg_len ,
2014-10-04 13:15:12 +04:00
int * fds , size_t num_fds ,
void * private_data ) ,
void * recv_cb_private_data ,
int * err )
{
struct msg_dgm_ref * result , * tmp_refs ;
result = talloc ( mem_ctx , struct msg_dgm_ref ) ;
if ( result = = NULL ) {
* err = ENOMEM ;
return NULL ;
}
2016-10-01 07:53:44 +03:00
result - > fde = NULL ;
2014-10-04 13:15:12 +04:00
tmp_refs = refs ;
2022-07-25 15:29:35 +03:00
if ( ( refs ! = NULL ) & & ( dgm_pid ! = tevent_cached_getpid ( ) ) ) {
2014-10-04 13:15:12 +04:00
/*
* Have to reinit after fork
*/
messaging_dgm_destroy ( ) ;
refs = NULL ;
}
if ( refs = = NULL ) {
int ret ;
ret = messaging_dgm_init ( ev , unique , socket_dir , lockfile_dir ,
msg_dgm_ref_recv , NULL ) ;
2015-10-21 21:07:57 +03:00
DBG_DEBUG ( " messaging_dgm_init returned %s \n " , strerror ( ret ) ) ;
2014-10-04 13:15:12 +04:00
if ( ret ! = 0 ) {
DEBUG ( 10 , ( " messaging_dgm_init failed: %s \n " ,
strerror ( ret ) ) ) ;
TALLOC_FREE ( result ) ;
* err = ret ;
return NULL ;
}
2022-07-25 15:29:35 +03:00
dgm_pid = tevent_cached_getpid ( ) ;
2015-05-18 14:15:35 +03:00
} else {
2015-10-12 22:30:30 +03:00
int ret ;
2022-07-25 15:29:35 +03:00
ret = messaging_dgm_get_unique ( tevent_cached_getpid ( ) , unique ) ;
2015-10-21 21:07:57 +03:00
DBG_DEBUG ( " messaging_dgm_get_unique returned %s \n " ,
strerror ( ret ) ) ;
2015-10-12 22:30:30 +03:00
if ( ret ! = 0 ) {
TALLOC_FREE ( result ) ;
* err = ret ;
return NULL ;
}
2016-09-30 16:42:40 +03:00
}
2016-10-01 07:53:44 +03:00
result - > fde = messaging_dgm_register_tevent_context ( result , ev ) ;
if ( result - > fde = = NULL ) {
2016-09-30 16:42:40 +03:00
TALLOC_FREE ( result ) ;
* err = ENOMEM ;
return NULL ;
2014-10-04 13:15:12 +04:00
}
2015-10-21 21:07:57 +03:00
DBG_DEBUG ( " unique = % " PRIu64 " \n " , * unique ) ;
2015-10-14 12:31:07 +03:00
2014-10-04 13:15:12 +04:00
refs = tmp_refs ;
result - > recv_cb = recv_cb ;
result - > recv_cb_private_data = recv_cb_private_data ;
DLIST_ADD ( refs , result ) ;
talloc_set_destructor ( result , msg_dgm_ref_destructor ) ;
return result ;
}
2016-09-24 04:36:15 +03:00
static void msg_dgm_ref_recv ( struct tevent_context * ev ,
const uint8_t * msg , size_t msg_len ,
2014-10-04 13:15:12 +04:00
int * fds , size_t num_fds , void * private_data )
{
2018-07-09 13:33:34 +03:00
struct msg_dgm_ref * r ;
2014-10-04 13:15:12 +04:00
/*
* We have to broadcast incoming messages to all refs . The first ref
* that grabs the fd ' s will get them .
*/
2018-07-09 13:33:34 +03:00
for ( r = refs ; r ! = NULL ; r = next_ref ) {
2016-10-01 07:53:44 +03:00
bool active ;
2016-09-30 16:42:40 +03:00
2018-07-09 13:33:34 +03:00
next_ref = r - > next ;
2016-09-30 16:42:40 +03:00
2016-10-01 07:53:44 +03:00
active = messaging_dgm_fde_active ( r - > fde ) ;
if ( ! active ) {
2016-09-30 16:42:40 +03:00
/*
* r ' s tevent_context has died .
*/
continue ;
}
2016-09-24 04:36:15 +03:00
r - > recv_cb ( ev , msg , msg_len , fds , num_fds ,
2014-10-04 13:15:12 +04:00
r - > recv_cb_private_data ) ;
}
}
static int msg_dgm_ref_destructor ( struct msg_dgm_ref * r )
{
if ( refs = = NULL ) {
abort ( ) ;
}
2018-07-09 13:33:34 +03:00
if ( r = = next_ref ) {
next_ref = r - > next ;
}
2014-10-04 13:15:12 +04:00
DLIST_REMOVE ( refs , r ) ;
2016-10-01 07:53:44 +03:00
TALLOC_FREE ( r - > fde ) ;
2015-05-18 14:15:35 +03:00
2015-10-21 21:07:57 +03:00
DBG_DEBUG ( " refs=%p \n " , refs ) ;
2015-10-14 12:31:07 +03:00
2014-10-04 13:15:12 +04:00
if ( refs = = NULL ) {
messaging_dgm_destroy ( ) ;
}
return 0 ;
}