1 #include "osrf_prefork.h"
3 #include "osrf_app_session.h"
4 #include "osrf_application.h"
6 /* true if we just deleted a child. This will allow us to make sure we're
7 not trying to use freed memory */
11 void sigchld_handler( int sig );
13 int osrf_prefork_run(char* appname) {
16 osrfLogError( OSRF_LOG_MARK, "osrf_prefork_run requires an appname to run!");
20 set_proc_title( "OpenSRF Listener [%s]", appname );
26 osrfLogInfo( OSRF_LOG_MARK, "Loading config in osrf_forker for app %s", appname);
28 jsonObject* max_req = osrf_settings_host_value_object("/apps/%s/unix_config/max_requests", appname);
29 jsonObject* min_children = osrf_settings_host_value_object("/apps/%s/unix_config/min_children", appname);
30 jsonObject* max_children = osrf_settings_host_value_object("/apps/%s/unix_config/max_children", appname);
32 char* keepalive = osrf_settings_host_value("/apps/%s/keepalive", appname);
35 kalive = atoi(keepalive);
38 kalive = 5; /* give it a default */
41 osrfLogInfo(OSRF_LOG_MARK, "keepalive setting = %d seconds", kalive);
45 if(!max_req) osrfLogWarning( OSRF_LOG_MARK, "Max requests not defined, assuming 1000");
46 else maxr = (int) jsonObjectGetNumber(max_req);
48 if(!min_children) osrfLogWarning( OSRF_LOG_MARK, "Min children not defined, assuming 3");
49 else minc = (int) jsonObjectGetNumber(min_children);
51 if(!max_children) osrfLogWarning( OSRF_LOG_MARK, "Max children not defined, assuming 10");
52 else maxc = (int) jsonObjectGetNumber(max_children);
54 jsonObjectFree(max_req);
55 jsonObjectFree(min_children);
56 jsonObjectFree(max_children);
57 /* --------------------------------------------------- */
59 char* resc = va_list_to_string("%s_listener", appname);
61 if(!osrf_system_bootstrap_client_resc( NULL, NULL, resc )) {
62 osrfLogError( OSRF_LOG_MARK, "Unable to bootstrap client for osrf_prefork_run()");
69 prefork_simple* forker = prefork_simple_init(
70 osrfSystemGetTransportClient(), maxr, minc, maxc);
72 forker->appname = strdup(appname);
73 forker->keepalive = kalive;
76 osrfLogError( OSRF_LOG_MARK, "osrf_prefork_run() failed to create prefork_simple object");
80 prefork_launch_children(forker);
82 osrf_prefork_register_routers(appname);
84 osrfLogInfo( OSRF_LOG_MARK, "Launching osrf_forker for app %s", appname);
87 osrfLogWarning( OSRF_LOG_MARK, "prefork_run() retuned - how??");
93 void osrf_prefork_register_routers( char* appname ) {
95 osrfStringArray* arr = osrfNewStringArray(4);
97 int c = osrfConfigGetValueList( NULL, arr, "/routers/router" );
98 char* routerName = osrfConfigGetValue( NULL, "/router_name" );
99 transport_client* client = osrfSystemGetTransportClient();
101 osrfLogInfo( OSRF_LOG_MARK, "router name is %s and we have %d routers to connect to", routerName, c );
104 char* domain = osrfStringArrayGetString(arr, --c);
107 char* jid = va_list_to_string( "%s@%s/router", routerName, domain );
108 osrfLogInfo( OSRF_LOG_MARK, "Registering with router %s", jid );
110 transport_message* msg = message_init("registering", NULL, NULL, jid, NULL );
111 message_set_router_info( msg, NULL, NULL, appname, "register", 0 );
113 client_send_message( client, msg );
120 osrfStringArrayFree(arr);
123 int prefork_child_init_hook(prefork_child* child) {
125 if(!child) return -1;
126 osrfLogDebug( OSRF_LOG_MARK, "Child init hook for child %d", child->pid);
127 char* resc = va_list_to_string("%s_drone",child->appname);
129 /* we want to remove traces of our parents socket connection
130 * so we can have our own */
131 osrfSystemIgnoreTransportClient();
133 if(!osrf_system_bootstrap_client_resc( NULL, NULL, resc)) {
134 osrfLogError( OSRF_LOG_MARK, "Unable to bootstrap client for osrf_prefork_run()");
141 if( ! osrfAppRunChildInit(child->appname) ) {
142 osrfLogDebug(OSRF_LOG_MARK, "Prefork child_init succeeded\n");
144 osrfLogError(OSRF_LOG_MARK, "Prefork child_init failed\n");
148 set_proc_title( "OpenSRF Drone [%s]", child->appname );
152 void prefork_child_process_request(prefork_child* child, char* data) {
155 transport_client* client = osrfSystemGetTransportClient();
157 if(!client_connected(client)) {
158 osrfSystemIgnoreTransportClient();
159 osrfLogWarning(OSRF_LOG_MARK, "Reconnecting child to opensrf after disconnect...");
160 if(!osrf_system_bootstrap_client(NULL, NULL)) {
161 osrfLogError( OSRF_LOG_MARK,
162 "Unable to bootstrap client in prefork_child_process_request()");
168 /* construct the message from the xml */
169 transport_message* msg = new_message_from_xml( data );
171 osrfAppSession* session = osrf_stack_transport_handler(msg, child->appname);
174 if( session->stateless && session->state != OSRF_SESSION_CONNECTED ) {
175 osrfAppSessionFree( session );
179 osrfLogDebug( OSRF_LOG_MARK, "Entering keepalive loop for session %s", session->session_id );
180 int keepalive = child->keepalive;
187 osrfLogDebug(OSRF_LOG_MARK,
188 "osrf_prefork calling queue_wait [%d] in keepalive loop", keepalive);
190 retval = osrf_app_session_queue_wait(session, keepalive);
194 osrfLogError(OSRF_LOG_MARK, "queue-wait returned non-success %d", retval);
198 /* see if the client disconnected from us */
199 if(session->state != OSRF_SESSION_CONNECTED) break;
201 /* see if the used up the timeout */
202 if( (end - start) >= keepalive ) {
204 osrfLogDebug(OSRF_LOG_MARK, "Keepalive timed out, exiting connected session");
206 osrfAppSessionStatus(
210 0, "Disconnected on timeout" );
216 osrfLogDebug( OSRF_LOG_MARK, "Exiting keepalive loop for session %s", session->session_id );
217 osrfAppSessionFree( session );
222 prefork_simple* prefork_simple_init( transport_client* client,
223 int max_requests, int min_children, int max_children ) {
225 if( min_children > max_children ) {
226 osrfLogError( OSRF_LOG_MARK, "min_children (%d) is greater "
227 "than max_children (%d)", min_children, max_children );
231 if( max_children > ABS_MAX_CHILDREN ) {
232 osrfLogError( OSRF_LOG_MARK, "max_children (%d) is greater than ABS_MAX_CHILDREN (%d)",
233 max_children, ABS_MAX_CHILDREN );
237 osrfLogInfo(OSRF_LOG_MARK, "Prefork launching child with max_request=%d,"
238 "min_children=%d, max_children=%d", max_requests, min_children, max_children );
240 /* flesh out the struct */
241 prefork_simple* prefork = (prefork_simple*) safe_malloc(sizeof(prefork_simple));
242 prefork->max_requests = max_requests;
243 prefork->min_children = min_children;
244 prefork->max_children = max_children;
245 prefork->first_child = NULL;
246 prefork->connection = client;
251 prefork_child* launch_child( prefork_simple* forker ) {
257 /* Set up the data pipes and add the child struct to the parent */
258 if( pipe(data_fd) < 0 ) { /* build the data pipe*/
259 osrfLogError( OSRF_LOG_MARK, "Pipe making error" );
263 if( pipe(status_fd) < 0 ) {/* build the status pipe */
264 osrfLogError( OSRF_LOG_MARK, "Pipe making error" );
268 osrfLogInternal( OSRF_LOG_MARK, "Pipes: %d %d %d %d", data_fd[0], data_fd[1], status_fd[0], status_fd[1] );
269 prefork_child* child = prefork_child_init( forker->max_requests, data_fd[0],
270 data_fd[1], status_fd[0], status_fd[1] );
272 child->appname = strdup(forker->appname);
273 child->keepalive = forker->keepalive;
276 add_prefork_child( forker, child );
278 if( (pid=fork()) < 0 ) {
279 osrfLogError( OSRF_LOG_MARK, "Forking Error" );
283 if( pid > 0 ) { /* parent */
285 signal(SIGCHLD, sigchld_handler);
286 (forker->current_num_children)++;
289 osrfLogDebug( OSRF_LOG_MARK, "Parent launched %d", pid );
290 /* *no* child pipe FD's can be closed or the parent will re-use fd's that
291 the children are currently using */
297 osrfLogInternal( OSRF_LOG_MARK, "I am new child with read_data_fd = %d and write_status_fd = %d",
298 child->read_data_fd, child->write_status_fd );
300 child->pid = getpid();
301 close( child->write_data_fd );
302 close( child->read_status_fd );
305 if( prefork_child_init_hook(child) == -1 ) {
306 osrfLogError(OSRF_LOG_MARK,
307 "Forker child going away because we could not connect to OpenSRF...");
311 prefork_child_wait( child );
312 exit(0); /* just to be sure */
318 void prefork_launch_children( prefork_simple* forker ) {
321 while( c++ < forker->min_children )
322 launch_child( forker );
326 void sigchld_handler( int sig ) {
327 signal(SIGCHLD, sigchld_handler);
332 void reap_children( prefork_simple* forker ) {
337 while( (child_pid=waitpid(-1,&status,WNOHANG)) > 0)
338 del_prefork_child( forker, child_pid );
341 while( forker->current_num_children < forker->min_children )
342 launch_child( forker );
347 void prefork_run(prefork_simple* forker) {
349 if( forker->first_child == NULL )
352 transport_message* cur_msg = NULL;
357 if( forker->first_child == NULL ) {/* no more children */
358 osrfLogWarning( OSRF_LOG_MARK, "No more children..." );
362 osrfLogDebug( OSRF_LOG_MARK, "Forker going into wait for data...");
363 cur_msg = client_recv( forker->connection, -1 );
365 //fprintf(stderr, "Got Data %f\n", get_timestamp_millis() );
367 if( cur_msg == NULL ) continue;
369 int honored = 0; /* true if we've serviced the request */
373 check_children( forker );
375 osrfLogDebug( OSRF_LOG_MARK, "Server received inbound data" );
377 prefork_child* cur_child = forker->first_child;
379 /* Look for an available child */
380 for( k = 0; k < forker->current_num_children; k++ ) {
382 osrfLogInternal( OSRF_LOG_MARK, "Searching for available child. cur_child->pid = %d", cur_child->pid );
383 osrfLogInternal( OSRF_LOG_MARK, "Current num children %d and loop %d", forker->current_num_children, k);
385 if( cur_child->available ) {
386 osrfLogDebug( OSRF_LOG_MARK, "forker sending data to %d", cur_child->pid );
388 message_prepare_xml( cur_msg );
389 char* data = cur_msg->msg_xml;
390 if( ! data || strlen(data) < 1 ) break;
392 cur_child->available = 0;
393 osrfLogInternal( OSRF_LOG_MARK, "Writing to child fd %d", cur_child->write_data_fd );
396 //fprintf(stderr, "Writing Data %f\n", get_timestamp_millis() );
397 if( (written = write( cur_child->write_data_fd, data, strlen(data) + 1 )) < 0 ) {
398 osrfLogWarning( OSRF_LOG_MARK, "Write returned error %d", errno);
399 cur_child = cur_child->next;
403 //fprintf(stderr, "Wrote %d bytes to child\n", written);
405 forker->first_child = cur_child->next;
409 cur_child = cur_child->next;
412 /* if none available, add a new child if we can */
414 osrfLogDebug( OSRF_LOG_MARK, "Not enough children, attempting to add...");
415 if( forker->current_num_children < forker->max_children ) {
416 osrfLogDebug( OSRF_LOG_MARK, "Launching new child with current_num = %d",
417 forker->current_num_children );
419 prefork_child* new_child = launch_child( forker );
420 message_prepare_xml( cur_msg );
421 char* data = cur_msg->msg_xml;
422 if( ! data || strlen(data) < 1 ) break;
423 new_child->available = 0;
424 osrfLogDebug( OSRF_LOG_MARK, "Writing to new child fd %d : pid %d",
425 new_child->write_data_fd, new_child->pid );
426 write( new_child->write_data_fd, data, strlen(data) + 1 );
427 forker->first_child = new_child->next;
433 osrfLogWarning( OSRF_LOG_MARK, "No children available, sleeping and looping..." );
434 usleep( 50000 ); /* 50 milliseconds */
438 reap_children(forker);
441 //fprintf(stderr, "Parent done with request %f\n", get_timestamp_millis() );
445 message_free( cur_msg );
447 } /* top level listen loop */
452 void check_children( prefork_simple* forker ) {
467 reap_children(forker);
469 prefork_child* cur_child = forker->first_child;
472 for( i = 0; i!= forker->current_num_children; i++ ) {
474 if( cur_child->read_status_fd > max_fd )
475 max_fd = cur_child->read_status_fd;
476 FD_SET( cur_child->read_status_fd, &read_set );
477 cur_child = cur_child->next;
480 FD_CLR(0,&read_set);/* just to be sure */
482 if( (select_ret=select( max_fd + 1 , &read_set, NULL, NULL, &tv)) == -1 ) {
483 osrfLogWarning( OSRF_LOG_MARK, "Select returned error %d on check_children", errno );
486 if( select_ret == 0 )
489 /* see if one of a child has told us it's done */
490 cur_child = forker->first_child;
493 for( j = 0; j!= forker->current_num_children && num_handled < select_ret ; j++ ) {
495 if( FD_ISSET( cur_child->read_status_fd, &read_set ) ) {
496 //printf( "Server received status from a child %d\n", cur_child->pid );
497 osrfLogDebug( OSRF_LOG_MARK, "Server received status from a child %d", cur_child->pid );
501 /* now suck off the data */
504 if( (n=read(cur_child->read_status_fd, buf, 63)) < 0 ) {
505 osrfLogWarning( OSRF_LOG_MARK, "Read error afer select in child status read with errno %d", errno);
508 osrfLogDebug( OSRF_LOG_MARK, "Read %d bytes from status buffer: %s", n, buf );
509 cur_child->available = 1;
511 cur_child = cur_child->next;
517 void prefork_child_wait( prefork_child* child ) {
520 growing_buffer* gbuf = buffer_init( READ_BUFSIZE );
521 char buf[READ_BUFSIZE];
522 memset( buf, 0, READ_BUFSIZE );
524 for( i = 0; i < child->max_requests; i++ ) {
528 clr_fl(child->read_data_fd, O_NONBLOCK );
530 while( (n=read(child->read_data_fd, buf, READ_BUFSIZE-1)) > 0 ) {
531 osrfLogDebug(OSRF_LOG_MARK, "Prefork child read %d bytes of data", n);
533 set_fl(child->read_data_fd, O_NONBLOCK );
534 buffer_add( gbuf, buf );
535 memset( buf, 0, READ_BUFSIZE );
539 if( errno == EAGAIN ) n = 0;
542 osrfLogWarning( OSRF_LOG_MARK, "Prefork child read returned error with errno %d", errno );
545 } else if( gotdata ) {
546 osrfLogDebug(OSRF_LOG_MARK, "Prefork child got a request.. processing..");
547 prefork_child_process_request(child, gbuf->buf);
548 buffer_reset( gbuf );
551 if( i < child->max_requests - 1 )
552 write( child->write_status_fd, "available" /*less than 64 bytes*/, 9 );
557 osrfLogDebug( OSRF_LOG_MARK, "Child with max-requests=%d, num-served=%d exiting...[%d]",
558 child->max_requests, i, getpid() );
564 void add_prefork_child( prefork_simple* forker, prefork_child* child ) {
566 if( forker->first_child == NULL ) {
567 forker->first_child = child;
572 /* we put the child in as the last because, regardless,
573 we have to do the DLL splice dance, and this is the
576 prefork_child* start_child = forker->first_child;
578 if( forker->first_child->next == start_child )
580 forker->first_child = forker->first_child->next;
583 /* here we know that forker->first_child is the last element
584 in the list and start_child is the first. Insert the
585 new child between them*/
587 forker->first_child->next = child;
588 child->next = start_child;
592 prefork_child* find_prefork_child( prefork_simple* forker, pid_t pid ) {
594 if( forker->first_child == NULL ) { return NULL; }
595 prefork_child* start_child = forker->first_child;
597 if( forker->first_child->pid == pid )
598 return forker->first_child;
599 } while( (forker->first_child = forker->first_child->next) != start_child );
605 void del_prefork_child( prefork_simple* forker, pid_t pid ) {
607 if( forker->first_child == NULL ) { return; }
609 (forker->current_num_children)--;
610 osrfLogDebug( OSRF_LOG_MARK, "Deleting Child: %d", pid );
612 prefork_child* start_child = forker->first_child; /* starting point */
613 prefork_child* cur_child = start_child; /* current pointer */
614 prefork_child* prev_child = start_child; /* the trailing pointer */
616 /* special case where there is only one in the list */
617 if( start_child == start_child->next ) {
618 if( start_child->pid == pid ) {
619 forker->first_child = NULL;
621 close( start_child->read_data_fd );
622 close( start_child->write_data_fd );
623 close( start_child->read_status_fd );
624 close( start_child->write_status_fd );
626 prefork_child_free( start_child );
632 /* special case where the first item in the list needs to be removed */
633 if( start_child->pid == pid ) {
635 /* find the last one so we can remove the start_child */
637 prev_child = cur_child;
638 cur_child = cur_child->next;
639 }while( cur_child != start_child );
641 /* now cur_child == start_child */
642 prev_child->next = cur_child->next;
643 forker->first_child = prev_child;
645 close( cur_child->read_data_fd );
646 close( cur_child->write_data_fd );
647 close( cur_child->read_status_fd );
648 close( cur_child->write_status_fd );
650 prefork_child_free( cur_child );
655 prev_child = cur_child;
656 cur_child = cur_child->next;
658 if( cur_child->pid == pid ) {
659 prev_child->next = cur_child->next;
661 close( cur_child->read_data_fd );
662 close( cur_child->write_data_fd );
663 close( cur_child->read_status_fd );
664 close( cur_child->write_status_fd );
666 prefork_child_free( cur_child );
670 } while(cur_child != start_child);
676 prefork_child* prefork_child_init(
677 int max_requests, int read_data_fd, int write_data_fd,
678 int read_status_fd, int write_status_fd ) {
680 prefork_child* child = (prefork_child*) safe_malloc(sizeof(prefork_child));
681 child->max_requests = max_requests;
682 child->read_data_fd = read_data_fd;
683 child->write_data_fd = write_data_fd;
684 child->read_status_fd = read_status_fd;
685 child->write_status_fd = write_status_fd;
686 child->available = 1;
692 int prefork_free( prefork_simple* prefork ) {
694 while( prefork->first_child != NULL ) {
695 osrfLogInfo( OSRF_LOG_MARK, "Killing children and sleeping 1 to reap..." );
700 client_free(prefork->connection);
701 free(prefork->appname);
706 int prefork_child_free( prefork_child* child ) {
707 free(child->appname);
708 close(child->read_data_fd);
709 close(child->write_status_fd);