2
0

km_flat_pool.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * $Id$
  3. *
  4. * Flatstore module connection pool
  5. *
  6. * Copyright (C) 2004 FhG Fokus
  7. *
  8. * This file is part of Kamailio, a free SIP server.
  9. *
  10. * Kamailio is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * Kamailio is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include <unistd.h>
  25. #include "../../dprint.h"
  26. #include "km_flat_pool.h"
  27. #include "km_flat_id.h"
  28. /* The head of the pool */
  29. static struct flat_con* pool = 0;
  30. /*
  31. * Pid of the process that added the last
  32. * connection to the pool. This is used to
  33. * check for inherited database connections.
  34. */
  35. static int pool_pid;
  36. /*
  37. * Get a connection from the pool, reuse existing
  38. * if possible, otherwise create a new one
  39. */
  40. struct flat_con* flat_get_connection(char* dir, char* table)
  41. {
  42. struct flat_id* id;
  43. struct flat_con* ptr;
  44. int pid;
  45. if (!dir || !table) {
  46. LM_ERR("invalid parameter value\n");
  47. return 0;
  48. }
  49. pid = getpid();
  50. if (pool && (pool_pid != pid)) {
  51. LM_ERR("inherited open database connections, "
  52. "this is not a good idea\n");
  53. return 0;
  54. }
  55. pool_pid = pid;
  56. id = new_flat_id(dir, table);
  57. if (!id) return 0;
  58. ptr = pool;
  59. while (ptr) {
  60. if (cmp_flat_id(id, ptr->id)) {
  61. LM_DBG("connection found in the pool\n");
  62. ptr->ref++;
  63. free_flat_id(id);
  64. return ptr;
  65. }
  66. ptr = ptr->next;
  67. }
  68. LM_DBG("connection not found in the pool\n");
  69. ptr = flat_new_connection(id);
  70. if (!ptr) {
  71. free_flat_id(id);
  72. return 0;
  73. }
  74. ptr->next = pool;
  75. pool = ptr;
  76. return ptr;
  77. }
  78. /*
  79. * Release a connection, the connection will be left
  80. * in the pool if ref count != 0, otherwise it
  81. * will be delete completely
  82. */
  83. void flat_release_connection(struct flat_con* con)
  84. {
  85. struct flat_con* ptr;
  86. if (!con) return;
  87. if (con->ref > 1) {
  88. /* There are still other users, just
  89. * decrease the reference count and return
  90. */
  91. LM_DBG("connection still kept in the pool\n");
  92. con->ref--;
  93. return;
  94. }
  95. LM_DBG("removing connection from the pool\n");
  96. if (pool == con) {
  97. pool = pool->next;
  98. } else {
  99. ptr = pool;
  100. while(ptr) {
  101. if (ptr->next == con) break;
  102. ptr = ptr->next;
  103. }
  104. if (!ptr) {
  105. LM_ERR("weird, connection not found in the pool\n");
  106. } else {
  107. /* Remove the connection from the pool */
  108. ptr->next = con->next;
  109. }
  110. }
  111. flat_free_connection(con);
  112. }
  113. /*
  114. * Close and reopen all opened connections
  115. */
  116. int flat_rotate_logs(void)
  117. {
  118. struct flat_con* ptr;
  119. ptr = pool;
  120. while(ptr) {
  121. if (flat_reopen_connection(ptr)) {
  122. return -1;
  123. }
  124. ptr = ptr->next;
  125. }
  126. return 0;
  127. }