process_standard: Move child pipe setup further down standard_accept_connection()
[metze/samba/wip.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 3 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, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "lib/events/events.h"
26 #include "smbd/process_model.h"
27 #include "system/filesys.h"
28 #include "cluster/cluster.h"
29 #include "param/param.h"
30 #include "ldb_wrap.h"
31 #include "lib/messaging/messaging.h"
32 #include "lib/util/debug.h"
33 #include "source3/lib/messages_dgm.h"
34
35 struct standard_child_state {
36         const char *name;
37         pid_t pid;
38         int to_parent_fd;
39         int from_child_fd;
40         struct tevent_fd *from_child_fde;
41 };
42
43 NTSTATUS process_model_standard_init(TALLOC_CTX *);
44 struct process_context {
45         char *name;
46         int from_parent_fd;
47         bool inhibit_fork_on_accept;
48         bool forked_on_accept;
49 };
50
51 /*
52   called when the process model is selected
53 */
54 static void standard_model_init(void)
55 {
56 }
57
58 static void sighup_signal_handler(struct tevent_context *ev,
59                                 struct tevent_signal *se,
60                                 int signum, int count, void *siginfo,
61                                 void *private_data)
62 {
63         debug_schedule_reopen_logs();
64 }
65
66 static void sigterm_signal_handler(struct tevent_context *ev,
67                                 struct tevent_signal *se,
68                                 int signum, int count, void *siginfo,
69                                 void *private_data)
70 {
71 #if HAVE_GETPGRP
72         if (getpgrp() == getpid()) {
73                 /*
74                  * We're the process group leader, send
75                  * SIGTERM to our process group.
76                  */
77                 DEBUG(0,("SIGTERM: killing children\n"));
78                 kill(-getpgrp(), SIGTERM);
79         }
80 #endif
81         DEBUG(0,("Exiting pid %u on SIGTERM\n", (unsigned int)getpid()));
82         talloc_free(ev);
83         exit(127);
84 }
85
86 /*
87   handle EOF on the parent-to-all-children pipe in the child
88 */
89 static void standard_pipe_handler(struct tevent_context *event_ctx, struct tevent_fd *fde, 
90                                   uint16_t flags, void *private_data)
91 {
92         DEBUG(10,("Child %d exiting\n", (int)getpid()));
93         talloc_free(event_ctx);
94         exit(0);
95 }
96
97 /*
98   handle EOF on the child pipe in the parent, so we know when a
99   process terminates without using SIGCHLD or waiting on all possible pids.
100
101   We need to ensure we do not ignore SIGCHLD because we need it to
102   work to get a valid error code from samba_runcmd_*().
103  */
104 static void standard_child_pipe_handler(struct tevent_context *ev,
105                                         struct tevent_fd *fde,
106                                         uint16_t flags,
107                                         void *private_data)
108 {
109         struct standard_child_state *state
110                 = talloc_get_type_abort(private_data, struct standard_child_state);
111         int status = 0;
112         pid_t pid;
113
114         messaging_dgm_cleanup(state->pid);
115
116         /* the child has closed the pipe, assume its dead */
117         errno = 0;
118         pid = waitpid(state->pid, &status, 0);
119
120         if (pid != state->pid) {
121                 if (errno == ECHILD) {
122                         /*
123                          * this happens when the
124                          * parent has set SIGCHLD to
125                          * SIG_IGN. In that case we
126                          * can only get error
127                          * information for the child
128                          * via its logging. We should
129                          * stop using SIG_IGN on
130                          * SIGCHLD in the standard
131                          * process model.
132                          */
133                         DEBUG(0, ("Error in waitpid() unexpectedly got ECHILD "
134                                   "for child %d (%s) - %s, someone has set SIGCHLD "
135                                   "to SIG_IGN!\n",
136                                   (int)state->pid, state->name,
137                                   strerror(errno)));
138                         TALLOC_FREE(state);
139                         return;
140                 }
141                 DEBUG(0, ("Error in waitpid() for child %d (%s) - %s \n",
142                           (int)state->pid, state->name, strerror(errno)));
143                 if (errno == 0) {
144                         errno = ECHILD;
145                 }
146                 TALLOC_FREE(state);
147                 return;
148         }
149         if (WIFEXITED(status)) {
150                 status = WEXITSTATUS(status);
151                 DEBUG(2, ("Child %d (%s) exited with status %d\n",
152                           (int)state->pid, state->name, status));
153         } else if (WIFSIGNALED(status)) {
154                 status = WTERMSIG(status);
155                 DEBUG(0, ("Child %d (%s) terminated with signal %d\n",
156                           (int)state->pid, state->name, status));
157         }
158         TALLOC_FREE(state);
159         return;
160 }
161
162 static struct standard_child_state *setup_standard_child_pipe(struct tevent_context *ev,
163                                                               const char *name)
164 {
165         struct standard_child_state *state;
166         int parent_child_pipe[2];
167         int ret;
168
169         /*
170          * Prepare a pipe to allow us to know when the child exits,
171          * because it will trigger a read event on this private
172          * pipe.
173          *
174          * We do all this before the accept and fork(), so we can
175          * clean up if it fails.
176          */
177         state = talloc_zero(ev, struct standard_child_state);
178         if (state == NULL) {
179                 return NULL;
180         }
181
182         if (name == NULL) {
183                 name = "";
184         }
185
186         state->name = talloc_strdup(state, name);
187         if (state->name == NULL) {
188                 TALLOC_FREE(state);
189                 return NULL;
190         }
191
192         ret = pipe(parent_child_pipe);
193         if (ret == -1) {
194                 DEBUG(0, ("Failed to create parent-child pipe to handle "
195                           "SIGCHLD to track new process for socket\n"));
196                 TALLOC_FREE(state);
197                 return NULL;
198         }
199
200         smb_set_close_on_exec(parent_child_pipe[0]);
201         smb_set_close_on_exec(parent_child_pipe[1]);
202
203         state->from_child_fd = parent_child_pipe[0];
204         state->to_parent_fd = parent_child_pipe[1];
205
206         /*
207          * The basic purpose of calling this handler is to ensure we
208          * call waitpid() and so avoid zombies (now that we no longer
209          * user SIGIGN on for SIGCHLD), but it also allows us to clean
210          * up other resources in the future.
211          */
212         state->from_child_fde = tevent_add_fd(ev, state,
213                                               state->from_child_fd,
214                                               TEVENT_FD_READ,
215                                               standard_child_pipe_handler,
216                                               state);
217         if (state->from_child_fde == NULL) {
218                 TALLOC_FREE(state);
219                 return NULL;
220         }
221         tevent_fd_set_auto_close(state->from_child_fde);
222
223         return state;
224 }
225
226 /*
227   called when a listening socket becomes readable. 
228 */
229 static void standard_accept_connection(
230                 struct tevent_context *ev,
231                 struct loadparm_context *lp_ctx,
232                 struct socket_context *sock,
233                 void (*new_conn)(struct tevent_context *,
234                                 struct loadparm_context *,
235                                 struct socket_context *,
236                                 struct server_id,
237                                 void *,
238                                 void *),
239                 void *private_data,
240                 void *process_context)
241 {
242         NTSTATUS status;
243         struct socket_context *sock2;
244         pid_t pid;
245         struct socket_address *c, *s;
246         struct standard_child_state *state;
247         struct tevent_fd *fde = NULL;
248         struct tevent_signal *se = NULL;
249         struct process_context *proc_ctx = NULL;
250
251
252         /* accept an incoming connection. */
253         status = socket_accept(sock, &sock2);
254         if (!NT_STATUS_IS_OK(status)) {
255                 DEBUG(0,("standard_accept_connection: accept: %s\n",
256                         nt_errstr(status)));
257                 /* this looks strange, but is correct. We need to throttle things until
258                    the system clears enough resources to handle this new socket */
259                 sleep(1);
260                 return;
261         }
262
263         proc_ctx = talloc_get_type_abort(process_context,
264                                          struct process_context);
265
266         state = setup_standard_child_pipe(ev, NULL);
267         if (state == NULL) {
268                 return;
269         }
270         pid = fork();
271
272         if (pid != 0) {
273                 close(state->to_parent_fd);
274                 state->to_parent_fd = -1;
275
276                 if (pid > 0) {
277                         state->pid = pid;
278                 } else {
279                         TALLOC_FREE(state);
280                 }
281
282                 /* parent or error code ... */
283                 talloc_free(sock2);
284                 /* go back to the event loop */
285                 return;
286         }
287
288         /* this leaves state->to_parent_fd open */
289         TALLOC_FREE(state);
290
291         /* Now in the child code so indicate that we forked
292          * so the terminate code knows what to do
293          */
294         proc_ctx->forked_on_accept = true;
295
296         pid = getpid();
297         setproctitle("task[%s] standard worker", proc_ctx->name);
298
299         /* This is now the child code. We need a completely new event_context to work with */
300
301         if (tevent_re_initialise(ev) != 0) {
302                 smb_panic("Failed to re-initialise tevent after fork");
303         }
304
305         /* this will free all the listening sockets and all state that
306            is not associated with this new connection */
307         talloc_free(sock);
308
309         /* we don't care if the dup fails, as its only a select()
310            speed optimisation */
311         socket_dup(sock2);
312                         
313         /* tdb needs special fork handling */
314         ldb_wrap_fork_hook();
315
316         /* Must be done after a fork() to reset messaging contexts. */
317         status = imessaging_reinit_all();
318         if (!NT_STATUS_IS_OK(status)) {
319                 smb_panic("Failed to re-initialise imessaging after fork");
320         }
321
322         fde = tevent_add_fd(ev, ev, proc_ctx->from_parent_fd, TEVENT_FD_READ,
323                       standard_pipe_handler, NULL);
324         if (fde == NULL) {
325                 smb_panic("Failed to add fd handler after fork");
326         }
327
328         se = tevent_add_signal(ev,
329                                 ev,
330                                 SIGHUP,
331                                 0,
332                                 sighup_signal_handler,
333                                 NULL);
334         if (se == NULL) {
335                 smb_panic("Failed to add SIGHUP handler after fork");
336         }
337
338         se = tevent_add_signal(ev,
339                                 ev,
340                                 SIGTERM,
341                                 0,
342                                 sigterm_signal_handler,
343                                 NULL);
344         if (se == NULL) {
345                 smb_panic("Failed to add SIGTERM handler after fork");
346         }
347
348         /* setup the process title */
349         c = socket_get_peer_addr(sock2, ev);
350         s = socket_get_my_addr(sock2, ev);
351         if (s && c) {
352                 setproctitle("conn c[%s:%u] s[%s:%u] server_id[%d]",
353                              c->addr, c->port, s->addr, s->port, (int)pid);
354         }
355         talloc_free(c);
356         talloc_free(s);
357
358         /* setup this new connection.  Cluster ID is PID based for this process model */
359         new_conn(ev, lp_ctx, sock2, cluster_id(pid, 0), private_data,
360                  process_context);
361
362         /* we can't return to the top level here, as that event context is gone,
363            so we now process events in the new event context until there are no
364            more to process */      
365         tevent_loop_wait(ev);
366
367         talloc_free(ev);
368         exit(0);
369 }
370
371 /*
372   called to create a new server task
373 */
374 static void standard_new_task(struct tevent_context *ev,
375                               struct loadparm_context *lp_ctx,
376                               const char *service_name,
377                               void (*new_task)(struct tevent_context *, struct loadparm_context *lp_ctx, struct server_id , void *, void *),
378                               void *private_data,
379                               const struct service_details *service_details,
380                               int from_parent_fd)
381 {
382         pid_t pid;
383         NTSTATUS status;
384         struct standard_child_state *state;
385         struct tevent_fd *fde = NULL;
386         struct tevent_signal *se = NULL;
387         struct process_context *proc_ctx = NULL;
388
389         state = setup_standard_child_pipe(ev, service_name);
390         if (state == NULL) {
391                 return;
392         }
393
394         pid = fork();
395
396         if (pid != 0) {
397                 close(state->to_parent_fd);
398                 state->to_parent_fd = -1;
399
400                 if (pid > 0) {
401                         state->pid = pid;
402                 } else {
403                         TALLOC_FREE(state);
404                 }
405
406                 /* parent or error code ... go back to the event loop */
407                 return;
408         }
409
410         /* this leaves state->to_parent_fd open */
411         TALLOC_FREE(state);
412
413         pid = getpid();
414
415         /* this will free all the listening sockets and all state that
416            is not associated with this new connection */
417         if (tevent_re_initialise(ev) != 0) {
418                 smb_panic("Failed to re-initialise tevent after fork");
419         }
420
421         /* ldb/tdb need special fork handling */
422         ldb_wrap_fork_hook();
423
424         /* Must be done after a fork() to reset messaging contexts. */
425         status = imessaging_reinit_all();
426         if (!NT_STATUS_IS_OK(status)) {
427                 smb_panic("Failed to re-initialise imessaging after fork");
428         }
429
430         fde = tevent_add_fd(ev, ev, from_parent_fd, TEVENT_FD_READ,
431                       standard_pipe_handler, NULL);
432         if (fde == NULL) {
433                 smb_panic("Failed to add fd handler after fork");
434         }
435
436         se = tevent_add_signal(ev,
437                                 ev,
438                                 SIGHUP,
439                                 0,
440                                 sighup_signal_handler,
441                                 NULL);
442         if (se == NULL) {
443                 smb_panic("Failed to add SIGHUP handler after fork");
444         }
445
446         se = tevent_add_signal(ev,
447                                 ev,
448                                 SIGTERM,
449                                 0,
450                                 sigterm_signal_handler,
451                                 NULL);
452         if (se == NULL) {
453                 smb_panic("Failed to add SIGTERM handler after fork");
454         }
455
456         setproctitle("task[%s]", service_name);
457
458         /*
459          * Set up the process context to be passed through to the terminate
460          * and accept_connection functions
461          */
462         proc_ctx = talloc(ev, struct process_context);
463         proc_ctx->name = talloc_strdup(ev, service_name);
464         proc_ctx->from_parent_fd = from_parent_fd;
465         proc_ctx->inhibit_fork_on_accept  =
466                 service_details->inhibit_fork_on_accept;
467         proc_ctx->forked_on_accept = false;
468
469         /* setup this new task.  Cluster ID is PID based for this process model */
470         new_task(ev, lp_ctx, cluster_id(pid, 0), private_data, proc_ctx);
471
472         /* we can't return to the top level here, as that event context is gone,
473            so we now process events in the new event context until there are no
474            more to process */
475         tevent_loop_wait(ev);
476
477         talloc_free(ev);
478         exit(0);
479 }
480
481
482 /* called when a task goes down */
483 static void standard_terminate(struct tevent_context *ev,
484                                struct loadparm_context *lp_ctx,
485                                const char *reason,
486                                void *process_context)
487 {
488         struct process_context *proc_ctx = NULL;
489
490         DBG_DEBUG("process terminating reason[%s]\n", reason);
491         if (process_context == NULL) {
492                 smb_panic("Panicking process_context is NULL");
493         }
494
495         proc_ctx = talloc_get_type(process_context, struct process_context);
496         if (proc_ctx->forked_on_accept == false) {
497                 /*
498                  * The current task was not forked on accept, so it needs to
499                  * keep running and process requests from other connections
500                  */
501                 return;
502         }
503         /*
504          * The current process was forked on accept to handle a single
505          * connection/request. That request has now finished and the process
506          * should terminate
507          */
508
509         /* this reload_charcnv() has the effect of freeing the iconv context memory,
510            which makes leak checking easier */
511         reload_charcnv(lp_ctx);
512
513         /* Always free event context last before exit. */
514         talloc_free(ev);
515
516         /* terminate this process */
517         exit(0);
518 }
519
520 /* called to set a title of a task or connection */
521 static void standard_set_title(struct tevent_context *ev, const char *title) 
522 {
523         if (title) {
524                 setproctitle("%s", title);
525         } else {
526                 setproctitle(NULL);
527         }
528 }
529
530 static const struct model_ops standard_ops = {
531         .name                   = "standard",
532         .model_init             = standard_model_init,
533         .accept_connection      = standard_accept_connection,
534         .new_task               = standard_new_task,
535         .terminate              = standard_terminate,
536         .set_title              = standard_set_title,
537 };
538
539 /*
540   initialise the standard process model, registering ourselves with the process model subsystem
541  */
542 NTSTATUS process_model_standard_init(TALLOC_CTX *ctx)
543 {
544         return register_process_model(&standard_ops);
545 }