1/*
2   Unix SMB/CIFS implementation.
3
4   process model: standard (1 process per client connection)
5
6   Copyright (C) Andrew Tridgell 1992-2005
7   Copyright (C) James J Myers 2003 <myersjj@samba.org>
8   Copyright (C) Stefan (metze) Metzmacher 2004
9
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.
14
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.
19
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/>.
22*/
23
24#include "includes.h"
25#include "lib/events/events.h"
26#include "../tdb/include/tdb.h"
27#include "smbd/process_model.h"
28#include "system/filesys.h"
29#include "cluster/cluster.h"
30#include "param/param.h"
31
32#ifdef HAVE_SETPROCTITLE
33#ifdef HAVE_SETPROCTITLE_H
34#include <setproctitle.h>
35#endif
36#else
37#define setproctitle none_setproctitle
38static int none_setproctitle(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);
39static int none_setproctitle(const char *fmt, ...)
40{
41	return 0;
42}
43#endif
44
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 */
48static int child_pipe[2];
49
50/*
51  called when the process model is selected
52*/
53static void standard_model_init(struct tevent_context *ev)
54{
55	pipe(child_pipe);
56	signal(SIGCHLD, SIG_IGN);
57}
58
59/*
60  handle EOF on the child pipe
61*/
62static void standard_pipe_handler(struct tevent_context *event_ctx, struct tevent_fd *fde,
63				  uint16_t flags, void *private_data)
64{
65	DEBUG(10,("Child %d exiting\n", (int)getpid()));
66	exit(0);
67}
68
69/*
70  called when a listening socket becomes readable.
71*/
72static void standard_accept_connection(struct tevent_context *ev,
73				       struct loadparm_context *lp_ctx,
74				       struct socket_context *sock,
75				       void (*new_conn)(struct tevent_context *,
76							struct loadparm_context *, struct socket_context *,
77							struct server_id , void *),
78				       void *private_data)
79{
80	NTSTATUS status;
81	struct socket_context *sock2;
82	pid_t pid;
83	struct tevent_context *ev2;
84	struct socket_address *c, *s;
85
86	/* accept an incoming connection. */
87	status = socket_accept(sock, &sock2);
88	if (!NT_STATUS_IS_OK(status)) {
89		DEBUG(0,("standard_accept_connection: accept: %s\n",
90			 nt_errstr(status)));
91		/* this looks strange, but is correct. We need to throttle things until
92		   the system clears enough resources to handle this new socket */
93		sleep(1);
94		return;
95	}
96
97	pid = fork();
98
99	if (pid != 0) {
100		/* parent or error code ... */
101		talloc_free(sock2);
102		/* go back to the event loop */
103		return;
104	}
105
106	pid = getpid();
107
108	/* This is now the child code. We need a completely new event_context to work with */
109	ev2 = s4_event_context_init(NULL);
110
111	/* the service has given us a private pointer that
112	   encapsulates the context it needs for this new connection -
113	   everything else will be freed */
114	talloc_steal(ev2, private_data);
115	talloc_steal(private_data, sock2);
116
117	/* this will free all the listening sockets and all state that
118	   is not associated with this new connection */
119	talloc_free(sock);
120	talloc_free(ev);
121
122	/* we don't care if the dup fails, as its only a select()
123	   speed optimisation */
124	socket_dup(sock2);
125
126	/* tdb needs special fork handling */
127	if (tdb_reopen_all(1) == -1) {
128		DEBUG(0,("standard_accept_connection: tdb_reopen_all failed.\n"));
129	}
130
131	tevent_add_fd(ev2, ev2, child_pipe[0], TEVENT_FD_READ,
132		      standard_pipe_handler, NULL);
133	close(child_pipe[1]);
134
135	/* Ensure that the forked children do not expose identical random streams */
136	set_need_random_reseed();
137
138	/* setup the process title */
139	c = socket_get_peer_addr(sock2, ev2);
140	s = socket_get_my_addr(sock2, ev2);
141	if (s && c) {
142		setproctitle("conn c[%s:%u] s[%s:%u] server_id[%d]",
143			     c->addr, c->port, s->addr, s->port, pid);
144	}
145	talloc_free(c);
146	talloc_free(s);
147
148	/* setup this new connection.  Cluster ID is PID based for this process modal */
149	new_conn(ev2, lp_ctx, sock2, cluster_id(pid, 0), private_data);
150
151	/* we can't return to the top level here, as that event context is gone,
152	   so we now process events in the new event context until there are no
153	   more to process */
154	event_loop_wait(ev2);
155
156	talloc_free(ev2);
157	exit(0);
158}
159
160/*
161  called to create a new server task
162*/
163static void standard_new_task(struct tevent_context *ev,
164			      struct loadparm_context *lp_ctx,
165			      const char *service_name,
166			      void (*new_task)(struct tevent_context *, struct loadparm_context *lp_ctx, struct server_id , void *),
167			      void *private_data)
168{
169	pid_t pid;
170	struct tevent_context *ev2;
171
172	pid = fork();
173
174	if (pid != 0) {
175		/* parent or error code ... go back to the event loop */
176		return;
177	}
178
179	pid = getpid();
180
181	/* This is now the child code. We need a completely new event_context to work with */
182	ev2 = s4_event_context_init(NULL);
183
184	/* the service has given us a private pointer that
185	   encapsulates the context it needs for this new connection -
186	   everything else will be freed */
187	talloc_steal(ev2, private_data);
188
189	/* this will free all the listening sockets and all state that
190	   is not associated with this new connection */
191	talloc_free(ev);
192
193	/* tdb needs special fork handling */
194	if (tdb_reopen_all(1) == -1) {
195		DEBUG(0,("standard_accept_connection: tdb_reopen_all failed.\n"));
196	}
197
198	tevent_add_fd(ev2, ev2, child_pipe[0], TEVENT_FD_READ,
199		      standard_pipe_handler, NULL);
200	close(child_pipe[1]);
201
202	/* Ensure that the forked children do not expose identical random streams */
203	set_need_random_reseed();
204
205	setproctitle("task %s server_id[%d]", service_name, pid);
206
207	/* setup this new task.  Cluster ID is PID based for this process modal */
208	new_task(ev2, lp_ctx, cluster_id(pid, 0), private_data);
209
210	/* we can't return to the top level here, as that event context is gone,
211	   so we now process events in the new event context until there are no
212	   more to process */
213	event_loop_wait(ev2);
214
215	talloc_free(ev2);
216	exit(0);
217}
218
219
220/* called when a task goes down */
221_NORETURN_ static void standard_terminate(struct tevent_context *ev, struct loadparm_context *lp_ctx,
222					  const char *reason)
223{
224	DEBUG(2,("standard_terminate: reason[%s]\n",reason));
225
226	/* this reload_charcnv() has the effect of freeing the iconv context memory,
227	   which makes leak checking easier */
228	reload_charcnv(lp_ctx);
229
230	talloc_free(ev);
231
232	/* terminate this process */
233	exit(0);
234}
235
236/* called to set a title of a task or connection */
237static void standard_set_title(struct tevent_context *ev, const char *title)
238{
239	if (title) {
240		setproctitle("%s", title);
241	} else {
242		setproctitle(NULL);
243	}
244}
245
246static const struct model_ops standard_ops = {
247	.name			= "standard",
248	.model_init		= standard_model_init,
249	.accept_connection	= standard_accept_connection,
250	.new_task               = standard_new_task,
251	.terminate              = standard_terminate,
252	.set_title              = standard_set_title,
253};
254
255/*
256  initialise the standard process model, registering ourselves with the process model subsystem
257 */
258NTSTATUS process_model_standard_init(void)
259{
260	return register_process_model(&standard_ops);
261}
262