Imported Upstream version 2.3
[ossec-hids.git] / src / logcollector / main.c
1 /* @(#) $Id: main.c,v 1.27 2009/11/18 19:07:40 dcid Exp $ */
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 3) 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     /* Setting the name */
49     OS_SetName(ARGV0);
50         
51
52     while((c = getopt(argc, argv, "VtdhfD:c:")) != -1)
53     {
54         switch(c)
55         {
56             case 'V':
57                 print_version();
58                 break;
59             case 'h':
60                 help(ARGV0);
61                 break;
62             case 'd':
63                 nowDebug();
64                 break;
65             case 'f':
66                 run_foreground = 1;
67                 break;
68             case 'D':
69                 if(!optarg)
70                     ErrorExit("%s: -D needs an argument",ARGV0);
71                 dir = optarg;
72                 break;
73             case 'c':
74                 if(!optarg)
75                     ErrorExit("%s: -c needs an argument",ARGV0);
76                 cfg = optarg;
77                 break;
78             case 't':
79                 test_config = 1;
80                 break;    
81             default:
82                 help(ARGV0);
83                 break;   
84         }
85
86     }
87
88     debug1(STARTED_MSG,ARGV0);
89
90
91     /* Reading config file */
92     if(LogCollectorConfig(cfg) < 0)
93         ErrorExit(CONFIG_ERROR, ARGV0, cfg);
94     
95     
96     /* Getting loop timeout */
97     loop_timeout = getDefine_Int("logcollector",
98                                  "loop_timeout",
99                                  1, 120);
100     
101     open_file_attempts = getDefine_Int("logcollector", "open_attempts",
102                                        2, 998);
103             
104     debug_flag = getDefine_Int("logcollector",
105                                "debug",
106                                0,2);
107     
108     /* Getting debug values */
109     while(debug_flag != 0)
110     {
111         nowDebug();
112         debug_flag--;
113     }
114
115
116     /* Exit if test config */
117     if(test_config)
118         exit(0);
119         
120
121     /* No file available to monitor -- continue */
122     if(logff == NULL)
123     {
124         os_calloc(2, sizeof(logreader), logff);
125         logff[0].file = NULL;
126         logff[0].ffile = NULL;
127         logff[0].logformat = NULL;
128         logff[0].fp = NULL;
129         logff[1].file = NULL;
130         logff[1].logformat = NULL;
131
132         merror(NO_FILE, ARGV0);
133     }
134             
135
136     /* Starting signal handler */
137     StartSIG(ARGV0);    
138
139
140     if (!run_foreground) 
141     {
142         /* Going on daemon mode */
143         nowDaemon();
144         goDaemon();
145     }
146
147
148     /* Creating PID file */
149     if(CreatePID(ARGV0, getpid()) < 0)
150         merror(PID_ERROR, ARGV0);
151
152    
153    
154     /* Waiting 6 seconds for the analysisd/agentd to settle */
155     debug1("%s: DEBUG: Waiting main daemons to settle.", ARGV0);
156     sleep(6);
157     
158      
159     /* Starting the queue. */
160     if((logr_queue = StartMQ(DEFAULTQPATH,WRITE)) < 0)
161         ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQPATH);
162
163
164     /* Main loop */        
165     LogCollectorStart();
166     
167
168     return(0);
169 }
170
171
172
173 /* EOF */