1
0

aclflush.cpp 6.8 KB

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