src/stored/dircmd.c
void *handle_connection_request(void *arg)
{
   BSOCK *bs = (BSOCK *)arg;
   JCR *jcr;
   int i;
   bool found, quit;
   int bnet_stat = 0;
   char tbuf[100];

   if (bs->recv() <= 0) {
      Qmsg1(NULL, M_ERROR, 0, _("Connection request from %s failed.\n"), bs->who());
      bmicrosleep(5, 0);   /* make user wait 5 seconds */
      bs->destroy();
      return NULL;
   }

   /* Check for client connection */
   if (is_client_connection(bs)) {
      handle_client_connection(bs);
      return NULL;
   }

   /*
    * This is a connection from the Director, so setup a JCR
    */
   Dmsg1(050, "Got a DIR connection at %s\n", bstrftimes(tbuf, sizeof(tbuf),
         (utime_t)time(NULL)));
   jcr = new_jcr(sizeof(JCR), stored_free_jcr); /* create Job Control Record */
   jcr->dir_bsock = bs;               /* save Director bsock */
   jcr->dir_bsock->set_jcr(jcr);
   jcr->dcrs = New(alist(10, not_owned_by_alist));
   create_jobmedia_queue(jcr);
   /* Initialize FD start condition variable */
   int errstat = pthread_cond_init(&jcr->job_start_wait, NULL);
   if (errstat != 0) {
      berrno be;
      Qmsg1(jcr, M_FATAL, 0, _("Unable to init job cond variable: ERR=%s\n"), be.bstrerror(errstat));
      goto bail_out;
   }

   Dmsg0(1000, "stored in start_job\n");

   /*
    * Validate then authenticate the Director
    */
   if (!validate_dir_hello(jcr)) {
      goto bail_out;
   }
   if (!authenticate_director(jcr)) {
      Qmsg(jcr, M_FATAL, 0, _("[SF0100] Unable to authenticate Director\n"));
      goto bail_out;
   }
   Dmsg0(90, "Message channel init completed.\n");

   dequeue_messages(jcr);     /* dequeue any daemon messages */

   jcr->set_killable(true);    /* allow dir to kill/cancel job */

   for (quit=false; !quit;) {
      /* Read command */
      if ((bnet_stat = bs->recv()) <= 0) {
         break;               /* connection terminated */
      }
      Dmsg1(199, "<dird: %s", bs->msg);
      /* Ensure that device initialization is complete */
      while (!init_done) {
         bmicrosleep(1, 0);
      }
      found = false;
      for (i=0; cmds[i].cmd; i++) {
        if (strncmp(cmds[i].cmd, bs->msg, strlen(cmds[i].cmd)) == 0) {
           if ((!cmds[i].monitoraccess) && (jcr->director->monitor)) {
              Dmsg1(100, "Command \"%s\" is invalid.\n", cmds[i].cmd);
              bs->fsend(invalid_cmd);
              bs->signal(BNET_EOD);
              break;
           }
           Dmsg1(200, "Do command: %s\n", cmds[i].cmd);
           if (!cmds[i].func(jcr)) { /* do command */
              quit = true; /* error, get out */
              Dmsg1(190, "Command %s requests quit\n", cmds[i].cmd);
           }
           found = true;             /* indicate command found */
           break;
        }
      }
      if (!found) {                   /* command not found */
        POOL_MEM err_msg;
        Mmsg(err_msg, "%s %s\n", derrmsg, bs->msg);
        bs->fsend(err_msg.c_str());
        break;
      }
   }
bail_out:
   generate_daemon_event(jcr, "JobEnd");
   generate_plugin_event(jcr, bsdEventJobEnd);
   flush_jobmedia_queue(jcr);
   dequeue_messages(jcr);             /* send any queued messages */
   dequeue_daemon_messages(jcr);
   bs->signal(BNET_TERMINATE);
   bs->destroy();
   jcr->dir_bsock = NULL;             /* just freed bsock */
   free_plugins(jcr);                 /* release instantiated plugins */
   free_jcr(jcr);
   return NULL;
}
/*
 * Connection request. We accept connections either from the
 *  Director or a Client (File daemon).
 *
 * Note, we are running as a separate thread of the Storage daemon.
 *  and it is because a Director has made a connection with
 *  us on the "Message" channel.
 *
 * Basic tasks done here:
 *  - Create a JCR record
 *  - If it was from the FD, call handle_filed_connection()
 *  - Authenticate the Director
 *  - We wait for a command
 *  - We execute the command
 *  - We continue or exit depending on the return status
 */
