~ubuntu-branches/ubuntu/karmic/dante/karmic

« back to all changes in this revision

Viewing changes to dlib/log.c

  • Committer: Bazaar Package Importer
  • Author(s): Thijs Kinkhorst
  • Date: 2006-10-19 12:09:39 UTC
  • mfrom: (3.1.1 dapper)
  • Revision ID: james.westby@ubuntu.com-20061019120939-t818x24e2tn8be5k
Tags: 1.1.18-2.1
* Non-maintainer upload for RC bug.
* Make sure changelogs are installed into all packages (Closes: #393568).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003
3
 
 *      Inferno Nettverk A/S, Norway.  All rights reserved.
4
 
 *
5
 
 * Redistribution and use in source and binary forms, with or without
6
 
 * modification, are permitted provided that the following conditions
7
 
 * are met:
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.
18
 
 *
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.
29
 
 *
30
 
 * Inferno Nettverk A/S requests users of this software to return to
31
 
 *
32
 
 *  Software Distribution Coordinator  or  sdc@inet.no
33
 
 *  Inferno Nettverk A/S
34
 
 *  Oslo Research Park
35
 
 *  Gaustadall�en 21
36
 
 *  NO-0349 Oslo
37
 
 *  Norway
38
 
 *
39
 
 * any improvements or extensions that they make and grant Inferno Nettverk A/S
40
 
 * the rights to redistribute these changes.
41
 
 *
42
 
 */
43
 
 
44
 
#include "common.h"
45
 
 
46
 
static const char rcsid[] =
47
 
"$Id: log.c,v 1.61 2003/07/01 13:21:30 michaels Exp $";
48
 
 
49
 
__BEGIN_DECLS
50
 
 
51
 
static char *
52
 
logformat __P((int priority, char *buf, size_t buflen, const char *message,
53
 
                                   va_list ap));
54
 
/*
55
 
 * formats "message" as appropriate.  The formated message is stored
56
 
 * in the buffer "buf", which is of size "buflen".
57
 
 * Returns:
58
 
 *              On success: pointer to "buf".
59
 
 *              On failure: NULL.
60
 
 */
61
 
 
62
 
__END_DECLS
63
 
 
64
 
void
65
 
newprocinit(void)
66
 
{
67
 
 
68
 
#if SOCKS_SERVER        /* don't want to override original clients stuff. */
69
 
        if (sockscf.log.type & LOGTYPE_SYSLOG) {
70
 
                closelog();
71
 
 
72
 
                /*
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.
75
 
                 */
76
 
                openlog(__progname, LOG_NDELAY | LOG_PID, sockscf.log.facility);
77
 
        }
78
 
#endif /* SOCKS_SERVER */
79
 
 
80
 
#if SOCKSLIBRARY_DYNAMIC
81
 
        symbolcheck();
82
 
#endif
83
 
 
84
 
        sockscf.state.pid = getpid();
85
 
 
86
 
}
87
 
 
88
 
void
89
 
#ifdef STDC_HEADERS
90
 
slog(int priority, const char *message, ...)
91
 
#else
92
 
slog(priority, message, va_alist)
93
 
        int priority;
94
 
        char *message;
95
 
        va_dcl
96
 
#endif  /* STDC_HEADERS */
97
 
{
98
 
        va_list ap;
99
 
 
100
 
#ifdef STDC_HEADERS
101
 
        /* LINTED pointer casts may be troublesome */
102
 
        va_start(ap, message);
103
 
#else
104
 
        va_start(ap);
105
 
#endif  /* STDC_HEADERS */
106
 
 
107
 
        vslog(priority, message, ap);
108
 
 
109
 
        /* LINTED expression has null effect */
110
 
        va_end(ap);
111
 
}
112
 
 
113
 
void
114
 
vslog(priority, message, ap)
115
 
        int priority;
116
 
        const char *message;
117
 
        va_list ap;
118
 
{
119
 
        const int errno_s = errno;
120
 
        char buf[2048];
121
 
 
122
 
#if SOCKS_SERVER /* no idea where stdout points to in client case. */
123
 
        if (!sockscf.state.init) {
124
 
 
125
 
#if 0
126
 
                if (priority == LOG_DEBUG)
127
 
                        return;
128
 
#endif
129
 
 
130
 
                if (logformat(priority, buf, sizeof(buf), message, ap) != NULL)
131
 
                        fprintf(stdout, "%s\n", buf);
132
 
                return;
133
 
        }
134
 
#endif
135
 
 
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. */
140
 
                else
141
 
                        vsyslog(priority, message, ap);
142
 
 
143
 
        if (sockscf.log.type & LOGTYPE_FILE) {
144
 
                size_t i;
145
 
 
146
 
                if (logformat(priority, buf, sizeof(buf), message, ap) == NULL)
147
 
                        return;
148
 
 
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]));
152
 
#endif
153
 
 
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]);
159
 
 
160
 
#if SOCKS_CLIENT && SOCKSLIBRARY_DYNAMIC
161
 
                        SYSCALL_END(fileno(sockscf.log.fpv[i]));
162
 
#endif
163
 
                }
164
 
        }
165
 
 
166
 
        errno = errno_s;
167
 
}
168
 
 
169
 
static char *
170
 
logformat(priority, buf, buflen, message, ap)
171
 
        int priority;
172
 
        char *buf;
173
 
        size_t buflen;
174
 
        const char *message;
175
 
        va_list ap;
176
 
{
177
 
        size_t bufused;
178
 
        time_t timenow;
179
 
 
180
 
        switch (priority) {
181
 
                case LOG_DEBUG:
182
 
                        if (sockscf.state.init && !sockscf.option.debug)
183
 
                                return NULL;
184
 
                        break;
185
 
 
186
 
        }
187
 
 
188
 
        time(&timenow);
189
 
        bufused = strftime(buf, buflen, "%h %e %T ", localtime(&timenow));
190
 
 
191
 
        bufused += snprintfn(&buf[bufused], buflen - bufused, "%s[%lu]: ",
192
 
        __progname,
193
 
#if SOCKS_SERVER
194
 
        (unsigned long)sockscf.state.pid
195
 
#else /* !SOCKS_SERVER, can't trust saved state. */
196
 
        (unsigned long)getpid()
197
 
#endif /* !SOCKS_SERVER */
198
 
        );
199
 
 
200
 
        vsnprintf(&buf[bufused], buflen - bufused, message, ap);
201
 
 
202
 
        return buf;
203
 
}