/* called to create a new server task */ static void onefork_new_task(struct tevent_context *ev, struct loadparm_context *lp_ctx, const char *service_name, void (*new_task_fn)(struct tevent_context *, struct loadparm_context *lp_ctx, struct server_id , void *), void *private_data) { pid_t pid; pid = fork(); if (pid != 0) { /* parent or error code ... go back to the event loop */ return; } pid = getpid(); if (tevent_re_initialise(ev) != 0) { smb_panic("Failed to re-initialise tevent after fork"); } setproctitle("task %s server_id[%d]", service_name, (int)pid); onefork_reload_after_fork(); /* setup this new connection: process will bind to it's sockets etc */ new_task_fn(ev, lp_ctx, cluster_id(pid, 0), private_data); event_loop_wait(ev); talloc_free(ev); exit(0); }
/* * called to create a new server task */ static void prefork_new_task( struct tevent_context *ev, struct loadparm_context *lp_ctx, const char *service_name, void (*new_task_fn)(struct tevent_context *, struct loadparm_context *lp_ctx, struct server_id , void *, void *), void *private_data, const struct service_details *service_details, int from_parent_fd) { pid_t pid; struct tfork* t = NULL; int i, num_children; struct tevent_context *ev2; t = tfork_create(); if (t == NULL) { smb_panic("failure in tfork\n"); } pid = tfork_child_pid(t); if (pid != 0) { struct tevent_fd *fde = NULL; int fd = tfork_event_fd(t); /* Register a pipe handler that gets called when the prefork * master process terminates. */ fde = tevent_add_fd(ev, ev, fd, TEVENT_FD_READ, prefork_child_pipe_handler, t); if (fde == NULL) { smb_panic("Failed to add child pipe handler, " "after fork"); } tevent_fd_set_auto_close(fde); return; } pid = getpid(); setproctitle("task[%s] pre-fork master", service_name); /* * this will free all the listening sockets and all state that * is not associated with this new connection */ if (tevent_re_initialise(ev) != 0) { smb_panic("Failed to re-initialise tevent after fork"); } prefork_reload_after_fork(); setup_handlers(ev, from_parent_fd); if (service_details->inhibit_pre_fork) { new_task_fn(ev, lp_ctx, cluster_id(pid, 0), private_data, NULL); /* The task does not support pre-fork */ tevent_loop_wait(ev); TALLOC_FREE(ev); exit(0); } /* * This is now the child code. We need a completely new event_context * to work with */ ev2 = s4_event_context_init(NULL); /* setup this new connection: process will bind to it's sockets etc * * While we can use ev for the child, which has been re-initialised * above we must run the new task under ev2 otherwise the children would * be listening on the sockets. Also we don't want the top level * process accepting and handling requests, it's responsible for * monitoring and controlling the child work processes. */ new_task_fn(ev2, lp_ctx, cluster_id(pid, 0), private_data, NULL); { int default_children; default_children = lpcfg_prefork_children(lp_ctx); num_children = lpcfg_parm_int(lp_ctx, NULL, "prefork children", service_name, default_children); } if (num_children == 0) { DBG_WARNING("Number of pre-fork children for %s is zero, " "NO worker processes will be started for %s\n", service_name, service_name); } DBG_NOTICE("Forking %d %s worker processes\n", num_children, service_name); /* We are now free to spawn some worker processes */ for (i=0; i < num_children; i++) { struct tfork* w = NULL; w = tfork_create(); if (w == NULL) { smb_panic("failure in tfork\n"); } pid = tfork_child_pid(w); if (pid != 0) { struct tevent_fd *fde = NULL; int fd = tfork_event_fd(w); fde = tevent_add_fd(ev, ev, fd, TEVENT_FD_READ, prefork_child_pipe_handler, w); if (fde == NULL) { smb_panic("Failed to add child pipe handler, " "after fork"); } tevent_fd_set_auto_close(fde); } else { /* tfork uses malloc */ free(w); TALLOC_FREE(ev); setproctitle("task[%s] pre-forked worker", service_name); prefork_reload_after_fork(); setup_handlers(ev2, from_parent_fd); tevent_loop_wait(ev2); talloc_free(ev2); exit(0); } } /* Don't listen on the sockets we just gave to the children */ tevent_loop_wait(ev); TALLOC_FREE(ev); /* We need to keep ev2 until we're finished for the messaging to work */ TALLOC_FREE(ev2); exit(0); }