1
0

repl5_replica_dnhash.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* repl5_replica_dnhash.c */
  42. #include "repl5.h"
  43. #include "plhash.h"
  44. /* global data */
  45. static PLHashTable *s_hash;
  46. static PRRWLock *s_lock;
  47. /* Forward declarations */
  48. static PRIntn replica_destroy_hash_entry (PLHashEntry *he, PRIntn index, void *arg);
  49. int replica_init_dn_hash ()
  50. {
  51. /* allocate table */
  52. s_hash = PL_NewHashTable(0, PL_HashString, PL_CompareStrings,
  53. PL_CompareValues, NULL, NULL);
  54. if (s_hash == NULL)
  55. {
  56. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_init_dn_hash: "
  57. "failed to allocate hash table; NSPR error - %d\n",
  58. PR_GetError ());
  59. return -1;
  60. }
  61. /* create lock */
  62. s_lock = PR_NewRWLock(PR_RWLOCK_RANK_NONE, "replica_dnhash_lock");
  63. if (s_lock == NULL)
  64. {
  65. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_init_dn_hash: "
  66. "failed to create lock; NSPR error - %d\n",
  67. PR_GetError ());
  68. replica_destroy_dn_hash ();
  69. return -1;
  70. }
  71. return 0;
  72. }
  73. void replica_destroy_dn_hash ()
  74. {
  75. /* destroy the content */
  76. PL_HashTableEnumerateEntries(s_hash, replica_destroy_hash_entry, NULL);
  77. if (s_hash)
  78. PL_HashTableDestroy(s_hash);
  79. if (s_lock)
  80. PR_DestroyRWLock (s_lock);
  81. }
  82. int replica_add_by_dn (const char *dn)
  83. {
  84. char *dn_copy = NULL;
  85. if (dn == NULL)
  86. {
  87. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_add_by_dn: NULL argument\n");
  88. return -1;
  89. }
  90. if (s_hash == NULL || s_lock == NULL)
  91. {
  92. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_add_by_dn: "
  93. "replica hash is not initialized\n");
  94. return -1;
  95. }
  96. PR_RWLock_Wlock (s_lock);
  97. /* make sure that the dn is unique */
  98. if (PL_HashTableLookup(s_hash, dn) != NULL)
  99. {
  100. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_add_by_dn: "
  101. "replica with dn (%s) already in the hash\n", dn);
  102. PR_RWLock_Unlock (s_lock);
  103. return -1 ;
  104. }
  105. /* add dn */
  106. dn_copy = slapi_ch_strdup(dn);
  107. if (PL_HashTableAdd(s_hash, dn_copy, dn_copy) == NULL)
  108. {
  109. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_add_by_dn: "
  110. "failed to add dn (%s); NSPR error - %d\n",
  111. dn_copy, PR_GetError ());
  112. slapi_ch_free((void **)&dn_copy);
  113. PR_RWLock_Unlock (s_lock);
  114. return -1;
  115. }
  116. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_add_by_dn: "
  117. "added dn (%s)\n",
  118. dn_copy);
  119. PR_RWLock_Unlock (s_lock);
  120. return 0;
  121. }
  122. int replica_delete_by_dn (const char *dn)
  123. {
  124. char *dn_copy = NULL;
  125. if (dn == NULL)
  126. {
  127. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_delete_by_dn: "
  128. "NULL argument\n");
  129. return -1;
  130. }
  131. if (s_hash == NULL || s_lock == NULL)
  132. {
  133. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_delete_by_dn: "
  134. "replica hash is not initialized\n");
  135. return -1;
  136. }
  137. PR_RWLock_Wlock (s_lock);
  138. /* locate object */
  139. if (NULL == (dn_copy = (char *)PL_HashTableLookup(s_hash, dn)))
  140. {
  141. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_delete_by_dn: "
  142. "dn (%s) is not in the hash.\n", dn);
  143. PR_RWLock_Unlock (s_lock);
  144. return -1;
  145. }
  146. /* remove from hash */
  147. PL_HashTableRemove(s_hash, dn);
  148. slapi_ch_free((void **)&dn_copy);
  149. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_delete_by_dn: "
  150. "removed dn (%s)\n",
  151. dn);
  152. PR_RWLock_Unlock (s_lock);
  153. return 0;
  154. }
  155. int replica_is_being_configured (const char *dn)
  156. {
  157. if (dn == NULL)
  158. {
  159. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_is_dn_in_hash: "
  160. "NULL argument\n");
  161. return 0;
  162. }
  163. if (s_hash == NULL || s_lock == NULL)
  164. {
  165. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_is_dn_in_hash: "
  166. "dn hash is not initialized\n");
  167. return 0;
  168. }
  169. PR_RWLock_Wlock (s_lock);
  170. /* locate object */
  171. if (NULL == PL_HashTableLookup(s_hash, dn))
  172. {
  173. PR_RWLock_Unlock (s_lock);
  174. return 0;
  175. }
  176. PR_RWLock_Unlock (s_lock);
  177. return 1;
  178. }
  179. /* Helper functions */
  180. /* this function called for each hash node during hash destruction */
  181. static PRIntn replica_destroy_hash_entry (PLHashEntry *he, PRIntn index, void *arg)
  182. {
  183. char *dn_copy;
  184. if (he == NULL)
  185. return HT_ENUMERATE_NEXT;
  186. dn_copy = (char*)he->value;
  187. slapi_ch_free((void **)&dn_copy);
  188. return HT_ENUMERATE_REMOVE;
  189. }