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