2003-08-13 01:53:07 +00:00
/*
Unix SMB / CIFS implementation .
process model : standard ( 1 process per client connection )
Copyright ( C ) Andrew Tridgell 1992 - 2003
Copyright ( C ) James J Myers 2003 < myersjj @ samba . org >
2004-07-13 21:04:56 +00:00
Copyright ( C ) Stefan ( metze ) Metzmacher 2004
2003-08-13 01:53:07 +00: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 .
*/
# include "includes.h"
/*
called when the process model is selected
*/
2004-07-13 21:04:56 +00:00
static void standard_model_startup ( void )
2003-08-13 01:53:07 +00:00
{
2004-04-07 07:17:11 +00:00
signal ( SIGCHLD , SIG_IGN ) ;
2004-07-13 21:04:56 +00:00
smbd_process_init ( ) ;
2003-08-13 01:53:07 +00:00
}
/*
called when a listening socket becomes readable
*/
2004-07-13 21:04:56 +00:00
static void standard_accept_connection ( struct event_context * ev , struct fd_event * srv_fde , time_t t , uint16_t flags )
2003-08-13 01:53:07 +00:00
{
2004-09-20 12:31:07 +00:00
NTSTATUS status ;
struct socket_context * sock ;
2004-07-13 21:04:56 +00:00
struct server_socket * server_socket = srv_fde - > private ;
struct server_connection * conn ;
2004-09-20 12:31:07 +00:00
pid_t pid ;
2004-07-13 21:04:56 +00:00
/* accept an incoming connection. */
2004-09-20 12:31:07 +00:00
status = socket_accept ( server_socket - > socket , & sock , 0 ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2004-09-28 12:30:42 +00:00
DEBUG ( 0 , ( " standard_accept_connection: accept: %s \n " ,
2004-09-20 12:31:07 +00:00
nt_errstr ( status ) ) ) ;
2003-08-13 01:53:07 +00:00
return ;
}
pid = fork ( ) ;
if ( pid ! = 0 ) {
/* parent or error code ... */
2004-09-20 12:31:07 +00:00
socket_destroy ( sock ) ;
2003-08-13 01:53:07 +00:00
/* go back to the event loop */
return ;
}
/* Child code ... */
/* close all the listening sockets */
2004-07-13 21:04:56 +00:00
event_remove_fd_all_handler ( ev , standard_accept_connection ) ;
2003-08-13 01:53:07 +00:00
/* tdb needs special fork handling */
if ( tdb_reopen_all ( ) = = - 1 ) {
2004-07-13 21:04:56 +00:00
DEBUG ( 0 , ( " standard_accept_connection: tdb_reopen_all failed. \n " ) ) ;
2003-08-13 01:53:07 +00:00
}
2004-07-14 12:14:07 +00:00
/* Ensure that the forked children do not expose identical random streams */
set_need_random_reseed ( ) ;
2004-10-17 02:55:47 +00:00
conn = server_setup_connection ( ev , server_socket , sock , t , getpid ( ) ) ;
2004-07-13 21:04:56 +00:00
if ( ! conn ) {
2004-09-20 12:31:07 +00:00
DEBUG ( 0 , ( " server_setup_connection(ev, server_socket, sock, t) failed \n " ) ) ;
2003-12-13 10:58:48 +00:00
return ;
}
2004-07-13 21:04:56 +00:00
2004-09-26 03:05:04 +00:00
talloc_steal ( conn , sock ) ;
2004-07-13 21:04:56 +00:00
DLIST_ADD ( server_socket - > connection_list , conn ) ;
/* return to the event loop */
2003-08-13 01:53:07 +00:00
}
2004-07-13 21:04:56 +00:00
/* called when a SMB connection goes down */
static void standard_terminate_connection ( struct server_connection * conn , const char * reason )
2003-12-13 23:25:15 +00:00
{
2004-09-28 12:30:42 +00:00
DEBUG ( 2 , ( " standard_terminate_connection: reason[%s] \n " , reason ) ) ;
2004-07-15 09:43:32 +00:00
if ( conn ) {
2004-09-26 03:50:24 +00:00
talloc_free ( conn - > service - > srv_ctx ) ;
2004-07-15 09:43:32 +00:00
}
2004-09-26 06:44:08 +00:00
/* this init_iconv() has the effect of freeing the iconv context memory,
which makes leak checking easier */
init_iconv ( ) ;
2003-12-13 23:25:15 +00:00
/* terminate this process */
exit ( 0 ) ;
}
2004-07-13 21:04:56 +00:00
static int standard_get_id ( struct smbsrv_request * req )
2003-08-13 01:53:07 +00:00
{
2004-06-29 07:40:14 +00:00
return ( int ) req - > smb_conn - > pid ;
2003-08-13 01:53:07 +00:00
}
2004-07-13 21:04:56 +00:00
static void standard_exit_server ( struct server_context * srv_ctx , const char * reason )
2004-02-02 13:43:03 +00:00
{
DEBUG ( 1 , ( " standard_exit_server: reason[%s] \n " , reason ) ) ;
}
2003-08-13 01:53:07 +00:00
/*
2004-02-02 13:43:03 +00:00
initialise the standard process model , registering ourselves with the process model subsystem
2003-08-13 01:53:07 +00:00
*/
2004-02-02 13:43:03 +00:00
NTSTATUS process_model_standard_init ( void )
2003-08-13 01:53:07 +00:00
{
2004-02-02 13:43:03 +00:00
NTSTATUS ret ;
2003-08-13 01:53:07 +00:00
struct model_ops ops ;
ZERO_STRUCT ( ops ) ;
2004-02-02 13:43:03 +00:00
/* fill in our name */
ops . name = " standard " ;
2003-08-13 01:53:07 +00:00
/* fill in all the operations */
2004-07-13 21:04:56 +00:00
ops . model_startup = standard_model_startup ;
ops . accept_connection = standard_accept_connection ;
ops . terminate_connection = standard_terminate_connection ;
2004-02-02 13:43:03 +00:00
ops . exit_server = standard_exit_server ;
2004-07-13 21:04:56 +00:00
ops . get_id = standard_get_id ;
2003-08-13 01:53:07 +00:00
2004-02-02 13:43:03 +00:00
/* register ourselves with the PROCESS_MODEL subsystem. */
ret = register_backend ( " process_model " , & ops ) ;
if ( ! NT_STATUS_IS_OK ( ret ) ) {
DEBUG ( 0 , ( " Failed to register process_model 'standard'! \n " ) ) ;
return ret ;
}
return ret ;
2003-08-13 01:53:07 +00:00
}