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 // module - CSSM Module objects
24 #include "attachment.h"
28 // Module object construction.
30 Module::Module(CssmManager
*mgr
, const MdsComponent
&info
, Plugin
*plug
)
31 : MdsComponent(info
), cssm(*mgr
), plugin(plug
)
33 // invoke module's load entry (tell it it's being loaded)
34 if (CSSM_RETURN err
= plugin
->CSSM_SPI_ModuleLoad(&cssm
.myGuid(), // CSSM's Guid
35 &myGuid(), // module's Guid
36 spiEventRelay
, this)) {
38 CssmError::throwMe(err
); // self-destruct this module
44 // Destroy the module object.
45 // The unload() method must have succeeded and returned true before
46 // you get to delete a Module. A destructor is too precarious a place
47 // to negotiate with a plugin...
54 bool Module::unload(const ModuleCallback
&callback
)
56 StLock
<Mutex
> _(mLock
);
57 // locked module - no more attachment creations possible
58 if (callbackCount() == 1) {
59 // would be last callback if successful, check for actual unload
60 if (attachmentCount() > 0)
61 CssmError::throwMe(CSSM_ERRCODE_FUNCTION_FAILED
); // @# module is busy
62 // no attachments active - we are idle and ready to unload
63 if (CSSM_RETURN err
= plugin
->CSSM_SPI_ModuleUnload(&cssm
.myGuid(), // CSSM's Guid
64 &myGuid(), // module's Guid
65 spiEventRelay
, this)) // our callback
66 CssmError::throwMe(err
); // tough...
72 // more callbacks - we're not going to unload
80 // Create a new attachment for this module
82 CSSM_HANDLE
Module::attach(const CSSM_VERSION
&version
,
84 CSSM_SERVICE_TYPE subserviceType
,
85 const CSSM_API_MEMORY_FUNCS
&memoryOps
,
86 CSSM_ATTACH_FLAGS attachFlags
,
87 CSSM_KEY_HIERARCHY keyHierarchy
,
88 CSSM_FUNC_NAME_ADDR
*functionTable
,
89 uint32 functionTableSize
)
91 StLock
<Mutex
> _(mLock
);
93 // check if the module can do this kind of service
94 if (!supportsService(subserviceType
))
95 CssmError::throwMe(CSSMERR_CSSM_INVALID_SERVICE_MASK
);
97 Attachment
*attachment
= cssm
.attachmentMakerFor(subserviceType
)->make(this,
99 subserviceId
, subserviceType
,
103 functionTable
, functionTableSize
);
106 // add to module's attachment map
107 attachmentMap
.insert(AttachmentMap::value_type(attachment
->handle(), attachment
));
114 return attachment
->handle();
119 // Detach an Attachment from this module.
120 // THREADS: Requires the attachment to be idled out, i.e. caller
121 // is responsible for keeping more users from entering it.
123 void Module::detach(Attachment
*attachment
)
125 StLock
<Mutex
> _(mLock
);
126 attachmentMap
.erase(attachment
->handle());
131 // Handle events sent by the loaded module.
133 void Module::spiEvent(CSSM_MODULE_EVENT event
,
136 CSSM_SERVICE_TYPE serviceType
)
138 StLock
<Mutex
> _(mLock
);
139 if (guid
!= myGuid())
140 CssmError::throwMe(CSSM_ERRCODE_INTERNAL_ERROR
);
141 callbackSet(event
, guid
, subserviceId
, serviceType
);
145 CSSM_RETURN
Module::spiEventRelay(const CSSM_GUID
*ModuleGuid
,
148 CSSM_SERVICE_TYPE ServiceType
,
149 CSSM_MODULE_EVENT EventType
)
152 static_cast<Module
*>(Context
)->spiEvent(EventType
,
153 Guid::required(ModuleGuid
),