]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/stored/fd_cmds.c
Apply Joao's patch to SQLite tables to make chars work.
[bacula/bacula] / bacula / src / stored / fd_cmds.c
index 330ee30b9a15910c51f800c1e4d6ffc9a743ff5b..7cd070e6c505664aea92ad6375d57b2f460759e1 100644 (file)
@@ -1,3 +1,30 @@
+/*
+   Bacula® - The Network Backup Solution
+
+   Copyright (C) 2000-2007 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
+   License as published by the Free Software Foundation and included
+   in the file LICENSE.
+
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   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
+   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 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.
+*/
 /*
  * This file handles commands from the File daemon.
  *
  *   Version $Id$
  *
  */
-/*
-   Copyright (C) 2000-2005 Kern Sibbald
-
-   This program is free software; you can redistribute it and/or
-   modify it under the terms of the GNU General Public License as
-   published by the Free Software Foundation; either version 2 of
-   the License, or (at your option) any later version.
-
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   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 along with this program; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
-   MA 02111-1307, USA.
-
- */
 
 #include "bacula.h"
 #include "stored.h"
@@ -53,9 +61,10 @@ static bool append_end_session(JCR *jcr);
 static bool read_open_session(JCR *jcr);
 static bool read_data_cmd(JCR *jcr);
 static bool read_close_session(JCR *jcr);
+static bool bootstrap_cmd(JCR *jcr);
 
 /* Exported function */
-bool bootstrap_cmd(JCR *jcr);
+bool get_bootstrap_file(JCR *jcr, BSOCK *bs);
 
 struct s_cmds {
    const char *cmd;
@@ -86,18 +95,18 @@ static char NOT_opened[]      = "3902 Error session not opened\n";
 static char OK_end[]          = "3000 OK end\n";
 static char OK_close[]        = "3000 OK close Status = %d\n";
 static char OK_open[]         = "3000 OK open ticket = %d\n";
-static char OK_append[]       = "3000 OK append data\n";
 static char ERROR_append[]    = "3903 Error append data\n";
 static char OK_bootstrap[]    = "3000 OK bootstrap\n";
 static char ERROR_bootstrap[] = "3904 Error bootstrap\n";
 
 /* Information sent to the Director */
 static char Job_start[] = "3010 Job %s start\n";
-static char Job_end[]   =
+char Job_end[]   =
    "3099 Job %s end JobStatus=%d JobFiles=%d JobBytes=%s\n";
 
 /*
  * Run a File daemon Job -- File daemon already authorized
+ *  Director sends us this command.
  *
  * Basic task here is:
  * - Read a command from the File daemon
@@ -106,26 +115,42 @@ static char Job_end[]   =
  */
 void run_job(JCR *jcr)
 {
-   int i;
-   bool found, quit;
-   BSOCK *fd = jcr->file_bsock;
    BSOCK *dir = jcr->dir_bsock;
    char ec1[30];
 
-
-   fd->jcr = jcr;
-   dir->jcr = jcr;
+   dir->set_jcr(jcr);
    Dmsg1(120, "Start run Job=%s\n", jcr->Job);
-   bnet_fsend(dir, Job_start, jcr->Job);
+   dir->fsend(Job_start, jcr->Job);
    jcr->start_time = time(NULL);
    jcr->run_time = jcr->start_time;
    set_jcr_job_status(jcr, JS_Running);
    dir_send_job_status(jcr);          /* update director */
+   do_fd_commands(jcr);
+   jcr->end_time = time(NULL);
+   dequeue_messages(jcr);             /* send any queued messages */
+   set_jcr_job_status(jcr, JS_Terminated);
+   generate_daemon_event(jcr, "JobEnd");
+   dir->fsend(Job_end, jcr->Job, jcr->JobStatus, jcr->JobFiles,
+      edit_uint64(jcr->JobBytes, ec1));
+   dir->signal(BNET_EOD);             /* send EOD to Director daemon */
+   return;
+}
+
+/*
+ * Now talk to the FD and do what he says
+ */
+void do_fd_commands(JCR *jcr)
+{
+   int i;
+   bool found, quit;
+   BSOCK *fd = jcr->file_bsock;
+
+   fd->set_jcr(jcr);
    for (quit=false; !quit;) {
       int stat;
 
       /* Read command coming from the File daemon */
-      stat = bnet_recv(fd);
+      stat = fd->recv();
       if (is_bnet_stop(fd)) {         /* hardeof or error */
          break;                       /* connection terminated */
       }
@@ -137,7 +162,7 @@ void run_job(JCR *jcr)
       for (i=0; fd_cmds[i].cmd; i++) {
          if (strncmp(fd_cmds[i].cmd, fd->msg, strlen(fd_cmds[i].cmd)) == 0) {
             found = true;               /* indicate command found */
-            if (!fd_cmds[i].func(jcr)) {    /* do command */
+            if (!fd_cmds[i].func(jcr) || job_canceled(jcr)) {    /* do command */
                set_jcr_job_status(jcr, JS_ErrorTerminated);
                quit = true;
             }
@@ -146,22 +171,13 @@ void run_job(JCR *jcr)
       }
       if (!found) {                   /* command not found */
          Dmsg1(110, "<filed: Command not found: %s\n", fd->msg);
-         bnet_fsend(fd, ferrmsg);
+         fd->fsend(ferrmsg);
          break;
       }
    }
-   bnet_sig(fd, BNET_TERMINATE);      /* signal to FD job is done */
-   jcr->end_time = time(NULL);
-   dequeue_messages(jcr);             /* send any queued messages */
-   set_jcr_job_status(jcr, JS_Terminated);
-   generate_daemon_event(jcr, "JobEnd");
-   bnet_fsend(dir, Job_end, jcr->Job, jcr->JobStatus, jcr->JobFiles,
-      edit_uint64(jcr->JobBytes, ec1));
-   bnet_sig(dir, BNET_EOD);           /* send EOD to Director daemon */
-   return;
+   fd->signal(BNET_TERMINATE);        /* signal to FD job is done */
 }
 
-
 /*
  *   Append Data command
  *     Open Data Channel and receive Data for archiving
@@ -174,15 +190,15 @@ static bool append_data_cmd(JCR *jcr)
    Dmsg1(120, "Append data: %s", fd->msg);
    if (jcr->session_opened) {
       Dmsg1(110, "<bfiled: %s", fd->msg);
-      jcr->JobType = JT_BACKUP;
+      jcr->set_JobType(JT_BACKUP);
       if (do_append_data(jcr)) {
-         return bnet_fsend(fd, OK_append);
+         return true;
       } else {
          bnet_suppress_error_messages(fd, 1); /* ignore errors at this point */
-         bnet_fsend(fd, ERROR_append);
+         fd->fsend(ERROR_append);
       }
    } else {
-      bnet_fsend(fd, NOT_opened);
+      fd->fsend(NOT_opened);
    }
    return false;
 }
