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