s4:dsdb:util_trusts: simplify the NULL case in dns_cmp
[Samba.git] / source4 / samba / process_standard.c
blobfba24ff5e7f69f8b4db71a9f9f88af9642a2b2e2
1 /*
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/>.
24 #include "includes.h"
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"
30 #include "ldb_wrap.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 {
40 const char *name;
41 pid_t pid;
42 int to_parent_fd;
43 int from_child_fd;
44 struct tevent_fd *from_child_fde;
47 NTSTATUS process_model_standard_init(TALLOC_CTX *);
48 struct process_context {
49 char *name;
50 int from_parent_fd;
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,
65 void *private_data)
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,
73 void *private_data)
75 #ifdef HAVE_GETPGRP
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);
84 #endif
85 DBG_ERR("Exiting pid %u on SIGTERM\n", (unsigned int)getpid());
86 talloc_free(ev);
87 exit(127);
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);
98 exit(0);
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,
110 uint16_t flags,
111 void *private_data)
113 struct standard_child_state *state
114 = talloc_get_type_abort(private_data, struct standard_child_state);
115 int status = 0;
116 pid_t pid;
118 messaging_dgm_cleanup(state->pid);
120 /* the child has closed the pipe, assume its dead */
121 errno = 0;
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
130 * can only get error
131 * information for the child
132 * via its logging. We should
133 * stop using SIG_IGN on
134 * SIGCHLD in the standard
135 * process model.
137 DBG_ERR("Error in waitpid() unexpectedly got ECHILD "
138 "for child %d (%s) - %s, someone has set SIGCHLD "
139 "to SIG_IGN!\n",
140 (int)state->pid, state->name,
141 strerror(errno));
142 TALLOC_FREE(state);
143 return;
145 DBG_ERR("Error in waitpid() for child %d (%s) - %s \n",
146 (int)state->pid, state->name, strerror(errno));
147 if (errno == 0) {
148 errno = ECHILD;
150 goto done;
152 if (WIFEXITED(status)) {
153 status = WEXITSTATUS(status);
154 if (status != 0) {
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);
163 done:
164 TALLOC_FREE(state);
165 if (smbd_max_processes > 0) {
166 if (connections_active < 1) {
167 DBG_ERR("Number of active connections "
168 "less than 1 (%d)\n",
169 connections_active);
170 connections_active = 1;
172 connections_active--;
174 return;
177 static struct standard_child_state *setup_standard_child_pipe(struct tevent_context *ev,
178 const char *name)
180 struct standard_child_state *state;
181 int parent_child_pipe[2];
182 int ret;
185 * Prepare a pipe to allow us to know when the child exits,
186 * because it will trigger a read event on this private
187 * pipe.
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);
193 if (state == NULL) {
194 return NULL;
197 if (name == NULL) {
198 name = "";
201 state->name = talloc_strdup(state, name);
202 if (state->name == NULL) {
203 TALLOC_FREE(state);
204 return NULL;
207 ret = pipe(parent_child_pipe);
208 if (ret == -1) {
209 DBG_ERR("Failed to create parent-child pipe to handle "
210 "SIGCHLD to track new process for socket\n");
211 TALLOC_FREE(state);
212 return NULL;
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,
229 TEVENT_FD_READ,
230 standard_child_pipe_handler,
231 state);
232 if (state->from_child_fde == NULL) {
233 TALLOC_FREE(state);
234 return NULL;
236 tevent_fd_set_auto_close(state->from_child_fde);
238 return state;
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 *,
251 struct server_id,
252 void *,
253 void *),
254 void *private_data,
255 void *process_context)
257 NTSTATUS status;
258 struct socket_context *sock2;
259 pid_t pid;
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",
271 nt_errstr(status));
272 /* this looks strange, but is correct. We need to throttle
273 * things until the system clears enough resources to handle
274 * this new socket
276 sleep(1);
277 return;
280 proc_ctx = talloc_get_type_abort(process_context,
281 struct process_context);
283 if (proc_ctx->inhibit_fork_on_accept) {
284 pid = getpid();
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,
294 process_context);
295 return;
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",
302 connections_active,
303 smbd_max_processes);
305 * Drop the connection as we're overloaded at the moment
307 talloc_free(sock2);
308 return;
310 connections_active++;
313 state = setup_standard_child_pipe(ev, NULL);
314 if (state == NULL) {
315 return;
317 pid = fork();
319 if (pid != 0) {
320 close(state->to_parent_fd);
321 state->to_parent_fd = -1;
323 if (pid > 0) {
324 state->pid = pid;
325 } else {
326 TALLOC_FREE(state);
329 /* parent or error code ... */
330 talloc_free(sock2);
331 /* go back to the event loop */
332 return;
335 /* this leaves state->to_parent_fd open */
336 TALLOC_FREE(state);
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;
343 pid = getpid();
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 */
355 talloc_free(sock);
357 /* we don't care if the dup fails, as its only a select()
358 speed optimisation */
359 socket_dup(sock2);
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);
372 if (fde == NULL) {
373 smb_panic("Failed to add fd handler after fork");
376 se = tevent_add_signal(ev,
378 SIGHUP,
380 sighup_signal_handler,
381 NULL);
382 if (se == NULL) {
383 smb_panic("Failed to add SIGHUP handler after fork");
386 se = tevent_add_signal(ev,
388 SIGTERM,
390 sigterm_signal_handler,
391 NULL);
392 if (se == NULL) {
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);
399 if (s && c) {
400 setproctitle("conn c[%s:%u] s[%s:%u] server_id[%d]",
401 c->addr, c->port, s->addr, s->port, (int)pid);
403 talloc_free(c);
404 talloc_free(s);
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,
410 process_context);
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
414 more to process */
415 tevent_loop_wait(ev);
417 talloc_free(ev);
418 exit(0);
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 *),
428 void *private_data,
429 const struct service_details *service_details,
430 int from_parent_fd)
432 pid_t pid;
433 NTSTATUS status;
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);
441 if (state == NULL) {
442 return;
445 pid = fork();
447 if (pid != 0) {
448 close(state->to_parent_fd);
449 state->to_parent_fd = -1;
451 if (pid > 0) {
452 state->pid = pid;
453 } else {
454 TALLOC_FREE(state);
457 /* parent or error code ... go back to the event loop */
458 return;
461 /* this leaves state->to_parent_fd open */
462 TALLOC_FREE(state);
464 pid = getpid();
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);
483 if (fde == NULL) {
484 smb_panic("Failed to add fd handler after fork");
487 se = tevent_add_signal(ev,
489 SIGHUP,
491 sighup_signal_handler,
492 NULL);
493 if (se == NULL) {
494 smb_panic("Failed to add SIGHUP handler after fork");
497 se = tevent_add_signal(ev,
499 SIGTERM,
501 sigterm_signal_handler,
502 NULL);
503 if (se == NULL) {
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
542 more to process */
543 tevent_loop_wait(ev);
545 talloc_free(ev);
546 exit(0);
550 /* called when a task goes down */
551 static void standard_terminate_task(struct tevent_context *ev,
552 struct loadparm_context *lp_ctx,
553 const char *reason,
554 bool fatal,
555 void *process_context)
557 if (fatal == true) {
558 exit(127);
560 exit(0);
563 /* called when a connection terminates*/
564 static void standard_terminate_connection(struct tevent_context *ev,
565 struct loadparm_context *lp_ctx,
566 const char *reason,
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
582 return;
585 * The current process was forked on accept to handle a single
586 * connection/request. That request has now finished and the process
587 * should terminate
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. */
595 talloc_free(ev);
597 /* terminate this process */
598 exit(0);
600 /* called to set a title of a task or connection */
601 static void standard_set_title(struct tevent_context *ev, const char *title)
603 if (title) {
604 setproctitle("%s", title);
605 } else {
606 setproctitle(NULL);
610 static const struct model_ops standard_ops = {
611 .name = "standard",
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);