xref: /titanic_52/usr/src/cmd/fm/modules/common/syslog-msgs/syslog.c (revision 18c2aff776a775d34a4c9893a4c72e0434d68e36)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/fm/protocol.h>
30 #include <sys/strlog.h>
31 #include <fm/fmd_api.h>
32 
33 #include <stropts.h>
34 #include <syslog.h>
35 #include <locale.h>
36 #include <strings.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <limits.h>
40 #include <alloca.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <time.h>
44 
45 /*
46  * SYSLOG_DOMAIN and SYSLOG_TEMPLATE define the dgettext() parameters the agent
47  * can use to retrieve the localized format string for diagnosis messages.
48  * The format string retrieved from SYSLOG_DOMAIN is the default format
49  * string, but when processing each suspect list, dgettext() is also called
50  * for the domain that matches the diagcode dictname and if SYSLOG_TEMPLATE
51  * is defined, it overrides the default for that suspect list only.
52  *
53  * Similarly, SYSLOG_URL is also checked to see if syslog_url
54  * should be overridden for each suspect list.
55  *
56  * The net effect of all this is that for a given diagcode DICT-1234-56:
57  *
58  *	- If DICT.mo defines syslog-msgs-message-template, it is used
59  *	  as the format string for the diagnosis message.
60  *
61  *	- Otherwise, syslog-msgs-message-template from FMD.mo is used.
62  *
63  *	- If DICT.mo defines syslog-url, it is used when filling
64  *	  in the %s in the "description" message.
65  *
66  *	- Otherwise, if syslog-msgs.conf defines a "url" property, that
67  *	  value is used.
68  *
69  *	- Otherwise, the default "http://sun.com/msg/" is used (via the
70  *	  fmd_props[] table defined in this file).
71  */
72 static const char SYSLOG_DOMAIN[] = "FMD";
73 static const char SYSLOG_TEMPLATE[] = "syslog-msgs-message-template";
74 static const char SYSLOG_URL[] = "syslog-url";
75 
76 static struct stats {
77 	fmd_stat_t bad_vers;
78 	fmd_stat_t bad_fmri;
79 	fmd_stat_t bad_code;
80 	fmd_stat_t bad_time;
81 	fmd_stat_t log_err;
82 	fmd_stat_t msg_err;
83 	fmd_stat_t no_msg;
84 } syslog_stats = {
85 	{ "bad_vers", FMD_TYPE_UINT64, "event version is missing or invalid" },
86 	{ "bad_fmri", FMD_TYPE_UINT64, "event fmri is missing or invalid" },
87 	{ "bad_code", FMD_TYPE_UINT64, "event code has no dictionary name" },
88 	{ "bad_time", FMD_TYPE_UINT64, "event time is not properly encoded" },
89 	{ "log_err", FMD_TYPE_UINT64, "failed to log message to log(7D)" },
90 	{ "msg_err", FMD_TYPE_UINT64, "failed to log message to sysmsg(7D)" },
91 	{ "no_msg", FMD_TYPE_UINT64, "message logging suppressed" }
92 };
93 
94 static const struct facility {
95 	const char *fac_name;
96 	int fac_value;
97 } syslog_facs[] = {
98 	{ "LOG_DAEMON", LOG_DAEMON },
99 	{ "LOG_LOCAL0", LOG_LOCAL0 },
100 	{ "LOG_LOCAL1", LOG_LOCAL1 },
101 	{ "LOG_LOCAL2", LOG_LOCAL2 },
102 	{ "LOG_LOCAL3", LOG_LOCAL3 },
103 	{ "LOG_LOCAL4", LOG_LOCAL4 },
104 	{ "LOG_LOCAL5", LOG_LOCAL5 },
105 	{ "LOG_LOCAL6", LOG_LOCAL6 },
106 	{ "LOG_LOCAL7", LOG_LOCAL7 },
107 	{ NULL, 0 }
108 };
109 
110 static char *syslog_locdir;	/* l10n messages directory (if alternate) */
111 static char *syslog_url;	/* current value of "url" property */
112 static int syslog_msgall;	/* set to message all faults */
113 static log_ctl_t syslog_ctl;	/* log(7D) meta-data for each msg */
114 static int syslog_logfd = -1;	/* log(7D) file descriptor */
115 static int syslog_msgfd = -1;	/* sysmsg(7D) file descriptor */
116 static int syslog_file;		/* log to syslog_logfd */
117 static int syslog_cons;		/* log to syslog_msgfd */
118 
119 /*
120  * Ideally we would just use syslog(3C) for outputting our messages, but our
121  * messaging standard defines a nice multi-line format and syslogd(1M) is very
122  * inflexible and stupid when it comes to multi-line messages.  It pulls data
123  * out of log(7D) and splits it up by \n, printing each line to the console
124  * with its usual prefix of date and sender; it uses the same behavior for the
125  * messages file as well.  Further, syslog(3C) provides no CE_CONT equivalent
126  * for userland callers (which at least works around repeated file prefixing).
127  * So with a multi-line message format, your file and console end up like this:
128  *
129  * Dec 02 18:08:40 hostname this is my nicely formatted
130  * Dec 02 18:08:40 hostname message designed for 80 cols
131  * ...
132  *
133  * To resolve these issues, we use our own syslog_emit() wrapper to emit
134  * messages and some knowledge of how the Solaris log drivers work.  We first
135  * construct an enlarged format string containing the appropriate msgid(1).
136  * We then format the caller's message using the provided format and buffer.
137  * We send this message to log(7D) using putmsg() with SL_CONSOLE | SL_LOGONLY
138  * set in the log_ctl_t.  The log driver allows us to set SL_LOGONLY when we
139  * construct messages ourself, indicating that syslogd should only emit the
140  * message to /var/adm/messages and any remote hosts, and skip the console.
141  * Then we emit the message a second time, without the special prefix, to the
142  * sysmsg(7D) device, which handles console redirection and also permits us
143  * to output any characters we like to the console, including \n and \r.
144  */
145 /*PRINTFLIKE4*/
146 static void
147 syslog_emit(fmd_hdl_t *hdl, char *buf, size_t len, const char *msgformat, ...)
148 {
149 	struct strbuf ctl, dat;
150 	uint32_t msgid;
151 
152 	char *format;
153 	size_t formatlen;
154 	va_list ap;
155 
156 	formatlen = strlen(msgformat) + 64; /* +64 for prefix and \0 */
157 	format = alloca(formatlen);
158 
159 	STRLOG_MAKE_MSGID(msgformat, msgid);
160 	(void) snprintf(format, formatlen,
161 	    "fmd: [ID %u FACILITY_AND_PRIORITY] %s", msgid, msgformat);
162 
163 	va_start(ap, msgformat);
164 	(void) vsnprintf(buf, len, format, ap);
165 	va_end(ap);
166 
167 	ctl.buf = (void *)&syslog_ctl;
168 	ctl.len = sizeof (syslog_ctl);
169 
170 	dat.buf = buf;
171 	dat.len = strlen(buf) + 1;
172 
173 	if (syslog_file && putmsg(syslog_logfd, &ctl, &dat, 0) != 0) {
174 		fmd_hdl_debug(hdl, "putmsg failed: %s\n", strerror(errno));
175 		syslog_stats.log_err.fmds_value.ui64++;
176 	}
177 
178 	dat.buf = strchr(buf, ']');
179 	dat.len -= (size_t)(dat.buf - buf);
180 
181 	dat.buf[0] = '\r'; /* overwrite ']' with carriage return */
182 	dat.buf[1] = '\n'; /* overwrite ' ' with newline */
183 
184 	if (syslog_cons && write(syslog_msgfd, dat.buf, dat.len) != dat.len) {
185 		fmd_hdl_debug(hdl, "write failed: %s\n", strerror(errno));
186 		syslog_stats.msg_err.fmds_value.ui64++;
187 	}
188 }
189 
190 /*ARGSUSED*/
191 static void
192 syslog_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl, const char *class)
193 {
194 	char *uuid, *code, *dict, *url, *urlcode, *template, *p;
195 	char *src_name, *src_vers, *platform, *chassis, *server;
196 	char *typ, *sev, *fmt, *trfmt, *rsp, *imp, *act;
197 	char msg[1024], desc[1024], date[64];
198 	boolean_t domsg;
199 
200 	nvlist_t *fmri, *auth;
201 	uint8_t version;
202 	struct tm tm, *tmp;
203 	int64_t *tv;
204 	time_t sec;
205 	uint_t tn = 0;
206 	char *olang = NULL;
207 	size_t len;
208 
209 	if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 ||
210 	    version > FM_SUSPECT_VERSION) {
211 		fmd_hdl_debug(hdl, "invalid event version: %u\n", version);
212 		syslog_stats.bad_vers.fmds_value.ui64++;
213 		return; /* invalid event version */
214 	}
215 
216 	if (!syslog_msgall && nvlist_lookup_boolean_value(nvl,
217 	    FM_SUSPECT_MESSAGE, &domsg) == 0 && !domsg) {
218 		fmd_hdl_debug(hdl, "%s requested no message\n", class);
219 		syslog_stats.no_msg.fmds_value.ui64++;
220 		return; /* event is not to be messaged */
221 	}
222 
223 	/*
224 	 * Extract the DE element, which is an FMRI for the diagnosis engine
225 	 * that made this event, and validate its meta-data before continuing.
226 	 */
227 	if (nvlist_lookup_nvlist(nvl, FM_SUSPECT_DE, &fmri) != 0 ||
228 	    nvlist_lookup_string(fmri, FM_FMRI_SCHEME, &p) != 0 ||
229 	    strcmp(p, FM_FMRI_SCHEME_FMD) != 0 ||
230 	    nvlist_lookup_uint8(fmri, FM_VERSION, &version) != 0 ||
231 	    version > FM_FMD_SCHEME_VERSION ||
232 	    nvlist_lookup_nvlist(fmri, FM_FMRI_AUTHORITY, &auth) != 0 ||
233 	    nvlist_lookup_uint8(auth, FM_VERSION, &version) != 0 ||
234 	    version > FM_FMRI_AUTH_VERSION) {
235 		syslog_stats.bad_fmri.fmds_value.ui64++;
236 		return; /* invalid de fmri */
237 	}
238 
239 	/*
240 	 * Extract the relevant identifying elements of the FMRI and authority.
241 	 * Note: for now, we ignore FM_FMRI_AUTH_DOMAIN (only for SPs).
242 	 */
243 	(void) nvlist_lookup_string(fmri, FM_FMRI_FMD_NAME, &src_name);
244 	(void) nvlist_lookup_string(fmri, FM_FMRI_FMD_VERSION, &src_vers);
245 	(void) nvlist_lookup_string(auth, FM_FMRI_AUTH_PRODUCT, &platform);
246 	(void) nvlist_lookup_string(auth, FM_FMRI_AUTH_SERVER, &server);
247 
248 	if (nvlist_lookup_string(auth, FM_FMRI_AUTH_CHASSIS, &chassis) != 0)
249 		chassis = "-"; /* chassis serial number may not be present */
250 
251 	/*
252 	 * Extract the uuid and diagcode dictionary from the event code.  The
253 	 * dictionary name is the text preceding the first "-" in the code.
254 	 */
255 	(void) nvlist_lookup_string(nvl, FM_SUSPECT_UUID, &uuid);
256 	(void) nvlist_lookup_string(nvl, FM_SUSPECT_DIAG_CODE, &code);
257 
258 	if ((p = strchr(code, '-')) == NULL || p == code) {
259 		fmd_hdl_debug(hdl, "invalid diagnosis code: %s\n", code);
260 		syslog_stats.bad_code.fmds_value.ui64++;
261 		return; /* invalid diagnosis code */
262 	}
263 
264 	dict = alloca((size_t)(p - code) + 1);
265 	(void) strncpy(dict, code, (size_t)(p - code));
266 	dict[(size_t)(p - code)] = '\0';
267 
268 	/*
269 	 * Alloca a hunk of memory and use it to create the msgid strings
270 	 * <code>.type, <code>.severity, <code>.description, and so forth.
271 	 * These form the msgids we will use to look up the localized text.
272 	 * Since we've allocated things to be of the right size, we know
273 	 * than snprintf() can't overflow: INT_MAX is used shut lint up and
274 	 * avoid code to needlessly recompute the remaining buffer space.
275 	 */
276 	typ = alloca(6 * (strlen(code) + 16));
277 	sev = typ + snprintf(typ, INT_MAX, "%s.type", code) + 1;
278 	fmt = sev + snprintf(sev, INT_MAX, "%s.severity", code) + 1;
279 	rsp = fmt + snprintf(fmt, INT_MAX, "%s.description", code) + 1;
280 	imp = rsp + snprintf(rsp, INT_MAX, "%s.response", code) + 1;
281 	act = imp + snprintf(imp, INT_MAX, "%s.impact", code) + 1;
282 	(void) snprintf(act, INT_MAX, "%s.action", code);
283 
284 	if (syslog_locdir != NULL)
285 		(void) bindtextdomain(dict, syslog_locdir);
286 
287 	if ((trfmt = dgettext(dict, fmt)) == fmt) {
288 		/*
289 		 * We didn't find a translation in the dictionary for the
290 		 * current language.  The string we passed to gettext is merely
291 		 * an index - it isn't sufficient, on its own, to be used as the
292 		 * message.  Fall back to C and try again.
293 		 */
294 		olang = setlocale(LC_MESSAGES, NULL);
295 		fmd_hdl_debug(hdl, "dgettext(%s, %s) in %s failed; trying C\n",
296 		    dict, fmt, olang);
297 		(void) setlocale(LC_MESSAGES, "C");
298 		trfmt = dgettext(dict, fmt);
299 	}
300 
301 	if ((url = dgettext(dict, SYSLOG_URL)) == SYSLOG_URL)
302 		url = syslog_url;
303 
304 	/*
305 	 * If the URL ends with a slash, that indicates the code should be
306 	 * appended to it.  After formatting the URL, reformat the DESC
307 	 * text using the URL as an snprintf argument.
308 	 */
309 	len = strlen(url);
310 	if (url[len - 1] == '/') {
311 		urlcode = alloca(len + strlen(code) + 1);
312 		(void) snprintf(urlcode, INT_MAX, "%s%s", url, code);
313 	} else {
314 		urlcode = url;
315 	}
316 	/* LINTED - variable format specifier to snprintf() */
317 	(void) snprintf(desc, sizeof (desc), trfmt, urlcode);
318 
319 	/*
320 	 * Extract the diagnosis time and format it using the locale's default.
321 	 * strftime() will use GMT or local time based on our "gmt" setting.
322 	 */
323 	if (nvlist_lookup_int64_array(nvl, FM_SUSPECT_DIAG_TIME,
324 	    &tv, &tn) == 0 && tn == 2 && (sec = (time_t)tv[0]) != (time_t)-1 &&
325 	    (tmp = localtime_r(&sec, &tm)) != NULL)
326 		(void) strftime(date, sizeof (date), "%C", tmp);
327 	else {
328 		syslog_stats.bad_time.fmds_value.ui64++;
329 		(void) strcpy(date, "-");
330 	}
331 
332 	/*
333 	 * Create and log the final string by filling in the template with the
334 	 * strings we've created and the strings from the message dictionary.
335 	 * If a template is provided for this dictionary, use it, otherwise
336 	 * fall back to the default template.
337 	 */
338 	if ((template = dgettext(dict, SYSLOG_TEMPLATE)) == SYSLOG_TEMPLATE)
339 		template = dgettext(SYSLOG_DOMAIN, SYSLOG_TEMPLATE);
340 
341 	syslog_emit(hdl, msg, sizeof (msg),
342 	    template, code, dgettext(dict, typ),
343 	    dgettext(dict, sev), date, platform, chassis, server, src_name,
344 	    src_vers, uuid, desc, dgettext(dict, rsp), dgettext(dict, imp),
345 	    dgettext(dict, act));
346 
347 	/*
348 	 * Switch back to our original language if we had to fall back to C.
349 	 */
350 	if (olang != NULL)
351 		(void) setlocale(LC_MESSAGES, olang);
352 }
353 
354 static const fmd_prop_t fmd_props[] = {
355 	{ "console", FMD_TYPE_BOOL, "true" },
356 	{ "facility", FMD_TYPE_STRING, "LOG_DAEMON" },
357 	{ "gmt", FMD_TYPE_BOOL, "false" },
358 	{ "syslogd", FMD_TYPE_BOOL, "true" },
359 	{ "url", FMD_TYPE_STRING, "http://sun.com/msg/" },
360 	{ "message_all", FMD_TYPE_BOOL, "false" },
361 	{ NULL, 0, NULL }
362 };
363 
364 static const fmd_hdl_ops_t fmd_ops = {
365 	syslog_recv,	/* fmdo_recv */
366 	NULL,		/* fmdo_timeout */
367 	NULL,		/* fmdo_close */
368 	NULL,		/* fmdo_stats */
369 	NULL,		/* fmdo_gc */
370 };
371 
372 static const fmd_hdl_info_t fmd_info = {
373 	"Syslog Messaging Agent", "1.0", &fmd_ops, fmd_props
374 };
375 
376 void
377 _fmd_init(fmd_hdl_t *hdl)
378 {
379 	const struct facility *fp;
380 	char *facname, *tz, *rootdir, *locdir;
381 
382 	if (fmd_hdl_register(hdl, FMD_API_VERSION, &fmd_info) != 0)
383 		return; /* invalid data in configuration file */
384 
385 	(void) fmd_stat_create(hdl, FMD_STAT_NOALLOC, sizeof (syslog_stats) /
386 	    sizeof (fmd_stat_t), (fmd_stat_t *)&syslog_stats);
387 
388 	if ((syslog_logfd = open("/dev/conslog", O_WRONLY | O_NOCTTY)) == -1)
389 		fmd_hdl_abort(hdl, "syslog-msgs failed to open /dev/conslog");
390 
391 	if ((syslog_msgfd = open("/dev/sysmsg", O_WRONLY | O_NOCTTY)) == -1)
392 		fmd_hdl_abort(hdl, "syslog-msgs failed to open /dev/sysmsg");
393 
394 	/*
395 	 * All FMA event dictionaries use msgfmt(1) message objects to produce
396 	 * messages, even for the C locale.  We therefore want to use dgettext
397 	 * for all message lookups, but its defined behavior in the C locale is
398 	 * to return the input string.  Since our input strings are event codes
399 	 * and not format strings, this doesn't help us.  We resolve this nit
400 	 * by setting NLSPATH to a non-existent file: the presence of NLSPATH
401 	 * is defined to force dgettext(3C) to do a full lookup even for C.
402 	 */
403 	if (getenv("NLSPATH") == NULL && putenv(fmd_hdl_strdup(hdl,
404 	    "NLSPATH=/usr/lib/fm/fmd/fmd.cat", FMD_SLEEP)) != 0)
405 		fmd_hdl_abort(hdl, "syslog-msgs failed to set NLSPATH");
406 
407 	(void) setlocale(LC_MESSAGES, "");
408 	fmd_hdl_debug(hdl, "locale=%s\n", setlocale(LC_MESSAGES, NULL));
409 
410 	/*
411 	 * If the "gmt" property is set to true, force our EVENT-TIME to be
412 	 * reported in GMT time; otherwise we use localtime.  tzset() affects
413 	 * the results of subsequent calls to strftime(3C) above.
414 	 */
415 	if (fmd_prop_get_int32(hdl, "gmt") == FMD_B_TRUE &&
416 	    ((tz = getenv("TZ")) == NULL || strcmp(tz, "GMT") != 0)) {
417 		(void) putenv(fmd_hdl_strdup(hdl, "TZ=GMT", FMD_SLEEP));
418 		tzset(); /* reload env */
419 	}
420 
421 	/*
422 	 * Look up the value of the "facility" property and use it to determine
423 	 * what syslog LOG_* facility value we use to fill in our log_ctl_t.
424 	 * The details of our logging method are described above syslog_emit().
425 	 */
426 	facname = fmd_prop_get_string(hdl, "facility");
427 
428 	for (fp = syslog_facs; fp->fac_name != NULL; fp++) {
429 		if (strcmp(fp->fac_name, facname) == 0)
430 			break;
431 	}
432 
433 	if (fp->fac_name == NULL)
434 		fmd_hdl_abort(hdl, "invalid 'facility' setting: %s\n", facname);
435 
436 	fmd_prop_free_string(hdl, facname);
437 	syslog_ctl.pri = fp->fac_value | LOG_ERR;
438 	syslog_ctl.flags = SL_CONSOLE | SL_LOGONLY;
439 
440 	/*
441 	 * Cache any properties we use every time we receive an event and
442 	 * subscribe to list.suspect events regardless of the .conf file.
443 	 */
444 	syslog_file = fmd_prop_get_int32(hdl, "syslogd");
445 	syslog_cons = fmd_prop_get_int32(hdl, "console");
446 	syslog_url = fmd_prop_get_string(hdl, "url");
447 	syslog_msgall = fmd_prop_get_int32(hdl, "message_all");
448 
449 	/*
450 	 * If fmd's rootdir property is set to a non-default root, then we are
451 	 * going to need to rebind the text domains we use for dgettext() as
452 	 * we go.  Look up the default l10n messages directory and make
453 	 * syslog_locdir be this path with fmd.rootdir prepended to it.
454 	 */
455 	rootdir = fmd_prop_get_string(hdl, "fmd.rootdir");
456 
457 	if (*rootdir != '\0' && strcmp(rootdir, "/") != 0 &&
458 	    (locdir = bindtextdomain(SYSLOG_DOMAIN, NULL)) != NULL) {
459 		size_t len = strlen(rootdir) + strlen(locdir) + 1;
460 		syslog_locdir = fmd_hdl_alloc(hdl, len, FMD_SLEEP);
461 		(void) snprintf(syslog_locdir, len, "%s%s", rootdir, locdir);
462 		(void) bindtextdomain(SYSLOG_DOMAIN, syslog_locdir);
463 		fmd_hdl_debug(hdl, "binding textdomain to %s\n", syslog_locdir);
464 	}
465 
466 	fmd_prop_free_string(hdl, rootdir);
467 	fmd_hdl_subscribe(hdl, FM_LIST_SUSPECT_CLASS);
468 }
469 
470 void
471 _fmd_fini(fmd_hdl_t *hdl)
472 {
473 	fmd_hdl_strfree(hdl, syslog_locdir);
474 	fmd_prop_free_string(hdl, syslog_url);
475 
476 	(void) close(syslog_logfd);
477 	(void) close(syslog_msgfd);
478 }
479