new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / logcollector / main.c
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All right reserved.
3  *
4  * This program is a free software; you can redistribute it
5  * and/or modify it under the terms of the GNU General Public
6  * License (version 2) as published by the FSF - Free Software
7  * Foundation
8  */
9
10 /* Logcollector daemon
11  * Monitor some files and forward the output to our analysis system
12  */
13
14 #include <sys/types.h>
15 #include <sys/time.h>
16 #include <stdio.h>
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <fcntl.h>
21
22 #include "os_regex/os_regex.h"
23 #include "logcollector.h"
24
25 /* Prototypes */
26 static void help_logcollector(void) __attribute__((noreturn));
27
28
29 /* Print help statement */
30 static void help_logcollector()
31 {
32     print_header();
33     print_out("  %s: -[Vhdtf] [-c config]", ARGV0);
34     print_out("    -V          Version and license message");
35     print_out("    -h          This help message");
36     print_out("    -d          Execute in debug mode. This parameter");
37     print_out("                can be specified multiple times");
38     print_out("                to increase the debug level.");
39     print_out("    -t          Test configuration");
40     print_out("    -f          Run in foreground");
41     print_out("    -c <config> Configuration file to use (default: %s)", DEFAULTCPATH);
42     print_out(" ");
43     exit(1);
44 }
45
46 int main(int argc, char **argv)
47 {
48     int c;
49     int debug_level = 0;
50     int test_config = 0, run_foreground = 0;
51     int accept_manager_commands = 0;
52     const char *cfg = DEFAULTCPATH;
53
54     /* Setup random */
55     srandom_init();
56
57     /* Set the name */
58     OS_SetName(ARGV0);
59
60     while ((c = getopt(argc, argv, "Vtdhfc:")) != -1) {
61         switch (c) {
62             case 'V':
63                 print_version();
64                 break;
65             case 'h':
66                 help_logcollector();
67                 break;
68             case 'd':
69                 nowDebug();
70                 debug_level = 1;
71                 break;
72             case 'f':
73                 run_foreground = 1;
74                 break;
75             case 'c':
76                 if (!optarg) {
77                     ErrorExit("%s: -c needs an argument", ARGV0);
78                 }
79                 cfg = optarg;
80                 break;
81             case 't':
82                 test_config = 1;
83                 break;
84             default:
85                 help_logcollector();
86                 break;
87         }
88
89     }
90
91     /* Check current debug_level
92      * Command line setting takes precedence
93      */
94     if (debug_level == 0) {
95         /* Get debug level */
96         debug_level = getDefine_Int("logcollector", "debug", 0, 2);
97         while (debug_level != 0) {
98             nowDebug();
99             debug_level--;
100         }
101     }
102
103     debug1(STARTED_MSG, ARGV0);
104
105     accept_manager_commands = getDefine_Int("logcollector", "remote_commands",
106                                             0, 1);
107
108     /* Read config file */
109     if (LogCollectorConfig(cfg, accept_manager_commands) < 0) {
110         ErrorExit(CONFIG_ERROR, ARGV0, cfg);
111     }
112
113     /* Get 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     /* Exit if test config */
122     if (test_config) {
123         exit(0);
124     }
125
126     /* No file available to monitor -- continue */
127     if (logff == NULL) {
128         os_calloc(2, sizeof(logreader), logff);
129         logff[0].file = NULL;
130         logff[0].ffile = NULL;
131         logff[0].logformat = NULL;
132         logff[0].fp = NULL;
133         logff[1].file = NULL;
134         logff[1].logformat = NULL;
135
136         merror(NO_FILE, ARGV0);
137     }
138
139     /* Start signal handler */
140     StartSIG(ARGV0);
141
142     if (!run_foreground) {
143         /* Going on daemon mode */
144         nowDaemon();
145         goDaemon();
146     }
147
148     /* Create PID file */
149     if (CreatePID(ARGV0, getpid()) < 0) {
150         merror(PID_ERROR, ARGV0);
151     }
152
153     /* Wait 6 seconds for the analysisd/agentd to settle */
154     debug1("%s: DEBUG: Waiting main daemons to settle.", ARGV0);
155     sleep(6);
156
157     /* Start the queue */
158     if ((logr_queue = StartMQ(DEFAULTQPATH, WRITE)) < 0) {
159         ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQPATH);
160     }
161
162     /* Main loop */
163     LogCollectorStart();
164 }
165