src/filed/job.c
static int storage_cmd(JCR *jcr)
{
   int stored_port = 0;            /* storage daemon port */
   int enable_ssl;                 /* enable ssl to sd */
   POOL_MEM sd_auth_key(PM_MESSAGE);
   BSOCK *dir = jcr->dir_bsock;
   BSOCK *sd;

   Dmsg1(100, "StorageCmd: %s", dir->msg);
   sd_auth_key.check_size(dir->msglen);
   if (sscanf(dir->msg, storaddr, &jcr->stored_addr, &stored_port,
              &enable_ssl, sd_auth_key.c_str()) == 4) {
      Dmsg1(100, "Set auth key %s\n", sd_auth_key.c_str());
      set_storage_auth_key(jcr, sd_auth_key.c_str());
  } else if (sscanf(dir->msg, storaddr_v1, &jcr->stored_addr,
                 &stored_port, &enable_ssl) != 3) {
      pm_strcpy(jcr->errmsg, dir->msg);
      Jmsg(jcr, M_FATAL, 0, _("Bad storage command: %s"), jcr->errmsg);
      Pmsg1(010, "Bad storage command: %s", jcr->errmsg);
      goto bail_out;
   }


   /* TODO: see if we put limit on restore and backup... */
   if (!jcr->max_bandwidth) {
      if (jcr->director->max_bandwidth_per_job) {
         jcr->max_bandwidth = jcr->director->max_bandwidth_per_job;

      } else if (me->max_bandwidth_per_job) {
         jcr->max_bandwidth = me->max_bandwidth_per_job;
      }
   }

   if (stored_port != 0) { /* We are doing the connecting */
      Dmsg3(110, "Connect to storage: %s:%d ssl=%d\n", jcr->stored_addr, stored_port,
            enable_ssl);
      jcr->sd_calls_client = false;
      sd = new_bsock();
      /* Open command communications with Storage daemon */
      /* Try to connect for 1 hour at 10 second intervals */
      sd->set_source_address(me->FDsrc_addr);
      if (!sd->connect(jcr, 10, (int)me->SDConnectTimeout, me->heartbeat_interval,
                _("Storage daemon"), jcr->stored_addr, NULL, stored_port, 1)) {
         /* destroy() OK because sd is local */
         sd->destroy();
         Jmsg2(jcr, M_FATAL, 0, _("Failed to connect to Storage daemon: %s:%d\n"),
             jcr->stored_addr, stored_port);
         Dmsg2(100, "Failed to connect to Storage daemon: %s:%d\n",
             jcr->stored_addr, stored_port);
         goto bail_out;
      }

      Dmsg0(110, "Connection OK to SD.\n");
      jcr->store_bsock = sd;
   } else {                      /* The storage daemon called us */
      struct timeval tv;
      struct timezone tz;
      struct timespec timeout;
      int errstat;

      free_bsock(jcr->store_bsock);
      jcr->sd_calls_client = true;

      /*
       * Wait for the Storage daemon to contact us to start the Job,
       *  when he does, we will be released, unless the 30 minutes
       *  expires.
       */
      gettimeofday(&tv, &tz);
      timeout.tv_nsec = tv.tv_usec * 1000;
      timeout.tv_sec = tv.tv_sec + 30 * 60;  /* wait 30 minutes */
      P(mutex);
      while (jcr->sd_calls_client_bsock == NULL && !jcr->is_job_canceled()) {
         errstat = pthread_cond_timedwait(&jcr->job_start_wait, &mutex, &timeout);
         if (errstat == ETIMEDOUT || errstat == EINVAL || errstat == EPERM) {
            break;
         }
         Dmsg1(800, "=== Auth cond errstat=%d\n", errstat);
      }
      V(mutex);
      Dmsg2(800, "Auth fail or cancel for jid=%d %p\n", jcr->JobId, jcr);

      /* We should already have a storage connection! */
      if (jcr->sd_calls_client_bsock == NULL) {
         Pmsg0(000, "Failed connect from Storage daemon. SD bsock=NULL.\n");
         Pmsg1(000, "Storagecmd: %s", dir->msg);
         Jmsg0(jcr, M_FATAL, 0, _("Failed connect from Storage daemon. SD bsock=NULL.\n"));
         goto bail_out;
      }
      if (jcr->is_job_canceled()) {
         goto bail_out;
      }
      /* Assign the new socket to the main one */
      jcr->lock_auth();
      jcr->store_bsock = jcr->sd_calls_client_bsock;
      jcr->sd_calls_client_bsock = NULL;
      jcr->unlock_auth();
   }
   jcr->store_bsock->set_bwlimit(jcr->max_bandwidth);

   if (!send_hello_sd(jcr, jcr->Job)) {
      goto bail_out;
   }

   if (!authenticate_storagedaemon(jcr)) {
      goto bail_out;
   }
   memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
   Dmsg0(110, "Authenticated with SD.\n");

   /* Send OK to Director */
   return dir->fsend(OKstore);

bail_out:
   dir->fsend(BADcmd, "storage");
   return 0;
}
/**
 * Get address of storage daemon from Director
 *
 */
