1 #include "osrf_prefork.h"
4 /* true if we just deleted a child. This will allow us to make sure we're
5 not trying to use freed memory */
9 void sigchld_handler( int sig );
11 int osrf_prefork_run(char* appname) {
13 if(!appname) fatal_handler("osrf_prefork_run requires an appname to run!");
19 info_handler("Loading config in osrf_forker for app %s", appname);
21 object* max_req = osrf_settings_host_value_object("/apps/%s/unix_config/max_requests", appname);
22 object* min_children = osrf_settings_host_value_object("/apps/%s/unix_config/min_children", appname);
23 object* max_children = osrf_settings_host_value_object("/apps/%s/unix_config/max_children", appname);
25 if(!max_req) warning_handler("Max requests not defined, assuming 1000");
26 else maxr = max_req->num_value;
28 if(!min_children) warning_handler("Min children not defined, assuming 3");
29 else minc = min_children->num_value;
31 if(!max_children) warning_handler("Max children not defined, assuming 10");
32 else maxc = max_children->num_value;
35 free_object(min_children);
36 free_object(max_children);
37 /* --------------------------------------------------- */
39 char* resc = va_list_to_string("%s_listener", appname);
41 if(!osrf_system_bootstrap_client_resc(
42 osrf_get_bootstrap_config(), osrf_get_config_context(), resc))
43 fatal_handler("Unable to bootstrap client for osrf_prefork_run()");
46 prefork_simple* forker = prefork_simple_init(
47 osrf_system_get_transport_client(), maxr, minc, maxc);
49 forker->appname = strdup(appname);
52 fatal_handler("osrf_prefork_run() failed to create prefork_simple object");
54 prefork_launch_children(forker);
56 info_handler("Launching osrf_forker for app %s", appname);
59 warning_handler("prefork_run() retuned - how??");
65 void osrf_prefork_register_routers() {
66 //char* router = osrf_config_value("//%s/
69 void prefork_child_init_hook(prefork_child* child) {
72 info_handler("Child init hook for child %d", child->pid);
73 char* resc = va_list_to_string("%s_drone",child->appname);
74 if(!osrf_system_bootstrap_client_resc(
75 osrf_get_bootstrap_config(), osrf_get_config_context(), resc))
76 fatal_handler("Unable to bootstrap client for osrf_prefork_run()");
80 void prefork_child_process_request(prefork_child* child, char* data) {
81 if(!child && child->connection) return;
83 /* construct the message from the xml */
84 debug_handler("Child %d received data from parent:\n%s\n", child->pid, data);
85 transport_message* msg = new_message_from_xml( data );
87 osrf_stack_transport_handler(msg, child->appname);
90 transport_message* ret_msg = message_init(
91 msg->body, msg->subject, msg->thread, msg->sender, NULL );
93 client_send_message(child->connection, ret_msg);
94 message_free( ret_msg );
96 printf("Message body size %d\n", strlen(msg->body));
98 printf( "Message Info\n" );
99 printf( "%s\n", msg->sender );
100 printf( "%s\n", msg->recipient );
101 printf( "%s\n", msg->thread );
102 printf( "%s\n", msg->body );
103 printf( "%s\n", msg->subject );
104 printf( "%s\n", msg->router_from );
105 printf( "%d\n", msg->broadcast );
112 prefork_simple* prefork_simple_init( transport_client* client,
113 int max_requests, int min_children, int max_children ) {
115 if( min_children > max_children )
116 fatal_handler( "min_children (%d) is greater "
117 "than max_children (%d)", min_children, max_children );
119 if( max_children > ABS_MAX_CHILDREN )
120 fatal_handler( "max_children (%d) is greater than ABS_MAX_CHILDREN (%d)",
121 max_children, ABS_MAX_CHILDREN );
123 /* flesh out the struct */
124 prefork_simple* prefork = (prefork_simple*) safe_malloc(sizeof(prefork_simple));
125 prefork->max_requests = max_requests;
126 prefork->min_children = min_children;
127 prefork->max_children = max_children;
128 prefork->first_child = NULL;
129 prefork->connection = client;
134 prefork_child* launch_child( prefork_simple* forker ) {
140 /* Set up the data pipes and add the child struct to the parent */
141 if( pipe(data_fd) < 0 ) /* build the data pipe*/
142 fatal_handler( "Pipe making error" );
144 if( pipe(status_fd) < 0 ) /* build the status pipe */
145 fatal_handler( "Pipe making error" );
147 debug_handler( "Pipes: %d %d %d %d", data_fd[0], data_fd[1], status_fd[0], status_fd[1] );
148 prefork_child* child = prefork_child_init( forker->max_requests, data_fd[0],
149 data_fd[1], status_fd[0], status_fd[1] );
151 child->appname = strdup(forker->appname);
154 add_prefork_child( forker, child );
156 if( (pid=fork()) < 0 ) fatal_handler( "Forking Error" );
158 if( pid > 0 ) { /* parent */
160 signal(SIGCHLD, sigchld_handler);
161 (forker->current_num_children)++;
164 info_handler( "Parent launched %d", pid );
165 /* *no* child pipe FD's can be closed or the parent will re-use fd's that
166 the children are currently using */
172 debug_handler("I am new child with read_data_fd = %d and write_status_fd = %d",
173 child->read_data_fd, child->write_status_fd );
175 child->pid = getpid();
176 close( child->write_data_fd );
177 close( child->read_status_fd );
180 prefork_child_init_hook(child);
182 prefork_child_wait( child );
183 exit(0); /* just to be sure */
189 void prefork_launch_children( prefork_simple* forker ) {
192 while( c++ < forker->min_children )
193 launch_child( forker );
197 void sigchld_handler( int sig ) {
198 signal(SIGCHLD, sigchld_handler);
203 void reap_children( prefork_simple* forker ) {
208 while( (child_pid=waitpid(-1,&status,WNOHANG)) > 0)
209 del_prefork_child( forker, child_pid );
212 while( forker->current_num_children < forker->min_children )
213 launch_child( forker );
218 void prefork_run(prefork_simple* forker) {
220 if( forker->first_child == NULL )
223 transport_message* cur_msg = NULL;
227 if( forker->first_child == NULL ) {/* no more children */
228 warning_handler("No more children..." );
232 debug_handler("Forker going into wait for data...");
234 cur_msg = client_recv( forker->connection, -1 );
236 fprintf(stderr, "Got Data %f\n", get_timestamp_millis() );
238 if( cur_msg == NULL ) continue;
240 int honored = 0; /* true if we've serviced the request */
244 check_children( forker );
246 debug_handler( "Server received inbound data" );
248 prefork_child* cur_child = forker->first_child;
250 /* Look for an available child */
251 for( k = 0; k < forker->current_num_children; k++ ) {
253 debug_handler("Searching for available child. cur_child->pid = %d", cur_child->pid );
254 debug_handler("Current num children %d and loop %d", forker->current_num_children, k);
256 if( cur_child->available ) {
257 debug_handler( "sending data to %d", cur_child->pid );
258 message_prepare_xml( cur_msg );
259 char* data = cur_msg->msg_xml;
260 if( ! data || strlen(data) < 1 ) break;
261 cur_child->available = 0;
262 debug_handler( "Writing to child fd %d", cur_child->write_data_fd );
265 fprintf(stderr, "Writing Data %f\n", get_timestamp_millis() );
266 if( (written = write( cur_child->write_data_fd, data, strlen(data) + 1 )) < 0 ) {
267 warning_handler("Write returned error %d", errno);
268 cur_child = cur_child->next;
272 fprintf(stderr, "Wrote %d bytes to child\n", written);
274 forker->first_child = cur_child->next;
278 cur_child = cur_child->next;
281 /* if none available, add a new child if we can */
283 debug_handler("Not enough children, attempting to add...");
284 if( forker->current_num_children < forker->max_children ) {
285 debug_handler( "Launching new child with current_num = %d",
286 forker->current_num_children );
288 prefork_child* new_child = launch_child( forker );
289 message_prepare_xml( cur_msg );
290 char* data = cur_msg->msg_xml;
291 if( ! data || strlen(data) < 1 ) break;
292 new_child->available = 0;
293 debug_handler( "sending data to %d", new_child->pid );
294 debug_handler( "Writing to new child fd %d", new_child->write_data_fd );
295 write( new_child->write_data_fd, data, strlen(data) + 1 );
296 forker->first_child = new_child->next;
302 warning_handler( "No children available, sleeping and looping..." );
303 usleep( 50000 ); /* 50 milliseconds */
307 reap_children(forker);
310 fprintf(stderr, "Parent done with request %f\n", get_timestamp_millis() );
314 } /* top level listen loop */
319 void check_children( prefork_simple* forker ) {
334 reap_children(forker);
336 prefork_child* cur_child = forker->first_child;
339 for( i = 0; i!= forker->current_num_children; i++ ) {
341 if( cur_child->read_status_fd > max_fd )
342 max_fd = cur_child->read_status_fd;
343 FD_SET( cur_child->read_status_fd, &read_set );
344 cur_child = cur_child->next;
347 FD_CLR(0,&read_set);/* just to be sure */
349 if( (select_ret=select( max_fd + 1 , &read_set, NULL, NULL, &tv)) == -1 ) {
350 warning_handler( "Select returned error %d on check_children", errno );
353 if( select_ret == 0 )
356 /* see if one of a child has told us it's done */
357 cur_child = forker->first_child;
360 for( j = 0; j!= forker->current_num_children && num_handled < select_ret ; j++ ) {
362 if( FD_ISSET( cur_child->read_status_fd, &read_set ) ) {
363 printf( "Server received status from a child %d\n", cur_child->pid );
364 debug_handler( "Server received status from a child %d", cur_child->pid );
368 /* now suck off the data */
371 if( (n=read(cur_child->read_status_fd, buf, 63)) < 0 ) {
372 warning_handler("Read error afer select in child status read with errno %d", errno);
375 debug_handler( "Read %d bytes from status buffer: %s", n, buf );
376 cur_child->available = 1;
378 cur_child = cur_child->next;
384 void prefork_child_wait( prefork_child* child ) {
387 growing_buffer* gbuf = buffer_init( READ_BUFSIZE );
388 char buf[READ_BUFSIZE];
389 memset( buf, 0, READ_BUFSIZE );
391 for( i = 0; i!= child->max_requests; i++ ) {
394 clr_fl(child->read_data_fd, O_NONBLOCK );
395 while( (n=read(child->read_data_fd, buf, READ_BUFSIZE-1)) > 0 ) {
396 buffer_add( gbuf, buf );
397 memset( buf, 0, READ_BUFSIZE );
399 fprintf(stderr, "Child read %d bytes\n", n);
401 if( n == READ_BUFSIZE ) {
402 fprintf(stderr, "We read READ_BUFSIZE data....\n");
404 /* either we have exactly READ_BUFSIZE data,
405 or there's more waiting that we need to grab*/
406 /* must set to non-block for reading more */
408 fprintf(stderr, "Read Data %f\n", get_timestamp_millis() );
409 prefork_child_process_request(child, gbuf->buf);
410 buffer_reset( gbuf );
416 warning_handler( "Child read returned error with errno %d", errno );
420 if( i < child->max_requests - 1 )
421 write( child->write_status_fd, "available" /*less than 64 bytes*/, 9 );
426 debug_handler("Child exiting...[%d]", getpid() );
432 void add_prefork_child( prefork_simple* forker, prefork_child* child ) {
434 if( forker->first_child == NULL ) {
435 forker->first_child = child;
440 /* we put the child in as the last because, regardless,
441 we have to do the DLL splice dance, and this is the
444 prefork_child* start_child = forker->first_child;
446 if( forker->first_child->next == start_child )
448 forker->first_child = forker->first_child->next;
451 /* here we know that forker->first_child is the last element
452 in the list and start_child is the first. Insert the
453 new child between them*/
455 forker->first_child->next = child;
456 child->next = start_child;
460 prefork_child* find_prefork_child( prefork_simple* forker, pid_t pid ) {
462 if( forker->first_child == NULL ) { return NULL; }
463 prefork_child* start_child = forker->first_child;
465 if( forker->first_child->pid == pid )
466 return forker->first_child;
467 } while( (forker->first_child = forker->first_child->next) != start_child );
473 void del_prefork_child( prefork_simple* forker, pid_t pid ) {
475 if( forker->first_child == NULL ) { return; }
477 (forker->current_num_children)--;
478 debug_handler("Deleting Child: %d", pid );
480 prefork_child* start_child = forker->first_child; /* starting point */
481 prefork_child* cur_child = start_child; /* current pointer */
482 prefork_child* prev_child = start_child; /* the trailing pointer */
484 /* special case where there is only one in the list */
485 if( start_child == start_child->next ) {
486 if( start_child->pid == pid ) {
487 forker->first_child = NULL;
489 close( start_child->read_data_fd );
490 close( start_child->write_data_fd );
491 close( start_child->read_status_fd );
492 close( start_child->write_status_fd );
494 prefork_child_free( start_child );
500 /* special case where the first item in the list needs to be removed */
501 if( start_child->pid == pid ) {
503 /* find the last one so we can remove the start_child */
505 prev_child = cur_child;
506 cur_child = cur_child->next;
507 }while( cur_child != start_child );
509 /* now cur_child == start_child */
510 prev_child->next = cur_child->next;
511 forker->first_child = prev_child;
513 close( cur_child->read_data_fd );
514 close( cur_child->write_data_fd );
515 close( cur_child->read_status_fd );
516 close( cur_child->write_status_fd );
518 prefork_child_free( cur_child );
523 prev_child = cur_child;
524 cur_child = cur_child->next;
526 if( cur_child->pid == pid ) {
527 prev_child->next = cur_child->next;
529 close( cur_child->read_data_fd );
530 close( cur_child->write_data_fd );
531 close( cur_child->read_status_fd );
532 close( cur_child->write_status_fd );
534 prefork_child_free( cur_child );
538 } while(cur_child != start_child);
544 prefork_child* prefork_child_init(
545 int max_requests, int read_data_fd, int write_data_fd,
546 int read_status_fd, int write_status_fd ) {
548 prefork_child* child = (prefork_child*) safe_malloc(sizeof(prefork_child));
549 child->max_requests = max_requests;
550 child->read_data_fd = read_data_fd;
551 child->write_data_fd = write_data_fd;
552 child->read_status_fd = read_status_fd;
553 child->write_status_fd = write_status_fd;
554 child->available = 1;
560 int prefork_free( prefork_simple* prefork ) {
562 while( prefork->first_child != NULL ) {
563 info_handler( "Killing children and sleeping 1 to reap..." );
568 client_free(prefork->connection);
569 free(prefork->appname);
574 int prefork_child_free( prefork_child* child ) {
575 free(child->appname);
576 close(child->read_data_fd);
577 close(child->write_status_fd);