repl5_replica_dnhash.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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
  21. replica_init_dn_hash()
  22. {
  23. /* allocate table */
  24. s_hash = PL_NewHashTable(0, PL_HashString, PL_CompareStrings,
  25. PL_CompareValues, NULL, NULL);
  26. if (s_hash == NULL) {
  27. slapi_log_err(SLAPI_LOG_ERR, 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. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "replica_init_dn_hash: "
  36. "failed to create lock; NSPR error - %d\n",
  37. PR_GetError());
  38. replica_destroy_dn_hash();
  39. return -1;
  40. }
  41. return 0;
  42. }
  43. void
  44. 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
  54. replica_add_by_dn(const char *dn)
  55. {
  56. char *dn_copy = NULL;
  57. if (dn == NULL) {
  58. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "replica_add_by_dn: NULL argument\n");
  59. return -1;
  60. }
  61. if (s_hash == NULL || s_lock == NULL) {
  62. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "replica_add_by_dn: "
  63. "replica hash is not initialized\n");
  64. return -1;
  65. }
  66. slapi_rwlock_wrlock(s_lock);
  67. /* make sure that the dn is unique */
  68. if (PL_HashTableLookup(s_hash, dn) != NULL) {
  69. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "replica_add_by_dn: "
  70. "replica with dn (%s) already in the hash\n",
  71. dn);
  72. slapi_rwlock_unlock(s_lock);
  73. return -1;
  74. }
  75. /* add dn */
  76. dn_copy = slapi_ch_strdup(dn);
  77. if (PL_HashTableAdd(s_hash, dn_copy, dn_copy) == NULL) {
  78. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "replica_add_by_dn: "
  79. "failed to add dn (%s); NSPR error - %d\n",
  80. dn_copy, PR_GetError());
  81. slapi_ch_free((void **)&dn_copy);
  82. slapi_rwlock_unlock(s_lock);
  83. return -1;
  84. }
  85. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "replica_add_by_dn: "
  86. "added dn (%s)\n",
  87. dn_copy);
  88. slapi_rwlock_unlock(s_lock);
  89. return 0;
  90. }
  91. int
  92. replica_delete_by_dn(const char *dn)
  93. {
  94. char *dn_copy = NULL;
  95. if (dn == NULL) {
  96. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "replica_delete_by_dn: "
  97. "NULL argument\n");
  98. return -1;
  99. }
  100. if (s_hash == NULL || s_lock == NULL) {
  101. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "replica_delete_by_dn: "
  102. "replica hash is not initialized\n");
  103. return -1;
  104. }
  105. slapi_rwlock_wrlock(s_lock);
  106. /* locate object */
  107. if (NULL == (dn_copy = (char *)PL_HashTableLookup(s_hash, dn))) {
  108. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "replica_delete_by_dn: "
  109. "dn (%s) is not in the hash.\n",
  110. dn);
  111. slapi_rwlock_unlock(s_lock);
  112. return -1;
  113. }
  114. /* remove from hash */
  115. PL_HashTableRemove(s_hash, dn);
  116. slapi_ch_free((void **)&dn_copy);
  117. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "replica_delete_by_dn: "
  118. "removed dn (%s)\n",
  119. dn);
  120. slapi_rwlock_unlock(s_lock);
  121. return 0;
  122. }
  123. int
  124. replica_is_being_configured(const char *dn)
  125. {
  126. if (dn == NULL) {
  127. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "replica_is_being_configured: "
  128. "NULL argument\n");
  129. return 0;
  130. }
  131. if (s_hash == NULL || s_lock == NULL) {
  132. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "replica_is_being_configured: "
  133. "dn hash is not initialized\n");
  134. return 0;
  135. }
  136. slapi_rwlock_wrlock(s_lock);
  137. /* locate object */
  138. if (NULL == PL_HashTableLookup(s_hash, dn)) {
  139. slapi_rwlock_unlock(s_lock);
  140. return 0;
  141. }
  142. slapi_rwlock_unlock(s_lock);
  143. return 1;
  144. }
  145. /* Helper functions */
  146. /* this function called for each hash node during hash destruction */
  147. static PRIntn
  148. replica_destroy_hash_entry(PLHashEntry *he, PRIntn index __attribute__((unused)), void *arg __attribute__((unused)))
  149. {
  150. char *dn_copy;
  151. if (he == NULL) {
  152. return HT_ENUMERATE_NEXT;
  153. }
  154. dn_copy = (char *)he->value;
  155. slapi_ch_free((void **)&dn_copy);
  156. return HT_ENUMERATE_REMOVE;
  157. }