File: log.c

package info (click to toggle)
c-icap 1:0.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,600 kB
  • sloc: ansic: 27,220; sh: 4,415; makefile: 242; perl: 95; awk: 10
file content (411 lines) | stat: -rw-r--r-- 11,259 bytes parent folder | download | duplicates (2)
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
/*
 *  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 <errno.h>
#include <assert.h>

logger_module_t *default_logger = NULL;


void logformat_release();

int log_open()
{
    if (default_logger)
        return default_logger->log_open();
    return 0;
}


void log_close()
{
    if (default_logger) {
        default_logger->log_close();
    }
}

void log_reset()
{
    logformat_release();
    default_logger = NULL;
}

void log_access(ci_request_t * req, int status)
{
    /*req can not be NULL */
    if (!req)
        return;

    if (default_logger)
        default_logger->log_access(req);
}

extern process_pid_t MY_PROC_PID;
void log_server(ci_request_t * req, const char *format, ...)
{
    /*req can be NULL......... */
    va_list ap;
    char prefix[64];
    va_start(ap, format);
    if (default_logger) {
        if (MY_PROC_PID)
            snprintf(prefix, 64, "%u/%u", (unsigned int)MY_PROC_PID, (unsigned int)ci_thread_self());
        else /*probably the main process*/
            strcpy(prefix, "main proc");

        default_logger->log_server(prefix, format, ap);    /*First argument must be changed..... */
    }
    va_end(ap);
}

void vlog_server(ci_request_t * req, const char *format, va_list ap)
{
    if (default_logger)
        default_logger->log_server("", 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 = LOGDIR;*/
char *SERVER_LOG_FILE = LOGDIR "/cicap-server.log";
/*char *ACCESS_LOG_FILE = 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;

        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(&(lf->rwlock)); // Initialize logfile::rwlock
    }

    ret = ci_thread_rwlock_init(&systemlog_rwlock);
    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[STR_TIME_SIZE];

    if (!server_log)
        return;

    ci_strtime(buf);
    ci_thread_rwlock_rdlock(&systemlog_rwlock); /*obtain a read lock*/
    fprintf(server_log, "%s, %s, ", buf, server);
    vfprintf(server_log, format, ap);
    ci_thread_rwlock_unlock(&systemlog_rwlock); /*release a read lock*/
//     fprintf(server_log,"\n");
}


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;
}