lib: modules: Change XXX_init interface from XXX_init(void) to XXX_init(TALLOC_CTX *)
[nivanova/samba-autobuild/.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 NTSTATUS process_model_single_init(TALLOC_CTX *);
30
31 /*
32   called when the process model is selected
33 */
34 static void single_model_init(void)
35 {
36 }
37
38 /*
39   called when a listening socket becomes readable. 
40 */
41 static void single_accept_connection(struct tevent_context *ev, 
42                                      struct loadparm_context *lp_ctx,
43                                      struct socket_context *listen_socket,
44                                      void (*new_conn)(struct tevent_context *, 
45                                                       struct loadparm_context *,
46                                                       struct socket_context *, 
47                                                       struct server_id , void *), 
48                                      void *private_data)
49 {
50         NTSTATUS status;
51         struct socket_context *connected_socket;
52         pid_t pid = getpid();
53
54         /* accept an incoming connection. */
55         status = socket_accept(listen_socket, &connected_socket);
56         if (!NT_STATUS_IS_OK(status)) {
57                 DEBUG(0,("single_accept_connection: accept: %s\n", nt_errstr(status)));
58                 /* this looks strange, but is correct. 
59
60                    We can only be here if woken up from select, due to
61                    an incoming connection.
62
63                    We need to throttle things until the system clears
64                    enough resources to handle this new socket. 
65
66                    If we don't then we will spin filling the log and
67                    causing more problems. We don't panic as this is
68                    probably a temporary resource constraint */
69                 sleep(1);
70                 return;
71         }
72
73         talloc_steal(private_data, connected_socket);
74
75         /*
76          * We use the PID so we cannot collide in with cluster ids
77          * generated in other single mode tasks, and, and won't
78          * collide with PIDs from process model standard because a the
79          * combination of pid/fd should be unique system-wide
80          */
81         new_conn(ev, lp_ctx, connected_socket,
82                  cluster_id(pid, socket_get_fd(connected_socket)), private_data);
83 }
84
85 /*
86   called to startup a new task
87 */
88 static void single_new_task(struct tevent_context *ev, 
89                             struct loadparm_context *lp_ctx,
90                             const char *service_name,
91                             void (*new_task)(struct tevent_context *, struct loadparm_context *, struct server_id, void *), 
92                             void *private_data)
93 {
94         pid_t pid = getpid();
95         /* start our taskids at MAX_INT32, the first 2^31 tasks are is reserved for fd numbers */
96         static uint32_t taskid = INT32_MAX;
97        
98         /*
99          * We use the PID so we cannot collide in with cluster ids
100          * generated in other single mode tasks, and, and won't
101          * collide with PIDs from process model starndard because a the
102          * combination of pid/task_id should be unique system-wide
103          *
104          * Using the pid unaltered makes debugging of which process
105          * owns the messaging socket easier.
106          */
107         new_task(ev, lp_ctx, cluster_id(pid, taskid++), private_data);
108 }
109
110
111 /* called when a task goes down */
112 static void single_terminate(struct tevent_context *ev, struct loadparm_context *lp_ctx, const char *reason)
113 {
114         DEBUG(3,("single_terminate: reason[%s]\n",reason));
115 }
116
117 /* called to set a title of a task or connection */
118 static void single_set_title(struct tevent_context *ev, const char *title) 
119 {
120 }
121
122 const struct model_ops single_ops = {
123         .name                   = "single",
124         .model_init             = single_model_init,
125         .new_task               = single_new_task,
126         .accept_connection      = single_accept_connection,
127         .terminate              = single_terminate,
128         .set_title              = single_set_title,
129 };
130
131 /*
132   initialise the single process model, registering ourselves with the
133   process model subsystem
134  */
135 NTSTATUS process_model_single_init(TALLOC_CTX *ctx)
136 {
137         return register_process_model(&single_ops);
138 }