s4.2/fsmo.py: fixed fsmo transfer exception
[Samba.git] / source4 / smbd / process_standard.c
blobcbc63b6db1a4a142709f011b1974c214a0910afd
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"
32 NTSTATUS process_model_standard_init(void);
34 /* we hold a pipe open in the parent, and the any child
35 processes wait for EOF on that pipe. This ensures that
36 children die when the parent dies */
37 static int child_pipe[2] = { -1, -1 };
40 called when the process model is selected
42 static void standard_model_init(void)
44 pipe(child_pipe);
45 signal(SIGCHLD, SIG_IGN);
49 handle EOF on the child pipe
51 static void standard_pipe_handler(struct tevent_context *event_ctx, struct tevent_fd *fde,
52 uint16_t flags, void *private_data)
54 DEBUG(10,("Child %d exiting\n", (int)getpid()));
55 exit(0);
59 called when a listening socket becomes readable.
61 static void standard_accept_connection(struct tevent_context *ev,
62 struct loadparm_context *lp_ctx,
63 struct socket_context *sock,
64 void (*new_conn)(struct tevent_context *,
65 struct loadparm_context *, struct socket_context *,
66 struct server_id , void *),
67 void *private_data)
69 NTSTATUS status;
70 struct socket_context *sock2;
71 pid_t pid;
72 struct socket_address *c, *s;
74 /* accept an incoming connection. */
75 status = socket_accept(sock, &sock2);
76 if (!NT_STATUS_IS_OK(status)) {
77 DEBUG(0,("standard_accept_connection: accept: %s\n",
78 nt_errstr(status)));
79 /* this looks strange, but is correct. We need to throttle things until
80 the system clears enough resources to handle this new socket */
81 sleep(1);
82 return;
85 pid = fork();
87 if (pid != 0) {
88 /* parent or error code ... */
89 talloc_free(sock2);
90 /* go back to the event loop */
91 return;
94 pid = getpid();
96 /* This is now the child code. We need a completely new event_context to work with */
98 if (tevent_re_initialise(ev) != 0) {
99 smb_panic("Failed to re-initialise tevent after fork");
102 /* this will free all the listening sockets and all state that
103 is not associated with this new connection */
104 talloc_free(sock);
106 /* we don't care if the dup fails, as its only a select()
107 speed optimisation */
108 socket_dup(sock2);
110 /* tdb needs special fork handling */
111 ldb_wrap_fork_hook();
113 tevent_add_fd(ev, ev, child_pipe[0], TEVENT_FD_READ,
114 standard_pipe_handler, NULL);
115 if (child_pipe[1] != -1) {
116 close(child_pipe[1]);
117 child_pipe[1] = -1;
120 /* Ensure that the forked children do not expose identical random streams */
121 set_need_random_reseed();
123 /* setup the process title */
124 c = socket_get_peer_addr(sock2, ev);
125 s = socket_get_my_addr(sock2, ev);
126 if (s && c) {
127 setproctitle("conn c[%s:%u] s[%s:%u] server_id[%d]",
128 c->addr, c->port, s->addr, s->port, (int)pid);
130 talloc_free(c);
131 talloc_free(s);
133 /* setup this new connection. Cluster ID is PID based for this process model */
134 new_conn(ev, lp_ctx, sock2, cluster_id(pid, 0), private_data);
136 /* we can't return to the top level here, as that event context is gone,
137 so we now process events in the new event context until there are no
138 more to process */
139 tevent_loop_wait(ev);
141 talloc_free(ev);
142 exit(0);
146 called to create a new server task
148 static void standard_new_task(struct tevent_context *ev,
149 struct loadparm_context *lp_ctx,
150 const char *service_name,
151 void (*new_task)(struct tevent_context *, struct loadparm_context *lp_ctx, struct server_id , void *),
152 void *private_data)
154 pid_t pid;
156 pid = fork();
158 if (pid != 0) {
159 /* parent or error code ... go back to the event loop */
160 return;
163 pid = getpid();
165 /* this will free all the listening sockets and all state that
166 is not associated with this new connection */
167 if (tevent_re_initialise(ev) != 0) {
168 smb_panic("Failed to re-initialise tevent after fork");
171 /* ldb/tdb need special fork handling */
172 ldb_wrap_fork_hook();
174 tevent_add_fd(ev, ev, child_pipe[0], TEVENT_FD_READ,
175 standard_pipe_handler, NULL);
176 if (child_pipe[1] != -1) {
177 close(child_pipe[1]);
178 child_pipe[1] = -1;
181 /* Ensure that the forked children do not expose identical random streams */
182 set_need_random_reseed();
184 setproctitle("task %s server_id[%d]", service_name, (int)pid);
186 /* setup this new task. Cluster ID is PID based for this process model */
187 new_task(ev, lp_ctx, cluster_id(pid, 0), private_data);
189 /* we can't return to the top level here, as that event context is gone,
190 so we now process events in the new event context until there are no
191 more to process */
192 tevent_loop_wait(ev);
194 talloc_free(ev);
195 exit(0);
199 /* called when a task goes down */
200 _NORETURN_ static void standard_terminate(struct tevent_context *ev, struct loadparm_context *lp_ctx,
201 const char *reason)
203 DEBUG(2,("standard_terminate: reason[%s]\n",reason));
205 talloc_free(ev);
207 /* this reload_charcnv() has the effect of freeing the iconv context memory,
208 which makes leak checking easier */
209 reload_charcnv(lp_ctx);
211 /* terminate this process */
212 exit(0);
215 /* called to set a title of a task or connection */
216 static void standard_set_title(struct tevent_context *ev, const char *title)
218 if (title) {
219 setproctitle("%s", title);
220 } else {
221 setproctitle(NULL);
225 static const struct model_ops standard_ops = {
226 .name = "standard",
227 .model_init = standard_model_init,
228 .accept_connection = standard_accept_connection,
229 .new_task = standard_new_task,
230 .terminate = standard_terminate,
231 .set_title = standard_set_title,
235 initialise the standard process model, registering ourselves with the process model subsystem
237 NTSTATUS process_model_standard_init(void)
239 return register_process_model(&standard_ops);