Clarify nomaclature of socket names in process_single and process_prefork
[ira/wip.git] / source4 / smbd / process_prefork.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    process model: prefork (n client connections per process)
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    Copyright (C) Andrew Bartlett 2008 <abartlet@samba.org>
10    Copyright (C) David Disseldorp 2008 <ddiss@sgi.com>
11    
12    This program is free software; you can redistribute it and/or modify
13    it under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 3 of the License, or
15    (at your option) any later version.
16    
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21    
22    You should have received a copy of the GNU General Public License
23    along with this program.  If not, see <http://www.gnu.org/licenses/>.
24 */
25
26 #include "includes.h"
27 #include "lib/events/events.h"
28 #include "lib/tdb/include/tdb.h"
29 #include "lib/socket/socket.h"
30 #include "smbd/process_model.h"
31 #include "param/secrets.h"
32 #include "system/filesys.h"
33 #include "cluster/cluster.h"
34 #include "param/param.h"
35
36 #ifdef HAVE_SETPROCTITLE
37 #ifdef HAVE_SETPROCTITLE_H
38 #include <setproctitle.h>
39 #endif
40 #else
41 #define setproctitle none_setproctitle
42 static int none_setproctitle(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);
43 static int none_setproctitle(const char *fmt, ...)
44 {
45         return 0;
46 }
47 #endif
48
49 /*
50   called when the process model is selected
51 */
52 static void prefork_model_init(struct event_context *ev)
53 {
54         signal(SIGCHLD, SIG_IGN);
55 }
56
57 /*
58   called when a listening socket becomes readable. 
59 */
60 static void prefork_accept_connection(struct event_context *ev, 
61                                       struct loadparm_context *lp_ctx,
62                                       struct socket_context *listen_socket,
63                                        void (*new_conn)(struct event_context *,
64                                                         struct loadparm_context *, struct socket_context *, 
65                                                         struct server_id , void *), 
66                                        void *private)
67 {
68         NTSTATUS status;
69         struct socket_context *connected_socket;
70         pid_t pid = getpid();
71
72         /* accept an incoming connection. */
73         status = socket_accept(listen_socket, &connected_socket);
74         if (!NT_STATUS_IS_OK(status)) {
75                 return;
76         }
77
78         talloc_steal(private, connected_socket);
79
80         new_conn(ev, lp_ctx, connected_socket, cluster_id(pid, socket_get_fd(connected_socket)), private);
81 }
82
83 /*
84   called to create a new server task
85 */
86 static void prefork_new_task(struct event_context *ev, 
87                              struct loadparm_context *lp_ctx,
88                              const char *service_name,
89                              void (*new_task_fn)(struct event_context *, struct loadparm_context *lp_ctx, struct server_id , void *), 
90                              void *private)
91 {
92         pid_t pid;
93         int i, num_children;
94
95         struct event_context *ev2, *ev_parent;
96
97         pid = fork();
98
99         if (pid != 0) {
100                 /* parent or error code ... go back to the event loop */
101                 return;
102         }
103
104         pid = getpid();
105
106         /* This is now the child code. We need a completely new event_context to work with */
107         ev2 = event_context_init(NULL);
108
109         /* the service has given us a private pointer that
110            encapsulates the context it needs for this new connection -
111            everything else will be freed */
112         talloc_steal(ev2, private);
113
114         /* this will free all the listening sockets and all state that
115            is not associated with this new connection */
116         talloc_free(ev);
117
118         /* tdb needs special fork handling */
119         if (tdb_reopen_all(1) == -1) {
120                 DEBUG(0,("prefork_accept_connection: tdb_reopen_all failed.\n"));
121         }
122
123         /* Ensure that the forked children do not expose identical random streams */
124         set_need_random_reseed();
125
126         setproctitle("task %s server_id[%d]", service_name, pid);
127
128         /* setup this new connection: process will bind to it's sockets etc */
129         new_task_fn(ev2, lp_ctx, cluster_id(pid, 0), private);
130
131         num_children = lp_parm_int(lp_ctx, NULL, "prefork children", service_name, 0);
132         if (num_children == 0) {
133
134                 /* We don't want any kids hanging around for this one,
135                  * let the parent do all the work */
136                 event_loop_wait(ev2);
137                 
138                 talloc_free(ev2);
139                 exit(0);
140         }
141
142         /* We are now free to spawn some child proccesses */
143
144         for (i=0; i < num_children; i++) {
145
146                 pid = fork();
147                 if (pid > 0) {
148                         continue;
149                 } else if (pid == -1) {
150                         return;
151                 } else {
152                         pid = getpid();
153                         setproctitle("task %s server_id[%d]", service_name, pid);
154                         /* we can't return to the top level here, as that event context is gone,
155                            so we now process events in the new event context until there are no
156                            more to process */      
157                         event_loop_wait(ev2);
158                         
159                         talloc_free(ev2);
160                         exit(0);
161                 }
162         }
163
164         /* Don't listen on the sockets we just gave to the children */
165         talloc_free(ev2);
166         
167         /* But we need a events system to handle reaping children */
168         ev_parent = event_context_init(NULL);
169         
170         /* TODO: Handle some events... */
171         
172         /* we can't return to the top level here, as that event context is gone,
173            so we now process events in the new event context until there are no
174            more to process */      
175         event_loop_wait(ev_parent);
176         
177         talloc_free(ev_parent);
178         exit(0);
179
180 }
181
182
183 /* called when a task goes down */
184 _NORETURN_ static void prefork_terminate(struct event_context *ev, const char *reason) 
185 {
186         DEBUG(2,("prefork_terminate: reason[%s]\n",reason));
187 }
188
189 /* called to set a title of a task or connection */
190 static void prefork_set_title(struct event_context *ev, const char *title) 
191 {
192         if (title) {
193                 setproctitle("%s", title);
194         } else {
195                 setproctitle(NULL);
196         }
197 }
198
199 static const struct model_ops prefork_ops = {
200         .name                   = "prefork",
201         .model_init             = prefork_model_init,
202         .accept_connection      = prefork_accept_connection,
203         .new_task               = prefork_new_task,
204         .terminate              = prefork_terminate,
205         .set_title              = prefork_set_title,
206 };
207
208 /*
209   initialise the prefork process model, registering ourselves with the process model subsystem
210  */
211 NTSTATUS process_model_prefork_init(void)
212 {
213         return register_process_model(&prefork_ops);
214 }