|
1 /* |
|
2 * Copyright (c) 2007-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: Maintains a list of process ids and matching origin* |
|
15 */ |
|
16 |
|
17 #include <e32base.h> |
|
18 #include <f32file.h> |
|
19 |
|
20 #include "processoriginmapper.h" |
|
21 #include "harvesterlog.h" |
|
22 |
|
23 // File header of the location/origin -mapping file. |
|
24 _LIT8( KFileHeader, "Process UID / Origin -mapping table\n" |
|
25 "\n" |
|
26 "UID Origin\n" ); |
|
27 |
|
28 // Location of the process/origin -mapping file. |
|
29 _LIT( KDefaultMappingFile, "C:\\private\\200009F5\\mappings.db" ); |
|
30 _LIT( KDefaultMappingRomFile, "Z:\\private\\200009F5\\mappings.db" ); |
|
31 |
|
32 // Maximum line length read from a file. |
|
33 // Thus also size of the line buffer and length of first file header line, |
|
34 // which should be file's longest line. |
|
35 const TInt KMaxLineLength = 36; |
|
36 |
|
37 // Length of all file header lines put together. |
|
38 const TInt KHeaderLength = 57; |
|
39 |
|
40 |
|
41 //----------------------------------------------------------------------------- |
|
42 // CProcessOriginMapper::NewL() |
|
43 //----------------------------------------------------------------------------- |
|
44 // |
|
45 CProcessOriginMapper* CProcessOriginMapper::NewL() |
|
46 { |
|
47 CProcessOriginMapper* self = new (ELeave) CProcessOriginMapper(); |
|
48 CleanupStack::PushL( self ); |
|
49 self->ConstructL(); |
|
50 CleanupStack::Pop( self ); |
|
51 return self; |
|
52 } |
|
53 |
|
54 //----------------------------------------------------------------------------- |
|
55 // CProcessOriginMapper::~CProcessOriginMapper() |
|
56 // Destructor. |
|
57 //----------------------------------------------------------------------------- |
|
58 // |
|
59 CProcessOriginMapper::~CProcessOriginMapper() |
|
60 { |
|
61 delete iLineBuffer; |
|
62 iProcessOriginMap.Close(); |
|
63 iReadStream.Close(); |
|
64 iWriteStream.Close(); |
|
65 iFs.Close(); |
|
66 } |
|
67 |
|
68 //----------------------------------------------------------------------------- |
|
69 // CProcessOriginMapper::CProcessOriginMapper() |
|
70 // Constructor. |
|
71 //----------------------------------------------------------------------------- |
|
72 // |
|
73 CProcessOriginMapper::CProcessOriginMapper() |
|
74 : iLineBuffer( NULL ), iDefaultFileRead( EFalse ) |
|
75 { |
|
76 } |
|
77 |
|
78 //----------------------------------------------------------------------------- |
|
79 // CProcessOriginMapper::ConstructL() |
|
80 // Second phase construction called by NewL(). |
|
81 //----------------------------------------------------------------------------- |
|
82 // |
|
83 void CProcessOriginMapper::ConstructL() |
|
84 { |
|
85 User::LeaveIfError( iFs.Connect() ); |
|
86 iLineBuffer = HBufC8::NewL( KMaxLineLength + 4 ); |
|
87 } |
|
88 |
|
89 //----------------------------------------------------------------------------- |
|
90 // CProcessOriginMapper::RegisterProcessL() |
|
91 // Add a process to the list. |
|
92 //----------------------------------------------------------------------------- |
|
93 // |
|
94 void CProcessOriginMapper::RegisterProcessL( const TUid& aProcessId, |
|
95 const TOrigin& aOrigin ) |
|
96 { |
|
97 WRITELOG2( "CProcessOriginMapper::RegisterProcessL - processId: 0x%.8x, origin: %d", aProcessId.iUid, (TInt)aOrigin ); |
|
98 const TInt index = FindProcess( aProcessId ); |
|
99 if ( index < 0 ) // not found |
|
100 { |
|
101 TProcessOriginPair pair = { aProcessId, aOrigin }; |
|
102 iProcessOriginMap.AppendL( pair ); |
|
103 } |
|
104 else if ( index < iProcessOriginMap.Count() ) |
|
105 { |
|
106 iProcessOriginMap[index].iOrigin = aOrigin; |
|
107 } |
|
108 else |
|
109 { |
|
110 User::Leave( KErrUnknown ); |
|
111 } |
|
112 |
|
113 if( !iDefaultFileRead ) |
|
114 { |
|
115 WriteFileL(); |
|
116 } |
|
117 } |
|
118 |
|
119 //----------------------------------------------------------------------------- |
|
120 // CProcessOriginMapper::UnregisterProcessL() |
|
121 // Remove a process from the list. |
|
122 //----------------------------------------------------------------------------- |
|
123 // |
|
124 void CProcessOriginMapper::UnregisterProcessL( const TUid& aProcessId ) |
|
125 { |
|
126 WRITELOG1( "CProcessOriginMapper::UnregisterProcessL - processId: 0x%.8x", aProcessId.iUid ); |
|
127 const TInt index = FindProcess( aProcessId ); |
|
128 if ( index >= 0 && index < iProcessOriginMap.Count() ) |
|
129 { |
|
130 iProcessOriginMap.Remove( index ); |
|
131 } |
|
132 else |
|
133 { |
|
134 User::Leave( KErrNotFound ); |
|
135 } |
|
136 |
|
137 WriteFileL(); |
|
138 } |
|
139 |
|
140 //----------------------------------------------------------------------------- |
|
141 // CProcessOriginMapper::OriginL() |
|
142 // Check a process from the list. |
|
143 //----------------------------------------------------------------------------- |
|
144 // |
|
145 TOrigin CProcessOriginMapper::OriginL( const TUid& aProcessId ) |
|
146 { |
|
147 WRITELOG1( "CProcessOriginMapper::OriginL - search for processId: 0x%.8x", aProcessId.iUid ); |
|
148 TOrigin origin = MdeConstants::Object::EOther; |
|
149 const TInt index = FindProcess( aProcessId ); |
|
150 if ( index >= 0 && index < iProcessOriginMap.Count() ) |
|
151 { |
|
152 origin = iProcessOriginMap[index].iOrigin; |
|
153 } |
|
154 else |
|
155 { |
|
156 WRITELOG( "CProcessOriginMapper::OriginL - search for processId: not found !!" ); |
|
157 User::Leave( KErrNotFound ); |
|
158 } |
|
159 |
|
160 WRITELOG2( "CProcessOriginMapper::OriginL - processId: 0x%.8x, origin: %d", aProcessId.iUid, (TInt)origin ); |
|
161 return origin; |
|
162 } |
|
163 |
|
164 //----------------------------------------------------------------------------- |
|
165 // CProcessOriginMapper::ReadFileL() |
|
166 // Read process id mappings from a file. |
|
167 // This doesn't clear the list of currently registered mappings. |
|
168 //----------------------------------------------------------------------------- |
|
169 // |
|
170 TInt CProcessOriginMapper::ReadFileL( const TDesC& aFile ) |
|
171 { |
|
172 WRITELOG1( "CProcessOriginMapper::ReadFileL - START reading file %S", &aFile ); |
|
173 if ( aFile.Length() <= 0 || aFile.Length() > KMaxFileName ) |
|
174 { |
|
175 User::Leave( KErrBadName ); |
|
176 } |
|
177 |
|
178 TInt err = iReadStream.Open( iFs, aFile, EFileRead | EFileStreamText ); |
|
179 if ( err == KErrNotFound || err == KErrPathNotFound ) |
|
180 { |
|
181 // if mappings file is not found, try to read from rom (Z) drive |
|
182 err = iReadStream.Open( iFs, KDefaultMappingRomFile, EFileRead | EFileStreamText ); |
|
183 } |
|
184 User::LeaveIfError( err ); |
|
185 |
|
186 // read / match file header |
|
187 |
|
188 const TChar KLineFeed = '\n'; |
|
189 TPtr8 ptr( iLineBuffer->Des() ); |
|
190 iReadStream.ReadL( ptr, KLineFeed ); |
|
191 ptr.SetLength( KMaxLineLength-1 ); |
|
192 TBufC8<KHeaderLength> headerBuf( KFileHeader ); |
|
193 if ( headerBuf.Locate( KLineFeed ) != KMaxLineLength-1 || |
|
194 iLineBuffer->Compare( headerBuf.Left(KMaxLineLength-1) ) != 0 ) |
|
195 { |
|
196 User::Leave( KErrCorrupt ); |
|
197 } |
|
198 iReadStream.ReadL( ptr, KLineFeed ); // skip the next two lines (header stuff) |
|
199 iReadStream.ReadL( ptr, KLineFeed ); |
|
200 |
|
201 TUid processId = { 0 }; |
|
202 TOrigin origin = MdeConstants::Object::EOther; |
|
203 |
|
204 // read and register pairs one by one |
|
205 // for-loop limits the maximum amount of loops |
|
206 TInt count = 0; |
|
207 for ( count = 0; count < KMaxMappingSize; count++ ) |
|
208 { |
|
209 TRAP( err, ReadProcessOriginPairL( processId, origin ) ); |
|
210 if ( err == KErrEof ) |
|
211 { |
|
212 // successful exit |
|
213 break; |
|
214 } |
|
215 else if ( err != KErrNone ) |
|
216 { |
|
217 User::Leave( KErrCorrupt ); |
|
218 } |
|
219 RegisterProcessL( processId, origin ); |
|
220 } |
|
221 |
|
222 iReadStream.Release(); |
|
223 WRITELOG2( "CProcessOriginMapper::ReadFileL - END reading file %S, count %d", &aFile, count ); |
|
224 return count; |
|
225 } |
|
226 |
|
227 //----------------------------------------------------------------------------- |
|
228 // CProcessOriginMapper::ReadFileL() |
|
229 // Read process id mappings from the default file. |
|
230 //----------------------------------------------------------------------------- |
|
231 // |
|
232 TInt CProcessOriginMapper::ReadFileL() |
|
233 { |
|
234 WRITELOG( "CProcessOriginMapper::ReadFileL - reading default file..." ); |
|
235 iDefaultFileRead = ETrue; |
|
236 const TInt count = ReadFileL( KDefaultMappingFile ); |
|
237 iDefaultFileRead = EFalse; |
|
238 return count; |
|
239 } |
|
240 |
|
241 //----------------------------------------------------------------------------- |
|
242 // CProcessOriginMapper::WriteFileL() |
|
243 // Writes process id mappings to a file. |
|
244 //----------------------------------------------------------------------------- |
|
245 // |
|
246 void CProcessOriginMapper::WriteFileL( const TDesC& aFile ) |
|
247 { |
|
248 WRITELOG1( "CProcessOriginMapper::WriteFileL - writing file %S", &aFile ); |
|
249 if ( aFile.Length() <= 0 || aFile.Length() > KMaxFileName ) |
|
250 { |
|
251 User::Leave( KErrBadName ); |
|
252 } |
|
253 |
|
254 TInt err = iWriteStream.Replace( iFs, aFile, |
|
255 EFileWrite | EFileShareExclusive | EFileStreamText ); |
|
256 User::LeaveIfError( err ); |
|
257 CleanupClosePushL( iWriteStream ); |
|
258 |
|
259 // write file header |
|
260 |
|
261 iWriteStream.WriteL( KFileHeader ); |
|
262 |
|
263 TUid processId = { 0 }; |
|
264 TOrigin origin = MdeConstants::Object::EOther; |
|
265 |
|
266 // write pairs one by one |
|
267 // for-loop limits the maximum amount of loops |
|
268 const TInt count = iProcessOriginMap.Count(); |
|
269 for ( TInt i( 0 ); i < count; i++ ) |
|
270 { |
|
271 processId = iProcessOriginMap[i].iProcessId; |
|
272 origin = iProcessOriginMap[i].iOrigin; |
|
273 TRAP( err, WriteProcessOriginPairL( processId, origin ) ); |
|
274 if ( err != KErrNone ) |
|
275 { |
|
276 User::Leave( KErrCorrupt ); |
|
277 } |
|
278 } |
|
279 |
|
280 iWriteStream.CommitL(); |
|
281 iWriteStream.Release(); |
|
282 |
|
283 CleanupStack::PopAndDestroy( &iWriteStream ); |
|
284 } |
|
285 |
|
286 //----------------------------------------------------------------------------- |
|
287 // CProcessOriginMapper::WriteFileL() |
|
288 // Write process id mappings to the default file. |
|
289 //----------------------------------------------------------------------------- |
|
290 // |
|
291 void CProcessOriginMapper::WriteFileL() |
|
292 { |
|
293 WRITELOG( "CProcessOriginMapper::WriteFileL - writing default file..." ); |
|
294 WriteFileL( KDefaultMappingFile ); |
|
295 } |
|
296 |
|
297 //----------------------------------------------------------------------------- |
|
298 // CProcessOriginMapper::Clear() |
|
299 // Clear mapping table in memory. |
|
300 //----------------------------------------------------------------------------- |
|
301 // |
|
302 void CProcessOriginMapper::Clear() |
|
303 { |
|
304 WRITELOG( "CProcessOriginMapper::Clear - clearing origin map db" ); |
|
305 iProcessOriginMap.Reset(); |
|
306 } |
|
307 |
|
308 //----------------------------------------------------------------------------- |
|
309 // CProcessOriginMapper::Count() |
|
310 // Return the count of currently registered mappings. |
|
311 //----------------------------------------------------------------------------- |
|
312 // |
|
313 TInt CProcessOriginMapper::Count() |
|
314 { |
|
315 return iProcessOriginMap.Count(); |
|
316 } |
|
317 |
|
318 |
|
319 // PRIVATE METHODS |
|
320 |
|
321 //----------------------------------------------------------------------------- |
|
322 // CProcessOriginMapper::FindProcess() |
|
323 // Find index by process id. |
|
324 //----------------------------------------------------------------------------- |
|
325 // |
|
326 TInt CProcessOriginMapper::FindProcess( const TUid& aProcessId ) |
|
327 { |
|
328 const TInt count = iProcessOriginMap.Count(); |
|
329 for ( TInt i( 0 ); i < count; i++ ) |
|
330 { |
|
331 if ( iProcessOriginMap[i].iProcessId == aProcessId ) |
|
332 { |
|
333 return i; |
|
334 } |
|
335 } |
|
336 |
|
337 return KErrNotFound; |
|
338 } |
|
339 |
|
340 //----------------------------------------------------------------------------- |
|
341 // CProcessOriginMapper::ReadProcessOriginPairL() |
|
342 // Read one related process/origin pair from a text file. |
|
343 //----------------------------------------------------------------------------- |
|
344 // |
|
345 void CProcessOriginMapper::ReadProcessOriginPairL( TUid& aProcessId, |
|
346 TOrigin& aOrigin ) |
|
347 { |
|
348 const TUint32 KMinTestRangeId = 0xE0000000; |
|
349 const TUint32 KMaxTestRangeId = 0xEFFFFFFF; |
|
350 |
|
351 const TUint32 KMaxProcessId = 0xFFFFFFFF; |
|
352 const TChar KLineFeed = '\n'; |
|
353 |
|
354 // read a line |
|
355 |
|
356 TPtr8 ptr( iLineBuffer->Des() ); |
|
357 iReadStream.ReadL( ptr, KLineFeed ); |
|
358 |
|
359 TUint32 tempId = 0; |
|
360 TInt16 tempOrigin = 0; |
|
361 |
|
362 // parse process id |
|
363 |
|
364 TLex8 parser( ptr ); |
|
365 TInt err = parser.Val( tempId, EHex ); |
|
366 if ( err == KErrGeneral ) |
|
367 { |
|
368 User::Leave( KErrEof ); |
|
369 } |
|
370 else if ( err != KErrNone ) |
|
371 { |
|
372 User::Leave( err ); // might be KErrOverflow |
|
373 } |
|
374 |
|
375 // parse origin |
|
376 |
|
377 parser.SkipSpace(); |
|
378 err = parser.Val( tempOrigin ); |
|
379 if ( err == KErrGeneral ) |
|
380 { |
|
381 User::Leave( KErrCorrupt ); |
|
382 } |
|
383 else if ( err != KErrNone ) |
|
384 { |
|
385 User::Leave( err ); // might be KErrOverflow |
|
386 } |
|
387 |
|
388 if ( tempOrigin < 0 ) |
|
389 { |
|
390 User::Leave( KErrCorrupt ); |
|
391 } |
|
392 if ( tempId >= KMinTestRangeId && |
|
393 tempId <= KMaxTestRangeId ) |
|
394 { |
|
395 User::Leave( KErrCorrupt ); |
|
396 } |
|
397 else if( tempId > KMaxProcessId ) |
|
398 { |
|
399 User::Leave( KErrCorrupt ); |
|
400 } |
|
401 |
|
402 // set results |
|
403 |
|
404 aProcessId = TUid::Uid( tempId ); |
|
405 aOrigin = TOrigin( tempOrigin ); |
|
406 } |
|
407 |
|
408 //----------------------------------------------------------------------------- |
|
409 // CProcessOriginMapper::WriteProcessOriginPairL() |
|
410 // Write one related process/origin pair to a file. |
|
411 //----------------------------------------------------------------------------- |
|
412 // |
|
413 void CProcessOriginMapper::WriteProcessOriginPairL( TUid& aProcessId, |
|
414 TOrigin& aOrigin ) |
|
415 { |
|
416 _LIT8( KDelimiter, "\t" ); |
|
417 _LIT8( KLineFeed, "\n" ); |
|
418 TPtr8 ptr( iLineBuffer->Des() ); |
|
419 ptr.Num( aProcessId.iUid ); |
|
420 iWriteStream.WriteL( ptr ); |
|
421 iWriteStream.WriteL( KDelimiter ); |
|
422 ptr.Num( aOrigin ); |
|
423 iWriteStream.WriteL( ptr ); |
|
424 iWriteStream.WriteL( KLineFeed ); |
|
425 } |
|
426 |