dosservices/dosserver/src/dosserver.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 /*
       
     2 * Copyright (c) 2002 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 "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 *    Implementation for the CDosServer class
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include <e32svr.h>
       
    21 #include <f32file.h>
       
    22 #include "dosserver.h"
       
    23 #include "doseventmanager.h"
       
    24 #include "dosfactorybase.h"
       
    25 #include "dossession.h"
       
    26 #include "dos_debug.h"
       
    27 #include <data_caging_path_literals.hrh>
       
    28 
       
    29 /*******************************************************
       
    30 *  DEFINITION OF THE DSY TO BE USED BY THIS DOS SERVER *
       
    31 ********************************************************/
       
    32 
       
    33 // This file tells the name of the DSY -module to be loaded.
       
    34 _LIT(KDosServerIniFile,"Z:\\private\\101f6efa\\DosServer.ini");
       
    35 
       
    36 // The path where the DSY-module binary must recide. This is hardcoded for
       
    37 // security reasons.
       
    38 _LIT(KDSYBinaryLocDrive, "z:");
       
    39 
       
    40 //
       
    41 // ---------------------------------------------------------
       
    42 // CDosServer Constructor
       
    43 // ---------------------------------------------------------
       
    44 //  
       
    45 CDosServer::CDosServer()
       
    46     : CPolicyServer( CActive::EPriorityStandard, KDosServerPolicy )
       
    47 {
       
    48 	iSessionCount=0;
       
    49 	iContainerIx = NULL;
       
    50 	iDosFactory = NULL;
       
    51 	iLibLoaded = EFalse;
       
    52 	iCurrentFreeDiskSpaceRequest = NULL;
       
    53 }
       
    54 
       
    55 
       
    56 //
       
    57 // ---------------------------------------------------------
       
    58 // CDosServer Desctructor
       
    59 // ---------------------------------------------------------
       
    60 //  
       
    61 CDosServer::~CDosServer()
       
    62 {
       
    63 	COM_TRACE_( "[DOSSERVER]\t CDosServer::~CDosServer()" );
       
    64 
       
    65 	// Delete our own stuff
       
    66 	delete iEventManager;
       
    67 
       
    68 	//We delete here all the open Sessions
       
    69 	//This is done also by ~CServer but we do it here because it must be done before
       
    70 	//deleting iContainerIx since iContainerIx will try to delete the CObjects that are
       
    71 	//referenced by the Session's containers
       
    72 
       
    73 	CDosSession* session=NULL;
       
    74 	iSessionIter.SetToFirst();
       
    75 	while((session = (CDosSession*)(iSessionIter++)) != NULL)
       
    76 	{
       
    77 		delete session;
       
    78 	}
       
    79 	
       
    80 	if ( iFileSystemNotifier )
       
    81         {
       
    82         COM_TRACE_( "CDosServer::~CDosServer(), deleting iFileSytemNotifier" );
       
    83         iFileSystemNotifier->Cancel();
       
    84         delete iFileSystemNotifier;
       
    85         }
       
    86 	delete iDosFactory;
       
    87 	delete iContainerIx;
       
    88 
       
    89 	COM_TRACE_( "[DOSSERVER]\t CDosServer::~CDosServer() - end" );
       
    90 
       
    91 	//Check if RLibrary::Load() has been called
       
    92 	if (iLibLoaded) 
       
    93 	{
       
    94 		iLib.Close();
       
    95 		iLibLoaded = EFalse;
       
    96 	}
       
    97 
       
    98     if ( iSaeThread )
       
    99     {
       
   100     delete iSaeThread;
       
   101     }
       
   102 }  
       
   103 
       
   104 //
       
   105 // ---------------------------------------------------------
       
   106 // CDosServer::ConstructL
       
   107 // ---------------------------------------------------------
       
   108 //  
       
   109 
       
   110 void CDosServer::ConstructL()
       
   111 {
       
   112 	User::RenameThread(KDosServerName);
       
   113     // set this as a system thread so if it crashes the phone will restart
       
   114 	User::SetCritical(User::ESystemCritical);
       
   115 	COM_TRACE_( "CDosServer::ConstructL(), creating iFileSytemNotifier" );
       
   116     iFileSystemNotifier = CSharedDataFileSystemNotifier::NewL(*this);
       
   117 
       
   118 	iEventManager = CDosEventManager::NewL();
       
   119 	LoadDsyModuleL();
       
   120 	iContainerIx = CObjectConIx::NewL();
       
   121 	
       
   122 	StartL(KDosServerName);
       
   123 	
       
   124     iSaeThread = CSaeThread::NewL();
       
   125     iSaeThread->StartThreadL();
       
   126 }
       
   127 
       
   128 //
       
   129 // ---------------------------------------------------------
       
   130 // CDosServer::NewL
       
   131 // ---------------------------------------------------------
       
   132 //  
       
   133 CDosServer* CDosServer::NewL()
       
   134 {
       
   135     CDosServer* self=new(ELeave) CDosServer;
       
   136     CleanupStack::PushL(self);
       
   137     self->ConstructL();
       
   138     CleanupStack::Pop();
       
   139 
       
   140     return self;
       
   141 }
       
   142 
       
   143 //
       
   144 // ---------------------------------------------------------
       
   145 // CDosServer::LoadDsyModuleL
       
   146 // ---------------------------------------------------------
       
   147 //  
       
   148 
       
   149 void CDosServer::LoadDsyModuleL()
       
   150 {
       
   151 	COM_TRACE_( "[DOSSERVER]\t CDosServer::LoadDsyModuleL()" );
       
   152 
       
   153 	//Finding out the name of the Dsy file.
       
   154 	RFs fs;
       
   155 	RFile file;
       
   156 
       
   157 	User::LeaveIfError(fs.Connect());
       
   158 	CleanupClosePushL(fs);
       
   159 
       
   160 	COM_TRACE_( "[DOSSERVER]\t CDosServer::LoadDsyModuleL() - fs connected" );
       
   161 
       
   162 	COM_TRACE_( "[DOSSERVER]\t Opening file: Z:\\system\\data\\DosServer.ini" );
       
   163 	COM_TRACE_( "[DOSSERVER]\t error occurs if above file is missing from build!" );
       
   164 
       
   165 	User::LeaveIfError(file.Open(fs,KDosServerIniFile,EFileRead));
       
   166 	CleanupClosePushL(file);
       
   167 
       
   168 	COM_TRACE_( "[DOSSERVER]\t CDosServer::LoadDsyModuleL() - file opened" );
       
   169 
       
   170 	TBuf8<KMaxFileName> rawFileName;
       
   171 	
       
   172 	User::LeaveIfError(file.Read(rawFileName));
       
   173 
       
   174 	CleanupStack::PopAndDestroy(2); //fs and file are closed
       
   175 
       
   176 	COM_TRACE_( "[DOSSERVER]\t CDosServer::LoadDsyModuleL() - file readed" );
       
   177 
       
   178 	HBufC* unicodeFileName = HBufC::NewLC(rawFileName.Length());
       
   179 	TPtr fileNamePtr(unicodeFileName->Des());
       
   180 	fileNamePtr.Copy(rawFileName);
       
   181 
       
   182 	COM_TRACE_1( "[DOSSERVER]\t CDosServer::LoadDsyModuleL() - name: %S", unicodeFileName );
       
   183 
       
   184 	//Loading of the Dsy module
       
   185 	TLibraryFunction entryPoint;
       
   186 
       
   187 	// Check for data caging support;
       
   188 	// Template parameter defines the maximum length for descriptor. 
       
   189 	// After copy the length is set to same as the copied descriptor.
       
   190 	TBuf<128> dsyBinaryLoc(KDC_SHARED_LIB_DIR);
       
   191 	dsyBinaryLoc.Insert(0, KDSYBinaryLocDrive);
       
   192 
       
   193 #ifdef _DEBUG // Trace used binary path.
       
   194 	HBufC* dsyFileName = HBufC::NewLC(dsyBinaryLoc.Length());
       
   195 	TPtr fNPtr(dsyFileName->Des());
       
   196 	fNPtr.Copy(dsyBinaryLoc);
       
   197 
       
   198 	COM_TRACE_1( "[DOSSERVER]\t CDosServer::LoadDsyModuleL() - bin path: %S", dsyFileName );
       
   199 	CleanupStack::PopAndDestroy(); //dsyFileName
       
   200 #endif
       
   201 	User::LeaveIfError(iLib.Load(fileNamePtr, dsyBinaryLoc));
       
   202 
       
   203 	iLibLoaded = ETrue; //RLibrary::Load() has been called
       
   204 	entryPoint = iLib.Lookup(1); //Polymorphic DLL only 1 entry point.
       
   205 	iDosFactory = (CDosFactoryBase *)entryPoint();
       
   206 	iDosFactory->iEventManager = CONST_CAST(CDosEventManager*,iEventManager);
       
   207 	CleanupStack::PopAndDestroy(); //unicodeFileName
       
   208 
       
   209 	COM_TRACE_( "[DOSSERVER]\t CDosServer::LoadDsyModuleL() completed" );
       
   210 }
       
   211 
       
   212 
       
   213 //
       
   214 // ---------------------------------------------------------
       
   215 // CDosServer::RunError
       
   216 // ---------------------------------------------------------
       
   217 //  
       
   218 TInt CDosServer::RunError(TInt aError)
       
   219 {
       
   220 	//
       
   221 	// Handles an error from CDosSession::ServiceL()
       
   222 	// Report the error to the client
       
   223 	//
       
   224 	COM_TRACE_1( "[DOSSERVER]\t CDosServer::RunError() error: %d", aError );
       
   225 
       
   226 	Message().Complete(aError);
       
   227 	ReStart();
       
   228 	return KErrNone;	// handled the error fully
       
   229 }
       
   230 
       
   231 //
       
   232 // ---------------------------------------------------------
       
   233 // CDosServer::NewSessionL
       
   234 // ---------------------------------------------------------
       
   235 //  
       
   236 
       
   237 CSession2* CDosServer::NewSessionL(const TVersion&) const
       
   238 {
       
   239 	COM_TRACE_( "CDosServer::NewSessionL()" );
       
   240 	//
       
   241 	// Create a new client session.
       
   242 	//
       
   243 	return new(ELeave) CDosSession();
       
   244 }
       
   245 
       
   246 CSession2* CDosServer::NewSessionL(const TVersion& aVersion, const RMessage2& /*aMessage*/) const
       
   247 {
       
   248 	return NewSessionL(aVersion);
       
   249 }
       
   250 //
       
   251 // ---------------------------------------------------------
       
   252 // CDosServer::AddSession
       
   253 // ---------------------------------------------------------
       
   254 //  
       
   255 
       
   256 void CDosServer::AddSession()
       
   257 {
       
   258 	//
       
   259 	// A new session is being created
       
   260 	//
       
   261 	++iSessionCount;
       
   262 	COM_TRACE_1( "[DOSSERVER]\t ** CDosServer - Add session, count: %d", iSessionCount );
       
   263 }
       
   264 
       
   265 //
       
   266 // ---------------------------------------------------------
       
   267 // CDosServer::DropSession
       
   268 // ---------------------------------------------------------
       
   269 //  
       
   270 
       
   271 void CDosServer::DropSession()
       
   272 {
       
   273 	//
       
   274 	// A session is being destroyed
       
   275 	//
       
   276     iSessionCount--;
       
   277 	COM_TRACE_1( "[DOSSERVER]\t ** CDosServer - Drop session, count: %d", iSessionCount );
       
   278 }
       
   279 
       
   280 //
       
   281 // ---------------------------------------------------------
       
   282 // CDosServer::ThreadStart
       
   283 //
       
   284 // It Starts the Server's Thread and the Server
       
   285 // ---------------------------------------------------------
       
   286 //  
       
   287 
       
   288 EXPORT_C TInt CDosServer::ThreadStart( CDosServer::TSignal& /*aSignal*/ )
       
   289 {
       
   290     // Get thread that started us
       
   291 	__UHEAP_MARK;
       
   292 	TInt err(KErrNone);
       
   293 
       
   294     CTrapCleanup* cleanup = NULL;
       
   295     CActiveScheduler* scheduler = NULL;
       
   296     CDosServer* server = NULL;
       
   297 	TInt srvErr=KErrNoMemory;
       
   298 
       
   299     cleanup = CTrapCleanup::New();
       
   300     scheduler = new CActiveScheduler;
       
   301 
       
   302     if ( cleanup && scheduler )
       
   303     {
       
   304         CActiveScheduler::Install( scheduler );
       
   305         TRAP(srvErr, server = CDosServer::NewL());      // adds server in scheduler
       
   306     }
       
   307 
       
   308     if ( !cleanup || !scheduler || srvErr!=KErrNone )
       
   309     {
       
   310         if ( scheduler ) 
       
   311 		{
       
   312 			delete scheduler;
       
   313 			scheduler = NULL;
       
   314 		}
       
   315 		if ( server )
       
   316 		{
       
   317 			delete server;
       
   318 			server = NULL;
       
   319 		}
       
   320 		if ( cleanup )
       
   321 		{
       
   322 	        delete cleanup;
       
   323 			cleanup = NULL;
       
   324 		}
       
   325         err = srvErr;
       
   326     }
       
   327 
       
   328     // signal that we've started
       
   329 	COM_TRACE_( "CDosServer::ThreadStart - calling SignalClient()" );
       
   330 	SignalClient();
       
   331 
       
   332     if ( err == KErrNone )
       
   333     {
       
   334         // start fielding requests from clients
       
   335         CActiveScheduler::Start();
       
   336 
       
   337         // comes here if server gets shut down
       
   338 
       
   339         delete scheduler;
       
   340 		scheduler = NULL;
       
   341         delete server;
       
   342 		server = NULL;
       
   343         delete cleanup;
       
   344 		cleanup = NULL;
       
   345     }
       
   346 
       
   347     // thread/process exit
       
   348 	__UHEAP_MARKEND;
       
   349     return err;
       
   350 }
       
   351 
       
   352 //
       
   353 // ---------------------------------------------------------
       
   354 // CDosServer::SignalClient
       
   355 //
       
   356 // Signal client we've started.
       
   357 // ---------------------------------------------------------
       
   358 //  
       
   359 void CDosServer::SignalClient()
       
   360 {
       
   361 	COM_TRACE_( "[DOSSERVER]\t CDosServer::SignalClient() called" );
       
   362 	RSemaphore startupSemaphore;
       
   363 	if ( startupSemaphore.OpenGlobal( KServerStartupSemaphoreName ) == KErrNone )
       
   364 	{
       
   365 		//Signal the client:The server might have started up successfully or not
       
   366 		startupSemaphore.Signal();
       
   367 		COM_TRACE_( "[DOSSERVER]\t CDosServer::SignalClient() semaphore signalled" );
       
   368 	}
       
   369 
       
   370 	startupSemaphore.Close();
       
   371 	COM_TRACE_( "[DOSSERVER]\t CDosServer::SignalClient() semaphore closed" );
       
   372 
       
   373 	// No point to return any error; 
       
   374 	// if signal client fails, startup will freeze anyway.
       
   375 }
       
   376 
       
   377 //
       
   378 // RMessage::Panic() also completes the message. This is:
       
   379 // (a) important for efficient cleanup within the kernel
       
   380 // (b) a problem if the message is completed a second time
       
   381 //
       
   382 void PanicClient(const RMessage2& aMessage,TDosPanic aPanic)
       
   383 {
       
   384 	COM_TRACE_( "[DOSSERVER]\t CDosServer::RMessage::PanicClient() called" );
       
   385 
       
   386 	aMessage.Panic(KDosServerName,aPanic);
       
   387 }
       
   388 
       
   389 //
       
   390 // ---------------------------------------------------------
       
   391 // CDosServer::CurrentDiskFreeSpace
       
   392 //
       
   393 // Returns current disk space
       
   394 // ---------------------------------------------------------
       
   395 //  
       
   396 TInt* CDosServer::CurrentFreeDiskSpaceRequest()
       
   397 {
       
   398     COM_TRACE_( "CDosServer::CurrentFreeDiskSpaceRequest()" );
       
   399     return iCurrentFreeDiskSpaceRequest;    
       
   400 }
       
   401 
       
   402 //
       
   403 // ---------------------------------------------------------
       
   404 // CDosServer::SetCurrentFreeDiskSpace
       
   405 //
       
   406 // Sets new current disk space
       
   407 // ---------------------------------------------------------
       
   408 //  
       
   409 void CDosServer::SetCurrentFreeDiskSpaceRequest(TInt* diskSpace)
       
   410 {
       
   411  COM_TRACE_( "CDosServer::SetCurrentFreeDiskSpaceRequest()" );
       
   412     iCurrentFreeDiskSpaceRequest = diskSpace;    
       
   413 }
       
   414