2 * Bacula message handling routines
4 * Kern Sibbald, April 2000
9 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2 of
14 the License, or (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public
22 License along with this program; if not, write to the Free
23 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
32 #define FULL_LOCATION 1 /* set for file:line in Debug messages */
34 char *working_directory = NULL; /* working directory path stored here */
35 int debug_level = 5; /* debug level */
36 time_t daemon_start_time = 0; /* Daemon start time */
38 char my_name[20]; /* daemon name is stored here */
39 char *exepath = (char *)NULL;
40 char *exename = (char *)NULL;
41 int console_msg_pending = 0;
45 /* Forward referenced functions */
47 /* Imported functions */
49 /* This chain contains all the possible destinations */
50 DEST *dest_chain = NULL;
52 * send_msg has a bit set for each type that has a
53 * message destination. The info in send_msg[] is
54 * contained in the dest structures,
55 * but we keep it here for speed so that we don't have to
56 * search all the structures in all the cases.
58 char send_msg[nbytes_for_bits(M_MAX+1)];
61 * Set daemon name. Also, find canonical execution
62 * path. Note, exepath has spare room for tacking on
63 * the exename so that we can reconstruct the full name.
65 * Note, this routine can get called multiple times
66 * The second time is to put the name as found in the
67 * Resource record. On the second call, generally,
68 * argv is NULL to avoid doing the path code twice.
70 #define BTRACE_EXTRA 20
71 void my_name_is(int argc, char *argv[], char *name)
74 char cpath[400], npath[400];
77 strncpy(my_name, name, sizeof(my_name));
78 my_name[sizeof(my_name)-1] = 0;
79 if (argc>0 && argv && argv[0]) {
80 /* strip trailing filename and save exepath */
81 for (l=p=argv[0]; *p; p++) {
83 l = p; /* set pos of last slash */
91 /* On Windows allow c: junk */
101 exename = (char *)malloc(len);
106 exepath = (char *)malloc(strlen(argv[0]) + 1 + len);
107 for (p=argv[0],q=exepath; p < l; ) {
111 Dmsg1(200, "exepath=%s\n", exepath);
112 if (strchr(exepath, '.') || exepath[0] != '/') {
114 if (getcwd(cpath, sizeof(cpath))) {
115 if (chdir(exepath) == 0) {
116 if (!getcwd(npath, sizeof(npath))) {
123 exepath = (char *)malloc(strlen(npath) + 1 + len);
124 strcpy(exepath, npath);
127 Dmsg1(200, "Normalized exepath=%s\n", exepath);
132 /* Initialize message handler */
136 DEST *d, *dnew, *temp_chain = NULL;
137 JCR *jcr = (JCR *)vjcr;
140 memset(send_msg, 0, sizeof(send_msg)); /* init daemon stuff */
141 } else { /* init for job */
142 /* Walk down the global chain duplicating it
143 * for the current Job. No need to duplicate
144 * the attached strings.
146 for (d=dest_chain; d; d=d->next) {
147 dnew = (DEST *) malloc(sizeof(DEST));
148 memcpy(dnew, d, sizeof(DEST));
149 dnew->next = temp_chain;
154 jcr->dest_chain = temp_chain;
155 memcpy(jcr->send_msg, send_msg, sizeof(send_msg));
159 /* Initialize so that the console (User Agent) can
160 * receive messages -- stored in a file.
162 void init_console_msg(char *wd)
166 sprintf(con_fname, "%s/%s.conmsg", wd, my_name);
167 fd = open(con_fname, O_CREAT|O_RDWR|O_BINARY, 0600);
169 Emsg2(M_ABORT, 0, "Could not open console message file %s: ERR=%s\n",
170 con_fname, strerror(errno));
172 if (lseek(fd, 0, SEEK_END) > 0) {
173 console_msg_pending = 1;
176 con_fd = fopen(con_fname, "a+");
178 Emsg2(M_ERROR, 0, "Could not open console message file %s: ERR=%s\n",
179 con_fname, strerror(errno));
184 * Called only during parsing of the config file.
186 * Add a message destination. I.e. associate a message type with
187 * a destination (code).
188 * Note, where in the case of dest_code FILE is a filename,
189 * but in the case of MAIL is a space separated list of
190 * email addresses, ...
192 void add_msg_dest(int dest_code, int msg_type, char *where, char *mail_cmd)
196 /* First search the existing chain and see if we
197 * can simply add this msg_type to an existing entry.
199 for (d=dest_chain; d; d=d->next) {
200 if (dest_code == d->dest_code && ((where == NULL && d->where == NULL) ||
201 (strcmp(where, d->where) == 0))) {
202 Dmsg4(200, "Add to existing d=%x msgtype=%d destcode=%d where=%s\n",
203 d, msg_type, dest_code, where);
204 set_bit(msg_type, d->msg_types);
205 set_bit(msg_type, send_msg); /* set msg_type bit in our local */
209 /* Not found, create a new entry */
210 d = (DEST *) malloc(sizeof(DEST));
211 memset(d, 0, sizeof(DEST));
212 d->next = dest_chain;
213 d->dest_code = dest_code;
214 set_bit(msg_type, d->msg_types); /* set type bit in structure */
215 set_bit(msg_type, send_msg); /* set type bit in our local */
217 d->where = bstrdup(where);
220 d->mail_cmd = bstrdup(mail_cmd);
222 Dmsg5(200, "add new d=%x msgtype=%d destcode=%d where=%s mailcmd=%s\n",
223 d, msg_type, dest_code, where?where:"(null)",
224 d->mail_cmd?d->mail_cmd:"(null)");
229 * Called only during parsing of the config file.
231 * Remove a message destination
233 void rem_msg_dest(int dest_code, int msg_type, char *where)
237 for (d=dest_chain; d; d=d->next) {
238 Dmsg2(200, "Remove_msg_dest d=%x where=%s\n", d, d->where);
239 if (bit_is_set(msg_type, d->msg_types) && (dest_code == d->dest_code) &&
240 ((where == NULL && d->where == NULL) ||
241 (strcmp(where, d->where) == 0))) {
242 Dmsg3(200, "Found for remove d=%x msgtype=%d destcode=%d\n",
243 d, msg_type, dest_code);
244 clear_bit(msg_type, d->msg_types);
245 Dmsg0(200, "Return rem_msg_dest\n");
252 * Concatenate a string (str) onto a message (msg)
253 * return new message pointer
255 static void add_str(char **base, char **msg, char *str)
257 int len = strlen(str) + 1;
261 *base = (char *) check_pool_memory_size(*base, len);
262 m = *base - b + *msg;
270 * Convert Job Termination Status into a string
272 static char *job_status_to_str(int stat)
290 str = "Unknown term code";
298 * Convert Job Type into a string
300 static char *job_type_to_str(int type)
315 str = "Unknown Job Type";
322 * Convert Job Level into a string
324 static char *job_level_to_str(int level)
336 str = "differential";
344 case L_VERIFY_CATALOG:
345 str = "verify catalog";
350 case L_VERIFY_VOLUME:
351 str = "verify volume";
357 str = "Unknown Job level";
365 * Edit job codes into main command line
368 * %t = Job type (Backup, ...)
373 * %d = Director's name
375 static char *edit_job_codes(JCR *jcr, char *omsg, char *imsg, char *to)
380 Dmsg1(200, "edit_job_codes: %s\n", imsg);
383 for (p=imsg; *p; p++) {
391 case 'j': /* Job name */
395 str = job_status_to_str(jcr->JobStatus);
398 str = job_type_to_str(jcr->JobType);
404 str = job_level_to_str(jcr->level);
407 str = jcr->client_name;
413 str = my_name; /* Director's name */
426 Dmsg1(200, "add_str %s\n", str);
427 add_str(&omsg, &o, str);
429 Dmsg1(200, "omsg=%s\n", omsg);
436 * Create a unique filename for the mail command
438 static void make_unique_mail_filename(JCR *jcr, char **name, DEST *d)
441 Mmsg(name, "%s/%s.mail.%s.%d", working_directory, my_name,
444 Mmsg(name, "%s/%s.mail.%s.%d", working_directory, my_name,
447 Dmsg1(200, "mailname=%s\n", *name);
453 static FILE *open_mail_pipe(JCR *jcr, char **cmd, DEST *d)
457 if (d->mail_cmd && jcr) {
458 *cmd = edit_job_codes(jcr, *cmd, d->mail_cmd, d->where);
460 Mmsg(cmd, "mail -s \"Bacula Message\" %s", d->where);
462 Dmsg1(200, "mailcmd=%s\n", cmd);
463 pfd = popen(*cmd, "w");
465 Emsg2(M_ERROR, 0, "popen %s failed: ERR=%s\n", cmd, strerror(errno));
467 Jmsg(jcr, M_ERROR, 0, "mail popen %s failed: ERR=%s\n", cmd, strerror(errno));
474 * Close the messages for this job, which means to close
475 * any open files, and dispatch any pending email messages.
477 * This closes messages only for this job, other jobs can
478 * still send messages.
480 * Note, we free our local message destination chain, but
481 * the global chain remains allowing other jobs to
484 void close_msg(void *vjcr)
490 JCR *jcr = (JCR *)vjcr;
492 Dmsg0(200, "Close_msg\n");
493 cmd = (char *)get_pool_memory(PM_MESSAGE);
494 for (d=jcr->dest_chain; d; ) {
496 switch (d->dest_code) {
500 fclose(d->fd); /* close open file descriptor */
504 case MD_MAIL_ON_ERROR:
508 if (d->dest_code == MD_MAIL_ON_ERROR &&
509 jcr->JobStatus == JS_Terminated) {
513 pfd = open_mail_pipe(jcr, &cmd, d);
518 line = (char *)get_memory(len);
520 while (fgets(line, len, d->fd)) {
523 pclose(pfd); /* close pipe, sending mail */
526 /* Remove temp file */
528 make_unique_mail_filename(jcr, &cmd, d);
529 Dmsg1(200, "unlink: %s\n", cmd);
537 old = d; /* save pointer to release */
538 d = d->next; /* point to next buffer */
539 free(old); /* free the destination item */
541 free_pool_memory(cmd);
542 jcr->dest_chain = NULL;
547 * Terminate the message handler for good.
548 * Release the global destination chain.
554 for (d=dest_chain; d; d=n) {
556 if (d->dest_code == MD_FILE || d->dest_code == MD_APPEND) {
557 fclose(d->fd); /* close open file descriptor */
558 } else if (d->dest_code == MD_MAIL || d->dest_code == MD_MAIL_ON_ERROR) {
559 pclose(d->fd); /* close open pipe */
564 free(d->where); /* free destination address */
587 * Handle sending the message to the appropriate place
589 void dispatch_message(void *vjcr, int type, int level, char *buf)
592 char cmd[MAXSTRING], *mcmd;
593 JCR *jcr = (JCR *) vjcr;
596 Dmsg2(200, "Enter dispatch_msg type=%d msg=%s\n", type, buf);
598 if (type == M_ABORT) {
599 fprintf(stdout, buf); /* print this here to INSURE that it is printed */
602 /* Now figure out where to send the message */
604 d = jcr->dest_chain; /* use job message chain */
606 d = dest_chain; /* use global chain */
608 for ( ; d; d=d->next) {
609 if (bit_is_set(type, d->msg_types)) {
610 switch (d->dest_code) {
612 Dmsg1(200, "CONSOLE for following err: %s\n", buf);
614 con_fd = fopen(con_fname, "a+");
615 Dmsg0(200, "Console file not open.\n");
618 fcntl(fileno(con_fd), F_SETLKW);
620 bstrftime(cmd, sizeof(cmd), time(NULL));
623 fwrite(cmd, len, 1, con_fd);
625 if (len > 0 && buf[len-1] != '\n') {
629 fwrite(buf, len, 1, con_fd);
631 fcntl(fileno(con_fd), F_UNLCK);
632 console_msg_pending = TRUE;
636 Dmsg1(200, "SYSLOG for following err: %s\n", buf);
637 /* We really should do an openlog() here */
638 syslog(LOG_DAEMON|LOG_ERR, buf);
641 Dmsg1(200, "OPERATOR for following err: %s\n", buf);
642 mcmd = (char *) get_pool_memory(PM_MESSAGE);
643 d->fd = open_mail_pipe(jcr, &mcmd, d);
644 free_pool_memory(mcmd);
647 /* Messages to the operator go one at a time */
652 case MD_MAIL_ON_ERROR:
653 Dmsg1(200, "MAIL for following err: %s\n", buf);
655 char *name = (char *) get_pool_memory(PM_MESSAGE);
656 make_unique_mail_filename(jcr, &name, d);
657 d->fd = fopen(name, "w+");
658 Dmsg2(100, "Open mail file %d: %s\n", d->fd, name);
660 Emsg2(M_ERROR, 0, "fopen %s failed: ERR=%s\n", name, strerror(errno));
661 free_pool_memory(name);
664 free_pool_memory(name);
667 if (len > d->max_len) {
668 d->max_len = len; /* keep max line length */
673 Dmsg1(200, "FILE for following err: %s\n", buf);
675 d->fd = fopen(d->where, "w+");
677 Emsg2(M_ERROR, 0, "fopen %s failed: ERR=%s\n", d->where, strerror(errno));
684 Dmsg1(200, "APPEND for following err: %s\n", buf);
686 d->fd = fopen(d->where, "a");
688 Emsg2(M_ERROR, 0, "fopen %s failed: ERR=%s\n", d->where, strerror(errno));
695 Dmsg1(200, "DIRECTOR for following err: %s\n", buf);
696 if (jcr && jcr->dir_bsock && !jcr->dir_bsock->errors) {
698 jcr->dir_bsock->msglen = Mmsg(&(jcr->dir_bsock->msg),
699 "Jmsg Job=%s type=%d level=%d %s", jcr->Job,
700 type, level, buf) + 1;
701 bnet_send(jcr->dir_bsock);
705 Dmsg1(200, "STDOUT for following err: %s\n", buf);
706 if (type != M_ABORT && type != M_FATAL) /* already printed */
707 fprintf(stdout, buf);
710 Dmsg1(200, "STDERR for following err: %s\n", buf);
711 fprintf(stderr, buf);
721 /*********************************************************************
723 * subroutine prints a debug message if the level number
724 * is less than or equal the debug_level. File and line numbers
725 * are included for more detail if desired, but not currently
728 * If the level is negative, the details of file and line number
732 d_msg(char *file, int line, int level, char *fmt,...)
744 /* printf("level=%d debug=%d fmt=%s\n", level, debug_level, fmt); */
746 if (level <= debug_level) {
749 sprintf(buf, "%s: %s:%d ", my_name, file, line);
757 va_start(arg_ptr, fmt);
758 bvsnprintf(buf+i, sizeof(buf)-i, (char *)fmt, arg_ptr);
761 fprintf(stdout, buf);
766 /* *********************************************************
768 * print an error message
772 e_msg(char *file, int line, int type, int level, char *fmt,...)
779 * Check if we have a message destination defined.
780 * We always report M_ABORT
782 if (type != M_ABORT && !bit_is_set(type, send_msg))
783 return; /* no destination */
786 sprintf(buf, "%s ABORTING due to ERROR in %s:%d\n",
787 my_name, file, line);
790 if (level == -1) /* skip details */
791 sprintf(buf, "%s: Fatal Error because: ", my_name);
793 sprintf(buf, "%s: Fatal Error at %s:%d because:\n", my_name, file, line);
796 if (level == -1) /* skip details */
797 sprintf(buf, "%s: Error: ", my_name);
799 sprintf(buf, "%s: Error in %s:%d ", my_name, file, line);
802 sprintf(buf, "%s: Warning: ", my_name);
805 sprintf(buf, "%s: ", my_name);
810 va_start(arg_ptr, fmt);
811 bvsnprintf(buf+i, sizeof(buf)-i, (char *)fmt, arg_ptr);
814 dispatch_message(NULL, type, level, buf);
816 if (type == M_ABORT) {
821 /* *********************************************************
823 * Generate a Job message
827 Jmsg(void *vjcr, int type, int level, char *fmt,...)
833 JCR *jcr = (JCR *) vjcr;
837 Dmsg1(200, "Enter Jmsg type=%d\n", type);
839 buf = rbuf; /* we are the Director */
841 * Check if we have a message destination defined.
842 * We always report M_ABORT
844 if (type != M_ABORT && !bit_is_set(type, jcr->send_msg)) {
845 Dmsg1(200, "No bit set for type %d\n", type);
846 return; /* no destination */
850 sprintf(buf, "%s ABORTING due to ERROR\n", my_name);
853 sprintf(buf, "%s: Job %s Cancelled because: ", my_name, jcr->Job);
856 sprintf(buf, "%s: Job %s Error: ", my_name, jcr->Job);
859 sprintf(buf, "%s: Job %s Warning: ", my_name, jcr->Job);
862 sprintf(buf, "%s: ", my_name);
867 va_start(arg_ptr, fmt);
868 len = bvsnprintf(buf+i, sizeof(rbuf)-i, fmt, arg_ptr);
871 ASSERT(typesave==type); /* type trashed, compiler bug???? */
872 dispatch_message(jcr, type, level, rbuf);
874 Dmsg3(500, "i=%d sizeof(rbuf)-i=%d len=%d\n", i, sizeof(rbuf)-i, len);
881 * Edit a message into a Pool memory buffer, with file:lineno
883 int m_msg(char *file, int line, char **pool_buf, char *fmt, ...)
888 sprintf(*pool_buf, "%s:%d ", file, line);
889 i = strlen(*pool_buf);
892 maxlen = sizeof_pool_memory(*pool_buf) - i - 1;
893 va_start(arg_ptr, fmt);
894 len = bvsnprintf(*pool_buf+i, maxlen, fmt, arg_ptr);
896 if (len < 0 || len >= maxlen) {
897 *pool_buf = (char *) realloc_pool_memory(*pool_buf, maxlen + i + 200);
904 * Edit a message into a Pool Memory buffer NO file:lineno
905 * Returns: string length of what was edited.
907 int Mmsg(char **pool_buf, char *fmt, ...)
913 maxlen = sizeof_pool_memory(*pool_buf) - 1;
914 va_start(arg_ptr, fmt);
915 len = bvsnprintf(*pool_buf, maxlen, fmt, arg_ptr);
917 if (len < 0 || len >= maxlen) {
918 *pool_buf = (char *) realloc_pool_memory(*pool_buf, maxlen + 200);
925 void j_msg(char *file, int line, void *jcr, int type, int level, char *fmt,...)
931 pool_buf = (char *) get_pool_memory(PM_EMSG);
932 sprintf(pool_buf, "%s:%d ", file, line);
933 i = strlen(pool_buf);
936 maxlen = sizeof_pool_memory(pool_buf) - i - 1;
937 va_start(arg_ptr, fmt);
938 len = bvsnprintf(pool_buf+i, maxlen, fmt, arg_ptr);
940 if (len < 0 || len >= maxlen) {
941 pool_buf = (char *) realloc_pool_memory(pool_buf, maxlen + i + 200);
945 Jmsg(jcr, type, level, pool_buf);
946 free_memory(pool_buf);