new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / os_dbd / dbd.c
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All rights 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 #include "shared.h"
11 #include "dbd.h"
12
13 #ifndef ARGV0
14 #define ARGV0 "ossec-dbd"
15 #endif
16
17
18 /* Monitor the alerts and insert them into the database
19  * Only returns in case of error
20  */
21 void OS_DBD(DBConfig *db_config)
22 {
23     time_t tm;
24     struct tm *p;
25     file_queue *fileq;
26     alert_data *al_data;
27
28     /* Get current time before starting */
29     tm = time(NULL);
30     p = localtime(&tm);
31
32     /* Initialize file queue to read the alerts */
33     os_calloc(1, sizeof(file_queue), fileq);
34     Init_FileQueue(fileq, p, 0);
35
36     /* Create location hash */
37     db_config->location_hash = OSHash_Create();
38     if (!db_config->location_hash) {
39         ErrorExit(MEM_ERROR, ARGV0, errno, strerror(errno));
40     }
41
42     /* Get maximum ID */
43     db_config->alert_id = OS_SelectMaxID(db_config);
44     db_config->alert_id++;
45
46     /* Infinite loop reading the alerts and inserting them */
47     while (1) {
48         tm = time(NULL);
49         p = localtime(&tm);
50
51         /* Get message if available (timeout of 5 seconds) */
52         al_data = Read_FileMon(fileq, p, 5);
53         if (!al_data) {
54             continue;
55         }
56
57         /* Insert into the db */
58         OS_Alert_InsertDB(al_data, db_config);
59
60         /* Clear the memory */
61         FreeAlertData(al_data);
62     }
63 }
64