/*
* Copyright (c) 1983, 1988, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
static void
disconnectlog_r(struct syslog_data *data)
{
/*
* If the user closed the FD and opened another in the same slot,
* that's their problem. They should close it before calling on
* system services.
*/
if (data->log_file != -1) {
(void)close(data->log_file);
data->log_file = -1;
}
data->log_connected = 0; /* retry connect */
}
static void
connectlog_r(struct syslog_data *data)
{
/* AF_UNIX address of local logger */
static const struct sockaddr_un sun = {
.sun_family = AF_LOCAL,
.sun_len = sizeof(sun),
.sun_path = _PATH_LOG,
};
/*
* We wouldn't need this mess if printf handled %m, or if
* strerror() had been invented before syslog().
*/
for (t = fmt_cpy, fmt_left = FMT_LEN; (ch = *fmt) != '\0'; ++fmt) {
if (ch == '%' && fmt[1] == 'm') {
char buf[256];
/* Get connected, output the message to the local logger. */
(*fun->lock)(data);
opened = !data->log_opened;
if (opened)
_openlog_unlocked_r(data->log_tag, data->log_stat, 0, data);
connectlog_r(data);
/*
* If the send() failed, there are two likely scenarios:
* 1) syslogd was restarted
* 2) /dev/log is out of socket buffer space
* We attempt to reconnect to /dev/log to take care of
* case #1 and keep send()ing data to cover case #2
* to give syslogd a chance to empty its socket buffer.
*/
for (tries = 0; tries < MAXTRIES; tries++) {
if (send(data->log_file, tbuf, cnt, 0) != -1)
break;
if (errno != ENOBUFS) {
disconnectlog_r(data);
connectlog_r(data);
} else
(void)usleep(1);
}
/*
* Output the message to the console; try not to block
* as a blocking console should not stop other processes.
* Make sure the error reported is the one from the syslogd failure.
*/
if (tries == MAXTRIES && (data->log_stat & LOG_CONS) &&
(fd = open(_PATH_CONSOLE,
O_WRONLY | O_NONBLOCK | O_CLOEXEC, 0)) >= 0) {
iov[iovcnt].iov_base = __UNCONST(CRLF);
iov[iovcnt].iov_len = 2;
(void)writev(fd, iov, iovcnt + 1);
(void)close(fd);
}
out:
if (!(*fun->unlock)(data) && opened)
_closelog_unlocked_r(data);
}
int
setlogmask_r(int pmask, struct syslog_data *data)
{
int omask;