]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/filed/job.c
This commit was manufactured by cvs2svn to create tag
[bacula/bacula] / bacula / src / filed / job.c
index 8c6652c4410b45b08e9de5bcdefc13dd063da597..fb0b90ab112edbc7d6220b1da13c172954f6be9c 100644 (file)
@@ -28,7 +28,6 @@
 
 #include "bacula.h"
 #include "filed.h"
-#include "host.h"
 
 extern char my_name[];
 extern CLIENT *me;                   /* our client resource */
@@ -55,6 +54,9 @@ static int response(JCR *jcr, BSOCK *sd, char *resp, char *cmd);
 static void filed_free_jcr(JCR *jcr);
 static int open_sd_read_session(JCR *jcr);
 static int send_bootstrap_file(JCR *jcr);
+static int runbefore_cmd(JCR *jcr);
+static int runafter_cmd(JCR *jcr);
+static int run_cmd(JCR *jcr, char *cmd, char *name);
 
 
 /* Exported functions */
@@ -68,21 +70,23 @@ struct s_cmds {
  * The following are the recognized commands from the Director. 
  */
 static struct s_cmds cmds[] = {
-   {"backup",   backup_cmd},
-   {"cancel",   cancel_cmd},
-   {"setdebug=", setdebug_cmd},
-   {"estimate", estimate_cmd},
-   {"exclude",  exclude_cmd},
-   {"Hello",    hello_cmd},
-   {"include",  include_cmd},
-   {"JobId=",   job_cmd},
-   {"level = ", level_cmd},
-   {"restore",  restore_cmd},
-   {"session",  session_cmd},
-   {"status",   status_cmd},
-   {"storage ", storage_cmd},
-   {"verify",   verify_cmd},
-   {"bootstrap",bootstrap_cmd},
+   {"backup",       backup_cmd},
+   {"cancel",       cancel_cmd},
+   {"setdebug=",    setdebug_cmd},
+   {"estimate",     estimate_cmd},
+   {"exclude",      exclude_cmd},
+   {"Hello",        hello_cmd},
+   {"include",      include_cmd},
+   {"JobId=",       job_cmd},
+   {"level = ",     level_cmd},
+   {"restore",      restore_cmd},
+   {"session",      session_cmd},
+   {"status",       status_cmd},
+   {"storage ",     storage_cmd},
+   {"verify",       verify_cmd},
+   {"bootstrap",    bootstrap_cmd},
+   {"RunBeforeJob", runbefore_cmd},
+   {"RunAfterJob",  runafter_cmd},
    {NULL,      NULL}                  /* list terminator */
 };
 
@@ -94,6 +98,8 @@ static char restorecmd[]  = "restore replace=%c prelinks=%d where=%s\n";
 static char restorecmd1[] = "restore replace=%c prelinks=%d where=\n";
 static char verifycmd[]   = "verify level=%30s\n";
 static char estimatecmd[] = "estimate listing=%d\n";
+static char runbefore[]   = "RunBeforeJob %s\n";
+static char runafter[]    = "RunAfterJob %s\n";
 
 /* Responses sent to Director */
 static char errmsg[]      = "2999 Invalid command\n";
@@ -108,10 +114,12 @@ static char OKverify[]    = "2000 OK verify\n";
 static char OKrestore[]   = "2000 OK restore\n";
 static char OKsession[]   = "2000 OK session\n";
 static char OKstore[]     = "2000 OK storage\n";
-static char OKjob[]       = "2000 OK Job " FDHOST "," DISTNAME "," DISTVER;
+static char OKjob[]       = "2000 OK Job " HOST_OS "," DISTNAME "," DISTVER;
 static char OKsetdebug[]  = "2000 OK setdebug=%d\n";
 static char BADjob[]      = "2901 Bad Job\n";
 static char EndJob[]      = "2800 End Job TermCode=%d JobFiles=%u ReadBytes=%s JobBytes=%s Errors=%u\n";
+static char OKRunBefore[] = "2000 OK RunBefore\n";
+static char OKRunAfter[]  = "2000 OK RunAfter\n";
 
 /* Responses received from Storage Daemon */
 static char OK_end[]       = "3000 OK end\n";
@@ -202,6 +210,10 @@ void *handle_client_request(void *dirp)
       bnet_sig(jcr->store_bsock, BNET_TERMINATE);
    }
 
