2 Unix SMB/CIFS implementation.
4 process model: standard (1 process per client connection)
6 Copyright (C) Andrew Tridgell 1992-2005
7 Copyright (C) James J Myers 2003 <myersjj@samba.org>
8 Copyright (C) Stefan (metze) Metzmacher 2004
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 2 of the License, or
13 (at your option) any later version.
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.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "lib/events/events.h"
27 #include "lib/tdb/include/tdb.h"
28 #include "dlinklist.h"
29 #include "smb_server/smb_server.h"
30 #include "system/filesys.h"
32 /* For sepecifiying event context to GSSAPI below */
33 #include "heimdal/lib/gssapi/gssapi_locl.h"
36 called when the process model is selected
38 static void standard_model_init(struct event_context *ev)
40 signal(SIGCHLD, SIG_IGN);
44 called when a listening socket becomes readable.
46 static void standard_accept_connection(struct event_context *ev,
47 struct socket_context *sock,
48 void (*new_conn)(struct event_context *, struct socket_context *,
53 struct socket_context *sock2;
55 struct event_context *ev2;
57 /* accept an incoming connection. */
58 status = socket_accept(sock, &sock2);
59 if (!NT_STATUS_IS_OK(status)) {
60 DEBUG(0,("standard_accept_connection: accept: %s\n",
62 /* this looks strange, but is correct. We need to throttle things until
63 the system clears enough resources to handle this new socket */
71 /* parent or error code ... */
73 /* go back to the event loop */
77 /* This is now the child code. We need a completely new event_context to work with */
78 ev2 = event_context_init(NULL);
80 /* the service has given us a private pointer that
81 encapsulates the context it needs for this new connection -
82 everything else will be freed */
83 talloc_steal(ev2, private);
84 talloc_steal(private, sock2);
86 /* this will free all the listening sockets and all state that
87 is not associated with this new connection */
91 /* we don't care if the dup fails, as its only a select()
95 /* tdb needs special fork handling */
96 if (tdb_reopen_all() == -1) {
97 DEBUG(0,("standard_accept_connection: tdb_reopen_all failed.\n"));
100 /* Hack to ensure that GSSAPI uses the right event context */
101 gssapi_krb5_init_ev(ev2);
103 /* Ensure that the forked children do not expose identical random streams */
104 set_need_random_reseed();
106 /* setup this new connection */
107 new_conn(ev2, sock2, getpid(), private);
109 /* we can't return to the top level here, as that event context is gone,
110 so we now process events in the new event context until there are no
112 event_loop_wait(ev2);
119 called to create a new server task
121 static void standard_new_task(struct event_context *ev,
122 void (*new_task)(struct event_context *, uint32_t , void *),
126 struct event_context *ev2;
131 /* parent or error code ... go back to the event loop */
135 /* This is now the child code. We need a completely new event_context to work with */
136 ev2 = event_context_init(NULL);
138 /* the service has given us a private pointer that
139 encapsulates the context it needs for this new connection -
140 everything else will be freed */
141 talloc_steal(ev2, private);
143 /* this will free all the listening sockets and all state that
144 is not associated with this new connection */
147 /* tdb needs special fork handling */
148 if (tdb_reopen_all() == -1) {
149 DEBUG(0,("standard_accept_connection: tdb_reopen_all failed.\n"));
152 /* Ensure that the forked children do not expose identical random streams */
153 set_need_random_reseed();
155 /* setup this new connection */
156 new_task(ev2, getpid(), private);
158 /* we can't return to the top level here, as that event context is gone,
159 so we now process events in the new event context until there are no
161 event_loop_wait(ev2);
168 /* called when a task goes down */
169 static void standard_terminate(struct event_context *ev, const char *reason)
171 DEBUG(2,("standard_terminate: reason[%s]\n",reason));
173 /* this init_iconv() has the effect of freeing the iconv context memory,
174 which makes leak checking easier */
177 /* the secrets db should really hang off the connection structure */
182 /* terminate this process */
187 static const struct model_ops standard_ops = {
189 .model_init = standard_model_init,
190 .accept_connection = standard_accept_connection,
191 .new_task = standard_new_task,
192 .terminate = standard_terminate,
196 initialise the standard process model, registering ourselves with the process model subsystem
198 NTSTATUS process_model_standard_init(void)
200 return register_process_model(&standard_ops);