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