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 /* 22627351e3Scy * Copyright 2008 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> 31e1c679faSaf #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 */ 145*78432d5eScy /*PRINTFLIKE2*/ 1467c478bd9Sstevel@tonic-gate static void 147*78432d5eScy syslog_emit(fmd_hdl_t *hdl, const char *msgformat, ...) 1487c478bd9Sstevel@tonic-gate { 1497c478bd9Sstevel@tonic-gate struct strbuf ctl, dat; 1507c478bd9Sstevel@tonic-gate uint32_t msgid; 1517c478bd9Sstevel@tonic-gate 152*78432d5eScy char *format, c; 153*78432d5eScy char *buf = NULL; 1547c478bd9Sstevel@tonic-gate size_t formatlen; 155*78432d5eScy int len; 1567c478bd9Sstevel@tonic-gate va_list ap; 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate formatlen = strlen(msgformat) + 64; /* +64 for prefix and \0 */ 1597c478bd9Sstevel@tonic-gate format = alloca(formatlen); 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate STRLOG_MAKE_MSGID(msgformat, msgid); 1627c478bd9Sstevel@tonic-gate (void) snprintf(format, formatlen, 1637c478bd9Sstevel@tonic-gate "fmd: [ID %u FACILITY_AND_PRIORITY] %s", msgid, msgformat); 1647c478bd9Sstevel@tonic-gate 165*78432d5eScy /* 166*78432d5eScy * Figure out the length of the message then allocate a buffer 167*78432d5eScy * of adequate size. 168*78432d5eScy */ 1697c478bd9Sstevel@tonic-gate va_start(ap, msgformat); 170*78432d5eScy if ((len = vsnprintf(&c, 1, format, ap)) >= 0 && 171*78432d5eScy (buf = fmd_hdl_alloc(hdl, len + 1, FMD_SLEEP)) != NULL) 172*78432d5eScy (void) vsnprintf(buf, len + 1, format, ap); 1737c478bd9Sstevel@tonic-gate va_end(ap); 1747c478bd9Sstevel@tonic-gate 175*78432d5eScy if (buf == NULL) 176*78432d5eScy return; 177*78432d5eScy 1787c478bd9Sstevel@tonic-gate ctl.buf = (void *)&syslog_ctl; 1797c478bd9Sstevel@tonic-gate ctl.len = sizeof (syslog_ctl); 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate dat.buf = buf; 1827c478bd9Sstevel@tonic-gate dat.len = strlen(buf) + 1; 1837c478bd9Sstevel@tonic-gate 1847c478bd9Sstevel@tonic-gate if (syslog_file && putmsg(syslog_logfd, &ctl, &dat, 0) != 0) { 1857c478bd9Sstevel@tonic-gate fmd_hdl_debug(hdl, "putmsg failed: %s\n", strerror(errno)); 1867c478bd9Sstevel@tonic-gate syslog_stats.log_err.fmds_value.ui64++; 1877c478bd9Sstevel@tonic-gate } 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate dat.buf = strchr(buf, ']'); 1907c478bd9Sstevel@tonic-gate dat.len -= (size_t)(dat.buf - buf); 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate dat.buf[0] = '\r'; /* overwrite ']' with carriage return */ 1937c478bd9Sstevel@tonic-gate dat.buf[1] = '\n'; /* overwrite ' ' with newline */ 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate if (syslog_cons && write(syslog_msgfd, dat.buf, dat.len) != dat.len) { 1967c478bd9Sstevel@tonic-gate fmd_hdl_debug(hdl, "write failed: %s\n", strerror(errno)); 1977c478bd9Sstevel@tonic-gate syslog_stats.msg_err.fmds_value.ui64++; 1987c478bd9Sstevel@tonic-gate } 199*78432d5eScy 200*78432d5eScy fmd_hdl_free(hdl, buf, len + 1); 2017c478bd9Sstevel@tonic-gate } 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 2047c478bd9Sstevel@tonic-gate static void 2057c478bd9Sstevel@tonic-gate syslog_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl, const char *class) 2067c478bd9Sstevel@tonic-gate { 2077c478bd9Sstevel@tonic-gate char *uuid, *code, *dict, *url, *urlcode, *template, *p; 2087c478bd9Sstevel@tonic-gate char *src_name, *src_vers, *platform, *chassis, *server; 209e1c679faSaf char *typ, *sev, *fmt, *trfmt, *rsp, *imp, *act, *locdir; 210*78432d5eScy char desc[1024], date[64]; 2117c478bd9Sstevel@tonic-gate boolean_t domsg; 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate nvlist_t *fmri, *auth; 2147c478bd9Sstevel@tonic-gate uint8_t version; 2157c478bd9Sstevel@tonic-gate struct tm tm, *tmp; 2167c478bd9Sstevel@tonic-gate int64_t *tv; 2177c478bd9Sstevel@tonic-gate time_t sec; 2187c478bd9Sstevel@tonic-gate uint_t tn = 0; 2197c478bd9Sstevel@tonic-gate char *olang = NULL; 220e1c679faSaf int locale_c = 0; 2217c478bd9Sstevel@tonic-gate size_t len; 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 || 2247c478bd9Sstevel@tonic-gate version > FM_SUSPECT_VERSION) { 2257c478bd9Sstevel@tonic-gate fmd_hdl_debug(hdl, "invalid event version: %u\n", version); 2267c478bd9Sstevel@tonic-gate syslog_stats.bad_vers.fmds_value.ui64++; 2277c478bd9Sstevel@tonic-gate return; /* invalid event version */ 2287c478bd9Sstevel@tonic-gate } 2297c478bd9Sstevel@tonic-gate 2307c478bd9Sstevel@tonic-gate if (!syslog_msgall && nvlist_lookup_boolean_value(nvl, 2317c478bd9Sstevel@tonic-gate FM_SUSPECT_MESSAGE, &domsg) == 0 && !domsg) { 2327c478bd9Sstevel@tonic-gate fmd_hdl_debug(hdl, "%s requested no message\n", class); 2337c478bd9Sstevel@tonic-gate syslog_stats.no_msg.fmds_value.ui64++; 2347c478bd9Sstevel@tonic-gate return; /* event is not to be messaged */ 2357c478bd9Sstevel@tonic-gate } 2367c478bd9Sstevel@tonic-gate 2377c478bd9Sstevel@tonic-gate /* 2387c478bd9Sstevel@tonic-gate * Extract the DE element, which is an FMRI for the diagnosis engine 2397c478bd9Sstevel@tonic-gate * that made this event, and validate its meta-data before continuing. 2407c478bd9Sstevel@tonic-gate */ 2417c478bd9Sstevel@tonic-gate if (nvlist_lookup_nvlist(nvl, FM_SUSPECT_DE, &fmri) != 0 || 2427c478bd9Sstevel@tonic-gate nvlist_lookup_string(fmri, FM_FMRI_SCHEME, &p) != 0 || 2437c478bd9Sstevel@tonic-gate strcmp(p, FM_FMRI_SCHEME_FMD) != 0 || 2447c478bd9Sstevel@tonic-gate nvlist_lookup_uint8(fmri, FM_VERSION, &version) != 0 || 2457c478bd9Sstevel@tonic-gate version > FM_FMD_SCHEME_VERSION || 2467c478bd9Sstevel@tonic-gate nvlist_lookup_nvlist(fmri, FM_FMRI_AUTHORITY, &auth) != 0 || 2477c478bd9Sstevel@tonic-gate nvlist_lookup_uint8(auth, FM_VERSION, &version) != 0 || 2487c478bd9Sstevel@tonic-gate version > FM_FMRI_AUTH_VERSION) { 2497c478bd9Sstevel@tonic-gate syslog_stats.bad_fmri.fmds_value.ui64++; 2507c478bd9Sstevel@tonic-gate return; /* invalid de fmri */ 2517c478bd9Sstevel@tonic-gate } 2527c478bd9Sstevel@tonic-gate 2537c478bd9Sstevel@tonic-gate /* 2547c478bd9Sstevel@tonic-gate * Extract the relevant identifying elements of the FMRI and authority. 2557c478bd9Sstevel@tonic-gate * Note: for now, we ignore FM_FMRI_AUTH_DOMAIN (only for SPs). 2567c478bd9Sstevel@tonic-gate */ 2577c478bd9Sstevel@tonic-gate (void) nvlist_lookup_string(fmri, FM_FMRI_FMD_NAME, &src_name); 2587c478bd9Sstevel@tonic-gate (void) nvlist_lookup_string(fmri, FM_FMRI_FMD_VERSION, &src_vers); 2597c478bd9Sstevel@tonic-gate (void) nvlist_lookup_string(auth, FM_FMRI_AUTH_PRODUCT, &platform); 2607c478bd9Sstevel@tonic-gate (void) nvlist_lookup_string(auth, FM_FMRI_AUTH_SERVER, &server); 2617c478bd9Sstevel@tonic-gate 2627c478bd9Sstevel@tonic-gate if (nvlist_lookup_string(auth, FM_FMRI_AUTH_CHASSIS, &chassis) != 0) 2637c478bd9Sstevel@tonic-gate chassis = "-"; /* chassis serial number may not be present */ 2647c478bd9Sstevel@tonic-gate 2657c478bd9Sstevel@tonic-gate /* 2667c478bd9Sstevel@tonic-gate * Extract the uuid and diagcode dictionary from the event code. The 2677c478bd9Sstevel@tonic-gate * dictionary name is the text preceding the first "-" in the code. 2687c478bd9Sstevel@tonic-gate */ 2697c478bd9Sstevel@tonic-gate (void) nvlist_lookup_string(nvl, FM_SUSPECT_UUID, &uuid); 2707c478bd9Sstevel@tonic-gate (void) nvlist_lookup_string(nvl, FM_SUSPECT_DIAG_CODE, &code); 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate if ((p = strchr(code, '-')) == NULL || p == code) { 2737c478bd9Sstevel@tonic-gate fmd_hdl_debug(hdl, "invalid diagnosis code: %s\n", code); 2747c478bd9Sstevel@tonic-gate syslog_stats.bad_code.fmds_value.ui64++; 2757c478bd9Sstevel@tonic-gate return; /* invalid diagnosis code */ 2767c478bd9Sstevel@tonic-gate } 2777c478bd9Sstevel@tonic-gate 2787c478bd9Sstevel@tonic-gate dict = alloca((size_t)(p - code) + 1); 2797c478bd9Sstevel@tonic-gate (void) strncpy(dict, code, (size_t)(p - code)); 2807c478bd9Sstevel@tonic-gate dict[(size_t)(p - code)] = '\0'; 2817c478bd9Sstevel@tonic-gate 2827c478bd9Sstevel@tonic-gate /* 2837c478bd9Sstevel@tonic-gate * Alloca a hunk of memory and use it to create the msgid strings 2847c478bd9Sstevel@tonic-gate * <code>.type, <code>.severity, <code>.description, and so forth. 2857c478bd9Sstevel@tonic-gate * These form the msgids we will use to look up the localized text. 2867c478bd9Sstevel@tonic-gate * Since we've allocated things to be of the right size, we know 2877c478bd9Sstevel@tonic-gate * than snprintf() can't overflow: INT_MAX is used shut lint up and 2887c478bd9Sstevel@tonic-gate * avoid code to needlessly recompute the remaining buffer space. 2897c478bd9Sstevel@tonic-gate */ 2907c478bd9Sstevel@tonic-gate typ = alloca(6 * (strlen(code) + 16)); 2917c478bd9Sstevel@tonic-gate sev = typ + snprintf(typ, INT_MAX, "%s.type", code) + 1; 2927c478bd9Sstevel@tonic-gate fmt = sev + snprintf(sev, INT_MAX, "%s.severity", code) + 1; 2937c478bd9Sstevel@tonic-gate rsp = fmt + snprintf(fmt, INT_MAX, "%s.description", code) + 1; 2947c478bd9Sstevel@tonic-gate imp = rsp + snprintf(rsp, INT_MAX, "%s.response", code) + 1; 2957c478bd9Sstevel@tonic-gate act = imp + snprintf(imp, INT_MAX, "%s.impact", code) + 1; 2967c478bd9Sstevel@tonic-gate (void) snprintf(act, INT_MAX, "%s.action", code); 2977c478bd9Sstevel@tonic-gate 298e1c679faSaf fmd_msg_lock(); 299e1c679faSaf 3007c478bd9Sstevel@tonic-gate if (syslog_locdir != NULL) 301e1c679faSaf locdir = bindtextdomain(dict, syslog_locdir); 3027c478bd9Sstevel@tonic-gate 3037c478bd9Sstevel@tonic-gate if ((trfmt = dgettext(dict, fmt)) == fmt) { 3047c478bd9Sstevel@tonic-gate /* 3057c478bd9Sstevel@tonic-gate * We didn't find a translation in the dictionary for the 3067c478bd9Sstevel@tonic-gate * current language. The string we passed to gettext is merely 3077c478bd9Sstevel@tonic-gate * an index - it isn't sufficient, on its own, to be used as the 3087c478bd9Sstevel@tonic-gate * message. Fall back to C and try again. 3097c478bd9Sstevel@tonic-gate */ 3107c478bd9Sstevel@tonic-gate olang = setlocale(LC_MESSAGES, NULL); 311e1c679faSaf if (olang) { 312e1c679faSaf p = alloca(strlen(olang) + 1); 313e1c679faSaf olang = strcpy(p, olang); 314e1c679faSaf } 315e1c679faSaf locale_c = 1; 3167c478bd9Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, "C"); 3177c478bd9Sstevel@tonic-gate trfmt = dgettext(dict, fmt); 3187c478bd9Sstevel@tonic-gate } 3197c478bd9Sstevel@tonic-gate 3207c478bd9Sstevel@tonic-gate if ((url = dgettext(dict, SYSLOG_URL)) == SYSLOG_URL) 3217c478bd9Sstevel@tonic-gate url = syslog_url; 3227c478bd9Sstevel@tonic-gate 3237c478bd9Sstevel@tonic-gate /* 3247c478bd9Sstevel@tonic-gate * If the URL ends with a slash, that indicates the code should be 3257c478bd9Sstevel@tonic-gate * appended to it. After formatting the URL, reformat the DESC 3267c478bd9Sstevel@tonic-gate * text using the URL as an snprintf argument. 3277c478bd9Sstevel@tonic-gate */ 3287c478bd9Sstevel@tonic-gate len = strlen(url); 3297c478bd9Sstevel@tonic-gate if (url[len - 1] == '/') { 3307c478bd9Sstevel@tonic-gate urlcode = alloca(len + strlen(code) + 1); 3317c478bd9Sstevel@tonic-gate (void) snprintf(urlcode, INT_MAX, "%s%s", url, code); 3327c478bd9Sstevel@tonic-gate } else { 3337c478bd9Sstevel@tonic-gate urlcode = url; 3347c478bd9Sstevel@tonic-gate } 3357c478bd9Sstevel@tonic-gate /* LINTED - variable format specifier to snprintf() */ 3367c478bd9Sstevel@tonic-gate (void) snprintf(desc, sizeof (desc), trfmt, urlcode); 3377c478bd9Sstevel@tonic-gate 3387c478bd9Sstevel@tonic-gate /* 3397c478bd9Sstevel@tonic-gate * Extract the diagnosis time and format it using the locale's default. 3407c478bd9Sstevel@tonic-gate * strftime() will use GMT or local time based on our "gmt" setting. 3417c478bd9Sstevel@tonic-gate */ 3427c478bd9Sstevel@tonic-gate if (nvlist_lookup_int64_array(nvl, FM_SUSPECT_DIAG_TIME, 3437c478bd9Sstevel@tonic-gate &tv, &tn) == 0 && tn == 2 && (sec = (time_t)tv[0]) != (time_t)-1 && 3447c478bd9Sstevel@tonic-gate (tmp = localtime_r(&sec, &tm)) != NULL) 3457c478bd9Sstevel@tonic-gate (void) strftime(date, sizeof (date), "%C", tmp); 3467c478bd9Sstevel@tonic-gate else { 3477c478bd9Sstevel@tonic-gate syslog_stats.bad_time.fmds_value.ui64++; 3487c478bd9Sstevel@tonic-gate (void) strcpy(date, "-"); 3497c478bd9Sstevel@tonic-gate } 3507c478bd9Sstevel@tonic-gate 3517c478bd9Sstevel@tonic-gate /* 3527c478bd9Sstevel@tonic-gate * Create and log the final string by filling in the template with the 3537c478bd9Sstevel@tonic-gate * strings we've created and the strings from the message dictionary. 3547c478bd9Sstevel@tonic-gate * If a template is provided for this dictionary, use it, otherwise 3557c478bd9Sstevel@tonic-gate * fall back to the default template. 3567c478bd9Sstevel@tonic-gate */ 3577c478bd9Sstevel@tonic-gate if ((template = dgettext(dict, SYSLOG_TEMPLATE)) == SYSLOG_TEMPLATE) 3587c478bd9Sstevel@tonic-gate template = dgettext(SYSLOG_DOMAIN, SYSLOG_TEMPLATE); 3597c478bd9Sstevel@tonic-gate 360627351e3Scy syslog_ctl.pri &= LOG_FACMASK; 361627351e3Scy if (strcmp(class, FM_LIST_REPAIRED_CLASS) == 0) 362627351e3Scy syslog_ctl.pri |= LOG_NOTICE; 363627351e3Scy else 364627351e3Scy syslog_ctl.pri |= LOG_ERR; 365*78432d5eScy syslog_emit(hdl, template, code, dgettext(dict, typ), 3667c478bd9Sstevel@tonic-gate dgettext(dict, sev), date, platform, chassis, server, src_name, 3677c478bd9Sstevel@tonic-gate src_vers, uuid, desc, dgettext(dict, rsp), dgettext(dict, imp), 3687c478bd9Sstevel@tonic-gate dgettext(dict, act)); 3697c478bd9Sstevel@tonic-gate 3707c478bd9Sstevel@tonic-gate /* 3717c478bd9Sstevel@tonic-gate * Switch back to our original language if we had to fall back to C. 3727c478bd9Sstevel@tonic-gate */ 3737c478bd9Sstevel@tonic-gate if (olang != NULL) 3747c478bd9Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, olang); 375e1c679faSaf 376e1c679faSaf if (syslog_locdir != NULL) 377e1c679faSaf (void) bindtextdomain(dict, locdir); 378e1c679faSaf 379e1c679faSaf fmd_msg_unlock(); 380e1c679faSaf 381e1c679faSaf if (locale_c) { 382e1c679faSaf fmd_hdl_debug(hdl, 383e1c679faSaf trfmt == fmt ? 384e1c679faSaf "dgettext(%s, %s) in %s and C failed\n" : 385e1c679faSaf "dgettext(%s, %s) in %s failed; C used\n", 386e1c679faSaf dict, fmt, olang ? olang : "<null>"); 387e1c679faSaf } 3887c478bd9Sstevel@tonic-gate } 3897c478bd9Sstevel@tonic-gate 3907c478bd9Sstevel@tonic-gate static const fmd_prop_t fmd_props[] = { 3917c478bd9Sstevel@tonic-gate { "console", FMD_TYPE_BOOL, "true" }, 3927c478bd9Sstevel@tonic-gate { "facility", FMD_TYPE_STRING, "LOG_DAEMON" }, 3937c478bd9Sstevel@tonic-gate { "gmt", FMD_TYPE_BOOL, "false" }, 3947c478bd9Sstevel@tonic-gate { "syslogd", FMD_TYPE_BOOL, "true" }, 3957c478bd9Sstevel@tonic-gate { "url", FMD_TYPE_STRING, "http://sun.com/msg/" }, 3967c478bd9Sstevel@tonic-gate { "message_all", FMD_TYPE_BOOL, "false" }, 3977c478bd9Sstevel@tonic-gate { NULL, 0, NULL } 3987c478bd9Sstevel@tonic-gate }; 3997c478bd9Sstevel@tonic-gate 4007c478bd9Sstevel@tonic-gate static const fmd_hdl_ops_t fmd_ops = { 4017c478bd9Sstevel@tonic-gate syslog_recv, /* fmdo_recv */ 4027c478bd9Sstevel@tonic-gate NULL, /* fmdo_timeout */ 4037c478bd9Sstevel@tonic-gate NULL, /* fmdo_close */ 4047c478bd9Sstevel@tonic-gate NULL, /* fmdo_stats */ 4057c478bd9Sstevel@tonic-gate NULL, /* fmdo_gc */ 4067c478bd9Sstevel@tonic-gate }; 4077c478bd9Sstevel@tonic-gate 4087c478bd9Sstevel@tonic-gate static const fmd_hdl_info_t fmd_info = { 4097c478bd9Sstevel@tonic-gate "Syslog Messaging Agent", "1.0", &fmd_ops, fmd_props 4107c478bd9Sstevel@tonic-gate }; 4117c478bd9Sstevel@tonic-gate 4127c478bd9Sstevel@tonic-gate void 4137c478bd9Sstevel@tonic-gate _fmd_init(fmd_hdl_t *hdl) 4147c478bd9Sstevel@tonic-gate { 4157c478bd9Sstevel@tonic-gate const struct facility *fp; 416e1c679faSaf char *facname, *tz, *rootdir, *locdir, *locale, *p; 4177c478bd9Sstevel@tonic-gate 4187c478bd9Sstevel@tonic-gate if (fmd_hdl_register(hdl, FMD_API_VERSION, &fmd_info) != 0) 4197c478bd9Sstevel@tonic-gate return; /* invalid data in configuration file */ 4207c478bd9Sstevel@tonic-gate 4217c478bd9Sstevel@tonic-gate (void) fmd_stat_create(hdl, FMD_STAT_NOALLOC, sizeof (syslog_stats) / 4227c478bd9Sstevel@tonic-gate sizeof (fmd_stat_t), (fmd_stat_t *)&syslog_stats); 4237c478bd9Sstevel@tonic-gate 4247c478bd9Sstevel@tonic-gate if ((syslog_logfd = open("/dev/conslog", O_WRONLY | O_NOCTTY)) == -1) 4257c478bd9Sstevel@tonic-gate fmd_hdl_abort(hdl, "syslog-msgs failed to open /dev/conslog"); 4267c478bd9Sstevel@tonic-gate 4277c478bd9Sstevel@tonic-gate if ((syslog_msgfd = open("/dev/sysmsg", O_WRONLY | O_NOCTTY)) == -1) 4287c478bd9Sstevel@tonic-gate fmd_hdl_abort(hdl, "syslog-msgs failed to open /dev/sysmsg"); 4297c478bd9Sstevel@tonic-gate 4307c478bd9Sstevel@tonic-gate /* 4317c478bd9Sstevel@tonic-gate * All FMA event dictionaries use msgfmt(1) message objects to produce 4327c478bd9Sstevel@tonic-gate * messages, even for the C locale. We therefore want to use dgettext 4337c478bd9Sstevel@tonic-gate * for all message lookups, but its defined behavior in the C locale is 4347c478bd9Sstevel@tonic-gate * to return the input string. Since our input strings are event codes 4357c478bd9Sstevel@tonic-gate * and not format strings, this doesn't help us. We resolve this nit 4367c478bd9Sstevel@tonic-gate * by setting NLSPATH to a non-existent file: the presence of NLSPATH 4377c478bd9Sstevel@tonic-gate * is defined to force dgettext(3C) to do a full lookup even for C. 4387c478bd9Sstevel@tonic-gate */ 4397c478bd9Sstevel@tonic-gate if (getenv("NLSPATH") == NULL && putenv(fmd_hdl_strdup(hdl, 4407c478bd9Sstevel@tonic-gate "NLSPATH=/usr/lib/fm/fmd/fmd.cat", FMD_SLEEP)) != 0) 4417c478bd9Sstevel@tonic-gate fmd_hdl_abort(hdl, "syslog-msgs failed to set NLSPATH"); 4427c478bd9Sstevel@tonic-gate 443e1c679faSaf fmd_msg_lock(); 4447c478bd9Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, ""); 445e1c679faSaf locale = setlocale(LC_MESSAGES, NULL); 446e1c679faSaf if (locale) { 447e1c679faSaf p = alloca(strlen(locale) + 1); 448e1c679faSaf locale = strcpy(p, locale); 449e1c679faSaf } else { 450e1c679faSaf locale = "<null>"; 451e1c679faSaf } 452e1c679faSaf fmd_msg_unlock(); 453e1c679faSaf fmd_hdl_debug(hdl, "locale=%s\n", locale); 4547c478bd9Sstevel@tonic-gate 4557c478bd9Sstevel@tonic-gate /* 4567c478bd9Sstevel@tonic-gate * If the "gmt" property is set to true, force our EVENT-TIME to be 4577c478bd9Sstevel@tonic-gate * reported in GMT time; otherwise we use localtime. tzset() affects 4587c478bd9Sstevel@tonic-gate * the results of subsequent calls to strftime(3C) above. 4597c478bd9Sstevel@tonic-gate */ 4607c478bd9Sstevel@tonic-gate if (fmd_prop_get_int32(hdl, "gmt") == FMD_B_TRUE && 4617c478bd9Sstevel@tonic-gate ((tz = getenv("TZ")) == NULL || strcmp(tz, "GMT") != 0)) { 4627c478bd9Sstevel@tonic-gate (void) putenv(fmd_hdl_strdup(hdl, "TZ=GMT", FMD_SLEEP)); 4637c478bd9Sstevel@tonic-gate tzset(); /* reload env */ 4647c478bd9Sstevel@tonic-gate } 4657c478bd9Sstevel@tonic-gate 4667c478bd9Sstevel@tonic-gate /* 4677c478bd9Sstevel@tonic-gate * Look up the value of the "facility" property and use it to determine 4687c478bd9Sstevel@tonic-gate * what syslog LOG_* facility value we use to fill in our log_ctl_t. 4697c478bd9Sstevel@tonic-gate * The details of our logging method are described above syslog_emit(). 4707c478bd9Sstevel@tonic-gate */ 4717c478bd9Sstevel@tonic-gate facname = fmd_prop_get_string(hdl, "facility"); 4727c478bd9Sstevel@tonic-gate 4737c478bd9Sstevel@tonic-gate for (fp = syslog_facs; fp->fac_name != NULL; fp++) { 4747c478bd9Sstevel@tonic-gate if (strcmp(fp->fac_name, facname) == 0) 4757c478bd9Sstevel@tonic-gate break; 4767c478bd9Sstevel@tonic-gate } 4777c478bd9Sstevel@tonic-gate 4787c478bd9Sstevel@tonic-gate if (fp->fac_name == NULL) 4797c478bd9Sstevel@tonic-gate fmd_hdl_abort(hdl, "invalid 'facility' setting: %s\n", facname); 4807c478bd9Sstevel@tonic-gate 4817c478bd9Sstevel@tonic-gate fmd_prop_free_string(hdl, facname); 482627351e3Scy syslog_ctl.pri = fp->fac_value; 4837c478bd9Sstevel@tonic-gate syslog_ctl.flags = SL_CONSOLE | SL_LOGONLY; 4847c478bd9Sstevel@tonic-gate 4857c478bd9Sstevel@tonic-gate /* 4867c478bd9Sstevel@tonic-gate * Cache any properties we use every time we receive an event and 4877c478bd9Sstevel@tonic-gate * subscribe to list.suspect events regardless of the .conf file. 4887c478bd9Sstevel@tonic-gate */ 4897c478bd9Sstevel@tonic-gate syslog_file = fmd_prop_get_int32(hdl, "syslogd"); 4907c478bd9Sstevel@tonic-gate syslog_cons = fmd_prop_get_int32(hdl, "console"); 4917c478bd9Sstevel@tonic-gate syslog_url = fmd_prop_get_string(hdl, "url"); 4927c478bd9Sstevel@tonic-gate syslog_msgall = fmd_prop_get_int32(hdl, "message_all"); 4937c478bd9Sstevel@tonic-gate 4947c478bd9Sstevel@tonic-gate /* 4957c478bd9Sstevel@tonic-gate * If fmd's rootdir property is set to a non-default root, then we are 4967c478bd9Sstevel@tonic-gate * going to need to rebind the text domains we use for dgettext() as 4977c478bd9Sstevel@tonic-gate * we go. Look up the default l10n messages directory and make 4987c478bd9Sstevel@tonic-gate * syslog_locdir be this path with fmd.rootdir prepended to it. 4997c478bd9Sstevel@tonic-gate */ 5007c478bd9Sstevel@tonic-gate rootdir = fmd_prop_get_string(hdl, "fmd.rootdir"); 5017c478bd9Sstevel@tonic-gate 502e1c679faSaf if (*rootdir != '\0' && strcmp(rootdir, "/") != 0) { 503e1c679faSaf fmd_msg_lock(); 504e1c679faSaf locdir = bindtextdomain(SYSLOG_DOMAIN, NULL); 505e1c679faSaf fmd_msg_unlock(); 506e1c679faSaf if (locdir != NULL) { 507e1c679faSaf size_t len = strlen(rootdir) + strlen(locdir) + 1; 508e1c679faSaf syslog_locdir = fmd_hdl_alloc(hdl, len, FMD_SLEEP); 509e1c679faSaf (void) snprintf(syslog_locdir, len, "%s%s", rootdir, 510e1c679faSaf locdir); 511e1c679faSaf fmd_hdl_debug(hdl, 512e1c679faSaf "binding textdomain to %s for syslog\n", 513e1c679faSaf syslog_locdir); 514e1c679faSaf } 5157c478bd9Sstevel@tonic-gate } 5167c478bd9Sstevel@tonic-gate 5177c478bd9Sstevel@tonic-gate fmd_prop_free_string(hdl, rootdir); 5187c478bd9Sstevel@tonic-gate fmd_hdl_subscribe(hdl, FM_LIST_SUSPECT_CLASS); 519627351e3Scy fmd_hdl_subscribe(hdl, FM_LIST_REPAIRED_CLASS); 5207c478bd9Sstevel@tonic-gate } 5217c478bd9Sstevel@tonic-gate 5227c478bd9Sstevel@tonic-gate void 5237c478bd9Sstevel@tonic-gate _fmd_fini(fmd_hdl_t *hdl) 5247c478bd9Sstevel@tonic-gate { 5257c478bd9Sstevel@tonic-gate fmd_hdl_strfree(hdl, syslog_locdir); 5267c478bd9Sstevel@tonic-gate fmd_prop_free_string(hdl, syslog_url); 5277c478bd9Sstevel@tonic-gate 5287c478bd9Sstevel@tonic-gate (void) close(syslog_logfd); 5297c478bd9Sstevel@tonic-gate (void) close(syslog_msgfd); 5307c478bd9Sstevel@tonic-gate } 531