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 5*e1c679faSaf * Common Development and Distribution License (the "License"). 6*e1c679faSaf * 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*e1c679faSaf * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include <sys/fm/protocol.h> 297c478bd9Sstevel@tonic-gate #include <sys/strlog.h> 307c478bd9Sstevel@tonic-gate #include <fm/fmd_api.h> 31*e1c679faSaf #include <fm/fmd_msg.h> 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate #include <stropts.h> 347c478bd9Sstevel@tonic-gate #include <syslog.h> 357c478bd9Sstevel@tonic-gate #include <locale.h> 367c478bd9Sstevel@tonic-gate #include <strings.h> 377c478bd9Sstevel@tonic-gate #include <stdlib.h> 387c478bd9Sstevel@tonic-gate #include <unistd.h> 397c478bd9Sstevel@tonic-gate #include <limits.h> 407c478bd9Sstevel@tonic-gate #include <alloca.h> 417c478bd9Sstevel@tonic-gate #include <errno.h> 427c478bd9Sstevel@tonic-gate #include <fcntl.h> 437c478bd9Sstevel@tonic-gate #include <time.h> 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate /* 467c478bd9Sstevel@tonic-gate * SYSLOG_DOMAIN and SYSLOG_TEMPLATE define the dgettext() parameters the agent 477c478bd9Sstevel@tonic-gate * can use to retrieve the localized format string for diagnosis messages. 487c478bd9Sstevel@tonic-gate * The format string retrieved from SYSLOG_DOMAIN is the default format 497c478bd9Sstevel@tonic-gate * string, but when processing each suspect list, dgettext() is also called 507c478bd9Sstevel@tonic-gate * for the domain that matches the diagcode dictname and if SYSLOG_TEMPLATE 517c478bd9Sstevel@tonic-gate * is defined, it overrides the default for that suspect list only. 527c478bd9Sstevel@tonic-gate * 537c478bd9Sstevel@tonic-gate * Similarly, SYSLOG_URL is also checked to see if syslog_url 547c478bd9Sstevel@tonic-gate * should be overridden for each suspect list. 557c478bd9Sstevel@tonic-gate * 567c478bd9Sstevel@tonic-gate * The net effect of all this is that for a given diagcode DICT-1234-56: 577c478bd9Sstevel@tonic-gate * 587c478bd9Sstevel@tonic-gate * - If DICT.mo defines syslog-msgs-message-template, it is used 597c478bd9Sstevel@tonic-gate * as the format string for the diagnosis message. 607c478bd9Sstevel@tonic-gate * 617c478bd9Sstevel@tonic-gate * - Otherwise, syslog-msgs-message-template from FMD.mo is used. 627c478bd9Sstevel@tonic-gate * 637c478bd9Sstevel@tonic-gate * - If DICT.mo defines syslog-url, it is used when filling 647c478bd9Sstevel@tonic-gate * in the %s in the "description" message. 657c478bd9Sstevel@tonic-gate * 667c478bd9Sstevel@tonic-gate * - Otherwise, if syslog-msgs.conf defines a "url" property, that 677c478bd9Sstevel@tonic-gate * value is used. 687c478bd9Sstevel@tonic-gate * 697c478bd9Sstevel@tonic-gate * - Otherwise, the default "http://sun.com/msg/" is used (via the 707c478bd9Sstevel@tonic-gate * fmd_props[] table defined in this file). 717c478bd9Sstevel@tonic-gate */ 727c478bd9Sstevel@tonic-gate static const char SYSLOG_DOMAIN[] = "FMD"; 737c478bd9Sstevel@tonic-gate static const char SYSLOG_TEMPLATE[] = "syslog-msgs-message-template"; 747c478bd9Sstevel@tonic-gate static const char SYSLOG_URL[] = "syslog-url"; 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 */ 1127c478bd9Sstevel@tonic-gate static int syslog_msgall; /* set to message all faults */ 1137c478bd9Sstevel@tonic-gate static log_ctl_t syslog_ctl; /* log(7D) meta-data for each msg */ 1147c478bd9Sstevel@tonic-gate static int syslog_logfd = -1; /* log(7D) file descriptor */ 1157c478bd9Sstevel@tonic-gate static int syslog_msgfd = -1; /* sysmsg(7D) file descriptor */ 1167c478bd9Sstevel@tonic-gate static int syslog_file; /* log to syslog_logfd */ 1177c478bd9Sstevel@tonic-gate static int syslog_cons; /* log to syslog_msgfd */ 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate /* 1207c478bd9Sstevel@tonic-gate * Ideally we would just use syslog(3C) for outputting our messages, but our 1217c478bd9Sstevel@tonic-gate * messaging standard defines a nice multi-line format and syslogd(1M) is very 1227c478bd9Sstevel@tonic-gate * inflexible and stupid when it comes to multi-line messages. It pulls data 1237c478bd9Sstevel@tonic-gate * out of log(7D) and splits it up by \n, printing each line to the console 1247c478bd9Sstevel@tonic-gate * with its usual prefix of date and sender; it uses the same behavior for the 1257c478bd9Sstevel@tonic-gate * messages file as well. Further, syslog(3C) provides no CE_CONT equivalent 1267c478bd9Sstevel@tonic-gate * for userland callers (which at least works around repeated file prefixing). 1277c478bd9Sstevel@tonic-gate * So with a multi-line message format, your file and console end up like this: 1287c478bd9Sstevel@tonic-gate * 1297c478bd9Sstevel@tonic-gate * Dec 02 18:08:40 hostname this is my nicely formatted 1307c478bd9Sstevel@tonic-gate * Dec 02 18:08:40 hostname message designed for 80 cols 1317c478bd9Sstevel@tonic-gate * ... 1327c478bd9Sstevel@tonic-gate * 1337c478bd9Sstevel@tonic-gate * To resolve these issues, we use our own syslog_emit() wrapper to emit 1347c478bd9Sstevel@tonic-gate * messages and some knowledge of how the Solaris log drivers work. We first 1357c478bd9Sstevel@tonic-gate * construct an enlarged format string containing the appropriate msgid(1). 1367c478bd9Sstevel@tonic-gate * We then format the caller's message using the provided format and buffer. 1377c478bd9Sstevel@tonic-gate * We send this message to log(7D) using putmsg() with SL_CONSOLE | SL_LOGONLY 1387c478bd9Sstevel@tonic-gate * set in the log_ctl_t. The log driver allows us to set SL_LOGONLY when we 1397c478bd9Sstevel@tonic-gate * construct messages ourself, indicating that syslogd should only emit the 1407c478bd9Sstevel@tonic-gate * message to /var/adm/messages and any remote hosts, and skip the console. 1417c478bd9Sstevel@tonic-gate * Then we emit the message a second time, without the special prefix, to the 1427c478bd9Sstevel@tonic-gate * sysmsg(7D) device, which handles console redirection and also permits us 1437c478bd9Sstevel@tonic-gate * to output any characters we like to the console, including \n and \r. 1447c478bd9Sstevel@tonic-gate */ 1457c478bd9Sstevel@tonic-gate /*PRINTFLIKE4*/ 1467c478bd9Sstevel@tonic-gate static void 1477c478bd9Sstevel@tonic-gate syslog_emit(fmd_hdl_t *hdl, char *buf, size_t len, const char *msgformat, ...) 1487c478bd9Sstevel@tonic-gate { 1497c478bd9Sstevel@tonic-gate struct strbuf ctl, dat; 1507c478bd9Sstevel@tonic-gate uint32_t msgid; 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate char *format; 1537c478bd9Sstevel@tonic-gate size_t formatlen; 1547c478bd9Sstevel@tonic-gate va_list ap; 1557c478bd9Sstevel@tonic-gate 1567c478bd9Sstevel@tonic-gate formatlen = strlen(msgformat) + 64; /* +64 for prefix and \0 */ 1577c478bd9Sstevel@tonic-gate format = alloca(formatlen); 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate STRLOG_MAKE_MSGID(msgformat, msgid); 1607c478bd9Sstevel@tonic-gate (void) snprintf(format, formatlen, 1617c478bd9Sstevel@tonic-gate "fmd: [ID %u FACILITY_AND_PRIORITY] %s", msgid, msgformat); 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate va_start(ap, msgformat); 1647c478bd9Sstevel@tonic-gate (void) vsnprintf(buf, len, format, ap); 1657c478bd9Sstevel@tonic-gate va_end(ap); 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate ctl.buf = (void *)&syslog_ctl; 1687c478bd9Sstevel@tonic-gate ctl.len = sizeof (syslog_ctl); 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate dat.buf = buf; 1717c478bd9Sstevel@tonic-gate dat.len = strlen(buf) + 1; 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate if (syslog_file && putmsg(syslog_logfd, &ctl, &dat, 0) != 0) { 1747c478bd9Sstevel@tonic-gate fmd_hdl_debug(hdl, "putmsg failed: %s\n", strerror(errno)); 1757c478bd9Sstevel@tonic-gate syslog_stats.log_err.fmds_value.ui64++; 1767c478bd9Sstevel@tonic-gate } 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate dat.buf = strchr(buf, ']'); 1797c478bd9Sstevel@tonic-gate dat.len -= (size_t)(dat.buf - buf); 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate dat.buf[0] = '\r'; /* overwrite ']' with carriage return */ 1827c478bd9Sstevel@tonic-gate dat.buf[1] = '\n'; /* overwrite ' ' with newline */ 1837c478bd9Sstevel@tonic-gate 1847c478bd9Sstevel@tonic-gate if (syslog_cons && write(syslog_msgfd, dat.buf, dat.len) != dat.len) { 1857c478bd9Sstevel@tonic-gate fmd_hdl_debug(hdl, "write failed: %s\n", strerror(errno)); 1867c478bd9Sstevel@tonic-gate syslog_stats.msg_err.fmds_value.ui64++; 1877c478bd9Sstevel@tonic-gate } 1887c478bd9Sstevel@tonic-gate } 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 1917c478bd9Sstevel@tonic-gate static void 1927c478bd9Sstevel@tonic-gate syslog_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl, const char *class) 1937c478bd9Sstevel@tonic-gate { 1947c478bd9Sstevel@tonic-gate char *uuid, *code, *dict, *url, *urlcode, *template, *p; 1957c478bd9Sstevel@tonic-gate char *src_name, *src_vers, *platform, *chassis, *server; 196*e1c679faSaf char *typ, *sev, *fmt, *trfmt, *rsp, *imp, *act, *locdir; 1977c478bd9Sstevel@tonic-gate char msg[1024], desc[1024], date[64]; 1987c478bd9Sstevel@tonic-gate boolean_t domsg; 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate nvlist_t *fmri, *auth; 2017c478bd9Sstevel@tonic-gate uint8_t version; 2027c478bd9Sstevel@tonic-gate struct tm tm, *tmp; 2037c478bd9Sstevel@tonic-gate int64_t *tv; 2047c478bd9Sstevel@tonic-gate time_t sec; 2057c478bd9Sstevel@tonic-gate uint_t tn = 0; 2067c478bd9Sstevel@tonic-gate char *olang = NULL; 207*e1c679faSaf int locale_c = 0; 2087c478bd9Sstevel@tonic-gate size_t len; 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 || 2117c478bd9Sstevel@tonic-gate version > FM_SUSPECT_VERSION) { 2127c478bd9Sstevel@tonic-gate fmd_hdl_debug(hdl, "invalid event version: %u\n", version); 2137c478bd9Sstevel@tonic-gate syslog_stats.bad_vers.fmds_value.ui64++; 2147c478bd9Sstevel@tonic-gate return; /* invalid event version */ 2157c478bd9Sstevel@tonic-gate } 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate if (!syslog_msgall && nvlist_lookup_boolean_value(nvl, 2187c478bd9Sstevel@tonic-gate FM_SUSPECT_MESSAGE, &domsg) == 0 && !domsg) { 2197c478bd9Sstevel@tonic-gate fmd_hdl_debug(hdl, "%s requested no message\n", class); 2207c478bd9Sstevel@tonic-gate syslog_stats.no_msg.fmds_value.ui64++; 2217c478bd9Sstevel@tonic-gate return; /* event is not to be messaged */ 2227c478bd9Sstevel@tonic-gate } 2237c478bd9Sstevel@tonic-gate 2247c478bd9Sstevel@tonic-gate /* 2257c478bd9Sstevel@tonic-gate * Extract the DE element, which is an FMRI for the diagnosis engine 2267c478bd9Sstevel@tonic-gate * that made this event, and validate its meta-data before continuing. 2277c478bd9Sstevel@tonic-gate */ 2287c478bd9Sstevel@tonic-gate if (nvlist_lookup_nvlist(nvl, FM_SUSPECT_DE, &fmri) != 0 || 2297c478bd9Sstevel@tonic-gate nvlist_lookup_string(fmri, FM_FMRI_SCHEME, &p) != 0 || 2307c478bd9Sstevel@tonic-gate strcmp(p, FM_FMRI_SCHEME_FMD) != 0 || 2317c478bd9Sstevel@tonic-gate nvlist_lookup_uint8(fmri, FM_VERSION, &version) != 0 || 2327c478bd9Sstevel@tonic-gate version > FM_FMD_SCHEME_VERSION || 2337c478bd9Sstevel@tonic-gate nvlist_lookup_nvlist(fmri, FM_FMRI_AUTHORITY, &auth) != 0 || 2347c478bd9Sstevel@tonic-gate nvlist_lookup_uint8(auth, FM_VERSION, &version) != 0 || 2357c478bd9Sstevel@tonic-gate version > FM_FMRI_AUTH_VERSION) { 2367c478bd9Sstevel@tonic-gate syslog_stats.bad_fmri.fmds_value.ui64++; 2377c478bd9Sstevel@tonic-gate return; /* invalid de fmri */ 2387c478bd9Sstevel@tonic-gate } 2397c478bd9Sstevel@tonic-gate 2407c478bd9Sstevel@tonic-gate /* 2417c478bd9Sstevel@tonic-gate * Extract the relevant identifying elements of the FMRI and authority. 2427c478bd9Sstevel@tonic-gate * Note: for now, we ignore FM_FMRI_AUTH_DOMAIN (only for SPs). 2437c478bd9Sstevel@tonic-gate */ 2447c478bd9Sstevel@tonic-gate (void) nvlist_lookup_string(fmri, FM_FMRI_FMD_NAME, &src_name); 2457c478bd9Sstevel@tonic-gate (void) nvlist_lookup_string(fmri, FM_FMRI_FMD_VERSION, &src_vers); 2467c478bd9Sstevel@tonic-gate (void) nvlist_lookup_string(auth, FM_FMRI_AUTH_PRODUCT, &platform); 2477c478bd9Sstevel@tonic-gate (void) nvlist_lookup_string(auth, FM_FMRI_AUTH_SERVER, &server); 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate if (nvlist_lookup_string(auth, FM_FMRI_AUTH_CHASSIS, &chassis) != 0) 2507c478bd9Sstevel@tonic-gate chassis = "-"; /* chassis serial number may not be present */ 2517c478bd9Sstevel@tonic-gate 2527c478bd9Sstevel@tonic-gate /* 2537c478bd9Sstevel@tonic-gate * Extract the uuid and diagcode dictionary from the event code. The 2547c478bd9Sstevel@tonic-gate * dictionary name is the text preceding the first "-" in the code. 2557c478bd9Sstevel@tonic-gate */ 2567c478bd9Sstevel@tonic-gate (void) nvlist_lookup_string(nvl, FM_SUSPECT_UUID, &uuid); 2577c478bd9Sstevel@tonic-gate (void) nvlist_lookup_string(nvl, FM_SUSPECT_DIAG_CODE, &code); 2587c478bd9Sstevel@tonic-gate 2597c478bd9Sstevel@tonic-gate if ((p = strchr(code, '-')) == NULL || p == code) { 2607c478bd9Sstevel@tonic-gate fmd_hdl_debug(hdl, "invalid diagnosis code: %s\n", code); 2617c478bd9Sstevel@tonic-gate syslog_stats.bad_code.fmds_value.ui64++; 2627c478bd9Sstevel@tonic-gate return; /* invalid diagnosis code */ 2637c478bd9Sstevel@tonic-gate } 2647c478bd9Sstevel@tonic-gate 2657c478bd9Sstevel@tonic-gate dict = alloca((size_t)(p - code) + 1); 2667c478bd9Sstevel@tonic-gate (void) strncpy(dict, code, (size_t)(p - code)); 2677c478bd9Sstevel@tonic-gate dict[(size_t)(p - code)] = '\0'; 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate /* 2707c478bd9Sstevel@tonic-gate * Alloca a hunk of memory and use it to create the msgid strings 2717c478bd9Sstevel@tonic-gate * <code>.type, <code>.severity, <code>.description, and so forth. 2727c478bd9Sstevel@tonic-gate * These form the msgids we will use to look up the localized text. 2737c478bd9Sstevel@tonic-gate * Since we've allocated things to be of the right size, we know 2747c478bd9Sstevel@tonic-gate * than snprintf() can't overflow: INT_MAX is used shut lint up and 2757c478bd9Sstevel@tonic-gate * avoid code to needlessly recompute the remaining buffer space. 2767c478bd9Sstevel@tonic-gate */ 2777c478bd9Sstevel@tonic-gate typ = alloca(6 * (strlen(code) + 16)); 2787c478bd9Sstevel@tonic-gate sev = typ + snprintf(typ, INT_MAX, "%s.type", code) + 1; 2797c478bd9Sstevel@tonic-gate fmt = sev + snprintf(sev, INT_MAX, "%s.severity", code) + 1; 2807c478bd9Sstevel@tonic-gate rsp = fmt + snprintf(fmt, INT_MAX, "%s.description", code) + 1; 2817c478bd9Sstevel@tonic-gate imp = rsp + snprintf(rsp, INT_MAX, "%s.response", code) + 1; 2827c478bd9Sstevel@tonic-gate act = imp + snprintf(imp, INT_MAX, "%s.impact", code) + 1; 2837c478bd9Sstevel@tonic-gate (void) snprintf(act, INT_MAX, "%s.action", code); 2847c478bd9Sstevel@tonic-gate 285*e1c679faSaf fmd_msg_lock(); 286*e1c679faSaf 2877c478bd9Sstevel@tonic-gate if (syslog_locdir != NULL) 288*e1c679faSaf locdir = bindtextdomain(dict, syslog_locdir); 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate if ((trfmt = dgettext(dict, fmt)) == fmt) { 2917c478bd9Sstevel@tonic-gate /* 2927c478bd9Sstevel@tonic-gate * We didn't find a translation in the dictionary for the 2937c478bd9Sstevel@tonic-gate * current language. The string we passed to gettext is merely 2947c478bd9Sstevel@tonic-gate * an index - it isn't sufficient, on its own, to be used as the 2957c478bd9Sstevel@tonic-gate * message. Fall back to C and try again. 2967c478bd9Sstevel@tonic-gate */ 2977c478bd9Sstevel@tonic-gate olang = setlocale(LC_MESSAGES, NULL); 298*e1c679faSaf if (olang) { 299*e1c679faSaf p = alloca(strlen(olang) + 1); 300*e1c679faSaf olang = strcpy(p, olang); 301*e1c679faSaf } 302*e1c679faSaf locale_c = 1; 3037c478bd9Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, "C"); 3047c478bd9Sstevel@tonic-gate trfmt = dgettext(dict, fmt); 3057c478bd9Sstevel@tonic-gate } 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate if ((url = dgettext(dict, SYSLOG_URL)) == SYSLOG_URL) 3087c478bd9Sstevel@tonic-gate url = syslog_url; 3097c478bd9Sstevel@tonic-gate 3107c478bd9Sstevel@tonic-gate /* 3117c478bd9Sstevel@tonic-gate * If the URL ends with a slash, that indicates the code should be 3127c478bd9Sstevel@tonic-gate * appended to it. After formatting the URL, reformat the DESC 3137c478bd9Sstevel@tonic-gate * text using the URL as an snprintf argument. 3147c478bd9Sstevel@tonic-gate */ 3157c478bd9Sstevel@tonic-gate len = strlen(url); 3167c478bd9Sstevel@tonic-gate if (url[len - 1] == '/') { 3177c478bd9Sstevel@tonic-gate urlcode = alloca(len + strlen(code) + 1); 3187c478bd9Sstevel@tonic-gate (void) snprintf(urlcode, INT_MAX, "%s%s", url, code); 3197c478bd9Sstevel@tonic-gate } else { 3207c478bd9Sstevel@tonic-gate urlcode = url; 3217c478bd9Sstevel@tonic-gate } 3227c478bd9Sstevel@tonic-gate /* LINTED - variable format specifier to snprintf() */ 3237c478bd9Sstevel@tonic-gate (void) snprintf(desc, sizeof (desc), trfmt, urlcode); 3247c478bd9Sstevel@tonic-gate 3257c478bd9Sstevel@tonic-gate /* 3267c478bd9Sstevel@tonic-gate * Extract the diagnosis time and format it using the locale's default. 3277c478bd9Sstevel@tonic-gate * strftime() will use GMT or local time based on our "gmt" setting. 3287c478bd9Sstevel@tonic-gate */ 3297c478bd9Sstevel@tonic-gate if (nvlist_lookup_int64_array(nvl, FM_SUSPECT_DIAG_TIME, 3307c478bd9Sstevel@tonic-gate &tv, &tn) == 0 && tn == 2 && (sec = (time_t)tv[0]) != (time_t)-1 && 3317c478bd9Sstevel@tonic-gate (tmp = localtime_r(&sec, &tm)) != NULL) 3327c478bd9Sstevel@tonic-gate (void) strftime(date, sizeof (date), "%C", tmp); 3337c478bd9Sstevel@tonic-gate else { 3347c478bd9Sstevel@tonic-gate syslog_stats.bad_time.fmds_value.ui64++; 3357c478bd9Sstevel@tonic-gate (void) strcpy(date, "-"); 3367c478bd9Sstevel@tonic-gate } 3377c478bd9Sstevel@tonic-gate 3387c478bd9Sstevel@tonic-gate /* 3397c478bd9Sstevel@tonic-gate * Create and log the final string by filling in the template with the 3407c478bd9Sstevel@tonic-gate * strings we've created and the strings from the message dictionary. 3417c478bd9Sstevel@tonic-gate * If a template is provided for this dictionary, use it, otherwise 3427c478bd9Sstevel@tonic-gate * fall back to the default template. 3437c478bd9Sstevel@tonic-gate */ 3447c478bd9Sstevel@tonic-gate if ((template = dgettext(dict, SYSLOG_TEMPLATE)) == SYSLOG_TEMPLATE) 3457c478bd9Sstevel@tonic-gate template = dgettext(SYSLOG_DOMAIN, SYSLOG_TEMPLATE); 3467c478bd9Sstevel@tonic-gate 3477c478bd9Sstevel@tonic-gate syslog_emit(hdl, msg, sizeof (msg), 3487c478bd9Sstevel@tonic-gate template, code, dgettext(dict, typ), 3497c478bd9Sstevel@tonic-gate dgettext(dict, sev), date, platform, chassis, server, src_name, 3507c478bd9Sstevel@tonic-gate src_vers, uuid, desc, dgettext(dict, rsp), dgettext(dict, imp), 3517c478bd9Sstevel@tonic-gate dgettext(dict, act)); 3527c478bd9Sstevel@tonic-gate 3537c478bd9Sstevel@tonic-gate /* 3547c478bd9Sstevel@tonic-gate * Switch back to our original language if we had to fall back to C. 3557c478bd9Sstevel@tonic-gate */ 3567c478bd9Sstevel@tonic-gate if (olang != NULL) 3577c478bd9Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, olang); 358*e1c679faSaf 359*e1c679faSaf if (syslog_locdir != NULL) 360*e1c679faSaf (void) bindtextdomain(dict, locdir); 361*e1c679faSaf 362*e1c679faSaf fmd_msg_unlock(); 363*e1c679faSaf 364*e1c679faSaf if (locale_c) { 365*e1c679faSaf fmd_hdl_debug(hdl, 366*e1c679faSaf trfmt == fmt ? 367*e1c679faSaf "dgettext(%s, %s) in %s and C failed\n" : 368*e1c679faSaf "dgettext(%s, %s) in %s failed; C used\n", 369*e1c679faSaf dict, fmt, olang ? olang : "<null>"); 370*e1c679faSaf } 3717c478bd9Sstevel@tonic-gate } 3727c478bd9Sstevel@tonic-gate 3737c478bd9Sstevel@tonic-gate static const fmd_prop_t fmd_props[] = { 3747c478bd9Sstevel@tonic-gate { "console", FMD_TYPE_BOOL, "true" }, 3757c478bd9Sstevel@tonic-gate { "facility", FMD_TYPE_STRING, "LOG_DAEMON" }, 3767c478bd9Sstevel@tonic-gate { "gmt", FMD_TYPE_BOOL, "false" }, 3777c478bd9Sstevel@tonic-gate { "syslogd", FMD_TYPE_BOOL, "true" }, 3787c478bd9Sstevel@tonic-gate { "url", FMD_TYPE_STRING, "http://sun.com/msg/" }, 3797c478bd9Sstevel@tonic-gate { "message_all", FMD_TYPE_BOOL, "false" }, 3807c478bd9Sstevel@tonic-gate { NULL, 0, NULL } 3817c478bd9Sstevel@tonic-gate }; 3827c478bd9Sstevel@tonic-gate 3837c478bd9Sstevel@tonic-gate static const fmd_hdl_ops_t fmd_ops = { 3847c478bd9Sstevel@tonic-gate syslog_recv, /* fmdo_recv */ 3857c478bd9Sstevel@tonic-gate NULL, /* fmdo_timeout */ 3867c478bd9Sstevel@tonic-gate NULL, /* fmdo_close */ 3877c478bd9Sstevel@tonic-gate NULL, /* fmdo_stats */ 3887c478bd9Sstevel@tonic-gate NULL, /* fmdo_gc */ 3897c478bd9Sstevel@tonic-gate }; 3907c478bd9Sstevel@tonic-gate 3917c478bd9Sstevel@tonic-gate static const fmd_hdl_info_t fmd_info = { 3927c478bd9Sstevel@tonic-gate "Syslog Messaging Agent", "1.0", &fmd_ops, fmd_props 3937c478bd9Sstevel@tonic-gate }; 3947c478bd9Sstevel@tonic-gate 3957c478bd9Sstevel@tonic-gate void 3967c478bd9Sstevel@tonic-gate _fmd_init(fmd_hdl_t *hdl) 3977c478bd9Sstevel@tonic-gate { 3987c478bd9Sstevel@tonic-gate const struct facility *fp; 399*e1c679faSaf char *facname, *tz, *rootdir, *locdir, *locale, *p; 4007c478bd9Sstevel@tonic-gate 4017c478bd9Sstevel@tonic-gate if (fmd_hdl_register(hdl, FMD_API_VERSION, &fmd_info) != 0) 4027c478bd9Sstevel@tonic-gate return; /* invalid data in configuration file */ 4037c478bd9Sstevel@tonic-gate 4047c478bd9Sstevel@tonic-gate (void) fmd_stat_create(hdl, FMD_STAT_NOALLOC, sizeof (syslog_stats) / 4057c478bd9Sstevel@tonic-gate sizeof (fmd_stat_t), (fmd_stat_t *)&syslog_stats); 4067c478bd9Sstevel@tonic-gate 4077c478bd9Sstevel@tonic-gate if ((syslog_logfd = open("/dev/conslog", O_WRONLY | O_NOCTTY)) == -1) 4087c478bd9Sstevel@tonic-gate fmd_hdl_abort(hdl, "syslog-msgs failed to open /dev/conslog"); 4097c478bd9Sstevel@tonic-gate 4107c478bd9Sstevel@tonic-gate if ((syslog_msgfd = open("/dev/sysmsg", O_WRONLY | O_NOCTTY)) == -1) 4117c478bd9Sstevel@tonic-gate fmd_hdl_abort(hdl, "syslog-msgs failed to open /dev/sysmsg"); 4127c478bd9Sstevel@tonic-gate 4137c478bd9Sstevel@tonic-gate /* 4147c478bd9Sstevel@tonic-gate * All FMA event dictionaries use msgfmt(1) message objects to produce 4157c478bd9Sstevel@tonic-gate * messages, even for the C locale. We therefore want to use dgettext 4167c478bd9Sstevel@tonic-gate * for all message lookups, but its defined behavior in the C locale is 4177c478bd9Sstevel@tonic-gate * to return the input string. Since our input strings are event codes 4187c478bd9Sstevel@tonic-gate * and not format strings, this doesn't help us. We resolve this nit 4197c478bd9Sstevel@tonic-gate * by setting NLSPATH to a non-existent file: the presence of NLSPATH 4207c478bd9Sstevel@tonic-gate * is defined to force dgettext(3C) to do a full lookup even for C. 4217c478bd9Sstevel@tonic-gate */ 4227c478bd9Sstevel@tonic-gate if (getenv("NLSPATH") == NULL && putenv(fmd_hdl_strdup(hdl, 4237c478bd9Sstevel@tonic-gate "NLSPATH=/usr/lib/fm/fmd/fmd.cat", FMD_SLEEP)) != 0) 4247c478bd9Sstevel@tonic-gate fmd_hdl_abort(hdl, "syslog-msgs failed to set NLSPATH"); 4257c478bd9Sstevel@tonic-gate 426*e1c679faSaf fmd_msg_lock(); 4277c478bd9Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, ""); 428*e1c679faSaf locale = setlocale(LC_MESSAGES, NULL); 429*e1c679faSaf if (locale) { 430*e1c679faSaf p = alloca(strlen(locale) + 1); 431*e1c679faSaf locale = strcpy(p, locale); 432*e1c679faSaf } else { 433*e1c679faSaf locale = "<null>"; 434*e1c679faSaf } 435*e1c679faSaf fmd_msg_unlock(); 436*e1c679faSaf fmd_hdl_debug(hdl, "locale=%s\n", locale); 4377c478bd9Sstevel@tonic-gate 4387c478bd9Sstevel@tonic-gate /* 4397c478bd9Sstevel@tonic-gate * If the "gmt" property is set to true, force our EVENT-TIME to be 4407c478bd9Sstevel@tonic-gate * reported in GMT time; otherwise we use localtime. tzset() affects 4417c478bd9Sstevel@tonic-gate * the results of subsequent calls to strftime(3C) above. 4427c478bd9Sstevel@tonic-gate */ 4437c478bd9Sstevel@tonic-gate if (fmd_prop_get_int32(hdl, "gmt") == FMD_B_TRUE && 4447c478bd9Sstevel@tonic-gate ((tz = getenv("TZ")) == NULL || strcmp(tz, "GMT") != 0)) { 4457c478bd9Sstevel@tonic-gate (void) putenv(fmd_hdl_strdup(hdl, "TZ=GMT", FMD_SLEEP)); 4467c478bd9Sstevel@tonic-gate tzset(); /* reload env */ 4477c478bd9Sstevel@tonic-gate } 4487c478bd9Sstevel@tonic-gate 4497c478bd9Sstevel@tonic-gate /* 4507c478bd9Sstevel@tonic-gate * Look up the value of the "facility" property and use it to determine 4517c478bd9Sstevel@tonic-gate * what syslog LOG_* facility value we use to fill in our log_ctl_t. 4527c478bd9Sstevel@tonic-gate * The details of our logging method are described above syslog_emit(). 4537c478bd9Sstevel@tonic-gate */ 4547c478bd9Sstevel@tonic-gate facname = fmd_prop_get_string(hdl, "facility"); 4557c478bd9Sstevel@tonic-gate 4567c478bd9Sstevel@tonic-gate for (fp = syslog_facs; fp->fac_name != NULL; fp++) { 4577c478bd9Sstevel@tonic-gate if (strcmp(fp->fac_name, facname) == 0) 4587c478bd9Sstevel@tonic-gate break; 4597c478bd9Sstevel@tonic-gate } 4607c478bd9Sstevel@tonic-gate 4617c478bd9Sstevel@tonic-gate if (fp->fac_name == NULL) 4627c478bd9Sstevel@tonic-gate fmd_hdl_abort(hdl, "invalid 'facility' setting: %s\n", facname); 4637c478bd9Sstevel@tonic-gate 4647c478bd9Sstevel@tonic-gate fmd_prop_free_string(hdl, facname); 4657c478bd9Sstevel@tonic-gate syslog_ctl.pri = fp->fac_value | LOG_ERR; 4667c478bd9Sstevel@tonic-gate syslog_ctl.flags = SL_CONSOLE | SL_LOGONLY; 4677c478bd9Sstevel@tonic-gate 4687c478bd9Sstevel@tonic-gate /* 4697c478bd9Sstevel@tonic-gate * Cache any properties we use every time we receive an event and 4707c478bd9Sstevel@tonic-gate * subscribe to list.suspect events regardless of the .conf file. 4717c478bd9Sstevel@tonic-gate */ 4727c478bd9Sstevel@tonic-gate syslog_file = fmd_prop_get_int32(hdl, "syslogd"); 4737c478bd9Sstevel@tonic-gate syslog_cons = fmd_prop_get_int32(hdl, "console"); 4747c478bd9Sstevel@tonic-gate syslog_url = fmd_prop_get_string(hdl, "url"); 4757c478bd9Sstevel@tonic-gate syslog_msgall = fmd_prop_get_int32(hdl, "message_all"); 4767c478bd9Sstevel@tonic-gate 4777c478bd9Sstevel@tonic-gate /* 4787c478bd9Sstevel@tonic-gate * If fmd's rootdir property is set to a non-default root, then we are 4797c478bd9Sstevel@tonic-gate * going to need to rebind the text domains we use for dgettext() as 4807c478bd9Sstevel@tonic-gate * we go. Look up the default l10n messages directory and make 4817c478bd9Sstevel@tonic-gate * syslog_locdir be this path with fmd.rootdir prepended to it. 4827c478bd9Sstevel@tonic-gate */ 4837c478bd9Sstevel@tonic-gate rootdir = fmd_prop_get_string(hdl, "fmd.rootdir"); 4847c478bd9Sstevel@tonic-gate 485*e1c679faSaf if (*rootdir != '\0' && strcmp(rootdir, "/") != 0) { 486*e1c679faSaf fmd_msg_lock(); 487*e1c679faSaf locdir = bindtextdomain(SYSLOG_DOMAIN, NULL); 488*e1c679faSaf fmd_msg_unlock(); 489*e1c679faSaf if (locdir != NULL) { 490*e1c679faSaf size_t len = strlen(rootdir) + strlen(locdir) + 1; 491*e1c679faSaf syslog_locdir = fmd_hdl_alloc(hdl, len, FMD_SLEEP); 492*e1c679faSaf (void) snprintf(syslog_locdir, len, "%s%s", rootdir, 493*e1c679faSaf locdir); 494*e1c679faSaf fmd_hdl_debug(hdl, 495*e1c679faSaf "binding textdomain to %s for syslog\n", 496*e1c679faSaf syslog_locdir); 497*e1c679faSaf } 4987c478bd9Sstevel@tonic-gate } 4997c478bd9Sstevel@tonic-gate 5007c478bd9Sstevel@tonic-gate fmd_prop_free_string(hdl, rootdir); 5017c478bd9Sstevel@tonic-gate fmd_hdl_subscribe(hdl, FM_LIST_SUSPECT_CLASS); 5027c478bd9Sstevel@tonic-gate } 5037c478bd9Sstevel@tonic-gate 5047c478bd9Sstevel@tonic-gate void 5057c478bd9Sstevel@tonic-gate _fmd_fini(fmd_hdl_t *hdl) 5067c478bd9Sstevel@tonic-gate { 5077c478bd9Sstevel@tonic-gate fmd_hdl_strfree(hdl, syslog_locdir); 5087c478bd9Sstevel@tonic-gate fmd_prop_free_string(hdl, syslog_url); 5097c478bd9Sstevel@tonic-gate 5107c478bd9Sstevel@tonic-gate (void) close(syslog_logfd); 5117c478bd9Sstevel@tonic-gate (void) close(syslog_msgfd); 5127c478bd9Sstevel@tonic-gate } 513