2 * Bacula message handling routines
4 * Kern Sibbald, April 2000
11 Copyright (C) 2000-2004 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 #if !defined(HAVE_CONSOLE)
35 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
40 #define FULL_LOCATION 1 /* set for file:line in Debug messages */
43 * This is where we define "Globals" because all the
44 * daemons include this file.
46 const char *working_directory = NULL; /* working directory path stored here */
47 int verbose = 0; /* increase User messages */
48 int debug_level = 0; /* debug level */
49 time_t daemon_start_time = 0; /* Daemon start time */
50 const char *version = VERSION " (" BDATE ")";
51 char my_name[30]; /* daemon name is stored here */
52 char *exepath = (char *)NULL;
53 char *exename = (char *)NULL;
54 int console_msg_pending = 0;
55 char con_fname[500]; /* Console filename */
56 FILE *con_fd = NULL; /* Console file descriptor */
57 brwlock_t con_lock; /* Console lock structure */
59 #ifdef HAVE_POSTGRESQL
60 char catalog_db[] = "PostgreSQL";
63 char catalog_db[] = "MySQL";
66 char catalog_db[] = "SQLite";
68 char catalog_db[] = "Internal";
73 const char *host_os = HOST_OS;
74 const char *distname = DISTNAME;
75 const char *distver = DISTVER;
76 static FILE *trace_fd = NULL;
78 static bool trace = true;
80 static bool trace = false;
83 /* Forward referenced functions */
85 /* Imported functions */
90 /* Used to allow only one thread close the daemon messages at a time */
91 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
92 static MSGS *daemon_msgs; /* global messages */
94 /* Define if e_msg must exit when M_ERROR_TERM is received */
95 static int exit_on_error = 1;
98 * Set daemon name. Also, find canonical execution
99 * path. Note, exepath has spare room for tacking on
100 * the exename so that we can reconstruct the full name.
102 * Note, this routine can get called multiple times
103 * The second time is to put the name as found in the
104 * Resource record. On the second call, generally,
105 * argv is NULL to avoid doing the path code twice.
107 #define BTRACE_EXTRA 20
108 void my_name_is(int argc, char *argv[], const char *name)
114 bstrncpy(my_name, name, sizeof(my_name));
115 if (argc>0 && argv && argv[0]) {
116 /* strip trailing filename and save exepath */
117 for (l=p=argv[0]; *p; p++) {
119 l = p; /* set pos of last slash */
126 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
127 /* On Windows allow c: junk */
137 exename = (char *)malloc(len);
143 exepath = (char *)malloc(strlen(argv[0]) + 1 + len);
144 for (p=argv[0],q=exepath; p < l; ) {
148 if (strchr(exepath, '.') || exepath[0] != '/') {
149 if (getcwd(cpath, sizeof(cpath))) {
151 exepath = (char *)malloc(strlen(cpath) + 1 + len);
152 strcpy(exepath, cpath);
155 Dmsg2(500, "exepath=%s\nexename=%s\n", exepath, exename);
160 * Initialize message handler for a daemon or a Job
161 * We make a copy of the MSGS resource passed, so it belows
162 * to the job or daemon and thus can be modified.
164 * NULL for jcr -> initialize global messages for daemon
165 * non-NULL -> initialize jcr using Message resource
168 init_msg(JCR *jcr, MSGS *msg)
170 DEST *d, *dnew, *temp_chain = NULL;
173 if (jcr == NULL && msg == NULL) {
174 init_last_jobs_list();
179 * Make sure we have fd's 0, 1, 2 open
180 * If we don't do this one of our sockets may open
181 * there and if we then use stdout, it could
182 * send total garbage to our socket.
186 fd = open("/dev/null", O_RDONLY, 0644);
190 for(i=1; fd + i <= 2; i++) {
197 * If msg is NULL, initialize global chain for STDOUT and syslog
200 daemon_msgs = (MSGS *)malloc(sizeof(MSGS));
201 memset(daemon_msgs, 0, sizeof(MSGS));
202 for (i=1; i<=M_MAX; i++) {
204 add_msg_dest(daemon_msgs, MD_STDOUT, i, NULL, NULL);
206 add_msg_dest(daemon_msgs, MD_SYSLOG, i, NULL, NULL);
208 Dmsg1(050, "Create daemon global message resource 0x%x\n", daemon_msgs);
213 * Walk down the message resource chain duplicating it
214 * for the current Job.
216 for (d=msg->dest_chain; d; d=d->next) {
217 dnew = (DEST *)malloc(sizeof(DEST));
218 memcpy(dnew, d, sizeof(DEST));
219 dnew->next = temp_chain;
221 dnew->mail_filename = NULL;
223 dnew->mail_cmd = bstrdup(d->mail_cmd);
226 dnew->where = bstrdup(d->where);
232 jcr->jcr_msgs = (MSGS *)malloc(sizeof(MSGS));
233 memset(jcr->jcr_msgs, 0, sizeof(MSGS));
234 jcr->jcr_msgs->dest_chain = temp_chain;
235 memcpy(jcr->jcr_msgs->send_msg, msg->send_msg, sizeof(msg->send_msg));
237 /* If we have default values, release them now */
239 free_msgs_res(daemon_msgs);
241 daemon_msgs = (MSGS *)malloc(sizeof(MSGS));
242 memset(daemon_msgs, 0, sizeof(MSGS));
243 daemon_msgs->dest_chain = temp_chain;
244 memcpy(daemon_msgs->send_msg, msg->send_msg, sizeof(msg->send_msg));
246 Dmsg2(250, "Copy message resource 0x%x to 0x%x\n", msg, temp_chain);
250 /* Initialize so that the console (User Agent) can
251 * receive messages -- stored in a file.
253 void init_console_msg(const char *wd)
257 bsnprintf(con_fname, sizeof(con_fname), "%s/%s.conmsg", wd, my_name);
258 fd = open(con_fname, O_CREAT|O_RDWR|O_BINARY, 0600);
261 Emsg2(M_ERROR_TERM, 0, _("Could not open console message file %s: ERR=%s\n"),
262 con_fname, be.strerror());
264 if (lseek(fd, 0, SEEK_END) > 0) {
265 console_msg_pending = 1;
268 con_fd = fopen(con_fname, "a+");
271 Emsg2(M_ERROR, 0, _("Could not open console message file %s: ERR=%s\n"),
272 con_fname, be.strerror());
274 if (rwl_init(&con_lock) != 0) {
276 Emsg1(M_ERROR_TERM, 0, _("Could not get con mutex: ERR=%s\n"),
282 * Called only during parsing of the config file.
284 * Add a message destination. I.e. associate a message type with
285 * a destination (code).
286 * Note, where in the case of dest_code FILE is a filename,
287 * but in the case of MAIL is a space separated list of
288 * email addresses, ...
290 void add_msg_dest(MSGS *msg, int dest_code, int msg_type, char *where, char *mail_cmd)
294 * First search the existing chain and see if we
295 * can simply add this msg_type to an existing entry.
297 for (d=msg->dest_chain; d; d=d->next) {
298 if (dest_code == d->dest_code && ((where == NULL && d->where == NULL) ||
299 (strcmp(where, d->where) == 0))) {
300 Dmsg4(200, "Add to existing d=%x msgtype=%d destcode=%d where=%s\n",
301 d, msg_type, dest_code, NPRT(where));
302 set_bit(msg_type, d->msg_types);
303 set_bit(msg_type, msg->send_msg); /* set msg_type bit in our local */
307 /* Not found, create a new entry */
308 d = (DEST *)malloc(sizeof(DEST));
309 memset(d, 0, sizeof(DEST));
310 d->next = msg->dest_chain;
311 d->dest_code = dest_code;
312 set_bit(msg_type, d->msg_types); /* set type bit in structure */
313 set_bit(msg_type, msg->send_msg); /* set type bit in our local */
315 d->where = bstrdup(where);
318 d->mail_cmd = bstrdup(mail_cmd);
320 Dmsg5(200, "add new d=%x msgtype=%d destcode=%d where=%s mailcmd=%s\n",
321 d, msg_type, dest_code, NPRT(where), NPRT(d->mail_cmd));
326 * Called only during parsing of the config file.
328 * Remove a message destination
330 void rem_msg_dest(MSGS *msg, int dest_code, int msg_type, char *where)
334 for (d=msg->dest_chain; d; d=d->next) {
335 Dmsg2(200, "Remove_msg_dest d=%x where=%s\n", d, NPRT(d->where));
336 if (bit_is_set(msg_type, d->msg_types) && (dest_code == d->dest_code) &&
337 ((where == NULL && d->where == NULL) ||
338 (strcmp(where, d->where) == 0))) {
339 Dmsg3(200, "Found for remove d=%x msgtype=%d destcode=%d\n",
340 d, msg_type, dest_code);
341 clear_bit(msg_type, d->msg_types);
342 Dmsg0(200, "Return rem_msg_dest\n");
350 * Create a unique filename for the mail command
352 static void make_unique_mail_filename(JCR *jcr, POOLMEM *&name, DEST *d)
355 Mmsg(name, "%s/%s.mail.%s.%d", working_directory, my_name,
356 jcr->Job, (int)(long)d);
358 Mmsg(name, "%s/%s.mail.%s.%d", working_directory, my_name,
359 my_name, (int)(long)d);
361 Dmsg1(200, "mailname=%s\n", name);
367 static BPIPE *open_mail_pipe(JCR *jcr, POOLMEM *&cmd, DEST *d)
372 cmd = edit_job_codes(jcr, cmd, d->mail_cmd, d->where);
374 Mmsg(cmd, "/usr/lib/sendmail -F Bacula %s", d->where);
378 if (!(bpipe = open_bpipe(cmd, 120, "rw"))) {
380 Jmsg(jcr, M_ERROR, 0, "open mail pipe %s failed: ERR=%s\n",
384 /* If we had to use sendmail, add subject */
386 fprintf(bpipe->wfd, "Subject: Bacula Message\r\n\r\n");
393 * Close the messages for this Messages resource, which means to close
394 * any open files, and dispatch any pending email messages.
396 void close_msg(JCR *jcr)
404 Dmsg1(350, "Close_msg jcr=0x%x\n", jcr);
406 if (jcr == NULL) { /* NULL -> global chain */
408 P(mutex); /* only one thread walking the chain */
410 msgs = jcr->jcr_msgs;
411 jcr->jcr_msgs = NULL;
416 Dmsg1(350, "===Begin close msg resource at 0x%x\n", msgs);
417 cmd = get_pool_memory(PM_MESSAGE);
418 for (d=msgs->dest_chain; d; ) {
420 switch (d->dest_code) {
424 fclose(d->fd); /* close open file descriptor */
428 case MD_MAIL_ON_ERROR:
429 Dmsg0(350, "Got MD_MAIL or MD_MAIL_ON_ERROR\n");
433 if (d->dest_code == MD_MAIL_ON_ERROR && jcr &&
434 jcr->JobStatus == JS_Terminated) {
438 if (!(bpipe=open_mail_pipe(jcr, cmd, d))) {
439 Pmsg0(000, "open mail pipe failed.\n");
442 Dmsg0(350, "Opened mail pipe\n");
444 line = get_memory(len);
446 while (fgets(line, len, d->fd)) {
447 fputs(line, bpipe->wfd);
449 if (!close_wpipe(bpipe)) { /* close write pipe sending mail */
451 Pmsg1(000, "close error: ERR=%s\n", be.strerror());
455 * Since we are closing all messages, before "recursing"
456 * make sure we are not closing the daemon messages, otherwise
459 if (msgs != daemon_msgs) {
460 /* read what mail prog returned -- should be nothing */
461 while (fgets(line, len, bpipe->rfd)) {
462 Jmsg1(jcr, M_INFO, 0, _("Mail prog: %s"), line);
466 stat = close_bpipe(bpipe);
467 if (stat != 0 && msgs != daemon_msgs) {
470 Dmsg1(350, "Calling emsg. CMD=%s\n", cmd);
471 Jmsg2(jcr, M_ERROR, 0, _("Mail program terminated in error.\n"
473 "ERR=%s\n"), cmd, be.strerror());
477 /* Remove temp file */
479 unlink(d->mail_filename);
480 free_pool_memory(d->mail_filename);
481 d->mail_filename = NULL;
482 Dmsg0(350, "end mail or mail on error\n");
489 d = d->next; /* point to next buffer */
491 free_pool_memory(cmd);
492 Dmsg0(350, "Done walking message chain.\n");
499 Dmsg0(350, "===End close msg resource\n");
503 * Free memory associated with Messages resource
505 void free_msgs_res(MSGS *msgs)
509 /* Walk down the message chain releasing allocated buffers */
510 for (d=msgs->dest_chain; d; ) {
517 old = d; /* save pointer to release */
518 d = d->next; /* point to next buffer */
519 free(old); /* free the destination item */
521 msgs->dest_chain = NULL;
522 free(msgs); /* free the head */
527 * Terminate the message handler for good.
528 * Release the global destination chain.
530 * Also, clean up a few other items (cons, exepath). Note,
531 * these really should be done elsewhere.
535 Dmsg0(300, "Enter term_msg\n");
536 close_msg(NULL); /* close global chain */
537 free_msgs_res(daemon_msgs); /* free the resources */
556 term_last_jobs_list();
562 * Handle sending the message to the appropriate place
564 void dispatch_message(JCR *jcr, int type, time_t mtime, char *msg)
567 char dt[MAX_TIME_LENGTH];
573 Dmsg2(800, "Enter dispatch_msg type=%d msg=%s\n", type, msg);
576 * Most messages are prefixed by a date and time. If mtime is
577 * zero, then we use the current time. If mtime is 1 (special
578 * kludge), we do not prefix the date and time. Otherwise,
579 * we assume mtime is a time_t and use it.
588 bstrftime_ny(dt, sizeof(dt), mtime);
594 if (type == M_ABORT || type == M_ERROR_TERM) {
597 fputs(msg, stdout); /* print this here to INSURE that it is printed */
600 #if !defined(HAVE_CONSOLE)
601 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
602 /* If we don't exit on error, error messages are parsed by UA */
604 MessageBox(NULL, msg, "Bacula", MB_OK);
610 /* Now figure out where to send the message */
613 msgs = jcr->jcr_msgs;
618 for (d=msgs->dest_chain; d; d=d->next) {
619 if (bit_is_set(type, d->msg_types)) {
620 switch (d->dest_code) {
622 Dmsg1(800, "CONSOLE for following msg: %s", msg);
624 con_fd = fopen(con_fname, "a+");
625 Dmsg0(800, "Console file not open.\n");
628 Pw(con_lock); /* get write lock on console message file */
631 fwrite(dt, dtlen, 1, con_fd);
635 fwrite(msg, len, 1, con_fd);
636 if (msg[len-1] != '\n') {
637 fwrite("\n", 2, 1, con_fd);
640 fwrite("\n", 2, 1, con_fd);
643 console_msg_pending = TRUE;
648 Dmsg1(800, "SYSLOG for collowing msg: %s\n", msg);
650 * We really should do an openlog() here.
652 syslog(LOG_DAEMON|LOG_ERR, "%s", msg);
655 Dmsg1(800, "OPERATOR for following msg: %s\n", msg);
656 mcmd = get_pool_memory(PM_MESSAGE);
657 if ((bpipe=open_mail_pipe(jcr, mcmd, d))) {
659 fputs(dt, bpipe->wfd);
660 fputs(msg, bpipe->wfd);
661 /* Messages to the operator go one at a time */
662 stat = close_bpipe(bpipe);
666 Qmsg2(jcr, M_ERROR, 0, _("Operator mail program terminated in error.\n"
668 "ERR=%s\n"), mcmd, be.strerror());
671 free_pool_memory(mcmd);
674 case MD_MAIL_ON_ERROR:
675 Dmsg1(800, "MAIL for following msg: %s", msg);
677 POOLMEM *name = get_pool_memory(PM_MESSAGE);
678 make_unique_mail_filename(jcr, name, d);
679 d->fd = fopen(name, "w+");
683 Qmsg2(jcr, M_ERROR, 0, "fopen %s failed: ERR=%s\n", name,
686 free_pool_memory(name);
689 d->mail_filename = name;
692 len = strlen(msg) + dtlen;;
693 if (len > d->max_len) {
694 d->max_len = len; /* keep max line length */
699 Dmsg1(800, "FILE for following msg: %s", msg);
701 d->fd = fopen(d->where, "w+");
705 Qmsg2(jcr, M_ERROR, 0, "fopen %s failed: ERR=%s\n", d->where,
715 Dmsg1(800, "APPEND for following msg: %s", msg);
717 d->fd = fopen(d->where, "a");
721 Qmsg2(jcr, M_ERROR, 0, "fopen %s failed: ERR=%s\n", d->where,
731 Dmsg1(800, "DIRECTOR for following msg: %s", msg);
732 if (jcr && jcr->dir_bsock && !jcr->dir_bsock->errors) {
733 bnet_fsend(jcr->dir_bsock, "Jmsg Job=%s type=%d level=%d %s",
734 jcr->Job, type, mtime, msg);
738 Dmsg1(800, "STDOUT for following msg: %s", msg);
739 if (type != M_ABORT && type != M_ERROR_TERM) { /* already printed */
745 Dmsg1(800, "STDERR for following msg: %s", msg);
757 /*********************************************************************
759 * This subroutine prints a debug message if the level number
760 * is less than or equal the debug_level. File and line numbers
761 * are included for more detail if desired, but not currently
764 * If the level is negative, the details of file and line number
768 d_msg(const char *file, int line, int level, const char *fmt,...)
780 if (level <= debug_level) {
783 /* visual studio passes the whole path to the file as well
784 * which makes for very long lines
786 const char *f = strrchr(file, '\\');
788 len = bsnprintf(buf, sizeof(buf), "%s: %s:%d ", my_name, file, line);
795 va_start(arg_ptr, fmt);
796 bvsnprintf(buf+len, sizeof(buf)-len, (char *)fmt, arg_ptr);
800 * Used the "trace on" command in the console to turn on
801 * output to the trace file. "trace off" will close the file.
805 bsnprintf(buf, sizeof(buf), "%s/bacula.trace", working_directory ? working_directory : ".");
806 trace_fd = fopen(buf, "a+");
809 fputs(buf, trace_fd);
812 } else { /* not tracing */
819 * Set trace flag on/off. If argument is negative, there is no change
821 void set_trace(int trace_flag)
823 if (trace_flag < 0) {
825 } else if (trace_flag > 0) {
830 if (!trace && trace_fd) {
831 FILE *ltrace_fd = trace_fd;
833 bmicrosleep(0, 100000); /* yield to prevent seg faults */
843 /*********************************************************************
845 * This subroutine prints a message regardless of the debug level
847 * If the level is negative, the details of file and line number
851 p_msg(const char *file, int line, int level, const char *fmt,...)
859 len = bsnprintf(buf, sizeof(buf), "%s: %s:%d ", my_name, file, line);
866 va_start(arg_ptr, fmt);
867 bvsnprintf(buf+len, sizeof(buf)-len, (char *)fmt, arg_ptr);
873 /*********************************************************************
875 * subroutine writes a debug message to the trace file if the level number
876 * is less than or equal the debug_level. File and line numbers
877 * are included for more detail if desired, but not currently
880 * If the level is negative, the details of file and line number
884 t_msg(const char *file, int line, int level, const char *fmt,...)
896 if (level <= debug_level) {
898 bsnprintf(buf, sizeof(buf), "%s/bacula.trace", working_directory);
899 trace_fd = fopen(buf, "a+");
904 len = bsnprintf(buf, sizeof(buf), "%s: %s:%d ", my_name, file, line);
911 va_start(arg_ptr, fmt);
912 bvsnprintf(buf+len, sizeof(buf)-len, (char *)fmt, arg_ptr);
914 if (trace_fd != NULL) {
915 fputs(buf, trace_fd);
923 /* *********************************************************
925 * print an error message
929 e_msg(const char *file, int line, int type, int level, const char *fmt,...)
936 * Check if we have a message destination defined.
937 * We always report M_ABORT and M_ERROR_TERM
939 if (!daemon_msgs || ((type != M_ABORT && type != M_ERROR_TERM) &&
940 !bit_is_set(type, daemon_msgs->send_msg))) {
941 return; /* no destination */
945 len = bsnprintf(buf, sizeof(buf), "%s: ABORTING due to ERROR in %s:%d\n",
946 my_name, file, line);
949 len = bsnprintf(buf, sizeof(buf), "%s: ERROR TERMINATION at %s:%d\n",
950 my_name, file, line);
953 if (level == -1) /* skip details */
954 len = bsnprintf(buf, sizeof(buf), "%s: Fatal Error because: ", my_name);
956 len = bsnprintf(buf, sizeof(buf), "%s: Fatal Error at %s:%d because:\n", my_name, file, line);
959 if (level == -1) /* skip details */
960 len = bsnprintf(buf, sizeof(buf), "%s: ERROR: ", my_name);
962 len = bsnprintf(buf, sizeof(buf), "%s: ERROR in %s:%d ", my_name, file, line);
965 len = bsnprintf(buf, sizeof(buf), "%s: Warning: ", my_name);
968 len = bsnprintf(buf, sizeof(buf), "%s: Security violation: ", my_name);
971 len = bsnprintf(buf, sizeof(buf), "%s: ", my_name);
975 va_start(arg_ptr, fmt);
976 bvsnprintf(buf+len, sizeof(buf)-len, (char *)fmt, arg_ptr);
979 dispatch_message(NULL, type, 0, buf);
981 if (type == M_ABORT) {
983 p[0] = 0; /* generate segmentation violation */
985 if ((type == M_ERROR_TERM) && exit_on_error) {
990 /* *********************************************************
992 * Generate a Job message
996 Jmsg(JCR *jcr, int type, time_t mtime, const char *fmt,...)
1005 Dmsg1(800, "Enter Jmsg type=%d\n", type);
1007 /* Special case for the console, which has a dir_bsock and JobId==0,
1008 * in that case, we send the message directly back to the
1011 if (jcr && jcr->JobId == 0 && jcr->dir_bsock) {
1012 BSOCK *dir = jcr->dir_bsock;
1013 va_start(arg_ptr, fmt);
1014 dir->msglen = bvsnprintf(dir->msg, sizeof_pool_memory(dir->msg),
1017 bnet_send(jcr->dir_bsock);
1024 msgs = jcr->jcr_msgs;
1028 msgs = daemon_msgs; /* if no jcr, we use daemon handler */
1031 job = ""; /* Set null job name if none */
1035 * Check if we have a message destination defined.
1036 * We always report M_ABORT and M_ERROR_TERM
1038 if (msgs && (type != M_ABORT && type != M_ERROR_TERM) &&
1039 !bit_is_set(type, msgs->send_msg)) {
1040 return; /* no destination */
1044 len = bsnprintf(rbuf, sizeof(rbuf), "%s ABORTING due to ERROR\n", my_name);
1047 len = bsnprintf(rbuf, sizeof(rbuf), "%s ERROR TERMINATION\n", my_name);
1050 len = bsnprintf(rbuf, sizeof(rbuf), "%s: %s Fatal error: ", my_name, job);
1052 set_jcr_job_status(jcr, JS_FatalError);
1056 len = bsnprintf(rbuf, sizeof(rbuf), "%s: %s Error: ", my_name, job);
1062 len = bsnprintf(rbuf, sizeof(rbuf), "%s: %s Warning: ", my_name, job);
1065 len = bsnprintf(rbuf, sizeof(rbuf), "%s: %s Security violation: ", my_name, job);
1068 len = bsnprintf(rbuf, sizeof(rbuf), "%s: ", my_name);
1072 va_start(arg_ptr, fmt);
1073 bvsnprintf(rbuf+len, sizeof(rbuf)-len, fmt, arg_ptr);
1076 dispatch_message(jcr, type, mtime, rbuf);
1078 if (type == M_ABORT){
1080 p[0] = 0; /* generate segmentation violation */
1082 if ((type == M_ERROR_TERM) && exit_on_error) {
1088 * If we come here, prefix the message with the file:line-number,
1089 * then pass it on to the normal Jmsg routine.
1091 void j_msg(const char *file, int line, JCR *jcr, int type, time_t mtime, const char *fmt,...)
1097 pool_buf = get_pool_memory(PM_EMSG);
1098 i = Mmsg(pool_buf, "%s:%d ", file, line);
1101 maxlen = sizeof_pool_memory(pool_buf) - i - 1;
1102 va_start(arg_ptr, fmt);
1103 len = bvsnprintf(pool_buf+i, maxlen, fmt, arg_ptr);
1105 if (len < 0 || len >= (maxlen-5)) {
1106 pool_buf = realloc_pool_memory(pool_buf, maxlen + i + maxlen/2);
1112 Jmsg(jcr, type, mtime, "%s", pool_buf);
1113 free_memory(pool_buf);
1118 * Edit a message into a Pool memory buffer, with file:lineno
1120 int m_msg(const char *file, int line, POOLMEM **pool_buf, const char *fmt, ...)
1125 i = sprintf(*pool_buf, "%s:%d ", file, line);
1128 maxlen = sizeof_pool_memory(*pool_buf) - i - 1;
1129 va_start(arg_ptr, fmt);
1130 len = bvsnprintf(*pool_buf+i, maxlen, fmt, arg_ptr);
1132 if (len < 0 || len >= (maxlen-5)) {
1133 *pool_buf = realloc_pool_memory(*pool_buf, maxlen + i + maxlen/2);
1141 int m_msg(const char *file, int line, POOLMEM *&pool_buf, const char *fmt, ...)
1146 i = sprintf(pool_buf, "%s:%d ", file, line);
1149 maxlen = sizeof_pool_memory(pool_buf) - i - 1;
1150 va_start(arg_ptr, fmt);
1151 len = bvsnprintf(pool_buf+i, maxlen, fmt, arg_ptr);
1153 if (len < 0 || len >= (maxlen-5)) {
1154 pool_buf = realloc_pool_memory(pool_buf, maxlen + i + maxlen/2);
1164 * Edit a message into a Pool Memory buffer NO file:lineno
1165 * Returns: string length of what was edited.
1167 int Mmsg(POOLMEM **pool_buf, const char *fmt, ...)
1173 maxlen = sizeof_pool_memory(*pool_buf) - 1;
1174 va_start(arg_ptr, fmt);
1175 len = bvsnprintf(*pool_buf, maxlen, fmt, arg_ptr);
1177 if (len < 0 || len >= (maxlen-5)) {
1178 *pool_buf = realloc_pool_memory(*pool_buf, maxlen + maxlen/2);
1186 int Mmsg(POOLMEM *&pool_buf, const char *fmt, ...)
1192 maxlen = sizeof_pool_memory(pool_buf) - 1;
1193 va_start(arg_ptr, fmt);
1194 len = bvsnprintf(pool_buf, maxlen, fmt, arg_ptr);
1196 if (len < 0 || len >= (maxlen-5)) {
1197 pool_buf = realloc_pool_memory(pool_buf, maxlen + maxlen/2);
1205 int Mmsg(POOL_MEM &pool_buf, const char *fmt, ...)
1211 maxlen = pool_buf.max_size() - 1;
1212 va_start(arg_ptr, fmt);
1213 len = bvsnprintf(pool_buf.c_str(), maxlen, fmt, arg_ptr);
1215 if (len < 0 || len >= (maxlen-5)) {
1216 pool_buf.realloc_pm(maxlen + maxlen/2);
1225 static pthread_mutex_t msg_queue_mutex = PTHREAD_MUTEX_INITIALIZER;
1228 * We queue messages rather than print them directly. This
1229 * is generally used in low level routines (msg handler, bnet)
1230 * to prevent recursion (i.e. if you are in the middle of
1231 * sending a message, it is a bit messy to recursively call
1232 * yourself when the bnet packet is not reentrant).
1234 void Qmsg(JCR *jcr, int type, time_t mtime, const char *fmt,...)
1241 pool_buf = get_pool_memory(PM_EMSG);
1244 maxlen = sizeof_pool_memory(pool_buf) - 1;
1245 va_start(arg_ptr, fmt);
1246 len = bvsnprintf(pool_buf, maxlen, fmt, arg_ptr);
1248 if (len < 0 || len >= (maxlen-5)) {
1249 pool_buf = realloc_pool_memory(pool_buf, maxlen + maxlen/2);
1254 item = (MQUEUE_ITEM *)malloc(sizeof(MQUEUE_ITEM) + strlen(pool_buf) + 1);
1256 item->mtime = time(NULL);
1257 strcpy(item->msg, pool_buf);
1258 /* If no jcr or dequeuing send to daemon to avoid recursion */
1259 if (!jcr || jcr->dequeuing) {
1260 /* jcr==NULL => daemon message, safe to send now */
1261 Jmsg(NULL, item->type, item->mtime, "%s", item->msg);
1264 /* Queue message for later sending */
1266 jcr->msg_queue->append(item);
1268 // Dmsg1(000, "queue item=%lu\n", (long unsigned)item);
1270 free_memory(pool_buf);
1276 void dequeue_messages(JCR *jcr)
1280 jcr->dequeuing = true;
1281 foreach_dlist(item, jcr->msg_queue) {
1282 // Dmsg1(000, "dequeue item=%lu\n", (long unsigned)item);
1283 Jmsg(jcr, item->type, item->mtime, "%s", item->msg);
1285 jcr->msg_queue->destroy();
1286 jcr->dequeuing = false;
1292 * If we come here, prefix the message with the file:line-number,
1293 * then pass it on to the normal Qmsg routine.
1295 void q_msg(const char *file, int line, JCR *jcr, int type, time_t mtime, const char *fmt,...)
1301 pool_buf = get_pool_memory(PM_EMSG);
1302 i = Mmsg(pool_buf, "%s:%d ", file, line);
1305 maxlen = sizeof_pool_memory(pool_buf) - i - 1;
1306 va_start(arg_ptr, fmt);
1307 len = bvsnprintf(pool_buf+i, maxlen, fmt, arg_ptr);
1309 if (len < 0 || len >= (maxlen-5)) {
1310 pool_buf = realloc_pool_memory(pool_buf, maxlen + i + maxlen/2);
1316 Qmsg(jcr, type, mtime, "%s", pool_buf);
1317 free_memory(pool_buf);
1321 * Define if e_msg must exit when M_ERROR_TERM is received
1323 void set_exit_on_error(int value) {
1324 exit_on_error = value;