1 /*
2  * db_berkeley module, portions of this code were templated using
3  * the dbtext and postgres modules.
4 
5  * Copyright (C) 2007 Cisco Systems
6  *
7  * This file is part of Kamailio, a free SIP server.
8  *
9  * Kamailio is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version
13  *
14  * Kamailio is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22  *
23  */
24 
25 /*! \file
26  * Berkeley DB : Module interface
27  *
28  * \ingroup database
29  */
30 
31 
32 #include <stdio.h>
33 #include <unistd.h>
34 #include <sys/stat.h>
35 
36 
37 #include "../../core/str.h"
38 #include "../../core/ut.h"
39 #include "../../core/mem/mem.h"
40 
41 #include "../../core/sr_module.h"
42 #include "../../lib/srdb2/db_res.h"
43 #include "../../lib/srdb2/db.h"
44 
45 #include "bdb_lib.h"
46 #include "bdb_con.h"
47 #include "bdb_uri.h"
48 #include "bdb_fld.h"
49 #include "bdb_res.h"
50 #include "bdb_cmd.h"
51 #include "db_berkeley.h"
52 
53 MODULE_VERSION
54 
55 int auto_reload = 0;
56 int log_enable = 0;
57 int journal_roll_interval = 0;
58 
59 static int bdb_mod_init(void);
60 static void bdb_mod_destroy(void);
61 
62 /*
63  * Exported functions
64  */
65 static cmd_export_t cmds[] = {
66 	{"db_ctx", (cmd_function)NULL, 0, 0, 0, 0},
67 	{"db_con", (cmd_function)bdb_con, 0, 0, 0, 0},
68 	{"db_uri", (cmd_function)bdb_uri, 0, 0, 0, 0},
69 	{"db_cmd", (cmd_function)bdb_cmd, 0, 0, 0, 0},
70 	{"db_put", (cmd_function)bdb_cmd_exec, 0, 0, 0, 0},
71 	{"db_del", (cmd_function)bdb_cmd_exec, 0, 0, 0, 0},
72 	{"db_get", (cmd_function)bdb_cmd_exec, 0, 0, 0, 0},
73 	{"db_upd", (cmd_function)bdb_cmd_exec, 0, 0, 0, 0},
74 	{"db_sql", (cmd_function)bdb_cmd_exec, 0, 0, 0, 0},
75 	{"db_first", (cmd_function)bdb_cmd_first, 0, 0, 0, 0},
76 	{"db_next", (cmd_function)bdb_cmd_next, 0, 0, 0, 0},
77 	{"db_res", (cmd_function)bdb_res, 0, 0, 0, 0},
78 	{"db_fld", (cmd_function)bdb_fld, 0, 0, 0, 0},
79 	{"db_bind_api", (cmd_function)bdb_bind_api, 0, 0, 0, 0},
80 	{0, 0, 0, 0, 0, 0}
81 };
82 
83 /*
84  * Exported parameters
85  */
86 static param_export_t params[] = {{"auto_reload", INT_PARAM, &auto_reload},
87 		{"log_enable", INT_PARAM, &log_enable},
88 		{"journal_roll_interval", INT_PARAM, &journal_roll_interval},
89 		{0, 0, 0}};
90 
91 struct module_exports exports = {
92 	"db_berkeley",   /* module name */
93 	DEFAULT_DLFLAGS, /* dlopen flags */
94 	cmds,            /* cmd (cfg function) exports */
95 	params,          /* param exports */
96 	0,               /* RPC method exports */
97 	0,               /* pseudo-variables exports */
98 	0,               /* response handling function */
99 	bdb_mod_init,    /* module init function */
100 	0,               /* per-child init function */
101 	bdb_mod_destroy  /* module destroy function */
102 };
103 
mod_register(char * path,int * dlflags,void * p1,void * p2)104 int mod_register(char *path, int *dlflags, void *p1, void *p2)
105 {
106 	if(db_api_init() < 0)
107 		return -1;
108 	return 0;
109 }
110 
bdb_mod_init(void)111 static int bdb_mod_init(void)
112 {
113 	bdb_params_t p;
114 
115 	p.auto_reload = auto_reload;
116 	p.log_enable = log_enable;
117 	p.cache_size = (4 * 1024 * 1024); //4Mb
118 	p.journal_roll_interval = journal_roll_interval;
119 
120 	if(bdblib_init(&p))
121 		return -1;
122 
123 	return km_mod_init();
124 }
125 
bdb_mod_destroy(void)126 static void bdb_mod_destroy(void)
127 {
128 	km_destroy();
129 	bdblib_destroy();
130 }
131