commsprocess/commsrootserverconfig/TE_rootserver/src/TE_RootServerServer.cpp
changeset 72 ae47d0499bee
equal deleted inserted replaced
68:5da8188e392b 72:ae47d0499bee
       
     1 // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 // This main DLL entry point for the TE_ConServer.dll
       
    16 
       
    17 // EPOC includes
       
    18 #include <e32base.h>
       
    19 
       
    20 // RootServer includes
       
    21 #include <rsshared.h>
       
    22 
       
    23 // Test system includes
       
    24 #include <comms-infras/commsdebugutility.h>
       
    25 #include "TestStepRootServer.h"
       
    26 #include "TestAsynchHandler.h"
       
    27 #include "RootServerTestSection1.h"
       
    28 #include "RootServerTestSection2.h"
       
    29 #include "RootServerTestSection3.h"
       
    30 #include "RootServerTestSection4.h"
       
    31 #include "RootServerTestSection5.h"
       
    32 #include "RootServerTestSection6.h"
       
    33 #include "RootServerTestSection7.h"
       
    34 #include "RootServerTestSection8.h"
       
    35 
       
    36 _LIT(KRootServerExe, "c32exe.exe");
       
    37 _LIT(KRootServerProcessPattern, "c32exe*");
       
    38 
       
    39 EXPORT_C CTE_RootServerServer* NewRootServerServer( void ) 
       
    40 //
       
    41 // NewTestSuiteRootServerL is exported at ordinal 1
       
    42 // this provides the interface to allow schedule test
       
    43 // to create instances of this test suite
       
    44 //
       
    45 	{
       
    46 
       
    47 	return new(ELeave) CTE_RootServerServer();
       
    48     }
       
    49 
       
    50 	
       
    51 	
       
    52 // __EDIT_ME__ - Substitute the name of your test server 
       
    53 _LIT(KServerName,"te_rootserver");
       
    54 // __EDIT_ME__ - Use your own server class name
       
    55 CTE_RootServerServer* CTE_RootServerServer::NewL()
       
    56 /**
       
    57  * @return - Instance of the test server
       
    58  * Called inside the MainL() function to create and start the
       
    59  * CTestServer derived server.
       
    60  */
       
    61 	{
       
    62 	// __EDIT_ME__ new your server class here
       
    63 	CTE_RootServerServer * server = new (ELeave) CTE_RootServerServer();
       
    64 	CleanupStack::PushL(server);
       
    65 	
       
    66 	// Either use a StartL or ConstructL, the latter will permit
       
    67 	// Server Logging.
       
    68 
       
    69 	//server->StartL(KServerName); 
       
    70 	server-> ConstructL(KServerName);
       
    71 	CleanupStack::Pop(server);
       
    72 	return server;
       
    73 	}
       
    74 
       
    75 // EKA2 much simpler
       
    76 // Just an E32Main and a MainL()
       
    77 LOCAL_C void MainL()
       
    78 /**
       
    79  * Much simpler, uses the new Rendezvous() call to sync with the client
       
    80  */
       
    81 	{
       
    82 	// Leave the hooks in for platform security
       
    83 #if (defined __DATA_CAGING__)
       
    84 	RProcess().DataCaging(RProcess::EDataCagingOn);
       
    85 	RProcess().SecureApi(RProcess::ESecureApiOn);
       
    86 #endif
       
    87 	CActiveScheduler* sched=NULL;
       
    88 	sched=new(ELeave) CActiveScheduler;
       
    89 	CActiveScheduler::Install(sched);
       
    90 	// __EDIT_ME__ Your server name
       
    91 	CTE_RootServerServer* server = NULL;
       
    92 	// Create the CTestServer derived server
       
    93 	// __EDIT_ME__ Your server name
       
    94 	TRAPD(err,server = CTE_RootServerServer::NewL());
       
    95 	if(!err)
       
    96 		{
       
    97 		// Sync with the client and enter the active scheduler
       
    98 		RProcess::Rendezvous(KErrNone);
       
    99 		sched->Start();
       
   100 		}
       
   101 	delete server;
       
   102 	delete sched;
       
   103 	}
       
   104 
       
   105 // Only a DLL on emulator for typhoon and earlier
       
   106 
       
   107 GLDEF_C TInt E32Main()
       
   108 /**
       
   109  * @return - Standard Epoc error code on exit
       
   110  */
       
   111 	{
       
   112 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
   113 	if(cleanup == NULL)
       
   114 		{
       
   115 		return KErrNoMemory;
       
   116 		}
       
   117 	TRAP_IGNORE(MainL());
       
   118 	delete cleanup;
       
   119 	return KErrNone;
       
   120     }
       
   121 
       
   122 // Create a thread in the calling process
       
   123 // Emulator typhoon and earlier
       
   124 
       
   125 // __EDIT_ME__ - Use your own server class name
       
   126 CTestStep* CTE_RootServerServer::CreateTestStep(const TDesC& aStepName)
       
   127 /**
       
   128  * @return - A CTestStep derived instance
       
   129  * Implementation of CTestServer pure virtual
       
   130  */
       
   131 	{
       
   132 	
       
   133 	CTestStep* testStep = NULL;
       
   134 	// __EDIT_ME__ - Create your own test steps here
       
   135 	// This server creates just one step but create as many as you want
       
   136 	// They are created "just in time" when the worker thread is created
       
   137 	
       
   138 	
       
   139 	if(aStepName.Compare(_L("Test1.1")) == 0 )
       
   140 		testStep = new(ELeave) CRootServerTest1_1(this);
       
   141 	else if(aStepName.Compare(_L("Test1.2")) == 0 )
       
   142 	    testStep = new(ELeave) CRootServerTest1_2(this);
       
   143 	else if(aStepName.Compare(_L("Test2.1")) == 0 )
       
   144 	    testStep = new(ELeave) CRootServerTest2_1(this);
       
   145 	else if(aStepName.Compare(_L("Test2.2")) == 0 )
       
   146 	    testStep = new(ELeave) CRootServerTest2_2(this);
       
   147 	else if(aStepName.Compare(_L("Test2.3")) == 0 )
       
   148 	    testStep = new(ELeave) CRootServerTest2_3(this);
       
   149 	//else if(aStepName.Compare(_L("Test3.1")) == 0 )
       
   150 	//    testStep = new(ELeave) CRootServerTest3_1(this);
       
   151 	else if(aStepName.Compare(_L("Test3.2")) == 0 )
       
   152 	    testStep = new(ELeave) CRootServerTest3_2(this);
       
   153 	else if(aStepName.Compare(_L("Test3.3")) == 0 )
       
   154 	    testStep = new(ELeave) CRootServerTest3_3(this);
       
   155 	else if(aStepName.Compare(_L("Test3.4")) == 0 )
       
   156 	    testStep = new(ELeave) CRootServerTest3_4(this);
       
   157 	else if(aStepName.Compare(_L("Test3.5")) == 0 )
       
   158 	    testStep = new(ELeave) CRootServerTest3_5(this);
       
   159 	else if(aStepName.Compare(_L("Test3.6")) == 0 )
       
   160 	    testStep = new(ELeave) CRootServerTest3_6(this);
       
   161 	else if(aStepName.Compare(_L("Test3.7")) == 0 )
       
   162 	    testStep = new(ELeave) CRootServerTest3_7(this);
       
   163 	else if(aStepName.Compare(_L("Test3.8")) == 0 )
       
   164 	    testStep = new(ELeave) CRootServerTest3_8(this);
       
   165 	else if(aStepName.Compare(_L("Test3.9")) == 0 )
       
   166 	    testStep = new(ELeave) CRootServerTest3_9(this);
       
   167 	else if(aStepName.Compare(_L("Test3.10")) == 0 )
       
   168 	    testStep = new(ELeave) CRootServerTest3_10(this);
       
   169 	else if(aStepName.Compare(_L("Test3.11")) == 0 )
       
   170 	    testStep = new(ELeave) CRootServerTest3_11(this);
       
   171 	else if(aStepName.Compare(_L("Test3.12")) == 0 )
       
   172 	    testStep = new(ELeave) CRootServerTest3_12(this);
       
   173 	else if(aStepName.Compare(_L("Test3.13")) == 0 )
       
   174 	    testStep = new(ELeave) CRootServerTest3_13(this);
       
   175 	else if(aStepName.Compare(_L("Test3.14")) == 0 )
       
   176 	    testStep = new(ELeave) CRootServerTest3_14(this);
       
   177 	else if(aStepName.Compare(_L("Test3.15")) == 0 )
       
   178 	    testStep = new(ELeave) CRootServerTest3_15(this);
       
   179 	else if(aStepName.Compare(_L("Test3.16")) == 0 )
       
   180 	    testStep = new(ELeave) CRootServerTest3_16(this);
       
   181 	else if(aStepName.Compare(_L("Test3.17")) == 0 )
       
   182 	    testStep = new(ELeave) CRootServerTest3_17(this);
       
   183 	else if(aStepName.Compare(_L("Test3.18")) == 0 )
       
   184 	    testStep = new(ELeave) CRootServerTest3_18(this);
       
   185 	else if(aStepName.Compare(_L("Test4.1")) == 0 )
       
   186 	    testStep = new(ELeave) CRootServerTest4_1(this);
       
   187 	else if(aStepName.Compare(_L("Test5.1")) == 0 )
       
   188 	    testStep = new(ELeave) CRootServerTest5_1(this);
       
   189 	else if(aStepName.Compare(_L("Test5.2")) == 0 )
       
   190 	    testStep = new(ELeave) CRootServerTest5_2(this);
       
   191 	else if(aStepName.Compare(_L("Test5.3")) == 0 )
       
   192 	    testStep = new(ELeave) CRootServerTest5_3(this);
       
   193 	else if(aStepName.Compare(_L("Test5.4")) == 0 )
       
   194 	    testStep = new(ELeave) CRootServerTest5_4(this);
       
   195 	else if(aStepName.Compare(_L("Test5.5")) == 0 )
       
   196 	    testStep = new(ELeave) CRootServerTest5_5(this);
       
   197 	else if(aStepName.Compare(_L("Test5.6")) == 0 )
       
   198 	    testStep = new(ELeave) CRootServerTest5_6(this);
       
   199 	else if(aStepName.Compare(_L("Test5.7")) == 0 )
       
   200 	    testStep = new(ELeave) CRootServerTest5_7(this);
       
   201 	else if(aStepName.Compare(_L("Test5.8")) == 0 )
       
   202 	    testStep = new(ELeave) CRootServerTest5_8(this);
       
   203 	else if(aStepName.Compare(_L("Test6.1")) == 0 )
       
   204 	    testStep = new(ELeave) CRootServerTest6_1(this);
       
   205 	else if(aStepName.Compare(_L("Test6.2")) == 0 )
       
   206 	    testStep = new(ELeave) CRootServerTest6_2(this);
       
   207 	else if(aStepName.Compare(_L("Test6.3")) == 0 )
       
   208 	    testStep = new(ELeave) CRootServerTest6_3(this);
       
   209 	else if(aStepName.Compare(_L("Test6.4")) == 0 )
       
   210 	    testStep = new(ELeave) CRootServerTest6_4(this);
       
   211 	else if(aStepName.Compare(_L("Test6.5")) == 0 )
       
   212 	    testStep = new(ELeave) CRootServerTest6_5(this);
       
   213 	else if(aStepName.Compare(_L("Test6.6")) == 0 )
       
   214 	    testStep = new(ELeave) CRootServerTest6_6(this);
       
   215 	else if(aStepName.Compare(_L("Test6.7")) == 0 )
       
   216 	    testStep = new(ELeave) CRootServerTest6_7(this);
       
   217 	else if(aStepName.Compare(_L("Test6.8")) == 0 )
       
   218 	    testStep = new(ELeave) CRootServerTest6_8(this);
       
   219 	else if(aStepName.Compare(_L("Test6.9")) == 0 )
       
   220 	    testStep = new(ELeave) CRootServerTest6_9(this);
       
   221 	else if(aStepName.Compare(_L("Test6.10")) == 0 )
       
   222 	    testStep = new(ELeave) CRootServerTest6_10(this);
       
   223 	else if(aStepName.Compare(_L("Test6.11")) == 0 )
       
   224 	    testStep = new(ELeave) CRootServerTest6_11(this);
       
   225 	else if(aStepName.Compare(_L("Test6.12")) == 0 )
       
   226 	    testStep = new(ELeave) CRootServerTest6_12(this);
       
   227 	else if(aStepName.Compare(_L("Test6.13")) == 0 )
       
   228 	    testStep = new(ELeave) CRootServerTest6_13(this);
       
   229 	else if(aStepName.Compare(_L("Test6.14")) == 0 )
       
   230 	    testStep = new(ELeave) CRootServerTest6_14(this);
       
   231 	else if(aStepName.Compare(_L("Test6.15")) == 0 )
       
   232 	    testStep = new(ELeave) CRootServerTest6_15(this);
       
   233 	else if(aStepName.Compare(_L("Test6.16")) == 0 )
       
   234 	    testStep = new(ELeave) CRootServerTest6_16(this);
       
   235 	else if(aStepName.Compare(_L("Test7.1")) == 0 )
       
   236 	    testStep = new(ELeave) CRootServerTest7_1(this);
       
   237 	else if(aStepName.Compare(_L("Test7.2")) == 0 )
       
   238 	    testStep = new(ELeave) CRootServerTest7_2(this);
       
   239 	else if(aStepName.Compare(_L("Test7.3")) == 0 )
       
   240 	    testStep = new(ELeave) CRootServerTest7_3(this);
       
   241 	else if(aStepName.Compare(_L("Test7.4")) == 0 )
       
   242 	    testStep = new(ELeave) CRootServerTest7_4(this);
       
   243 	else if(aStepName.Compare(_L("Test8.1")) == 0 )
       
   244 	    testStep = new(ELeave) CRootServerTest8_1(this);
       
   245 
       
   246 		
       
   247 	return testStep;
       
   248 	}
       
   249 
       
   250 TInt CTE_RootServerServer::StartRootServer()
       
   251 /**
       
   252  * Start the Root Server and connect the standard session to it
       
   253  *
       
   254  * @return TInt - An error code
       
   255  */
       
   256     {    
       
   257     TInt err = KErrNone;
       
   258     TFindServer findCS(KRootServerName);
       
   259     TFullName name;
       
   260     RProcess rootServer;
       
   261 
       
   262     if (findCS.Next(name) == KErrNone)
       
   263 		{
       
   264 		INFO_PRINTF1(_L("Shutting down existing RootServer"));
       
   265 
       
   266 		// Try to shutdown any existing instance of the rootserver so we can
       
   267 		// safely change the MBuf pool size before any modules are loaded.
       
   268 		// A sticky module such as ETel may prevent complete shutdown so we
       
   269 		// kill the process if necessary.
       
   270 		if(!ShutdownRootServer())
       
   271 			{
       
   272 			INFO_PRINTF1(_L("Killing existing RootServer"));
       
   273 
       
   274 			// Kill the rootserver process if normal shutdown fails.
       
   275 			TFindProcess findPS(KRootServerProcessPattern);
       
   276 			err = findPS.Next(name);
       
   277 			if(err == KErrNone)
       
   278 				{
       
   279 	 			err = rootServer.Open(findPS);
       
   280 	 			if(err == KErrNone)
       
   281 	 				{
       
   282 		 			TRequestStatus status;
       
   283 					rootServer.Logon(status);
       
   284 					rootServer.Kill(KErrNone);
       
   285 					User::WaitForRequest(status);
       
   286 					
       
   287 					// Create a timer in case some other entity holds an open handle on the
       
   288 					// configurator which prevents the kernel from destroying it.  We timeout
       
   289 					// after one second.
       
   290 					TAutoClose<RTimer> timer;
       
   291 					if(timer.iObj.CreateLocal() == KErrNone)
       
   292 						{
       
   293 						// Request destruction notification so we know when it is safe to start
       
   294 						// the process again.
       
   295 						TRequestStatus destructionStatus;
       
   296 						rootServer.NotifyDestruction(destructionStatus);
       
   297 						rootServer.Close();
       
   298 					
       
   299 						enum{ KProcessDestructionTimeout = 1000000 };
       
   300 				  					
       
   301 						TRequestStatus timerStatus;
       
   302 						timer.iObj.After(timerStatus, KProcessDestructionTimeout);
       
   303 						
       
   304 						// Wait for the process to be destroyed or for the timeout.
       
   305 						User::WaitForRequest(destructionStatus, timerStatus);
       
   306 						if(timerStatus.Int() == KRequestPending)
       
   307 							{
       
   308 							timer.iObj.Cancel();
       
   309 							User::WaitForRequest(timerStatus);
       
   310 							}
       
   311 						else
       
   312 							{
       
   313 							User::CancelMiscNotifier(destructionStatus);
       
   314 							User::WaitForRequest(destructionStatus);
       
   315 							
       
   316 							INFO_PRINTF2(_L("CTestSuiteRootServer::StartRootServer - existing RootServer process has still not been destroyed after %f.0s"), KProcessDestructionTimeout / 1000000.0);
       
   317 							}
       
   318 						}
       
   319 	 				}
       
   320 				}
       
   321 			}
       
   322 		}
       
   323 	else
       
   324 		{
       
   325 	    err = rootServer.Create(KRootServerExe, KNullDesC);
       
   326 		TRequestStatus status;
       
   327 	    if (err == KErrNone)
       
   328 			{
       
   329 			rootServer.Rendezvous(status);
       
   330 			rootServer.Resume();
       
   331 			User::WaitForRequest(status);
       
   332 			rootServer.Close();
       
   333 			err = status.Int();
       
   334 			}
       
   335 		if(err == KErrNone)
       
   336 			{
       
   337 			err = RootSess().Connect();
       
   338 			}
       
   339 		}
       
   340 
       
   341     return err;
       
   342     }
       
   343 
       
   344 
       
   345 TBool CTE_RootServerServer::ShutdownRootServer()
       
   346 /**
       
   347  * Tell the Root Server to shutdown via its standard session, and validate that it did so.
       
   348  *
       
   349  * @return TBool - True if shutdown was successful
       
   350  */
       
   351     {
       
   352 	TInt err;
       
   353 	// Some tests close the session
       
   354 	if(RootSess().Handle() == 0)
       
   355 		{
       
   356 		err = RootSess().Connect();
       
   357 		if(err != KErrNone)
       
   358 			{
       
   359 			INFO_PRINTF2(_L("Failed to reconnect session (err %d) - maybe dead already?"), err);
       
   360 			return EFalse;
       
   361 			}
       
   362 		}
       
   363 
       
   364 	TRSIter position;
       
   365 	while((err = RootSess().Shutdown()) == KErrRSModulesStillRunning)
       
   366 		{
       
   367 		TCFModuleName moduleName;
       
   368 		err = RootSess().EnumerateModules(position, moduleName);
       
   369 		if(err == KErrEof)
       
   370 			{
       
   371 			INFO_PRINTF1(_L("RRootServ::Shutdown failed, reporting spurious modules"));
       
   372 			return EFalse;
       
   373 			}
       
   374 		TRSModuleInfo moduleInfo;
       
   375 		err = RootSess().GetModuleInfo(moduleName, moduleInfo);
       
   376 		if(err == KErrRSModuleUnknown)
       
   377 			continue;	// an unload probably just completed
       
   378 		else if(err != KErrNone)
       
   379 			{
       
   380 			INFO_PRINTF1(_L("RRootServ::GetModuleInfo failed during shutdown"));
       
   381 			return EFalse;
       
   382 			}
       
   383 		TBuf<KCFMaxModuleName> wideModuleName;
       
   384 		wideModuleName.Copy(moduleName);
       
   385 		INFO_PRINTF3(_L("RRootServ::Shutdown in progress; need to unload %S (state %d)"), &wideModuleName, moduleInfo().iState);
       
   386 		TRequestStatus status;
       
   387 		switch(moduleInfo().iState)
       
   388 			{
       
   389 			case EStarting:		// treat a starting module as a zombie coz we're in a hurry, and who knows how long that rendezvous could take?
       
   390 			case EZombie:
       
   391 				{
       
   392 				RootSess().UnloadCpm(status, moduleName, EUnGraceful);
       
   393 				User::WaitForRequest(status);
       
   394 				if(status != KErrNone && status != KErrRSSuddenDeath && status != KErrRSModuleUnknown)
       
   395 					{
       
   396 					INFO_PRINTF1(_L("RRootServ::Shutdown failed, zombie could not be killed"));
       
   397 					return EFalse;
       
   398 					}
       
   399 				break;
       
   400 				}
       
   401 			case ERunning:
       
   402 				{
       
   403 				RootSess().UnloadCpm(status, moduleName, EGraceful);
       
   404 				User::WaitForRequest(status);
       
   405 				if(status.Int() == KErrLocked)
       
   406 					{
       
   407 					INFO_PRINTF1(_L("Module is sticky and cannot be unloaded"));
       
   408 					
       
   409 					// Move to the next module.
       
   410 					continue;
       
   411 					}
       
   412 				else
       
   413 					{
       
   414 				INFO_PRINTF2(_L("UnloadCPM returned  %d"), status.Int());
       
   415 					}
       
   416 				break;	// either it worked or it should have become a zombie
       
   417 				}
       
   418 			case EStopping:
       
   419 				{
       
   420 				// transition states; snooze and try again
       
   421 				User::After(1000000);
       
   422 				break;
       
   423 				}
       
   424 			default:
       
   425 				{
       
   426 				INFO_PRINTF1(_L("RRootServ::Shutdown failed, module in improper states"));
       
   427 				return EFalse;
       
   428 				}
       
   429 			}
       
   430 			
       
   431 			// Try to unload this module again.
       
   432 			position.Reset();
       
   433 		}
       
   434 	RootSess().Close();
       
   435 	RRootServ probeSess;
       
   436 	TBool res = (probeSess.Connect() != KErrNone);
       
   437 	if(!res)
       
   438 		{
       
   439 		probeSess.Close();
       
   440 		}
       
   441 
       
   442     return res;
       
   443     }
       
   444 
       
   445 
       
   446 
       
   447 // make a version string available for test system 
       
   448 TPtrC CTE_RootServerServer::GetVersion( void )
       
   449 	{
       
   450 
       
   451 #ifdef _DEBUG
       
   452 	_LIT(KTxtVersion,"1.00 (udeb)");
       
   453 #else
       
   454 	_LIT(KTxtVersion,"1.00");
       
   455 #endif
       
   456 
       
   457 	return KTxtVersion();
       
   458 	}
       
   459 
       
   460 
       
   461 CSelfPopScheduler* CSelfPopScheduler::CreateLC()
       
   462 	{
       
   463 	CSelfPopScheduler* self = new(ELeave)CSelfPopScheduler;
       
   464 	CleanupStack::PushL(self);
       
   465 	CActiveScheduler::Install(self);
       
   466 	return self;
       
   467 	}
       
   468 
       
   469 CSelfPopScheduler::operator TCleanupItem()
       
   470 	{
       
   471 	return TCleanupItem(Cleanup, this);
       
   472 	}
       
   473 
       
   474 void CSelfPopScheduler::Cleanup(TAny* aItem)
       
   475 	{
       
   476 	CActiveScheduler::Install(NULL);
       
   477 	delete reinterpret_cast<CSelfPopScheduler*>(aItem);
       
   478 	}