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. <defaultenabled>no</defaultenabled>
  41. ***/
  42. #include "asterisk.h"
  43. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  44. #include <libpq-fe.h>
  45. #include "asterisk/config.h"
  46. #include "asterisk/options.h"
  47. #include "asterisk/channel.h"
  48. #include "asterisk/cel.h"
  49. #include "asterisk/module.h"
  50. #include "asterisk/logger.h"
  51. #include "asterisk.h"
  52. #define DATE_FORMAT "%Y-%m-%d %T.%6q"
  53. static char *config = "cel_pgsql.conf";
  54. static char *pghostname = NULL, *pgdbname = NULL, *pgdbuser = NULL, *pgpassword = NULL, *pgdbport = NULL, *table = NULL;
  55. static int connected = 0;
  56. static int maxsize = 512, maxsize2 = 512;
  57. AST_MUTEX_DEFINE_STATIC(pgsql_lock);
  58. static PGconn *conn = NULL;
  59. static PGresult *result = NULL;
  60. static struct ast_event_sub *event_sub = NULL;
  61. struct columns {
  62. char *name;
  63. char *type;
  64. int len;
  65. unsigned int notnull:1;
  66. unsigned int hasdefault:1;
  67. AST_RWLIST_ENTRY(columns) list;
  68. };
  69. static AST_RWLIST_HEAD_STATIC(psql_columns, columns);
  70. #define LENGTHEN_BUF1(size) \
  71. do { \
  72. /* Lengthen buffer, if necessary */ \
  73. if (ast_str_strlen(sql) + size + 1 > ast_str_size(sql)) { \
  74. if (ast_str_make_space(&sql, ((ast_str_size(sql) + size + 3) / 512 + 1) * 512) != 0) { \
  75. ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CDR failed.\n"); \
  76. ast_free(sql); \
  77. ast_free(sql2); \
  78. AST_RWLIST_UNLOCK(&psql_columns); \
  79. return; \
  80. } \
  81. } \
  82. } while (0)
  83. #define LENGTHEN_BUF2(size) \
  84. do { \
  85. if (ast_str_strlen(sql2) + size + 1 > ast_str_size(sql2)) { \
  86. if (ast_str_make_space(&sql2, ((ast_str_size(sql2) + size + 3) / 512 + 1) * 512) != 0) { \
  87. ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CDR failed.\n"); \
  88. ast_free(sql); \
  89. ast_free(sql2); \
  90. AST_RWLIST_UNLOCK(&psql_columns); \
  91. return; \
  92. } \
  93. } \
  94. } while (0)
  95. static void pgsql_log(const struct ast_event *event, void *userdata)
  96. {
  97. struct ast_tm tm;
  98. char timestr[128];
  99. char *pgerror;
  100. struct ast_cel_event_record record = {
  101. .version = AST_CEL_EVENT_RECORD_VERSION,
  102. };
  103. if (ast_cel_fill_record(event, &record)) {
  104. return;
  105. }
  106. ast_mutex_lock(&pgsql_lock);
  107. ast_localtime(&record.event_time, &tm, NULL);
  108. ast_strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
  109. if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) {
  110. conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
  111. if (PQstatus(conn) != CONNECTION_BAD) {
  112. connected = 1;
  113. } else {
  114. pgerror = PQerrorMessage(conn);
  115. ast_log(LOG_ERROR, "cel_pgsql: Unable to connect to database server %s. Calls will not be logged!\n", pghostname);
  116. ast_log(LOG_ERROR, "cel_pgsql: Reason: %s\n", pgerror);
  117. PQfinish(conn);
  118. conn = NULL;
  119. }
  120. }
  121. if (connected) {
  122. struct columns *cur;
  123. struct ast_str *sql = ast_str_create(maxsize), *sql2 = ast_str_create(maxsize2);
  124. char buf[257], escapebuf[513];
  125. const char *value;
  126. int first = 1;
  127. if (!sql || !sql2) {
  128. goto ast_log_cleanup;
  129. }
  130. ast_str_set(&sql, 0, "INSERT INTO %s (", table);
  131. ast_str_set(&sql2, 0, " VALUES (");
  132. #define SEP (first ? "" : ",")
  133. AST_RWLIST_RDLOCK(&psql_columns);
  134. AST_RWLIST_TRAVERSE(&psql_columns, cur, list) {
  135. LENGTHEN_BUF1(strlen(cur->name) + 2);
  136. ast_str_append(&sql, 0, "%s\"%s\"", first ? "" : ",", cur->name);
  137. if (strcmp(cur->name, "eventtime") == 0) {
  138. if (strncmp(cur->type, "int", 3) == 0) {
  139. LENGTHEN_BUF2(13);
  140. ast_str_append(&sql2, 0, "%s%ld", SEP, (long) record.event_time.tv_sec);
  141. } else if (strncmp(cur->type, "float", 5) == 0) {
  142. LENGTHEN_BUF2(31);
  143. ast_str_append(&sql2, 0, "%s%f",
  144. SEP,
  145. (double) record.event_time.tv_sec +
  146. (double) record.event_time.tv_usec / 1000000.0);
  147. } else {
  148. /* char, hopefully */
  149. LENGTHEN_BUF2(31);
  150. ast_localtime(&record.event_time, &tm, NULL);
  151. ast_strftime(buf, sizeof(buf), DATE_FORMAT, &tm);
  152. ast_str_append(&sql2, 0, "%s'%s'", SEP, buf);
  153. }
  154. } else if (strcmp(cur->name, "eventtype") == 0) {
  155. if (cur->type[0] == 'i') {
  156. /* Get integer, no need to escape anything */
  157. LENGTHEN_BUF2(5);
  158. ast_str_append(&sql2, 0, "%s%d", SEP, (int) record.event_type);
  159. } else if (strncmp(cur->type, "float", 5) == 0) {
  160. LENGTHEN_BUF2(31);
  161. ast_str_append(&sql2, 0, "%s%f", SEP, (double) record.event_type);
  162. } else {
  163. /* Char field, probably */
  164. LENGTHEN_BUF2(strlen(record.event_name) + 1);
  165. ast_str_append(&sql2, 0, "%s'%s'", SEP, record.event_name);
  166. }
  167. } else if (strcmp(cur->name, "amaflags") == 0) {
  168. if (strncmp(cur->type, "int", 3) == 0) {
  169. /* Integer, no need to escape anything */
  170. LENGTHEN_BUF2(13);
  171. ast_str_append(&sql2, 0, "%s%d", SEP, record.amaflag);
  172. } else {
  173. /* Although this is a char field, there are no special characters in the values for these fields */
  174. LENGTHEN_BUF2(31);
  175. ast_str_append(&sql2, 0, "%s'%d'", SEP, record.amaflag);
  176. }
  177. } else {
  178. /* Arbitrary field, could be anything */
  179. if (strcmp(cur->name, "userdeftype") == 0) {
  180. value = record.user_defined_name;
  181. } else if (strcmp(cur->name, "cid_name") == 0) {
  182. value = record.caller_id_name;
  183. } else if (strcmp(cur->name, "cid_num") == 0) {
  184. value = record.caller_id_num;
  185. } else if (strcmp(cur->name, "cid_ani") == 0) {
  186. value = record.caller_id_ani;
  187. } else if (strcmp(cur->name, "cid_rdnis") == 0) {
  188. value = record.caller_id_rdnis;
  189. } else if (strcmp(cur->name, "cid_dnid") == 0) {
  190. value = record.caller_id_dnid;
  191. } else if (strcmp(cur->name, "exten") == 0) {
  192. value = record.extension;
  193. } else if (strcmp(cur->name, "context") == 0) {
  194. value = record.context;
  195. } else if (strcmp(cur->name, "channame") == 0) {
  196. value = record.channel_name;
  197. } else if (strcmp(cur->name, "appname") == 0) {
  198. value = record.application_name;
  199. } else if (strcmp(cur->name, "appdata") == 0) {
  200. value = record.application_data;
  201. } else if (strcmp(cur->name, "accountcode") == 0) {
  202. value = record.account_code;
  203. } else if (strcmp(cur->name, "peeraccount") == 0) {
  204. value = record.peer_account;
  205. } else if (strcmp(cur->name, "uniqueid") == 0) {
  206. value = record.unique_id;
  207. } else if (strcmp(cur->name, "linkedid") == 0) {
  208. value = record.linked_id;
  209. } else if (strcmp(cur->name, "userfield") == 0) {
  210. value = record.user_field;
  211. } else if (strcmp(cur->name, "peer") == 0) {
  212. value = record.peer;
  213. } else if (strcmp(cur->name, "extra") == 0) {
  214. value = record.extra;
  215. } else {
  216. value = NULL;
  217. }
  218. if (value == NULL) {
  219. ast_str_append(&sql2, 0, "%sDEFAULT", SEP);
  220. } else if (strncmp(cur->type, "int", 3) == 0) {
  221. long long whatever;
  222. if (value && sscanf(value, "%30lld", &whatever) == 1) {
  223. LENGTHEN_BUF2(26);
  224. ast_str_append(&sql2, 0, "%s%lld", SEP, whatever);
  225. } else {
  226. LENGTHEN_BUF2(2);
  227. ast_str_append(&sql2, 0, "%s0", SEP);
  228. }
  229. } else if (strncmp(cur->type, "float", 5) == 0) {
  230. long double whatever;
  231. if (value && sscanf(value, "%30Lf", &whatever) == 1) {
  232. LENGTHEN_BUF2(51);
  233. ast_str_append(&sql2, 0, "%s%30Lf", SEP, whatever);
  234. } else {
  235. LENGTHEN_BUF2(2);
  236. ast_str_append(&sql2, 0, "%s0", SEP);
  237. }
  238. /* XXX Might want to handle dates, times, and other misc fields here XXX */
  239. } else {
  240. if (value) {
  241. PQescapeStringConn(conn, escapebuf, value, strlen(value), NULL);
  242. } else {
  243. escapebuf[0] = '\0';
  244. }
  245. LENGTHEN_BUF2(strlen(escapebuf) + 3);
  246. ast_str_append(&sql2, 0, "%s'%s'", SEP, escapebuf);
  247. }
  248. }
  249. first = 0;
  250. }
  251. AST_RWLIST_UNLOCK(&psql_columns);
  252. LENGTHEN_BUF1(ast_str_strlen(sql2) + 2);
  253. ast_str_append(&sql, 0, ")%s)", ast_str_buffer(sql2));
  254. ast_verb(11, "[%s]\n", ast_str_buffer(sql));
  255. ast_debug(2, "inserting a CEL record.\n");
  256. /* Test to be sure we're still connected... */
  257. /* If we're connected, and connection is working, good. */
  258. /* Otherwise, attempt reconnect. If it fails... sorry... */
  259. if (PQstatus(conn) == CONNECTION_OK) {
  260. connected = 1;
  261. } else {
  262. ast_log(LOG_WARNING, "Connection was lost... attempting to reconnect.\n");
  263. PQreset(conn);
  264. if (PQstatus(conn) == CONNECTION_OK) {
  265. ast_log(LOG_NOTICE, "Connection reestablished.\n");
  266. connected = 1;
  267. } else {
  268. pgerror = PQerrorMessage(conn);
  269. ast_log(LOG_ERROR, "Unable to reconnect to database server %s. Calls will not be logged!\n", pghostname);
  270. ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
  271. PQfinish(conn);
  272. conn = NULL;
  273. connected = 0;
  274. goto ast_log_cleanup;
  275. }
  276. }
  277. result = PQexec(conn, ast_str_buffer(sql));
  278. if (PQresultStatus(result) != PGRES_COMMAND_OK) {
  279. pgerror = PQresultErrorMessage(result);
  280. ast_log(LOG_WARNING, "Failed to insert call detail record into database!\n");
  281. ast_log(LOG_WARNING, "Reason: %s\n", pgerror);
  282. ast_log(LOG_WARNING, "Connection may have been lost... attempting to reconnect.\n");
  283. PQreset(conn);
  284. if (PQstatus(conn) == CONNECTION_OK) {
  285. ast_log(LOG_NOTICE, "Connection reestablished.\n");
  286. connected = 1;
  287. PQclear(result);
  288. result = PQexec(conn, ast_str_buffer(sql));
  289. if (PQresultStatus(result) != PGRES_COMMAND_OK) {
  290. pgerror = PQresultErrorMessage(result);
  291. ast_log(LOG_ERROR, "HARD ERROR! Attempted reconnection failed. DROPPING CALL RECORD!\n");
  292. ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
  293. }
  294. }
  295. PQclear(result);
  296. goto ast_log_cleanup;
  297. }
  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. );