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 "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 |
|
---|
32 | #ifdef HAVE_SETPROCTITLE
|
---|
33 | #ifdef HAVE_SETPROCTITLE_H
|
---|
34 | #include <setproctitle.h>
|
---|
35 | #endif
|
---|
36 | #else
|
---|
37 | #define setproctitle none_setproctitle
|
---|
38 | static int none_setproctitle(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);
|
---|
39 | static 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 */
|
---|
48 | static int child_pipe[2];
|
---|
49 |
|
---|
50 | /*
|
---|
51 | called when the process model is selected
|
---|
52 | */
|
---|
53 | static void standard_model_init(void)
|
---|
54 | {
|
---|
55 | pipe(child_pipe);
|
---|
56 | signal(SIGCHLD, SIG_IGN);
|
---|
57 | }
|
---|
58 |
|
---|
59 | /*
|
---|
60 | handle EOF on the child pipe
|
---|
61 | */
|
---|
62 | static 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 | */
|
---|
72 | static 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 socket_address *c, *s;
|
---|
84 |
|
---|
85 | /* accept an incoming connection. */
|
---|
86 | status = socket_accept(sock, &sock2);
|
---|
87 | if (!NT_STATUS_IS_OK(status)) {
|
---|
88 | DEBUG(0,("standard_accept_connection: accept: %s\n",
|
---|
89 | nt_errstr(status)));
|
---|
90 | /* this looks strange, but is correct. We need to throttle things until
|
---|
91 | the system clears enough resources to handle this new socket */
|
---|
92 | sleep(1);
|
---|
93 | return;
|
---|
94 | }
|
---|
95 |
|
---|
96 | pid = fork();
|
---|
97 |
|
---|
98 | if (pid != 0) {
|
---|
99 | /* parent or error code ... */
|
---|
100 | talloc_free(sock2);
|
---|
101 | /* go back to the event loop */
|
---|
102 | return;
|
---|
103 | }
|
---|
104 |
|
---|
105 | pid = getpid();
|
---|
106 |
|
---|
107 | /* This is now the child code. We need a completely new event_context to work with */
|
---|
108 |
|
---|
109 | if (tevent_re_initialise(ev) != 0) {
|
---|
110 | smb_panic("Failed to re-initialise tevent after fork");
|
---|
111 | }
|
---|
112 |
|
---|
113 | /* this will free all the listening sockets and all state that
|
---|
114 | is not associated with this new connection */
|
---|
115 | talloc_free(sock);
|
---|
116 |
|
---|
117 | /* we don't care if the dup fails, as its only a select()
|
---|
118 | speed optimisation */
|
---|
119 | socket_dup(sock2);
|
---|
120 |
|
---|
121 | /* tdb needs special fork handling */
|
---|
122 | ldb_wrap_fork_hook();
|
---|
123 |
|
---|
124 | tevent_add_fd(ev, ev, child_pipe[0], TEVENT_FD_READ,
|
---|
125 | standard_pipe_handler, NULL);
|
---|
126 | close(child_pipe[1]);
|
---|
127 |
|
---|
128 | /* Ensure that the forked children do not expose identical random streams */
|
---|
129 | set_need_random_reseed();
|
---|
130 |
|
---|
131 | /* setup the process title */
|
---|
132 | c = socket_get_peer_addr(sock2, ev);
|
---|
133 | s = socket_get_my_addr(sock2, ev);
|
---|
134 | if (s && c) {
|
---|
135 | setproctitle("conn c[%s:%u] s[%s:%u] server_id[%d]",
|
---|
136 | c->addr, c->port, s->addr, s->port, (int)pid);
|
---|
137 | }
|
---|
138 | talloc_free(c);
|
---|
139 | talloc_free(s);
|
---|
140 |
|
---|
141 | /* setup this new connection. Cluster ID is PID based for this process modal */
|
---|
142 | new_conn(ev, lp_ctx, sock2, cluster_id(pid, 0), private_data);
|
---|
143 |
|
---|
144 | /* we can't return to the top level here, as that event context is gone,
|
---|
145 | so we now process events in the new event context until there are no
|
---|
146 | more to process */
|
---|
147 | event_loop_wait(ev);
|
---|
148 |
|
---|
149 | talloc_free(ev);
|
---|
150 | exit(0);
|
---|
151 | }
|
---|
152 |
|
---|
153 | /*
|
---|
154 | called to create a new server task
|
---|
155 | */
|
---|
156 | static void standard_new_task(struct tevent_context *ev,
|
---|
157 | struct loadparm_context *lp_ctx,
|
---|
158 | const char *service_name,
|
---|
159 | void (*new_task)(struct tevent_context *, struct loadparm_context *lp_ctx, struct server_id , void *),
|
---|
160 | void *private_data)
|
---|
161 | {
|
---|
162 | pid_t pid;
|
---|
163 |
|
---|
164 | pid = fork();
|
---|
165 |
|
---|
166 | if (pid != 0) {
|
---|
167 | /* parent or error code ... go back to the event loop */
|
---|
168 | return;
|
---|
169 | }
|
---|
170 |
|
---|
171 | pid = getpid();
|
---|
172 |
|
---|
173 | /* this will free all the listening sockets and all state that
|
---|
174 | is not associated with this new connection */
|
---|
175 | if (tevent_re_initialise(ev) != 0) {
|
---|
176 | smb_panic("Failed to re-initialise tevent after fork");
|
---|
177 | }
|
---|
178 |
|
---|
179 | /* ldb/tdb need special fork handling */
|
---|
180 | ldb_wrap_fork_hook();
|
---|
181 |
|
---|
182 | tevent_add_fd(ev, ev, child_pipe[0], TEVENT_FD_READ,
|
---|
183 | standard_pipe_handler, NULL);
|
---|
184 | close(child_pipe[1]);
|
---|
185 |
|
---|
186 | /* Ensure that the forked children do not expose identical random streams */
|
---|
187 | set_need_random_reseed();
|
---|
188 |
|
---|
189 | setproctitle("task %s server_id[%d]", service_name, (int)pid);
|
---|
190 |
|
---|
191 | /* setup this new task. Cluster ID is PID based for this process modal */
|
---|
192 | new_task(ev, lp_ctx, cluster_id(pid, 0), private_data);
|
---|
193 |
|
---|
194 | /* we can't return to the top level here, as that event context is gone,
|
---|
195 | so we now process events in the new event context until there are no
|
---|
196 | more to process */
|
---|
197 | event_loop_wait(ev);
|
---|
198 |
|
---|
199 | talloc_free(ev);
|
---|
200 | exit(0);
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 | /* called when a task goes down */
|
---|
205 | _NORETURN_ static void standard_terminate(struct tevent_context *ev, struct loadparm_context *lp_ctx,
|
---|
206 | const char *reason)
|
---|
207 | {
|
---|
208 | DEBUG(2,("standard_terminate: reason[%s]\n",reason));
|
---|
209 |
|
---|
210 | talloc_free(ev);
|
---|
211 |
|
---|
212 | /* this reload_charcnv() has the effect of freeing the iconv context memory,
|
---|
213 | which makes leak checking easier */
|
---|
214 | reload_charcnv(lp_ctx);
|
---|
215 |
|
---|
216 | /* terminate this process */
|
---|
217 | exit(0);
|
---|
218 | }
|
---|
219 |
|
---|
220 | /* called to set a title of a task or connection */
|
---|
221 | static void standard_set_title(struct tevent_context *ev, const char *title)
|
---|
222 | {
|
---|
223 | if (title) {
|
---|
224 | setproctitle("%s", title);
|
---|
225 | } else {
|
---|
226 | setproctitle(NULL);
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | static const struct model_ops standard_ops = {
|
---|
231 | .name = "standard",
|
---|
232 | .model_init = standard_model_init,
|
---|
233 | .accept_connection = standard_accept_connection,
|
---|
234 | .new_task = standard_new_task,
|
---|
235 | .terminate = standard_terminate,
|
---|
236 | .set_title = standard_set_title,
|
---|
237 | };
|
---|
238 |
|
---|
239 | /*
|
---|
240 | initialise the standard process model, registering ourselves with the process model subsystem
|
---|
241 | */
|
---|
242 | NTSTATUS process_model_standard_init(void)
|
---|
243 | {
|
---|
244 | return register_process_model(&standard_ops);
|
---|
245 | }
|
---|