1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: bsprocessstarter.cpp |
|
15 * |
|
16 */ |
|
17 // INCLUDE FILES |
|
18 #include "bsprocessstarter.h" |
|
19 #include <e32std.h> |
|
20 #include <f32file.h> |
|
21 |
|
22 |
|
23 // CONSTANTS |
|
24 _LIT( KEka2ExeDir,"\\sys\\bin\\"); |
|
25 _LIT( KEka2LaunchMutexExt, "[lMtx]" ); |
|
26 const TInt KEka2SrvConnTries = 7; |
|
27 const TInt KEka2SrvConnInitialRetryWait = 500; //MicroSeconds => 0.0005s |
|
28 |
|
29 |
|
30 |
|
31 // ============================================================== |
|
32 // ====================== HELPER CLASS ========================== |
|
33 // ============================================================== |
|
34 |
|
35 /** |
|
36 * RSessionBase accessor to give to the ProcessStarter |
|
37 * access to RSessionBase::CreateSession(). |
|
38 */ |
|
39 class REka2SessionBaseAccessor : public RSessionBase |
|
40 { |
|
41 public: // Constructor |
|
42 inline REka2SessionBaseAccessor() |
|
43 { |
|
44 } |
|
45 |
|
46 public: // New functions |
|
47 |
|
48 /** |
|
49 * Public access to RSessionBase::CreateSession(). |
|
50 */ |
|
51 inline TInt CreateSession( const TDesC& aServer, |
|
52 const TVersion& aVersion, |
|
53 TInt aAsyncMessageSlots ) |
|
54 { |
|
55 return RSessionBase::CreateSession( aServer, |
|
56 aVersion, |
|
57 aAsyncMessageSlots ); |
|
58 } |
|
59 }; |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 // ============================================================== |
|
66 // ====================== PROCESSSTARTER ======================== |
|
67 // ============================================================== |
|
68 |
|
69 |
|
70 // -------------------------------------------------------------- |
|
71 // BSProcessStarter::FullExePathForClienLocation() |
|
72 // -------------------------------------------------------------- |
|
73 // |
|
74 void BSProcessStarter::FullExePathForClienLocation( |
|
75 const TDesC& aExeName, |
|
76 TFileName& aFullExePath ) |
|
77 { |
|
78 //Get drive (C:) where this client code is installed |
|
79 { |
|
80 TFileName tmp; |
|
81 Dll::FileName( tmp ); |
|
82 aFullExePath.Copy( TParsePtrC( tmp ).Drive() ); |
|
83 } |
|
84 |
|
85 //Build the rest from the exe path |
|
86 aFullExePath.Append( KEka2ExeDir ); |
|
87 aFullExePath.Append( aExeName ); |
|
88 } |
|
89 |
|
90 |
|
91 |
|
92 // -------------------------------------------------------------- |
|
93 // BSProcessStarter::StartInstance() |
|
94 // -------------------------------------------------------------- |
|
95 // |
|
96 TInt BSProcessStarter::StartInstance( |
|
97 const TDesC& aFullExePath, |
|
98 const TDesC& aCommand, |
|
99 const TArray< TProcessStartupParam >* aParams ) |
|
100 { |
|
101 RMutex launchMutex; |
|
102 TInt error = KErrNotFound; |
|
103 |
|
104 { |
|
105 // Dynamic mutex name used to allow code share |
|
106 TName launchMutexName( TParsePtrC( aFullExePath ).Name() ); |
|
107 launchMutexName.Append( KEka2LaunchMutexExt ); |
|
108 |
|
109 // Open or Create mutex to serialize to access to server startup code. |
|
110 // (race condition safe way) |
|
111 while( error == KErrNotFound ) |
|
112 { |
|
113 error = launchMutex.CreateGlobal( launchMutexName ); |
|
114 if( error != KErrAlreadyExists ) |
|
115 { |
|
116 break; |
|
117 } |
|
118 error = launchMutex.OpenGlobal( launchMutexName ); |
|
119 } |
|
120 |
|
121 if( error != KErrNone ) |
|
122 { |
|
123 return error; |
|
124 } |
|
125 } |
|
126 |
|
127 |
|
128 launchMutex.Wait(); |
|
129 |
|
130 //Serialized access |
|
131 error = BSProcessStarter::DoStartInstance( aFullExePath, |
|
132 aCommand, |
|
133 aParams ); |
|
134 |
|
135 launchMutex.Signal(); |
|
136 launchMutex.Close(); |
|
137 |
|
138 return error; |
|
139 } |
|
140 |
|
141 |
|
142 |
|
143 // -------------------------------------------------------------- |
|
144 // BSProcessStarter::ConnectToServer() |
|
145 // -------------------------------------------------------------- |
|
146 // |
|
147 TInt BSProcessStarter::ConnectToServer( |
|
148 const TDesC& aFullExePath, |
|
149 const TDesC& aCommand, |
|
150 const TArray< TProcessStartupParam >* aParams, |
|
151 RSessionBase& aSessionToConnect, |
|
152 const TDesC& aServerName, |
|
153 const TVersion& aClientVersion, |
|
154 TInt aAsyncMessageSlots ) |
|
155 { |
|
156 if( aSessionToConnect.Handle() != KNullHandle ) |
|
157 { |
|
158 return KErrInUse; |
|
159 } |
|
160 |
|
161 TInt err = KErrNone; |
|
162 TInt startupWait = KEka2SrvConnInitialRetryWait; |
|
163 |
|
164 //Server connect and launch loop |
|
165 for( TInt trie = 0 ; trie < KEka2SrvConnTries ; trie++ ) |
|
166 { |
|
167 REka2SessionBaseAccessor acc; |
|
168 err = acc.CreateSession( aServerName, |
|
169 aClientVersion, |
|
170 aAsyncMessageSlots ); |
|
171 |
|
172 if( err == KErrNone ) |
|
173 { |
|
174 //session ownership is now on client |
|
175 aSessionToConnect = acc; |
|
176 return KErrNone; |
|
177 } |
|
178 |
|
179 else if( ( err == KErrNotFound ) || |
|
180 ( err == KErrServerTerminated ) ) |
|
181 { |
|
182 //Server missing or died when connecting |
|
183 //Start a new server |
|
184 err = BSProcessStarter::StartInstance( aFullExePath, |
|
185 aCommand, |
|
186 aParams ); |
|
187 |
|
188 //If process exist already, then all is fine |
|
189 //(some other process started it between the origical connect and launch trie) |
|
190 if( err == KErrAlreadyExists ) |
|
191 { |
|
192 err = KErrNone; |
|
193 } |
|
194 |
|
195 //If server process start failed, bail out. |
|
196 if( err != KErrNone ) |
|
197 { |
|
198 return err; |
|
199 } |
|
200 |
|
201 //If this is 2nd or subsequent try, |
|
202 //give some time for server to startup |
|
203 if( trie > 0 ) |
|
204 { |
|
205 // Code scanner warning : Use of User::After (id:92) |
|
206 // it is required to be used here |
|
207 User::After( startupWait ); // CSI: 92 # See above |
|
208 startupWait = 2 * startupWait; |
|
209 } |
|
210 } |
|
211 |
|
212 else |
|
213 { |
|
214 //Server process start failed. Bail out. |
|
215 return err; |
|
216 } |
|
217 } |
|
218 |
|
219 return err; |
|
220 } |
|
221 |
|
222 |
|
223 |
|
224 // -------------------------------------------------------------- |
|
225 // BSProcessStarter::DoStartServerInstance() |
|
226 // -------------------------------------------------------------- |
|
227 // |
|
228 TInt BSProcessStarter::DoStartInstance( |
|
229 const TDesC& aFullExePath, |
|
230 const TDesC& aCommand, |
|
231 const TArray< TProcessStartupParam >* aParams ) |
|
232 { |
|
233 TInt error = KErrNone; |
|
234 |
|
235 //Create process |
|
236 RProcess process; |
|
237 error = process.Create( aFullExePath, aCommand ); |
|
238 |
|
239 if( error == KErrNone ) |
|
240 { |
|
241 //Set process startup parameters |
|
242 error = ApplyParameters( aParams, process ); |
|
243 |
|
244 //And execute the process and wait it's startup |
|
245 if( error == KErrNone ) |
|
246 { |
|
247 TRequestStatus rendezvousStatus; |
|
248 process.Rendezvous( rendezvousStatus ); |
|
249 |
|
250 process.Resume(); |
|
251 // Codescanner warning: user of User::WaitForRequest (Id:94) |
|
252 // it is required to use at server startup |
|
253 User::WaitForRequest( rendezvousStatus ); // CSI: 94 # See above |
|
254 error = rendezvousStatus.Int(); |
|
255 |
|
256 if( process.ExitType() != EExitPending ) |
|
257 { |
|
258 //Something failed in server startup |
|
259 //Force the error code to be always something |
|
260 //else than KErrNone |
|
261 if( error == KErrNone ) |
|
262 { |
|
263 error = KErrServerTerminated; |
|
264 } |
|
265 } |
|
266 } |
|
267 } |
|
268 |
|
269 process.Close(); |
|
270 |
|
271 return error; |
|
272 } |
|
273 |
|
274 |
|
275 |
|
276 // -------------------------------------------------------------- |
|
277 // BSProcessStarter::ApplyParameters() |
|
278 // -------------------------------------------------------------- |
|
279 // |
|
280 TInt BSProcessStarter::ApplyParameters( |
|
281 const TArray< TProcessStartupParam >* aParams, |
|
282 RProcess& aProcess ) |
|
283 { |
|
284 TInt error = KErrNone; |
|
285 if( aParams ) |
|
286 { |
|
287 const TInt paramCount = aParams->Count(); |
|
288 for( TInt ix = 0; ix < paramCount; ix++ ) |
|
289 { |
|
290 error = (*aParams)[ ix ].ApplyParam( aProcess ); |
|
291 if( error != KErrNone ) |
|
292 { |
|
293 break; |
|
294 } |
|
295 } |
|
296 } |
|
297 |
|
298 return error; |
|
299 } |
|
300 |
|
301 |
|
302 |
|
303 // ============================================================== |
|
304 // ================== TPROCESSSTARTUPPARAM ====================== |
|
305 // ============================================================== |
|
306 |
|
307 // -------------------------------------------------------------- |
|
308 // TProcessStartupParam::TProcessStartupParam() |
|
309 // -------------------------------------------------------------- |
|
310 // |
|
311 TProcessStartupParam::TProcessStartupParam() |
|
312 : iType( EUnknown ), |
|
313 iSlot( KErrNotFound ) |
|
314 { |
|
315 } |
|
316 |
|
317 |
|
318 // -------------------------------------------------------------- |
|
319 // TProcessStartupParam::Set() |
|
320 // -------------------------------------------------------------- |
|
321 // |
|
322 void TProcessStartupParam::Set( TInt aSlot, |
|
323 const RHandleBase& aHandle ) |
|
324 { |
|
325 iType = EHandle; |
|
326 iSlot = aSlot; |
|
327 iHandle = &aHandle; |
|
328 } |
|
329 |
|
330 |
|
331 // -------------------------------------------------------------- |
|
332 // TProcessStartupParam::Set() |
|
333 // -------------------------------------------------------------- |
|
334 // |
|
335 void TProcessStartupParam::Set( TInt aSlot, |
|
336 const RSubSessionBase& aSubSession ) |
|
337 { |
|
338 iType = ESubSession; |
|
339 iSlot = aSlot; |
|
340 iSubSession = &aSubSession; |
|
341 } |
|
342 |
|
343 |
|
344 // -------------------------------------------------------------- |
|
345 // TProcessStartupParam::Set() |
|
346 // -------------------------------------------------------------- |
|
347 // |
|
348 void TProcessStartupParam::Set( TInt aSlot, |
|
349 const TDesC16& aDes ) |
|
350 { |
|
351 iType = EDes16; |
|
352 iSlot = aSlot; |
|
353 iDes16.Set( aDes ); |
|
354 } |
|
355 |
|
356 |
|
357 // -------------------------------------------------------------- |
|
358 // TProcessStartupParam::Set() |
|
359 // -------------------------------------------------------------- |
|
360 // |
|
361 void TProcessStartupParam::Set( TInt aSlot, |
|
362 const TDesC8& aDes ) |
|
363 { |
|
364 iType = EDes8; |
|
365 iSlot = aSlot; |
|
366 iDes8.Set( aDes ); |
|
367 } |
|
368 |
|
369 |
|
370 // -------------------------------------------------------------- |
|
371 // TProcessStartupParam::Set() |
|
372 // -------------------------------------------------------------- |
|
373 // |
|
374 void TProcessStartupParam::Set( TInt aSlot, |
|
375 TInt aInt ) |
|
376 { |
|
377 iType = EInt; |
|
378 iSlot = aSlot; |
|
379 iInt = aInt; |
|
380 } |
|
381 |
|
382 |
|
383 // -------------------------------------------------------------- |
|
384 // TProcessStartupParam::ApplyParam() |
|
385 // -------------------------------------------------------------- |
|
386 // |
|
387 TInt TProcessStartupParam::ApplyParam( RProcess& aProcess ) const |
|
388 { |
|
389 switch( iType ) |
|
390 { |
|
391 case EHandle: return aProcess.SetParameter( iSlot, *iHandle ); |
|
392 case ESubSession: return aProcess.SetParameter( iSlot, *iSubSession ); |
|
393 case EDes16: return aProcess.SetParameter( iSlot, iDes16 ); |
|
394 case EDes8: return aProcess.SetParameter( iSlot, iDes8 ); |
|
395 case EInt: return aProcess.SetParameter( iSlot, iInt ); |
|
396 } |
|
397 |
|
398 return KErrUnknown; |
|
399 } |
|
400 |
|
401 |
|
402 // END OF FILE |
|
403 |
|
404 |
|
405 |
|