watchdog.cpp

Go to the documentation of this file.
00001 // generated code -- do not edit
00002 
00003 #include "watchdog.h"
00004 
00005 static TInt const KNormalWait = 30;
00006 
00007 CWatchdog *CWatchdog::NewLC(CActiveSchedulerWait &aLoop)
00008 {
00009   CWatchdog *object = new (ELeave) CWatchdog(aLoop);
00010   CleanupStack::PushL(object);
00011   object->ConstructL();
00012   return object;
00013 }
00014 
00015 CWatchdog *CWatchdog::NewL(CActiveSchedulerWait &aLoop)
00016 {
00017   CWatchdog *object = NewLC(aLoop);
00018   CleanupStack::Pop();
00019   return object;
00020 }
00021 
00022 CWatchdog::CWatchdog(CActiveSchedulerWait &aLoop) : iLoop(aLoop)
00023 {
00024   UpdateAppStartTime();
00025   iRandSeed = iAppStartTime.Int64();
00026 }
00027 
00028 void CWatchdog::ConstructL()
00029 {
00030   User::LeaveIfError(iFs.Connect()); iFsOpen = ETrue;
00031   User::LeaveIfError(iWsSession.Connect()); iWsSessionOpen = ETrue;
00032   iTimerAo = CTimerAo::NewL(*this, CActive::EPriorityStandard);
00033   iLogonAo = CProcessHandleObserver::NewL(*this, CActive::EPriorityStandard, iProcess);
00034 }
00035 
00036 CWatchdog::~CWatchdog()
00037 {
00038   (delete iLogonAo);
00039   (delete iTimerAo);
00040   CloseProcess();
00041   if (iWsSessionOpen) {
00042     iWsSession.Close();
00043   }
00044   if (iFsOpen) {
00045     iFs.Close();
00046   }
00047 }
00048 
00049 void CWatchdog::Start()
00050 {
00051   WaitWhile(KNormalWait);
00052 }
00053 
00054 void CWatchdog::UpdateAppStartTime()
00055 {
00056   iAppStartTime.UniversalTime();
00057 }
00058 
00059 void CWatchdog::WaitWhile(TInt aSecs)
00060 {
00061   iTimerAo->After(SecsToUsecs(aSecs));
00062 }
00063 
00064 void CWatchdog::HandleMinorError()
00065 {
00066   iNumMinorErrors++;
00067   TInt randFactor = (Math::Rand(iRandSeed) % 10);
00068   TInt waitTime = iNumMinorErrors * KNormalWait + randFactor;
00069   WaitWhile(waitTime);
00070 }
00071 
00072 void CWatchdog::ObserveProcess()
00073 {
00074   assert((iProcessOpen) && "logic error");
00075   iLogonAo->MakeRequest();
00076 }
00077 
00078 void CWatchdog::StartApp()
00079 {
00080   assert((!iProcessOpen) && "logic error");
00081   _LIT(literalDes1128, "\\sys\\bin\\cl2app.exe");
00082   TInt errCode = iProcess.Create(literalDes1128, KNullDesC);
00083   logg("RProcess.Create %d", errCode);
00084   switch (errCode) {
00085   case KErrNone:
00086     {
00087       iProcessOpen = ETrue;
00088       UpdateAppStartTime();
00089       ObserveProcess();
00090       iProcess.Resume();
00091       break;
00092     }
00093   default:
00094     {
00095       HandleMinorError();
00096     }
00097   }
00098 }
00099 
00100 void CWatchdog::FindApp()
00101 {
00102   assert((!iProcessOpen) && "logic error");
00103   _LIT(KProcessName, "cl2app");
00104   TInt errCode = iProcess.Open(KProcessName, EOwnerThread);
00105   logg("RProcess.Open(KProcessName) %d", errCode);
00106   switch (errCode) {
00107   case KErrNone:
00108     {
00109       iProcessOpen = ETrue;
00110       ObserveProcess();
00111       break;
00112     }
00113   case KErrNotFound:
00114     {
00115       FindAppByPattern();
00116       break;
00117     }
00118   default:
00119     {
00120       HandleMinorError();
00121     }
00122   }
00123 }
00124 
00125 void CWatchdog::FindAppByPattern()
00126 {
00127   _LIT(KProcessSpec, "*[e8460002]*");
00128   TFindProcess processFinder(KProcessSpec);
00129   TFullName processName;
00130   TInt errCode = processFinder.Next(processName);
00131   logg("TFindProcess.Next(KProcessSpec) %d", errCode);
00132   switch (errCode) {
00133   case KErrNone:
00134     {
00135       
00136 #if __DO_LOGGING__
00137     TBuf8<KMaxFullName+1> buf8;
00138     buf8.Copy(processName); // convert to ASCII
00139     logg("found '%s'", (char*)buf8.PtrZ());
00140 #endif
00141 
00142       errCode = iProcess.Open(processFinder, EOwnerThread);
00143       logg("RProcess.Open(TFindProcess) %d", errCode);
00144       switch (errCode) {
00145       case KErrNone:
00146         {
00147           iProcessOpen = ETrue;
00148           ObserveProcess();
00149           break;
00150         }
00151       case KErrNotFound:
00152         {
00153           StartApp();
00154           break;
00155         }
00156       default:
00157         {
00158           HandleMinorError();
00159         }
00160       }
00161       break;
00162     }
00163   case KErrNotFound:
00164     {
00165       StartApp();
00166       break;
00167     }
00168   default:
00169     {
00170       HandleMinorError();
00171     }
00172   }
00173 }
00174 
00175 void CWatchdog::CloseProcess()
00176 {
00177   if (iProcessOpen) {
00178     iProcess.Close();
00179     (iProcessOpen = EFalse);
00180   }
00181 }
00182 
00183 void CWatchdog::HandleTimerEvent(TInt errCode)
00184 {
00185   if (errCode) {
00186     ExitWatchdog(errCode, "timer error");
00187   } else {
00188     FindApp();
00189   }
00190 }
00191 
00192 void CWatchdog::HandleProcessHandleEvent(TInt errCode)
00193 {
00194   logg("process observation result %d", errCode);
00195   CloseProcess();
00196   switch (errCode) {
00197   case KErrNoMemory:
00198     {
00199       HandleMinorError();
00200       break;
00201     }
00202   case KErrCancel:
00203     {
00204       break;
00205     }
00206   default:
00207     {
00208       if (DiedQuickly()) {
00209         HandleMinorError();
00210       } else {
00211         WaitWhile(KNormalWait);
00212       }
00213     }
00214   }
00215 }
00216 
00217 void CWatchdog::ExitWatchdog(TInt errCode, char const *errText)
00218 {
00219   Cancel();
00220   logg("exiting watchdog due to error: %s (%d)", errText, errCode);
00221   iLoop.AsyncStop();
00222 }
00223 
00224 void CWatchdog::Cancel()
00225 {
00226   iTimerAo->Cancel();
00227   if (iLogonAo) {
00228     iLogonAo->Cancel();
00229   }
00230 }
00231 
00232 TBool CWatchdog::DiedQuickly()
00233 {
00234   TTime now;
00235   now.UniversalTime();
00236   TTimeIntervalSeconds interval;
00237   TInt errCode;
00238   (errCode = now.SecondsFrom(iAppStartTime, interval));
00239   return (!errCode && (interval <= TTimeIntervalSeconds(10)));
00240 }
00241 
00242 /** 
00243     Copyright 2008 Helsinki Institute for Information Technology (HIIT)
00244     and Tero Hasu <tero.hasu@hut.fi>. All rights reserved.
00245 
00246     This license applies:
00247 
00248     This program is free software; you can redistribute it and/or modify
00249     it under the terms of the GNU General Public License as published by
00250     the Free Software Foundation; either version 2 of the License, or
00251     (at your option) any later version.
00252 
00253     This program is distributed in the hope that it will be useful,
00254     but WITHOUT ANY WARRANTY; without even the implied warranty of
00255     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00256     GNU General Public License for more details.
00257 
00258     You should have received a copy of the GNU General Public License
00259     along with this program; if not, write to the Free Software
00260     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00261 
00262     Alternatively, this license applies:
00263 
00264     Permission is hereby granted, free of charge, to any person
00265     obtaining a copy of this software and associated documentation files
00266     (the "Software"), to deal in the Software without restriction,
00267     including without limitation the rights to use, copy, modify, merge,
00268     publish, distribute, sublicense, and/or sell copies of the Software,
00269     and to permit persons to whom the Software is furnished to do so,
00270     subject to the following conditions:
00271 
00272     The above copyright notice and this permission notice shall be
00273     included in all copies or substantial portions of the Software.
00274 
00275     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00276     EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00277     MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00278     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
00279     BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
00280     ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00281     CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00282     SOFTWARE.
00283  */

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:57 2011 by Doxygen 1.6.1