1 #include <opensrf/osrf_prefork.h>
2 #include <opensrf/osrf_app_session.h>
3 #include <opensrf/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 );
27 osrfLogInfo( OSRF_LOG_MARK, "Loading config in osrf_forker for app %s", appname);
29 char* max_req = osrf_settings_host_value("/apps/%s/unix_config/max_requests", appname);
30 char* min_children = osrf_settings_host_value("/apps/%s/unix_config/min_children", appname);
31 char* max_children = osrf_settings_host_value("/apps/%s/unix_config/max_children", appname);
32 char* keepalive = osrf_settings_host_value("/apps/%s/keepalive", appname);
34 if(!keepalive) osrfLogWarning( OSRF_LOG_MARK, "Keepalive is not defined, assuming %d", kalive);
35 else kalive = atoi(keepalive);
37 if(!max_req) osrfLogWarning( OSRF_LOG_MARK, "Max requests not defined, assuming %d", maxr);
38 else maxr = atoi(max_req);
40 if(!min_children) osrfLogWarning( OSRF_LOG_MARK, "Min children not defined, assuming %d", minc);
41 else minc = atoi(min_children);
43 if(!max_children) osrfLogWarning( OSRF_LOG_MARK, "Max children not defined, assuming %d", maxc);
44 else maxc = atoi(max_children);
50 /* --------------------------------------------------- */
52 char* resc = va_list_to_string("%s_listener", appname);
54 if(!osrfSystemBootstrapClientResc( NULL, NULL, resc )) {
55 osrfLogError( OSRF_LOG_MARK, "Unable to bootstrap client for osrf_prefork_run()");
62 prefork_simple* forker = prefork_simple_init(
63 osrfSystemGetTransportClient(), maxr, minc, maxc);
66 osrfLogError( OSRF_LOG_MARK, "osrf_prefork_run() failed to create prefork_simple object");
70 forker->appname = strdup(appname);
71 forker->keepalive = kalive;
73 prefork_launch_children(forker);
75 osrf_prefork_register_routers(appname);
77 osrfLogInfo( OSRF_LOG_MARK, "Launching osrf_forker for app %s", appname);
80 osrfLogWarning( OSRF_LOG_MARK, "prefork_run() retuned - how??");
86 void osrf_prefork_register_routers( char* appname ) {
88 osrfStringArray* arr = osrfNewStringArray(4);
90 int c = osrfConfigGetValueList( NULL, arr, "/routers/router" );
91 char* routerName = osrfConfigGetValue( NULL, "/router_name" );
92 transport_client* client = osrfSystemGetTransportClient();
94 osrfLogInfo( OSRF_LOG_MARK, "router name is %s and we have %d routers to connect to", routerName, c );
97 char* domain = osrfStringArrayGetString(arr, --c);
100 char* jid = va_list_to_string( "%s@%s/router", routerName, domain );
101 osrfLogInfo( OSRF_LOG_MARK, "Registering with router %s", jid );
103 transport_message* msg = message_init("registering", NULL, NULL, jid, NULL );
104 message_set_router_info( msg, NULL, NULL, appname, "register", 0 );
106 client_send_message( client, msg );
113 osrfStringArrayFree(arr);
116 int prefork_child_init_hook(prefork_child* child) {
118 if(!child) return -1;
119 osrfLogDebug( OSRF_LOG_MARK, "Child init hook for child %d", child->pid);
120 char* resc = va_list_to_string("%s_drone",child->appname);
122 /* if we're a source-client, tell the logger now that we're a new process*/
123 char* isclient = osrfConfigGetValue(NULL, "/client");
124 if( isclient && !strcasecmp(isclient,"true") )
125 osrfLogSetIsClient(1);
129 /* we want to remove traces of our parents socket connection
130 * so we can have our own */
131 osrfSystemIgnoreTransportClient();
133 if(!osrfSystemBootstrapClientResc( 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()");
164 osrf_prefork_child_exit(child);
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;
188 osrfLogDebug(OSRF_LOG_MARK,
189 "osrf_prefork calling queue_wait [%d] in keepalive loop", keepalive);
191 retval = osrf_app_session_queue_wait(session, keepalive, &recvd);
194 osrfLogDebug(OSRF_LOG_MARK, "Data received == %d", recvd);
197 osrfLogError(OSRF_LOG_MARK, "queue-wait returned non-success %d", retval);
201 /* see if the client disconnected from us */
202 if(session->state != OSRF_SESSION_CONNECTED) break;
204 /* if no data was reveived within the timeout interval */
205 if( !recvd && (end - start) >= keepalive ) {
206 osrfLogInfo(OSRF_LOG_MARK, "No request was received in %d seconds, exiting stateful session", keepalive);
207 osrfAppSessionStatus(
211 0, "Disconnected on timeout" );
217 osrfLogDebug( OSRF_LOG_MARK, "Exiting keepalive loop for session %s", session->session_id );
218 osrfAppSessionFree( session );
223 prefork_simple* prefork_simple_init( transport_client* client,
224 int max_requests, int min_children, int max_children ) {
226 if( min_children > max_children ) {
227 osrfLogError( OSRF_LOG_MARK, "min_children (%d) is greater "
228 "than max_children (%d)", min_children, max_children );
232 if( max_children > ABS_MAX_CHILDREN ) {
233 osrfLogError( OSRF_LOG_MARK, "max_children (%d) is greater than ABS_MAX_CHILDREN (%d)",
234 max_children, ABS_MAX_CHILDREN );
238 osrfLogInfo(OSRF_LOG_MARK, "Prefork launching child with max_request=%d,"
239 "min_children=%d, max_children=%d", max_requests, min_children, max_children );
241 /* flesh out the struct */
242 prefork_simple* prefork = (prefork_simple*) safe_malloc(sizeof(prefork_simple));
243 prefork->max_requests = max_requests;
244 prefork->min_children = min_children;
245 prefork->max_children = max_children;
246 prefork->first_child = NULL;
247 prefork->connection = client;
252 prefork_child* launch_child( prefork_simple* forker ) {
258 /* Set up the data pipes and add the child struct to the parent */
259 if( pipe(data_fd) < 0 ) { /* build the data pipe*/
260 osrfLogError( OSRF_LOG_MARK, "Pipe making error" );
264 if( pipe(status_fd) < 0 ) {/* build the status pipe */
265 osrfLogError( OSRF_LOG_MARK, "Pipe making error" );
269 osrfLogInternal( OSRF_LOG_MARK, "Pipes: %d %d %d %d", data_fd[0], data_fd[1], status_fd[0], status_fd[1] );
270 prefork_child* child = prefork_child_init( forker->max_requests, data_fd[0],
271 data_fd[1], status_fd[0], status_fd[1] );
273 child->appname = strdup(forker->appname);
274 child->keepalive = forker->keepalive;
277 add_prefork_child( forker, child );
279 if( (pid=fork()) < 0 ) {
280 osrfLogError( OSRF_LOG_MARK, "Forking Error" );
284 if( pid > 0 ) { /* parent */
286 signal(SIGCHLD, sigchld_handler);
287 (forker->current_num_children)++;
290 osrfLogDebug( OSRF_LOG_MARK, "Parent launched %d", pid );
291 /* *no* child pipe FD's can be closed or the parent will re-use fd's that
292 the children are currently using */
298 osrfLogInternal( OSRF_LOG_MARK, "I am new child with read_data_fd = %d and write_status_fd = %d",
299 child->read_data_fd, child->write_status_fd );
301 child->pid = getpid();
302 close( child->write_data_fd );
303 close( child->read_status_fd );
306 if( prefork_child_init_hook(child) == -1 ) {
307 osrfLogError(OSRF_LOG_MARK,
308 "Forker child going away because we could not connect to OpenSRF...");
309 osrf_prefork_child_exit(child);
312 prefork_child_wait( child );
313 osrf_prefork_child_exit(child); /* just to be sure */
318 void osrf_prefork_child_exit(prefork_child* child) {
319 osrfAppRunExitCode();
323 void prefork_launch_children( prefork_simple* forker ) {
326 while( c++ < forker->min_children )
327 launch_child( forker );
331 void sigchld_handler( int sig ) {
332 signal(SIGCHLD, sigchld_handler);
337 void reap_children( prefork_simple* forker ) {
342 while( (child_pid=waitpid(-1,&status,WNOHANG)) > 0)
343 del_prefork_child( forker, child_pid );
346 while( forker->current_num_children < forker->min_children )
347 launch_child( forker );
352 void prefork_run(prefork_simple* forker) {
354 if( forker->first_child == NULL )
357 transport_message* cur_msg = NULL;
362 if( forker->first_child == NULL ) {/* no more children */
363 osrfLogWarning( OSRF_LOG_MARK, "No more children..." );
367 osrfLogDebug( OSRF_LOG_MARK, "Forker going into wait for data...");
368 cur_msg = client_recv( forker->connection, -1 );
370 //fprintf(stderr, "Got Data %f\n", get_timestamp_millis() );
372 if( cur_msg == NULL ) continue;
374 int honored = 0; /* true if we've serviced the request */
379 if(!no_recheck) check_children( forker, 0 );
382 osrfLogDebug( OSRF_LOG_MARK, "Server received inbound data" );
384 prefork_child* cur_child = forker->first_child;
386 /* Look for an available child */
387 for( k = 0; k < forker->current_num_children; k++ ) {
389 osrfLogInternal( OSRF_LOG_MARK, "Searching for available child. cur_child->pid = %d", cur_child->pid );
390 osrfLogInternal( OSRF_LOG_MARK, "Current num children %d and loop %d", forker->current_num_children, k);
392 if( cur_child->available ) {
393 osrfLogDebug( OSRF_LOG_MARK, "forker sending data to %d", cur_child->pid );
395 message_prepare_xml( cur_msg );
396 char* data = cur_msg->msg_xml;
397 if( ! data || strlen(data) < 1 ) break;
399 cur_child->available = 0;
400 osrfLogInternal( OSRF_LOG_MARK, "Writing to child fd %d", cur_child->write_data_fd );
403 //fprintf(stderr, "Writing Data %f\n", get_timestamp_millis() );
404 if( (written = write( cur_child->write_data_fd, data, strlen(data) + 1 )) < 0 ) {
405 osrfLogWarning( OSRF_LOG_MARK, "Write returned error %d", errno);
406 cur_child = cur_child->next;
410 //fprintf(stderr, "Wrote %d bytes to child\n", written);
412 forker->first_child = cur_child->next;
416 cur_child = cur_child->next;
419 /* if none available, add a new child if we can */
421 osrfLogDebug( OSRF_LOG_MARK, "Not enough children, attempting to add...");
423 if( forker->current_num_children < forker->max_children ) {
424 osrfLogDebug( OSRF_LOG_MARK, "Launching new child with current_num = %d",
425 forker->current_num_children );
427 prefork_child* new_child = launch_child( forker );
430 message_prepare_xml( cur_msg );
431 char* data = cur_msg->msg_xml;
434 int len = strlen(data);
437 new_child->available = 0;
438 osrfLogDebug( OSRF_LOG_MARK, "Writing to new child fd %d : pid %d",
439 new_child->write_data_fd, new_child->pid );
441 if( write( new_child->write_data_fd, data, len + 1 ) >= 0 ) {
442 forker->first_child = new_child->next;
453 osrfLogWarning( OSRF_LOG_MARK, "No children available, waiting...");
455 check_children( forker, 1 ); /* non-poll version */
456 /* tell the loop no to call check_children again, since we're calling it now */
461 reap_children(forker);
464 //fprintf(stderr, "Parent done with request %f\n", get_timestamp_millis() );
468 message_free( cur_msg );
470 } /* top level listen loop */
475 /** XXX Add a flag which tells select() to wait forever on children
476 * in the best case, this will be faster than calling usleep(x), and
477 * in the worst case it won't be slower and will do less logging...
480 void check_children( prefork_simple* forker, int forever ) {
492 reap_children(forker);
494 prefork_child* cur_child = forker->first_child;
497 for( i = 0; i!= forker->current_num_children; i++ ) {
499 if( cur_child->read_status_fd > max_fd )
500 max_fd = cur_child->read_status_fd;
501 FD_SET( cur_child->read_status_fd, &read_set );
502 cur_child = cur_child->next;
505 FD_CLR(0,&read_set);/* just to be sure */
508 osrfLogWarning(OSRF_LOG_MARK, "We have no children available - waiting for one to show up...");
510 if( (select_ret=select( max_fd + 1 , &read_set, NULL, NULL, NULL)) == -1 ) {
511 osrfLogWarning( OSRF_LOG_MARK, "Select returned error %d on check_children", errno );
513 osrfLogInfo(OSRF_LOG_MARK, "select() completed after waiting on children to become available");
521 if( (select_ret=select( max_fd + 1 , &read_set, NULL, NULL, &tv)) == -1 ) {
522 osrfLogWarning( OSRF_LOG_MARK, "Select returned error %d on check_children", errno );
526 if( select_ret == 0 )
529 /* see if one of a child has told us it's done */
530 cur_child = forker->first_child;
533 for( j = 0; j!= forker->current_num_children && num_handled < select_ret ; j++ ) {
535 if( FD_ISSET( cur_child->read_status_fd, &read_set ) ) {
536 //printf( "Server received status from a child %d\n", cur_child->pid );
537 osrfLogDebug( OSRF_LOG_MARK, "Server received status from a child %d", cur_child->pid );
541 /* now suck off the data */
543 osrf_clearbuf( buf, sizeof(buf) );
544 if( (n=read(cur_child->read_status_fd, buf, 63)) < 0 ) {
545 osrfLogWarning( OSRF_LOG_MARK, "Read error after select in child status read with errno %d", errno);
548 osrfLogDebug( OSRF_LOG_MARK, "Read %d bytes from status buffer: %s", n, buf );
549 cur_child->available = 1;
551 cur_child = cur_child->next;
557 void prefork_child_wait( prefork_child* child ) {
560 growing_buffer* gbuf = buffer_init( READ_BUFSIZE );
561 char buf[READ_BUFSIZE];
562 osrf_clearbuf( buf, sizeof(buf) );
564 for( i = 0; i < child->max_requests; i++ ) {
568 clr_fl(child->read_data_fd, O_NONBLOCK );
570 while( (n=read(child->read_data_fd, buf, READ_BUFSIZE-1)) > 0 ) {
571 osrfLogDebug(OSRF_LOG_MARK, "Prefork child read %d bytes of data", n);
573 set_fl(child->read_data_fd, O_NONBLOCK );
574 buffer_add( gbuf, buf );
575 osrf_clearbuf( buf, sizeof(buf) );
579 if( errno == EAGAIN ) n = 0;
581 if( errno == EPIPE ) {
582 osrfLogDebug(OSRF_LOG_MARK, "C child attempted read on broken pipe, exiting...");
587 osrfLogWarning( OSRF_LOG_MARK, "Prefork child read returned error with errno %d", errno );
590 } else if( gotdata ) {
591 osrfLogDebug(OSRF_LOG_MARK, "Prefork child got a request.. processing..");
592 prefork_child_process_request(child, gbuf->buf);
593 buffer_reset( gbuf );
596 if( i < child->max_requests - 1 )
597 write( child->write_status_fd, "available" /*less than 64 bytes*/, 9 );
602 osrfLogDebug( OSRF_LOG_MARK, "Child with max-requests=%d, num-served=%d exiting...[%ld]",
603 child->max_requests, i, (long) getpid() );
605 osrf_prefork_child_exit(child); /* just to be sure */
609 void add_prefork_child( prefork_simple* forker, prefork_child* child ) {
611 if( forker->first_child == NULL ) {
612 forker->first_child = child;
617 /* we put the child in as the last because, regardless,
618 we have to do the DLL splice dance, and this is the
621 prefork_child* start_child = forker->first_child;
623 if( forker->first_child->next == start_child )
625 forker->first_child = forker->first_child->next;
628 /* here we know that forker->first_child is the last element
629 in the list and start_child is the first. Insert the
630 new child between them*/
632 forker->first_child->next = child;
633 child->next = start_child;
637 prefork_child* find_prefork_child( prefork_simple* forker, pid_t pid ) {
639 if( forker->first_child == NULL ) { return NULL; }
640 prefork_child* start_child = forker->first_child;
642 if( forker->first_child->pid == pid )
643 return forker->first_child;
644 } while( (forker->first_child = forker->first_child->next) != start_child );
650 void del_prefork_child( prefork_simple* forker, pid_t pid ) {
652 if( forker->first_child == NULL ) { return; }
654 (forker->current_num_children)--;
655 osrfLogDebug( OSRF_LOG_MARK, "Deleting Child: %d", pid );
657 prefork_child* start_child = forker->first_child; /* starting point */
658 prefork_child* cur_child = start_child; /* current pointer */
659 prefork_child* prev_child = start_child; /* the trailing pointer */
661 /* special case where there is only one in the list */
662 if( start_child == start_child->next ) {
663 if( start_child->pid == pid ) {
664 forker->first_child = NULL;
666 close( start_child->read_data_fd );
667 close( start_child->write_data_fd );
668 close( start_child->read_status_fd );
669 close( start_child->write_status_fd );
671 prefork_child_free( start_child );
677 /* special case where the first item in the list needs to be removed */
678 if( start_child->pid == pid ) {
680 /* find the last one so we can remove the start_child */
682 prev_child = cur_child;
683 cur_child = cur_child->next;
684 }while( cur_child != start_child );
686 /* now cur_child == start_child */
687 prev_child->next = cur_child->next;
688 forker->first_child = prev_child;
690 close( cur_child->read_data_fd );
691 close( cur_child->write_data_fd );
692 close( cur_child->read_status_fd );
693 close( cur_child->write_status_fd );
695 prefork_child_free( cur_child );
700 prev_child = cur_child;
701 cur_child = cur_child->next;
703 if( cur_child->pid == pid ) {
704 prev_child->next = cur_child->next;
706 close( cur_child->read_data_fd );
707 close( cur_child->write_data_fd );
708 close( cur_child->read_status_fd );
709 close( cur_child->write_status_fd );
711 prefork_child_free( cur_child );
715 } while(cur_child != start_child);
721 prefork_child* prefork_child_init(
722 int max_requests, int read_data_fd, int write_data_fd,
723 int read_status_fd, int write_status_fd ) {
725 prefork_child* child = (prefork_child*) safe_malloc(sizeof(prefork_child));
726 child->max_requests = max_requests;
727 child->read_data_fd = read_data_fd;
728 child->write_data_fd = write_data_fd;
729 child->read_status_fd = read_status_fd;
730 child->write_status_fd = write_status_fd;
731 child->available = 1;
737 int prefork_free( prefork_simple* prefork ) {
739 while( prefork->first_child != NULL ) {
740 osrfLogInfo( OSRF_LOG_MARK, "Killing children and sleeping 1 to reap..." );
745 client_free(prefork->connection);
746 free(prefork->appname);
751 int prefork_child_free( prefork_child* child ) {
752 free(child->appname);
753 close(child->read_data_fd);
754 close(child->write_status_fd);