7 /* direct diagnostics to syslog daemon
9 /* #include <msg_syslog.h>
11 /* void msg_syslog_init(progname, log_opt, facility)
12 /* const char *progname;
16 /* int msg_syslog_facility(facility_name)
17 /* const char *facility_name;
19 /* This module implements support to report msg(3) diagnostics
20 /* via the syslog daemon.
22 /* msg_syslog_init() is a wrapper around the openlog(3) routine
23 /* that directs subsequent msg(3) output to the syslog daemon.
25 /* msg_syslog_facility() is a helper routine that overrides the
26 /* logging facility that is specified with msg_syslog_init().
27 /* The result is zero in case of an unknown facility name.
29 /* syslog(3) syslog library
30 /* msg(3) diagnostics module
32 /* Output records are truncated to 2000 characters. This is done in
33 /* order to defend against a buffer overflow problem in some
34 /* implementations of the syslog() library routine.
38 /* The Secure Mailer license must be distributed with this software.
41 /* IBM T.J. Watson Research
43 /* Yorktown Heights, NY 10598, USA
46 /* System libraries. */
49 #include <stdlib.h> /* 44BSD stdarg.h uses abort() */
56 /* Application-specific. */
59 #include "stringops.h"
61 #include "msg_output.h"
62 #include "msg_syslog.h"
66 * Stay a little below the 2048-byte limit of older syslog()
69 #define MSG_SYSLOG_RECLEN 2000
71 struct facility_list
{
76 static struct facility_list facility_list
[] = {
81 "authpriv", LOG_AUTHPRIV
,
105 "security", LOG_SECURITY
,
108 "syslog", LOG_SYSLOG
,
117 "local0", LOG_LOCAL0
,
120 "local1", LOG_LOCAL1
,
123 "local2", LOG_LOCAL2
,
126 "local3", LOG_LOCAL3
,
129 "local4", LOG_LOCAL4
,
132 "local5", LOG_LOCAL5
,
135 "local6", LOG_LOCAL6
,
138 "local7", LOG_LOCAL7
,
143 static int syslog_facility
;
145 /* msg_syslog_print - log info to syslog daemon */
147 static void msg_syslog_print(int level
, const char *text
)
149 static int log_level
[] = {
150 LOG_INFO
, LOG_WARNING
, LOG_ERR
, LOG_CRIT
, LOG_CRIT
,
152 static char *severity_name
[] = {
153 "info", "warning", "error", "fatal", "panic",
156 if (level
< 0 || level
>= (int) (sizeof(log_level
) / sizeof(log_level
[0])))
157 msg_panic("msg_syslog_print: invalid severity level: %d", level
);
159 if (level
== MSG_INFO
) {
160 syslog(syslog_facility
| log_level
[level
], "%.*s",
161 (int) MSG_SYSLOG_RECLEN
, text
);
163 syslog(syslog_facility
| log_level
[level
], "%s: %.*s",
164 severity_name
[level
], (int) MSG_SYSLOG_RECLEN
, text
);
168 /* msg_syslog_init - initialize */
170 void msg_syslog_init(const char *name
, int logopt
, int facility
)
172 static int first_call
= 1;
175 * XXX If this program is set-gid, then TZ must not be trusted. This
176 * scrubbing code is in the wrong place.
181 openlog(name
, LOG_NDELAY
| logopt
, facility
);
184 msg_output(msg_syslog_print
);
188 /* msg_syslog_facility - set logging facility by name */
190 int msg_syslog_facility(const char *facility_name
)
192 struct facility_list
*fnp
;
194 for (fnp
= facility_list
; fnp
->name
; ++fnp
) {
195 if (!strcmp(fnp
->name
, facility_name
)) {
196 syslog_facility
= fnp
->facility
;
206 * Proof-of-concept program to test the syslogging diagnostics interface
208 * Usage: msg_syslog_test text...
211 int main(int argc
, char **argv
)
213 VSTRING
*vp
= vstring_alloc(256);
215 msg_syslog_init(argv
[0], LOG_PID
, LOG_MAIL
);
217 msg_error("usage: %s text to be logged", argv
[0]);
218 while (--argc
&& *++argv
) {
219 vstring_strcat(vp
, *argv
);
221 vstring_strcat(vp
, " ");
223 msg_warn("static text");
224 msg_warn("dynamic text: >%s<", vstring_str(vp
));
225 msg_warn("dynamic numeric: >%d<", 42);
226 msg_warn("error text: >%m<");
227 msg_warn("dynamic: >%s<: error: >%m<", vstring_str(vp
));