2 Unix SMB/CIFS implementation.
4 process model: standard (1 process per client connection)
6 Copyright (C) Andrew Tridgell 1992-2005
7 Copyright (C) James J Myers 2003 <myersjj@samba.org>
8 Copyright (C) Stefan (metze) Metzmacher 2004
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.
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.
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/>.
25 #include "lib/events/events.h"
26 #include "samba/process_model.h"
27 #include "system/filesys.h"
28 #include "cluster/cluster.h"
29 #include "param/param.h"
31 #include "lib/messaging/messaging.h"
32 #include "lib/util/debug.h"
33 #include "lib/messaging/messages_dgm.h"
34 #include "lib/util/util_process.h"
36 static unsigned connections_active
= 0;
37 static unsigned smbd_max_processes
= 0;
39 struct standard_child_state
{
44 struct tevent_fd
*from_child_fde
;
47 NTSTATUS
process_model_standard_init(TALLOC_CTX
*);
48 struct process_context
{
51 bool inhibit_fork_on_accept
;
52 bool forked_on_accept
;
56 called when the process model is selected
58 static void standard_model_init(void)
62 static void sighup_signal_handler(struct tevent_context
*ev
,
63 struct tevent_signal
*se
,
64 int signum
, int count
, void *siginfo
,
67 debug_schedule_reopen_logs();
70 static void sigterm_signal_handler(struct tevent_context
*ev
,
71 struct tevent_signal
*se
,
72 int signum
, int count
, void *siginfo
,
76 if (getpgrp() == getpid()) {
78 * We're the process group leader, send
79 * SIGTERM to our process group.
81 DBG_ERR("SIGTERM: killing children\n");
82 kill(-getpgrp(), SIGTERM
);
85 DBG_ERR("Exiting pid %u on SIGTERM\n", (unsigned int)getpid());
91 handle EOF on the parent-to-all-children pipe in the child
93 static void standard_pipe_handler(struct tevent_context
*event_ctx
, struct tevent_fd
*fde
,
94 uint16_t flags
, void *private_data
)
96 DBG_DEBUG("Child %d exiting\n", (int)getpid());
97 talloc_free(event_ctx
);
102 handle EOF on the child pipe in the parent, so we know when a
103 process terminates without using SIGCHLD or waiting on all possible pids.
105 We need to ensure we do not ignore SIGCHLD because we need it to
106 work to get a valid error code from samba_runcmd_*().
108 static void standard_child_pipe_handler(struct tevent_context
*ev
,
109 struct tevent_fd
*fde
,
113 struct standard_child_state
*state
114 = talloc_get_type_abort(private_data
, struct standard_child_state
);
118 messaging_dgm_cleanup(state
->pid
);
120 /* the child has closed the pipe, assume its dead */
122 pid
= waitpid(state
->pid
, &status
, 0);
124 if (pid
!= state
->pid
) {
125 if (errno
== ECHILD
) {
127 * this happens when the
128 * parent has set SIGCHLD to
129 * SIG_IGN. In that case we
131 * information for the child
132 * via its logging. We should
133 * stop using SIG_IGN on
134 * SIGCHLD in the standard
137 DBG_ERR("Error in waitpid() unexpectedly got ECHILD "
138 "for child %d (%s) - %s, someone has set SIGCHLD "
140 (int)state
->pid
, state
->name
,
145 DBG_ERR("Error in waitpid() for child %d (%s) - %s \n",
146 (int)state
->pid
, state
->name
, strerror(errno
));
152 if (WIFEXITED(status
)) {
153 status
= WEXITSTATUS(status
);
155 DBG_ERR("Child %d (%s) exited with status %d\n",
156 (int)state
->pid
, state
->name
, status
);
158 } else if (WIFSIGNALED(status
)) {
159 status
= WTERMSIG(status
);
160 DBG_ERR("Child %d (%s) terminated with signal %d\n",
161 (int)state
->pid
, state
->name
, status
);
165 if (smbd_max_processes
> 0) {
166 if (connections_active
< 1) {
167 DBG_ERR("Number of active connections "
168 "less than 1 (%d)\n",
170 connections_active
= 1;
172 connections_active
--;
177 static struct standard_child_state
*setup_standard_child_pipe(struct tevent_context
*ev
,
180 struct standard_child_state
*state
;
181 int parent_child_pipe
[2];
185 * Prepare a pipe to allow us to know when the child exits,
186 * because it will trigger a read event on this private
189 * We do all this before the accept and fork(), so we can
190 * clean up if it fails.
192 state
= talloc_zero(ev
, struct standard_child_state
);
201 state
->name
= talloc_strdup(state
, name
);
202 if (state
->name
== NULL
) {
207 ret
= pipe(parent_child_pipe
);
209 DBG_ERR("Failed to create parent-child pipe to handle "
210 "SIGCHLD to track new process for socket\n");
215 smb_set_close_on_exec(parent_child_pipe
[0]);
216 smb_set_close_on_exec(parent_child_pipe
[1]);
218 state
->from_child_fd
= parent_child_pipe
[0];
219 state
->to_parent_fd
= parent_child_pipe
[1];
222 * The basic purpose of calling this handler is to ensure we
223 * call waitpid() and so avoid zombies (now that we no longer
224 * user SIGIGN on for SIGCHLD), but it also allows us to clean
225 * up other resources in the future.
227 state
->from_child_fde
= tevent_add_fd(ev
, state
,
228 state
->from_child_fd
,
230 standard_child_pipe_handler
,
232 if (state
->from_child_fde
== NULL
) {
236 tevent_fd_set_auto_close(state
->from_child_fde
);
242 called when a listening socket becomes readable.
244 static void standard_accept_connection(
245 struct tevent_context
*ev
,
246 struct loadparm_context
*lp_ctx
,
247 struct socket_context
*sock
,
248 void (*new_conn
)(struct tevent_context
*,
249 struct loadparm_context
*,
250 struct socket_context
*,
255 void *process_context
)
258 struct socket_context
*sock2
;
260 struct socket_address
*c
, *s
;
261 struct standard_child_state
*state
;
262 struct tevent_fd
*fde
= NULL
;
263 struct tevent_signal
*se
= NULL
;
264 struct process_context
*proc_ctx
= NULL
;
267 /* accept an incoming connection. */
268 status
= socket_accept(sock
, &sock2
);
269 if (!NT_STATUS_IS_OK(status
)) {
270 DBG_DEBUG("standard_accept_connection: accept: %s\n",
272 /* this looks strange, but is correct. We need to throttle
273 * things until the system clears enough resources to handle
280 proc_ctx
= talloc_get_type_abort(process_context
,
281 struct process_context
);
283 if (proc_ctx
->inhibit_fork_on_accept
) {
286 * Service does not support forking a new process on a
287 * new connection, either it's maintaining shared
288 * state or the overhead of forking a new process is a
289 * significant fraction of the response time.
291 talloc_steal(private_data
, sock2
);
292 new_conn(ev
, lp_ctx
, sock2
,
293 cluster_id(pid
, socket_get_fd(sock2
)), private_data
,
298 if (smbd_max_processes
> 0) {
299 if (connections_active
>= smbd_max_processes
) {
300 DBG_ERR("(%d) connections already active, "
301 "maximum is (%d). Dropping request\n",
305 * Drop the connection as we're overloaded at the moment
310 connections_active
++;
313 state
= setup_standard_child_pipe(ev
, NULL
);
320 close(state
->to_parent_fd
);
321 state
->to_parent_fd
= -1;
329 /* parent or error code ... */
331 /* go back to the event loop */
335 /* this leaves state->to_parent_fd open */
338 /* Now in the child code so indicate that we forked
339 * so the terminate code knows what to do
341 proc_ctx
->forked_on_accept
= true;
345 process_set_title("%s[work]", "task[%s] standard worker", proc_ctx
->name
);
347 /* This is now the child code. We need a completely new event_context to work with */
349 if (tevent_re_initialise(ev
) != 0) {
350 smb_panic("Failed to re-initialise tevent after fork");
353 /* this will free all the listening sockets and all state that
354 is not associated with this new connection */
357 /* we don't care if the dup fails, as its only a select()
358 speed optimisation */
361 /* tdb needs special fork handling */
362 ldb_wrap_fork_hook();
364 /* Must be done after a fork() to reset messaging contexts. */
365 status
= imessaging_reinit_all();
366 if (!NT_STATUS_IS_OK(status
)) {
367 smb_panic("Failed to re-initialise imessaging after fork");
370 fde
= tevent_add_fd(ev
, ev
, proc_ctx
->from_parent_fd
, TEVENT_FD_READ
,
371 standard_pipe_handler
, NULL
);
373 smb_panic("Failed to add fd handler after fork");
376 se
= tevent_add_signal(ev
,
380 sighup_signal_handler
,
383 smb_panic("Failed to add SIGHUP handler after fork");
386 se
= tevent_add_signal(ev
,
390 sigterm_signal_handler
,
393 smb_panic("Failed to add SIGTERM handler after fork");
396 /* setup the process title */
397 c
= socket_get_peer_addr(sock2
, ev
);
398 s
= socket_get_my_addr(sock2
, ev
);
400 setproctitle("conn c[%s:%u] s[%s:%u] server_id[%d]",
401 c
->addr
, c
->port
, s
->addr
, s
->port
, (int)pid
);
406 force_check_log_size();
408 /* setup this new connection. Cluster ID is PID based for this process model */
409 new_conn(ev
, lp_ctx
, sock2
, cluster_id(pid
, 0), private_data
,
412 /* we can't return to the top level here, as that event context is gone,
413 so we now process events in the new event context until there are no
415 tevent_loop_wait(ev
);
422 called to create a new server task
424 static void standard_new_task(struct tevent_context
*ev
,
425 struct loadparm_context
*lp_ctx
,
426 const char *service_name
,
427 struct task_server
*(*new_task
)(struct tevent_context
*, struct loadparm_context
*lp_ctx
, struct server_id
, void *, void *),
429 const struct service_details
*service_details
,
434 struct standard_child_state
*state
;
435 struct tevent_fd
*fde
= NULL
;
436 struct tevent_signal
*se
= NULL
;
437 struct process_context
*proc_ctx
= NULL
;
438 struct task_server
* task
= NULL
;
440 state
= setup_standard_child_pipe(ev
, service_name
);
448 close(state
->to_parent_fd
);
449 state
->to_parent_fd
= -1;
457 /* parent or error code ... go back to the event loop */
461 /* this leaves state->to_parent_fd open */
466 /* this will free all the listening sockets and all state that
467 is not associated with this new connection */
468 if (tevent_re_initialise(ev
) != 0) {
469 smb_panic("Failed to re-initialise tevent after fork");
472 /* ldb/tdb need special fork handling */
473 ldb_wrap_fork_hook();
475 /* Must be done after a fork() to reset messaging contexts. */
476 status
= imessaging_reinit_all();
477 if (!NT_STATUS_IS_OK(status
)) {
478 smb_panic("Failed to re-initialise imessaging after fork");
481 fde
= tevent_add_fd(ev
, ev
, from_parent_fd
, TEVENT_FD_READ
,
482 standard_pipe_handler
, NULL
);
484 smb_panic("Failed to add fd handler after fork");
487 se
= tevent_add_signal(ev
,
491 sighup_signal_handler
,
494 smb_panic("Failed to add SIGHUP handler after fork");
497 se
= tevent_add_signal(ev
,
501 sigterm_signal_handler
,
504 smb_panic("Failed to add SIGTERM handler after fork");
507 process_set_title("%s[task]", "task[%s]", service_name
);
509 force_check_log_size();
512 * Set up the process context to be passed through to the terminate
513 * and accept_connection functions
515 proc_ctx
= talloc(ev
, struct process_context
);
516 proc_ctx
->name
= talloc_strdup(ev
, service_name
);
517 proc_ctx
->from_parent_fd
= from_parent_fd
;
518 proc_ctx
->inhibit_fork_on_accept
=
519 service_details
->inhibit_fork_on_accept
;
520 proc_ctx
->forked_on_accept
= false;
522 smbd_max_processes
= lpcfg_max_smbd_processes(lp_ctx
);
524 /* setup this new task. Cluster ID is PID based for this process model */
525 task
= new_task(ev
, lp_ctx
, cluster_id(pid
, 0), private_data
, proc_ctx
);
527 * Currently we don't support the post_fork functionality in the
528 * standard model, i.e. it is only called here not after a new process
529 * is forked in standard_accept_connection.
531 if (task
!= NULL
&& service_details
->post_fork
!= NULL
) {
532 struct process_details pd
= initial_process_details
;
533 service_details
->post_fork(task
, &pd
);
536 if (task
!= NULL
&& service_details
->before_loop
!= NULL
) {
537 service_details
->before_loop(task
);
540 /* we can't return to the top level here, as that event context is gone,
541 so we now process events in the new event context until there are no
543 tevent_loop_wait(ev
);
550 /* called when a task goes down */
551 static void standard_terminate_task(struct tevent_context
*ev
,
552 struct loadparm_context
*lp_ctx
,
555 void *process_context
)
563 /* called when a connection terminates*/
564 static void standard_terminate_connection(struct tevent_context
*ev
,
565 struct loadparm_context
*lp_ctx
,
567 void *process_context
)
569 struct process_context
*proc_ctx
= NULL
;
571 DBG_DEBUG("connection terminating reason[%s]\n", reason
);
572 if (process_context
== NULL
) {
573 smb_panic("Panicking process_context is NULL");
576 proc_ctx
= talloc_get_type(process_context
, struct process_context
);
577 if (proc_ctx
->forked_on_accept
== false) {
579 * The current task was not forked on accept, so it needs to
580 * keep running and process requests from other connections
585 * The current process was forked on accept to handle a single
586 * connection/request. That request has now finished and the process
590 /* this reload_charcnv() has the effect of freeing the iconv context memory,
591 which makes leak checking easier */
592 reload_charcnv(lp_ctx
);
594 /* Always free event context last before exit. */
597 /* terminate this process */
600 /* called to set a title of a task or connection */
601 static void standard_set_title(struct tevent_context
*ev
, const char *title
)
604 setproctitle("%s", title
);
610 static const struct model_ops standard_ops
= {
612 .model_init
= standard_model_init
,
613 .accept_connection
= standard_accept_connection
,
614 .new_task
= standard_new_task
,
615 .terminate_task
= standard_terminate_task
,
616 .terminate_connection
= standard_terminate_connection
,
617 .set_title
= standard_set_title
,
621 initialise the standard process model, registering ourselves with the process model subsystem
623 NTSTATUS
process_model_standard_init(TALLOC_CTX
*ctx
)
625 return register_process_model(&standard_ops
);