]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/message.c
a0079a6acbd39779324a75f574bc9cc8a1930c6a
[bacula/bacula] / bacula / src / lib / message.c
1 /*
2  * Bacula message handling routines
3  *
4  *   Kern Sibbald, April 2000 
5  *
6  *   Version $Id$
7  *
8  */
9
10 /*
11    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
12
13    This program is free software; you can redistribute it and/or
14    modify it under the terms of the GNU General Public License as
15    published by the Free Software Foundation; either version 2 of
16    the License, or (at your option) any later version.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21    General Public License for more details.
22
23    You should have received a copy of the GNU General Public
24    License along with this program; if not, write to the Free
25    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26    MA 02111-1307, USA.
27
28  */
29
30
31 #include "bacula.h"
32 #include "jcr.h"
33
34 #define FULL_LOCATION 1               /* set for file:line in Debug messages */
35
36 char *working_directory = NULL;       /* working directory path stored here */
37 int debug_level = 5;                  /* debug level */
38 time_t daemon_start_time = 0;         /* Daemon start time */
39
40 char my_name[20];                     /* daemon name is stored here */
41 char *exepath = (char *)NULL;
42 char *exename = (char *)NULL;
43 int console_msg_pending = 0;
44 char con_fname[1000];
45 FILE *con_fd = NULL;
46 brwlock_t con_lock; 
47
48 /* Forward referenced functions */
49
50 /* Imported functions */
51
52
53 /* Static storage */
54
55 static MSGS *daemon_msgs;              /* global messages */
56
57 /* 
58  * Set daemon name. Also, find canonical execution
59  *  path.  Note, exepath has spare room for tacking on
60  *  the exename so that we can reconstruct the full name.
61  *
62  * Note, this routine can get called multiple times
63  *  The second time is to put the name as found in the
64  *  Resource record. On the second call, generally,
65  *  argv is NULL to avoid doing the path code twice.
66  */
67 #define BTRACE_EXTRA 20
68 void my_name_is(int argc, char *argv[], char *name)
69 {
70    char *l, *p, *q;
71    char cpath[400], npath[400];
72    int len;
73
74    bstrncpy(my_name, name, sizeof(my_name));
75    if (argc>0 && argv && argv[0]) {
76       /* strip trailing filename and save exepath */
77       for (l=p=argv[0]; *p; p++) {
78          if (*p == '/') {
79             l = p;                       /* set pos of last slash */
80          }
81       }
82       if (*l == '/') {
83          l++;
84       } else {
85          l = argv[0];
86 #ifdef HAVE_CYGWIN
87          /* On Windows allow c: junk */
88          if (l[1] == ':') {
89             l += 2;
90          }
91 #endif
92       }
93       len = strlen(l) + 1;
94       if (exename) {
95          free(exename);
96       }
97       exename = (char *)malloc(len);
98       strcpy(exename, l);
99
100       if (exepath) {
101          free(exepath);
102       }
103       exepath = (char *)malloc(strlen(argv[0]) + 1 + len);
104       for (p=argv[0],q=exepath; p < l; ) {
105          *q++ = *p++;
106       }
107       *q = 0;
108       Dmsg1(200, "exepath=%s\n", exepath);
109       if (strchr(exepath, '.') || exepath[0] != '/') {
110          npath[0] = 0;
111          if (getcwd(cpath, sizeof(cpath))) {
112             if (chdir(exepath) == 0) {
113                if (!getcwd(npath, sizeof(npath))) {
114                   npath[0] = 0;
115                }
116                chdir(cpath);
117             }
118             if (npath[0]) {
119                free(exepath);
120                exepath = (char *)malloc(strlen(npath) + 1 + len);
121                strcpy(exepath, npath);
122             }
123          }
124          Dmsg1(200, "Normalized exepath=%s\n", exepath);
125       }
126    }
127 }
128
129 /* 
130  * Initialize message handler for a daemon or a Job
131  *   We make a copy of the MSGS resource passed, so it belows
132  *   to the job or daemon and thus can be modified.
133  * 
134  *   NULL for jcr -> initialize global messages for daemon
135  *   non-NULL     -> initialize jcr using Message resource
136  */
137 void
138 init_msg(void *vjcr, MSGS *msg)
139 {
140    DEST *d, *dnew, *temp_chain = NULL;
141    JCR *jcr = (JCR *)vjcr;
142
143    /*
144     * If msg is NULL, initialize global chain for STDOUT and syslog
145     */
146    if (msg == NULL) {
147       int i;
148       daemon_msgs = (MSGS *)malloc(sizeof(MSGS));
149       memset(daemon_msgs, 0, sizeof(MSGS));
150       for (i=1; i<=M_MAX; i++) {
151          add_msg_dest(daemon_msgs, MD_STDOUT, i, NULL, NULL);
152          add_msg_dest(daemon_msgs, MD_SYSLOG, i, NULL, NULL);
153       }
154       Dmsg1(050, "Create daemon global message resource 0x%x\n", daemon_msgs);
155       return;
156    }
157
158    /*
159     * Walk down the message resource chain duplicating it
160     * for the current Job.   ****FIXME***** segfault on memcpy
161     */
162    for (d=msg->dest_chain; d; d=d->next) {
163       dnew = (DEST *)malloc(sizeof(DEST));
164       memcpy(dnew, d, sizeof(DEST));
165       dnew->next = temp_chain;
166       dnew->fd = NULL;
167       dnew->mail_filename = NULL;
168       if (d->mail_cmd) {
169          dnew->mail_cmd = bstrdup(d->mail_cmd);
170       }
171       if (d->where) {
172          dnew->where = bstrdup(d->where);
173       }
174       temp_chain = dnew;
175    }
176
177    if (jcr) {
178       jcr->jcr_msgs = (MSGS *)malloc(sizeof(MSGS));
179       memset(jcr->jcr_msgs, 0, sizeof(MSGS));
180       jcr->jcr_msgs->dest_chain = temp_chain;
181       memcpy(jcr->jcr_msgs->send_msg, msg->send_msg, sizeof(msg->send_msg));
182    } else {
183       daemon_msgs = (MSGS *)malloc(sizeof(MSGS));
184       memset(daemon_msgs, 0, sizeof(MSGS));
185       daemon_msgs->dest_chain = temp_chain;
186       memcpy(daemon_msgs->send_msg, msg->send_msg, sizeof(msg->send_msg));
187    }
188    Dmsg2(050, "Copy message resource 0x%x to 0x%x\n", msg, temp_chain);
189 }
190
191 /* Initialize so that the console (User Agent) can
192  * receive messages -- stored in a file.
193  */
194 void init_console_msg(char *wd)
195 {
196    int fd;
197
198    sprintf(con_fname, "%s/%s.conmsg", wd, my_name);
199    fd = open(con_fname, O_CREAT|O_RDWR|O_BINARY, 0600);
200    if (fd == -1) {
201       Emsg2(M_ERROR_TERM, 0, _("Could not open console message file %s: ERR=%s\n"),
202           con_fname, strerror(errno));
203    }
204    if (lseek(fd, 0, SEEK_END) > 0) {
205       console_msg_pending = 1;
206    }
207    close(fd);
208    con_fd = fopen(con_fname, "a+");
209    if (!con_fd) {
210       Emsg2(M_ERROR, 0, _("Could not open console message file %s: ERR=%s\n"),
211           con_fname, strerror(errno));
212    }
213    if (rwl_init(&con_lock) != 0) {
214       Emsg1(M_ERROR_TERM, 0, _("Could not get con mutex: ERR=%s\n"), 
215          strerror(errno));
216    }
217 }
218
219 /* 
220  * Called only during parsing of the config file.
221  *
222  * Add a message destination. I.e. associate a message type with
223  *  a destination (code).
224  * Note, where in the case of dest_code FILE is a filename,
225  *  but in the case of MAIL is a space separated list of
226  *  email addresses, ...
227  */
228 void add_msg_dest(MSGS *msg, int dest_code, int msg_type, char *where, char *mail_cmd)
229 {
230    DEST *d; 
231    /*
232     * First search the existing chain and see if we
233     * can simply add this msg_type to an existing entry.
234     */
235    for (d=msg->dest_chain; d; d=d->next) {
236       if (dest_code == d->dest_code && ((where == NULL && d->where == NULL) ||
237                      (strcmp(where, d->where) == 0))) {  
238          Dmsg4(200, "Add to existing d=%x msgtype=%d destcode=%d where=%s\n", 
239              d, msg_type, dest_code, NPRT(where));
240          set_bit(msg_type, d->msg_types);
241          set_bit(msg_type, msg->send_msg);  /* set msg_type bit in our local */
242          return;
243       }
244    }
245    /* Not found, create a new entry */
246    d = (DEST *)malloc(sizeof(DEST));
247    memset(d, 0, sizeof(DEST));
248    d->next = msg->dest_chain;
249    d->dest_code = dest_code;
250    set_bit(msg_type, d->msg_types);      /* set type bit in structure */
251    set_bit(msg_type, msg->send_msg);     /* set type bit in our local */
252    if (where) {
253       d->where = bstrdup(where);
254    }
255    if (mail_cmd) {
256       d->mail_cmd = bstrdup(mail_cmd);
257    }
258    Dmsg5(200, "add new d=%x msgtype=%d destcode=%d where=%s mailcmd=%s\n", 
259           d, msg_type, dest_code, NPRT(where), NPRT(d->mail_cmd));
260    msg->dest_chain = d;
261 }
262
263 /* 
264  * Called only during parsing of the config file.
265  *
266  * Remove a message destination   
267  */
268 void rem_msg_dest(MSGS *msg, int dest_code, int msg_type, char *where)
269 {
270    DEST *d;
271
272    for (d=msg->dest_chain; d; d=d->next) {
273       Dmsg2(200, "Remove_msg_dest d=%x where=%s\n", d, NPRT(d->where));
274       if (bit_is_set(msg_type, d->msg_types) && (dest_code == d->dest_code) &&
275           ((where == NULL && d->where == NULL) ||
276                      (strcmp(where, d->where) == 0))) {  
277          Dmsg3(200, "Found for remove d=%x msgtype=%d destcode=%d\n", 
278                d, msg_type, dest_code);
279          clear_bit(msg_type, d->msg_types);
280          Dmsg0(200, "Return rem_msg_dest\n");
281          return;
282       }
283    }
284 }
285
286
287
288
289 /*
290  * Edit job codes into main command line
291  *  %% = %
292  *  %j = Job name
293  *  %t = Job type (Backup, ...)
294  *  %e = Job Exit code
295  *  %i = JobId
296  *  %l = job level
297  *  %c = Client's name
298  *  %r = Recipients
299  *  %d = Director's name
300  *
301  *  omsg = edited output message
302  *  imsg = input string containing edit codes (%x)
303  *  to = recepients list 
304  *
305  */
306 static char *edit_job_codes(JCR *jcr, char *omsg, char *imsg, char *to)   
307 {
308    char *p, *str;
309    char add[20];
310
311    *omsg = 0;
312    Dmsg1(200, "edit_job_codes: %s\n", imsg);
313    for (p=imsg; *p; p++) {
314       if (*p == '%') {
315          switch (*++p) {
316          case '%':
317             str = "%";
318             break;
319          case 'c':
320             str = jcr->client_name;
321             if (!str) {
322                str = "";
323             }
324             break;
325          case 'd':
326             str = my_name;            /* Director's name */
327             break;
328          case 'e':
329             str = job_status_to_str(jcr->JobStatus); 
330             break;
331          case 'i':
332             sprintf(add, "%d", jcr->JobId);
333             str = add;
334             break;
335          case 'j':                    /* Job name */
336             str = jcr->Job;
337             break;
338          case 'l':
339             str = job_level_to_str(jcr->JobLevel);
340             break;
341          case 'r':
342             str = to;
343             break;
344          case 't':
345             str = job_type_to_str(jcr->JobType);
346             break;
347          default:
348             add[0] = '%';
349             add[1] = *p;
350             add[2] = 0;
351             str = add;
352             break;
353          }
354       } else {
355          add[0] = *p;
356          add[1] = 0;
357          str = add;
358       }
359       Dmsg1(1200, "add_str %s\n", str);
360       pm_strcat(&omsg, str);
361       Dmsg1(1200, "omsg=%s\n", omsg);
362    }
363    return omsg;
364 }
365
366 static void make_unique_spool_filename(JCR *jcr, POOLMEM **name, int fd)
367 {
368    Mmsg(name, "%s/%s.spool.%s.%d", working_directory, my_name,
369       jcr->Job, fd);
370 }
371
372 int open_spool_file(void *vjcr, BSOCK *bs)
373 {
374     POOLMEM *name  = get_pool_memory(PM_MESSAGE);
375     JCR *jcr = (JCR *)vjcr;
376
377     make_unique_spool_filename(jcr, &name, bs->fd);
378     bs->spool_fd = fopen(name, "w+");
379     if (!bs->spool_fd) {
380        Jmsg(jcr, M_ERROR, 0, "fopen spool file %s failed: ERR=%s\n", name, strerror(errno));
381        free_pool_memory(name);
382        return 0;
383     }
384     free_pool_memory(name);
385     return 1;
386 }
387
388 int close_spool_file(void *vjcr, BSOCK *bs)
389 {
390     POOLMEM *name  = get_pool_memory(PM_MESSAGE);
391     JCR *jcr = (JCR *)vjcr;
392
393     make_unique_spool_filename(jcr, &name, bs->fd);
394     fclose(bs->spool_fd);
395     unlink(name);
396     free_pool_memory(name);
397     bs->spool_fd = NULL;
398     bs->spool = 0;
399     return 1;
400 }
401
402
403 /*
404  * Create a unique filename for the mail command
405  */
406 static void make_unique_mail_filename(JCR *jcr, POOLMEM **name, DEST *d)
407 {
408    if (jcr) {
409       Mmsg(name, "%s/%s.mail.%s.%d", working_directory, my_name,
410                  jcr->Job, (int)d);
411    } else {
412       Mmsg(name, "%s/%s.mail.%s.%d", working_directory, my_name,
413                  my_name, (int)d);
414    }
415    Dmsg1(200, "mailname=%s\n", *name);
416 }
417
418 /*
419  * Open a mail pipe
420  */
421 static BPIPE *open_mail_pipe(JCR *jcr, POOLMEM **cmd, DEST *d)
422 {
423    BPIPE *bpipe;
424
425    if (d->mail_cmd && jcr) {
426       *cmd = edit_job_codes(jcr, *cmd, d->mail_cmd, d->where);
427    } else {
428       Mmsg(cmd, "mail -s \"Bacula Message\" %s", d->where);
429    }
430    fflush(stdout);
431
432    if (!(bpipe = open_bpipe(*cmd, 120, "rw"))) {
433       Jmsg(jcr, M_ERROR, 0, "open mail pipe %s failed: ERR=%s\n", *cmd, strerror(errno));
434    } 
435    return bpipe;
436 }
437
438 /* 
439  * Close the messages for this Messages resource, which means to close
440  *  any open files, and dispatch any pending email messages.
441  */
442 void close_msg(void *vjcr)
443 {
444    MSGS *msgs;
445    JCR *jcr = (JCR *)vjcr;
446    DEST *d;
447    BPIPE *bpipe;
448    POOLMEM *cmd, *line;
449    int len, stat;
450    
451    Dmsg1(050, "Close_msg jcr=0x%x\n", jcr);
452
453    if (jcr == NULL) {                /* NULL -> global chain */
454       msgs = daemon_msgs;
455       daemon_msgs = NULL;
456    } else {
457       msgs = jcr->jcr_msgs;
458       jcr->jcr_msgs = NULL;
459    }
460    if (msgs == NULL) {
461       return;
462    }
463    Dmsg1(150, "===Begin close msg resource at 0x%x\n", msgs);
464    cmd = get_pool_memory(PM_MESSAGE);
465    for (d=msgs->dest_chain; d; ) {
466       if (d->fd) {
467          switch (d->dest_code) {
468          case MD_FILE:
469          case MD_APPEND:
470             if (d->fd) {
471                fclose(d->fd);            /* close open file descriptor */
472             }
473             break;
474          case MD_MAIL:
475          case MD_MAIL_ON_ERROR:
476             Dmsg0(150, "Got MD_MAIL or MD_MAIL_ON_ERROR\n");
477             if (!d->fd) {
478                break;
479             }
480             if (d->dest_code == MD_MAIL_ON_ERROR && jcr &&
481                 jcr->JobStatus == JS_Terminated) {
482                goto rem_temp_file;
483             }
484             
485             if (!(bpipe=open_mail_pipe(jcr, &cmd, d))) {
486                Dmsg0(000, "open mail pipe failed.\n");
487                goto rem_temp_file;
488             }
489             Dmsg0(150, "Opened mail pipe\n");
490             len = d->max_len+10;
491             line = get_memory(len);
492             rewind(d->fd);
493             while (fgets(line, len, d->fd)) {
494                fputs(line, bpipe->wfd);
495             }
496             if (!close_wpipe(bpipe)) {       /* close write pipe sending mail */
497                Dmsg1(000, "close error: ERR=%s\n", strerror(errno));
498             }
499
500             /*
501              * Since we are closing all messages, before "recursing"
502              * make sure we are not closing the daemon messages, otherwise
503              * kaboom.
504              */
505             if (msgs != daemon_msgs) {
506                /* read what mail prog returned -- should be nothing */
507                while (fgets(line, len, bpipe->rfd)) {
508 //                Dmsg1(000, "Mail prog got: %s", line);
509                   Jmsg1(jcr, M_INFO, 0, _("Mail prog: %s"), line);
510                }
511             }
512
513             stat = close_bpipe(bpipe);
514             if (stat != 0 && msgs != daemon_msgs) {
515                Dmsg1(150, "Calling emsg. CMD=%s\n", cmd);
516                Jmsg2(jcr, M_ERROR, 0, _("Mail program terminated in error. stat=%d\n"
517                                         "CMD=%s\n"), stat, cmd);
518             }
519             free_memory(line);
520 rem_temp_file:
521             /* Remove temp file */
522             fclose(d->fd);
523             unlink(d->mail_filename);
524             free_pool_memory(d->mail_filename);
525             d->mail_filename = NULL;
526             Dmsg0(150, "end mail or mail on error\n");
527             break;
528          default:
529             break;
530          }
531          d->fd = NULL;
532       }
533       d = d->next;                    /* point to next buffer */
534    }
535    free_pool_memory(cmd);
536    Dmsg0(150, "Done walking message chain.\n");
537    free_msgs_res(msgs);
538    msgs = NULL;
539    Dmsg0(150, "===End close msg resource\n");
540 }
541
542 /*
543  * Free memory associated with Messages resource  
544  */
545 void free_msgs_res(MSGS *msgs)
546 {
547    DEST *d, *old;
548
549    for (d=msgs->dest_chain; d; ) {
550       if (d->where) {
551          free(d->where);
552       }
553       if (d->mail_cmd) {
554          free(d->mail_cmd);
555       }
556       old = d;                        /* save pointer to release */
557       d = d->next;                    /* point to next buffer */
558       free(old);                      /* free the destination item */
559    }
560    msgs->dest_chain = NULL;
561    free(msgs);
562 }
563
564
565 /* 
566  * Terminate the message handler for good. 
567  * Release the global destination chain.
568  * 
569  * Also, clean up a few other items (cons, exepath). Note,
570  *   these really should be done elsewhere.
571  */
572 void term_msg()
573 {
574    Dmsg0(100, "Enter term_msg\n");
575    close_msg(NULL);                   /* close global chain */
576    daemon_msgs = NULL;
577    if (con_fd) {
578       fflush(con_fd);
579       fclose(con_fd);
580       con_fd = NULL;
581    }
582    if (exepath) {
583       free(exepath);
584       exepath = NULL;
585    }
586    if (exename) {
587       free(exename);
588       exename = NULL;
589    }
590 }
591
592
593
594 /*
595  * Handle sending the message to the appropriate place
596  */
597 void dispatch_message(void *vjcr, int type, int level, char *msg)
598 {
599     DEST *d;   
600     char cmd[MAXSTRING];
601     POOLMEM *mcmd;
602     JCR *jcr = (JCR *) vjcr;
603     int len;
604     MSGS *msgs;
605     BPIPE *bpipe;
606
607     Dmsg2(200, "Enter dispatch_msg type=%d msg=%s\n", type, msg);
608
609     if (type == M_ABORT || type == M_ERROR_TERM) {
610        fprintf(stdout, msg);          /* print this here to INSURE that it is printed */
611     }
612
613     /* Now figure out where to send the message */
614     msgs = NULL;
615     if (jcr) {
616        msgs = jcr->jcr_msgs;
617     } 
618     if (msgs == NULL) {
619        msgs = daemon_msgs;
620     }
621     for (d=msgs->dest_chain; d; d=d->next) {
622        if (bit_is_set(type, d->msg_types)) {
623           switch (d->dest_code) {
624              case MD_CONSOLE:
625                 Dmsg1(400, "CONSOLE for following msg: %s", msg);
626                 if (!con_fd) {
627                    con_fd = fopen(con_fname, "a+");
628                    Dmsg0(200, "Console file not open.\n");
629                 }
630                 if (con_fd) {
631                    Pw(con_lock);      /* get write lock on console message file */
632                    errno = 0;
633                    bstrftime(cmd, sizeof(cmd), time(NULL));
634                    len = strlen(cmd);
635                    cmd[len++] = ' ';
636                    fwrite(cmd, len, 1, con_fd);
637                    len = strlen(msg);
638                    if (len > 0 && msg[len-1] != '\n') {
639                       msg[len++] = '\n';
640                       msg[len] = 0;
641                    }
642                    fwrite(msg, len, 1, con_fd);
643                    fflush(con_fd);
644                    console_msg_pending = TRUE;
645                    Vw(con_lock);
646                 }
647                 break;
648              case MD_SYSLOG:
649                 Dmsg1(400, "SYSLOG for collowing msg: %s\n", msg);
650                 /* We really should do an openlog() here */
651                 syslog(LOG_DAEMON|LOG_ERR, msg);
652                 break;
653              case MD_OPERATOR:
654                 Dmsg1(400, "OPERATOR for collowing msg: %s\n", msg);
655                 mcmd = get_pool_memory(PM_MESSAGE);
656                 if ((bpipe=open_mail_pipe(jcr, &mcmd, d))) {
657                    int stat;
658                    fputs(msg, bpipe->wfd);
659                    /* Messages to the operator go one at a time */
660                    stat = close_bpipe(bpipe);
661                    if (stat != 0) {
662                       Emsg1(M_ERROR, 0, _("Operator mail program terminated in error.\nCMD=%s\n"),
663                          mcmd);
664                    }
665                 }
666                 free_pool_memory(mcmd);
667                 break;
668              case MD_MAIL:
669              case MD_MAIL_ON_ERROR:
670                 Dmsg1(400, "MAIL for following msg: %s", msg);
671                 if (!d->fd) {
672                    POOLMEM *name  = get_pool_memory(PM_MESSAGE);
673                    make_unique_mail_filename(jcr, &name, d);
674                    d->fd = fopen(name, "w+");
675                    if (!d->fd) {
676                       d->fd = stdout;
677                       Emsg2(M_ERROR, 0, "fopen %s failed: ERR=%s\n", name, strerror(errno));
678                       d->fd = NULL;
679                       free_pool_memory(name);
680                       break;
681                    }
682                    d->mail_filename = name;
683                 }
684                 len = strlen(msg);
685                 if (len > d->max_len) {
686                    d->max_len = len;      /* keep max line length */
687                 }
688                 fputs(msg, d->fd);
689                 break;
690              case MD_FILE:
691                 Dmsg1(400, "FILE for following msg: %s", msg);
692                 if (!d->fd) {
693                    d->fd = fopen(d->where, "w+");
694                    if (!d->fd) {
695                       d->fd = stdout;
696                       Emsg2(M_ERROR, 0, "fopen %s failed: ERR=%s\n", d->where, strerror(errno));
697                       d->fd = NULL;
698                       break;
699                    }
700                 }
701                 fputs(msg, d->fd);
702                 break;
703              case MD_APPEND:
704                 Dmsg1(400, "APPEND for following msg: %s", msg);
705                 if (!d->fd) {
706                    d->fd = fopen(d->where, "a");
707                    if (!d->fd) {
708                       d->fd = stdout;
709                       Emsg2(M_ERROR, 0, "fopen %s failed: ERR=%s\n", d->where, strerror(errno));
710                       d->fd = NULL;
711                       break;
712                    }
713                 }
714                 fputs(msg, d->fd);
715                 break;
716              case MD_DIRECTOR:
717                 Dmsg1(400, "DIRECTOR for following msg: %s", msg);
718                 if (jcr && jcr->dir_bsock && !jcr->dir_bsock->errors) {
719
720                    jcr->dir_bsock->msglen = Mmsg(&(jcr->dir_bsock->msg),
721                         "Jmsg Job=%s type=%d level=%d %s", jcr->Job,
722                          type, level, msg) + 1;
723                    bnet_send(jcr->dir_bsock);
724                 }
725                 break;
726              case MD_STDOUT:
727                 Dmsg1(400, "STDOUT for following msg: %s", msg);
728                 if (type != M_ABORT && type != M_ERROR_TERM)  /* already printed */
729                    fprintf(stdout, msg);
730                 break;
731              case MD_STDERR:
732                 Dmsg1(400, "STDERR for following msg: %s", msg);
733                 fprintf(stderr, msg);
734                 break;
735              default:
736                 break;
737           }
738        }
739     }
740 }
741
742
743 /*********************************************************************
744  *
745  *  subroutine prints a debug message if the level number
746  *  is less than or equal the debug_level. File and line numbers
747  *  are included for more detail if desired, but not currently
748  *  printed.
749  *  
750  *  If the level is negative, the details of file and line number
751  *  are not printed.
752  */
753 void 
754 d_msg(char *file, int line, int level, char *fmt,...)
755 {
756     char      buf[2000];
757     int       i;
758     va_list   arg_ptr;
759     int       details = TRUE;
760
761     if (level < 0) {
762        details = FALSE;
763        level = -level;
764     }
765
766     if (level <= debug_level) {
767 #ifdef FULL_LOCATION
768        if (details) {
769           sprintf(buf, "%s: %s:%d ", my_name, file, line);
770           i = strlen(buf);
771        } else {
772           i = 0;
773        }
774 #else
775        i = 0;
776 #endif
777        va_start(arg_ptr, fmt);
778        bvsnprintf(buf+i, sizeof(buf)-i, (char *)fmt, arg_ptr);
779        va_end(arg_ptr);
780
781        fprintf(stdout, buf);
782     }
783 }
784
785
786 /* *********************************************************
787  *
788  * print an error message
789  *
790  */
791 void 
792 e_msg(char *file, int line, int type, int level, char *fmt,...)
793 {
794     char     buf[2000];
795     va_list   arg_ptr;
796     int i;
797
798     /* 
799      * Check if we have a message destination defined.  
800      * We always report M_ABORT and M_ERROR_TERM 
801      */
802     if (!daemon_msgs || ((type != M_ABORT && type != M_ERROR_TERM) && 
803                          !bit_is_set(type, daemon_msgs->send_msg)))
804        return;                        /* no destination */
805     switch (type) {
806        case M_ABORT:
807           sprintf(buf, "%s: ABORTING due to ERROR in %s:%d\n", 
808                   my_name, file, line);
809           break;
810        case M_ERROR_TERM:
811           sprintf(buf, "%s: ERROR TERMINATION at %s:%d\n", 
812                   my_name, file, line);
813           break;
814        case M_FATAL:
815           if (level == -1)            /* skip details */
816              sprintf(buf, "%s: Fatal Error because: ", my_name);
817           else
818              sprintf(buf, "%s: Fatal Error at %s:%d because:\n", my_name, file, line);
819           break;
820        case M_ERROR:
821           if (level == -1)            /* skip details */
822              sprintf(buf, "%s: Error: ", my_name);
823           else
824              sprintf(buf, "%s: Error in %s:%d ", my_name, file, line);
825           break;
826        case M_WARNING:
827           sprintf(buf, "%s: Warning: ", my_name);
828           break;
829        default:
830           sprintf(buf, "%s: ", my_name);
831           break;
832     }
833
834     i = strlen(buf);
835     va_start(arg_ptr, fmt);
836     bvsnprintf(buf+i, sizeof(buf)-i, (char *)fmt, arg_ptr);
837     va_end(arg_ptr);
838
839     dispatch_message(NULL, type, level, buf);
840
841     if (type == M_ABORT) {
842        char *p = 0;
843        p[0] = 0;                      /* generate segmentation violation */
844     }
845     if (type == M_ERROR_TERM) {
846        _exit(1);
847     }
848 }
849
850 /* *********************************************************
851  *
852  * Generate a Job message
853  *
854  */
855 void 
856 Jmsg(void *vjcr, int type, int level, char *fmt,...)
857 {
858     char     rbuf[2000];
859     char     *buf;
860     va_list   arg_ptr;
861     int i, len;
862     JCR *jcr = (JCR *)vjcr;
863     MSGS *msgs;
864     char *job;
865
866     
867     Dmsg1(200, "Enter Jmsg type=%d\n", type);
868
869     msgs = NULL;
870     job = NULL;
871     if (jcr) {
872        msgs = jcr->jcr_msgs;
873        job = jcr->Job;
874     } 
875     if (!msgs) {
876        msgs = daemon_msgs;
877     }
878     if (!job) {
879        job = "";
880     }
881
882     buf = rbuf;                    /* we are the Director */
883     /* 
884      * Check if we have a message destination defined.  
885      * We always report M_ABORT and M_ERROR_TERM 
886      */
887     if ((type != M_ABORT && type != M_ERROR_TERM) && msgs && !bit_is_set(type, msgs->send_msg)) {
888        Dmsg1(200, "No bit set for type %d\n", type);
889        return;                        /* no destination */
890     }
891     switch (type) {
892        case M_ABORT:
893           sprintf(buf, "%s ABORTING due to ERROR\n", my_name);
894           break;
895        case M_ERROR_TERM:
896           sprintf(buf, "%s ERROR TERMINATION\n", my_name);
897           break;
898        case M_FATAL:
899           sprintf(buf, "%s: %s Fatal error: ", my_name, job);
900           if (jcr) {
901              jcr->JobStatus = JS_FatalError;
902           }
903           break;
904        case M_ERROR:
905           sprintf(buf, "%s: %s Error: ", my_name, job);
906           if (jcr) {
907              jcr->Errors++;
908           }
909           break;
910        case M_WARNING:
911           sprintf(buf, "%s: %s Warning: ", my_name, job);
912           break;
913        default:
914           sprintf(buf, "%s: ", my_name);
915           break;
916     }
917
918     i = strlen(buf);
919     va_start(arg_ptr, fmt);
920     len = bvsnprintf(buf+i, sizeof(rbuf)-i, fmt, arg_ptr);
921     va_end(arg_ptr);
922
923     dispatch_message(jcr, type, level, rbuf);
924
925     Dmsg3(500, "i=%d sizeof(rbuf)-i=%d len=%d\n", i, sizeof(rbuf)-i, len);
926
927     if (type == M_ABORT){
928        char *p = 0;
929        p[0] = 0;                      /* generate segmentation violation */
930     }
931     if (type == M_ERROR_TERM) {
932        _exit(1);
933     }
934 }
935
936 /*
937  * Edit a message into a Pool memory buffer, with file:lineno
938  */
939 int m_msg(char *file, int line, POOLMEM **pool_buf, char *fmt, ...)
940 {
941    va_list   arg_ptr;
942    int i, len, maxlen;
943
944    sprintf(*pool_buf, "%s:%d ", file, line);
945    i = strlen(*pool_buf);
946
947 again:
948    maxlen = sizeof_pool_memory(*pool_buf) - i - 1; 
949    va_start(arg_ptr, fmt);
950    len = bvsnprintf(*pool_buf+i, maxlen, fmt, arg_ptr);
951    va_end(arg_ptr);
952    if (len < 0 || len >= maxlen) {
953       *pool_buf = realloc_pool_memory(*pool_buf, maxlen + i + 200);
954       goto again;
955    }
956    return len;
957 }
958
959 /*
960  * Edit a message into a Pool Memory buffer NO file:lineno
961  *  Returns: string length of what was edited.
962  */
963 int Mmsg(POOLMEM **pool_buf, char *fmt, ...)
964 {
965    va_list   arg_ptr;
966    int len, maxlen;
967
968 again:
969    maxlen = sizeof_pool_memory(*pool_buf) - 1; 
970    va_start(arg_ptr, fmt);
971    len = bvsnprintf(*pool_buf, maxlen, fmt, arg_ptr);
972    va_end(arg_ptr);
973    if (len < 0 || len >= maxlen) {
974       *pool_buf = realloc_pool_memory(*pool_buf, maxlen + 200);
975       goto again;
976    }
977    return len;
978 }
979
980
981 /*
982  * If we come here, prefix the message with the file:line-number,
983  *  then pass it on to the normal Jmsg routine.
984  */
985 void j_msg(char *file, int line, void *jcr, int type, int level, char *fmt,...)
986 {
987    va_list   arg_ptr;
988    int i, len, maxlen;
989    POOLMEM *pool_buf;
990
991    pool_buf = get_pool_memory(PM_EMSG);
992    sprintf(pool_buf, "%s:%d ", file, line);
993    i = strlen(pool_buf);
994
995 again:
996    maxlen = sizeof_pool_memory(pool_buf) - i - 1; 
997    va_start(arg_ptr, fmt);
998    len = bvsnprintf(pool_buf+i, maxlen, fmt, arg_ptr);
999    va_end(arg_ptr);
1000    if (len < 0 || len >= maxlen) {
1001       pool_buf = realloc_pool_memory(pool_buf, maxlen + i + 200);
1002       goto again;
1003    }
1004
1005    Jmsg(jcr, type, level, pool_buf);
1006    free_memory(pool_buf);
1007 }