r15356: Remove unused 'flags' argument from socket_send() and friends.
[bbaumbach/samba-autobuild/.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 "lib/socket/socket.h"
29 #include "smbd/process_model.h"
30
31 /* For specifiying event context to GSSAPI below */
32 #include "system/kerberos.h"
33 #include "heimdal/lib/gssapi/gssapi_locl.h"
34
35 #include "passdb/secrets.h"
36
37 #ifdef HAVE_SETPROCTITLE
38 #ifdef HAVE_SETPROCTITLE_H
39 #include <setproctitle.h>
40 #endif
41 #else
42 #define setproctitle none_setproctitle
43 static int none_setproctitle(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);
44 static int none_setproctitle(const char *fmt, ...)
45 {
46         return 0;
47 }
48 #endif
49
50 /*
51   called when the process model is selected
52 */
53 static void standard_model_init(struct event_context *ev)
54 {
55         signal(SIGCHLD, SIG_IGN);
56 }
57
58 /*
59   called when a listening socket becomes readable. 
60 */
61 static void standard_accept_connection(struct event_context *ev, 
62                                        struct socket_context *sock, 
63                                        void (*new_conn)(struct event_context *, struct socket_context *, 
64                                                         uint32_t , void *), 
65                                        void *private)
66 {
67         NTSTATUS status;
68         struct socket_context *sock2;
69         pid_t pid;
70         struct event_context *ev2;
71         struct socket_address *c, *s;
72
73         /* accept an incoming connection. */
74         status = socket_accept(sock, &sock2);
75         if (!NT_STATUS_IS_OK(status)) {
76                 DEBUG(0,("standard_accept_connection: accept: %s\n",
77                          nt_errstr(status)));
78                 /* this looks strange, but is correct. We need to throttle things until
79                    the system clears enough resources to handle this new socket */
80                 sleep(1);
81                 return;
82         }
83
84         pid = fork();
85
86         if (pid != 0) {
87                 /* parent or error code ... */
88                 talloc_free(sock2);
89                 /* go back to the event loop */
90                 return;
91         }
92
93         pid = getpid();
94
95         /* This is now the child code. We need a completely new event_context to work with */
96         ev2 = event_context_init(NULL);
97
98         /* the service has given us a private pointer that
99            encapsulates the context it needs for this new connection -
100            everything else will be freed */
101         talloc_steal(ev2, private);
102         talloc_steal(private, sock2);
103
104         /* this will free all the listening sockets and all state that
105            is not associated with this new connection */
106         talloc_free(sock);
107         talloc_free(ev);
108
109         /* we don't care if the dup fails, as its only a select()
110            speed optimisation */
111         socket_dup(sock2);
112                         
113         /* tdb needs special fork handling */
114         if (tdb_reopen_all(1) == -1) {
115                 DEBUG(0,("standard_accept_connection: tdb_reopen_all failed.\n"));
116         }
117
118         /* Hack to ensure that GSSAPI uses the right event context */
119         gssapi_krb5_init_ev(ev2);
120
121         /* Ensure that the forked children do not expose identical random streams */
122         set_need_random_reseed();
123
124         /* setup the process title */
125         c = socket_get_peer_addr(sock2, ev2);
126         s = socket_get_my_addr(sock2, ev2);
127         if (s && c) {
128                 setproctitle("conn c[%s:%u] s[%s:%u] server_id[%d]",
129                              c->addr, c->port, s->addr, s->port, pid);
130         }
131         talloc_free(c);
132         talloc_free(s);
133
134         /* setup this new connection */
135         new_conn(ev2, sock2, pid, private);
136
137         /* we can't return to the top level here, as that event context is gone,
138            so we now process events in the new event context until there are no
139            more to process */      
140         event_loop_wait(ev2);
141
142         talloc_free(ev2);
143         exit(0);
144 }
145
146 /*
147   called to create a new server task
148 */
149 static void standard_new_task(struct event_context *ev, 
150                               void (*new_task)(struct event_context *, uint32_t , void *), 
151                               void *private)
152 {
153         pid_t pid;
154         struct event_context *ev2;
155
156         pid = fork();
157
158         if (pid != 0) {
159                 /* parent or error code ... go back to the event loop */
160                 return;
161         }
162
163         pid = getpid();
164
165         /* This is now the child code. We need a completely new event_context to work with */
166         ev2 = event_context_init(NULL);
167
168         /* the service has given us a private pointer that
169            encapsulates the context it needs for this new connection -
170            everything else will be freed */
171         talloc_steal(ev2, private);
172
173         /* this will free all the listening sockets and all state that
174            is not associated with this new connection */
175         talloc_free(ev);
176
177         /* tdb needs special fork handling */
178         if (tdb_reopen_all(1) == -1) {
179                 DEBUG(0,("standard_accept_connection: tdb_reopen_all failed.\n"));
180         }
181
182         /* Ensure that the forked children do not expose identical random streams */
183         set_need_random_reseed();
184
185         setproctitle("task server_id[%d]", pid);
186
187         /* setup this new connection */
188         new_task(ev2, pid, private);
189
190         /* we can't return to the top level here, as that event context is gone,
191            so we now process events in the new event context until there are no
192            more to process */      
193         event_loop_wait(ev2);
194
195         talloc_free(ev2);
196         exit(0);
197 }
198
199
200 /* called when a task goes down */
201 static void standard_terminate(struct event_context *ev, const char *reason) 
202 {
203         DEBUG(2,("standard_terminate: reason[%s]\n",reason));
204
205         /* this init_iconv() has the effect of freeing the iconv context memory,
206            which makes leak checking easier */
207         init_iconv();
208
209         /* the secrets db should really hang off the connection structure */
210         secrets_shutdown();
211
212         talloc_free(ev);
213
214         /* terminate this process */
215         exit(0);
216 }
217
218 /* called to set a title of a task or connection */
219 static void standard_set_title(struct event_context *ev, const char *title) 
220 {
221         if (title) {
222                 setproctitle("%s", title);
223         } else {
224                 setproctitle(NULL);
225         }
226 }
227
228 static const struct model_ops standard_ops = {
229         .name                   = "standard",
230         .model_init             = standard_model_init,
231         .accept_connection      = standard_accept_connection,
232         .new_task               = standard_new_task,
233         .terminate              = standard_terminate,
234         .set_title              = standard_set_title,
235 };
236
237 /*
238   initialise the standard process model, registering ourselves with the process model subsystem
239  */
240 NTSTATUS process_model_standard_init(void)
241 {
242         return register_process_model(&standard_ops);
243 }