2 Unix SMB/CIFS implementation.
4 process model: prefork (n client connections per process)
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>
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.
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.
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/>.
27 #include "lib/events/events.h"
28 #include "lib/socket/socket.h"
29 #include "smbd/process_model.h"
30 #include "param/secrets.h"
31 #include "system/filesys.h"
32 #include "cluster/cluster.h"
33 #include "param/param.h"
36 #ifdef HAVE_SETPROCTITLE
37 #ifdef HAVE_SETPROCTITLE_H
38 #include <setproctitle.h>
41 #define setproctitle none_setproctitle
42 static int none_setproctitle(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);
43 static int none_setproctitle(const char *fmt, ...)
50 called when the process model is selected
52 static void prefork_model_init(struct tevent_context *ev)
54 signal(SIGCHLD, SIG_IGN);
57 static void prefork_reload_after_fork(void)
61 /* Ensure that the forked children do not expose identical random streams */
62 set_need_random_reseed();
66 called when a listening socket becomes readable.
68 static void prefork_accept_connection(struct tevent_context *ev,
69 struct loadparm_context *lp_ctx,
70 struct socket_context *listen_socket,
71 void (*new_conn)(struct tevent_context *,
72 struct loadparm_context *, struct socket_context *,
73 struct server_id , void *),
77 struct socket_context *connected_socket;
80 /* accept an incoming connection. */
81 status = socket_accept(listen_socket, &connected_socket);
82 if (!NT_STATUS_IS_OK(status)) {
86 talloc_steal(private_data, connected_socket);
88 new_conn(ev, lp_ctx, connected_socket, cluster_id(pid, socket_get_fd(connected_socket)), private_data);
92 called to create a new server task
94 static void prefork_new_task(struct tevent_context *ev,
95 struct loadparm_context *lp_ctx,
96 const char *service_name,
97 void (*new_task_fn)(struct tevent_context *, struct loadparm_context *lp_ctx, struct server_id , void *),
103 struct tevent_context *ev2, *ev_parent;
108 /* parent or error code ... go back to the event loop */
114 /* This is now the child code. We need a completely new event_context to work with */
115 ev2 = s4_event_context_init(NULL);
117 /* the service has given us a private pointer that
118 encapsulates the context it needs for this new connection -
119 everything else will be freed */
120 talloc_steal(ev2, private_data);
122 /* this will free all the listening sockets and all state that
123 is not associated with this new connection */
126 setproctitle("task %s server_id[%d]", service_name, pid);
128 prefork_reload_after_fork();
130 /* setup this new connection: process will bind to it's sockets etc */
131 new_task_fn(ev2, lp_ctx, cluster_id(pid, 0), private_data);
133 num_children = lpcfg_parm_int(lp_ctx, NULL, "prefork children", service_name, 0);
134 if (num_children == 0) {
136 /* We don't want any kids hanging around for this one,
137 * let the parent do all the work */
138 event_loop_wait(ev2);
144 /* We are now free to spawn some child proccesses */
146 for (i=0; i < num_children; i++) {
151 } else if (pid == -1) {
155 setproctitle("task %s server_id[%d]", service_name, pid);
157 prefork_reload_after_fork();
159 /* we can't return to the top level here, as that event context is gone,
160 so we now process events in the new event context until there are no
162 event_loop_wait(ev2);
169 /* Don't listen on the sockets we just gave to the children */
172 /* But we need a events system to handle reaping children */
173 ev_parent = s4_event_context_init(NULL);
175 /* TODO: Handle some events... */
177 /* we can't return to the top level here, as that event context is gone,
178 so we now process events in the new event context until there are no
180 event_loop_wait(ev_parent);
182 talloc_free(ev_parent);
188 /* called when a task goes down */
189 static void prefork_terminate(struct tevent_context *ev, struct loadparm_context *lp_ctx, const char *reason)
191 DEBUG(2,("prefork_terminate: reason[%s]\n",reason));
194 /* called to set a title of a task or connection */
195 static void prefork_set_title(struct tevent_context *ev, const char *title)
198 setproctitle("%s", title);
204 static const struct model_ops prefork_ops = {
206 .model_init = prefork_model_init,
207 .accept_connection = prefork_accept_connection,
208 .new_task = prefork_new_task,
209 .terminate = prefork_terminate,
210 .set_title = prefork_set_title,
214 initialise the prefork process model, registering ourselves with the process model subsystem
216 NTSTATUS process_model_prefork_init(void)
218 return register_process_model(&prefork_ops);