7 /* diagnostics output management
9 /* #include <msg_output.h>
11 /* typedef void (*MSG_OUTPUT_FN)(int level, char *text)
13 /* void msg_output(output_fn)
14 /* MSG_OUTPUT_FN output_fn;
16 /* void msg_printf(level, format, ...)
18 /* const char *format;
20 /* void msg_vprintf(level, format, ap)
22 /* const char *format;
25 /* void msg_text(level, text)
29 /* This module implements low-level output management for the
30 /* msg(3) diagnostics interface.
32 /* msg_output() registers an output handler for the diagnostics
33 /* interface. An application can register multiple output handlers.
34 /* Output handlers are called in the specified order.
35 /* An output handler takes as arguments a severity level (MSG_INFO,
36 /* MSG_WARN, MSG_ERROR, MSG_FATAL, MSG_PANIC, monotonically increasing
37 /* integer values ranging from 0 to MSG_LAST) and pre-formatted,
38 /* sanitized, text in the form of a null-terminated string.
40 /* msg_printf() and msg_vprintf() format their arguments, sanitize the
41 /* result, and call the output handlers registered with msg_output().
43 /* msg_text() copies a pre-formatted text, sanitizes the result, and
44 /* calls the output handlers registered with msg_output().
48 /* The above output routines are protected against ordinary
49 /* recursive calls and against re-entry by signal
50 /* handlers, with the following limitations:
52 /* The signal handlers must never return. In other words, the
53 /* signal handlers must do one or more of the following: call
54 /* _exit(), kill the process with a signal, and permanently
57 /* The signal handlers must call the above output routines not
58 /* until after msg_output() completes initialization, and not
59 /* until after the first formatted output to a VSTRING or
62 /* Each msg_output() call-back function, and each Postfix or
63 /* system function called by that call-back function, either
64 /* must protect itself against recursive calls and re-entry
65 /* by a terminating signal handler, or it must be called
66 /* exclusively by functions in the msg_output(3) module.
68 /* When re-entrancy is detected, the requested output operation
69 /* is skipped. This prevents memory corruption of VSTREAM_ERR
70 /* data structures, and prevents deadlock on Linux releases
71 /* that use mutexes within system library routines such as
72 /* syslog(). This protection exists under the condition that
73 /* these specific resources are accessed exclusively via
74 /* msg_output() call-back functions.
78 /* The Secure Mailer license must be distributed with this software.
81 /* IBM T.J. Watson Research
83 /* Yorktown Heights, NY 10598, USA
92 /* Utility library. */
97 #include <msg_vstream.h>
98 #include <stringops.h>
100 #include <msg_output.h>
103 * Global scope, to discourage the compiler from doing smart things.
105 volatile int msg_vprintf_lock
;
106 volatile int msg_text_lock
;
111 static MSG_OUTPUT_FN
*msg_output_fn
= 0;
112 static int msg_output_fn_count
= 0;
113 static VSTRING
*msg_buffer
= 0;
115 /* msg_output - specify output handler */
117 void msg_output(MSG_OUTPUT_FN output_fn
)
121 * Allocate all resources during initialization.
124 msg_buffer
= vstring_alloc(100);
127 * We're not doing this often, so avoid complexity and allocate memory
130 if (msg_output_fn_count
== 0)
131 msg_output_fn
= (MSG_OUTPUT_FN
*) mymalloc(sizeof(*msg_output_fn
));
133 msg_output_fn
= (MSG_OUTPUT_FN
*) myrealloc((char *) msg_output_fn
,
134 (msg_output_fn_count
+ 1) * sizeof(*msg_output_fn
));
135 msg_output_fn
[msg_output_fn_count
++] = output_fn
;
138 /* msg_printf - format text and log it */
140 void msg_printf(int level
, const char *format
,...)
144 va_start(ap
, format
);
145 msg_vprintf(level
, format
, ap
);
149 /* msg_vprintf - format text and log it */
151 void msg_vprintf(int level
, const char *format
, va_list ap
)
153 if (msg_vprintf_lock
== 0) {
154 msg_vprintf_lock
= 1;
155 /* On-the-fly initialization for debugging test programs only. */
156 if (msg_output_fn_count
== 0)
157 msg_vstream_init("unknown", VSTREAM_ERR
);
158 /* OK if terminating signal handler hijacks control before next stmt. */
159 vstring_vsprintf(msg_buffer
, percentm(format
, errno
), ap
);
160 msg_text(level
, vstring_str(msg_buffer
));
161 msg_vprintf_lock
= 0;
165 /* msg_text - sanitize and log pre-formatted text */
167 void msg_text(int level
, const char *text
)
172 * Sanitize the text. Use a private copy if necessary.
174 if (msg_text_lock
== 0) {
176 /* OK if terminating signal handler hijacks control before next stmt. */
177 if (text
!= vstring_str(msg_buffer
))
178 vstring_strcpy(msg_buffer
, text
);
179 printable(vstring_str(msg_buffer
), '?');
180 /* On-the-fly initialization for debugging test programs only. */
181 if (msg_output_fn_count
== 0)
182 msg_vstream_init("unknown", VSTREAM_ERR
);
183 for (i
= 0; i
< msg_output_fn_count
; i
++)
184 msg_output_fn
[i
] (level
, vstring_str(msg_buffer
));