aclflush.cpp 6.8 KB

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