]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/bpipe.c
Detect mount/junction points and ignore junctions in Windows
[bacula/bacula] / bacula / src / lib / bpipe.c
index d95236d79422497f0ddf1a30d3e99f720096904b..79a6c0cbceb5de4cb86389cc04217ff177e1129c 100644 (file)
@@ -1,12 +1,12 @@
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2002-2007 Free Software Foundation Europe e.V.
+   Copyright (C) 2002-2010 Free Software Foundation Europe e.V.
 
    The main author of Bacula is Kern Sibbald, with contributions from
    many others, a complete list can be found in the file AUTHORS.
    This program is Free Software; you can redistribute it and/or
-   modify it under the terms of version two of the GNU General Public
+   modify it under the terms of version three of the GNU Affero General Public
    License as published by the Free Software Foundation and included
    in the file LICENSE.
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    General Public License for more details.
 
-   You should have received a copy of the GNU General Public License
+   You should have received a copy of the GNU Affero General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   Bacula® is a registered trademark of John Walker.
+   Bacula® is a registered trademark of Kern Sibbald.
    The licensor of Bacula is the Free Software Foundation Europe
    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
    Switzerland, email:ftf@fsfeurope.org.
@@ -30,7 +30,6 @@
  *
  *    Kern Sibbald, November MMII
  *
- *   Version $Id$
  */
 
 
@@ -135,7 +134,8 @@ BPIPE *open_bpipe(char *prog, int wait, const char *mode)
          dup2(readp[1], 1);           /* dup our read to his stdout */
          dup2(readp[1], 2);           /*   and his stderr */
       }
-      closelog();                     /* close syslog if open */
+/* Note, the close log cause problems, see bug #1536 */
+/*    closelog();                        close syslog if open */
       for (i=3; i<=32; i++) {         /* close any open file descriptors */
          close(i);
       }
@@ -163,7 +163,7 @@ BPIPE *open_bpipe(char *prog, int wait, const char *mode)
    bpipe->worker_stime = time(NULL);
    bpipe->wait = wait;
    if (wait > 0) {
-      bpipe->timer_id = start_child_timer(bpipe->worker_pid, wait);
+      bpipe->timer_id = start_child_timer(NULL, bpipe->worker_pid, wait);
    }
    return bpipe;
 }
@@ -326,45 +326,41 @@ static void build_argc_argv(char *cmd, int *bargc, char *bargv[], int max_argv)
  *  Returns: 0 on success
  *           non-zero on error == berrno status
  */
-int run_program(char *prog, int wait, POOLMEM *results)
+int run_program(char *prog, int wait, POOLMEM *&results)
 {
    BPIPE *bpipe;
    int stat1, stat2;
    char *mode;
 
-   mode = (char *)(results != NULL ? "r" : "");
+   mode = (char *)"r";
    bpipe = open_bpipe(prog, wait, mode);
    if (!bpipe) {
       return ENOENT;
    }
-   if (results) {
-      results[0] = 0;
-      int len = sizeof_pool_memory(results) - 1;
-      fgets(results, len, bpipe->rfd);
-      results[len] = 0;
-      if (feof(bpipe->rfd)) {
-         stat1 = 0;
-      } else {
-         stat1 = ferror(bpipe->rfd);
-      }
-      if (stat1 < 0) {
-         berrno be;
-         Dmsg2(150, "Run program fgets stat=%d ERR=%s\n", stat1, be.bstrerror(errno));
-      } else if (stat1 != 0) {
-         Dmsg1(150, "Run program fgets stat=%d\n", stat1);
-         if (bpipe->timer_id) {
-            Dmsg1(150, "Run program fgets killed=%d\n", bpipe->timer_id->killed);
-            /* NB: I'm not sure it is really useful for run_program. Without the
-             * following lines run_program would not detect if the program was killed
-             * by the watchdog. */
-            if (bpipe->timer_id->killed) {
-               stat1 = ETIME;
-               pm_strcat(results, _("Program killed by Bacula watchdog (timeout)\n"));
-            }
+   results[0] = 0;
+   int len = sizeof_pool_memory(results) - 1;
+   fgets(results, len, bpipe->rfd);
+   results[len] = 0;
+   if (feof(bpipe->rfd)) {
+      stat1 = 0;
+   } else {
+      stat1 = ferror(bpipe->rfd);
+   }
+   if (stat1 < 0) {
+      berrno be;
+      Dmsg2(150, "Run program fgets stat=%d ERR=%s\n", stat1, be.bstrerror(errno));
+   } else if (stat1 != 0) {
+      Dmsg1(150, "Run program fgets stat=%d\n", stat1);
+      if (bpipe->timer_id) {
+         Dmsg1(150, "Run program fgets killed=%d\n", bpipe->timer_id->killed);
+         /* NB: I'm not sure it is really useful for run_program. Without the
+          * following lines run_program would not detect if the program was killed
+          * by the watchdog. */
+         if (bpipe->timer_id->killed) {
+            stat1 = ETIME;
+            pm_strcpy(results, _("Program killed by Bacula (timeout)\n"));
          }
       }
-   } else {
-      stat1 = 0;
    }
    stat2 = close_bpipe(bpipe);
    stat1 = stat2 != 0 ? stat2 : stat1;
@@ -388,7 +384,7 @@ int run_program(char *prog, int wait, POOLMEM *results)
  *           non-zero on error == berrno status
  *
  */
-int run_program_full_output(char *prog, int wait, POOLMEM *results)
+int run_program_full_output(char *prog, int wait, POOLMEM *&results)
 {
    BPIPE *bpipe;
    int stat1, stat2;
@@ -397,22 +393,18 @@ int run_program_full_output(char *prog, int wait, POOLMEM *results)
    char *buf;
    const int bufsize = 32000;
 
-   if (results == NULL) {
-      return run_program(prog, wait, NULL);
-   }
    
    sm_check(__FILE__, __LINE__, false);
 
    tmp = get_pool_memory(PM_MESSAGE);
    buf = (char *)malloc(bufsize+1);
    
+   results[0] = 0;
    mode = (char *)"r";
    bpipe = open_bpipe(prog, wait, mode);
    if (!bpipe) {
-      if (results) {
-         results[0] = 0;
-      }
-      return ENOENT;
+      stat1 = ENOENT;
+      goto bail_out;
    }
    
    sm_check(__FILE__, __LINE__, false);
@@ -449,16 +441,16 @@ int run_program_full_output(char *prog, int wait, POOLMEM *results)
     */
    if (bpipe->timer_id && bpipe->timer_id->killed) {
       Dmsg1(150, "Run program fgets killed=%d\n", bpipe->timer_id->killed);
-      pm_strcat(tmp, _("Program killed by Bacula watchdog (timeout)\n"));
+      pm_strcpy(tmp, _("Program killed by Bacula (timeout)\n"));
       stat1 = ETIME;
    }
-   int len = sizeof_pool_memory(results) - 1;
-   bstrncpy(results, tmp, len);
+   pm_strcpy(results, tmp);
    Dmsg3(1900, "resadr=0x%x reslen=%d res=%s\n", results, strlen(results), results);
    stat2 = close_bpipe(bpipe);
    stat1 = stat2 != 0 ? stat2 : stat1;
    
    Dmsg1(900, "Run program returning %d\n", stat1);
+bail_out:
    free_pool_memory(tmp);
    free(buf);
    return stat1;