]>
git.saurik.com Git - apple/security.git/blob - securityd/src/structure.cpp
2 * Copyright (c) 2000-2001,2004,2009 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 // structure - structural framework for securityd objects
28 #include "structure.h"
32 // NodeCore always has a destructor (because it's virtual),
33 // but its dump support is conditionally included.
37 #if defined(DEBUGDUMP)
38 StLock
<Mutex
> _(mCoreLock
);
39 mCoreNodes
.erase(this);
47 // Basic object mesh maintainance
49 void NodeCore::parent(NodeCore
&p
)
51 StLock
<Mutex
> _(*this);
55 void NodeCore::referent(NodeCore
&r
)
57 StLock
<Mutex
> _(*this);
62 void NodeCore::clearReferent()
64 StLock
<Mutex
> _(*this);
66 assert(!mReferent
->hasReference(*this));
71 void NodeCore::addReference(NodeCore
&p
)
73 StLock
<Mutex
> _(*this);
74 assert(p
.mReferent
== this);
75 mReferences
.insert(&p
);
78 void NodeCore::removeReference(NodeCore
&p
)
80 StLock
<Mutex
> _(*this);
81 assert(hasReference(p
));
82 mReferences
.erase(&p
);
87 bool NodeCore::hasReference(NodeCore
&p
)
89 assert(p
.refCountForDebuggingOnly() > 0);
90 return mReferences
.find(&p
) != mReferences
.end();
97 // ClearReferences clears the reference set but does not propagate
98 // anything; it is NOT recursive.
100 void NodeCore::clearReferences()
102 StLock
<Mutex
> _(*this);
103 secinfo("ssnode", "%p clearing all %d references",
104 this, int(mReferences
.size()));
105 mReferences
.erase(mReferences
.begin(), mReferences
.end());
110 // Kill should be overloaded by Nodes to implement any cleanup and release
111 // operations that should happen at LOGICAL death of the represented object.
112 // This is where you should release ports, close files, etc.
113 // This default behavior, which you MUST include in your override,
114 // propagates kills to all active references, recursively.
116 void NodeCore::kill()
118 StLock
<Mutex
> _(*this);
119 for (ReferenceSet::const_iterator it
= mReferences
.begin(); it
!= mReferences
.end(); it
++)
125 void NodeCore::kill(NodeCore
&ref
)
127 StLock
<Mutex
> _(*this);
128 assert(hasReference(ref
));
130 removeReference(ref
);
135 // NodeCore-level support for state dumping.
136 // Call NodeCore::dumpAll() to debug-dump all nodes.
137 // Note that enabling DEBUGDUMP serializes all node creation/destruction
138 // operations, and thus may cause significant shifts in thread interactions.
140 #if defined(DEBUGDUMP)
142 // The (uncounted) set of all known NodeCores in existence, with protective lock
143 set
<NodeCore
*> NodeCore::mCoreNodes
;
144 Mutex
NodeCore::mCoreLock
;
146 // add a new NodeCore to the known set
148 : Mutex(Mutex::recursive
)
150 StLock
<Mutex
> _(mCoreLock
);
151 mCoreNodes
.insert(this);
154 // partial-line common dump text for any NodeCore
155 // override this to add text to your Node type's state dump output
156 void NodeCore::dumpNode()
158 Debug::dump("%s@%p rc=%u", Debug::typeName(*this).c_str(), this, unsigned(refCountForDebuggingOnly()));
160 Debug::dump(" parent=%p", mParent
.get());
162 Debug::dump(" referent=%p", mReferent
.get());
165 // full-line dump of a NodeCore
166 // override this to completely re-implement the dump format for your Node type
167 void NodeCore::dump()
170 if (!mReferences
.empty()) {
172 for (ReferenceSet::const_iterator it
= mReferences
.begin(); it
!= mReferences
.end(); it
++) {
173 Debug::dump(" %p", it
->get());
174 if ((*it
)->mReferent
!= this)
175 Debug::dump("!*INVALID*");
182 // dump all known nodes
183 void NodeCore::dumpAll()
185 StLock
<Mutex
> _(mCoreLock
);
186 time_t now
; time(&now
);
187 Debug::dump("\nNODE DUMP (%24.24s)\n", ctime(&now
));
188 for (set
<NodeCore
*>::const_iterator it
= mCoreNodes
.begin(); it
!= mCoreNodes
.end(); it
++)
190 Debug::dump("END NODE DUMP\n\n");