+   if (jcr->RunAfterJob && !job_canceled(jcr)) {
+      run_cmd(jcr, jcr->RunAfterJob, "ClientRunAfterJob");
+   }
+
    /* Inform Director that we are done */
    bnet_sig(dir, BNET_TERMINATE);
 
@@ -295,7 +307,7 @@ static int estimate_cmd(JCR *jcr)
    }
    make_estimate(jcr);
    bnet_fsend(dir, OKest, jcr->num_files_examined, 
-      edit_uint64(jcr->JobBytes, ed2));
+      edit_uint64_with_commas(jcr->JobBytes, ed2));
    bnet_sig(dir, BNET_EOD);
    return 1;
 }
@@ -313,8 +325,8 @@ static int job_cmd(JCR *jcr)
              &jcr->VolSessionId, &jcr->VolSessionTime,
              sd_auth_key) != 5) {
       pm_strcpy(&jcr->errmsg, dir->msg);
-      bnet_fsend(dir, BADjob);
       Jmsg(jcr, M_FATAL, 0, _("Bad Job Command: %s"), jcr->errmsg);
+      bnet_fsend(dir, BADjob);
       free_pool_memory(sd_auth_key);
       return 0;
    }
@@ -324,22 +336,172 @@ static int job_cmd(JCR *jcr)
    return bnet_fsend(dir, OKjob);
 }
 
