2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
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>.
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
18 #include "dispatcher.h"
29 #include <utils/linked_list.h>
31 typedef struct private_dispatcher_t private_dispatcher_t
;
34 * private data of the task manager
36 struct private_dispatcher_t
{
54 * number of threads in "threads"
59 * session locking mutex
61 pthread_mutex_t mutex
;
66 linked_list_t
*sessions
;
74 * running in debug mode?
79 * List of controllers controller_constructor_t
81 linked_list_t
*controllers
;
84 * List of filters filter_constructor_t
86 linked_list_t
*filters
;
89 * constructor function to create session context (in controller_entry_t)
91 context_constructor_t context_constructor
;
94 * user param to context constructor
100 /** constructor function */
101 controller_constructor_t constructor
;
102 /** parameter to constructor */
104 } controller_entry_t
;
107 /** constructor function */
108 filter_constructor_t constructor
;
109 /** parameter to constructor */
114 /** session instance */
116 /** condvar to wait for session */
118 /** client host address, to prevent session hijacking */
120 /** TRUE if session is in use */
122 /** last use of the session */
124 /** has the session been closed by the handler? */
129 * create a session and instanciate controllers
131 static session_t
* load_session(private_dispatcher_t
*this)
133 iterator_t
*iterator
;
134 controller_entry_t
*centry
;
135 filter_entry_t
*fentry
;
137 context_t
*context
= NULL
;
138 controller_t
*controller
;
141 if (this->context_constructor
)
143 context
= this->context_constructor(this->param
);
145 session
= session_create(context
);
147 iterator
= this->controllers
->create_iterator(this->controllers
, TRUE
);
148 while (iterator
->iterate(iterator
, (void**)¢ry
))
150 controller
= centry
->constructor(context
, centry
->param
);
151 session
->add_controller(session
, controller
);
153 iterator
->destroy(iterator
);
155 iterator
= this->filters
->create_iterator(this->filters
, TRUE
);
156 while (iterator
->iterate(iterator
, (void**)&fentry
))
158 filter
= fentry
->constructor(context
, fentry
->param
);
159 session
->add_filter(session
, filter
);
161 iterator
->destroy(iterator
);
167 * create a new session entry
169 static session_entry_t
*session_entry_create(private_dispatcher_t
*this,
172 session_entry_t
*entry
;
174 entry
= malloc_thing(session_entry_t
);
175 entry
->in_use
= FALSE
;
176 entry
->closed
= FALSE
;
177 pthread_cond_init(&entry
->cond
, NULL
);
178 entry
->session
= load_session(this);
179 entry
->used
= time(NULL
);
180 entry
->host
= strdup(host
);
185 static void session_entry_destroy(session_entry_t
*entry
)
187 entry
->session
->destroy(entry
->session
);
193 * Implementation of dispatcher_t.add_controller.
195 static void add_controller(private_dispatcher_t
*this,
196 controller_constructor_t constructor
, void *param
)
198 controller_entry_t
*entry
= malloc_thing(controller_entry_t
);
200 entry
->constructor
= constructor
;
201 entry
->param
= param
;
202 this->controllers
->insert_last(this->controllers
, entry
);
206 * Implementation of dispatcher_t.add_filter.
208 static void add_filter(private_dispatcher_t
*this,
209 filter_constructor_t constructor
, void *param
)
211 filter_entry_t
*entry
= malloc_thing(filter_entry_t
);
213 entry
->constructor
= constructor
;
214 entry
->param
= param
;
215 this->filters
->insert_last(this->filters
, entry
);
219 * Actual dispatching code
221 static void dispatch(private_dispatcher_t
*this)
223 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, NULL
);
228 session_entry_t
*current
, *found
= NULL
;
229 iterator_t
*iterator
;
233 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, NULL
);
234 request
= request_create(this->fd
, this->debug
);
235 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, NULL
);
241 sid
= request
->get_cookie(request
, "SID");
245 pthread_mutex_lock(&this->mutex
);
246 iterator
= this->sessions
->create_iterator(this->sessions
, TRUE
);
247 while (iterator
->iterate(iterator
, (void**)¤t
))
249 /* check all sessions for timeout or close flag
250 * TODO: use a seperate cleanup thread */
251 if (!current
->in_use
&&
252 (current
->used
< now
- this->timeout
|| current
->closed
))
254 iterator
->remove(iterator
);
255 session_entry_destroy(current
);
258 /* find by session ID. Prevent session hijacking by host check */
260 streq(current
->session
->get_sid(current
->session
), sid
) &&
261 streq(current
->host
, request
->get_host(request
)))
266 iterator
->destroy(iterator
);
270 /* wait until session is unused */
271 while (found
->in_use
)
273 pthread_cond_wait(&found
->cond
, &this->mutex
);
277 { /* create a new session if not found */
278 found
= session_entry_create(this, request
->get_host(request
));
279 this->sessions
->insert_first(this->sessions
, found
);
281 found
->in_use
= TRUE
;
282 pthread_mutex_unlock(&this->mutex
);
284 /* start processing */
285 found
->session
->process(found
->session
, request
);
286 found
->used
= time(NULL
);
288 /* release session */
289 pthread_mutex_lock(&this->mutex
);
290 found
->in_use
= FALSE
;
291 found
->closed
= request
->session_closed(request
);
292 pthread_cond_signal(&found
->cond
);
293 pthread_mutex_unlock(&this->mutex
);
296 request
->destroy(request
);
301 * Implementation of dispatcher_t.run.
303 static void run(private_dispatcher_t
*this, int threads
)
305 this->thread_count
= threads
;
306 this->threads
= malloc(sizeof(pthread_t
) * threads
);
309 if (pthread_create(&this->threads
[threads
- 1],
310 NULL
, (void*)dispatch
, this) == 0)
318 * Implementation of dispatcher_t.waitsignal.
320 static void waitsignal(private_dispatcher_t
*this)
326 sigaddset(&set
, SIGINT
);
327 sigaddset(&set
, SIGTERM
);
328 sigaddset(&set
, SIGHUP
);
329 sigprocmask(SIG_BLOCK
, &set
, NULL
);
334 * Implementation of dispatcher_t.destroy
336 static void destroy(private_dispatcher_t
*this)
338 FCGX_ShutdownPending();
339 while (this->thread_count
--)
341 pthread_cancel(this->threads
[this->thread_count
]);
342 pthread_join(this->threads
[this->thread_count
], NULL
);
344 this->sessions
->destroy_function(this->sessions
, (void*)session_entry_destroy
);
345 this->controllers
->destroy_function(this->controllers
, free
);
346 this->filters
->destroy_function(this->filters
, free
);
354 dispatcher_t
*dispatcher_create(char *socket
, bool debug
, int timeout
,
355 context_constructor_t constructor
, void *param
)
357 private_dispatcher_t
*this = malloc_thing(private_dispatcher_t
);
359 this->public.add_controller
= (void(*)(dispatcher_t
*, controller_constructor_t
, void*))add_controller
;
360 this->public.add_filter
= (void(*)(dispatcher_t
*,filter_constructor_t constructor
, void *param
))add_filter
;
361 this->public.run
= (void(*)(dispatcher_t
*, int threads
))run
;
362 this->public.waitsignal
= (void(*)(dispatcher_t
*))waitsignal
;
363 this->public.destroy
= (void(*)(dispatcher_t
*))destroy
;
365 this->sessions
= linked_list_create();
366 this->controllers
= linked_list_create();
367 this->filters
= linked_list_create();
368 this->context_constructor
= constructor
;
369 pthread_mutex_init(&this->mutex
, NULL
);
372 this->timeout
= timeout
;
374 this->threads
= NULL
;
381 this->fd
= FCGX_OpenSocket(socket
, 10);
383 return &this->public;