src/lib/bpipe.c
int close_bpipe(BPIPE *bpipe)
{
   int chldstatus = 0;
   int stat = 0;
   int wait_option;
   int remaining_wait;
   pid_t wpid = 0;


   /* Close pipes */
   if (bpipe->rfd) {
      fclose(bpipe->rfd);
      bpipe->rfd = NULL;
   }
   if (bpipe->wfd) {
      fclose(bpipe->wfd);
      bpipe->wfd = NULL;
   }
   if (bpipe->efd) {
      fclose(bpipe->efd);
      bpipe->efd = NULL;
   }

   if (bpipe->wait == 0) {
      wait_option = 0;                /* wait indefinitely */
   } else {
      wait_option = WNOHANG;          /* don't hang */
   }
   remaining_wait = bpipe->wait;

   /* wait for worker child to exit */
   for ( ;; ) {
      Dmsg2(100, "Wait for %d opt=%d\n", bpipe->worker_pid, wait_option);
      do {
         wpid = waitpid(bpipe->worker_pid, &chldstatus, wait_option);
      } while (wpid == -1 && (errno == EINTR || errno == EAGAIN));
      if (wpid == bpipe->worker_pid || wpid == -1) {
         berrno be;
         stat = errno;
         Dmsg3(100, "Got break wpid=%d status=%d ERR=%s\n", wpid, chldstatus,
            wpid==-1?be.bstrerror():"none");
         break;
      }
      Dmsg3(100, "Got wpid=%d status=%d ERR=%s\n", wpid, chldstatus,
            wpid==-1?strerror(errno):"none");
      if (remaining_wait > 0) {
         bmicrosleep(1, 0);           /* wait one second */
         remaining_wait--;
      } else {
         stat = ETIME;                /* set error status */
         wpid = -1;
         break;                       /* don't wait any longer */
      }
   }
   if (wpid > 0) {
      if (WIFEXITED(chldstatus)) {    /* process exit()ed */
         stat = WEXITSTATUS(chldstatus);
         if (stat != 0) {
            Dmsg1(100, "Non-zero status %d returned from child.\n", stat);
            stat |= b_errno_exit;        /* exit status returned */
         }
         Dmsg1(100, "child status=%d\n", stat & ~b_errno_exit);
      } else if (WIFSIGNALED(chldstatus)) {  /* process died */
#ifndef HAVE_WIN32
         stat = WTERMSIG(chldstatus);
#else
         stat = 1;                    /* fake child status */
#endif
         Dmsg1(100, "Child died from signal %d\n", stat);
         stat |= b_errno_signal;      /* exit signal returned */
      }
   }
   if (bpipe->timer_id) {
      stop_child_timer(bpipe->timer_id);
   }
   free(bpipe);
   Dmsg2(100, "returning stat=%d,%d\n", stat & ~(b_errno_exit|b_errno_signal), stat);
   return stat;
}
/*
 * Close both pipes and free resources
 *
 *  Returns: 0 on success
 *           berrno on failure
 */
