]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/bpipe.c
Document SHA1 + work on returning child status
[bacula/bacula] / bacula / src / lib / bpipe.c
index 8c415d65e1872db92cf7bd78d3c341e39f8cc57c..729d5e2a10e10f071da245d44b5299ea73ebe0bf 100644 (file)
@@ -58,7 +58,7 @@ BPIPE *open_bpipe(char *prog, int wait, char *mode)
    tprog = get_pool_memory(PM_FNAME);
    pm_strcpy(&tprog, prog);
    build_argc_argv(tprog, &bargc, bargv, MAX_ARGV);
-#ifdef xxxxxx
+#ifdef xxxxxx
    printf("argc=%d\n", bargc);
    int i;
    for (i=0; i<bargc; i++) {
@@ -108,6 +108,9 @@ BPIPE *open_bpipe(char *prog, int wait, char *mode)
    }
    bpipe->worker_stime = time(NULL);
    bpipe->wait = wait;
+   if (wait > 0) {
+      bpipe->timer_id = start_child_timer(bpipe->worker_pid, wait);
+   }
    return bpipe;
 }
 
@@ -117,6 +120,7 @@ int close_wpipe(BPIPE *bpipe)
    int stat = 1;
 
    if (bpipe->wfd) {
+      fflush(bpipe->wfd);
       if (fclose(bpipe->wfd) != 0) {
         stat = 0;
       }
@@ -129,9 +133,11 @@ int close_wpipe(BPIPE *bpipe)
 int close_bpipe(BPIPE *bpipe) 
 {
    int chldstatus = 0;
-   int stat = ETIME;
+   int stat = 0;    
    int wait_option;
    int remaining_wait;
+   pid_t wpid = 0;
+
 
    /* Close pipes */
    if (bpipe->rfd) {
@@ -152,7 +158,6 @@ int close_bpipe(BPIPE *bpipe)
 
    /* wait for worker child to exit */
    for ( ;; ) {
-      pid_t wpid;
       wpid = waitpid(bpipe->worker_pid, &chldstatus, wait_option);
       if (wpid == bpipe->worker_pid || (wpid == -1 && errno != EINTR)) {
         break;
@@ -161,13 +166,29 @@ int close_bpipe(BPIPE *bpipe)
         sleep(1);                    /* wait one second */
         remaining_wait--;
       } else {
+        stat = 1;                    /* set error status */
+        errno = ETIME;               /* set timed out */
+        wpid = -1;
          break;                       /* don't wait any longer */
       }
    }
-   if (WIFEXITED(chldstatus)) {
-      stat = WEXITSTATUS(chldstatus);
+   if (wpid > 0) {
+      if (WIFEXITED(chldstatus)) {          /* process exit()ed */
+        stat = WEXITSTATUS(chldstatus);
+      } else if (WIFSIGNALED(chldstatus)) {  /* process died */
+        stat = 1;
+      }
+      if (stat != 0) {
+        errno = ECHILD;              /* set child errno */
+      }
+   }  
+   if (bpipe->timer_id) {
+      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
    return stat;
 }
 
@@ -204,8 +225,8 @@ int run_program(char *prog, int wait, POOLMEM *results)
  */
 static void build_argc_argv(char *cmd, int *bargc, char *bargv[], int max_argv)
 {
-   int i, quote;
-   char *p, *q;
+   int i;      
+   char *p, *q, quote;
    int argc = 0;
 
    argc = 0;
@@ -216,15 +237,15 @@ static void build_argc_argv(char *cmd, int *bargc, char *bargv[], int max_argv)
    quote = 0;
    while  (*p && (*p == ' ' || *p == '\t'))
       p++;
-   if (*p == '\"') {
-      quote = 1;
+   if (*p == '\"' || *p == '\'') {
+      quote = *p;
       p++;
    }
    if (*p) {
       while (*p && argc < MAX_ARGV) {
         q = p;
         if (quote) {
-            while (*q && *q != '\"')
+           while (*q && *q != quote)
            q++;
            quote = 0;
         } else {
@@ -237,8 +258,8 @@ static void build_argc_argv(char *cmd, int *bargc, char *bargv[], int max_argv)
         p = q;
          while (*p && (*p == ' ' || *p == '\t'))
            p++;
-         if (*p == '\"') {
-           quote = 1;
+         if (*p == '\"' || *p == '\'') {
+           quote = *p;
            p++;
         }
       }