os/ossrv/ssl/libssl/src/wsd_solution.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/*
sl@0
     2
Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
sl@0
     3
sl@0
     4
Redistribution and use in source and binary forms, with or without 
sl@0
     5
modification, are permitted provided that the following conditions are met:
sl@0
     6
sl@0
     7
* Redistributions of source code must retain the above copyright notice, this 
sl@0
     8
  list of conditions and the following disclaimer.
sl@0
     9
* Redistributions in binary form must reproduce the above copyright notice, 
sl@0
    10
  this list of conditions and the following disclaimer in the documentation 
sl@0
    11
  and/or other materials provided with the distribution.
sl@0
    12
* Neither the name of Nokia Corporation nor the names of its contributors 
sl@0
    13
  may be used to endorse or promote products derived from this software 
sl@0
    14
  without specific prior written permission.
sl@0
    15
sl@0
    16
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
sl@0
    17
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
sl@0
    18
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
sl@0
    19
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 
sl@0
    20
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
sl@0
    21
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
sl@0
    22
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
sl@0
    23
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
sl@0
    24
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
sl@0
    25
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
sl@0
    26
sl@0
    27
Description:  Contains the WSD solution
sl@0
    28
*/
sl@0
    29
sl@0
    30
sl@0
    31
// INCLUDE FILES
sl@0
    32
#include "libssl_wsd_defs.h"
sl@0
    33
sl@0
    34
#if EMULATOR
sl@0
    35
sl@0
    36
#include <e32std.h>
sl@0
    37
#include <pthread.h>
sl@0
    38
#include <windows.h>
sl@0
    39
#include "wsd_solution.h"
sl@0
    40
sl@0
    41
//definition of all WSD related funcitons 
sl@0
    42
sl@0
    43
#define MAX_NUMBER_OF_PROCESSES	100
sl@0
    44
sl@0
    45
struct TWsdArray
sl@0
    46
{
sl@0
    47
	TUint 		iPid;
sl@0
    48
	TFullName 	iFullName;
sl@0
    49
	void* 		iPtr;
sl@0
    50
};
sl@0
    51
sl@0
    52
#pragma data_seg(".wsd")
sl@0
    53
TInt NumOfProcesses = 0;
sl@0
    54
#pragma data_seg()
sl@0
    55
sl@0
    56
#pragma bss_seg(".wsd")
sl@0
    57
struct TWsdArray wsdArray[MAX_NUMBER_OF_PROCESSES];
sl@0
    58
#pragma bss_seg()
sl@0
    59
sl@0
    60
// This function frees up the used slots within the wsdArray
sl@0
    61
// for use by other processes; internal to the implementation
sl@0
    62
// that provides WSD support
sl@0
    63
LOCAL_C TInt FindSlot();
sl@0
    64
sl@0
    65
void *Pls()
sl@0
    66
{
sl@0
    67
	HANDLE mutexHandle = NULL;
sl@0
    68
	unsigned short p[] = 
sl@0
    69
	{
sl@0
    70
		'l', 'i', 'b', 's', 's', 'l', '_', 'm','u','t','e','x'
sl@0
    71
	};
sl@0
    72
	
sl@0
    73
	if( !(mutexHandle = CreateMutex(NULL, TRUE, p)) )
sl@0
    74
	{
sl@0
    75
		// Mutex already created. Try to acquire it
sl@0
    76
		mutexHandle = OpenMutex(SYNCHRONIZE, FALSE, p);
sl@0
    77
		WaitForSingleObject(mutexHandle, INFINITE);
sl@0
    78
	}
sl@0
    79
sl@0
    80
	TProcessId procId = RProcess().Id();
sl@0
    81
	TFullName fullName = RProcess().FullName();
sl@0
    82
	TInt pid = *(TUint *)&procId;
sl@0
    83
	TInt slot = -1;
sl@0
    84
sl@0
    85
	for(int i=0 ; i < MAX_NUMBER_OF_PROCESSES ; ++i )
sl@0
    86
	{
sl@0
    87
		if(!wsdArray[i].iPid)
sl@0
    88
		{
sl@0
    89
			if(slot == -1)
sl@0
    90
				slot = i;
sl@0
    91
			
sl@0
    92
			continue;
sl@0
    93
		}
sl@0
    94
		else if(wsdArray[i].iPid == pid)
sl@0
    95
		{
sl@0
    96
			ReleaseMutex(mutexHandle);
sl@0
    97
			return wsdArray[i].iPtr;
sl@0
    98
		}
sl@0
    99
	}
sl@0
   100
	
sl@0
   101
	// NEW PROCESS
sl@0
   102
	
sl@0
   103
	if(MAX_NUMBER_OF_PROCESSES == NumOfProcesses)
sl@0
   104
	{
sl@0
   105
		// Find out if one of the slots reserved for previous processes
sl@0
   106
		// could be reused.
sl@0
   107
		TInt returnValue = -1;
sl@0
   108
		returnValue = FindSlot();
sl@0
   109
		if(returnValue != -1)
sl@0
   110
		{
sl@0
   111
			slot = returnValue;
sl@0
   112
		}
sl@0
   113
		else
sl@0
   114
		{
sl@0
   115
			User::Panic(_L("Pls() Reached the naximum number for the processes"),KErrNoMemory);
sl@0
   116
		}
sl@0
   117
	}
sl@0
   118
sl@0
   119
	wsdArray[slot].iPid = pid;
sl@0
   120
	wsdArray[slot].iFullName = fullName;
sl@0
   121
	wsdArray[slot].iPtr = NULL;
sl@0
   122
	
sl@0
   123
	// Increment the count for the number of processes
sl@0
   124
	++NumOfProcesses;
sl@0
   125
	
sl@0
   126
	// Release the mutex
sl@0
   127
	ReleaseMutex(mutexHandle);
sl@0
   128
	return wsdArray[slot].iPtr;
sl@0
   129
}
sl@0
   130
