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