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