]>
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);
69 void NodeCore::addReference(NodeCore
&p
)
71 StLock
<Mutex
> _(*this);
72 assert(p
.mReferent
== this);
73 mReferences
.insert(&p
);
76 void NodeCore::removeReference(NodeCore
&p
)
78 StLock
<Mutex
> _(*this);
79 mReferences
.erase(&p
);
83 // ClearReferences clears the reference set but does not propagate
84 // anything; it is NOT recursive.
86 void NodeCore::clearReferences()
88 StLock
<Mutex
> _(*this);
89 secinfo("ssnode", "%p clearing all %d references", this, int(mReferences
.size()));
90 mReferences
.erase(mReferences
.begin(), mReferences
.end());
95 // Kill should be overloaded by Nodes to implement any cleanup and release
96 // operations that should happen at LOGICAL death of the represented object.
97 // This is where you should release ports, close files, etc.
98 // This default behavior, which you MUST include in your override,
99 // propagates kills to all active references, recursively.
101 void NodeCore::kill()
103 StLock
<Mutex
> _(*this);
104 for (ReferenceSet::const_iterator it
= mReferences
.begin(); it
!= mReferences
.end(); it
++)
110 void NodeCore::kill(NodeCore
&ref
)
112 StLock
<Mutex
> _(*this);
114 removeReference(ref
);
119 // NodeCore-level support for state dumping.
120 // Call NodeCore::dumpAll() to debug-dump all nodes.
121 // Note that enabling DEBUGDUMP serializes all node creation/destruction
122 // operations, and thus may cause significant shifts in thread interactions.
124 #if defined(DEBUGDUMP)
126 // The (uncounted) set of all known NodeCores in existence, with protective lock
127 set
<NodeCore
*> NodeCore::mCoreNodes
;
128 Mutex
NodeCore::mCoreLock
;
130 // add a new NodeCore to the known set
132 : Mutex(Mutex::recursive
)
134 StLock
<Mutex
> _(mCoreLock
);
135 mCoreNodes
.insert(this);
138 // partial-line common dump text for any NodeCore
139 // override this to add text to your Node type's state dump output
140 void NodeCore::dumpNode()
142 Debug::dump("%s@%p rc=%u", Debug::typeName(*this).c_str(), this, unsigned(refCountForDebuggingOnly()));
144 Debug::dump(" parent=%p", mParent
.get());
146 Debug::dump(" referent=%p", mReferent
.get());
149 // full-line dump of a NodeCore
150 // override this to completely re-implement the dump format for your Node type
151 void NodeCore::dump()
154 if (!mReferences
.empty()) {
156 for (ReferenceSet::const_iterator it
= mReferences
.begin(); it
!= mReferences
.end(); it
++) {
157 Debug::dump(" %p", it
->get());
158 if ((*it
)->mReferent
!= this)
159 Debug::dump("!*INVALID*");
166 // dump all known nodes
167 void NodeCore::dumpAll()
169 StLock
<Mutex
> _(mCoreLock
);
170 time_t now
; time(&now
);
171 Debug::dump("\nNODE DUMP (%24.24s)\n", ctime(&now
));
172 for (set
<NodeCore
*>::const_iterator it
= mCoreNodes
.begin(); it
!= mCoreNodes
.end(); it
++)
174 Debug::dump("END NODE DUMP\n\n");