aclflush.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. /*
  13. * Source file for the ACL_CacheFlush-related routines.
  14. */
  15. #include <prlog.h>
  16. #include <base/util.h>
  17. #include <libaccess/acl.h>
  18. #include "aclpriv.h"
  19. #include <libaccess/aclproto.h>
  20. #include <libaccess/aclglobal.h>
  21. #include <libaccess/las.h>
  22. #include "aclcache.h"
  23. extern void ACL_DatabaseDestroy(void);
  24. PRIntn
  25. deletelists(PRHashEntry *he, PRIntn i, void *arg)
  26. {
  27. ACLListHandle_t *acllist=(ACLListHandle_t *)he->value;
  28. NSErr_t *errp = 0;
  29. PR_ASSERT(he);
  30. PR_ASSERT(he->value);
  31. if (acllist->ref_count) {
  32. // If the list is in use, increment the counter. Then set the
  33. // stale flag. The other user can't delete the list since we're
  34. // counted as well. Finally, decrement the counter and whoever
  35. // sets it to zero will delete the ACL List.
  36. PR_ASSERT(ACL_CritHeld());
  37. acllist->ref_count++;
  38. acllist->flags |= ACL_LIST_STALE;
  39. if (--acllist->ref_count == 0)
  40. ACL_ListDestroy(errp, acllist);
  41. } else {
  42. ACL_ListDestroy(errp, acllist);
  43. }
  44. return 0;
  45. }
  46. PRIntn
  47. restartdeletelists(PRHashEntry *he, PRIntn i, void *arg)
  48. {
  49. NSErr_t *errp = 0;
  50. // Cannot be anyone left using the lists, so just free them no matter
  51. // what.
  52. ACLListHandle_t *acllist=(ACLListHandle_t *)he->value;
  53. PR_ASSERT(he);
  54. PR_ASSERT(he->value);
  55. ACL_ListDestroy(errp, acllist);
  56. return 0;
  57. }
  58. static AclCacheFlushFunc_t AclCacheFlushRoutine = NULL;
  59. NSAPI_PUBLIC int
  60. ACL_CacheFlushRegister(AclCacheFlushFunc_t flush_func)
  61. {
  62. PR_ASSERT(flush_func);
  63. AclCacheFlushRoutine = flush_func;
  64. return 0;
  65. }
  66. NSAPI_PUBLIC int
  67. ACL_CacheFlush(void)
  68. {
  69. ACLGlobal_p newACLGlobal;
  70. NSErr_t *errp = 0;
  71. PR_ASSERT(ACLGlobal);
  72. PR_ASSERT(ACLGlobal->masterlist);
  73. PR_ASSERT(ACLGlobal->listhash);
  74. PR_ASSERT(ACLGlobal->urihash);
  75. PR_ASSERT(ACLGlobal->urigethash);
  76. PR_ASSERT(ACLGlobal->pool);
  77. ACL_CritEnter();
  78. // Swap the pointers. Keep using the current database/method tables
  79. // until the new ones are built. This is a kludge. An in-progress
  80. // evaluation could conceivably get messed up, but the window seems
  81. // small.
  82. newACLGlobal = oldACLGlobal;
  83. oldACLGlobal = ACLGlobal;
  84. ACLGlobal = newACLGlobal;
  85. // Prepare the new ACLGlobal structure
  86. ACL_UriHashInit(); /* Also initializes ACLGlobal->pool */
  87. ACL_ListHashInit();
  88. ACLGlobal->evalhash = oldACLGlobal->evalhash;
  89. ACLGlobal->flushhash = oldACLGlobal->flushhash;
  90. ACLGlobal->methodhash = oldACLGlobal->methodhash;
  91. ACLGlobal->dbtypehash = oldACLGlobal->dbtypehash;
  92. ACLGlobal->dbnamehash = oldACLGlobal->dbnamehash;
  93. ACLGlobal->attrgetterhash = oldACLGlobal->attrgetterhash;
  94. ACLGlobal->databasepool = oldACLGlobal->databasepool;
  95. ACLGlobal->methodpool = oldACLGlobal->methodpool;
  96. // Mark all existing ACL Lists as stale. Delete any unreferenced ones.
  97. PR_HashTableEnumerateEntries(oldACLGlobal->listhash, deletelists, NULL);
  98. // Delete the old master list.
  99. ACL_ListDestroy(errp, oldACLGlobal->masterlist);
  100. oldACLGlobal->masterlist = NULL;
  101. PR_HashTableDestroy(oldACLGlobal->listhash);
  102. oldACLGlobal->listhash = NULL;
  103. PR_HashTableDestroy(oldACLGlobal->urihash);
  104. oldACLGlobal->urihash = NULL;
  105. PR_HashTableDestroy(oldACLGlobal->urigethash);
  106. oldACLGlobal->urigethash = NULL;
  107. pool_destroy(oldACLGlobal->pool);
  108. oldACLGlobal->pool = NULL;
  109. memset(oldACLGlobal, 0, sizeof(ACLGlobal_s));
  110. // Read in the ACLs again in lib/frame
  111. if (AclCacheFlushRoutine) {
  112. (*AclCacheFlushRoutine)();
  113. }
  114. ACL_CritExit();
  115. return 0;
  116. }
  117. NSAPI_PUBLIC void
  118. ACL_Restart(void *clntData)
  119. {
  120. NSErr_t *errp = 0;
  121. PR_ASSERT(ACLGlobal);
  122. PR_ASSERT(ACLGlobal->masterlist);
  123. PR_ASSERT(ACLGlobal->listhash);
  124. PR_ASSERT(ACLGlobal->urihash);
  125. PR_ASSERT(ACLGlobal->urigethash);
  126. PR_ASSERT(ACLGlobal->pool);
  127. // Unlike ACL_CacheFlush, this routine can be much more cavalier about
  128. // freeing up memory, since there's guaranteed to be no users about at
  129. // this time.
  130. ACL_DatabaseDestroy();
  131. ACL_MethodSetDefault(errp, ACL_METHOD_INVALID);
  132. // Mark all existing ACL Lists as stale. Delete any unreferenced ones
  133. // (i.e. all of them)
  134. PR_HashTableEnumerateEntries(ACLGlobal->listhash, restartdeletelists, NULL);
  135. // Delete the master list.
  136. ACL_ListDestroy(errp, ACLGlobal->masterlist);
  137. ACL_LasHashDestroy();
  138. PR_HashTableDestroy(ACLGlobal->listhash);
  139. PR_HashTableDestroy(ACLGlobal->urihash);
  140. PR_HashTableDestroy(ACLGlobal->urigethash);
  141. pool_destroy(ACLGlobal->pool);
  142. PERM_FREE(ACLGlobal);
  143. ACLGlobal = NULL;
  144. PERM_FREE(oldACLGlobal);
  145. oldACLGlobal = NULL;
  146. return;
  147. }