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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
/*
* Copyright (C) 2002 - 2004 Tomasz Kojm <tkojm@clamav.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <sys/stat.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/socket.h>
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#if defined(USE_SYSLOG) && !defined(C_AIX)
#include <syslog.h>
#endif
#include "output.h"
#include "memory.h"
#ifdef CL_THREAD_SAFE
#include <pthread.h>
pthread_mutex_t logg_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
FILE *logg_fd = NULL;
short int logg_verbose = 0, logg_lock = 0, logg_time = 0;
int logg_size = 0;
const char *logg_file = NULL;
#if defined(USE_SYSLOG) && !defined(C_AIX)
short logg_syslog = 0;
#endif
short int mprintf_disabled = 0, mprintf_verbose = 0, mprintf_quiet = 0,
mprintf_stdout = 0;
int mdprintf(int desc, const char *str, ...)
{
va_list args;
char buff[512];
int bytes;
va_start(args, str);
bytes = vsnprintf(buff, 512, str, args);
va_end(args);
write(desc, buff, bytes);
return bytes;
}
void logg_close(void) {
#ifdef CL_THREAD_SAFE
pthread_mutex_lock(&logg_mutex);
#endif
if (logg_fd) {
fclose(logg_fd);
logg_fd = NULL;
}
#ifdef CL_THREAD_SAFE
pthread_mutex_unlock(&logg_mutex);
#endif
#if defined(USE_SYSLOG) && !defined(C_AIX)
if(logg_syslog) {
closelog();
}
#endif
}
int logg(const char *str, ...)
{
va_list args;
struct flock fl;
char *pt, *timestr, vbuff[1025];
time_t currtime;
struct stat sb;
mode_t old_umask;
va_start(args, str);
if(logg_file) {
#ifdef CL_THREAD_SAFE
pthread_mutex_lock(&logg_mutex);
#endif
if(!logg_fd) {
old_umask = umask(0037);
if((logg_fd = fopen(logg_file, "a")) == NULL) {
umask(old_umask);
#ifdef CL_THREAD_SAFE
pthread_mutex_unlock(&logg_mutex);
#endif
printf("ERROR: Can't open %s in append mode.\n", logg_file);
return -1;
} else umask(old_umask);
if(logg_lock) {
memset(&fl, 0, sizeof(fl));
fl.l_type = F_WRLCK;
if(fcntl(fileno(logg_fd), F_SETLK, &fl) == -1) {
#ifdef CL_THREAD_SAFE
pthread_mutex_unlock(&logg_mutex);
#endif
return -1;
}
}
}
/* Need to avoid logging time for verbose messages when logverbose
is not set or we get a bunch of timestamps in the log without
newlines... */
if(logg_time && ((*str != '*') || logg_verbose)) {
time(&currtime);
pt = ctime(&currtime);
timestr = mcalloc(strlen(pt), sizeof(char));
strncpy(timestr, pt, strlen(pt) - 1);
fprintf(logg_fd, "%s -> ", timestr);
free(timestr);
}
if(logg_size) {
if(stat(logg_file, &sb) != -1) {
if(sb.st_size > logg_size) {
logg_file = NULL;
fprintf(logg_fd, "Log size = %d, maximal = %d\n", (int) sb.st_size, logg_size);
fprintf(logg_fd, "LOGGING DISABLED (Maximal log file size exceeded).\n");
fclose(logg_fd);
logg_fd = NULL;
#ifdef CL_THREAD_SAFE
pthread_mutex_unlock(&logg_mutex);
#endif
return 0;
}
}
}
if(*str == '!') {
fprintf(logg_fd, "ERROR: ");
vfprintf(logg_fd, str + 1, args);
} else if(*str == '^') {
fprintf(logg_fd, "WARNING: ");
vfprintf(logg_fd, str + 1, args);
} else if(*str == '*') {
if(logg_verbose)
vfprintf(logg_fd, str + 1, args);
} else vfprintf(logg_fd, str, args);
fflush(logg_fd);
#ifdef CL_THREAD_SAFE
pthread_mutex_unlock(&logg_mutex);
#endif
}
#if defined(USE_SYSLOG) && !defined(C_AIX)
if(logg_syslog) {
/* due to a problem with superfluous control characters (which
* vsnprintf() handles correctly) in (v)syslog we have to remove
* them in a final string
*
* FIXME: substitute %% instead of _
*/
vsnprintf(vbuff, 1024, str, args);
vbuff[1024] = 0;
while((pt = strchr(vbuff, '%')))
*pt = '_';
if(vbuff[0] == '!') {
syslog(LOG_ERR, vbuff + 1);
} else if(vbuff[0] == '^') {
syslog(LOG_WARNING, vbuff + 1);
} else if(vbuff[0] == '*') {
if(logg_verbose) {
syslog(LOG_DEBUG, vbuff + 1);
}
} else syslog(LOG_INFO, vbuff);
}
#endif
va_end(args);
return 0;
}
void mprintf(const char *str, ...)
{
va_list args;
FILE *fd;
char logbuf[512];
if(mprintf_disabled) {
if(*str == '@') {
va_start(args, str);
#ifdef NO_SNPRINTF
vsprintf(logbuf, ++str, args);
#else
vsnprintf(logbuf, sizeof(logbuf), ++str, args);
#endif
va_end(args);
logg("ERROR: %s", logbuf);
}
return;
}
if(mprintf_stdout)
fd = stdout;
else
fd = stderr;
/* legend:
* ! - error
* @ - error with logging
* ...
*/
/*
* ERROR WARNING STANDARD
* normal yes yes yes
*
* verbose yes yes yes
*
* quiet yes no no
*/
va_start(args, str);
if(*str == '!') {
fprintf(fd, "ERROR: ");
vfprintf(fd, ++str, args);
} else if(*str == '@') {
fprintf(fd, "ERROR: ");
vfprintf(fd, ++str, args);
#ifdef NO_SNPRINTF
vsprintf(logbuf, str, args);
#else
vsnprintf(logbuf, sizeof(logbuf), str, args);
#endif
logg("ERROR: %s", logbuf);
} else if(!mprintf_quiet) {
if(*str == '^') {
fprintf(fd, "WARNING: ");
vfprintf(fd, ++str, args);
} else if(*str == '*') {
if(mprintf_verbose)
vfprintf(fd, ++str, args);
} else vfprintf(fd, str, args);
}
va_end(args);
if(fd == stdout)
fflush(stdout);
}
struct facstruct {
const char *name;
int code;
};
#if defined(USE_SYSLOG) && !defined(C_AIX)
static const struct facstruct facilitymap[] = {
#ifdef LOG_AUTH
{ "LOG_AUTH", LOG_AUTH },
#endif
#ifdef LOG_AUTHPRIV
{ "LOG_AUTHPRIV", LOG_AUTHPRIV },
#endif
#ifdef LOG_CRON
{ "LOG_CRON", LOG_CRON },
#endif
#ifdef LOG_DAEMON
{ "LOG_DAEMON", LOG_DAEMON },
#endif
#ifdef LOG_FTP
{ "LOG_FTP", LOG_FTP },
#endif
#ifdef LOG_KERN
{ "LOG_KERN", LOG_KERN },
#endif
#ifdef LOG_LPR
{ "LOG_LPR", LOG_LPR },
#endif
#ifdef LOG_MAIL
{ "LOG_MAIL", LOG_MAIL },
#endif
#ifdef LOG_NEWS
{ "LOG_NEWS", LOG_NEWS },
#endif
#ifdef LOG_AUTH
{ "LOG_AUTH", LOG_AUTH },
#endif
#ifdef LOG_SYSLOG
{ "LOG_SYSLOG", LOG_SYSLOG },
#endif
#ifdef LOG_USER
{ "LOG_USER", LOG_USER },
#endif
#ifdef LOG_UUCP
{ "LOG_UUCP", LOG_UUCP },
#endif
#ifdef LOG_LOCAL0
{ "LOG_LOCAL0", LOG_LOCAL0 },
#endif
#ifdef LOG_LOCAL1
{ "LOG_LOCAL1", LOG_LOCAL1 },
#endif
#ifdef LOG_LOCAL2
{ "LOG_LOCAL2", LOG_LOCAL2 },
#endif
#ifdef LOG_LOCAL3
{ "LOG_LOCAL3", LOG_LOCAL3 },
#endif
#ifdef LOG_LOCAL4
{ "LOG_LOCAL4", LOG_LOCAL4 },
#endif
#ifdef LOG_LOCAL5
{ "LOG_LOCAL5", LOG_LOCAL5 },
#endif
#ifdef LOG_LOCAL6
{ "LOG_LOCAL6", LOG_LOCAL6 },
#endif
#ifdef LOG_LOCAL7
{ "LOG_LOCAL7", LOG_LOCAL7 },
#endif
{ NULL, -1 }
};
int logg_facility(const char *name)
{
int i;
for(i = 0; facilitymap[i].name; i++)
if(!strcmp(facilitymap[i].name, name))
return facilitymap[i].code;
return -1;
}
#endif
|