xref: /illumos-gate/usr/src/cmd/fm/modules/common/syslog-msgs/syslog.c (revision 19e1255fca03b62e8105a1c6f74377fe34b2ff77)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5e1c679faSaf  * Common Development and Distribution License (the "License").
6e1c679faSaf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22627351e3Scy  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/fm/protocol.h>
297c478bd9Sstevel@tonic-gate #include <sys/strlog.h>
30072c378dSmb #include <sys/log.h>
317c478bd9Sstevel@tonic-gate #include <fm/fmd_api.h>
32e1c679faSaf #include <fm/fmd_msg.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <stropts.h>
357c478bd9Sstevel@tonic-gate #include <syslog.h>
367c478bd9Sstevel@tonic-gate #include <locale.h>
377c478bd9Sstevel@tonic-gate #include <strings.h>
387c478bd9Sstevel@tonic-gate #include <stdlib.h>
397c478bd9Sstevel@tonic-gate #include <unistd.h>
407c478bd9Sstevel@tonic-gate #include <limits.h>
417c478bd9Sstevel@tonic-gate #include <alloca.h>
427c478bd9Sstevel@tonic-gate #include <errno.h>
437c478bd9Sstevel@tonic-gate #include <fcntl.h>
447c478bd9Sstevel@tonic-gate #include <time.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * SYSLOG_DOMAIN and SYSLOG_TEMPLATE define the dgettext() parameters the agent
487c478bd9Sstevel@tonic-gate  * can use to retrieve the localized format string for diagnosis messages.
497c478bd9Sstevel@tonic-gate  * The format string retrieved from SYSLOG_DOMAIN is the default format
507c478bd9Sstevel@tonic-gate  * string, but when processing each suspect list, dgettext() is also called
517c478bd9Sstevel@tonic-gate  * for the domain that matches the diagcode dictname and if SYSLOG_TEMPLATE
527c478bd9Sstevel@tonic-gate  * is defined, it overrides the default for that suspect list only.
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  * Similarly, SYSLOG_URL is also checked to see if syslog_url
557c478bd9Sstevel@tonic-gate  * should be overridden for each suspect list.
567c478bd9Sstevel@tonic-gate  *
577c478bd9Sstevel@tonic-gate  * The net effect of all this is that for a given diagcode DICT-1234-56:
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  *	- If DICT.mo defines syslog-msgs-message-template, it is used
607c478bd9Sstevel@tonic-gate  *	  as the format string for the diagnosis message.
617c478bd9Sstevel@tonic-gate  *
627c478bd9Sstevel@tonic-gate  *	- Otherwise, syslog-msgs-message-template from FMD.mo is used.
637c478bd9Sstevel@tonic-gate  *
647c478bd9Sstevel@tonic-gate  *	- If DICT.mo defines syslog-url, it is used when filling
657c478bd9Sstevel@tonic-gate  *	  in the %s in the "description" message.
667c478bd9Sstevel@tonic-gate  *
677c478bd9Sstevel@tonic-gate  *	- Otherwise, if syslog-msgs.conf defines a "url" property, that
687c478bd9Sstevel@tonic-gate  *	  value is used.
697c478bd9Sstevel@tonic-gate  *
707c478bd9Sstevel@tonic-gate  *	- Otherwise, the default "http://sun.com/msg/" is used (via the
717c478bd9Sstevel@tonic-gate  *	  fmd_props[] table defined in this file).
727c478bd9Sstevel@tonic-gate  */
737c478bd9Sstevel@tonic-gate static const char SYSLOG_DOMAIN[] = "FMD";
747c478bd9Sstevel@tonic-gate static const char SYSLOG_TEMPLATE[] = "syslog-msgs-message-template";
757c478bd9Sstevel@tonic-gate static const char SYSLOG_URL[] = "syslog-url";
76*19e1255fScy static const char SYSLOG_POINTER[] = "syslog-msgs-pointer";
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate static struct stats {
797c478bd9Sstevel@tonic-gate 	fmd_stat_t bad_vers;
807c478bd9Sstevel@tonic-gate 	fmd_stat_t bad_fmri;
817c478bd9Sstevel@tonic-gate 	fmd_stat_t bad_code;
827c478bd9Sstevel@tonic-gate 	fmd_stat_t bad_time;
837c478bd9Sstevel@tonic-gate 	fmd_stat_t log_err;
847c478bd9Sstevel@tonic-gate 	fmd_stat_t msg_err;
857c478bd9Sstevel@tonic-gate 	fmd_stat_t no_msg;
867c478bd9Sstevel@tonic-gate } syslog_stats = {
877c478bd9Sstevel@tonic-gate 	{ "bad_vers", FMD_TYPE_UINT64, "event version is missing or invalid" },
887c478bd9Sstevel@tonic-gate 	{ "bad_fmri", FMD_TYPE_UINT64, "event fmri is missing or invalid" },
897c478bd9Sstevel@tonic-gate 	{ "bad_code", FMD_TYPE_UINT64, "event code has no dictionary name" },
907c478bd9Sstevel@tonic-gate 	{ "bad_time", FMD_TYPE_UINT64, "event time is not properly encoded" },
917c478bd9Sstevel@tonic-gate 	{ "log_err", FMD_TYPE_UINT64, "failed to log message to log(7D)" },
927c478bd9Sstevel@tonic-gate 	{ "msg_err", FMD_TYPE_UINT64, "failed to log message to sysmsg(7D)" },
937c478bd9Sstevel@tonic-gate 	{ "no_msg", FMD_TYPE_UINT64, "message logging suppressed" }
947c478bd9Sstevel@tonic-gate };
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate static const struct facility {
977c478bd9Sstevel@tonic-gate 	const char *fac_name;
987c478bd9Sstevel@tonic-gate 	int fac_value;
997c478bd9Sstevel@tonic-gate } syslog_facs[] = {
1007c478bd9Sstevel@tonic-gate 	{ "LOG_DAEMON", LOG_DAEMON },
1017c478bd9Sstevel@tonic-gate 	{ "LOG_LOCAL0", LOG_LOCAL0 },
1027c478bd9Sstevel@tonic-gate 	{ "LOG_LOCAL1", LOG_LOCAL1 },
1037c478bd9Sstevel@tonic-gate 	{ "LOG_LOCAL2", LOG_LOCAL2 },
1047c478bd9Sstevel@tonic-gate 	{ "LOG_LOCAL3", LOG_LOCAL3 },
1057c478bd9Sstevel@tonic-gate 	{ "LOG_LOCAL4", LOG_LOCAL4 },
1067c478bd9Sstevel@tonic-gate 	{ "LOG_LOCAL5", LOG_LOCAL5 },
1077c478bd9Sstevel@tonic-gate 	{ "LOG_LOCAL6", LOG_LOCAL6 },
1087c478bd9Sstevel@tonic-gate 	{ "LOG_LOCAL7", LOG_LOCAL7 },
1097c478bd9Sstevel@tonic-gate 	{ NULL, 0 }
1107c478bd9Sstevel@tonic-gate };
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate static char *syslog_locdir;	/* l10n messages directory (if alternate) */
1137c478bd9Sstevel@tonic-gate static char *syslog_url;	/* current value of "url" property */
114*19e1255fScy static char *syslog_pointer;	/* info to point user to the full message */
1157c478bd9Sstevel@tonic-gate static int syslog_msgall;	/* set to message all faults */
1167c478bd9Sstevel@tonic-gate static log_ctl_t syslog_ctl;	/* log(7D) meta-data for each msg */
1177c478bd9Sstevel@tonic-gate static int syslog_logfd = -1;	/* log(7D) file descriptor */
1187c478bd9Sstevel@tonic-gate static int syslog_msgfd = -1;	/* sysmsg(7D) file descriptor */
1197c478bd9Sstevel@tonic-gate static int syslog_file;		/* log to syslog_logfd */
1207c478bd9Sstevel@tonic-gate static int syslog_cons;		/* log to syslog_msgfd */
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /*
1237c478bd9Sstevel@tonic-gate  * Ideally we would just use syslog(3C) for outputting our messages, but our
1247c478bd9Sstevel@tonic-gate  * messaging standard defines a nice multi-line format and syslogd(1M) is very
1257c478bd9Sstevel@tonic-gate  * inflexible and stupid when it comes to multi-line messages.  It pulls data
1267c478bd9Sstevel@tonic-gate  * out of log(7D) and splits it up by \n, printing each line to the console
1277c478bd9Sstevel@tonic-gate  * with its usual prefix of date and sender; it uses the same behavior for the
1287c478bd9Sstevel@tonic-gate  * messages file as well.  Further, syslog(3C) provides no CE_CONT equivalent
1297c478bd9Sstevel@tonic-gate  * for userland callers (which at least works around repeated file prefixing).
1307c478bd9Sstevel@tonic-gate  * So with a multi-line message format, your file and console end up like this:
1317c478bd9Sstevel@tonic-gate  *
1327c478bd9Sstevel@tonic-gate  * Dec 02 18:08:40 hostname this is my nicely formatted
1337c478bd9Sstevel@tonic-gate  * Dec 02 18:08:40 hostname message designed for 80 cols
1347c478bd9Sstevel@tonic-gate  * ...
1357c478bd9Sstevel@tonic-gate  *
1367c478bd9Sstevel@tonic-gate  * To resolve these issues, we use our own syslog_emit() wrapper to emit
1377c478bd9Sstevel@tonic-gate  * messages and some knowledge of how the Solaris log drivers work.  We first
1387c478bd9Sstevel@tonic-gate  * construct an enlarged format string containing the appropriate msgid(1).
1397c478bd9Sstevel@tonic-gate  * We then format the caller's message using the provided format and buffer.
1407c478bd9Sstevel@tonic-gate  * We send this message to log(7D) using putmsg() with SL_CONSOLE | SL_LOGONLY
1417c478bd9Sstevel@tonic-gate  * set in the log_ctl_t.  The log driver allows us to set SL_LOGONLY when we
1427c478bd9Sstevel@tonic-gate  * construct messages ourself, indicating that syslogd should only emit the
1437c478bd9Sstevel@tonic-gate  * message to /var/adm/messages and any remote hosts, and skip the console.
144072c378dSmb  * Note: the log driver packet size limit for output via putmsg is LOGMAX_PS.
1457c478bd9Sstevel@tonic-gate  * Then we emit the message a second time, without the special prefix, to the
1467c478bd9Sstevel@tonic-gate  * sysmsg(7D) device, which handles console redirection and also permits us
1477c478bd9Sstevel@tonic-gate  * to output any characters we like to the console, including \n and \r.
1487c478bd9Sstevel@tonic-gate  */
14978432d5eScy /*PRINTFLIKE2*/
1507c478bd9Sstevel@tonic-gate static void
15178432d5eScy syslog_emit(fmd_hdl_t *hdl, const char *msgformat, ...)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate 	struct strbuf ctl, dat;
1547c478bd9Sstevel@tonic-gate 	uint32_t msgid;
1557c478bd9Sstevel@tonic-gate 
156*19e1255fScy 	char *format, *p, c;
15778432d5eScy 	char *buf = NULL;
158072c378dSmb 	size_t formatlen, logmsglen;
159*19e1255fScy 	int len, plen;
1607c478bd9Sstevel@tonic-gate 	va_list ap;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	formatlen = strlen(msgformat) + 64; /* +64 for prefix and \0 */
1637c478bd9Sstevel@tonic-gate 	format = alloca(formatlen);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	STRLOG_MAKE_MSGID(msgformat, msgid);
1667c478bd9Sstevel@tonic-gate 	(void) snprintf(format, formatlen,
1677c478bd9Sstevel@tonic-gate 	    "fmd: [ID %u FACILITY_AND_PRIORITY] %s", msgid, msgformat);
1687c478bd9Sstevel@tonic-gate 
16978432d5eScy 	/*
17078432d5eScy 	 * Figure out the length of the message then allocate a buffer
17178432d5eScy 	 * of adequate size.
17278432d5eScy 	 */
1737c478bd9Sstevel@tonic-gate 	va_start(ap, msgformat);
17478432d5eScy 	if ((len = vsnprintf(&c, 1, format, ap)) >= 0 &&
17578432d5eScy 	    (buf = fmd_hdl_alloc(hdl, len + 1, FMD_SLEEP)) != NULL)
17678432d5eScy 		(void) vsnprintf(buf, len + 1, format, ap);
1777c478bd9Sstevel@tonic-gate 	va_end(ap);
1787c478bd9Sstevel@tonic-gate 
17978432d5eScy 	if (buf == NULL)
18078432d5eScy 		return;
18178432d5eScy 
1827c478bd9Sstevel@tonic-gate 	ctl.buf = (void *)&syslog_ctl;
1837c478bd9Sstevel@tonic-gate 	ctl.len = sizeof (syslog_ctl);
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	dat.buf = buf;
186072c378dSmb 	logmsglen = strlen(buf) + 1;
187072c378dSmb 
188072c378dSmb 	/*
189072c378dSmb 	 * The underlying log driver won't accept (ERANGE) messages
190*19e1255fScy 	 * longer than LOG_MAXPS bytes.  The long message will be truncated
191*19e1255fScy 	 * and appended with a pointer to the full message.
192072c378dSmb 	 */
193*19e1255fScy 	if (logmsglen <= LOG_MAXPS) {
194072c378dSmb 		dat.len = logmsglen;
195*19e1255fScy 	} else {
196*19e1255fScy 		plen = strlen(syslog_pointer) + 1;
197*19e1255fScy 		buf[LOG_MAXPS - plen] = '\0';
198*19e1255fScy 		/*
199*19e1255fScy 		 * If possible, the pointer is appended after a newline
200*19e1255fScy 		 */
201*19e1255fScy 		if ((p = strrchr(buf, '\n')) == NULL)
202*19e1255fScy 			p = &buf[LOG_MAXPS - plen];
203*19e1255fScy 
204*19e1255fScy 		(void) strcpy(p, syslog_pointer);
205*19e1255fScy 		dat.len = strlen(buf) + 1;
206*19e1255fScy 	}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	if (syslog_file && putmsg(syslog_logfd, &ctl, &dat, 0) != 0) {
2097c478bd9Sstevel@tonic-gate 		fmd_hdl_debug(hdl, "putmsg failed: %s\n", strerror(errno));
2107c478bd9Sstevel@tonic-gate 		syslog_stats.log_err.fmds_value.ui64++;
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	dat.buf = strchr(buf, ']');
214072c378dSmb 	dat.len = (size_t)(logmsglen - (dat.buf - buf));
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	dat.buf[0] = '\r'; /* overwrite ']' with carriage return */
2177c478bd9Sstevel@tonic-gate 	dat.buf[1] = '\n'; /* overwrite ' ' with newline */
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	if (syslog_cons && write(syslog_msgfd, dat.buf, dat.len) != dat.len) {
2207c478bd9Sstevel@tonic-gate 		fmd_hdl_debug(hdl, "write failed: %s\n", strerror(errno));
2217c478bd9Sstevel@tonic-gate 		syslog_stats.msg_err.fmds_value.ui64++;
2227c478bd9Sstevel@tonic-gate 	}
22378432d5eScy 
22478432d5eScy 	fmd_hdl_free(hdl, buf, len + 1);
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2287c478bd9Sstevel@tonic-gate static void
2297c478bd9Sstevel@tonic-gate syslog_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl, const char *class)
2307c478bd9Sstevel@tonic-gate {
2317c478bd9Sstevel@tonic-gate 	char *uuid, *code, *dict, *url, *urlcode, *template, *p;
2327c478bd9Sstevel@tonic-gate 	char *src_name, *src_vers, *platform, *chassis, *server;
233e1c679faSaf 	char *typ, *sev, *fmt, *trfmt, *rsp, *imp, *act, *locdir;
23478432d5eScy 	char desc[1024], date[64];
2357c478bd9Sstevel@tonic-gate 	boolean_t domsg;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	nvlist_t *fmri, *auth;
2387c478bd9Sstevel@tonic-gate 	uint8_t version;
2397c478bd9Sstevel@tonic-gate 	struct tm tm, *tmp;
2407c478bd9Sstevel@tonic-gate 	int64_t *tv;
2417c478bd9Sstevel@tonic-gate 	time_t sec;
2427c478bd9Sstevel@tonic-gate 	uint_t tn = 0;
2437c478bd9Sstevel@tonic-gate 	char *olang = NULL;
244e1c679faSaf 	int locale_c = 0;
2457c478bd9Sstevel@tonic-gate 	size_t len;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 ||
2487c478bd9Sstevel@tonic-gate 	    version > FM_SUSPECT_VERSION) {
2497c478bd9Sstevel@tonic-gate 		fmd_hdl_debug(hdl, "invalid event version: %u\n", version);
2507c478bd9Sstevel@tonic-gate 		syslog_stats.bad_vers.fmds_value.ui64++;
2517c478bd9Sstevel@tonic-gate 		return; /* invalid event version */
2527c478bd9Sstevel@tonic-gate 	}
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	if (!syslog_msgall && nvlist_lookup_boolean_value(nvl,
2557c478bd9Sstevel@tonic-gate 	    FM_SUSPECT_MESSAGE, &domsg) == 0 && !domsg) {
2567c478bd9Sstevel@tonic-gate 		fmd_hdl_debug(hdl, "%s requested no message\n", class);
2577c478bd9Sstevel@tonic-gate 		syslog_stats.no_msg.fmds_value.ui64++;
2587c478bd9Sstevel@tonic-gate 		return; /* event is not to be messaged */
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	/*
2627c478bd9Sstevel@tonic-gate 	 * Extract the DE element, which is an FMRI for the diagnosis engine
2637c478bd9Sstevel@tonic-gate 	 * that made this event, and validate its meta-data before continuing.
2647c478bd9Sstevel@tonic-gate 	 */
2657c478bd9Sstevel@tonic-gate 	if (nvlist_lookup_nvlist(nvl, FM_SUSPECT_DE, &fmri) != 0 ||
2667c478bd9Sstevel@tonic-gate 	    nvlist_lookup_string(fmri, FM_FMRI_SCHEME, &p) != 0 ||
2677c478bd9Sstevel@tonic-gate 	    strcmp(p, FM_FMRI_SCHEME_FMD) != 0 ||
2687c478bd9Sstevel@tonic-gate 	    nvlist_lookup_uint8(fmri, FM_VERSION, &version) != 0 ||
2697c478bd9Sstevel@tonic-gate 	    version > FM_FMD_SCHEME_VERSION ||
2707c478bd9Sstevel@tonic-gate 	    nvlist_lookup_nvlist(fmri, FM_FMRI_AUTHORITY, &auth) != 0 ||
2717c478bd9Sstevel@tonic-gate 	    nvlist_lookup_uint8(auth, FM_VERSION, &version) != 0 ||
2727c478bd9Sstevel@tonic-gate 	    version > FM_FMRI_AUTH_VERSION) {
2737c478bd9Sstevel@tonic-gate 		syslog_stats.bad_fmri.fmds_value.ui64++;
2747c478bd9Sstevel@tonic-gate 		return; /* invalid de fmri */
2757c478bd9Sstevel@tonic-gate 	}
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	/*
2787c478bd9Sstevel@tonic-gate 	 * Extract the relevant identifying elements of the FMRI and authority.
2797c478bd9Sstevel@tonic-gate 	 * Note: for now, we ignore FM_FMRI_AUTH_DOMAIN (only for SPs).
2807c478bd9Sstevel@tonic-gate 	 */
2817c478bd9Sstevel@tonic-gate 	(void) nvlist_lookup_string(fmri, FM_FMRI_FMD_NAME, &src_name);
2827c478bd9Sstevel@tonic-gate 	(void) nvlist_lookup_string(fmri, FM_FMRI_FMD_VERSION, &src_vers);
2837c478bd9Sstevel@tonic-gate 	(void) nvlist_lookup_string(auth, FM_FMRI_AUTH_PRODUCT, &platform);
2847c478bd9Sstevel@tonic-gate 	(void) nvlist_lookup_string(auth, FM_FMRI_AUTH_SERVER, &server);
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	if (nvlist_lookup_string(auth, FM_FMRI_AUTH_CHASSIS, &chassis) != 0)
2877c478bd9Sstevel@tonic-gate 		chassis = "-"; /* chassis serial number may not be present */
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	/*
2907c478bd9Sstevel@tonic-gate 	 * Extract the uuid and diagcode dictionary from the event code.  The
2917c478bd9Sstevel@tonic-gate 	 * dictionary name is the text preceding the first "-" in the code.
2927c478bd9Sstevel@tonic-gate 	 */
2937c478bd9Sstevel@tonic-gate 	(void) nvlist_lookup_string(nvl, FM_SUSPECT_UUID, &uuid);
2947c478bd9Sstevel@tonic-gate 	(void) nvlist_lookup_string(nvl, FM_SUSPECT_DIAG_CODE, &code);
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	if ((p = strchr(code, '-')) == NULL || p == code) {
2977c478bd9Sstevel@tonic-gate 		fmd_hdl_debug(hdl, "invalid diagnosis code: %s\n", code);
2987c478bd9Sstevel@tonic-gate 		syslog_stats.bad_code.fmds_value.ui64++;
2997c478bd9Sstevel@tonic-gate 		return; /* invalid diagnosis code */
3007c478bd9Sstevel@tonic-gate 	}
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	dict = alloca((size_t)(p - code) + 1);
3037c478bd9Sstevel@tonic-gate 	(void) strncpy(dict, code, (size_t)(p - code));
3047c478bd9Sstevel@tonic-gate 	dict[(size_t)(p - code)] = '\0';
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	/*
3077c478bd9Sstevel@tonic-gate 	 * Alloca a hunk of memory and use it to create the msgid strings
3087c478bd9Sstevel@tonic-gate 	 * <code>.type, <code>.severity, <code>.description, and so forth.
3097c478bd9Sstevel@tonic-gate 	 * These form the msgids we will use to look up the localized text.
3107c478bd9Sstevel@tonic-gate 	 * Since we've allocated things to be of the right size, we know
3117c478bd9Sstevel@tonic-gate 	 * than snprintf() can't overflow: INT_MAX is used shut lint up and
3127c478bd9Sstevel@tonic-gate 	 * avoid code to needlessly recompute the remaining buffer space.
3137c478bd9Sstevel@tonic-gate 	 */
3147c478bd9Sstevel@tonic-gate 	typ = alloca(6 * (strlen(code) + 16));
3157c478bd9Sstevel@tonic-gate 	sev = typ + snprintf(typ, INT_MAX, "%s.type", code) + 1;
3167c478bd9Sstevel@tonic-gate 	fmt = sev + snprintf(sev, INT_MAX, "%s.severity", code) + 1;
3177c478bd9Sstevel@tonic-gate 	rsp = fmt + snprintf(fmt, INT_MAX, "%s.description", code) + 1;
3187c478bd9Sstevel@tonic-gate 	imp = rsp + snprintf(rsp, INT_MAX, "%s.response", code) + 1;
3197c478bd9Sstevel@tonic-gate 	act = imp + snprintf(imp, INT_MAX, "%s.impact", code) + 1;
3207c478bd9Sstevel@tonic-gate 	(void) snprintf(act, INT_MAX, "%s.action", code);
3217c478bd9Sstevel@tonic-gate 
322e1c679faSaf 	fmd_msg_lock();
323e1c679faSaf 
3247c478bd9Sstevel@tonic-gate 	if (syslog_locdir != NULL)
325e1c679faSaf 		locdir =  bindtextdomain(dict, syslog_locdir);
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	if ((trfmt = dgettext(dict, fmt)) == fmt) {
3287c478bd9Sstevel@tonic-gate 		/*
3297c478bd9Sstevel@tonic-gate 		 * We didn't find a translation in the dictionary for the
3307c478bd9Sstevel@tonic-gate 		 * current language.  The string we passed to gettext is merely
3317c478bd9Sstevel@tonic-gate 		 * an index - it isn't sufficient, on its own, to be used as the
3327c478bd9Sstevel@tonic-gate 		 * message.  Fall back to C and try again.
3337c478bd9Sstevel@tonic-gate 		 */
3347c478bd9Sstevel@tonic-gate 		olang = setlocale(LC_MESSAGES, NULL);
335e1c679faSaf 		if (olang) {
336e1c679faSaf 			p = alloca(strlen(olang) + 1);
337e1c679faSaf 			olang = strcpy(p, olang);
338e1c679faSaf 		}
339e1c679faSaf 		locale_c = 1;
3407c478bd9Sstevel@tonic-gate 		(void) setlocale(LC_MESSAGES, "C");
3417c478bd9Sstevel@tonic-gate 		trfmt = dgettext(dict, fmt);
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	if ((url = dgettext(dict, SYSLOG_URL)) == SYSLOG_URL)
3457c478bd9Sstevel@tonic-gate 		url = syslog_url;
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	/*
3487c478bd9Sstevel@tonic-gate 	 * If the URL ends with a slash, that indicates the code should be
3497c478bd9Sstevel@tonic-gate 	 * appended to it.  After formatting the URL, reformat the DESC
3507c478bd9Sstevel@tonic-gate 	 * text using the URL as an snprintf argument.
3517c478bd9Sstevel@tonic-gate 	 */
3527c478bd9Sstevel@tonic-gate 	len = strlen(url);
3537c478bd9Sstevel@tonic-gate 	if (url[len - 1] == '/') {
3547c478bd9Sstevel@tonic-gate 		urlcode = alloca(len + strlen(code) + 1);
3557c478bd9Sstevel@tonic-gate 		(void) snprintf(urlcode, INT_MAX, "%s%s", url, code);
3567c478bd9Sstevel@tonic-gate 	} else {
3577c478bd9Sstevel@tonic-gate 		urlcode = url;
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 	/* LINTED - variable format specifier to snprintf() */
3607c478bd9Sstevel@tonic-gate 	(void) snprintf(desc, sizeof (desc), trfmt, urlcode);
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	/*
3637c478bd9Sstevel@tonic-gate 	 * Extract the diagnosis time and format it using the locale's default.
3647c478bd9Sstevel@tonic-gate 	 * strftime() will use GMT or local time based on our "gmt" setting.
3657c478bd9Sstevel@tonic-gate 	 */
3667c478bd9Sstevel@tonic-gate 	if (nvlist_lookup_int64_array(nvl, FM_SUSPECT_DIAG_TIME,
3677c478bd9Sstevel@tonic-gate 	    &tv, &tn) == 0 && tn == 2 && (sec = (time_t)tv[0]) != (time_t)-1 &&
3687c478bd9Sstevel@tonic-gate 	    (tmp = localtime_r(&sec, &tm)) != NULL)
3697c478bd9Sstevel@tonic-gate 		(void) strftime(date, sizeof (date), "%C", tmp);
3707c478bd9Sstevel@tonic-gate 	else {
3717c478bd9Sstevel@tonic-gate 		syslog_stats.bad_time.fmds_value.ui64++;
3727c478bd9Sstevel@tonic-gate 		(void) strcpy(date, "-");
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	/*
3767c478bd9Sstevel@tonic-gate 	 * Create and log the final string by filling in the template with the
3777c478bd9Sstevel@tonic-gate 	 * strings we've created and the strings from the message dictionary.
3787c478bd9Sstevel@tonic-gate 	 * If a template is provided for this dictionary, use it, otherwise
3797c478bd9Sstevel@tonic-gate 	 * fall back to the default template.
3807c478bd9Sstevel@tonic-gate 	 */
3817c478bd9Sstevel@tonic-gate 	if ((template = dgettext(dict, SYSLOG_TEMPLATE)) == SYSLOG_TEMPLATE)
3827c478bd9Sstevel@tonic-gate 		template = dgettext(SYSLOG_DOMAIN, SYSLOG_TEMPLATE);
383*19e1255fScy 	/*
384*19e1255fScy 	 * Do samely for the message pointer.  If the message is too long
385*19e1255fScy 	 * to be handled by the underlying log drvier, the message will be
386*19e1255fScy 	 * truncated and the pointer will be added to point user to the
387*19e1255fScy 	 * full message.
388*19e1255fScy 	 */
389*19e1255fScy 	if ((syslog_pointer = dgettext(dict, SYSLOG_POINTER)) == SYSLOG_POINTER)
390*19e1255fScy 		syslog_pointer = dgettext(SYSLOG_DOMAIN, SYSLOG_POINTER);
3917c478bd9Sstevel@tonic-gate 
392627351e3Scy 	syslog_ctl.pri &= LOG_FACMASK;
393627351e3Scy 	if (strcmp(class, FM_LIST_REPAIRED_CLASS) == 0)
394627351e3Scy 		syslog_ctl.pri |= LOG_NOTICE;
395627351e3Scy 	else
396627351e3Scy 		syslog_ctl.pri |= LOG_ERR;
39778432d5eScy 	syslog_emit(hdl, template, code, dgettext(dict, typ),
3987c478bd9Sstevel@tonic-gate 	    dgettext(dict, sev), date, platform, chassis, server, src_name,
3997c478bd9Sstevel@tonic-gate 	    src_vers, uuid, desc, dgettext(dict, rsp), dgettext(dict, imp),
4007c478bd9Sstevel@tonic-gate 	    dgettext(dict, act));
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	/*
4037c478bd9Sstevel@tonic-gate 	 * Switch back to our original language if we had to fall back to C.
4047c478bd9Sstevel@tonic-gate 	 */
4057c478bd9Sstevel@tonic-gate 	if (olang != NULL)
4067c478bd9Sstevel@tonic-gate 		(void) setlocale(LC_MESSAGES, olang);
407e1c679faSaf 
408e1c679faSaf 	if (syslog_locdir != NULL)
409e1c679faSaf 		(void) bindtextdomain(dict, locdir);
410e1c679faSaf 
411e1c679faSaf 	fmd_msg_unlock();
412e1c679faSaf 
413e1c679faSaf 	if (locale_c) {
414e1c679faSaf 		fmd_hdl_debug(hdl,
415e1c679faSaf 		    trfmt == fmt ?
416e1c679faSaf 		    "dgettext(%s, %s) in %s and C failed\n" :
417e1c679faSaf 		    "dgettext(%s, %s) in %s failed; C used\n",
418e1c679faSaf 		    dict, fmt, olang ? olang : "<null>");
419e1c679faSaf 	}
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate static const fmd_prop_t fmd_props[] = {
4237c478bd9Sstevel@tonic-gate 	{ "console", FMD_TYPE_BOOL, "true" },
4247c478bd9Sstevel@tonic-gate 	{ "facility", FMD_TYPE_STRING, "LOG_DAEMON" },
4257c478bd9Sstevel@tonic-gate 	{ "gmt", FMD_TYPE_BOOL, "false" },
4267c478bd9Sstevel@tonic-gate 	{ "syslogd", FMD_TYPE_BOOL, "true" },
4277c478bd9Sstevel@tonic-gate 	{ "url", FMD_TYPE_STRING, "http://sun.com/msg/" },
4287c478bd9Sstevel@tonic-gate 	{ "message_all", FMD_TYPE_BOOL, "false" },
4297c478bd9Sstevel@tonic-gate 	{ NULL, 0, NULL }
4307c478bd9Sstevel@tonic-gate };
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate static const fmd_hdl_ops_t fmd_ops = {
4337c478bd9Sstevel@tonic-gate 	syslog_recv,	/* fmdo_recv */
4347c478bd9Sstevel@tonic-gate 	NULL,		/* fmdo_timeout */
4357c478bd9Sstevel@tonic-gate 	NULL,		/* fmdo_close */
4367c478bd9Sstevel@tonic-gate 	NULL,		/* fmdo_stats */
4377c478bd9Sstevel@tonic-gate 	NULL,		/* fmdo_gc */
4387c478bd9Sstevel@tonic-gate };
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate static const fmd_hdl_info_t fmd_info = {
4417c478bd9Sstevel@tonic-gate 	"Syslog Messaging Agent", "1.0", &fmd_ops, fmd_props
4427c478bd9Sstevel@tonic-gate };
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate void
4457c478bd9Sstevel@tonic-gate _fmd_init(fmd_hdl_t *hdl)
4467c478bd9Sstevel@tonic-gate {
4477c478bd9Sstevel@tonic-gate 	const struct facility *fp;
448e1c679faSaf 	char *facname, *tz, *rootdir, *locdir, *locale, *p;
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	if (fmd_hdl_register(hdl, FMD_API_VERSION, &fmd_info) != 0)
4517c478bd9Sstevel@tonic-gate 		return; /* invalid data in configuration file */
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	(void) fmd_stat_create(hdl, FMD_STAT_NOALLOC, sizeof (syslog_stats) /
4547c478bd9Sstevel@tonic-gate 	    sizeof (fmd_stat_t), (fmd_stat_t *)&syslog_stats);
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	if ((syslog_logfd = open("/dev/conslog", O_WRONLY | O_NOCTTY)) == -1)
4577c478bd9Sstevel@tonic-gate 		fmd_hdl_abort(hdl, "syslog-msgs failed to open /dev/conslog");
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	if ((syslog_msgfd = open("/dev/sysmsg", O_WRONLY | O_NOCTTY)) == -1)
4607c478bd9Sstevel@tonic-gate 		fmd_hdl_abort(hdl, "syslog-msgs failed to open /dev/sysmsg");
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	/*
4637c478bd9Sstevel@tonic-gate 	 * All FMA event dictionaries use msgfmt(1) message objects to produce
4647c478bd9Sstevel@tonic-gate 	 * messages, even for the C locale.  We therefore want to use dgettext
4657c478bd9Sstevel@tonic-gate 	 * for all message lookups, but its defined behavior in the C locale is
4667c478bd9Sstevel@tonic-gate 	 * to return the input string.  Since our input strings are event codes
4677c478bd9Sstevel@tonic-gate 	 * and not format strings, this doesn't help us.  We resolve this nit
4687c478bd9Sstevel@tonic-gate 	 * by setting NLSPATH to a non-existent file: the presence of NLSPATH
4697c478bd9Sstevel@tonic-gate 	 * is defined to force dgettext(3C) to do a full lookup even for C.
4707c478bd9Sstevel@tonic-gate 	 */
4717c478bd9Sstevel@tonic-gate 	if (getenv("NLSPATH") == NULL && putenv(fmd_hdl_strdup(hdl,
4727c478bd9Sstevel@tonic-gate 	    "NLSPATH=/usr/lib/fm/fmd/fmd.cat", FMD_SLEEP)) != 0)
4737c478bd9Sstevel@tonic-gate 		fmd_hdl_abort(hdl, "syslog-msgs failed to set NLSPATH");
4747c478bd9Sstevel@tonic-gate 
475e1c679faSaf 	fmd_msg_lock();
4767c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_MESSAGES, "");
477e1c679faSaf 	locale = setlocale(LC_MESSAGES, NULL);
478e1c679faSaf 	if (locale) {
479e1c679faSaf 		p = alloca(strlen(locale) + 1);
480e1c679faSaf 		locale = strcpy(p, locale);
481e1c679faSaf 	} else {
482e1c679faSaf 		locale = "<null>";
483e1c679faSaf 	}
484e1c679faSaf 	fmd_msg_unlock();
485e1c679faSaf 	fmd_hdl_debug(hdl, "locale=%s\n", locale);
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	/*
4887c478bd9Sstevel@tonic-gate 	 * If the "gmt" property is set to true, force our EVENT-TIME to be
4897c478bd9Sstevel@tonic-gate 	 * reported in GMT time; otherwise we use localtime.  tzset() affects
4907c478bd9Sstevel@tonic-gate 	 * the results of subsequent calls to strftime(3C) above.
4917c478bd9Sstevel@tonic-gate 	 */
4927c478bd9Sstevel@tonic-gate 	if (fmd_prop_get_int32(hdl, "gmt") == FMD_B_TRUE &&
4937c478bd9Sstevel@tonic-gate 	    ((tz = getenv("TZ")) == NULL || strcmp(tz, "GMT") != 0)) {
4947c478bd9Sstevel@tonic-gate 		(void) putenv(fmd_hdl_strdup(hdl, "TZ=GMT", FMD_SLEEP));
4957c478bd9Sstevel@tonic-gate 		tzset(); /* reload env */
4967c478bd9Sstevel@tonic-gate 	}
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	/*
4997c478bd9Sstevel@tonic-gate 	 * Look up the value of the "facility" property and use it to determine
5007c478bd9Sstevel@tonic-gate 	 * what syslog LOG_* facility value we use to fill in our log_ctl_t.
5017c478bd9Sstevel@tonic-gate 	 * The details of our logging method are described above syslog_emit().
5027c478bd9Sstevel@tonic-gate 	 */
5037c478bd9Sstevel@tonic-gate 	facname = fmd_prop_get_string(hdl, "facility");
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	for (fp = syslog_facs; fp->fac_name != NULL; fp++) {
5067c478bd9Sstevel@tonic-gate 		if (strcmp(fp->fac_name, facname) == 0)
5077c478bd9Sstevel@tonic-gate 			break;
5087c478bd9Sstevel@tonic-gate 	}
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	if (fp->fac_name == NULL)
5117c478bd9Sstevel@tonic-gate 		fmd_hdl_abort(hdl, "invalid 'facility' setting: %s\n", facname);
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	fmd_prop_free_string(hdl, facname);
514627351e3Scy 	syslog_ctl.pri = fp->fac_value;
5157c478bd9Sstevel@tonic-gate 	syslog_ctl.flags = SL_CONSOLE | SL_LOGONLY;
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	/*
5187c478bd9Sstevel@tonic-gate 	 * Cache any properties we use every time we receive an event and
5197c478bd9Sstevel@tonic-gate 	 * subscribe to list.suspect events regardless of the .conf file.
5207c478bd9Sstevel@tonic-gate 	 */
5217c478bd9Sstevel@tonic-gate 	syslog_file = fmd_prop_get_int32(hdl, "syslogd");
5227c478bd9Sstevel@tonic-gate 	syslog_cons = fmd_prop_get_int32(hdl, "console");
5237c478bd9Sstevel@tonic-gate 	syslog_url = fmd_prop_get_string(hdl, "url");
5247c478bd9Sstevel@tonic-gate 	syslog_msgall = fmd_prop_get_int32(hdl, "message_all");
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	/*
5277c478bd9Sstevel@tonic-gate 	 * If fmd's rootdir property is set to a non-default root, then we are
5287c478bd9Sstevel@tonic-gate 	 * going to need to rebind the text domains we use for dgettext() as
5297c478bd9Sstevel@tonic-gate 	 * we go.  Look up the default l10n messages directory and make
5307c478bd9Sstevel@tonic-gate 	 * syslog_locdir be this path with fmd.rootdir prepended to it.
5317c478bd9Sstevel@tonic-gate 	 */
5327c478bd9Sstevel@tonic-gate 	rootdir = fmd_prop_get_string(hdl, "fmd.rootdir");
5337c478bd9Sstevel@tonic-gate 
534e1c679faSaf 	if (*rootdir != '\0' && strcmp(rootdir, "/") != 0) {
535e1c679faSaf 		fmd_msg_lock();
536e1c679faSaf 		locdir = bindtextdomain(SYSLOG_DOMAIN, NULL);
537e1c679faSaf 		fmd_msg_unlock();
538e1c679faSaf 		if (locdir != NULL) {
539e1c679faSaf 			size_t len = strlen(rootdir) + strlen(locdir) + 1;
540e1c679faSaf 			syslog_locdir = fmd_hdl_alloc(hdl, len, FMD_SLEEP);
541e1c679faSaf 			(void) snprintf(syslog_locdir, len, "%s%s", rootdir,
542e1c679faSaf 			    locdir);
543e1c679faSaf 			fmd_hdl_debug(hdl,
544e1c679faSaf 			    "binding textdomain to %s for syslog\n",
545e1c679faSaf 			    syslog_locdir);
546e1c679faSaf 		}
5477c478bd9Sstevel@tonic-gate 	}
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	fmd_prop_free_string(hdl, rootdir);
5507c478bd9Sstevel@tonic-gate 	fmd_hdl_subscribe(hdl, FM_LIST_SUSPECT_CLASS);
551627351e3Scy 	fmd_hdl_subscribe(hdl, FM_LIST_REPAIRED_CLASS);
5527c478bd9Sstevel@tonic-gate }
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate void
5557c478bd9Sstevel@tonic-gate _fmd_fini(fmd_hdl_t *hdl)
5567c478bd9Sstevel@tonic-gate {
5577c478bd9Sstevel@tonic-gate 	fmd_hdl_strfree(hdl, syslog_locdir);
5587c478bd9Sstevel@tonic-gate 	fmd_prop_free_string(hdl, syslog_url);
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	(void) close(syslog_logfd);
5617c478bd9Sstevel@tonic-gate 	(void) close(syslog_msgfd);
5627c478bd9Sstevel@tonic-gate }
563