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