ul_check.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* sp-ul_db module
  2. *
  3. * Copyright (C) 2007 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. #include "../../mem/shm_mem.h"
  22. #include "ul_check.h"
  23. #include "time.h"
  24. static struct check_list_head * head = NULL;
  25. static struct check_list_element * initialise_element(void);
  26. static void destroy_element(struct check_list_element * element);
  27. int init_list(void) {
  28. if(!head){
  29. if((head = (struct check_list_head *)shm_malloc(sizeof(struct check_list_head))) == NULL){
  30. LM_ERR("couldn't allocate shared memory.\n");
  31. return -1;
  32. }
  33. }
  34. memset(head, 0, sizeof(struct check_list_head));
  35. if(lock_init(&head->list_lock) == 0){
  36. LM_ERR("cannot initialise lock.\n");
  37. shm_free(head);
  38. return -1;
  39. }
  40. return 0;
  41. }
  42. struct check_data * get_new_element(void) {
  43. struct check_list_element * ret;
  44. if(!head){
  45. LM_ERR("list not initialised.\n");
  46. return NULL;
  47. }
  48. LM_DBG("start.\n");
  49. lock_get(&head->list_lock);
  50. if((ret = initialise_element()) == NULL){
  51. lock_release(&head->list_lock);
  52. return NULL;
  53. }
  54. head->element_count++;
  55. if(head->first == NULL){
  56. LM_DBG("new element is the first.\n");
  57. LM_DBG("element_count: %i\n", head->element_count);
  58. head->first = ret;
  59. lock_release(&head->list_lock);
  60. return ret->data;
  61. }
  62. LM_DBG("new element.\n");
  63. LM_DBG("element_count: %i\n", head->element_count);
  64. ret->next = head->first;
  65. head->first = ret;
  66. lock_release(&head->list_lock);
  67. return ret->data;
  68. }
  69. int must_refresh(struct check_data * element) {
  70. int ret;
  71. lock_get(&element->flag_lock);
  72. ret = element->refresh_flag;
  73. LM_DBG("refresh_flag is at %i.\n", ret);
  74. element->refresh_flag = 0;
  75. lock_release(&element->flag_lock);
  76. return ret;
  77. }
  78. int must_reconnect(struct check_data * element) {
  79. int ret;
  80. lock_get(&element->flag_lock);
  81. ret = element->reconnect_flag;
  82. LM_DBG("reconnect_flag is at %i.\n", ret);
  83. element->reconnect_flag = 0;
  84. lock_release(&element->flag_lock);
  85. return ret;
  86. }
  87. int set_must_refresh(void) {
  88. struct check_list_element * tmp;
  89. int i = 0;
  90. lock_get(&head->list_lock);
  91. tmp = head->first;
  92. while(tmp){
  93. lock_get(&tmp->data->flag_lock);
  94. tmp->data->refresh_flag = 1;
  95. lock_release(&tmp->data->flag_lock);
  96. tmp = tmp->next;
  97. i++;
  98. LM_DBG("element no %i.\n", i);
  99. }
  100. lock_release(&head->list_lock);
  101. return i;
  102. }
  103. int set_must_reconnect(void) {
  104. struct check_list_element * tmp;
  105. int i = 0;
  106. lock_get(&head->list_lock);
  107. tmp = head->first;
  108. while(tmp){
  109. lock_get(&tmp->data->flag_lock);
  110. tmp->data->reconnect_flag = 1;
  111. lock_release(&tmp->data->flag_lock);
  112. tmp = tmp->next;
  113. i++;
  114. LM_DBG("element no %i.\n", i);
  115. }
  116. lock_release(&head->list_lock);
  117. return i;
  118. }
  119. int must_retry(time_t * timer, time_t interval){
  120. if(!timer){
  121. return -1;
  122. }
  123. LM_DBG("must_retry: time is at %i, retry at %i.\n", (int)time(NULL), (int)(*timer));
  124. if(*timer <= time(NULL)){
  125. *timer = time(NULL) + interval;
  126. return 1;
  127. }
  128. return 0;
  129. }
  130. void destroy_list(void) {
  131. struct check_list_element * tmp;
  132. struct check_list_element * del;
  133. if(head){
  134. tmp = head->first;
  135. while(tmp){
  136. del = tmp;
  137. tmp = tmp->next;
  138. destroy_element(del);
  139. }
  140. lock_destroy(&head->list_lock);
  141. shm_free(head);
  142. }
  143. return;
  144. }
  145. static struct check_list_element * initialise_element(void){
  146. struct check_list_element * ret;
  147. if((ret = (struct check_list_element *)shm_malloc(sizeof(struct check_list_element))) == NULL){
  148. LM_ERR("couldn't allocate shared memory.\n");
  149. return NULL;
  150. }
  151. memset(ret, 0, sizeof(struct check_list_element));
  152. if((ret->data = (struct check_data *)shm_malloc(sizeof(struct check_data))) == NULL){
  153. LM_ERR("couldn't allocate shared memory.\n");
  154. shm_free(ret);
  155. return NULL;
  156. }
  157. memset(ret->data, 0, sizeof(struct check_data));
  158. if(lock_init(&ret->data->flag_lock) == 0){
  159. LM_ERR("cannot initialise flag lock.\n");
  160. shm_free(ret->data);
  161. shm_free(ret);
  162. return NULL;
  163. }
  164. return ret;
  165. }
  166. static void destroy_element(struct check_list_element * element){
  167. if(element){
  168. if(element->data){
  169. /* if(element->data->flag_lock){ */
  170. lock_destroy(&element->data->flag_lock);
  171. /* }*/
  172. shm_free(element->data);
  173. }
  174. shm_free(element);
  175. }
  176. return;
  177. }