aclflush.cpp 6.8 KB

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