-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathngx_http_log_feed_module.c
1505 lines (1239 loc) · 46.2 KB
/
ngx_http_log_feed_module.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
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) Zhigang Zhang
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <ngx_md5.h>
#include <sys/stat.h>
#define DEFAULT_FEED_LOG_ROOT "feed"
#define DEFAULT_FEED_LOG_FNAME "feed"
#define FEED_LOG_FILE_LOCK ".feed_lock"
#define FEED_LOG_FNAME_UNIQ_DUMMY_STR "XXXXXX"
#define FEED_LOG_DATE_PADDING_STR "YYYY_MM_DD_hHH"
#define FEED_LOG_DATE_FMT_STR "%4d_%02d_%02d_h%02d"
#define FEED_LOG_DATE_NUM_FMT(date_num) (date_num < 10 ? "0%d" : "%d")
#define FEED_LOG_FNAME_PADDING_TEMPLATE "_"FEED_LOG_DATE_PADDING_STR"_"FEED_LOG_FNAME_UNIQ_DUMMY_STR".log"
#define LOG_HOST_DIR 1
#define MAX_FEED_LOG_FILE_NUM (1<<10)
#define MAX_FEED_LOG_FILE_SPACE (MAX_FEED_LOG_FILE_NUM + 1)
#define MAX_FEED_LOG_FILE_SIZE (1<<26)
#define MAX_FEED_LOG_FNAME_LEN 256
#define FEED_LOG_UNLIKED_FILE -1
#define MD5_BIN_LEN 16
#define MD5_STR_LEN 32
#define FEED_LOG_LINE_BUF_SIZE 4096
#define FEED_LOG_DIR_DEFAULT_ACCESS 0755
#define DEFAULT_FEED_LOG_FORMAT "$remote_addr - [$local_time] $host \"$request\" $args $status $cookies \"$referer\" \"$user_agent\""
#define FEED_LOG_LINE_VAR_ENTRY(var_name) \
{ngx_string(#var_name), {-1, NULL}, feed_log_line_var_get_##var_name, {-1, NULL}, -1, 0}
#define FEED_LOG_LINE_VAR_ARG_ENTRY(var_name, var_param) \
{ngx_string(#var_name), ngx_string(var_param), feed_log_line_var_get_from_args, {-1, NULL}, -1, 0}
#define FEED_LOG_LINE_VAR_NULL \
{{-1, NULL}, {-1, NULL}, NULL, {-1, NULL}, -1, 0}
#define FEED_LOG_LINE_VAR_GET(var_name) \
static ngx_int_t feed_log_line_var_get_##var_name(ngx_str_t *param, \
ngx_str_t *value, \
ngx_http_request_t *r)
#define do_feed_log_get_uniq_fname mktemp
#define FEED_LOG_HASH_FIND(head, fname) \
({ \
feed_log_file_t *__p; \
for (__p = head; __p != NULL; __p = __p->hash_next) \
if (ngx_strncmp(__p->ffname.data, (fname)->data, (fname)->len) == 0) \
break; \
__p; \
})
#define FEED_LOG_HASH_ADD(head, bucket) \
do { \
(bucket)->hash_next = head; \
(bucket)->hash_prev = NULL; \
if (head != NULL) { \
(head)->hash_prev = bucket; \
} \
head = bucket; \
} while(0)
#define FEED_LOG_HASH_REMOVE(head, bucket) \
do { \
if ((bucket)->hash_prev != NULL) { \
(bucket)->hash_prev->hash_next = (bucket)->hash_next; \
} \
if ((bucket)->hash_next != NULL) { \
(bucket)->hash_next->hash_prev = (bucket)->hash_prev; \
} \
if (head == bucket) { \
head = bucket->hash_next; \
} \
} while(0)
#define FEED_LOG_PATH_EXISTS(pathname) \
({ \
ngx_int_t __ret; \
__ret = access((const char *)(pathname), F_OK); \
__ret == -1 ? 0 : 1; \
})
typedef struct {
u_char data[MAX_FEED_LOG_FNAME_LEN + 1]; //padding '\0'
size_t len;
} feed_fstr_t;
typedef struct {
u_char data[sizeof(FEED_LOG_DATE_PADDING_STR)];
size_t len;
} feed_tstr_t;
typedef struct feed_log_file_s{
feed_fstr_t ffname; /*full file name*/
uint64_t file_size;
uint64_t write_len;
feed_tstr_t create_date;
ngx_int_t in_use; /*if in servising*/
uint64_t uc_stat; /*use count stats*/
ngx_uint_t ref_count; /*opening reference counts by processes*/
struct feed_log_file_s *free_next;
struct feed_log_file_s *hash_prev;
struct feed_log_file_s *hash_next;
} feed_log_file_t;
typedef struct {
ngx_fd_t fd;
feed_log_file_t *flp;
} feed_log_file_ex_t;
typedef struct {
//mutex
ngx_shmtx_sh_t shmtx_addr;
ngx_shmtx_t shmtx;
feed_log_file_t *hash_table[MAX_FEED_LOG_FILE_NUM];
feed_log_file_t *free_list;
feed_log_file_t feed_log_files[MAX_FEED_LOG_FILE_SPACE];
} feed_log_file_summary_shm_t;
typedef struct {
ngx_shm_t shm;
ngx_shmtx_t *shmtx_ptr;
feed_log_file_summary_shm_t *addr;
} feed_log_shm_t;
typedef struct {
ngx_str_t name;
ngx_str_t param;
ngx_int_t (*get_val)(ngx_str_t *, ngx_str_t *, ngx_http_request_t *);
ngx_str_t value;
ngx_int_t pos;
ngx_int_t valid;
} feed_log_line_var_t;
typedef struct {
ngx_str_t buf;
size_t size; //pre-allocate mem size.
} feed_log_line_t;
typedef struct {
ngx_uint_t log_feed;
ngx_str_t log_format;
ngx_str_t log_root;
ngx_str_t log_root_dir;
} ngx_http_log_feed_srv_conf_t;
static void* ngx_http_log_feed_create_srv_conf(ngx_conf_t *cf);
static char* ngx_http_log_feed_merge_srv_conf(ngx_conf_t *cf,
void *parent, void *child);
static ngx_int_t ngx_http_log_feed_init(ngx_conf_t *cf);
static ngx_int_t do_log_feed(ngx_str_t *dir, ngx_str_t *fname,
ngx_str_t *log_format, ngx_http_request_t *r);
static ngx_int_t ngx_http_log_feed_module_init(ngx_cycle_t *cycle);
static void ngx_http_log_feed_module_exit(ngx_cycle_t *cycle);
static ngx_int_t feed_log_create_newfile(ngx_str_t *fname,
feed_log_file_t *feed_log_p,
ngx_http_request_t *r);
static ngx_int_t feed_log_get_file(feed_log_file_ex_t *fp,
ngx_int_t write_len,
ngx_str_t *fname,
feed_log_shm_t *shm,
ngx_http_request_t *r);
static inline void feed_log_put_file(feed_log_file_ex_t *fp, feed_log_shm_t *shm)
{
ngx_shmtx_lock(shm->shmtx_ptr);
--fp->flp->ref_count;
ngx_shmtx_unlock(shm->shmtx_ptr);
}
static ngx_int_t feed_log_get_id(u_char *feed_id, ngx_http_request_t *r);
FEED_LOG_LINE_VAR_GET(remote_addr);
FEED_LOG_LINE_VAR_GET(local_time);
FEED_LOG_LINE_VAR_GET(status);
FEED_LOG_LINE_VAR_GET(cookies);
FEED_LOG_LINE_VAR_GET(user_agent);
FEED_LOG_LINE_VAR_GET(args);
static ngx_int_t feed_log_line_var_get_from_args(ngx_str_t *param, ngx_str_t *value,
ngx_http_request_t *r);
static ngx_str_t feed_gif = ngx_string("/feed.gif");
static feed_log_shm_t feed_log_shm = {
.shm = {
.addr = NULL,
.size = sizeof(feed_log_file_summary_shm_t)
}
};
static feed_log_line_var_t feed_log_line_vars[] = {
FEED_LOG_LINE_VAR_ENTRY(remote_addr),
FEED_LOG_LINE_VAR_ENTRY(local_time),
FEED_LOG_LINE_VAR_ENTRY(status),
FEED_LOG_LINE_VAR_ENTRY(cookies),
FEED_LOG_LINE_VAR_ENTRY(user_agent),
FEED_LOG_LINE_VAR_ARG_ENTRY(host, "FL.host"),
FEED_LOG_LINE_VAR_ARG_ENTRY(request, "FL.request"),
FEED_LOG_LINE_VAR_ARG_ENTRY(referer, "FL.ref"),
FEED_LOG_LINE_VAR_ENTRY(args), //must be last
FEED_LOG_LINE_VAR_NULL
};
static feed_log_line_var_t *feed_log_line_vps[sizeof(feed_log_line_vars)] = {NULL, };
static struct {
ngx_fd_t fd; //open fd in current process.
feed_fstr_t ffname; //opened full file name.
} cached_files[MAX_FEED_LOG_FILE_SPACE];
static ngx_command_t ngx_http_log_feed_commands[] = {
{
ngx_string("log_feed"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_flag_slot,
NGX_HTTP_SRV_CONF_OFFSET,
offsetof(ngx_http_log_feed_srv_conf_t, log_feed),
NULL
},
{
ngx_string("feed_log_root"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_SRV_CONF_OFFSET,
offsetof(ngx_http_log_feed_srv_conf_t, log_root),
NULL
},
{
ngx_string("feed_log_format"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_SRV_CONF_OFFSET,
offsetof(ngx_http_log_feed_srv_conf_t, log_format),
NULL
},
ngx_null_command
};
static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
static ngx_http_module_t ngx_http_log_feed_module_ctx = {
NULL, /* preconfiguration */
ngx_http_log_feed_init, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
ngx_http_log_feed_create_srv_conf, /* create server configuration */
ngx_http_log_feed_merge_srv_conf, /* merge server configuration */
NULL, /* create location configuration */
NULL /* merge location configuration */
};
ngx_module_t ngx_http_log_feed_module = {
NGX_MODULE_V1,
&ngx_http_log_feed_module_ctx, /* module context */
ngx_http_log_feed_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
ngx_http_log_feed_module_init, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
ngx_http_log_feed_module_exit, /* exit master */
NGX_MODULE_V1_PADDING
};
static inline ngx_uint_t _hashfn(ngx_str_t *str)
{
ngx_uint_t i, hash = 0;
for (i = 0; i < str->len; i++) {
hash = ((hash << 5) + hash) + (ngx_uint_t)str->data[i];
}
return hash;
}
static inline void feed_log_get_now_date(feed_tstr_t *date_str)
{
ngx_tm_t tm;
time_t now;
now = ngx_time();
ngx_localtime(now, &tm);
date_str->len = ngx_sprintf(date_str->data,
FEED_LOG_DATE_FMT_STR,
tm.ngx_tm_year, tm.ngx_tm_mon, tm.ngx_tm_mday, tm.ngx_tm_hour, tm.ngx_tm_min, tm.ngx_tm_sec) - date_str->data;
}
static inline ngx_int_t feed_log_check_file(feed_log_file_t *feed_log_p,
ngx_http_request_t *r)
{
feed_tstr_t now_date;
/*
ngx_file_info_t fi;
if (ngx_file_info(feed_log_p->ffname.data, &fi) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
"Stat %s failed\n",
feed_log_p->ffname.data);
return FEED_LOG_UNLIKED_FILE;
}
if (fi.st_size >= MAX_FEED_LOG_FILE_SIZE) {
return FEED_LOG_UNLIKED_FILE;
}
*/
feed_log_get_now_date(&now_date);
if (ngx_strncmp(now_date.data, feed_log_p->create_date.data,
now_date.len) != 0) {
return FEED_LOG_UNLIKED_FILE;
}
if ((feed_log_p->file_size + feed_log_p->write_len) > MAX_FEED_LOG_FILE_SIZE) {
return FEED_LOG_UNLIKED_FILE;
}
return NGX_OK;
}
static inline void feed_log_get_uniq_fname(ngx_str_t *uniq_str)
{
uniq_str->data = (u_char *)do_feed_log_get_uniq_fname((char *)uniq_str->data);
}
static inline void ngx_md5_get(u_char *md5_str_buf, u_char *data, size_t len)
{
ngx_int_t i;
ngx_md5_t md5;
u_char md5_bin_buf[MD5_BIN_LEN];
ngx_md5_init(&md5);
ngx_md5_update(&md5, data, len);
ngx_md5_final(md5_bin_buf, &md5);
for (i = 0; i < MD5_STR_LEN; i++) {
ngx_int_t idx = i/2;
if (i%2 == 0) {
md5_str_buf[i] = md5_bin_buf[idx] & 0xF0;
md5_str_buf[i] >>= 4;
} else {
md5_str_buf[i] = md5_bin_buf[idx] & 0x0F;
}
if (md5_str_buf[i] <= 9) {
md5_str_buf[i] += '0';
continue;
}
if (md5_str_buf[i] >= 10) {
md5_str_buf[i] = (md5_str_buf[i] - 10) + 'A';
}
}
}
static inline ngx_int_t feed_log_write_file(feed_log_file_ex_t *fp,
u_char *buf, ngx_int_t count,
ngx_http_request_t *r)
{
ngx_int_t ret;
do {
ret = write(fp->fd, (const char *)buf, count);
} while (ret == -1 && (ngx_errno == EINTR || ngx_errno == EAGAIN));
if (ret == -1) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
"Write file %s error: %s!", fp->flp->ffname.data);
if (ngx_close_file(fp->fd) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
"Close file %s failed!", fp->flp->ffname.data);
}
return NGX_ERROR;
}
return NGX_OK;
}
static ngx_int_t feed_log_create_newfile(ngx_str_t *fname,
feed_log_file_t *feed_log_p,
ngx_http_request_t *r)
{
ngx_fd_t fd;
ngx_str_t uniq_str;
feed_tstr_t date_str = {
.data = {0, },
.len = sizeof(FEED_LOG_DATE_PADDING_STR) - 1
};
ngx_str_t feed_log_init_msg = ngx_string(
"#Software: FeedLog SmartSource Data Collector\n"
"#Version: 1.0\n"
);
ngx_int_t retry = 0;
if (fname->len + sizeof(FEED_LOG_FNAME_PADDING_TEMPLATE) -1
> MAX_FEED_LOG_FNAME_LEN) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"Log file name exceeds the max name len limit: %d!",
MAX_FEED_LOG_FNAME_LEN-sizeof(FEED_LOG_FNAME_PADDING_TEMPLATE)+1);
return NGX_ERROR;
}
uniq_str.len = sizeof(FEED_LOG_FNAME_UNIQ_DUMMY_STR) - 1;
uniq_str.data = ngx_pcalloc(r->pool, uniq_str.len + 1);
if (uniq_str.data == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno, "No more memory!");
return NGX_ERROR;
}
ngx_memcpy(uniq_str.data, FEED_LOG_FNAME_UNIQ_DUMMY_STR,
sizeof(FEED_LOG_FNAME_UNIQ_DUMMY_STR) - 1);
repeat:
feed_log_get_now_date(&date_str);
feed_log_get_uniq_fname(&uniq_str);
ngx_memzero(&feed_log_p->ffname, sizeof(feed_fstr_t));
feed_log_p->ffname.len = ngx_sprintf(feed_log_p->ffname.data,
"%s_%s_%s.log",
fname->data, date_str.data, uniq_str.data) - feed_log_p->ffname.data;
fd = ngx_open_file(feed_log_p->ffname.data,
O_WRONLY, O_CREAT|O_EXCL,
NGX_FILE_DEFAULT_ACCESS);
if (fd == NGX_INVALID_FILE) {
if (ngx_errno != EEXIST) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
ngx_open_file_n " \"%s\" failed!", feed_log_p->ffname.data);
return NGX_ERROR;
}
if (retry++ < 3) {
goto repeat;
} else {
return NGX_ERROR;
}
}
{
feed_log_file_ex_t flf;
flf.fd = fd;
flf.flp = feed_log_p;
if (feed_log_write_file(&flf, feed_log_init_msg.data, feed_log_init_msg.len, r)
!= NGX_OK) {
return NGX_ERROR;
}
}
feed_log_p->file_size = feed_log_init_msg.len;
if (ngx_close_file(fd) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
"Close %s failed!",
feed_log_p->ffname.data);
}
feed_log_p->create_date.len = date_str.len;
ngx_memcpy(feed_log_p->create_date.data, date_str.data, date_str.len);
return NGX_OK;
}
static ngx_int_t feed_log_get_file(feed_log_file_ex_t *fp,
ngx_int_t write_len,
ngx_str_t *fname,
feed_log_shm_t *shm,
ngx_http_request_t *r)
{
feed_log_file_t *feed_log_p;
feed_log_file_summary_shm_t *FLG = shm->addr;
ngx_uint_t hash = _hashfn(fname) % MAX_FEED_LOG_FILE_NUM;
ngx_int_t current_cached_file_idx;
typeof(cached_files[0]) *current_cached_file;
get_repeat:
ngx_shmtx_lock(shm->shmtx_ptr);
feed_log_p = FEED_LOG_HASH_FIND(FLG->hash_table[hash], fname);
if (feed_log_p != NULL) {
feed_log_p->write_len = write_len;
if (feed_log_check_file(feed_log_p, r) == FEED_LOG_UNLIKED_FILE) {
if (feed_log_p->ref_count != 0) {
ngx_shmtx_unlock(shm->shmtx_ptr);
ngx_msleep(1000);
goto get_repeat;
}
feed_log_p->in_use = 0;
FLG->free_list = feed_log_p;
FEED_LOG_HASH_REMOVE(FLG->hash_table[hash], feed_log_p);
goto create_newfile;
} else {
goto found;
}
}
create_newfile:
{
//Get free feed log file.
typeof(*feed_log_p) *p, *free_feed_log_p = NULL;
uint64_t uc = ~0;
p = FLG->free_list;
while (p->free_next != FLG->free_list) {
if (p->in_use == 0) {
free_feed_log_p = p;
FLG->free_list = p->free_next;
break;
}
if (p->uc_stat < uc && p->ref_count == 0) {
uc = p->uc_stat;
free_feed_log_p = p;
}
p = p->free_next;
}
if (!free_feed_log_p) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"Serving feed log files exceeds max feed files limit: %d, please consider to raise the feed log files limit;",
MAX_FEED_LOG_FILE_NUM);
goto out_unlock_err;
}
//LRU
if (free_feed_log_p->in_use != 0) {
ngx_str_t free_fname = {
.data = free_feed_log_p->ffname.data,
.len = free_feed_log_p->ffname.len - (sizeof(FEED_LOG_FNAME_PADDING_TEMPLATE - 1))
};
free_feed_log_p->in_use = 0;
free_feed_log_p->uc_stat = 0;
hash = _hashfn(&free_fname) % MAX_FEED_LOG_FILE_NUM;
FEED_LOG_HASH_REMOVE(FLG->hash_table[hash], free_feed_log_p);
}
feed_log_p = free_feed_log_p;
}
if (feed_log_create_newfile(fname, feed_log_p, r) == NGX_OK) {
feed_log_p->in_use = 1;
FEED_LOG_HASH_ADD(FLG->hash_table[hash], feed_log_p);
} else {
goto out_unlock_err;
}
found:
feed_log_p->file_size += write_len;
current_cached_file_idx = ((u_char *)feed_log_p - (u_char *)FLG->feed_log_files)/(sizeof(*feed_log_p));
current_cached_file = cached_files + current_cached_file_idx;
if (current_cached_file->fd >= 0) {
if ((current_cached_file->ffname.len == feed_log_p->ffname.len) && ngx_strncmp(current_cached_file->ffname.data, feed_log_p->ffname.data, current_cached_file->ffname.len) == 0) {
goto out_unlock_ok;
} else {
if (ngx_close_file(current_cached_file->fd) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
"Close %s failed!",
current_cached_file->ffname.data);
}
}
}
current_cached_file->fd = ngx_open_file(feed_log_p->ffname.data,
O_WRONLY, O_APPEND,
NGX_FILE_DEFAULT_ACCESS);
if (current_cached_file->fd == NGX_INVALID_FILE) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
ngx_open_file_n " \"%s\" failed!", feed_log_p->ffname.data);
goto out_unlock_err;
} else {
current_cached_file->ffname.len = feed_log_p->ffname.len;
ngx_memcpy(current_cached_file->ffname.data, feed_log_p->ffname.data, feed_log_p->ffname.len);
}
out_unlock_ok:
++feed_log_p->ref_count;
++feed_log_p->uc_stat;
ngx_shmtx_unlock(shm->shmtx_ptr);
fp->fd = current_cached_file->fd;
fp->flp = feed_log_p;
return NGX_OK;
out_unlock_err:
ngx_shmtx_unlock(shm->shmtx_ptr);
return NGX_ERROR;
}
static void feed_log_sshm_init(feed_log_file_summary_shm_t *sshm)
{
ngx_int_t i;
for (i = 0;i < MAX_FEED_LOG_FILE_SPACE; i++) {
sshm->feed_log_files[i].free_next = &sshm->feed_log_files[i+1];
}
sshm->feed_log_files[i].free_next = sshm->feed_log_files;
sshm->free_list = sshm->feed_log_files;
}
static inline void feed_log_shm_destroy(feed_log_shm_t *fl_shm)
{
ngx_shmtx_destroy(fl_shm->shmtx_ptr);
ngx_shm_free(&fl_shm->shm);
}
static ngx_int_t feed_log_shm_init(feed_log_shm_t *fl_shm)
{
if (fl_shm->addr != NULL) {
feed_log_shm_destroy(fl_shm);
}
if (ngx_shm_alloc(&fl_shm->shm) != NGX_OK) {
return NGX_ERROR;
}
fl_shm->addr = (feed_log_file_summary_shm_t *) fl_shm->shm.addr;
fl_shm->shmtx_ptr = &fl_shm->addr->shmtx;
ngx_memzero(fl_shm->addr, fl_shm->shm.size);
if (ngx_shmtx_create(fl_shm->shmtx_ptr, &fl_shm->addr->shmtx_addr,
(u_char *)FEED_LOG_FILE_LOCK) != NGX_OK) {
return NGX_ERROR;
}
feed_log_sshm_init(fl_shm->addr);
return NGX_OK;
}
static ngx_int_t ngx_http_log_feed_module_init(ngx_cycle_t *cycle)
{
if (feed_log_shm_init(&feed_log_shm) != NGX_OK) {
return NGX_ERROR;
}
//init cached files by master to each process
{
ngx_int_t i;
for(i = 0; i < MAX_FEED_LOG_FILE_SPACE; i++) {
cached_files[i].fd = -1;
}
}
return NGX_OK;
}
static void ngx_http_log_feed_module_exit(ngx_cycle_t *cycle)
{
feed_log_shm_destroy(&feed_log_shm);
}
static ngx_int_t feed_log_get_id(u_char *feed_id, ngx_http_request_t *r)
{
ngx_uint_t nip;
unsigned short int nport;
struct timeval tv;
ngx_uint_t rand;
struct sockaddr_in *sin;
ngx_str_t uniq_str;
u_char uniq_str_fmt[] = "%ud:%ud_%ul:%ul_%ud";
sin = (struct sockaddr_in *)r->connection->sockaddr;
nip = sin->sin_addr.s_addr;
nport = sin->sin_port;
ngx_gettimeofday(&tv);
rand = ngx_random();
uniq_str.data = ngx_pcalloc(r->pool, 128);
if (uniq_str.data == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno, "No more memory!");
return NGX_ERROR;
}
uniq_str.len = ngx_sprintf(uniq_str.data, (const char *)uniq_str_fmt,
nip, nport, tv.tv_sec, tv.tv_usec, rand) - uniq_str.data;
ngx_md5_get(feed_id, uniq_str.data, uniq_str.len);
return NGX_OK;
}
FEED_LOG_LINE_VAR_GET(remote_addr)
{
value->data = r->connection->addr_text.data;
value->len = r->connection->addr_text.len;
return NGX_OK;
}
FEED_LOG_LINE_VAR_GET(local_time)
{
ngx_tm_t tm;
time_t now;
u_char time_format[] = "yyyy/mm/dd hh:ii:ss";
now = ngx_time();
ngx_localtime(now, &tm);
value->len = sizeof(time_format) - 1;
value->data = ngx_pcalloc(r->pool, value->len + 1); //prespace for '\0'
if (value->data == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "No more memory!");
return NGX_ERROR;
}
ngx_sprintf(value->data,
"%4d/%02d/%02d %02d:%02d:%02d",
tm.ngx_tm_year, tm.ngx_tm_mon, tm.ngx_tm_mday,
tm.ngx_tm_hour, tm.ngx_tm_min, tm.ngx_tm_sec);
return NGX_OK;
}
#define ngx_http_parse_arg(r, arg, len, value) \
ngx_http_arg(r, (u_char *)arg, len, value)
static ngx_int_t feed_log_line_var_get_from_args(ngx_str_t *param, ngx_str_t *value,
ngx_http_request_t * r)
{
if (param->data == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Wrong arguments!");
return NGX_ERROR;
}
if (ngx_http_parse_arg(r, param->data, param->len, value) != NGX_OK) {
value->data = (u_char *)"-";
value->len = 1;
} else {
//strip the vars str from original args
u_char *new_data, *var_strpos;
ngx_int_t var_strlen, shift_strlen;
new_data = ngx_pcalloc(r->pool, value->len + 1); //prespace for '\0'
if (new_data == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "No more memory!");
return NGX_ERROR;
}
ngx_memcpy(new_data, value->data, value->len); //store value->data
var_strpos = value->data - 1/*=*/ - param->len - 1 /*&*/;
var_strlen = param->len + 1 /*=*/ + value->len + 1 /*&*/;
shift_strlen = (r->args.data + r->args.len) - (value->data + value->len);
if (shift_strlen > 0) {
ngx_memcpy(var_strpos, var_strpos + var_strlen, shift_strlen);
}
r->args.len -= var_strlen;
value->data = new_data;
}
return NGX_OK;
}
FEED_LOG_LINE_VAR_GET(args)
{
if (r->args.len == 0) {
value->data = (u_char *)"-";
value->len = 1;
} else {
value->data = r->args.data;
value->len = r->args.len;
}
return NGX_OK;
}
FEED_LOG_LINE_VAR_GET(status)
{
value->len = 3;
value->data = ngx_pcalloc(r->pool, value->len + 1);
if (value->data == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "No more memory!");
return NGX_ERROR;
}
sprintf((char *)value->data, "%d", (int)r->headers_out.status);
return NGX_OK;
}
FEED_LOG_LINE_VAR_GET(cookies)
{
size_t len;
u_char *p, *end;
ngx_uint_t i, n;
ngx_array_t *a;
ngx_table_elt_t **h;
a = &r->headers_in.cookies;
n = a->nelts;
h = a->elts;
len = 0;
for (i = 0; i < n; i++) {
if (h[i]->hash == 0) {
continue;
}
len += h[i]->value.len + 2;
}
if (len == 0) {
value->data = NULL;
value->len = 0;
goto out_parse;
}
len -= 2;
if (n == 1) {
value->len = (*h)->value.len;
value->data = (*h)->value.data;
goto out_parse;
}
p = ngx_pcalloc(r->pool, len + 1);
if (p == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "No more memory!");
return NGX_ERROR;
}
value->len = len;
value->data = p;
end = p + len;
for (i = 0; /* void */ ; i++) {
if (h[i]->hash == 0) {
continue;
}
p = ngx_copy(p, h[i]->value.data, h[i]->value.len);
if (p == end) {
break;
}
*p++ = ';', *p++ = ' ';
}
out_parse:
{
u_char feed_id_part[] = "+FEED_ID=";
u_char feed_id[MD5_STR_LEN+1] = {0, };
ngx_int_t feed_id_found = 0;
if (value->len > 0) {
u_char *p;
p = ngx_pcalloc(r->pool, 1 + value->len); //prepend the '+'
if (p == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "No more memory!");
return NGX_ERROR;
}
*p++ = '+';
p = ngx_cpymem(p, value->data, value->len);
value->data = p - (1 + value->len);
value->len += 1;
}
for (i = 0; i < value->len; i++) {
if (value->data[i] == ';' && value->data[i+1] == ' ') {
value->data[i+1] = '+';
}
if (!feed_id_found
&& ngx_strncmp(feed_id_part, &value->data[i],
sizeof(feed_id_part) - 1) == 0) {
feed_id_found = 1;
}
}
if (!feed_id_found) {
u_char *p, *q, *cookie;
ngx_int_t len;
u_char expires[] = "; expires=Thu, 31-Dec-37 23:55:55 GMT";
u_char cookie_path[] = "; path=/";
ngx_table_elt_t *set_cookie;
//prepend feed_id cookie
if (feed_log_get_id(feed_id, r) != NGX_OK) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"Get feed id error!");
return NGX_ERROR;
}
len = (sizeof(feed_id_part) - 1) + MD5_STR_LEN;
if (n > 0) {
len += 1 + value->len; //prespace for ';'
}
p = ngx_pcalloc(r->pool, len + 1); //prespace for '\0'
if (p == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "No more memory!");
return NGX_ERROR;
}
q = p;
p = ngx_cpymem(p, feed_id_part, sizeof(feed_id_part) - 1);
p = ngx_cpymem(p, feed_id, MD5_STR_LEN);
*p++ = ';';
ngx_memcpy(p, value->data, value->len);
value->data = q;
value->len = len;
//send feed_id cookie
len = sizeof(feed_id_part) - 1 + MD5_STR_LEN
+ sizeof(expires) - 1
+ sizeof(cookie_path) - 1 ;
cookie = ngx_pcalloc(r->pool, len);
if (cookie == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "No more memory!");
return NGX_ERROR;
}
p = ngx_cpymem(cookie, &feed_id_part[1], sizeof(feed_id_part) - 2);
p = ngx_cpymem(p, feed_id, MD5_STR_LEN);
p = ngx_cpymem(p, expires, sizeof(expires) - 1);
ngx_memcpy(p, cookie_path, sizeof(cookie_path) - 1);
set_cookie = ngx_list_push(&r->headers_out.headers);
if (set_cookie == NULL) {
return NGX_ERROR;
}
set_cookie->hash = 1;
ngx_str_set(&set_cookie->key, "Set-Cookie");
set_cookie->value.len = len;
set_cookie->value.data = cookie;
}
}
return NGX_OK;
}
FEED_LOG_LINE_VAR_GET(user_agent)
{
ngx_table_elt_t *user_agent;
u_char *p;
user_agent = r->headers_in.user_agent;
if (user_agent->value.len == 0) {
value->data = (u_char *)"-";
value->len = 1;
return NGX_OK;
}
value->len = user_agent->key.len + 2 + user_agent->value.len; // ': ' split
value->data = ngx_pcalloc(r->pool, value->len + 1); //prespace for '\0'
if (value->data == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "No more memory!");
return NGX_ERROR;
}
p = value->data;
p = ngx_cpymem(p, user_agent->key.data, user_agent->key.len);
*p++ = ':', *p++ = ' ';
ngx_memcpy(p, user_agent->value.data, user_agent->value.len);
return NGX_OK;
}