5b7d1f53ec2d0305b52426af0d96cdc3d982d49c
[Evergreen.git] / OpenSRF / 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         time_t start;
183         time_t end;
184
185         while(1) {
186
187                 osrfLogDebug(OSRF_LOG_MARK, 
188                                 "osrf_prefork calling queue_wait [%d] in keepalive loop", keepalive);
189                 start           = time(NULL);
190                 retval  = osrf_app_session_queue_wait(session, keepalive);
191                 end             = time(NULL);
192
193                 if(retval) {
194                         osrfLogError(OSRF_LOG_MARK, "queue-wait returned non-success %d", retval);
195                         break;
196                 }
197
198                 /* see if the client disconnected from us */
199                 if(session->state != OSRF_SESSION_CONNECTED) break;
200
201                 /* see if the used up the timeout */
202                 if( (end - start) >= keepalive ) {
203
204                         osrfLogDebug(OSRF_LOG_MARK, "Keepalive timed out, exiting connected session");
205
206                         osrfAppSessionStatus( 
207                                         session, 
208                                         OSRF_STATUS_TIMEOUT, 
209                                         "osrfConnectStatus", 
210                                         0, "Disconnected on timeout" );
211
212                         break;
213                 }
214         }
215
216         osrfLogDebug( OSRF_LOG_MARK, "Exiting keepalive loop for session %s", session->session_id );
217         osrfAppSessionFree( session );
218         return;
219 }
220
221
222 prefork_simple*  prefork_simple_init( transport_client* client, 
223                 int max_requests, int min_children, int max_children ) {
224
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 );
228                 return NULL;
229         }
230
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 );
234                 return NULL;
235         }
236
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 );
239
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;
247
248         return prefork;
249 }
250
251 prefork_child*  launch_child( prefork_simple* forker ) {
252
253         pid_t pid;
254         int data_fd[2];
255         int status_fd[2];
256
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" );
260                 return NULL;
261         }
262
263         if( pipe(status_fd) < 0 ) {/* build the status pipe */
264                 osrfLogError( OSRF_LOG_MARK,  "Pipe making error" );
265                 return NULL;
266         }
267
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] );
271
272         child->appname = strdup(forker->appname);
273         child->keepalive = forker->keepalive;
274
275
276         add_prefork_child( forker, child );
277
278         if( (pid=fork()) < 0 ) {
279                 osrfLogError( OSRF_LOG_MARK,  "Forking Error" );
280                 return NULL;
281         }
282
283         if( pid > 0 ) {  /* parent */
284
285                 signal(SIGCHLD, sigchld_handler);
286                 (forker->current_num_children)++;
287                 child->pid = pid;
288
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 */
292                 return child;
293         }
294
295         else { /* child */
296
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 );
299
300                 child->pid = getpid();
301                 close( child->write_data_fd );
302                 close( child->read_status_fd );
303
304                 /* do the initing */
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...");
308                         exit(1);
309                 }
310
311                 prefork_child_wait( child );
312                 exit(0); /* just to be sure */
313          }
314         return NULL;
315 }
316
317
318 void prefork_launch_children( prefork_simple* forker ) {
319         if(!forker) return;
320         int c = 0;
321         while( c++ < forker->min_children )
322                 launch_child( forker );
323 }
324
325
326 void sigchld_handler( int sig ) {
327         signal(SIGCHLD, sigchld_handler);
328         child_dead = 1;
329 }
330
331
332 void reap_children( prefork_simple* forker ) {
333
334         pid_t child_pid;
335         int status;
336
337         while( (child_pid=waitpid(-1,&status,WNOHANG)) > 0) 
338                 del_prefork_child( forker, child_pid ); 
339
340         /* replenish */
341         while( forker->current_num_children < forker->min_children ) 
342                 launch_child( forker );
343
344         child_dead = 0;
345 }
346
347 void prefork_run(prefork_simple* forker) {
348
349         if( forker->first_child == NULL )
350                 return;
351
352         transport_message* cur_msg = NULL;
353
354
355         while(1) {
356
357                 if( forker->first_child == NULL ) {/* no more children */
358                         osrfLogWarning( OSRF_LOG_MARK, "No more children..." );
359                         return;
360                 }
361
362                 osrfLogDebug( OSRF_LOG_MARK, "Forker going into wait for data...");
363                 cur_msg = client_recv( forker->connection, -1 );
364
365                 //fprintf(stderr, "Got Data %f\n", get_timestamp_millis() );
366
367                 if( cur_msg == NULL ) continue;
368
369                 int honored = 0;        /* true if we've serviced the request */
370                 int no_recheck = 0;
371
372                 while( ! honored ) {
373
374                         if(!no_recheck) check_children( forker, 0 ); 
375                         no_recheck = 0;
376
377                         osrfLogDebug( OSRF_LOG_MARK,  "Server received inbound data" );
378                         int k;
379                         prefork_child* cur_child = forker->first_child;
380
381                         /* Look for an available child */
382                         for( k = 0; k < forker->current_num_children; k++ ) {
383
384                                 osrfLogInternal( OSRF_LOG_MARK, "Searching for available child. cur_child->pid = %d", cur_child->pid );
385                                 osrfLogInternal( OSRF_LOG_MARK, "Current num children %d and loop %d", forker->current_num_children, k);
386                         
387                                 if( cur_child->available ) {
388                                         osrfLogDebug( OSRF_LOG_MARK,  "forker sending data to %d", cur_child->pid );
389
390                                         message_prepare_xml( cur_msg );
391                                         char* data = cur_msg->msg_xml;
392                                         if( ! data || strlen(data) < 1 ) break;
393
394                                         cur_child->available = 0;
395                                         osrfLogInternal( OSRF_LOG_MARK,  "Writing to child fd %d", cur_child->write_data_fd );
396
397                                         int written = 0;
398                                         //fprintf(stderr, "Writing Data %f\n", get_timestamp_millis() );
399                                         if( (written = write( cur_child->write_data_fd, data, strlen(data) + 1 )) < 0 ) {
400                                                 osrfLogWarning( OSRF_LOG_MARK, "Write returned error %d", errno);
401                                                 cur_child = cur_child->next;
402                                                 continue;
403                                         }
404
405                                         //fprintf(stderr, "Wrote %d bytes to child\n", written);
406
407                                         forker->first_child = cur_child->next;
408                                         honored = 1;
409                                         break;
410                                 } else 
411                                         cur_child = cur_child->next;
412                         } 
413
414                         /* if none available, add a new child if we can */
415                         if( ! honored ) {
416                                 osrfLogDebug( OSRF_LOG_MARK, "Not enough children, attempting to add...");
417                                 if( forker->current_num_children < forker->max_children ) {
418                                         osrfLogDebug( OSRF_LOG_MARK,  "Launching new child with current_num = %d",
419                                                         forker->current_num_children );
420
421                                         prefork_child* new_child = launch_child( forker );
422                                         message_prepare_xml( cur_msg );
423                                         char* data = cur_msg->msg_xml;
424                                         if( ! data || strlen(data) < 1 ) break;
425                                         new_child->available = 0;
426                                         osrfLogDebug( OSRF_LOG_MARK,  "Writing to new child fd %d : pid %d", 
427                                                         new_child->write_data_fd, new_child->pid );
428                                         write( new_child->write_data_fd, data, strlen(data) + 1 );
429                                         forker->first_child = new_child->next;
430                                         honored = 1;
431                                 }
432                         }
433
434                         if( !honored ) {
435                                 osrfLogWarning( OSRF_LOG_MARK,  "No children available, waiting...");
436
437                                 check_children( forker, 1 );  /* non-poll version */
438                                 /* tell the loop no to call check_children again, since we're calling it now */
439                                 no_recheck = 1;
440                                 //usleep(50000);
441                         }
442
443                         if( child_dead )
444                                 reap_children(forker);
445
446
447                         //fprintf(stderr, "Parent done with request %f\n", get_timestamp_millis() );
448
449                 } // honored?
450
451                 message_free( cur_msg );
452
453         } /* top level listen loop */
454
455 }
456
457
458 /** XXX Add a flag which tells select() to wait forever on children
459  * in the best case, this will be faster than calling usleep(x), and
460  * in the worst case it won't be slower and will do less logging...
461  */
462
463 void check_children( prefork_simple* forker, int forever ) {
464
465         //check_begin:
466
467         int select_ret;
468         fd_set read_set;
469         FD_ZERO(&read_set);
470         int max_fd = 0;
471         int n;
472
473
474         if( child_dead )
475                 reap_children(forker);
476
477         prefork_child* cur_child = forker->first_child;
478
479         int i;
480         for( i = 0; i!= forker->current_num_children; i++ ) {
481
482                 if( cur_child->read_status_fd > max_fd )
483                         max_fd = cur_child->read_status_fd;
484                 FD_SET( cur_child->read_status_fd, &read_set );
485                 cur_child = cur_child->next;
486         }
487
488         FD_CLR(0,&read_set);/* just to be sure */
489
490         if( forever ) {
491                 osrfLogWarning(OSRF_LOG_MARK, "We have no children available - waiting for one to show up...");
492
493                 if( (select_ret=select( max_fd + 1 , &read_set, NULL, NULL, NULL)) == -1 ) {
494                         osrfLogWarning( OSRF_LOG_MARK,  "Select returned error %d on check_children", errno );
495                 }
496                 osrfLogInfo(OSRF_LOG_MARK, "select() completed after waiting on children to become available");
497
498         } else {
499
500                 struct timeval tv;
501                 tv.tv_sec       = 0;
502                 tv.tv_usec      = 0;
503         
504                 if( (select_ret=select( max_fd + 1 , &read_set, NULL, NULL, &tv)) == -1 ) {
505                         osrfLogWarning( OSRF_LOG_MARK,  "Select returned error %d on check_children", errno );
506                 }
507         }
508
509         if( select_ret == 0 )
510                 return;
511
512         /* see if one of a child has told us it's done */
513         cur_child = forker->first_child;
514         int j;
515         int num_handled = 0;
516         for( j = 0; j!= forker->current_num_children && num_handled < select_ret ; j++ ) {
517
518                 if( FD_ISSET( cur_child->read_status_fd, &read_set ) ) {
519                         //printf( "Server received status from a child %d\n", cur_child->pid );
520                         osrfLogDebug( OSRF_LOG_MARK,  "Server received status from a child %d", cur_child->pid );
521
522                         num_handled++;
523
524                         /* now suck off the data */
525                         char buf[64];
526                         memset( buf, 0, 64);
527                         if( (n=read(cur_child->read_status_fd, buf, 63))  < 0 ) {
528                                 osrfLogWarning( OSRF_LOG_MARK, "Read error afer select in child status read with errno %d", errno);
529                         }
530
531                         osrfLogDebug( OSRF_LOG_MARK,  "Read %d bytes from status buffer: %s", n, buf );
532                         cur_child->available = 1;
533                 }
534                 cur_child = cur_child->next;
535         } 
536
537 }
538
539
540 void prefork_child_wait( prefork_child* child ) {
541
542         int i,n;
543         growing_buffer* gbuf = buffer_init( READ_BUFSIZE );
544         char buf[READ_BUFSIZE];
545         memset( buf, 0, READ_BUFSIZE );
546
547         for( i = 0; i < child->max_requests; i++ ) {
548
549                 n = -1;
550                 int gotdata = 0;
551                 clr_fl(child->read_data_fd, O_NONBLOCK );
552
553                 while( (n=read(child->read_data_fd, buf, READ_BUFSIZE-1)) > 0 ) {
554                         osrfLogDebug(OSRF_LOG_MARK, "Prefork child read %d bytes of data", n);
555                         if(!gotdata)
556                                 set_fl(child->read_data_fd, O_NONBLOCK );
557                         buffer_add( gbuf, buf );
558                         memset( buf, 0, READ_BUFSIZE );
559                         gotdata = 1;
560                 }
561
562                 if( errno == EAGAIN ) n = 0;
563
564                 if( n < 0 ) {
565                         osrfLogWarning( OSRF_LOG_MARK,  "Prefork child read returned error with errno %d", errno );
566                         break;
567
568                 } else if( gotdata ) {
569                         osrfLogDebug(OSRF_LOG_MARK, "Prefork child got a request.. processing..");
570                         prefork_child_process_request(child, gbuf->buf);
571                         buffer_reset( gbuf );
572                 }
573
574                 if( i < child->max_requests - 1 ) 
575                         write( child->write_status_fd, "available" /*less than 64 bytes*/, 9 );
576         }
577
578         buffer_free(gbuf);
579
580         osrfLogDebug( OSRF_LOG_MARK, "Child with max-requests=%d, num-served=%d exiting...[%d]", 
581                         child->max_requests, i, getpid() );
582
583         exit(0);
584 }
585
586
587 void add_prefork_child( prefork_simple* forker, prefork_child* child ) {
588         
589         if( forker->first_child == NULL ) {
590                 forker->first_child = child;
591                 child->next = child;
592                 return;
593         }
594
595         /* we put the child in as the last because, regardless, 
596                 we have to do the DLL splice dance, and this is the
597            simplest way */
598
599         prefork_child* start_child = forker->first_child;
600         while(1) {
601                 if( forker->first_child->next == start_child ) 
602                         break;
603                 forker->first_child = forker->first_child->next;
604         }
605
606         /* here we know that forker->first_child is the last element 
607                 in the list and start_child is the first.  Insert the
608                 new child between them*/
609
610         forker->first_child->next = child;
611         child->next = start_child;
612         return;
613 }
614
615 prefork_child* find_prefork_child( prefork_simple* forker, pid_t pid ) {
616
617         if( forker->first_child == NULL ) { return NULL; }
618         prefork_child* start_child = forker->first_child;
619         do {
620                 if( forker->first_child->pid == pid ) 
621                         return forker->first_child;
622         } while( (forker->first_child = forker->first_child->next) != start_child );
623
624         return NULL;
625 }
626
627
628 void del_prefork_child( prefork_simple* forker, pid_t pid ) { 
629
630         if( forker->first_child == NULL ) { return; }
631
632         (forker->current_num_children)--;
633         osrfLogDebug( OSRF_LOG_MARK, "Deleting Child: %d", pid );
634
635         prefork_child* start_child = forker->first_child; /* starting point */
636         prefork_child* cur_child        = start_child; /* current pointer */
637         prefork_child* prev_child       = start_child; /* the trailing pointer */
638
639         /* special case where there is only one in the list */
640         if( start_child == start_child->next ) {
641                 if( start_child->pid == pid ) {
642                         forker->first_child = NULL;
643
644                         close( start_child->read_data_fd );
645                         close( start_child->write_data_fd );
646                         close( start_child->read_status_fd );
647                         close( start_child->write_status_fd );
648
649                         prefork_child_free( start_child );
650                 }
651                 return;
652         }
653
654
655         /* special case where the first item in the list needs to be removed */
656         if( start_child->pid == pid ) { 
657
658                 /* find the last one so we can remove the start_child */
659                 do { 
660                         prev_child = cur_child;
661                         cur_child = cur_child->next;
662                 }while( cur_child != start_child );
663
664                 /* now cur_child == start_child */
665                 prev_child->next = cur_child->next;
666                 forker->first_child = prev_child;
667
668                 close( cur_child->read_data_fd );
669                 close( cur_child->write_data_fd );
670                 close( cur_child->read_status_fd );
671                 close( cur_child->write_status_fd );
672
673                 prefork_child_free( cur_child );
674                 return;
675         } 
676
677         do {
678                 prev_child = cur_child;
679                 cur_child = cur_child->next;
680
681                 if( cur_child->pid == pid ) {
682                         prev_child->next = cur_child->next;
683
684                         close( cur_child->read_data_fd );
685                         close( cur_child->write_data_fd );
686                         close( cur_child->read_status_fd );
687                         close( cur_child->write_status_fd );
688
689                         prefork_child_free( cur_child );
690                         return;
691                 }
692
693         } while(cur_child != start_child);
694 }
695
696
697
698
699 prefork_child* prefork_child_init( 
700         int max_requests, int read_data_fd, int write_data_fd, 
701         int read_status_fd, int write_status_fd ) {
702
703         prefork_child* child = (prefork_child*) safe_malloc(sizeof(prefork_child));
704         child->max_requests             = max_requests;
705         child->read_data_fd             = read_data_fd;
706         child->write_data_fd            = write_data_fd;
707         child->read_status_fd   = read_status_fd;
708         child->write_status_fd  = write_status_fd;
709         child->available                        = 1;
710
711         return child;
712 }
713
714
715 int prefork_free( prefork_simple* prefork ) {
716         
717         while( prefork->first_child != NULL ) {
718                 osrfLogInfo( OSRF_LOG_MARK,  "Killing children and sleeping 1 to reap..." );
719                 kill( 0,        SIGKILL );
720                 sleep(1);
721         }
722
723         client_free(prefork->connection);
724         free(prefork->appname);
725         free( prefork );
726         return 1;
727 }
728
729 int prefork_child_free( prefork_child* child ) { 
730         free(child->appname);
731         close(child->read_data_fd);
732         close(child->write_status_fd);
733         free( child ); 
734         return 1;
735 }
736