2010-05-13 01:48:04 +04:00
/*
Unix SMB / CIFS implementation .
Main SMB server routines
Copyright ( C ) Andrew Tridgell 1992 - 1998
Copyright ( C ) Martin Pool 2002
Copyright ( C ) Jelmer Vernooij 2002 - 2003
Copyright ( C ) Volker Lendecke 1993 - 2007
Copyright ( C ) Jeremy Allison 1993 - 2007
Copyright ( C ) Andrew Bartlett 2010
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 "includes.h"
2011-03-22 18:57:01 +03:00
# include "smbd/smbd.h"
2010-05-13 01:48:04 +04:00
# include "smbd/globals.h"
2011-05-02 15:21:53 +04:00
# include "ntdomain.h"
2010-12-20 13:42:17 +03:00
# include "../librpc/gen_ndr/srv_dfs.h"
# include "../librpc/gen_ndr/srv_dssetup.h"
# include "../librpc/gen_ndr/srv_echo.h"
# include "../librpc/gen_ndr/srv_eventlog.h"
# include "../librpc/gen_ndr/srv_initshutdown.h"
# include "../librpc/gen_ndr/srv_lsa.h"
# include "../librpc/gen_ndr/srv_netlogon.h"
# include "../librpc/gen_ndr/srv_ntsvcs.h"
# include "../librpc/gen_ndr/srv_samr.h"
# include "../librpc/gen_ndr/srv_spoolss.h"
# include "../librpc/gen_ndr/srv_srvsvc.h"
# include "../librpc/gen_ndr/srv_svcctl.h"
# include "../librpc/gen_ndr/srv_winreg.h"
# include "../librpc/gen_ndr/srv_wkssvc.h"
2011-02-22 12:28:29 +03:00
# include "printing/notify.h"
2011-02-22 21:24:31 +03:00
# include "printing.h"
2011-02-25 01:05:57 +03:00
# include "serverid.h"
2011-12-15 14:51:20 +04:00
# include "messages.h"
2018-03-20 03:14:38 +03:00
# include "passdb.h"
2012-07-20 03:36:18 +04:00
# include "../lib/util/pidfile.h"
s3:smbprofile: Replace sysv shmem with tdb
What?
This patch gets rid of the central shared memory segment referenced by
"profile_p". Instead, every smbd gets a static profile_area where it collects
profiling data. Once a second, every smbd writes this profiling data into a
record of its own in a "smbprofile.tdb". smbstatus -P does a tdb_traverse on this
database and sums up what it finds.
Why?
At least in my perception sysv IPC has not the best reputation on earth. The
code before this patch uses shmat(). Samba ages ago has developed a good
abstraction of shared memory: It's called tdb.
The main reason why I started this is that I have a request to become
more flexible with profiling data. Samba should be able to collect data
per share or per user, something which is almost impossible to do with
a fixed structure. My idea is to for example install a profile area per
share and every second marshall this into one tdb record indexed by share
name. smbstatus -P would then also collect the data and either aggregate
them or put them into individual per-share statistics. This flexibility
in the data model is not really possible with one fixed structure.
But isn't it slow?
Well, I don't think so. I can't really prove it, but I do believe that on large
boxes atomically incrementing a shared memory value for every SMB does show up
due to NUMA effects. With this patch the hot code path is completely
process-local. Once a second every smbd writes into a central tdb, this of
course does atomic operations. But it's once a second, not on every SMB2 read.
There's two places where I would like to improve things: With the current code
all smbds wake up once a second. With 10,000 potentially idle smbds this will
become noticable. That's why the current only starts the timer when something has
changed.
The second place is the tdb traverse: Right now traverse is blocking in the
sense that when it has to switch hash chains it will block. With mutexes, this
means a syscall. I have a traverse light in mind that works as follows: It
assumes a locked hash chain and then walks the complete chain in one run
without unlocking in between. This way the caller can do nonblocking locks in
the first round and only do blocking locks in a second round. Also, a lot of
syscall overhead will vanish. This way smbstatus -P will have almost zero
impact on normal operations.
Pair-Programmed-With: Stefan Metzmacher <metze@samba.org>
Signed-off-by: Volker Lendecke <vl@samba.org>
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
2014-09-29 20:08:17 +04:00
# include "smbprofile.h"
2017-07-25 02:12:45 +03:00
# include "libcli/auth/netlogon_creds_cli.h"
2010-12-20 13:42:17 +03:00
2010-05-13 01:48:04 +04:00
static struct files_struct * log_writeable_file_fn (
struct files_struct * fsp , void * private_data )
{
bool * found = ( bool * ) private_data ;
char * path ;
if ( ! fsp - > can_write ) {
return NULL ;
}
if ( ! ( * found ) ) {
DEBUG ( 0 , ( " Writable files open at exit: \n " ) ) ;
* found = true ;
}
path = talloc_asprintf ( talloc_tos ( ) , " %s/%s " , fsp - > conn - > connectpath ,
smb_fname_str_dbg ( fsp - > fsp_name ) ) ;
if ( path = = NULL ) {
DEBUGADD ( 0 , ( " <NOMEM> \n " ) ) ;
}
DEBUGADD ( 0 , ( " %s \n " , path ) ) ;
TALLOC_FREE ( path ) ;
return NULL ;
}
/****************************************************************************
Exit the server .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Reasons for shutting down a server process. */
enum server_exit_reason { SERVER_EXIT_NORMAL , SERVER_EXIT_ABNORMAL } ;
static void exit_server_common ( enum server_exit_reason how ,
2012-05-22 15:18:06 +04:00
const char * reason ) _NORETURN_ ;
2010-05-13 01:48:04 +04:00
static void exit_server_common ( enum server_exit_reason how ,
2012-05-22 15:18:06 +04:00
const char * reason )
2010-05-13 01:48:04 +04:00
{
2014-05-20 21:47:13 +04:00
struct smbXsrv_client * client = global_smbXsrv_client ;
struct smbXsrv_connection * xconn = NULL ;
2012-05-24 15:46:11 +04:00
struct smbd_server_connection * sconn = NULL ;
2012-05-31 17:28:11 +04:00
struct messaging_context * msg_ctx = server_messaging_context ( ) ;
2012-05-24 15:46:11 +04:00
2014-05-20 21:47:13 +04:00
if ( client ! = NULL ) {
sconn = client - > sconn ;
/*
* Here we typically have just one connection
*/
xconn = client - > connections ;
2012-05-24 15:46:11 +04:00
}
2010-05-13 01:48:04 +04:00
if ( ! exit_firsttime )
exit ( 0 ) ;
exit_firsttime = false ;
change_to_root_user ( ) ;
2014-05-20 21:47:13 +04:00
if ( xconn ! = NULL ) {
/*
* This is typically the disconnect for the only
* ( or with multi - channel last ) connection of the client
*/
2014-06-10 17:05:41 +04:00
if ( NT_STATUS_IS_OK ( xconn - > transport . status ) ) {
2014-02-17 15:01:12 +04:00
switch ( how ) {
case SERVER_EXIT_ABNORMAL :
2014-06-10 17:05:41 +04:00
xconn - > transport . status = NT_STATUS_INTERNAL_ERROR ;
2014-02-17 15:01:12 +04:00
break ;
case SERVER_EXIT_NORMAL :
2014-06-10 17:05:41 +04:00
xconn - > transport . status = NT_STATUS_LOCAL_DISCONNECT ;
2014-02-17 15:01:12 +04:00
break ;
}
}
2014-06-10 17:05:41 +04:00
TALLOC_FREE ( xconn - > smb1 . negprot . auth_context ) ;
2014-05-20 21:47:13 +04:00
}
change_to_root_user ( ) ;
2014-02-17 15:05:42 +04:00
2014-05-20 21:47:13 +04:00
if ( sconn ! = NULL ) {
2010-09-27 05:53:00 +04:00
if ( lp_log_writeable_files_on_exit ( ) ) {
bool found = false ;
files_forall ( sconn , log_writeable_file_fn , & found ) ;
}
2014-05-20 21:47:13 +04:00
}
change_to_root_user ( ) ;
if ( xconn ! = NULL ) {
NTSTATUS status ;
2012-05-22 15:18:06 +04:00
/*
* Note : this is a no - op for smb2 as
* conn - > tcon_table is empty
*/
2014-06-10 17:05:41 +04:00
status = smb1srv_tcon_disconnect_all ( xconn ) ;
2012-05-22 15:18:06 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
DEBUG ( 0 , ( " Server exit (%s) \n " ,
( reason ? reason : " normal exit " ) ) ) ;
DEBUG ( 0 , ( " exit_server_common: "
" smb1srv_tcon_disconnect_all() failed (%s) - "
" triggering cleanup \n " , nt_errstr ( status ) ) ) ;
}
2014-06-10 17:05:41 +04:00
status = smbXsrv_session_logoff_all ( xconn ) ;
2012-05-22 15:18:06 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
DEBUG ( 0 , ( " Server exit (%s) \n " ,
( reason ? reason : " normal exit " ) ) ) ;
DEBUG ( 0 , ( " exit_server_common: "
" smbXsrv_session_logoff_all() failed (%s) - "
" triggering cleanup \n " , nt_errstr ( status ) ) ) ;
}
2010-05-13 01:48:04 +04:00
}
2014-05-20 21:47:13 +04:00
change_to_root_user ( ) ;
2010-05-13 01:48:04 +04:00
/* 3 second timeout. */
2012-05-31 17:28:11 +04:00
print_notify_send_messages ( msg_ctx , 3 ) ;
2010-05-13 01:48:04 +04:00
# ifdef USE_DMAPI
/* Destroy Samba DMAPI session only if we are master smbd process */
if ( am_parent ) {
if ( ! dmapi_destroy_session ( ) ) {
DEBUG ( 0 , ( " Unable to close Samba DMAPI session \n " ) ) ;
}
}
# endif
2010-12-20 13:42:17 +03:00
if ( am_parent ) {
rpc_wkssvc_shutdown ( ) ;
rpc_dssetup_shutdown ( ) ;
2011-01-04 18:53:33 +03:00
# ifdef DEVELOPER
2010-12-20 13:42:17 +03:00
rpc_rpcecho_shutdown ( ) ;
2011-01-04 18:53:33 +03:00
# endif
2010-12-20 13:42:17 +03:00
rpc_netdfs_shutdown ( ) ;
rpc_initshutdown_shutdown ( ) ;
rpc_eventlog_shutdown ( ) ;
rpc_ntsvcs_shutdown ( ) ;
rpc_svcctl_shutdown ( ) ;
rpc_spoolss_shutdown ( ) ;
rpc_srvsvc_shutdown ( ) ;
rpc_winreg_shutdown ( ) ;
rpc_netlogon_shutdown ( ) ;
rpc_samr_shutdown ( ) ;
rpc_lsarpc_shutdown ( ) ;
}
2010-05-13 01:48:04 +04:00
/*
* we need to force the order of freeing the following ,
* because smbd_msg_ctx is not a talloc child of smbd_server_conn .
*/
2014-05-20 21:47:13 +04:00
if ( client ! = NULL ) {
2014-10-10 13:40:14 +04:00
struct smbXsrv_connection * next ;
for ( ; xconn ! = NULL ; xconn = next ) {
next = xconn - > next ;
2014-05-20 21:47:13 +04:00
DLIST_REMOVE ( client - > connections , xconn ) ;
talloc_free ( xconn ) ;
2014-11-14 14:47:01 +03:00
DO_PROFILE_INC ( disconnect ) ;
2014-05-20 21:47:13 +04:00
}
2015-12-07 19:55:27 +03:00
TALLOC_FREE ( client - > sconn ) ;
2014-05-20 21:47:13 +04:00
}
2010-05-13 01:48:04 +04:00
sconn = NULL ;
2014-06-10 17:05:41 +04:00
xconn = NULL ;
2014-05-20 21:47:13 +04:00
client = NULL ;
2017-07-25 02:12:45 +03:00
netlogon_creds_cli_close_global_db ( ) ;
2014-05-20 21:47:13 +04:00
TALLOC_FREE ( global_smbXsrv_client ) ;
s3:smbprofile: Replace sysv shmem with tdb
What?
This patch gets rid of the central shared memory segment referenced by
"profile_p". Instead, every smbd gets a static profile_area where it collects
profiling data. Once a second, every smbd writes this profiling data into a
record of its own in a "smbprofile.tdb". smbstatus -P does a tdb_traverse on this
database and sums up what it finds.
Why?
At least in my perception sysv IPC has not the best reputation on earth. The
code before this patch uses shmat(). Samba ages ago has developed a good
abstraction of shared memory: It's called tdb.
The main reason why I started this is that I have a request to become
more flexible with profiling data. Samba should be able to collect data
per share or per user, something which is almost impossible to do with
a fixed structure. My idea is to for example install a profile area per
share and every second marshall this into one tdb record indexed by share
name. smbstatus -P would then also collect the data and either aggregate
them or put them into individual per-share statistics. This flexibility
in the data model is not really possible with one fixed structure.
But isn't it slow?
Well, I don't think so. I can't really prove it, but I do believe that on large
boxes atomically incrementing a shared memory value for every SMB does show up
due to NUMA effects. With this patch the hot code path is completely
process-local. Once a second every smbd writes into a central tdb, this of
course does atomic operations. But it's once a second, not on every SMB2 read.
There's two places where I would like to improve things: With the current code
all smbds wake up once a second. With 10,000 potentially idle smbds this will
become noticable. That's why the current only starts the timer when something has
changed.
The second place is the tdb traverse: Right now traverse is blocking in the
sense that when it has to switch hash chains it will block. With mutexes, this
means a syscall. I have a traverse light in mind that works as follows: It
assumes a locked hash chain and then walks the complete chain in one run
without unlocking in between. This way the caller can do nonblocking locks in
the first round and only do blocking locks in a second round. Also, a lot of
syscall overhead will vanish. This way smbstatus -P will have almost zero
impact on normal operations.
Pair-Programmed-With: Stefan Metzmacher <metze@samba.org>
Signed-off-by: Volker Lendecke <vl@samba.org>
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
2014-09-29 20:08:17 +04:00
smbprofile_dump ( ) ;
2010-06-10 19:54:00 +04:00
server_messaging_context_free ( ) ;
server_event_context_free ( ) ;
2010-06-11 00:17:35 +04:00
TALLOC_FREE ( smbd_memcache_ctx ) ;
2010-05-13 01:48:04 +04:00
2011-07-04 19:55:54 +04:00
locking_end ( ) ;
printing_end ( ) ;
2010-05-13 01:48:04 +04:00
if ( how ! = SERVER_EXIT_NORMAL ) {
2013-03-27 07:17:11 +04:00
smb_panic ( reason ) ;
2010-05-13 01:48:04 +04:00
2012-05-31 23:35:04 +04:00
/* Notreached. */
exit ( 1 ) ;
2010-05-13 01:48:04 +04:00
} else {
DEBUG ( 3 , ( " Server exit (%s) \n " ,
( reason ? reason : " normal exit " ) ) ) ;
if ( am_parent ) {
2014-02-03 06:57:21 +04:00
pidfile_unlink ( lp_pid_directory ( ) , " smbd " ) ;
2010-05-13 01:48:04 +04:00
}
gencache_stabilize ( ) ;
}
2012-05-31 23:35:04 +04:00
exit ( 0 ) ;
2010-05-13 01:48:04 +04:00
}
2012-10-09 16:35:04 +04:00
void smbd_exit_server ( const char * const explanation )
2010-05-13 01:48:04 +04:00
{
exit_server_common ( SERVER_EXIT_ABNORMAL , explanation ) ;
}
2012-10-09 16:35:04 +04:00
void smbd_exit_server_cleanly ( const char * const explanation )
2010-05-13 01:48:04 +04:00
{
exit_server_common ( SERVER_EXIT_NORMAL , explanation ) ;
}
2015-04-22 12:57:24 +03:00
/*
* reinit_after_fork ( ) wrapper that should be called when forking from
* smbd .
*/
NTSTATUS smbd_reinit_after_fork ( struct messaging_context * msg_ctx ,
struct tevent_context * ev_ctx ,
2015-09-23 21:14:05 +03:00
bool parent_longlived , const char * comment )
2015-04-22 12:57:24 +03:00
{
2018-03-20 03:14:38 +03:00
NTSTATUS ret ;
2015-04-22 12:57:24 +03:00
am_parent = NULL ;
2018-03-20 03:14:38 +03:00
ret = reinit_after_fork ( msg_ctx , ev_ctx , parent_longlived , comment ) ;
initialize_password_db ( true , ev_ctx ) ;
return ret ;
2015-04-22 12:57:24 +03:00
}