2003-08-13 05:53:07 +04:00
/*
Unix SMB / CIFS implementation .
2005-01-30 03:54:57 +03:00
2003-08-13 05:53:07 +04:00
process model : process ( 1 process handles all client connections )
2005-01-30 03:54:57 +03:00
2003-08-13 05:53:07 +04:00
Copyright ( C ) Andrew Tridgell 2003
Copyright ( C ) James J Myers 2003 < myersjj @ samba . org >
2004-07-14 01:04:56 +04:00
Copyright ( C ) Stefan ( metze ) Metzmacher 2004
2003-08-13 05:53:07 +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
2007-07-10 06:07:03 +04:00
the Free Software Foundation ; either version 3 of the License , or
2003-08-13 05:53:07 +04:00
( 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
2007-07-10 06:07:03 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2003-08-13 05:53:07 +04:00
*/
# include "includes.h"
2020-11-20 17:27:17 +03:00
# include "samba/process_model.h"
2006-11-07 15:03:01 +03:00
# include "system/filesys.h"
2007-01-10 13:52:09 +03:00
# include "cluster/cluster.h"
2003-08-13 05:53:07 +04:00
2017-04-20 22:24:43 +03:00
NTSTATUS process_model_single_init ( TALLOC_CTX * ) ;
2011-03-19 02:43:15 +03:00
2003-08-13 05:53:07 +04:00
/*
called when the process model is selected
*/
2010-10-30 04:24:15 +04:00
static void single_model_init ( void )
2005-01-14 04:32:56 +03:00
{
2003-08-13 05:53:07 +04:00
}
/*
2005-01-30 03:54:57 +03:00
called when a listening socket becomes readable .
2003-08-13 05:53:07 +04:00
*/
2008-12-29 22:24:57 +03:00
static void single_accept_connection ( struct tevent_context * ev ,
2008-01-06 04:03:43 +03:00
struct loadparm_context * lp_ctx ,
2008-02-05 06:51:01 +03:00
struct socket_context * listen_socket ,
2008-12-29 22:24:57 +03:00
void ( * new_conn ) ( struct tevent_context * ,
2008-01-06 04:03:43 +03:00
struct loadparm_context * ,
struct socket_context * ,
2017-09-14 22:09:23 +03:00
struct server_id , void * ,
void * ) ,
void * private_data ,
void * process_context )
2003-08-13 05:53:07 +04:00
{
2004-09-20 16:31:07 +04:00
NTSTATUS status ;
2008-02-05 06:51:01 +03:00
struct socket_context * connected_socket ;
2013-01-25 06:15:51 +04:00
pid_t pid = getpid ( ) ;
2004-07-14 01:04:56 +04:00
2003-08-13 05:53:07 +04:00
/* accept an incoming connection. */
2008-02-05 06:51:01 +03:00
status = socket_accept ( listen_socket , & connected_socket ) ;
2004-09-20 16:31:07 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2017-09-11 02:39:39 +03:00
DBG_ERR ( " single_accept_connection: accept: %s \n " ,
nt_errstr ( status ) ) ;
2008-02-04 09:53:01 +03:00
/* this looks strange, but is correct.
We can only be here if woken up from select , due to
2010-02-21 09:46:46 +03:00
an incoming connection .
2008-02-04 09:53:01 +03:00
We need to throttle things until the system clears
enough resources to handle this new socket .
If we don ' t then we will spin filling the log and
causing more problems . We don ' t panic as this is
probably a temporary resource constraint */
2005-10-12 15:04:01 +04:00
sleep ( 1 ) ;
2003-12-13 13:58:48 +03:00
return ;
}
2004-07-14 01:04:56 +04:00
2009-02-02 10:41:28 +03:00
talloc_steal ( private_data , connected_socket ) ;
2004-09-26 07:05:04 +04:00
2013-01-25 06:15:51 +04:00
/*
* We use the PID so we cannot collide in with cluster ids
* generated in other single mode tasks , and , and won ' t
2023-09-07 07:24:56 +03:00
* collide with PIDs from process model standard because the
2013-01-25 06:15:51 +04:00
* combination of pid / fd should be unique system - wide
*/
2010-07-16 08:32:42 +04:00
new_conn ( ev , lp_ctx , connected_socket ,
2017-09-14 22:09:23 +03:00
cluster_id ( pid , socket_get_fd ( connected_socket ) ) , private_data ,
process_context ) ;
2003-08-13 05:53:07 +04:00
}
2005-01-30 05:55:30 +03:00
/*
called to startup a new task
*/
2017-08-21 22:58:14 +03:00
static void single_new_task ( struct tevent_context * ev ,
2010-07-16 08:32:42 +04:00
struct loadparm_context * lp_ctx ,
2008-02-04 09:53:01 +03:00
const char * service_name ,
2018-08-23 00:35:52 +03:00
struct task_server * ( * new_task ) ( struct tevent_context * ,
2017-09-14 22:09:23 +03:00
struct loadparm_context * ,
struct server_id , void * , void * ) ,
2017-08-21 22:58:14 +03:00
void * private_data ,
2017-09-14 22:09:23 +03:00
const struct service_details * service_details ,
2017-08-21 22:58:14 +03:00
int from_parent_fd )
2005-01-30 05:55:30 +03:00
{
2013-01-25 16:00:12 +04:00
pid_t pid = getpid ( ) ;
/* start our taskids at MAX_INT32, the first 2^31 tasks are is reserved for fd numbers */
static uint32_t taskid = INT32_MAX ;
2018-08-23 00:35:52 +03:00
struct task_server * task = NULL ;
2013-01-25 16:00:12 +04:00
/*
* We use the PID so we cannot collide in with cluster ids
* generated in other single mode tasks , and , and won ' t
2023-09-07 07:24:56 +03:00
* collide with PIDs from process model standard because the
2013-01-25 16:00:12 +04:00
* combination of pid / task_id should be unique system - wide
*
* Using the pid unaltered makes debugging of which process
* owns the messaging socket easier .
*/
2018-08-23 00:35:52 +03:00
task = new_task ( ev , lp_ctx , cluster_id ( pid , taskid + + ) , private_data , NULL ) ;
if ( task ! = NULL & & service_details - > post_fork ! = NULL ) {
2018-08-29 04:16:08 +03:00
struct process_details pd = initial_process_details ;
service_details - > post_fork ( task , & pd ) ;
2018-08-23 00:35:52 +03:00
}
2023-03-01 12:48:18 +03:00
if ( task ! = NULL & & service_details - > before_loop ! = NULL ) {
service_details - > before_loop ( task ) ;
}
2005-01-30 05:55:30 +03:00
}
2018-09-04 03:12:49 +03:00
/*
* Called when a task goes down
*/
static void single_terminate_task ( struct tevent_context * ev ,
struct loadparm_context * lp_ctx ,
const char * reason ,
bool fatal ,
void * process_context )
2003-12-14 02:25:15 +03:00
{
2017-09-11 02:39:39 +03:00
DBG_NOTICE ( " single_terminate: reason[%s] \n " , reason ) ;
2004-02-02 16:43:03 +03:00
}
2018-09-04 03:12:49 +03:00
/*
* Called when a connection has ended
*/
static void single_terminate_connection ( struct tevent_context * ev ,
struct loadparm_context * lp_ctx ,
const char * reason ,
void * process_context )
{
}
2006-03-09 20:48:41 +03:00
/* called to set a title of a task or connection */
2008-12-29 22:24:57 +03:00
static void single_set_title ( struct tevent_context * ev , const char * title )
2006-03-09 20:48:41 +03:00
{
}
2007-09-03 17:13:25 +04:00
const struct model_ops single_ops = {
2005-01-14 04:32:56 +03:00
. name = " single " ,
. model_init = single_model_init ,
2018-09-04 03:12:49 +03:00
. new_task = single_new_task ,
2005-01-14 04:32:56 +03:00
. accept_connection = single_accept_connection ,
2018-09-04 03:12:49 +03:00
. terminate_task = single_terminate_task ,
. terminate_connection = single_terminate_connection ,
2006-03-09 20:48:41 +03:00
. set_title = single_set_title ,
2005-01-14 04:32:56 +03:00
} ;
2003-08-13 05:53:07 +04:00
/*
2005-01-30 03:54:57 +03:00
initialise the single process model , registering ourselves with the
process model subsystem
2003-08-13 05:53:07 +04:00
*/
2017-04-20 22:24:43 +03:00
NTSTATUS process_model_single_init ( TALLOC_CTX * ctx )
2003-08-13 05:53:07 +04:00
{
2005-01-30 03:54:57 +03:00
return register_process_model ( & single_ops ) ;
2003-08-13 05:53:07 +04:00
}