r14079: I just found the setproctitle library from alt linux:-)
[jelmer/samba4-debian.git] / source / smbd / process_standard.c
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 2 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, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26 #include "lib/events/events.h"
27 #include "lib/tdb/include/tdb.h"
28 #include "smb_server/smb_server.h"
29 #include "lib/socket/socket.h"
30
31 /* For specifiying event context to GSSAPI below */
32 #include "system/kerberos.h"
33 #include "heimdal/lib/gssapi/gssapi_locl.h"
34
35 #include "passdb/secrets.h"
36
37 #ifdef HAVE_SETPROCTITLE
38 #include <setproctitle.h>
39 #define SETPROCTITLE(x) setproctitle x
40 #else
41 #define SETPROCTITLE(x)
42 #endif
43
44 /*
45   called when the process model is selected
46 */
47 static void standard_model_init(struct event_context *ev)
48 {
49         signal(SIGCHLD, SIG_IGN);
50 }
51
52 /*
53   called when a listening socket becomes readable. 
54 */
55 static void standard_accept_connection(struct event_context *ev, 
56                                        struct socket_context *sock, 
57                                        void (*new_conn)(struct event_context *, struct socket_context *, 
58                                                         uint32_t , void *), 
59                                        void *private)
60 {
61         NTSTATUS status;
62         struct socket_context *sock2;
63         pid_t pid;
64         struct event_context *ev2;
65         struct socket_address *c, *s;
66
67         /* accept an incoming connection. */
68         status = socket_accept(sock, &sock2);
69         if (!NT_STATUS_IS_OK(status)) {
70                 DEBUG(0,("standard_accept_connection: accept: %s\n",
71                          nt_errstr(status)));
72                 /* this looks strange, but is correct. We need to throttle things until
73                    the system clears enough resources to handle this new socket */
74                 sleep(1);
75                 return;
76         }
77
78         pid = fork();
79
80         if (pid != 0) {
81                 /* parent or error code ... */
82                 talloc_free(sock2);
83                 /* go back to the event loop */
84                 return;
85         }
86
87         pid = getpid();
88
89         /* This is now the child code. We need a completely new event_context to work with */
90         ev2 = event_context_init(NULL);
91
92         /* the service has given us a private pointer that
93            encapsulates the context it needs for this new connection -
94            everything else will be freed */
95         talloc_steal(ev2, private);
96         talloc_steal(private, sock2);
97
98         /* this will free all the listening sockets and all state that
99            is not associated with this new connection */
100         talloc_free(sock);
101         talloc_free(ev);
102
103         /* we don't care if the dup fails, as its only a select()
104            speed optimisation */
105         socket_dup(sock2);
106                         
107         /* tdb needs special fork handling */
108         if (tdb_reopen_all() == -1) {
109                 DEBUG(0,("standard_accept_connection: tdb_reopen_all failed.\n"));
110         }
111
112         /* Hack to ensure that GSSAPI uses the right event context */
113         gssapi_krb5_init_ev(ev2);
114
115         /* Ensure that the forked children do not expose identical random streams */
116         set_need_random_reseed();
117
118         /* setup the process title */
119         c = socket_get_peer_addr(sock2, ev2);
120         s = socket_get_my_addr(sock2, ev2);
121         if (s && c) {
122                 SETPROCTITLE(("conn c[%s:%u] s[%s:%u] server_id[%d]",
123                               c->addr, c->port, s->addr, s->port, pid));
124         }
125         talloc_free(c);
126         talloc_free(s);
127
128         /* setup this new connection */
129         new_conn(ev2, sock2, pid, private);
130
131         /* we can't return to the top level here, as that event context is gone,
132            so we now process events in the new event context until there are no
133            more to process */      
134         event_loop_wait(ev2);
135
136         talloc_free(ev2);
137         exit(0);
138 }
139
140 /*
141   called to create a new server task
142 */
143 static void standard_new_task(struct event_context *ev, 
144                               void (*new_task)(struct event_context *, uint32_t , void *), 
145                               void *private)
146 {
147         pid_t pid;
148         struct event_context *ev2;
149
150         pid = fork();
151
152         if (pid != 0) {
153                 /* parent or error code ... go back to the event loop */
154                 return;
155         }
156
157         pid = getpid();
158
159         /* This is now the child code. We need a completely new event_context to work with */
160         ev2 = event_context_init(NULL);
161
162         /* the service has given us a private pointer that
163            encapsulates the context it needs for this new connection -
164            everything else will be freed */
165         talloc_steal(ev2, private);
166
167         /* this will free all the listening sockets and all state that
168            is not associated with this new connection */
169         talloc_free(ev);
170
171         /* tdb needs special fork handling */
172         if (tdb_reopen_all() == -1) {
173                 DEBUG(0,("standard_accept_connection: tdb_reopen_all failed.\n"));
174         }
175
176         /* Ensure that the forked children do not expose identical random streams */
177         set_need_random_reseed();
178
179         SETPROCTITLE(("task server_id[%d]", pid));
180
181         /* setup this new connection */
182         new_task(ev2, pid, private);
183
184         /* we can't return to the top level here, as that event context is gone,
185            so we now process events in the new event context until there are no
186            more to process */      
187         event_loop_wait(ev2);
188
189         talloc_free(ev2);
190         exit(0);
191 }
192
193
194 /* called when a task goes down */
195 static void standard_terminate(struct event_context *ev, const char *reason) 
196 {
197         DEBUG(2,("standard_terminate: reason[%s]\n",reason));
198
199         /* this init_iconv() has the effect of freeing the iconv context memory,
200            which makes leak checking easier */
201         init_iconv();
202
203         /* the secrets db should really hang off the connection structure */
204         secrets_shutdown();
205
206         talloc_free(ev);
207
208         /* terminate this process */
209         exit(0);
210 }
211
212 /* called to set a title of a task or connection */
213 static void standard_set_title(struct event_context *ev, const char *title) 
214 {
215         if (title) {
216                 SETPROCTITLE(("%s", title));
217         } else {
218                 SETPROCTITLE((NULL));
219         }
220 }
221
222 static const struct model_ops standard_ops = {
223         .name                   = "standard",
224         .model_init             = standard_model_init,
225         .accept_connection      = standard_accept_connection,
226         .new_task               = standard_new_task,
227         .terminate              = standard_terminate,
228         .set_title              = standard_set_title,
229 };
230
231 /*
232   initialise the standard process model, registering ourselves with the process model subsystem
233  */
234 NTSTATUS process_model_standard_init(void)
235 {
236         return register_process_model(&standard_ops);
237 }