sysstatemgmt/systemstarter/sysmonsrc/sysmonclisess.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 #include <startupproperties.h>
       
    17 
       
    18 #include "sysmonclisess.h"
       
    19 #include "sysmoncliserv.h"
       
    20 #include "../src/SysStartDebug.h"
       
    21 
       
    22 /**
       
    23 Default constructor.
       
    24 Creates an instance of the RSysMonSession class
       
    25 @publishedAll
       
    26 @released
       
    27 */
       
    28 EXPORT_C RSysMonSession::RSysMonSession() : RSessionBase()
       
    29 	{
       
    30 	}
       
    31 
       
    32 /**
       
    33 Opens connection with SysMonServer
       
    34 @return returns KErrNone or an error code.
       
    35 @publishedAll
       
    36 @released
       
    37 */
       
    38 EXPORT_C void RSysMonSession::OpenL()
       
    39 	{
       
    40 	DEBUGPRINT1(_L("SysMonCli: Opening session"));
       
    41 	User::LeaveIfError(CreateSession(KSysMonServerName, TVersion(0,0,0), 1));
       
    42 	}
       
    43 
       
    44 /**
       
    45 Closes connection with SysMonServer
       
    46 @publishedAll
       
    47 @released
       
    48 */
       
    49 EXPORT_C void RSysMonSession::Close()
       
    50 	{
       
    51 	DEBUGPRINT1(_L("SysMonCli: Closing session"));
       
    52 	RSessionBase::Close();
       
    53 	}
       
    54 
       
    55 /**
       
    56 Used to initiate the monitoring of a started component. 
       
    57 Example use:
       
    58 @code
       
    59 	CStartupProperties* prop = CStartupProperties::NewLC(KFilename, KNullDesC);
       
    60 	prop->SetMonitored(ETrue);
       
    61 	prop->SetStartupType(EStartApp);
       
    62 	prop->SetStartMethod(EWaitForStart);
       
    63 	prop->SetNoOfRetries(1);
       
    64 	prop->SetTimeout(1000);
       
    65 	prop->SetRecoveryParams(EIgnoreOnFailure, 0);
       
    66 	iMySysMonSession.MonitorL(*prop, process);
       
    67 	CleanupStack::PopAndDestroy(prop);
       
    68 @endcode
       
    69 
       
    70 @param aStartupProperties Properties for the component to be monitored.
       
    71 		Number of retries and timeout in @c aStartupProperties should only be used with 
       
    72 		start-method @c EWaitForStart. A monitored component with start-method @c EFireAndForget 
       
    73 		will not be restarted on failure, the recovery method will be executed immediately. 
       
    74 		Start-method @c EDeferredWaitForStart can not be used for monitoring.
       
    75 @param aProcess The running instance of the component to be monitored.
       
    76 @param aExecuteRecoveryMethodOnFailure Gives the option to initiate monitoring on an already 
       
    77 		dead process. If the process is already dead, the monitor will be scheduled to recover 
       
    78 		the process. The default behaviour is to leave and not allow monitoring to be setup if 
       
    79 		the process is already dead.
       
    80 @capability ECapabilityProtServ
       
    81 @capability ECapabilityPowerMgmt for ERestartOSWithMode
       
    82 @publishedAll
       
    83 @deprecated
       
    84 @leave KErrDied if @c aProcess is no longer running and @c aExecuteRecoveryMethodOnFailure is false
       
    85 @leave KErrArgument if @c aStartupProperties contains invalid argument or if @c aProcess contain NULL handle
       
    86 @leave KErrPermissionDenied if client does not have the requied PlatSec capability
       
    87 @leave KErrAlreadyExists if the process is already registered for monitoring
       
    88 */
       
    89 EXPORT_C void RSysMonSession::MonitorL(const CStartupProperties& aStartupProperties, const RProcess& aProcess, TBool aExecuteRecoveryMethodOnFailure)
       
    90 	{
       
    91 	DEBUGPRINT1(_L("SysMonCli: Monitor process"));
       
    92 	DoMonitorL(aStartupProperties, &aProcess, aExecuteRecoveryMethodOnFailure);
       
    93 	}
       
    94 
       
    95 /**
       
    96 Used by a started component to initiate self-monitoring.
       
    97 
       
    98 @param aStartupProperties Properties for itself.
       
    99 		Number of retries and timeout in @c aStartupProperties should only be used with 
       
   100 		start-method @c EWaitForStart. A monitored component with start-method @c EFireAndForget 
       
   101 		will not be restarted on failure, the recovery method will be executed immediately. 
       
   102 		Start-method @c EDeferredWaitForStart can not be used for monitoring.		
       
   103 @capability ECapabilityProtServ for ERestartOS and ERestartOSWithMode
       
   104 @capability ECapabilityPowerMgmt for ERestartOSWithMode
       
   105 @publishedAll
       
   106 @deprecated
       
   107 @leave KErrArgument if @c aStartupProperties contains invalid argument or if @c aProcess contain NULL handle
       
   108 @leave KErrPermissionDenied if client does not have the requied PlatSec capability
       
   109 @leave KErrAlreadyExists if the process is already registered for monitoring
       
   110 */
       
   111 EXPORT_C void RSysMonSession::MonitorSelfL(const CStartupProperties& aStartupProperties)
       
   112 	{
       
   113 	DEBUGPRINT1(_L("SysMonCli: Monitor self"));
       
   114 	DoMonitorL(aStartupProperties, NULL, EFalse);
       
   115 	}
       
   116 
       
   117 
       
   118 void RSysMonSession::DoMonitorL(const CStartupProperties& aStartupProperties, const RProcess* aProcess, TBool aExecuteRecoveryMethodOnFailure)
       
   119 	{
       
   120 	User::LeaveIfError(Validate(aStartupProperties));
       
   121 	
       
   122 	CBufFlat* const propsBuf = CBufFlat::NewL(aStartupProperties.Size());
       
   123     CleanupStack::PushL(propsBuf);
       
   124       
       
   125     aStartupProperties.ExternalizeL(*propsBuf);
       
   126     TPtr8 propsPtr(propsBuf->Ptr(0));
       
   127 
       
   128 #ifdef _DEBUG
       
   129 	TPtrC fileName = aStartupProperties.FileName();
       
   130 	DEBUGPRINT2(_L("SysMonCli: Initiating monitor for %S"), &fileName);
       
   131 #endif
       
   132 
       
   133 	if (aProcess)
       
   134 		{
       
   135 		if (aProcess->Handle() == KNullHandle)
       
   136 			{
       
   137 			User::Leave(KErrArgument);
       
   138 			}
       
   139 		
       
   140 		TPckg<TProcessId> pidPckg(aProcess->Id());
       
   141 		TIpcArgs ipcArgs(&propsPtr, &pidPckg, aExecuteRecoveryMethodOnFailure);
       
   142 		User::LeaveIfError(SendReceive(EMonitor, ipcArgs));
       
   143 		}
       
   144 	else
       
   145 		{
       
   146 		TIpcArgs ipcArgs(&propsPtr);
       
   147 		User::LeaveIfError(SendReceive(EMonitorSelf, ipcArgs));
       
   148 		}
       
   149 
       
   150 	CleanupStack::PopAndDestroy(propsBuf);
       
   151 	}
       
   152 
       
   153 /**
       
   154 Used to cancel self-monitoring of a component.
       
   155 
       
   156 This function is to be called by the component being monitored either by calling 
       
   157 MonitorL() or MonitorSelfL() function. By calling this function before exiting, 
       
   158 System Monitor will cancel the monitor request of the component so that System Monitor 
       
   159 will not restart it after it exit.
       
   160 @publishedAll
       
   161 @released
       
   162 */
       
   163 EXPORT_C void RSysMonSession::CancelMonitorSelfL()
       
   164 	{
       
   165 	DEBUGPRINT1(_L("SysMonCli: Cancelling monitor"));
       
   166 	User::LeaveIfError(SendReceive(ECancelSelf));
       
   167 	}
       
   168 
       
   169 /** Validate CStartupProperties parameter
       
   170 */
       
   171 TInt RSysMonSession::Validate(const CStartupProperties& aProp)
       
   172 	{
       
   173 	if(aProp.FileName().Length() > 0)
       
   174 		{
       
   175 		if(aProp.Monitored())
       
   176 			{
       
   177 			if(aProp.StartupType() == EStartProcess || aProp.StartupType() == EStartApp)
       
   178 				{
       
   179 				if(((aProp.StartMethod() == EFireAndForget) && (aProp.NoOfRetries() == 0)) ||
       
   180 					(aProp.StartMethod() == EWaitForStart))
       
   181 					{
       
   182 					if(aProp.NoOfRetries() >= 0)
       
   183 						{
       
   184 						if(aProp.Timeout() >= 0)
       
   185 							{
       
   186 							if(aProp.RecoveryMethod() >= EIgnoreOnFailure && aProp.RecoveryMethod() <= ERestartOSWithMode)
       
   187 								{
       
   188 								return KErrNone;
       
   189 								}
       
   190 							}
       
   191 						}
       
   192 					}
       
   193 				}
       
   194 			}
       
   195 		}
       
   196 	return KErrArgument;
       
   197 	}