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