-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathtail_file.c
1831 lines (1593 loc) · 52.3 KB
/
tail_file.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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Fluent Bit
* ==========
* Copyright (C) 2015-2022 The Fluent Bit Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#ifdef FLB_SYSTEM_FREEBSD
#include <sys/user.h>
#include <libutil.h>
#endif
#include <fluent-bit/flb_compat.h>
#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_input_plugin.h>
#include <fluent-bit/flb_parser.h>
#ifdef FLB_HAVE_REGEX
#include <fluent-bit/flb_regex.h>
#include <fluent-bit/flb_hash_table.h>
#endif
#include "tail.h"
#include "tail_file.h"
#include "tail_config.h"
#include "tail_db.h"
#include "tail_signal.h"
#include "tail_dockermode.h"
#include "tail_multiline.h"
#include "tail_scan.h"
#ifdef FLB_SYSTEM_WINDOWS
#include "win32.h"
#endif
#include <cfl/cfl.h>
static inline void consume_bytes(char *buf, int bytes, int length)
{
memmove(buf, buf + bytes, length - bytes);
}
static uint64_t stat_get_st_dev(struct stat *st)
{
#ifdef FLB_SYSTEM_WINDOWS
/* do you want to contribute with a way to extract volume serial number ? */
return 0;
#else
return st->st_dev;
#endif
}
static int stat_to_hash_bits(struct flb_tail_config *ctx, struct stat *st,
uint64_t *out_hash)
{
int len;
uint64_t st_dev;
char tmp[64];
st_dev = stat_get_st_dev(st);
len = snprintf(tmp, sizeof(tmp) - 1, "%" PRIu64 ":%" PRIu64,
st_dev, (uint64_t)st->st_ino);
*out_hash = cfl_hash_64bits(tmp, len);
return 0;
}
static int stat_to_hash_key(struct flb_tail_config *ctx, struct stat *st,
flb_sds_t *key)
{
uint64_t st_dev;
flb_sds_t tmp;
flb_sds_t buf;
buf = flb_sds_create_size(64);
if (!buf) {
return -1;
}
st_dev = stat_get_st_dev(st);
tmp = flb_sds_printf(&buf, "%" PRIu64 ":%" PRIu64,
st_dev, (uint64_t)st->st_ino);
if (!tmp) {
flb_sds_destroy(buf);
return -1;
}
*key = buf;
return 0;
}
/* Append custom keys and report the number of records processed */
static int record_append_custom_keys(struct flb_tail_file *file,
char *in_data, size_t in_size,
char **out_data, size_t *out_size)
{
int i;
int ok = MSGPACK_UNPACK_SUCCESS;
int len;
int records = 0;
size_t off = 0;
size_t total;
msgpack_unpacked result;
msgpack_object time;
msgpack_object map;
msgpack_object k;
msgpack_object v;
msgpack_sbuffer mp_sbuf;
msgpack_packer mp_pck;
struct flb_mp_map_header mh;
struct flb_tail_config *ctx = file->config;
/* init new buffers */
msgpack_sbuffer_init(&mp_sbuf);
msgpack_packer_init(&mp_pck, &mp_sbuf, msgpack_sbuffer_write);
/* Some extra content will be added... */
msgpack_unpacked_init(&result);
while ((msgpack_unpack_next(&result, in_data, in_size, &off) == ok)) {
time = result.data.via.array.ptr[0];
map = result.data.via.array.ptr[1];
msgpack_pack_array(&mp_pck, 2);
msgpack_pack_object(&mp_pck, time);
/* pack map */
flb_mp_map_header_init(&mh, &mp_pck);
/* append previous map keys */
for (i = 0; i < map.via.map.size; i++) {
k = map.via.map.ptr[i].key;
v = map.via.map.ptr[i].val;
flb_mp_map_header_append(&mh);
msgpack_pack_object(&mp_pck, k);
msgpack_pack_object(&mp_pck, v);
}
/* path_key */
if (ctx->path_key) {
len = flb_sds_len(file->config->path_key);
flb_mp_map_header_append(&mh);
/* key */
msgpack_pack_str(&mp_pck, len);
msgpack_pack_str_body(&mp_pck, file->config->path_key, len);
/* val */
msgpack_pack_str(&mp_pck, file->orig_name_len);
msgpack_pack_str_body(&mp_pck, file->orig_name, file->orig_name_len);
}
/* offset_key */
if (ctx->offset_key) {
len = flb_sds_len(file->config->offset_key);
flb_mp_map_header_append(&mh);
/* key */
msgpack_pack_str(&mp_pck, len);
msgpack_pack_str_body(&mp_pck, file->config->offset_key, len);
/* val */
total = file->offset + file->last_processed_bytes;
msgpack_pack_uint64(&mp_pck, total);
}
/* finalize map */
flb_mp_map_header_end(&mh);
/* counter */
records++;
}
*out_data = mp_sbuf.data;
*out_size = mp_sbuf.size;
return records;
}
static int unpack_and_pack(msgpack_packer *pck, msgpack_object *root,
const char *key, size_t key_len,
const char *val, size_t val_len, size_t val_uint64)
{
int i;
int size = root->via.map.size;
msgpack_pack_map(pck, size + 1);
/* Append new k/v */
msgpack_pack_str(pck, key_len);
msgpack_pack_str_body(pck, key, key_len);
if (val != NULL) {
msgpack_pack_str(pck, val_len);
msgpack_pack_str_body(pck, val, val_len);
}
else {
msgpack_pack_uint64(pck, val_uint64);
}
for (i = 0; i < size; i++) {
msgpack_object k = root->via.map.ptr[i].key;
msgpack_object v = root->via.map.ptr[i].val;;
msgpack_pack_object(pck, k);
msgpack_pack_object(pck, v);
}
return 0;
}
static int append_record_to_map(char **data, size_t *data_size,
const char *key, size_t key_len,
const char *val, size_t val_len,
size_t val_uint64)
{
int ret;
msgpack_unpacked result;
msgpack_object root;
msgpack_sbuffer sbuf;
msgpack_packer pck;
size_t off = 0;
msgpack_sbuffer_init(&sbuf);
msgpack_packer_init(&pck, &sbuf, msgpack_sbuffer_write);
msgpack_unpacked_init(&result);
ret = msgpack_unpack_next(&result, *data, *data_size, &off);
if (ret != MSGPACK_UNPACK_SUCCESS) {
msgpack_unpacked_destroy(&result);
msgpack_sbuffer_destroy(&sbuf);
return -1;
}
root = result.data;
ret = unpack_and_pack(&pck, &root,
key, key_len, val, val_len, val_uint64);
if (ret < 0) {
/* fail! */
msgpack_unpacked_destroy(&result);
msgpack_sbuffer_destroy(&sbuf);
return -1;
}
else {
/* success !*/
flb_free(*data);
*data = sbuf.data;
*data_size = sbuf.size;
}
msgpack_unpacked_destroy(&result);
return 0;
}
int flb_tail_pack_line_map(msgpack_sbuffer *mp_sbuf, msgpack_packer *mp_pck,
struct flb_time *time, char **data,
size_t *data_size, struct flb_tail_file *file,
size_t processed_bytes)
{
int map_num = 1;
if (file->config->path_key != NULL) {
map_num++; /* to append path_key */
}
if (file->config->offset_key != NULL) {
map_num++; /* to append offset_key */
}
if (file->config->path_key != NULL) {
append_record_to_map(data, data_size,
file->config->path_key,
flb_sds_len(file->config->path_key),
file->name, file->name_len, 0);
}
if (file->config->offset_key != NULL) {
append_record_to_map(data, data_size,
file->config->offset_key,
flb_sds_len(file->config->offset_key),
NULL, 0, file->offset + processed_bytes);
}
msgpack_pack_array(mp_pck, 2);
flb_time_append_to_msgpack(time, mp_pck, 0);
msgpack_sbuffer_write(mp_sbuf, *data, *data_size);
return 0;
}
int flb_tail_file_pack_line(msgpack_sbuffer *mp_sbuf, msgpack_packer *mp_pck,
struct flb_time *time, char *data, size_t data_size,
struct flb_tail_file *file, size_t processed_bytes)
{
int map_num = 1;
struct flb_tail_config *ctx = file->config;
if (file->config->path_key != NULL) {
map_num++; /* to append path_key */
}
if (file->config->offset_key != NULL) {
map_num++; /* to append offset_key */
}
msgpack_pack_array(mp_pck, 2);
flb_time_append_to_msgpack(time, mp_pck, 0);
msgpack_pack_map(mp_pck, map_num);
if (file->config->path_key != NULL) {
/* append path_key */
msgpack_pack_str(mp_pck, flb_sds_len(file->config->path_key));
msgpack_pack_str_body(mp_pck, file->config->path_key,
flb_sds_len(file->config->path_key));
msgpack_pack_str(mp_pck, file->name_len);
msgpack_pack_str_body(mp_pck, file->name, file->name_len);
}
if (file->config->offset_key != NULL) {
/* append offset_key */
msgpack_pack_str(mp_pck, flb_sds_len(file->config->offset_key));
msgpack_pack_str_body(mp_pck, file->config->offset_key,
flb_sds_len(file->config->offset_key));
msgpack_pack_uint64(mp_pck, file->offset + processed_bytes);
}
msgpack_pack_str(mp_pck, flb_sds_len(ctx->key));
msgpack_pack_str_body(mp_pck, ctx->key, flb_sds_len(ctx->key));
msgpack_pack_str(mp_pck, data_size);
msgpack_pack_str_body(mp_pck, data, data_size);
return 0;
}
static int ml_stream_buffer_append(struct flb_tail_file *file, char *buf_data, size_t buf_size)
{
msgpack_sbuffer_write(&file->ml_sbuf, buf_data, buf_size);
return 0;
}
static int ml_stream_buffer_flush(struct flb_tail_config *ctx, struct flb_tail_file *file)
{
if (file->ml_sbuf.size > 0) {
flb_input_log_append(ctx->ins,
file->tag_buf,
file->tag_len,
file->ml_sbuf.data, file->ml_sbuf.size);
file->ml_sbuf.size = 0;
}
return 0;
}
static int process_content(struct flb_tail_file *file, size_t *bytes)
{
size_t len;
int lines = 0;
int ret;
size_t processed_bytes = 0;
char *data;
char *end;
char *p;
void *out_buf;
size_t out_size;
int crlf;
char *line;
size_t line_len;
char *repl_line;
size_t repl_line_len;
time_t now = time(NULL);
struct flb_time out_time = {0};
msgpack_sbuffer mp_sbuf;
msgpack_packer mp_pck;
msgpack_sbuffer *out_sbuf;
msgpack_packer *out_pck;
struct flb_tail_config *ctx = file->config;
/* Create a temporary msgpack buffer */
msgpack_sbuffer_init(&mp_sbuf);
msgpack_packer_init(&mp_pck, &mp_sbuf, msgpack_sbuffer_write);
out_sbuf = &mp_sbuf;
out_pck = &mp_pck;
/* Parse the data content */
data = file->buf_data;
end = data + file->buf_len;
/* reset last processed bytes */
file->last_processed_bytes = 0;
/* Skip null characters from the head (sometimes introduced by copy-truncate log rotation) */
while (data < end && *data == '\0') {
data++;
processed_bytes++;
}
while (data < end && (p = memchr(data, '\n', end - data))) {
len = (p - data);
crlf = 0;
if (file->skip_next == FLB_TRUE) {
data += len + 1;
processed_bytes += len + 1;
file->skip_next = FLB_FALSE;
continue;
}
/*
* Empty line (just breakline)
* ---------------------------
* [NOTE] with the new Multiline core feature and Multiline Filter on
* Fluent Bit v1.8.2, there are a couple of cases where stack traces
* or multi line patterns expects an empty line (meaning only the
* breakline), skipping empty lines on this plugin will break that
* functionality.
*
* We are introducing 'skip_empty_lines=off' configuration
* property to revert this behavior if some user is affected by
* this change.
*/
if (len == 0 && ctx->skip_empty_lines) {
data++;
processed_bytes++;
continue;
}
/* Process '\r\n' */
if (len >= 2) {
crlf = (data[len-1] == '\r');
if (len == 1 && crlf) {
data += 2;
processed_bytes += 2;
continue;
}
}
/* Reset time for each line */
flb_time_zero(&out_time);
line = data;
line_len = len - crlf;
repl_line = NULL;
if (ctx->ml_ctx) {
ret = flb_ml_append(ctx->ml_ctx, file->ml_stream_id,
FLB_ML_TYPE_TEXT,
&out_time, line, line_len);
goto go_next;
}
else if (ctx->docker_mode) {
ret = flb_tail_dmode_process_content(now, line, line_len,
&repl_line, &repl_line_len,
file, ctx, out_sbuf, out_pck);
if (ret >= 0) {
if (repl_line == line) {
repl_line = NULL;
}
else {
line = repl_line;
line_len = repl_line_len;
}
/* Skip normal parsers flow */
goto go_next;
}
else {
flb_tail_dmode_flush(out_sbuf, out_pck, file, ctx);
}
}
#ifdef FLB_HAVE_PARSER
if (ctx->parser) {
/* Common parser (non-multiline) */
ret = flb_parser_do(ctx->parser, line, line_len,
&out_buf, &out_size, &out_time);
if (ret >= 0) {
if (flb_time_to_nanosec(&out_time) == 0L) {
flb_time_get(&out_time);
}
/* If multiline is enabled, flush any buffered data */
if (ctx->multiline == FLB_TRUE) {
flb_tail_mult_flush(out_sbuf, out_pck, file, ctx);
}
flb_tail_pack_line_map(out_sbuf, out_pck, &out_time,
(char**) &out_buf, &out_size, file,
processed_bytes);
flb_free(out_buf);
}
else {
/* Parser failed, pack raw text */
flb_time_get(&out_time);
flb_tail_file_pack_line(out_sbuf, out_pck, &out_time,
data, len, file, processed_bytes);
}
}
else if (ctx->multiline == FLB_TRUE) {
ret = flb_tail_mult_process_content(now,
line, line_len,
file, ctx, processed_bytes);
/* No multiline */
if (ret == FLB_TAIL_MULT_NA) {
flb_tail_mult_flush(out_sbuf, out_pck, file, ctx);
flb_time_get(&out_time);
flb_tail_file_pack_line(out_sbuf, out_pck, &out_time,
line, line_len, file, processed_bytes);
}
else if (ret == FLB_TAIL_MULT_MORE) {
/* we need more data, do nothing */
goto go_next;
}
else if (ret == FLB_TAIL_MULT_DONE) {
/* Finalized */
}
}
else {
flb_time_get(&out_time);
flb_tail_file_pack_line(out_sbuf, out_pck, &out_time,
line, line_len, file, processed_bytes);
}
#else
flb_time_get(&out_time);
flb_tail_file_pack_line(out_sbuf, out_pck, &out_time,
line, line_len, file, processed_bytes);
#endif
go_next:
flb_free(repl_line);
repl_line = NULL;
/* Adjust counters */
data += len + 1;
processed_bytes += len + 1;
lines++;
file->parsed = 0;
file->last_processed_bytes += processed_bytes;
}
file->parsed = file->buf_len;
if (lines > 0) {
/* Append buffer content to a chunk */
*bytes = processed_bytes;
if (out_sbuf->size > 0) {
flb_input_log_append_records(ctx->ins,
lines,
file->tag_buf,
file->tag_len,
out_sbuf->data,
out_sbuf->size);
}
}
else if (file->skip_next) {
*bytes = file->buf_len;
}
else {
*bytes = processed_bytes;
}
if (ctx->ml_ctx) {
ml_stream_buffer_flush(ctx, file);
}
msgpack_sbuffer_destroy(out_sbuf);
return lines;
}
static inline void drop_bytes(char *buf, size_t len, int pos, int bytes)
{
memmove(buf + pos,
buf + pos + bytes,
len - pos - bytes);
}
#ifdef FLB_HAVE_REGEX
static void cb_results(const char *name, const char *value,
size_t vlen, void *data)
{
struct flb_hash_table *ht = data;
if (vlen == 0) {
return;
}
flb_hash_table_add(ht, name, strlen(name), (void *) value, vlen);
}
#endif
#ifdef FLB_HAVE_REGEX
static int tag_compose(char *tag, struct flb_regex *tag_regex, char *fname,
char *out_buf, size_t *out_size,
struct flb_tail_config *ctx)
#else
static int tag_compose(char *tag, char *fname, char *out_buf, size_t *out_size,
struct flb_tail_config *ctx)
#endif
{
int i;
size_t len;
char *p;
size_t buf_s = 0;
#ifdef FLB_HAVE_REGEX
ssize_t n;
struct flb_regex_search result;
struct flb_hash_table *ht;
char *beg;
char *end;
int ret;
const char *tmp;
size_t tmp_s;
#endif
#ifdef FLB_HAVE_REGEX
if (tag_regex) {
n = flb_regex_do(tag_regex, fname, strlen(fname), &result);
if (n <= 0) {
flb_plg_error(ctx->ins, "invalid tag_regex pattern for file %s",
fname);
return -1;
}
else {
ht = flb_hash_table_create(FLB_HASH_TABLE_EVICT_NONE,
FLB_HASH_TABLE_SIZE, FLB_HASH_TABLE_SIZE);
flb_regex_parse(tag_regex, &result, cb_results, ht);
for (p = tag, beg = p; (beg = strchr(p, '<')); p = end + 2) {
if (beg != p) {
len = (beg - p);
memcpy(out_buf + buf_s, p, len);
buf_s += len;
}
beg++;
end = strchr(beg, '>');
if (end && !memchr(beg, '<', end - beg)) {
end--;
len = end - beg + 1;
ret = flb_hash_table_get(ht, beg, len, (void *) &tmp, &tmp_s);
if (ret != -1) {
memcpy(out_buf + buf_s, tmp, tmp_s);
buf_s += tmp_s;
}
else {
memcpy(out_buf + buf_s, "_", 1);
buf_s++;
}
}
else {
flb_plg_error(ctx->ins,
"missing closing angle bracket in tag %s "
"at position %lu", tag, beg - tag);
flb_hash_table_destroy(ht);
return -1;
}
}
flb_hash_table_destroy(ht);
if (*p) {
len = strlen(p);
memcpy(out_buf + buf_s, p, len);
buf_s += len;
}
}
}
else {
#endif
p = strchr(tag, '*');
if (!p) {
return -1;
}
/* Copy tag prefix if any */
len = (p - tag);
if (len > 0) {
memcpy(out_buf, tag, len);
buf_s += len;
}
/* Append file name */
len = strlen(fname);
memcpy(out_buf + buf_s, fname, len);
buf_s += len;
/* Tag suffix (if any) */
p++;
if (*p) {
len = strlen(tag);
memcpy(out_buf + buf_s, p, (len - (p - tag)));
buf_s += (len - (p - tag));
}
/* Sanitize buffer */
for (i = 0; i < buf_s; i++) {
if (out_buf[i] == '/' || out_buf[i] == '\\' || out_buf[i] == ':') {
if (i > 0) {
out_buf[i] = '.';
}
else {
drop_bytes(out_buf, buf_s, i, 1);
buf_s--;
i--;
}
}
if (i > 0 && out_buf[i] == '.') {
if (out_buf[i - 1] == '.') {
drop_bytes(out_buf, buf_s, i, 1);
buf_s--;
i--;
}
}
else if (out_buf[i] == '*') {
drop_bytes(out_buf, buf_s, i, 1);
buf_s--;
i--;
}
}
/* Check for an ending '.' */
if (out_buf[buf_s - 1] == '.') {
drop_bytes(out_buf, buf_s, buf_s - 1, 1);
buf_s--;
}
#ifdef FLB_HAVE_REGEX
}
#endif
out_buf[buf_s] = '\0';
*out_size = buf_s;
return 0;
}
static inline int flb_tail_file_exists_old(struct stat *st,
struct flb_tail_config *ctx)
{
struct mk_list *head;
struct flb_tail_file *file;
/* Iterate static list */
mk_list_foreach(head, &ctx->files_static) {
file = mk_list_entry(head, struct flb_tail_file, _head);
if (file->inode == st->st_ino) {
return FLB_TRUE;
}
}
/* Iterate dynamic list */
mk_list_foreach(head, &ctx->files_event) {
file = mk_list_entry(head, struct flb_tail_file, _head);
if (file->inode == st->st_ino) {
return FLB_TRUE;
}
}
return FLB_FALSE;
}
static inline int flb_tail_file_exists(struct stat *st,
struct flb_tail_config *ctx)
{
int ret;
uint64_t hash;
ret = stat_to_hash_bits(ctx, st, &hash);
if (ret != 0) {
return -1;
}
/* static hash */
if (flb_hash_table_exists(ctx->static_hash, hash)) {
return FLB_TRUE;
}
/* event hash */
if (flb_hash_table_exists(ctx->event_hash, hash)) {
return FLB_TRUE;
}
return FLB_FALSE;
}
/*
* Based in the configuration or database offset, set the proper 'offset' for the
* file in question.
*/
static int set_file_position(struct flb_tail_config *ctx,
struct flb_tail_file *file)
{
int64_t ret;
#ifdef FLB_HAVE_SQLDB
/*
* If the database option is enabled, try to gather the file position. The
* database function updates the file->offset entry.
*/
if (ctx->db) {
ret = flb_tail_db_file_set(file, ctx);
if (ret == 0) {
if (file->offset > 0) {
ret = lseek(file->fd, file->offset, SEEK_SET);
if (ret == -1) {
flb_errno();
return -1;
}
}
else if (ctx->read_from_head == FLB_FALSE) {
ret = lseek(file->fd, 0, SEEK_END);
if (ret == -1) {
flb_errno();
return -1;
}
file->offset = ret;
flb_tail_db_file_offset(file, ctx);
}
return 0;
}
}
#endif
if (ctx->read_from_head == FLB_TRUE) {
/* no need to seek, offset position is already zero */
return 0;
}
/* tail... */
ret = lseek(file->fd, 0, SEEK_END);
if (ret == -1) {
flb_errno();
return -1;
}
file->offset = ret;
return 0;
}
/* Multiline flush callback: invoked every time some content is complete */
static int ml_flush_callback(struct flb_ml_parser *parser,
struct flb_ml_stream *mst,
void *data, char *buf_data, size_t buf_size)
{
size_t mult_size = 0;
char *mult_buf = NULL;
struct flb_tail_file *file = data;
struct flb_tail_config *ctx = file->config;
if (ctx->path_key == NULL && ctx->offset_key == NULL) {
ml_stream_buffer_append(file, buf_data, buf_size);
}
else {
/* adjust the records in a new buffer */
record_append_custom_keys(file,
buf_data,
buf_size,
&mult_buf, &mult_size);
ml_stream_buffer_append(file, mult_buf, mult_size);
flb_free(mult_buf);
}
if (mst->forced_flush) {
ml_stream_buffer_flush(ctx, file);
}
return 0;
}
int flb_tail_file_append(char *path, struct stat *st, int mode,
struct flb_tail_config *ctx)
{
int fd;
int ret;
uint64_t stream_id;
uint64_t ts;
uint64_t hash_bits;
flb_sds_t hash_key;
size_t len;
char *tag;
char *name;
size_t tag_len;
struct flb_tail_file *file;
struct stat lst;
flb_sds_t inode_str;
if (!S_ISREG(st->st_mode)) {
return -1;
}
if (flb_tail_file_exists(st, ctx) == FLB_TRUE) {
return -1;
}
fd = open(path, O_RDONLY);
if (fd == -1) {
flb_errno();
flb_plg_error(ctx->ins, "cannot open %s", path);
return -1;
}
file = flb_calloc(1, sizeof(struct flb_tail_file));
if (!file) {
flb_errno();
goto error;
}
/* Initialize */
file->watch_fd = -1;
file->fd = fd;
/* On non-windows environments check if the original path is a link */
ret = lstat(path, &lst);
if (ret == 0) {
if (S_ISLNK(lst.st_mode)) {
file->is_link = FLB_TRUE;
file->link_inode = lst.st_ino;
}
}
/* get unique hash for this file */
ret = stat_to_hash_bits(ctx, st, &hash_bits);
if (ret != 0) {
flb_plg_error(ctx->ins, "error procesisng hash bits for file %s", path);
goto error;
}
file->hash_bits = hash_bits;
/* store the hash key used for hash_bits */
ret = stat_to_hash_key(ctx, st, &hash_key);
if (ret != 0) {
flb_plg_error(ctx->ins, "error procesisng hash key for file %s", path);
goto error;
}
file->hash_key = hash_key;
file->inode = st->st_ino;
file->offset = 0;
file->size = st->st_size;
file->buf_len = 0;
file->parsed = 0;
file->config = ctx;
file->tail_mode = mode;
file->tag_len = 0;
file->tag_buf = NULL;
file->rotated = 0;
file->pending_bytes = 0;
file->mult_firstline = FLB_FALSE;
file->mult_keys = 0;
file->mult_flush_timeout = 0;
file->mult_skipping = FLB_FALSE;
/*
* Duplicate string into 'file' structure, the called function
* take cares to resolve real-name of the file in case we are
* running in a non-Linux system.
*
* Depending of the operating system, the way to obtain the file
* name associated to it file descriptor can have different behaviors
* specifically if it root path it's under a symbolic link. On Linux
* we can trust the file name but in others it's better to solve it
* with some extra calls.
*/
ret = flb_tail_file_name_dup(path, file);
if (!file->name) {
flb_errno();
goto error;
}
/* We keep a copy of the initial filename in orig_name. This is required
* for path_key to continue working after rotation. */
file->orig_name = flb_strdup(file->name);
if (!file->orig_name) {
flb_free(file->name);
flb_errno();
goto error;
}
file->orig_name_len = file->name_len;
/* multiline msgpack buffers */
file->mult_records = 0;
msgpack_sbuffer_init(&file->mult_sbuf);
msgpack_packer_init(&file->mult_pck, &file->mult_sbuf,
msgpack_sbuffer_write);