+static int runbefore_cmd(JCR *jcr)
+{
+   int stat;
+   BSOCK *dir = jcr->dir_bsock;
+   POOLMEM *cmd = get_memory(dir->msglen+1);
+
+   Dmsg1(100, "runbefore_cmd: %s", dir->msg);
+   if (sscanf(dir->msg, runbefore, cmd) != 1) {
+      pm_strcpy(&jcr->errmsg, dir->msg);
+      Jmsg1(jcr, M_FATAL, 0, _("Bad RunBeforeJob command: %s\n"), jcr->errmsg);
+      bnet_fsend(dir, "2905 Bad RunBeforeJob command.\n");
+      free_memory(cmd);
+      return 0;
+   }
+   unbash_spaces(cmd);
+
+   /* Run the command now */
+   stat = run_cmd(jcr, cmd, "ClientRunBeforeJob");
+   free_memory(cmd);
+   if (stat) {
+      bnet_fsend(dir, OKRunBefore);
+      return 1;
+   } else {
+      bnet_fsend(dir, "2905 Bad RunBeforeJob command.\n");
+      return 0;
+   }
+}
+
+static int runafter_cmd(JCR *jcr)
+{
+   BSOCK *dir = jcr->dir_bsock;
+   POOLMEM *msg = get_memory(dir->msglen+1);
+
+   Dmsg1(100, "runafter_cmd: %s", dir->msg);
+   if (sscanf(dir->msg, runafter, msg) != 1) {
+      pm_strcpy(&jcr->errmsg, dir->msg);
+      Jmsg1(jcr, M_FATAL, 0, _("Bad RunAfter command: %s\n"), jcr->errmsg);
+      bnet_fsend(dir, "2905 Bad RunAfterJob command.\n");
+      free_memory(msg);
+      return 0;
+   }
+   unbash_spaces(msg);
+   if (jcr->RunAfterJob) {
+      free_pool_memory(jcr->RunAfterJob);
+   }
+   jcr->RunAfterJob = get_pool_memory(PM_FNAME);
+   pm_strcpy(&jcr->RunAfterJob, msg);
+   free_pool_memory(msg);
+   return bnet_fsend(dir, OKRunAfter);
+}
+
+static int run_cmd(JCR *jcr, char *cmd, char *name)
+{
+   POOLMEM *ecmd = get_pool_memory(PM_FNAME);
+   int status;
+   BPIPE *bpipe;
+   char line[MAXSTRING];
+   
+   ecmd = edit_job_codes(jcr, ecmd, cmd, "");
+   bpipe = open_bpipe(ecmd, 0, "r");
+   free_pool_memory(ecmd);
+   while (fgets(line, sizeof(line), bpipe->rfd)) {
+      Jmsg(jcr, M_INFO, 0, _("%s: %s"), name, line);
+   }
+   status = close_bpipe(bpipe);
+   if (status != 0) {
+      Jmsg(jcr, M_FATAL, 0, _("%s returned non-zero status=%d\n"), name,
+        status);
+      set_jcr_job_status(jcr, JS_FatalError);
+      return 0;
+   }
+   return 1;
+}
+
+
 #define INC_LIST 0
 #define EXC_LIST 1
 
 static void add_fname_to_list(JCR *jcr, char *fname, int list)
 {
-   char *p;  
-   if (list == INC_LIST) {
-      add_fname_to_include_list((FF_PKT *)jcr->ff, 1, fname);
-   } else {
-      /* Skip leading options -- currently ignored */
-      for (p=fname; *p && *p != ' '; p++)
-        { }
-      /* Skip spaces */
-      for ( ; *p && *p == ' '; p++)
-        { }
-      add_fname_to_exclude_list((FF_PKT *)jcr->ff, p);
+   char *p, *q;
+   BPIPE *bpipe;
+   POOLMEM *fn;
+   FILE *ffd;
+   char buf[1000];
+   int optlen;
+   int stat;
+
+   /* Skip leading options -- currently ignored */
+   for (p=fname; *p && *p != ' '; p++)
+      { }
+   /* Skip spaces, and q points to first space */
+   for (q=NULL; *p && *p == ' '; p++) {
+      if (!q) {
+        q = p;
+      }
+   }
+
+   switch (*p) {
+   case '|':
+      p++;                           /* skip over | */
+      fn = get_pool_memory(PM_FNAME);
+      fn = edit_job_codes(jcr, fn, p, "");
+      bpipe = open_bpipe(fn, 0, "r");
+      free_pool_memory(fn);
+      if (!bpipe) {
+         Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
+           p, strerror(errno));
+        return;
+      }
+      /* Copy File options */
+      if (list == INC_LIST) {
+        *q = 0;                      /* terminate options */
+        strcpy(buf, fname);
+         strcat(buf, " ");
+        optlen = strlen(buf);
+      } else {
+        optlen = 0;
+      }
+      while (fgets(buf+optlen, sizeof(buf)-optlen, bpipe->rfd)) {
+        strip_trailing_junk(buf);
+        if (list == INC_LIST) {
+           add_fname_to_include_list((FF_PKT *)jcr->ff, 1, buf);
+        } else {
+           add_fname_to_exclude_list((FF_PKT *)jcr->ff, buf);
+        }
+      }
+      if ((stat=close_bpipe(bpipe)) != 0) {
+         Jmsg(jcr, M_FATAL, 0, _("Error running program: %s. RtnStat=%d ERR=%s\n"),
+           p, stat, strerror(errno));
+        return;
+      }
+      break;
+   case '<':
+      p++;                     /* skip over < */
+      if ((ffd = fopen(p, "r")) == NULL) {
+         Jmsg(jcr, M_FATAL, 0, _("Cannot open %s file: %s. ERR=%s\n"),
+            list==INC_LIST?"included":"excluded", p, strerror(errno));
+        return;
+      }
+      /* Copy File options */
+      if (list == INC_LIST) {
+        *q = 0;                      /* terminate options */
+        strcpy(buf, fname);
+         strcat(buf, " ");
+        optlen = strlen(buf);
+      } else {
+        optlen = 0;
+      }
+      while (fgets(buf+optlen, sizeof(buf)-optlen, ffd)) {
+        strip_trailing_junk(buf);
+        if (list == INC_LIST) {
+           add_fname_to_include_list((FF_PKT *)jcr->ff, 1, buf);
+        } else {
+           add_fname_to_exclude_list((FF_PKT *)jcr->ff, buf);
+        }
+      }
+      fclose(ffd);
+      break;
+   default:
+      if (list == INC_LIST) {
+        add_fname_to_include_list((FF_PKT *)jcr->ff, 1, fname);
+      } else {
+        add_fname_to_exclude_list((FF_PKT *)jcr->ff, p);
+      }
+      break;
    }
 }
 
