VERSION: Bump version up to 4.7.0rc4...
[Samba.git] / source4 / smbd / process_standard.c
blob8d962d55130deee9af9f8e1f4dbb8d9f6a034f52
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 "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"
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;
43 NTSTATUS process_model_standard_init(TALLOC_CTX *);
45 /* we hold a pipe open in the parent, and the any child
46 processes wait for EOF on that pipe. This ensures that
47 children die when the parent dies */
48 static int child_pipe[2] = { -1, -1 };
51 called when the process model is selected
53 static void standard_model_init(void)
55 int rc;
57 rc = pipe(child_pipe);
58 if (rc < 0) {
59 smb_panic("Failed to initialize pipe!");
63 static void sighup_signal_handler(struct tevent_context *ev,
64 struct tevent_signal *se,
65 int signum, int count, void *siginfo,
66 void *private_data)
68 debug_schedule_reopen_logs();
71 static void sigterm_signal_handler(struct tevent_context *ev,
72 struct tevent_signal *se,
73 int signum, int count, void *siginfo,
74 void *private_data)
76 #if HAVE_GETPGRP
77 if (getpgrp() == getpid()) {
79 * We're the process group leader, send
80 * SIGTERM to our process group.
82 DEBUG(0,("SIGTERM: killing children\n"));
83 kill(-getpgrp(), SIGTERM);
85 #endif
86 DEBUG(0,("Exiting pid %u on SIGTERM\n", (unsigned int)getpid()));
87 talloc_free(ev);
88 exit(127);
92 handle EOF on the parent-to-all-children pipe in the child
94 static void standard_pipe_handler(struct tevent_context *event_ctx, struct tevent_fd *fde,
95 uint16_t flags, void *private_data)
97 DEBUG(10,("Child %d exiting\n", (int)getpid()));
98 talloc_free(event_ctx);
99 exit(0);
103 handle EOF on the child pipe in the parent, so we know when a
104 process terminates without using SIGCHLD or waiting on all possible pids.
106 We need to ensure we do not ignore SIGCHLD because we need it to
107 work to get a valid error code from samba_runcmd_*().
109 static void standard_child_pipe_handler(struct tevent_context *ev,
110 struct tevent_fd *fde,
111 uint16_t flags,
112 void *private_data)
114 struct standard_child_state *state
115 = talloc_get_type_abort(private_data, struct standard_child_state);
116 int status = 0;
117 pid_t pid;
119 messaging_dgm_cleanup(state->pid);
121 /* the child has closed the pipe, assume its dead */
122 errno = 0;
123 pid = waitpid(state->pid, &status, 0);
125 if (pid != state->pid) {
126 if (errno == ECHILD) {
128 * this happens when the
129 * parent has set SIGCHLD to
130 * SIG_IGN. In that case we
131 * can only get error
132 * information for the child
133 * via its logging. We should
134 * stop using SIG_IGN on
135 * SIGCHLD in the standard
136 * process model.
138 DEBUG(0, ("Error in waitpid() unexpectedly got ECHILD "
139 "for child %d (%s) - %s, someone has set SIGCHLD "
140 "to SIG_IGN!\n",
141 (int)state->pid, state->name,
142 strerror(errno)));
143 TALLOC_FREE(state);
144 return;
146 DEBUG(0, ("Error in waitpid() for child %d (%s) - %s \n",
147 (int)state->pid, state->name, strerror(errno)));
148 if (errno == 0) {
149 errno = ECHILD;
151 TALLOC_FREE(state);
152 return;
154 if (WIFEXITED(status)) {
155 status = WEXITSTATUS(status);
156 DEBUG(2, ("Child %d (%s) exited with status %d\n",
157 (int)state->pid, state->name, status));
158 } else if (WIFSIGNALED(status)) {
159 status = WTERMSIG(status);
160 DEBUG(0, ("Child %d (%s) terminated with signal %d\n",
161 (int)state->pid, state->name, status));
163 TALLOC_FREE(state);
164 return;
167 static struct standard_child_state *setup_standard_child_pipe(struct tevent_context *ev,
168 const char *name)
170 struct standard_child_state *state;
171 int parent_child_pipe[2];
172 int ret;
175 * Prepare a pipe to allow us to know when the child exits,
176 * because it will trigger a read event on this private
177 * pipe.
179 * We do all this before the accept and fork(), so we can
180 * clean up if it fails.
182 state = talloc_zero(ev, struct standard_child_state);
183 if (state == NULL) {
184 return NULL;
187 if (name == NULL) {
188 name = "";
191 state->name = talloc_strdup(state, name);
192 if (state->name == NULL) {
193 TALLOC_FREE(state);
194 return NULL;
197 ret = pipe(parent_child_pipe);
198 if (ret == -1) {
199 DEBUG(0, ("Failed to create parent-child pipe to handle "
200 "SIGCHLD to track new process for socket\n"));
201 TALLOC_FREE(state);
202 return NULL;
205 smb_set_close_on_exec(parent_child_pipe[0]);
206 smb_set_close_on_exec(parent_child_pipe[1]);
208 state->from_child_fd = parent_child_pipe[0];
209 state->to_parent_fd = parent_child_pipe[1];
212 * The basic purpose of calling this handler is to ensure we
213 * call waitpid() and so avoid zombies (now that we no longer
214 * user SIGIGN on for SIGCHLD), but it also allows us to clean
215 * up other resources in the future.
217 state->from_child_fde = tevent_add_fd(ev, state,
218 state->from_child_fd,
219 TEVENT_FD_READ,
220 standard_child_pipe_handler,
221 state);
222 if (state->from_child_fde == NULL) {
223 TALLOC_FREE(state);
224 return NULL;
226 tevent_fd_set_auto_close(state->from_child_fde);
228 return state;
232 called when a listening socket becomes readable.
234 static void standard_accept_connection(struct tevent_context *ev,
235 struct loadparm_context *lp_ctx,
236 struct socket_context *sock,
237 void (*new_conn)(struct tevent_context *,
238 struct loadparm_context *, struct socket_context *,
239 struct server_id , void *),
240 void *private_data)
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;
250 state = setup_standard_child_pipe(ev, NULL);
251 if (state == NULL) {
252 return;
255 /* accept an incoming connection. */
256 status = socket_accept(sock, &sock2);
257 if (!NT_STATUS_IS_OK(status)) {
258 DEBUG(0,("standard_accept_connection: accept: %s\n",
259 nt_errstr(status)));
260 /* this looks strange, but is correct. We need to throttle things until
261 the system clears enough resources to handle this new socket */
262 sleep(1);
263 close(state->to_parent_fd);
264 state->to_parent_fd = -1;
265 TALLOC_FREE(state);
266 return;
269 pid = fork();
271 if (pid != 0) {
272 close(state->to_parent_fd);
273 state->to_parent_fd = -1;
275 if (pid > 0) {
276 state->pid = pid;
277 } else {
278 TALLOC_FREE(state);
281 /* parent or error code ... */
282 talloc_free(sock2);
283 /* go back to the event loop */
284 return;
287 /* this leaves state->to_parent_fd open */
288 TALLOC_FREE(state);
290 pid = getpid();
292 /* This is now the child code. We need a completely new event_context to work with */
294 if (tevent_re_initialise(ev) != 0) {
295 smb_panic("Failed to re-initialise tevent after fork");
298 /* this will free all the listening sockets and all state that
299 is not associated with this new connection */
300 talloc_free(sock);
302 /* we don't care if the dup fails, as its only a select()
303 speed optimisation */
304 socket_dup(sock2);
306 /* tdb needs special fork handling */
307 ldb_wrap_fork_hook();
309 /* Must be done after a fork() to reset messaging contexts. */
310 status = imessaging_reinit_all();
311 if (!NT_STATUS_IS_OK(status)) {
312 smb_panic("Failed to re-initialise imessaging after fork");
315 fde = tevent_add_fd(ev, ev, child_pipe[0], TEVENT_FD_READ,
316 standard_pipe_handler, NULL);
317 if (fde == NULL) {
318 smb_panic("Failed to add fd handler after fork");
321 if (child_pipe[1] != -1) {
322 close(child_pipe[1]);
323 child_pipe[1] = -1;
326 se = tevent_add_signal(ev,
328 SIGHUP,
330 sighup_signal_handler,
331 NULL);
332 if (se == NULL) {
333 smb_panic("Failed to add SIGHUP handler after fork");
336 se = tevent_add_signal(ev,
338 SIGTERM,
340 sigterm_signal_handler,
341 NULL);
342 if (se == NULL) {
343 smb_panic("Failed to add SIGTERM handler after fork");
346 /* setup the process title */
347 c = socket_get_peer_addr(sock2, ev);
348 s = socket_get_my_addr(sock2, ev);
349 if (s && c) {
350 setproctitle("conn c[%s:%u] s[%s:%u] server_id[%d]",
351 c->addr, c->port, s->addr, s->port, (int)pid);
353 talloc_free(c);
354 talloc_free(s);
356 /* setup this new connection. Cluster ID is PID based for this process model */
357 new_conn(ev, lp_ctx, sock2, cluster_id(pid, 0), private_data);
359 /* we can't return to the top level here, as that event context is gone,
360 so we now process events in the new event context until there are no
361 more to process */
362 tevent_loop_wait(ev);
364 talloc_free(ev);
365 exit(0);
369 called to create a new server task
371 static void standard_new_task(struct tevent_context *ev,
372 struct loadparm_context *lp_ctx,
373 const char *service_name,
374 void (*new_task)(struct tevent_context *, struct loadparm_context *lp_ctx, struct server_id , void *),
375 void *private_data)
377 pid_t pid;
378 NTSTATUS status;
379 struct standard_child_state *state;
380 struct tevent_fd *fde = NULL;
381 struct tevent_signal *se = NULL;
383 state = setup_standard_child_pipe(ev, service_name);
384 if (state == NULL) {
385 return;
388 pid = fork();
390 if (pid != 0) {
391 close(state->to_parent_fd);
392 state->to_parent_fd = -1;
394 if (pid > 0) {
395 state->pid = pid;
396 } else {
397 TALLOC_FREE(state);
400 /* parent or error code ... go back to the event loop */
401 return;
404 /* this leaves state->to_parent_fd open */
405 TALLOC_FREE(state);
407 pid = getpid();
409 /* this will free all the listening sockets and all state that
410 is not associated with this new connection */
411 if (tevent_re_initialise(ev) != 0) {
412 smb_panic("Failed to re-initialise tevent after fork");
415 /* ldb/tdb need special fork handling */
416 ldb_wrap_fork_hook();
418 /* Must be done after a fork() to reset messaging contexts. */
419 status = imessaging_reinit_all();
420 if (!NT_STATUS_IS_OK(status)) {
421 smb_panic("Failed to re-initialise imessaging after fork");
424 fde = tevent_add_fd(ev, ev, child_pipe[0], TEVENT_FD_READ,
425 standard_pipe_handler, NULL);
426 if (fde == NULL) {
427 smb_panic("Failed to add fd handler after fork");
429 if (child_pipe[1] != -1) {
430 close(child_pipe[1]);
431 child_pipe[1] = -1;
434 se = tevent_add_signal(ev,
436 SIGHUP,
438 sighup_signal_handler,
439 NULL);
440 if (se == NULL) {
441 smb_panic("Failed to add SIGHUP handler after fork");
444 se = tevent_add_signal(ev,
446 SIGTERM,
448 sigterm_signal_handler,
449 NULL);
450 if (se == NULL) {
451 smb_panic("Failed to add SIGTERM handler after fork");
454 setproctitle("task %s server_id[%d]", service_name, (int)pid);
456 /* setup this new task. Cluster ID is PID based for this process model */
457 new_task(ev, lp_ctx, cluster_id(pid, 0), private_data);
459 /* we can't return to the top level here, as that event context is gone,
460 so we now process events in the new event context until there are no
461 more to process */
462 tevent_loop_wait(ev);
464 talloc_free(ev);
465 exit(0);
469 /* called when a task goes down */
470 _NORETURN_ static void standard_terminate(struct tevent_context *ev, struct loadparm_context *lp_ctx,
471 const char *reason)
473 DEBUG(2,("standard_terminate: reason[%s]\n",reason));
475 /* this reload_charcnv() has the effect of freeing the iconv context memory,
476 which makes leak checking easier */
477 reload_charcnv(lp_ctx);
479 /* Always free event context last before exit. */
480 talloc_free(ev);
482 /* terminate this process */
483 exit(0);
486 /* called to set a title of a task or connection */
487 static void standard_set_title(struct tevent_context *ev, const char *title)
489 if (title) {
490 setproctitle("%s", title);
491 } else {
492 setproctitle(NULL);
496 static const struct model_ops standard_ops = {
497 .name = "standard",
498 .model_init = standard_model_init,
499 .accept_connection = standard_accept_connection,
500 .new_task = standard_new_task,
501 .terminate = standard_terminate,
502 .set_title = standard_set_title,
506 initialise the standard process model, registering ourselves with the process model subsystem
508 NTSTATUS process_model_standard_init(TALLOC_CTX *ctx)
510 return register_process_model(&standard_ops);