-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
logcollector.c
2907 lines (2408 loc) · 92.2 KB
/
logcollector.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) 2015-2021, Wazuh Inc.
* Copyright (C) 2009 Trend Micro Inc.
* All right reserved.
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public
* License (version 2) as published by the FSF - Free Software
* Foundation
*/
#include "shared.h"
#include "logcollector.h"
#include "state.h"
#include <math.h>
#include <pthread.h>
// Remove STATIC qualifier from tests
#ifdef WAZUH_UNIT_TESTING
#define STATIC
#else
#define STATIC static
#endif
#define MAX_ASCII_LINES 10
#define MAX_UTF8_CHARS 1400
#define OFFSET_SIZE 21 ///< Maximum 64-bit integer is 20-char long, plus 1 because of the '\0'
/* Prototypes */
static int update_fname(int i, int j);
static int update_current(logreader **current, int *i, int *j);
static void set_read(logreader *current, int i, int j);
static IT_control remove_duplicates(logreader *current, int i, int j);
static int find_duplicate_inode(logreader * lf);
static void set_sockets();
static void files_lock_init(void);
static void check_text_only();
static int check_pattern_expand(int do_seek);
static void check_pattern_expand_excluded();
static void set_can_read(int value);
/**
* @brief Create files_status hash and load the previous estatus from JSON file
*/
STATIC void w_initialize_file_status();
/**
* @brief Before stop logcollector save the files_status hash on JSON file
*/
STATIC void w_save_file_status();
/**
* @brief Load files_status data to hash
* @param global_json json wich contains the previous files_status hash
*/
STATIC void w_load_files_status(cJSON *global_json);
/**
* @brief Parse the hash files_status to JSON
* @return json of all read status files in a string
*/
STATIC char * w_save_files_status_to_cJSON();
/**
* @brief Set file on the last line read or on the end in case the status hasn't been saved.
* @param lf logreader to set
* @return 0 on success, otherwise -1
*/
STATIC int w_set_to_last_line_read(logreader *lf);
/**
* @brief Set file on the end
* @param lf logreader to set
* @return 0 on success, otherwise -1
*/
STATIC int64_t w_set_to_pos(logreader *lf, int64_t pos, int mode);
/**
* @brief Update or add (if it not exit) hash node
* @param path Hash key
* @param pos Offset of hash
* @return 0 on success, otherwise -1
*/
STATIC int w_update_hash_node(char * path, int64_t pos);
/* Global variables */
int loop_timeout;
int logr_queue;
int open_file_attempts;
logreader *logff;
logreader_glob *globs;
logsocket *logsk;
int vcheck_files;
int maximum_lines;
int sample_log_length;
int force_reload;
int reload_interval;
int reload_delay;
int free_excluded_files_interval;
int state_interval;
OSHash * msg_queues_table;
///< To asociate the path, the position to read, and the hash key of lines read.
OSHash * files_status;
///< Use for log messages
char *files_status_name = "file_status";
static int _cday = 0;
int N_INPUT_THREADS = N_MIN_INPUT_THREADS;
int OUTPUT_QUEUE_SIZE = OUTPUT_MIN_QUEUE_SIZE;
logsocket default_agent = { .name = "agent" };
logtarget default_target[2] = { { .log_socket = &default_agent } };
/* Output thread variables */
static pthread_mutex_t mutex;
#ifdef WIN32
static pthread_mutex_t win_el_mutex;
static pthread_mutexattr_t win_el_mutex_attr;
#endif
/* can read synchronization */
static int _can_read = 0;
static pthread_rwlock_t can_read_rwlock;
/* Multiple readers / one write mutex */
static pthread_rwlock_t files_update_rwlock;
static OSHash *excluded_files = NULL;
static OSHash *excluded_binaries = NULL;
/* Handle file management */
void LogCollectorStart()
{
int i = 0, j = -1, tg;
int f_check = 0;
int f_reload = 0;
int f_free_excluded = 0;
IT_control f_control = 0;
IT_control duplicates_removed = 0;
logreader *current;
/* Create store data */
excluded_files = OSHash_Create();
if (!excluded_files) {
merror_exit(LIST_ERROR);
}
/* Create store for binaries data */
excluded_binaries = OSHash_Create();
if (!excluded_binaries) {
merror_exit(LIST_ERROR);
}
/* Initialize status file struct (files_status) and set w_save_file_status at the process exit */
w_initialize_file_status();
if (atexit(w_save_file_status)) {
merror(ATEXIT_ERROR);
}
/* Initialize state component */
if (state_interval == 0) {
w_logcollector_state_init(LC_STATE_GLOBAL, false);
} else if (state_interval > 0) {
w_logcollector_state_init(LC_STATE_GLOBAL | LC_STATE_INTERVAL, true);
}
/* Create the state thread */
#ifndef WIN32
w_create_thread(w_logcollector_state_main, (void *) &state_interval);
#else
w_create_thread(NULL,
0,
w_logcollector_state_main,
(void *) &state_interval,
0,
NULL);
#endif
set_sockets();
files_lock_init();
// Check for expanded files
check_pattern_expand(1);
check_pattern_expand_excluded();
w_mutex_init(&mutex, NULL);
#ifndef WIN32
/* To check for inode changes */
struct stat tmp_stat;
/* Check for ASCII, UTF-8 */
check_text_only();
/* Set the files mutexes */
w_set_file_mutexes();
#else
BY_HANDLE_FILE_INFORMATION lpFileInformation;
memset(&lpFileInformation, 0, sizeof(BY_HANDLE_FILE_INFORMATION));
const char *m_uname;
m_uname = getuname();
/* Check if we are on Windows Vista */
if (!checkVista()) {
minfo("Windows version is older than 6.0. (%s).", m_uname);
} else {
minfo("Windows version is 6.0 or newer. (%s).", m_uname);
}
/* Read vista descriptions */
if (isVista) {
win_read_vista_sec();
}
/* Check for ASCII, UTF-8 */
check_text_only();
w_mutexattr_init(&win_el_mutex_attr);
w_mutexattr_settype(&win_el_mutex_attr, PTHREAD_MUTEX_ERRORCHECK);
#endif
mdebug1("Entering LogCollectorStart().");
/* Initialize each file and structure */
for (i = 0;; i++) {
if (f_control = update_current(¤t, &i, &j), f_control) {
if (f_control == NEXT_IT) {
continue;
} else {
break;
}
}
/* Remove duplicate entries */
/* Returns NEXT_IT if duplicates were removed, LEAVE_IT if an error occurred
or CONTINUE_IT to continue with the current iteration */
duplicates_removed = remove_duplicates(current, i, j);
if (duplicates_removed == NEXT_IT) {
i--;
continue;
}
if (!current->file) {
/* Do nothing, duplicated entry */
} else if (!strcmp(current->logformat, "eventlog")) {
#ifdef WIN32
minfo(READING_EVTLOG, current->file);
os_strdup(current->file, current->channel_str);
win_startel(current->file);
/* Mutexes are not previously initialized under Windows*/
w_mutex_init(¤t->mutex, &win_el_mutex_attr);
#else
free(current->file);
#endif
current->file = NULL;
current->command = NULL;
current->fp = NULL;
} else if (!strcmp(current->logformat, "eventchannel")) {
#ifdef WIN32
#ifdef EVENTCHANNEL_SUPPORT
minfo(READING_EVTLOG, current->file);
os_strdup(current->file, current->channel_str);
win_start_event_channel(current->file, current->future, current->query, current->reconnect_time);
#else
mwarn("eventchannel not available on this version of Windows");
#endif
/* Mutexes are not previously initialized under Windows*/
w_mutex_init(¤t->mutex, &win_el_mutex_attr);
#else
free(current->file);
#endif
current->file = NULL;
current->command = NULL;
current->fp = NULL;
} else if (strcmp(current->logformat, "command") == 0) {
current->file = NULL;
current->fp = NULL;
current->size = 0;
#ifdef WIN32
/* Mutexes are not previously initialized under Windows*/
w_mutex_init(¤t->mutex, &win_el_mutex_attr);
#endif
if (current->command) {
current->read = read_command;
minfo("Monitoring output of command(%d): %s", current->ign, current->command);
tg = 0;
if (current->target) {
while (current->target[tg]) {
mdebug1("Socket target for '%s' -> %s", current->command, current->target[tg]);
tg++;
}
}
if (!current->alias) {
os_strdup(current->command, current->alias);
}
} else {
merror("Missing command argument. Ignoring it.");
}
} else if (strcmp(current->logformat, "full_command") == 0) {
current->file = NULL;
current->fp = NULL;
current->size = 0;
#ifdef WIN32
/* Mutexes are not previously initialized under Windows*/
w_mutex_init(¤t->mutex, &win_el_mutex_attr);
#endif
if (current->command) {
current->read = read_fullcommand;
minfo("Monitoring full output of command(%d): %s", current->ign, current->command);
tg = 0;
if (current->target){
while (current->target[tg]) {
mdebug1("Socket target for '%s' -> %s", current->command, current->target[tg]);
tg++;
}
}
if (!current->alias) {
os_strdup(current->command, current->alias);
}
} else {
merror("Missing command argument. Ignoring it.");
}
}
else if (j < 0) {
set_read(current, i, j);
if (current->file) {
minfo(READING_FILE, current->file);
}
/* More tweaks for Windows. For some reason IIS places
* some weird characters at the end of the files and getc
* always returns 0 (even after clearerr).
*/
#ifdef WIN32
if (current->fp) {
if (current->future == 0) {
w_set_to_last_line_read(current);
} else {
int64_t offset = w_set_to_pos(current, 0, SEEK_END);
w_update_hash_node(current->file, offset);
}
}
/* Mutexes are not previously initialized under Windows*/
w_mutex_init(¤t->mutex, &win_el_mutex_attr);
#endif
} else {
/* On Windows we need to forward the seek for wildcard files */
#ifdef WIN32
if (current->file) {
minfo(READING_FILE, current->file);
}
if (current->fp) {
if (current->future == 0) {
w_set_to_last_line_read(current);
} else {
int64_t offset = w_set_to_pos(current, 0, SEEK_END);
w_update_hash_node(current->file, offset);
}
}
#endif
}
if (current->alias) {
int ii = 0;
while (current->alias[ii] != '\0') {
if (current->alias[ii] == ':') {
current->alias[ii] = '\\';
}
ii++;
}
}
}
//Save status localfiles to disk
w_save_file_status();
// Initialize message queue's log builder
mq_log_builder_init();
/* Create the output threads */
w_create_output_threads();
/* Create the input threads */
w_create_input_threads();
/* Start up message */
minfo(STARTUP_MSG, (int)getpid());
mdebug1(CURRENT_FILES, current_files, maximum_files);
#ifndef WIN32
// Start com request thread
w_create_thread(lccom_main, NULL);
#endif
set_can_read(1);
/* Daemon loop */
while (1) {
/* Free hash table content for excluded files */
if (f_free_excluded >= free_excluded_files_interval) {
set_can_read(0); // Stop reading threads
w_rwlock_wrlock(&files_update_rwlock);
set_can_read(1); // Clean signal once we have the lock
mdebug1("Refreshing excluded files list.");
OSHash_Free(excluded_files);
excluded_files = OSHash_Create();
if (!excluded_files) {
merror_exit(LIST_ERROR);
}
OSHash_Free(excluded_binaries);
excluded_binaries = OSHash_Create();
if (!excluded_binaries) {
merror_exit(LIST_ERROR);
}
f_free_excluded = 0;
w_rwlock_unlock(&files_update_rwlock);
}
if (f_check >= vcheck_files) {
set_can_read(0); // Stop reading threads
w_rwlock_wrlock(&files_update_rwlock);
set_can_read(1); // Clean signal once we have the lock
int i;
int j = -1;
f_reload += f_check;
mdebug1("Performing file check.");
// Force reload, if enabled
if (force_reload && f_reload >= reload_interval) {
struct timespec delay = { reload_delay / 1000, (reload_delay % 1000) * 1000000 };
// Close files
for (i = 0, j = -1;; i++) {
if (f_control = update_current(¤t, &i, &j), f_control) {
if (f_control == NEXT_IT) {
continue;
} else {
break;
}
}
if (current->file && current->fp) {
close_file(current);
}
}
// Delay: yield mutex
w_rwlock_unlock(&files_update_rwlock);
if (reload_delay) {
nanosleep(&delay, NULL);
}
set_can_read(0); // Stop reading threads
w_rwlock_wrlock(&files_update_rwlock);
set_can_read(1); // Clean signal once we have the lock
// Open files again, and restore position
for (i = 0, j = -1;; i++) {
if (f_control = update_current(¤t, &i, &j), f_control) {
if (f_control == NEXT_IT) {
continue;
} else {
break;
}
}
if (current->file && current->exists) {
if (reload_file(current) == -1) {
minfo(FORGET_FILE, current->file);
os_file_status_t * old_file_status = OSHash_Delete_ex(files_status, current->file);
os_free(old_file_status);
w_logcollector_state_delete_file(current->file);
current->exists = 0;
current->ign++;
// Only expanded files that have been deleted will be forgotten
if (j >= 0) {
if (Remove_Localfile(&(globs[j].gfiles), i, 1, 0,&globs[j])) {
merror(REM_ERROR, current->file);
} else {
mdebug1(CURRENT_FILES, current_files, maximum_files);
i--;
continue;
}
} else if (open_file_attempts) {
mdebug1(OPEN_ATTEMPT, current->file, open_file_attempts - current->ign);
} else {
mdebug1(OPEN_UNABLE, current->file);
}
}
}
}
}
/* Check if any file has been renamed/removed */
for (i = 0, j = -1;; i++) {
if (f_control = update_current(¤t, &i, &j), f_control) {
if (f_control == NEXT_IT) {
continue;
} else {
break;
}
}
/* These are the windows logs or ignored files */
if (!current->file) {
continue;
}
/* Files with date -- check for day change */
if (current->ffile) {
if (update_fname(i, j)) {
if (current->fp) {
fclose(current->fp);
#ifdef WIN32
CloseHandle(current->h);
#endif
}
current->fp = NULL;
current->exists = 1;
handle_file(i, j, 0, 1);
continue;
}
/* Variable file name */
else if (!current->fp && open_file_attempts - current->ign > 0) {
handle_file(i, j, 1, 1);
continue;
}
}
/* Check for file change -- if the file is open already */
if (current->fp) {
#ifndef WIN32
/* To help detect a file rollover, temporarily open the file a second time.
* Previously the fstat would work on "cached" file data, but this should
* ensure it's fresh when hardlinks are used (like alerts.log).
*/
FILE *tf;
tf = fopen(current->file, "r");
if(tf == NULL) {
if (errno == ENOENT) {
if(current->exists==1){
minfo(FORGET_FILE, current->file);
os_file_status_t * old_file_status = OSHash_Delete_ex(files_status, current->file);
os_free(old_file_status);
w_logcollector_state_delete_file(current->file);
current->exists = 0;
}
current->ign++;
// Only expanded files that have been deleted will be forgotten
if (j >= 0) {
if (Remove_Localfile(&(globs[j].gfiles), i, 1, 0,&globs[j])) {
merror(REM_ERROR, current->file);
} else {
mdebug1(CURRENT_FILES, current_files, maximum_files);
i--;
continue;
}
} else if (open_file_attempts) {
mdebug1(OPEN_ATTEMPT, current->file, open_file_attempts - current->ign);
} else {
mdebug1(OPEN_UNABLE, current->file);
}
} else {
merror(FOPEN_ERROR, current->file, errno, strerror(errno));
}
}
else if ((fstat(fileno(tf), &tmp_stat)) == -1) {
fclose(current->fp);
fclose(tf);
current->fp = NULL;
merror(FSTAT_ERROR, current->file, errno, strerror(errno));
}
else if (fclose(tf) == EOF) {
merror("Closing the temporary file %s did not work (%d): %s", current->file, errno, strerror(errno));
}
#else
HANDLE h1;
h1 = CreateFile(current->file, GENERIC_READ,
FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (h1 == INVALID_HANDLE_VALUE) {
fclose(current->fp);
CloseHandle(current->h);
current->fp = NULL;
minfo(LOGCOLLECTOR_INVALID_HANDLE_VALUE, current->file);
} else if (GetFileInformationByHandle(h1, &lpFileInformation) == 0) {
fclose(current->fp);
CloseHandle(current->h);
CloseHandle(h1);
current->fp = NULL;
minfo(LOGCOLLECTOR_INVALID_HANDLE_VALUE, current->file);
}
if (!current->fp) {
if(current->exists==1){
minfo(FORGET_FILE, current->file);
os_file_status_t * old_file_status = OSHash_Delete_ex(files_status, current->file);
os_free(old_file_status);
w_logcollector_state_delete_file(current->file);
current->exists = 0;
}
current->ign++;
// Only expanded files that have been deleted will be forgotten
if (j >= 0) {
if (Remove_Localfile(&(globs[j].gfiles), i, 1, 0,&globs[j])) {
merror(REM_ERROR, current->file);
} else {
mdebug2(CURRENT_FILES, current_files, maximum_files);
i--;
continue;
}
} else if (open_file_attempts) {
mdebug1(OPEN_ATTEMPT, current->file, open_file_attempts - current->ign);
} else {
mdebug1(OPEN_UNABLE, current->file);
}
}
#endif
#ifdef WIN32
else if (current->fd != (lpFileInformation.nFileIndexLow + lpFileInformation.nFileIndexHigh))
#else
else if (current->fd != tmp_stat.st_ino)
#endif
{
current->exists = 1;
char msg_alert[512 + 1];
snprintf(msg_alert, 512, "ossec: File rotated (inode "
"changed): '%s'.",
current->file);
/* Send message about log rotated */
w_msg_hash_queues_push(msg_alert, "wazuh-logcollector", strlen(msg_alert) + 1, default_target, LOCALFILE_MQ);
mdebug1("File inode changed. %s",
current->file);
os_file_status_t * old_file_status = OSHash_Delete_ex(files_status, current->file);
os_free(old_file_status);
w_logcollector_state_delete_file(current->file);
fclose(current->fp);
#ifdef WIN32
CloseHandle(current->h);
CloseHandle(h1);
#endif
current->fp = NULL;
handle_file(i, j, 0, 1);
continue;
}
#ifdef WIN32
else if ((DWORD)current->size > (lpFileInformation.nFileSizeHigh + lpFileInformation.nFileSizeLow))
#else
else if (current->size > tmp_stat.st_size)
#endif
{
current->exists = 1;
char msg_alert[512 + 1];
snprintf(msg_alert, 512, "ossec: File size reduced "
"(inode remained): '%s'.",
current->file);
/* Send message about log rotated */
w_msg_hash_queues_push(msg_alert, "wazuh-logcollector", strlen(msg_alert) + 1, default_target, LOCALFILE_MQ);
mdebug1("File size reduced. %s",
current->file);
/* Get new file */
os_file_status_t * old_file_status = OSHash_Delete_ex(files_status, current->file);
os_free(old_file_status);
w_logcollector_state_delete_file(current->file);
fclose(current->fp);
#ifdef WIN32
CloseHandle(current->h);
CloseHandle(h1);
#endif
current->fp = NULL;
handle_file(i, j, 0, 1);
} else {
#ifdef WIN32
CloseHandle(h1);
/* Update file size */
current->size = lpFileInformation.nFileSizeHigh + lpFileInformation.nFileSizeLow;
#else
current->exists = 1;
current->size = tmp_stat.st_size;
#endif
}
} else {
#ifdef WIN32
if (!current->command && strcmp(current->logformat,EVENTCHANNEL) && strcmp(current->logformat,EVENTLOG)) {
int file_exists = 1;
HANDLE h1;
h1 = CreateFile(current->file, GENERIC_READ,
FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (h1 == INVALID_HANDLE_VALUE) {
if (current->h) {
CloseHandle(current->h);
}
mdebug1(LOGCOLLECTOR_INVALID_HANDLE_VALUE, current->file);
file_exists = 0;
w_logcollector_state_delete_file(current->file);
} else if (GetFileInformationByHandle(h1, &lpFileInformation) == 0) {
if (current->h) {
CloseHandle(current->h);
}
mdebug1(LOGCOLLECTOR_INVALID_HANDLE_VALUE, current->file);
file_exists = 0;
w_logcollector_state_delete_file(current->file);
}
CloseHandle(h1);
// Only expanded files that have been deleted will be forgotten
if (j >= 0) {
if (!file_exists) {
if (Remove_Localfile(&(globs[j].gfiles), i, 1, 0, &globs[j])) {
merror(REM_ERROR, current->file);
} else {
mdebug2(CURRENT_FILES, current_files, maximum_files);
i--;
continue;
}
}
} else if (open_file_attempts) {
mdebug1(OPEN_ATTEMPT, current->file, open_file_attempts - current->ign);
} else {
mdebug1(OPEN_UNABLE, current->file);
}
}
#endif
}
/* If open_file_attempts is at 0 the files aren't forgotted ever*/
if(open_file_attempts == 0){
current->ign = -1;
}
/* Too many errors for the file */
if (current->ign >= open_file_attempts) {
/* 999 Maximum ignore */
if (current->ign == 999) {
continue;
}
if(!strcmp(current->logformat, "eventchannel")){
mdebug1(LOGC_FILE_ERROR, current->file);
} else {
minfo(LOGC_FILE_ERROR, current->file);
}
if (current->fp) {
fclose(current->fp);
#ifdef WIN32
CloseHandle(current->h);
#endif
}
current->fp = NULL;
current->ign = 999;
if (j >= 0) {
#ifndef WIN32
struct stat stat_fd;
if (stat(current->file, &stat_fd) == -1 && ENOENT == errno) {
#else
if (!PathFileExists(current->file)) {
#endif
os_file_status_t * old_file_status = OSHash_Delete_ex(files_status, current->file);
os_free(old_file_status);
w_logcollector_state_delete_file(current->file);
if (Remove_Localfile(&(globs[j].gfiles), i, 1, 0,&globs[j])) {
merror(REM_ERROR, current->file);
} else {
mdebug1(CURRENT_FILES, current_files, maximum_files);
i--;
}
} else {
#ifndef WIN32
merror(FSTAT_ERROR, current->file, errno, strerror(errno));
#endif
}
}
continue;
}
/* File not open */
if (!current->fp) {
if (current->ign >= 999) {
continue;
} else {
/* Try for a few times to open the file */
handle_file(i, j, 1, 1);
continue;
}
}
}
// Check for new files to be expanded
if (check_pattern_expand(1)) {
/* Remove duplicate entries */
for (i = 0, j = -1;; i++) {
if (f_control = update_current(¤t, &i, &j), f_control) {
if (f_control == NEXT_IT) {
continue;
} else {
break;
}
}
duplicates_removed = remove_duplicates(current, i, j);
if (duplicates_removed == NEXT_IT) {
i--;
continue;
}
}
}
/* Check for excluded files */
check_pattern_expand_excluded();
/* Check for ASCII, UTF-8 */
check_text_only();
w_rwlock_unlock(&files_update_rwlock);
if (f_reload >= reload_interval) {
f_reload = 0;
}
//Save status localfiles to disk
w_save_file_status();
f_check = 0;
if (mq_log_builder_update() == -1) {
mdebug1("Output log pattern data could not be updated.");
}
}
sleep(1);
f_check++;
f_free_excluded++;
}
}
int update_fname(int i, int j)
{
time_t __ctime = time(0);
char lfile[OS_FLSIZE + 1];
size_t ret;
logreader *lf;
struct tm tm_result = { .tm_sec = 0 };
if (j < 0) {
lf = &logff[i];
} else {
lf = &globs[j].gfiles[i];
}
localtime_r(&__ctime, &tm_result);
/* Handle file */
if (tm_result.tm_mday == _cday) {
return (0);
}
lfile[OS_FLSIZE] = '\0';
ret = strftime(lfile, OS_FLSIZE, lf->ffile, &tm_result);
if (ret == 0) {
merror_exit(PARSE_ERROR, lf->ffile);
}
/* Update the filename */
if (strcmp(lfile, lf->file)) {
os_free(lf->file);
os_strdup(lfile, lf->file);
minfo(VAR_LOG_MON, lf->file);
/* Setting cday to zero because other files may need
* to be changed.
*/
_cday = 0;
return (1);
}
_cday = tm_result.tm_mday;
return (0);
}
/* Open, get the fileno, seek to the end and update mtime */
int handle_file(int i, int j, __attribute__((unused)) int do_fseek, int do_log)
{
int fd;
logreader *lf;
if (j < 0) {
lf = &logff[i];
} else {
lf = &globs[j].gfiles[i];
}
/* We must be able to open the file, fseek and get the
* time of change from it.
*/
#ifndef WIN32
struct stat stat_fd = { .st_mode = 0 };
lf->fp = fopen(lf->file, "r");
if (!lf->fp) {
if (do_log == 1 && lf->exists == 1) {
merror(FOPEN_ERROR, lf->file, errno, strerror(errno));
lf->exists = 0;
}
goto error;
}
/* Get inode number for fp */
fd = fileno(lf->fp);
if (fstat(fd, &stat_fd) == -1) {
merror(FSTAT_ERROR, lf->file, errno, strerror(errno));
fclose(lf->fp);
lf->fp = NULL;
goto error;
}
lf->fd = stat_fd.st_ino;
lf->size = stat_fd.st_size;
lf->dev = stat_fd.st_dev;
#else
BY_HANDLE_FILE_INFORMATION lpFileInformation;
memset(&lpFileInformation, 0, sizeof(BY_HANDLE_FILE_INFORMATION));
lf->fp = NULL;
lf->h = CreateFile(lf->file, GENERIC_READ,
FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (lf->h == INVALID_HANDLE_VALUE) {
if (do_log == 1) {
DWORD error = GetLastError();
merror(FOPEN_ERROR, lf->file, (int)error, win_strerror(error));
}
goto error;
}
fd = _open_osfhandle((intptr_t)lf->h, 0);
if (fd == -1) {
merror(FOPEN_ERROR, lf->file, errno, strerror(errno));
CloseHandle(lf->h);
goto error;