os/ossrv/ssl/libssl/src/wsd_solution.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ssl/libssl/src/wsd_solution.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,210 @@
     1.4 +/*
     1.5 +Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
     1.6 +
     1.7 +Redistribution and use in source and binary forms, with or without 
     1.8 +modification, are permitted provided that the following conditions are met:
     1.9 +
    1.10 +* Redistributions of source code must retain the above copyright notice, this 
    1.11 +  list of conditions and the following disclaimer.
    1.12 +* Redistributions in binary form must reproduce the above copyright notice, 
    1.13 +  this list of conditions and the following disclaimer in the documentation 
    1.14 +  and/or other materials provided with the distribution.
    1.15 +* Neither the name of Nokia Corporation nor the names of its contributors 
    1.16 +  may be used to endorse or promote products derived from this software 
    1.17 +  without specific prior written permission.
    1.18 +
    1.19 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
    1.20 +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
    1.21 +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
    1.22 +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 
    1.23 +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
    1.24 +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
    1.25 +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
    1.26 +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
    1.27 +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
    1.28 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.29 +
    1.30 +Description:  Contains the WSD solution
    1.31 +*/
    1.32 +
    1.33 +
    1.34 +// INCLUDE FILES
    1.35 +#include "libssl_wsd_defs.h"
    1.36 +
    1.37 +#if EMULATOR
    1.38 +
    1.39 +#include <e32std.h>
    1.40 +#include <pthread.h>
    1.41 +#include <windows.h>
    1.42 +#include "wsd_solution.h"
    1.43 +
    1.44 +//definition of all WSD related funcitons 
    1.45 +
    1.46 +#define MAX_NUMBER_OF_PROCESSES	100
    1.47 +
    1.48 +struct TWsdArray
    1.49 +{
    1.50 +	TUint 		iPid;
    1.51 +	TFullName 	iFullName;
    1.52 +	void* 		iPtr;
    1.53 +};
    1.54 +
    1.55 +#pragma data_seg(".wsd")
    1.56 +TInt NumOfProcesses = 0;
    1.57 +#pragma data_seg()
    1.58 +
    1.59 +#pragma bss_seg(".wsd")
    1.60 +struct TWsdArray wsdArray[MAX_NUMBER_OF_PROCESSES];
    1.61 +#pragma bss_seg()
    1.62 +
    1.63 +// This function frees up the used slots within the wsdArray
    1.64 +// for use by other processes; internal to the implementation
    1.65 +// that provides WSD support
    1.66 +LOCAL_C TInt FindSlot();
    1.67 +
    1.68 +void *Pls()
    1.69 +{
    1.70 +	HANDLE mutexHandle = NULL;
    1.71 +	unsigned short p[] = 
    1.72 +	{
    1.73 +		'l', 'i', 'b', 's', 's', 'l', '_', 'm','u','t','e','x'
    1.74 +	};
    1.75 +	
    1.76 +	if( !(mutexHandle = CreateMutex(NULL, TRUE, p)) )
    1.77 +	{
    1.78 +		// Mutex already created. Try to acquire it
    1.79 +		mutexHandle = OpenMutex(SYNCHRONIZE, FALSE, p);
    1.80 +		WaitForSingleObject(mutexHandle, INFINITE);
    1.81 +	}
    1.82 +
    1.83 +	TProcessId procId = RProcess().Id();
    1.84 +	TFullName fullName = RProcess().FullName();
    1.85 +	TInt pid = *(TUint *)&procId;
    1.86 +	TInt slot = -1;
    1.87 +
    1.88 +	for(int i=0 ; i < MAX_NUMBER_OF_PROCESSES ; ++i )
    1.89 +	{
    1.90 +		if(!wsdArray[i].iPid)
    1.91 +		{
    1.92 +			if(slot == -1)
    1.93 +				slot = i;
    1.94 +			
    1.95 +			continue;
    1.96 +		}
    1.97 +		else if(wsdArray[i].iPid == pid)
    1.98 +		{
    1.99 +			ReleaseMutex(mutexHandle);
   1.100 +			return wsdArray[i].iPtr;
   1.101 +		}
   1.102 +	}
   1.103 +	
   1.104 +	// NEW PROCESS
   1.105 +	
   1.106 +	if(MAX_NUMBER_OF_PROCESSES == NumOfProcesses)
   1.107 +	{
   1.108 +		// Find out if one of the slots reserved for previous processes
   1.109 +		// could be reused.
   1.110 +		TInt returnValue = -1;
   1.111 +		returnValue = FindSlot();
   1.112 +		if(returnValue != -1)
   1.113 +		{
   1.114 +			slot = returnValue;
   1.115 +		}
   1.116 +		else
   1.117 +		{
   1.118 +			User::Panic(_L("Pls() Reached the naximum number for the processes"),KErrNoMemory);
   1.119 +		}
   1.120 +	}
   1.121 +
   1.122 +	wsdArray[slot].iPid = pid;
   1.123 +	wsdArray[slot].iFullName = fullName;
   1.124 +	wsdArray[slot].iPtr = NULL;
   1.125 +	
   1.126 +	// Increment the count for the number of processes
   1.127 +	++NumOfProcesses;
   1.128 +	
   1.129 +	// Release the mutex
   1.130 +	ReleaseMutex(mutexHandle);
   1.131 +	return wsdArray[slot].iPtr;
   1.132 +}
   1.133 +
   1.134 +LOCAL_C TInt FindSlot()
   1.135 +{
   1.136 +	TInt slot = -1;
   1.137 +	TFullName fullName;
   1.138 +	TInt currentCount = 0;
   1.139 +	
   1.140 +	for(int i = 0 ; i < MAX_NUMBER_OF_PROCESSES ; ++i)
   1.141 +	{
   1.142 +		TFindProcess search(wsdArray[i].iFullName);
   1.143 +		if(search.Next(fullName) == KErrNone)
   1.144 +		{
   1.145 +			++currentCount;
   1.146 +			continue;
   1.147 +		}
   1.148 +		
   1.149 +		// Process with the given name does not exist in
   1.150 +		// the system. So the slot could be reused.
   1.151 +		wsdArray[i].iPid = 0;
   1.152 +
   1.153 +		// Free the VAS associated with this "process" (terminated)
   1.154 +		VirtualFree(wsdArray[i].iPtr, 0, MEM_RELEASE);
   1.155 +		wsdArray[i].iPtr = NULL;
   1.156 +
   1.157 +		if(slot == -1)
   1.158 +		{
   1.159 +			// Update
   1.160 +			slot = i;
   1.161 +		}
   1.162 +	}
   1.163 +	NumOfProcesses = currentCount;
   1.164 +	return slot;
   1.165 +}
   1.166 +
   1.167 +TInt SetPls(void *aArg)
   1.168 +{
   1.169 +	HANDLE mutexHandle = NULL;
   1.170 +	unsigned short p[] = 
   1.171 +	{
   1.172 +		'l', 'i', 'b', 's', 's', 'l', '_', 'm','u','t','e','x'
   1.173 +	};
   1.174 +	
   1.175 +	if( !(mutexHandle = CreateMutex(NULL, TRUE, p)) )
   1.176 +	{
   1.177 +		// Mutex already created. Try to acquire it
   1.178 +		mutexHandle = OpenMutex(SYNCHRONIZE, FALSE, p);
   1.179 +		WaitForSingleObject(mutexHandle, INFINITE);
   1.180 +	}
   1.181 +
   1.182 +	TProcessId procId = RProcess().Id();
   1.183 +	TFullName fullName = RProcess().FullName();
   1.184 +	TInt pid = *(TUint *)&procId;
   1.185 +
   1.186 +	for(int i=0 ; i < MAX_NUMBER_OF_PROCESSES ; ++i )
   1.187 +	{
   1.188 +		if(wsdArray[i].iPid == pid)
   1.189 +		{
   1.190 +			wsdArray[i].iPtr = aArg;
   1.191 +			ReleaseMutex(mutexHandle);
   1.192 +			return KErrNone;
   1.193 +		}
   1.194 +	}
   1.195 +	
   1.196 +	ReleaseMutex(mutexHandle);
   1.197 +	return KErrNotFound;
   1.198 +}
   1.199 +
   1.200 +void* AllocatePls(TInt aSize)
   1.201 +{
   1.202 +	void *r = VirtualAlloc(NULL, 
   1.203 +						   aSize,
   1.204 +						   MEM_COMMIT | MEM_RESERVE,
   1.205 +						   PAGE_READWRITE);
   1.206 +	if(!r)
   1.207 +	{
   1.208 +		User::Panic(_L("AllocatePls() VIRTUALALLOC"),KErrNoMemory);
   1.209 +	}
   1.210 +	return r;
   1.211 +}
   1.212 +
   1.213 +#endif