src/lib/message.c
void close_msg(JCR *jcr)
{
   MSGS *msgs;
   DEST *d;
   BPIPE *bpipe;
   POOLMEM *cmd, *line;
   int len, stat;

   Dmsg1(580, "Close_msg jcr=%p\n", jcr);

   if (jcr == NULL) {                /* NULL -> global chain */
      msgs = daemon_msgs;
   } else {
      msgs = jcr->jcr_msgs;
      jcr->jcr_msgs = NULL;
   }
   if (msgs == NULL) {
      return;
   }

   /* Wait for item to be not in use, then mark closing */
   if (msgs->is_closing()) {
      return;
   }
   msgs->wait_not_in_use();          /* leaves fides_mutex set */
   /* Note get_closing() does not lock because we are already locked */
   if (msgs->get_closing()) {
      msgs->unlock();
      return;
   }
   msgs->set_closing();
   msgs->unlock();

   Dmsg1(850, "===Begin close msg resource at %p\n", msgs);
   cmd = get_pool_memory(PM_MESSAGE);
   for (d=msgs->dest_chain; d; ) {
      bool success;
      if (d->fd) {
         switch (d->dest_code) {
         case MD_FILE:
         case MD_APPEND:
            if (d->fd) {
               fclose(d->fd);            /* close open file descriptor */
               d->fd = NULL;
            }
            break;
         case MD_MAIL:
         case MD_MAIL_ON_ERROR:
         case MD_MAIL_ON_SUCCESS:
            Dmsg0(850, "Got MD_MAIL, MD_MAIL_ON_ERROR or MD_MAIL_ON_SUCCESS\n");
            if (!d->fd) {
               break;
            }
            success = jcr && (jcr->JobStatus == JS_Terminated || jcr->JobStatus == JS_Warnings);

            if (d->dest_code == MD_MAIL_ON_ERROR && success) {
               goto rem_temp_file;       /* no mail */
            } else if (d->dest_code == MD_MAIL_ON_SUCCESS && !success) {
               goto rem_temp_file;       /* no mail */
            }

            if (!(bpipe=open_mail_pipe(jcr, cmd, d))) {
               Pmsg0(000, _("open mail pipe failed.\n"));
               goto rem_temp_file;       /* error get out */
            }
            Dmsg0(850, "Opened mail pipe\n");
            len = d->max_len+10;
            line = get_memory(len);
            rewind(d->fd);
            while (fgets(line, len, d->fd)) {
               fputs(line, bpipe->wfd);
            }
            if (!close_wpipe(bpipe)) {       /* close write pipe sending mail */
               berrno be;
               Pmsg1(000, _("close error: ERR=%s\n"), be.bstrerror());
            }

            /*
             * Since we are closing all messages, before "recursing"
             * make sure we are not closing the daemon messages, otherwise
             * kaboom.
             */
            if (msgs != daemon_msgs) {
               /* read what mail prog returned -- should be nothing */
               while (fgets(line, len, bpipe->rfd)) {
                  delivery_error(_("Mail prog: %s"), line);
               }
            }

            stat = close_bpipe(bpipe);
            if (stat != 0 && msgs != daemon_msgs) {
               berrno be;
               be.set_errno(stat);
               Dmsg1(850, "Calling emsg. CMD=%s\n", cmd);
               delivery_error(_("Mail program terminated in error.\n"
                                 "CMD=%s\n"
                                 "ERR=%s\n"), cmd, be.bstrerror());
            }
            free_memory(line);

rem_temp_file:
            /* Remove temp mail file */
            if (d->fd) {
              fclose(d->fd);
              d->fd = NULL;
            }
            /* Exclude spaces in mail_filename */
            if (d->mail_filename) {
               safer_unlink(d->mail_filename, MAIL_REGEX);
               free_pool_memory(d->mail_filename);
               d->mail_filename = NULL;
            }
            Dmsg0(850, "end mail or mail on error\n");
            break;
         default:
            break;
         }
         d->fd = NULL;
      }
      d = d->next;                    /* point to next buffer */
   }
   free_pool_memory(cmd);
   Dmsg0(850, "Done walking message chain.\n");
   if (jcr) {
      free_msgs_res(msgs);
      msgs = NULL;
   } else {
      msgs->clear_closing();
   }
   Dmsg0(850, "===End close msg resource\n");
}
/*
 * Close the messages for this Messages resource, which means to close
 *  any open files, and dispatch any pending email messages.
 */
