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
16 #include "dispatcher.h"
27 #include <utils/linked_list.h>
29 typedef struct private_dispatcher_t private_dispatcher_t
;
32 * private data of the task manager
34 struct private_dispatcher_t
{
52 * number of threads in "threads"
57 * session locking mutex
59 pthread_mutex_t mutex
;
64 linked_list_t
*sessions
;
72 * running in debug mode?
77 * List of controllers controller_constructor_t
79 linked_list_t
*controllers
;
82 * List of filters filter_constructor_t
84 linked_list_t
*filters
;
87 * constructor function to create session context (in controller_entry_t)
89 context_constructor_t context_constructor
;
92 * user param to context constructor
98 /** constructor function */
99 controller_constructor_t constructor
;
100 /** parameter to constructor */
102 } controller_entry_t
;
105 /** constructor function */
106 filter_constructor_t constructor
;
107 /** parameter to constructor */
112 /** session instance */
114 /** condvar to wait for session */
116 /** client host address, to prevent session hijacking */
118 /** TRUE if session is in use */
120 /** last use of the session */
122 /** has the session been closed by the handler? */
127 * create a session and instanciate controllers
129 static session_t
* load_session(private_dispatcher_t
*this)
131 iterator_t
*iterator
;
132 controller_entry_t
*centry
;
133 filter_entry_t
*fentry
;
135 context_t
*context
= NULL
;
136 controller_t
*controller
;
139 if (this->context_constructor
)
141 context
= this->context_constructor(this->param
);
143 session
= session_create(context
);
145 iterator
= this->controllers
->create_iterator(this->controllers
, TRUE
);
146 while (iterator
->iterate(iterator
, (void**)¢ry
))
148 controller
= centry
->constructor(context
, centry
->param
);
149 session
->add_controller(session
, controller
);
151 iterator
->destroy(iterator
);
153 iterator
= this->filters
->create_iterator(this->filters
, TRUE
);
154 while (iterator
->iterate(iterator
, (void**)&fentry
))
156 filter
= fentry
->constructor(context
, fentry
->param
);
157 session
->add_filter(session
, filter
);
159 iterator
->destroy(iterator
);
165 * create a new session entry
167 static session_entry_t
*session_entry_create(private_dispatcher_t
*this,
170 session_entry_t
*entry
;
172 entry
= malloc_thing(session_entry_t
);
173 entry
->in_use
= FALSE
;
174 entry
->closed
= FALSE
;
175 pthread_cond_init(&entry
->cond
, NULL
);
176 entry
->session
= load_session(this);
177 entry
->used
= time(NULL
);
178 entry
->host
= strdup(host
);
183 static void session_entry_destroy(session_entry_t
*entry
)
185 entry
->session
->destroy(entry
->session
);
191 * Implementation of dispatcher_t.add_controller.
193 static void add_controller(private_dispatcher_t
*this,
194 controller_constructor_t constructor
, void *param
)
196 controller_entry_t
*entry
= malloc_thing(controller_entry_t
);
198 entry
->constructor
= constructor
;
199 entry
->param
= param
;
200 this->controllers
->insert_last(this->controllers
, entry
);
204 * Implementation of dispatcher_t.add_filter.
206 static void add_filter(private_dispatcher_t
*this,
207 filter_constructor_t constructor
, void *param
)
209 filter_entry_t
*entry
= malloc_thing(filter_entry_t
);
211 entry
->constructor
= constructor
;
212 entry
->param
= param
;
213 this->filters
->insert_last(this->filters
, entry
);
217 * Actual dispatching code
219 static void dispatch(private_dispatcher_t
*this)
221 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, NULL
);
226 session_entry_t
*current
, *found
= NULL
;
227 iterator_t
*iterator
;
231 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, NULL
);
232 request
= request_create(this->fd
, this->debug
);
233 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, NULL
);
239 sid
= request
->get_cookie(request
, "SID");
243 pthread_mutex_lock(&this->mutex
);
244 iterator
= this->sessions
->create_iterator(this->sessions
, TRUE
);
245 while (iterator
->iterate(iterator
, (void**)¤t
))
247 /* check all sessions for timeout or close flag
248 * TODO: use a seperate cleanup thread */
249 if (!current
->in_use
&&
250 (current
->used
< now
- this->timeout
|| current
->closed
))
252 iterator
->remove(iterator
);
253 session_entry_destroy(current
);
256 /* find by session ID. Prevent session hijacking by host check */
258 streq(current
->session
->get_sid(current
->session
), sid
) &&
259 streq(current
->host
, request
->get_host(request
)))
264 iterator
->destroy(iterator
);
268 /* wait until session is unused */
269 while (found
->in_use
)
271 pthread_cond_wait(&found
->cond
, &this->mutex
);
275 { /* create a new session if not found */
276 found
= session_entry_create(this, request
->get_host(request
));
277 this->sessions
->insert_first(this->sessions
, found
);
279 found
->in_use
= TRUE
;
280 pthread_mutex_unlock(&this->mutex
);
282 /* start processing */
283 found
->session
->process(found
->session
, request
);
284 found
->used
= time(NULL
);
286 /* release session */
287 pthread_mutex_lock(&this->mutex
);
288 found
->in_use
= FALSE
;
289 found
->closed
= request
->session_closed(request
);
290 pthread_cond_signal(&found
->cond
);
291 pthread_mutex_unlock(&this->mutex
);
294 request
->destroy(request
);
299 * Implementation of dispatcher_t.run.
301 static void run(private_dispatcher_t
*this, int threads
)
303 this->thread_count
= threads
;
304 this->threads
= malloc(sizeof(pthread_t
) * threads
);
307 if (pthread_create(&this->threads
[threads
- 1],
308 NULL
, (void*)dispatch
, this) == 0)
316 * Implementation of dispatcher_t.waitsignal.
318 static void waitsignal(private_dispatcher_t
*this)
324 sigaddset(&set
, SIGINT
);
325 sigaddset(&set
, SIGTERM
);
326 sigaddset(&set
, SIGHUP
);
327 sigprocmask(SIG_BLOCK
, &set
, NULL
);
332 * Implementation of dispatcher_t.destroy
334 static void destroy(private_dispatcher_t
*this)
336 FCGX_ShutdownPending();
337 while (this->thread_count
--)
339 pthread_cancel(this->threads
[this->thread_count
]);
340 pthread_join(this->threads
[this->thread_count
], NULL
);
342 this->sessions
->destroy_function(this->sessions
, (void*)session_entry_destroy
);
343 this->controllers
->destroy_function(this->controllers
, free
);
344 this->filters
->destroy_function(this->filters
, free
);
352 dispatcher_t
*dispatcher_create(char *socket
, bool debug
, int timeout
,
353 context_constructor_t constructor
, void *param
)
355 private_dispatcher_t
*this = malloc_thing(private_dispatcher_t
);
357 this->public.add_controller
= (void(*)(dispatcher_t
*, controller_constructor_t
, void*))add_controller
;
358 this->public.add_filter
= (void(*)(dispatcher_t
*,filter_constructor_t constructor
, void *param
))add_filter
;
359 this->public.run
= (void(*)(dispatcher_t
*, int threads
))run
;
360 this->public.waitsignal
= (void(*)(dispatcher_t
*))waitsignal
;
361 this->public.destroy
= (void(*)(dispatcher_t
*))destroy
;
363 this->sessions
= linked_list_create();
364 this->controllers
= linked_list_create();
365 this->filters
= linked_list_create();
366 this->context_constructor
= constructor
;
367 pthread_mutex_init(&this->mutex
, NULL
);
370 this->timeout
= timeout
;
372 this->threads
= NULL
;
379 this->fd
= FCGX_OpenSocket(socket
, 10);
381 return &this->public;