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