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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
|
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <aio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "log.h"
#include "events.h"
#include "ppp.h"
#include "spinlock.h"
#include "mempool.h"
#include "memdebug.h"
#define LOG_BUF_SIZE 16*1024
#define RED_COLOR "\033[1;31m"
#define GREEN_COLOR "\033[1;32m"
#define YELLOW_COLOR "\033[1;33m"
#define BLUE_COLOR "\033[1;34m"
#define NORMAL_COLOR "\033[0;39m"
struct log_file_t
{
struct list_head entry;
struct list_head msgs;
spinlock_t lock;
int need_free:1;
int queued:1;
struct log_file_pd_t *lpd;
int fd;
int new_fd;
off_t offset;
unsigned long magic;
};
struct log_file_pd_t
{
struct ppp_pd_t pd;
struct log_file_t lf;
unsigned long tmp;
};
static int conf_color;
static int conf_per_session;
static char *conf_per_user_dir;
static char *conf_per_session_dir;
static int conf_copy;
static const char* level_name[]={" msg", "error", " warn", " info", " info", "debug"};
static const char* level_color[]={NORMAL_COLOR, RED_COLOR, YELLOW_COLOR, GREEN_COLOR, GREEN_COLOR, BLUE_COLOR};
static void *pd_key1;
static void *pd_key2;
static struct log_file_t *log_file;
static mempool_t lpd_pool;
static char *log_buf;
static struct aiocb aiocb = {
.aio_lio_opcode = LIO_WRITE,
.aio_sigevent.sigev_notify = SIGEV_SIGNAL,
.aio_sigevent.sigev_signo = SIGIO,
};
static LIST_HEAD(lf_queue);
static spinlock_t lf_queue_lock = SPINLOCK_INITIALIZER;
static int lf_queue_sleeping = 1;
static unsigned long temp_seq;
static void send_next_chunk();
static void log_file_init(struct log_file_t *lf)
{
spinlock_init(&lf->lock);
INIT_LIST_HEAD(&lf->msgs);
lf->fd = -1;
lf->new_fd = -1;
}
static int log_file_open(struct log_file_t *lf, const char *fname)
{
lf->fd = open(fname, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
if (lf->fd < 0) {
log_emerg("log_file: open '%s': %s\n", fname, strerror(errno));
return -1;
}
lf->offset = lseek(lf->fd, 0, SEEK_END);
return 0;
}
static void sigio(int num, siginfo_t *si, void *uc)
{
struct log_file_t *lf;
int n;
if (si->si_signo != SIGIO)
return;
if (si->si_code != SI_ASYNCIO) {
if (aio_write(&aiocb))
log_emerg("log_file: aio_write: %s\n", strerror(errno));
return;
}
lf = (struct log_file_t *)si->si_ptr;
n = aio_return(&aiocb);
if (n < 0)
log_emerg("log_file: %s\n", strerror(aio_error(&aiocb)));
else if (n != aiocb.aio_nbytes)
log_emerg("log_file: short write %p %i %lu\n", lf, n, aiocb.aio_nbytes);
spin_lock(&lf->lock);
lf->offset += n;
if (list_empty(&lf->msgs)) {
if (lf->need_free) {
spin_unlock(&lf->lock);
close(lf->fd);
mempool_free(lf->lpd);
} else {
lf->queued = 0;
spin_unlock(&lf->lock);
}
} else {
spin_unlock(&lf->lock);
spin_lock(&lf_queue_lock);
list_add_tail(&lf->entry, &lf_queue);
spin_unlock(&lf_queue_lock);
}
send_next_chunk();
}
static int dequeue_log(struct log_file_t *lf)
{
int n, pos = 0;
struct log_msg_t *msg;
struct log_chunk_t *chunk;
while (1) {
spin_lock(&lf->lock);
if (list_empty(&lf->msgs)) {
spin_unlock(&lf->lock);
return pos;
}
msg = list_entry(lf->msgs.next, typeof(*msg), entry);
list_del(&msg->entry);
spin_unlock(&lf->lock);
if (pos + msg->hdr->len > LOG_BUF_SIZE)
goto overrun;
memcpy(log_buf + pos, msg->hdr->msg, msg->hdr->len);
n = msg->hdr->len;
list_for_each_entry(chunk, msg->chunks, entry) {
if (pos + n + chunk->len > LOG_BUF_SIZE)
goto overrun;
memcpy(log_buf + pos + n, chunk->msg, chunk->len);
n += chunk->len;
}
log_free_msg(msg);
pos += n;
}
overrun:
spin_lock(&lf->lock);
list_add(&msg->entry, &lf->msgs);
spin_unlock(&lf->lock);
return pos;
}
static void send_next_chunk(void)
{
struct log_file_t *lf;
int n;
spin_lock(&lf_queue_lock);
if (list_empty(&lf_queue)) {
lf_queue_sleeping = 1;
spin_unlock(&lf_queue_lock);
return;
}
lf = list_entry(lf_queue.next, typeof(*lf), entry);
n = log_file->entry.next == NULL;
list_del(&lf->entry);
spin_unlock(&lf_queue_lock);
if (lf->new_fd != -1) {
close(lf->fd);
lf->fd = lf->new_fd;
lf->new_fd = -1;
lf->offset = 0;
}
aiocb.aio_fildes = lf->fd;
aiocb.aio_offset = lf->offset;
aiocb.aio_sigevent.sigev_value.sival_ptr = lf;
aiocb.aio_nbytes = dequeue_log(lf);
if (aio_write(&aiocb))
log_emerg("log_file: aio_write: %s\n", strerror(errno));
}
static void queue_lf(struct log_file_t *lf)
{
int r;
spin_lock(&lf_queue_lock);
list_add_tail(&lf->entry, &lf_queue);
r = lf_queue_sleeping;
lf_queue_sleeping = 0;
spin_unlock(&lf_queue_lock);
if (r)
send_next_chunk();
}
static void queue_log(struct log_file_t *lf, struct log_msg_t *msg)
{
int r;
spin_lock(&lf->lock);
list_add_tail(&msg->entry, &lf->msgs);
if (lf->fd != -1) {
r = lf->queued;
lf->queued = 1;
} else
r = 1;
spin_unlock(&lf->lock);
if (!r)
queue_lf(lf);
}
static void set_hdr(struct log_msg_t *msg, struct ppp_t *ppp)
{
struct tm tm;
char timestamp[32];
localtime_r(&msg->timestamp.tv_sec, &tm);
strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", &tm);
sprintf(msg->hdr->msg, "%s[%s]: %s: %s%s%s", conf_color ? level_color[msg->level] : "",
timestamp, level_name[msg->level],
ppp ? ppp->ifname : "",
ppp ? ": " : "",
conf_color ? NORMAL_COLOR : "");
msg->hdr->len = strlen(msg->hdr->msg);
}
static void general_log(struct log_target_t *t, struct log_msg_t *msg, struct ppp_t *ppp)
{
if (ppp && !conf_copy) {
log_free_msg(msg);
return;
}
set_hdr(msg, ppp);
queue_log(log_file, msg);
}
static struct log_file_pd_t *find_pd(struct ppp_t *ppp, void *pd_key)
{
struct ppp_pd_t *pd;
struct log_file_pd_t *lpd;
list_for_each_entry(pd, &ppp->pd_list, entry) {
if (pd->key == pd_key) {
lpd = container_of(pd, typeof(*lpd), pd);
return lpd;
}
}
return NULL;
}
static void per_user_log(struct log_target_t *t, struct log_msg_t *msg, struct ppp_t *ppp)
{
struct log_file_pd_t *lpd;
if (!ppp) {
log_free_msg(msg);
return;
}
lpd = find_pd(ppp, &pd_key1);
if (!lpd) {
log_free_msg(msg);
return;
}
set_hdr(msg, ppp);
queue_log(&lpd->lf, msg);
}
static void per_session_log(struct log_target_t *t, struct log_msg_t *msg, struct ppp_t *ppp)
{
struct log_file_pd_t *lpd;
if (!ppp) {
log_free_msg(msg);
return;
}
lpd = find_pd(ppp, &pd_key2);
if (!lpd) {
log_free_msg(msg);
return;
}
set_hdr(msg, ppp);
queue_log(&lpd->lf, msg);
}
static void general_reopen(void)
{
char *fname = conf_get_opt("log", "log-file");
int fd = open(fname, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
if (fd < 0) {
log_emerg("log_file: open '%s': %s\n", fname, strerror(errno));
return;
}
log_file->new_fd = fd;
}
static void free_lpd(struct log_file_pd_t *lpd)
{
struct log_msg_t *msg;
spin_lock(&lpd->lf.lock);
list_del(&lpd->pd.entry);
lpd->lf.need_free = 1;
if (lpd->lf.queued)
spin_unlock(&lpd->lf.lock);
else {
while (!list_empty(&lpd->lf.msgs)) {
msg = list_entry(lpd->lf.msgs.next, typeof(*msg), entry);
list_del(&msg->entry);
log_free_msg(msg);
}
if (lpd->lf.fd != -1)
close(lpd->lf.fd);
spin_unlock(&lpd->lf.lock);
mempool_free(lpd);
}
}
static void ev_ctrl_started(struct ppp_t *ppp)
{
struct log_file_pd_t *lpd;
char *fname;
if (conf_per_user_dir) {
lpd = mempool_alloc(lpd_pool);
if (!lpd) {
log_emerg("log_file: out of memory\n");
return;
}
memset(lpd, 0, sizeof(*lpd));
lpd->pd.key = &pd_key1;
log_file_init(&lpd->lf);
lpd->lf.lpd = lpd;
list_add_tail(&lpd->pd.entry, &ppp->pd_list);
}
if (conf_per_session_dir) {
lpd = mempool_alloc(lpd_pool);
if (!lpd) {
log_emerg("log_file: out of memory\n");
return;
}
memset(lpd, 0, sizeof(*lpd));
lpd->pd.key = &pd_key2;
log_file_init(&lpd->lf);
lpd->lf.lpd = lpd;
fname = _malloc(PATH_MAX);
if (!fname) {
mempool_free(lpd);
log_emerg("log_file: out of memory\n");
return;
}
lpd->tmp = temp_seq++;
strcpy(fname, conf_per_session_dir);
strcat(fname, "/tmp");
sprintf(fname + strlen(fname), "%lu", lpd->tmp);
if (log_file_open(&lpd->lf, fname)) {
mempool_free(lpd);
_free(fname);
return;
}
_free(fname);
list_add_tail(&lpd->pd.entry, &ppp->pd_list);
}
}
static void ev_ctrl_finished(struct ppp_t *ppp)
{
struct log_file_pd_t *lpd;
char *fname;
lpd = find_pd(ppp, &pd_key1);
if (lpd)
free_lpd(lpd);
lpd = find_pd(ppp, &pd_key2);
if (lpd) {
if (lpd->tmp) {
fname = _malloc(PATH_MAX);
if (fname) {
strcpy(fname, conf_per_session_dir);
strcat(fname, "/tmp");
sprintf(fname + strlen(fname), "%lu", lpd->tmp);
if (unlink(fname))
log_emerg("log_file: unlink '%s': %s\n", fname, strerror(errno));
_free(fname);
} else
log_emerg("log_file: out of memory\n");
}
free_lpd(lpd);
}
}
static void ev_ppp_starting(struct ppp_t *ppp)
{
struct log_file_pd_t *lpd;
char *fname1, *fname2;
lpd = find_pd(ppp, &pd_key2);
if (!lpd)
return;
fname1 = _malloc(PATH_MAX);
if (!fname1) {
log_emerg("log_file: out of memory\n");
return;
}
fname2 = _malloc(PATH_MAX);
if (!fname2) {
log_emerg("log_file: out of memory\n");
_free(fname1);
return;
}
strcpy(fname1, conf_per_session_dir);
strcat(fname1, "/tmp");
sprintf(fname1 + strlen(fname1), "%lu", lpd->tmp);
strcpy(fname2, conf_per_session_dir);
strcat(fname2, "/");
strcat(fname2, ppp->sessionid);
strcat(fname2, ".log");
if (rename(fname1, fname2))
log_emerg("log_file: rename '%s' to '%s': %s\n", fname1, fname2, strerror(errno));
lpd->tmp = 0;
_free(fname1);
_free(fname2);
}
static void ev_ppp_authorized(struct ppp_t *ppp)
{
struct log_file_pd_t *lpd;
char *fname;
lpd = find_pd(ppp, &pd_key1);
if (!lpd)
return;
fname = _malloc(PATH_MAX);
if (!fname) {
log_emerg("log_file: out of memory\n");
return;
}
strcpy(fname, conf_per_user_dir);
strcat(fname, "/");
strcat(fname, ppp->username);
if (conf_per_session) {
if (mkdir(fname, S_IRWXU) && errno != EEXIST) {
log_emerg("log_file: mkdir '%s': %s'\n", fname, strerror(errno));
goto out_err;
}
strcat(fname, "/");
strcat(fname, ppp->sessionid);
}
strcat(fname, ".log");
if (log_file_open(&lpd->lf, fname))
goto out_err;
_free(fname);
if (!list_empty(&lpd->lf.msgs)) {
lpd->lf.queued = 1;
queue_lf(&lpd->lf);
}
return;
out_err:
_free(fname);
list_del(&lpd->pd.entry);
free_lpd(lpd);
}
static struct log_target_t general_target =
{
.log = general_log,
.reopen = general_reopen,
};
static struct log_target_t per_user_target =
{
.log = per_user_log,
};
static struct log_target_t per_session_target =
{
.log = per_session_log,
};
static void __init init(void)
{
char *opt;
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGIO);
struct sigaction sa = {
.sa_sigaction = sigio,
.sa_flags = SA_SIGINFO,
.sa_mask = set,
};
lpd_pool = mempool_create(sizeof(struct log_file_pd_t));
log_buf = malloc(LOG_BUF_SIZE);
aiocb.aio_buf = log_buf;
if (sigaction(SIGIO, &sa, NULL)) {
log_emerg("log_file: sigaction: %s\n", strerror(errno));
return;
}
opt = conf_get_opt("log", "log-file");
if (opt) {
log_file = malloc(sizeof(*log_file));
memset(log_file, 0, sizeof(*log_file));
log_file_init(log_file);
if (log_file_open(log_file, opt)) {
free(log_file);
_exit(EXIT_FAILURE);
}
}
opt = conf_get_opt("log","color");
if (opt && atoi(opt) > 0)
conf_color = 1;
opt = conf_get_opt("log", "per-user-dir");
if (opt)
conf_per_user_dir = _strdup(opt);
opt = conf_get_opt("log", "per-session-dir");
if (opt)
conf_per_session_dir = _strdup(opt);
opt = conf_get_opt("log", "per-session");
if (opt && atoi(opt) > 0)
conf_per_session = 1;
opt = conf_get_opt("log", "copy");
if (opt && atoi(opt) > 0)
conf_copy = 1;
log_register_target(&general_target);
if (conf_per_user_dir)
log_register_target(&per_user_target);
if (conf_per_session_dir)
log_register_target(&per_session_target);
triton_event_register_handler(EV_CTRL_STARTED, (triton_event_func)ev_ctrl_started);
triton_event_register_handler(EV_CTRL_FINISHED, (triton_event_func)ev_ctrl_finished);
triton_event_register_handler(EV_PPP_STARTING, (triton_event_func)ev_ppp_starting);
triton_event_register_handler(EV_PPP_AUTHORIZED, (triton_event_func)ev_ppp_authorized);
}
|