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 */ 217c478bd9Sstevel@tonic-gate /* 22*cbf75e67SStephen Hanson * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #include <sys/fm/protocol.h> 277c478bd9Sstevel@tonic-gate #include <sys/strlog.h> 28072c378dSmb #include <sys/log.h> 297c478bd9Sstevel@tonic-gate #include <fm/fmd_api.h> 30e1c679faSaf #include <fm/fmd_msg.h> 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate #include <stropts.h> 337c478bd9Sstevel@tonic-gate #include <syslog.h> 347c478bd9Sstevel@tonic-gate #include <locale.h> 357c478bd9Sstevel@tonic-gate #include <strings.h> 367c478bd9Sstevel@tonic-gate #include <stdlib.h> 377c478bd9Sstevel@tonic-gate #include <unistd.h> 387c478bd9Sstevel@tonic-gate #include <limits.h> 397c478bd9Sstevel@tonic-gate #include <alloca.h> 407c478bd9Sstevel@tonic-gate #include <errno.h> 417c478bd9Sstevel@tonic-gate #include <fcntl.h> 427c478bd9Sstevel@tonic-gate #include <time.h> 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate /* 457c478bd9Sstevel@tonic-gate * SYSLOG_DOMAIN and SYSLOG_TEMPLATE define the dgettext() parameters the agent 467c478bd9Sstevel@tonic-gate * can use to retrieve the localized format string for diagnosis messages. 477c478bd9Sstevel@tonic-gate * The format string retrieved from SYSLOG_DOMAIN is the default format 487c478bd9Sstevel@tonic-gate * string, but when processing each suspect list, dgettext() is also called 497c478bd9Sstevel@tonic-gate * for the domain that matches the diagcode dictname and if SYSLOG_TEMPLATE 507c478bd9Sstevel@tonic-gate * is defined, it overrides the default for that suspect list only. 517c478bd9Sstevel@tonic-gate * 527c478bd9Sstevel@tonic-gate * Similarly, SYSLOG_URL is also checked to see if syslog_url 537c478bd9Sstevel@tonic-gate * should be overridden for each suspect list. 547c478bd9Sstevel@tonic-gate * 557c478bd9Sstevel@tonic-gate * The net effect of all this is that for a given diagcode DICT-1234-56: 567c478bd9Sstevel@tonic-gate * 577c478bd9Sstevel@tonic-gate * - If DICT.mo defines syslog-msgs-message-template, it is used 587c478bd9Sstevel@tonic-gate * as the format string for the diagnosis message. 597c478bd9Sstevel@tonic-gate * 607c478bd9Sstevel@tonic-gate * - Otherwise, syslog-msgs-message-template from FMD.mo is used. 617c478bd9Sstevel@tonic-gate * 627c478bd9Sstevel@tonic-gate * - If DICT.mo defines syslog-url, it is used when filling 637c478bd9Sstevel@tonic-gate * in the %s in the "description" message. 647c478bd9Sstevel@tonic-gate * 657c478bd9Sstevel@tonic-gate * - Otherwise, if syslog-msgs.conf defines a "url" property, that 667c478bd9Sstevel@tonic-gate * value is used. 677c478bd9Sstevel@tonic-gate * 687c478bd9Sstevel@tonic-gate * - Otherwise, the default "http://sun.com/msg/" is used (via the 697c478bd9Sstevel@tonic-gate * fmd_props[] table defined in this file). 707c478bd9Sstevel@tonic-gate */ 717c478bd9Sstevel@tonic-gate static const char SYSLOG_DOMAIN[] = "FMD"; 727c478bd9Sstevel@tonic-gate static const char SYSLOG_TEMPLATE[] = "syslog-msgs-message-template"; 737c478bd9Sstevel@tonic-gate static const char SYSLOG_URL[] = "syslog-url"; 7419e1255fScy static const char SYSLOG_POINTER[] = "syslog-msgs-pointer"; 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate static struct stats { 777c478bd9Sstevel@tonic-gate fmd_stat_t bad_vers; 787c478bd9Sstevel@tonic-gate fmd_stat_t bad_fmri; 797c478bd9Sstevel@tonic-gate fmd_stat_t bad_code; 807c478bd9Sstevel@tonic-gate fmd_stat_t bad_time; 817c478bd9Sstevel@tonic-gate fmd_stat_t log_err; 827c478bd9Sstevel@tonic-gate fmd_stat_t msg_err; 837c478bd9Sstevel@tonic-gate fmd_stat_t no_msg; 847c478bd9Sstevel@tonic-gate } syslog_stats = { 857c478bd9Sstevel@tonic-gate { "bad_vers", FMD_TYPE_UINT64, "event version is missing or invalid" }, 867c478bd9Sstevel@tonic-gate { "bad_fmri", FMD_TYPE_UINT64, "event fmri is missing or invalid" }, 877c478bd9Sstevel@tonic-gate { "bad_code", FMD_TYPE_UINT64, "event code has no dictionary name" }, 887c478bd9Sstevel@tonic-gate { "bad_time", FMD_TYPE_UINT64, "event time is not properly encoded" }, 897c478bd9Sstevel@tonic-gate { "log_err", FMD_TYPE_UINT64, "failed to log message to log(7D)" }, 907c478bd9Sstevel@tonic-gate { "msg_err", FMD_TYPE_UINT64, "failed to log message to sysmsg(7D)" }, 917c478bd9Sstevel@tonic-gate { "no_msg", FMD_TYPE_UINT64, "message logging suppressed" } 927c478bd9Sstevel@tonic-gate }; 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate static const struct facility { 957c478bd9Sstevel@tonic-gate const char *fac_name; 967c478bd9Sstevel@tonic-gate int fac_value; 977c478bd9Sstevel@tonic-gate } syslog_facs[] = { 987c478bd9Sstevel@tonic-gate { "LOG_DAEMON", LOG_DAEMON }, 997c478bd9Sstevel@tonic-gate { "LOG_LOCAL0", LOG_LOCAL0 }, 1007c478bd9Sstevel@tonic-gate { "LOG_LOCAL1", LOG_LOCAL1 }, 1017c478bd9Sstevel@tonic-gate { "LOG_LOCAL2", LOG_LOCAL2 }, 1027c478bd9Sstevel@tonic-gate { "LOG_LOCAL3", LOG_LOCAL3 }, 1037c478bd9Sstevel@tonic-gate { "LOG_LOCAL4", LOG_LOCAL4 }, 1047c478bd9Sstevel@tonic-gate { "LOG_LOCAL5", LOG_LOCAL5 }, 1057c478bd9Sstevel@tonic-gate { "LOG_LOCAL6", LOG_LOCAL6 }, 1067c478bd9Sstevel@tonic-gate { "LOG_LOCAL7", LOG_LOCAL7 }, 1077c478bd9Sstevel@tonic-gate { NULL, 0 } 1087c478bd9Sstevel@tonic-gate }; 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate static char *syslog_locdir; /* l10n messages directory (if alternate) */ 1117c478bd9Sstevel@tonic-gate static char *syslog_url; /* current value of "url" property */ 11219e1255fScy static char *syslog_pointer; /* info to point user to the full message */ 1137c478bd9Sstevel@tonic-gate static int syslog_msgall; /* set to message all faults */ 1147c478bd9Sstevel@tonic-gate static log_ctl_t syslog_ctl; /* log(7D) meta-data for each msg */ 1157c478bd9Sstevel@tonic-gate static int syslog_logfd = -1; /* log(7D) file descriptor */ 1167c478bd9Sstevel@tonic-gate static int syslog_msgfd = -1; /* sysmsg(7D) file descriptor */ 1177c478bd9Sstevel@tonic-gate static int syslog_file; /* log to syslog_logfd */ 1187c478bd9Sstevel@tonic-gate static int syslog_cons; /* log to syslog_msgfd */ 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate /* 1217c478bd9Sstevel@tonic-gate * Ideally we would just use syslog(3C) for outputting our messages, but our 1227c478bd9Sstevel@tonic-gate * messaging standard defines a nice multi-line format and syslogd(1M) is very 1237c478bd9Sstevel@tonic-gate * inflexible and stupid when it comes to multi-line messages. It pulls data 1247c478bd9Sstevel@tonic-gate * out of log(7D) and splits it up by \n, printing each line to the console 1257c478bd9Sstevel@tonic-gate * with its usual prefix of date and sender; it uses the same behavior for the 1267c478bd9Sstevel@tonic-gate * messages file as well. Further, syslog(3C) provides no CE_CONT equivalent 1277c478bd9Sstevel@tonic-gate * for userland callers (which at least works around repeated file prefixing). 1287c478bd9Sstevel@tonic-gate * So with a multi-line message format, your file and console end up like this: 1297c478bd9Sstevel@tonic-gate * 1307c478bd9Sstevel@tonic-gate * Dec 02 18:08:40 hostname this is my nicely formatted 1317c478bd9Sstevel@tonic-gate * Dec 02 18:08:40 hostname message designed for 80 cols 1327c478bd9Sstevel@tonic-gate * ... 1337c478bd9Sstevel@tonic-gate * 1347c478bd9Sstevel@tonic-gate * To resolve these issues, we use our own syslog_emit() wrapper to emit 1357c478bd9Sstevel@tonic-gate * messages and some knowledge of how the Solaris log drivers work. We first 1367c478bd9Sstevel@tonic-gate * construct an enlarged format string containing the appropriate msgid(1). 1377c478bd9Sstevel@tonic-gate * We then format the caller's message using the provided format and buffer. 1387c478bd9Sstevel@tonic-gate * We send this message to log(7D) using putmsg() with SL_CONSOLE | SL_LOGONLY 1397c478bd9Sstevel@tonic-gate * set in the log_ctl_t. The log driver allows us to set SL_LOGONLY when we 1407c478bd9Sstevel@tonic-gate * construct messages ourself, indicating that syslogd should only emit the 1417c478bd9Sstevel@tonic-gate * message to /var/adm/messages and any remote hosts, and skip the console. 142072c378dSmb * Note: the log driver packet size limit for output via putmsg is LOGMAX_PS. 1437c478bd9Sstevel@tonic-gate * Then we emit the message a second time, without the special prefix, to the 1447c478bd9Sstevel@tonic-gate * sysmsg(7D) device, which handles console redirection and also permits us 1457c478bd9Sstevel@tonic-gate * to output any characters we like to the console, including \n and \r. 1467c478bd9Sstevel@tonic-gate */ 14778432d5eScy /*PRINTFLIKE2*/ 1487c478bd9Sstevel@tonic-gate static void 14978432d5eScy syslog_emit(fmd_hdl_t *hdl, const char *msgformat, ...) 1507c478bd9Sstevel@tonic-gate { 1517c478bd9Sstevel@tonic-gate struct strbuf ctl, dat; 1527c478bd9Sstevel@tonic-gate uint32_t msgid; 1537c478bd9Sstevel@tonic-gate 15419e1255fScy char *format, *p, c; 15578432d5eScy char *buf = NULL; 156072c378dSmb size_t formatlen, logmsglen; 15719e1255fScy int len, plen; 1587c478bd9Sstevel@tonic-gate va_list ap; 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate formatlen = strlen(msgformat) + 64; /* +64 for prefix and \0 */ 1617c478bd9Sstevel@tonic-gate format = alloca(formatlen); 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate STRLOG_MAKE_MSGID(msgformat, msgid); 1647c478bd9Sstevel@tonic-gate (void) snprintf(format, formatlen, 1657c478bd9Sstevel@tonic-gate "fmd: [ID %u FACILITY_AND_PRIORITY] %s", msgid, msgformat); 1667c478bd9Sstevel@tonic-gate 16778432d5eScy /* 16878432d5eScy * Figure out the length of the message then allocate a buffer 16978432d5eScy * of adequate size. 17078432d5eScy */ 1717c478bd9Sstevel@tonic-gate va_start(ap, msgformat); 17278432d5eScy if ((len = vsnprintf(&c, 1, format, ap)) >= 0 && 17378432d5eScy (buf = fmd_hdl_alloc(hdl, len + 1, FMD_SLEEP)) != NULL) 17478432d5eScy (void) vsnprintf(buf, len + 1, format, ap); 1757c478bd9Sstevel@tonic-gate va_end(ap); 1767c478bd9Sstevel@tonic-gate 17778432d5eScy if (buf == NULL) 17878432d5eScy return; 17978432d5eScy 1807c478bd9Sstevel@tonic-gate ctl.buf = (void *)&syslog_ctl; 1817c478bd9Sstevel@tonic-gate ctl.len = sizeof (syslog_ctl); 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate dat.buf = buf; 184072c378dSmb logmsglen = strlen(buf) + 1; 185072c378dSmb 186072c378dSmb /* 187072c378dSmb * The underlying log driver won't accept (ERANGE) messages 18819e1255fScy * longer than LOG_MAXPS bytes. The long message will be truncated 18919e1255fScy * and appended with a pointer to the full message. 190072c378dSmb */ 19119e1255fScy if (logmsglen <= LOG_MAXPS) { 192072c378dSmb dat.len = logmsglen; 19319e1255fScy } else { 19419e1255fScy plen = strlen(syslog_pointer) + 1; 19519e1255fScy buf[LOG_MAXPS - plen] = '\0'; 19619e1255fScy /* 19719e1255fScy * If possible, the pointer is appended after a newline 19819e1255fScy */ 19919e1255fScy if ((p = strrchr(buf, '\n')) == NULL) 20019e1255fScy p = &buf[LOG_MAXPS - plen]; 20119e1255fScy 20219e1255fScy (void) strcpy(p, syslog_pointer); 20319e1255fScy dat.len = strlen(buf) + 1; 20419e1255fScy } 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate if (syslog_file && putmsg(syslog_logfd, &ctl, &dat, 0) != 0) { 2077c478bd9Sstevel@tonic-gate fmd_hdl_debug(hdl, "putmsg failed: %s\n", strerror(errno)); 2087c478bd9Sstevel@tonic-gate syslog_stats.log_err.fmds_value.ui64++; 2097c478bd9Sstevel@tonic-gate } 2107c478bd9Sstevel@tonic-gate 2117c478bd9Sstevel@tonic-gate dat.buf = strchr(buf, ']'); 212072c378dSmb dat.len = (size_t)(logmsglen - (dat.buf - buf)); 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate dat.buf[0] = '\r'; /* overwrite ']' with carriage return */ 2157c478bd9Sstevel@tonic-gate dat.buf[1] = '\n'; /* overwrite ' ' with newline */ 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate if (syslog_cons && write(syslog_msgfd, dat.buf, dat.len) != dat.len) { 2187c478bd9Sstevel@tonic-gate fmd_hdl_debug(hdl, "write failed: %s\n", strerror(errno)); 2197c478bd9Sstevel@tonic-gate syslog_stats.msg_err.fmds_value.ui64++; 2207c478bd9Sstevel@tonic-gate } 22178432d5eScy 22278432d5eScy fmd_hdl_free(hdl, buf, len + 1); 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 2267c478bd9Sstevel@tonic-gate static void 2277c478bd9Sstevel@tonic-gate syslog_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl, const char *class) 2287c478bd9Sstevel@tonic-gate { 2297c478bd9Sstevel@tonic-gate char *uuid, *code, *dict, *url, *urlcode, *template, *p; 2307c478bd9Sstevel@tonic-gate char *src_name, *src_vers, *platform, *chassis, *server; 231e1c679faSaf char *typ, *sev, *fmt, *trfmt, *rsp, *imp, *act, *locdir; 23278432d5eScy char desc[1024], date[64]; 2337c478bd9Sstevel@tonic-gate boolean_t domsg; 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate nvlist_t *fmri, *auth; 2367c478bd9Sstevel@tonic-gate uint8_t version; 2377c478bd9Sstevel@tonic-gate struct tm tm, *tmp; 2387c478bd9Sstevel@tonic-gate int64_t *tv; 2397c478bd9Sstevel@tonic-gate time_t sec; 2407c478bd9Sstevel@tonic-gate uint_t tn = 0; 2417c478bd9Sstevel@tonic-gate char *olang = NULL; 242e1c679faSaf int locale_c = 0; 2437c478bd9Sstevel@tonic-gate size_t len; 2447c478bd9Sstevel@tonic-gate 245*cbf75e67SStephen Hanson /* 246*cbf75e67SStephen Hanson * don't log updated and isolated events (for now) 247*cbf75e67SStephen Hanson */ 248*cbf75e67SStephen Hanson if (strcmp(class, FM_LIST_ISOLATED_CLASS) == 0 || 249*cbf75e67SStephen Hanson strcmp(class, FM_LIST_UPDATED_CLASS) == 0) 250*cbf75e67SStephen Hanson return; 251*cbf75e67SStephen Hanson 2527c478bd9Sstevel@tonic-gate if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 || 2537c478bd9Sstevel@tonic-gate version > FM_SUSPECT_VERSION) { 2547c478bd9Sstevel@tonic-gate fmd_hdl_debug(hdl, "invalid event version: %u\n", version); 2557c478bd9Sstevel@tonic-gate syslog_stats.bad_vers.fmds_value.ui64++; 2567c478bd9Sstevel@tonic-gate return; /* invalid event version */ 2577c478bd9Sstevel@tonic-gate } 2587c478bd9Sstevel@tonic-gate 2597c478bd9Sstevel@tonic-gate if (!syslog_msgall && nvlist_lookup_boolean_value(nvl, 2607c478bd9Sstevel@tonic-gate FM_SUSPECT_MESSAGE, &domsg) == 0 && !domsg) { 2617c478bd9Sstevel@tonic-gate fmd_hdl_debug(hdl, "%s requested no message\n", class); 2627c478bd9Sstevel@tonic-gate syslog_stats.no_msg.fmds_value.ui64++; 2637c478bd9Sstevel@tonic-gate return; /* event is not to be messaged */ 2647c478bd9Sstevel@tonic-gate } 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate /* 2677c478bd9Sstevel@tonic-gate * Extract the DE element, which is an FMRI for the diagnosis engine 2687c478bd9Sstevel@tonic-gate * that made this event, and validate its meta-data before continuing. 2697c478bd9Sstevel@tonic-gate */ 2707c478bd9Sstevel@tonic-gate if (nvlist_lookup_nvlist(nvl, FM_SUSPECT_DE, &fmri) != 0 || 2717c478bd9Sstevel@tonic-gate nvlist_lookup_string(fmri, FM_FMRI_SCHEME, &p) != 0 || 2727c478bd9Sstevel@tonic-gate strcmp(p, FM_FMRI_SCHEME_FMD) != 0 || 2737c478bd9Sstevel@tonic-gate nvlist_lookup_uint8(fmri, FM_VERSION, &version) != 0 || 2747c478bd9Sstevel@tonic-gate version > FM_FMD_SCHEME_VERSION || 2757c478bd9Sstevel@tonic-gate nvlist_lookup_nvlist(fmri, FM_FMRI_AUTHORITY, &auth) != 0 || 2767c478bd9Sstevel@tonic-gate nvlist_lookup_uint8(auth, FM_VERSION, &version) != 0 || 2777c478bd9Sstevel@tonic-gate version > FM_FMRI_AUTH_VERSION) { 2787c478bd9Sstevel@tonic-gate syslog_stats.bad_fmri.fmds_value.ui64++; 2797c478bd9Sstevel@tonic-gate return; /* invalid de fmri */ 2807c478bd9Sstevel@tonic-gate } 2817c478bd9Sstevel@tonic-gate 2827c478bd9Sstevel@tonic-gate /* 2837c478bd9Sstevel@tonic-gate * Extract the relevant identifying elements of the FMRI and authority. 2847c478bd9Sstevel@tonic-gate * Note: for now, we ignore FM_FMRI_AUTH_DOMAIN (only for SPs). 2857c478bd9Sstevel@tonic-gate */ 2867c478bd9Sstevel@tonic-gate (void) nvlist_lookup_string(fmri, FM_FMRI_FMD_NAME, &src_name); 2877c478bd9Sstevel@tonic-gate (void) nvlist_lookup_string(fmri, FM_FMRI_FMD_VERSION, &src_vers); 2887c478bd9Sstevel@tonic-gate (void) nvlist_lookup_string(auth, FM_FMRI_AUTH_PRODUCT, &platform); 2897c478bd9Sstevel@tonic-gate (void) nvlist_lookup_string(auth, FM_FMRI_AUTH_SERVER, &server); 2907c478bd9Sstevel@tonic-gate 2917c478bd9Sstevel@tonic-gate if (nvlist_lookup_string(auth, FM_FMRI_AUTH_CHASSIS, &chassis) != 0) 2927c478bd9Sstevel@tonic-gate chassis = "-"; /* chassis serial number may not be present */ 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate /* 2957c478bd9Sstevel@tonic-gate * Extract the uuid and diagcode dictionary from the event code. The 2967c478bd9Sstevel@tonic-gate * dictionary name is the text preceding the first "-" in the code. 2977c478bd9Sstevel@tonic-gate */ 2987c478bd9Sstevel@tonic-gate (void) nvlist_lookup_string(nvl, FM_SUSPECT_UUID, &uuid); 2997c478bd9Sstevel@tonic-gate (void) nvlist_lookup_string(nvl, FM_SUSPECT_DIAG_CODE, &code); 3007c478bd9Sstevel@tonic-gate 3017c478bd9Sstevel@tonic-gate if ((p = strchr(code, '-')) == NULL || p == code) { 3027c478bd9Sstevel@tonic-gate fmd_hdl_debug(hdl, "invalid diagnosis code: %s\n", code); 3037c478bd9Sstevel@tonic-gate syslog_stats.bad_code.fmds_value.ui64++; 3047c478bd9Sstevel@tonic-gate return; /* invalid diagnosis code */ 3057c478bd9Sstevel@tonic-gate } 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate dict = alloca((size_t)(p - code) + 1); 3087c478bd9Sstevel@tonic-gate (void) strncpy(dict, code, (size_t)(p - code)); 3097c478bd9Sstevel@tonic-gate dict[(size_t)(p - code)] = '\0'; 3107c478bd9Sstevel@tonic-gate 3117c478bd9Sstevel@tonic-gate /* 3127c478bd9Sstevel@tonic-gate * Alloca a hunk of memory and use it to create the msgid strings 3137c478bd9Sstevel@tonic-gate * <code>.type, <code>.severity, <code>.description, and so forth. 3147c478bd9Sstevel@tonic-gate * These form the msgids we will use to look up the localized text. 3157c478bd9Sstevel@tonic-gate * Since we've allocated things to be of the right size, we know 3167c478bd9Sstevel@tonic-gate * than snprintf() can't overflow: INT_MAX is used shut lint up and 3177c478bd9Sstevel@tonic-gate * avoid code to needlessly recompute the remaining buffer space. 3187c478bd9Sstevel@tonic-gate */ 3197c478bd9Sstevel@tonic-gate typ = alloca(6 * (strlen(code) + 16)); 3207c478bd9Sstevel@tonic-gate sev = typ + snprintf(typ, INT_MAX, "%s.type", code) + 1; 3217c478bd9Sstevel@tonic-gate fmt = sev + snprintf(sev, INT_MAX, "%s.severity", code) + 1; 3227c478bd9Sstevel@tonic-gate rsp = fmt + snprintf(fmt, INT_MAX, "%s.description", code) + 1; 3237c478bd9Sstevel@tonic-gate imp = rsp + snprintf(rsp, INT_MAX, "%s.response", code) + 1; 3247c478bd9Sstevel@tonic-gate act = imp + snprintf(imp, INT_MAX, "%s.impact", code) + 1; 3257c478bd9Sstevel@tonic-gate (void) snprintf(act, INT_MAX, "%s.action", code); 3267c478bd9Sstevel@tonic-gate 327e1c679faSaf fmd_msg_lock(); 328e1c679faSaf 3297c478bd9Sstevel@tonic-gate if (syslog_locdir != NULL) 330e1c679faSaf locdir = bindtextdomain(dict, syslog_locdir); 3317c478bd9Sstevel@tonic-gate 3327c478bd9Sstevel@tonic-gate if ((trfmt = dgettext(dict, fmt)) == fmt) { 3337c478bd9Sstevel@tonic-gate /* 3347c478bd9Sstevel@tonic-gate * We didn't find a translation in the dictionary for the 3357c478bd9Sstevel@tonic-gate * current language. The string we passed to gettext is merely 3367c478bd9Sstevel@tonic-gate * an index - it isn't sufficient, on its own, to be used as the 3377c478bd9Sstevel@tonic-gate * message. Fall back to C and try again. 3387c478bd9Sstevel@tonic-gate */ 3397c478bd9Sstevel@tonic-gate olang = setlocale(LC_MESSAGES, NULL); 340e1c679faSaf if (olang) { 341e1c679faSaf p = alloca(strlen(olang) + 1); 342e1c679faSaf olang = strcpy(p, olang); 343e1c679faSaf } 344e1c679faSaf locale_c = 1; 3457c478bd9Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, "C"); 3467c478bd9Sstevel@tonic-gate trfmt = dgettext(dict, fmt); 3477c478bd9Sstevel@tonic-gate } 3487c478bd9Sstevel@tonic-gate 3497c478bd9Sstevel@tonic-gate if ((url = dgettext(dict, SYSLOG_URL)) == SYSLOG_URL) 3507c478bd9Sstevel@tonic-gate url = syslog_url; 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate /* 3537c478bd9Sstevel@tonic-gate * If the URL ends with a slash, that indicates the code should be 3547c478bd9Sstevel@tonic-gate * appended to it. After formatting the URL, reformat the DESC 3557c478bd9Sstevel@tonic-gate * text using the URL as an snprintf argument. 3567c478bd9Sstevel@tonic-gate */ 3577c478bd9Sstevel@tonic-gate len = strlen(url); 3587c478bd9Sstevel@tonic-gate if (url[len - 1] == '/') { 3597c478bd9Sstevel@tonic-gate urlcode = alloca(len + strlen(code) + 1); 3607c478bd9Sstevel@tonic-gate (void) snprintf(urlcode, INT_MAX, "%s%s", url, code); 3617c478bd9Sstevel@tonic-gate } else { 3627c478bd9Sstevel@tonic-gate urlcode = url; 3637c478bd9Sstevel@tonic-gate } 3647c478bd9Sstevel@tonic-gate /* LINTED - variable format specifier to snprintf() */ 3657c478bd9Sstevel@tonic-gate (void) snprintf(desc, sizeof (desc), trfmt, urlcode); 3667c478bd9Sstevel@tonic-gate 3677c478bd9Sstevel@tonic-gate /* 3687c478bd9Sstevel@tonic-gate * Extract the diagnosis time and format it using the locale's default. 3697c478bd9Sstevel@tonic-gate * strftime() will use GMT or local time based on our "gmt" setting. 3707c478bd9Sstevel@tonic-gate */ 3717c478bd9Sstevel@tonic-gate if (nvlist_lookup_int64_array(nvl, FM_SUSPECT_DIAG_TIME, 3727c478bd9Sstevel@tonic-gate &tv, &tn) == 0 && tn == 2 && (sec = (time_t)tv[0]) != (time_t)-1 && 3737c478bd9Sstevel@tonic-gate (tmp = localtime_r(&sec, &tm)) != NULL) 3747c478bd9Sstevel@tonic-gate (void) strftime(date, sizeof (date), "%C", tmp); 3757c478bd9Sstevel@tonic-gate else { 3767c478bd9Sstevel@tonic-gate syslog_stats.bad_time.fmds_value.ui64++; 3777c478bd9Sstevel@tonic-gate (void) strcpy(date, "-"); 3787c478bd9Sstevel@tonic-gate } 3797c478bd9Sstevel@tonic-gate 3807c478bd9Sstevel@tonic-gate /* 3817c478bd9Sstevel@tonic-gate * Create and log the final string by filling in the template with the 3827c478bd9Sstevel@tonic-gate * strings we've created and the strings from the message dictionary. 3837c478bd9Sstevel@tonic-gate * If a template is provided for this dictionary, use it, otherwise 3847c478bd9Sstevel@tonic-gate * fall back to the default template. 3857c478bd9Sstevel@tonic-gate */ 3867c478bd9Sstevel@tonic-gate if ((template = dgettext(dict, SYSLOG_TEMPLATE)) == SYSLOG_TEMPLATE) 3877c478bd9Sstevel@tonic-gate template = dgettext(SYSLOG_DOMAIN, SYSLOG_TEMPLATE); 38819e1255fScy /* 38919e1255fScy * Do samely for the message pointer. If the message is too long 39019e1255fScy * to be handled by the underlying log drvier, the message will be 39119e1255fScy * truncated and the pointer will be added to point user to the 39219e1255fScy * full message. 39319e1255fScy */ 39419e1255fScy if ((syslog_pointer = dgettext(dict, SYSLOG_POINTER)) == SYSLOG_POINTER) 39519e1255fScy syslog_pointer = dgettext(SYSLOG_DOMAIN, SYSLOG_POINTER); 3967c478bd9Sstevel@tonic-gate 397627351e3Scy syslog_ctl.pri &= LOG_FACMASK; 39825c6ff4bSstephh if (strcmp(class, FM_LIST_RESOLVED_CLASS) == 0 || 39925c6ff4bSstephh strcmp(class, FM_LIST_REPAIRED_CLASS) == 0) 400627351e3Scy syslog_ctl.pri |= LOG_NOTICE; 401627351e3Scy else 402627351e3Scy syslog_ctl.pri |= LOG_ERR; 40378432d5eScy syslog_emit(hdl, template, code, dgettext(dict, typ), 4047c478bd9Sstevel@tonic-gate dgettext(dict, sev), date, platform, chassis, server, src_name, 4057c478bd9Sstevel@tonic-gate src_vers, uuid, desc, dgettext(dict, rsp), dgettext(dict, imp), 4067c478bd9Sstevel@tonic-gate dgettext(dict, act)); 4077c478bd9Sstevel@tonic-gate 4087c478bd9Sstevel@tonic-gate /* 4097c478bd9Sstevel@tonic-gate * Switch back to our original language if we had to fall back to C. 4107c478bd9Sstevel@tonic-gate */ 4117c478bd9Sstevel@tonic-gate if (olang != NULL) 4127c478bd9Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, olang); 413e1c679faSaf 414e1c679faSaf if (syslog_locdir != NULL) 415e1c679faSaf (void) bindtextdomain(dict, locdir); 416e1c679faSaf 417e1c679faSaf fmd_msg_unlock(); 418e1c679faSaf 419e1c679faSaf if (locale_c) { 420e1c679faSaf fmd_hdl_debug(hdl, 421e1c679faSaf trfmt == fmt ? 422e1c679faSaf "dgettext(%s, %s) in %s and C failed\n" : 423e1c679faSaf "dgettext(%s, %s) in %s failed; C used\n", 424e1c679faSaf dict, fmt, olang ? olang : "<null>"); 425e1c679faSaf } 4267c478bd9Sstevel@tonic-gate } 4277c478bd9Sstevel@tonic-gate 4287c478bd9Sstevel@tonic-gate static const fmd_prop_t fmd_props[] = { 4297c478bd9Sstevel@tonic-gate { "console", FMD_TYPE_BOOL, "true" }, 4307c478bd9Sstevel@tonic-gate { "facility", FMD_TYPE_STRING, "LOG_DAEMON" }, 4317c478bd9Sstevel@tonic-gate { "gmt", FMD_TYPE_BOOL, "false" }, 4327c478bd9Sstevel@tonic-gate { "syslogd", FMD_TYPE_BOOL, "true" }, 4337c478bd9Sstevel@tonic-gate { "url", FMD_TYPE_STRING, "http://sun.com/msg/" }, 4347c478bd9Sstevel@tonic-gate { "message_all", FMD_TYPE_BOOL, "false" }, 4357c478bd9Sstevel@tonic-gate { NULL, 0, NULL } 4367c478bd9Sstevel@tonic-gate }; 4377c478bd9Sstevel@tonic-gate 4387c478bd9Sstevel@tonic-gate static const fmd_hdl_ops_t fmd_ops = { 4397c478bd9Sstevel@tonic-gate syslog_recv, /* fmdo_recv */ 4407c478bd9Sstevel@tonic-gate NULL, /* fmdo_timeout */ 4417c478bd9Sstevel@tonic-gate NULL, /* fmdo_close */ 4427c478bd9Sstevel@tonic-gate NULL, /* fmdo_stats */ 4437c478bd9Sstevel@tonic-gate NULL, /* fmdo_gc */ 4447c478bd9Sstevel@tonic-gate }; 4457c478bd9Sstevel@tonic-gate 4467c478bd9Sstevel@tonic-gate static const fmd_hdl_info_t fmd_info = { 4477c478bd9Sstevel@tonic-gate "Syslog Messaging Agent", "1.0", &fmd_ops, fmd_props 4487c478bd9Sstevel@tonic-gate }; 4497c478bd9Sstevel@tonic-gate 4507c478bd9Sstevel@tonic-gate void 4517c478bd9Sstevel@tonic-gate _fmd_init(fmd_hdl_t *hdl) 4527c478bd9Sstevel@tonic-gate { 4537c478bd9Sstevel@tonic-gate const struct facility *fp; 454e1c679faSaf char *facname, *tz, *rootdir, *locdir, *locale, *p; 4557c478bd9Sstevel@tonic-gate 4567c478bd9Sstevel@tonic-gate if (fmd_hdl_register(hdl, FMD_API_VERSION, &fmd_info) != 0) 4577c478bd9Sstevel@tonic-gate return; /* invalid data in configuration file */ 4587c478bd9Sstevel@tonic-gate 4597c478bd9Sstevel@tonic-gate (void) fmd_stat_create(hdl, FMD_STAT_NOALLOC, sizeof (syslog_stats) / 4607c478bd9Sstevel@tonic-gate sizeof (fmd_stat_t), (fmd_stat_t *)&syslog_stats); 4617c478bd9Sstevel@tonic-gate 4627c478bd9Sstevel@tonic-gate if ((syslog_logfd = open("/dev/conslog", O_WRONLY | O_NOCTTY)) == -1) 4637c478bd9Sstevel@tonic-gate fmd_hdl_abort(hdl, "syslog-msgs failed to open /dev/conslog"); 4647c478bd9Sstevel@tonic-gate 4657c478bd9Sstevel@tonic-gate if ((syslog_msgfd = open("/dev/sysmsg", O_WRONLY | O_NOCTTY)) == -1) 4667c478bd9Sstevel@tonic-gate fmd_hdl_abort(hdl, "syslog-msgs failed to open /dev/sysmsg"); 4677c478bd9Sstevel@tonic-gate 4687c478bd9Sstevel@tonic-gate /* 4697c478bd9Sstevel@tonic-gate * All FMA event dictionaries use msgfmt(1) message objects to produce 4707c478bd9Sstevel@tonic-gate * messages, even for the C locale. We therefore want to use dgettext 4717c478bd9Sstevel@tonic-gate * for all message lookups, but its defined behavior in the C locale is 4727c478bd9Sstevel@tonic-gate * to return the input string. Since our input strings are event codes 4737c478bd9Sstevel@tonic-gate * and not format strings, this doesn't help us. We resolve this nit 4747c478bd9Sstevel@tonic-gate * by setting NLSPATH to a non-existent file: the presence of NLSPATH 4757c478bd9Sstevel@tonic-gate * is defined to force dgettext(3C) to do a full lookup even for C. 4767c478bd9Sstevel@tonic-gate */ 4777c478bd9Sstevel@tonic-gate if (getenv("NLSPATH") == NULL && putenv(fmd_hdl_strdup(hdl, 4787c478bd9Sstevel@tonic-gate "NLSPATH=/usr/lib/fm/fmd/fmd.cat", FMD_SLEEP)) != 0) 4797c478bd9Sstevel@tonic-gate fmd_hdl_abort(hdl, "syslog-msgs failed to set NLSPATH"); 4807c478bd9Sstevel@tonic-gate 481e1c679faSaf fmd_msg_lock(); 4827c478bd9Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, ""); 483e1c679faSaf locale = setlocale(LC_MESSAGES, NULL); 484e1c679faSaf if (locale) { 485e1c679faSaf p = alloca(strlen(locale) + 1); 486e1c679faSaf locale = strcpy(p, locale); 487e1c679faSaf } else { 488e1c679faSaf locale = "<null>"; 489e1c679faSaf } 490e1c679faSaf fmd_msg_unlock(); 491e1c679faSaf fmd_hdl_debug(hdl, "locale=%s\n", locale); 4927c478bd9Sstevel@tonic-gate 4937c478bd9Sstevel@tonic-gate /* 4947c478bd9Sstevel@tonic-gate * If the "gmt" property is set to true, force our EVENT-TIME to be 4957c478bd9Sstevel@tonic-gate * reported in GMT time; otherwise we use localtime. tzset() affects 4967c478bd9Sstevel@tonic-gate * the results of subsequent calls to strftime(3C) above. 4977c478bd9Sstevel@tonic-gate */ 4987c478bd9Sstevel@tonic-gate if (fmd_prop_get_int32(hdl, "gmt") == FMD_B_TRUE && 4997c478bd9Sstevel@tonic-gate ((tz = getenv("TZ")) == NULL || strcmp(tz, "GMT") != 0)) { 5007c478bd9Sstevel@tonic-gate (void) putenv(fmd_hdl_strdup(hdl, "TZ=GMT", FMD_SLEEP)); 5017c478bd9Sstevel@tonic-gate tzset(); /* reload env */ 5027c478bd9Sstevel@tonic-gate } 5037c478bd9Sstevel@tonic-gate 5047c478bd9Sstevel@tonic-gate /* 5057c478bd9Sstevel@tonic-gate * Look up the value of the "facility" property and use it to determine 5067c478bd9Sstevel@tonic-gate * what syslog LOG_* facility value we use to fill in our log_ctl_t. 5077c478bd9Sstevel@tonic-gate * The details of our logging method are described above syslog_emit(). 5087c478bd9Sstevel@tonic-gate */ 5097c478bd9Sstevel@tonic-gate facname = fmd_prop_get_string(hdl, "facility"); 5107c478bd9Sstevel@tonic-gate 5117c478bd9Sstevel@tonic-gate for (fp = syslog_facs; fp->fac_name != NULL; fp++) { 5127c478bd9Sstevel@tonic-gate if (strcmp(fp->fac_name, facname) == 0) 5137c478bd9Sstevel@tonic-gate break; 5147c478bd9Sstevel@tonic-gate } 5157c478bd9Sstevel@tonic-gate 5167c478bd9Sstevel@tonic-gate if (fp->fac_name == NULL) 5177c478bd9Sstevel@tonic-gate fmd_hdl_abort(hdl, "invalid 'facility' setting: %s\n", facname); 5187c478bd9Sstevel@tonic-gate 5197c478bd9Sstevel@tonic-gate fmd_prop_free_string(hdl, facname); 520627351e3Scy syslog_ctl.pri = fp->fac_value; 5217c478bd9Sstevel@tonic-gate syslog_ctl.flags = SL_CONSOLE | SL_LOGONLY; 5227c478bd9Sstevel@tonic-gate 5237c478bd9Sstevel@tonic-gate /* 5247c478bd9Sstevel@tonic-gate * Cache any properties we use every time we receive an event and 5257c478bd9Sstevel@tonic-gate * subscribe to list.suspect events regardless of the .conf file. 5267c478bd9Sstevel@tonic-gate */ 5277c478bd9Sstevel@tonic-gate syslog_file = fmd_prop_get_int32(hdl, "syslogd"); 5287c478bd9Sstevel@tonic-gate syslog_cons = fmd_prop_get_int32(hdl, "console"); 5297c478bd9Sstevel@tonic-gate syslog_url = fmd_prop_get_string(hdl, "url"); 5307c478bd9Sstevel@tonic-gate syslog_msgall = fmd_prop_get_int32(hdl, "message_all"); 5317c478bd9Sstevel@tonic-gate 5327c478bd9Sstevel@tonic-gate /* 5337c478bd9Sstevel@tonic-gate * If fmd's rootdir property is set to a non-default root, then we are 5347c478bd9Sstevel@tonic-gate * going to need to rebind the text domains we use for dgettext() as 5357c478bd9Sstevel@tonic-gate * we go. Look up the default l10n messages directory and make 5367c478bd9Sstevel@tonic-gate * syslog_locdir be this path with fmd.rootdir prepended to it. 5377c478bd9Sstevel@tonic-gate */ 5387c478bd9Sstevel@tonic-gate rootdir = fmd_prop_get_string(hdl, "fmd.rootdir"); 5397c478bd9Sstevel@tonic-gate 540e1c679faSaf if (*rootdir != '\0' && strcmp(rootdir, "/") != 0) { 541e1c679faSaf fmd_msg_lock(); 542e1c679faSaf locdir = bindtextdomain(SYSLOG_DOMAIN, NULL); 543e1c679faSaf fmd_msg_unlock(); 544e1c679faSaf if (locdir != NULL) { 545e1c679faSaf size_t len = strlen(rootdir) + strlen(locdir) + 1; 546e1c679faSaf syslog_locdir = fmd_hdl_alloc(hdl, len, FMD_SLEEP); 547e1c679faSaf (void) snprintf(syslog_locdir, len, "%s%s", rootdir, 548e1c679faSaf locdir); 549e1c679faSaf fmd_hdl_debug(hdl, 550e1c679faSaf "binding textdomain to %s for syslog\n", 551e1c679faSaf syslog_locdir); 552e1c679faSaf } 5537c478bd9Sstevel@tonic-gate } 5547c478bd9Sstevel@tonic-gate 5557c478bd9Sstevel@tonic-gate fmd_prop_free_string(hdl, rootdir); 5567c478bd9Sstevel@tonic-gate fmd_hdl_subscribe(hdl, FM_LIST_SUSPECT_CLASS); 557*cbf75e67SStephen Hanson fmd_hdl_subscribe(hdl, FM_LIST_UPDATED_CLASS); 558*cbf75e67SStephen Hanson fmd_hdl_subscribe(hdl, FM_LIST_ISOLATED_CLASS); 559627351e3Scy fmd_hdl_subscribe(hdl, FM_LIST_REPAIRED_CLASS); 56025c6ff4bSstephh fmd_hdl_subscribe(hdl, FM_LIST_RESOLVED_CLASS); 5617c478bd9Sstevel@tonic-gate } 5627c478bd9Sstevel@tonic-gate 5637c478bd9Sstevel@tonic-gate void 5647c478bd9Sstevel@tonic-gate _fmd_fini(fmd_hdl_t *hdl) 5657c478bd9Sstevel@tonic-gate { 5667c478bd9Sstevel@tonic-gate fmd_hdl_strfree(hdl, syslog_locdir); 5677c478bd9Sstevel@tonic-gate fmd_prop_free_string(hdl, syslog_url); 5687c478bd9Sstevel@tonic-gate 5697c478bd9Sstevel@tonic-gate (void) close(syslog_logfd); 5707c478bd9Sstevel@tonic-gate (void) close(syslog_msgfd); 5717c478bd9Sstevel@tonic-gate } 572