aclflush.cpp 4.8 KB

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