izmjene licence
[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_level = 0;
44     int test_config = 0,run_foreground = 0;
45     int accept_manager_commands = 0;
46     char *cfg = DEFAULTCPATH;
47     // TODO: delete or implement
48     char *dir __attribute__((unused)) = DEFAULTDIR;
49
50
51     /* Setuping up random */
52     #ifndef WIN32
53         #ifdef __OpenBSD__
54         srandomdev();
55         #else
56         srandom(time(0));
57         #endif
58     #else
59     srandom(time(0))
60     #endif
61
62     /* Setting the name */
63     OS_SetName(ARGV0);
64
65
66     while((c = getopt(argc, argv, "VtdhfD:c:")) != -1)
67     {
68         switch(c)
69         {
70             case 'V':
71                 print_version();
72                 break;
73             case 'h':
74                 help(ARGV0);
75                 break;
76             case 'd':
77                 nowDebug();
78                 debug_level = 1;
79                 break;
80             case 'f':
81                 run_foreground = 1;
82                 break;
83             case 'D':
84                 if(!optarg)
85                     ErrorExit("%s: -D needs an argument",ARGV0);
86                 dir = optarg;
87                 break;
88             case 'c':
89                 if(!optarg)
90                     ErrorExit("%s: -c needs an argument",ARGV0);
91                 cfg = optarg;
92                 break;
93             case 't':
94                 test_config = 1;
95                 break;
96             default:
97                 help(ARGV0);
98                 break;
99         }
100
101     }
102
103     /* Check current debug_level
104      * Command line setting takes precedence
105      */
106     if (debug_level == 0)
107     {
108         /* Getting debug level */
109         debug_level = getDefine_Int("logcollector", "debug", 0, 2);
110         while(debug_level != 0)
111         {
112             nowDebug();
113             debug_level--;
114         }
115     }
116
117
118     debug1(STARTED_MSG,ARGV0);
119
120
121     accept_manager_commands = getDefine_Int("logcollector", "remote_commands",
122                                        0, 1);
123
124
125     /* Reading config file */
126     if(LogCollectorConfig(cfg, accept_manager_commands) < 0)
127         ErrorExit(CONFIG_ERROR, ARGV0, cfg);
128
129
130     /* Getting loop timeout */
131     loop_timeout = getDefine_Int("logcollector",
132                                  "loop_timeout",
133                                  1, 120);
134
135     open_file_attempts = getDefine_Int("logcollector", "open_attempts",
136                                        2, 998);
137
138     accept_manager_commands = getDefine_Int("logcollector", "remote_commands",
139                                        0, 1);
140
141
142     /* Exit if test config */
143     if(test_config)
144         exit(0);
145
146
147     /* No file available to monitor -- continue */
148     if(logff == NULL)
149     {
150         os_calloc(2, sizeof(logreader), logff);
151         logff[0].file = NULL;
152         logff[0].ffile = NULL;
153         logff[0].logformat = NULL;
154         logff[0].fp = NULL;
155         logff[1].file = NULL;
156         logff[1].logformat = NULL;
157
158         merror(NO_FILE, ARGV0);
159     }
160
161
162     /* Starting signal handler */
163     StartSIG(ARGV0);
164
165
166     if (!run_foreground)
167     {
168         /* Going on daemon mode */
169         nowDaemon();
170         goDaemon();
171     }
172
173
174     /* Creating PID file */
175     if(CreatePID(ARGV0, getpid()) < 0)
176         merror(PID_ERROR, ARGV0);
177
178
179
180     /* Waiting 6 seconds for the analysisd/agentd to settle */
181     debug1("%s: DEBUG: Waiting main daemons to settle.", ARGV0);
182     sleep(6);
183
184
185     /* Starting the queue. */
186     if((logr_queue = StartMQ(DEFAULTQPATH,WRITE)) < 0)
187         ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQPATH);
188
189
190     /* Main loop */
191     LogCollectorStart();
192
193
194     return(0);
195 }
196
197
198
199 /* EOF */