syslog.c revision 78432d5ed5122db8b0bbfa41e00d063bf8ae7171
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stropts.h>
#include <syslog.h>
#include <locale.h>
#include <strings.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <alloca.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>
/*
* SYSLOG_DOMAIN and SYSLOG_TEMPLATE define the dgettext() parameters the agent
* can use to retrieve the localized format string for diagnosis messages.
* The format string retrieved from SYSLOG_DOMAIN is the default format
* string, but when processing each suspect list, dgettext() is also called
* for the domain that matches the diagcode dictname and if SYSLOG_TEMPLATE
* is defined, it overrides the default for that suspect list only.
*
* Similarly, SYSLOG_URL is also checked to see if syslog_url
* should be overridden for each suspect list.
*
* The net effect of all this is that for a given diagcode DICT-1234-56:
*
* - If DICT.mo defines syslog-msgs-message-template, it is used
* as the format string for the diagnosis message.
*
* - Otherwise, syslog-msgs-message-template from FMD.mo is used.
*
* - If DICT.mo defines syslog-url, it is used when filling
* in the %s in the "description" message.
*
* - Otherwise, if syslog-msgs.conf defines a "url" property, that
* value is used.
*
* - Otherwise, the default "http://sun.com/msg/" is used (via the
* fmd_props[] table defined in this file).
*/
static const char SYSLOG_DOMAIN[] = "FMD";
static const char SYSLOG_TEMPLATE[] = "syslog-msgs-message-template";
static const char SYSLOG_URL[] = "syslog-url";
static struct stats {
} syslog_stats = {
};
static const struct facility {
const char *fac_name;
int fac_value;
} syslog_facs[] = {
{ "LOG_DAEMON", LOG_DAEMON },
{ "LOG_LOCAL0", LOG_LOCAL0 },
{ "LOG_LOCAL1", LOG_LOCAL1 },
{ "LOG_LOCAL2", LOG_LOCAL2 },
{ "LOG_LOCAL3", LOG_LOCAL3 },
{ "LOG_LOCAL4", LOG_LOCAL4 },
{ "LOG_LOCAL5", LOG_LOCAL5 },
{ "LOG_LOCAL6", LOG_LOCAL6 },
{ "LOG_LOCAL7", LOG_LOCAL7 },
{ NULL, 0 }
};
static char *syslog_locdir; /* l10n messages directory (if alternate) */
static char *syslog_url; /* current value of "url" property */
static int syslog_msgall; /* set to message all faults */
static int syslog_file; /* log to syslog_logfd */
static int syslog_cons; /* log to syslog_msgfd */
/*
* Ideally we would just use syslog(3C) for outputting our messages, but our
* messaging standard defines a nice multi-line format and syslogd(1M) is very
* inflexible and stupid when it comes to multi-line messages. It pulls data
* out of log(7D) and splits it up by \n, printing each line to the console
* with its usual prefix of date and sender; it uses the same behavior for the
* messages file as well. Further, syslog(3C) provides no CE_CONT equivalent
* for userland callers (which at least works around repeated file prefixing).
* So with a multi-line message format, your file and console end up like this:
*
* Dec 02 18:08:40 hostname this is my nicely formatted
* Dec 02 18:08:40 hostname message designed for 80 cols
* ...
*
* To resolve these issues, we use our own syslog_emit() wrapper to emit
* messages and some knowledge of how the Solaris log drivers work. We first
* construct an enlarged format string containing the appropriate msgid(1).
* We then format the caller's message using the provided format and buffer.
* We send this message to log(7D) using putmsg() with SL_CONSOLE | SL_LOGONLY
* set in the log_ctl_t. The log driver allows us to set SL_LOGONLY when we
* construct messages ourself, indicating that syslogd should only emit the
* Then we emit the message a second time, without the special prefix, to the
* sysmsg(7D) device, which handles console redirection and also permits us
* to output any characters we like to the console, including \n and \r.
*/
/*PRINTFLIKE2*/
static void
{
char *format, c;
int len;
/*
* Figure out the length of the message then allocate a buffer
* of adequate size.
*/
return;
}
}
}
/*ARGSUSED*/
static void
{
int locale_c = 0;
return; /* invalid event version */
}
return; /* event is not to be messaged */
}
/*
* Extract the DE element, which is an FMRI for the diagnosis engine
* that made this event, and validate its meta-data before continuing.
*/
strcmp(p, FM_FMRI_SCHEME_FMD) != 0 ||
return; /* invalid de fmri */
}
/*
* Extract the relevant identifying elements of the FMRI and authority.
* Note: for now, we ignore FM_FMRI_AUTH_DOMAIN (only for SPs).
*/
/*
* Extract the uuid and diagcode dictionary from the event code. The
* dictionary name is the text preceding the first "-" in the code.
*/
return; /* invalid diagnosis code */
}
/*
* Alloca a hunk of memory and use it to create the msgid strings
* <code>.type, <code>.severity, <code>.description, and so forth.
* These form the msgids we will use to look up the localized text.
* Since we've allocated things to be of the right size, we know
* than snprintf() can't overflow: INT_MAX is used shut lint up and
* avoid code to needlessly recompute the remaining buffer space.
*/
fmd_msg_lock();
if (syslog_locdir != NULL)
/*
* We didn't find a translation in the dictionary for the
* current language. The string we passed to gettext is merely
* an index - it isn't sufficient, on its own, to be used as the
* message. Fall back to C and try again.
*/
if (olang) {
}
locale_c = 1;
}
url = syslog_url;
/*
* If the URL ends with a slash, that indicates the code should be
* appended to it. After formatting the URL, reformat the DESC
* text using the URL as an snprintf argument.
*/
} else {
}
/* LINTED - variable format specifier to snprintf() */
/*
* Extract the diagnosis time and format it using the locale's default.
* strftime() will use GMT or local time based on our "gmt" setting.
*/
else {
}
/*
* Create and log the final string by filling in the template with the
* strings we've created and the strings from the message dictionary.
* If a template is provided for this dictionary, use it, otherwise
* fall back to the default template.
*/
else
/*
* Switch back to our original language if we had to fall back to C.
*/
if (syslog_locdir != NULL)
if (locale_c) {
"dgettext(%s, %s) in %s and C failed\n" :
"dgettext(%s, %s) in %s failed; C used\n",
}
}
static const fmd_prop_t fmd_props[] = {
};
static const fmd_hdl_ops_t fmd_ops = {
syslog_recv, /* fmdo_recv */
NULL, /* fmdo_timeout */
NULL, /* fmdo_close */
NULL, /* fmdo_stats */
NULL, /* fmdo_gc */
};
static const fmd_hdl_info_t fmd_info = {
};
void
{
return; /* invalid data in configuration file */
/*
* All FMA event dictionaries use msgfmt(1) message objects to produce
* messages, even for the C locale. We therefore want to use dgettext
* for all message lookups, but its defined behavior in the C locale is
* to return the input string. Since our input strings are event codes
* and not format strings, this doesn't help us. We resolve this nit
* by setting NLSPATH to a non-existent file: the presence of NLSPATH
* is defined to force dgettext(3C) to do a full lookup even for C.
*/
fmd_msg_lock();
if (locale) {
} else {
locale = "<null>";
}
/*
* If the "gmt" property is set to true, force our EVENT-TIME to be
* reported in GMT time; otherwise we use localtime. tzset() affects
* the results of subsequent calls to strftime(3C) above.
*/
tzset(); /* reload env */
}
/*
* Look up the value of the "facility" property and use it to determine
* what syslog LOG_* facility value we use to fill in our log_ctl_t.
* The details of our logging method are described above syslog_emit().
*/
break;
}
/*
* Cache any properties we use every time we receive an event and
* subscribe to list.suspect events regardless of the .conf file.
*/
/*
* If fmd's rootdir property is set to a non-default root, then we are
* going to need to rebind the text domains we use for dgettext() as
* we go. Look up the default l10n messages directory and make
* syslog_locdir be this path with fmd.rootdir prepended to it.
*/
fmd_msg_lock();
locdir);
"binding textdomain to %s for syslog\n",
}
}
}
void
{
(void) close(syslog_logfd);
(void) close(syslog_msgfd);
}