src/dird/fd_cmds.c
int connect_to_file_daemon(JCR *jcr, int retry_interval, int max_retry_time,
                           int verbose)
{
   BSOCK   *fd = jcr->file_bsock;
   char ed1[30];
   utime_t heart_beat;

   if (!jcr->client) {
      Jmsg(jcr, M_FATAL, 0, _("File daemon not defined for current Job\n"));
      Dmsg0(10, "No Client defined for the job.\n");
      return 0;
   }

   if (jcr->client->heartbeat_interval) {
      heart_beat = jcr->client->heartbeat_interval;
   } else {
      heart_beat = director->heartbeat_interval;
   }

   if (!is_bsock_open(jcr->file_bsock)) {
      char name[MAX_NAME_LENGTH + 100];
      POOL_MEM buf;

      if (!fd) {
         fd = jcr->file_bsock = new_bsock();
      }
      bstrncpy(name, _("Client: "), sizeof(name));
      bstrncat(name, jcr->client->name(), sizeof(name));

      fd->set_source_address(director->DIRsrc_addr);
      if (!fd->connect(jcr,retry_interval,
                       max_retry_time,
                       heart_beat, name,
                       jcr->client->address(buf.addr()),
                       NULL,
                       jcr->client->FDport,
                       verbose)) {
         fd->close();
         jcr->setJobStatus(JS_ErrorTerminated);
         return 0;
      }
      Dmsg0(10, "Opened connection with File daemon\n");
   }
   fd->res = (RES *)jcr->client;      /* save resource in BSOCK */
   jcr->setJobStatus(JS_Running);

   if (!authenticate_file_daemon(jcr)) {
      jcr->setJobStatus(JS_ErrorTerminated);
      Dmsg0(10, "Authentication error with FD.\n");
      return 0;
   }

   /*
    * Now send JobId and authorization key
    */
   if (jcr->sd_auth_key == NULL) {
      jcr->sd_auth_key = bstrdup("dummy");
   }
   fd->fsend(jobcmd, edit_int64(jcr->JobId, ed1), jcr->Job, jcr->VolSessionId,
             jcr->VolSessionTime, jcr->sd_auth_key);
   if (!jcr->keep_sd_auth_key && strcmp(jcr->sd_auth_key, "dummy")) {
      memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
   }
   Dmsg1(100, ">filed: %s", fd->msg);
   if (bget_dirmsg(fd) > 0) {
       Dmsg1(110, "<filed: %s", fd->msg);
       if (strncmp(fd->msg, OKjob, strlen(OKjob)) != 0) {
          Jmsg(jcr, M_FATAL, 0, _("File daemon \"%s\" rejected Job command: %s\n"),
             jcr->client->hdr.name, fd->msg);
          jcr->setJobStatus(JS_ErrorTerminated);
          return 0;
       } else if (jcr->db) {
          CLIENT_DBR cr;
          memset(&cr, 0, sizeof(cr));
          bstrncpy(cr.Name, jcr->client->hdr.name, sizeof(cr.Name));
          cr.AutoPrune = jcr->client->AutoPrune;
          cr.FileRetention = jcr->client->FileRetention;
          cr.JobRetention = jcr->client->JobRetention;
          bstrncpy(cr.Uname, fd->msg+strlen(OKjob)+1, sizeof(cr.Uname));
          if (!db_update_client_record(jcr, jcr->db, &cr)) {
             Jmsg(jcr, M_WARNING, 0, _("Error updating Client record. ERR=%s\n"),
                db_strerror(jcr->db));
          }
       }
   } else {
      Jmsg(jcr, M_FATAL, 0, _("FD gave bad response to JobId command: %s\n"),
         fd->bstrerror());
      jcr->setJobStatus(JS_ErrorTerminated);
      return 0;
   }
   return 1;
}
/*
 * Open connection with File daemon.
 * Try connecting every retry_interval (default 10 sec), and
 *   give up after max_retry_time (default 30 mins).
 */
