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