Close both pipes and free resources
Returns: 0 on success
berrno on failure
Close both pipes and free resources Returns : 0 on success berrno on failure
int close_bpipe(BPIPE *bpipe)
{
   int chldstatus = 0;
   int stat = 0;
   int wait_option;
   int remaining_wait;
   pid_t wpid = 0;
   if (bpipe->rfd) {
      fclose(bpipe->rfd);
      bpipe->rfd = NULL;
   }
   if (bpipe->wfd) {
      fclose(bpipe->wfd);
      bpipe->wfd = NULL;
   }
   if (bpipe->efd) {
      fclose(bpipe->efd);
      bpipe->efd = NULL;
   }
   if (bpipe->wait == 0) {
      wait_option = 0;                
   } else {
      wait_option = WNOHANG;          
   }
   remaining_wait = bpipe->wait;
   for ( ;; ) {
      Dmsg2(100, "Wait for %d opt=%d\n", bpipe->worker_pid, wait_option);
      do {
         wpid = waitpid(bpipe->worker_pid, &chldstatus, wait_option);
      } while (wpid == -1 && (errno == EINTR || errno == EAGAIN));
      if (wpid == bpipe->worker_pid || wpid == -1) {
         berrno be;
         stat = errno;
         Dmsg3(100, "Got break wpid=%d status=%d ERR=%s\n", wpid, chldstatus,
            wpid==-1?be.bstrerror():"none");
         break;
      }
      Dmsg3(100, "Got wpid=%d status=%d ERR=%s\n", wpid, chldstatus,
            wpid==-1?strerror(errno):"none");
      if (remaining_wait > 0) {
         bmicrosleep(1, 0);           
         remaining_wait--;
      } else {
         stat = ETIME;                
         wpid = -1;
         break;                       
      }
   }
   if (wpid > 0) {
      if (WIFEXITED(chldstatus)) {    
         stat = WEXITSTATUS(chldstatus);
         if (stat != 0) {
            Dmsg1(100, "Non-zero status %d returned from child.\n", stat);
            stat |= b_errno_exit;        
         }
         Dmsg1(100, "child status=%d\n", stat & ~b_errno_exit);
      } else if (WIFSIGNALED(chldstatus)) {  
#ifndef HAVE_WIN32
         stat = WTERMSIG(chldstatus);
#else
         stat = 1;                    
#endif
         Dmsg1(100, "Child died from signal %d\n", stat);
         stat |= b_errno_signal;      
      }
   }
   if (bpipe->timer_id) {
      stop_child_timer(bpipe->timer_id);
   }
   free(bpipe);
   Dmsg2(100, "returning stat=%d,%d\n", stat & ~(b_errno_exit|b_errno_signal), stat);
   return stat;
}
int close_bpipe ( BPIPE * bpipe ) { int chldstatus = 0 ; int stat = 0 ; int wait_option ; int remaining_wait ; pid_t wpid = 0 ; if ( bpipe -> rfd ) { fclose ( bpipe -> rfd ) ; bpipe -> rfd = NULL ; } if ( bpipe -> wfd ) { fclose ( bpipe -> wfd ) ; bpipe -> wfd = NULL ; } if ( bpipe -> efd ) { fclose ( bpipe -> efd ) ; bpipe -> efd = NULL ; } if ( bpipe -> wait == 0 ) { wait_option = 0 ; } else { wait_option = WNOHANG ; } remaining_wait = bpipe -> wait ; for ( ; ; ) { Dmsg2 ( 100 , " \n " , bpipe -> worker_pid , wait_option ) ; do { wpid = waitpid ( bpipe -> worker_pid , & chldstatus , wait_option ) ; } while ( wpid == -1 && ( errno == EINTR || errno == EAGAIN ) ) ; if ( wpid == bpipe -> worker_pid || wpid == -1 ) { berrno be ; stat = errno ; Dmsg3 ( 100 , " \n " , wpid , chldstatus , wpid == -1 ? be . bstrerror ( ) : " " ) ; break ; } Dmsg3 ( 100 , " \n " , wpid , chldstatus , wpid == -1 ? strerror ( errno ) : " " ) ; if ( remaining_wait > 0 ) { bmicrosleep ( 1 , 0 ) ; remaining_wait -- ; } else { stat = ETIME ; wpid = -1 ; break ; } } if ( wpid > 0 ) { if ( WIFEXITED ( chldstatus ) ) { stat = WEXITSTATUS ( chldstatus ) ; if ( stat != 0 ) { Dmsg1 ( 100 , " \n " , stat ) ; stat |= b_errno_exit ; } Dmsg1 ( 100 , " \n " , stat & ~ b_errno_exit ) ; } else if ( WIFSIGNALED ( chldstatus ) ) { #ifndef HAVE_WIN32 stat = WTERMSIG ( chldstatus ) ; #else stat = 1 ; #endif Dmsg1 ( 100 , " \n " , stat ) ; stat |= b_errno_signal ; } } if ( bpipe -> timer_id ) { stop_child_timer ( bpipe -> timer_id ) ; } free ( bpipe ) ; Dmsg2 ( 100 , " \n " , stat & ~ ( b_errno_exit | b_errno_signal ) , stat ) ; return stat ; }
Close both pipes and free resources
  Returns: 0 on success
           berrno on failure
Close both pipes and free resources Returns : 0 on success berrno on failure
/* Close pipes */ /* wait indefinitely */ /* don't hang */ /* wait for worker child to exit */ /* wait one second */ /* set error status */ /* don't wait any longer */ /* process exit()ed */ /* exit status returned */ /* process died */ /* fake child status */ /* exit signal returned */