s4:libcli:smb2: add the smb2_capabilities to the smbcli_options
[kai/samba-autobuild/.git] / source4 / smbd / process_prefork.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    process model: prefork (n client connections per process)
5
6    Copyright (C) Andrew Tridgell 1992-2005
7    Copyright (C) James J Myers 2003 <myersjj@samba.org>
8    Copyright (C) Stefan (metze) Metzmacher 2004
9    Copyright (C) Andrew Bartlett 2008 <abartlet@samba.org>
10    Copyright (C) David Disseldorp 2008 <ddiss@sgi.com>
11    
12    This program is free software; you can redistribute it and/or modify
13    it under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 3 of the License, or
15    (at your option) any later version.
16    
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21    
22    You should have received a copy of the GNU General Public License
23    along with this program.  If not, see <http://www.gnu.org/licenses/>.
24 */
25
26 #include "includes.h"
27 #include "lib/events/events.h"
28 #include "lib/socket/socket.h"
29 #include "smbd/process_model.h"
30 #include "system/filesys.h"
31 #include "cluster/cluster.h"
32 #include "param/param.h"
33 #include "ldb_wrap.h"
34
35 NTSTATUS process_model_prefork_init(void);
36
37 /*
38   called when the process model is selected
39 */
40 static void prefork_model_init(void)
41 {
42         signal(SIGCHLD, SIG_IGN);
43 }
44
45 static void prefork_reload_after_fork(void)
46 {
47         ldb_wrap_fork_hook();
48
49         /* Ensure that the forked children do not expose identical random streams */
50         set_need_random_reseed();
51 }
52
53 /*
54   called when a listening socket becomes readable. 
55 */
56 static void prefork_accept_connection(struct tevent_context *ev, 
57                                       struct loadparm_context *lp_ctx,
58                                       struct socket_context *listen_socket,
59                                        void (*new_conn)(struct tevent_context *,
60                                                         struct loadparm_context *, struct socket_context *, 
61                                                         struct server_id , void *), 
62                                        void *private_data)
63 {
64         NTSTATUS status;
65         struct socket_context *connected_socket;
66         pid_t pid = getpid();
67
68         /* accept an incoming connection. */
69         status = socket_accept(listen_socket, &connected_socket);
70         if (!NT_STATUS_IS_OK(status)) {
71                 return;
72         }
73
74         talloc_steal(private_data, connected_socket);
75
76         new_conn(ev, lp_ctx, connected_socket, cluster_id(pid, socket_get_fd(connected_socket)), private_data);
77 }
78
79 /*
80   called to create a new server task
81 */
82 static void prefork_new_task(struct tevent_context *ev, 
83                              struct loadparm_context *lp_ctx,
84                              const char *service_name,
85                              void (*new_task_fn)(struct tevent_context *, struct loadparm_context *lp_ctx, struct server_id , void *),
86                              void *private_data)
87 {
88         pid_t pid;
89         int i, num_children;
90
91         struct tevent_context *ev2, *ev_parent;
92
93         pid = fork();
94
95         if (pid != 0) {
96                 /* parent or error code ... go back to the event loop */
97                 return;
98         }
99
100         pid = getpid();
101
102         /* This is now the child code. We need a completely new event_context to work with */
103         ev2 = s4_event_context_init(NULL);
104
105         /* the service has given us a private pointer that
106            encapsulates the context it needs for this new connection -
107            everything else will be freed */
108         talloc_steal(ev2, private_data);
109
110         /* this will free all the listening sockets and all state that
111            is not associated with this new connection */
112         talloc_free(ev);
113
114         setproctitle("task %s server_id[%d]", service_name, (int)pid);
115
116         prefork_reload_after_fork();
117
118         /* setup this new connection: process will bind to it's sockets etc */
119         new_task_fn(ev2, lp_ctx, cluster_id(pid, 0), private_data);
120
121         num_children = lpcfg_parm_int(lp_ctx, NULL, "prefork children", service_name, 0);
122         if (num_children == 0) {
123
124                 /* We don't want any kids hanging around for this one,
125                  * let the parent do all the work */
126                 tevent_loop_wait(ev2);
127                 
128                 talloc_free(ev2);
129                 exit(0);
130         }
131
132         /* We are now free to spawn some child proccesses */
133
134         for (i=0; i < num_children; i++) {
135
136                 pid = fork();
137                 if (pid > 0) {
138                         continue;
139                 } else if (pid == -1) {
140                         return;
141                 } else {
142                         pid = getpid();
143                         setproctitle("task %s server_id[%d]", service_name, (int)pid);
144
145                         prefork_reload_after_fork();
146
147                         /* we can't return to the top level here, as that event context is gone,
148                            so we now process events in the new event context until there are no
149                            more to process */      
150                         tevent_loop_wait(ev2);
151                         
152                         talloc_free(ev2);
153                         exit(0);
154                 }
155         }
156
157         /* Don't listen on the sockets we just gave to the children */
158         talloc_free(ev2);
159         
160         /* But we need a events system to handle reaping children */
161         ev_parent = s4_event_context_init(NULL);
162
163         /* TODO: Handle some events... */
164         
165         /* we can't return to the top level here, as that event context is gone,
166            so we now process events in the new event context until there are no
167            more to process */      
168         tevent_loop_wait(ev_parent);
169         
170         talloc_free(ev_parent);
171         exit(0);
172
173 }
174
175
176 /* called when a task goes down */
177 static void prefork_terminate(struct tevent_context *ev, struct loadparm_context *lp_ctx, const char *reason)
178 {
179         DEBUG(2,("prefork_terminate: reason[%s]\n",reason));
180 }
181
182 /* called to set a title of a task or connection */
183 static void prefork_set_title(struct tevent_context *ev, const char *title) 
184 {
185         if (title) {
186                 setproctitle("%s", title);
187         } else {
188                 setproctitle(NULL);
189         }
190 }
191
192 static const struct model_ops prefork_ops = {
193         .name                   = "prefork",
194         .model_init             = prefork_model_init,
195         .accept_connection      = prefork_accept_connection,
196         .new_task               = prefork_new_task,
197         .terminate              = prefork_terminate,
198         .set_title              = prefork_set_title,
199 };
200
201 /*
202   initialise the prefork process model, registering ourselves with the process model subsystem
203  */
204 NTSTATUS process_model_prefork_init(void)
205 {
206         return register_process_model(&prefork_ops);
207 }