1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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
* or http://www.opensolaris.org/os/licensing.
* 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
*/
/*
* PPPoE Server-mode daemon log file support.
*
* Copyright (c) 2000-2001 by Sun Microsystems, Inc.
* All rights reserved.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <unistd.h>
#include <stdarg.h>
#include <alloca.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <syslog.h>
#include <assert.h>
#include <sys/types.h>
#include "common.h"
#include "logging.h"
/* Not all functions are used by all applications. Let lint know this. */
/*LINTLIBRARY*/
const char *prog_name = "none"; /* Subsystem name for syslog */
int log_level; /* Higher number for more detail. */
static int curlogfd = -1; /* Current log file */
static const char *curfname; /* Name of current log file */
static const char *stderr_name = "stderr";
#define SMALLSTR 254 /* Don't allocate for most strings. */
/*
* Returns -1 on error (with errno set), 0 on blocked write (file
* system full), or N (buffer length) on success.
*/
static int
dowrite(int fd, const void *buf, int len)
{
int retv;
const uint8_t *bp = (uint8_t *)buf;
while (len > 0) {
retv = write(fd, bp, len);
if (retv == 0) {
break;
}
if (retv == -1) {
if (errno != EINTR)
break;
} else {
bp += retv;
len -= retv;
}
}
if (len <= 0)
return (bp - (uint8_t *)buf);
return (retv);
}
/* A close that avoids closing stderr */
static int
doclose(void)
{
int retval = 0;
if (curlogfd == -1)
return (0);
if ((curlogfd != STDERR_FILENO) || (curfname != stderr_name))
retval = close(curlogfd);
curlogfd = -1;
return (retval);
}
/*
* Log levels are 0 for no messages, 1 for errors, 2 for warnings, 3
* for informational messages, and 4 for debugging messages.
*/
static void
vlogat(int loglev, const char *fmt, va_list args)
{
char timbuf[64];
char regbuf[SMALLSTR+2];
char *ostr;
int timlen;
int slen;
char *nstr;
int err1, err2;
int sloglev;
int retv;
va_list args2;
static int xlate_loglev[] = {
LOG_ERR, LOG_WARNING, LOG_INFO, LOG_DEBUG
};
if (loglev >= log_level)
return;
timbuf[0] = '\0';
timlen = 0;
if (curlogfd >= 0) {
time_t now = time(NULL);
/*
* Form a time/date string for file (non-syslog) logging.
* Caution: string broken in two so that SCCS doesn't mangle
* the %-T-% sequence.
*/
timlen = strftime(timbuf, sizeof (timbuf), "%Y/%m/%d %T"
"%Z: ", localtime(&now));
}
/* Try formatting once into the small buffer. */
va_copy(args2, args);
slen = vsnprintf(regbuf, SMALLSTR, fmt, args);
if (slen < SMALLSTR) {
ostr = regbuf;
} else {
/*
* Length returned by vsnprintf doesn't include null,
* and may also be missing a terminating \n.
*/
ostr = alloca(slen + 2);
slen = vsnprintf(ostr, slen + 1, fmt, args2);
}
/* Don't bother logging empty lines. */
if (slen <= 0)
return;
/* Tack on a \n if needed. */
if (ostr[slen - 1] != '\n') {
ostr[slen++] = '\n';
ostr[slen] = '\0';
}
/* Translate our log levels into syslog standard values */
assert(loglev >= 0 && loglev < Dim(xlate_loglev));
sloglev = xlate_loglev[loglev];
/* Log each line separately */
for (; *ostr != '\0'; ostr = nstr + 1) {
nstr = strchr(ostr, '\n');
/* Ignore zero-length lines. */
if (nstr == ostr)
continue;
slen = nstr - ostr + 1;
/*
* If we're supposed to be logging to a file, then try
* that first. Ditch the file and revert to syslog if
* any errors occur.
*/
if (curlogfd >= 0) {
if ((retv = dowrite(curlogfd, timbuf, timlen)) > 0)
retv = dowrite(curlogfd, ostr, slen);
/*
* If we've successfully logged this line,
* then go do the next one.
*/
if (retv > 0)
continue;
/* Save errno (if any) and close log file */
err1 = errno;
if (doclose() == -1)
err2 = errno;
else
err2 = 0;
/*
* Recursion is safe here because we cleared
* out curlogfd above.
*/
if (retv == -1)
logerr("write log %s: %s", curfname,
mystrerror(err1));
else
logerr("cannot write %s", curfname);
if (err2 == 0)
logdbg("closed log %s", curfname);
else
logerr("closing log %s: %s", curfname,
mystrerror(err2));
}
syslog(sloglev, "%.*s", slen, ostr);
}
}
/* Log at debug level */
void
logdbg(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vlogat(LOGLVL_DBG, fmt, args);
va_end(args);
}
/* Log informational messages */
void
loginfo(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vlogat(LOGLVL_INFO, fmt, args);
va_end(args);
}
/* Log warning messages */
void
logwarn(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vlogat(LOGLVL_WARN, fmt, args);
va_end(args);
}
/* Log error messages */
void
logerr(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vlogat(LOGLVL_ERR, fmt, args);
va_end(args);
}
/* Log a strerror message */
void
logstrerror(const char *emsg)
{
logerr("%s: %s\n", emsg, mystrerror(errno));
}
void
log_to_stderr(int dbglvl)
{
log_level = dbglvl;
if (curlogfd >= 0)
close_log_files();
curlogfd = STDERR_FILENO;
curfname = stderr_name;
}
/*
* Set indicated log file and debug level.
*/
void
log_for_service(const char *fname, int dbglvl)
{
int err1, err2;
boolean_t closed;
log_level = dbglvl;
if (fname != NULL &&
(*fname == '\0' || strcasecmp(fname, "syslog") == 0))
fname = NULL;
if (fname == NULL && curfname == NULL)
return;
err1 = err2 = 0;
closed = B_FALSE;
if (curlogfd >= 0) {
if (fname == curfname ||
(fname != NULL && strcmp(fname, curfname) == 0)) {
curfname = fname;
return;
}
if (doclose() == -1)
err1 = errno;
closed = B_TRUE;
}
if (fname != NULL) {
curlogfd = open(fname, O_WRONLY|O_APPEND|O_CREAT, 0600);
if (curlogfd == -1)
err2 = errno;
}
if (closed) {
if (err1 == 0)
logdbg("closed log %s", curfname);
else
logerr("closing log %s: %s", curfname,
mystrerror(err1));
}
if (fname != NULL) {
if (err2 == 0)
logdbg("opened log %s", fname);
else
logerr("opening log %s: %s", fname, mystrerror(err2));
}
curfname = fname;
}
/*
* Close any open log file. This is used for SIGHUP (to support log
* file rotation) and when execing.
*/
void
close_log_files(void)
{
int err = 0;
if (curlogfd >= 0) {
if (doclose() == -1)
err = errno;
if (err == 0)
logdbg("closed log %s", curfname);
else
logerr("closing log %s: %s", curfname,
mystrerror(err));
}
}
/*
* Reopen syslog connection; in case it was closed.
*/
void
reopen_log(void)
{
openlog(prog_name, LOG_PID | LOG_NDELAY | LOG_NOWAIT, LOG_DAEMON);
/* I control the log level */
(void) setlogmask(LOG_UPTO(LOG_DEBUG));
}
|