Connection request. We accept connections either from the
Director or a Client (File daemon).

Note, we are running as a separate thread of the Storage daemon.
and it is because a Director has made a connection with
us on the "Message" channel.


Connection request . We accept connections either from the Director or a Client ( File daemon ) . Note we are running as a separate thread of the Storage daemon . and it is because a Director has made a connection with us on the " Message " channel .
void *handle_connection_request(void *arg)
{
   BSOCK *bs = (BSOCK *)arg;
   JCR *jcr;
   int i;
   bool found, quit;
   int bnet_stat = 0;
   char tbuf[100];
   if (bs->recv() <= 0) {
      Qmsg1(NULL, M_ERROR, 0, _("Connection request from %s failed.\n"), bs->who());
      bmicrosleep(5, 0);   
      bs->destroy();
      return NULL;
   }
   if (is_client_connection(bs)) {
      handle_client_connection(bs);
      return NULL;
   }
   Dmsg1(050, "Got a DIR connection at %s\n", bstrftimes(tbuf, sizeof(tbuf),
         (utime_t)time(NULL)));
   jcr = new_jcr(sizeof(JCR), stored_free_jcr); 
   jcr->dir_bsock = bs;               
   jcr->dir_bsock->set_jcr(jcr);
   jcr->dcrs = New(alist(10, not_owned_by_alist));
   create_jobmedia_queue(jcr);
   int errstat = pthread_cond_init(&jcr->job_start_wait, NULL);
   if (errstat != 0) {
      berrno be;
      Qmsg1(jcr, M_FATAL, 0, _("Unable to init job cond variable: ERR=%s\n"), be.bstrerror(errstat));
      goto bail_out;
   }
   Dmsg0(1000, "stored in start_job\n");
   if (!validate_dir_hello(jcr)) {
      goto bail_out;
   }
   if (!authenticate_director(jcr)) {
      Qmsg(jcr, M_FATAL, 0, _("[SF0100] Unable to authenticate Director\n"));
      goto bail_out;
   }
   Dmsg0(90, "Message channel init completed.\n");
   dequeue_messages(jcr);     
   jcr->set_killable(true);    
   for (quit=false; !quit;) {
      if ((bnet_stat = bs->recv()) <= 0) {
         break;               
      }
      Dmsg1(199, "<dird: %s", bs->msg);
      while (!init_done) {
         bmicrosleep(1, 0);
      }
      found = false;
      for (i=0; cmds[i].cmd; i++) {
        if (strncmp(cmds[i].cmd, bs->msg, strlen(cmds[i].cmd)) == 0) {
           if ((!cmds[i].monitoraccess) && (jcr->director->monitor)) {
              Dmsg1(100, "Command \"%s\" is invalid.\n", cmds[i].cmd);
              bs->fsend(invalid_cmd);
              bs->signal(BNET_EOD);
              break;
           }
           Dmsg1(200, "Do command: %s\n", cmds[i].cmd);
           if (!cmds[i].func(jcr)) { 
              quit = true; 
              Dmsg1(190, "Command %s requests quit\n", cmds[i].cmd);
           }
           found = true;             
           break;
        }
      }
      if (!found) {                   
        POOL_MEM err_msg;
        Mmsg(err_msg, "%s %s\n", derrmsg, bs->msg);
        bs->fsend(err_msg.c_str());
        break;
      }
   }
bail_out:
   generate_daemon_event(jcr, "JobEnd");
   generate_plugin_event(jcr, bsdEventJobEnd);
   flush_jobmedia_queue(jcr);
   dequeue_messages(jcr);             
   dequeue_daemon_messages(jcr);
   bs->signal(BNET_TERMINATE);
   bs->destroy();
   jcr->dir_bsock = NULL;             
   free_plugins(jcr);                 
   free_jcr(jcr);
   return NULL;
}
void * handle_connection_request ( void * arg ) { BSOCK * bs = ( BSOCK * ) arg ; JCR * jcr ; int i ; bool found , quit ; int bnet_stat = 0 ; char tbuf [ 100 ] ; if ( bs -> recv ( ) <= 0 ) { Qmsg1 ( NULL , M_ERROR , 0 , _ ( " \n " ) , bs -> who ( ) ) ; bmicrosleep ( 5 , 0 ) ; bs -> destroy ( ) ; return NULL ; } if ( is_client_connection ( bs ) ) { handle_client_connection ( bs ) ; return NULL ; } Dmsg1 ( 050 , " \n " , bstrftimes ( tbuf , sizeof ( tbuf ) , ( utime_t ) time ( NULL ) ) ) ; jcr = new_jcr ( sizeof ( JCR ) , stored_free_jcr ) ; jcr -> dir_bsock = bs ; jcr -> dir_bsock -> set_jcr ( jcr ) ; jcr -> dcrs = New ( alist ( 10 , not_owned_by_alist ) ) ; create_jobmedia_queue ( jcr ) ; int errstat = pthread_cond_init ( & jcr -> job_start_wait , NULL ) ; if ( errstat != 0 ) { berrno be ; Qmsg1 ( jcr , M_FATAL , 0 , _ ( " \n " ) , be . bstrerror ( errstat ) ) ; goto bail_out ; } Dmsg0 ( 1000 , " \n " ) ; if ( ! validate_dir_hello ( jcr ) ) { goto bail_out ; } if ( ! authenticate_director ( jcr ) ) { Qmsg ( jcr , M_FATAL , 0 , _ ( " \n " ) ) ; goto bail_out ; } Dmsg0 ( 90 , " \n " ) ; dequeue_messages ( jcr ) ; jcr -> set_killable ( true ) ; for ( quit = false ; ! quit ; ) { if ( ( bnet_stat = bs -> recv ( ) ) <= 0 ) { break ; } Dmsg1 ( 199 , " " , bs -> msg ) ; while ( ! init_done ) { bmicrosleep ( 1 , 0 ) ; } found = false ; for ( i = 0 ; cmds [ i ] . cmd ; i ++ ) { if ( strncmp ( cmds [ i ] . cmd , bs -> msg , strlen ( cmds [ i ] . cmd ) ) == 0 ) { if ( ( ! cmds [ i ] . monitoraccess ) && ( jcr -> director -> monitor ) ) { Dmsg1 ( 100 , " \" \" \n " , cmds [ i ] . cmd ) ; bs -> fsend ( invalid_cmd ) ; bs -> signal ( BNET_EOD ) ; break ; } Dmsg1 ( 200 , " \n " , cmds [ i ] . cmd ) ; if ( ! cmds [ i ] . func ( jcr ) ) { quit = true ; Dmsg1 ( 190 , " \n " , cmds [ i ] . cmd ) ; } found = true ; break ; } } if ( ! found ) { POOL_MEM err_msg ; Mmsg ( err_msg , " \n " , derrmsg , bs -> msg ) ; bs -> fsend ( err_msg . c_str ( ) ) ; break ; } } bail_out : generate_daemon_event ( jcr , " " ) ; generate_plugin_event ( jcr , bsdEventJobEnd ) ; flush_jobmedia_queue ( jcr ) ; dequeue_messages ( jcr ) ; dequeue_daemon_messages ( jcr ) ; bs -> signal ( BNET_TERMINATE ) ; bs -> destroy ( ) ; jcr -> dir_bsock = NULL ; free_plugins ( jcr ) ; free_jcr ( jcr ) ; return NULL ; }
Connection request.
Connection request .
/* make user wait 5 seconds */ /* Check for client connection */ /*
    * This is a connection from the Director, so setup a JCR
    */ /* create Job Control Record */ /* save Director bsock */ /* Initialize FD start condition variable */ /*
    * Validate then authenticate the Director
    */ /* dequeue any daemon messages */ /* allow dir to kill/cancel job */ /* Read command */ /* connection terminated */ /* Ensure that device initialization is complete */ /* do command */ /* error, get out */ /* indicate command found */ /* command not found */ /* send any queued messages */ /* just freed bsock */ /* release instantiated plugins */