5149ab55b676a0f768ee032cba1ddd2c2a5b7c35
[kamenim/samba.git] / source4 / smbd / process_standard.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    process model: standard (1 process per client connection)
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    
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.
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, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
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"
31
32 /* For sepecifiying event context to GSSAPI below */
33 #include "heimdal/lib/gssapi/gssapi_locl.h"
34
35 /*
36   called when the process model is selected
37 */
38 static void standard_model_init(struct event_context *ev)
39 {
40         signal(SIGCHLD, SIG_IGN);
41 }
42
43 /*
44   called when a listening socket becomes readable. 
45 */
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 *, 
49                                                         uint32_t , void *), 
50                                        void *private)
51 {
52         NTSTATUS status;
53         struct socket_context *sock2;
54         pid_t pid;
55         struct event_context *ev2;
56
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",
61                          nt_errstr(status)));
62                 /* this looks strange, but is correct. We need to throttle things until
63                    the system clears enough resources to handle this new socket */
64                 sleep(1);
65                 return;
66         }
67
68         pid = fork();
69
70         if (pid != 0) {
71                 /* parent or error code ... */
72                 talloc_free(sock2);
73                 /* go back to the event loop */
74                 return;
75         }
76
77         /* This is now the child code. We need a completely new event_context to work with */
78         ev2 = event_context_init(NULL);
79
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);
85
86         /* this will free all the listening sockets and all state that
87            is not associated with this new connection */
88         talloc_free(sock);
89         talloc_free(ev);
90
91         /* we don't care if the dup fails, as its only a select()
92            speed optimisation */
93         socket_dup(sock2);
94                         
95         /* tdb needs special fork handling */
96         if (tdb_reopen_all() == -1) {
97                 DEBUG(0,("standard_accept_connection: tdb_reopen_all failed.\n"));
98         }
99
100         /* Hack to ensure that GSSAPI uses the right event context */
101         gssapi_krb5_init_ev(ev2);
102
103         /* Ensure that the forked children do not expose identical random streams */
104         set_need_random_reseed();
105
106         /* setup this new connection */
107         new_conn(ev2, sock2, getpid(), private);
108
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
111            more to process */      
112         event_loop_wait(ev2);
113
114         talloc_free(ev2);
115         exit(0);
116 }
117
118 /*
119   called to create a new server task
120 */
121 static void standard_new_task(struct event_context *ev, 
122                               void (*new_task)(struct event_context *, uint32_t , void *), 
123                               void *private)
124 {
125         pid_t pid;
126         struct event_context *ev2;
127
128         pid = fork();
129
130         if (pid != 0) {
131                 /* parent or error code ... go back to the event loop */
132                 return;
133         }
134
135         /* This is now the child code. We need a completely new event_context to work with */
136         ev2 = event_context_init(NULL);
137
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);
142
143         /* this will free all the listening sockets and all state that
144            is not associated with this new connection */
145         talloc_free(ev);
146
147         /* tdb needs special fork handling */
148         if (tdb_reopen_all() == -1) {
149                 DEBUG(0,("standard_accept_connection: tdb_reopen_all failed.\n"));
150         }
151
152         /* Ensure that the forked children do not expose identical random streams */
153         set_need_random_reseed();
154
155         /* setup this new connection */
156         new_task(ev2, getpid(), private);
157
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
160            more to process */      
161         event_loop_wait(ev2);
162
163         talloc_free(ev2);
164         exit(0);
165 }
166
167
168 /* called when a task goes down */
169 static void standard_terminate(struct event_context *ev, const char *reason) 
170 {
171         DEBUG(2,("standard_terminate: reason[%s]\n",reason));
172
173         /* this init_iconv() has the effect of freeing the iconv context memory,
174            which makes leak checking easier */
175         init_iconv();
176
177         /* the secrets db should really hang off the connection structure */
178         secrets_shutdown();
179
180         talloc_free(ev);
181
182         /* terminate this process */
183         exit(0);
184 }
185
186
187 static const struct model_ops standard_ops = {
188         .name                   = "standard",
189         .model_init             = standard_model_init,
190         .accept_connection      = standard_accept_connection,
191         .new_task               = standard_new_task,
192         .terminate              = standard_terminate,
193 };
194
195 /*
196   initialise the standard process model, registering ourselves with the process model subsystem
197  */
198 NTSTATUS process_model_standard_init(void)
199 {
200         return register_process_model(&standard_ops);
201 }