Menu

[r362]: / framework / trunk / manager.cpp  Maximize  Restore  History

Download this file

455 lines (389 with data), 8.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#include "manager.h"
#include "cgicc_connection.h"
#include <poll.h>
#include <signal.h>
#include <errno.h>
#include "scgi.h"
#include <unistd.h>
#include <stdlib.h>
#include <semaphore.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <sys/types.h>
#include "thread_cache.h"
#include "process_cache.h"
namespace cppcms {
namespace details {
fast_cgi_application *fast_cgi_application::handlers_owner=NULL;
fast_cgi_application::fast_cgi_application(cgi_api &a) : api(a)
{
}
fast_cgi_application::event_t fast_cgi_application::wait()
{
while(!the_end) {
struct pollfd fds;
fds.fd=api.get_socket();
fds.events=POLLIN | POLLERR;
fds.revents=0;
/* Wait for two events:
* 1. New connection and then do accept
* 2. Exit message - exit and do clean up */
if(poll(&fds,1,-1)<0) {
if(errno==EINTR && !the_end)
continue;
return EXIT;
}
else if(fds.revents) {
if(fds.revents & POLLERR)
return EXIT;
return ACCEPT;
}
}
return EXIT;
}
void fast_cgi_application::shutdown()
{
// Rise exit signal on
// the selfpipe
the_end=true;
}
void fast_cgi_application::handler(int id)
{
fast_cgi_application *ptr=fast_cgi_application::get_instance();
if(ptr) {
ptr->shutdown();
}
}
void fast_cgi_application::set_signal_handlers()
{
handlers_owner=this;
/* Signals defined by standard */
signal(SIGTERM,handler);
signal(SIGUSR1,handler);
/* Additional signal */
signal(SIGINT,handler);
}
fast_cgi_single_threaded_app::fast_cgi_single_threaded_app(base_factory const &factory,cache_factory const &cf,cgi_api &a) :
fast_cgi_application(a)
{
worker=factory(cf);
}
bool fast_cgi_single_threaded_app::run()
{
// Blocking loop
event_t event=wait();
if(event==EXIT) {
return false;
} // ELSE event==ACCEPT
cgi_session *session=api.accept_session();
if(session) {
try {
if(session->prepare()) {
worker->run(session->get_connection());
}
}
catch(...) {
delete session;
throw;
}
delete session;
}
return true;
};
void fast_cgi_application::execute()
{
the_end=false;
set_signal_handlers();
while(run()){
/* Do Continue */
}
}
fast_cgi_multiple_threaded_app::fast_cgi_multiple_threaded_app( int threads_num,
int buffer,
base_factory const &factory,cache_factory const &cf,
cgi_api &a) :
fast_cgi_application(a)
{
int i;
threads_info=NULL;
pids=NULL;
size=threads_num;
// Init Worker Threads
workers.resize(size);
for(i=0;i<size;i++) {
workers[i]=factory(cf);
}
// Setup Jobs Manager
jobs.init(buffer);
start_threads();
}
void *fast_cgi_multiple_threaded_app::thread_func(void *p)
{
info_t *params=(info_t *)p;
int id=params->first;
fast_cgi_multiple_threaded_app *self=params->second;
cgi_session *session;
while((session=self->jobs.pop())!=NULL) {
try{
if(session->prepare()){
self->workers[id]->run(session->get_connection());
}
}
catch(...){
delete session;
return NULL;
}
delete session;
}
return NULL;
}
void fast_cgi_multiple_threaded_app::start_threads()
{
int i;
pids = new pthread_t [size];
threads_info = new info_t [size];
for(i=0;i<size;i++) {
threads_info[i].first=i;
threads_info[i].second=this;
pthread_create(pids+i,NULL,thread_func,threads_info+i);
}
}
void fast_cgi_multiple_threaded_app::wait_threads()
{
int i;
for(i=0;i<size;i++) {
pthread_join(pids[i],NULL);
}
}
bool fast_cgi_multiple_threaded_app::run()
{
if(wait()==ACCEPT) {
cgi_session *session=api.accept_session();
if(session){
jobs.push(session);
}
return true;
}
else {// Exit event
int i;
for(i=0;i<size;i++) {
jobs.push(NULL);
}
wait_threads();
return false;
}
}
// Single instance of prefork
prefork *prefork::self;
void prefork::parent_handler(int s_catched)
{
int i;
int s;
if(self->exit_flag==0) {
self->exit_flag=1;
s=SIGTERM;
}
else {
s=SIGKILL;
}
signal(SIGALRM,parent_handler);
alarm(3);
for(i=0;i<self->procs;i++) {
kill(self->pids[i],s);
}
}
void prefork::chaild_handler(int s)
{
self->exit_flag=1;
signal(SIGALRM,chaild_handler);
alarm(1);
}
void prefork::run()
{
signal(SIGTERM,chaild_handler);
shared_ptr<worker_thread> worker=factory(cache);
int res,post_on_throw;
int limit=global_config.lval("server.iterations_limit",-1);
if(limit!=-1) {
srand(getpid());
limit=limit+(limit / 10 *(rand() % 100))/100;
}
int counter=0;
while(!exit_flag){
if(limit!=-1 && counter>limit)
return;
counter++;
res=sem_wait(semaphore);
if(res<0){
if(errno==EINTR)
continue;
else {
perror("sem_wait");
exit(1);
}
}
cgi_session *session=NULL;
try{
post_on_throw=1;
struct pollfd fds;
fds.fd=api.get_socket();
fds.revents=0;
fds.events=POLLIN | POLLERR;
if(poll(&fds,1,-1)==1 && (fds.revents & POLLIN)) {
session=api.accept_session();
}
post_on_throw=0;
sem_post(semaphore);
if(session && session->prepare()) {
worker->run(session->get_connection());
}
}
catch(cppcms_error const &e){
if(post_on_throw) sem_post(semaphore);
cerr<<e.what();
}
catch(...){
if(post_on_throw) sem_post(semaphore);
exit(1);
}
delete session;
}
}
void prefork::execute()
{
int i;
void *mem=mmap(NULL,sizeof(sem_t),PROT_READ | PROT_WRITE,
MAP_SHARED |MAP_ANONYMOUS,-1,0);
if(mem==MAP_FAILED) {
throw cppcms_error(errno,"mmap failed");
}
semaphore=(sem_t*)mem;
sem_init(semaphore,1,1);
for(i=0;i<procs;i++) {
pid_t pid=fork();
if(pid<0) {
perror("fork:");
int j;
for(j=0;j<i;j++) {
kill(pids[j],SIGKILL);
wait(NULL);
}
exit(1);
}
if(pid>0) {
pids[i]=pid;
}
else { // pid==0
run();
return;
}
}
/* Signals defined by standard */
signal(SIGTERM,parent_handler);
signal(SIGUSR1,parent_handler);
/* Additional signal */
signal(SIGINT,parent_handler);
while(!prefork::exit_flag) {
int stat;
pid_t pid=wait(&stat);
if(pid<0) {
continue;
}
if(exit_flag) break;
for(i=0;i<procs;i++) {
if(pids[i]==pid) {
if(!WIFEXITED(stat) || WEXITSTATUS(stat)!=0){
if(WIFEXITED(stat)) {
cerr<<"Chaild "<<pid<<" exited with "<<WEXITSTATUS(stat)<<endl;
}
else if(WIFSIGNALED(stat)) {
cerr<<"Chaild "<<pid<<" killed by "<<WTERMSIG(stat)<<endl;
}
else {
cerr<<"Chaild "<<pid<<" exited for unknown reason"<<endl;
}
}
pid=fork();
if(pid==0) {
run();
return;
}
pids[i]=pid;
break;
}
}
}
for(i=0;i<procs;i++) {
while(wait(NULL)<0 && errno==EINTR)
;
}
sem_close(semaphore);
munmap(mem,sizeof(sem_t));
}
} // END oF Details
static cache_factory *get_cache_factory()
{
string backend=global_config.sval("cache.backend","none");
if(backend=="none") {
return new cache_factory();
}
else if(backend=="threaded") {
int n=global_config.lval("cache.limit",100);
return new thread_cache_factory(n);
}
else if(backend=="fork") {
size_t s=global_config.lval("cache.memsize",64);
string f=global_config.sval("cache.file","");
return new process_cache_factory(s*1024U,f=="" ? NULL: f.c_str());
}
else {
throw cppcms_error("Unkown cache backend:" + backend);
}
}
void run_application(int argc,char *argv[],base_factory const &factory)
{
int n,max;
global_config.load(argc,argv);
string api=global_config.sval("server.api") ;
auto_ptr<cache_factory> cf(get_cache_factory());
if(api=="cgi") {
shared_ptr<worker_thread> ptr=factory(*cf);
cgicc_connection_cgi cgi;
ptr->run(cgi);
}
else
{
auto_ptr<cgi_api> capi;
if(api=="fastcgi" || api=="scgi" ) {
string socket=global_config.sval("server.socket","");
int backlog=global_config.lval("server.buffer",1);
if(api=="fastcgi")
capi=auto_ptr<cgi_api>(new fcgi_api(socket.c_str(),backlog));
else
capi=auto_ptr<cgi_api>(new scgi_api(socket.c_str(),backlog));
}
else {
throw cppcms_error("Unknown api:"+api);
}
string mod=global_config.sval("server.mod");
if(mod=="process") {
details::fast_cgi_single_threaded_app app(factory,*cf.get(),*capi.get());
app.execute();
}
else if(mod=="thread") {
n=global_config.lval("server.threads",5);
max=global_config.lval("server.buffer",1);
details::fast_cgi_multiple_threaded_app app(n,max,factory,*cf.get(),*capi.get());
app.execute();
}
else if(mod=="prefork") {
n=global_config.lval("server.procs",5);
details::prefork app(factory,*cf.get(),*capi.get(),n);
app.execute();
}
else {
throw cppcms_error("Unknown mod:" + mod);
}
}
};
} // END OF CPPCMS
MongoDB Logo MongoDB