-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatabase.c
228 lines (196 loc) · 5.77 KB
/
database.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
/* app_my: damn simple MySQL application for asterisk
* Copyright (C) 2010 - Steve Frécinaux
* Licensed under the GPL2+
*/
#include <asterisk.h>
#include <asterisk/cli.h>
#include <asterisk/logger.h>
#include <mysql/mysql.h>
#include <mysql/errmsg.h>
#include <stdio.h>
#include <sys/types.h>
#include <time.h>
#include "config.h"
#include "database.h"
AST_MUTEX_DEFINE_STATIC(mysql_lock);
static MYSQL mysql;
static int connected = 0;
static time_t connect_time = 0;
/*****************************************************************************
* Functions *
*****************************************************************************/
MYSQL *
database_get (void)
{
ast_mutex_lock (&mysql_lock);
return &mysql;
}
void
database_release (MYSQL *mysql)
{
if (mysql != NULL)
ast_mutex_unlock (&mysql_lock);
else
ast_log (LOG_ERROR, AST_MODULE ": releasing NULL connection.");
}
void
ast_log_mysql_error (const char *func_name)
{
ast_log (LOG_ERROR,
AST_MODULE ": %s returned %s (%d)\n",
func_name,
mysql_error (&mysql),
mysql_errno (&mysql));
}
int
ensure_mysql_connection (MYSQL *mysql)
{
int retries = 5;
#if MYSQL_VERSION_ID >= 50013
int true_value = 1;
#endif
db_reconnect:
if (!connected)
{
mysql_init (mysql);
/* Add option to quickly timeout the connection */
if (config.timeout != 0 && mysql_options (mysql, MYSQL_OPT_CONNECT_TIMEOUT, (char *) &config.timeout))
ast_log_mysql_error ("mysql_options");
#if MYSQL_VERSION_ID >= 50013
/* Add option for automatic reconnection */
if (mysql_options (mysql, MYSQL_OPT_RECONNECT, (char *) &true_value) != 0)
ast_log_mysql_error ("mysql_options");
#endif
if (mysql_real_connect (mysql, config.hostname, config.username, config.password,
config.database, config.port, NULL, 0))
{
connected = 1;
connect_time = time (NULL);
}
else
{
ast_log (LOG_ERROR,
AST_MODULE ": cannot connect to database server %s.\n",
config.hostname);
connected = 0;
}
}
else
{
/* Long connection - ping the server */
if (mysql_ping (mysql) != 0)
{
connected = 0;
switch (mysql_errno (mysql))
{
case CR_SERVER_GONE_ERROR:
case CR_SERVER_LOST:
ast_log (LOG_ERROR, AST_MODULE ": Server has gone away. Attempting to reconnect.\n");
break;
default:
ast_log_mysql_error ("mysql_ping");
break;
}
retries--;
if (retries)
goto db_reconnect;
ast_log (LOG_ERROR, AST_MODULE ": Retried to connect fives times, giving up.\n");
}
}
return connected;
}
/**
* Drop the existing mysql connection if there is one.
**/
static void
drop_mysql_connection (MYSQL *mysql)
{
if (connected)
{
mysql_close (mysql);
connected = 0;
}
}
/*****************************************************************************
* CLI *
*****************************************************************************/
static int
handle_cli_my_show_status (int fd,
int argc,
char **argv)
{
if (argc != 3)
return RESULT_SHOWUSAGE;
/* Refresh the connection status. */
if (connected && mysql_ping (&mysql) != 0)
connected = 0;
/* The following if{} got borrowed from cdr_addon_mysql.c */
if (connected)
{
char status[255], status2[100] = "", timestr[255];
int ctime = time (NULL) - connect_time;
snprintf (status, sizeof (status), "Connected to %s@%s", config.database, config.hostname);
if (config.username && config.username[0])
snprintf (status2, sizeof (status2), " with username %s", config.username);
if (ctime > 31536000)
{
snprintf (timestr, sizeof (timestr), "%d years, %d days, %d hours, %d minutes, %d seconds",
ctime / 31536000, (ctime % 31536000) / 86400,
(ctime % 86400) / 3600, (ctime % 3600) / 60, ctime % 60);
}
else if (ctime > 86400)
{
snprintf (timestr, sizeof (timestr), "%d days, %d hours, %d minutes, %d seconds",
ctime / 86400, (ctime % 86400) / 3600, (ctime % 3600) / 60, ctime % 60);
}
else if (ctime > 3600)
{
snprintf (timestr, sizeof (timestr), "%d hours, %d minutes, %d seconds",
ctime / 3600, (ctime % 3600) / 60, ctime % 60);
}
else if (ctime > 60)
{
snprintf (timestr, sizeof (timestr), "%d minutes, %d seconds", ctime / 60, ctime % 60);
}
else
{
snprintf (timestr, sizeof (timestr), "%d seconds", ctime);
}
ast_cli(fd, "%s%s for %s.\n", status, status2, timestr);
}
else
{
ast_cli (fd, "Not currently connected to a MySQL server.\n");
}
return RESULT_SUCCESS;
}
static struct ast_cli_entry cli_entry = {
{ "my", "show", "status", NULL },
handle_cli_my_show_status,
"Shows database status"
};
void
database_init (void)
{
ast_mutex_lock (&mysql_lock);
ensure_mysql_connection (&mysql);
ast_mutex_unlock (&mysql_lock);
ast_cli_register (&cli_entry);
}
void
database_reset (void)
{
ast_mutex_lock (&mysql_lock);
drop_mysql_connection (&mysql);
ensure_mysql_connection (&mysql);
ast_mutex_unlock (&mysql_lock);
}
void
database_clean (void)
{
ast_cli_unregister (&cli_entry);
ast_mutex_lock (&mysql_lock);
drop_mysql_connection (&mysql);
ast_mutex_unlock (&mysql_lock);
}
/* ex:set ts=2 et sw=2 ai: */