@@ -429,7 +591,7 @@ static int bootstrap_cmd(JCR *jcr)
 static int level_cmd(JCR *jcr)
 {
    BSOCK *dir = jcr->dir_bsock;
-   POOLMEM *level;
+   POOLMEM *level, *buf = NULL;
    struct tm tm;
    time_t mtime;
    int mtime_only;
@@ -437,30 +599,25 @@ static int level_cmd(JCR *jcr)
    level = get_memory(dir->msglen+1);
    Dmsg1(110, "level_cmd: %s", dir->msg);
    if (sscanf(dir->msg, "level = %s ", level) != 1) {
-      pm_strcpy(&jcr->errmsg, dir->msg);
-      Jmsg1(jcr, M_FATAL, 0, _("Bad level command: %s\n"), jcr->errmsg);
-      free_memory(level);
-      return 0;
+      goto bail_out;
    }
    /* Base backup requested? */
    if (strcmp(level, "base") == 0) {
-      jcr->save_level = L_BASE;
+      jcr->JobLevel = L_BASE;
    /* Full backup requested? */ 
    } else if (strcmp(level, "full") == 0) {
-      jcr->save_level = L_FULL;
+      jcr->JobLevel = L_FULL;
    /* 
     * Backup requested since <date> <time>
     *  This form is also used for incremental and differential
+    *  This code is deprecated.  See since_utime for new code.
     */
    } else if (strcmp(level, "since") == 0) {
-      jcr->save_level = L_SINCE;
+      jcr->JobLevel = L_SINCE;
       if (sscanf(dir->msg, "level = since %d-%d-%d %d:%d:%d mtime_only=%d", 
                 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
                 &tm.tm_hour, &tm.tm_min, &tm.tm_sec, &mtime_only) != 7) {
-        pm_strcpy(&jcr->errmsg, dir->msg);
-         Jmsg1(jcr, M_FATAL, 0, _("Bad scan of date/time: %s\n"), jcr->errmsg);
-        free_memory(level);
-        return 0;
+        goto bail_out;
       }
       tm.tm_year -= 1900;
       tm.tm_mon  -= 1;
@@ -471,13 +628,75 @@ static int level_cmd(JCR *jcr)
       jcr->incremental = 1;          /* set incremental or decremental backup */
       jcr->mtime = mtime;            /* set since time */
       jcr->mtime_only = mtime_only;   /* and what to compare */
+   /*
+    * We get his UTC since time, then sync the clocks and correct it
+    *  to agree with our clock.
+    */
+   } else if (strcmp(level, "since_utime") == 0) {
+      buf = get_memory(dir->msglen+1);
+      utime_t since_time, adj;
+      btime_t his_time, bt_start, rt=0, bt_adj=0;
+      jcr->JobLevel = L_SINCE;
+      if (sscanf(dir->msg, "level = since_utime %s mtime_only=%d", 
+                buf, &mtime_only) != 2) { 
+        goto bail_out;
+      }
+      since_time = str_to_uint64(buf); /* this is the since time */
+      char ed1[50], ed2[50];
+      /* 
+       * Sync clocks by polling him for the time. We take       
+       *   10 samples of his time throwing out the first two.
+       */
+      for (int i=0; i<10; i++) {
+        bt_start = get_current_btime();
+        bnet_sig(dir, BNET_BTIME);   /* poll for time */
+        if (bnet_recv(dir) <= 0) {   /* get response */
+           goto bail_out;
+        }
+         if (sscanf(dir->msg, "btime %s", buf) != 1) {
+           goto bail_out;
+        }
+        if (i < 2) {                 /* toss first two results */
+           continue;
+        }
+        his_time = str_to_uint64(buf);
+        rt = get_current_btime() - bt_start; /* compute round trip time */
+        bt_adj -= his_time - bt_start - rt/2;
+         Dmsg2(100, "rt=%s adj=%s\n", edit_uint64(rt, ed1), edit_uint64(bt_adj, ed2));
+      }
+
+      bt_adj = bt_adj / 8;           /* compute average time */
+      Dmsg2(100, "rt=%s adj=%s\n", edit_uint64(rt, ed1), edit_uint64(bt_adj, ed2));
+      adj = btime_to_utime(bt_adj);
+      since_time += adj;             /* adjust for clock difference */
+      if (adj != 0) {
+         Jmsg(jcr, M_INFO, 0, _("Since time adjusted by %d seconds.\n"), adj);
+      }
+      bnet_sig(dir, BNET_EOD);
+
+      Dmsg2(100, "adj = %d since_time=%d\n", (int)adj, (int)since_time);
+      jcr->incremental = 1;          /* set incremental or decremental backup */
+      jcr->mtime = since_time;       /* set since time */
+      jcr->mtime_only = mtime_only;   /* and what to compare */
    } else {
       Jmsg1(jcr, M_FATAL, 0, "Unknown backup level: %s\n", level);
       free_memory(level);
       return 0;
    }
    free_memory(level);
+   if (buf) {
+      free_memory(buf);
+   }
    return bnet_fsend(dir, OKlevel);
+
+bail_out:
+   pm_strcpy(&jcr->errmsg, dir->msg);
+   Jmsg1(jcr, M_FATAL, 0, _("Bad level command: %s\n"), jcr->errmsg);
+   free_memory(level);
+   if (buf) {
+      free_memory(buf);
+   }
+   return 0;
 }
 
 /*
@@ -520,7 +739,7 @@ static int storage_cmd(JCR *jcr)
    Dmsg3(110, "Open storage: %s:%d ssl=%d\n", jcr->stored_addr, stored_port, enable_ssl);
    /* Open command communications with Storage daemon */
    /* Try to connect for 1 hour at 10 second intervals */
