xref: /onnv-gate/usr/src/cmd/fm/modules/common/syslog-msgs/syslog.c (revision 12967:ab9ae749152f)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53723Saf  * Common Development and Distribution License (the "License").
63723Saf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
219501SRobert.Johnston@Sun.COM 
220Sstevel@tonic-gate /*
23*12967Sgavin.maltby@oracle.com  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/fm/protocol.h>
270Sstevel@tonic-gate #include <sys/strlog.h>
286981Smb91622 #include <sys/log.h>
29*12967Sgavin.maltby@oracle.com #include <libscf.h>
309501SRobert.Johnston@Sun.COM 
310Sstevel@tonic-gate #include <fm/fmd_api.h>
323723Saf #include <fm/fmd_msg.h>
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <stropts.h>
359501SRobert.Johnston@Sun.COM #include <strings.h>
360Sstevel@tonic-gate #include <syslog.h>
379501SRobert.Johnston@Sun.COM #include <alloca.h>
389501SRobert.Johnston@Sun.COM #include <unistd.h>
390Sstevel@tonic-gate #include <stdlib.h>
400Sstevel@tonic-gate #include <errno.h>
410Sstevel@tonic-gate #include <fcntl.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate static struct stats {
440Sstevel@tonic-gate 	fmd_stat_t bad_vers;
450Sstevel@tonic-gate 	fmd_stat_t bad_code;
460Sstevel@tonic-gate 	fmd_stat_t log_err;
470Sstevel@tonic-gate 	fmd_stat_t msg_err;
480Sstevel@tonic-gate 	fmd_stat_t no_msg;
490Sstevel@tonic-gate } syslog_stats = {
500Sstevel@tonic-gate 	{ "bad_vers", FMD_TYPE_UINT64, "event version is missing or invalid" },
510Sstevel@tonic-gate 	{ "bad_code", FMD_TYPE_UINT64, "event code has no dictionary name" },
520Sstevel@tonic-gate 	{ "log_err", FMD_TYPE_UINT64, "failed to log message to log(7D)" },
530Sstevel@tonic-gate 	{ "msg_err", FMD_TYPE_UINT64, "failed to log message to sysmsg(7D)" },
540Sstevel@tonic-gate 	{ "no_msg", FMD_TYPE_UINT64, "message logging suppressed" }
550Sstevel@tonic-gate };
560Sstevel@tonic-gate 
570Sstevel@tonic-gate static const struct facility {
580Sstevel@tonic-gate 	const char *fac_name;
590Sstevel@tonic-gate 	int fac_value;
600Sstevel@tonic-gate } syslog_facs[] = {
610Sstevel@tonic-gate 	{ "LOG_DAEMON", LOG_DAEMON },
620Sstevel@tonic-gate 	{ "LOG_LOCAL0", LOG_LOCAL0 },
630Sstevel@tonic-gate 	{ "LOG_LOCAL1", LOG_LOCAL1 },
640Sstevel@tonic-gate 	{ "LOG_LOCAL2", LOG_LOCAL2 },
650Sstevel@tonic-gate 	{ "LOG_LOCAL3", LOG_LOCAL3 },
660Sstevel@tonic-gate 	{ "LOG_LOCAL4", LOG_LOCAL4 },
670Sstevel@tonic-gate 	{ "LOG_LOCAL5", LOG_LOCAL5 },
680Sstevel@tonic-gate 	{ "LOG_LOCAL6", LOG_LOCAL6 },
690Sstevel@tonic-gate 	{ "LOG_LOCAL7", LOG_LOCAL7 },
700Sstevel@tonic-gate 	{ NULL, 0 }
710Sstevel@tonic-gate };
720Sstevel@tonic-gate 
739501SRobert.Johnston@Sun.COM static fmd_msg_hdl_t *syslog_msghdl; /* handle for libfmd_msg calls */
740Sstevel@tonic-gate static int syslog_msgall;	/* set to message all faults */
750Sstevel@tonic-gate static log_ctl_t syslog_ctl;	/* log(7D) meta-data for each msg */
760Sstevel@tonic-gate static int syslog_logfd = -1;	/* log(7D) file descriptor */
770Sstevel@tonic-gate static int syslog_msgfd = -1;	/* sysmsg(7D) file descriptor */
780Sstevel@tonic-gate static int syslog_file;		/* log to syslog_logfd */
790Sstevel@tonic-gate static int syslog_cons;		/* log to syslog_msgfd */
809501SRobert.Johnston@Sun.COM static const char SYSLOG_POINTER[] = "syslog-msgs-pointer";
810Sstevel@tonic-gate 
820Sstevel@tonic-gate /*
830Sstevel@tonic-gate  * Ideally we would just use syslog(3C) for outputting our messages, but our
840Sstevel@tonic-gate  * messaging standard defines a nice multi-line format and syslogd(1M) is very
850Sstevel@tonic-gate  * inflexible and stupid when it comes to multi-line messages.  It pulls data
860Sstevel@tonic-gate  * out of log(7D) and splits it up by \n, printing each line to the console
870Sstevel@tonic-gate  * with its usual prefix of date and sender; it uses the same behavior for the
880Sstevel@tonic-gate  * messages file as well.  Further, syslog(3C) provides no CE_CONT equivalent
890Sstevel@tonic-gate  * for userland callers (which at least works around repeated file prefixing).
900Sstevel@tonic-gate  * So with a multi-line message format, your file and console end up like this:
910Sstevel@tonic-gate  *
920Sstevel@tonic-gate  * Dec 02 18:08:40 hostname this is my nicely formatted
930Sstevel@tonic-gate  * Dec 02 18:08:40 hostname message designed for 80 cols
940Sstevel@tonic-gate  * ...
950Sstevel@tonic-gate  *
960Sstevel@tonic-gate  * To resolve these issues, we use our own syslog_emit() wrapper to emit
970Sstevel@tonic-gate  * messages and some knowledge of how the Solaris log drivers work.  We first
980Sstevel@tonic-gate  * construct an enlarged format string containing the appropriate msgid(1).
990Sstevel@tonic-gate  * We then format the caller's message using the provided format and buffer.
1000Sstevel@tonic-gate  * We send this message to log(7D) using putmsg() with SL_CONSOLE | SL_LOGONLY
1010Sstevel@tonic-gate  * set in the log_ctl_t.  The log driver allows us to set SL_LOGONLY when we
1020Sstevel@tonic-gate  * construct messages ourself, indicating that syslogd should only emit the
1030Sstevel@tonic-gate  * message to /var/adm/messages and any remote hosts, and skip the console.
1040Sstevel@tonic-gate  * Then we emit the message a second time, without the special prefix, to the
1050Sstevel@tonic-gate  * sysmsg(7D) device, which handles console redirection and also permits us
1060Sstevel@tonic-gate  * to output any characters we like to the console, including \n and \r.
1070Sstevel@tonic-gate  */
1080Sstevel@tonic-gate static void
syslog_emit(fmd_hdl_t * hdl,const char * msg)1099501SRobert.Johnston@Sun.COM syslog_emit(fmd_hdl_t *hdl, const char *msg)
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate 	struct strbuf ctl, dat;
1120Sstevel@tonic-gate 	uint32_t msgid;
1130Sstevel@tonic-gate 
1149501SRobert.Johnston@Sun.COM 	char *buf;
1159501SRobert.Johnston@Sun.COM 	size_t buflen;
1160Sstevel@tonic-gate 
1179501SRobert.Johnston@Sun.COM 	const char *format = "fmd: [ID %u FACILITY_AND_PRIORITY] %s";
1189501SRobert.Johnston@Sun.COM 	STRLOG_MAKE_MSGID(format, msgid);
1190Sstevel@tonic-gate 
1209501SRobert.Johnston@Sun.COM 	buflen = snprintf(NULL, 0, format, msgid, msg);
1219501SRobert.Johnston@Sun.COM 	buf = alloca(buflen + 1);
1229501SRobert.Johnston@Sun.COM 	(void) snprintf(buf, buflen + 1, format, msgid, msg);
1236341Scy152378 
1240Sstevel@tonic-gate 	ctl.buf = (void *)&syslog_ctl;
1250Sstevel@tonic-gate 	ctl.len = sizeof (syslog_ctl);
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	dat.buf = buf;
1289501SRobert.Johnston@Sun.COM 	dat.len = buflen + 1;
1296981Smb91622 
1306981Smb91622 	/*
1319501SRobert.Johnston@Sun.COM 	 * The underlying log driver won't accept messages longer than
1329501SRobert.Johnston@Sun.COM 	 * LOG_MAXPS bytes.  Therefore, messages which exceed this limit will
1339501SRobert.Johnston@Sun.COM 	 * be truncated and appended with a pointer to the full message.
1346981Smb91622 	 */
1359501SRobert.Johnston@Sun.COM 	if (dat.len > LOG_MAXPS) {
1369501SRobert.Johnston@Sun.COM 		char *syslog_pointer, *p;
1379501SRobert.Johnston@Sun.COM 		size_t plen;
1387139Scy152378 
1399501SRobert.Johnston@Sun.COM 		if ((syslog_pointer = fmd_msg_gettext_id(syslog_msghdl, NULL,
1409501SRobert.Johnston@Sun.COM 		    SYSLOG_POINTER)) == NULL) {
1419501SRobert.Johnston@Sun.COM 			/*
1429501SRobert.Johnston@Sun.COM 			 * This shouldn't happen, but if it does we'll just
1439501SRobert.Johnston@Sun.COM 			 * truncate the message.
1449501SRobert.Johnston@Sun.COM 			 */
1459501SRobert.Johnston@Sun.COM 			buf[LOG_MAXPS - 1] = '\0';
1469501SRobert.Johnston@Sun.COM 			dat.len = LOG_MAXPS;
1479501SRobert.Johnston@Sun.COM 		} else {
1489501SRobert.Johnston@Sun.COM 			plen = strlen(syslog_pointer) + 1;
1499501SRobert.Johnston@Sun.COM 			buf[LOG_MAXPS - plen] = '\0';
1509501SRobert.Johnston@Sun.COM 			/*
1519501SRobert.Johnston@Sun.COM 			 * If possible, the pointer is appended after a newline
1529501SRobert.Johnston@Sun.COM 			 */
1539501SRobert.Johnston@Sun.COM 			if ((p = strrchr(buf, '\n')) == NULL)
1549501SRobert.Johnston@Sun.COM 				p = &buf[LOG_MAXPS - plen];
1559501SRobert.Johnston@Sun.COM 
1569501SRobert.Johnston@Sun.COM 			(void) strcpy(p, syslog_pointer);
1579501SRobert.Johnston@Sun.COM 			free(syslog_pointer);
1589501SRobert.Johnston@Sun.COM 			dat.len = strlen(buf) + 1;
1599501SRobert.Johnston@Sun.COM 		}
1607139Scy152378 	}
1610Sstevel@tonic-gate 	if (syslog_file && putmsg(syslog_logfd, &ctl, &dat, 0) != 0) {
1620Sstevel@tonic-gate 		fmd_hdl_debug(hdl, "putmsg failed: %s\n", strerror(errno));
1630Sstevel@tonic-gate 		syslog_stats.log_err.fmds_value.ui64++;
1640Sstevel@tonic-gate 	}
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	dat.buf = strchr(buf, ']');
1679501SRobert.Johnston@Sun.COM 	dat.len -= (size_t)(dat.buf - buf);
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	dat.buf[0] = '\r'; /* overwrite ']' with carriage return */
1700Sstevel@tonic-gate 	dat.buf[1] = '\n'; /* overwrite ' ' with newline */
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	if (syslog_cons && write(syslog_msgfd, dat.buf, dat.len) != dat.len) {
1730Sstevel@tonic-gate 		fmd_hdl_debug(hdl, "write failed: %s\n", strerror(errno));
1740Sstevel@tonic-gate 		syslog_stats.msg_err.fmds_value.ui64++;
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate 
178*12967Sgavin.maltby@oracle.com static void
free_notify_prefs(fmd_hdl_t * hdl,nvlist_t ** prefs,uint_t nprefs)179*12967Sgavin.maltby@oracle.com free_notify_prefs(fmd_hdl_t *hdl, nvlist_t **prefs, uint_t nprefs)
180*12967Sgavin.maltby@oracle.com {
181*12967Sgavin.maltby@oracle.com 	int i;
182*12967Sgavin.maltby@oracle.com 
183*12967Sgavin.maltby@oracle.com 	for (i = 0; i < nprefs; i++) {
184*12967Sgavin.maltby@oracle.com 		if (prefs[i])
185*12967Sgavin.maltby@oracle.com 			nvlist_free(prefs[i]);
186*12967Sgavin.maltby@oracle.com 	}
187*12967Sgavin.maltby@oracle.com 
188*12967Sgavin.maltby@oracle.com 	fmd_hdl_free(hdl, prefs, sizeof (nvlist_t *) * nprefs);
189*12967Sgavin.maltby@oracle.com }
190*12967Sgavin.maltby@oracle.com 
191*12967Sgavin.maltby@oracle.com static int
get_notify_prefs(fmd_hdl_t * hdl,nvlist_t * ev_nvl,nvlist_t *** pref_nvl,uint_t * nprefs)192*12967Sgavin.maltby@oracle.com get_notify_prefs(fmd_hdl_t *hdl, nvlist_t *ev_nvl, nvlist_t ***pref_nvl,
193*12967Sgavin.maltby@oracle.com     uint_t *nprefs)
194*12967Sgavin.maltby@oracle.com {
195*12967Sgavin.maltby@oracle.com 	nvlist_t *top_nvl, **np_nvlarr, *mech_nvl;
196*12967Sgavin.maltby@oracle.com 	nvlist_t **tmparr;
197*12967Sgavin.maltby@oracle.com 	int ret, i;
198*12967Sgavin.maltby@oracle.com 	uint_t nelem, nslelem;
199*12967Sgavin.maltby@oracle.com 
200*12967Sgavin.maltby@oracle.com 	if ((ret = smf_notify_get_params(&top_nvl, ev_nvl)) != SCF_SUCCESS) {
201*12967Sgavin.maltby@oracle.com 		ret = scf_error();
202*12967Sgavin.maltby@oracle.com 		if (ret != SCF_ERROR_NOT_FOUND) {
203*12967Sgavin.maltby@oracle.com 			fmd_hdl_debug(hdl, "Error looking up notification "
204*12967Sgavin.maltby@oracle.com 			    "preferences (%s)", scf_strerror(ret));
205*12967Sgavin.maltby@oracle.com 			return (ret);
206*12967Sgavin.maltby@oracle.com 		}
207*12967Sgavin.maltby@oracle.com 		return (ret);
208*12967Sgavin.maltby@oracle.com 	}
209*12967Sgavin.maltby@oracle.com 
210*12967Sgavin.maltby@oracle.com 	if (nvlist_lookup_nvlist_array(top_nvl, SCF_NOTIFY_PARAMS, &np_nvlarr,
211*12967Sgavin.maltby@oracle.com 	    &nelem) != 0) {
212*12967Sgavin.maltby@oracle.com 		fmd_hdl_debug(hdl, "Malformed preference nvlist\n");
213*12967Sgavin.maltby@oracle.com 		ret = SCF_ERROR_INVALID_ARGUMENT;
214*12967Sgavin.maltby@oracle.com 		goto pref_done;
215*12967Sgavin.maltby@oracle.com 	}
216*12967Sgavin.maltby@oracle.com 
217*12967Sgavin.maltby@oracle.com 	tmparr = fmd_hdl_alloc(hdl, nelem * sizeof (nvlist_t *), FMD_SLEEP);
218*12967Sgavin.maltby@oracle.com 	nslelem = 0;
219*12967Sgavin.maltby@oracle.com 
220*12967Sgavin.maltby@oracle.com 	for (i = 0; i < nelem; i++) {
221*12967Sgavin.maltby@oracle.com 		if (nvlist_lookup_nvlist(np_nvlarr[i], "syslog", &mech_nvl)
222*12967Sgavin.maltby@oracle.com 		    == 0)
223*12967Sgavin.maltby@oracle.com 			tmparr[nslelem++] = fmd_nvl_dup(hdl, mech_nvl,
224*12967Sgavin.maltby@oracle.com 			    FMD_SLEEP);
225*12967Sgavin.maltby@oracle.com 	}
226*12967Sgavin.maltby@oracle.com 
227*12967Sgavin.maltby@oracle.com 	if (nslelem != 0) {
228*12967Sgavin.maltby@oracle.com 		size_t sz = nslelem * sizeof (nvlist_t *);
229*12967Sgavin.maltby@oracle.com 
230*12967Sgavin.maltby@oracle.com 		*pref_nvl = fmd_hdl_zalloc(hdl, sz, FMD_SLEEP);
231*12967Sgavin.maltby@oracle.com 		*nprefs = nslelem;
232*12967Sgavin.maltby@oracle.com 		bcopy(tmparr, *pref_nvl, sz);
233*12967Sgavin.maltby@oracle.com 		ret = 0;
234*12967Sgavin.maltby@oracle.com 	} else {
235*12967Sgavin.maltby@oracle.com 		*pref_nvl = NULL;
236*12967Sgavin.maltby@oracle.com 		*nprefs = 0;
237*12967Sgavin.maltby@oracle.com 		ret = SCF_ERROR_NOT_FOUND;
238*12967Sgavin.maltby@oracle.com 	}
239*12967Sgavin.maltby@oracle.com 
240*12967Sgavin.maltby@oracle.com 	fmd_hdl_free(hdl, tmparr, nelem * sizeof (nvlist_t *));
241*12967Sgavin.maltby@oracle.com pref_done:
242*12967Sgavin.maltby@oracle.com 	nvlist_free(top_nvl);
243*12967Sgavin.maltby@oracle.com 	return (ret);
244*12967Sgavin.maltby@oracle.com }
245*12967Sgavin.maltby@oracle.com 
2460Sstevel@tonic-gate /*ARGSUSED*/
2470Sstevel@tonic-gate static void
syslog_recv(fmd_hdl_t * hdl,fmd_event_t * ep,nvlist_t * nvl,const char * class)2480Sstevel@tonic-gate syslog_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl, const char *class)
2490Sstevel@tonic-gate {
2509501SRobert.Johnston@Sun.COM 	uint8_t version;
251*12967Sgavin.maltby@oracle.com 	boolean_t domsg, *active;
2529501SRobert.Johnston@Sun.COM 	char *msg;
253*12967Sgavin.maltby@oracle.com 	nvlist_t **prefs;
254*12967Sgavin.maltby@oracle.com 	uint_t nprefs, nelems;
255*12967Sgavin.maltby@oracle.com 	int ret;
2569120SStephen.Hanson@Sun.COM 
2570Sstevel@tonic-gate 	if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 ||
2580Sstevel@tonic-gate 	    version > FM_SUSPECT_VERSION) {
2590Sstevel@tonic-gate 		fmd_hdl_debug(hdl, "invalid event version: %u\n", version);
2600Sstevel@tonic-gate 		syslog_stats.bad_vers.fmds_value.ui64++;
2610Sstevel@tonic-gate 		return; /* invalid event version */
2620Sstevel@tonic-gate 	}
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	if (!syslog_msgall && nvlist_lookup_boolean_value(nvl,
2650Sstevel@tonic-gate 	    FM_SUSPECT_MESSAGE, &domsg) == 0 && !domsg) {
2660Sstevel@tonic-gate 		fmd_hdl_debug(hdl, "%s requested no message\n", class);
2670Sstevel@tonic-gate 		syslog_stats.no_msg.fmds_value.ui64++;
2680Sstevel@tonic-gate 		return; /* event is not to be messaged */
2690Sstevel@tonic-gate 	}
2700Sstevel@tonic-gate 
271*12967Sgavin.maltby@oracle.com 	ret = get_notify_prefs(hdl, nvl, &prefs, &nprefs);
272*12967Sgavin.maltby@oracle.com 	if (ret == SCF_ERROR_NOT_FOUND) {
273*12967Sgavin.maltby@oracle.com 		/*
274*12967Sgavin.maltby@oracle.com 		 * No syslog notification preferences specified for this type of
275*12967Sgavin.maltby@oracle.com 		 * event, so we're done
276*12967Sgavin.maltby@oracle.com 		 */
277*12967Sgavin.maltby@oracle.com 		fmd_hdl_debug(hdl, "No syslog notification preferences "
278*12967Sgavin.maltby@oracle.com 		    "configured for class %s\n", class);
279*12967Sgavin.maltby@oracle.com 		syslog_stats.no_msg.fmds_value.ui64++;
280*12967Sgavin.maltby@oracle.com 		return;
281*12967Sgavin.maltby@oracle.com 	} else if (ret != 0 || nvlist_lookup_boolean_array(prefs[0], "active",
282*12967Sgavin.maltby@oracle.com 	    &active, &nelems)) {
283*12967Sgavin.maltby@oracle.com 		fmd_hdl_debug(hdl, "Failed to retrieve notification "
284*12967Sgavin.maltby@oracle.com 		    "preferences for class %s\n", class);
285*12967Sgavin.maltby@oracle.com 		if (ret == 0)
286*12967Sgavin.maltby@oracle.com 			free_notify_prefs(hdl, prefs, nprefs);
287*12967Sgavin.maltby@oracle.com 		return;
288*12967Sgavin.maltby@oracle.com 	} else if (!active[0]) {
289*12967Sgavin.maltby@oracle.com 		fmd_hdl_debug(hdl, "Syslog notifications disabled for "
290*12967Sgavin.maltby@oracle.com 		    "class %s\n", class);
291*12967Sgavin.maltby@oracle.com 		syslog_stats.no_msg.fmds_value.ui64++;
292*12967Sgavin.maltby@oracle.com 		free_notify_prefs(hdl, prefs, nprefs);
293*12967Sgavin.maltby@oracle.com 		return;
294*12967Sgavin.maltby@oracle.com 	}
295*12967Sgavin.maltby@oracle.com 	free_notify_prefs(hdl, prefs, nprefs);
296*12967Sgavin.maltby@oracle.com 
2979501SRobert.Johnston@Sun.COM 	if ((msg = fmd_msg_gettext_nv(syslog_msghdl, NULL, nvl)) == NULL) {
2989501SRobert.Johnston@Sun.COM 		fmd_hdl_debug(hdl, "failed to format message");
2990Sstevel@tonic-gate 		syslog_stats.bad_code.fmds_value.ui64++;
3009501SRobert.Johnston@Sun.COM 		return; /* libfmd_msg error */
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate 
3039648SRobert.Johnston@Sun.COM 	syslog_ctl.pri &= LOG_FACMASK;
304*12967Sgavin.maltby@oracle.com 	if (strcmp(class, FM_LIST_ISOLATED_CLASS) == 0 ||
305*12967Sgavin.maltby@oracle.com 	    strcmp(class, FM_LIST_RESOLVED_CLASS) == 0 ||
306*12967Sgavin.maltby@oracle.com 	    strcmp(class, FM_LIST_REPAIRED_CLASS) == 0 ||
307*12967Sgavin.maltby@oracle.com 	    strcmp(class, FM_LIST_UPDATED_CLASS) == 0)
3089648SRobert.Johnston@Sun.COM 		syslog_ctl.pri |= LOG_NOTICE;
3099648SRobert.Johnston@Sun.COM 	else
3109648SRobert.Johnston@Sun.COM 		syslog_ctl.pri |= LOG_ERR;
3119648SRobert.Johnston@Sun.COM 
3129501SRobert.Johnston@Sun.COM 	syslog_emit(hdl, msg);
3139501SRobert.Johnston@Sun.COM 	free(msg);
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate static const fmd_prop_t fmd_props[] = {
3170Sstevel@tonic-gate 	{ "console", FMD_TYPE_BOOL, "true" },
3180Sstevel@tonic-gate 	{ "facility", FMD_TYPE_STRING, "LOG_DAEMON" },
3190Sstevel@tonic-gate 	{ "gmt", FMD_TYPE_BOOL, "false" },
3200Sstevel@tonic-gate 	{ "syslogd", FMD_TYPE_BOOL, "true" },
3210Sstevel@tonic-gate 	{ "url", FMD_TYPE_STRING, "http://sun.com/msg/" },
3220Sstevel@tonic-gate 	{ "message_all", FMD_TYPE_BOOL, "false" },
3230Sstevel@tonic-gate 	{ NULL, 0, NULL }
3240Sstevel@tonic-gate };
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate static const fmd_hdl_ops_t fmd_ops = {
3270Sstevel@tonic-gate 	syslog_recv,	/* fmdo_recv */
3280Sstevel@tonic-gate 	NULL,		/* fmdo_timeout */
3290Sstevel@tonic-gate 	NULL,		/* fmdo_close */
3300Sstevel@tonic-gate 	NULL,		/* fmdo_stats */
3310Sstevel@tonic-gate 	NULL,		/* fmdo_gc */
3320Sstevel@tonic-gate };
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate static const fmd_hdl_info_t fmd_info = {
335*12967Sgavin.maltby@oracle.com 	"Syslog Messaging Agent", "1.1", &fmd_ops, fmd_props
3360Sstevel@tonic-gate };
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate void
_fmd_init(fmd_hdl_t * hdl)3390Sstevel@tonic-gate _fmd_init(fmd_hdl_t *hdl)
3400Sstevel@tonic-gate {
3410Sstevel@tonic-gate 	const struct facility *fp;
3429501SRobert.Johnston@Sun.COM 	char *facname, *tz, *rootdir, *urlbase;
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	if (fmd_hdl_register(hdl, FMD_API_VERSION, &fmd_info) != 0)
3450Sstevel@tonic-gate 		return; /* invalid data in configuration file */
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	(void) fmd_stat_create(hdl, FMD_STAT_NOALLOC, sizeof (syslog_stats) /
3480Sstevel@tonic-gate 	    sizeof (fmd_stat_t), (fmd_stat_t *)&syslog_stats);
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	if ((syslog_logfd = open("/dev/conslog", O_WRONLY | O_NOCTTY)) == -1)
3510Sstevel@tonic-gate 		fmd_hdl_abort(hdl, "syslog-msgs failed to open /dev/conslog");
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	if ((syslog_msgfd = open("/dev/sysmsg", O_WRONLY | O_NOCTTY)) == -1)
3540Sstevel@tonic-gate 		fmd_hdl_abort(hdl, "syslog-msgs failed to open /dev/sysmsg");
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	/*
3570Sstevel@tonic-gate 	 * If the "gmt" property is set to true, force our EVENT-TIME to be
3580Sstevel@tonic-gate 	 * reported in GMT time; otherwise we use localtime.  tzset() affects
3590Sstevel@tonic-gate 	 * the results of subsequent calls to strftime(3C) above.
3600Sstevel@tonic-gate 	 */
3610Sstevel@tonic-gate 	if (fmd_prop_get_int32(hdl, "gmt") == FMD_B_TRUE &&
3620Sstevel@tonic-gate 	    ((tz = getenv("TZ")) == NULL || strcmp(tz, "GMT") != 0)) {
3630Sstevel@tonic-gate 		(void) putenv(fmd_hdl_strdup(hdl, "TZ=GMT", FMD_SLEEP));
3640Sstevel@tonic-gate 		tzset(); /* reload env */
3650Sstevel@tonic-gate 	}
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	/*
3680Sstevel@tonic-gate 	 * Look up the value of the "facility" property and use it to determine
3690Sstevel@tonic-gate 	 * what syslog LOG_* facility value we use to fill in our log_ctl_t.
3700Sstevel@tonic-gate 	 * The details of our logging method are described above syslog_emit().
3710Sstevel@tonic-gate 	 */
3720Sstevel@tonic-gate 	facname = fmd_prop_get_string(hdl, "facility");
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	for (fp = syslog_facs; fp->fac_name != NULL; fp++) {
3750Sstevel@tonic-gate 		if (strcmp(fp->fac_name, facname) == 0)
3760Sstevel@tonic-gate 			break;
3770Sstevel@tonic-gate 	}
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	if (fp->fac_name == NULL)
3800Sstevel@tonic-gate 		fmd_hdl_abort(hdl, "invalid 'facility' setting: %s\n", facname);
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	fmd_prop_free_string(hdl, facname);
3836276Scy152378 	syslog_ctl.pri = fp->fac_value;
3840Sstevel@tonic-gate 	syslog_ctl.flags = SL_CONSOLE | SL_LOGONLY;
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 	/*
3870Sstevel@tonic-gate 	 * Cache any properties we use every time we receive an event and
3880Sstevel@tonic-gate 	 * subscribe to list.suspect events regardless of the .conf file.
3890Sstevel@tonic-gate 	 */
3900Sstevel@tonic-gate 	syslog_file = fmd_prop_get_int32(hdl, "syslogd");
3910Sstevel@tonic-gate 	syslog_cons = fmd_prop_get_int32(hdl, "console");
3920Sstevel@tonic-gate 	syslog_msgall = fmd_prop_get_int32(hdl, "message_all");
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	rootdir = fmd_prop_get_string(hdl, "fmd.rootdir");
3959501SRobert.Johnston@Sun.COM 	syslog_msghdl = fmd_msg_init(rootdir, FMD_MSG_VERSION);
3969501SRobert.Johnston@Sun.COM 	fmd_prop_free_string(hdl, rootdir);
3970Sstevel@tonic-gate 
3989501SRobert.Johnston@Sun.COM 	if (syslog_msghdl == NULL)
3999501SRobert.Johnston@Sun.COM 		fmd_hdl_abort(hdl, "failed to initialize libfmd_msg");
4000Sstevel@tonic-gate 
4019501SRobert.Johnston@Sun.COM 	urlbase = fmd_prop_get_string(hdl, "url");
4029501SRobert.Johnston@Sun.COM 	(void) fmd_msg_url_set(syslog_msghdl, urlbase);
4039501SRobert.Johnston@Sun.COM 	fmd_prop_free_string(hdl, urlbase);
4049501SRobert.Johnston@Sun.COM 
405*12967Sgavin.maltby@oracle.com 	/*
406*12967Sgavin.maltby@oracle.com 	 * We subscribe to all FM events and then consult the notification
407*12967Sgavin.maltby@oracle.com 	 * preferences in the serice configuration repo to determine whether
408*12967Sgavin.maltby@oracle.com 	 * or not to emit a console message.
409*12967Sgavin.maltby@oracle.com 	 */
4100Sstevel@tonic-gate 	fmd_hdl_subscribe(hdl, FM_LIST_SUSPECT_CLASS);
4116276Scy152378 	fmd_hdl_subscribe(hdl, FM_LIST_REPAIRED_CLASS);
4127275Sstephh 	fmd_hdl_subscribe(hdl, FM_LIST_RESOLVED_CLASS);
413*12967Sgavin.maltby@oracle.com 	fmd_hdl_subscribe(hdl, FM_LIST_ISOLATED_CLASS);
414*12967Sgavin.maltby@oracle.com 	fmd_hdl_subscribe(hdl, FM_LIST_UPDATED_CLASS);
4150Sstevel@tonic-gate }
4160Sstevel@tonic-gate 
4179501SRobert.Johnston@Sun.COM /*ARGSUSED*/
4180Sstevel@tonic-gate void
_fmd_fini(fmd_hdl_t * hdl)4190Sstevel@tonic-gate _fmd_fini(fmd_hdl_t *hdl)
4200Sstevel@tonic-gate {
4219501SRobert.Johnston@Sun.COM 	fmd_msg_fini(syslog_msghdl);
4220Sstevel@tonic-gate 	(void) close(syslog_logfd);
4230Sstevel@tonic-gate 	(void) close(syslog_msgfd);
4240Sstevel@tonic-gate }
425