2
* Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003
3
* Inferno Nettverk A/S, Norway. All rights reserved.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
8
* 1. The above copyright notice, this list of conditions and the following
9
* disclaimer must appear in all copies of the software, derivative works
10
* or modified versions, and any portions thereof, aswell as in all
11
* supporting documentation.
12
* 2. All advertising materials mentioning features or use of this software
13
* must display the following acknowledgement:
14
* This product includes software developed by
15
* Inferno Nettverk A/S, Norway.
16
* 3. The name of the author may not be used to endorse or promote products
17
* derived from this software without specific prior written permission.
19
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
* Inferno Nettverk A/S requests users of this software to return to
32
* Software Distribution Coordinator or sdc@inet.no
33
* Inferno Nettverk A/S
39
* any improvements or extensions that they make and grant Inferno Nettverk A/S
40
* the rights to redistribute these changes.
46
static const char rcsid[] =
47
"$Id: log.c,v 1.61 2003/07/01 13:21:30 michaels Exp $";
52
logformat __P((int priority, char *buf, size_t buflen, const char *message,
55
* formats "message" as appropriate. The formated message is stored
56
* in the buffer "buf", which is of size "buflen".
58
* On success: pointer to "buf".
68
#if SOCKS_SERVER /* don't want to override original clients stuff. */
69
if (sockscf.log.type & LOGTYPE_SYSLOG) {
73
* LOG_NDELAY so we don't end up in a situation where we
74
* have no free descriptors and haven't yet syslog-ed anything.
76
openlog(__progname, LOG_NDELAY | LOG_PID, sockscf.log.facility);
78
#endif /* SOCKS_SERVER */
80
#if SOCKSLIBRARY_DYNAMIC
84
sockscf.state.pid = getpid();
90
slog(int priority, const char *message, ...)
92
slog(priority, message, va_alist)
96
#endif /* STDC_HEADERS */
101
/* LINTED pointer casts may be troublesome */
102
va_start(ap, message);
105
#endif /* STDC_HEADERS */
107
vslog(priority, message, ap);
109
/* LINTED expression has null effect */
114
vslog(priority, message, ap)
119
const int errno_s = errno;
122
#if SOCKS_SERVER /* no idea where stdout points to in client case. */
123
if (!sockscf.state.init) {
126
if (priority == LOG_DEBUG)
130
if (logformat(priority, buf, sizeof(buf), message, ap) != NULL)
131
fprintf(stdout, "%s\n", buf);
136
if (sockscf.log.type & LOGTYPE_SYSLOG)
137
if (priority == LOG_DEBUG && sockscf.state.init
138
&& !sockscf.option.debug)
139
; /* don't waste resources on this. */
141
vsyslog(priority, message, ap);
143
if (sockscf.log.type & LOGTYPE_FILE) {
146
if (logformat(priority, buf, sizeof(buf), message, ap) == NULL)
149
for (i = 0; i < sockscf.log.fpc; ++i) {
150
#if SOCKS_CLIENT && SOCKSLIBRARY_DYNAMIC /* XXX should not need SOCKS_CLIENT. */
151
SYSCALL_START(fileno(sockscf.log.fpv[i]));
154
socks_lock(sockscf.log.fplockv[i], F_WRLCK, -1);
155
fprintf(sockscf.log.fpv[i], "%s%s",
156
buf, buf[strlen(buf) - 1] == '\n' ? "" : "\n");
157
/* fflush(sockscf.log.fpv[i]); */ /* should not be needed. */
158
socks_unlock(sockscf.log.fplockv[i]);
160
#if SOCKS_CLIENT && SOCKSLIBRARY_DYNAMIC
161
SYSCALL_END(fileno(sockscf.log.fpv[i]));
170
logformat(priority, buf, buflen, message, ap)
182
if (sockscf.state.init && !sockscf.option.debug)
189
bufused = strftime(buf, buflen, "%h %e %T ", localtime(&timenow));
191
bufused += snprintfn(&buf[bufused], buflen - bufused, "%s[%lu]: ",
194
(unsigned long)sockscf.state.pid
195
#else /* !SOCKS_SERVER, can't trust saved state. */
196
(unsigned long)getpid()
197
#endif /* !SOCKS_SERVER */
200
vsnprintf(&buf[bufused], buflen - bufused, message, ap);