@@ -193,11 +209,11 @@ static bool append_end_session(JCR *jcr)
 
    Dmsg1(120, "store<file: %s", fd->msg);
    if (!jcr->session_opened) {
-      bnet_fsend(fd, NOT_opened);
+      fd->fsend(NOT_opened);
       return false;
    }
    set_jcr_job_status(jcr, JS_Terminated);
-   return bnet_fsend(fd, OK_end);
+   return fd->fsend(OK_end);
 }
 
 
@@ -211,14 +227,14 @@ static bool append_open_session(JCR *jcr)
 
    Dmsg1(120, "Append open session: %s", fd->msg);
    if (jcr->session_opened) {
-      bnet_fsend(fd, NO_open);
+      fd->fsend(NO_open);
       return false;
    }
 
    jcr->session_opened = true;
 
    /* Send "Ticket" to File Daemon */
-   bnet_fsend(fd, OK_open, jcr->VolSessionId);
+   fd->fsend(OK_open, jcr->VolSessionId);
    Dmsg1(110, ">filed: %s", fd->msg);
 
    return true;
@@ -235,14 +251,14 @@ static bool append_close_session(JCR *jcr)
 
    Dmsg1(120, "<filed: %s", fd->msg);
    if (!jcr->session_opened) {
-      bnet_fsend(fd, NOT_opened);
+      fd->fsend(NOT_opened);
       return false;
    }
    /* Send final statistics to File daemon */
-   bnet_fsend(fd, OK_close, jcr->JobStatus);
+   fd->fsend(OK_close, jcr->JobStatus);
    Dmsg1(120, ">filed: %s", fd->msg);
 
-   bnet_sig(fd, BNET_EOD);            /* send EOD to File daemon */
+   fd->signal(BNET_EOD);              /* send EOD to File daemon */
 
    jcr->session_opened = false;
    return true;
@@ -263,7 +279,7 @@ static bool read_data_cmd(JCR *jcr)
       Dmsg1(120, "<bfiled: %s", fd->msg);
       return do_read_data(jcr);
    } else {
-      bnet_fsend(fd, NOT_opened);
+      fd->fsend(NOT_opened);
       return false;
    }
 }
@@ -280,19 +296,19 @@ static bool read_open_session(JCR *jcr)
 
    Dmsg1(120, "%s\n", fd->msg);
    if (jcr->session_opened) {
-      bnet_fsend(fd, NO_open);
+      fd->fsend(NO_open);
       return false;
    }
 
