]>
git.saurik.com Git - apple/securityd.git/blob - src/structure.h
f5b61bf6b3591e72a423936216bb88ed510f3bce
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
28 // structure - structural framework for securityd objects
33 #include "securityserver.h"
34 #include <security_utilities/refcount.h>
35 #include <security_cdsa_utilities/handleobject.h>
39 // Track a per-process real world object
41 template <class Base
, class Glob
> class Node
;
49 // A generic core node of the object mesh.
50 // Repeat after me: "Everything that matters is a Node."
52 // This contains the mesh links (as smart pointers to NodeCores).
53 // The 'parent' is the next-more-global related object in the mesh, if any;
54 // nodes with the same parent "belong together" at the more global layer.
55 // For example, processes have their sessions as parents.
56 // The 'referent' is an object at the *same* globality layer that controls
57 // the lifetime of this node. For example, a Database has its Process as
59 // Both parent and referent are optional (can be NULL).
60 // The references set is a partial referent back-link. All NodeCores listed
61 // in a node's References have this node as a referent, but the set is
62 // selective (not necessarily complete). The References set propagates the
63 // 'kill' operation up the referents chain; thus being included in a node's
64 // References means that a kill() on the referent will (recursively) kill
65 // all references, too.
67 // Do not inherit directly from NodeCore; use Node<> (below).
69 class NodeCore
: public RefCount
, public Mutex
{
70 template <class Base
, class Glob
> friend class Node
;
72 #if !defined(DEBUGDUMP) // (see below if DEBUGDUMP)
73 NodeCore() : Mutex(Mutex::recursive
) { }
77 bool hasParent() const { return mParent
; }
78 bool hasReferent() const { return mReferent
; }
80 // reference set operations
82 void allReferences(void (Sub::*func
)());
83 template <class Sub
, class Value
>
84 Sub
*findFirst(Value (Sub::*func
)() const, Value compare
);
85 void clearReferences();
87 virtual void kill(); // always invoke NodeCore's in your override
89 // for STL ordering (so we can have sets of RefPointers of NodeCores)
90 bool operator < (const NodeCore
&other
) const
91 { return this < &other
; }
94 RefPointer
<NodeCore
> mParent
;
95 RefPointer
<NodeCore
> mReferent
;
96 typedef set
<RefPointer
<NodeCore
> > ReferenceSet
;
97 ReferenceSet mReferences
;
99 #if defined(DEBUGDUMP)
100 public: // dump support
101 NodeCore(); // dump-only constructor (registers node)
103 virtual void dumpNode(); // node description (partial line)
104 virtual void dump(); // dumpNode() + references + NL
105 static void dumpAll(); // dump all nodes
107 static Mutex mCoreLock
; // lock for mCoreNodes
108 static set
<NodeCore
*> mCoreNodes
; // (debug) set of all known nodes
114 void NodeCore::allReferences(void (Sub::*func
)())
116 StLock
<Mutex
> _(*this);
117 for (ReferenceSet::const_iterator it
= mReferences
.begin(); it
!= mReferences
.end(); it
++)
118 if (Sub
*sub
= dynamic_cast<Sub
*>(it
->get()))
122 template <class Sub
, class Value
>
123 Sub
*NodeCore::findFirst(Value (Sub::*func
)() const, Value compare
)
125 StLock
<Mutex
> _(*this);
126 for (ReferenceSet::const_iterator it
= mReferences
.begin(); it
!= mReferences
.end(); it
++)
127 if (Sub
*sub
= dynamic_cast<Sub
*>(it
->get()))
128 if ((sub
->*func
)() == compare
)
135 // A typed node of the object mesh.
136 // This adds type-safe accessors and modifiers to NodeCore.
138 template <class Base
, class Glob
>
139 class Node
: public NodeCore
{
142 { StLock
<Mutex
> _(*this); mParent
= &p
; }
144 virtual void referent(Base
&r
)
145 { StLock
<Mutex
> _(*this); mReferent
= &r
; }
149 StLock
<Mutex
> _(*this);
156 { assert(mParent
); return safer_cast
<T
&>(*mParent
); }
160 { assert(mReferent
); return safer_cast
<T
&>(*mReferent
); }
163 void addReference(Base
&p
)
164 { StLock
<Mutex
> _(*this); assert(p
.mReferent
== this); mReferences
.insert(&p
); }
165 void removeReference(Base
&p
) { StLock
<Mutex
> _(*this); mReferences
.erase(&p
); }
170 // Connection (client thread) layer nodes
172 class PerConnection
: public Node
<PerConnection
, PerProcess
> {
178 // Process (client process) layer nodes
180 class PerProcess
: public HandleObject
, public Node
<PerProcess
, PerSession
> {
186 // Session (client-side session) layer nodes
188 class PerSession
: public Node
<PerSession
, PerGlobal
> {
194 // Global (per-system) layer nodes
196 class PerGlobal
: public Node
<PerGlobal
, PerGlobal
> {
202 // A map from mach port names to (refcounted) pointers-to-somethings
204 template <class Node
>
205 class PortMap
: public Mutex
, public std::map
<Port
, RefPointer
<Node
> > {
206 typedef std::map
<Port
, RefPointer
<Node
> > _Map
;
208 bool contains(mach_port_t port
) const { return find(port
) != end(); }
209 Node
*getOpt(mach_port_t port
) const
211 typename
_Map::const_iterator it
= find(port
);
212 return (it
== end()) ? NULL
: it
->second
;
215 Node
*get(mach_port_t port
) const
217 typename
_Map::const_iterator it
= find(port
);
222 Node
*get(mach_port_t port
, OSStatus error
) const
224 typename
_Map::const_iterator it
= find(port
);
226 MacOSError::throwMe(error
);
233 template <class Node
>
234 void PortMap
<Node
>::dump()
236 for (typename
_Map::const_iterator it
= begin(); it
!= end(); it
++)
241 #endif //_H_STRUCTURE