Open connection with File daemon.
Try connecting every retry_interval (default 10 sec), and
give up after max_retry_time (default 30 mins).
Open connection with File daemon . Try connecting every retry_interval ( default 10 sec ) and give up after max_retry_time ( default 30 mins ) .
int connect_to_file_daemon(JCR *jcr, int retry_interval, int max_retry_time,
                           int verbose)
{
   BSOCK   *fd = jcr->file_bsock;
   char ed1[30];
   utime_t heart_beat;
   if (!jcr->client) {
      Jmsg(jcr, M_FATAL, 0, _("File daemon not defined for current Job\n"));
      Dmsg0(10, "No Client defined for the job.\n");
      return 0;
   }
   if (jcr->client->heartbeat_interval) {
      heart_beat = jcr->client->heartbeat_interval;
   } else {
      heart_beat = director->heartbeat_interval;
   }
   if (!is_bsock_open(jcr->file_bsock)) {
      char name[MAX_NAME_LENGTH + 100];
      POOL_MEM buf;
      if (!fd) {
         fd = jcr->file_bsock = new_bsock();
      }
      bstrncpy(name, _("Client: "), sizeof(name));
      bstrncat(name, jcr->client->name(), sizeof(name));
      fd->set_source_address(director->DIRsrc_addr);
      if (!fd->connect(jcr,retry_interval,
                       max_retry_time,
                       heart_beat, name,
                       jcr->client->address(buf.addr()),
                       NULL,
                       jcr->client->FDport,
                       verbose)) {
         fd->close();
         jcr->setJobStatus(JS_ErrorTerminated);
         return 0;
      }
      Dmsg0(10, "Opened connection with File daemon\n");
   }
   fd->res = (RES *)jcr->client;      
   jcr->setJobStatus(JS_Running);
   if (!authenticate_file_daemon(jcr)) {
      jcr->setJobStatus(JS_ErrorTerminated);
      Dmsg0(10, "Authentication error with FD.\n");
      return 0;
   }
   if (jcr->sd_auth_key == NULL) {
      jcr->sd_auth_key = bstrdup("dummy");
   }
   fd->fsend(jobcmd, edit_int64(jcr->JobId, ed1), jcr->Job, jcr->VolSessionId,
             jcr->VolSessionTime, jcr->sd_auth_key);
   if (!jcr->keep_sd_auth_key && strcmp(jcr->sd_auth_key, "dummy")) {
      memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
   }
   Dmsg1(100, ">filed: %s", fd->msg);
   if (bget_dirmsg(fd) > 0) {
       Dmsg1(110, "<filed: %s", fd->msg);
       if (strncmp(fd->msg, OKjob, strlen(OKjob)) != 0) {
          Jmsg(jcr, M_FATAL, 0, _("File daemon \"%s\" rejected Job command: %s\n"),
             jcr->client->hdr.name, fd->msg);
          jcr->setJobStatus(JS_ErrorTerminated);
          return 0;
       } else if (jcr->db) {
          CLIENT_DBR cr;
          memset(&cr, 0, sizeof(cr));
          bstrncpy(cr.Name, jcr->client->hdr.name, sizeof(cr.Name));
          cr.AutoPrune = jcr->client->AutoPrune;
          cr.FileRetention = jcr->client->FileRetention;
          cr.JobRetention = jcr->client->JobRetention;
          bstrncpy(cr.Uname, fd->msg+strlen(OKjob)+1, sizeof(cr.Uname));
          if (!db_update_client_record(jcr, jcr->db, &cr)) {
             Jmsg(jcr, M_WARNING, 0, _("Error updating Client record. ERR=%s\n"),
                db_strerror(jcr->db));
          }
       }
   } else {
      Jmsg(jcr, M_FATAL, 0, _("FD gave bad response to JobId command: %s\n"),
         fd->bstrerror());
      jcr->setJobStatus(JS_ErrorTerminated);
      return 0;
   }
   return 1;
}
int connect_to_file_daemon ( JCR * jcr , int retry_interval , int max_retry_time , int verbose ) { BSOCK * fd = jcr -> file_bsock ; char ed1 [ 30 ] ; utime_t heart_beat ; if ( ! jcr -> client ) { Jmsg ( jcr , M_FATAL , 0 , _ ( " \n " ) ) ; Dmsg0 ( 10 , " \n " ) ; return 0 ; } if ( jcr -> client -> heartbeat_interval ) { heart_beat = jcr -> client -> heartbeat_interval ; } else { heart_beat = director -> heartbeat_interval ; } if ( ! is_bsock_open ( jcr -> file_bsock ) ) { char name [ MAX_NAME_LENGTH + 100 ] ; POOL_MEM buf ; if ( ! fd ) { fd = jcr -> file_bsock = new_bsock ( ) ; } bstrncpy ( name , _ ( " " ) , sizeof ( name ) ) ; bstrncat ( name , jcr -> client -> name ( ) , sizeof ( name ) ) ; fd -> set_source_address ( director -> DIRsrc_addr ) ; if ( ! fd -> connect ( jcr , retry_interval , max_retry_time , heart_beat , name , jcr -> client -> address ( buf . addr ( ) ) , NULL , jcr -> client -> FDport , verbose ) ) { fd -> close ( ) ; jcr -> setJobStatus ( JS_ErrorTerminated ) ; return 0 ; } Dmsg0 ( 10 , " \n " ) ; } fd -> res = ( RES * ) jcr -> client ; jcr -> setJobStatus ( JS_Running ) ; if ( ! authenticate_file_daemon ( jcr ) ) { jcr -> setJobStatus ( JS_ErrorTerminated ) ; Dmsg0 ( 10 , " \n " ) ; return 0 ; } if ( jcr -> sd_auth_key == NULL ) { jcr -> sd_auth_key = bstrdup ( " " ) ; } fd -> fsend ( jobcmd , edit_int64 ( jcr -> JobId , ed1 ) , jcr -> Job , jcr -> VolSessionId , jcr -> VolSessionTime , jcr -> sd_auth_key ) ; if ( ! jcr -> keep_sd_auth_key && strcmp ( jcr -> sd_auth_key , " " ) ) { memset ( jcr -> sd_auth_key , 0 , strlen ( jcr -> sd_auth_key ) ) ; } Dmsg1 ( 100 , " " , fd -> msg ) ; if ( bget_dirmsg ( fd ) > 0 ) { Dmsg1 ( 110 , " " , fd -> msg ) ; if ( strncmp ( fd -> msg , OKjob , strlen ( OKjob ) ) != 0 ) { Jmsg ( jcr , M_FATAL , 0 , _ ( " \" \" \n " ) , jcr -> client -> hdr . name , fd -> msg ) ; jcr -> setJobStatus ( JS_ErrorTerminated ) ; return 0 ; } else if ( jcr -> db ) { CLIENT_DBR cr ; memset ( & cr , 0 , sizeof ( cr ) ) ; bstrncpy ( cr . Name , jcr -> client -> hdr . name , sizeof ( cr . Name ) ) ; cr . AutoPrune = jcr -> client -> AutoPrune ; cr . FileRetention = jcr -> client -> FileRetention ; cr . JobRetention = jcr -> client -> JobRetention ; bstrncpy ( cr . Uname , fd -> msg + strlen ( OKjob ) + 1 , sizeof ( cr . Uname ) ) ; if ( ! db_update_client_record ( jcr , jcr -> db , & cr ) ) { Jmsg ( jcr , M_WARNING , 0 , _ ( " \n " ) , db_strerror ( jcr -> db ) ) ; } } } else { Jmsg ( jcr , M_FATAL , 0 , _ ( " \n " ) , fd -> bstrerror ( ) ) ; jcr -> setJobStatus ( JS_ErrorTerminated ) ; return 0 ; } return 1 ; }
Open connection with File daemon.
Open connection with File daemon .
/* save resource in BSOCK */ /*
    * Now send JobId and authorization key
    */