libfast: add a fast_ prefix to all classes, avoiding namespace clashes
[strongswan.git] / src / libfast / fast_dispatcher.c
1 /*
2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "fast_dispatcher.h"
17
18 #include "fast_request.h"
19 #include "fast_session.h"
20
21 #include <fcgiapp.h>
22 #include <signal.h>
23 #include <unistd.h>
24
25 #include <utils/debug.h>
26 #include <threading/thread.h>
27 #include <threading/condvar.h>
28 #include <threading/mutex.h>
29 #include <collections/linked_list.h>
30 #include <collections/hashtable.h>
31
32 /** Intervall to check for expired sessions, in seconds */
33 #define CLEANUP_INTERVAL 30
34
35 typedef struct private_fast_dispatcher_t private_fast_dispatcher_t;
36
37 /**
38 * private data of the task manager
39 */
40 struct private_fast_dispatcher_t {
41
42 /**
43 * public functions
44 */
45 fast_dispatcher_t public;
46
47 /**
48 * fcgi socket fd
49 */
50 int fd;
51
52 /**
53 * thread list
54 */
55 thread_t **threads;
56
57 /**
58 * number of threads in "threads"
59 */
60 int thread_count;
61
62 /**
63 * session locking mutex
64 */
65 mutex_t *mutex;
66
67 /**
68 * Hahstable with active sessions
69 */
70 hashtable_t *sessions;
71
72 /**
73 * session timeout
74 */
75 time_t timeout;
76
77 /**
78 * timestamp of last session cleanup round
79 */
80 time_t last_cleanup;
81
82 /**
83 * running in debug mode?
84 */
85 bool debug;
86
87 /**
88 * List of controllers controller_constructor_t
89 */
90 linked_list_t *controllers;
91
92 /**
93 * List of filters filter_constructor_t
94 */
95 linked_list_t *filters;
96
97 /**
98 * constructor function to create session context (in controller_entry_t)
99 */
100 fast_context_constructor_t context_constructor;
101
102 /**
103 * user param to context constructor
104 */
105 void *param;
106 };
107
108 typedef struct {
109 /** constructor function */
110 fast_controller_constructor_t constructor;
111 /** parameter to constructor */
112 void *param;
113 } controller_entry_t;
114
115 typedef struct {
116 /** constructor function */
117 fast_filter_constructor_t constructor;
118 /** parameter to constructor */
119 void *param;
120 } filter_entry_t;
121
122 typedef struct {
123 /** session instance */
124 fast_session_t *session;
125 /** condvar to wait for session */
126 condvar_t *cond;
127 /** client host address, to prevent session hijacking */
128 char *host;
129 /** TRUE if session is in use */
130 bool in_use;
131 /** last use of the session */
132 time_t used;
133 /** has the session been closed by the handler? */
134 bool closed;
135 } session_entry_t;
136
137 /**
138 * create a session and instanciate controllers
139 */
140 static fast_session_t* load_session(private_fast_dispatcher_t *this)
141 {
142 enumerator_t *enumerator;
143 controller_entry_t *centry;
144 filter_entry_t *fentry;
145 fast_session_t *session;
146 fast_context_t *context = NULL;
147 fast_controller_t *controller;
148 fast_filter_t *filter;
149
150 if (this->context_constructor)
151 {
152 context = this->context_constructor(this->param);
153 }
154 session = fast_session_create(context);
155
156 enumerator = this->controllers->create_enumerator(this->controllers);
157 while (enumerator->enumerate(enumerator, &centry))
158 {
159 controller = centry->constructor(context, centry->param);
160 session->add_controller(session, controller);
161 }
162 enumerator->destroy(enumerator);
163
164 enumerator = this->filters->create_enumerator(this->filters);
165 while (enumerator->enumerate(enumerator, &fentry))
166 {
167 filter = fentry->constructor(context, fentry->param);
168 session->add_filter(session, filter);
169 }
170 enumerator->destroy(enumerator);
171
172 return session;
173 }
174
175 /**
176 * create a new session entry
177 */
178 static session_entry_t *session_entry_create(private_fast_dispatcher_t *this,
179 char *host)
180 {
181 session_entry_t *entry;
182 fast_session_t *session;
183
184 session = load_session(this);
185 if (!session)
186 {
187 return NULL;
188 }
189 INIT(entry,
190 .cond = condvar_create(CONDVAR_TYPE_DEFAULT),
191 .session = session,
192 .host = strdup(host),
193 .used = time_monotonic(NULL),
194 );
195 return entry;
196 }
197
198 /**
199 * destroy a session
200 */
201 static void session_entry_destroy(session_entry_t *entry)
202 {
203 entry->session->destroy(entry->session);
204 entry->cond->destroy(entry->cond);
205 free(entry->host);
206 free(entry);
207 }
208
209 METHOD(fast_dispatcher_t, add_controller, void,
210 private_fast_dispatcher_t *this, fast_controller_constructor_t constructor,
211 void *param)
212 {
213 controller_entry_t *entry;
214
215 INIT(entry,
216 .constructor = constructor,
217 .param = param,
218 );
219 this->controllers->insert_last(this->controllers, entry);
220 }
221
222 METHOD(fast_dispatcher_t, add_filter, void,
223 private_fast_dispatcher_t *this, fast_filter_constructor_t constructor,
224 void *param)
225 {
226 filter_entry_t *entry;
227
228 INIT(entry,
229 .constructor = constructor,
230 .param = param,
231 );
232 this->filters->insert_last(this->filters, entry);
233 }
234
235 /**
236 * Hashtable hash function
237 */
238 static u_int session_hash(char *sid)
239 {
240 return chunk_hash(chunk_create(sid, strlen(sid)));
241 }
242
243 /**
244 * Hashtable equals function
245 */
246 static bool session_equals(char *sid1, char *sid2)
247 {
248 return streq(sid1, sid2);
249 }
250
251 /**
252 * Cleanup unused sessions
253 */
254 static void cleanup_sessions(private_fast_dispatcher_t *this, time_t now)
255 {
256 if (this->last_cleanup < now - CLEANUP_INTERVAL)
257 {
258 char *sid;
259 session_entry_t *entry;
260 enumerator_t *enumerator;
261 linked_list_t *remove;
262
263 this->last_cleanup = now;
264 remove = linked_list_create();
265 enumerator = this->sessions->create_enumerator(this->sessions);
266 while (enumerator->enumerate(enumerator, &sid, &entry))
267 {
268 /* check all sessions for timeout or close flag */
269 if (!entry->in_use &&
270 (entry->used < now - this->timeout || entry->closed))
271 {
272 remove->insert_last(remove, sid);
273 }
274 }
275 enumerator->destroy(enumerator);
276
277 while (remove->remove_last(remove, (void**)&sid) == SUCCESS)
278 {
279 entry = this->sessions->remove(this->sessions, sid);
280 if (entry)
281 {
282 session_entry_destroy(entry);
283 }
284 }
285 remove->destroy(remove);
286 }
287 }
288
289 /**
290 * Actual dispatching code
291 */
292 static void dispatch(private_fast_dispatcher_t *this)
293 {
294 thread_cancelability(FALSE);
295
296 while (TRUE)
297 {
298 fast_request_t *request;
299 session_entry_t *found = NULL;
300 time_t now;
301 char *sid;
302
303 thread_cancelability(TRUE);
304 request = fast_request_create(this->fd, this->debug);
305 thread_cancelability(FALSE);
306
307 if (request == NULL)
308 {
309 continue;
310 }
311 now = time_monotonic(NULL);
312 sid = request->get_cookie(request, "SID");
313
314 this->mutex->lock(this->mutex);
315 if (sid)
316 {
317 found = this->sessions->get(this->sessions, sid);
318 }
319 if (found && !streq(found->host, request->get_host(request)))
320 {
321 found = NULL;
322 }
323 if (found)
324 {
325 /* wait until session is unused */
326 while (found->in_use)
327 {
328 found->cond->wait(found->cond, this->mutex);
329 }
330 }
331 else
332 { /* create a new session if not found */
333 found = session_entry_create(this, request->get_host(request));
334 if (!found)
335 {
336 request->destroy(request);
337 this->mutex->unlock(this->mutex);
338 continue;
339 }
340 sid = found->session->get_sid(found->session);
341 this->sessions->put(this->sessions, sid, found);
342 }
343 found->in_use = TRUE;
344 this->mutex->unlock(this->mutex);
345
346 /* start processing */
347 found->session->process(found->session, request);
348 found->used = time_monotonic(NULL);
349
350 /* release session */
351 this->mutex->lock(this->mutex);
352 found->in_use = FALSE;
353 found->closed = request->session_closed(request);
354 found->cond->signal(found->cond);
355 cleanup_sessions(this, now);
356 this->mutex->unlock(this->mutex);
357
358 request->destroy(request);
359 }
360 }
361
362 METHOD(fast_dispatcher_t, run, void,
363 private_fast_dispatcher_t *this, int threads)
364 {
365 this->thread_count = threads;
366 this->threads = malloc(sizeof(thread_t*) * threads);
367 while (threads)
368 {
369 this->threads[threads - 1] = thread_create((thread_main_t)dispatch,
370 this);
371 if (this->threads[threads - 1])
372 {
373 threads--;
374 }
375 }
376 }
377
378 METHOD(fast_dispatcher_t, waitsignal, void,
379 private_fast_dispatcher_t *this)
380 {
381 sigset_t set;
382 int sig;
383
384 sigemptyset(&set);
385 sigaddset(&set, SIGINT);
386 sigaddset(&set, SIGTERM);
387 sigaddset(&set, SIGHUP);
388 sigprocmask(SIG_BLOCK, &set, NULL);
389 sigwait(&set, &sig);
390 }
391
392 METHOD(fast_dispatcher_t, destroy, void,
393 private_fast_dispatcher_t *this)
394 {
395 char *sid;
396 session_entry_t *entry;
397 enumerator_t *enumerator;
398
399 FCGX_ShutdownPending();
400 while (this->thread_count--)
401 {
402 thread_t *thread = this->threads[this->thread_count];
403 thread->cancel(thread);
404 thread->join(thread);
405 }
406 enumerator = this->sessions->create_enumerator(this->sessions);
407 while (enumerator->enumerate(enumerator, &sid, &entry))
408 {
409 session_entry_destroy(entry);
410 }
411 enumerator->destroy(enumerator);
412 this->sessions->destroy(this->sessions);
413 this->controllers->destroy_function(this->controllers, free);
414 this->filters->destroy_function(this->filters, free);
415 this->mutex->destroy(this->mutex);
416 free(this->threads);
417 free(this);
418 }
419
420 /*
421 * see header file
422 */
423 fast_dispatcher_t *fast_dispatcher_create(char *socket, bool debug, int timeout,
424 fast_context_constructor_t constructor, void *param)
425 {
426 private_fast_dispatcher_t *this;
427
428 INIT(this,
429 .public = {
430 .add_controller = _add_controller,
431 .add_filter = _add_filter,
432 .run = _run,
433 .waitsignal = _waitsignal,
434 .destroy = _destroy,
435 },
436 .sessions = hashtable_create((void*)session_hash,
437 (void*)session_equals, 4096),
438 .controllers = linked_list_create(),
439 .filters = linked_list_create(),
440 .context_constructor = constructor,
441 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
442 .param = param,
443 .timeout = timeout,
444 .last_cleanup = time_monotonic(NULL),
445 .debug = debug,
446 );
447
448 FCGX_Init();
449
450 if (socket)
451 {
452 unlink(socket);
453 this->fd = FCGX_OpenSocket(socket, 10);
454 }
455 return &this->public;
456 }