2 * Bacula message handling routines
4 * Kern Sibbald, April 2000
11 Copyright (C) 2000-2003 Kern Sibbald and John Walker
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.
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.
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,
34 #define FULL_LOCATION 1 /* set for file:line in Debug messages */
36 char *working_directory = NULL; /* working directory path stored here */
37 int verbose = 0; /* increase User messages */
38 int debug_level = 0; /* debug level */
39 time_t daemon_start_time = 0; /* Daemon start time */
41 char my_name[20]; /* daemon name is stored here */
42 char *exepath = (char *)NULL;
43 char *exename = (char *)NULL;
44 int console_msg_pending = 0;
45 char con_fname[500]; /* Console filename */
46 FILE *con_fd = NULL; /* Console file descriptor */
47 brwlock_t con_lock; /* Console lock structure */
50 FILE *trace_fd = NULL;
53 /* Forward referenced functions */
55 /* Imported functions */
60 static MSGS *daemon_msgs; /* global messages */
63 * Set daemon name. Also, find canonical execution
64 * path. Note, exepath has spare room for tacking on
65 * the exename so that we can reconstruct the full name.
67 * Note, this routine can get called multiple times
68 * The second time is to put the name as found in the
69 * Resource record. On the second call, generally,
70 * argv is NULL to avoid doing the path code twice.
72 #define BTRACE_EXTRA 20
73 void my_name_is(int argc, char *argv[], char *name)
76 char cpath[400], npath[400];
79 bstrncpy(my_name, name, sizeof(my_name));
80 if (argc>0 && argv && argv[0]) {
81 /* strip trailing filename and save exepath */
82 for (l=p=argv[0]; *p; p++) {
84 l = p; /* set pos of last slash */
92 /* On Windows allow c: junk */
102 exename = (char *)malloc(len);
108 exepath = (char *)malloc(strlen(argv[0]) + 1 + len);
109 for (p=argv[0],q=exepath; p < l; ) {
113 Dmsg1(200, "exepath=%s\n", exepath);
114 if (strchr(exepath, '.') || exepath[0] != '/') {
116 if (getcwd(cpath, sizeof(cpath))) {
117 if (chdir(exepath) == 0) {
118 if (!getcwd(npath, sizeof(npath))) {
125 exepath = (char *)malloc(strlen(npath) + 1 + len);
126 strcpy(exepath, npath);
129 Dmsg1(200, "Normalized exepath=%s\n", exepath);
135 * Initialize message handler for a daemon or a Job
136 * We make a copy of the MSGS resource passed, so it belows
137 * to the job or daemon and thus can be modified.
139 * NULL for jcr -> initialize global messages for daemon
140 * non-NULL -> initialize jcr using Message resource
143 init_msg(void *vjcr, MSGS *msg)
145 DEST *d, *dnew, *temp_chain = NULL;
146 JCR *jcr = (JCR *)vjcr;
149 * If msg is NULL, initialize global chain for STDOUT and syslog
153 daemon_msgs = (MSGS *)malloc(sizeof(MSGS));
154 memset(daemon_msgs, 0, sizeof(MSGS));
155 for (i=1; i<=M_MAX; i++) {
156 add_msg_dest(daemon_msgs, MD_STDOUT, i, NULL, NULL);
157 add_msg_dest(daemon_msgs, MD_SYSLOG, i, NULL, NULL);
159 Dmsg1(050, "Create daemon global message resource 0x%x\n", daemon_msgs);
164 * Walk down the message resource chain duplicating it
165 * for the current Job.
167 for (d=msg->dest_chain; d; d=d->next) {
168 dnew = (DEST *)malloc(sizeof(DEST));
169 memcpy(dnew, d, sizeof(DEST));
170 dnew->next = temp_chain;
172 dnew->mail_filename = NULL;
174 dnew->mail_cmd = bstrdup(d->mail_cmd);
177 dnew->where = bstrdup(d->where);
183 jcr->jcr_msgs = (MSGS *)malloc(sizeof(MSGS));
184 memset(jcr->jcr_msgs, 0, sizeof(MSGS));
185 jcr->jcr_msgs->dest_chain = temp_chain;
186 memcpy(jcr->jcr_msgs->send_msg, msg->send_msg, sizeof(msg->send_msg));
188 /* If we have default values, release them now */
190 free_msgs_res(daemon_msgs);
192 daemon_msgs = (MSGS *)malloc(sizeof(MSGS));
193 memset(daemon_msgs, 0, sizeof(MSGS));
194 daemon_msgs->dest_chain = temp_chain;
195 memcpy(daemon_msgs->send_msg, msg->send_msg, sizeof(msg->send_msg));
197 Dmsg2(050, "Copy message resource 0x%x to 0x%x\n", msg, temp_chain);
201 /* Initialize so that the console (User Agent) can
202 * receive messages -- stored in a file.
204 void init_console_msg(char *wd)
208 bsnprintf(con_fname, sizeof(con_fname), "%s/%s.conmsg", wd, my_name);
209 fd = open(con_fname, O_CREAT|O_RDWR|O_BINARY, 0600);
211 Emsg2(M_ERROR_TERM, 0, _("Could not open console message file %s: ERR=%s\n"),
212 con_fname, strerror(errno));
214 if (lseek(fd, 0, SEEK_END) > 0) {
215 console_msg_pending = 1;
218 con_fd = fopen(con_fname, "a+");
220 Emsg2(M_ERROR, 0, _("Could not open console message file %s: ERR=%s\n"),
221 con_fname, strerror(errno));
223 if (rwl_init(&con_lock) != 0) {
224 Emsg1(M_ERROR_TERM, 0, _("Could not get con mutex: ERR=%s\n"),
230 * Called only during parsing of the config file.
232 * Add a message destination. I.e. associate a message type with
233 * a destination (code).
234 * Note, where in the case of dest_code FILE is a filename,
235 * but in the case of MAIL is a space separated list of
236 * email addresses, ...
238 void add_msg_dest(MSGS *msg, int dest_code, int msg_type, char *where, char *mail_cmd)
242 * First search the existing chain and see if we
243 * can simply add this msg_type to an existing entry.
245 for (d=msg->dest_chain; d; d=d->next) {
246 if (dest_code == d->dest_code && ((where == NULL && d->where == NULL) ||
247 (strcmp(where, d->where) == 0))) {
248 Dmsg4(200, "Add to existing d=%x msgtype=%d destcode=%d where=%s\n",
249 d, msg_type, dest_code, NPRT(where));
250 set_bit(msg_type, d->msg_types);
251 set_bit(msg_type, msg->send_msg); /* set msg_type bit in our local */
255 /* Not found, create a new entry */
256 d = (DEST *)malloc(sizeof(DEST));
257 memset(d, 0, sizeof(DEST));
258 d->next = msg->dest_chain;
259 d->dest_code = dest_code;
260 set_bit(msg_type, d->msg_types); /* set type bit in structure */
261 set_bit(msg_type, msg->send_msg); /* set type bit in our local */
263 d->where = bstrdup(where);
266 d->mail_cmd = bstrdup(mail_cmd);
268 Dmsg5(200, "add new d=%x msgtype=%d destcode=%d where=%s mailcmd=%s\n",
269 d, msg_type, dest_code, NPRT(where), NPRT(d->mail_cmd));
274 * Called only during parsing of the config file.
276 * Remove a message destination
278 void rem_msg_dest(MSGS *msg, int dest_code, int msg_type, char *where)
282 for (d=msg->dest_chain; d; d=d->next) {
283 Dmsg2(200, "Remove_msg_dest d=%x where=%s\n", d, NPRT(d->where));
284 if (bit_is_set(msg_type, d->msg_types) && (dest_code == d->dest_code) &&
285 ((where == NULL && d->where == NULL) ||
286 (strcmp(where, d->where) == 0))) {
287 Dmsg3(200, "Found for remove d=%x msgtype=%d destcode=%d\n",
288 d, msg_type, dest_code);
289 clear_bit(msg_type, d->msg_types);
290 Dmsg0(200, "Return rem_msg_dest\n");
296 static void make_unique_spool_filename(JCR *jcr, POOLMEM **name, int fd)
298 Mmsg(name, "%s/%s.spool.%s.%d", working_directory, my_name,
302 int open_spool_file(void *vjcr, BSOCK *bs)
304 POOLMEM *name = get_pool_memory(PM_MESSAGE);
305 JCR *jcr = (JCR *)vjcr;
307 make_unique_spool_filename(jcr, &name, bs->fd);
308 bs->spool_fd = fopen(name, "w+");
310 Jmsg(jcr, M_ERROR, 0, "fopen spool file %s failed: ERR=%s\n", name, strerror(errno));
311 free_pool_memory(name);
314 free_pool_memory(name);
318 int close_spool_file(void *vjcr, BSOCK *bs)
320 POOLMEM *name = get_pool_memory(PM_MESSAGE);
321 JCR *jcr = (JCR *)vjcr;
323 make_unique_spool_filename(jcr, &name, bs->fd);
324 fclose(bs->spool_fd);
326 free_pool_memory(name);
333 * Create a unique filename for the mail command
335 static void make_unique_mail_filename(JCR *jcr, POOLMEM **name, DEST *d)
338 Mmsg(name, "%s/%s.mail.%s.%d", working_directory, my_name,
341 Mmsg(name, "%s/%s.mail.%s.%d", working_directory, my_name,
344 Dmsg1(200, "mailname=%s\n", *name);
350 static BPIPE *open_mail_pipe(JCR *jcr, POOLMEM **cmd, DEST *d)
354 if (d->mail_cmd && jcr) {
355 *cmd = edit_job_codes(jcr, *cmd, d->mail_cmd, d->where);
357 Mmsg(cmd, "mail -s \"Bacula Message\" %s", d->where);
361 if (!(bpipe = open_bpipe(*cmd, 120, "rw"))) {
362 Jmsg(jcr, M_ERROR, 0, "open mail pipe %s failed: ERR=%s\n", *cmd, strerror(errno));
368 * Close the messages for this Messages resource, which means to close
369 * any open files, and dispatch any pending email messages.
371 void close_msg(void *vjcr)
374 JCR *jcr = (JCR *)vjcr;
380 Dmsg1(050, "Close_msg jcr=0x%x\n", jcr);
382 if (jcr == NULL) { /* NULL -> global chain */
385 msgs = jcr->jcr_msgs;
386 jcr->jcr_msgs = NULL;
391 Dmsg1(150, "===Begin close msg resource at 0x%x\n", msgs);
392 cmd = get_pool_memory(PM_MESSAGE);
393 for (d=msgs->dest_chain; d; ) {
395 switch (d->dest_code) {
399 fclose(d->fd); /* close open file descriptor */
403 case MD_MAIL_ON_ERROR:
404 Dmsg0(150, "Got MD_MAIL or MD_MAIL_ON_ERROR\n");
408 if (d->dest_code == MD_MAIL_ON_ERROR && jcr &&
409 jcr->JobStatus == JS_Terminated) {
413 if (!(bpipe=open_mail_pipe(jcr, &cmd, d))) {
414 Dmsg0(000, "open mail pipe failed.\n");
417 Dmsg0(150, "Opened mail pipe\n");
419 line = get_memory(len);
421 while (fgets(line, len, d->fd)) {
422 fputs(line, bpipe->wfd);
424 if (!close_wpipe(bpipe)) { /* close write pipe sending mail */
425 Dmsg1(000, "close error: ERR=%s\n", strerror(errno));
429 * Since we are closing all messages, before "recursing"
430 * make sure we are not closing the daemon messages, otherwise
433 if (msgs != daemon_msgs) {
434 /* read what mail prog returned -- should be nothing */
435 while (fgets(line, len, bpipe->rfd)) {
436 Jmsg1(jcr, M_INFO, 0, _("Mail prog: %s"), line);
440 stat = close_bpipe(bpipe);
441 if (stat != 0 && msgs != daemon_msgs) {
442 Dmsg1(150, "Calling emsg. CMD=%s\n", cmd);
443 Jmsg2(jcr, M_ERROR, 0, _("Mail program terminated in error. stat=%d\n"
444 "CMD=%s\n"), stat, cmd);
448 /* Remove temp file */
450 unlink(d->mail_filename);
451 free_pool_memory(d->mail_filename);
452 d->mail_filename = NULL;
453 Dmsg0(150, "end mail or mail on error\n");
460 d = d->next; /* point to next buffer */
462 free_pool_memory(cmd);
463 Dmsg0(150, "Done walking message chain.\n");
468 Dmsg0(150, "===End close msg resource\n");
472 * Free memory associated with Messages resource
474 void free_msgs_res(MSGS *msgs)
478 /* Walk down the message chain releasing allocated buffers */
479 for (d=msgs->dest_chain; d; ) {
486 old = d; /* save pointer to release */
487 d = d->next; /* point to next buffer */
488 free(old); /* free the destination item */
490 msgs->dest_chain = NULL;
491 free(msgs); /* free the head */
496 * Terminate the message handler for good.
497 * Release the global destination chain.
499 * Also, clean up a few other items (cons, exepath). Note,
500 * these really should be done elsewhere.
504 Dmsg0(100, "Enter term_msg\n");
505 close_msg(NULL); /* close global chain */
506 free_msgs_res(daemon_msgs); /* free the resources */
530 * Handle sending the message to the appropriate place
532 void dispatch_message(void *vjcr, int type, int level, char *msg)
535 char dt[MAX_TIME_LENGTH];
537 JCR *jcr = (JCR *) vjcr;
542 Dmsg2(200, "Enter dispatch_msg type=%d msg=%s\n", type, msg);
544 if (type == M_ABORT || type == M_ERROR_TERM) {
545 fputs(msg, stdout); /* print this here to INSURE that it is printed */
548 /* Now figure out where to send the message */
551 msgs = jcr->jcr_msgs;
556 for (d=msgs->dest_chain; d; d=d->next) {
557 if (bit_is_set(type, d->msg_types)) {
558 switch (d->dest_code) {
560 Dmsg1(400, "CONSOLE for following msg: %s", msg);
562 con_fd = fopen(con_fname, "a+");
563 Dmsg0(200, "Console file not open.\n");
566 Pw(con_lock); /* get write lock on console message file */
568 bstrftime(dt, sizeof(dt), time(NULL));
571 fwrite(dt, len, 1, con_fd);
574 fwrite(msg, len, 1, con_fd);
575 if (msg[len-1] != '\n') {
576 fwrite("\n", 2, 1, con_fd);
579 fwrite("\n", 2, 1, con_fd);
582 console_msg_pending = TRUE;
587 Dmsg1(400, "SYSLOG for collowing msg: %s\n", msg);
589 * We really should do an openlog() here.
591 syslog(LOG_DAEMON|LOG_ERR, "%s", msg);
594 Dmsg1(400, "OPERATOR for collowing msg: %s\n", msg);
595 mcmd = get_pool_memory(PM_MESSAGE);
596 if ((bpipe=open_mail_pipe(jcr, &mcmd, d))) {
598 fputs(msg, bpipe->wfd);
599 /* Messages to the operator go one at a time */
600 stat = close_bpipe(bpipe);
602 Emsg1(M_ERROR, 0, _("Operator mail program terminated in error.\nCMD=%s\n"),
606 free_pool_memory(mcmd);
609 case MD_MAIL_ON_ERROR:
610 Dmsg1(400, "MAIL for following msg: %s", msg);
612 POOLMEM *name = get_pool_memory(PM_MESSAGE);
613 make_unique_mail_filename(jcr, &name, d);
614 d->fd = fopen(name, "w+");
617 Emsg2(M_ERROR, 0, "fopen %s failed: ERR=%s\n", name, strerror(errno));
619 free_pool_memory(name);
622 d->mail_filename = name;
625 if (len > d->max_len) {
626 d->max_len = len; /* keep max line length */
631 Dmsg1(400, "FILE for following msg: %s", msg);
633 d->fd = fopen(d->where, "w+");
636 Emsg2(M_ERROR, 0, "fopen %s failed: ERR=%s\n", d->where, strerror(errno));
644 Dmsg1(400, "APPEND for following msg: %s", msg);
646 d->fd = fopen(d->where, "a");
649 Emsg2(M_ERROR, 0, "fopen %s failed: ERR=%s\n", d->where, strerror(errno));
657 Dmsg1(400, "DIRECTOR for following msg: %s", msg);
658 if (jcr && jcr->dir_bsock && !jcr->dir_bsock->errors) {
660 jcr->dir_bsock->msglen = Mmsg(&(jcr->dir_bsock->msg),
661 "Jmsg Job=%s type=%d level=%d %s", jcr->Job,
662 type, level, msg) + 1;
663 bnet_send(jcr->dir_bsock);
667 Dmsg1(400, "STDOUT for following msg: %s", msg);
668 if (type != M_ABORT && type != M_ERROR_TERM) { /* already printed */
673 Dmsg1(400, "STDERR for following msg: %s", msg);
684 /*********************************************************************
686 * subroutine prints a debug message if the level number
687 * is less than or equal the debug_level. File and line numbers
688 * are included for more detail if desired, but not currently
691 * If the level is negative, the details of file and line number
695 d_msg(char *file, int line, int level, char *fmt,...)
708 if (level <= debug_level) {
711 len= sprintf(buf, "%s: %s:%d ", my_name, file, line);
718 va_start(arg_ptr, fmt);
719 bvsnprintf(buf+len, sizeof(buf)-len, (char *)fmt, arg_ptr);
727 /*********************************************************************
729 * subroutine writes a debug message to the trace file if the level number
730 * is less than or equal the debug_level. File and line numbers
731 * are included for more detail if desired, but not currently
734 * If the level is negative, the details of file and line number
738 t_msg(char *file, int line, int level, char *fmt,...)
752 if (level <= debug_level) {
754 trace_fd = fopen("bacula.trace", "a+");
756 Emsg1(M_ABORT, 0, _("Cannot open bacula.trace: ERR=%s\n"),
763 len = sprintf(buf, "%s: %s:%d ", my_name, file, line);
770 va_start(arg_ptr, fmt);
771 bvsnprintf(buf+len, sizeof(buf)-len, (char *)fmt, arg_ptr);
774 fputs(buf, trace_fd);
781 /* *********************************************************
783 * print an error message
787 e_msg(char *file, int line, int type, int level, char *fmt,...)
794 * Check if we have a message destination defined.
795 * We always report M_ABORT and M_ERROR_TERM
797 if (!daemon_msgs || ((type != M_ABORT && type != M_ERROR_TERM) &&
798 !bit_is_set(type, daemon_msgs->send_msg))) {
799 return; /* no destination */
803 len = sprintf(buf, "%s: ABORTING due to ERROR in %s:%d\n",
804 my_name, file, line);
807 len = sprintf(buf, "%s: ERROR TERMINATION at %s:%d\n",
808 my_name, file, line);
811 if (level == -1) /* skip details */
812 len = sprintf(buf, "%s: Fatal Error because: ", my_name);
814 len = sprintf(buf, "%s: Fatal Error at %s:%d because:\n", my_name, file, line);
817 if (level == -1) /* skip details */
818 len = sprintf(buf, "%s: Error: ", my_name);
820 len = sprintf(buf, "%s: Error in %s:%d ", my_name, file, line);
823 len = sprintf(buf, "%s: Warning: ", my_name);
826 len = sprintf(buf, "%s: ", my_name);
830 va_start(arg_ptr, fmt);
831 bvsnprintf(buf+len, sizeof(buf)-len, (char *)fmt, arg_ptr);
834 dispatch_message(NULL, type, level, buf);
836 if (type == M_ABORT) {
838 p[0] = 0; /* generate segmentation violation */
840 if (type == M_ERROR_TERM) {
845 /* *********************************************************
847 * Generate a Job message
851 Jmsg(void *vjcr, int type, int level, char *fmt,...)
856 JCR *jcr = (JCR *)vjcr;
861 Dmsg1(200, "Enter Jmsg type=%d\n", type);
866 msgs = jcr->jcr_msgs;
870 msgs = daemon_msgs; /* if no jcr, we use daemon handler */
873 job = ""; /* Set null job name if none */
877 * Check if we have a message destination defined.
878 * We always report M_ABORT and M_ERROR_TERM
880 if (msgs && (type != M_ABORT && type != M_ERROR_TERM) &&
881 !bit_is_set(type, msgs->send_msg)) {
882 return; /* no destination */
886 len = sprintf(rbuf, "%s ABORTING due to ERROR\n", my_name);
889 len = sprintf(rbuf, "%s ERROR TERMINATION\n", my_name);
892 len = sprintf(rbuf, "%s: %s Fatal error: ", my_name, job);
894 set_jcr_job_status(jcr, JS_FatalError);
898 len = sprintf(rbuf, "%s: %s Error: ", my_name, job);
904 len = sprintf(rbuf, "%s: %s Warning: ", my_name, job);
907 len = sprintf(rbuf, "%s: ", my_name);
911 va_start(arg_ptr, fmt);
912 bvsnprintf(rbuf+len, sizeof(rbuf)-len, fmt, arg_ptr);
915 dispatch_message(jcr, type, level, rbuf);
917 if (type == M_ABORT){
919 p[0] = 0; /* generate segmentation violation */
921 if (type == M_ERROR_TERM) {
927 * Edit a message into a Pool memory buffer, with file:lineno
929 int m_msg(char *file, int line, POOLMEM **pool_buf, char *fmt, ...)
934 i = sprintf(*pool_buf, "%s:%d ", file, line);
937 maxlen = sizeof_pool_memory(*pool_buf) - i - 1;
938 va_start(arg_ptr, fmt);
939 len = bvsnprintf(*pool_buf+i, maxlen, fmt, arg_ptr);
941 if (len < 0 || len >= maxlen) {
942 *pool_buf = realloc_pool_memory(*pool_buf, maxlen + i + 200);
949 * Edit a message into a Pool Memory buffer NO file:lineno
950 * Returns: string length of what was edited.
952 int Mmsg(POOLMEM **pool_buf, char *fmt, ...)
958 maxlen = sizeof_pool_memory(*pool_buf) - 1;
959 va_start(arg_ptr, fmt);
960 len = bvsnprintf(*pool_buf, maxlen, fmt, arg_ptr);
962 if (len < 0 || len >= maxlen) {
963 *pool_buf = realloc_pool_memory(*pool_buf, maxlen + 200);
971 * If we come here, prefix the message with the file:line-number,
972 * then pass it on to the normal Jmsg routine.
974 void j_msg(char *file, int line, void *jcr, int type, int level, char *fmt,...)
980 pool_buf = get_pool_memory(PM_EMSG);
981 i = sprintf(pool_buf, "%s:%d ", file, line);
984 maxlen = sizeof_pool_memory(pool_buf) - i - 1;
985 va_start(arg_ptr, fmt);
986 len = bvsnprintf(pool_buf+i, maxlen, fmt, arg_ptr);
988 if (len < 0 || len >= maxlen) {
989 pool_buf = realloc_pool_memory(pool_buf, maxlen + i + 200);
993 Jmsg(jcr, type, level, "%s", pool_buf);
994 free_memory(pool_buf);