2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 // attachment - CSSM module attachment objects
23 #define _CPP_ATTACHMENT
25 #include "attachment.h"
28 #include "cssmcontext.h"
31 // Construct an Attachment object.
32 // This constructor does almost all the work: it initializes the Attachment
33 // object, calls the plugin's attach function, and initializes everything.
34 // The only job left for the subclass's constructor is to take the spiFunctionTable
35 // field and extract from it the plugin's dispatch table in suitable form.
37 Attachment::Attachment(Module
*parent
,
38 const CSSM_VERSION
&version
,
40 CSSM_SERVICE_TYPE ssType
,
41 const CSSM_API_MEMORY_FUNCS
&memoryOps
,
42 CSSM_ATTACH_FLAGS attachFlags
,
43 CSSM_KEY_HIERARCHY keyHierarchy
)
44 : CssmMemoryFunctionsAllocator(memoryOps
), module(*parent
)
49 mSubserviceType
= ssType
;
50 mAttachFlags
= attachFlags
;
51 mKeyHierarchy
= keyHierarchy
;
53 // we are not (yet) attached to our plugin
56 // build the upcalls table
57 // (we could do this once in a static, but then we'd have to lock on it)
58 upcalls
.malloc_func
= upcallMalloc
;
59 upcalls
.free_func
= upcallFree
;
60 upcalls
.realloc_func
= upcallRealloc
;
61 upcalls
.calloc_func
= upcallCalloc
;
62 upcalls
.CcToHandle_func
= upcallCcToHandle
;
63 upcalls
.GetModuleInfo_func
= upcallGetModuleInfo
;
65 // tell the module to create an attachment
66 spiFunctionTable
= NULL
; // preset invalid
67 if (CSSM_RETURN err
= module.plugin
->CSSM_SPI_ModuleAttach(&module.myGuid(),
74 &module.cssm
.myGuid(), // CSSM's Guid
75 &module.cssm
.myGuid(), // module manager Guid
76 &module.cssm
.callerGuid(), // caller Guid
79 // attach rejected by module
80 CssmError::throwMe(err
);
83 if (spiFunctionTable
== NULL
|| spiFunctionTable
->ServiceType
!= subserviceType())
84 CssmError::throwMe(CSSMERR_CSSM_INVALID_ADDIN_FUNCTION_TABLE
);
85 mIsActive
= true; // now officially attached to plugin
86 // subclass is responsible for taking spiFunctionTable and build
87 // whatever dispatch is needed
89 module.plugin
->CSSM_SPI_ModuleDetach(handle()); // with extreme prejudice
96 // Detach an attachment.
97 // This is the polite way to detach from the plugin. It may be refused safely
98 // (though perhaps not meaningfully).
99 // THREADS: mLock is locked on entry IFF isLocked, and will be unlocked on exit.
101 void Attachment::detach(bool isLocked
)
103 StLock
<Mutex
> locker(*this, isLocked
); // pre-state locker
104 locker
.lock(); // make sure it's locked
108 CssmError::throwMe(CSSM_ERRCODE_FUNCTION_FAILED
); //@#attachment busy
109 if (CSSM_RETURN error
= module.plugin
->CSSM_SPI_ModuleDetach(handle()))
110 CssmError::throwMe(error
); // I'm sorry Dave, ...
118 // Destroy the Attachment object
120 Attachment::~Attachment()
125 // too bad - you're dead
132 // These do not lock the attachment object. The attachment can't go away
133 // because we incremented the busy count on entry to the plugin; and these
134 // fields are quite constant for the life of the Attachment.
136 void *Attachment::upcallMalloc(CSSM_HANDLE handle
, uint32 size
)
139 return findHandle
<Attachment
>(handle
).malloc(size
);
143 void Attachment::upcallFree(CSSM_HANDLE handle
, void *mem
)
146 return findHandle
<Attachment
>(handle
).free(mem
);
150 void *Attachment::upcallRealloc(CSSM_HANDLE handle
, void *mem
, uint32 size
)
153 return findHandle
<Attachment
>(handle
).realloc(mem
, size
);
157 void *Attachment::upcallCalloc(CSSM_HANDLE handle
, uint32 num
, uint32 size
)
160 return findHandle
<Attachment
>(handle
).calloc(num
, size
);
164 CSSM_RETURN
Attachment::upcallCcToHandle(CSSM_CC_HANDLE handle
,
165 CSSM_MODULE_HANDLE
*modHandle
)
168 Required(modHandle
) = findHandle
<HandleContext
>(handle
).attachment
.handle();
172 CSSM_RETURN
Attachment::upcallGetModuleInfo(CSSM_MODULE_HANDLE handle
,
174 CSSM_VERSION_PTR version
,
175 uint32
*subserviceId
,
176 CSSM_SERVICE_TYPE
*subserviceType
,
177 CSSM_ATTACH_FLAGS
*attachFlags
,
178 CSSM_KEY_HIERARCHY
*keyHierarchy
,
179 CSSM_API_MEMORY_FUNCS_PTR memoryOps
,
180 CSSM_FUNC_NAME_ADDR_PTR FunctionTable
,
184 Attachment
&attachment
= findHandle
<Attachment
>(handle
);
185 Required(guid
) = attachment
.myGuid();
186 Required(version
) = attachment
.mVersion
;
187 Required(subserviceId
) = attachment
.mSubserviceId
;
188 Required(subserviceType
) = attachment
.mSubserviceType
;
189 Required(attachFlags
) = attachment
.mAttachFlags
;
190 Required(keyHierarchy
) = attachment
.mKeyHierarchy
;
191 Required(memoryOps
) = attachment
;
193 attachment
.resolveSymbols(FunctionTable
, NumFunctions
);