|
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 "monitor.h" |
|
17 #include "startsafe.h" |
|
18 #include "restartsys.h" |
|
19 #include "sysmonserver.h" |
|
20 #include "sysmoncliserv.h" |
|
21 #include "timerlist.h" |
|
22 #include "sysmon_patch.h" |
|
23 #include <startupproperties.h> |
|
24 |
|
25 #include "SysStartDebug.h" |
|
26 #include "sysstartpanic.h" |
|
27 |
|
28 const TInt CMonitor::iOffset = _FOFF(CMonitor, iSglQueLink); |
|
29 |
|
30 TProcessId CMonitor::ProcessId() const |
|
31 { |
|
32 return iProcessId; |
|
33 } |
|
34 |
|
35 |
|
36 CMonitor* CMonitor::NewL(CSysMonServer& aServer, const TProcessId& aId, CStartupProperties* aStartupProperties, TBool aExecuteRecoveryMethodOnFailure) |
|
37 { |
|
38 ASSERT(aStartupProperties); |
|
39 CMonitor *monitor = new(ELeave) CMonitor(aServer, aId); |
|
40 CleanupStack::PushL(monitor); |
|
41 monitor->ConstructL(aStartupProperties, aExecuteRecoveryMethodOnFailure); |
|
42 CleanupStack::Pop(monitor); |
|
43 return monitor; |
|
44 } |
|
45 |
|
46 |
|
47 CMonitor::CMonitor(CSysMonServer& aServer, const TProcessId& aId) |
|
48 :CActive(EPriorityHigh), |
|
49 iSysMonServer(aServer), |
|
50 iProcessId(aId), |
|
51 iProcess(aId), |
|
52 iLoadTime() |
|
53 { |
|
54 CActiveScheduler::Add(this); |
|
55 } |
|
56 |
|
57 |
|
58 void CMonitor::ConstructL(CStartupProperties* aStartupProperties, TBool aExecuteRecoveryMethodOnFailure) |
|
59 { |
|
60 iLogonBackoffTimer = CLogonBackoffTimer::NewL( *this ); |
|
61 |
|
62 User::LeaveIfError(iProcess.Open(iProcessId)); |
|
63 |
|
64 // Don't support monitoring of system critical components as they will restart the device on failure anyway |
|
65 User::TCritical critical = User::ProcessCritical(iProcess); |
|
66 if (critical == User::ESystemCritical || critical == User::ESystemPermanent) |
|
67 { |
|
68 User::Leave(KErrNotSupported); |
|
69 } |
|
70 |
|
71 //Make sure the process is still running |
|
72 if((EExitPending == iProcess.ExitType())) |
|
73 { |
|
74 //Ensure the the filename in aStartupProperties is the same as in iProcess |
|
75 //(not possible to read filename from a dead process). |
|
76 TParse nameInProc, nameInProp; |
|
77 nameInProc.SetNoWild(iProcess.FileName(),NULL,NULL); |
|
78 nameInProp.SetNoWild(aStartupProperties->FileName(),NULL,NULL); |
|
79 |
|
80 if( nameInProc.Name().CompareF(nameInProp.Name()) ) |
|
81 { |
|
82 User::Leave(KErrArgument); |
|
83 } |
|
84 } |
|
85 else |
|
86 { |
|
87 //The process is already dead, either leave now or let this monitor recover the process |
|
88 if(!aExecuteRecoveryMethodOnFailure) |
|
89 User::Leave(KErrDied); |
|
90 } |
|
91 |
|
92 // Can't leave after taking ownership of aStartupProperties |
|
93 iStartupProperties = aStartupProperties; |
|
94 iLoadTime.UniversalTime(); |
|
95 } |
|
96 |
|
97 |
|
98 CMonitor::~CMonitor() |
|
99 { |
|
100 delete iLogonBackoffTimer; |
|
101 |
|
102 Cancel(); |
|
103 delete iStartupProperties; |
|
104 iProcess.Close(); |
|
105 } |
|
106 |
|
107 |
|
108 void CMonitor::Start() |
|
109 { |
|
110 iLogonBackoffTimer->ProcessLogon(); |
|
111 } |
|
112 |
|
113 |
|
114 |
|
115 void CMonitor::DoCancel() |
|
116 { |
|
117 #ifdef _DEBUG |
|
118 TPtrC fileName = iStartupProperties->FileName(); |
|
119 DEBUGPRINT2(_L("SysMonMonitor: Monitor Cancelled for %S"), &(fileName)); |
|
120 #endif |
|
121 |
|
122 iProcess.LogonCancel(iStatus); // cancels monitoring |
|
123 } |
|
124 |
|
125 |
|
126 void CMonitor::RestartProcessL() |
|
127 { |
|
128 #ifdef _DEBUG |
|
129 TPtrC fileName = iStartupProperties->FileName(); |
|
130 #endif |
|
131 DEBUGPRINT3(_L("SysMonMonitor: Going to restart %S, old process id=%d"), &fileName, iProcessId.Id()); |
|
132 |
|
133 CStartSafe* startSafe = CStartSafe::NewL(); |
|
134 CleanupStack::PushL(startSafe); |
|
135 |
|
136 __ASSERT_DEBUG(iStartupProperties->StartMethod() == EWaitForStart, PanicNow(KPanicMonitor, EInvalidStartMethod)); |
|
137 iStartupProperties->SetStartMethod(EWaitForStart); |
|
138 |
|
139 TInt propRetries = iStartupProperties->NoOfRetries(); |
|
140 TBool restoreRetries = EFalse; |
|
141 |
|
142 if(propRetries > 0) |
|
143 { |
|
144 // In the restart scenario we want StartSafe to make 'NoOfRetries' attempts |
|
145 // rather than '1 + NoOfRetries' which it will otherwise do. |
|
146 restoreRetries = ETrue; |
|
147 iStartupProperties->SetNoOfRetries(--propRetries); |
|
148 } |
|
149 |
|
150 TInt retried = 0; |
|
151 |
|
152 // Attempt restart/s. Do not allow a leave until NoOfRetries has been restored. |
|
153 TRAPD( err, startSafe->StartL(*iStartupProperties, iProcess, retried) ); |
|
154 |
|
155 if(restoreRetries) |
|
156 { |
|
157 iStartupProperties->SetNoOfRetries(++propRetries); |
|
158 } |
|
159 |
|
160 User::LeaveIfError(err); |
|
161 CleanupStack::PopAndDestroy(startSafe); |
|
162 DEBUGPRINT3(_L("SysMonMonitor: %S restarted, new iProcessId=%d. Logon to monitor again"), &fileName, iProcess.Id().Id()); |
|
163 |
|
164 iProcessId = iProcess.Id(); |
|
165 iLogonBackoffTimer->ProcessLogon(); |
|
166 } |
|
167 |
|
168 |
|
169 /** |
|
170 This function is inherited from MLogonCallback and is called from CLogonBackoffTimer |
|
171 */ |
|
172 TInt CMonitor::DoProcessLogon() |
|
173 { |
|
174 |
|
175 iProcess.Logon( iStatus ); |
|
176 return ( (iStatus == KRequestPending) ? KErrNone : iStatus.Int() ); |
|
177 } |
|
178 |
|
179 |
|
180 |
|
181 /** |
|
182 This function is inherited from MLogonCallback and is called from CLogonBackoffTimer |
|
183 */ |
|
184 void CMonitor::ActivateSelf() |
|
185 { |
|
186 |
|
187 iLoadTime.UniversalTime(); |
|
188 SetActive(); |
|
189 } |
|
190 |
|
191 |
|
192 |
|
193 TInt CMonitor::Callback(TAny* aParent) |
|
194 { |
|
195 CMonitor* monitor = reinterpret_cast<CMonitor*> (aParent); |
|
196 DEBUGPRINT2(_L("SysMonMonitor: Finished waiting for throttle time, try to restart failed processId=%d"), monitor->iProcessId.Id()); |
|
197 |
|
198 TRAPD(err, monitor->RestartProcessL()); |
|
199 |
|
200 if (err != KErrNone) |
|
201 { |
|
202 // process failed to be started, cancel monitoring of this process |
|
203 DEBUGPRINT2(_L("SysMonMonitor::RestartProcessL failed with err=%d, cancelling"), err); |
|
204 monitor->CancelMonitor(); |
|
205 } |
|
206 |
|
207 return KErrNone; |
|
208 } |
|
209 |
|
210 |
|
211 /* |
|
212 CMonitor::RunL() gets called when a monitor process terminates. |
|
213 */ |
|
214 void CMonitor::RunL() |
|
215 { |
|
216 DEBUGPRINT1(_L("SysMonMonitor: CMonitor::RunL() called")); |
|
217 |
|
218 iProcess.Close(); // closing the current handle |
|
219 |
|
220 if (iStartupProperties->NoOfRetries() == 0 || |
|
221 iStartupProperties->StartMethod() == EFireAndForget) |
|
222 { |
|
223 // 1. If NoOfRetries() == 0, execute recovery method immediately. |
|
224 // 2. If StartMethod() == EFireAndForget, the retry value is ignored during monitoring because |
|
225 // restarting the process can result in a forever loop that make SysMon keep restarting the |
|
226 // process. This is because in EFireAndForget, we don't need to check the successful start |
|
227 // of the process, so as soon as the process fail again, this monitoring function would get |
|
228 // called again. So for EFireAndForget we execute the recovery action if a process failed. |
|
229 TRecoveryMethod recoveryMethod = iStartupProperties->RecoveryMethod(); |
|
230 TInt err = KErrNone; |
|
231 DEBUGPRINT2(_L("SysMonMonitor: Process failed RecoveryMethod=%d"), recoveryMethod); |
|
232 if (recoveryMethod == ERestartOS) |
|
233 { |
|
234 err = RestartSys::RestartSystem() ; // restart the system |
|
235 |
|
236 if (KErrNone != err) |
|
237 { |
|
238 DEBUGPRINT2(_L("Sysstart: RestartSystem error %d"), err); |
|
239 PanicNow(KPanicMonitor, ERestartSystemCallFailed); |
|
240 } |
|
241 |
|
242 User::After(5000000); // required by RestartSys API, see comments in RestartSys::RestartSystem() |
|
243 } |
|
244 else if (recoveryMethod == ERestartOSWithMode) |
|
245 { |
|
246 err = RestartSys::RestartSystem(iStartupProperties->RestartMode()) ; // restart system in a mode |
|
247 |
|
248 if (KErrNone != err) |
|
249 { |
|
250 DEBUGPRINT2(_L("Sysstart: RestartSystem with mode error %d"), err); |
|
251 PanicNow(KPanicMonitor, ERestartSystemCallWithMode); |
|
252 } |
|
253 |
|
254 User::After(5000000); // required by RestartSys API, see comments in RestartSys::RestartSystem() |
|
255 } |
|
256 else |
|
257 { |
|
258 // ignore on failure, cancel monitoring of this process |
|
259 CancelMonitor(); |
|
260 } |
|
261 } |
|
262 else |
|
263 { |
|
264 TTime curTime; |
|
265 curTime.UniversalTime(); // current time, can be considered as the time of termination for the process |
|
266 |
|
267 TTime thresholdTime = (iLoadTime + TTimeIntervalMicroSeconds32(KWaitTime)); // time, till when no restart should take place |
|
268 |
|
269 if (curTime < thresholdTime) |
|
270 { |
|
271 // Implies process terminated less than KWaitTime since the last launch of the process |
|
272 // So to reduce Denial of Service we wait the remaining time of KWaitTime. |
|
273 #ifdef _DEBUG |
|
274 TPtrC fileName = iStartupProperties->FileName(); |
|
275 DEBUGPRINT2(_L("SysMonMonitor: Wait for throttle time before restarting process %S"), &(fileName)); |
|
276 #endif |
|
277 iSysMonServer.TimerListL().AddL(thresholdTime, TCallBack(Callback, this)); |
|
278 } |
|
279 else |
|
280 { |
|
281 RestartProcessL(); |
|
282 } |
|
283 } |
|
284 } |
|
285 |
|
286 |
|
287 #ifdef _DEBUG |
|
288 TInt CMonitor::RunError(TInt aError) |
|
289 #else |
|
290 TInt CMonitor::RunError(TInt /*aError*/) |
|
291 #endif |
|
292 { |
|
293 DEBUGPRINT2(_L("SysMonMonitor: RunError called with error=%d, cancelling"), aError); |
|
294 // process failed to be started, cancel monitoring of this process |
|
295 CancelMonitor(); |
|
296 |
|
297 return KErrNone; |
|
298 } |
|
299 |
|
300 |
|
301 void CMonitor::CancelMonitor() |
|
302 { |
|
303 DEBUGPRINT2(_L("SysMonMonitor: CMonitor cancelling monitor with iProcessId=%d"), iProcessId.Id()); |
|
304 iSysMonServer.CancelMonitor(iProcessId); |
|
305 } |