cel_pgsql.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2008
  5. *
  6. * Steve Murphy - adapted to CEL, from:
  7. * Matthew D. Hardeman <mhardemn@papersoft.com>
  8. * Adapted from the MySQL CDR logger originally by James Sharp
  9. *
  10. * Modified April, 2007; Dec, 2008
  11. * Steve Murphy <murf@digium.com>
  12. * Modified September 2003
  13. * Matthew D. Hardeman <mhardemn@papersoft.com>
  14. *
  15. * See http://www.asterisk.org for more information about
  16. * the Asterisk project. Please do not directly contact
  17. * any of the maintainers of this project for assistance;
  18. * the project provides a web site, mailing lists and IRC
  19. * channels for your use.
  20. *
  21. * This program is free software, distributed under the terms of
  22. * the GNU General Public License Version 2. See the LICENSE file
  23. * at the top of the source tree.
  24. */
  25. /*! \file
  26. *
  27. * \brief PostgreSQL CEL logger
  28. *
  29. * \author Steve Murphy <murf@digium.com>
  30. * \extref PostgreSQL http://www.postgresql.org/
  31. *
  32. * See also
  33. * \arg \ref Config_cel
  34. * \extref PostgreSQL http://www.postgresql.org/
  35. * \ingroup cel_drivers
  36. */
  37. /*** MODULEINFO
  38. <depend>pgsql</depend>
  39. <support_level>extended</support_level>
  40. ***/
  41. #include "asterisk.h"
  42. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  43. #include <libpq-fe.h>
  44. #include "asterisk/config.h"
  45. #include "asterisk/options.h"
  46. #include "asterisk/channel.h"
  47. #include "asterisk/cel.h"
  48. #include "asterisk/module.h"
  49. #include "asterisk/logger.h"
  50. #include "asterisk.h"
  51. #define DATE_FORMAT "%Y-%m-%d %T.%6q"
  52. static char *config = "cel_pgsql.conf";
  53. static char *pghostname = NULL, *pgdbname = NULL, *pgdbuser = NULL, *pgpassword = NULL, *pgdbport = NULL, *table = NULL;
  54. static int connected = 0;
  55. static int maxsize = 512, maxsize2 = 512;
  56. AST_MUTEX_DEFINE_STATIC(pgsql_lock);
  57. static PGconn *conn = NULL;
  58. static PGresult *result = NULL;
  59. static struct ast_event_sub *event_sub = NULL;
  60. struct columns {
  61. char *name;
  62. char *type;
  63. int len;
  64. unsigned int notnull:1;
  65. unsigned int hasdefault:1;
  66. AST_RWLIST_ENTRY(columns) list;
  67. };
  68. static AST_RWLIST_HEAD_STATIC(psql_columns, columns);
  69. #define LENGTHEN_BUF1(size) \
  70. do { \
  71. /* Lengthen buffer, if necessary */ \
  72. if (ast_str_strlen(sql) + size + 1 > ast_str_size(sql)) { \
  73. if (ast_str_make_space(&sql, ((ast_str_size(sql) + size + 3) / 512 + 1) * 512) != 0) { \
  74. ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CDR failed.\n"); \
  75. ast_free(sql); \
  76. ast_free(sql2); \
  77. AST_RWLIST_UNLOCK(&psql_columns); \
  78. return; \
  79. } \
  80. } \
  81. } while (0)
  82. #define LENGTHEN_BUF2(size) \
  83. do { \
  84. if (ast_str_strlen(sql2) + size + 1 > ast_str_size(sql2)) { \
  85. if (ast_str_make_space(&sql2, ((ast_str_size(sql2) + size + 3) / 512 + 1) * 512) != 0) { \
  86. ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CDR failed.\n"); \
  87. ast_free(sql); \
  88. ast_free(sql2); \
  89. AST_RWLIST_UNLOCK(&psql_columns); \
  90. return; \
  91. } \
  92. } \
  93. } while (0)
  94. static void pgsql_log(const struct ast_event *event, void *userdata)
  95. {
  96. struct ast_tm tm;
  97. char timestr[128];
  98. char *pgerror;
  99. struct ast_cel_event_record record = {
  100. .version = AST_CEL_EVENT_RECORD_VERSION,
  101. };
  102. if (ast_cel_fill_record(event, &record)) {
  103. return;
  104. }
  105. ast_mutex_lock(&pgsql_lock);
  106. ast_localtime(&record.event_time, &tm, NULL);
  107. ast_strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
  108. if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) {
  109. conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
  110. if (PQstatus(conn) != CONNECTION_BAD) {
  111. connected = 1;
  112. } else {
  113. pgerror = PQerrorMessage(conn);
  114. ast_log(LOG_ERROR, "cel_pgsql: Unable to connect to database server %s. Calls will not be logged!\n", pghostname);
  115. ast_log(LOG_ERROR, "cel_pgsql: Reason: %s\n", pgerror);
  116. PQfinish(conn);
  117. conn = NULL;
  118. }
  119. }
  120. if (connected) {
  121. struct columns *cur;
  122. struct ast_str *sql = ast_str_create(maxsize), *sql2 = ast_str_create(maxsize2);
  123. char buf[257], escapebuf[513];
  124. const char *value;
  125. int first = 1;
  126. if (!sql || !sql2) {
  127. goto ast_log_cleanup;
  128. }
  129. ast_str_set(&sql, 0, "INSERT INTO %s (", table);
  130. ast_str_set(&sql2, 0, " VALUES (");
  131. #define SEP (first ? "" : ",")
  132. AST_RWLIST_RDLOCK(&psql_columns);
  133. AST_RWLIST_TRAVERSE(&psql_columns, cur, list) {
  134. LENGTHEN_BUF1(strlen(cur->name) + 2);
  135. ast_str_append(&sql, 0, "%s\"%s\"", first ? "" : ",", cur->name);
  136. if (strcmp(cur->name, "eventtime") == 0) {
  137. if (strncmp(cur->type, "int", 3) == 0) {
  138. LENGTHEN_BUF2(13);
  139. ast_str_append(&sql2, 0, "%s%ld", SEP, (long) record.event_time.tv_sec);
  140. } else if (strncmp(cur->type, "float", 5) == 0) {
  141. LENGTHEN_BUF2(31);
  142. ast_str_append(&sql2, 0, "%s%f",
  143. SEP,
  144. (double) record.event_time.tv_sec +
  145. (double) record.event_time.tv_usec / 1000000.0);
  146. } else {
  147. /* char, hopefully */
  148. LENGTHEN_BUF2(31);
  149. ast_localtime(&record.event_time, &tm, NULL);
  150. ast_strftime(buf, sizeof(buf), DATE_FORMAT, &tm);
  151. ast_str_append(&sql2, 0, "%s'%s'", SEP, buf);
  152. }
  153. } else if (strcmp(cur->name, "eventtype") == 0) {
  154. if (cur->type[0] == 'i') {
  155. /* Get integer, no need to escape anything */
  156. LENGTHEN_BUF2(5);
  157. ast_str_append(&sql2, 0, "%s%d", SEP, (int) record.event_type);
  158. } else if (strncmp(cur->type, "float", 5) == 0) {
  159. LENGTHEN_BUF2(31);
  160. ast_str_append(&sql2, 0, "%s%f", SEP, (double) record.event_type);
  161. } else {
  162. /* Char field, probably */
  163. LENGTHEN_BUF2(strlen(record.event_name) + 1);
  164. ast_str_append(&sql2, 0, "%s'%s'", SEP, record.event_name);
  165. }
  166. } else if (strcmp(cur->name, "amaflags") == 0) {
  167. if (strncmp(cur->type, "int", 3) == 0) {
  168. /* Integer, no need to escape anything */
  169. LENGTHEN_BUF2(13);
  170. ast_str_append(&sql2, 0, "%s%d", SEP, record.amaflag);
  171. } else {
  172. /* Although this is a char field, there are no special characters in the values for these fields */
  173. LENGTHEN_BUF2(31);
  174. ast_str_append(&sql2, 0, "%s'%d'", SEP, record.amaflag);
  175. }
  176. } else {
  177. /* Arbitrary field, could be anything */
  178. if (strcmp(cur->name, "userdeftype") == 0) {
  179. value = record.user_defined_name;
  180. } else if (strcmp(cur->name, "cid_name") == 0) {
  181. value = record.caller_id_name;
  182. } else if (strcmp(cur->name, "cid_num") == 0) {
  183. value = record.caller_id_num;
  184. } else if (strcmp(cur->name, "cid_ani") == 0) {
  185. value = record.caller_id_ani;
  186. } else if (strcmp(cur->name, "cid_rdnis") == 0) {
  187. value = record.caller_id_rdnis;
  188. } else if (strcmp(cur->name, "cid_dnid") == 0) {
  189. value = record.caller_id_dnid;
  190. } else if (strcmp(cur->name, "exten") == 0) {
  191. value = record.extension;
  192. } else if (strcmp(cur->name, "context") == 0) {
  193. value = record.context;
  194. } else if (strcmp(cur->name, "channame") == 0) {
  195. value = record.channel_name;
  196. } else if (strcmp(cur->name, "appname") == 0) {
  197. value = record.application_name;
  198. } else if (strcmp(cur->name, "appdata") == 0) {
  199. value = record.application_data;
  200. } else if (strcmp(cur->name, "accountcode") == 0) {
  201. value = record.account_code;
  202. } else if (strcmp(cur->name, "peeraccount") == 0) {
  203. value = record.peer_account;
  204. } else if (strcmp(cur->name, "uniqueid") == 0) {
  205. value = record.unique_id;
  206. } else if (strcmp(cur->name, "linkedid") == 0) {
  207. value = record.linked_id;
  208. } else if (strcmp(cur->name, "userfield") == 0) {
  209. value = record.user_field;
  210. } else if (strcmp(cur->name, "peer") == 0) {
  211. value = record.peer;
  212. } else if (strcmp(cur->name, "extra") == 0) {
  213. value = record.extra;
  214. } else {
  215. value = NULL;
  216. }
  217. if (value == NULL) {
  218. ast_str_append(&sql2, 0, "%sDEFAULT", SEP);
  219. } else if (strncmp(cur->type, "int", 3) == 0) {
  220. long long whatever;
  221. if (value && sscanf(value, "%30lld", &whatever) == 1) {
  222. LENGTHEN_BUF2(26);
  223. ast_str_append(&sql2, 0, "%s%lld", SEP, whatever);
  224. } else {
  225. LENGTHEN_BUF2(2);
  226. ast_str_append(&sql2, 0, "%s0", SEP);
  227. }
  228. } else if (strncmp(cur->type, "float", 5) == 0) {
  229. long double whatever;
  230. if (value && sscanf(value, "%30Lf", &whatever) == 1) {
  231. LENGTHEN_BUF2(51);
  232. ast_str_append(&sql2, 0, "%s%30Lf", SEP, whatever);
  233. } else {
  234. LENGTHEN_BUF2(2);
  235. ast_str_append(&sql2, 0, "%s0", SEP);
  236. }
  237. /* XXX Might want to handle dates, times, and other misc fields here XXX */
  238. } else {
  239. if (value) {
  240. PQescapeStringConn(conn, escapebuf, value, strlen(value), NULL);
  241. } else {
  242. escapebuf[0] = '\0';
  243. }
  244. LENGTHEN_BUF2(strlen(escapebuf) + 3);
  245. ast_str_append(&sql2, 0, "%s'%s'", SEP, escapebuf);
  246. }
  247. }
  248. first = 0;
  249. }
  250. AST_RWLIST_UNLOCK(&psql_columns);
  251. LENGTHEN_BUF1(ast_str_strlen(sql2) + 2);
  252. ast_str_append(&sql, 0, ")%s)", ast_str_buffer(sql2));
  253. ast_verb(11, "[%s]\n", ast_str_buffer(sql));
  254. ast_debug(2, "inserting a CEL record.\n");
  255. /* Test to be sure we're still connected... */
  256. /* If we're connected, and connection is working, good. */
  257. /* Otherwise, attempt reconnect. If it fails... sorry... */
  258. if (PQstatus(conn) == CONNECTION_OK) {
  259. connected = 1;
  260. } else {
  261. ast_log(LOG_WARNING, "Connection was lost... attempting to reconnect.\n");
  262. PQreset(conn);
  263. if (PQstatus(conn) == CONNECTION_OK) {
  264. ast_log(LOG_NOTICE, "Connection reestablished.\n");
  265. connected = 1;
  266. } else {
  267. pgerror = PQerrorMessage(conn);
  268. ast_log(LOG_ERROR, "Unable to reconnect to database server %s. Calls will not be logged!\n", pghostname);
  269. ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
  270. PQfinish(conn);
  271. conn = NULL;
  272. connected = 0;
  273. goto ast_log_cleanup;
  274. }
  275. }
  276. result = PQexec(conn, ast_str_buffer(sql));
  277. if (PQresultStatus(result) != PGRES_COMMAND_OK) {
  278. pgerror = PQresultErrorMessage(result);
  279. ast_log(LOG_WARNING, "Failed to insert call detail record into database!\n");
  280. ast_log(LOG_WARNING, "Reason: %s\n", pgerror);
  281. ast_log(LOG_WARNING, "Connection may have been lost... attempting to reconnect.\n");
  282. PQreset(conn);
  283. if (PQstatus(conn) == CONNECTION_OK) {
  284. ast_log(LOG_NOTICE, "Connection reestablished.\n");
  285. connected = 1;
  286. PQclear(result);
  287. result = PQexec(conn, ast_str_buffer(sql));
  288. if (PQresultStatus(result) != PGRES_COMMAND_OK) {
  289. pgerror = PQresultErrorMessage(result);
  290. ast_log(LOG_ERROR, "HARD ERROR! Attempted reconnection failed. DROPPING CALL RECORD!\n");
  291. ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
  292. }
  293. }
  294. PQclear(result);
  295. goto ast_log_cleanup;
  296. }
  297. PQclear(result);
  298. ast_log_cleanup:
  299. ast_free(sql);
  300. ast_free(sql2);
  301. }
  302. ast_mutex_unlock(&pgsql_lock);
  303. }
  304. static int my_unload_module(void)
  305. {
  306. struct columns *current;
  307. AST_RWLIST_WRLOCK(&psql_columns);
  308. if (event_sub) {
  309. event_sub = ast_event_unsubscribe(event_sub);
  310. event_sub = NULL;
  311. }
  312. if (conn) {
  313. PQfinish(conn);
  314. conn = NULL;
  315. }
  316. if (pghostname) {
  317. ast_free(pghostname);
  318. pghostname = NULL;
  319. }
  320. if (pgdbname) {
  321. ast_free(pgdbname);
  322. pgdbname = NULL;
  323. }
  324. if (pgdbuser) {
  325. ast_free(pgdbuser);
  326. pgdbuser = NULL;
  327. }
  328. if (pgpassword) {
  329. ast_free(pgpassword);
  330. pgpassword = NULL;
  331. }
  332. if (pgdbport) {
  333. ast_free(pgdbport);
  334. pgdbport = NULL;
  335. }
  336. if (table) {
  337. ast_free(table);
  338. table = NULL;
  339. }
  340. while ((current = AST_RWLIST_REMOVE_HEAD(&psql_columns, list))) {
  341. ast_free(current);
  342. }
  343. AST_RWLIST_UNLOCK(&psql_columns);
  344. return 0;
  345. }
  346. static int unload_module(void)
  347. {
  348. return my_unload_module();
  349. }
  350. static int process_my_load_module(struct ast_config *cfg)
  351. {
  352. struct ast_variable *var;
  353. char *pgerror;
  354. const char *tmp;
  355. PGresult *result;
  356. struct columns *cur;
  357. if (!(var = ast_variable_browse(cfg, "global"))) {
  358. ast_log(LOG_WARNING,"CEL pgsql config file missing global section.\n");
  359. return AST_MODULE_LOAD_DECLINE;
  360. }
  361. if (!(tmp = ast_variable_retrieve(cfg,"global","hostname"))) {
  362. ast_log(LOG_WARNING,"PostgreSQL server hostname not specified. Assuming unix socket connection\n");
  363. tmp = ""; /* connect via UNIX-socket by default */
  364. }
  365. if (pghostname)
  366. ast_free(pghostname);
  367. if (!(pghostname = ast_strdup(tmp))) {
  368. ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying host info\n");
  369. return AST_MODULE_LOAD_DECLINE;
  370. }
  371. if (!(tmp = ast_variable_retrieve(cfg, "global", "dbname"))) {
  372. ast_log(LOG_WARNING,"PostgreSQL database not specified. Assuming asterisk\n");
  373. tmp = "asteriskceldb";
  374. }
  375. if (pgdbname)
  376. ast_free(pgdbname);
  377. if (!(pgdbname = ast_strdup(tmp))) {
  378. ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying dbname info\n");
  379. return AST_MODULE_LOAD_DECLINE;
  380. }
  381. if (!(tmp = ast_variable_retrieve(cfg, "global", "user"))) {
  382. ast_log(LOG_WARNING,"PostgreSQL database user not specified. Assuming asterisk\n");
  383. tmp = "asterisk";
  384. }
  385. if (pgdbuser)
  386. ast_free(pgdbuser);
  387. if (!(pgdbuser = ast_strdup(tmp))) {
  388. ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying user info\n");
  389. return AST_MODULE_LOAD_DECLINE;
  390. }
  391. if (!(tmp = ast_variable_retrieve(cfg, "global", "password"))) {
  392. ast_log(LOG_WARNING, "PostgreSQL database password not specified. Assuming blank\n");
  393. tmp = "";
  394. }
  395. if (pgpassword)
  396. ast_free(pgpassword);
  397. if (!(pgpassword = ast_strdup(tmp))) {
  398. ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying password info\n");
  399. return AST_MODULE_LOAD_DECLINE;
  400. }
  401. if (!(tmp = ast_variable_retrieve(cfg,"global","port"))) {
  402. ast_log(LOG_WARNING,"PostgreSQL database port not specified. Using default 5432.\n");
  403. tmp = "5432";
  404. }
  405. if (pgdbport)
  406. ast_free(pgdbport);
  407. if (!(pgdbport = ast_strdup(tmp))) {
  408. ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying port info\n");
  409. return AST_MODULE_LOAD_DECLINE;
  410. }
  411. if (!(tmp = ast_variable_retrieve(cfg, "global", "table"))) {
  412. ast_log(LOG_WARNING,"CEL table not specified. Assuming cel\n");
  413. tmp = "cel";
  414. }
  415. if (table)
  416. ast_free(table);
  417. if (!(table = ast_strdup(tmp))) {
  418. return AST_MODULE_LOAD_DECLINE;
  419. }
  420. if (option_debug) {
  421. if (ast_strlen_zero(pghostname)) {
  422. ast_debug(3, "cel_pgsql: using default unix socket\n");
  423. } else {
  424. ast_debug(3, "cel_pgsql: got hostname of %s\n", pghostname);
  425. }
  426. ast_debug(3, "cel_pgsql: got port of %s\n", pgdbport);
  427. ast_debug(3, "cel_pgsql: got user of %s\n", pgdbuser);
  428. ast_debug(3, "cel_pgsql: got dbname of %s\n", pgdbname);
  429. ast_debug(3, "cel_pgsql: got password of %s\n", pgpassword);
  430. ast_debug(3, "cel_pgsql: got sql table name of %s\n", table);
  431. }
  432. conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
  433. if (PQstatus(conn) != CONNECTION_BAD) {
  434. char sqlcmd[512];
  435. char *fname, *ftype, *flen, *fnotnull, *fdef;
  436. char *tableptr;
  437. int i, rows;
  438. ast_debug(1, "Successfully connected to PostgreSQL database.\n");
  439. connected = 1;
  440. /* Remove any schema name from the table */
  441. if ((tableptr = strrchr(table, '.'))) {
  442. tableptr++;
  443. } else {
  444. tableptr = table;
  445. }
  446. /* Query the columns */
  447. snprintf(sqlcmd, sizeof(sqlcmd), "select a.attname, t.typname, a.attlen, a.attnotnull, d.adsrc from pg_class c, pg_type t, pg_attribute a left outer join pg_attrdef d on a.atthasdef and d.adrelid = a.attrelid and d.adnum = a.attnum where c.oid = a.attrelid and a.atttypid = t.oid and (a.attnum > 0) and c.relname = '%s' order by c.relname, attnum", tableptr);
  448. result = PQexec(conn, sqlcmd);
  449. if (PQresultStatus(result) != PGRES_TUPLES_OK) {
  450. pgerror = PQresultErrorMessage(result);
  451. ast_log(LOG_ERROR, "Failed to query database columns: %s\n", pgerror);
  452. PQclear(result);
  453. unload_module();
  454. return AST_MODULE_LOAD_DECLINE;
  455. }
  456. rows = PQntuples(result);
  457. for (i = 0; i < rows; i++) {
  458. fname = PQgetvalue(result, i, 0);
  459. ftype = PQgetvalue(result, i, 1);
  460. flen = PQgetvalue(result, i, 2);
  461. fnotnull = PQgetvalue(result, i, 3);
  462. fdef = PQgetvalue(result, i, 4);
  463. ast_verb(4, "Found column '%s' of type '%s'\n", fname, ftype);
  464. cur = ast_calloc(1, sizeof(*cur) + strlen(fname) + strlen(ftype) + 2);
  465. if (cur) {
  466. sscanf(flen, "%30d", &cur->len);
  467. cur->name = (char *)cur + sizeof(*cur);
  468. cur->type = (char *)cur + sizeof(*cur) + strlen(fname) + 1;
  469. strcpy(cur->name, fname);
  470. strcpy(cur->type, ftype);
  471. if (*fnotnull == 't') {
  472. cur->notnull = 1;
  473. } else {
  474. cur->notnull = 0;
  475. }
  476. if (!ast_strlen_zero(fdef)) {
  477. cur->hasdefault = 1;
  478. } else {
  479. cur->hasdefault = 0;
  480. }
  481. AST_RWLIST_INSERT_TAIL(&psql_columns, cur, list);
  482. }
  483. }
  484. PQclear(result);
  485. } else {
  486. pgerror = PQerrorMessage(conn);
  487. ast_log(LOG_ERROR, "cel_pgsql: Unable to connect to database server %s. CALLS WILL NOT BE LOGGED!!\n", pghostname);
  488. ast_log(LOG_ERROR, "cel_pgsql: Reason: %s\n", pgerror);
  489. connected = 0;
  490. }
  491. return AST_MODULE_LOAD_SUCCESS;
  492. }
  493. static int my_load_module(int reload)
  494. {
  495. struct ast_config *cfg;
  496. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  497. if ((cfg = ast_config_load(config, config_flags)) == NULL || cfg == CONFIG_STATUS_FILEINVALID) {
  498. ast_log(LOG_WARNING, "Unable to load config for PostgreSQL CEL's: %s\n", config);
  499. return AST_MODULE_LOAD_DECLINE;
  500. } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  501. return AST_MODULE_LOAD_SUCCESS;
  502. }
  503. process_my_load_module(cfg);
  504. ast_config_destroy(cfg);
  505. event_sub = ast_event_subscribe(AST_EVENT_CEL, pgsql_log, "CEL PGSQL backend", NULL, AST_EVENT_IE_END);
  506. if (!event_sub) {
  507. ast_log(LOG_WARNING, "Unable to subscribe to CEL events for pgsql\n");
  508. return AST_MODULE_LOAD_DECLINE;
  509. }
  510. return AST_MODULE_LOAD_SUCCESS;
  511. }
  512. static int load_module(void)
  513. {
  514. return my_load_module(0);
  515. }
  516. static int reload(void)
  517. {
  518. my_unload_module();
  519. return my_load_module(1);
  520. }
  521. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PostgreSQL CEL Backend",
  522. .load = load_module,
  523. .unload = unload_module,
  524. .reload = reload,
  525. .load_pri = AST_MODPRI_CDR_DRIVER,
  526. );