genericservices/taskscheduler/Test/TSUtils/tprocesskiller.cpp
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 2004-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 <f32file.h>
       
    17 #include <e32debug.h>
       
    18 	
       
    19 //This function is used in the test code to kill SCHSVR or MinimalTaskHandler
       
    20 //processes (or some other) when they leftover and may cause OOM condinitons.
       
    21 static TInt KillProcess(const TDesC& aProcessName)
       
    22 	{
       
    23 	TFullName name;
       
    24 	TInt err = KErrNotFound;
       
    25 
       
    26 	RDebug::Print(_L("Find and kill \"%S\" process.\n"), &aProcessName);
       
    27 
       
    28 	TBuf<64> pattern(aProcessName);
       
    29 	TInt length = pattern.Length();
       
    30 	pattern += _L("*");
       
    31 	TFindProcess procFinder(pattern);
       
    32 
       
    33 	while(procFinder.Next(name) == KErrNone)
       
    34 		{
       
    35 		if(name.Length() > length) 
       
    36 			{//If found name is a string containing aProcessName string.
       
    37 			TChar c(name[length]);
       
    38 			if(c.IsAlphaDigit() || c == TChar('_') || c == TChar('-'))
       
    39 				{//If the found name is other valid application name starting with aProcessName string.
       
    40 				RDebug::Print(_L(":: Process name: \"%S\".\n"), &name);
       
    41 				continue;
       
    42 				}
       
    43 			}
       
    44 		RProcess proc;
       
    45 		
       
    46 		err = proc.Open(name);
       
    47 		if(err == KErrNone)
       
    48 			{
       
    49 			
       
    50 			//If the process is alive, terminate it
       
    51 			if(proc.ExitType() == EExitPending)
       
    52 			{
       
    53 			proc.Kill(0);
       
    54 			RDebug::Print(_L("\"%S\" process killed.\n"), &name);	
       
    55 			}
       
    56 			
       
    57 			//If the process has already died record the error
       
    58 			else err = KErrDied;
       
    59 
       
    60 			}
       
    61 		proc.Close();
       
    62 		}
       
    63 	return err;
       
    64 	}
       
    65 
       
    66 GLDEF_C TInt E32Main()
       
    67 	{
       
    68 	TFileName name;
       
    69 	User::CommandLine(name);
       
    70 	return KillProcess(name);
       
    71 	}
       
    72