systemhealthmanagement/systemhealthmgr/sysmonsrc/sysmonservsess.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 // Copyright (c) 2006-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 
       
    16 /**
       
    17  @file
       
    18  @internalComponent 
       
    19 */
       
    20 
       
    21 #include <e32capability.h>
       
    22 #include "sysmonservsess.h"
       
    23 #include "sysmoncliserv.h"
       
    24 #include "sysmonserver.h"
       
    25 #include "monitor.h"
       
    26 #include <startupproperties.h>
       
    27 #include <hal.h>
       
    28 #include <hal_data.h>
       
    29 
       
    30 #include "shmadebug.h"
       
    31 
       
    32 
       
    33 CSysMonSession::~CSysMonSession()
       
    34 	{
       
    35 	}
       
    36 
       
    37 
       
    38 CSysMonSession::CSysMonSession()
       
    39 	{
       
    40 	}
       
    41 
       
    42 
       
    43 CSysMonSession* CSysMonSession::NewL()
       
    44 	{
       
    45 	CSysMonSession *session = new(ELeave) CSysMonSession();
       
    46 	return session;
       
    47 	}
       
    48 
       
    49 
       
    50 void CSysMonSession::ServiceL(const RMessage2 &aMessage)
       
    51 	{
       
    52 	DEBUGPRINT1(_L("SysMonServSess: Received message"));
       
    53 
       
    54 	iMessage = const_cast<RMessage2&> (aMessage);
       
    55 	switch (iMessage.Function())
       
    56 		{
       
    57 	case EMonitor:
       
    58 		DoMonitorL(EFalse);
       
    59 		iMessage.Complete(KErrNone);
       
    60 		break;
       
    61 	case EMonitorSelf:
       
    62 		DoMonitorL(ETrue);
       
    63 		iMessage.Complete(KErrNone);
       
    64 		break;
       
    65 	case ECancelSelf:
       
    66 		CancelMonitorSelfL();
       
    67 		iMessage.Complete(KErrNone);
       
    68 		break;
       
    69 	case ECancelAllMonitors:
       
    70 	    {
       
    71 	    SysMonServer()->CancelAllMonitors();
       
    72 	    iMessage.Complete(KErrNone);
       
    73 	    break;
       
    74 	    }
       
    75 	default:
       
    76 		iMessage.Complete(KErrNotSupported);
       
    77 		break;
       
    78 		}
       
    79 	}
       
    80 
       
    81 
       
    82 void CSysMonSession::ServiceError(const RMessage2 &aMessage, TInt aError)
       
    83 	{
       
    84 	aMessage.Complete(aError);
       
    85 	}
       
    86 
       
    87 
       
    88 /*
       
    89 Handles EMonitor and EMonitorSelf message to monitor a process
       
    90 */
       
    91 void CSysMonSession::DoMonitorL(TBool aMonitorSelf)
       
    92 	{
       
    93 	// no new process monitoring will be initiate if shut down is in progress
       
    94 	if (SysMonServer()->ShutDownInProgress())
       
    95 		{
       
    96 		User::Leave(KErrCancel);
       
    97 		}
       
    98 
       
    99 	HBufC8 *msg = HBufC8::NewL(iMessage.GetDesLengthL(0));
       
   100 	CleanupStack::PushL(msg);
       
   101 
       
   102 	TPtr8 msgPtr(msg->Des());
       
   103 	iMessage.ReadL(0, msgPtr);
       
   104 
       
   105 	CStartupProperties *prop = CStartupProperties::NewL();
       
   106 	CleanupStack::PushL(prop);
       
   107 
       
   108 	prop->InternalizeL(msgPtr);
       
   109 
       
   110 	// Retrieve process id depending function type and check for platform security
       
   111 	// For self monitoring, EIgnoreOnFailure does not require any capability
       
   112 	// For moniting other process, EIgnoreOnFailure require capability ProtServ
       
   113 	TProcessId id;
       
   114 	if (aMonitorSelf)
       
   115 		{
       
   116 		RThread thread;
       
   117 		User::LeaveIfError(iMessage.Client(thread));
       
   118 
       
   119 		RProcess process;
       
   120 		User::LeaveIfError(thread.Process(process));
       
   121 
       
   122 		id = process.Id();
       
   123 		
       
   124 		// Use the filename from the proces
       
   125 		TFileName name = process.FileName();
       
   126 		prop->SetFileParamsL(name, prop->Args());
       
   127 		
       
   128 		process.Close();
       
   129 		thread.Close();
       
   130 		}
       
   131 	else
       
   132 		{
       
   133 		if (prop->RecoveryMethod() == EIgnoreOnFailure)
       
   134 			{
       
   135 			if (!iMessage.HasCapability(ECapabilityProtServ))
       
   136 				{
       
   137 				User::Leave(KErrPermissionDenied);
       
   138 				}
       
   139 			}
       
   140 
       
   141 		TPckg<TProcessId> procPckg(id);
       
   142 		iMessage.ReadL(1, procPckg);
       
   143 		}
       
   144 
       
   145 	// For ERestartOS and ECriticalNoRetries, capability ProtServ is required for both self-monitoring and monitoring other
       
   146 	if (prop->RecoveryMethod() == ERestartOS || prop->RecoveryMethod() == ECriticalNoRetries)
       
   147 		{
       
   148 		if (!iMessage.HasCapability(ECapabilityProtServ))
       
   149 			{
       
   150 			User::Leave(KErrPermissionDenied);
       
   151 			}
       
   152 		}
       
   153 	// For ERestartOSWithMode, capability ProtServ and PowerMgmt are required for both self-monitoring and monitoring other
       
   154 	else if (prop->RecoveryMethod() == ERestartOSWithMode)
       
   155 		{
       
   156 		if ((!iMessage.HasCapability(ECapabilityProtServ)) || (!iMessage.HasCapability(ECapabilityPowerMgmt)))
       
   157 			{
       
   158 			User::Leave(KErrPermissionDenied);
       
   159 			}
       
   160 		
       
   161 #ifndef __WINS__ // HAL::Get(EMaximumRestartStartupModes...) is only supported in H4
       
   162 		// check that the startup mode is valid, first retrieve max startup mode if not retrieved yet
       
   163 		TInt maxStartupMode = 0;
       
   164 		TInt err = HAL::Get(HALData::EMaximumRestartStartupModes, maxStartupMode);
       
   165 		if (err != KErrNone)
       
   166 			{
       
   167 			DEBUGPRINT2(_L("Failed to get max startup mode err=%d"), err);
       
   168 			User::Leave(err);
       
   169 			}
       
   170 		
       
   171 		if (prop->RestartMode() > maxStartupMode)
       
   172 			{
       
   173 			DEBUGPRINT2(_L("Invalid startupMode=%d"), prop->RestartMode());
       
   174 			User::Leave(KErrArgument);
       
   175 			}
       
   176 #endif
       
   177 		}
       
   178 
       
   179 #ifdef _DEBUG
       
   180 	TPtrC fileName = prop->FileName();
       
   181 	DEBUGPRINT2(_L("SysMonServSess: Creating monitor for %S"), &fileName);
       
   182 #endif
       
   183 
       
   184 	const TBool KExecuteRecoveryMethodOnFailure = iMessage.Int2();
       
   185 	CMonitor *monitor = CMonitor::NewL(*SysMonServer(), id, prop, KExecuteRecoveryMethodOnFailure); // a new monitor is created - ownership of prop transferred
       
   186 
       
   187 	CleanupStack::Pop(prop); // ownership of prop is passed into monitor
       
   188 	CleanupStack::PushL(monitor);
       
   189 
       
   190 	DEBUGPRINT1(_L("SysMonServSess: Monitor created."));
       
   191 
       
   192 	// new monitor is added to the list maintained by SysMonServer - ownership of monitor transferred.
       
   193 	// monitor will be popped from cleanup stack in AddMonitorL
       
   194 	SysMonServer()->AddAndStartMonitorL(monitor); 
       
   195 
       
   196 	CleanupStack::PopAndDestroy(msg);
       
   197 	}
       
   198 
       
   199 
       
   200 /*
       
   201 CSysMonSession::CancelMonitorSelfL() is invoked to cancel self-monitoring
       
   202 */
       
   203 void CSysMonSession::CancelMonitorSelfL() const
       
   204 	{
       
   205 	RThread thread;
       
   206 	User::LeaveIfError(iMessage.Client(thread));
       
   207 
       
   208 	RProcess process;
       
   209 	User::LeaveIfError(thread.Process(process));
       
   210 
       
   211 	SysMonServer()->CancelMonitor(process.Id());
       
   212 
       
   213 	DEBUGPRINT2(_L("SysMonServSess: Monitor Cancelled. ProcessId=%Lu"), process.Id().Id());
       
   214 	
       
   215 	process.Close();
       
   216 	thread.Close();
       
   217 	}
       
   218 
       
   219 
       
   220 /*
       
   221 CSysMonSession::SysMonServer() returns a pointer to the server object
       
   222 */
       
   223 CSysMonServer* CSysMonSession::SysMonServer() const
       
   224 	{
       
   225 	return static_cast<CSysMonServer*> (const_cast<CServer2*> (Server()));
       
   226 	}