process_standard: Do not log at level 2 every time a child exits
[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 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                 if (status != 0) {
152                         DBG_ERR("Child %d (%s) exited with status %d\n",
153                                 (int)state->pid, state->name, status);
154                 }
155         } else if (WIFSIGNALED(status)) {
156                 status = WTERMSIG(status);
157                 DEBUG(0, ("Child %d (%s) terminated with signal %d\n",
158                           (int)state->pid, state->name, status));
159         }
160         TALLOC_FREE(state);
161         return;
162 }
163
164 static struct standard_child_state *setup_standard_child_pipe(struct tevent_context *ev,
165                                                               const char *name)
166 {
167         struct standard_child_state *state;
168         int parent_child_pipe[2];
169         int ret;
170
171         /*
172          * Prepare a pipe to allow us to know when the child exits,
173          * because it will trigger a read event on this private
174          * pipe.
175          *
176          * We do all this before the accept and fork(), so we can
177          * clean up if it fails.
178          */
179         state = talloc_zero(ev, struct standard_child_state);
180         if (state == NULL) {
181                 return NULL;
182         }
183
184         if (name == NULL) {
185                 name = "";
186         }
187
188         state->name = talloc_strdup(state, name);
189         if (state->name == NULL) {
190                 TALLOC_FREE(state);
191                 return NULL;
192         }
193
194         ret = pipe(parent_child_pipe);
195         if (ret == -1) {
196                 DEBUG(0, ("Failed to create parent-child pipe to handle "
197                           "SIGCHLD to track new process for socket\n"));
198                 TALLOC_FREE(state);
199                 return NULL;
200         }
201
202         smb_set_close_on_exec(parent_child_pipe[0]);
203         smb_set_close_on_exec(parent_child_pipe[1]);
204
205         state->from_child_fd = parent_child_pipe[0];
206         state->to_parent_fd = parent_child_pipe[1];
207
208         /*
209          * The basic purpose of calling this handler is to ensure we
210          * call waitpid() and so avoid zombies (now that we no longer
211          * user SIGIGN on for SIGCHLD), but it also allows us to clean
212          * up other resources in the future.
213          */
214         state->from_child_fde = tevent_add_fd(ev, state,
215                                               state->from_child_fd,
216                                               TEVENT_FD_READ,
217                                               standard_child_pipe_handler,
218                                               state);
219         if (state->from_child_fde == NULL) {
220                 TALLOC_FREE(state);
221                 return NULL;
222         }
223         tevent_fd_set_auto_close(state->from_child_fde);
224
225         return state;
226 }
227
228 /*
229   called when a listening socket becomes readable. 
230 */
231 static void standard_accept_connection(
232                 struct tevent_context *ev,
233                 struct loadparm_context *lp_ctx,
234                 struct socket_context *sock,
235                 void (*new_conn)(struct tevent_context *,
236                                 struct loadparm_context *,
237                                 struct socket_context *,
238                                 struct server_id,
239                                 void *,
240                                 void *),
241                 void *private_data,
242                 void *process_context)
243 {
244         NTSTATUS status;
245         struct socket_context *sock2;
246         pid_t pid;
247         struct socket_address *c, *s;
248         struct standard_child_state *state;
249         struct tevent_fd *fde = NULL;
250         struct tevent_signal *se = NULL;
251         struct process_context *proc_ctx = NULL;
252
253
254         /* accept an incoming connection. */
255         status = socket_accept(sock, &sock2);
256         if (!NT_STATUS_IS_OK(status)) {
257                 DEBUG(0,("standard_accept_connection: accept: %s\n",
258                         nt_errstr(status)));
259                 /* this looks strange, but is correct. We need to throttle things until
260                    the system clears enough resources to handle this new socket */
261                 sleep(1);
262                 return;
263         }
264
265         proc_ctx = talloc_get_type_abort(process_context,
266                                          struct process_context);
267
268         if (proc_ctx->inhibit_fork_on_accept) {
269                 pid = getpid();
270                 /*
271                  * Service does not support forking a new process on a
272                  * new connection, either it's maintaining shared
273                  * state or the overhead of forking a new process is a
274                  * significant fraction of the response time.
275                  */
276                 talloc_steal(private_data, sock2);
277                 new_conn(ev, lp_ctx, sock2,
278                          cluster_id(pid, socket_get_fd(sock2)), private_data,
279                          process_context);
280                 return;
281         }
282
283         state = setup_standard_child_pipe(ev, NULL);
284         if (state == NULL) {
285                 return;
286         }
287         pid = fork();
288
289         if (pid != 0) {
290                 close(state->to_parent_fd);
291                 state->to_parent_fd = -1;
292
293                 if (pid > 0) {
294                         state->pid = pid;
295                 } else {
296                         TALLOC_FREE(state);
297                 }
298
299                 /* parent or error code ... */
300                 talloc_free(sock2);
301                 /* go back to the event loop */
302                 return;
303         }
304
305         /* this leaves state->to_parent_fd open */
306         TALLOC_FREE(state);
307
308         /* Now in the child code so indicate that we forked
309          * so the terminate code knows what to do
310          */
311         proc_ctx->forked_on_accept = true;
312
313         pid = getpid();
314         setproctitle("task[%s] standard worker", proc_ctx->name);
315
316         /* This is now the child code. We need a completely new event_context to work with */
317
318         if (tevent_re_initialise(ev) != 0) {
319                 smb_panic("Failed to re-initialise tevent after fork");
320         }
321
322         /* this will free all the listening sockets and all state that
323            is not associated with this new connection */
324         talloc_free(sock);
325
326         /* we don't care if the dup fails, as its only a select()
327            speed optimisation */
328         socket_dup(sock2);
329                         
330         /* tdb needs special fork handling */
331         ldb_wrap_fork_hook();
332
333         /* Must be done after a fork() to reset messaging contexts. */
334         status = imessaging_reinit_all();
335         if (!NT_STATUS_IS_OK(status)) {
336                 smb_panic("Failed to re-initialise imessaging after fork");
337         }
338
339         fde = tevent_add_fd(ev, ev, proc_ctx->from_parent_fd, TEVENT_FD_READ,
340                       standard_pipe_handler, NULL);
341         if (fde == NULL) {
342                 smb_panic("Failed to add fd handler after fork");
343         }
344
345         se = tevent_add_signal(ev,
346                                 ev,
347                                 SIGHUP,
348                                 0,
349                                 sighup_signal_handler,
350                                 NULL);
351         if (se == NULL) {
352                 smb_panic("Failed to add SIGHUP handler after fork");
353         }
354
355         se = tevent_add_signal(ev,
356                                 ev,
357                                 SIGTERM,
358                                 0,
359                                 sigterm_signal_handler,
360                                 NULL);
361         if (se == NULL) {
362                 smb_panic("Failed to add SIGTERM handler after fork");
363         }
364
365         /* setup the process title */
366         c = socket_get_peer_addr(sock2, ev);
367         s = socket_get_my_addr(sock2, ev);
368         if (s && c) {
369                 setproctitle("conn c[%s:%u] s[%s:%u] server_id[%d]",
370                              c->addr, c->port, s->addr, s->port, (int)pid);
371         }
372         talloc_free(c);
373         talloc_free(s);
374
375         /* setup this new connection.  Cluster ID is PID based for this process model */
376         new_conn(ev, lp_ctx, sock2, cluster_id(pid, 0), private_data,
377                  process_context);
378
379         /* we can't return to the top level here, as that event context is gone,
380            so we now process events in the new event context until there are no
381            more to process */      
382         tevent_loop_wait(ev);
383
384         talloc_free(ev);
385         exit(0);
386 }
387
388 /*
389   called to create a new server task
390 */
391 static void standard_new_task(struct tevent_context *ev,
392                               struct loadparm_context *lp_ctx,
393                               const char *service_name,
394                               void (*new_task)(struct tevent_context *, struct loadparm_context *lp_ctx, struct server_id , void *, void *),
395                               void *private_data,
396                               const struct service_details *service_details,
397                               int from_parent_fd)
398 {
399         pid_t pid;
400         NTSTATUS status;
401         struct standard_child_state *state;
402         struct tevent_fd *fde = NULL;
403         struct tevent_signal *se = NULL;
404         struct process_context *proc_ctx = NULL;
405
406         state = setup_standard_child_pipe(ev, service_name);
407         if (state == NULL) {
408                 return;
409         }
410
411         pid = fork();
412
413         if (pid != 0) {
414                 close(state->to_parent_fd);
415                 state->to_parent_fd = -1;
416
417                 if (pid > 0) {
418                         state->pid = pid;
419                 } else {
420                         TALLOC_FREE(state);
421                 }
422
423                 /* parent or error code ... go back to the event loop */
424                 return;
425         }
426
427         /* this leaves state->to_parent_fd open */
428         TALLOC_FREE(state);
429
430         pid = getpid();
431
432         /* this will free all the listening sockets and all state that
433            is not associated with this new connection */
434         if (tevent_re_initialise(ev) != 0) {
435                 smb_panic("Failed to re-initialise tevent after fork");
436         }
437
438         /* ldb/tdb need special fork handling */
439         ldb_wrap_fork_hook();
440
441         /* Must be done after a fork() to reset messaging contexts. */
442         status = imessaging_reinit_all();
443         if (!NT_STATUS_IS_OK(status)) {
444                 smb_panic("Failed to re-initialise imessaging after fork");
445         }
446
447         fde = tevent_add_fd(ev, ev, from_parent_fd, TEVENT_FD_READ,
448                       standard_pipe_handler, NULL);
449         if (fde == NULL) {
450                 smb_panic("Failed to add fd handler after fork");
451         }
452
453         se = tevent_add_signal(ev,
454                                 ev,
455                                 SIGHUP,
456                                 0,
457                                 sighup_signal_handler,
458                                 NULL);
459         if (se == NULL) {
460                 smb_panic("Failed to add SIGHUP handler after fork");
461         }
462
463         se = tevent_add_signal(ev,
464                                 ev,
465                                 SIGTERM,
466                                 0,
467                                 sigterm_signal_handler,
468                                 NULL);
469         if (se == NULL) {
470                 smb_panic("Failed to add SIGTERM handler after fork");
471         }
472
473         setproctitle("task[%s]", service_name);
474
475         /*
476          * Set up the process context to be passed through to the terminate
477          * and accept_connection functions
478          */
479         proc_ctx = talloc(ev, struct process_context);
480         proc_ctx->name = talloc_strdup(ev, service_name);
481         proc_ctx->from_parent_fd = from_parent_fd;
482         proc_ctx->inhibit_fork_on_accept  =
483                 service_details->inhibit_fork_on_accept;
484         proc_ctx->forked_on_accept = false;
485
486         /* setup this new task.  Cluster ID is PID based for this process model */
487         new_task(ev, lp_ctx, cluster_id(pid, 0), private_data, proc_ctx);
488
489         /* we can't return to the top level here, as that event context is gone,
490            so we now process events in the new event context until there are no
491            more to process */
492         tevent_loop_wait(ev);
493
494         talloc_free(ev);
495         exit(0);
496 }
497
498
499 /* called when a task goes down */
500 static void standard_terminate(struct tevent_context *ev,
501                                struct loadparm_context *lp_ctx,
502                                const char *reason,
503                                void *process_context)
504 {
505         struct process_context *proc_ctx = NULL;
506
507         DBG_DEBUG("process terminating reason[%s]\n", reason);
508         if (process_context == NULL) {
509                 smb_panic("Panicking process_context is NULL");
510         }
511
512         proc_ctx = talloc_get_type(process_context, struct process_context);
513         if (proc_ctx->forked_on_accept == false) {
514                 /*
515                  * The current task was not forked on accept, so it needs to
516                  * keep running and process requests from other connections
517                  */
518                 return;
519         }
520         /*
521          * The current process was forked on accept to handle a single
522          * connection/request. That request has now finished and the process
523          * should terminate
524          */
525
526         /* this reload_charcnv() has the effect of freeing the iconv context memory,
527            which makes leak checking easier */
528         reload_charcnv(lp_ctx);
529
530         /* Always free event context last before exit. */
531         talloc_free(ev);
532
533         /* terminate this process */
534         exit(0);
535 }
536
537 /* called to set a title of a task or connection */
538 static void standard_set_title(struct tevent_context *ev, const char *title) 
539 {
540         if (title) {
541                 setproctitle("%s", title);
542         } else {
543                 setproctitle(NULL);
544         }
545 }
546
547 static const struct model_ops standard_ops = {
548         .name                   = "standard",
549         .model_init             = standard_model_init,
550         .accept_connection      = standard_accept_connection,
551         .new_task               = standard_new_task,
552         .terminate              = standard_terminate,
553         .set_title              = standard_set_title,
554 };
555
556 /*
557   initialise the standard process model, registering ourselves with the process model subsystem
558  */
559 NTSTATUS process_model_standard_init(TALLOC_CTX *ctx)
560 {
561         return register_process_model(&standard_ops);
562 }