forked from c-icap/c-icap-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.c
484 lines (415 loc) · 13.2 KB
/
log.c
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*
* Copyright (C) 2004-2008 Christos Tsantilas
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA.
*/
#include "common.h"
#include "c-icap.h"
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include "log.h"
#include "util.h"
#include "access.h"
#include "module.h"
#include "cfg_param.h"
#include "debug.h"
#include "txt_format.h"
#include "acl.h"
#include "proc_threads_queues.h"
#include "commands.h"
#include "array.h"
#include <errno.h>
#include <assert.h>
static ci_list_t *default_loggers = NULL;
static int disable_logs = 0;
void logformat_release();
static void log_file_logger_makedefault();
void log_add_logger(logger_module_t *logger)
{
if (!default_loggers) {
default_loggers = ci_list_create(1024, sizeof(logger_module_t *));
}
ci_list_push_back(default_loggers, &logger);
disable_logs = 0;
}
void log_disable_logs()
{
disable_logs = 1;
if (default_loggers) {
ci_list_destroy(default_loggers);
default_loggers = NULL;
}
}
int log_open()
{
int ok = 0;
if (!default_loggers && !disable_logs)
log_file_logger_makedefault();
if (default_loggers) {
logger_module_t **logger = NULL;
ci_list_iterator_t it;
int ret = 0;
for (logger = ci_list_iterator_first(default_loggers, &it); logger != NULL; logger = ci_list_iterator_next(&it)) {
if (!(ret = (*logger)->log_open())) {
ci_debug_printf(1, "WARNING: log_open: logger '%s' can not be initialized\n", (*logger)->name);
} else
ok = ret;
}
return ok;
}
return 1;
}
void log_close()
{
if (default_loggers) {
logger_module_t **logger = NULL;
ci_list_iterator_t it;
for (logger = ci_list_iterator_first(default_loggers, &it); logger != NULL; logger = ci_list_iterator_next(&it)) {
(*logger)->log_close();
}
}
}
void log_reset()
{
logformat_release();
if (default_loggers) {
ci_list_destroy(default_loggers);
default_loggers = NULL;
}
}
void log_access(ci_request_t * req, int status)
{
/*req can not be NULL */
if (!req)
return;
if (default_loggers) {
logger_module_t **logger = NULL;
ci_list_iterator_t it;
for (logger = ci_list_iterator_first(default_loggers, &it); logger != NULL; logger = ci_list_iterator_next(&it)) {
(*logger)->log_access(req);
}
}
}
extern process_pid_t MY_PROC_PID;
static const char *log_prefix(char *buf, size_t buf_size)
{
if (MY_PROC_PID)
snprintf(buf, buf_size, "%" PRIu64 "/%" PRIu64, (uint64_t)MY_PROC_PID, (uint64_t)ci_thread_self());
else
snprintf(buf, buf_size, "main proc");
return buf;
}
void log_server(ci_request_t * req, const char *format, ...)
{
/*req can be NULL......... */
va_list ap;
char buf[64];
va_start(ap, format);
if (default_loggers) {
logger_module_t **logger = NULL;
ci_list_iterator_t it;
for (logger = ci_list_iterator_first(default_loggers, &it); logger != NULL; logger = ci_list_iterator_next(&it)) {
(*logger)->log_server(log_prefix(buf, sizeof(buf)), format, ap);
}
}
va_end(ap);
}
void vlog_server(ci_request_t * req, const char *format, va_list ap)
{
char buf[64];
if (default_loggers) {
logger_module_t **logger = NULL;
ci_list_iterator_t it;
for (logger = ci_list_iterator_first(default_loggers, &it); logger != NULL; logger = ci_list_iterator_next(&it)) {
(*logger)->log_server(log_prefix(buf, sizeof(buf)), format, ap);
}
}
}
/*************************************************************/
/* logformat */
/*
Maybe logformat manipulation functions should moved to the c-icap library,
because some platforms (eg. MS-WINDOWS) can not use functions and objects
defined in main executable. At this time ony the sys_logger.c module uses these
functions which make sense on unix platforms (where there is not a such problem).
Moreover the sys_logger can be compiled inside c-icap main executable to avoid
such problems.
*/
struct logformat {
char *name;
char *fmt;
struct logformat *next;
};
struct logformat *LOGFORMATS = NULL;
int logformat_add(const char *name, const char *format)
{
struct logformat *lf, *tmp;
lf = malloc(sizeof(struct logformat));
if (!lf) {
ci_debug_printf(1, "Error allocating memory in add_logformat\n");
return 0;
}
lf->name = strdup(name);
lf->fmt = strdup(format);
if (!lf->name || !lf->fmt) {
ci_debug_printf(1, "Error strduping in add_logformat\n");
free(lf);
return 0;
}
lf->next = NULL;
if (LOGFORMATS==NULL) {
LOGFORMATS = lf;
return 1;
}
tmp = LOGFORMATS;
while (tmp->next != NULL)
tmp = tmp->next;
tmp->next = lf;
return 1;
}
void logformat_release()
{
struct logformat *cur, *tmp;
if (!(tmp = LOGFORMATS))
return;
do {
cur = tmp;
tmp = tmp->next;
free(cur->name);
free(cur->fmt);
free(cur);
} while (tmp);
LOGFORMATS = NULL;
}
char *logformat_fmt(const char *name)
{
struct logformat *tmp;
if (!(tmp = LOGFORMATS))
return NULL;
while (tmp) {
if (strcmp(tmp->name, name) == 0)
return tmp->fmt;
tmp = tmp->next;
}
return NULL;
}
/******************************************************************/
/* file_logger implementation. This is the default logger */
/* */
int file_log_open();
void file_log_close();
void file_log_access(ci_request_t *req);
void file_log_server(const char *server, const char *format, va_list ap);
void file_log_relog(const char *name, int type, const char **argv);
/*char *LOGS_DIR = CI_LOGDIR;*/
char *SERVER_LOG_FILE = CI_LOGDIR "/cicap-server.log";
/*char *ACCESS_LOG_FILE = CI_LOGDIR "/cicap-access.log";*/
struct logfile {
char *file;
FILE *access_log;
const char *log_fmt;
ci_access_entry_t *access_list;
ci_thread_rwlock_t rwlock;
struct logfile *next;
};
struct logfile *ACCESS_LOG_FILES = NULL;
static ci_thread_rwlock_t systemlog_rwlock;
logger_module_t file_logger = {
"file_logger",
NULL,
file_log_open,
file_log_close,
file_log_access,
file_log_server,
NULL /*NULL configuration table */
};
FILE *server_log = NULL;
const char *DEFAULT_LOG_FORMAT = "%tl, %la %a %im %iu %is";
FILE *logfile_open(const char *fname)
{
FILE *f = fopen(fname, "a+");
if (f)
setvbuf(f, NULL, _IONBF, 0);
return f;
}
int file_log_open()
{
int error = 0, ret = 0;
struct logfile *lf;
assert(ret == 0);
register_command("relog", MONITOR_PROC_CMD | CHILDS_PROC_CMD, file_log_relog);
for (lf = ACCESS_LOG_FILES; lf != NULL; lf = lf->next) {
if (!lf->file) {
ci_debug_printf (1, "This is a bug! lf->file==NULL\n");
continue;
}
if (lf->log_fmt == NULL)
lf->log_fmt = (char *)DEFAULT_LOG_FORMAT;
if (ci_thread_rwlock_init(&(lf->rwlock)) != 0) {
ci_debug_printf (1, "WARNING! Can not initialize structures for log file: %s\n", lf->file);
continue;
}
lf->access_log = logfile_open(lf->file);
if (!lf->access_log) {
error = 1;
ci_debug_printf (1, "WARNING! Can not open log file: %s\n", lf->file);
}
}
ret = ci_thread_rwlock_init(&systemlog_rwlock);
if (ret != 0)
return 0;
server_log = logfile_open(SERVER_LOG_FILE);
if (!server_log)
return 0;
if (error)
return 0;
else
return 1;
}
void file_log_close()
{
struct logfile *lf, *tmp;
lf = ACCESS_LOG_FILES;
while (lf != NULL) {
if (lf->access_log)
fclose(lf->access_log);
free(lf->file);
if (lf->access_list)
ci_access_entry_release(lf->access_list);
ci_thread_rwlock_destroy(&(lf->rwlock)); // Initialize logfile::rwlock
tmp = lf;
lf = lf->next;
ACCESS_LOG_FILES = lf;
free(tmp);
}
if (server_log)
fclose(server_log);
server_log = NULL;
ci_thread_rwlock_destroy(&systemlog_rwlock); // destroy rwlock
}
void file_log_relog(const char *name, int type, const char **argv)
{
struct logfile *lf;
/* This code should match the appropriate code from file_log_close */
for (lf = ACCESS_LOG_FILES; lf != NULL; lf = lf->next) {
ci_thread_rwlock_wrlock(&(lf->rwlock)); /*obtain a write lock. When this function returns all file_log_access will block until write unlock*/
if (lf->access_log)
fclose(lf->access_log);
lf->access_log = logfile_open(lf->file);
ci_thread_rwlock_unlock(&(lf->rwlock));
if (!lf->access_log)
ci_debug_printf (1, "WARNING! Can not open log file: %s\n", lf->file);
}
ci_thread_rwlock_wrlock(&systemlog_rwlock);
if (server_log)
fclose(server_log);
server_log = logfile_open(SERVER_LOG_FILE);
ci_thread_rwlock_unlock(&systemlog_rwlock);
/*if !server_log ???*/
}
void file_log_access(ci_request_t *req)
{
struct logfile *lf;
char logline[4096];
for (lf = ACCESS_LOG_FILES; lf != NULL; lf = lf->next) {
if (lf->access_list && !(ci_access_entry_match_request(lf->access_list, req) == CI_ACCESS_ALLOW)) {
ci_debug_printf(6, "access log file %s does not match, skiping\n", lf->file);
continue;
}
ci_debug_printf(6, "Log request to access log file %s\n", lf->file);
ci_format_text(req, lf->log_fmt, logline, sizeof(logline), NULL);
ci_thread_rwlock_rdlock(&lf->rwlock); /*obtain a read lock*/
if (lf->access_log)
fprintf(lf->access_log,"%s\n", logline);
ci_thread_rwlock_unlock(&lf->rwlock); /*obtain a read lock*/
}
}
void file_log_server(const char *server, const char *format, va_list ap)
{
char buf[1024];
if (!server_log)
return;
ci_strntime(buf, sizeof(buf));
const size_t len = strlen(buf);
const size_t written = snprintf(buf + len, sizeof(buf) - len, ", %s, %s", server, format);
assert(written < sizeof(buf) - len);
ci_thread_rwlock_rdlock(&systemlog_rwlock); /*obtain a read lock*/
vfprintf(server_log, buf, ap);
ci_thread_rwlock_unlock(&systemlog_rwlock); /*release a read lock*/
}
int file_log_addlogfile(const char *file, const char *format, const char **acls)
{
char *access_log_file, *access_log_format;
const char *acl_name;
struct logfile *lf, *newlf;
int i;
access_log_file = strdup(file);
if (!access_log_file)
return 0;
if (format) {
/*the folowing return format txt or NULL. It is OK*/
access_log_format = logformat_fmt(format);
} else
access_log_format = NULL;
newlf = malloc(sizeof(struct logfile));
newlf->file = access_log_file;
newlf->log_fmt = (access_log_format != NULL? access_log_format : DEFAULT_LOG_FORMAT);
newlf->access_log = NULL;
newlf->access_list = NULL;
newlf->next = NULL;
if (acls != NULL && acls[0] != NULL) {
if (ci_access_entry_new(&(newlf->access_list), CI_ACCESS_ALLOW) == NULL) {
ci_debug_printf(1, "Error creating access list for access log file %s!\n",
newlf->file);
free(newlf->file);
free(newlf);
return 0;
}
for (i = 0; acls[i] != NULL; i++) {
acl_name = acls[i];
if (!ci_access_entry_add_acl_by_name(newlf->access_list, acl_name)) {
ci_debug_printf(1, "Error addind acl %s to access list for access log file %s!\n",
acl_name, newlf->file);
ci_access_entry_release(newlf->access_list);
free(newlf->file);
free(newlf);
return 0;
}
}
}
if (!ACCESS_LOG_FILES) {
ACCESS_LOG_FILES = newlf;
} else {
for (lf = ACCESS_LOG_FILES; lf->next != NULL; lf = lf->next) {
if (strcmp(lf->file, newlf->file)==0) {
ci_debug_printf(1, "Access log file %s already defined!\n",
newlf->file);
if (newlf->access_list)
ci_access_entry_release(newlf->access_list);
free(newlf->file);
free(newlf);
return 0;
}
}
lf->next = newlf;
}
return 1;
}
void log_file_logger_makedefault()
{
log_add_logger(&file_logger);
}