]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/message.c
More cleanup
[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-2003 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                   Jmsg1(jcr, M_INFO, 0, _("Mail prog: %s"), line);
436                }
437             }
438
439             stat = close_bpipe(bpipe);
440             if (stat != 0 && msgs != daemon_msgs) {
441                Dmsg1(150, "Calling emsg. CMD=%s\n", cmd);
442                Jmsg2(jcr, M_ERROR, 0, _("Mail program terminated in error. stat=%d\n"
443                                         "CMD=%s\n"), stat, cmd);
444             }
445             free_memory(line);
446 rem_temp_file:
447             /* Remove temp file */
448             fclose(d->fd);
449             unlink(d->mail_filename);
450             free_pool_memory(d->mail_filename);
451             d->mail_filename = NULL;
452             Dmsg0(150, "end mail or mail on error\n");
453             break;
454          default:
455             break;
456          }
457          d->fd = NULL;
458       }
459       d = d->next;                    /* point to next buffer */
460    }
461    free_pool_memory(cmd);
462    Dmsg0(150, "Done walking message chain.\n");
463    free_msgs_res(msgs);
464    msgs = NULL;
465    Dmsg0(150, "===End close msg resource\n");
466 }
467
468 /*
469  * Free memory associated with Messages resource  
470  */
471 void free_msgs_res(MSGS *msgs)
472 {
473    DEST *d, *old;
474
475    for (d=msgs->dest_chain; d; ) {
476       if (d->where) {
477          free(d->where);
478       }
479       if (d->mail_cmd) {
480          free(d->mail_cmd);
481       }
482       old = d;                        /* save pointer to release */
483       d = d->next;                    /* point to next buffer */
484       free(old);                      /* free the destination item */
485    }
486    msgs->dest_chain = NULL;
487    free(msgs);
488 }
489
490
491 /* 
492  * Terminate the message handler for good. 
493  * Release the global destination chain.
494  * 
495  * Also, clean up a few other items (cons, exepath). Note,
496  *   these really should be done elsewhere.
497  */
498 void term_msg()
499 {
500    Dmsg0(100, "Enter term_msg\n");
501    close_msg(NULL);                   /* close global chain */
502    daemon_msgs = NULL;
503    if (con_fd) {
504       fflush(con_fd);
505       fclose(con_fd);
506       con_fd = NULL;
507    }
508    if (exepath) {
509       free(exepath);
510       exepath = NULL;
511    }
512    if (exename) {
513       free(exename);
514       exename = NULL;
515    }
516    if (trace_fd) {
517       fclose(trace_fd);
518       trace_fd = NULL;
519    }
520 }
521
522
523
524 /*
525  * Handle sending the message to the appropriate place
526  */
527 void dispatch_message(void *vjcr, int type, int level, char *msg)
528 {
529     DEST *d;   
530     char dt[MAX_TIME_LENGTH];
531     POOLMEM *mcmd;
532     JCR *jcr = (JCR *) vjcr;
533     int len;
534     MSGS *msgs;
535     BPIPE *bpipe;
536
537     Dmsg2(200, "Enter dispatch_msg type=%d msg=%s\n", type, msg);
538
539     if (type == M_ABORT || type == M_ERROR_TERM) {
540        fputs(msg, stdout);         /* print this here to INSURE that it is printed */
541     }
542
543     /* Now figure out where to send the message */
544     msgs = NULL;
545     if (jcr) {
546        msgs = jcr->jcr_msgs;
547     } 
548     if (msgs == NULL) {
549        msgs = daemon_msgs;
550     }
551     for (d=msgs->dest_chain; d; d=d->next) {
552        if (bit_is_set(type, d->msg_types)) {
553           switch (d->dest_code) {
554              case MD_CONSOLE:
555                 Dmsg1(400, "CONSOLE for following msg: %s", msg);
556                 if (!con_fd) {
557                    con_fd = fopen(con_fname, "a+");
558                    Dmsg0(200, "Console file not open.\n");
559                 }
560                 if (con_fd) {
561                    Pw(con_lock);      /* get write lock on console message file */
562                    errno = 0;
563                    bstrftime(dt, sizeof(dt), time(NULL));
564                    len = strlen(dt);
565                    dt[len++] = ' ';
566                    fwrite(dt, len, 1, con_fd);
567                    len = strlen(msg);
568                    if (len > 0) {
569                       fwrite(msg, len, 1, con_fd);
570                       if (msg[len-1] != '\n') {
571                          fwrite("\n", 2, 1, con_fd);
572                       }
573                    } else {
574                       fwrite("\n", 2, 1, con_fd);
575                    }
576                    fflush(con_fd);
577                    console_msg_pending = TRUE;
578                    Vw(con_lock);
579                 }
580                 break;
581              case MD_SYSLOG:
582                 Dmsg1(400, "SYSLOG for collowing msg: %s\n", msg);
583                 /* We really should do an openlog() here */
584                 syslog(LOG_DAEMON|LOG_ERR, msg);
585                 break;
586              case MD_OPERATOR:
587                 Dmsg1(400, "OPERATOR for collowing msg: %s\n", msg);
588                 mcmd = get_pool_memory(PM_MESSAGE);
589                 if ((bpipe=open_mail_pipe(jcr, &mcmd, d))) {
590                    int stat;
591                    fputs(msg, bpipe->wfd);
592                    /* Messages to the operator go one at a time */
593                    stat = close_bpipe(bpipe);
594                    if (stat != 0) {
595                       Emsg1(M_ERROR, 0, _("Operator mail program terminated in error.\nCMD=%s\n"),
596                          mcmd);
597                    }
598                 }
599                 free_pool_memory(mcmd);
600                 break;
601              case MD_MAIL:
602              case MD_MAIL_ON_ERROR:
603                 Dmsg1(400, "MAIL for following msg: %s", msg);
604                 if (!d->fd) {
605                    POOLMEM *name  = get_pool_memory(PM_MESSAGE);
606                    make_unique_mail_filename(jcr, &name, d);
607                    d->fd = fopen(name, "w+");
608                    if (!d->fd) {
609                       d->fd = stdout;
610                       Emsg2(M_ERROR, 0, "fopen %s failed: ERR=%s\n", name, strerror(errno));
611                       d->fd = NULL;
612                       free_pool_memory(name);
613                       break;
614                    }
615                    d->mail_filename = name;
616                 }
617                 len = strlen(msg);
618                 if (len > d->max_len) {
619                    d->max_len = len;      /* keep max line length */
620                 }
621                 fputs(msg, d->fd);
622                 break;
623              case MD_FILE:
624                 Dmsg1(400, "FILE for following msg: %s", msg);
625                 if (!d->fd) {
626                    d->fd = fopen(d->where, "w+");
627                    if (!d->fd) {
628                       d->fd = stdout;
629                       Emsg2(M_ERROR, 0, "fopen %s failed: ERR=%s\n", d->where, strerror(errno));
630                       d->fd = NULL;
631                       break;
632                    }
633                 }
634                 fputs(msg, d->fd);
635                 break;
636              case MD_APPEND:
637                 Dmsg1(400, "APPEND for following msg: %s", msg);
638                 if (!d->fd) {
639                    d->fd = fopen(d->where, "a");
640                    if (!d->fd) {
641                       d->fd = stdout;
642                       Emsg2(M_ERROR, 0, "fopen %s failed: ERR=%s\n", d->where, strerror(errno));
643                       d->fd = NULL;
644                       break;
645                    }
646                 }
647                 fputs(msg, d->fd);
648                 break;
649              case MD_DIRECTOR:
650                 Dmsg1(400, "DIRECTOR for following msg: %s", msg);
651                 if (jcr && jcr->dir_bsock && !jcr->dir_bsock->errors) {
652
653                    jcr->dir_bsock->msglen = Mmsg(&(jcr->dir_bsock->msg),
654                         "Jmsg Job=%s type=%d level=%d %s", jcr->Job,
655                          type, level, msg) + 1;
656                    bnet_send(jcr->dir_bsock);
657                 }
658                 break;
659              case MD_STDOUT:
660                 Dmsg1(400, "STDOUT for following msg: %s", msg);
661                 if (type != M_ABORT && type != M_ERROR_TERM)  /* already printed */
662                    fputs(msg, stdout);
663                 break;
664              case MD_STDERR:
665                 Dmsg1(400, "STDERR for following msg: %s", msg);
666                 fputs(msg, stderr);
667                 break;
668              default:
669                 break;
670           }
671        }
672     }
673 }
674
675
676 /*********************************************************************
677  *
678  *  subroutine prints a debug message if the level number
679  *  is less than or equal the debug_level. File and line numbers
680  *  are included for more detail if desired, but not currently
681  *  printed.
682  *  
683  *  If the level is negative, the details of file and line number
684  *  are not printed.
685  */
686 void 
687 d_msg(char *file, int line, int level, char *fmt,...)
688 {
689     char      buf[5000];
690     int       len;
691     va_list   arg_ptr;
692     int       details = TRUE;
693
694     if (level < 0) {
695        details = FALSE;
696        level = -level;
697     }
698
699     if (level <= debug_level) {
700 #ifdef FULL_LOCATION
701        if (details) {
702           len= sprintf(buf, "%s: %s:%d ", my_name, file, line);
703        } else {
704           len = 0;
705        }
706 #else
707        len = 0;
708 #endif
709        va_start(arg_ptr, fmt);
710        bvsnprintf(buf+len, sizeof(buf)-len, (char *)fmt, arg_ptr);
711        va_end(arg_ptr);
712
713        fputs(buf, stdout);
714     }
715 }
716
717
718 /*********************************************************************
719  *
720  *  subroutine writes a debug message to the trace file if the level number
721  *  is less than or equal the debug_level. File and line numbers
722  *  are included for more detail if desired, but not currently
723  *  printed.
724  *  
725  *  If the level is negative, the details of file and line number
726  *  are not printed.
727  */
728 void 
729 t_msg(char *file, int line, int level, char *fmt,...)
730 {
731     char      buf[5000];
732     int       len;
733     va_list   arg_ptr;
734     int       details = TRUE;
735
736     return;
737
738     if (level < 0) {
739        details = FALSE;
740        level = -level;
741     }
742
743     if (level <= debug_level) {
744        if (!trace_fd) {
745           trace_fd = fopen("bacula.trace", "a+");
746           if (!trace_fd) {
747              Emsg1(M_ABORT, 0, _("Cannot open bacula.trace: ERR=%s\n"),
748                   strerror(errno));
749           }
750        }
751     
752 #ifdef FULL_LOCATION
753        if (details) {
754           len = sprintf(buf, "%s: %s:%d ", my_name, file, line);
755        } else {
756           len = 0;
757        }
758 #else
759        len = 0;
760 #endif
761        va_start(arg_ptr, fmt);
762        bvsnprintf(buf+len, sizeof(buf)-len, (char *)fmt, arg_ptr);
763        va_end(arg_ptr);
764
765        fputs(buf, trace_fd);
766        fflush(trace_fd);
767     }
768 }
769
770
771
772 /* *********************************************************
773  *
774  * print an error message
775  *
776  */
777 void 
778 e_msg(char *file, int line, int type, int level, char *fmt,...)
779 {
780     char     buf[5000];
781     va_list   arg_ptr;
782     int len;
783
784     /* 
785      * Check if we have a message destination defined.  
786      * We always report M_ABORT and M_ERROR_TERM 
787      */
788     if (!daemon_msgs || ((type != M_ABORT && type != M_ERROR_TERM) && 
789                          !bit_is_set(type, daemon_msgs->send_msg))) {
790        return;                        /* no destination */
791     }
792     switch (type) {
793        case M_ABORT:
794           len = sprintf(buf, "%s: ABORTING due to ERROR in %s:%d\n", 
795                   my_name, file, line);
796           break;
797        case M_ERROR_TERM:
798           len = sprintf(buf, "%s: ERROR TERMINATION at %s:%d\n", 
799                   my_name, file, line);
800           break;
801        case M_FATAL:
802           if (level == -1)            /* skip details */
803              len = sprintf(buf, "%s: Fatal Error because: ", my_name);
804           else
805              len = sprintf(buf, "%s: Fatal Error at %s:%d because:\n", my_name, file, line);
806           break;
807        case M_ERROR:
808           if (level == -1)            /* skip details */
809              len = sprintf(buf, "%s: Error: ", my_name);
810           else
811              len = sprintf(buf, "%s: Error in %s:%d ", my_name, file, line);
812           break;
813        case M_WARNING:
814           len = sprintf(buf, "%s: Warning: ", my_name);
815           break;
816        default:
817           len = sprintf(buf, "%s: ", my_name);
818           break;
819     }
820
821     va_start(arg_ptr, fmt);
822     bvsnprintf(buf+len, sizeof(buf)-len, (char *)fmt, arg_ptr);
823     va_end(arg_ptr);
824
825     dispatch_message(NULL, type, level, buf);
826
827     if (type == M_ABORT) {
828        char *p = 0;
829        p[0] = 0;                      /* generate segmentation violation */
830     }
831     if (type == M_ERROR_TERM) {
832        _exit(1);
833     }
834 }
835
836 /* *********************************************************
837  *
838  * Generate a Job message
839  *
840  */
841 void 
842 Jmsg(void *vjcr, int type, int level, char *fmt,...)
843 {
844     char     rbuf[5000];
845     va_list   arg_ptr;
846     int len;
847     JCR *jcr = (JCR *)vjcr;
848     MSGS *msgs;
849     char *job;
850
851     
852     Dmsg1(200, "Enter Jmsg type=%d\n", type);
853
854     msgs = NULL;
855     job = NULL;
856     if (jcr) {
857        msgs = jcr->jcr_msgs;
858        job = jcr->Job;
859     } 
860     if (!msgs) {
861        msgs = daemon_msgs;            /* if no jcr, we use daemon handler */
862     }
863     if (!job) {
864        job = "";                      /* Set null job name if none */
865     }
866
867     /* 
868      * Check if we have a message destination defined.  
869      * We always report M_ABORT and M_ERROR_TERM 
870      */
871
872     /* There is an apparent compiler bug with the following if
873      *  statement, so the set_jcr... is simply a noop to reload 
874      *  registers.
875      */
876     set_jcr_job_status(jcr, jcr->JobStatus);
877     if (msgs && (type != M_ABORT && type != M_ERROR_TERM) &&
878          !bit_is_set(type, msgs->send_msg)) {
879        return;                        /* no destination */
880     }
881     switch (type) {
882        case M_ABORT:
883           len = sprintf(rbuf, "%s ABORTING due to ERROR\n", my_name);
884           break;
885        case M_ERROR_TERM:
886           len = sprintf(rbuf, "%s ERROR TERMINATION\n", my_name);
887           break;
888        case M_FATAL:
889           len = sprintf(rbuf, "%s: %s Fatal error: ", my_name, job);
890           if (jcr) {
891              set_jcr_job_status(jcr, JS_FatalError);
892           }
893           break;
894        case M_ERROR:
895           len = sprintf(rbuf, "%s: %s Error: ", my_name, job);
896           if (jcr) {
897              jcr->Errors++;
898           }
899           break;
900        case M_WARNING:
901           len = sprintf(rbuf, "%s: %s Warning: ", my_name, job);
902           break;
903        default:
904           len = sprintf(rbuf, "%s: ", my_name);
905           break;
906     }
907
908     va_start(arg_ptr, fmt);
909     bvsnprintf(rbuf+len,  sizeof(rbuf)-len, fmt, arg_ptr);
910     va_end(arg_ptr);
911
912     dispatch_message(jcr, type, level, rbuf);
913
914     if (type == M_ABORT){
915        char *p = 0;
916        p[0] = 0;                      /* generate segmentation violation */
917     }
918     if (type == M_ERROR_TERM) {
919        _exit(1);
920     }
921 }
922
923 /*
924  * Edit a message into a Pool memory buffer, with file:lineno
925  */
926 int m_msg(char *file, int line, POOLMEM **pool_buf, char *fmt, ...)
927 {
928    va_list   arg_ptr;
929    int i, len, maxlen;
930
931    i = sprintf(*pool_buf, "%s:%d ", file, line);
932
933 again:
934    maxlen = sizeof_pool_memory(*pool_buf) - i - 1; 
935    va_start(arg_ptr, fmt);
936    len = bvsnprintf(*pool_buf+i, maxlen, fmt, arg_ptr);
937    va_end(arg_ptr);
938    if (len < 0 || len >= maxlen) {
939       *pool_buf = realloc_pool_memory(*pool_buf, maxlen + i + 200);
940       goto again;
941    }
942    return len;
943 }
944
945 /*
946  * Edit a message into a Pool Memory buffer NO file:lineno
947  *  Returns: string length of what was edited.
948  */
949 int Mmsg(POOLMEM **pool_buf, char *fmt, ...)
950 {
951    va_list   arg_ptr;
952    int len, maxlen;
953
954 again:
955    maxlen = sizeof_pool_memory(*pool_buf) - 1; 
956    va_start(arg_ptr, fmt);
957    len = bvsnprintf(*pool_buf, maxlen, fmt, arg_ptr);
958    va_end(arg_ptr);
959    if (len < 0 || len >= maxlen) {
960       *pool_buf = realloc_pool_memory(*pool_buf, maxlen + 200);
961       goto again;
962    }
963    return len;
964 }
965
966
967 /*
968  * If we come here, prefix the message with the file:line-number,
969  *  then pass it on to the normal Jmsg routine.
970  */
971 void j_msg(char *file, int line, void *jcr, int type, int level, char *fmt,...)
972 {
973    va_list   arg_ptr;
974    int i, len, maxlen;
975    POOLMEM *pool_buf;
976
977    pool_buf = get_pool_memory(PM_EMSG);
978    i = sprintf(pool_buf, "%s:%d ", file, line);
979
980 again:
981    maxlen = sizeof_pool_memory(pool_buf) - i - 1; 
982    va_start(arg_ptr, fmt);
983    len = bvsnprintf(pool_buf+i, maxlen, fmt, arg_ptr);
984    va_end(arg_ptr);
985    if (len < 0 || len >= maxlen) {
986       pool_buf = realloc_pool_memory(pool_buf, maxlen + i + 200);
987       goto again;
988    }
989
990    Jmsg(jcr, type, level, "%s", pool_buf);
991    free_memory(pool_buf);
992 }