cryptoservices/filebasedcertificateandkeystores/source/generic/server/fsserver.cpp
changeset 0 2c201484c85f
child 8 35751d3474b7
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     1 /*
       
     2 * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     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".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 * fstokenserver.cpp
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include "fsserver.h"
       
    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"
       
    29 #include "FSDialog.h"
       
    30 #include "tokenserverdebug.h"
       
    31 #include "fstokenservername.h"
       
    32 
       
    33 // CTokenServerSession /////////////////////////////////////////////////////////
       
    34 
       
    35 CTokenServerSession::CTokenServerSession()
       
    36 	{
       
    37 	}
       
    38 
       
    39 inline CTokenServer& CTokenServerSession::Server()
       
    40 	{
       
    41 	return static_cast<CTokenServer&>(const_cast<CServer2&>(*CSession2::Server()));
       
    42 	}
       
    43 
       
    44 void CTokenServerSession::CreateL()
       
    45 	{
       
    46 	Server().AddSession();
       
    47 	}
       
    48 
       
    49 CTokenServerSession::~CTokenServerSession()
       
    50 	{
       
    51 	Server().DropSession();
       
    52 	}
       
    53 
       
    54 /**
       
    55  * Handle a client request.  Leaving is handled by
       
    56  * CTokenServerSession::ServiceError() which reports the error code to the
       
    57  * client.
       
    58  */
       
    59 void CTokenServerSession::ServiceL(const RMessage2& aMessage)
       
    60 	{
       
    61 #ifdef _DEBUG
       
    62 	// OOM testing
       
    63 	switch (aMessage.Function())
       
    64 		{
       
    65 		case EStartOOMTest:
       
    66 			TokenServerDebug::StartOOMTest();
       
    67 			aMessage.Complete(KErrNone);
       
    68 			return;
       
    69 			
       
    70 		case EIncHeapFailPoint:
       
    71 			TokenServerDebug::IncHeapFailPoint();
       
    72 			aMessage.Complete(KErrNone);
       
    73 			return;
       
    74 			
       
    75 		case EResetHeapFail:
       
    76 			TokenServerDebug::ResetHeapFail();
       
    77 			aMessage.Complete(KErrNone);
       
    78 			return;
       
    79 
       
    80 		case EAllocCount:
       
    81 			aMessage.Complete(User::CountAllocCells());
       
    82 			return;
       
    83 		}	
       
    84 #endif
       
    85 	
       
    86 	DoServiceL(aMessage);
       
    87 	}
       
    88 
       
    89 /**
       
    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.
       
    92  */
       
    93 void CTokenServerSession::ServiceError(const RMessage2& aMessage, TInt aError)
       
    94 	{
       
    95 	if (aError==KErrBadDescriptor)
       
    96 		{
       
    97 		PanicClient(aMessage, EPanicBadDescriptor);
       
    98 		}
       
    99 	
       
   100 	CSession2::ServiceError(aMessage, aError);
       
   101 	}
       
   102 
       
   103 // CTokenServer ////////////////////////////////////////////////////////////////
       
   104 
       
   105 inline CTokenServer::CTokenServer() :
       
   106 	CServer2(0, ESharableSessions)
       
   107 	{
       
   108 	}
       
   109 
       
   110 CServer2* CTokenServer::NewLC()
       
   111 	{
       
   112 	CTokenServer* self=new(ELeave) CTokenServer;
       
   113 	CleanupStack::PushL(self);
       
   114 	self->ConstructL();
       
   115 	return self;
       
   116 	}
       
   117 
       
   118 CTokenServer::~CTokenServer()
       
   119 	{
       
   120 	FSResources::Cleanup();
       
   121 	FSDialog::Cleanup();
       
   122 
       
   123 	delete iKeyStoreServer;
       
   124 	delete iCertStoreServer;
       
   125 	delete iCertAppsServer;
       
   126 	}
       
   127 
       
   128 /**	2nd phase construction - ensure the timer and server objects are running. */
       
   129 void CTokenServer::ConstructL()
       
   130 	{
       
   131 	FSResources::InitialiseL();
       
   132 	FSDialog::InitialiseL();
       
   133 	StartL(KFSTokenServerName);
       
   134 
       
   135 	// Ensure that the server still exits even if the 1st client fails to connect
       
   136 	iShutdown.ConstructL();
       
   137 	iShutdown.Start();
       
   138 	}
       
   139 
       
   140 /** A new session is being created - cancel the shutdown timer if it was running. */
       
   141 void CTokenServer::AddSession()
       
   142 	{
       
   143 	++iSessionCount;
       
   144 	iShutdown.Cancel();
       
   145 	}
       
   146 
       
   147 /** A session is being destroyed - start the shutdown timer if it is the last session. */
       
   148 void CTokenServer::DropSession()
       
   149 	{
       
   150 	if (--iSessionCount==0)
       
   151 		{
       
   152 		iShutdown.Start();
       
   153 		}
       
   154 	}
       
   155 
       
   156 /** Lazily create key store server object. */
       
   157 CFSKeyStoreServer& CTokenServer::KeyStoreServerL() const
       
   158 	{
       
   159 	if (!iKeyStoreServer)
       
   160 		{
       
   161 		iKeyStoreServer = CFSKeyStoreServer::NewL();
       
   162 		}
       
   163 	
       
   164 	return *iKeyStoreServer;
       
   165 	}
       
   166 
       
   167 /** Lazily create cert store server object. */
       
   168 CFSCertStoreServer& CTokenServer::CertStoreServerL() const
       
   169 	{
       
   170 	if (!iCertStoreServer)
       
   171 		{
       
   172 		iCertStoreServer = CFSCertStoreServer::NewL();
       
   173 		}
       
   174 	
       
   175 	return *iCertStoreServer;
       
   176 	}
       
   177 
       
   178 /** Lazily create cert apps server object. */
       
   179 CFSCertAppsServer& CTokenServer::CertAppsServerL() const
       
   180 	{
       
   181 	if (!iCertAppsServer)
       
   182 		{
       
   183 		iCertAppsServer = CFSCertAppsServer::NewL();
       
   184 		}
       
   185 	
       
   186 	return *iCertAppsServer;
       
   187 	}
       
   188 
       
   189 /** Create a new client session. */
       
   190 CSession2* CTokenServer::NewSessionL(const TVersion& aVersion, const RMessage2& /*aMessage*/) const
       
   191 	{
       
   192 	// The token the client wants to talk to is encoded in the major version number
       
   193 	ETokenEnum token = static_cast<ETokenEnum>(aVersion.iMajor);	
       
   194 
       
   195 	// The minor version number represents the version of the protocol
       
   196 	if (aVersion.iMinor != KFSProtolVersion)
       
   197 		{
       
   198 		User::Leave(KErrNotSupported);
       
   199 		}
       
   200 
       
   201 	CTokenServerSession* result = NULL;
       
   202 	
       
   203 	switch (token)
       
   204 		{
       
   205 		case EFileKeyStore:
       
   206 			result = KeyStoreServerL().CreateSessionL();
       
   207 			break;
       
   208 
       
   209 		case EFileCertStore:
       
   210 			result = CertStoreServerL().CreateSessionL();
       
   211 			break;
       
   212 			
       
   213 		case EFileCertApps:
       
   214 			result = CertAppsServerL().CreateSessionL();
       
   215 			break;
       
   216 			
       
   217 		default:
       
   218 			User::Leave(KErrNotSupported);
       
   219 			break;
       
   220 		}
       
   221 
       
   222 	return result;
       
   223 	}
       
   224 
       
   225 // CShutdown ///////////////////////////////////////////////////////////////////
       
   226 
       
   227 inline CShutdown::CShutdown() :
       
   228 	CTimer(-1)
       
   229 	{
       
   230 	CActiveScheduler::Add(this);
       
   231 	}
       
   232 
       
   233 inline void CShutdown::ConstructL()
       
   234 	{
       
   235 	CTimer::ConstructL();
       
   236 	}
       
   237 
       
   238 inline void CShutdown::Start()
       
   239 	{
       
   240 	After(KServerShutdownDelay);
       
   241 	}
       
   242 
       
   243 /** Initiate server exit when the timer expires. */
       
   244 void CShutdown::RunL()
       
   245 	{
       
   246 	CActiveScheduler::Stop();
       
   247 	}
       
   248 
       
   249 // Server startup //////////////////////////////////////////////////////////////
       
   250 
       
   251 /**
       
   252  * Perform all server initialisation, in particular creation of the scheduler
       
   253  * and server and then run the scheduler.
       
   254  */
       
   255 static void RunServerL()
       
   256 	{
       
   257 	// Naming the server thread after the server helps to debug panics
       
   258 	User::LeaveIfError(User::RenameThread(KFSTokenServerName));
       
   259 	
       
   260 	// Create and install the active scheduler we need
       
   261 	CActiveScheduler* s=new(ELeave) CActiveScheduler;
       
   262 	CleanupStack::PushL(s);
       
   263 	CActiveScheduler::Install(s);
       
   264 	
       
   265 	// Create the server and leave it on the cleanup stack
       
   266 	CTokenServer::NewLC();
       
   267 	
       
   268 	// Before starting the server, notify client that initialisation is
       
   269 	// complete.
       
   270 	// (note that WINS on EKA1 uses threads since it lacks process emulation)
       
   271 	RProcess::Rendezvous(KErrNone);
       
   272 
       
   273 	// Ready to run
       
   274 	CActiveScheduler::Start();
       
   275 	
       
   276 	// Cleanup the server and scheduler
       
   277 	CleanupStack::PopAndDestroy(2);
       
   278 	}
       
   279 
       
   280 /** Server process entry point. */
       
   281 TInt E32Main()
       
   282 	{
       
   283 #ifdef _DEBUG
       
   284 	TokenServerDebug::HeapCheckStart();
       
   285 #endif
       
   286 
       
   287 	CTrapCleanup* cleanup=CTrapCleanup::New();
       
   288 	TInt r=KErrNoMemory;
       
   289 	if (cleanup)
       
   290 		{
       
   291 		TRAP(r,RunServerL());
       
   292 		delete cleanup;
       
   293 		}
       
   294 
       
   295 #ifdef _DEBUG
       
   296 	TokenServerDebug::HeapCheckEnd();
       
   297 #endif
       
   298 	return r;
       
   299 	}
       
   300 
       
   301 // Only for wins on EKA1 - WINS loads a DLL and starts a new thread
       
   302 // by calling WinsMain which does the "server" startup