src/lib/message.c
void
init_msg(JCR *jcr, MSGS *msg, job_code_callback_t job_code_callback)
{
   DEST *d, *dnew, *temp_chain = NULL;
   int i;

   if (jcr == NULL && msg == NULL) {
      init_last_jobs_list();
      /* Create a daemon key then set invalid jcr */
      /* Maybe we should give the daemon a jcr??? */
      create_jcr_key();
      set_jcr_in_tsd(INVALID_JCR);
   }

   message_job_code_callback = job_code_callback;

#if !defined(HAVE_WIN32)
   /*
    * Make sure we have fd's 0, 1, 2 open
    *  If we don't do this one of our sockets may open
    *  there and if we then use stdout, it could
    *  send total garbage to our socket.
    *
    */
   int fd;
   fd = open("/dev/null", O_RDONLY, 0644);
   if (fd > 2) {
      close(fd);
   } else {
      for(i=1; fd + i <= 2; i++) {
         dup2(fd, fd+i);
      }
   }

#endif
   /*
    * If msg is NULL, initialize global chain for STDOUT and syslog
    */
   if (msg == NULL) {
      daemon_msgs = (MSGS *)malloc(sizeof(MSGS));
      memset(daemon_msgs, 0, sizeof(MSGS));
      for (i=1; i<=M_MAX; i++) {
         add_msg_dest(daemon_msgs, MD_STDOUT, i, NULL, NULL);
      }
      Dmsg1(050, "Create daemon global message resource %p\n", daemon_msgs);
      return;
   }

   /*
    * Walk down the message resource chain duplicating it
    * for the current Job.
    */
   for (d=msg->dest_chain; d; d=d->next) {
      dnew = (DEST *)malloc(sizeof(DEST));
      memcpy(dnew, d, sizeof(DEST));
      dnew->next = temp_chain;
      dnew->fd = NULL;
      dnew->mail_filename = NULL;
      if (d->mail_cmd) {
         dnew->mail_cmd = bstrdup(d->mail_cmd);
      }
      if (d->where) {
         dnew->where = bstrdup(d->where);
      }
      temp_chain = dnew;
   }

   if (jcr) {
      jcr->jcr_msgs = (MSGS *)malloc(sizeof(MSGS));
      memset(jcr->jcr_msgs, 0, sizeof(MSGS));
      jcr->jcr_msgs->dest_chain = temp_chain;
      memcpy(jcr->jcr_msgs->send_msg, msg->send_msg, sizeof(msg->send_msg));
   } else {
      /* If we have default values, release them now */
      if (daemon_msgs) {
         free_msgs_res(daemon_msgs);
      }
      daemon_msgs = (MSGS *)malloc(sizeof(MSGS));
      memset(daemon_msgs, 0, sizeof(MSGS));
      daemon_msgs->dest_chain = temp_chain;
      memcpy(daemon_msgs->send_msg, msg->send_msg, sizeof(msg->send_msg));
   }

   Dmsg2(250, "Copy message resource %p to %p\n", msg, temp_chain);
}
/*
 * Initialize message handler for a daemon or a Job
 *   We make a copy of the MSGS resource passed, so it belows
 *   to the job or daemon and thus can be modified.
 *
 *   NULL for jcr -> initialize global messages for daemon
 *   non-NULL     -> initialize jcr using Message resource
 */
Initialize message handler for a daemon or a Job
We make a copy of the MSGS resource passed, so it belows
to the job or daemon and thus can be modified.

