]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libstack/osrf_prefork.c
changed log line
[OpenSRF.git] / src / libstack / osrf_prefork.c
1 #include "osrf_prefork.h"
2 #include <signal.h>
3 #include "osrf_app_session.h"
4 #include "osrf_application.h"
5
6 /* true if we just deleted a child.  This will allow us to make sure we're
7         not trying to use freed memory */
8 int child_dead;
9
10 int main();
11 void sigchld_handler( int sig );
12
13 int osrf_prefork_run(char* appname) {
14
15         if(!appname) {
16                 osrfLogError( OSRF_LOG_MARK, "osrf_prefork_run requires an appname to run!");
17                 return -1;
18         }
19
20         set_proc_title( "OpenSRF Listener [%s]", appname );
21
22         int maxr = 1000; 
23         int maxc = 10;
24         int minc = 3;
25
26         osrfLogInfo( OSRF_LOG_MARK, "Loading config in osrf_forker for app %s", appname);
27
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);
31
32         char* keepalive = osrf_settings_host_value("/apps/%s/keepalive", appname);
33         time_t kalive;
34         if( keepalive ) {
35                 kalive = atoi(keepalive);
36                 free(keepalive);
37         } else {
38                 kalive = 5; /* give it a default */
39         }
40
41         osrfLogInfo(OSRF_LOG_MARK, "keepalive setting = %d seconds", kalive);
42
43
44         
45         if(!max_req) osrfLogWarning( OSRF_LOG_MARK, "Max requests not defined, assuming 1000");
46         else maxr = (int) jsonObjectGetNumber(max_req);
47
48         if(!min_children) osrfLogWarning( OSRF_LOG_MARK, "Min children not defined, assuming 3");
49         else minc = (int) jsonObjectGetNumber(min_children);
50
51         if(!max_children) osrfLogWarning( OSRF_LOG_MARK, "Max children not defined, assuming 10");
52         else maxc = (int) jsonObjectGetNumber(max_children);
53
54         jsonObjectFree(max_req);
55         jsonObjectFree(min_children);
56         jsonObjectFree(max_children);
57         /* --------------------------------------------------- */
58
59         char* resc = va_list_to_string("%s_listener", appname);
60
61         if(!osrf_system_bootstrap_client_resc( NULL, NULL, resc )) {
62                 osrfLogError( OSRF_LOG_MARK, "Unable to bootstrap client for osrf_prefork_run()");
63                 free(resc);
64                 return -1;
65         }
66
67         free(resc);
68
69         prefork_simple* forker = prefork_simple_init(
70                 osrfSystemGetTransportClient(), maxr, minc, maxc);
71
72         forker->appname = strdup(appname);
73         forker->keepalive       = kalive;
74
75         if(forker == NULL) {
76                 osrfLogError( OSRF_LOG_MARK, "osrf_prefork_run() failed to create prefork_simple object");
77                 return -1;
78         }
79
80         prefork_launch_children(forker);
81
82         osrf_prefork_register_routers(appname);
83         
84         osrfLogInfo( OSRF_LOG_MARK, "Launching osrf_forker for app %s", appname);
85         prefork_run(forker);
86         
87         osrfLogWarning( OSRF_LOG_MARK, "prefork_run() retuned - how??");
88         prefork_free(forker);
89         return 0;
90
91 }
92
93 void osrf_prefork_register_routers( char* appname ) {
94
95         osrfStringArray* arr = osrfNewStringArray(4);
96
97         int c = osrfConfigGetValueList( NULL, arr, "/routers/router" );
98         char* routerName = osrfConfigGetValue( NULL, "/router_name" );
99         transport_client* client = osrfSystemGetTransportClient();
100
101         osrfLogInfo( OSRF_LOG_MARK, "router name is %s and we have %d routers to connect to", routerName, c );
102
103         while( c ) {
104                 char* domain = osrfStringArrayGetString(arr, --c);
105                 if(domain) {
106
107                         char* jid = va_list_to_string( "%s@%s/router", routerName, domain );
108                         osrfLogInfo( OSRF_LOG_MARK, "Registering with router %s", jid );
109
110                         transport_message* msg = message_init("registering", NULL, NULL, jid, NULL );
111                         message_set_router_info( msg, NULL, NULL, appname, "register", 0 );
112
113                         client_send_message( client, msg );
114                         message_free( msg );
115                         free(jid);
116                 }
117         }
118
119         free(routerName);
120         osrfStringArrayFree(arr);
121 }
122
123 int prefork_child_init_hook(prefork_child* child) {
124
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);
128
129         /* we want to remove traces of our parents socket connection 
130          * so we can have our own */
131         osrfSystemIgnoreTransportClient();
132
133         if(!osrf_system_bootstrap_client_resc( NULL, NULL, resc)) {
134                 osrfLogError( OSRF_LOG_MARK, "Unable to bootstrap client for osrf_prefork_run()");
135                 free(resc);
136                 return -1;
137         }
138
139         free(resc);
140
141         if( ! osrfAppRunChildInit(child->appname) ) {
142                 osrfLogDebug(OSRF_LOG_MARK, "Prefork child_init succeeded\n");
143         } else {
144                 osrfLogError(OSRF_LOG_MARK, "Prefork child_init failed\n");
145                 return -1;
146         }
147
148         set_proc_title( "OpenSRF Drone [%s]", child->appname );
149         return 0;
150 }
151
152 void prefork_child_process_request(prefork_child* child, char* data) {
153         if( !child ) return;
154
155         transport_client* client = osrfSystemGetTransportClient();
156
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()");
163                         sleep(1);
164                         exit(1);
165                 }
166         }
167
168         /* construct the message from the xml */
169         transport_message* msg = new_message_from_xml( data );
170
171         osrfAppSession* session = osrf_stack_transport_handler(msg, child->appname);
172         if(!session) return;
173
174         if( session->stateless && session->state != OSRF_SESSION_CONNECTED ) {
175                 osrfAppSessionFree( session );
176                 return;
177         }
178
179         osrfLogDebug( OSRF_LOG_MARK, "Entering keepalive loop for session %s", session->session_id );
180         int keepalive = child->keepalive;
181         int retval;
182         int recvd;
183         time_t start;
184         time_t end;
185
186         while(1) {
187
188                 osrfLogDebug(OSRF_LOG_MARK, 
189                                 "osrf_prefork calling queue_wait [%d] in keepalive loop", keepalive);
190                 start           = time(NULL);
191                 retval  = osrf_app_session_queue_wait(session, keepalive, &recvd);
192                 end             = time(NULL);
193
194                 osrfLogDebug(OSRF_LOG_MARK, "Data received == %d", recvd);
195
196                 if(retval) {
197                         osrfLogError(OSRF_LOG_MARK, "queue-wait returned non-success %d", retval);
198                         break;
199                 }
200
201                 /* see if the client disconnected from us */
202                 if(session->state != OSRF_SESSION_CONNECTED) break;
203
204                 /* if no data was reveived within the timeout interval */
205                 if( !recvd && (end - start) >= keepalive ) {
206                         osrfLogInfo(OSRF_LOG_MARK, "No request was reveived in %d seconds, exiting stateful session", keepalive);
207                         osrfAppSessionStatus( 
208                                         session, 
209                                         OSRF_STATUS_TIMEOUT, 
210                                         "osrfConnectStatus", 
211                                         0, "Disconnected on timeout" );
212
213                         break;
214                 }
215         }
216
217         osrfLogDebug( OSRF_LOG_MARK, "Exiting keepalive loop for session %s", session->session_id );
218         osrfAppSessionFree( session );
219         return;
220 }
221
222
223 prefork_simple*  prefork_simple_init( transport_client* client, 
224                 int max_requests, int min_children, int max_children ) {
225
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 );
229                 return NULL;
230         }
231
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 );
235                 return NULL;
236         }
237
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 );
240
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;
248
249         return prefork;
250 }
251
252 prefork_child*  launch_child( prefork_simple* forker ) {
253
254         pid_t pid;
255         int data_fd[2];
256         int status_fd[2];
257
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" );
261                 return NULL;
262         }
263
264         if( pipe(status_fd) < 0 ) {/* build the status pipe */
265                 osrfLogError( OSRF_LOG_MARK,  "Pipe making error" );
266                 return NULL;
267         }
268
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] );
272
273         child->appname = strdup(forker->appname);
274         child->keepalive = forker->keepalive;
275
276
277         add_prefork_child( forker, child );
278
279         if( (pid=fork()) < 0 ) {
280                 osrfLogError( OSRF_LOG_MARK,  "Forking Error" );
281                 return NULL;
282         }
283
284         if( pid > 0 ) {  /* parent */
285
286                 signal(SIGCHLD, sigchld_handler);
287                 (forker->current_num_children)++;
288                 child->pid = pid;
289
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 */
293                 return child;
294         }
295
296         else { /* child */
297
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 );
300
301                 child->pid = getpid();
302                 close( child->write_data_fd );
303                 close( child->read_status_fd );
304
305                 /* do the initing */
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                         exit(1);
310                 }
311
312                 prefork_child_wait( child );
313                 exit(0); /* just to be sure */
314          }
315         return NULL;
316 }
317
318
319 void prefork_launch_children( prefork_simple* forker ) {
320         if(!forker) return;
321         int c = 0;
322         while( c++ < forker->min_children )
323                 launch_child( forker );
324 }
325
326
327 void sigchld_handler( int sig ) {
328         signal(SIGCHLD, sigchld_handler);
329         child_dead = 1;
330 }
331
332
333 void reap_children( prefork_simple* forker ) {
334
335         pid_t child_pid;
336         int status;
337
338         while( (child_pid=waitpid(-1,&status,WNOHANG)) > 0) 
339                 del_prefork_child( forker, child_pid ); 
340
341         /* replenish */
342         while( forker->current_num_children < forker->min_children ) 
343                 launch_child( forker );
344
345         child_dead = 0;
346 }
347
348 void prefork_run(prefork_simple* forker) {
349
350         if( forker->first_child == NULL )
351                 return;
352
353         transport_message* cur_msg = NULL;
354
355
356         while(1) {
357
358                 if( forker->first_child == NULL ) {/* no more children */
359                         osrfLogWarning( OSRF_LOG_MARK, "No more children..." );
360                         return;
361                 }
362
363                 osrfLogDebug( OSRF_LOG_MARK, "Forker going into wait for data...");
364                 cur_msg = client_recv( forker->connection, -1 );
365
366                 //fprintf(stderr, "Got Data %f\n", get_timestamp_millis() );
367
368                 if( cur_msg == NULL ) continue;
369
370                 int honored = 0;        /* true if we've serviced the request */
371                 int no_recheck = 0;
372
373                 while( ! honored ) {
374
375                         if(!no_recheck) check_children( forker, 0 ); 
376                         no_recheck = 0;
377
378                         osrfLogDebug( OSRF_LOG_MARK,  "Server received inbound data" );
379                         int k;
380                         prefork_child* cur_child = forker->first_child;
381
382                         /* Look for an available child */
383                         for( k = 0; k < forker->current_num_children; k++ ) {
384
385                                 osrfLogInternal( OSRF_LOG_MARK, "Searching for available child. cur_child->pid = %d", cur_child->pid );
386                                 osrfLogInternal( OSRF_LOG_MARK, "Current num children %d and loop %d", forker->current_num_children, k);
387                         
388                                 if( cur_child->available ) {
389                                         osrfLogDebug( OSRF_LOG_MARK,  "forker sending data to %d", cur_child->pid );
390
391                                         message_prepare_xml( cur_msg );
392                                         char* data = cur_msg->msg_xml;
393                                         if( ! data || strlen(data) < 1 ) break;
394
395                                         cur_child->available = 0;
396                                         osrfLogInternal( OSRF_LOG_MARK,  "Writing to child fd %d", cur_child->write_data_fd );
397
398                                         int written = 0;
399                                         //fprintf(stderr, "Writing Data %f\n", get_timestamp_millis() );
400                                         if( (written = write( cur_child->write_data_fd, data, strlen(data) + 1 )) < 0 ) {
401                                                 osrfLogWarning( OSRF_LOG_MARK, "Write returned error %d", errno);
402                                                 cur_child = cur_child->next;
403                                                 continue;
404                                         }
405
406                                         //fprintf(stderr, "Wrote %d bytes to child\n", written);
407
408                                         forker->first_child = cur_child->next;
409                                         honored = 1;
410                                         break;
411                                 } else 
412                                         cur_child = cur_child->next;
413                         } 
414
415                         /* if none available, add a new child if we can */
416                         if( ! honored ) {
417                                 osrfLogDebug( OSRF_LOG_MARK, "Not enough children, attempting to add...");
418                                 if( forker->current_num_children < forker->max_children ) {
419                                         osrfLogDebug( OSRF_LOG_MARK,  "Launching new child with current_num = %d",
420                                                         forker->current_num_children );
421
422                                         prefork_child* new_child = launch_child( forker );
423                                         message_prepare_xml( cur_msg );
424                                         char* data = cur_msg->msg_xml;
425                                         if( ! data || strlen(data) < 1 ) break;
426                                         new_child->available = 0;
427                                         osrfLogDebug( OSRF_LOG_MARK,  "Writing to new child fd %d : pid %d", 
428                                                         new_child->write_data_fd, new_child->pid );
429                                         write( new_child->write_data_fd, data, strlen(data) + 1 );
430                                         forker->first_child = new_child->next;
431                                         honored = 1;
432                                 }
433                         }
434
435                         if( !honored ) {
436                                 osrfLogWarning( OSRF_LOG_MARK,  "No children available, waiting...");
437
438                                 check_children( forker, 1 );  /* non-poll version */
439                                 /* tell the loop no to call check_children again, since we're calling it now */
440                                 no_recheck = 1;
441                                 //usleep(50000);
442                         }
443
444                         if( child_dead )
445                                 reap_children(forker);
446
447
448                         //fprintf(stderr, "Parent done with request %f\n", get_timestamp_millis() );
449
450                 } // honored?
451
452                 message_free( cur_msg );
453
454         } /* top level listen loop */
455
456 }
457
458
459 /** XXX Add a flag which tells select() to wait forever on children
460  * in the best case, this will be faster than calling usleep(x), and
461  * in the worst case it won't be slower and will do less logging...
462  */
463
464 void check_children( prefork_simple* forker, int forever ) {
465
466         //check_begin:
467
468         int select_ret;
469         fd_set read_set;
470         FD_ZERO(&read_set);
471         int max_fd = 0;
472         int n;
473
474
475         if( child_dead )
476                 reap_children(forker);
477
478         prefork_child* cur_child = forker->first_child;
479
480         int i;
481         for( i = 0; i!= forker->current_num_children; i++ ) {
482
483                 if( cur_child->read_status_fd > max_fd )
484                         max_fd = cur_child->read_status_fd;
485                 FD_SET( cur_child->read_status_fd, &read_set );
486                 cur_child = cur_child->next;
487         }
488
489         FD_CLR(0,&read_set);/* just to be sure */
490
491         if( forever ) {
492                 osrfLogWarning(OSRF_LOG_MARK, "We have no children available - waiting for one to show up...");
493
494                 if( (select_ret=select( max_fd + 1 , &read_set, NULL, NULL, NULL)) == -1 ) {
495                         osrfLogWarning( OSRF_LOG_MARK,  "Select returned error %d on check_children", errno );
496                 }
497                 osrfLogInfo(OSRF_LOG_MARK, "select() completed after waiting on children to become available");
498
499         } else {
500
501                 struct timeval tv;
502                 tv.tv_sec       = 0;
503                 tv.tv_usec      = 0;
504         
505                 if( (select_ret=select( max_fd + 1 , &read_set, NULL, NULL, &tv)) == -1 ) {
506                         osrfLogWarning( OSRF_LOG_MARK,  "Select returned error %d on check_children", errno );
507                 }
508         }
509
510         if( select_ret == 0 )
511                 return;
512
513         /* see if one of a child has told us it's done */
514         cur_child = forker->first_child;
515         int j;
516         int num_handled = 0;
517         for( j = 0; j!= forker->current_num_children && num_handled < select_ret ; j++ ) {
518
519                 if( FD_ISSET( cur_child->read_status_fd, &read_set ) ) {
520                         //printf( "Server received status from a child %d\n", cur_child->pid );
521                         osrfLogDebug( OSRF_LOG_MARK,  "Server received status from a child %d", cur_child->pid );
522
523                         num_handled++;
524
525                         /* now suck off the data */
526                         char buf[64];
527                         memset( buf, 0, 64);
528                         if( (n=read(cur_child->read_status_fd, buf, 63))  < 0 ) {
529                                 osrfLogWarning( OSRF_LOG_MARK, "Read error afer select in child status read with errno %d", errno);
530                         }
531
532                         osrfLogDebug( OSRF_LOG_MARK,  "Read %d bytes from status buffer: %s", n, buf );
533                         cur_child->available = 1;
534                 }
535                 cur_child = cur_child->next;
536         } 
537
538 }
539
540
541 void prefork_child_wait( prefork_child* child ) {
542
543         int i,n;
544         growing_buffer* gbuf = buffer_init( READ_BUFSIZE );
545         char buf[READ_BUFSIZE];
546         memset( buf, 0, READ_BUFSIZE );
547
548         for( i = 0; i < child->max_requests; i++ ) {
549
550                 n = -1;
551                 int gotdata = 0;
552                 clr_fl(child->read_data_fd, O_NONBLOCK );
553
554                 while( (n=read(child->read_data_fd, buf, READ_BUFSIZE-1)) > 0 ) {
555                         osrfLogDebug(OSRF_LOG_MARK, "Prefork child read %d bytes of data", n);
556                         if(!gotdata)
557                                 set_fl(child->read_data_fd, O_NONBLOCK );
558                         buffer_add( gbuf, buf );
559                         memset( buf, 0, READ_BUFSIZE );
560                         gotdata = 1;
561                 }
562
563                 if( errno == EAGAIN ) n = 0;
564
565                 if( n < 0 ) {
566                         osrfLogWarning( OSRF_LOG_MARK,  "Prefork child read returned error with errno %d", errno );
567                         break;
568
569                 } else if( gotdata ) {
570                         osrfLogDebug(OSRF_LOG_MARK, "Prefork child got a request.. processing..");
571                         prefork_child_process_request(child, gbuf->buf);
572                         buffer_reset( gbuf );
573                 }
574
575                 if( i < child->max_requests - 1 ) 
576                         write( child->write_status_fd, "available" /*less than 64 bytes*/, 9 );
577         }
578
579         buffer_free(gbuf);
580
581         osrfLogDebug( OSRF_LOG_MARK, "Child with max-requests=%d, num-served=%d exiting...[%d]", 
582                         child->max_requests, i, getpid() );
583
584         exit(0);
585 }
586
587
588 void add_prefork_child( prefork_simple* forker, prefork_child* child ) {
589         
590         if( forker->first_child == NULL ) {
591                 forker->first_child = child;
592                 child->next = child;
593                 return;
594         }
595
596         /* we put the child in as the last because, regardless, 
597                 we have to do the DLL splice dance, and this is the
598            simplest way */
599
600         prefork_child* start_child = forker->first_child;
601         while(1) {
602                 if( forker->first_child->next == start_child ) 
603                         break;
604                 forker->first_child = forker->first_child->next;
605         }
606
607         /* here we know that forker->first_child is the last element 
608                 in the list and start_child is the first.  Insert the
609                 new child between them*/
610
611         forker->first_child->next = child;
612         child->next = start_child;
613         return;
614 }
615
616 prefork_child* find_prefork_child( prefork_simple* forker, pid_t pid ) {
617
618         if( forker->first_child == NULL ) { return NULL; }
619         prefork_child* start_child = forker->first_child;
620         do {
621                 if( forker->first_child->pid == pid ) 
622                         return forker->first_child;
623         } while( (forker->first_child = forker->first_child->next) != start_child );
624
625         return NULL;
626 }
627
628
629 void del_prefork_child( prefork_simple* forker, pid_t pid ) { 
630
631         if( forker->first_child == NULL ) { return; }
632
633         (forker->current_num_children)--;
634         osrfLogDebug( OSRF_LOG_MARK, "Deleting Child: %d", pid );
635
636         prefork_child* start_child = forker->first_child; /* starting point */
637         prefork_child* cur_child        = start_child; /* current pointer */
638         prefork_child* prev_child       = start_child; /* the trailing pointer */
639
640         /* special case where there is only one in the list */
641         if( start_child == start_child->next ) {
642                 if( start_child->pid == pid ) {
643                         forker->first_child = NULL;
644
645                         close( start_child->read_data_fd );
646                         close( start_child->write_data_fd );
647                         close( start_child->read_status_fd );
648                         close( start_child->write_status_fd );
649
650                         prefork_child_free( start_child );
651                 }
652                 return;
653         }
654
655
656         /* special case where the first item in the list needs to be removed */
657         if( start_child->pid == pid ) { 
658
659                 /* find the last one so we can remove the start_child */
660                 do { 
661                         prev_child = cur_child;
662                         cur_child = cur_child->next;
663                 }while( cur_child != start_child );
664
665                 /* now cur_child == start_child */
666                 prev_child->next = cur_child->next;
667                 forker->first_child = prev_child;
668
669                 close( cur_child->read_data_fd );
670                 close( cur_child->write_data_fd );
671                 close( cur_child->read_status_fd );
672                 close( cur_child->write_status_fd );
673
674                 prefork_child_free( cur_child );
675                 return;
676         } 
677
678         do {
679                 prev_child = cur_child;
680                 cur_child = cur_child->next;
681
682                 if( cur_child->pid == pid ) {
683                         prev_child->next = cur_child->next;
684
685                         close( cur_child->read_data_fd );
686                         close( cur_child->write_data_fd );
687                         close( cur_child->read_status_fd );
688                         close( cur_child->write_status_fd );
689
690                         prefork_child_free( cur_child );
691                         return;
692                 }
693
694         } while(cur_child != start_child);
695 }
696
697
698
699
700 prefork_child* prefork_child_init( 
701         int max_requests, int read_data_fd, int write_data_fd, 
702         int read_status_fd, int write_status_fd ) {
703
704         prefork_child* child = (prefork_child*) safe_malloc(sizeof(prefork_child));
705         child->max_requests             = max_requests;
706         child->read_data_fd             = read_data_fd;
707         child->write_data_fd            = write_data_fd;
708         child->read_status_fd   = read_status_fd;
709         child->write_status_fd  = write_status_fd;
710         child->available                        = 1;
711
712         return child;
713 }
714
715
716 int prefork_free( prefork_simple* prefork ) {
717         
718         while( prefork->first_child != NULL ) {
719                 osrfLogInfo( OSRF_LOG_MARK,  "Killing children and sleeping 1 to reap..." );
720                 kill( 0,        SIGKILL );
721                 sleep(1);
722         }
723
724         client_free(prefork->connection);
725         free(prefork->appname);
726         free( prefork );
727         return 1;
728 }
729
730 int prefork_child_free( prefork_child* child ) { 
731         free(child->appname);
732         close(child->read_data_fd);
733         close(child->write_status_fd);
734         free( child ); 
735         return 1;
736 }
737