-
Notifications
You must be signed in to change notification settings - Fork 910
/
ul_db_watch.c
234 lines (216 loc) · 5.32 KB
/
ul_db_watch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* sp-ul_db module
*
* Copyright (C) 2007 1&1 Internet AG
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* Kamailio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../core/timer.h"
#include "../../core/timer_proc.h"
#include "../../core/sr_module.h"
#include "ul_db_watch.h"
#include "ul_db.h"
#include "p_usrloc_mod.h"
typedef struct ul_db_watch_list {
int id;
int active;
struct ul_db_watch_list * next;
} ul_db_watch_list_t;
ul_db_watch_list_t ** list = NULL;
gen_lock_t * list_lock;
static ul_db_handle_list_t * handles;
void check_dbs(unsigned int ticks, void *param);
static int init_watch_db_list(void);
int ul_db_watch_init(void){
if(init_watch_db_list() < 0){
return -1;
}
if((list = shm_malloc(sizeof(ul_db_watch_list_t *))) == NULL){
LM_ERR("couldn't allocate shared memory.\n");
return -1;
}
*list = NULL;
return 0;
}
int init_db_check(void){
int ret;
if(db_master_write){
LM_INFO("start timer, interval %i seconds\n", retry_interval);
ret = fork_dummy_timer(PROC_TIMER, "TIMER UL WATCH", 1, check_dbs, NULL, retry_interval);
} else {
ret = 0;
}
return ret;
}
void ul_db_watch_destroy(void){
ul_db_watch_list_t * del;
ul_db_handle_list_t * del2;
if(list_lock){
lock_destroy(list_lock);
lock_dealloc(list_lock);
list_lock = NULL;
}
if(list){
while(list && *list){
del = *list;
*list = (*list)->next;
shm_free(del);
}
shm_free(list);
list = NULL;
}
while(handles){
del2 = handles;
handles = handles->next;
pkg_free(del2);
}
return;
}
void check_dbs(unsigned int ticks, void *param){
LM_DBG("check availability of databases");
ul_db_watch_list_t * tmp;
ul_db_handle_list_t * tmp2, * new_element;
int found;
int i;
if(mdb_availability_control) {
check_master_db(db_master_write);
}
if(!list_lock){
return;
}
lock_get(list_lock);
tmp = *list;
while(tmp){
tmp2 = handles;
found = 0;
while(tmp2){
if(tmp2->handle->id == tmp->id){
found = 1;
if(tmp->active){
LM_INFO("handle %i found, check it\n", tmp->id);
tmp2->handle->active = 1;
ul_db_check(tmp2->handle);
} else if (tmp2->handle->active) {
for(i=0; i<DB_NUM; i++){
if(tmp2->handle->db[i].dbh){
tmp2->handle->db[i].dbf.close(tmp2->handle->db[i].dbh);
tmp2->handle->db[i].dbh = NULL;
}
}
tmp2->handle->active = 0;
}
}
tmp2 = tmp2->next;
}
if(!found){
LM_NOTICE("handle %i not found, create it\n", tmp->id);
if((new_element = pkg_malloc(sizeof(ul_db_handle_list_t))) == NULL){
LM_ERR("couldn't allocate private memory\n");
lock_release(list_lock);
return;
}
memset(new_element, 0, sizeof(ul_db_handle_list_t));
if((new_element->handle = pkg_malloc(sizeof(ul_db_handle_t))) == NULL){
LM_ERR("couldn't allocate private memory\n");
pkg_free(new_element);
lock_release(list_lock);
return;
}
memset(new_element->handle, 0, sizeof(ul_db_handle_t));
new_element->next = handles;
handles = new_element;
handles->handle->id = tmp->id;
ul_db_check(handles->handle);
}
tmp = tmp->next;
}
lock_release(list_lock);
}
void check_master_db(int dbm_write_default) {
if(mdb_availability_control) {
if(mdb.write.dbh){
mdb.write.dbf.close(mdb.write.dbh);
mdb.write.dbh = NULL;
}
lock_get(&write_on_master_db_shared->lock);
if((mdb.write.dbh = mdb.write.dbf.init(mdb.write.url)) == NULL) {
write_on_master_db_shared->val = 0;
LM_WARN("Master db is unavailable.\n");
} else {
write_on_master_db_shared->val = dbm_write_default;
}
lock_release(&write_on_master_db_shared->lock);
}
}
int ul_register_watch_db(int id){
ul_db_watch_list_t * new_id = NULL, * tmp;
if(!list_lock){
if(init_watch_db_list() < 0){
return -1;
}
}
lock_get(list_lock);
tmp = *list;
while(tmp){
if(tmp->id == id){
tmp->active = 1;
lock_release(list_lock);
return 0;
}
tmp = tmp->next;
}
if((new_id = shm_malloc(sizeof(ul_db_watch_list_t))) == NULL){
LM_ERR("couldn't allocate shared memory\n");
lock_release(list_lock);
return -1;
}
memset(new_id, 0, sizeof(ul_db_watch_list_t));
new_id->active = 1;
new_id->next = *list;
new_id->id = id;
*list = new_id;
lock_release(list_lock);
return 0;
}
int ul_unregister_watch_db(int id){
ul_db_watch_list_t * tmp;
if(!list_lock){
return 0;
}
lock_get(list_lock);
tmp = *list;
while(tmp){
if(tmp->id == id){
tmp->active = 0;
lock_release(list_lock);
return 0;
}
tmp = tmp->next;
}
lock_release(list_lock);
return 0;
}
static int init_watch_db_list(void){
if((list_lock = lock_alloc()) == NULL){
LM_ERR("could not allocate lock\n");
return -1;
}
if(lock_init(list_lock) == NULL){
LM_ERR("could not initialise lock\n");
return -1;
}
return 0;
}