os/ossrv/lowlevellibsandfws/apputils/tsrc/t_processkiller.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/lowlevellibsandfws/apputils/tsrc/t_processkiller.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,85 @@
     1.4 +// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include <f32file.h>
    1.20 +#include <e32debug.h>
    1.21 +
    1.22 +//This function is used in the test code to kill SCHSVR or MinimalTaskHandler
    1.23 +//processes (or some other) when they leftover and may cause OOM condinitons.
    1.24 +static void KillProcessL(const TDesC& aProcessName)
    1.25 +	{
    1.26 +	TFullName name;
    1.27 +
    1.28 +	TInt err = KErrNotFound;
    1.29 +	
    1.30 +	TBuf<64> pattern(aProcessName);
    1.31 +	TInt length = pattern.Length();
    1.32 +	pattern += _L("*");
    1.33 +	TFindProcess procFinder(pattern);
    1.34 +
    1.35 +	while(procFinder.Next(name) == KErrNone)
    1.36 +		{
    1.37 +		if(name.Length() > length) 
    1.38 +			{//If found name is a string containing aProcessName string.
    1.39 +			TChar c(name[length]);
    1.40 +			if(c.IsAlphaDigit() || c == TChar('_') || c == TChar('-'))
    1.41 +				{//If the found name is other valid application name starting with aProcessName string.
    1.42 +				RDebug::Print(_L(":: Process name: \"%S\".\n"), &name);
    1.43 +				continue;
    1.44 +				}
    1.45 +			}
    1.46 +		RProcess proc;
    1.47 +		
    1.48 +		err = proc.Open(name);
    1.49 +		if(err == KErrNone)
    1.50 +			{
    1.51 +			
    1.52 +			//If the process is alive, terminate it
    1.53 +			if(proc.ExitType() == EExitPending)
    1.54 +			{
    1.55 +			proc.Kill(0);
    1.56 +			RDebug::Print(_L("\"%S\" process killed.\n"), &name);	
    1.57 +			}
    1.58 +			
    1.59 +			//If the process has already died record the error
    1.60 +			else err = KErrDied;
    1.61 +
    1.62 +			}
    1.63 +		proc.Close();
    1.64 +		}
    1.65 +	User::Leave(err);
    1.66 +	}
    1.67 +
    1.68 +void DoTestsL()
    1.69 +	{
    1.70 +	TFileName name;
    1.71 +	User::CommandLine(name);
    1.72 +	KillProcessL(name);
    1.73 +	}
    1.74 +
    1.75 +GLDEF_C TInt E32Main()
    1.76 +	{
    1.77 +	CTrapCleanup* cleanup=CTrapCleanup::New();
    1.78 +	if(cleanup == NULL)
    1.79 +	{
    1.80 +		return KErrNoMemory;
    1.81 +	}
    1.82 +	
    1.83 +	TRAPD(err,DoTestsL());
    1.84 +
    1.85 +	delete cleanup;
    1.86 +	return err;
    1.87 +	}
    1.88 +