os/security/cryptoservices/filebasedcertificateandkeystores/source/generic/server/fsserver.cpp
Update contrib.
2 * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of the License "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
21 #include "fstokenutil.h"
22 #include "cfskeystoreserver.h"
23 #include "CKeyStoreSession.h"
24 #include "filecertstore.h"
25 #include "CCertStoreSession.h"
26 #include "CFSCertAppsServer.h"
27 #include "CCertAppsSession.h"
28 #include "FSResources.h"
30 #include "tokenserverdebug.h"
31 #include "fstokenservername.h"
33 // CTokenServerSession /////////////////////////////////////////////////////////
35 CTokenServerSession::CTokenServerSession()
39 inline CTokenServer& CTokenServerSession::Server()
41 return static_cast<CTokenServer&>(const_cast<CServer2&>(*CSession2::Server()));
44 void CTokenServerSession::CreateL()
46 Server().AddSession();
49 CTokenServerSession::~CTokenServerSession()
51 Server().DropSession();
55 * Handle a client request. Leaving is handled by
56 * CTokenServerSession::ServiceError() which reports the error code to the
59 void CTokenServerSession::ServiceL(const RMessage2& aMessage)
63 switch (aMessage.Function())
66 TokenServerDebug::StartOOMTest();
67 aMessage.Complete(KErrNone);
70 case EIncHeapFailPoint:
71 TokenServerDebug::IncHeapFailPoint();
72 aMessage.Complete(KErrNone);
76 TokenServerDebug::ResetHeapFail();
77 aMessage.Complete(KErrNone);
81 aMessage.Complete(User::CountAllocCells());
90 * Handle an error from ServiceL() A bad descriptor error implies a badly
91 * programmed client, so panic it - otherwise report the error to the client.
93 void CTokenServerSession::ServiceError(const RMessage2& aMessage, TInt aError)
95 if (aError==KErrBadDescriptor)
97 PanicClient(aMessage, EPanicBadDescriptor);
100 CSession2::ServiceError(aMessage, aError);
103 // CTokenServer ////////////////////////////////////////////////////////////////
105 inline CTokenServer::CTokenServer() :
106 CServer2(0, ESharableSessions)
110 CServer2* CTokenServer::NewLC()
112 CTokenServer* self=new(ELeave) CTokenServer;
113 CleanupStack::PushL(self);
118 CTokenServer::~CTokenServer()
120 FSResources::Cleanup();
123 delete iKeyStoreServer;
124 delete iCertStoreServer;
125 delete iCertAppsServer;
128 /** 2nd phase construction - ensure the timer and server objects are running. */
129 void CTokenServer::ConstructL()
131 FSResources::InitialiseL();
132 FSDialog::InitialiseL();
134 TPtrC serverName(KFSTokenServerName());
135 // Naming the server thread after the server helps to debug panics
137 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
138 serverName.Set(KFSNewTokenServerName());
139 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
144 // Ensure that the server still exits even if the 1st client fails to connect
145 iShutdown.ConstructL();
149 /** A new session is being created - cancel the shutdown timer if it was running. */
150 void CTokenServer::AddSession()
156 /** A session is being destroyed - start the shutdown timer if it is the last session. */
157 void CTokenServer::DropSession()
159 if (--iSessionCount==0)
165 /** Lazily create key store server object. */
166 CFSKeyStoreServer& CTokenServer::KeyStoreServerL() const
168 if (!iKeyStoreServer)
170 iKeyStoreServer = CFSKeyStoreServer::NewL();
173 return *iKeyStoreServer;
176 /** Lazily create cert store server object. */
177 CFSCertStoreServer& CTokenServer::CertStoreServerL() const
179 if (!iCertStoreServer)
181 iCertStoreServer = CFSCertStoreServer::NewL();
184 return *iCertStoreServer;
187 /** Lazily create cert apps server object. */
188 CFSCertAppsServer& CTokenServer::CertAppsServerL() const
190 if (!iCertAppsServer)
192 iCertAppsServer = CFSCertAppsServer::NewL();
195 return *iCertAppsServer;
198 /** Create a new client session. */
199 CSession2* CTokenServer::NewSessionL(const TVersion& aVersion, const RMessage2& /*aMessage*/) const
201 // The token the client wants to talk to is encoded in the major version number
202 ETokenEnum token = static_cast<ETokenEnum>(aVersion.iMajor);
204 // The minor version number represents the version of the protocol
205 if (aVersion.iMinor != KFSProtolVersion)
207 User::Leave(KErrNotSupported);
210 CTokenServerSession* result = NULL;
215 result = KeyStoreServerL().CreateSessionL();
219 result = CertStoreServerL().CreateSessionL();
223 result = CertAppsServerL().CreateSessionL();
227 User::Leave(KErrNotSupported);
234 // CShutdown ///////////////////////////////////////////////////////////////////
236 inline CShutdown::CShutdown() :
239 CActiveScheduler::Add(this);
242 inline void CShutdown::ConstructL()
244 CTimer::ConstructL();
247 inline void CShutdown::Start()
249 After(KServerShutdownDelay);
252 /** Initiate server exit when the timer expires. */
253 void CShutdown::RunL()
255 CActiveScheduler::Stop();
258 // Server startup //////////////////////////////////////////////////////////////
261 * Perform all server initialisation, in particular creation of the scheduler
262 * and server and then run the scheduler.
264 static void RunServerL()
266 TPtrC serverName(KFSTokenServerName());
267 // Naming the server thread after the server helps to debug panics
269 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
270 serverName.Set(KFSNewTokenServerName());
271 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
274 User::LeaveIfError(User::RenameThread(serverName));
276 // Create and install the active scheduler we need
277 CActiveScheduler* s=new(ELeave) CActiveScheduler;
278 CleanupStack::PushL(s);
279 CActiveScheduler::Install(s);
281 // Create the server and leave it on the cleanup stack
282 CTokenServer::NewLC();
284 // Before starting the server, notify client that initialisation is
286 // (note that WINS on EKA1 uses threads since it lacks process emulation)
287 RProcess::Rendezvous(KErrNone);
290 CActiveScheduler::Start();
292 // Cleanup the server and scheduler
293 CleanupStack::PopAndDestroy(2);
296 /** Server process entry point. */
300 TokenServerDebug::HeapCheckStart();
303 CTrapCleanup* cleanup=CTrapCleanup::New();
307 TRAP(r,RunServerL());
312 TokenServerDebug::HeapCheckEnd();
317 // Only for wins on EKA1 - WINS loads a DLL and starts a new thread
318 // by calling WinsMain which does the "server" startup