sl@0
   131
LOCAL_C TInt FindSlot()
sl@0
   132
{
sl@0
   133
	TInt slot = -1;
sl@0
   134
	TFullName fullName;
sl@0
   135
	TInt currentCount = 0;
sl@0
   136
	
sl@0
   137
	for(int i = 0 ; i < MAX_NUMBER_OF_PROCESSES ; ++i)
sl@0
   138
	{
sl@0
   139
		TFindProcess search(wsdArray[i].iFullName);
sl@0
   140
		if(search.Next(fullName) == KErrNone)
sl@0
   141
		{
sl@0
   142
			++currentCount;
sl@0
   143
			continue;
sl@0
   144
		}
sl@0
   145
		
sl@0
   146
		// Process with the given name does not exist in
sl@0
   147
		// the system. So the slot could be reused.
sl@0
   148
		wsdArray[i].iPid = 0;
sl@0
   149
sl@0
   150
		// Free the VAS associated with this "process" (terminated)
sl@0
   151
		VirtualFree(wsdArray[i].iPtr, 0, MEM_RELEASE);
sl@0
   152
		wsdArray[i].iPtr = NULL;
sl@0
   153
sl@0
   154
		if(slot == -1)
sl@0
   155
		{
sl@0
   156
			// Update
sl@0
   157
			slot = i;
sl@0
   158
		}
sl@0
   159
	}
sl@0
   160
	NumOfProcesses = currentCount;
sl@0
   161
	return slot;
sl@0
   162
}
sl@0
   163
sl@0
   164
TInt SetPls(void *aArg)
sl@0
   165
{
sl@0
   166
	HANDLE mutexHandle = NULL;
sl@0
   167
	unsigned short p[] = 
sl@0
   168
	{
sl@0
   169
		'l', 'i', 'b', 's', 's', 'l', '_', 'm','u','t','e','x'
sl@0
   170
	};
sl@0
   171
	
sl@0
   172
	if( !(mutexHandle = CreateMutex(NULL, TRUE, p)) )
sl@0
   173
	{
sl@0
   174
		// Mutex already created. Try to acquire it
sl@0
   175
		mutexHandle = OpenMutex(SYNCHRONIZE, FALSE, p);
sl@0
   176
		WaitForSingleObject(mutexHandle, INFINITE);
sl@0
   177
	}
sl@0
   178
sl@0
   179
	TProcessId procId = RProcess().Id();
sl@0
   180
	TFullName fullName = RProcess().FullName();
sl@0
   181
	TInt pid = *(TUint *)&procId;
sl@0
   182
sl@0
   183
	for(int i=0 ; i < MAX_NUMBER_OF_PROCESSES ; ++i )
sl@0
   184
	{
sl@0
   185
		if(wsdArray[i].iPid == pid)
sl@0
   186
		{
sl@0
   187
			wsdArray[i].iPtr = aArg;
sl@0
   188
			ReleaseMutex(mutexHandle);
sl@0
   189
			return KErrNone;
sl@0
   190
		}
sl@0
   191
	}
sl@0
   192
	
sl@0
   193
	ReleaseMutex(mutexHandle);
sl@0
   194
	return KErrNotFound;
sl@0
   195
}
sl@0
   196
sl@0
   197
void* AllocatePls(TInt aSize)
sl@0
   198
{
sl@0
   199
	void *r = VirtualAlloc(NULL, 
sl@0
   200
						   aSize,
sl@0
   201
						   MEM_COMMIT | MEM_RESERVE,
sl@0
   202
						   PAGE_READWRITE);
sl@0
   203
	if(!r)
sl@0
   204
	{
sl@0
   205
		User::Panic(_L("AllocatePls() VIRTUALALLOC"),KErrNoMemory);
sl@0
   206
	}
sl@0
   207
	return r;
sl@0
   208
}
sl@0
   209
sl@0
   210
#endif