Home | History | Annotate | Download | only in syslog-msgs
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License (the "License").
      6  * You may not use this file except in compliance with the License.
      7  *
      8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  * or http://www.opensolaris.org/os/licensing.
     10  * See the License for the specific language governing permissions
     11  * and limitations under the License.
     12  *
     13  * When distributing Covered Code, include this CDDL HEADER in each
     14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  * If applicable, add the following below this CDDL HEADER, with the
     16  * fields enclosed by brackets "[]" replaced with your own identifying
     17  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  *
     19  * CDDL HEADER END
     20  */
     21 
     22 /*
     23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
     24  * Use is subject to license terms.
     25  */
     26 
     27 #include <sys/fm/protocol.h>
     28 #include <sys/strlog.h>
     29 #include <sys/log.h>
     30 
     31 #include <fm/fmd_api.h>
     32 #include <fm/fmd_msg.h>
     33 
     34 #include <stropts.h>
     35 #include <strings.h>
     36 #include <syslog.h>
     37 #include <alloca.h>
     38 #include <unistd.h>
     39 #include <stdlib.h>
     40 #include <errno.h>
     41 #include <fcntl.h>
     42 
     43 static struct stats {
     44 	fmd_stat_t bad_vers;
     45 	fmd_stat_t bad_code;
     46 	fmd_stat_t log_err;
     47 	fmd_stat_t msg_err;
     48 	fmd_stat_t no_msg;
     49 } syslog_stats = {
     50 	{ "bad_vers", FMD_TYPE_UINT64, "event version is missing or invalid" },
     51 	{ "bad_code", FMD_TYPE_UINT64, "event code has no dictionary name" },
     52 	{ "log_err", FMD_TYPE_UINT64, "failed to log message to log(7D)" },
     53 	{ "msg_err", FMD_TYPE_UINT64, "failed to log message to sysmsg(7D)" },
     54 	{ "no_msg", FMD_TYPE_UINT64, "message logging suppressed" }
     55 };
     56 
     57 static const struct facility {
     58 	const char *fac_name;
     59 	int fac_value;
     60 } syslog_facs[] = {
     61 	{ "LOG_DAEMON", LOG_DAEMON },
     62 	{ "LOG_LOCAL0", LOG_LOCAL0 },
     63 	{ "LOG_LOCAL1", LOG_LOCAL1 },
     64 	{ "LOG_LOCAL2", LOG_LOCAL2 },
     65 	{ "LOG_LOCAL3", LOG_LOCAL3 },
     66 	{ "LOG_LOCAL4", LOG_LOCAL4 },
     67 	{ "LOG_LOCAL5", LOG_LOCAL5 },
     68 	{ "LOG_LOCAL6", LOG_LOCAL6 },
     69 	{ "LOG_LOCAL7", LOG_LOCAL7 },
     70 	{ NULL, 0 }
     71 };
     72 
     73 static fmd_msg_hdl_t *syslog_msghdl; /* handle for libfmd_msg calls */
     74 static int syslog_msgall;	/* set to message all faults */
     75 static log_ctl_t syslog_ctl;	/* log(7D) meta-data for each msg */
     76 static int syslog_logfd = -1;	/* log(7D) file descriptor */
     77 static int syslog_msgfd = -1;	/* sysmsg(7D) file descriptor */
     78 static int syslog_file;		/* log to syslog_logfd */
     79 static int syslog_cons;		/* log to syslog_msgfd */
     80 static const char SYSLOG_POINTER[] = "syslog-msgs-pointer";
     81 
     82 /*
     83  * Ideally we would just use syslog(3C) for outputting our messages, but our
     84  * messaging standard defines a nice multi-line format and syslogd(1M) is very
     85  * inflexible and stupid when it comes to multi-line messages.  It pulls data
     86  * out of log(7D) and splits it up by \n, printing each line to the console
     87  * with its usual prefix of date and sender; it uses the same behavior for the
     88  * messages file as well.  Further, syslog(3C) provides no CE_CONT equivalent
     89  * for userland callers (which at least works around repeated file prefixing).
     90  * So with a multi-line message format, your file and console end up like this:
     91  *
     92  * Dec 02 18:08:40 hostname this is my nicely formatted
     93  * Dec 02 18:08:40 hostname message designed for 80 cols
     94  * ...
     95  *
     96  * To resolve these issues, we use our own syslog_emit() wrapper to emit
     97  * messages and some knowledge of how the Solaris log drivers work.  We first
     98  * construct an enlarged format string containing the appropriate msgid(1).
     99  * We then format the caller's message using the provided format and buffer.
    100  * We send this message to log(7D) using putmsg() with SL_CONSOLE | SL_LOGONLY
    101  * set in the log_ctl_t.  The log driver allows us to set SL_LOGONLY when we
    102  * construct messages ourself, indicating that syslogd should only emit the
    103  * message to /var/adm/messages and any remote hosts, and skip the console.
    104  * Then we emit the message a second time, without the special prefix, to the
    105  * sysmsg(7D) device, which handles console redirection and also permits us
    106  * to output any characters we like to the console, including \n and \r.
    107  */
    108 static void
    109 syslog_emit(fmd_hdl_t *hdl, const char *msg)
    110 {
    111 	struct strbuf ctl, dat;
    112 	uint32_t msgid;
    113 
    114 	char *buf;
    115 	size_t buflen;
    116 
    117 	const char *format = "fmd: [ID %u FACILITY_AND_PRIORITY] %s";
    118 	STRLOG_MAKE_MSGID(format, msgid);
    119 
    120 	buflen = snprintf(NULL, 0, format, msgid, msg);
    121 	buf = alloca(buflen + 1);
    122 	(void) snprintf(buf, buflen + 1, format, msgid, msg);
    123 
    124 	ctl.buf = (void *)&syslog_ctl;
    125 	ctl.len = sizeof (syslog_ctl);
    126 
    127 	dat.buf = buf;
    128 	dat.len = buflen + 1;
    129 
    130 	/*
    131 	 * The underlying log driver won't accept messages longer than
    132 	 * LOG_MAXPS bytes.  Therefore, messages which exceed this limit will
    133 	 * be truncated and appended with a pointer to the full message.
    134 	 */
    135 	if (dat.len > LOG_MAXPS) {
    136 		char *syslog_pointer, *p;
    137 		size_t plen;
    138 
    139 		if ((syslog_pointer = fmd_msg_gettext_id(syslog_msghdl, NULL,
    140 		    SYSLOG_POINTER)) == NULL) {
    141 			/*
    142 			 * This shouldn't happen, but if it does we'll just
    143 			 * truncate the message.
    144 			 */
    145 			buf[LOG_MAXPS - 1] = '\0';
    146 			dat.len = LOG_MAXPS;
    147 		} else {
    148 			plen = strlen(syslog_pointer) + 1;
    149 			buf[LOG_MAXPS - plen] = '\0';
    150 			/*
    151 			 * If possible, the pointer is appended after a newline
    152 			 */
    153 			if ((p = strrchr(buf, '\n')) == NULL)
    154 				p = &buf[LOG_MAXPS - plen];
    155 
    156 			(void) strcpy(p, syslog_pointer);
    157 			free(syslog_pointer);
    158 			dat.len = strlen(buf) + 1;
    159 		}
    160 	}
    161 	if (syslog_file && putmsg(syslog_logfd, &ctl, &dat, 0) != 0) {
    162 		fmd_hdl_debug(hdl, "putmsg failed: %s\n", strerror(errno));
    163 		syslog_stats.log_err.fmds_value.ui64++;
    164 	}
    165 
    166 	dat.buf = strchr(buf, ']');
    167 	dat.len -= (size_t)(dat.buf - buf);
    168 
    169 	dat.buf[0] = '\r'; /* overwrite ']' with carriage return */
    170 	dat.buf[1] = '\n'; /* overwrite ' ' with newline */
    171 
    172 	if (syslog_cons && write(syslog_msgfd, dat.buf, dat.len) != dat.len) {
    173 		fmd_hdl_debug(hdl, "write failed: %s\n", strerror(errno));
    174 		syslog_stats.msg_err.fmds_value.ui64++;
    175 	}
    176 }
    177 
    178 /*ARGSUSED*/
    179 static void
    180 syslog_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl, const char *class)
    181 {
    182 	uint8_t version;
    183 	boolean_t domsg;
    184 	char *msg;
    185 
    186 	if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 ||
    187 	    version > FM_SUSPECT_VERSION) {
    188 		fmd_hdl_debug(hdl, "invalid event version: %u\n", version);
    189 		syslog_stats.bad_vers.fmds_value.ui64++;
    190 		return; /* invalid event version */
    191 	}
    192 
    193 	if (!syslog_msgall && nvlist_lookup_boolean_value(nvl,
    194 	    FM_SUSPECT_MESSAGE, &domsg) == 0 && !domsg) {
    195 		fmd_hdl_debug(hdl, "%s requested no message\n", class);
    196 		syslog_stats.no_msg.fmds_value.ui64++;
    197 		return; /* event is not to be messaged */
    198 	}
    199 
    200 	if ((msg = fmd_msg_gettext_nv(syslog_msghdl, NULL, nvl)) == NULL) {
    201 		fmd_hdl_debug(hdl, "failed to format message");
    202 		syslog_stats.bad_code.fmds_value.ui64++;
    203 		return; /* libfmd_msg error */
    204 	}
    205 
    206 	syslog_ctl.pri &= LOG_FACMASK;
    207 	if (strcmp(class, FM_LIST_RESOLVED_CLASS) == 0 ||
    208 	    strcmp(class, FM_LIST_REPAIRED_CLASS) == 0)
    209 		syslog_ctl.pri |= LOG_NOTICE;
    210 	else
    211 		syslog_ctl.pri |= LOG_ERR;
    212 
    213 	syslog_emit(hdl, msg);
    214 	free(msg);
    215 }
    216 
    217 static const fmd_prop_t fmd_props[] = {
    218 	{ "console", FMD_TYPE_BOOL, "true" },
    219 	{ "facility", FMD_TYPE_STRING, "LOG_DAEMON" },
    220 	{ "gmt", FMD_TYPE_BOOL, "false" },
    221 	{ "syslogd", FMD_TYPE_BOOL, "true" },
    222 	{ "url", FMD_TYPE_STRING, "http://sun.com/msg/" },
    223 	{ "message_all", FMD_TYPE_BOOL, "false" },
    224 	{ NULL, 0, NULL }
    225 };
    226 
    227 static const fmd_hdl_ops_t fmd_ops = {
    228 	syslog_recv,	/* fmdo_recv */
    229 	NULL,		/* fmdo_timeout */
    230 	NULL,		/* fmdo_close */
    231 	NULL,		/* fmdo_stats */
    232 	NULL,		/* fmdo_gc */
    233 };
    234 
    235 static const fmd_hdl_info_t fmd_info = {
    236 	"Syslog Messaging Agent", "1.0", &fmd_ops, fmd_props
    237 };
    238 
    239 void
    240 _fmd_init(fmd_hdl_t *hdl)
    241 {
    242 	const struct facility *fp;
    243 	char *facname, *tz, *rootdir, *urlbase;
    244 
    245 	if (fmd_hdl_register(hdl, FMD_API_VERSION, &fmd_info) != 0)
    246 		return; /* invalid data in configuration file */
    247 
    248 	(void) fmd_stat_create(hdl, FMD_STAT_NOALLOC, sizeof (syslog_stats) /
    249 	    sizeof (fmd_stat_t), (fmd_stat_t *)&syslog_stats);
    250 
    251 	if ((syslog_logfd = open("/dev/conslog", O_WRONLY | O_NOCTTY)) == -1)
    252 		fmd_hdl_abort(hdl, "syslog-msgs failed to open /dev/conslog");
    253 
    254 	if ((syslog_msgfd = open("/dev/sysmsg", O_WRONLY | O_NOCTTY)) == -1)
    255 		fmd_hdl_abort(hdl, "syslog-msgs failed to open /dev/sysmsg");
    256 
    257 	/*
    258 	 * If the "gmt" property is set to true, force our EVENT-TIME to be
    259 	 * reported in GMT time; otherwise we use localtime.  tzset() affects
    260 	 * the results of subsequent calls to strftime(3C) above.
    261 	 */
    262 	if (fmd_prop_get_int32(hdl, "gmt") == FMD_B_TRUE &&
    263 	    ((tz = getenv("TZ")) == NULL || strcmp(tz, "GMT") != 0)) {
    264 		(void) putenv(fmd_hdl_strdup(hdl, "TZ=GMT", FMD_SLEEP));
    265 		tzset(); /* reload env */
    266 	}
    267 
    268 	/*
    269 	 * Look up the value of the "facility" property and use it to determine
    270 	 * what syslog LOG_* facility value we use to fill in our log_ctl_t.
    271 	 * The details of our logging method are described above syslog_emit().
    272 	 */
    273 	facname = fmd_prop_get_string(hdl, "facility");
    274 
    275 	for (fp = syslog_facs; fp->fac_name != NULL; fp++) {
    276 		if (strcmp(fp->fac_name, facname) == 0)
    277 			break;
    278 	}
    279 
    280 	if (fp->fac_name == NULL)
    281 		fmd_hdl_abort(hdl, "invalid 'facility' setting: %s\n", facname);
    282 
    283 	fmd_prop_free_string(hdl, facname);
    284 	syslog_ctl.pri = fp->fac_value;
    285 	syslog_ctl.flags = SL_CONSOLE | SL_LOGONLY;
    286 
    287 	/*
    288 	 * Cache any properties we use every time we receive an event and
    289 	 * subscribe to list.suspect events regardless of the .conf file.
    290 	 */
    291 	syslog_file = fmd_prop_get_int32(hdl, "syslogd");
    292 	syslog_cons = fmd_prop_get_int32(hdl, "console");
    293 	syslog_msgall = fmd_prop_get_int32(hdl, "message_all");
    294 
    295 	rootdir = fmd_prop_get_string(hdl, "fmd.rootdir");
    296 	syslog_msghdl = fmd_msg_init(rootdir, FMD_MSG_VERSION);
    297 	fmd_prop_free_string(hdl, rootdir);
    298 
    299 	if (syslog_msghdl == NULL)
    300 		fmd_hdl_abort(hdl, "failed to initialize libfmd_msg");
    301 
    302 	urlbase = fmd_prop_get_string(hdl, "url");
    303 	(void) fmd_msg_url_set(syslog_msghdl, urlbase);
    304 	fmd_prop_free_string(hdl, urlbase);
    305 
    306 	fmd_hdl_subscribe(hdl, FM_LIST_SUSPECT_CLASS);
    307 	fmd_hdl_subscribe(hdl, FM_LIST_REPAIRED_CLASS);
    308 	fmd_hdl_subscribe(hdl, FM_LIST_RESOLVED_CLASS);
    309 }
    310 
    311 /*ARGSUSED*/
    312 void
    313 _fmd_fini(fmd_hdl_t *hdl)
    314 {
    315 	fmd_msg_fini(syslog_msghdl);
    316 	(void) close(syslog_logfd);
    317 	(void) close(syslog_msgfd);
    318 }
    319