os/ossrv/ssl/libcrypto/src/crypto/libcrypto_wsd_solution.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ssl/libcrypto/src/crypto/libcrypto_wsd_solution.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,250 @@
     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 +#ifndef EMULATOR
    1.36 +#define EMULATOR (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
    1.37 +#endif
    1.38 +
    1.39 +#include <e32std.h>
    1.40 +#include <stddef.h>
    1.41 +#include <windows.h>
    1.42 +
    1.43 +//definition of all WSD related funcitons 
    1.44 +
    1.45 +#define MAX_NUMBER_OF_PROCESSES	100
    1.46 +
    1.47 +struct TWsdArray
    1.48 +{
    1.49 +	TUint 		iPid;
    1.50 +	TFullName 	iFullName;
    1.51 +	void* 		iPtr;
    1.52 +};
    1.53 +
    1.54 +#pragma data_seg(".wsd")
    1.55 +TInt NumOfProcesses = 0;
    1.56 +TInt MutexInitialized = 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 +	LPCTSTR p = (unsigned short *)"libcrypto_mutex";
    1.72 +	
    1.73 +	if(!MutexInitialized)
    1.74 +	{
    1.75 +		// Keep an unused handle to the mutex object to prevent
    1.76 +		// handle exhaustion
    1.77 +		CreateMutex(NULL, FALSE, p);
    1.78 +		MutexInitialized = 1;
    1.79 +	}
    1.80 +	
    1.81 +	if(mutexHandle = CreateMutex(NULL, FALSE, p))
    1.82 +    {
    1.83 +	    DWORD dwWaitResult = WaitForSingleObject(mutexHandle, INFINITE);
    1.84 +	    switch (dwWaitResult) 
    1.85 +		{
    1.86 +		    // Cannot get mutex ownership due to time-out.
    1.87 +		    case WAIT_TIMEOUT: 
    1.88 +		        return NULL;
    1.89 +		
    1.90 +		    // Got ownership of the abandoned mutex object.
    1.91 +		    case WAIT_ABANDONED: 
    1.92 +		         return NULL; 
    1.93 +   	    }
    1.94 +
    1.95 +		if(dwWaitResult == WAIT_OBJECT_0)
    1.96 +		{
    1.97 +			// Thread got the mutex ownership
    1.98 +			TProcessId procId = RProcess().Id();
    1.99 +			TFullName fullName = RProcess().FullName();
   1.100 +			TInt pid = *(TUint *)&procId;
   1.101 +			TInt slot = -1;
   1.102 +
   1.103 +			for(int i=0 ; i < MAX_NUMBER_OF_PROCESSES ; ++i )
   1.104 +			{
   1.105 +				if(!wsdArray[i].iPid)
   1.106 +				{
   1.107 +					if(slot == -1)
   1.108 +						slot = i;
   1.109 +					
   1.110 +					continue;
   1.111 +				}
   1.112 +				else if(wsdArray[i].iPid == pid)
   1.113 +				{
   1.114 +					ReleaseMutex(mutexHandle);
   1.115 +					CloseHandle(mutexHandle);
   1.116 +					return wsdArray[i].iPtr;
   1.117 +				}
   1.118 +			}
   1.119 +			
   1.120 +			// NEW PROCESS
   1.121 +			
   1.122 +			if(MAX_NUMBER_OF_PROCESSES == NumOfProcesses)
   1.123 +			{
   1.124 +				// Find out if one of the slots reserved for previous processes
   1.125 +				// could be reused.
   1.126 +				TInt returnValue = -1;
   1.127 +				returnValue = FindSlot();
   1.128 +				if(returnValue != -1)
   1.129 +				{
   1.130 +					slot = returnValue;
   1.131 +				}
   1.132 +				else
   1.133 +				{
   1.134 +					User::Panic(_L("Pls() Reached the naximum number for the processes"),KErrNoMemory);
   1.135 +				}
   1.136 +			}
   1.137 +
   1.138 +			wsdArray[slot].iPid = pid;
   1.139 +			wsdArray[slot].iFullName = fullName;
   1.140 +			wsdArray[slot].iPtr = NULL;
   1.141 +			
   1.142 +			// Increment the count for the number of processes
   1.143 +			++NumOfProcesses;
   1.144 +			
   1.145 +			// Release the mutex
   1.146 +			ReleaseMutex(mutexHandle);
   1.147 +			CloseHandle(mutexHandle);
   1.148 +			return wsdArray[slot].iPtr;
   1.149 +		}
   1.150 +    }
   1.151 +}
   1.152 +
   1.153 +LOCAL_C TInt FindSlot()
   1.154 +{
   1.155 +	TInt slot = -1;
   1.156 +	TFullName fullName;
   1.157 +	TInt currentCount = 0;
   1.158 +	
   1.159 +	for(int i = 0 ; i < MAX_NUMBER_OF_PROCESSES ; ++i)
   1.160 +	{
   1.161 +		TFindProcess search(wsdArray[i].iFullName);
   1.162 +		if(search.Next(fullName) == KErrNone)
   1.163 +		{
   1.164 +			++currentCount;
   1.165 +			continue;
   1.166 +		}
   1.167 +		
   1.168 +		// Process with the given name does not exist in
   1.169 +		// the system. So the slot could be reused.
   1.170 +		wsdArray[i].iPid = 0;
   1.171 +
   1.172 +		// Free the VAS associated with this "process" (terminated)
   1.173 +		if(wsdArray[i].iPtr)
   1.174 +			{
   1.175 +			VirtualFree(wsdArray[i].iPtr, 0, MEM_RELEASE);
   1.176 +			}
   1.177 +		wsdArray[i].iPtr = NULL;
   1.178 +
   1.179 +		if(slot == -1)
   1.180 +		{
   1.181 +			// Update
   1.182 +			slot = i;
   1.183 +		}
   1.184 +	}
   1.185 +	NumOfProcesses = currentCount;
   1.186 +	return slot;
   1.187 +}
   1.188 +
   1.189 +TInt SetPls(void *aArg)
   1.190 +{
   1.191 +	HANDLE mutexHandle = NULL;
   1.192 +
   1.193 +	LPCTSTR p = (unsigned short *)"libcrypto_mutex";
   1.194 +	
   1.195 +	if(!MutexInitialized)
   1.196 +	{
   1.197 +		// Keep an unused handle to the mutex object to prevent
   1.198 +		// handle exhaustion
   1.199 +		CreateMutex(NULL, FALSE, p);
   1.200 +		MutexInitialized = 1;
   1.201 +	}
   1.202 +	
   1.203 +	if(mutexHandle = CreateMutex(NULL, FALSE, p))
   1.204 +    {
   1.205 +	    DWORD dwWaitResult = WaitForSingleObject(mutexHandle, INFINITE);
   1.206 +	    switch (dwWaitResult) 
   1.207 +		{
   1.208 +		    // Cannot get mutex ownership due to time-out.
   1.209 +		    case WAIT_TIMEOUT: 
   1.210 +		        return KErrNotFound;
   1.211 +		
   1.212 +		    // Got ownership of the abandoned mutex object.
   1.213 +		    case WAIT_ABANDONED: 
   1.214 +		         return KErrNotFound; 
   1.215 +   	    }
   1.216 +
   1.217 +		if(dwWaitResult == WAIT_OBJECT_0)
   1.218 +		{
   1.219 +			// Thread got the mutex ownership
   1.220 +			TProcessId procId = RProcess().Id();
   1.221 +			TFullName fullName = RProcess().FullName();
   1.222 +			TInt pid = *(TUint *)&procId;
   1.223 +
   1.224 +			for(int i=0 ; i < MAX_NUMBER_OF_PROCESSES ; ++i )
   1.225 +			{
   1.226 +				if(wsdArray[i].iPid == pid)
   1.227 +				{
   1.228 +					wsdArray[i].iPtr = aArg;
   1.229 +					ReleaseMutex(mutexHandle);
   1.230 +					CloseHandle(mutexHandle);
   1.231 +					return KErrNone;
   1.232 +				}
   1.233 +			}
   1.234 +			
   1.235 +			ReleaseMutex(mutexHandle);
   1.236 +			CloseHandle(mutexHandle);
   1.237 +			return KErrNotFound;
   1.238 +		}
   1.239 +    }
   1.240 +}
   1.241 +
   1.242 +void* AllocatePls(TInt aSize)
   1.243 +{
   1.244 +	void *r = VirtualAlloc(NULL, 
   1.245 +						   aSize,
   1.246 +						   MEM_COMMIT | MEM_RESERVE,
   1.247 +						   PAGE_READWRITE);
   1.248 +	if(!r)
   1.249 +	{
   1.250 +		User::Panic(_L("AllocatePls() VIRTUALALLOC"),KErrNoMemory);
   1.251 +	}
   1.252 +	return r;
   1.253 +}