Close the messages for this Messages resource, which means to close
any open files, and dispatch any pending email messages.
Close the messages for this Messages resource which means to close any open files and dispatch any pending email messages .
void close_msg(JCR *jcr)
{
   MSGS *msgs;
   DEST *d;
   BPIPE *bpipe;
   POOLMEM *cmd, *line;
   int len, stat;
   Dmsg1(580, "Close_msg jcr=%p\n", jcr);
   if (jcr == NULL) {                
      msgs = daemon_msgs;
   } else {
      msgs = jcr->jcr_msgs;
      jcr->jcr_msgs = NULL;
   }
   if (msgs == NULL) {
      return;
   }
   if (msgs->is_closing()) {
      return;
   }
   msgs->wait_not_in_use();          
   if (msgs->get_closing()) {
      msgs->unlock();
      return;
   }
   msgs->set_closing();
   msgs->unlock();
   Dmsg1(850, "===Begin close msg resource at %p\n", msgs);
   cmd = get_pool_memory(PM_MESSAGE);
   for (d=msgs->dest_chain; d; ) {
      bool success;
      if (d->fd) {
         switch (d->dest_code) {
         case MD_FILE:
         case MD_APPEND:
            if (d->fd) {
               fclose(d->fd);            
               d->fd = NULL;
            }
            break;
         case MD_MAIL:
         case MD_MAIL_ON_ERROR:
         case MD_MAIL_ON_SUCCESS:
            Dmsg0(850, "Got MD_MAIL, MD_MAIL_ON_ERROR or MD_MAIL_ON_SUCCESS\n");
            if (!d->fd) {
               break;
            }
            success = jcr && (jcr->JobStatus == JS_Terminated || jcr->JobStatus == JS_Warnings);
            if (d->dest_code == MD_MAIL_ON_ERROR && success) {
               goto rem_temp_file;       
            } else if (d->dest_code == MD_MAIL_ON_SUCCESS && !success) {
               goto rem_temp_file;       
            }
            if (!(bpipe=open_mail_pipe(jcr, cmd, d))) {
               Pmsg0(000, _("open mail pipe failed.\n"));
               goto rem_temp_file;       
            }
            Dmsg0(850, "Opened mail pipe\n");
            len = d->max_len+10;
            line = get_memory(len);
            rewind(d->fd);
            while (fgets(line, len, d->fd)) {
               fputs(line, bpipe->wfd);
            }
            if (!close_wpipe(bpipe)) {       
               berrno be;
               Pmsg1(000, _("close error: ERR=%s\n"), be.bstrerror());
            }
            if (msgs != daemon_msgs) {
               while (fgets(line, len, bpipe->rfd)) {
                  delivery_error(_("Mail prog: %s"), line);
               }
            }
            stat = close_bpipe(bpipe);
            if (stat != 0 && msgs != daemon_msgs) {
               berrno be;
               be.set_errno(stat);
               Dmsg1(850, "Calling emsg. CMD=%s\n", cmd);
               delivery_error(_("Mail program terminated in error.\n"
                                 "CMD=%s\n"
                                 "ERR=%s\n"), cmd, be.bstrerror());
            }
            free_memory(line);
rem_temp_file:
            if (d->fd) {
              fclose(d->fd);
              d->fd = NULL;
            }
            if (d->mail_filename) {
               safer_unlink(d->mail_filename, MAIL_REGEX);
               free_pool_memory(d->mail_filename);
               d->mail_filename = NULL;
            }
            Dmsg0(850, "end mail or mail on error\n");
            break;
         default:
            break;
         }
         d->fd = NULL;
      }
      d = d->next;                    
   }
   free_pool_memory(cmd);
   Dmsg0(850, "Done walking message chain.\n");
   if (jcr) {
      free_msgs_res(msgs);
      msgs = NULL;
   } else {
      msgs->clear_closing();
   }
   Dmsg0(850, "===End close msg resource\n");
}
void close_msg ( JCR * jcr ) { MSGS * msgs ; DEST * d ; BPIPE * bpipe ; POOLMEM * cmd , * line ; int len , stat ; Dmsg1 ( 580 , " \n " , jcr ) ; if ( jcr == NULL ) { msgs = daemon_msgs ; } else { msgs = jcr -> jcr_msgs ; jcr -> jcr_msgs = NULL ; } if ( msgs == NULL ) { return ; } if ( msgs -> is_closing ( ) ) { return ; } msgs -> wait_not_in_use ( ) ; if ( msgs -> get_closing ( ) ) { msgs -> unlock ( ) ; return ; } msgs -> set_closing ( ) ; msgs -> unlock ( ) ; Dmsg1 ( 850 , " \n " , msgs ) ; cmd = get_pool_memory ( PM_MESSAGE ) ; for ( d = msgs -> dest_chain ; d ; ) { bool success ; if ( d -> fd ) { switch ( d -> dest_code ) { case MD_FILE : case MD_APPEND : if ( d -> fd ) { fclose ( d -> fd ) ; d -> fd = NULL ; } break ; case MD_MAIL : case MD_MAIL_ON_ERROR : case MD_MAIL_ON_SUCCESS : Dmsg0 ( 850 , " \n " ) ; if ( ! d -> fd ) { break ; } success = jcr && ( jcr -> JobStatus == JS_Terminated || jcr -> JobStatus == JS_Warnings ) ; if ( d -> dest_code == MD_MAIL_ON_ERROR && success ) { goto rem_temp_file ; } else if ( d -> dest_code == MD_MAIL_ON_SUCCESS && ! success ) { goto rem_temp_file ; } if ( ! ( bpipe = open_mail_pipe ( jcr , cmd , d ) ) ) { Pmsg0 ( 000 , _ ( " \n " ) ) ; goto rem_temp_file ; } Dmsg0 ( 850 , " \n " ) ; len = d -> max_len + 10 ; line = get_memory ( len ) ; rewind ( d -> fd ) ; while ( fgets ( line , len , d -> fd ) ) { fputs ( line , bpipe -> wfd ) ; } if ( ! close_wpipe ( bpipe ) ) { berrno be ; Pmsg1 ( 000 , _ ( " \n " ) , be . bstrerror ( ) ) ; } if ( msgs != daemon_msgs ) { while ( fgets ( line , len , bpipe -> rfd ) ) { delivery_error ( _ ( " " ) , line ) ; } } stat = close_bpipe ( bpipe ) ; if ( stat != 0 && msgs != daemon_msgs ) { berrno be ; be . set_errno ( stat ) ; Dmsg1 ( 850 , " \n " , cmd ) ; delivery_error ( _ ( " \n " " \n " " \n " ) , cmd , be . bstrerror ( ) ) ; } free_memory ( line ) ; rem_temp_file : if ( d -> fd ) { fclose ( d -> fd ) ; d -> fd = NULL ; } if ( d -> mail_filename ) { safer_unlink ( d -> mail_filename , MAIL_REGEX ) ; free_pool_memory ( d -> mail_filename ) ; d -> mail_filename = NULL ; } Dmsg0 ( 850 , " \n " ) ; break ; default : break ; } d -> fd = NULL ; } d = d -> next ; } free_pool_memory ( cmd ) ; Dmsg0 ( 850 , " \n " ) ; if ( jcr ) { free_msgs_res ( msgs ) ; msgs = NULL ; } else { msgs -> clear_closing ( ) ; } Dmsg0 ( 850 , " \n " ) ; }
Close the messages for this Messages resource, which means to close
  any open files, and dispatch any pending email messages.
Close the messages for this Messages resource which means to close any open files and dispatch any pending email messages .
/* NULL -> global chain */ /* Wait for item to be not in use, then mark closing */ /* leaves fides_mutex set */ /* Note get_closing() does not lock because we are already locked */ /* close open file descriptor */ /* no mail */ /* no mail */ /* error get out */ /* close write pipe sending mail */ /*
             * Since we are closing all messages, before "recursing"
             * make sure we are not closing the daemon messages, otherwise
             * kaboom.
             */ /* read what mail prog returned -- should be nothing */ /* Remove temp mail file */ /* Exclude spaces in mail_filename */ /* point to next buffer */