s4-ldb: ldap attribute names can contain a '.'
[Samba/aatanasov.git] / source4 / smbd / process_standard.c
blob730e185e5a2e312cdea048f83162e8327efc7eea
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 "../tdb/include/tdb.h"
27 #include "lib/socket/socket.h"
28 #include "smbd/process_model.h"
29 #include "param/secrets.h"
30 #include "system/filesys.h"
31 #include "cluster/cluster.h"
32 #include "param/param.h"
34 #ifdef HAVE_SETPROCTITLE
35 #ifdef HAVE_SETPROCTITLE_H
36 #include <setproctitle.h>
37 #endif
38 #else
39 #define setproctitle none_setproctitle
40 static int none_setproctitle(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);
41 static int none_setproctitle(const char *fmt, ...)
43 return 0;
45 #endif
47 /* we hold a pipe open in the parent, and the any child
48 processes wait for EOF on that pipe. This ensures that
49 children die when the parent dies */
50 static int child_pipe[2];
53 called when the process model is selected
55 static void standard_model_init(struct tevent_context *ev)
57 pipe(child_pipe);
58 signal(SIGCHLD, SIG_IGN);
62 handle EOF on the child pipe
64 static void standard_pipe_handler(struct tevent_context *event_ctx, struct tevent_fd *fde,
65 uint16_t flags, void *private_data)
67 DEBUG(10,("Child %d exiting\n", (int)getpid()));
68 exit(0);
72 called when a listening socket becomes readable.
74 static void standard_accept_connection(struct tevent_context *ev,
75 struct loadparm_context *lp_ctx,
76 struct socket_context *sock,
77 void (*new_conn)(struct tevent_context *,
78 struct loadparm_context *, struct socket_context *,
79 struct server_id , void *),
80 void *private_data)
82 NTSTATUS status;
83 struct socket_context *sock2;
84 pid_t pid;
85 struct tevent_context *ev2;
86 struct socket_address *c, *s;
88 /* accept an incoming connection. */
89 status = socket_accept(sock, &sock2);
90 if (!NT_STATUS_IS_OK(status)) {
91 DEBUG(0,("standard_accept_connection: accept: %s\n",
92 nt_errstr(status)));
93 /* this looks strange, but is correct. We need to throttle things until
94 the system clears enough resources to handle this new socket */
95 sleep(1);
96 return;
99 pid = fork();
101 if (pid != 0) {
102 /* parent or error code ... */
103 talloc_free(sock2);
104 /* go back to the event loop */
105 return;
108 pid = getpid();
110 /* This is now the child code. We need a completely new event_context to work with */
111 ev2 = s4_event_context_init(NULL);
113 /* the service has given us a private pointer that
114 encapsulates the context it needs for this new connection -
115 everything else will be freed */
116 talloc_steal(ev2, private_data);
117 talloc_steal(private_data, sock2);
119 /* this will free all the listening sockets and all state that
120 is not associated with this new connection */
121 talloc_free(sock);
122 talloc_free(ev);
124 /* we don't care if the dup fails, as its only a select()
125 speed optimisation */
126 socket_dup(sock2);
128 /* tdb needs special fork handling */
129 if (tdb_reopen_all(1) == -1) {
130 DEBUG(0,("standard_accept_connection: tdb_reopen_all failed.\n"));
133 tevent_add_fd(ev2, ev2, child_pipe[0], TEVENT_FD_READ,
134 standard_pipe_handler, NULL);
135 close(child_pipe[1]);
137 /* Ensure that the forked children do not expose identical random streams */
138 set_need_random_reseed();
140 /* setup the process title */
141 c = socket_get_peer_addr(sock2, ev2);
142 s = socket_get_my_addr(sock2, ev2);
143 if (s && c) {
144 setproctitle("conn c[%s:%u] s[%s:%u] server_id[%d]",
145 c->addr, c->port, s->addr, s->port, pid);
147 talloc_free(c);
148 talloc_free(s);
150 /* setup this new connection. Cluster ID is PID based for this process modal */
151 new_conn(ev2, lp_ctx, sock2, cluster_id(pid, 0), private_data);
153 /* we can't return to the top level here, as that event context is gone,
154 so we now process events in the new event context until there are no
155 more to process */
156 event_loop_wait(ev2);
158 talloc_free(ev2);
159 exit(0);
163 called to create a new server task
165 static void standard_new_task(struct tevent_context *ev,
166 struct loadparm_context *lp_ctx,
167 const char *service_name,
168 void (*new_task)(struct tevent_context *, struct loadparm_context *lp_ctx, struct server_id , void *),
169 void *private_data)
171 pid_t pid;
172 struct tevent_context *ev2;
174 pid = fork();
176 if (pid != 0) {
177 /* parent or error code ... go back to the event loop */
178 return;
181 pid = getpid();
183 /* This is now the child code. We need a completely new event_context to work with */
184 ev2 = s4_event_context_init(NULL);
186 /* the service has given us a private pointer that
187 encapsulates the context it needs for this new connection -
188 everything else will be freed */
189 talloc_steal(ev2, private_data);
191 /* this will free all the listening sockets and all state that
192 is not associated with this new connection */
193 talloc_free(ev);
195 /* tdb needs special fork handling */
196 if (tdb_reopen_all(1) == -1) {
197 DEBUG(0,("standard_accept_connection: tdb_reopen_all failed.\n"));
200 tevent_add_fd(ev2, ev2, child_pipe[0], TEVENT_FD_READ,
201 standard_pipe_handler, NULL);
202 close(child_pipe[1]);
204 /* Ensure that the forked children do not expose identical random streams */
205 set_need_random_reseed();
207 setproctitle("task %s server_id[%d]", service_name, pid);
209 /* setup this new task. Cluster ID is PID based for this process modal */
210 new_task(ev2, lp_ctx, cluster_id(pid, 0), private_data);
212 /* we can't return to the top level here, as that event context is gone,
213 so we now process events in the new event context until there are no
214 more to process */
215 event_loop_wait(ev2);
217 talloc_free(ev2);
218 exit(0);
222 /* called when a task goes down */
223 _NORETURN_ static void standard_terminate(struct tevent_context *ev, struct loadparm_context *lp_ctx,
224 const char *reason)
226 DEBUG(2,("standard_terminate: reason[%s]\n",reason));
228 /* this reload_charcnv() has the effect of freeing the iconv context memory,
229 which makes leak checking easier */
230 reload_charcnv(lp_ctx);
232 talloc_free(ev);
234 /* terminate this process */
235 exit(0);
238 /* called to set a title of a task or connection */
239 static void standard_set_title(struct tevent_context *ev, const char *title)
241 if (title) {
242 setproctitle("%s", title);
243 } else {
244 setproctitle(NULL);
248 static const struct model_ops standard_ops = {
249 .name = "standard",
250 .model_init = standard_model_init,
251 .accept_connection = standard_accept_connection,
252 .new_task = standard_new_task,
253 .terminate = standard_terminate,
254 .set_title = standard_set_title,
258 initialise the standard process model, registering ourselves with the process model subsystem
260 NTSTATUS process_model_standard_init(void)
262 return register_process_model(&standard_ops);