1
0

repl5_replica_dnhash.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /* repl5_replica_dnhash.c */
  13. #include "repl5.h"
  14. #include "plhash.h"
  15. /* global data */
  16. static PLHashTable *s_hash;
  17. static Slapi_RWLock *s_lock;
  18. /* Forward declarations */
  19. static PRIntn replica_destroy_hash_entry (PLHashEntry *he, PRIntn index, void *arg);
  20. int replica_init_dn_hash ()
  21. {
  22. /* allocate table */
  23. s_hash = PL_NewHashTable(0, PL_HashString, PL_CompareStrings,
  24. PL_CompareValues, NULL, NULL);
  25. if (s_hash == NULL)
  26. {
  27. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_init_dn_hash: "
  28. "failed to allocate hash table; NSPR error - %d\n",
  29. PR_GetError ());
  30. return -1;
  31. }
  32. /* create lock */
  33. s_lock = slapi_new_rwlock();
  34. if (s_lock == NULL)
  35. {
  36. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_init_dn_hash: "
  37. "failed to create lock; NSPR error - %d\n",
  38. PR_GetError ());
  39. replica_destroy_dn_hash ();
  40. return -1;
  41. }
  42. return 0;
  43. }
  44. void replica_destroy_dn_hash ()
  45. {
  46. /* destroy the content */
  47. PL_HashTableEnumerateEntries(s_hash, replica_destroy_hash_entry, NULL);
  48. if (s_hash)
  49. PL_HashTableDestroy(s_hash);
  50. if (s_lock)
  51. slapi_destroy_rwlock (s_lock);
  52. }
  53. int replica_add_by_dn (const char *dn)
  54. {
  55. char *dn_copy = NULL;
  56. if (dn == NULL)
  57. {
  58. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_add_by_dn: NULL argument\n");
  59. return -1;
  60. }
  61. if (s_hash == NULL || s_lock == NULL)
  62. {
  63. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_add_by_dn: "
  64. "replica hash is not initialized\n");
  65. return -1;
  66. }
  67. slapi_rwlock_wrlock (s_lock);
  68. /* make sure that the dn is unique */
  69. if (PL_HashTableLookup(s_hash, dn) != NULL)
  70. {
  71. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_add_by_dn: "
  72. "replica with dn (%s) already in the hash\n", dn);
  73. slapi_rwlock_unlock (s_lock);
  74. return -1 ;
  75. }
  76. /* add dn */
  77. dn_copy = slapi_ch_strdup(dn);
  78. if (PL_HashTableAdd(s_hash, dn_copy, dn_copy) == NULL)
  79. {
  80. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_add_by_dn: "
  81. "failed to add dn (%s); NSPR error - %d\n",
  82. dn_copy, PR_GetError ());
  83. slapi_ch_free((void **)&dn_copy);
  84. slapi_rwlock_unlock (s_lock);
  85. return -1;
  86. }
  87. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_add_by_dn: "
  88. "added dn (%s)\n",
  89. dn_copy);
  90. slapi_rwlock_unlock (s_lock);
  91. return 0;
  92. }
  93. int replica_delete_by_dn (const char *dn)
  94. {
  95. char *dn_copy = NULL;
  96. if (dn == NULL)
  97. {
  98. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_delete_by_dn: "
  99. "NULL argument\n");
  100. return -1;
  101. }
  102. if (s_hash == NULL || s_lock == NULL)
  103. {
  104. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_delete_by_dn: "
  105. "replica hash is not initialized\n");
  106. return -1;
  107. }
  108. slapi_rwlock_wrlock (s_lock);
  109. /* locate object */
  110. if (NULL == (dn_copy = (char *)PL_HashTableLookup(s_hash, dn)))
  111. {
  112. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_delete_by_dn: "
  113. "dn (%s) is not in the hash.\n", dn);
  114. slapi_rwlock_unlock (s_lock);
  115. return -1;
  116. }
  117. /* remove from hash */
  118. PL_HashTableRemove(s_hash, dn);
  119. slapi_ch_free((void **)&dn_copy);
  120. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_delete_by_dn: "
  121. "removed dn (%s)\n",
  122. dn);
  123. slapi_rwlock_unlock (s_lock);
  124. return 0;
  125. }
  126. int replica_is_being_configured (const char *dn)
  127. {
  128. if (dn == NULL)
  129. {
  130. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_is_dn_in_hash: "
  131. "NULL argument\n");
  132. return 0;
  133. }
  134. if (s_hash == NULL || s_lock == NULL)
  135. {
  136. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_is_dn_in_hash: "
  137. "dn hash is not initialized\n");
  138. return 0;
  139. }
  140. slapi_rwlock_wrlock (s_lock);
  141. /* locate object */
  142. if (NULL == PL_HashTableLookup(s_hash, dn))
  143. {
  144. slapi_rwlock_unlock (s_lock);
  145. return 0;
  146. }
  147. slapi_rwlock_unlock (s_lock);
  148. return 1;
  149. }
  150. /* Helper functions */
  151. /* this function called for each hash node during hash destruction */
  152. static PRIntn replica_destroy_hash_entry (PLHashEntry *he, PRIntn index, void *arg)
  153. {
  154. char *dn_copy;
  155. if (he == NULL)
  156. return HT_ENUMERATE_NEXT;
  157. dn_copy = (char*)he->value;
  158. slapi_ch_free((void **)&dn_copy);
  159. return HT_ENUMERATE_REMOVE;
  160. }