2 * Bacula message handling routines
4 * Kern Sibbald, April 2000
11 Copyright (C) 2000, 2001, 2002 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 debug_level = 5; /* debug level */
38 time_t daemon_start_time = 0; /* Daemon start time */
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;
48 /* Forward referenced functions */
50 /* Imported functions */
55 static MSGS *daemon_msgs; /* global messages */
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.
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.
67 #define BTRACE_EXTRA 20
68 void my_name_is(int argc, char *argv[], char *name)
71 char cpath[400], npath[400];
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++) {
80 l = p; /* set pos of last slash */
88 /* On Windows allow c: junk */
98 exename = (char *)malloc(len);
104 exepath = (char *)malloc(strlen(argv[0]) + 1 + len);
105 for (p=argv[0],q=exepath; p < l; ) {
109 Dmsg1(200, "exepath=%s\n", exepath);
110 if (strchr(exepath, '.') || exepath[0] != '/') {
112 if (getcwd(cpath, sizeof(cpath))) {
113 if (chdir(exepath) == 0) {
114 if (!getcwd(npath, sizeof(npath))) {
121 exepath = (char *)malloc(strlen(npath) + 1 + len);
122 strcpy(exepath, npath);
125 Dmsg1(200, "Normalized exepath=%s\n", exepath);
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.
135 * NULL for jcr -> initialize global messages for daemon
136 * non-NULL -> initialize jcr using Message resource
139 init_msg(void *vjcr, MSGS *msg)
141 DEST *d, *dnew, *temp_chain = NULL;
142 JCR *jcr = (JCR *)vjcr;
145 * If msg is NULL, initialize global chain for STDOUT and syslog
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);
155 Dmsg1(050, "Create daemon global message resource 0x%x\n", daemon_msgs);
160 * Walk down the message resource chain duplicating it
161 * for the current Job. ****FIXME***** segfault on memcpy
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;
168 dnew->mail_filename = NULL;
170 dnew->mail_cmd = bstrdup(d->mail_cmd);
173 dnew->where = bstrdup(d->where);
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));
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));
189 Dmsg2(050, "Copy message resource 0x%x to 0x%x\n", msg, temp_chain);
192 /* Initialize so that the console (User Agent) can
193 * receive messages -- stored in a file.
195 void init_console_msg(char *wd)
199 sprintf(con_fname, "%s/%s.conmsg", wd, my_name);
200 fd = open(con_fname, O_CREAT|O_RDWR|O_BINARY, 0600);
202 Emsg2(M_ERROR_TERM, 0, _("Could not open console message file %s: ERR=%s\n"),
203 con_fname, strerror(errno));
205 if (lseek(fd, 0, SEEK_END) > 0) {
206 console_msg_pending = 1;
209 con_fd = fopen(con_fname, "a+");
211 Emsg2(M_ERROR, 0, _("Could not open console message file %s: ERR=%s\n"),
212 con_fname, strerror(errno));
214 if (rwl_init(&con_lock) != 0) {
215 Emsg1(M_ERROR_TERM, 0, _("Could not get con mutex: ERR=%s\n"),
221 * Called only during parsing of the config file.
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, ...
229 void add_msg_dest(MSGS *msg, int dest_code, int msg_type, char *where, char *mail_cmd)
233 * First search the existing chain and see if we
234 * can simply add this msg_type to an existing entry.
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 */
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 */
254 d->where = bstrdup(where);
257 d->mail_cmd = bstrdup(mail_cmd);
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));
265 * Called only during parsing of the config file.
267 * Remove a message destination
269 void rem_msg_dest(MSGS *msg, int dest_code, int msg_type, char *where)
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");
291 * Edit job codes into main command line
294 * %t = Job type (Backup, ...)
300 * %d = Director's name
302 * omsg = edited output message
303 * imsg = input string containing edit codes (%x)
304 * to = recepients list
307 static char *edit_job_codes(JCR *jcr, char *omsg, char *imsg, char *to)
313 Dmsg1(200, "edit_job_codes: %s\n", imsg);
314 for (p=imsg; *p; p++) {
321 str = jcr->client_name;
327 str = my_name; /* Director's name */
330 str = job_status_to_str(jcr->JobStatus);
333 sprintf(add, "%d", jcr->JobId);
336 case 'j': /* Job name */
340 str = job_level_to_str(jcr->JobLevel);
346 str = job_type_to_str(jcr->JobType);
360 Dmsg1(200, "add_str %s\n", str);
361 pm_strcat(&omsg, str);
362 Dmsg1(200, "omsg=%s\n", omsg);
367 static void make_unique_spool_filename(JCR *jcr, POOLMEM **name, int fd)
369 Mmsg(name, "%s/%s.spool.%s.%d", working_directory, my_name,
373 int open_spool_file(void *vjcr, BSOCK *bs)
375 POOLMEM *name = get_pool_memory(PM_MESSAGE);
376 JCR *jcr = (JCR *)vjcr;
378 make_unique_spool_filename(jcr, &name, bs->fd);
379 bs->spool_fd = fopen(name, "w+");
381 Jmsg(jcr, M_ERROR, 0, "fopen spool file %s failed: ERR=%s\n", name, strerror(errno));
382 free_pool_memory(name);
385 free_pool_memory(name);
389 int close_spool_file(void *vjcr, BSOCK *bs)
391 POOLMEM *name = get_pool_memory(PM_MESSAGE);
392 JCR *jcr = (JCR *)vjcr;
394 make_unique_spool_filename(jcr, &name, bs->fd);
395 fclose(bs->spool_fd);
397 free_pool_memory(name);
405 * Create a unique filename for the mail command
407 static void make_unique_mail_filename(JCR *jcr, POOLMEM **name, DEST *d)
410 Mmsg(name, "%s/%s.mail.%s.%d", working_directory, my_name,
413 Mmsg(name, "%s/%s.mail.%s.%d", working_directory, my_name,
416 Dmsg1(200, "mailname=%s\n", *name);
422 static FILE *open_mail_pipe(JCR *jcr, POOLMEM **cmd, DEST *d)
426 if (d->mail_cmd && jcr) {
427 *cmd = edit_job_codes(jcr, *cmd, d->mail_cmd, d->where);
429 Mmsg(cmd, "mail -s \"Bacula Message\" %s", d->where);
431 Dmsg1(200, "mailcmd=%s\n", cmd);
432 pfd = popen(*cmd, "w");
434 Jmsg(jcr, M_ERROR, 0, "mail popen %s failed: ERR=%s\n", cmd, strerror(errno));
440 * Close the messages for this Messages resource, which means to close
441 * any open files, and dispatch any pending email messages.
443 void close_msg(void *vjcr)
446 JCR *jcr = (JCR *)vjcr;
452 Dmsg1(050, "Close_msg jcr=0x%x\n", jcr);
454 if (jcr == NULL) { /* NULL -> global chain */
458 msgs = jcr->jcr_msgs;
459 jcr->jcr_msgs = NULL;
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; ) {
468 switch (d->dest_code) {
472 fclose(d->fd); /* close open file descriptor */
476 case MD_MAIL_ON_ERROR:
480 if (d->dest_code == MD_MAIL_ON_ERROR && jcr &&
481 jcr->JobStatus == JS_Terminated) {
485 pfd = open_mail_pipe(jcr, &cmd, d);
490 line = get_memory(len);
492 while (fgets(line, len, d->fd)) {
495 stat = pclose(pfd); /* close pipe, sending mail */
497 * Since we are closing all messages, before "recursing"
498 * make sure we are not closing the daemon messages, otherwise
501 if (stat < 0 && msgs != daemon_msgs) {
502 Emsg0(M_ERROR, 0, _("Mail program terminated in error.\n"));
506 /* Remove temp file */
508 unlink(d->mail_filename);
509 free_pool_memory(d->mail_filename);
510 d->mail_filename = NULL;
517 d = d->next; /* point to next buffer */
519 free_pool_memory(cmd);
526 * Free memory associated with Messages resource
528 void free_msgs_res(MSGS *msgs)
532 for (d=msgs->dest_chain; d; ) {
539 old = d; /* save pointer to release */
540 d = d->next; /* point to next buffer */
541 free(old); /* free the destination item */
543 msgs->dest_chain = NULL;
549 * Terminate the message handler for good.
550 * Release the global destination chain.
552 * Also, clean up a few other items (cons, exepath). Note,
553 * these really should be done elsewhere.
557 Dmsg0(100, "Enter term_msg\n");
558 close_msg(NULL); /* close global chain */
578 * Handle sending the message to the appropriate place
580 void dispatch_message(void *vjcr, int type, int level, char *msg)
585 JCR *jcr = (JCR *) vjcr;
589 Dmsg2(200, "Enter dispatch_msg type=%d msg=%s\n", type, msg);
591 if (type == M_ABORT || type == M_ERROR_TERM) {
592 fprintf(stdout, msg); /* print this here to INSURE that it is printed */
595 /* Now figure out where to send the message */
598 msgs = jcr->jcr_msgs;
603 for (d=msgs->dest_chain; d; d=d->next) {
604 if (bit_is_set(type, d->msg_types)) {
605 switch (d->dest_code) {
607 Dmsg1(200, "CONSOLE for following err: %s\n", msg);
609 con_fd = fopen(con_fname, "a+");
610 Dmsg0(200, "Console file not open.\n");
613 Pw(con_lock); /* get write lock on console message file */
615 bstrftime(cmd, sizeof(cmd), time(NULL));
618 fwrite(cmd, len, 1, con_fd);
620 if (len > 0 && msg[len-1] != '\n') {
624 fwrite(msg, len, 1, con_fd);
626 console_msg_pending = TRUE;
631 Dmsg1(200, "SYSLOG for following err: %s\n", msg);
632 /* We really should do an openlog() here */
633 syslog(LOG_DAEMON|LOG_ERR, msg);
636 Dmsg1(200, "OPERATOR for following err: %s\n", msg);
637 mcmd = get_pool_memory(PM_MESSAGE);
638 d->fd = open_mail_pipe(jcr, &mcmd, d);
639 free_pool_memory(mcmd);
643 /* Messages to the operator go one at a time */
644 stat = pclose(d->fd);
647 Emsg0(M_ERROR, 0, _("Operator mail program terminated in error.\n"));
652 case MD_MAIL_ON_ERROR:
653 Dmsg1(200, "MAIL for following err: %s\n", msg);
655 POOLMEM *name = get_pool_memory(PM_MESSAGE);
656 make_unique_mail_filename(jcr, &name, d);
657 d->fd = fopen(name, "w+");
660 Emsg2(M_ERROR, 0, "fopen %s failed: ERR=%s\n", name, strerror(errno));
662 free_pool_memory(name);
665 d->mail_filename = name;
668 if (len > d->max_len) {
669 d->max_len = len; /* keep max line length */
674 Dmsg1(200, "FILE for following err: %s\n", msg);
676 d->fd = fopen(d->where, "w+");
679 Emsg2(M_ERROR, 0, "fopen %s failed: ERR=%s\n", d->where, strerror(errno));
687 Dmsg1(200, "APPEND for following err: %s\n", msg);
689 d->fd = fopen(d->where, "a");
692 Emsg2(M_ERROR, 0, "fopen %s failed: ERR=%s\n", d->where, strerror(errno));
700 Dmsg1(200, "DIRECTOR for following err: %s\n", msg);
701 if (jcr && jcr->dir_bsock && !jcr->dir_bsock->errors) {
703 jcr->dir_bsock->msglen = Mmsg(&(jcr->dir_bsock->msg),
704 "Jmsg Job=%s type=%d level=%d %s", jcr->Job,
705 type, level, msg) + 1;
706 bnet_send(jcr->dir_bsock);
710 Dmsg1(200, "STDOUT for following err: %s\n", msg);
711 if (type != M_ABORT && type != M_ERROR_TERM) /* already printed */
712 fprintf(stdout, msg);
715 Dmsg1(200, "STDERR for following err: %s\n", msg);
716 fprintf(stderr, msg);
726 /*********************************************************************
728 * subroutine prints a debug message if the level number
729 * is less than or equal the debug_level. File and line numbers
730 * are included for more detail if desired, but not currently
733 * If the level is negative, the details of file and line number
737 d_msg(char *file, int line, int level, char *fmt,...)
749 if (level <= debug_level) {
752 sprintf(buf, "%s: %s:%d ", my_name, file, line);
760 va_start(arg_ptr, fmt);
761 bvsnprintf(buf+i, sizeof(buf)-i, (char *)fmt, arg_ptr);
764 fprintf(stdout, buf);
769 /* *********************************************************
771 * print an error message
775 e_msg(char *file, int line, int type, int level, char *fmt,...)
782 * Check if we have a message destination defined.
783 * We always report M_ABORT and M_ERROR_TERM
785 if (!daemon_msgs || ((type != M_ABORT && type != M_ERROR_TERM) &&
786 !bit_is_set(type, daemon_msgs->send_msg)))
787 return; /* no destination */
790 sprintf(buf, "%s ABORTING due to ERROR in %s:%d\n",
791 my_name, file, line);
794 sprintf(buf, "%s ERROR TERMINATION at %s:%d\n",
795 my_name, file, line);
798 if (level == -1) /* skip details */
799 sprintf(buf, "%s: Fatal Error because: ", my_name);
801 sprintf(buf, "%s: Fatal Error at %s:%d because:\n", my_name, file, line);
804 if (level == -1) /* skip details */
805 sprintf(buf, "%s: Error: ", my_name);
807 sprintf(buf, "%s: Error in %s:%d ", my_name, file, line);
810 sprintf(buf, "%s: Warning: ", my_name);
813 sprintf(buf, "%s: ", my_name);
818 va_start(arg_ptr, fmt);
819 bvsnprintf(buf+i, sizeof(buf)-i, (char *)fmt, arg_ptr);
822 dispatch_message(NULL, type, level, buf);
824 if (type == M_ABORT) {
826 p[0] = 0; /* generate segmentation violation */
828 if (type == M_ERROR_TERM) {
833 /* *********************************************************
835 * Generate a Job message
839 Jmsg(void *vjcr, int type, int level, char *fmt,...)
845 JCR *jcr = (JCR *)vjcr;
850 Dmsg1(200, "Enter Jmsg type=%d\n", type);
855 msgs = jcr->jcr_msgs;
865 buf = rbuf; /* we are the Director */
867 * Check if we have a message destination defined.
868 * We always report M_ABORT and M_ERROR_TERM
870 if ((type != M_ABORT && type != M_ERROR_TERM) && msgs && !bit_is_set(type, msgs->send_msg)) {
871 Dmsg1(200, "No bit set for type %d\n", type);
872 return; /* no destination */
876 sprintf(buf, "%s ABORTING due to ERROR\n", my_name);
879 sprintf(buf, "%s ERROR TERMINATION\n", my_name);
882 sprintf(buf, "%s: Job %s Fatal error: ", my_name, job);
884 jcr->JobStatus = JS_FatalError;
888 sprintf(buf, "%s: Job %s Error: ", my_name, job);
894 sprintf(buf, "%s: Job %s Warning: ", my_name, job);
897 sprintf(buf, "%s: ", my_name);
902 va_start(arg_ptr, fmt);
903 len = bvsnprintf(buf+i, sizeof(rbuf)-i, fmt, arg_ptr);
906 dispatch_message(jcr, type, level, rbuf);
908 Dmsg3(500, "i=%d sizeof(rbuf)-i=%d len=%d\n", i, sizeof(rbuf)-i, len);
910 if (type == M_ABORT){
912 p[0] = 0; /* generate segmentation violation */
914 if (type == M_ERROR_TERM) {
920 * Edit a message into a Pool memory buffer, with file:lineno
922 int m_msg(char *file, int line, POOLMEM **pool_buf, char *fmt, ...)
927 sprintf(*pool_buf, "%s:%d ", file, line);
928 i = strlen(*pool_buf);
931 maxlen = sizeof_pool_memory(*pool_buf) - i - 1;
932 va_start(arg_ptr, fmt);
933 len = bvsnprintf(*pool_buf+i, maxlen, fmt, arg_ptr);
935 if (len < 0 || len >= maxlen) {
936 *pool_buf = realloc_pool_memory(*pool_buf, maxlen + i + 200);
943 * Edit a message into a Pool Memory buffer NO file:lineno
944 * Returns: string length of what was edited.
946 int Mmsg(POOLMEM **pool_buf, char *fmt, ...)
952 maxlen = sizeof_pool_memory(*pool_buf) - 1;
953 va_start(arg_ptr, fmt);
954 len = bvsnprintf(*pool_buf, maxlen, fmt, arg_ptr);
956 if (len < 0 || len >= maxlen) {
957 *pool_buf = realloc_pool_memory(*pool_buf, maxlen + 200);
965 * If we come here, prefix the message with the file:line-number,
966 * then pass it on to the normal Jmsg routine.
968 void j_msg(char *file, int line, void *jcr, int type, int level, char *fmt,...)
974 pool_buf = get_pool_memory(PM_EMSG);
975 sprintf(pool_buf, "%s:%d ", file, line);
976 i = strlen(pool_buf);
979 maxlen = sizeof_pool_memory(pool_buf) - i - 1;
980 va_start(arg_ptr, fmt);
981 len = bvsnprintf(pool_buf+i, maxlen, fmt, arg_ptr);
983 if (len < 0 || len >= maxlen) {
984 pool_buf = realloc_pool_memory(pool_buf, maxlen + i + 200);
988 Jmsg(jcr, type, level, pool_buf);
989 free_memory(pool_buf);