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;
47 /* Forward referenced functions */
49 /* Imported functions */
54 static MSGS *daemon_msgs; /* global messages */
57 * Set daemon name. Also, find canonical execution
58 * path. Note, exepath has spare room for tacking on
59 * the exename so that we can reconstruct the full name.
61 * Note, this routine can get called multiple times
62 * The second time is to put the name as found in the
63 * Resource record. On the second call, generally,
64 * argv is NULL to avoid doing the path code twice.
66 #define BTRACE_EXTRA 20
67 void my_name_is(int argc, char *argv[], char *name)
70 char cpath[400], npath[400];
73 strncpy(my_name, name, sizeof(my_name));
74 my_name[sizeof(my_name)-1] = 0;
75 if (argc>0 && argv && argv[0]) {
76 /* strip trailing filename and save exepath */
77 for (l=p=argv[0]; *p; p++) {
79 l = p; /* set pos of last slash */
87 /* On Windows allow c: junk */
97 exename = (char *)malloc(len);
102 exepath = (char *)malloc(strlen(argv[0]) + 1 + len);
103 for (p=argv[0],q=exepath; p < l; ) {
107 Dmsg1(200, "exepath=%s\n", exepath);
108 if (strchr(exepath, '.') || exepath[0] != '/') {
110 if (getcwd(cpath, sizeof(cpath))) {
111 if (chdir(exepath) == 0) {
112 if (!getcwd(npath, sizeof(npath))) {
119 exepath = (char *)malloc(strlen(npath) + 1 + len);
120 strcpy(exepath, npath);
123 Dmsg1(200, "Normalized exepath=%s\n", exepath);
129 * Initialize message handler for a daemon or a Job
131 * NULL for jcr -> initialize global messages for daemon
132 * non-NULL -> initialize jcr using Message resource
135 init_msg(void *vjcr, MSGS *msg)
137 DEST *d, *dnew, *temp_chain = NULL;
138 JCR *jcr = (JCR *)vjcr;
141 * If msg is NULL, initialize global chain for STDOUT and syslog
145 daemon_msgs = (MSGS *)malloc(sizeof(MSGS));
146 memset(daemon_msgs, 0, sizeof(MSGS));
147 for (i=1; i<=M_MAX; i++) {
148 add_msg_dest(daemon_msgs, MD_STDOUT, i, NULL, NULL);
149 add_msg_dest(daemon_msgs, MD_SYSLOG, i, NULL, NULL);
151 Dmsg1(050, "Create daemon global message resource 0x%x\n", daemon_msgs);
156 * Walk down the message resource chain duplicating it
157 * for the current Job.
159 for (d=msg->dest_chain; d; d=d->next) {
160 dnew = (DEST *) malloc(sizeof(DEST));
161 memcpy(dnew, d, sizeof(DEST));
162 dnew->next = temp_chain;
164 dnew->mail_filename = NULL;
166 dnew->mail_cmd = bstrdup(d->mail_cmd);
169 dnew->where = bstrdup(d->where);
175 jcr->msgs = (MSGS *)malloc(sizeof(MSGS));
176 memset(jcr->msgs, 0, sizeof(MSGS));
177 jcr->msgs->dest_chain = temp_chain;
178 memcpy(jcr->msgs->send_msg, msg->send_msg, sizeof(msg->send_msg));
180 daemon_msgs = (MSGS *)malloc(sizeof(MSGS));
181 memset(daemon_msgs, 0, sizeof(MSGS));
182 daemon_msgs->dest_chain = temp_chain;
183 memcpy(daemon_msgs->send_msg, msg->send_msg, sizeof(msg->send_msg));
185 Dmsg2(050, "Copy message resource 0x%x to 0x%x\n", msg, temp_chain);
188 /* Initialize so that the console (User Agent) can
189 * receive messages -- stored in a file.
191 void init_console_msg(char *wd)
195 sprintf(con_fname, "%s/%s.conmsg", wd, my_name);
196 fd = open(con_fname, O_CREAT|O_RDWR|O_BINARY, 0600);
198 Emsg2(M_ABORT, 0, "Could not open console message file %s: ERR=%s\n",
199 con_fname, strerror(errno));
201 if (lseek(fd, 0, SEEK_END) > 0) {
202 console_msg_pending = 1;
205 con_fd = fopen(con_fname, "a+");
207 Emsg2(M_ERROR, 0, "Could not open console message file %s: ERR=%s\n",
208 con_fname, strerror(errno));
213 * Called only during parsing of the config file.
215 * Add a message destination. I.e. associate a message type with
216 * a destination (code).
217 * Note, where in the case of dest_code FILE is a filename,
218 * but in the case of MAIL is a space separated list of
219 * email addresses, ...
221 void add_msg_dest(MSGS *msg, int dest_code, int msg_type, char *where, char *mail_cmd)
225 * First search the existing chain and see if we
226 * can simply add this msg_type to an existing entry.
228 for (d=msg->dest_chain; d; d=d->next) {
229 if (dest_code == d->dest_code && ((where == NULL && d->where == NULL) ||
230 (strcmp(where, d->where) == 0))) {
231 Dmsg4(200, "Add to existing d=%x msgtype=%d destcode=%d where=%s\n",
232 d, msg_type, dest_code, where);
233 set_bit(msg_type, d->msg_types);
234 set_bit(msg_type, msg->send_msg); /* set msg_type bit in our local */
238 /* Not found, create a new entry */
239 d = (DEST *)malloc(sizeof(DEST));
240 memset(d, 0, sizeof(DEST));
241 d->next = msg->dest_chain;
242 d->dest_code = dest_code;
243 set_bit(msg_type, d->msg_types); /* set type bit in structure */
244 set_bit(msg_type, msg->send_msg); /* set type bit in our local */
246 d->where = bstrdup(where);
249 d->mail_cmd = bstrdup(mail_cmd);
251 Dmsg5(200, "add new d=%x msgtype=%d destcode=%d where=%s mailcmd=%s\n",
252 d, msg_type, dest_code, where?where:"(null)",
253 d->mail_cmd?d->mail_cmd:"(null)");
258 * Called only during parsing of the config file.
260 * Remove a message destination
262 void rem_msg_dest(MSGS *msg, int dest_code, int msg_type, char *where)
266 for (d=msg->dest_chain; d; d=d->next) {
267 Dmsg2(200, "Remove_msg_dest d=%x where=%s\n", d, d->where);
268 if (bit_is_set(msg_type, d->msg_types) && (dest_code == d->dest_code) &&
269 ((where == NULL && d->where == NULL) ||
270 (strcmp(where, d->where) == 0))) {
271 Dmsg3(200, "Found for remove d=%x msgtype=%d destcode=%d\n",
272 d, msg_type, dest_code);
273 clear_bit(msg_type, d->msg_types);
274 Dmsg0(200, "Return rem_msg_dest\n");
281 * Concatenate a string (str) onto a message (msg)
282 * return new message pointer
284 static void add_str(char **base, char **msg, char *str)
286 int len = strlen(str) + 1;
290 *base = (char *) check_pool_memory_size(*base, len);
291 m = *base - b + *msg;
299 * Convert Job Termination Status into a string
301 static char *job_status_to_str(int stat)
319 str = "Unknown term code";
327 * Convert Job Type into a string
329 static char *job_type_to_str(int type)
344 str = "Unknown Job Type";
351 * Convert Job Level into a string
353 static char *job_level_to_str(int level)
365 str = "differential";
373 case L_VERIFY_CATALOG:
374 str = "verify catalog";
379 case L_VERIFY_VOLUME:
380 str = "verify volume";
386 str = "Unknown Job level";
394 * Edit job codes into main command line
397 * %t = Job type (Backup, ...)
402 * %d = Director's name
404 static char *edit_job_codes(JCR *jcr, char *omsg, char *imsg, char *to)
409 Dmsg1(200, "edit_job_codes: %s\n", imsg);
412 for (p=imsg; *p; p++) {
420 case 'j': /* Job name */
424 str = job_status_to_str(jcr->JobStatus);
427 str = job_type_to_str(jcr->JobType);
433 str = job_level_to_str(jcr->level);
436 str = jcr->client_name;
442 str = my_name; /* Director's name */
455 Dmsg1(200, "add_str %s\n", str);
456 add_str(&omsg, &o, str);
458 Dmsg1(200, "omsg=%s\n", omsg);
465 * Create a unique filename for the mail command
467 static void make_unique_mail_filename(JCR *jcr, char **name, DEST *d)
470 Mmsg(name, "%s/%s.mail.%s.%d", working_directory, my_name,
473 Mmsg(name, "%s/%s.mail.%s.%d", working_directory, my_name,
476 Dmsg1(200, "mailname=%s\n", *name);
482 static FILE *open_mail_pipe(JCR *jcr, char **cmd, DEST *d)
486 if (d->mail_cmd && jcr) {
487 *cmd = edit_job_codes(jcr, *cmd, d->mail_cmd, d->where);
489 Mmsg(cmd, "mail -s \"Bacula Message\" %s", d->where);
491 Dmsg1(200, "mailcmd=%s\n", cmd);
492 pfd = popen(*cmd, "w");
494 Jmsg(jcr, M_ERROR, 0, "mail popen %s failed: ERR=%s\n", cmd, strerror(errno));
500 * Close the messages for this Messages resource, which means to close
501 * any open files, and dispatch any pending email messages.
503 void close_msg(void *vjcr)
506 JCR *jcr = (JCR *)vjcr;
512 Dmsg1(050, "Close_msg jcr=0x%x\n", jcr);
514 if (jcr == NULL) { /* NULL -> global chain */
524 Dmsg1(050, "close msg resource at 0x%x\n", msgs);
525 cmd = get_pool_memory(PM_MESSAGE);
526 for (d=msgs->dest_chain; d; ) {
528 switch (d->dest_code) {
532 fclose(d->fd); /* close open file descriptor */
536 case MD_MAIL_ON_ERROR:
540 if (d->dest_code == MD_MAIL_ON_ERROR && jcr &&
541 jcr->JobStatus == JS_Terminated) {
545 pfd = open_mail_pipe(jcr, &cmd, d);
550 line = get_memory(len);
552 while (fgets(line, len, d->fd)) {
555 pclose(pfd); /* close pipe, sending mail */
558 /* Remove temp file */
560 unlink(d->mail_filename);
561 free_pool_memory(d->mail_filename);
562 d->mail_filename = NULL;
569 d = d->next; /* point to next buffer */
571 free_pool_memory(cmd);
577 * Free memory associated with Messages resource
579 void free_msgs_res(MSGS *msgs)
583 for (d=msgs->dest_chain; d; ) {
590 old = d; /* save pointer to release */
591 d = d->next; /* point to next buffer */
592 free(old); /* free the destination item */
594 msgs->dest_chain = NULL;
600 * Terminate the message handler for good.
601 * Release the global destination chain.
603 * Also, clean up a few other items (cons, exepath). Note,
604 * these really should be done elsewhere.
608 Dmsg0(100, "Enter term_msg\n");
609 close_msg(NULL); /* close global chain */
629 * Handle sending the message to the appropriate place
631 void dispatch_message(void *vjcr, int type, int level, char *buf)
636 JCR *jcr = (JCR *) vjcr;
640 Dmsg2(200, "Enter dispatch_msg type=%d msg=%s\n", type, buf);
642 if (type == M_ABORT) {
643 fprintf(stdout, buf); /* print this here to INSURE that it is printed */
646 /* Now figure out where to send the message */
654 for (d=msgs->dest_chain; d; d=d->next) {
655 if (bit_is_set(type, d->msg_types)) {
656 switch (d->dest_code) {
658 Dmsg1(200, "CONSOLE for following err: %s\n", buf);
660 con_fd = fopen(con_fname, "a+");
661 Dmsg0(200, "Console file not open.\n");
664 fcntl(fileno(con_fd), F_SETLKW);
666 bstrftime(cmd, sizeof(cmd), time(NULL));
669 fwrite(cmd, len, 1, con_fd);
671 if (len > 0 && buf[len-1] != '\n') {
675 fwrite(buf, len, 1, con_fd);
677 fcntl(fileno(con_fd), F_UNLCK);
678 console_msg_pending = TRUE;
682 Dmsg1(200, "SYSLOG for following err: %s\n", buf);
683 /* We really should do an openlog() here */
684 syslog(LOG_DAEMON|LOG_ERR, buf);
687 Dmsg1(200, "OPERATOR for following err: %s\n", buf);
688 mcmd = get_pool_memory(PM_MESSAGE);
689 d->fd = open_mail_pipe(jcr, &mcmd, d);
690 free_pool_memory(mcmd);
693 /* Messages to the operator go one at a time */
699 case MD_MAIL_ON_ERROR:
700 Dmsg1(200, "MAIL for following err: %s\n", buf);
702 char *name = (char *)get_pool_memory(PM_MESSAGE);
703 make_unique_mail_filename(jcr, &name, d);
704 d->fd = fopen(name, "w+");
706 Emsg2(M_ERROR, 0, "fopen %s failed: ERR=%s\n", name, strerror(errno));
707 free_pool_memory(name);
710 d->mail_filename = name;
713 if (len > d->max_len) {
714 d->max_len = len; /* keep max line length */
719 Dmsg1(200, "FILE for following err: %s\n", buf);
721 d->fd = fopen(d->where, "w+");
723 Emsg2(M_ERROR, 0, "fopen %s failed: ERR=%s\n", d->where, strerror(errno));
730 Dmsg1(200, "APPEND for following err: %s\n", buf);
732 d->fd = fopen(d->where, "a");
734 Emsg2(M_ERROR, 0, "fopen %s failed: ERR=%s\n", d->where, strerror(errno));
741 Dmsg1(200, "DIRECTOR for following err: %s\n", buf);
742 if (jcr && jcr->dir_bsock && !jcr->dir_bsock->errors) {
744 jcr->dir_bsock->msglen = Mmsg(&(jcr->dir_bsock->msg),
745 "Jmsg Job=%s type=%d level=%d %s", jcr->Job,
746 type, level, buf) + 1;
747 bnet_send(jcr->dir_bsock);
751 Dmsg1(200, "STDOUT for following err: %s\n", buf);
752 if (type != M_ABORT && type != M_FATAL) /* already printed */
753 fprintf(stdout, buf);
756 Dmsg1(200, "STDERR for following err: %s\n", buf);
757 fprintf(stderr, buf);
767 /*********************************************************************
769 * subroutine prints a debug message if the level number
770 * is less than or equal the debug_level. File and line numbers
771 * are included for more detail if desired, but not currently
774 * If the level is negative, the details of file and line number
778 d_msg(char *file, int line, int level, char *fmt,...)
790 /* printf("level=%d debug=%d fmt=%s\n", level, debug_level, fmt); */
792 if (level <= debug_level) {
795 sprintf(buf, "%s: %s:%d ", my_name, file, line);
803 va_start(arg_ptr, fmt);
804 bvsnprintf(buf+i, sizeof(buf)-i, (char *)fmt, arg_ptr);
807 fprintf(stdout, buf);
812 /* *********************************************************
814 * print an error message
818 e_msg(char *file, int line, int type, int level, char *fmt,...)
825 * Check if we have a message destination defined.
826 * We always report M_ABORT
828 if (!daemon_msgs || (type != M_ABORT &&
829 !bit_is_set(type, daemon_msgs->send_msg)))
830 return; /* no destination */
833 sprintf(buf, "%s ABORTING due to ERROR in %s:%d\n",
834 my_name, file, line);
837 if (level == -1) /* skip details */
838 sprintf(buf, "%s: Fatal Error because: ", my_name);
840 sprintf(buf, "%s: Fatal Error at %s:%d because:\n", my_name, file, line);
843 if (level == -1) /* skip details */
844 sprintf(buf, "%s: Error: ", my_name);
846 sprintf(buf, "%s: Error in %s:%d ", my_name, file, line);
849 sprintf(buf, "%s: Warning: ", my_name);
852 sprintf(buf, "%s: ", my_name);
857 va_start(arg_ptr, fmt);
858 bvsnprintf(buf+i, sizeof(buf)-i, (char *)fmt, arg_ptr);
861 dispatch_message(NULL, type, level, buf);
863 if (type == M_ABORT) {
868 /* *********************************************************
870 * Generate a Job message
874 Jmsg(void *vjcr, int type, int level, char *fmt,...)
880 JCR *jcr = (JCR *) vjcr;
884 Dmsg1(200, "Enter Jmsg type=%d\n", type);
886 buf = rbuf; /* we are the Director */
888 * Check if we have a message destination defined.
889 * We always report M_ABORT
891 if (type != M_ABORT && jcr->msgs && !bit_is_set(type, jcr->msgs->send_msg)) {
892 Dmsg1(200, "No bit set for type %d\n", type);
893 return; /* no destination */
897 sprintf(buf, "%s ABORTING due to ERROR\n", my_name);
900 sprintf(buf, "%s: Job %s Cancelled because: ", my_name, jcr->Job);
903 sprintf(buf, "%s: Job %s Error: ", my_name, jcr->Job);
906 sprintf(buf, "%s: Job %s Warning: ", my_name, jcr->Job);
909 sprintf(buf, "%s: ", my_name);
914 va_start(arg_ptr, fmt);
915 len = bvsnprintf(buf+i, sizeof(rbuf)-i, fmt, arg_ptr);
918 ASSERT(typesave==type); /* type trashed, compiler bug???? */
919 dispatch_message(jcr, type, level, rbuf);
921 Dmsg3(500, "i=%d sizeof(rbuf)-i=%d len=%d\n", i, sizeof(rbuf)-i, len);
928 * Edit a message into a Pool memory buffer, with file:lineno
930 int m_msg(char *file, int line, char **pool_buf, char *fmt, ...)
935 sprintf(*pool_buf, "%s:%d ", file, line);
936 i = strlen(*pool_buf);
939 maxlen = sizeof_pool_memory(*pool_buf) - i - 1;
940 va_start(arg_ptr, fmt);
941 len = bvsnprintf(*pool_buf+i, maxlen, fmt, arg_ptr);
943 if (len < 0 || len >= maxlen) {
944 *pool_buf = (char *) realloc_pool_memory(*pool_buf, maxlen + i + 200);
951 * Edit a message into a Pool Memory buffer NO file:lineno
952 * Returns: string length of what was edited.
954 int Mmsg(char **pool_buf, char *fmt, ...)
960 maxlen = sizeof_pool_memory(*pool_buf) - 1;
961 va_start(arg_ptr, fmt);
962 len = bvsnprintf(*pool_buf, maxlen, fmt, arg_ptr);
964 if (len < 0 || len >= maxlen) {
965 *pool_buf = (char *) realloc_pool_memory(*pool_buf, maxlen + 200);
972 void j_msg(char *file, int line, void *jcr, int type, int level, char *fmt,...)
978 pool_buf = (char *) get_pool_memory(PM_EMSG);
979 sprintf(pool_buf, "%s:%d ", file, line);
980 i = strlen(pool_buf);
983 maxlen = sizeof_pool_memory(pool_buf) - i - 1;
984 va_start(arg_ptr, fmt);
985 len = bvsnprintf(pool_buf+i, maxlen, fmt, arg_ptr);
987 if (len < 0 || len >= maxlen) {
988 pool_buf = (char *) realloc_pool_memory(pool_buf, maxlen + i + 200);
992 Jmsg(jcr, type, level, pool_buf);
993 free_memory(pool_buf);