-   if (sscanf(fd->msg, read_open, jcr->dcr->VolumeName, &jcr->read_VolSessionId,
+   if (sscanf(fd->msg, read_open, jcr->read_dcr->VolumeName, &jcr->read_VolSessionId,
          &jcr->read_VolSessionTime, &jcr->read_StartFile, &jcr->read_EndFile,
          &jcr->read_StartBlock, &jcr->read_EndBlock) == 7) {
       if (jcr->session_opened) {
-         bnet_fsend(fd, NOT_opened);
+         fd->fsend(NOT_opened);
          return false;
       }
       Dmsg4(100, "read_open_session got: JobId=%d Vol=%s VolSessId=%ld VolSessT=%ld\n",
-         jcr->JobId, jcr->dcr->VolumeName, jcr->read_VolSessionId,
+         jcr->JobId, jcr->read_dcr->VolumeName, jcr->read_VolSessionId,
          jcr->read_VolSessionTime);
       Dmsg4(100, "  StartF=%ld EndF=%ld StartB=%ld EndB=%ld\n",
          jcr->read_StartFile, jcr->read_EndFile, jcr->read_StartBlock,
@@ -300,18 +316,25 @@ static bool read_open_session(JCR *jcr)
    }
 
    jcr->session_opened = true;
-   jcr->JobType = JT_RESTORE;
+   jcr->set_JobType(JT_RESTORE);
 
    /* Send "Ticket" to File Daemon */
-   bnet_fsend(fd, OK_open, jcr->VolSessionId);
+   fd->fsend(OK_open, jcr->VolSessionId);
    Dmsg1(110, ">filed: %s", fd->msg);
 
    return true;
 }
 
-bool bootstrap_cmd(JCR *jcr)
+static bool bootstrap_cmd(JCR *jcr)
+{
+   return get_bootstrap_file(jcr, jcr->file_bsock);
+}
+
+static pthread_mutex_t bsr_mutex = PTHREAD_MUTEX_INITIALIZER;
+static uint32_t bsr_uniq = 0;
+
+bool get_bootstrap_file(JCR *jcr, BSOCK *sock)
 {
-   BSOCK *fd = jcr->file_bsock;
    POOLMEM *fname = get_pool_memory(PM_FNAME);
    FILE *bs;
    bool ok = false;
@@ -320,27 +343,33 @@ bool bootstrap_cmd(JCR *jcr)
       unlink(jcr->RestoreBootstrap);
       free_pool_memory(jcr->RestoreBootstrap);
    }
-   Mmsg(fname, "%s/%s.%s.bootstrap", me->working_directory, me->hdr.name,
-      jcr->Job);
+   P(bsr_mutex);
+   bsr_uniq++;
+   Mmsg(fname, "%s/%s.%s.%d.bootstrap", me->working_directory, me->hdr.name,
+      jcr->Job, bsr_uniq);
+   V(bsr_mutex);
    Dmsg1(400, "bootstrap=%s\n", fname);
    jcr->RestoreBootstrap = fname;
-   bs = fopen(fname, "a+");           /* create file */
+   bs = fopen(fname, "a+b");           /* create file */
    if (!bs) {
+      berrno be;
       Jmsg(jcr, M_FATAL, 0, _("Could not create bootstrap file %s: ERR=%s\n"),
-         jcr->RestoreBootstrap, strerror(errno));
+         jcr->RestoreBootstrap, be.bstrerror());
       goto bail_out;
    }
-   while (bnet_recv(fd) >= 0) {
-       Dmsg1(400, "stored<filed: bootstrap file %s\n", fd->msg);
-       fputs(fd->msg, bs);
+   Dmsg0(10, "=== Bootstrap file ===\n");
+   while (sock->recv() >= 0) {
+       Dmsg1(10, "%s", sock->msg);
+       fputs(sock->msg, bs);
    }
    fclose(bs);
+   Dmsg0(10, "=== end bootstrap file ===\n");
    jcr->bsr = parse_bsr(jcr, jcr->RestoreBootstrap);
    if (!jcr->bsr) {
       Jmsg(jcr, M_FATAL, 0, _("Error parsing bootstrap file.\n"));
       goto bail_out;
    }
-   if (debug_level > 20) {
+   if (debug_level >= 10) {
       dump_bsr(jcr->bsr, true);
    }
    ok = true;
@@ -350,10 +379,10 @@ bail_out:
    free_pool_memory(jcr->RestoreBootstrap);
    jcr->RestoreBootstrap = NULL;
    if (!ok) {
-      bnet_fsend(fd, ERROR_bootstrap);
+      sock->fsend(ERROR_bootstrap);
       return false;
    }
-   return bnet_fsend(fd, OK_bootstrap);
+   return sock->fsend(OK_bootstrap);
 }
 
 
@@ -367,14 +396,14 @@ static bool read_close_session(JCR *jcr)
 
    Dmsg1(120, "Read close session: %s\n", fd->msg);
    if (!jcr->session_opened) {
-      bnet_fsend(fd, NOT_opened);
+      fd->fsend(NOT_opened);
       return false;
    }
-   /* Send final statistics to File daemon */
-   bnet_fsend(fd, OK_close);
+   /* Send final close msg to File daemon */
+   fd->fsend(OK_close, jcr->JobStatus);
    Dmsg1(160, ">filed: %s\n", fd->msg);
 
-   bnet_sig(fd, BNET_EOD);          /* send EOD to File daemon */
+   fd->signal(BNET_EOD);            /* send EOD to File daemon */
 
    jcr->session_opened = false;
    return true;