17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
5e1c679faf * Common Development and Distribution License (the "License").
6e1c679faf * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
21b695575Robert Johnston
227c478bdstevel@tonic-gate/*
23f6e214cGavin Maltby * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bdstevel@tonic-gate */
257c478bdstevel@tonic-gate
267c478bdstevel@tonic-gate#include <sys/fm/protocol.h>
277c478bdstevel@tonic-gate#include <sys/strlog.h>
28072c378mb#include <sys/log.h>
29f6e214cGavin Maltby#include <libscf.h>
30b695575Robert Johnston
317c478bdstevel@tonic-gate#include <fm/fmd_api.h>
32e1c679faf#include <fm/fmd_msg.h>
337c478bdstevel@tonic-gate
347c478bdstevel@tonic-gate#include <stropts.h>
357c478bdstevel@tonic-gate#include <strings.h>
36b695575Robert Johnston#include <syslog.h>
377c478bdstevel@tonic-gate#include <alloca.h>
38b695575Robert Johnston#include <unistd.h>
39b695575Robert Johnston#include <stdlib.h>
407c478bdstevel@tonic-gate#include <errno.h>
417c478bdstevel@tonic-gate#include <fcntl.h>
427c478bdstevel@tonic-gate
437c478bdstevel@tonic-gatestatic struct stats {
447c478bdstevel@tonic-gate	fmd_stat_t bad_vers;
457c478bdstevel@tonic-gate	fmd_stat_t bad_code;
467c478bdstevel@tonic-gate	fmd_stat_t log_err;
477c478bdstevel@tonic-gate	fmd_stat_t msg_err;
487c478bdstevel@tonic-gate	fmd_stat_t no_msg;
497c478bdstevel@tonic-gate} syslog_stats = {
507c478bdstevel@tonic-gate	{ "bad_vers", FMD_TYPE_UINT64, "event version is missing or invalid" },
517c478bdstevel@tonic-gate	{ "bad_code", FMD_TYPE_UINT64, "event code has no dictionary name" },
527c478bdstevel@tonic-gate	{ "log_err", FMD_TYPE_UINT64, "failed to log message to log(7D)" },
537c478bdstevel@tonic-gate	{ "msg_err", FMD_TYPE_UINT64, "failed to log message to sysmsg(7D)" },
547c478bdstevel@tonic-gate	{ "no_msg", FMD_TYPE_UINT64, "message logging suppressed" }
557c478bdstevel@tonic-gate};
567c478bdstevel@tonic-gate
577c478bdstevel@tonic-gatestatic const struct facility {
587c478bdstevel@tonic-gate	const char *fac_name;
597c478bdstevel@tonic-gate	int fac_value;
607c478bdstevel@tonic-gate} syslog_facs[] = {
617c478bdstevel@tonic-gate	{ "LOG_DAEMON", LOG_DAEMON },
627c478bdstevel@tonic-gate	{ "LOG_LOCAL0", LOG_LOCAL0 },
637c478bdstevel@tonic-gate	{ "LOG_LOCAL1", LOG_LOCAL1 },
647c478bdstevel@tonic-gate	{ "LOG_LOCAL2", LOG_LOCAL2 },
657c478bdstevel@tonic-gate	{ "LOG_LOCAL3", LOG_LOCAL3 },
667c478bdstevel@tonic-gate	{ "LOG_LOCAL4", LOG_LOCAL4 },
677c478bdstevel@tonic-gate	{ "LOG_LOCAL5", LOG_LOCAL5 },
687c478bdstevel@tonic-gate	{ "LOG_LOCAL6", LOG_LOCAL6 },
697c478bdstevel@tonic-gate	{ "LOG_LOCAL7", LOG_LOCAL7 },
707c478bdstevel@tonic-gate	{ NULL, 0 }
717c478bdstevel@tonic-gate};
727c478bdstevel@tonic-gate
73b695575Robert Johnstonstatic fmd_msg_hdl_t *syslog_msghdl; /* handle for libfmd_msg calls */
747c478bdstevel@tonic-gatestatic int syslog_msgall;	/* set to message all faults */
757c478bdstevel@tonic-gatestatic log_ctl_t syslog_ctl;	/* log(7D) meta-data for each msg */
767c478bdstevel@tonic-gatestatic int syslog_logfd = -1;	/* log(7D) file descriptor */
777c478bdstevel@tonic-gatestatic int syslog_msgfd = -1;	/* sysmsg(7D) file descriptor */
787c478bdstevel@tonic-gatestatic int syslog_file;		/* log to syslog_logfd */
797c478bdstevel@tonic-gatestatic int syslog_cons;		/* log to syslog_msgfd */
80b695575Robert Johnstonstatic const char SYSLOG_POINTER[] = "syslog-msgs-pointer";
817c478bdstevel@tonic-gate
827c478bdstevel@tonic-gate/*
837c478bdstevel@tonic-gate * Ideally we would just use syslog(3C) for outputting our messages, but our
847c478bdstevel@tonic-gate * messaging standard defines a nice multi-line format and syslogd(1M) is very
857c478bdstevel@tonic-gate * inflexible and stupid when it comes to multi-line messages.  It pulls data
867c478bdstevel@tonic-gate * out of log(7D) and splits it up by \n, printing each line to the console
877c478bdstevel@tonic-gate * with its usual prefix of date and sender; it uses the same behavior for the
887c478bdstevel@tonic-gate * messages file as well.  Further, syslog(3C) provides no CE_CONT equivalent
897c478bdstevel@tonic-gate * for userland callers (which at least works around repeated file prefixing).
907c478bdstevel@tonic-gate * So with a multi-line message format, your file and console end up like this:
917c478bdstevel@tonic-gate *
927c478bdstevel@tonic-gate * Dec 02 18:08:40 hostname this is my nicely formatted
937c478bdstevel@tonic-gate * Dec 02 18:08:40 hostname message designed for 80 cols
947c478bdstevel@tonic-gate * ...
957c478bdstevel@tonic-gate *
967c478bdstevel@tonic-gate * To resolve these issues, we use our own syslog_emit() wrapper to emit
977c478bdstevel@tonic-gate * messages and some knowledge of how the Solaris log drivers work.  We first
987c478bdstevel@tonic-gate * construct an enlarged format string containing the appropriate msgid(1).
997c478bdstevel@tonic-gate * We then format the caller's message using the provided format and buffer.
1007c478bdstevel@tonic-gate * We send this message to log(7D) using putmsg() with SL_CONSOLE | SL_LOGONLY
1017c478bdstevel@tonic-gate * set in the log_ctl_t.  The log driver allows us to set SL_LOGONLY when we
1027c478bdstevel@tonic-gate * construct messages ourself, indicating that syslogd should only emit the
1037c478bdstevel@tonic-gate * message to /var/adm/messages and any remote hosts, and skip the console.
1047c478bdstevel@tonic-gate * Then we emit the message a second time, without the special prefix, to the
1057c478bdstevel@tonic-gate * sysmsg(7D) device, which handles console redirection and also permits us
1067c478bdstevel@tonic-gate * to output any characters we like to the console, including \n and \r.
1077c478bdstevel@tonic-gate */
1087c478bdstevel@tonic-gatestatic void
109b695575Robert Johnstonsyslog_emit(fmd_hdl_t *hdl, const char *msg)
1107c478bdstevel@tonic-gate{
1117c478bdstevel@tonic-gate	struct strbuf ctl, dat;
1127c478bdstevel@tonic-gate	uint32_t msgid;
1137c478bdstevel@tonic-gate
114b695575Robert Johnston	char *buf;
115b695575Robert Johnston	size_t buflen;
1167c478bdstevel@tonic-gate
117b695575Robert Johnston	const char *format = "fmd: [ID %u FACILITY_AND_PRIORITY] %s";
118b695575Robert Johnston	STRLOG_MAKE_MSGID(format, msgid);
1197c478bdstevel@tonic-gate
120b695575Robert Johnston	buflen = snprintf(NULL, 0, format, msgid, msg);
121b695575Robert Johnston	buf = alloca(buflen + 1);
122b695575Robert Johnston	(void) snprintf(buf, buflen + 1, format, msgid, msg);
12378432d5cy
1247c478bdstevel@tonic-gate	ctl.buf = (void *)&syslog_ctl;
1257c478bdstevel@tonic-gate	ctl.len = sizeof (syslog_ctl);
1267c478bdstevel@tonic-gate
1277c478bdstevel@tonic-gate	dat.buf = buf;
128b695575Robert Johnston	dat.len = buflen + 1;
129072c378mb
130072c378mb	/*
131b695575Robert Johnston	 * The underlying log driver won't accept messages longer than
132b695575Robert Johnston	 * LOG_MAXPS bytes.  Therefore, messages which exceed this limit will
133b695575Robert Johnston	 * be truncated and appended with a pointer to the full message.
134072c378mb	 */
135b695575Robert Johnston	if (dat.len > LOG_MAXPS) {
136b695575Robert Johnston		char *syslog_pointer, *p;
137b695575Robert Johnston		size_t plen;
138b695575Robert Johnston
139b695575Robert Johnston		if ((syslog_pointer = fmd_msg_gettext_id(syslog_msghdl, NULL,
140b695575Robert Johnston		    SYSLOG_POINTER)) == NULL) {
141b695575Robert Johnston			/*
142b695575Robert Johnston			 * This shouldn't happen, but if it does we'll just
143b695575Robert Johnston			 * truncate the message.
144b695575Robert Johnston			 */
145b695575Robert Johnston			buf[LOG_MAXPS - 1] = '\0';
146b695575Robert Johnston			dat.len = LOG_MAXPS;
147b695575Robert Johnston		} else {
148b695575Robert Johnston			plen = strlen(syslog_pointer) + 1;
149b695575Robert Johnston			buf[LOG_MAXPS - plen] = '\0';
150b695575Robert Johnston			/*
151b695575Robert Johnston			 * If possible, the pointer is appended after a newline
152b695575Robert Johnston			 */
153b695575Robert Johnston			if ((p = strrchr(buf, '\n')) == NULL)
154b695575Robert Johnston				p = &buf[LOG_MAXPS - plen];
155b695575Robert Johnston
156b695575Robert Johnston			(void) strcpy(p, syslog_pointer);
157b695575Robert Johnston			free(syslog_pointer);
158b695575Robert Johnston			dat.len = strlen(buf) + 1;
159b695575Robert Johnston		}
16019e1255cy	}
1617c478bdstevel@tonic-gate	if (syslog_file && putmsg(syslog_logfd, &ctl, &dat, 0) != 0) {
1627c478bdstevel@tonic-gate		fmd_hdl_debug(hdl, "putmsg failed: %s\n", strerror(errno));
1637c478bdstevel@tonic-gate		syslog_stats.log_err.fmds_value.ui64++;
1647c478bdstevel@tonic-gate	}
1657c478bdstevel@tonic-gate
1667c478bdstevel@tonic-gate	dat.buf = strchr(buf, ']');
167b695575Robert Johnston	dat.len -= (size_t)(dat.buf - buf);
1687c478bdstevel@tonic-gate
1697c478bdstevel@tonic-gate	dat.buf[0] = '\r'; /* overwrite ']' with carriage return */
1707c478bdstevel@tonic-gate	dat.buf[1] = '\n'; /* overwrite ' ' with newline */
1717c478bdstevel@tonic-gate
1727c478bdstevel@tonic-gate	if (syslog_cons && write(syslog_msgfd, dat.buf, dat.len) != dat.len) {
1737c478bdstevel@tonic-gate		fmd_hdl_debug(hdl, "write failed: %s\n", strerror(errno));
1747c478bdstevel@tonic-gate		syslog_stats.msg_err.fmds_value.ui64++;
1757c478bdstevel@tonic-gate	}
1767c478bdstevel@tonic-gate}
1777c478bdstevel@tonic-gate
178f6e214cGavin Maltbystatic void
179f6e214cGavin Maltbyfree_notify_prefs(fmd_hdl_t *hdl, nvlist_t **prefs, uint_t nprefs)
180f6e214cGavin Maltby{
181f6e214cGavin Maltby	int i;
182f6e214cGavin Maltby
183f6e214cGavin Maltby	for (i = 0; i < nprefs; i++) {
184aab83bbJosef 'Jeff' Sipek		nvlist_free(prefs[i]);
185f6e214cGavin Maltby	}
186f6e214cGavin Maltby
187f6e214cGavin Maltby	fmd_hdl_free(hdl, prefs, sizeof (nvlist_t *) * nprefs);
188f6e214cGavin Maltby}
189f6e214cGavin Maltby
190f6e214cGavin Maltbystatic int
191f6e214cGavin Maltbyget_notify_prefs(fmd_hdl_t *hdl, nvlist_t *ev_nvl, nvlist_t ***pref_nvl,
192f6e214cGavin Maltby    uint_t *nprefs)
193f6e214cGavin Maltby{
194f6e214cGavin Maltby	nvlist_t *top_nvl, **np_nvlarr, *mech_nvl;
195f6e214cGavin Maltby	nvlist_t **tmparr;
196f6e214cGavin Maltby	int ret, i;
197f6e214cGavin Maltby	uint_t nelem, nslelem;
198f6e214cGavin Maltby
199f6e214cGavin Maltby	if ((ret = smf_notify_get_params(&top_nvl, ev_nvl)) != SCF_SUCCESS) {
200f6e214cGavin Maltby		ret = scf_error();
201f6e214cGavin Maltby		if (ret != SCF_ERROR_NOT_FOUND) {
202f6e214cGavin Maltby			fmd_hdl_debug(hdl, "Error looking up notification "
203f6e214cGavin Maltby			    "preferences (%s)", scf_strerror(ret));
204f6e214cGavin Maltby			return (ret);
205f6e214cGavin Maltby		}
206f6e214cGavin Maltby		return (ret);
207f6e214cGavin Maltby	}
208f6e214cGavin Maltby
209f6e214cGavin Maltby	if (nvlist_lookup_nvlist_array(top_nvl, SCF_NOTIFY_PARAMS, &np_nvlarr,
210f6e214cGavin Maltby	    &nelem) != 0) {
211f6e214cGavin Maltby		fmd_hdl_debug(hdl, "Malformed preference nvlist\n");
212f6e214cGavin Maltby		ret = SCF_ERROR_INVALID_ARGUMENT;
213f6e214cGavin Maltby		goto pref_done;
214f6e214cGavin Maltby	}
215f6e214cGavin Maltby
216f6e214cGavin Maltby	tmparr = fmd_hdl_alloc(hdl, nelem * sizeof (nvlist_t *), FMD_SLEEP);
217f6e214cGavin Maltby	nslelem = 0;
218f6e214cGavin Maltby
219f6e214cGavin Maltby	for (i = 0; i < nelem; i++) {
220f6e214cGavin Maltby		if (nvlist_lookup_nvlist(np_nvlarr[i], "syslog", &mech_nvl)
221f6e214cGavin Maltby		    == 0)
222f6e214cGavin Maltby			tmparr[nslelem++] = fmd_nvl_dup(hdl, mech_nvl,
223f6e214cGavin Maltby			    FMD_SLEEP);
224f6e214cGavin Maltby	}
225f6e214cGavin Maltby
226f6e214cGavin Maltby	if (nslelem != 0) {
227f6e214cGavin Maltby		size_t sz = nslelem * sizeof (nvlist_t *);
228f6e214cGavin Maltby
229f6e214cGavin Maltby		*pref_nvl = fmd_hdl_zalloc(hdl, sz, FMD_SLEEP);
230f6e214cGavin Maltby		*nprefs = nslelem;
231f6e214cGavin Maltby		bcopy(tmparr, *pref_nvl, sz);
232f6e214cGavin Maltby		ret = 0;
233f6e214cGavin Maltby	} else {
234f6e214cGavin Maltby		*pref_nvl = NULL;
235f6e214cGavin Maltby		*nprefs = 0;
236f6e214cGavin Maltby		ret = SCF_ERROR_NOT_FOUND;
237f6e214cGavin Maltby	}
238f6e214cGavin Maltby
239f6e214cGavin Maltby	fmd_hdl_free(hdl, tmparr, nelem * sizeof (nvlist_t *));
240f6e214cGavin Maltbypref_done:
241f6e214cGavin Maltby	nvlist_free(top_nvl);
242f6e214cGavin Maltby	return (ret);
243f6e214cGavin Maltby}
244f6e214cGavin Maltby
2457c478bdstevel@tonic-gate/*ARGSUSED*/
2467c478bdstevel@tonic-gatestatic void
2477c478bdstevel@tonic-gatesyslog_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl, const char *class)
2487c478bdstevel@tonic-gate{
2497c478bdstevel@tonic-gate	uint8_t version;
250f6e214cGavin Maltby	boolean_t domsg, *active;
251b695575Robert Johnston	char *msg;
252f6e214cGavin Maltby	nvlist_t **prefs;
253f6e214cGavin Maltby	uint_t nprefs, nelems;
254f6e214cGavin Maltby	int ret;
255cbf75e6Stephen Hanson
2567c478bdstevel@tonic-gate	if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 ||
2577c478bdstevel@tonic-gate	    version > FM_SUSPECT_VERSION) {
2587c478bdstevel@tonic-gate		fmd_hdl_debug(hdl, "invalid event version: %u\n", version);
2597c478bdstevel@tonic-gate		syslog_stats.bad_vers.fmds_value.ui64++;
2607c478bdstevel@tonic-gate		return; /* invalid event version */
2617c478bdstevel@tonic-gate	}
2627c478bdstevel@tonic-gate
2637c478bdstevel@tonic-gate	if (!syslog_msgall && nvlist_lookup_boolean_value(nvl,
2647c478bdstevel@tonic-gate	    FM_SUSPECT_MESSAGE, &domsg) == 0 && !domsg) {
2657c478bdstevel@tonic-gate		fmd_hdl_debug(hdl, "%s requested no message\n", class);
2667c478bdstevel@tonic-gate		syslog_stats.no_msg.fmds_value.ui64++;
2677c478bdstevel@tonic-gate		return; /* event is not to be messaged */
2687c478bdstevel@tonic-gate	}
2697c478bdstevel@tonic-gate
270f6e214cGavin Maltby	ret = get_notify_prefs(hdl, nvl, &prefs, &nprefs);
271f6e214cGavin Maltby	if (ret == SCF_ERROR_NOT_FOUND) {
272f6e214cGavin Maltby		/*
273f6e214cGavin Maltby		 * No syslog notification preferences specified for this type of
274f6e214cGavin Maltby		 * event, so we're done
275f6e214cGavin Maltby		 */
276f6e214cGavin Maltby		fmd_hdl_debug(hdl, "No syslog notification preferences "
277f6e214cGavin Maltby		    "configured for class %s\n", class);
278f6e214cGavin Maltby		syslog_stats.no_msg.fmds_value.ui64++;
279f6e214cGavin Maltby		return;
280f6e214cGavin Maltby	} else if (ret != 0 || nvlist_lookup_boolean_array(prefs[0], "active",
281f6e214cGavin Maltby	    &active, &nelems)) {
282f6e214cGavin Maltby		fmd_hdl_debug(hdl, "Failed to retrieve notification "
283f6e214cGavin Maltby		    "preferences for class %s\n", class);
284f6e214cGavin Maltby		if (ret == 0)
285f6e214cGavin Maltby			free_notify_prefs(hdl, prefs, nprefs);
286f6e214cGavin Maltby		return;
287f6e214cGavin Maltby	} else if (!active[0]) {
288f6e214cGavin Maltby		fmd_hdl_debug(hdl, "Syslog notifications disabled for "
289f6e214cGavin Maltby		    "class %s\n", class);
290f6e214cGavin Maltby		syslog_stats.no_msg.fmds_value.ui64++;
291f6e214cGavin Maltby		free_notify_prefs(hdl, prefs, nprefs);
292f6e214cGavin Maltby		return;
293f6e214cGavin Maltby	}
294f6e214cGavin Maltby	free_notify_prefs(hdl, prefs, nprefs);
295f6e214cGavin Maltby
296b695575Robert Johnston	if ((msg = fmd_msg_gettext_nv(syslog_msghdl, NULL, nvl)) == NULL) {
297b695575Robert Johnston		fmd_hdl_debug(hdl, "failed to format message");
2987c478bdstevel@tonic-gate		syslog_stats.bad_code.fmds_value.ui64++;
299b695575Robert Johnston		return; /* libfmd_msg error */
3007c478bdstevel@tonic-gate	}
3017c478bdstevel@tonic-gate
3023ca4d65Robert Johnston	syslog_ctl.pri &= LOG_FACMASK;
303f6e214cGavin Maltby	if (strcmp(class, FM_LIST_ISOLATED_CLASS) == 0 ||
304f6e214cGavin Maltby	    strcmp(class, FM_LIST_RESOLVED_CLASS) == 0 ||
305f6e214cGavin Maltby	    strcmp(class, FM_LIST_REPAIRED_CLASS) == 0 ||
306f6e214cGavin Maltby	    strcmp(class, FM_LIST_UPDATED_CLASS) == 0)
3073ca4d65Robert Johnston		syslog_ctl.pri |= LOG_NOTICE;
3083ca4d65Robert Johnston	else
3093ca4d65Robert Johnston		syslog_ctl.pri |= LOG_ERR;
3103ca4d65Robert Johnston
311b695575Robert Johnston	syslog_emit(hdl, msg);
312b695575Robert Johnston	free(msg);
3137c478bdstevel@tonic-gate}
3147c478bdstevel@tonic-gate
3157c478bdstevel@tonic-gatestatic const fmd_prop_t fmd_props[] = {
3167c478bdstevel@tonic-gate	{ "console", FMD_TYPE_BOOL, "true" },
3177c478bdstevel@tonic-gate	{ "facility", FMD_TYPE_STRING, "LOG_DAEMON" },
3187c478bdstevel@tonic-gate	{ "gmt", FMD_TYPE_BOOL, "false" },
3197c478bdstevel@tonic-gate	{ "syslogd", FMD_TYPE_BOOL, "true" },
320654b400Joshua M. Clulow	{ "url", FMD_TYPE_STRING, "http://illumos.org/msg/" },
3217c478bdstevel@tonic-gate	{ "message_all", FMD_TYPE_BOOL, "false" },
3227c478bdstevel@tonic-gate	{ NULL, 0, NULL }
3237c478bdstevel@tonic-gate};
3247c478bdstevel@tonic-gate
3257c478bdstevel@tonic-gatestatic const fmd_hdl_ops_t fmd_ops = {
3267c478bdstevel@tonic-gate	syslog_recv,	/* fmdo_recv */
3277c478bdstevel@tonic-gate	NULL,		/* fmdo_timeout */
3287c478bdstevel@tonic-gate	NULL,		/* fmdo_close */
3297c478bdstevel@tonic-gate	NULL,		/* fmdo_stats */
3307c478bdstevel@tonic-gate	NULL,		/* fmdo_gc */
3317c478bdstevel@tonic-gate};
3327c478bdstevel@tonic-gate
3337c478bdstevel@tonic-gatestatic const fmd_hdl_info_t fmd_info = {
334f6e214cGavin Maltby	"Syslog Messaging Agent", "1.1", &fmd_ops, fmd_props
3357c478bdstevel@tonic-gate};
3367c478bdstevel@tonic-gate
3377c478bdstevel@tonic-gatevoid
3387c478bdstevel@tonic-gate_fmd_init(fmd_hdl_t *hdl)
3397c478bdstevel@tonic-gate{
3407c478bdstevel@tonic-gate	const struct facility *fp;
341b695575Robert Johnston	char *facname, *tz, *rootdir, *urlbase;
3427c478bdstevel@tonic-gate
3437c478bdstevel@tonic-gate	if (fmd_hdl_register(hdl, FMD_API_VERSION, &fmd_info) != 0)
3447c478bdstevel@tonic-gate		return; /* invalid data in configuration file */
3457c478bdstevel@tonic-gate
3467c478bdstevel@tonic-gate	(void) fmd_stat_create(hdl, FMD_STAT_NOALLOC, sizeof (syslog_stats) /
3477c478bdstevel@tonic-gate	    sizeof (fmd_stat_t), (fmd_stat_t *)&syslog_stats);
3487c478bdstevel@tonic-gate
3497c478bdstevel@tonic-gate	if ((syslog_logfd = open("/dev/conslog", O_WRONLY | O_NOCTTY)) == -1)
3507c478bdstevel@tonic-gate		fmd_hdl_abort(hdl, "syslog-msgs failed to open /dev/conslog");
3517c478bdstevel@tonic-gate
3527c478bdstevel@tonic-gate	if ((syslog_msgfd = open("/dev/sysmsg", O_WRONLY | O_NOCTTY)) == -1)
3537c478bdstevel@tonic-gate		fmd_hdl_abort(hdl, "syslog-msgs failed to open /dev/sysmsg");
3547c478bdstevel@tonic-gate
3557c478bdstevel@tonic-gate	/*
3567c478bdstevel@tonic-gate	 * If the "gmt" property is set to true, force our EVENT-TIME to be
3577c478bdstevel@tonic-gate	 * reported in GMT time; otherwise we use localtime.  tzset() affects
3587c478bdstevel@tonic-gate	 * the results of subsequent calls to strftime(3C) above.
3597c478bdstevel@tonic-gate	 */
3607c478bdstevel@tonic-gate	if (fmd_prop_get_int32(hdl, "gmt") == FMD_B_TRUE &&
3617c478bdstevel@tonic-gate	    ((tz = getenv("TZ")) == NULL || strcmp(tz, "GMT") != 0)) {
3627c478bdstevel@tonic-gate		(void) putenv(fmd_hdl_strdup(hdl, "TZ=GMT", FMD_SLEEP));
3637c478bdstevel@tonic-gate		tzset(); /* reload env */
3647c478bdstevel@tonic-gate	}
3657c478bdstevel@tonic-gate
3667c478bdstevel@tonic-gate	/*
3677c478bdstevel@tonic-gate	 * Look up the value of the "facility" property and use it to determine
3687c478bdstevel@tonic-gate	 * what syslog LOG_* facility value we use to fill in our log_ctl_t.
3697c478bdstevel@tonic-gate	 * The details of our logging method are described above syslog_emit().
3707c478bdstevel@tonic-gate	 */
3717c478bdstevel@tonic-gate	facname = fmd_prop_get_string(hdl, "facility");
3727c478bdstevel@tonic-gate
3737c478bdstevel@tonic-gate	for (fp = syslog_facs; fp->fac_name != NULL; fp++) {
3747c478bdstevel@tonic-gate		if (strcmp(fp->fac_name, facname) == 0)
3757c478bdstevel@tonic-gate			break;
3767c478bdstevel@tonic-gate	}
3777c478bdstevel@tonic-gate
3787c478bdstevel@tonic-gate	if (fp->fac_name == NULL)
3797c478bdstevel@tonic-gate		fmd_hdl_abort(hdl, "invalid 'facility' setting: %s\n", facname);
3807c478bdstevel@tonic-gate
3817c478bdstevel@tonic-gate	fmd_prop_free_string(hdl, facname);
382627351ecy	syslog_ctl.pri = fp->fac_value;
3837c478bdstevel@tonic-gate	syslog_ctl.flags = SL_CONSOLE | SL_LOGONLY;
3847c478bdstevel@tonic-gate
3857c478bdstevel@tonic-gate	/*
3867c478bdstevel@tonic-gate	 * Cache any properties we use every time we receive an event and
3877c478bdstevel@tonic-gate	 * subscribe to list.suspect events regardless of the .conf file.
3887c478bdstevel@tonic-gate	 */
3897c478bdstevel@tonic-gate	syslog_file = fmd_prop_get_int32(hdl, "syslogd");
3907c478bdstevel@tonic-gate	syslog_cons = fmd_prop_get_int32(hdl, "console");
3917c478bdstevel@tonic-gate	syslog_msgall = fmd_prop_get_int32(hdl, "message_all");
3927c478bdstevel@tonic-gate
3937c478bdstevel@tonic-gate	rootdir = fmd_prop_get_string(hdl, "fmd.rootdir");
394b695575Robert Johnston	syslog_msghdl = fmd_msg_init(rootdir, FMD_MSG_VERSION);
395b695575Robert Johnston	fmd_prop_free_string(hdl, rootdir);
3967c478bdstevel@tonic-gate
397b695575Robert Johnston	if (syslog_msghdl == NULL)
398b695575Robert Johnston		fmd_hdl_abort(hdl, "failed to initialize libfmd_msg");
399b695575Robert Johnston
400b695575Robert Johnston	urlbase = fmd_prop_get_string(hdl, "url");
401b695575Robert Johnston	(void) fmd_msg_url_set(syslog_msghdl, urlbase);
402b695575Robert Johnston	fmd_prop_free_string(hdl, urlbase);
4037c478bdstevel@tonic-gate
404f6e214cGavin Maltby	/*
405f6e214cGavin Maltby	 * We subscribe to all FM events and then consult the notification
406f6e214cGavin Maltby	 * preferences in the serice configuration repo to determine whether
407f6e214cGavin Maltby	 * or not to emit a console message.
408f6e214cGavin Maltby	 */
4097c478bdstevel@tonic-gate	fmd_hdl_subscribe(hdl, FM_LIST_SUSPECT_CLASS);
410627351ecy	fmd_hdl_subscribe(hdl, FM_LIST_REPAIRED_CLASS);
41125c6ff4stephh	fmd_hdl_subscribe(hdl, FM_LIST_RESOLVED_CLASS);
412f6e214cGavin Maltby	fmd_hdl_subscribe(hdl, FM_LIST_ISOLATED_CLASS);
413f6e214cGavin Maltby	fmd_hdl_subscribe(hdl, FM_LIST_UPDATED_CLASS);
4147c478bdstevel@tonic-gate}
4157c478bdstevel@tonic-gate
416b695575Robert Johnston/*ARGSUSED*/
4177c478bdstevel@tonic-gatevoid
4187c478bdstevel@tonic-gate_fmd_fini(fmd_hdl_t *hdl)
4197c478bdstevel@tonic-gate{
420b695575Robert Johnston	fmd_msg_fini(syslog_msghdl);
4217c478bdstevel@tonic-gate	(void) close(syslog_logfd);
4227c478bdstevel@tonic-gate	(void) close(syslog_msgfd);
4237c478bdstevel@tonic-gate}
424