]>
git.saurik.com Git - apple/security.git/blob - securityd/src/structure.h
2 * Copyright (c) 2000-2001,2004-2005,2007-2008 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
31 #include <security_utilities/refcount.h>
32 #include <security_utilities/mach++.h>
33 #include <security_cdsa_utilities/u32handleobject.h>
37 using MachPlusPlus::Port
;
41 // Track a per-process real world object
43 template <class Base
, class Glob
> class Node
;
51 // A generic core node of the object mesh.
52 // Repeat after me: "Everything that matters is a Node."
54 // This contains the mesh links (as smart pointers to NodeCores).
55 // The 'parent' is the next-more-global related object in the mesh, if any;
56 // nodes with the same parent "belong together" at the more global layer.
57 // For example, processes have their sessions as parents.
58 // The 'referent' is an object at the *same* globality layer that controls
59 // the lifetime of this node. For example, a Database has its Process as
61 // Both parent and referent are optional (can be NULL).
62 // The references set is a partial referent back-link. All NodeCores listed
63 // in a node's References have this node as a referent, but the set is
64 // selective (not necessarily complete). The References set propagates the
65 // 'kill' operation up the referents chain; thus being included in a node's
66 // References means that a kill() on the referent will (recursively) kill
67 // all references, too.
69 // Do not inherit directly from NodeCore; use Node<> (below).
71 class NodeCore
: public RefCount
, public Mutex
{
72 template <class Base
, class Glob
> friend class Node
;
74 #if !defined(DEBUGDUMP) // (see below if DEBUGDUMP)
75 NodeCore() : Mutex(Mutex::recursive
) { }
79 void addReference(NodeCore
&p
);
80 void removeReference(NodeCore
&p
);
82 // reference set operations
84 void allReferences(void (Sub::*func
)());
85 template <class Sub
, class Value
>
86 RefPointer
<Sub
> findFirst(Value (Sub::*func
)() const, Value compare
);
87 void clearReferences();
89 virtual void kill(); // kill all references and self
90 virtual void kill(NodeCore
&ref
); // kill ref from my references()
92 // for STL ordering (so we can have sets of RefPointers of NodeCores)
93 bool operator < (const NodeCore
&other
) const
94 { return this < &other
; }
97 void parent(NodeCore
&p
); // set parent
98 void referent(NodeCore
&r
); // set referent
99 void clearReferent(); // clear referent
101 bool hasParent() const { return mParent
; }
102 bool hasReferent() const { return mReferent
; }
105 RefPointer
<NodeCore
> mParent
;
106 RefPointer
<NodeCore
> mReferent
;
107 typedef set
<RefPointer
<NodeCore
> > ReferenceSet
;
108 ReferenceSet mReferences
;
110 #if defined(DEBUGDUMP)
111 public: // dump support
112 NodeCore(); // dump-only constructor (registers node)
114 virtual void dumpNode(); // node description (partial line)
115 virtual void dump(); // dumpNode() + references + NL
116 static void dumpAll(); // dump all nodes
118 static Mutex mCoreLock
; // lock for mCoreNodes
119 static set
<NodeCore
*> mCoreNodes
; // (debug) set of all known nodes
125 // Call a member on each reference of a Node<> object.
126 // The object lock is held throughout, and we keep a RefPointer to each object
127 // as it's being processed. Thus it's safe for a reference to self-unlink in
128 // the object mesh; it'll get destroyed after its method returns.
131 void NodeCore::allReferences(void (Sub::*func
)())
133 StLock
<Mutex
> _(*this);
134 for (ReferenceSet::const_iterator it
= mReferences
.begin(); it
!= mReferences
.end();)
135 if (RefPointer
<Sub
> sub
= dynamic_cast<Sub
*>((it
++)->get()))
141 // Find a reference of a Node<> object that satisfies a simple "method returns value"
142 // condition. There is no defined order of the scan, so if the condition is not unique,
143 // any one reference may be returned. If none are found, we return NULL.
144 // This returns a RefPointer: lifetime of the returned instance (if any) is ensured even
145 // if it is asynchronously removed from the references set.
147 template <class Sub
, class Value
>
148 RefPointer
<Sub
> NodeCore::findFirst(Value (Sub::*func
)() const, Value compare
)
150 StLock
<Mutex
> _(*this);
151 for (ReferenceSet::const_iterator it
= mReferences
.begin(); it
!= mReferences
.end(); it
++)
152 if (Sub
*sub
= dynamic_cast<Sub
*>(it
->get()))
153 if ((sub
->*func
)() == compare
)
160 // A typed node of the object mesh.
161 // This adds type-safe accessors and modifiers to NodeCore.
163 template <class Base
, class Glob
>
164 class Node
: public NodeCore
{
166 // type-safer versions of node mesh setters
167 void parent(Glob
&p
) { NodeCore::parent(p
); }
168 void referent(Base
&r
) { NodeCore::referent(r
); }
173 { assert(mParent
); return safer_cast
<T
&>(*mParent
); }
177 { assert(mReferent
); return safer_cast
<T
&>(*mReferent
); }
180 void addReference(Base
&p
) { NodeCore::addReference(p
); }
181 void removeReference(Base
&p
) { NodeCore::removeReference(p
); }
186 // Connection (client thread) layer nodes
188 class PerConnection
: public Node
<PerConnection
, PerProcess
> {
194 // Process (client process) layer nodes
196 class PerProcess
: public U32HandleObject
, public Node
<PerProcess
, PerSession
> {
202 // Session (client-side session) layer nodes
204 class PerSession
: public Node
<PerSession
, PerGlobal
> {
210 // Global (per-system) layer nodes
212 class PerGlobal
: public Node
<PerGlobal
, PerGlobal
> {
218 // A map from mach port names to (refcounted) pointers-to-somethings
220 template <class Node
>
221 class PortMap
: public Mutex
, public std::map
<Port
, RefPointer
<Node
> > {
222 typedef std::map
<Port
, RefPointer
<Node
> > _Map
;
224 bool contains(mach_port_t port
) const { return this->find(port
) != this->end(); }
225 Node
*getOpt(mach_port_t port
) const
227 typename
_Map::const_iterator it
= this->find(port
);
228 return (it
== this->end()) ? NULL
: it
->second
;
231 Node
*get(mach_port_t port
) const
233 typename
_Map::const_iterator it
= this->find(port
);
234 assert(it
!= this->end());
238 Node
*get(mach_port_t port
, OSStatus error
) const
240 typename
_Map::const_iterator it
= this->find(port
);
241 if (it
== this->end())
242 MacOSError::throwMe(error
);
249 template <class Node
>
250 void PortMap
<Node
>::dump()
252 for (typename
_Map::const_iterator it
= this->begin(); it
!= this->end(); it
++)
257 #endif //_H_STRUCTURE