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;
46 pthread_mutex_t con_mutex = PTHREAD_MUTEX_INITIALIZER;
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
133 * NULL for jcr -> initialize global messages for daemon
134 * non-NULL -> initialize jcr using Message resource
137 init_msg(void *vjcr, MSGS *msg)
139 DEST *d, *dnew, *temp_chain = NULL;
140 JCR *jcr = (JCR *)vjcr;
143 * If msg is NULL, initialize global chain for STDOUT and syslog
147 daemon_msgs = (MSGS *)malloc(sizeof(MSGS));
148 memset(daemon_msgs, 0, sizeof(MSGS));
149 for (i=1; i<=M_MAX; i++) {
150 add_msg_dest(daemon_msgs, MD_STDOUT, i, NULL, NULL);
151 add_msg_dest(daemon_msgs, MD_SYSLOG, i, NULL, NULL);
153 Dmsg1(050, "Create daemon global message resource 0x%x\n", daemon_msgs);
158 * Walk down the message resource chain duplicating it
159 * for the current Job.
161 for (d=msg->dest_chain; d; d=d->next) {
162 dnew = (DEST *)malloc(sizeof(DEST));
163 memcpy(dnew, d, sizeof(DEST));
164 dnew->next = temp_chain;
166 dnew->mail_filename = NULL;
168 dnew->mail_cmd = bstrdup(d->mail_cmd);
171 dnew->where = bstrdup(d->where);
177 jcr->msgs = (MSGS *)malloc(sizeof(MSGS));
178 memset(jcr->msgs, 0, sizeof(MSGS));
179 jcr->msgs->dest_chain = temp_chain;
180 memcpy(jcr->msgs->send_msg, msg->send_msg, sizeof(msg->send_msg));
182 daemon_msgs = (MSGS *)malloc(sizeof(MSGS));
183 memset(daemon_msgs, 0, sizeof(MSGS));
184 daemon_msgs->dest_chain = temp_chain;
185 memcpy(daemon_msgs->send_msg, msg->send_msg, sizeof(msg->send_msg));
187 Dmsg2(050, "Copy message resource 0x%x to 0x%x\n", msg, temp_chain);
190 /* Initialize so that the console (User Agent) can
191 * receive messages -- stored in a file.
193 void init_console_msg(char *wd)
197 sprintf(con_fname, "%s/%s.conmsg", wd, my_name);
198 fd = open(con_fname, O_CREAT|O_RDWR|O_BINARY, 0600);
200 Emsg2(M_ERROR_TERM, 0, "Could not open console message file %s: ERR=%s\n",
201 con_fname, strerror(errno));
203 if (lseek(fd, 0, SEEK_END) > 0) {
204 console_msg_pending = 1;
207 con_fd = fopen(con_fname, "a+");
209 Emsg2(M_ERROR, 0, "Could not open console message file %s: ERR=%s\n",
210 con_fname, strerror(errno));
215 * Called only during parsing of the config file.
217 * Add a message destination. I.e. associate a message type with
218 * a destination (code).
219 * Note, where in the case of dest_code FILE is a filename,
220 * but in the case of MAIL is a space separated list of
221 * email addresses, ...
223 void add_msg_dest(MSGS *msg, int dest_code, int msg_type, char *where, char *mail_cmd)
227 * First search the existing chain and see if we
228 * can simply add this msg_type to an existing entry.
230 for (d=msg->dest_chain; d; d=d->next) {
231 if (dest_code == d->dest_code && ((where == NULL && d->where == NULL) ||
232 (strcmp(where, d->where) == 0))) {
233 Dmsg4(200, "Add to existing d=%x msgtype=%d destcode=%d where=%s\n",
234 d, msg_type, dest_code, NPRT(where));
235 set_bit(msg_type, d->msg_types);
236 set_bit(msg_type, msg->send_msg); /* set msg_type bit in our local */
240 /* Not found, create a new entry */
241 d = (DEST *)malloc(sizeof(DEST));
242 memset(d, 0, sizeof(DEST));
243 d->next = msg->dest_chain;
244 d->dest_code = dest_code;
245 set_bit(msg_type, d->msg_types); /* set type bit in structure */
246 set_bit(msg_type, msg->send_msg); /* set type bit in our local */
248 d->where = bstrdup(where);
251 d->mail_cmd = bstrdup(mail_cmd);
253 Dmsg5(200, "add new d=%x msgtype=%d destcode=%d where=%s mailcmd=%s\n",
254 d, msg_type, dest_code, NPRT(where), NPRT(d->mail_cmd));
259 * Called only during parsing of the config file.
261 * Remove a message destination
263 void rem_msg_dest(MSGS *msg, int dest_code, int msg_type, char *where)
267 for (d=msg->dest_chain; d; d=d->next) {
268 Dmsg2(200, "Remove_msg_dest d=%x where=%s\n", d, NPRT(d->where));
269 if (bit_is_set(msg_type, d->msg_types) && (dest_code == d->dest_code) &&
270 ((where == NULL && d->where == NULL) ||
271 (strcmp(where, d->where) == 0))) {
272 Dmsg3(200, "Found for remove d=%x msgtype=%d destcode=%d\n",
273 d, msg_type, dest_code);
274 clear_bit(msg_type, d->msg_types);
275 Dmsg0(200, "Return rem_msg_dest\n");
282 * Concatenate a string (str) onto a message (msg)
283 * return new message pointer
285 static void add_str(POOLMEM **base, char **msg, char *str)
287 int len = strlen(str) + 1;
291 *base = check_pool_memory_size(*base, len);
292 m = *base - b + *msg;
300 * Convert Job Termination Status into a string
302 static char *job_status_to_str(int stat)
310 case JS_ErrorTerminated:
324 str = "Unknown term code";
332 * Convert Job Type into a string
334 static char *job_type_to_str(int type)
349 str = "Unknown Job Type";
356 * Convert Job Level into a string
358 static char *job_level_to_str(int level)
370 str = "differential";
378 case L_VERIFY_CATALOG:
379 str = "verify catalog";
384 case L_VERIFY_VOLUME_TO_CATALOG:
385 str = "verify volume to catalog";
391 str = "Unknown Job level";
399 * Edit job codes into main command line
402 * %t = Job type (Backup, ...)
407 * %d = Director's name
409 static char *edit_job_codes(JCR *jcr, char *omsg, char *imsg, char *to)
414 Dmsg1(200, "edit_job_codes: %s\n", imsg);
417 for (p=imsg; *p; p++) {
425 case 'j': /* Job name */
429 str = job_status_to_str(jcr->JobStatus);
432 str = job_type_to_str(jcr->JobType);
438 str = job_level_to_str(jcr->JobLevel);
441 str = jcr->client_name;
447 str = my_name; /* Director's name */
460 Dmsg1(200, "add_str %s\n", str);
461 add_str(&omsg, &o, str);
463 Dmsg1(200, "omsg=%s\n", omsg);
470 * Create a unique filename for the mail command
472 static void make_unique_mail_filename(JCR *jcr, char **name, DEST *d)
475 Mmsg(name, "%s/%s.mail.%s.%d", working_directory, my_name,
478 Mmsg(name, "%s/%s.mail.%s.%d", working_directory, my_name,
481 Dmsg1(200, "mailname=%s\n", *name);
487 static FILE *open_mail_pipe(JCR *jcr, char **cmd, DEST *d)
491 if (d->mail_cmd && jcr) {
492 *cmd = edit_job_codes(jcr, *cmd, d->mail_cmd, d->where);
494 Mmsg(cmd, "mail -s \"Bacula Message\" %s", d->where);
496 Dmsg1(200, "mailcmd=%s\n", cmd);
497 pfd = popen(*cmd, "w");
499 Jmsg(jcr, M_ERROR, 0, "mail popen %s failed: ERR=%s\n", cmd, strerror(errno));
505 * Close the messages for this Messages resource, which means to close
506 * any open files, and dispatch any pending email messages.
508 void close_msg(void *vjcr)
511 JCR *jcr = (JCR *)vjcr;
517 Dmsg1(050, "Close_msg jcr=0x%x\n", jcr);
519 if (jcr == NULL) { /* NULL -> global chain */
529 Dmsg1(050, "close msg resource at 0x%x\n", msgs);
530 cmd = get_pool_memory(PM_MESSAGE);
531 for (d=msgs->dest_chain; d; ) {
533 switch (d->dest_code) {
537 fclose(d->fd); /* close open file descriptor */
541 case MD_MAIL_ON_ERROR:
545 if (d->dest_code == MD_MAIL_ON_ERROR && jcr &&
546 jcr->JobStatus == JS_Terminated) {
550 pfd = open_mail_pipe(jcr, &cmd, d);
555 line = get_memory(len);
557 while (fgets(line, len, d->fd)) {
560 pclose(pfd); /* close pipe, sending mail */
563 /* Remove temp file */
565 unlink(d->mail_filename);
566 free_pool_memory(d->mail_filename);
567 d->mail_filename = NULL;
574 d = d->next; /* point to next buffer */
576 free_pool_memory(cmd);
583 * Free memory associated with Messages resource
585 void free_msgs_res(MSGS *msgs)
589 for (d=msgs->dest_chain; d; ) {
596 old = d; /* save pointer to release */
597 d = d->next; /* point to next buffer */
598 free(old); /* free the destination item */
600 msgs->dest_chain = NULL;
606 * Terminate the message handler for good.
607 * Release the global destination chain.
609 * Also, clean up a few other items (cons, exepath). Note,
610 * these really should be done elsewhere.
614 Dmsg0(100, "Enter term_msg\n");
615 close_msg(NULL); /* close global chain */
635 * Handle sending the message to the appropriate place
637 void dispatch_message(void *vjcr, int type, int level, char *msg)
642 JCR *jcr = (JCR *) vjcr;
646 Dmsg2(200, "Enter dispatch_msg type=%d msg=%s\n", type, msg);
648 if (type == M_ABORT || type == M_ERROR_TERM) {
649 fprintf(stdout, msg); /* print this here to INSURE that it is printed */
652 /* Now figure out where to send the message */
660 for (d=msgs->dest_chain; d; d=d->next) {
661 if (bit_is_set(type, d->msg_types)) {
662 switch (d->dest_code) {
664 Dmsg1(200, "CONSOLE for following err: %s\n", msg);
666 con_fd = fopen(con_fname, "a+");
667 Dmsg0(200, "Console file not open.\n");
672 bstrftime(cmd, sizeof(cmd), time(NULL));
675 fwrite(cmd, len, 1, con_fd);
677 if (len > 0 && msg[len-1] != '\n') {
681 fwrite(msg, len, 1, con_fd);
683 console_msg_pending = TRUE;
688 Dmsg1(200, "SYSLOG for following err: %s\n", msg);
689 /* We really should do an openlog() here */
690 syslog(LOG_DAEMON|LOG_ERR, msg);
693 Dmsg1(200, "OPERATOR for following err: %s\n", msg);
694 mcmd = get_pool_memory(PM_MESSAGE);
695 d->fd = open_mail_pipe(jcr, &mcmd, d);
696 free_pool_memory(mcmd);
699 /* Messages to the operator go one at a time */
705 case MD_MAIL_ON_ERROR:
706 Dmsg1(200, "MAIL for following err: %s\n", msg);
708 POOLMEM *name = get_pool_memory(PM_MESSAGE);
709 make_unique_mail_filename(jcr, &name, d);
710 d->fd = fopen(name, "w+");
712 Emsg2(M_ERROR, 0, "fopen %s failed: ERR=%s\n", name, strerror(errno));
713 free_pool_memory(name);
716 d->mail_filename = name;
719 if (len > d->max_len) {
720 d->max_len = len; /* keep max line length */
725 Dmsg1(200, "FILE for following err: %s\n", msg);
727 d->fd = fopen(d->where, "w+");
729 Emsg2(M_ERROR, 0, "fopen %s failed: ERR=%s\n", d->where, strerror(errno));
736 Dmsg1(200, "APPEND for following err: %s\n", msg);
738 d->fd = fopen(d->where, "a");
740 Emsg2(M_ERROR, 0, "fopen %s failed: ERR=%s\n", d->where, strerror(errno));
747 Dmsg1(200, "DIRECTOR for following err: %s\n", msg);
748 if (jcr && jcr->dir_bsock && !jcr->dir_bsock->errors) {
750 jcr->dir_bsock->msglen = Mmsg(&(jcr->dir_bsock->msg),
751 "Jmsg Job=%s type=%d level=%d %s", jcr->Job,
752 type, level, msg) + 1;
753 bnet_send(jcr->dir_bsock);
757 Dmsg1(200, "STDOUT for following err: %s\n", msg);
758 if (type != M_ABORT && type != M_ERROR_TERM) /* already printed */
759 fprintf(stdout, msg);
762 Dmsg1(200, "STDERR for following err: %s\n", msg);
763 fprintf(stderr, msg);
773 /*********************************************************************
775 * subroutine prints a debug message if the level number
776 * is less than or equal the debug_level. File and line numbers
777 * are included for more detail if desired, but not currently
780 * If the level is negative, the details of file and line number
784 d_msg(char *file, int line, int level, char *fmt,...)
796 if (level <= debug_level) {
799 sprintf(buf, "%s: %s:%d ", my_name, file, line);
807 va_start(arg_ptr, fmt);
808 bvsnprintf(buf+i, sizeof(buf)-i, (char *)fmt, arg_ptr);
811 fprintf(stdout, buf);
816 /* *********************************************************
818 * print an error message
822 e_msg(char *file, int line, int type, int level, char *fmt,...)
829 * Check if we have a message destination defined.
830 * We always report M_ABORT and M_ERROR_TERM
832 if (!daemon_msgs || ((type != M_ABORT && type != M_ERROR_TERM) &&
833 !bit_is_set(type, daemon_msgs->send_msg)))
834 return; /* no destination */
837 sprintf(buf, "%s ABORTING due to ERROR in %s:%d\n",
838 my_name, file, line);
841 sprintf(buf, "%s ERROR TERMINATING at %s:%d\n",
842 my_name, file, line);
845 if (level == -1) /* skip details */
846 sprintf(buf, "%s: Fatal Error because: ", my_name);
848 sprintf(buf, "%s: Fatal Error at %s:%d because:\n", my_name, file, line);
851 if (level == -1) /* skip details */
852 sprintf(buf, "%s: Error: ", my_name);
854 sprintf(buf, "%s: Error in %s:%d ", my_name, file, line);
857 sprintf(buf, "%s: Warning: ", my_name);
860 sprintf(buf, "%s: ", my_name);
865 va_start(arg_ptr, fmt);
866 bvsnprintf(buf+i, sizeof(buf)-i, (char *)fmt, arg_ptr);
869 dispatch_message(NULL, type, level, buf);
871 if (type == M_ABORT) {
873 p[0] = 0; /* generate segmentation violation */
875 if (type == M_ERROR_TERM) {
880 /* *********************************************************
882 * Generate a Job message
886 Jmsg(void *vjcr, int type, int level, char *fmt,...)
892 JCR *jcr = (JCR *) vjcr;
897 Dmsg1(200, "Enter Jmsg type=%d\n", type);
912 buf = rbuf; /* we are the Director */
914 * Check if we have a message destination defined.
915 * We always report M_ABORT and M_ERROR_TERM
917 if ((type != M_ABORT && type != M_ERROR_TERM) && msgs && !bit_is_set(type, msgs->send_msg)) {
918 Dmsg1(200, "No bit set for type %d\n", type);
919 return; /* no destination */
923 sprintf(buf, "%s ABORTING due to ERROR\n", my_name);
926 sprintf(buf, "%s ERROR TERMINATING\n", my_name);
929 sprintf(buf, "%s: Job %s Fatal error: ", my_name, job);
931 jcr->JobStatus = JS_FatalError;
935 sprintf(buf, "%s: Job %s Error: ", my_name, job);
941 sprintf(buf, "%s: Job %s Warning: ", my_name, job);
944 sprintf(buf, "%s: ", my_name);
949 va_start(arg_ptr, fmt);
950 len = bvsnprintf(buf+i, sizeof(rbuf)-i, fmt, arg_ptr);
953 dispatch_message(jcr, type, level, rbuf);
955 Dmsg3(500, "i=%d sizeof(rbuf)-i=%d len=%d\n", i, sizeof(rbuf)-i, len);
957 if (type == M_ABORT){
959 p[0] = 0; /* generate segmentation violation */
961 if (type == M_ERROR_TERM) {
967 * Edit a message into a Pool memory buffer, with file:lineno
969 int m_msg(char *file, int line, POOLMEM **pool_buf, char *fmt, ...)
974 sprintf(*pool_buf, "%s:%d ", file, line);
975 i = strlen(*pool_buf);
978 maxlen = sizeof_pool_memory(*pool_buf) - i - 1;
979 va_start(arg_ptr, fmt);
980 len = bvsnprintf(*pool_buf+i, maxlen, fmt, arg_ptr);
982 if (len < 0 || len >= maxlen) {
983 *pool_buf = realloc_pool_memory(*pool_buf, maxlen + i + 200);
990 * Edit a message into a Pool Memory buffer NO file:lineno
991 * Returns: string length of what was edited.
993 int Mmsg(POOLMEM **pool_buf, char *fmt, ...)
999 maxlen = sizeof_pool_memory(*pool_buf) - 1;
1000 va_start(arg_ptr, fmt);
1001 len = bvsnprintf(*pool_buf, maxlen, fmt, arg_ptr);
1003 if (len < 0 || len >= maxlen) {
1004 *pool_buf = realloc_pool_memory(*pool_buf, maxlen + 200);
1012 * If we come here, prefix the message with the file:line-number,
1013 * then pass it on to the normal Jmsg routine.
1015 void j_msg(char *file, int line, void *jcr, int type, int level, char *fmt,...)
1021 pool_buf = get_pool_memory(PM_EMSG);
1022 sprintf(pool_buf, "%s:%d ", file, line);
1023 i = strlen(pool_buf);
1026 maxlen = sizeof_pool_memory(pool_buf) - i - 1;
1027 va_start(arg_ptr, fmt);
1028 len = bvsnprintf(pool_buf+i, maxlen, fmt, arg_ptr);
1030 if (len < 0 || len >= maxlen) {
1031 pool_buf = realloc_pool_memory(pool_buf, maxlen + i + 200);
1035 Jmsg(jcr, type, level, pool_buf);
1036 free_memory(pool_buf);