]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/bpipe.c
This commit was manufactured by cvs2svn to create tag
[bacula/bacula] / bacula / src / lib / bpipe.c
index 975c5f6896f5efbec918dbbc07091f58a9f6c8f7..1501e18d2c41fd4b8333e709ebaaf5591e2c69b7 100644 (file)
@@ -57,7 +57,7 @@ BPIPE *open_bpipe(char *prog, int wait, char *mode)
    /* Build arguments for running program. */
    tprog = get_pool_memory(PM_FNAME);
    pm_strcpy(&tprog, prog);
-   build_argc_argv(tprog, &bargc, bargv, MAX_ARGV);
+   build_argc_argv(mp_chr(tprog), &bargc, bargv, MAX_ARGV);
 #ifdef xxxxxx
    printf("argc=%d\n", bargc);
    int i;
@@ -78,7 +78,7 @@ BPIPE *open_bpipe(char *prog, int wait, char *mode)
    }
    /* Start worker process */
    switch (bpipe->worker_pid = fork()) {
-   case -1:
+   case -1:                          /* error */
       free(bpipe);
       return NULL;
 
@@ -92,6 +92,9 @@ BPIPE *open_bpipe(char *prog, int wait, char *mode)
         dup2(readp[1], 1);           /* dup our read to his stdout */
         dup2(readp[1], 2);           /*   and his stderr */
       }
+      for (int i=3; i<=32; i++) {     /* close any open file descriptors */
+        close(i);
+      }
       execvp(bargv[0], bargv);       /* call the program */
       exit(errno);                    /* shouldn't get here */
 
@@ -158,10 +161,17 @@ int close_bpipe(BPIPE *bpipe)
 
    /* wait for worker child to exit */
    for ( ;; ) {
-      wpid = waitpid(bpipe->worker_pid, &chldstatus, wait_option);
-      if (wpid == bpipe->worker_pid || (wpid == -1 && errno != EINTR)) {
+      Dmsg2(200, "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) {
+         Dmsg3(200, "Got break wpid=%d status=%d ERR=%s\n", wpid, chldstatus,
+            wpid==-1?strerror(errno):"none");
         break;
       }
+      Dmsg3(200, "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--;
@@ -175,8 +185,10 @@ int close_bpipe(BPIPE *bpipe)
    if (wpid > 0) {
       if (WIFEXITED(chldstatus)) {          /* process exit()ed */
         stat = WEXITSTATUS(chldstatus);
+          Dmsg1(200, "status =%d\n", stat);
       } else if (WIFSIGNALED(chldstatus)) {  /* process died */
         stat = 1;
+         Dmsg0(200, "Signaled\n");
       }
       if (stat != 0) {
         errno = ECHILD;              /* set child errno */
@@ -186,9 +198,7 @@ int close_bpipe(BPIPE *bpipe)
       stop_child_timer(bpipe->timer_id);
    }
    free(bpipe);
-#ifdef HAVE_FREEBSD_OS
-   stat = 0;  /* kludge because FreeBSD doesn't seem to return valid status */
-#endif
+   Dmsg1(200, "returning stat = %d\n", stat);
    return stat;
 }
 
@@ -215,8 +225,8 @@ int run_program(char *prog, int wait, POOLMEM *results)
       return 0;
    }
    if (results) {
-      results[0] = 0;
-      stat1 = fgets(results, sizeof_pool_memory(results), bpipe->rfd) == NULL;
+      mp_chr(results)[0] = 0;
+      stat1 = fgets(mp_chr(results), sizeof_pool_memory(results), bpipe->rfd) == NULL;
    } else {
       stat1 = 0;
    }