2017-11-20 19:49:47 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
1994-05-27 05:00:24 +00:00
|
|
|
* 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.
|
2017-02-28 23:42:47 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-27 05:00:24 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2002-02-01 01:32:19 +00:00
|
|
|
#include <sys/cdefs.h>
|
2018-07-03 17:31:45 +00:00
|
|
|
__SCCSID("@(#)syslog.c 8.5 (Berkeley) 4/29/95");
|
2002-02-01 01:32:19 +00:00
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2001-01-24 13:01:12 +00:00
|
|
|
#include "namespace.h"
|
2018-04-06 13:00:45 +00:00
|
|
|
#include <sys/param.h>
|
1994-05-27 05:00:24 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/syslog.h>
|
2018-04-06 13:00:45 +00:00
|
|
|
#include <sys/time.h>
|
1994-05-27 05:00:24 +00:00
|
|
|
#include <sys/uio.h>
|
1998-03-05 22:17:59 +00:00
|
|
|
#include <sys/un.h>
|
1994-05-27 05:00:24 +00:00
|
|
|
#include <netdb.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <paths.h>
|
2004-12-30 16:05:11 +00:00
|
|
|
#include <pthread.h>
|
1994-05-27 05:00:24 +00:00
|
|
|
#include <stdio.h>
|
2002-03-29 22:43:43 +00:00
|
|
|
#include <stdlib.h>
|
1994-05-27 05:00:24 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
2001-01-24 13:01:12 +00:00
|
|
|
#include "un-namespace.h"
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2002-03-29 22:43:43 +00:00
|
|
|
#include "libc_private.h"
|
|
|
|
|
2021-03-23 16:01:15 +00:00
|
|
|
/* Maximum number of characters of syslog message */
|
|
|
|
#define MAXLINE 8192
|
|
|
|
|
1994-05-27 05:00:24 +00:00
|
|
|
static int LogFile = -1; /* fd for log */
|
2004-11-04 23:09:57 +00:00
|
|
|
static int status; /* connection status */
|
1996-03-02 19:56:16 +00:00
|
|
|
static int opened; /* have done openlog() */
|
1994-05-27 05:00:24 +00:00
|
|
|
static int LogStat = 0; /* status bits, set by openlog() */
|
|
|
|
static const char *LogTag = NULL; /* string to tag the entry with */
|
|
|
|
static int LogFacility = LOG_USER; /* default facility code */
|
|
|
|
static int LogMask = 0xff; /* mask of priorities to be logged */
|
2004-12-30 16:05:11 +00:00
|
|
|
static pthread_mutex_t syslog_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
|
|
|
#define THREAD_LOCK() \
|
|
|
|
do { \
|
|
|
|
if (__isthreaded) _pthread_mutex_lock(&syslog_mutex); \
|
|
|
|
} while(0)
|
|
|
|
#define THREAD_UNLOCK() \
|
|
|
|
do { \
|
|
|
|
if (__isthreaded) _pthread_mutex_unlock(&syslog_mutex); \
|
|
|
|
} while(0)
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2020-08-14 00:18:18 +00:00
|
|
|
/* RFC5424 defined value. */
|
|
|
|
#define NILVALUE "-"
|
|
|
|
|
2002-02-01 01:32:19 +00:00
|
|
|
static void disconnectlog(void); /* disconnect from syslogd */
|
|
|
|
static void connectlog(void); /* (re)connect to syslogd */
|
2004-12-30 16:05:11 +00:00
|
|
|
static void openlog_unlocked(const char *, int, int);
|
1996-03-02 19:56:16 +00:00
|
|
|
|
2004-11-04 23:09:57 +00:00
|
|
|
enum {
|
|
|
|
NOCONN = 0,
|
|
|
|
CONNDEF,
|
|
|
|
CONNPRIV,
|
|
|
|
};
|
|
|
|
|
1995-09-15 13:53:39 +00:00
|
|
|
/*
|
|
|
|
* Format of the magic cookie passed through the stdio hook
|
|
|
|
*/
|
|
|
|
struct bufcookie {
|
|
|
|
char *base; /* start of buffer */
|
|
|
|
int left;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* stdio write hook for writing to a static string buffer
|
|
|
|
* XXX: Maybe one day, dynamically allocate it so that the line length
|
|
|
|
* is `unlimited'.
|
|
|
|
*/
|
2005-04-08 21:15:38 +00:00
|
|
|
static int
|
|
|
|
writehook(void *cookie, const char *buf, int len)
|
1995-09-15 13:53:39 +00:00
|
|
|
{
|
|
|
|
struct bufcookie *h; /* private `handle' */
|
|
|
|
|
|
|
|
h = (struct bufcookie *)cookie;
|
|
|
|
if (len > h->left) {
|
|
|
|
/* clip in case of wraparound */
|
|
|
|
len = h->left;
|
|
|
|
}
|
|
|
|
if (len > 0) {
|
|
|
|
(void)memcpy(h->base, buf, len); /* `write' it. */
|
|
|
|
h->base += len;
|
|
|
|
h->left -= len;
|
|
|
|
}
|
2006-08-07 20:12:18 +00:00
|
|
|
return len;
|
1995-09-15 13:53:39 +00:00
|
|
|
}
|
|
|
|
|
1994-05-27 05:00:24 +00:00
|
|
|
/*
|
|
|
|
* syslog, vsyslog --
|
|
|
|
* print message on log file; output is intended for syslogd(8).
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
syslog(int pri, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsyslog(pri, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2017-06-17 11:29:59 +00:00
|
|
|
static void
|
|
|
|
vsyslog1(int pri, const char *fmt, va_list ap)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2018-04-06 13:00:45 +00:00
|
|
|
struct timeval now;
|
|
|
|
struct tm tm;
|
2002-02-01 01:32:19 +00:00
|
|
|
char ch, *p;
|
2018-04-06 13:00:45 +00:00
|
|
|
long tz_offset;
|
|
|
|
int cnt, fd, saved_errno;
|
2021-03-23 16:01:15 +00:00
|
|
|
char hostname[MAXHOSTNAMELEN], *stdp, tbuf[MAXLINE], fmt_cpy[MAXLINE],
|
2018-04-06 13:00:45 +00:00
|
|
|
errstr[64], tz_sign;
|
1995-09-15 13:53:39 +00:00
|
|
|
FILE *fp, *fmt_fp;
|
|
|
|
struct bufcookie tbuf_cookie;
|
|
|
|
struct bufcookie fmt_cookie;
|
1994-05-27 05:00:24 +00:00
|
|
|
|
|
|
|
#define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
|
|
|
|
/* Check for invalid bits. */
|
|
|
|
if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
|
|
|
|
syslog(INTERNALLOG,
|
|
|
|
"syslog: unknown facility/priority: %x", pri);
|
|
|
|
pri &= LOG_PRIMASK|LOG_FACMASK;
|
|
|
|
}
|
|
|
|
|
2006-05-11 09:10:33 +00:00
|
|
|
saved_errno = errno;
|
|
|
|
|
1994-05-27 05:00:24 +00:00
|
|
|
/* Check priority against setlogmask values. */
|
2017-06-17 11:29:59 +00:00
|
|
|
if (!(LOG_MASK(LOG_PRI(pri)) & LogMask))
|
1994-05-27 05:00:24 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* Set default facility if none specified. */
|
|
|
|
if ((pri & LOG_FACMASK) == 0)
|
|
|
|
pri |= LogFacility;
|
|
|
|
|
1995-09-15 13:53:39 +00:00
|
|
|
/* Create the primary stdio hook */
|
|
|
|
tbuf_cookie.base = tbuf;
|
|
|
|
tbuf_cookie.left = sizeof(tbuf);
|
|
|
|
fp = fwopen(&tbuf_cookie, writehook);
|
2017-06-17 11:29:59 +00:00
|
|
|
if (fp == NULL)
|
1995-09-15 13:53:39 +00:00
|
|
|
return;
|
|
|
|
|
2018-04-06 13:00:45 +00:00
|
|
|
/* Build the message according to RFC 5424. Tag and version. */
|
|
|
|
(void)fprintf(fp, "<%d>1 ", pri);
|
|
|
|
/* Timestamp similar to RFC 3339. */
|
|
|
|
if (gettimeofday(&now, NULL) == 0 &&
|
|
|
|
localtime_r(&now.tv_sec, &tm) != NULL) {
|
|
|
|
if (tm.tm_gmtoff < 0) {
|
|
|
|
tz_sign = '-';
|
|
|
|
tz_offset = -tm.tm_gmtoff;
|
|
|
|
} else {
|
|
|
|
tz_sign = '+';
|
|
|
|
tz_offset = tm.tm_gmtoff;
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)fprintf(fp,
|
|
|
|
"%04d-%02d-%02d" /* Date. */
|
|
|
|
"T%02d:%02d:%02d.%06ld" /* Time. */
|
|
|
|
"%c%02ld:%02ld ", /* Time zone offset. */
|
|
|
|
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
|
|
|
|
tm.tm_hour, tm.tm_min, tm.tm_sec, now.tv_usec,
|
|
|
|
tz_sign, tz_offset / 3600, (tz_offset % 3600) / 60);
|
|
|
|
} else
|
2020-08-14 00:18:18 +00:00
|
|
|
(void)fputs(NILVALUE " ", fp);
|
2018-04-06 13:00:45 +00:00
|
|
|
/* Hostname. */
|
|
|
|
(void)gethostname(hostname, sizeof(hostname));
|
2020-08-14 00:18:18 +00:00
|
|
|
(void)fprintf(fp, "%s ",
|
|
|
|
hostname[0] == '\0' ? NILVALUE : hostname);
|
1995-09-15 13:53:39 +00:00
|
|
|
if (LogStat & LOG_PERROR) {
|
|
|
|
/* Transfer to string buffer */
|
|
|
|
(void)fflush(fp);
|
|
|
|
stdp = tbuf + (sizeof(tbuf) - tbuf_cookie.left);
|
|
|
|
}
|
2020-08-14 00:18:18 +00:00
|
|
|
/* Application name. */
|
|
|
|
if (LogTag == NULL)
|
|
|
|
LogTag = _getprogname();
|
|
|
|
(void)fprintf(fp, "%s ", LogTag == NULL ? NILVALUE : LogTag);
|
2018-04-06 13:00:45 +00:00
|
|
|
/*
|
|
|
|
* Provide the process ID regardless of whether LOG_PID has been
|
|
|
|
* specified, as it provides valuable information. Many
|
|
|
|
* applications tend not to use this, even though they should.
|
|
|
|
*/
|
2020-08-14 00:18:18 +00:00
|
|
|
(void)fprintf(fp, "%d ", getpid());
|
|
|
|
/* Message ID. */
|
|
|
|
(void)fputs(NILVALUE " ", fp);
|
|
|
|
/* Structured data. */
|
|
|
|
(void)fputs(NILVALUE " ", fp);
|
1995-09-15 13:53:39 +00:00
|
|
|
|
|
|
|
/* Check to see if we can skip expanding the %m */
|
|
|
|
if (strstr(fmt, "%m")) {
|
|
|
|
|
|
|
|
/* Create the second stdio hook */
|
|
|
|
fmt_cookie.base = fmt_cpy;
|
|
|
|
fmt_cookie.left = sizeof(fmt_cpy) - 1;
|
|
|
|
fmt_fp = fwopen(&fmt_cookie, writehook);
|
|
|
|
if (fmt_fp == NULL) {
|
|
|
|
fclose(fp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-02-10 08:31:28 +00:00
|
|
|
/*
|
|
|
|
* Substitute error message for %m. Be careful not to
|
|
|
|
* molest an escaped percent "%%m". We want to pass it
|
|
|
|
* on untouched as the format is later parsed by vfprintf.
|
|
|
|
*/
|
|
|
|
for ( ; (ch = *fmt); ++fmt) {
|
1995-09-15 13:53:39 +00:00
|
|
|
if (ch == '%' && fmt[1] == 'm') {
|
|
|
|
++fmt;
|
2004-12-30 16:05:11 +00:00
|
|
|
strerror_r(saved_errno, errstr, sizeof(errstr));
|
|
|
|
fputs(errstr, fmt_fp);
|
2003-02-10 08:31:28 +00:00
|
|
|
} else if (ch == '%' && fmt[1] == '%') {
|
|
|
|
++fmt;
|
|
|
|
fputc(ch, fmt_fp);
|
1995-09-15 13:53:39 +00:00
|
|
|
fputc(ch, fmt_fp);
|
2003-02-10 08:31:28 +00:00
|
|
|
} else {
|
|
|
|
fputc(ch, fmt_fp);
|
|
|
|
}
|
|
|
|
}
|
1995-09-15 13:53:39 +00:00
|
|
|
|
|
|
|
/* Null terminate if room */
|
|
|
|
fputc(0, fmt_fp);
|
|
|
|
fclose(fmt_fp);
|
|
|
|
|
|
|
|
/* Guarantee null termination */
|
|
|
|
fmt_cpy[sizeof(fmt_cpy) - 1] = '\0';
|
|
|
|
|
|
|
|
fmt = fmt_cpy;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
2020-08-14 00:18:18 +00:00
|
|
|
/* Message. */
|
1995-09-15 13:53:39 +00:00
|
|
|
(void)vfprintf(fp, fmt, ap);
|
|
|
|
(void)fclose(fp);
|
|
|
|
|
|
|
|
cnt = sizeof(tbuf) - tbuf_cookie.left;
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2004-05-10 17:12:52 +00:00
|
|
|
/* Remove a trailing newline */
|
|
|
|
if (tbuf[cnt - 1] == '\n')
|
|
|
|
cnt--;
|
|
|
|
|
1994-05-27 05:00:24 +00:00
|
|
|
/* Output to stderr if requested. */
|
|
|
|
if (LogStat & LOG_PERROR) {
|
|
|
|
struct iovec iov[2];
|
2002-02-01 01:32:19 +00:00
|
|
|
struct iovec *v = iov;
|
1994-05-27 05:00:24 +00:00
|
|
|
|
|
|
|
v->iov_base = stdp;
|
|
|
|
v->iov_len = cnt - (stdp - tbuf);
|
|
|
|
++v;
|
|
|
|
v->iov_base = "\n";
|
|
|
|
v->iov_len = 1;
|
2001-01-24 13:01:12 +00:00
|
|
|
(void)_writev(STDERR_FILENO, iov, 2);
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get connected, output the message to the local logger. */
|
1996-03-02 19:56:16 +00:00
|
|
|
if (!opened)
|
2004-12-30 16:05:11 +00:00
|
|
|
openlog_unlocked(LogTag, LogStat | LOG_NDELAY, 0);
|
1996-03-02 19:56:16 +00:00
|
|
|
connectlog();
|
|
|
|
|
|
|
|
/*
|
2014-11-25 20:59:22 +00:00
|
|
|
* If the send() fails, there are two likely scenarios:
|
2004-10-08 21:15:21 +00:00
|
|
|
* 1) syslogd was restarted
|
2004-11-04 23:09:57 +00:00
|
|
|
* 2) /var/run/log is out of socket buffer space, which
|
|
|
|
* in most cases means local DoS.
|
2014-11-25 20:59:22 +00:00
|
|
|
* If the error does not indicate a full buffer, we address
|
|
|
|
* case #1 by attempting to reconnect to /var/run/log[priv]
|
|
|
|
* and resending the message once.
|
2004-11-04 23:09:57 +00:00
|
|
|
*
|
2014-11-25 20:59:22 +00:00
|
|
|
* If we are working with a privileged socket, the retry
|
|
|
|
* attempts end there, because we don't want to freeze a
|
2004-11-04 23:09:57 +00:00
|
|
|
* critical application like su(1) or sshd(8).
|
|
|
|
*
|
2014-11-25 20:59:22 +00:00
|
|
|
* Otherwise, we address case #2 by repeatedly retrying the
|
|
|
|
* send() to give syslogd a chance to empty its socket buffer.
|
1996-03-02 19:56:16 +00:00
|
|
|
*/
|
2004-10-08 21:15:21 +00:00
|
|
|
|
|
|
|
if (send(LogFile, tbuf, cnt, 0) < 0) {
|
|
|
|
if (errno != ENOBUFS) {
|
2014-11-25 20:59:22 +00:00
|
|
|
/*
|
|
|
|
* Scenario 1: syslogd was restarted
|
|
|
|
* reconnect and resend once
|
|
|
|
*/
|
2004-10-08 21:15:21 +00:00
|
|
|
disconnectlog();
|
|
|
|
connectlog();
|
2017-06-17 11:29:59 +00:00
|
|
|
if (send(LogFile, tbuf, cnt, 0) >= 0)
|
2014-11-25 20:59:22 +00:00
|
|
|
return;
|
|
|
|
/*
|
|
|
|
* if the resend failed, fall through to
|
|
|
|
* possible scenario 2
|
|
|
|
*/
|
2004-10-08 21:15:21 +00:00
|
|
|
}
|
2014-11-25 20:59:22 +00:00
|
|
|
while (errno == ENOBUFS) {
|
|
|
|
/*
|
|
|
|
* Scenario 2: out of socket buffer space
|
|
|
|
* possible DoS, fail fast on a privileged
|
|
|
|
* socket
|
|
|
|
*/
|
2011-12-02 01:06:33 +00:00
|
|
|
if (status == CONNPRIV)
|
|
|
|
break;
|
2005-08-03 00:45:58 +00:00
|
|
|
_usleep(1);
|
2017-06-17 11:29:59 +00:00
|
|
|
if (send(LogFile, tbuf, cnt, 0) >= 0)
|
2004-12-24 09:48:44 +00:00
|
|
|
return;
|
2014-11-25 20:59:22 +00:00
|
|
|
}
|
2017-06-17 11:29:59 +00:00
|
|
|
} else
|
2004-12-24 09:48:44 +00:00
|
|
|
return;
|
1994-05-27 05:00:24 +00:00
|
|
|
|
|
|
|
/*
|
2002-03-09 11:38:01 +00:00
|
|
|
* 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.
|
1994-05-27 05:00:24 +00:00
|
|
|
*/
|
|
|
|
if (LogStat & LOG_CONS &&
|
2012-09-29 11:54:34 +00:00
|
|
|
(fd = _open(_PATH_CONSOLE, O_WRONLY|O_NONBLOCK|O_CLOEXEC, 0)) >=
|
|
|
|
0) {
|
1995-10-05 00:11:15 +00:00
|
|
|
struct iovec iov[2];
|
2002-02-01 01:32:19 +00:00
|
|
|
struct iovec *v = iov;
|
1995-10-05 00:11:15 +00:00
|
|
|
|
2018-04-06 13:00:45 +00:00
|
|
|
p = strchr(tbuf, '>') + 3;
|
1995-10-05 00:11:15 +00:00
|
|
|
v->iov_base = p;
|
|
|
|
v->iov_len = cnt - (p - tbuf);
|
|
|
|
++v;
|
|
|
|
v->iov_base = "\r\n";
|
|
|
|
v->iov_len = 2;
|
2001-01-24 13:01:12 +00:00
|
|
|
(void)_writev(fd, iov, 2);
|
Simplify sytem call renaming. Instead of _foo() <-- _libc_foo <-- foo(),
just use _foo() <-- foo(). In the case of a libpthread that doesn't do
call conversion (such as linuxthreads and our upcoming libpthread), this
is adequate. In the case of libc_r, we still need three names, which are
now _thread_sys_foo() <-- _foo() <-- foo().
Convert all internal libc usage of: aio_suspend(), close(), fsync(), msync(),
nanosleep(), open(), fcntl(), read(), and write() to _foo() instead of foo().
Remove all internal libc usage of: creat(), pause(), sleep(), system(),
tcdrain(), wait(), and waitpid().
Make thread cancellation fully POSIX-compliant.
Suggested by: deischen
2000-01-27 23:07:25 +00:00
|
|
|
(void)_close(fd);
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
2017-06-17 11:29:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
syslog_cancel_cleanup(void *arg __unused)
|
|
|
|
{
|
2004-12-30 16:05:11 +00:00
|
|
|
|
|
|
|
THREAD_UNLOCK();
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
2004-12-30 16:05:11 +00:00
|
|
|
|
2017-06-17 11:29:59 +00:00
|
|
|
void
|
|
|
|
vsyslog(int pri, const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
|
|
|
|
THREAD_LOCK();
|
|
|
|
pthread_cleanup_push(syslog_cancel_cleanup, NULL);
|
|
|
|
vsyslog1(pri, fmt, ap);
|
|
|
|
pthread_cleanup_pop(1);
|
|
|
|
}
|
|
|
|
|
2004-12-30 16:05:11 +00:00
|
|
|
/* Should be called with mutex acquired */
|
1996-03-02 19:56:16 +00:00
|
|
|
static void
|
2005-04-08 21:15:38 +00:00
|
|
|
disconnectlog(void)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
1996-03-02 19:56:16 +00:00
|
|
|
/*
|
|
|
|
* 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 (LogFile != -1) {
|
Simplify sytem call renaming. Instead of _foo() <-- _libc_foo <-- foo(),
just use _foo() <-- foo(). In the case of a libpthread that doesn't do
call conversion (such as linuxthreads and our upcoming libpthread), this
is adequate. In the case of libc_r, we still need three names, which are
now _thread_sys_foo() <-- _foo() <-- foo().
Convert all internal libc usage of: aio_suspend(), close(), fsync(), msync(),
nanosleep(), open(), fcntl(), read(), and write() to _foo() instead of foo().
Remove all internal libc usage of: creat(), pause(), sleep(), system(),
tcdrain(), wait(), and waitpid().
Make thread cancellation fully POSIX-compliant.
Suggested by: deischen
2000-01-27 23:07:25 +00:00
|
|
|
_close(LogFile);
|
1996-03-02 19:56:16 +00:00
|
|
|
LogFile = -1;
|
|
|
|
}
|
2004-11-04 23:09:57 +00:00
|
|
|
status = NOCONN; /* retry connect */
|
1996-03-02 19:56:16 +00:00
|
|
|
}
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2004-12-30 16:05:11 +00:00
|
|
|
/* Should be called with mutex acquired */
|
1996-03-02 19:56:16 +00:00
|
|
|
static void
|
2005-04-08 21:15:38 +00:00
|
|
|
connectlog(void)
|
1996-03-02 19:56:16 +00:00
|
|
|
{
|
1998-03-05 22:17:59 +00:00
|
|
|
struct sockaddr_un SyslogAddr; /* AF_UNIX address of local logger */
|
1997-03-20 16:28:27 +00:00
|
|
|
|
1994-05-27 05:00:24 +00:00
|
|
|
if (LogFile == -1) {
|
2021-03-23 16:01:15 +00:00
|
|
|
socklen_t len;
|
|
|
|
|
2013-10-20 21:04:44 +00:00
|
|
|
if ((LogFile = _socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC,
|
|
|
|
0)) == -1)
|
1996-03-02 19:56:16 +00:00
|
|
|
return;
|
2021-03-23 16:01:15 +00:00
|
|
|
if (_getsockopt(LogFile, SOL_SOCKET, SO_SNDBUF, &len,
|
|
|
|
&(socklen_t){sizeof(len)}) == 0) {
|
|
|
|
if (len < MAXLINE) {
|
|
|
|
len = MAXLINE;
|
|
|
|
(void)_setsockopt(LogFile, SOL_SOCKET, SO_SNDBUF,
|
|
|
|
&len, sizeof(len));
|
|
|
|
}
|
|
|
|
}
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
2004-11-04 23:09:57 +00:00
|
|
|
if (LogFile != -1 && status == NOCONN) {
|
1998-03-05 22:17:59 +00:00
|
|
|
SyslogAddr.sun_len = sizeof(SyslogAddr);
|
|
|
|
SyslogAddr.sun_family = AF_UNIX;
|
2004-11-04 23:09:57 +00:00
|
|
|
|
|
|
|
/*
|
2014-11-25 20:59:22 +00:00
|
|
|
* First try privileged socket. If no success,
|
2004-11-04 23:09:57 +00:00
|
|
|
* then try default socket.
|
|
|
|
*/
|
|
|
|
(void)strncpy(SyslogAddr.sun_path, _PATH_LOG_PRIV,
|
1998-03-06 03:10:49 +00:00
|
|
|
sizeof SyslogAddr.sun_path);
|
2004-11-04 23:09:57 +00:00
|
|
|
if (_connect(LogFile, (struct sockaddr *)&SyslogAddr,
|
|
|
|
sizeof(SyslogAddr)) != -1)
|
|
|
|
status = CONNPRIV;
|
|
|
|
|
|
|
|
if (status == NOCONN) {
|
|
|
|
(void)strncpy(SyslogAddr.sun_path, _PATH_LOG,
|
|
|
|
sizeof SyslogAddr.sun_path);
|
|
|
|
if (_connect(LogFile, (struct sockaddr *)&SyslogAddr,
|
|
|
|
sizeof(SyslogAddr)) != -1)
|
|
|
|
status = CONNDEF;
|
|
|
|
}
|
1997-03-20 16:28:27 +00:00
|
|
|
|
2004-11-04 23:09:57 +00:00
|
|
|
if (status == NOCONN) {
|
1997-03-20 16:28:27 +00:00
|
|
|
/*
|
|
|
|
* Try the old "/dev/log" path, for backward
|
|
|
|
* compatibility.
|
|
|
|
*/
|
1998-03-05 22:17:59 +00:00
|
|
|
(void)strncpy(SyslogAddr.sun_path, _PATH_OLDLOG,
|
1998-03-06 03:10:49 +00:00
|
|
|
sizeof SyslogAddr.sun_path);
|
2004-11-04 23:09:57 +00:00
|
|
|
if (_connect(LogFile, (struct sockaddr *)&SyslogAddr,
|
|
|
|
sizeof(SyslogAddr)) != -1)
|
|
|
|
status = CONNDEF;
|
1997-03-20 16:28:27 +00:00
|
|
|
}
|
|
|
|
|
2004-11-04 23:09:57 +00:00
|
|
|
if (status == NOCONN) {
|
Simplify sytem call renaming. Instead of _foo() <-- _libc_foo <-- foo(),
just use _foo() <-- foo(). In the case of a libpthread that doesn't do
call conversion (such as linuxthreads and our upcoming libpthread), this
is adequate. In the case of libc_r, we still need three names, which are
now _thread_sys_foo() <-- _foo() <-- foo().
Convert all internal libc usage of: aio_suspend(), close(), fsync(), msync(),
nanosleep(), open(), fcntl(), read(), and write() to _foo() instead of foo().
Remove all internal libc usage of: creat(), pause(), sleep(), system(),
tcdrain(), wait(), and waitpid().
Make thread cancellation fully POSIX-compliant.
Suggested by: deischen
2000-01-27 23:07:25 +00:00
|
|
|
(void)_close(LogFile);
|
1994-05-27 05:00:24 +00:00
|
|
|
LogFile = -1;
|
1997-03-20 16:28:27 +00:00
|
|
|
}
|
1996-03-02 19:56:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-30 16:05:11 +00:00
|
|
|
static void
|
2005-04-08 21:15:38 +00:00
|
|
|
openlog_unlocked(const char *ident, int logstat, int logfac)
|
1996-03-02 19:56:16 +00:00
|
|
|
{
|
|
|
|
if (ident != NULL)
|
|
|
|
LogTag = ident;
|
|
|
|
LogStat = logstat;
|
|
|
|
if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
|
|
|
|
LogFacility = logfac;
|
|
|
|
|
|
|
|
if (LogStat & LOG_NDELAY) /* open immediately */
|
|
|
|
connectlog();
|
|
|
|
|
|
|
|
opened = 1; /* ident and facility has been set */
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
2004-12-30 16:05:11 +00:00
|
|
|
void
|
2005-04-08 21:15:38 +00:00
|
|
|
openlog(const char *ident, int logstat, int logfac)
|
2004-12-30 16:05:11 +00:00
|
|
|
{
|
2017-06-17 11:29:59 +00:00
|
|
|
|
2004-12-30 16:05:11 +00:00
|
|
|
THREAD_LOCK();
|
2017-06-17 11:29:59 +00:00
|
|
|
pthread_cleanup_push(syslog_cancel_cleanup, NULL);
|
2004-12-30 16:05:11 +00:00
|
|
|
openlog_unlocked(ident, logstat, logfac);
|
2017-06-17 11:29:59 +00:00
|
|
|
pthread_cleanup_pop(1);
|
2004-12-30 16:05:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-05-27 05:00:24 +00:00
|
|
|
void
|
2005-04-08 21:15:38 +00:00
|
|
|
closelog(void)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2004-12-30 16:05:11 +00:00
|
|
|
THREAD_LOCK();
|
2012-06-20 06:38:41 +00:00
|
|
|
if (LogFile != -1) {
|
|
|
|
(void)_close(LogFile);
|
|
|
|
LogFile = -1;
|
|
|
|
}
|
2002-11-14 12:40:14 +00:00
|
|
|
LogTag = NULL;
|
2004-11-04 23:09:57 +00:00
|
|
|
status = NOCONN;
|
2004-12-30 16:05:11 +00:00
|
|
|
THREAD_UNLOCK();
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* setlogmask -- set the log mask level */
|
|
|
|
int
|
2005-04-08 21:15:38 +00:00
|
|
|
setlogmask(int pmask)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
|
|
|
int omask;
|
|
|
|
|
2004-12-30 16:05:11 +00:00
|
|
|
THREAD_LOCK();
|
1994-05-27 05:00:24 +00:00
|
|
|
omask = LogMask;
|
|
|
|
if (pmask != 0)
|
|
|
|
LogMask = pmask;
|
2004-12-30 16:05:11 +00:00
|
|
|
THREAD_UNLOCK();
|
1994-05-27 05:00:24 +00:00
|
|
|
return (omask);
|
|
|
|
}
|