Get address of storage daemon from Director
Get address of storage daemon from Director
static int storage_cmd(JCR *jcr)
{
   int stored_port = 0;            
   int enable_ssl;                 
   POOL_MEM sd_auth_key(PM_MESSAGE);
   BSOCK *dir = jcr->dir_bsock;
   BSOCK *sd;
   Dmsg1(100, "StorageCmd: %s", dir->msg);
   sd_auth_key.check_size(dir->msglen);
   if (sscanf(dir->msg, storaddr, &jcr->stored_addr, &stored_port,
              &enable_ssl, sd_auth_key.c_str()) == 4) {
      Dmsg1(100, "Set auth key %s\n", sd_auth_key.c_str());
      set_storage_auth_key(jcr, sd_auth_key.c_str());
  } else if (sscanf(dir->msg, storaddr_v1, &jcr->stored_addr,
                 &stored_port, &enable_ssl) != 3) {
      pm_strcpy(jcr->errmsg, dir->msg);
      Jmsg(jcr, M_FATAL, 0, _("Bad storage command: %s"), jcr->errmsg);
      Pmsg1(010, "Bad storage command: %s", jcr->errmsg);
      goto bail_out;
   }
   if (!jcr->max_bandwidth) {
      if (jcr->director->max_bandwidth_per_job) {
         jcr->max_bandwidth = jcr->director->max_bandwidth_per_job;
      } else if (me->max_bandwidth_per_job) {
         jcr->max_bandwidth = me->max_bandwidth_per_job;
      }
   }
   if (stored_port != 0) { 
      Dmsg3(110, "Connect to storage: %s:%d ssl=%d\n", jcr->stored_addr, stored_port,
            enable_ssl);
      jcr->sd_calls_client = false;
      sd = new_bsock();
      sd->set_source_address(me->FDsrc_addr);
      if (!sd->connect(jcr, 10, (int)me->SDConnectTimeout, me->heartbeat_interval,
                _("Storage daemon"), jcr->stored_addr, NULL, stored_port, 1)) {
         sd->destroy();
         Jmsg2(jcr, M_FATAL, 0, _("Failed to connect to Storage daemon: %s:%d\n"),
             jcr->stored_addr, stored_port);
         Dmsg2(100, "Failed to connect to Storage daemon: %s:%d\n",
             jcr->stored_addr, stored_port);
         goto bail_out;
      }
      Dmsg0(110, "Connection OK to SD.\n");
      jcr->store_bsock = sd;
   } else {                      
      struct timeval tv;
      struct timezone tz;
      struct timespec timeout;
      int errstat;
      free_bsock(jcr->store_bsock);
      jcr->sd_calls_client = true;
      gettimeofday(&tv, &tz);
      timeout.tv_nsec = tv.tv_usec * 1000;
      timeout.tv_sec = tv.tv_sec + 30 * 60;  
      P(mutex);
      while (jcr->sd_calls_client_bsock == NULL && !jcr->is_job_canceled()) {
         errstat = pthread_cond_timedwait(&jcr->job_start_wait, &mutex, &timeout);
         if (errstat == ETIMEDOUT || errstat == EINVAL || errstat == EPERM) {
            break;
         }
         Dmsg1(800, "=== Auth cond errstat=%d\n", errstat);
      }
      V(mutex);
      Dmsg2(800, "Auth fail or cancel for jid=%d %p\n", jcr->JobId, jcr);
      if (jcr->sd_calls_client_bsock == NULL) {
         Pmsg0(000, "Failed connect from Storage daemon. SD bsock=NULL.\n");
         Pmsg1(000, "Storagecmd: %s", dir->msg);
         Jmsg0(jcr, M_FATAL, 0, _("Failed connect from Storage daemon. SD bsock=NULL.\n"));
         goto bail_out;
      }
      if (jcr->is_job_canceled()) {
         goto bail_out;
      }
      jcr->lock_auth();
      jcr->store_bsock = jcr->sd_calls_client_bsock;
      jcr->sd_calls_client_bsock = NULL;
      jcr->unlock_auth();
   }
   jcr->store_bsock->set_bwlimit(jcr->max_bandwidth);
   if (!send_hello_sd(jcr, jcr->Job)) {
      goto bail_out;
   }
   if (!authenticate_storagedaemon(jcr)) {
      goto bail_out;
   }
   memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
   Dmsg0(110, "Authenticated with SD.\n");
   return dir->fsend(OKstore);
bail_out:
   dir->fsend(BADcmd, "storage");
   return 0;
}
static int storage_cmd ( JCR * jcr ) { int stored_port = 0 ; int enable_ssl ; POOL_MEM sd_auth_key ( PM_MESSAGE ) ; BSOCK * dir = jcr -> dir_bsock ; BSOCK * sd ; Dmsg1 ( 100 , " " , dir -> msg ) ; sd_auth_key . check_size ( dir -> msglen ) ; if ( sscanf ( dir -> msg , storaddr , & jcr -> stored_addr , & stored_port , & enable_ssl , sd_auth_key . c_str ( ) ) == 4 ) { Dmsg1 ( 100 , " \n " , sd_auth_key . c_str ( ) ) ; set_storage_auth_key ( jcr , sd_auth_key . c_str ( ) ) ; } else if ( sscanf ( dir -> msg , storaddr_v1 , & jcr -> stored_addr , & stored_port , & enable_ssl ) != 3 ) { pm_strcpy ( jcr -> errmsg , dir -> msg ) ; Jmsg ( jcr , M_FATAL , 0 , _ ( " " ) , jcr -> errmsg ) ; Pmsg1 ( 010 , " " , jcr -> errmsg ) ; goto bail_out ; } if ( ! jcr -> max_bandwidth ) { if ( jcr -> director -> max_bandwidth_per_job ) { jcr -> max_bandwidth = jcr -> director -> max_bandwidth_per_job ; } else if ( me -> max_bandwidth_per_job ) { jcr -> max_bandwidth = me -> max_bandwidth_per_job ; } } if ( stored_port != 0 ) { Dmsg3 ( 110 , " \n " , jcr -> stored_addr , stored_port , enable_ssl ) ; jcr -> sd_calls_client = false ; sd = new_bsock ( ) ; sd -> set_source_address ( me -> FDsrc_addr ) ; if ( ! sd -> connect ( jcr , 10 , ( int ) me -> SDConnectTimeout , me -> heartbeat_interval , _ ( " " ) , jcr -> stored_addr , NULL , stored_port , 1 ) ) { sd -> destroy ( ) ; Jmsg2 ( jcr , M_FATAL , 0 , _ ( " \n " ) , jcr -> stored_addr , stored_port ) ; Dmsg2 ( 100 , " \n " , jcr -> stored_addr , stored_port ) ; goto bail_out ; } Dmsg0 ( 110 , " \n " ) ; jcr -> store_bsock = sd ; } else { struct timeval tv ; struct timezone tz ; struct timespec timeout ; int errstat ; free_bsock ( jcr -> store_bsock ) ; jcr -> sd_calls_client = true ; gettimeofday ( & tv , & tz ) ; timeout . tv_nsec = tv . tv_usec * 1000 ; timeout . tv_sec = tv . tv_sec + 30 * 60 ; P ( mutex ) ; while ( jcr -> sd_calls_client_bsock == NULL && ! jcr -> is_job_canceled ( ) ) { errstat = pthread_cond_timedwait ( & jcr -> job_start_wait , & mutex , & timeout ) ; if ( errstat == ETIMEDOUT || errstat == EINVAL || errstat == EPERM ) { break ; } Dmsg1 ( 800 , " \n " , errstat ) ; } V ( mutex ) ; Dmsg2 ( 800 , " \n " , jcr -> JobId , jcr ) ; if ( jcr -> sd_calls_client_bsock == NULL ) { Pmsg0 ( 000 , " \n " ) ; Pmsg1 ( 000 , " " , dir -> msg ) ; Jmsg0 ( jcr , M_FATAL , 0 , _ ( " \n " ) ) ; goto bail_out ; } if ( jcr -> is_job_canceled ( ) ) { goto bail_out ; } jcr -> lock_auth ( ) ; jcr -> store_bsock = jcr -> sd_calls_client_bsock ; jcr -> sd_calls_client_bsock = NULL ; jcr -> unlock_auth ( ) ; } jcr -> store_bsock -> set_bwlimit ( jcr -> max_bandwidth ) ; if ( ! send_hello_sd ( jcr , jcr -> Job ) ) { goto bail_out ; } if ( ! authenticate_storagedaemon ( jcr ) ) { goto bail_out ; } memset ( jcr -> sd_auth_key , 0 , strlen ( jcr -> sd_auth_key ) ) ; Dmsg0 ( 110 , " \n " ) ; return dir -> fsend ( OKstore ) ; bail_out : dir -> fsend ( BADcmd , " " ) ; return 0 ; }
Get address of storage daemon from Director
Get address of storage daemon from Director
/* storage daemon port */ /* enable ssl to sd */ /* TODO: see if we put limit on restore and backup... */ /* We are doing the connecting */ /* Open command communications with Storage daemon */ /* Try to connect for 1 hour at 10 second intervals */ /* destroy() OK because sd is local */ /* The storage daemon called us */ /*
       * Wait for the Storage daemon to contact us to start the Job,
       *  when he does, we will be released, unless the 30 minutes
       *  expires.
       */ /* wait 30 minutes */ /* We should already have a storage connection! */ /* Assign the new socket to the main one */ /* Send OK to Director */