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