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