Merge branch 'master' of ssh://git.samba.org/data/git/samba
[amitay/samba.git] / source4 / smbd / process_single.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    process model: process (1 process handles all client connections)
5
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) James J Myers 2003 <myersjj@samba.org>
8    Copyright (C) Stefan (metze) Metzmacher 2004
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "smbd/process_model.h"
26 #include "system/filesys.h"
27 #include "cluster/cluster.h"
28
29 /*
30   called when the process model is selected
31 */
32 static void single_model_init(struct tevent_context *ev)
33 {
34 }
35
36 /*
37   called when a listening socket becomes readable. 
38 */
39 static void single_accept_connection(struct tevent_context *ev, 
40                                      struct loadparm_context *lp_ctx,
41                                      struct socket_context *listen_socket,
42                                      void (*new_conn)(struct tevent_context *, 
43                                                       struct loadparm_context *,
44                                                       struct socket_context *, 
45                                                       struct server_id , void *), 
46                                      void *private_data)
47 {
48         NTSTATUS status;
49         struct socket_context *connected_socket;
50
51         /* accept an incoming connection. */
52         status = socket_accept(listen_socket, &connected_socket);
53         if (!NT_STATUS_IS_OK(status)) {
54                 DEBUG(0,("single_accept_connection: accept: %s\n", nt_errstr(status)));
55                 /* this looks strange, but is correct. 
56
57                    We can only be here if woken up from select, due to
58                    an incomming connection.
59
60                    We need to throttle things until the system clears
61                    enough resources to handle this new socket. 
62
63                    If we don't then we will spin filling the log and
64                    causing more problems. We don't panic as this is
65                    probably a temporary resource constraint */
66                 sleep(1);
67                 return;
68         }
69
70         talloc_steal(private_data, connected_socket);
71
72         /* The cluster_id(0, fd) cannot collide with the incrementing
73          * task below, as the first component is 0, not 1 */
74         new_conn(ev, lp_ctx, connected_socket, 
75                  cluster_id(0, socket_get_fd(connected_socket)), private_data);
76 }
77
78 /*
79   called to startup a new task
80 */
81 static void single_new_task(struct tevent_context *ev, 
82                             struct loadparm_context *lp_ctx, 
83                             const char *service_name,
84                             void (*new_task)(struct tevent_context *, struct loadparm_context *, struct server_id, void *), 
85                             void *private_data)
86 {
87         static uint32_t taskid = 0;
88        
89         /* We use 1 so we cannot collide in with cluster ids generated
90          * in the accept connection above, and unlikly to collide with
91          * PIDs from process modal standard (don't run samba as
92          * init) */
93         new_task(ev, lp_ctx, cluster_id(1, taskid++), private_data);
94 }
95
96
97 /* called when a task goes down */
98 static void single_terminate(struct tevent_context *ev, struct loadparm_context *lp_ctx, const char *reason) 
99 {
100         DEBUG(2,("single_terminate: reason[%s]\n",reason));
101 }
102
103 /* called to set a title of a task or connection */
104 static void single_set_title(struct tevent_context *ev, const char *title) 
105 {
106 }
107
108 const struct model_ops single_ops = {
109         .name                   = "single",
110         .model_init             = single_model_init,
111         .new_task               = single_new_task,
112         .accept_connection      = single_accept_connection,
113         .terminate              = single_terminate,
114         .set_title              = single_set_title,
115 };
116
117 /*
118   initialise the single process model, registering ourselves with the
119   process model subsystem
120  */
121 NTSTATUS process_model_single_init(void)
122 {
123         return register_process_model(&single_ops);
124 }