xref: /illumos-gate/usr/src/cmd/fm/modules/common/syslog-msgs/syslog.c (revision 7c478bd95313f5f23a4c958a745db2134aa0324)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/fm/protocol.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/strlog.h>
31*7c478bd9Sstevel@tonic-gate #include <fm/fmd_api.h>
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <stropts.h>
34*7c478bd9Sstevel@tonic-gate #include <syslog.h>
35*7c478bd9Sstevel@tonic-gate #include <locale.h>
36*7c478bd9Sstevel@tonic-gate #include <strings.h>
37*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
38*7c478bd9Sstevel@tonic-gate #include <unistd.h>
39*7c478bd9Sstevel@tonic-gate #include <limits.h>
40*7c478bd9Sstevel@tonic-gate #include <alloca.h>
41*7c478bd9Sstevel@tonic-gate #include <errno.h>
42*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
43*7c478bd9Sstevel@tonic-gate #include <time.h>
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate /*
46*7c478bd9Sstevel@tonic-gate  * SYSLOG_DOMAIN and SYSLOG_TEMPLATE define the dgettext() parameters the agent
47*7c478bd9Sstevel@tonic-gate  * can use to retrieve the localized format string for diagnosis messages.
48*7c478bd9Sstevel@tonic-gate  * The format string retrieved from SYSLOG_DOMAIN is the default format
49*7c478bd9Sstevel@tonic-gate  * string, but when processing each suspect list, dgettext() is also called
50*7c478bd9Sstevel@tonic-gate  * for the domain that matches the diagcode dictname and if SYSLOG_TEMPLATE
51*7c478bd9Sstevel@tonic-gate  * is defined, it overrides the default for that suspect list only.
52*7c478bd9Sstevel@tonic-gate  *
53*7c478bd9Sstevel@tonic-gate  * Similarly, SYSLOG_URL is also checked to see if syslog_url
54*7c478bd9Sstevel@tonic-gate  * should be overridden for each suspect list.
55*7c478bd9Sstevel@tonic-gate  *
56*7c478bd9Sstevel@tonic-gate  * The net effect of all this is that for a given diagcode DICT-1234-56:
57*7c478bd9Sstevel@tonic-gate  *
58*7c478bd9Sstevel@tonic-gate  *	- If DICT.mo defines syslog-msgs-message-template, it is used
59*7c478bd9Sstevel@tonic-gate  *	  as the format string for the diagnosis message.
60*7c478bd9Sstevel@tonic-gate  *
61*7c478bd9Sstevel@tonic-gate  *	- Otherwise, syslog-msgs-message-template from FMD.mo is used.
62*7c478bd9Sstevel@tonic-gate  *
63*7c478bd9Sstevel@tonic-gate  *	- If DICT.mo defines syslog-url, it is used when filling
64*7c478bd9Sstevel@tonic-gate  *	  in the %s in the "description" message.
65*7c478bd9Sstevel@tonic-gate  *
66*7c478bd9Sstevel@tonic-gate  *	- Otherwise, if syslog-msgs.conf defines a "url" property, that
67*7c478bd9Sstevel@tonic-gate  *	  value is used.
68*7c478bd9Sstevel@tonic-gate  *
69*7c478bd9Sstevel@tonic-gate  *	- Otherwise, the default "http://sun.com/msg/" is used (via the
70*7c478bd9Sstevel@tonic-gate  *	  fmd_props[] table defined in this file).
71*7c478bd9Sstevel@tonic-gate  */
72*7c478bd9Sstevel@tonic-gate static const char SYSLOG_DOMAIN[] = "FMD";
73*7c478bd9Sstevel@tonic-gate static const char SYSLOG_TEMPLATE[] = "syslog-msgs-message-template";
74*7c478bd9Sstevel@tonic-gate static const char SYSLOG_URL[] = "syslog-url";
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate static struct stats {
77*7c478bd9Sstevel@tonic-gate 	fmd_stat_t bad_vers;
78*7c478bd9Sstevel@tonic-gate 	fmd_stat_t bad_fmri;
79*7c478bd9Sstevel@tonic-gate 	fmd_stat_t bad_code;
80*7c478bd9Sstevel@tonic-gate 	fmd_stat_t bad_time;
81*7c478bd9Sstevel@tonic-gate 	fmd_stat_t log_err;
82*7c478bd9Sstevel@tonic-gate 	fmd_stat_t msg_err;
83*7c478bd9Sstevel@tonic-gate 	fmd_stat_t no_msg;
84*7c478bd9Sstevel@tonic-gate } syslog_stats = {
85*7c478bd9Sstevel@tonic-gate 	{ "bad_vers", FMD_TYPE_UINT64, "event version is missing or invalid" },
86*7c478bd9Sstevel@tonic-gate 	{ "bad_fmri", FMD_TYPE_UINT64, "event fmri is missing or invalid" },
87*7c478bd9Sstevel@tonic-gate 	{ "bad_code", FMD_TYPE_UINT64, "event code has no dictionary name" },
88*7c478bd9Sstevel@tonic-gate 	{ "bad_time", FMD_TYPE_UINT64, "event time is not properly encoded" },
89*7c478bd9Sstevel@tonic-gate 	{ "log_err", FMD_TYPE_UINT64, "failed to log message to log(7D)" },
90*7c478bd9Sstevel@tonic-gate 	{ "msg_err", FMD_TYPE_UINT64, "failed to log message to sysmsg(7D)" },
91*7c478bd9Sstevel@tonic-gate 	{ "no_msg", FMD_TYPE_UINT64, "message logging suppressed" }
92*7c478bd9Sstevel@tonic-gate };
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate static const struct facility {
95*7c478bd9Sstevel@tonic-gate 	const char *fac_name;
96*7c478bd9Sstevel@tonic-gate 	int fac_value;
97*7c478bd9Sstevel@tonic-gate } syslog_facs[] = {
98*7c478bd9Sstevel@tonic-gate 	{ "LOG_DAEMON", LOG_DAEMON },
99*7c478bd9Sstevel@tonic-gate 	{ "LOG_LOCAL0", LOG_LOCAL0 },
100*7c478bd9Sstevel@tonic-gate 	{ "LOG_LOCAL1", LOG_LOCAL1 },
101*7c478bd9Sstevel@tonic-gate 	{ "LOG_LOCAL2", LOG_LOCAL2 },
102*7c478bd9Sstevel@tonic-gate 	{ "LOG_LOCAL3", LOG_LOCAL3 },
103*7c478bd9Sstevel@tonic-gate 	{ "LOG_LOCAL4", LOG_LOCAL4 },
104*7c478bd9Sstevel@tonic-gate 	{ "LOG_LOCAL5", LOG_LOCAL5 },
105*7c478bd9Sstevel@tonic-gate 	{ "LOG_LOCAL6", LOG_LOCAL6 },
106*7c478bd9Sstevel@tonic-gate 	{ "LOG_LOCAL7", LOG_LOCAL7 },
107*7c478bd9Sstevel@tonic-gate 	{ NULL, 0 }
108*7c478bd9Sstevel@tonic-gate };
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate static char *syslog_locdir;	/* l10n messages directory (if alternate) */
111*7c478bd9Sstevel@tonic-gate static char *syslog_url;	/* current value of "url" property */
112*7c478bd9Sstevel@tonic-gate static int syslog_msgall;	/* set to message all faults */
113*7c478bd9Sstevel@tonic-gate static log_ctl_t syslog_ctl;	/* log(7D) meta-data for each msg */
114*7c478bd9Sstevel@tonic-gate static int syslog_logfd = -1;	/* log(7D) file descriptor */
115*7c478bd9Sstevel@tonic-gate static int syslog_msgfd = -1;	/* sysmsg(7D) file descriptor */
116*7c478bd9Sstevel@tonic-gate static int syslog_file;		/* log to syslog_logfd */
117*7c478bd9Sstevel@tonic-gate static int syslog_cons;		/* log to syslog_msgfd */
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate /*
120*7c478bd9Sstevel@tonic-gate  * Ideally we would just use syslog(3C) for outputting our messages, but our
121*7c478bd9Sstevel@tonic-gate  * messaging standard defines a nice multi-line format and syslogd(1M) is very
122*7c478bd9Sstevel@tonic-gate  * inflexible and stupid when it comes to multi-line messages.  It pulls data
123*7c478bd9Sstevel@tonic-gate  * out of log(7D) and splits it up by \n, printing each line to the console
124*7c478bd9Sstevel@tonic-gate  * with its usual prefix of date and sender; it uses the same behavior for the
125*7c478bd9Sstevel@tonic-gate  * messages file as well.  Further, syslog(3C) provides no CE_CONT equivalent
126*7c478bd9Sstevel@tonic-gate  * for userland callers (which at least works around repeated file prefixing).
127*7c478bd9Sstevel@tonic-gate  * So with a multi-line message format, your file and console end up like this:
128*7c478bd9Sstevel@tonic-gate  *
129*7c478bd9Sstevel@tonic-gate  * Dec 02 18:08:40 hostname this is my nicely formatted
130*7c478bd9Sstevel@tonic-gate  * Dec 02 18:08:40 hostname message designed for 80 cols
131*7c478bd9Sstevel@tonic-gate  * ...
132*7c478bd9Sstevel@tonic-gate  *
133*7c478bd9Sstevel@tonic-gate  * To resolve these issues, we use our own syslog_emit() wrapper to emit
134*7c478bd9Sstevel@tonic-gate  * messages and some knowledge of how the Solaris log drivers work.  We first
135*7c478bd9Sstevel@tonic-gate  * construct an enlarged format string containing the appropriate msgid(1).
136*7c478bd9Sstevel@tonic-gate  * We then format the caller's message using the provided format and buffer.
137*7c478bd9Sstevel@tonic-gate  * We send this message to log(7D) using putmsg() with SL_CONSOLE | SL_LOGONLY
138*7c478bd9Sstevel@tonic-gate  * set in the log_ctl_t.  The log driver allows us to set SL_LOGONLY when we
139*7c478bd9Sstevel@tonic-gate  * construct messages ourself, indicating that syslogd should only emit the
140*7c478bd9Sstevel@tonic-gate  * message to /var/adm/messages and any remote hosts, and skip the console.
141*7c478bd9Sstevel@tonic-gate  * Then we emit the message a second time, without the special prefix, to the
142*7c478bd9Sstevel@tonic-gate  * sysmsg(7D) device, which handles console redirection and also permits us
143*7c478bd9Sstevel@tonic-gate  * to output any characters we like to the console, including \n and \r.
144*7c478bd9Sstevel@tonic-gate  */
145*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE4*/
146*7c478bd9Sstevel@tonic-gate static void
147*7c478bd9Sstevel@tonic-gate syslog_emit(fmd_hdl_t *hdl, char *buf, size_t len, const char *msgformat, ...)
148*7c478bd9Sstevel@tonic-gate {
149*7c478bd9Sstevel@tonic-gate 	struct strbuf ctl, dat;
150*7c478bd9Sstevel@tonic-gate 	uint32_t msgid;
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	char *format;
153*7c478bd9Sstevel@tonic-gate 	size_t formatlen;
154*7c478bd9Sstevel@tonic-gate 	va_list ap;
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	formatlen = strlen(msgformat) + 64; /* +64 for prefix and \0 */
157*7c478bd9Sstevel@tonic-gate 	format = alloca(formatlen);
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 	STRLOG_MAKE_MSGID(msgformat, msgid);
160*7c478bd9Sstevel@tonic-gate 	(void) snprintf(format, formatlen,
161*7c478bd9Sstevel@tonic-gate 	    "fmd: [ID %u FACILITY_AND_PRIORITY] %s", msgid, msgformat);
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 	va_start(ap, msgformat);
164*7c478bd9Sstevel@tonic-gate 	(void) vsnprintf(buf, len, format, ap);
165*7c478bd9Sstevel@tonic-gate 	va_end(ap);
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 	ctl.buf = (void *)&syslog_ctl;
168*7c478bd9Sstevel@tonic-gate 	ctl.len = sizeof (syslog_ctl);
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 	dat.buf = buf;
171*7c478bd9Sstevel@tonic-gate 	dat.len = strlen(buf) + 1;
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 	if (syslog_file && putmsg(syslog_logfd, &ctl, &dat, 0) != 0) {
174*7c478bd9Sstevel@tonic-gate 		fmd_hdl_debug(hdl, "putmsg failed: %s\n", strerror(errno));
175*7c478bd9Sstevel@tonic-gate 		syslog_stats.log_err.fmds_value.ui64++;
176*7c478bd9Sstevel@tonic-gate 	}
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 	dat.buf = strchr(buf, ']');
179*7c478bd9Sstevel@tonic-gate 	dat.len -= (size_t)(dat.buf - buf);
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	dat.buf[0] = '\r'; /* overwrite ']' with carriage return */
182*7c478bd9Sstevel@tonic-gate 	dat.buf[1] = '\n'; /* overwrite ' ' with newline */
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	if (syslog_cons && write(syslog_msgfd, dat.buf, dat.len) != dat.len) {
185*7c478bd9Sstevel@tonic-gate 		fmd_hdl_debug(hdl, "write failed: %s\n", strerror(errno));
186*7c478bd9Sstevel@tonic-gate 		syslog_stats.msg_err.fmds_value.ui64++;
187*7c478bd9Sstevel@tonic-gate 	}
188*7c478bd9Sstevel@tonic-gate }
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
191*7c478bd9Sstevel@tonic-gate static void
192*7c478bd9Sstevel@tonic-gate syslog_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl, const char *class)
193*7c478bd9Sstevel@tonic-gate {
194*7c478bd9Sstevel@tonic-gate 	char *uuid, *code, *dict, *url, *urlcode, *template, *p;
195*7c478bd9Sstevel@tonic-gate 	char *src_name, *src_vers, *platform, *chassis, *server;
196*7c478bd9Sstevel@tonic-gate 	char *typ, *sev, *fmt, *trfmt, *rsp, *imp, *act;
197*7c478bd9Sstevel@tonic-gate 	char msg[1024], desc[1024], date[64];
198*7c478bd9Sstevel@tonic-gate 	boolean_t domsg;
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate 	nvlist_t *fmri, *auth;
201*7c478bd9Sstevel@tonic-gate 	uint8_t version;
202*7c478bd9Sstevel@tonic-gate 	struct tm tm, *tmp;
203*7c478bd9Sstevel@tonic-gate 	int64_t *tv;
204*7c478bd9Sstevel@tonic-gate 	time_t sec;
205*7c478bd9Sstevel@tonic-gate 	uint_t tn = 0;
206*7c478bd9Sstevel@tonic-gate 	char *olang = NULL;
207*7c478bd9Sstevel@tonic-gate 	size_t len;
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 	if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 ||
210*7c478bd9Sstevel@tonic-gate 	    version > FM_SUSPECT_VERSION) {
211*7c478bd9Sstevel@tonic-gate 		fmd_hdl_debug(hdl, "invalid event version: %u\n", version);
212*7c478bd9Sstevel@tonic-gate 		syslog_stats.bad_vers.fmds_value.ui64++;
213*7c478bd9Sstevel@tonic-gate 		return; /* invalid event version */
214*7c478bd9Sstevel@tonic-gate 	}
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 	if (!syslog_msgall && nvlist_lookup_boolean_value(nvl,
217*7c478bd9Sstevel@tonic-gate 	    FM_SUSPECT_MESSAGE, &domsg) == 0 && !domsg) {
218*7c478bd9Sstevel@tonic-gate 		fmd_hdl_debug(hdl, "%s requested no message\n", class);
219*7c478bd9Sstevel@tonic-gate 		syslog_stats.no_msg.fmds_value.ui64++;
220*7c478bd9Sstevel@tonic-gate 		return; /* event is not to be messaged */
221*7c478bd9Sstevel@tonic-gate 	}
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 	/*
224*7c478bd9Sstevel@tonic-gate 	 * Extract the DE element, which is an FMRI for the diagnosis engine
225*7c478bd9Sstevel@tonic-gate 	 * that made this event, and validate its meta-data before continuing.
226*7c478bd9Sstevel@tonic-gate 	 */
227*7c478bd9Sstevel@tonic-gate 	if (nvlist_lookup_nvlist(nvl, FM_SUSPECT_DE, &fmri) != 0 ||
228*7c478bd9Sstevel@tonic-gate 	    nvlist_lookup_string(fmri, FM_FMRI_SCHEME, &p) != 0 ||
229*7c478bd9Sstevel@tonic-gate 	    strcmp(p, FM_FMRI_SCHEME_FMD) != 0 ||
230*7c478bd9Sstevel@tonic-gate 	    nvlist_lookup_uint8(fmri, FM_VERSION, &version) != 0 ||
231*7c478bd9Sstevel@tonic-gate 	    version > FM_FMD_SCHEME_VERSION ||
232*7c478bd9Sstevel@tonic-gate 	    nvlist_lookup_nvlist(fmri, FM_FMRI_AUTHORITY, &auth) != 0 ||
233*7c478bd9Sstevel@tonic-gate 	    nvlist_lookup_uint8(auth, FM_VERSION, &version) != 0 ||
234*7c478bd9Sstevel@tonic-gate 	    version > FM_FMRI_AUTH_VERSION) {
235*7c478bd9Sstevel@tonic-gate 		syslog_stats.bad_fmri.fmds_value.ui64++;
236*7c478bd9Sstevel@tonic-gate 		return; /* invalid de fmri */
237*7c478bd9Sstevel@tonic-gate 	}
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate 	/*
240*7c478bd9Sstevel@tonic-gate 	 * Extract the relevant identifying elements of the FMRI and authority.
241*7c478bd9Sstevel@tonic-gate 	 * Note: for now, we ignore FM_FMRI_AUTH_DOMAIN (only for SPs).
242*7c478bd9Sstevel@tonic-gate 	 */
243*7c478bd9Sstevel@tonic-gate 	(void) nvlist_lookup_string(fmri, FM_FMRI_FMD_NAME, &src_name);
244*7c478bd9Sstevel@tonic-gate 	(void) nvlist_lookup_string(fmri, FM_FMRI_FMD_VERSION, &src_vers);
245*7c478bd9Sstevel@tonic-gate 	(void) nvlist_lookup_string(auth, FM_FMRI_AUTH_PRODUCT, &platform);
246*7c478bd9Sstevel@tonic-gate 	(void) nvlist_lookup_string(auth, FM_FMRI_AUTH_SERVER, &server);
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 	if (nvlist_lookup_string(auth, FM_FMRI_AUTH_CHASSIS, &chassis) != 0)
249*7c478bd9Sstevel@tonic-gate 		chassis = "-"; /* chassis serial number may not be present */
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate 	/*
252*7c478bd9Sstevel@tonic-gate 	 * Extract the uuid and diagcode dictionary from the event code.  The
253*7c478bd9Sstevel@tonic-gate 	 * dictionary name is the text preceding the first "-" in the code.
254*7c478bd9Sstevel@tonic-gate 	 */
255*7c478bd9Sstevel@tonic-gate 	(void) nvlist_lookup_string(nvl, FM_SUSPECT_UUID, &uuid);
256*7c478bd9Sstevel@tonic-gate 	(void) nvlist_lookup_string(nvl, FM_SUSPECT_DIAG_CODE, &code);
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate 	if ((p = strchr(code, '-')) == NULL || p == code) {
259*7c478bd9Sstevel@tonic-gate 		fmd_hdl_debug(hdl, "invalid diagnosis code: %s\n", code);
260*7c478bd9Sstevel@tonic-gate 		syslog_stats.bad_code.fmds_value.ui64++;
261*7c478bd9Sstevel@tonic-gate 		return; /* invalid diagnosis code */
262*7c478bd9Sstevel@tonic-gate 	}
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate 	dict = alloca((size_t)(p - code) + 1);
265*7c478bd9Sstevel@tonic-gate 	(void) strncpy(dict, code, (size_t)(p - code));
266*7c478bd9Sstevel@tonic-gate 	dict[(size_t)(p - code)] = '\0';
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate 	/*
269*7c478bd9Sstevel@tonic-gate 	 * Alloca a hunk of memory and use it to create the msgid strings
270*7c478bd9Sstevel@tonic-gate 	 * <code>.type, <code>.severity, <code>.description, and so forth.
271*7c478bd9Sstevel@tonic-gate 	 * These form the msgids we will use to look up the localized text.
272*7c478bd9Sstevel@tonic-gate 	 * Since we've allocated things to be of the right size, we know
273*7c478bd9Sstevel@tonic-gate 	 * than snprintf() can't overflow: INT_MAX is used shut lint up and
274*7c478bd9Sstevel@tonic-gate 	 * avoid code to needlessly recompute the remaining buffer space.
275*7c478bd9Sstevel@tonic-gate 	 */
276*7c478bd9Sstevel@tonic-gate 	typ = alloca(6 * (strlen(code) + 16));
277*7c478bd9Sstevel@tonic-gate 	sev = typ + snprintf(typ, INT_MAX, "%s.type", code) + 1;
278*7c478bd9Sstevel@tonic-gate 	fmt = sev + snprintf(sev, INT_MAX, "%s.severity", code) + 1;
279*7c478bd9Sstevel@tonic-gate 	rsp = fmt + snprintf(fmt, INT_MAX, "%s.description", code) + 1;
280*7c478bd9Sstevel@tonic-gate 	imp = rsp + snprintf(rsp, INT_MAX, "%s.response", code) + 1;
281*7c478bd9Sstevel@tonic-gate 	act = imp + snprintf(imp, INT_MAX, "%s.impact", code) + 1;
282*7c478bd9Sstevel@tonic-gate 	(void) snprintf(act, INT_MAX, "%s.action", code);
283*7c478bd9Sstevel@tonic-gate 
284*7c478bd9Sstevel@tonic-gate 	if (syslog_locdir != NULL)
285*7c478bd9Sstevel@tonic-gate 		(void) bindtextdomain(dict, syslog_locdir);
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate 	if ((trfmt = dgettext(dict, fmt)) == fmt) {
288*7c478bd9Sstevel@tonic-gate 		/*
289*7c478bd9Sstevel@tonic-gate 		 * We didn't find a translation in the dictionary for the
290*7c478bd9Sstevel@tonic-gate 		 * current language.  The string we passed to gettext is merely
291*7c478bd9Sstevel@tonic-gate 		 * an index - it isn't sufficient, on its own, to be used as the
292*7c478bd9Sstevel@tonic-gate 		 * message.  Fall back to C and try again.
293*7c478bd9Sstevel@tonic-gate 		 */
294*7c478bd9Sstevel@tonic-gate 		olang = setlocale(LC_MESSAGES, NULL);
295*7c478bd9Sstevel@tonic-gate 		fmd_hdl_debug(hdl, "dgettext(%s, %s) in %s failed; trying C\n",
296*7c478bd9Sstevel@tonic-gate 		    dict, fmt, olang);
297*7c478bd9Sstevel@tonic-gate 		(void) setlocale(LC_MESSAGES, "C");
298*7c478bd9Sstevel@tonic-gate 		trfmt = dgettext(dict, fmt);
299*7c478bd9Sstevel@tonic-gate 	}
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate 	if ((url = dgettext(dict, SYSLOG_URL)) == SYSLOG_URL)
302*7c478bd9Sstevel@tonic-gate 		url = syslog_url;
303*7c478bd9Sstevel@tonic-gate 
304*7c478bd9Sstevel@tonic-gate 	/*
305*7c478bd9Sstevel@tonic-gate 	 * If the URL ends with a slash, that indicates the code should be
306*7c478bd9Sstevel@tonic-gate 	 * appended to it.  After formatting the URL, reformat the DESC
307*7c478bd9Sstevel@tonic-gate 	 * text using the URL as an snprintf argument.
308*7c478bd9Sstevel@tonic-gate 	 */
309*7c478bd9Sstevel@tonic-gate 	len = strlen(url);
310*7c478bd9Sstevel@tonic-gate 	if (url[len - 1] == '/') {
311*7c478bd9Sstevel@tonic-gate 		urlcode = alloca(len + strlen(code) + 1);
312*7c478bd9Sstevel@tonic-gate 		(void) snprintf(urlcode, INT_MAX, "%s%s", url, code);
313*7c478bd9Sstevel@tonic-gate 	} else {
314*7c478bd9Sstevel@tonic-gate 		urlcode = url;
315*7c478bd9Sstevel@tonic-gate 	}
316*7c478bd9Sstevel@tonic-gate 	/* LINTED - variable format specifier to snprintf() */
317*7c478bd9Sstevel@tonic-gate 	(void) snprintf(desc, sizeof (desc), trfmt, urlcode);
318*7c478bd9Sstevel@tonic-gate 
319*7c478bd9Sstevel@tonic-gate 	/*
320*7c478bd9Sstevel@tonic-gate 	 * Extract the diagnosis time and format it using the locale's default.
321*7c478bd9Sstevel@tonic-gate 	 * strftime() will use GMT or local time based on our "gmt" setting.
322*7c478bd9Sstevel@tonic-gate 	 */
323*7c478bd9Sstevel@tonic-gate 	if (nvlist_lookup_int64_array(nvl, FM_SUSPECT_DIAG_TIME,
324*7c478bd9Sstevel@tonic-gate 	    &tv, &tn) == 0 && tn == 2 && (sec = (time_t)tv[0]) != (time_t)-1 &&
325*7c478bd9Sstevel@tonic-gate 	    (tmp = localtime_r(&sec, &tm)) != NULL)
326*7c478bd9Sstevel@tonic-gate 		(void) strftime(date, sizeof (date), "%C", tmp);
327*7c478bd9Sstevel@tonic-gate 	else {
328*7c478bd9Sstevel@tonic-gate 		syslog_stats.bad_time.fmds_value.ui64++;
329*7c478bd9Sstevel@tonic-gate 		(void) strcpy(date, "-");
330*7c478bd9Sstevel@tonic-gate 	}
331*7c478bd9Sstevel@tonic-gate 
332*7c478bd9Sstevel@tonic-gate 	/*
333*7c478bd9Sstevel@tonic-gate 	 * Create and log the final string by filling in the template with the
334*7c478bd9Sstevel@tonic-gate 	 * strings we've created and the strings from the message dictionary.
335*7c478bd9Sstevel@tonic-gate 	 * If a template is provided for this dictionary, use it, otherwise
336*7c478bd9Sstevel@tonic-gate 	 * fall back to the default template.
337*7c478bd9Sstevel@tonic-gate 	 */
338*7c478bd9Sstevel@tonic-gate 	if ((template = dgettext(dict, SYSLOG_TEMPLATE)) == SYSLOG_TEMPLATE)
339*7c478bd9Sstevel@tonic-gate 		template = dgettext(SYSLOG_DOMAIN, SYSLOG_TEMPLATE);
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate 	syslog_emit(hdl, msg, sizeof (msg),
342*7c478bd9Sstevel@tonic-gate 	    template, code, dgettext(dict, typ),
343*7c478bd9Sstevel@tonic-gate 	    dgettext(dict, sev), date, platform, chassis, server, src_name,
344*7c478bd9Sstevel@tonic-gate 	    src_vers, uuid, desc, dgettext(dict, rsp), dgettext(dict, imp),
345*7c478bd9Sstevel@tonic-gate 	    dgettext(dict, act));
346*7c478bd9Sstevel@tonic-gate 
347*7c478bd9Sstevel@tonic-gate 	/*
348*7c478bd9Sstevel@tonic-gate 	 * Switch back to our original language if we had to fall back to C.
349*7c478bd9Sstevel@tonic-gate 	 */
350*7c478bd9Sstevel@tonic-gate 	if (olang != NULL)
351*7c478bd9Sstevel@tonic-gate 		(void) setlocale(LC_MESSAGES, olang);
352*7c478bd9Sstevel@tonic-gate }
353*7c478bd9Sstevel@tonic-gate 
354*7c478bd9Sstevel@tonic-gate static const fmd_prop_t fmd_props[] = {
355*7c478bd9Sstevel@tonic-gate 	{ "console", FMD_TYPE_BOOL, "true" },
356*7c478bd9Sstevel@tonic-gate 	{ "facility", FMD_TYPE_STRING, "LOG_DAEMON" },
357*7c478bd9Sstevel@tonic-gate 	{ "gmt", FMD_TYPE_BOOL, "false" },
358*7c478bd9Sstevel@tonic-gate 	{ "syslogd", FMD_TYPE_BOOL, "true" },
359*7c478bd9Sstevel@tonic-gate 	{ "url", FMD_TYPE_STRING, "http://sun.com/msg/" },
360*7c478bd9Sstevel@tonic-gate 	{ "message_all", FMD_TYPE_BOOL, "false" },
361*7c478bd9Sstevel@tonic-gate 	{ NULL, 0, NULL }
362*7c478bd9Sstevel@tonic-gate };
363*7c478bd9Sstevel@tonic-gate 
364*7c478bd9Sstevel@tonic-gate static const fmd_hdl_ops_t fmd_ops = {
365*7c478bd9Sstevel@tonic-gate 	syslog_recv,	/* fmdo_recv */
366*7c478bd9Sstevel@tonic-gate 	NULL,		/* fmdo_timeout */
367*7c478bd9Sstevel@tonic-gate 	NULL,		/* fmdo_close */
368*7c478bd9Sstevel@tonic-gate 	NULL,		/* fmdo_stats */
369*7c478bd9Sstevel@tonic-gate 	NULL,		/* fmdo_gc */
370*7c478bd9Sstevel@tonic-gate };
371*7c478bd9Sstevel@tonic-gate 
372*7c478bd9Sstevel@tonic-gate static const fmd_hdl_info_t fmd_info = {
373*7c478bd9Sstevel@tonic-gate 	"Syslog Messaging Agent", "1.0", &fmd_ops, fmd_props
374*7c478bd9Sstevel@tonic-gate };
375*7c478bd9Sstevel@tonic-gate 
376*7c478bd9Sstevel@tonic-gate void
377*7c478bd9Sstevel@tonic-gate _fmd_init(fmd_hdl_t *hdl)
378*7c478bd9Sstevel@tonic-gate {
379*7c478bd9Sstevel@tonic-gate 	const struct facility *fp;
380*7c478bd9Sstevel@tonic-gate 	char *facname, *tz, *rootdir, *locdir;
381*7c478bd9Sstevel@tonic-gate 
382*7c478bd9Sstevel@tonic-gate 	if (fmd_hdl_register(hdl, FMD_API_VERSION, &fmd_info) != 0)
383*7c478bd9Sstevel@tonic-gate 		return; /* invalid data in configuration file */
384*7c478bd9Sstevel@tonic-gate 
385*7c478bd9Sstevel@tonic-gate 	(void) fmd_stat_create(hdl, FMD_STAT_NOALLOC, sizeof (syslog_stats) /
386*7c478bd9Sstevel@tonic-gate 	    sizeof (fmd_stat_t), (fmd_stat_t *)&syslog_stats);
387*7c478bd9Sstevel@tonic-gate 
388*7c478bd9Sstevel@tonic-gate 	if ((syslog_logfd = open("/dev/conslog", O_WRONLY | O_NOCTTY)) == -1)
389*7c478bd9Sstevel@tonic-gate 		fmd_hdl_abort(hdl, "syslog-msgs failed to open /dev/conslog");
390*7c478bd9Sstevel@tonic-gate 
391*7c478bd9Sstevel@tonic-gate 	if ((syslog_msgfd = open("/dev/sysmsg", O_WRONLY | O_NOCTTY)) == -1)
392*7c478bd9Sstevel@tonic-gate 		fmd_hdl_abort(hdl, "syslog-msgs failed to open /dev/sysmsg");
393*7c478bd9Sstevel@tonic-gate 
394*7c478bd9Sstevel@tonic-gate 	/*
395*7c478bd9Sstevel@tonic-gate 	 * All FMA event dictionaries use msgfmt(1) message objects to produce
396*7c478bd9Sstevel@tonic-gate 	 * messages, even for the C locale.  We therefore want to use dgettext
397*7c478bd9Sstevel@tonic-gate 	 * for all message lookups, but its defined behavior in the C locale is
398*7c478bd9Sstevel@tonic-gate 	 * to return the input string.  Since our input strings are event codes
399*7c478bd9Sstevel@tonic-gate 	 * and not format strings, this doesn't help us.  We resolve this nit
400*7c478bd9Sstevel@tonic-gate 	 * by setting NLSPATH to a non-existent file: the presence of NLSPATH
401*7c478bd9Sstevel@tonic-gate 	 * is defined to force dgettext(3C) to do a full lookup even for C.
402*7c478bd9Sstevel@tonic-gate 	 */
403*7c478bd9Sstevel@tonic-gate 	if (getenv("NLSPATH") == NULL && putenv(fmd_hdl_strdup(hdl,
404*7c478bd9Sstevel@tonic-gate 	    "NLSPATH=/usr/lib/fm/fmd/fmd.cat", FMD_SLEEP)) != 0)
405*7c478bd9Sstevel@tonic-gate 		fmd_hdl_abort(hdl, "syslog-msgs failed to set NLSPATH");
406*7c478bd9Sstevel@tonic-gate 
407*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_MESSAGES, "");
408*7c478bd9Sstevel@tonic-gate 	fmd_hdl_debug(hdl, "locale=%s\n", setlocale(LC_MESSAGES, NULL));
409*7c478bd9Sstevel@tonic-gate 
410*7c478bd9Sstevel@tonic-gate 	/*
411*7c478bd9Sstevel@tonic-gate 	 * If the "gmt" property is set to true, force our EVENT-TIME to be
412*7c478bd9Sstevel@tonic-gate 	 * reported in GMT time; otherwise we use localtime.  tzset() affects
413*7c478bd9Sstevel@tonic-gate 	 * the results of subsequent calls to strftime(3C) above.
414*7c478bd9Sstevel@tonic-gate 	 */
415*7c478bd9Sstevel@tonic-gate 	if (fmd_prop_get_int32(hdl, "gmt") == FMD_B_TRUE &&
416*7c478bd9Sstevel@tonic-gate 	    ((tz = getenv("TZ")) == NULL || strcmp(tz, "GMT") != 0)) {
417*7c478bd9Sstevel@tonic-gate 		(void) putenv(fmd_hdl_strdup(hdl, "TZ=GMT", FMD_SLEEP));
418*7c478bd9Sstevel@tonic-gate 		tzset(); /* reload env */
419*7c478bd9Sstevel@tonic-gate 	}
420*7c478bd9Sstevel@tonic-gate 
421*7c478bd9Sstevel@tonic-gate 	/*
422*7c478bd9Sstevel@tonic-gate 	 * Look up the value of the "facility" property and use it to determine
423*7c478bd9Sstevel@tonic-gate 	 * what syslog LOG_* facility value we use to fill in our log_ctl_t.
424*7c478bd9Sstevel@tonic-gate 	 * The details of our logging method are described above syslog_emit().
425*7c478bd9Sstevel@tonic-gate 	 */
426*7c478bd9Sstevel@tonic-gate 	facname = fmd_prop_get_string(hdl, "facility");
427*7c478bd9Sstevel@tonic-gate 
428*7c478bd9Sstevel@tonic-gate 	for (fp = syslog_facs; fp->fac_name != NULL; fp++) {
429*7c478bd9Sstevel@tonic-gate 		if (strcmp(fp->fac_name, facname) == 0)
430*7c478bd9Sstevel@tonic-gate 			break;
431*7c478bd9Sstevel@tonic-gate 	}
432*7c478bd9Sstevel@tonic-gate 
433*7c478bd9Sstevel@tonic-gate 	if (fp->fac_name == NULL)
434*7c478bd9Sstevel@tonic-gate 		fmd_hdl_abort(hdl, "invalid 'facility' setting: %s\n", facname);
435*7c478bd9Sstevel@tonic-gate 
436*7c478bd9Sstevel@tonic-gate 	fmd_prop_free_string(hdl, facname);
437*7c478bd9Sstevel@tonic-gate 	syslog_ctl.pri = fp->fac_value | LOG_ERR;
438*7c478bd9Sstevel@tonic-gate 	syslog_ctl.flags = SL_CONSOLE | SL_LOGONLY;
439*7c478bd9Sstevel@tonic-gate 
440*7c478bd9Sstevel@tonic-gate 	/*
441*7c478bd9Sstevel@tonic-gate 	 * Cache any properties we use every time we receive an event and
442*7c478bd9Sstevel@tonic-gate 	 * subscribe to list.suspect events regardless of the .conf file.
443*7c478bd9Sstevel@tonic-gate 	 */
444*7c478bd9Sstevel@tonic-gate 	syslog_file = fmd_prop_get_int32(hdl, "syslogd");
445*7c478bd9Sstevel@tonic-gate 	syslog_cons = fmd_prop_get_int32(hdl, "console");
446*7c478bd9Sstevel@tonic-gate 	syslog_url = fmd_prop_get_string(hdl, "url");
447*7c478bd9Sstevel@tonic-gate 	syslog_msgall = fmd_prop_get_int32(hdl, "message_all");
448*7c478bd9Sstevel@tonic-gate 
449*7c478bd9Sstevel@tonic-gate 	/*
450*7c478bd9Sstevel@tonic-gate 	 * If fmd's rootdir property is set to a non-default root, then we are
451*7c478bd9Sstevel@tonic-gate 	 * going to need to rebind the text domains we use for dgettext() as
452*7c478bd9Sstevel@tonic-gate 	 * we go.  Look up the default l10n messages directory and make
453*7c478bd9Sstevel@tonic-gate 	 * syslog_locdir be this path with fmd.rootdir prepended to it.
454*7c478bd9Sstevel@tonic-gate 	 */
455*7c478bd9Sstevel@tonic-gate 	rootdir = fmd_prop_get_string(hdl, "fmd.rootdir");
456*7c478bd9Sstevel@tonic-gate 
457*7c478bd9Sstevel@tonic-gate 	if (*rootdir != '\0' && strcmp(rootdir, "/") != 0 &&
458*7c478bd9Sstevel@tonic-gate 	    (locdir = bindtextdomain(SYSLOG_DOMAIN, NULL)) != NULL) {
459*7c478bd9Sstevel@tonic-gate 		size_t len = strlen(rootdir) + strlen(locdir) + 1;
460*7c478bd9Sstevel@tonic-gate 		syslog_locdir = fmd_hdl_alloc(hdl, len, FMD_SLEEP);
461*7c478bd9Sstevel@tonic-gate 		(void) snprintf(syslog_locdir, len, "%s%s", rootdir, locdir);
462*7c478bd9Sstevel@tonic-gate 		(void) bindtextdomain(SYSLOG_DOMAIN, syslog_locdir);
463*7c478bd9Sstevel@tonic-gate 		fmd_hdl_debug(hdl, "binding textdomain to %s\n", syslog_locdir);
464*7c478bd9Sstevel@tonic-gate 	}
465*7c478bd9Sstevel@tonic-gate 
466*7c478bd9Sstevel@tonic-gate 	fmd_prop_free_string(hdl, rootdir);
467*7c478bd9Sstevel@tonic-gate 	fmd_hdl_subscribe(hdl, FM_LIST_SUSPECT_CLASS);
468*7c478bd9Sstevel@tonic-gate }
469*7c478bd9Sstevel@tonic-gate 
470*7c478bd9Sstevel@tonic-gate void
471*7c478bd9Sstevel@tonic-gate _fmd_fini(fmd_hdl_t *hdl)
472*7c478bd9Sstevel@tonic-gate {
473*7c478bd9Sstevel@tonic-gate 	fmd_hdl_strfree(hdl, syslog_locdir);
474*7c478bd9Sstevel@tonic-gate 	fmd_prop_free_string(hdl, syslog_url);
475*7c478bd9Sstevel@tonic-gate 
476*7c478bd9Sstevel@tonic-gate 	(void) close(syslog_logfd);
477*7c478bd9Sstevel@tonic-gate 	(void) close(syslog_msgfd);
478*7c478bd9Sstevel@tonic-gate }
479