-   sd = bnet_connect(jcr, 10, 3600, _("Storage daemon"), 
+   sd = bnet_connect(jcr, 10, me->SDConnectTimeout, _("Storage daemon"), 
                     jcr->stored_addr, NULL, stored_port, 1);
    if (sd == NULL) {
       Jmsg(jcr, M_FATAL, 0, _("Failed to connect to Storage daemon: %s:%d\n"),
@@ -682,6 +901,8 @@ static int verify_cmd(JCR *jcr)
       jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
    } else if (strcasecmp(level, "data") == 0){
       jcr->JobLevel = L_VERIFY_DATA;
+   } else if (strcasecmp(level, "disk_to_catalog") == 0) {
+      jcr->JobLevel = L_VERIFY_DISK_TO_CATALOG;
    } else {   
       bnet_fsend(dir, "2994 Bad verify level: %s\n", dir->msg);
       return 0;   
@@ -714,6 +935,9 @@ static int verify_cmd(JCR *jcr)
       /* Inform Storage daemon that we are done */
       bnet_sig(sd, BNET_TERMINATE);
 
+      break;
+   case L_VERIFY_DISK_TO_CATALOG:
+      do_verify(jcr);
       break;
    default:
       bnet_fsend(dir, "2994 Bad verify level: %s\n", dir->msg);
@@ -888,10 +1112,14 @@ static void filed_free_jcr(JCR *jcr)
    if (jcr->RestoreBootstrap) {
       unlink(jcr->RestoreBootstrap);
       free_pool_memory(jcr->RestoreBootstrap);
+      jcr->RestoreBootstrap = NULL;
    }
    if (jcr->last_fname) {
       free_pool_memory(jcr->last_fname);
    }
+   if (jcr->RunAfterJob) {
+      free_pool_memory(jcr->RunAfterJob);
+   }
    return;
 }
 
@@ -904,12 +1132,10 @@ static void filed_free_jcr(JCR *jcr)
  */
 int response(JCR *jcr, BSOCK *sd, char *resp, char *cmd)
 {
-   int n;
-
    if (sd->errors) {
       return 0;
    }
-   if ((n = bget_msg(sd)) > 0) {
+   if (bget_msg(sd) > 0) {
       Dmsg0(110, sd->msg);
       if (strcmp(sd->msg, resp) == 0) {
         return 1;
@@ -934,6 +1160,7 @@ static int send_bootstrap_file(JCR *jcr)
    char buf[2000];
    BSOCK *sd = jcr->store_bsock;
    char *bootstrap = "bootstrap\n";
+   int stat = 0;
 
    Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
    if (!jcr->RestoreBootstrap) {
@@ -944,9 +1171,9 @@ static int send_bootstrap_file(JCR *jcr)
       Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"), 
         jcr->RestoreBootstrap, strerror(errno));
       set_jcr_job_status(jcr, JS_ErrorTerminated);
-      return 0;
+      goto bail_out;
    }
-   strcpy(sd->msg, bootstrap); 
+   pm_strcpy(&sd->msg, bootstrap);  
    sd->msglen = strlen(sd->msg);
    bnet_send(sd);
    while (fgets(buf, sizeof(buf), bs)) {
@@ -957,7 +1184,16 @@ static int send_bootstrap_file(JCR *jcr)
    fclose(bs);
    if (!response(jcr, sd, OKSDbootstrap, "Bootstrap")) {
       set_jcr_job_status(jcr, JS_ErrorTerminated);
-      return 0;
+      goto bail_out;
    }
-   return 1;
+   stat = 1;
+
+bail_out:
+   if (jcr->RestoreBootstrap) {
+      unlink(jcr->RestoreBootstrap);
+      free_pool_memory(jcr->RestoreBootstrap);
+      jcr->RestoreBootstrap = NULL;
+   }
+
+   return stat;
 }