8608a586ecc9cecdf0cf17df3efadc957683f1b9
[ossec-hids.git] / src / logcollector / main.c
1 /* @(#) $Id$ */
2
3 /* Copyright (C) 2009 Trend Micro Inc.
4  * All right reserved.
5  *
6  * This program is a free software; you can redistribute it
7  * and/or modify it under the terms of the GNU General Public
8  * License (version 2) as published by the FSF - Free Software
9  * Foundation
10  */
11
12
13 /* v0.4 (2005/11/11): Some cleanup and bug fixes
14  * v0.3 (2005/08/26): Reading all files in just one process 
15  * v0.2 (2005/04/04):
16  */  
17
18
19 /* Logcollector daemon.
20  * Monitor some files and forward the output to our analysis system.
21  */
22
23
24 #include <sys/types.h>
25 #include <sys/time.h>
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <fcntl.h>
31
32 #include "os_regex/os_regex.h"
33
34 #include "logcollector.h"
35
36
37
38 /* main: v0.3: 2005/04/04 */
39 int main(int argc, char **argv)
40 {
41     int c;
42     int debug_flag = 0;
43     int test_config = 0,run_foreground = 0;
44     char *cfg = DEFAULTCPATH;
45     char *dir = DEFAULTDIR;
46
47
48     /* Setuping up random */
49     #ifndef WIN32
50         #ifdef __OpenBSD__
51         srandomdev();
52         #else
53         srandom(time(0));
54         #endif
55     #else
56     srandom(time(0))
57     #endif
58
59     /* Setting the name */
60     OS_SetName(ARGV0);
61         
62
63     while((c = getopt(argc, argv, "VtdhfD:c:")) != -1)
64     {
65         switch(c)
66         {
67             case 'V':
68                 print_version();
69                 break;
70             case 'h':
71                 help(ARGV0);
72                 break;
73             case 'd':
74                 nowDebug();
75                 break;
76             case 'f':
77                 run_foreground = 1;
78                 break;
79             case 'D':
80                 if(!optarg)
81                     ErrorExit("%s: -D needs an argument",ARGV0);
82                 dir = optarg;
83                 break;
84             case 'c':
85                 if(!optarg)
86                     ErrorExit("%s: -c needs an argument",ARGV0);
87                 cfg = optarg;
88                 break;
89             case 't':
90                 test_config = 1;
91                 break;    
92             default:
93                 help(ARGV0);
94                 break;   
95         }
96
97     }
98
99     debug1(STARTED_MSG,ARGV0);
100
101
102     /* Reading config file */
103     if(LogCollectorConfig(cfg) < 0)
104         ErrorExit(CONFIG_ERROR, ARGV0, cfg);
105     
106     
107     /* Getting loop timeout */
108     loop_timeout = getDefine_Int("logcollector",
109                                  "loop_timeout",
110                                  1, 120);
111     
112     open_file_attempts = getDefine_Int("logcollector", "open_attempts",
113                                        2, 998);
114             
115     debug_flag = getDefine_Int("logcollector",
116                                "debug",
117                                0,2);
118     
119     /* Getting debug values */
120     while(debug_flag != 0)
121     {
122         nowDebug();
123         debug_flag--;
124     }
125
126
127     /* Exit if test config */
128     if(test_config)
129         exit(0);
130         
131
132     /* No file available to monitor -- continue */
133     if(logff == NULL)
134     {
135         os_calloc(2, sizeof(logreader), logff);
136         logff[0].file = NULL;
137         logff[0].ffile = NULL;
138         logff[0].logformat = NULL;
139         logff[0].fp = NULL;
140         logff[1].file = NULL;
141         logff[1].logformat = NULL;
142
143         merror(NO_FILE, ARGV0);
144     }
145             
146
147     /* Starting signal handler */
148     StartSIG(ARGV0);    
149
150
151     if (!run_foreground) 
152     {
153         /* Going on daemon mode */
154         nowDaemon();
155         goDaemon();
156     }
157
158
159     /* Creating PID file */
160     if(CreatePID(ARGV0, getpid()) < 0)
161         merror(PID_ERROR, ARGV0);
162
163    
164    
165     /* Waiting 6 seconds for the analysisd/agentd to settle */
166     debug1("%s: DEBUG: Waiting main daemons to settle.", ARGV0);
167     sleep(6);
168     
169      
170     /* Starting the queue. */
171     if((logr_queue = StartMQ(DEFAULTQPATH,WRITE)) < 0)
172         ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQPATH);
173
174
175     /* Main loop */        
176     LogCollectorStart();
177     
178
179     return(0);
180 }
181
182
183
184 /* EOF */