syslog.c revision b695575577bae0337af339d76949713bfe1c9013
/*
* 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 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <stropts.h>
#include <strings.h>
#include <syslog.h>
#include <alloca.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
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 int syslog_msgall; /* set to message all faults */
static int syslog_file; /* log to syslog_logfd */
static int syslog_cons; /* log to syslog_msgfd */
static const char SYSLOG_POINTER[] = "syslog-msgs-pointer";
/*
* 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.
*/
static void
{
char *buf;
const char *format = "fmd: [ID %u FACILITY_AND_PRIORITY] %s";
/*
* The underlying log driver won't accept messages longer than
* LOG_MAXPS bytes. Therefore, messages which exceed this limit will
* be truncated and appended with a pointer to the full message.
*/
char *syslog_pointer, *p;
SYSLOG_POINTER)) == NULL) {
/*
* This shouldn't happen, but if it does we'll just
* truncate the message.
*/
} else {
/*
* If possible, the pointer is appended after a newline
*/
(void) strcpy(p, syslog_pointer);
}
}
}
}
}
/*ARGSUSED*/
static void
{
char *msg;
return; /* invalid event version */
}
return; /* event is not to be messaged */
}
return; /* libfmd_msg error */
}
}
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 */
/*
* 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 (syslog_msghdl == NULL)
}
/*ARGSUSED*/
void
{
(void) close(syslog_logfd);
(void) close(syslog_msgfd);
}