syslog.c revision 6276:2a58fa6611ef
11556Srgrimes/*
21556Srgrimes * CDDL HEADER START
31556Srgrimes *
41556Srgrimes * The contents of this file are subject to the terms of the
51556Srgrimes * Common Development and Distribution License (the "License").
61556Srgrimes * You may not use this file except in compliance with the License.
71556Srgrimes *
81556Srgrimes * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91556Srgrimes * or http://www.opensolaris.org/os/licensing.
101556Srgrimes * See the License for the specific language governing permissions
111556Srgrimes * and limitations under the License.
121556Srgrimes *
131556Srgrimes * When distributing Covered Code, include this CDDL HEADER in each
141556Srgrimes * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151556Srgrimes * If applicable, add the following below this CDDL HEADER, with the
161556Srgrimes * fields enclosed by brackets "[]" replaced with your own identifying
171556Srgrimes * information: Portions Copyright [yyyy] [name of copyright owner]
181556Srgrimes *
191556Srgrimes * CDDL HEADER END
201556Srgrimes */
211556Srgrimes/*
221556Srgrimes * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
231556Srgrimes * Use is subject to license terms.
241556Srgrimes */
251556Srgrimes
261556Srgrimes#pragma ident	"%Z%%M%	%I%	%E% SMI"
271556Srgrimes
281556Srgrimes#include <sys/fm/protocol.h>
291556Srgrimes#include <sys/strlog.h>
301556Srgrimes#include <fm/fmd_api.h>
311556Srgrimes#include <fm/fmd_msg.h>
321556Srgrimes
331556Srgrimes#include <stropts.h>
3436150Scharnier#include <syslog.h>
3536150Scharnier#include <locale.h>
3636150Scharnier#include <strings.h>
371556Srgrimes#include <stdlib.h>
3899110Sobrien#include <unistd.h>
3999110Sobrien#include <limits.h>
401556Srgrimes#include <alloca.h>
4117987Speter#include <errno.h>
4217987Speter#include <fcntl.h>
43114763Sobrien#include <time.h>
4417987Speter
451556Srgrimes/*
461556Srgrimes * SYSLOG_DOMAIN and SYSLOG_TEMPLATE define the dgettext() parameters the agent
471556Srgrimes * can use to retrieve the localized format string for diagnosis messages.
481556Srgrimes * The format string retrieved from SYSLOG_DOMAIN is the default format
4917525Sache * string, but when processing each suspect list, dgettext() is also called
5017525Sache * for the domain that matches the diagcode dictname and if SYSLOG_TEMPLATE
511556Srgrimes * is defined, it overrides the default for that suspect list only.
521556Srgrimes *
531556Srgrimes * Similarly, SYSLOG_URL is also checked to see if syslog_url
541556Srgrimes * should be overridden for each suspect list.
551556Srgrimes *
561556Srgrimes * The net effect of all this is that for a given diagcode DICT-1234-56:
571556Srgrimes *
581556Srgrimes *	- If DICT.mo defines syslog-msgs-message-template, it is used
591556Srgrimes *	  as the format string for the diagnosis message.
601556Srgrimes *
611556Srgrimes *	- Otherwise, syslog-msgs-message-template from FMD.mo is used.
621556Srgrimes *
631556Srgrimes *	- If DICT.mo defines syslog-url, it is used when filling
6420425Ssteve *	  in the %s in the "description" message.
6517987Speter *
6617987Speter *	- Otherwise, if syslog-msgs.conf defines a "url" property, that
6717987Speter *	  value is used.
681556Srgrimes *
691556Srgrimes *	- Otherwise, the default "http://sun.com/msg/" is used (via the
701556Srgrimes *	  fmd_props[] table defined in this file).
711556Srgrimes */
721556Srgrimesstatic const char SYSLOG_DOMAIN[] = "FMD";
731556Srgrimesstatic const char SYSLOG_TEMPLATE[] = "syslog-msgs-message-template";
741556Srgrimesstatic const char SYSLOG_URL[] = "syslog-url";
751556Srgrimes
76201056Sjillesstatic struct stats {
7790111Simp	fmd_stat_t bad_vers;
781556Srgrimes	fmd_stat_t bad_fmri;
791556Srgrimes	fmd_stat_t bad_code;
801556Srgrimes	fmd_stat_t bad_time;
8117987Speter	fmd_stat_t log_err;
821556Srgrimes	fmd_stat_t msg_err;
83208755Sjilles	fmd_stat_t no_msg;
8417987Speter} syslog_stats = {
851556Srgrimes	{ "bad_vers", FMD_TYPE_UINT64, "event version is missing or invalid" },
861556Srgrimes	{ "bad_fmri", FMD_TYPE_UINT64, "event fmri is missing or invalid" },
871556Srgrimes	{ "bad_code", FMD_TYPE_UINT64, "event code has no dictionary name" },
881556Srgrimes	{ "bad_time", FMD_TYPE_UINT64, "event time is not properly encoded" },
8997689Stjr	{ "log_err", FMD_TYPE_UINT64, "failed to log message to log(7D)" },
901556Srgrimes	{ "msg_err", FMD_TYPE_UINT64, "failed to log message to sysmsg(7D)" },
911556Srgrimes	{ "no_msg", FMD_TYPE_UINT64, "message logging suppressed" }
92159632Sstefanf};
931556Srgrimes
94213760Sobrienstatic const struct facility {
951556Srgrimes	const char *fac_name;
96213760Sobrien	int fac_value;
9717987Speter} syslog_facs[] = {
98201056Sjilles	{ "LOG_DAEMON", LOG_DAEMON },
9920425Ssteve	{ "LOG_LOCAL0", LOG_LOCAL0 },
10017987Speter	{ "LOG_LOCAL1", LOG_LOCAL1 },
101201056Sjilles	{ "LOG_LOCAL2", LOG_LOCAL2 },
10220425Ssteve	{ "LOG_LOCAL3", LOG_LOCAL3 },
103201056Sjilles	{ "LOG_LOCAL4", LOG_LOCAL4 },
10420425Ssteve	{ "LOG_LOCAL5", LOG_LOCAL5 },
105201056Sjilles	{ "LOG_LOCAL6", LOG_LOCAL6 },
10620425Ssteve	{ "LOG_LOCAL7", LOG_LOCAL7 },
107201056Sjilles	{ NULL, 0 }
10820425Ssteve};
109201056Sjilles
11097689Stjrstatic char *syslog_locdir;	/* l10n messages directory (if alternate) */
1118855Srgrimesstatic char *syslog_url;	/* current value of "url" property */
1121556Srgrimesstatic int syslog_msgall;	/* set to message all faults */
1131556Srgrimesstatic log_ctl_t syslog_ctl;	/* log(7D) meta-data for each msg */
114201056Sjillesstatic int syslog_logfd = -1;	/* log(7D) file descriptor */
11520425Sstevestatic int syslog_msgfd = -1;	/* sysmsg(7D) file descriptor */
116201056Sjillesstatic int syslog_file;		/* log to syslog_logfd */
117159632Sstefanfstatic int syslog_cons;		/* log to syslog_msgfd */
118208755Sjilles
119208755Sjilles/*
120208755Sjilles * Ideally we would just use syslog(3C) for outputting our messages, but our
121208755Sjilles * messaging standard defines a nice multi-line format and syslogd(1M) is very
122201056Sjilles * inflexible and stupid when it comes to multi-line messages.  It pulls data
12320425Ssteve * out of log(7D) and splits it up by \n, printing each line to the console
12420425Ssteve * with its usual prefix of date and sender; it uses the same behavior for the
12520425Ssteve * messages file as well.  Further, syslog(3C) provides no CE_CONT equivalent
1261556Srgrimes * for userland callers (which at least works around repeated file prefixing).
1271556Srgrimes * So with a multi-line message format, your file and console end up like this:
128213760Sobrien *
1291556Srgrimes * Dec 02 18:08:40 hostname this is my nicely formatted
130213760Sobrien * Dec 02 18:08:40 hostname message designed for 80 cols
131207678Sjilles * ...
132207678Sjilles *
133207678Sjilles * To resolve these issues, we use our own syslog_emit() wrapper to emit
134213760Sobrien * messages and some knowledge of how the Solaris log drivers work.  We first
135207678Sjilles * construct an enlarged format string containing the appropriate msgid(1).
136207678Sjilles * We then format the caller's message using the provided format and buffer.
137207678Sjilles * We send this message to log(7D) using putmsg() with SL_CONSOLE | SL_LOGONLY
138200956Sjilles * set in the log_ctl_t.  The log driver allows us to set SL_LOGONLY when we
139200956Sjilles * construct messages ourself, indicating that syslogd should only emit the
140200956Sjilles * message to /var/adm/messages and any remote hosts, and skip the console.
1411556Srgrimes * Then we emit the message a second time, without the special prefix, to the
1421556Srgrimes * sysmsg(7D) device, which handles console redirection and also permits us
143155302Sschweikh * to output any characters we like to the console, including \n and \r.
1441556Srgrimes */
1451556Srgrimes/*PRINTFLIKE4*/
1461556Srgrimesstatic void
1471556Srgrimessyslog_emit(fmd_hdl_t *hdl, char *buf, size_t len, const char *msgformat, ...)
148201053Sjilles{
1491556Srgrimes	struct strbuf ctl, dat;
1501556Srgrimes	uint32_t msgid;
1511556Srgrimes
1521556Srgrimes	char *format;
1531556Srgrimes	size_t formatlen;
1541556Srgrimes	va_list ap;
1551556Srgrimes
1561556Srgrimes	formatlen = strlen(msgformat) + 64; /* +64 for prefix and \0 */
1571556Srgrimes	format = alloca(formatlen);
1581556Srgrimes
1591556Srgrimes	STRLOG_MAKE_MSGID(msgformat, msgid);
1601556Srgrimes	(void) snprintf(format, formatlen,
1611556Srgrimes	    "fmd: [ID %u FACILITY_AND_PRIORITY] %s", msgid, msgformat);
1621556Srgrimes
1631556Srgrimes	va_start(ap, msgformat);
1641556Srgrimes	(void) vsnprintf(buf, len, format, ap);
1651556Srgrimes	va_end(ap);
1661556Srgrimes
1671556Srgrimes	ctl.buf = (void *)&syslog_ctl;
16890111Simp	ctl.len = sizeof (syslog_ctl);
16990111Simp
17097689Stjr	dat.buf = buf;
1711556Srgrimes	dat.len = strlen(buf) + 1;
1721556Srgrimes
1731556Srgrimes	if (syslog_file && putmsg(syslog_logfd, &ctl, &dat, 0) != 0) {
1741556Srgrimes		fmd_hdl_debug(hdl, "putmsg failed: %s\n", strerror(errno));
1751556Srgrimes		syslog_stats.log_err.fmds_value.ui64++;
1761556Srgrimes	}
1771556Srgrimes
1781556Srgrimes	dat.buf = strchr(buf, ']');
1791556Srgrimes	dat.len -= (size_t)(dat.buf - buf);
180201056Sjilles
181201056Sjilles	dat.buf[0] = '\r'; /* overwrite ']' with carriage return */
18220425Ssteve	dat.buf[1] = '\n'; /* overwrite ' ' with newline */
1831556Srgrimes
1841556Srgrimes	if (syslog_cons && write(syslog_msgfd, dat.buf, dat.len) != dat.len) {
1851556Srgrimes		fmd_hdl_debug(hdl, "write failed: %s\n", strerror(errno));
1861556Srgrimes		syslog_stats.msg_err.fmds_value.ui64++;
1871556Srgrimes	}
1881556Srgrimes}
1891556Srgrimes
1901556Srgrimes/*ARGSUSED*/
1911556Srgrimesstatic void
192201056Sjillessyslog_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl, const char *class)
1931556Srgrimes{
1941556Srgrimes	char *uuid, *code, *dict, *url, *urlcode, *template, *p;
19597689Stjr	char *src_name, *src_vers, *platform, *chassis, *server;
19697689Stjr	char *typ, *sev, *fmt, *trfmt, *rsp, *imp, *act, *locdir;
19797689Stjr	char msg[1024], desc[1024], date[64];
19897689Stjr	boolean_t domsg;
1991556Srgrimes
2001556Srgrimes	nvlist_t *fmri, *auth;
2011556Srgrimes	uint8_t version;
20220425Ssteve	struct tm tm, *tmp;
20320425Ssteve	int64_t *tv;
20420425Ssteve	time_t sec;
20520425Ssteve	uint_t tn = 0;
206200956Sjilles	char *olang = NULL;
20720425Ssteve	int locale_c = 0;
20820425Ssteve	size_t len;
209194765Sjilles
21020425Ssteve	if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 ||
211199660Sjilles	    version > FM_SUSPECT_VERSION) {
21220425Ssteve		fmd_hdl_debug(hdl, "invalid event version: %u\n", version);
213199660Sjilles		syslog_stats.bad_vers.fmds_value.ui64++;
21420425Ssteve		return; /* invalid event version */
21520425Ssteve	}
21620425Ssteve
21720425Ssteve	if (!syslog_msgall && nvlist_lookup_boolean_value(nvl,
21820425Ssteve	    FM_SUSPECT_MESSAGE, &domsg) == 0 && !domsg) {
21920425Ssteve		fmd_hdl_debug(hdl, "%s requested no message\n", class);
22020425Ssteve		syslog_stats.no_msg.fmds_value.ui64++;
221199660Sjilles		return; /* event is not to be messaged */
22220425Ssteve	}
22320425Ssteve
22420425Ssteve	/*
22520425Ssteve	 * Extract the DE element, which is an FMRI for the diagnosis engine
226155302Sschweikh	 * that made this event, and validate its meta-data before continuing.
2271556Srgrimes	 */
2281556Srgrimes	if (nvlist_lookup_nvlist(nvl, FM_SUSPECT_DE, &fmri) != 0 ||
2291556Srgrimes	    nvlist_lookup_string(fmri, FM_FMRI_SCHEME, &p) != 0 ||
2301556Srgrimes	    strcmp(p, FM_FMRI_SCHEME_FMD) != 0 ||
231200956Sjilles	    nvlist_lookup_uint8(fmri, FM_VERSION, &version) != 0 ||
23217987Speter	    version > FM_FMD_SCHEME_VERSION ||
233200956Sjilles	    nvlist_lookup_nvlist(fmri, FM_FMRI_AUTHORITY, &auth) != 0 ||
2341556Srgrimes	    nvlist_lookup_uint8(auth, FM_VERSION, &version) != 0 ||
2351556Srgrimes	    version > FM_FMRI_AUTH_VERSION) {
2361556Srgrimes		syslog_stats.bad_fmri.fmds_value.ui64++;
2371556Srgrimes		return; /* invalid de fmri */
2381556Srgrimes	}
2391556Srgrimes
2401556Srgrimes	/*
24117525Sache	 * Extract the relevant identifying elements of the FMRI and authority.
2421556Srgrimes	 * Note: for now, we ignore FM_FMRI_AUTH_DOMAIN (only for SPs).
24317525Sache	 */
2441556Srgrimes	(void) nvlist_lookup_string(fmri, FM_FMRI_FMD_NAME, &src_name);
2451556Srgrimes	(void) nvlist_lookup_string(fmri, FM_FMRI_FMD_VERSION, &src_vers);
2461556Srgrimes	(void) nvlist_lookup_string(auth, FM_FMRI_AUTH_PRODUCT, &platform);
2471556Srgrimes	(void) nvlist_lookup_string(auth, FM_FMRI_AUTH_SERVER, &server);
2481556Srgrimes
2491556Srgrimes	if (nvlist_lookup_string(auth, FM_FMRI_AUTH_CHASSIS, &chassis) != 0)
2501556Srgrimes		chassis = "-"; /* chassis serial number may not be present */
2511556Srgrimes
2521556Srgrimes	/*
2531556Srgrimes	 * Extract the uuid and diagcode dictionary from the event code.  The
2541556Srgrimes	 * dictionary name is the text preceding the first "-" in the code.
2551556Srgrimes	 */
2561556Srgrimes	(void) nvlist_lookup_string(nvl, FM_SUSPECT_UUID, &uuid);
2571556Srgrimes	(void) nvlist_lookup_string(nvl, FM_SUSPECT_DIAG_CODE, &code);
2581556Srgrimes
2591556Srgrimes	if ((p = strchr(code, '-')) == NULL || p == code) {
2601556Srgrimes		fmd_hdl_debug(hdl, "invalid diagnosis code: %s\n", code);
261200956Sjilles		syslog_stats.bad_code.fmds_value.ui64++;
262200956Sjilles		return; /* invalid diagnosis code */
263200956Sjilles	}
2641556Srgrimes
265200956Sjilles	dict = alloca((size_t)(p - code) + 1);
266200956Sjilles	(void) strncpy(dict, code, (size_t)(p - code));
267200956Sjilles	dict[(size_t)(p - code)] = '\0';
2681556Srgrimes
2691556Srgrimes	/*
2701556Srgrimes	 * Alloca a hunk of memory and use it to create the msgid strings
27117525Sache	 * <code>.type, <code>.severity, <code>.description, and so forth.
272200956Sjilles	 * These form the msgids we will use to look up the localized text.
27390111Simp	 * Since we've allocated things to be of the right size, we know
274207678Sjilles	 * than snprintf() can't overflow: INT_MAX is used shut lint up and
2751556Srgrimes	 * avoid code to needlessly recompute the remaining buffer space.
27617525Sache	 */
27717525Sache	typ = alloca(6 * (strlen(code) + 16));
27817525Sache	sev = typ + snprintf(typ, INT_MAX, "%s.type", code) + 1;
27917525Sache	fmt = sev + snprintf(sev, INT_MAX, "%s.severity", code) + 1;
28017525Sache	rsp = fmt + snprintf(fmt, INT_MAX, "%s.description", code) + 1;
28117525Sache	imp = rsp + snprintf(rsp, INT_MAX, "%s.response", code) + 1;
282207678Sjilles	act = imp + snprintf(imp, INT_MAX, "%s.impact", code) + 1;
283207678Sjilles	(void) snprintf(act, INT_MAX, "%s.action", code);
284207678Sjilles
285207678Sjilles	fmd_msg_lock();
28617525Sache
28717525Sache	if (syslog_locdir != NULL)
28817525Sache		locdir =  bindtextdomain(dict, syslog_locdir);
2891556Srgrimes
290171268Sscf	if ((trfmt = dgettext(dict, fmt)) == fmt) {
2911556Srgrimes		/*
292171268Sscf		 * We didn't find a translation in the dictionary for the
293171268Sscf		 * current language.  The string we passed to gettext is merely
294171268Sscf		 * an index - it isn't sufficient, on its own, to be used as the
295213760Sobrien		 * message.  Fall back to C and try again.
296200956Sjilles		 */
297171268Sscf		olang = setlocale(LC_MESSAGES, NULL);
298171268Sscf		if (olang) {
299171268Sscf			p = alloca(strlen(olang) + 1);
300171268Sscf			olang = strcpy(p, olang);
301171268Sscf		}
302171268Sscf		locale_c = 1;
303171268Sscf		(void) setlocale(LC_MESSAGES, "C");
304171268Sscf		trfmt = dgettext(dict, fmt);
305171268Sscf	}
306171268Sscf
307171268Sscf	if ((url = dgettext(dict, SYSLOG_URL)) == SYSLOG_URL)
308171268Sscf		url = syslog_url;
309171268Sscf
310171268Sscf	/*
311171268Sscf	 * If the URL ends with a slash, that indicates the code should be
312171268Sscf	 * appended to it.  After formatting the URL, reformat the DESC
313171268Sscf	 * text using the URL as an snprintf argument.
314171268Sscf	 */
3151556Srgrimes	len = strlen(url);
3161556Srgrimes	if (url[len - 1] == '/') {
3171556Srgrimes		urlcode = alloca(len + strlen(code) + 1);
3181556Srgrimes		(void) snprintf(urlcode, INT_MAX, "%s%s", url, code);
3191556Srgrimes	} else {
3201556Srgrimes		urlcode = url;
3211556Srgrimes	}
32290111Simp	/* LINTED - variable format specifier to snprintf() */
32317987Speter	(void) snprintf(desc, sizeof (desc), trfmt, urlcode);
3241556Srgrimes
32590112Simp	/*
3261556Srgrimes	 * Extract the diagnosis time and format it using the locale's default.
32745263Scracauer	 * strftime() will use GMT or local time based on our "gmt" setting.
32845263Scracauer	 */
3291556Srgrimes	if (nvlist_lookup_int64_array(nvl, FM_SUSPECT_DIAG_TIME,
3301556Srgrimes	    &tv, &tn) == 0 && tn == 2 && (sec = (time_t)tv[0]) != (time_t)-1 &&
3311556Srgrimes	    (tmp = localtime_r(&sec, &tm)) != NULL)
3321556Srgrimes		(void) strftime(date, sizeof (date), "%C", tmp);
33390112Simp	else {
3341556Srgrimes		syslog_stats.bad_time.fmds_value.ui64++;
3351556Srgrimes		(void) strcpy(date, "-");
3361556Srgrimes	}
33720425Ssteve
33820425Ssteve	/*
33920425Ssteve	 * Create and log the final string by filling in the template with the
34020425Ssteve	 * strings we've created and the strings from the message dictionary.
3411556Srgrimes	 * If a template is provided for this dictionary, use it, otherwise
3421556Srgrimes	 * fall back to the default template.
34320425Ssteve	 */
34420425Ssteve	if ((template = dgettext(dict, SYSLOG_TEMPLATE)) == SYSLOG_TEMPLATE)
3451556Srgrimes		template = dgettext(SYSLOG_DOMAIN, SYSLOG_TEMPLATE);
3461556Srgrimes
34720425Ssteve	syslog_ctl.pri &= LOG_FACMASK;
34820425Ssteve	if (strcmp(class, FM_LIST_REPAIRED_CLASS) == 0)
34920425Ssteve		syslog_ctl.pri |= LOG_NOTICE;
35020425Ssteve	else
351203576Sjilles		syslog_ctl.pri |= LOG_ERR;
352203576Sjilles	syslog_emit(hdl, msg, sizeof (msg),
353203576Sjilles	    template, code, dgettext(dict, typ),
354203576Sjilles	    dgettext(dict, sev), date, platform, chassis, server, src_name,
35520425Ssteve	    src_vers, uuid, desc, dgettext(dict, rsp), dgettext(dict, imp),
356203576Sjilles	    dgettext(dict, act));
357203576Sjilles
3581556Srgrimes	/*
35917525Sache	 * Switch back to our original language if we had to fall back to C.
360171268Sscf	 */
36117525Sache	if (olang != NULL)
36217525Sache		(void) setlocale(LC_MESSAGES, olang);
3631556Srgrimes
3641556Srgrimes	if (syslog_locdir != NULL)
3651556Srgrimes		(void) bindtextdomain(dict, locdir);
3661556Srgrimes
3671556Srgrimes	fmd_msg_unlock();
3681556Srgrimes
3691556Srgrimes	if (locale_c) {
3701556Srgrimes		fmd_hdl_debug(hdl,
3711556Srgrimes		    trfmt == fmt ?
37220425Ssteve		    "dgettext(%s, %s) in %s and C failed\n" :
37317525Sache		    "dgettext(%s, %s) in %s failed; C used\n",
3741556Srgrimes		    dict, fmt, olang ? olang : "<null>");
37517525Sache	}
376171268Sscf}
37717525Sache
37817525Sachestatic const fmd_prop_t fmd_props[] = {
37917525Sache	{ "console", FMD_TYPE_BOOL, "true" },
3801556Srgrimes	{ "facility", FMD_TYPE_STRING, "LOG_DAEMON" },
3811556Srgrimes	{ "gmt", FMD_TYPE_BOOL, "false" },
3821556Srgrimes	{ "syslogd", FMD_TYPE_BOOL, "true" },
3831556Srgrimes	{ "url", FMD_TYPE_STRING, "http://sun.com/msg/" },
3841556Srgrimes	{ "message_all", FMD_TYPE_BOOL, "false" },
3851556Srgrimes	{ NULL, 0, NULL }
3861556Srgrimes};
3871556Srgrimes
3881556Srgrimesstatic const fmd_hdl_ops_t fmd_ops = {
38990111Simp	syslog_recv,	/* fmdo_recv */
39090111Simp	NULL,		/* fmdo_timeout */
3911556Srgrimes	NULL,		/* fmdo_close */
3921556Srgrimes	NULL,		/* fmdo_stats */
3931556Srgrimes	NULL,		/* fmdo_gc */
3941556Srgrimes};
3951556Srgrimes
3961556Srgrimesstatic const fmd_hdl_info_t fmd_info = {
3971556Srgrimes	"Syslog Messaging Agent", "1.0", &fmd_ops, fmd_props
3981556Srgrimes};
3991556Srgrimes
4001556Srgrimesvoid
4011556Srgrimes_fmd_init(fmd_hdl_t *hdl)
4021556Srgrimes{
4031556Srgrimes	const struct facility *fp;
4041556Srgrimes	char *facname, *tz, *rootdir, *locdir, *locale, *p;
4051556Srgrimes
4061556Srgrimes	if (fmd_hdl_register(hdl, FMD_API_VERSION, &fmd_info) != 0)
407200956Sjilles		return; /* invalid data in configuration file */
40890111Simp
4091556Srgrimes	(void) fmd_stat_create(hdl, FMD_STAT_NOALLOC, sizeof (syslog_stats) /
4101556Srgrimes	    sizeof (fmd_stat_t), (fmd_stat_t *)&syslog_stats);
4111556Srgrimes
4121556Srgrimes	if ((syslog_logfd = open("/dev/conslog", O_WRONLY | O_NOCTTY)) == -1)
4131556Srgrimes		fmd_hdl_abort(hdl, "syslog-msgs failed to open /dev/conslog");
4141556Srgrimes
4151556Srgrimes	if ((syslog_msgfd = open("/dev/sysmsg", O_WRONLY | O_NOCTTY)) == -1)
4161556Srgrimes		fmd_hdl_abort(hdl, "syslog-msgs failed to open /dev/sysmsg");
4171556Srgrimes
4181556Srgrimes	/*
4191556Srgrimes	 * All FMA event dictionaries use msgfmt(1) message objects to produce
4201556Srgrimes	 * messages, even for the C locale.  We therefore want to use dgettext
4211556Srgrimes	 * for all message lookups, but its defined behavior in the C locale is
4221556Srgrimes	 * to return the input string.  Since our input strings are event codes
4231556Srgrimes	 * and not format strings, this doesn't help us.  We resolve this nit
4241556Srgrimes	 * by setting NLSPATH to a non-existent file: the presence of NLSPATH
4251556Srgrimes	 * is defined to force dgettext(3C) to do a full lookup even for C.
4261556Srgrimes	 */
4271556Srgrimes	if (getenv("NLSPATH") == NULL && putenv(fmd_hdl_strdup(hdl,
4281556Srgrimes	    "NLSPATH=/usr/lib/fm/fmd/fmd.cat", FMD_SLEEP)) != 0)
4291556Srgrimes		fmd_hdl_abort(hdl, "syslog-msgs failed to set NLSPATH");
430200956Sjilles
43117987Speter	fmd_msg_lock();
4321556Srgrimes	(void) setlocale(LC_MESSAGES, "");
4331556Srgrimes	locale = setlocale(LC_MESSAGES, NULL);
434212467Sjilles	if (locale) {
4351556Srgrimes		p = alloca(strlen(locale) + 1);
436212467Sjilles		locale = strcpy(p, locale);
4371556Srgrimes	} else {
4381556Srgrimes		locale = "<null>";
439212467Sjilles	}
4401556Srgrimes	fmd_msg_unlock();
441212467Sjilles	fmd_hdl_debug(hdl, "locale=%s\n", locale);
442212467Sjilles
4431556Srgrimes	/*
4441556Srgrimes	 * If the "gmt" property is set to true, force our EVENT-TIME to be
44517987Speter	 * reported in GMT time; otherwise we use localtime.  tzset() affects
44617987Speter	 * the results of subsequent calls to strftime(3C) above.
4471556Srgrimes	 */
4481556Srgrimes	if (fmd_prop_get_int32(hdl, "gmt") == FMD_B_TRUE &&
4491556Srgrimes	    ((tz = getenv("TZ")) == NULL || strcmp(tz, "GMT") != 0)) {
4501556Srgrimes		(void) putenv(fmd_hdl_strdup(hdl, "TZ=GMT", FMD_SLEEP));
4511556Srgrimes		tzset(); /* reload env */
4521556Srgrimes	}
4531556Srgrimes
4541556Srgrimes	/*
455207678Sjilles	 * Look up the value of the "facility" property and use it to determine
456207678Sjilles	 * what syslog LOG_* facility value we use to fill in our log_ctl_t.
457207678Sjilles	 * The details of our logging method are described above syslog_emit().
458207678Sjilles	 */
459207678Sjilles	facname = fmd_prop_get_string(hdl, "facility");
460207678Sjilles
461207678Sjilles	for (fp = syslog_facs; fp->fac_name != NULL; fp++) {
462207678Sjilles		if (strcmp(fp->fac_name, facname) == 0)
463207678Sjilles			break;
464207678Sjilles	}
4651556Srgrimes
466207678Sjilles	if (fp->fac_name == NULL)
467207678Sjilles		fmd_hdl_abort(hdl, "invalid 'facility' setting: %s\n", facname);
468207678Sjilles
469207678Sjilles	fmd_prop_free_string(hdl, facname);
470207678Sjilles	syslog_ctl.pri = fp->fac_value;
471207678Sjilles	syslog_ctl.flags = SL_CONSOLE | SL_LOGONLY;
472207678Sjilles
473207678Sjilles	/*
474207678Sjilles	 * Cache any properties we use every time we receive an event and
475207678Sjilles	 * subscribe to list.suspect events regardless of the .conf file.
476207678Sjilles	 */
477207678Sjilles	syslog_file = fmd_prop_get_int32(hdl, "syslogd");
478207678Sjilles	syslog_cons = fmd_prop_get_int32(hdl, "console");
479207678Sjilles	syslog_url = fmd_prop_get_string(hdl, "url");
480207678Sjilles	syslog_msgall = fmd_prop_get_int32(hdl, "message_all");
481207678Sjilles
482207678Sjilles	/*
483207678Sjilles	 * If fmd's rootdir property is set to a non-default root, then we are
484207678Sjilles	 * going to need to rebind the text domains we use for dgettext() as
485207678Sjilles	 * we go.  Look up the default l10n messages directory and make
486207678Sjilles	 * syslog_locdir be this path with fmd.rootdir prepended to it.
487207678Sjilles	 */
488207678Sjilles	rootdir = fmd_prop_get_string(hdl, "fmd.rootdir");
489207678Sjilles
490207678Sjilles	if (*rootdir != '\0' && strcmp(rootdir, "/") != 0) {
491207678Sjilles		fmd_msg_lock();
4921556Srgrimes		locdir = bindtextdomain(SYSLOG_DOMAIN, NULL);
493207678Sjilles		fmd_msg_unlock();
494207678Sjilles		if (locdir != NULL) {
495207678Sjilles			size_t len = strlen(rootdir) + strlen(locdir) + 1;
496207678Sjilles			syslog_locdir = fmd_hdl_alloc(hdl, len, FMD_SLEEP);
497207678Sjilles			(void) snprintf(syslog_locdir, len, "%s%s", rootdir,
498207678Sjilles			    locdir);
499207678Sjilles			fmd_hdl_debug(hdl,
500207678Sjilles			    "binding textdomain to %s for syslog\n",
501207678Sjilles			    syslog_locdir);
502207678Sjilles		}
503207678Sjilles	}
504207678Sjilles
505207678Sjilles	fmd_prop_free_string(hdl, rootdir);
506207678Sjilles	fmd_hdl_subscribe(hdl, FM_LIST_SUSPECT_CLASS);
507207678Sjilles	fmd_hdl_subscribe(hdl, FM_LIST_REPAIRED_CLASS);
508207678Sjilles}
509207678Sjilles
510207678Sjillesvoid
511207678Sjilles_fmd_fini(fmd_hdl_t *hdl)
5121556Srgrimes{
5131556Srgrimes	fmd_hdl_strfree(hdl, syslog_locdir);
5141556Srgrimes	fmd_prop_free_string(hdl, syslog_url);
5151556Srgrimes
5161556Srgrimes	(void) close(syslog_logfd);
51790111Simp	(void) close(syslog_msgfd);
51890111Simp}
5191556Srgrimes