Imported Upstream version 2.7
[ossec-hids.git] / src / os_dbd / dbd.c
1 /* @(#) $Id: ./src/os_dbd/dbd.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 Trend Micro Inc.
5  * All rights 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  * License details at the LICENSE file included with OSSEC or
13  * online at: http://www.ossec.net/en/licensing.html
14  */
15
16
17 #ifndef DBD
18    #define DBD
19 #endif
20
21 #ifndef ARGV0
22    #define ARGV0 "ossec-dbd"
23 #endif
24
25 #include "shared.h"
26 #include "dbd.h"
27
28
29
30 /* OS_DBD: Monitor the alerts and insert them into the database.
31  * Only return in case of error.
32  */
33 void OS_DBD(DBConfig *db_config)
34 {
35     time_t tm;
36     struct tm *p;
37
38     file_queue *fileq;
39     alert_data *al_data;
40
41
42     /* Getting currently time before starting */
43     tm = time(NULL);
44     p = localtime(&tm); 
45
46
47     /* Initating file queue - to read the alerts */
48     os_calloc(1, sizeof(file_queue), fileq);
49     Init_FileQueue(fileq, p, 0);
50
51
52     /* Creating location hash */
53     db_config->location_hash = OSHash_Create();
54     if(!db_config->location_hash)
55     {
56         ErrorExit(MEM_ERROR, ARGV0);
57     }
58
59
60     /* Getting maximum ID */
61     db_config->alert_id = OS_SelectMaxID(db_config);
62     db_config->alert_id++;
63
64
65     /* Infinite loop reading the alerts and inserting them. */
66     while(1)
67     {
68         tm = time(NULL);
69         p = localtime(&tm);
70
71
72         /* Get message if available (timeout of 5 seconds) */
73         al_data = Read_FileMon(fileq, p, 5);
74         if(!al_data)
75         {
76             continue;
77         }
78
79
80         /* Inserting into the db */
81         OS_Alert_InsertDB(al_data, db_config);
82
83
84         /* Clearing the memory */
85         FreeAlertData(al_data);
86     }
87 }
88
89 /* EOF */