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