|
1 // Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #include "SYSIF.H" |
|
17 #include "FDESC.H" |
|
18 #include "LTIME.H" |
|
19 #include "LPOSIX.H" |
|
20 #include <fcntl.h> |
|
21 #include <sys/errno.h> |
|
22 #include <sys/serial.h> |
|
23 |
|
24 // System Interface for thread-local form of STDLIB |
|
25 |
|
26 /** |
|
27 Two-phase constructor method to create a CLocalSystemInterface instance. |
|
28 */ |
|
29 CLocalSystemInterface* CLocalSystemInterface::NewL() |
|
30 { |
|
31 CLocalSystemInterface* self = new (ELeave) CLocalSystemInterface(); |
|
32 CleanupStack::PushL(self); |
|
33 self->ConstructL(); |
|
34 CleanupStack::Pop(self); |
|
35 return self; |
|
36 } |
|
37 |
|
38 /** |
|
39 Constructor. |
|
40 */ |
|
41 CLocalSystemInterface::CLocalSystemInterface() |
|
42 { |
|
43 } |
|
44 |
|
45 /** |
|
46 Second-phase constructor. |
|
47 Initialises some data members. |
|
48 This function leaves if any system-wide error happens. |
|
49 */ |
|
50 void CLocalSystemInterface::ConstructL() |
|
51 { |
|
52 iFids.InitL(); |
|
53 |
|
54 CTtyDesc* console=new CTtyDesc; |
|
55 User::LeaveIfNull(console); |
|
56 |
|
57 iFids.Default(console); |
|
58 console->Close(); |
|
59 |
|
60 User::LeaveIfError(iFs.Connect()); |
|
61 User::LeaveIfError(PosixFilesystem::SetDefaultDir(iFs)); |
|
62 } |
|
63 |
|
64 CLocalSystemInterface::~CLocalSystemInterface() |
|
65 // |
|
66 // Shut down all server connections in use |
|
67 // |
|
68 { |
|
69 iFids.Close(); |
|
70 iCs.Close(); |
|
71 iSs.Close(); |
|
72 iFs.Close(); |
|
73 } |
|
74 |
|
75 MSystemInterface& CLocalSystemInterface::Clone() |
|
76 { |
|
77 iUseCount++; |
|
78 return *this; |
|
79 } |
|
80 |
|
81 void CLocalSystemInterface::Release() |
|
82 { |
|
83 if (--iUseCount<0) |
|
84 delete this; |
|
85 } |
|
86 |
|
87 void CLocalSystemInterface::TerminateProcess(int status) |
|
88 { |
|
89 delete this; |
|
90 RProcess().Terminate(status); |
|
91 } |
|
92 |
|
93 |
|
94 // Simple layer over PosixFilesystem |
|
95 |
|
96 wchar_t * CLocalSystemInterface::getcwd (wchar_t* buf, unsigned long len, int& anErrno) |
|
97 { |
|
98 return PosixFilesystem::getcwd(iFs,buf,len,anErrno); |
|
99 } |
|
100 |
|
101 int CLocalSystemInterface::chdir (const wchar_t* aPath, int& anErrno) |
|
102 { |
|
103 return PosixFilesystem::chdir(iFs,aPath,anErrno); |
|
104 } |
|
105 |
|
106 int CLocalSystemInterface::rmdir (const wchar_t* aPath, int& anErrno) |
|
107 { |
|
108 return PosixFilesystem::rmdir(iFs,aPath,anErrno); |
|
109 } |
|
110 |
|
111 int CLocalSystemInterface::mkdir (const wchar_t* aPath, int perms, int& anErrno) |
|
112 { |
|
113 return PosixFilesystem::mkdir(iFs,aPath,perms,anErrno); |
|
114 } |
|
115 |
|
116 int CLocalSystemInterface::stat (const wchar_t* name, struct stat *st, int& anErrno) |
|
117 { |
|
118 return PosixFilesystem::stat(iFs,name,st,anErrno); |
|
119 } |
|
120 |
|
121 int CLocalSystemInterface::chmod (const wchar_t* name, int perms, int& anErrno) |
|
122 { |
|
123 return PosixFilesystem::chmod(iFs,name,perms,anErrno); |
|
124 } |
|
125 |
|
126 int CLocalSystemInterface::unlink (const wchar_t* name, int& anErrno) |
|
127 { |
|
128 return PosixFilesystem::unlink(iFs,name,anErrno); |
|
129 } |
|
130 |
|
131 int CLocalSystemInterface::rename (const wchar_t* oldname, const wchar_t* newname, int& anErrno) |
|
132 { |
|
133 return PosixFilesystem::rename(iFs,oldname,newname,anErrno); |
|
134 } |
|
135 |
|
136 |
|
137 TInt CLocalSystemInterface::ResolvePath (TParse& aResult, const wchar_t* path, TDes* aFilename) |
|
138 { |
|
139 return PosixFilesystem::ResolvePath(iFs, aResult, path, aFilename); |
|
140 } |
|
141 |
|
142 // Simple layer over CFileTable synchronous routines |
|
143 |
|
144 int CLocalSystemInterface::open (const wchar_t* name, int mode, int perms, int& anErrno) |
|
145 { |
|
146 //at this point we need to know if it is a serial port or file |
|
147 if ((L'C' == name[0]) && (L'O' == name[1]) && (L'M' == name[2]) && (L':' == name[4]) && ((name[3] >= L'1') && (name[3] <= L'9')) || |
|
148 (L'I' == name[0]) && (L'R' == name[1]) && (L'C' == name[2]) && (L'O' == name[3]) && (L'M' == name[4]) && (L':' == name[6]) && ((name[5] >= L'1') && (name[5] <= L'9'))) |
|
149 return iFids.open(name,mode,perms,anErrno, iCs); |
|
150 else |
|
151 return iFids.open(name,mode,perms,anErrno, iFs); |
|
152 } |
|
153 |
|
154 int CLocalSystemInterface::dup (int fid, int& anErrno) |
|
155 { |
|
156 return iFids.dup(fid,anErrno); |
|
157 } |
|
158 |
|
159 int CLocalSystemInterface::dup2 (int fid, int fid2, int& anErrno) |
|
160 { |
|
161 return iFids.dup2(fid,fid2,anErrno); |
|
162 } |
|
163 |
|
164 int CLocalSystemInterface::close (int fid, int& anErrno) |
|
165 { |
|
166 // return iFids.close(fid,anErrno); |
|
167 return iFids.userclose(fid,anErrno); |
|
168 } |
|
169 |
|
170 int CLocalSystemInterface::lseek (int fid, int offset, int whence, int& anErrno) |
|
171 { |
|
172 return iFids.lseek(fid,offset,whence,anErrno); |
|
173 } |
|
174 |
|
175 int CLocalSystemInterface::fstat (int fid, struct stat *st, int& anErrno) |
|
176 { |
|
177 return iFids.fstat(fid,st,anErrno); |
|
178 } |
|
179 |
|
180 int CLocalSystemInterface::socket (int family, int style, int protocol, int& anErrno) |
|
181 { |
|
182 return iFids.socket(family,style,protocol,anErrno,iSs); |
|
183 } |
|
184 |
|
185 int CLocalSystemInterface::listen (int fid, int n, int& anErrno) |
|
186 { |
|
187 return iFids.listen(fid,n,anErrno); |
|
188 } |
|
189 |
|
190 int CLocalSystemInterface::bind (int fid, struct sockaddr* addr, unsigned long size, int& anErrno) |
|
191 { |
|
192 TUSockAddr address(addr,size); |
|
193 return iFids.bind(fid,address,anErrno); |
|
194 } |
|
195 |
|
196 int CLocalSystemInterface::sockname (int fid, struct sockaddr* addr, unsigned long* size, int anEnd, int& anErrno) |
|
197 { |
|
198 TUSockAddr address(addr); |
|
199 TInt ret=iFids.sockname(fid,address,anEnd,anErrno); |
|
200 if (ret==0) |
|
201 address.Get(addr,size); |
|
202 return ret; |
|
203 } |
|
204 |
|
205 int CLocalSystemInterface::getsockopt (int fid, int level, int opt, void* buf, unsigned long* len, int& anErrno) |
|
206 { |
|
207 return iFids.getsockopt(fid,level,opt,buf,len,anErrno); |
|
208 } |
|
209 |
|
210 int CLocalSystemInterface::setsockopt (int fid, int level, int opt, void* buf, unsigned long len, int& anErrno) |
|
211 { |
|
212 return iFids.setsockopt(fid,level,opt,buf,len,anErrno); |
|
213 } |
|
214 |
|
215 wchar_t* CLocalSystemInterface::getenv (const wchar_t* name) |
|
216 { |
|
217 return iEnv.getenv(name); |
|
218 } |
|
219 |
|
220 void CLocalSystemInterface::unsetenv (const wchar_t* name) |
|
221 { |
|
222 iEnv.unsetenv(name); |
|
223 } |
|
224 |
|
225 int CLocalSystemInterface::setenv (const wchar_t* name, const wchar_t* value, int rewrite, int& anErrno) |
|
226 { |
|
227 return iEnv.setenv(name,value,rewrite,anErrno); |
|
228 } |
|
229 |
|
230 |
|
231 int CLocalSystemInterface::popen3 (const wchar_t *, const wchar_t *, const wchar_t *, wchar_t**, int [3], int& anErrno) |
|
232 { |
|
233 return MapError(KErrNotSupported,anErrno); |
|
234 } |
|
235 |
|
236 int CLocalSystemInterface::waitpid (int, int*, int, int& anErrno) |
|
237 { |
|
238 return MapError(KErrNotSupported,anErrno); |
|
239 } |
|
240 |
|
241 // Synchronous layer over CFileTable asynchronous routines |
|
242 |
|
243 int CLocalSystemInterface::read (int fid, char* buf, unsigned long len, int& anErrno) |
|
244 { |
|
245 CFileDescBase* f=0; |
|
246 TBool patchErr = EFalse; |
|
247 |
|
248 TInt err=iFids.Asynch(fid,f); |
|
249 if (!err) |
|
250 { |
|
251 TRequestStatus readStatus; |
|
252 TRequestStatus timerStatus(KRequestPending); |
|
253 RTimer theTimer; |
|
254 TBool timerRunning = EFalse; |
|
255 |
|
256 TPtr8 ptr((TText8 *)buf, len); |
|
257 |
|
258 if (f->TimedRead()) |
|
259 { |
|
260 TTimeIntervalMicroSeconds32 timeout(f->TimeoutValue()*1000); |
|
261 theTimer.CreateLocal(); |
|
262 theTimer.After(timerStatus, timeout); |
|
263 timerRunning = ETrue; |
|
264 } |
|
265 |
|
266 f->Read(ptr, readStatus); |
|
267 |
|
268 User::WaitForRequest(readStatus, timerStatus); |
|
269 |
|
270 if (timerRunning) |
|
271 { |
|
272 if (timerStatus.Int() != KRequestPending) |
|
273 { |
|
274 //cancel the read and wait for it |
|
275 f->ReadCancel(); |
|
276 patchErr = ETrue; //report this as a timeout not a cancel!! |
|
277 User::WaitForRequest(readStatus); |
|
278 } |
|
279 else |
|
280 { |
|
281 //if the timer was in operation |
|
282 //cancel the timer |
|
283 theTimer.Cancel(); |
|
284 User::WaitForRequest(timerStatus); |
|
285 } |
|
286 theTimer.Close(); |
|
287 } |
|
288 |
|
289 err=f->ReadCompletion(ptr, readStatus.Int()); |
|
290 f->Close(); // balances the Dup() in CFileTable::Asynch() |
|
291 if (err==0) |
|
292 return ptr.Length(); |
|
293 } |
|
294 if (patchErr) |
|
295 err = ETIMEDOUT; |
|
296 |
|
297 return MapError(err,anErrno); |
|
298 } |
|
299 |
|
300 int CLocalSystemInterface::write (int fid, const char* buf, unsigned long len, int& anErrno) |
|
301 { |
|
302 CFileDescBase* f=0; |
|
303 TInt err=iFids.Asynch(fid,f); |
|
304 if (!err) |
|
305 { |
|
306 TRequestStatus status; |
|
307 TPtr8 ptr((TText8 *)buf, len, len); |
|
308 f->Write(ptr,status); |
|
309 User::WaitForRequest(status); |
|
310 err=f->WriteCompletion(ptr, status.Int()); |
|
311 f->Close(); // balances the Dup() in CFileTable::Asynch() |
|
312 if (err==0) |
|
313 return ptr.Length(); |
|
314 } |
|
315 return MapError(err,anErrno); |
|
316 } |
|
317 |
|
318 int CLocalSystemInterface::recvfrom (int fid, char* buf, unsigned long len, int flags, struct sockaddr* from, unsigned long* fromsize, int& anErrno) |
|
319 { |
|
320 CFileDescBase* f=0; |
|
321 TInt err=iFids.Asynch(fid,f); |
|
322 if (!err) |
|
323 { |
|
324 TRequestStatus status; |
|
325 TPtr8 ptr((TText8 *)buf, len); |
|
326 TUSockAddr addr(from); |
|
327 f->RecvFrom(ptr,addr,flags,status); |
|
328 User::WaitForRequest(status); |
|
329 TInt ret=0; |
|
330 err=f->RecvFromCompletion(ret, status.Int()); |
|
331 f->Close(); // balances the Dup() in CFileTable::Asynch() |
|
332 if (err==0) |
|
333 { |
|
334 addr.Get(from,fromsize); |
|
335 return ptr.Length(); |
|
336 } |
|
337 } |
|
338 return MapError(err,anErrno); |
|
339 } |
|
340 |
|
341 int CLocalSystemInterface::sendto (int fid, const char* buf, unsigned long len, int flags, struct sockaddr* to, unsigned long tosize, int& anErrno) |
|
342 { |
|
343 CFileDescBase* f=0; |
|
344 TInt err=iFids.Asynch(fid,f); |
|
345 if (!err) |
|
346 { |
|
347 TRequestStatus status; |
|
348 TPtr8 ptr((TText8 *)buf, len, len); |
|
349 TUSockAddr addr(to,tosize); |
|
350 f->SendTo(ptr,addr,flags,status); |
|
351 User::WaitForRequest(status); |
|
352 err=f->SendToCompletion(ptr, status.Int()); |
|
353 f->Close(); // balances the Dup() in CFileTable::Asynch() |
|
354 if (err==0) |
|
355 return ptr.Length(); |
|
356 } |
|
357 return MapError(err,anErrno); |
|
358 } |
|
359 |
|
360 int CLocalSystemInterface::fsync (int fid, int& anErrno) |
|
361 { |
|
362 CFileDescBase* f=0; |
|
363 TInt err=iFids.Asynch(fid,f); |
|
364 if (!err) |
|
365 { |
|
366 TRequestStatus status; |
|
367 f->Sync(status); |
|
368 User::WaitForRequest(status); |
|
369 f->Close(); // balances the Dup() in CFileTable::Asynch() |
|
370 err=status.Int(); |
|
371 } |
|
372 return MapError(err,anErrno); |
|
373 } |
|
374 |
|
375 int CLocalSystemInterface::shutdown (int fid, int how, int& anErrno) |
|
376 { |
|
377 CFileDescBase* f=0; |
|
378 TInt err=iFids.Asynch(fid,f); |
|
379 if (!err) |
|
380 { |
|
381 TRequestStatus status; |
|
382 f->Shutdown(how,status); |
|
383 User::WaitForRequest(status); |
|
384 f->Close(); // balances the Dup() in CFileTable::Asynch() |
|
385 err=status.Int(); |
|
386 } |
|
387 return MapError(err,anErrno); |
|
388 } |
|
389 |
|
390 int CLocalSystemInterface::connect (int fid, struct sockaddr* addr, unsigned long size, int& anErrno) |
|
391 { |
|
392 CFileDescBase* f=0; |
|
393 TInt err=iFids.Asynch(fid,f); |
|
394 if (!err) |
|
395 { |
|
396 TRequestStatus status; |
|
397 TUSockAddr address(addr,size); |
|
398 f->Connect(address,status); |
|
399 User::WaitForRequest(status); |
|
400 f->Close(); // balances the Dup() in CFileTable::Asynch() |
|
401 err=status.Int(); |
|
402 } |
|
403 return MapError(err,anErrno); |
|
404 } |
|
405 |
|
406 int CLocalSystemInterface::accept (int fid, int& anErrno) |
|
407 // |
|
408 // The CSocketDesc performing the Accept is responsible for creating the new CSocketDesc |
|
409 // |
|
410 { |
|
411 CFileDescBase* f=0; |
|
412 TInt err=iFids.Asynch(fid,f); |
|
413 if (!err) |
|
414 { |
|
415 CSocketDesc* newf=0; |
|
416 int fd=iFids.Reserve(); |
|
417 err=fd; |
|
418 if (fd>=0) |
|
419 { |
|
420 TRequestStatus status; |
|
421 f->Accept(newf,status,iSs); |
|
422 CleanupStack::PushL(newf); |
|
423 User::WaitForRequest(status); |
|
424 f->Close(); // balances the Dup() in CFileTable::Asynch() |
|
425 err=status.Int(); |
|
426 if (!err) |
|
427 { |
|
428 err=iFids.Attach(fd,newf); |
|
429 if (!err) |
|
430 { |
|
431 CleanupStack::PopAndDestroy(newf); |
|
432 return fd; |
|
433 } |
|
434 } |
|
435 iFids.Attach(fd,0); // cancel the reservation |
|
436 CleanupStack::PopAndDestroy(newf); |
|
437 } |
|
438 } |
|
439 return MapError(err,anErrno); |
|
440 } |
|
441 |
|
442 int CLocalSystemInterface::ioctl (int fid, int cmd, void* param, int& anErrno) |
|
443 { |
|
444 TRequestStatus ioctlStatus; |
|
445 TInt err=ioctl(fid,cmd,param,ioctlStatus,anErrno); |
|
446 if (err==KErrNone) |
|
447 { |
|
448 User::WaitForRequest(ioctlStatus); |
|
449 err=ioctl_complete(fid,cmd,param,ioctlStatus,anErrno); |
|
450 } |
|
451 return err; |
|
452 } |
|
453 |
|
454 // C++ version of asynchronous ioctl |
|
455 |
|
456 int CLocalSystemInterface::ioctl (int fid, int cmd, void* param, TRequestStatus& aStatus, int& anErrno) |
|
457 { |
|
458 CFileDescBase* f=0; |
|
459 TInt err=iFids.Asynch(fid,f); |
|
460 if (!err) |
|
461 { |
|
462 f->Ioctl(cmd,param,aStatus); |
|
463 f->Close(); // balances the Dup() in CFileTable::Asynch() - live dangerously! |
|
464 } |
|
465 return MapError(err,anErrno); |
|
466 } |
|
467 |
|
468 int CLocalSystemInterface::ioctl_complete (int fid, int cmd, void* param, TRequestStatus& aStatus, int& anErrno) |
|
469 { |
|
470 return iFids.ioctlcomplete(fid,cmd,param,aStatus,anErrno); |
|
471 } |
|
472 |
|
473 int CLocalSystemInterface::ioctl_cancel (int fid, int& anErrno) |
|
474 { |
|
475 return iFids.ioctlcancel(fid,anErrno); |
|
476 } |