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(POOLMEM **base, char **msg, char *str)
286 int len = strlen(str) + 1;
290 *base = 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)
309 case JS_ErrorTerminated:
323 str = "Unknown term code";
331 * Convert Job Type into a string
333 static char *job_type_to_str(int type)
348 str = "Unknown Job Type";
355 * Convert Job Level into a string
357 static char *job_level_to_str(int level)
369 str = "differential";
377 case L_VERIFY_CATALOG:
378 str = "verify catalog";
383 case L_VERIFY_VOLUME:
384 str = "verify volume";
390 str = "Unknown Job level";
398 * Edit job codes into main command line
401 * %t = Job type (Backup, ...)
406 * %d = Director's name
408 static char *edit_job_codes(JCR *jcr, char *omsg, char *imsg, char *to)
413 Dmsg1(200, "edit_job_codes: %s\n", imsg);
416 for (p=imsg; *p; p++) {
424 case 'j': /* Job name */
428 str = job_status_to_str(jcr->JobStatus);
431 str = job_type_to_str(jcr->JobType);
437 str = job_level_to_str(jcr->level);
440 str = jcr->client_name;
446 str = my_name; /* Director's name */
459 Dmsg1(200, "add_str %s\n", str);
460 add_str(&omsg, &o, str);
462 Dmsg1(200, "omsg=%s\n", omsg);
469 * Create a unique filename for the mail command
471 static void make_unique_mail_filename(JCR *jcr, char **name, DEST *d)
474 Mmsg(name, "%s/%s.mail.%s.%d", working_directory, my_name,
477 Mmsg(name, "%s/%s.mail.%s.%d", working_directory, my_name,
480 Dmsg1(200, "mailname=%s\n", *name);
486 static FILE *open_mail_pipe(JCR *jcr, char **cmd, DEST *d)
490 if (d->mail_cmd && jcr) {
491 *cmd = edit_job_codes(jcr, *cmd, d->mail_cmd, d->where);
493 Mmsg(cmd, "mail -s \"Bacula Message\" %s", d->where);
495 Dmsg1(200, "mailcmd=%s\n", cmd);
496 pfd = popen(*cmd, "w");
498 Jmsg(jcr, M_ERROR, 0, "mail popen %s failed: ERR=%s\n", cmd, strerror(errno));
504 * Close the messages for this Messages resource, which means to close
505 * any open files, and dispatch any pending email messages.
507 void close_msg(void *vjcr)
510 JCR *jcr = (JCR *)vjcr;
516 Dmsg1(050, "Close_msg jcr=0x%x\n", jcr);
518 if (jcr == NULL) { /* NULL -> global chain */
528 Dmsg1(050, "close msg resource at 0x%x\n", msgs);
529 cmd = get_pool_memory(PM_MESSAGE);
530 for (d=msgs->dest_chain; d; ) {
532 switch (d->dest_code) {
536 fclose(d->fd); /* close open file descriptor */
540 case MD_MAIL_ON_ERROR:
544 if (d->dest_code == MD_MAIL_ON_ERROR && jcr &&
545 jcr->JobStatus == JS_Terminated) {
549 pfd = open_mail_pipe(jcr, &cmd, d);
554 line = get_memory(len);
556 while (fgets(line, len, d->fd)) {
559 pclose(pfd); /* close pipe, sending mail */
562 /* Remove temp file */
564 unlink(d->mail_filename);
565 free_pool_memory(d->mail_filename);
566 d->mail_filename = NULL;
573 d = d->next; /* point to next buffer */
575 free_pool_memory(cmd);
582 * Free memory associated with Messages resource
584 void free_msgs_res(MSGS *msgs)
588 for (d=msgs->dest_chain; d; ) {
595 old = d; /* save pointer to release */
596 d = d->next; /* point to next buffer */
597 free(old); /* free the destination item */
599 msgs->dest_chain = NULL;
605 * Terminate the message handler for good.
606 * Release the global destination chain.
608 * Also, clean up a few other items (cons, exepath). Note,
609 * these really should be done elsewhere.
613 Dmsg0(100, "Enter term_msg\n");
614 close_msg(NULL); /* close global chain */
634 * Handle sending the message to the appropriate place
636 void dispatch_message(void *vjcr, int type, int level, char *msg)
641 JCR *jcr = (JCR *) vjcr;
645 Dmsg2(200, "Enter dispatch_msg type=%d msg=%s\n", type, msg);
647 if (type == M_ABORT) {
648 fprintf(stdout, msg); /* print this here to INSURE that it is printed */
651 /* Now figure out where to send the message */
659 for (d=msgs->dest_chain; d; d=d->next) {
660 if (bit_is_set(type, d->msg_types)) {
661 switch (d->dest_code) {
663 Dmsg1(200, "CONSOLE for following err: %s\n", msg);
665 con_fd = fopen(con_fname, "a+");
666 Dmsg0(200, "Console file not open.\n");
669 fcntl(fileno(con_fd), F_SETLKW);
671 bstrftime(cmd, sizeof(cmd), time(NULL));
674 fwrite(cmd, len, 1, con_fd);
676 if (len > 0 && msg[len-1] != '\n') {
680 fwrite(msg, len, 1, con_fd);
682 fcntl(fileno(con_fd), F_UNLCK);
683 console_msg_pending = TRUE;
687 Dmsg1(200, "SYSLOG for following err: %s\n", msg);
688 /* We really should do an openlog() here */
689 syslog(LOG_DAEMON|LOG_ERR, msg);
692 Dmsg1(200, "OPERATOR for following err: %s\n", msg);
693 mcmd = get_pool_memory(PM_MESSAGE);
694 d->fd = open_mail_pipe(jcr, &mcmd, d);
695 free_pool_memory(mcmd);
698 /* Messages to the operator go one at a time */
704 case MD_MAIL_ON_ERROR:
705 Dmsg1(200, "MAIL for following err: %s\n", msg);
707 POOLMEM *name = get_pool_memory(PM_MESSAGE);
708 make_unique_mail_filename(jcr, &name, d);
709 d->fd = fopen(name, "w+");
711 Emsg2(M_ERROR, 0, "fopen %s failed: ERR=%s\n", name, strerror(errno));
712 free_pool_memory(name);
715 d->mail_filename = name;
718 if (len > d->max_len) {
719 d->max_len = len; /* keep max line length */
724 Dmsg1(200, "FILE for following err: %s\n", msg);
726 d->fd = fopen(d->where, "w+");
728 Emsg2(M_ERROR, 0, "fopen %s failed: ERR=%s\n", d->where, strerror(errno));
735 Dmsg1(200, "APPEND for following err: %s\n", msg);
737 d->fd = fopen(d->where, "a");
739 Emsg2(M_ERROR, 0, "fopen %s failed: ERR=%s\n", d->where, strerror(errno));
746 Dmsg1(200, "DIRECTOR for following err: %s\n", msg);
747 if (jcr && jcr->dir_bsock && !jcr->dir_bsock->errors) {
749 jcr->dir_bsock->msglen = Mmsg(&(jcr->dir_bsock->msg),
750 "Jmsg Job=%s type=%d level=%d %s", jcr->Job,
751 type, level, msg) + 1;
752 bnet_send(jcr->dir_bsock);
756 Dmsg1(200, "STDOUT for following err: %s\n", msg);
757 if (type != M_ABORT && type != M_FATAL) /* already printed */
758 fprintf(stdout, msg);
761 Dmsg1(200, "STDERR for following err: %s\n", msg);
762 fprintf(stderr, msg);
772 /*********************************************************************
774 * subroutine prints a debug message if the level number
775 * is less than or equal the debug_level. File and line numbers
776 * are included for more detail if desired, but not currently
779 * If the level is negative, the details of file and line number
783 d_msg(char *file, int line, int level, char *fmt,...)
795 /* printf("level=%d debug=%d fmt=%s\n", level, debug_level, fmt); */
797 if (level <= debug_level) {
800 sprintf(buf, "%s: %s:%d ", my_name, file, line);
808 va_start(arg_ptr, fmt);
809 bvsnprintf(buf+i, sizeof(buf)-i, (char *)fmt, arg_ptr);
812 fprintf(stdout, buf);
817 /* *********************************************************
819 * print an error message
823 e_msg(char *file, int line, int type, int level, char *fmt,...)
830 * Check if we have a message destination defined.
831 * We always report M_ABORT
833 if (!daemon_msgs || (type != M_ABORT &&
834 !bit_is_set(type, daemon_msgs->send_msg)))
835 return; /* no destination */
838 sprintf(buf, "%s ABORTING due to ERROR in %s:%d\n",
839 my_name, file, line);
842 if (level == -1) /* skip details */
843 sprintf(buf, "%s: Fatal Error because: ", my_name);
845 sprintf(buf, "%s: Fatal Error at %s:%d because:\n", my_name, file, line);
848 if (level == -1) /* skip details */
849 sprintf(buf, "%s: Error: ", my_name);
851 sprintf(buf, "%s: Error in %s:%d ", my_name, file, line);
854 sprintf(buf, "%s: Warning: ", my_name);
857 sprintf(buf, "%s: ", my_name);
862 va_start(arg_ptr, fmt);
863 bvsnprintf(buf+i, sizeof(buf)-i, (char *)fmt, arg_ptr);
866 dispatch_message(NULL, type, level, buf);
868 if (type == M_ABORT) {
873 /* *********************************************************
875 * Generate a Job message
879 Jmsg(void *vjcr, int type, int level, char *fmt,...)
885 JCR *jcr = (JCR *) vjcr;
891 Dmsg1(200, "Enter Jmsg type=%d\n", type);
906 buf = rbuf; /* we are the Director */
908 * Check if we have a message destination defined.
909 * We always report M_ABORT
911 if (type != M_ABORT && msgs && !bit_is_set(type, msgs->send_msg)) {
912 Dmsg1(200, "No bit set for type %d\n", type);
913 return; /* no destination */
917 sprintf(buf, "%s ABORTING due to ERROR\n", my_name);
920 sprintf(buf, "%s: Job %s Fatal error: ", my_name, job);
922 jcr->JobStatus = JS_FatalError;
926 sprintf(buf, "%s: Job %s Error: ", my_name, job);
932 sprintf(buf, "%s: Job %s Warning: ", my_name, job);
935 sprintf(buf, "%s: ", my_name);
940 va_start(arg_ptr, fmt);
941 len = bvsnprintf(buf+i, sizeof(rbuf)-i, fmt, arg_ptr);
944 ASSERT(typesave==type); /* type trashed, compiler bug???? */
945 dispatch_message(jcr, type, level, rbuf);
947 Dmsg3(500, "i=%d sizeof(rbuf)-i=%d len=%d\n", i, sizeof(rbuf)-i, len);
954 * Edit a message into a Pool memory buffer, with file:lineno
956 int m_msg(char *file, int line, POOLMEM **pool_buf, char *fmt, ...)
961 sprintf(*pool_buf, "%s:%d ", file, line);
962 i = strlen(*pool_buf);
965 maxlen = sizeof_pool_memory(*pool_buf) - i - 1;
966 va_start(arg_ptr, fmt);
967 len = bvsnprintf(*pool_buf+i, maxlen, fmt, arg_ptr);
969 if (len < 0 || len >= maxlen) {
970 *pool_buf = realloc_pool_memory(*pool_buf, maxlen + i + 200);
977 * Edit a message into a Pool Memory buffer NO file:lineno
978 * Returns: string length of what was edited.
980 int Mmsg(POOLMEM **pool_buf, char *fmt, ...)
986 maxlen = sizeof_pool_memory(*pool_buf) - 1;
987 va_start(arg_ptr, fmt);
988 len = bvsnprintf(*pool_buf, maxlen, fmt, arg_ptr);
990 if (len < 0 || len >= maxlen) {
991 *pool_buf = realloc_pool_memory(*pool_buf, maxlen + 200);
999 * If we come here, prefix the message with the file:line-number,
1000 * then pass it on to the normal Jmsg routine.
1002 void j_msg(char *file, int line, void *jcr, int type, int level, char *fmt,...)
1008 pool_buf = get_pool_memory(PM_EMSG);
1009 sprintf(pool_buf, "%s:%d ", file, line);
1010 i = strlen(pool_buf);
1013 maxlen = sizeof_pool_memory(pool_buf) - i - 1;
1014 va_start(arg_ptr, fmt);
1015 len = bvsnprintf(pool_buf+i, maxlen, fmt, arg_ptr);
1017 if (len < 0 || len >= maxlen) {
1018 pool_buf = realloc_pool_memory(pool_buf, maxlen + i + 200);
1022 Jmsg(jcr, type, level, pool_buf);
1023 free_memory(pool_buf);