km_pg_con.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (C) 2001-2004 iptel.org
  3. * Copyright (C) 2008 1&1 Internet AG
  4. *
  5. * This file is part of Kamailio, a free SIP server.
  6. *
  7. * Kamailio is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version
  11. *
  12. * Kamailio is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /*! \file
  22. * \brief DB_POSTGRES :: Core
  23. * \ingroup db_postgres
  24. * Module: \ref db_postgres
  25. */
  26. #include "km_pg_con.h"
  27. #include "../../mem/mem.h"
  28. #include "../../dprint.h"
  29. #include "../../ut.h"
  30. #include "../../tls_hooks_init.h"
  31. #include <string.h>
  32. #include <time.h>
  33. /*!
  34. * \brief Create a new connection
  35. *
  36. * Create a new connection structure in private memory, open the PostgreSQL
  37. * connection and set reference count to 1
  38. * \param id database id
  39. * \return postgres connection structure, 0 on error
  40. */
  41. struct pg_con* db_postgres_new_connection(struct db_id* id)
  42. {
  43. struct pg_con* ptr;
  44. char *ports;
  45. LM_DBG("db_id = %p\n", id);
  46. if (!id) {
  47. LM_ERR("invalid db_id parameter value\n");
  48. return 0;
  49. }
  50. ptr = (struct pg_con*)pkg_malloc(sizeof(struct pg_con));
  51. if (!ptr) {
  52. LM_ERR("failed trying to allocated %lu bytes for connection structure."
  53. "\n", (unsigned long)sizeof(struct pg_con));
  54. return 0;
  55. }
  56. LM_DBG("%p=pkg_malloc(%lu)\n", ptr, (unsigned long)sizeof(struct pg_con));
  57. memset(ptr, 0, sizeof(struct pg_con));
  58. ptr->ref = 1;
  59. if (id->port) {
  60. ports = int2str(id->port, 0);
  61. LM_DBG("opening connection: postgres://xxxx:xxxx@%s:%d/%s\n", ZSW(id->host),
  62. id->port, ZSW(id->database));
  63. } else {
  64. ports = NULL;
  65. LM_DBG("opening connection: postgres://xxxx:xxxx@%s/%s\n", ZSW(id->host),
  66. ZSW(id->database));
  67. }
  68. /* don't attempt to re-init openssl if done already */
  69. if(tls_loaded()) PQinitSSL(0);
  70. ptr->con = PQsetdbLogin(id->host, ports, NULL, NULL, id->database, id->username, id->password);
  71. LM_DBG("PQsetdbLogin(%p)\n", ptr->con);
  72. if( (ptr->con == 0) || (PQstatus(ptr->con) != CONNECTION_OK) )
  73. {
  74. LM_ERR("%s\n", PQerrorMessage(ptr->con));
  75. PQfinish(ptr->con);
  76. goto err;
  77. }
  78. ptr->connected = 1;
  79. ptr->timestamp = time(0);
  80. ptr->id = id;
  81. return ptr;
  82. err:
  83. if (ptr) {
  84. LM_ERR("cleaning up %p=pkg_free()\n", ptr);
  85. pkg_free(ptr);
  86. }
  87. return 0;
  88. }
  89. /*!
  90. * \brief Close the connection and release memory
  91. * \param con connection
  92. */
  93. void db_postgres_free_connection(struct pool_con* con)
  94. {
  95. struct pg_con * _c;
  96. if (!con) return;
  97. _c = (struct pg_con*)con;
  98. if (_c->res) {
  99. LM_DBG("PQclear(%p)\n", _c->res);
  100. PQclear(_c->res);
  101. _c->res = 0;
  102. }
  103. if (_c->id) free_db_id(_c->id);
  104. if (_c->con) {
  105. LM_DBG("PQfinish(%p)\n", _c->con);
  106. PQfinish(_c->con);
  107. _c->con = 0;
  108. }
  109. LM_DBG("pkg_free(%p)\n", _c);
  110. pkg_free(_c);
  111. }