NULL for jcr -> initialize global messages for daemon
non-NULL     -> initialize jcr using Message resource
Initialize message handler for a daemon or a Job We make a copy of the MSGS resource passed so it belows to the job or daemon and thus can be modified . NULL for jcr - > initialize global messages for daemon non - NULL - > initialize jcr using Message resource
void
init_msg(JCR *jcr, MSGS *msg, job_code_callback_t job_code_callback)
{
   DEST *d, *dnew, *temp_chain = NULL;
   int i;
   if (jcr == NULL && msg == NULL) {
      init_last_jobs_list();
      create_jcr_key();
      set_jcr_in_tsd(INVALID_JCR);
   }
   message_job_code_callback = job_code_callback;
#if !defined(HAVE_WIN32)
   int fd;
   fd = open("/dev/null", O_RDONLY, 0644);
   if (fd > 2) {
      close(fd);
   } else {
      for(i=1; fd + i <= 2; i++) {
         dup2(fd, fd+i);
      }
   }
#endif
   if (msg == NULL) {
      daemon_msgs = (MSGS *)malloc(sizeof(MSGS));
      memset(daemon_msgs, 0, sizeof(MSGS));
      for (i=1; i<=M_MAX; i++) {
         add_msg_dest(daemon_msgs, MD_STDOUT, i, NULL, NULL);
      }
      Dmsg1(050, "Create daemon global message resource %p\n", daemon_msgs);
      return;
   }
   for (d=msg->dest_chain; d; d=d->next) {
      dnew = (DEST *)malloc(sizeof(DEST));
      memcpy(dnew, d, sizeof(DEST));
      dnew->next = temp_chain;
      dnew->fd = NULL;
      dnew->mail_filename = NULL;
      if (d->mail_cmd) {
         dnew->mail_cmd = bstrdup(d->mail_cmd);
      }
      if (d->where) {
         dnew->where = bstrdup(d->where);
      }
      temp_chain = dnew;
   }
   if (jcr) {
      jcr->jcr_msgs = (MSGS *)malloc(sizeof(MSGS));
      memset(jcr->jcr_msgs, 0, sizeof(MSGS));
      jcr->jcr_msgs->dest_chain = temp_chain;
      memcpy(jcr->jcr_msgs->send_msg, msg->send_msg, sizeof(msg->send_msg));
   } else {
      if (daemon_msgs) {
         free_msgs_res(daemon_msgs);
      }
      daemon_msgs = (MSGS *)malloc(sizeof(MSGS));
      memset(daemon_msgs, 0, sizeof(MSGS));
      daemon_msgs->dest_chain = temp_chain;
      memcpy(daemon_msgs->send_msg, msg->send_msg, sizeof(msg->send_msg));
   }
   Dmsg2(250, "Copy message resource %p to %p\n", msg, temp_chain);
}
void init_msg ( JCR * jcr , MSGS * msg , job_code_callback_t job_code_callback ) { DEST * d , * dnew , * temp_chain = NULL ; int i ; if ( jcr == NULL && msg == NULL ) { init_last_jobs_list ( ) ; create_jcr_key ( ) ; set_jcr_in_tsd ( INVALID_JCR ) ; } message_job_code_callback = job_code_callback ; #if ! defined ( HAVE_WIN32 ) int fd ; fd = open ( " " , O_RDONLY , 0644 ) ; if ( fd > 2 ) { close ( fd ) ; } else { for ( i = 1 ; fd + i <= 2 ; i ++ ) { dup2 ( fd , fd + i ) ; } } #endif if ( msg == NULL ) { daemon_msgs = ( MSGS * ) malloc ( sizeof ( MSGS ) ) ; memset ( daemon_msgs , 0 , sizeof ( MSGS ) ) ; for ( i = 1 ; i <= M_MAX ; i ++ ) { add_msg_dest ( daemon_msgs , MD_STDOUT , i , NULL , NULL ) ; } Dmsg1 ( 050 , " \n " , daemon_msgs ) ; return ; } for ( d = msg -> dest_chain ; d ; d = d -> next ) { dnew = ( DEST * ) malloc ( sizeof ( DEST ) ) ; memcpy ( dnew , d , sizeof ( DEST ) ) ; dnew -> next = temp_chain ; dnew -> fd = NULL ; dnew -> mail_filename = NULL ; if ( d -> mail_cmd ) { dnew -> mail_cmd = bstrdup ( d -> mail_cmd ) ; } if ( d -> where ) { dnew -> where = bstrdup ( d -> where ) ; } temp_chain = dnew ; } if ( jcr ) { jcr -> jcr_msgs = ( MSGS * ) malloc ( sizeof ( MSGS ) ) ; memset ( jcr -> jcr_msgs , 0 , sizeof ( MSGS ) ) ; jcr -> jcr_msgs -> dest_chain = temp_chain ; memcpy ( jcr -> jcr_msgs -> send_msg , msg -> send_msg , sizeof ( msg -> send_msg ) ) ; } else { if ( daemon_msgs ) { free_msgs_res ( daemon_msgs ) ; } daemon_msgs = ( MSGS * ) malloc ( sizeof ( MSGS ) ) ; memset ( daemon_msgs , 0 , sizeof ( MSGS ) ) ; daemon_msgs -> dest_chain = temp_chain ; memcpy ( daemon_msgs -> send_msg , msg -> send_msg , sizeof ( msg -> send_msg ) ) ; } Dmsg2 ( 250 , " \n " , msg , temp_chain ) ; }
Initialize message handler for a daemon or a Job
   We make a copy of the MSGS resource passed, so it belows
   to the job or daemon and thus can be modified.
Initialize message handler for a daemon or a Job We make a copy of the MSGS resource passed so it belows to the job or daemon and thus can be modified .
/* Create a daemon key then set invalid jcr */ /* Maybe we should give the daemon a jcr??? */ /*
    * Make sure we have fd's 0, 1, 2 open
    *  If we don't do this one of our sockets may open
    *  there and if we then use stdout, it could
    *  send total garbage to our socket.
    *
    */ /*
    * If msg is NULL, initialize global chain for STDOUT and syslog
    */ /*
    * Walk down the message resource chain duplicating it
    * for the current Job.
    */ /* If we have default values, release them now */