os/ossrv/genericopenlibs/openenvcore/backend/inc/sysif.h
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 1998-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:
    15 *
    16 */
    17 
    18 
    19 #ifndef SYSIF_H
    20 #define SYSIF_H
    21 #include <e32std.h>
    22 #include <e32base.h>
    23 #include <f32file.h>
    24 #include <es_sock.h>
    25 #include <c32comm.h>
    26 #include <sys/select.h>
    27 #include <spawn.h>
    28 #include <sys/types.h>
    29 #include <wchar.h>
    30 #include "ipcclient.h"
    31 #include <dirent.h>
    32 #include <sys/socket.h>
    33 #include <stdapis/net/if.h>
    34 #include <commdb.h>
    35 #include <commdbconnpref.h>
    36 #include <rpipe.h>
    37 
    38 #include<tz.h>
    39 #include <e32atomics.h>
    40 
    41 #ifdef SYMBIAN_OE_POSIX_SIGNALS
    42 #include "signalclient.h"
    43 #include "tsignalmessage.h"
    44 
    45 typedef void (*TSignalHandler)(int);
    46 typedef void (*TSignalAction)(int, siginfo_t *, void *);
    47 #endif
    48 
    49 /*
    50 @internalComponent
    51 */
    52 _LIT(KEstlibInit, "STDLIBS-INIT");
    53 
    54 _LIT(KFDTransferSvr,"FDTrnsfrSvr");
    55 //Forward declaration for references below
    56 class RPipe;
    57 class CFileDesTransferServer;
    58 class RFileDesTransferSession;
    59 class CFileDescBase;
    60 /* TODO: We don't have to do this in CFileTable. Move to methods */
    61 class CFileSocketDesc;
    62 class CSocketDesc;
    63 struct TChild
    64 /*
    65 @internalComponent
    66 */
    67 	{
    68 	TInt iFid;
    69 	TProcessId iPid;
    70 	RProcess iProc;
    71 
    72 #ifdef __X86GCC__
    73 	// The underlying data for a TProcessId is a TUint64. GCC raises an error if this is intialised to a negative value. Consider changing for all platforms.
    74 	TChild(TInt aFid = -1, TProcessId aPid = 0) : iFid(aFid), iPid(aPid)
    75 #else
    76 	TChild(TInt aFid = -1, TProcessId aPid = -1) : iFid(aFid), iPid(aPid)
    77 #endif //__X86GCC__
    78 		{
    79 		// nada
    80 		}
    81 
    82 #ifdef __X86GCC__
    83 	// The underlying data for a TProcessId is a TUint64. GCC raises an error if this is intialised to a negative value. Consider changing for all platforms.
    84 	TChild(RProcess &aProc, TProcessId aFid = 0) : iProc(aProc), iFid(aFid)
    85 #else
    86 	TChild(RProcess &aProc, TProcessId aFid = -1) : iProc(aProc), iFid(aFid)
    87 #endif //__X86GCC__
    88 		{
    89 		iPid = aProc.Id();
    90 		}
    91 
    92 
    93 	static TBool MatchByFid(const TChild& aChild1, const TChild& aChild2)
    94 		{
    95 		if (aChild1.iFid == aChild2.iFid)
    96 			{
    97 			return ETrue;
    98 			}
    99 		return EFalse;
   100 		}
   101 
   102 	static TBool MatchByPid(const TChild& aChild1, const TChild& aChild2)
   103 		{
   104 		if (aChild1.iPid == aChild2.iPid)
   105 			{
   106 			return ETrue;
   107 			}
   108 		return EFalse;
   109 		}
   110 	};
   111 
   112 class TCLSICleanup
   113 /*
   114 @internalComponent
   115 */
   116 	{
   117 public:
   118 	void StorePtrs(RHeap* aHeap, RFs* aFs, RSocketServ* aSs, RCommServ* aCs, RFastLock* aSsLock, RFastLock* aCsLock,RFastLock* aDefConnLock,RFastLock* aAESelectLock,RFastLock* aTzServerLock)
   119 		{
   120 		iHeap = aHeap;
   121 		iFs = aFs;
   122 		iSs = aSs;
   123 		iCs = aCs;
   124 		iSsLock = aSsLock;
   125 		iCsLock = aCsLock;
   126 		iDefConnLock = aDefConnLock;
   127 		iAESelectLock = aAESelectLock;
   128 		iTzServerLock = aTzServerLock;
   129 		}
   130 
   131 	~TCLSICleanup()
   132 		{
   133 		iFs->Close();
   134 		iSs->Close();
   135 		iSsLock->Close();
   136 		iCs->Close();
   137 		iCsLock->Close();
   138 		iHeap->Close();
   139 		iDefConnLock->Close();
   140 		iAESelectLock->Close();
   141 		iTzServerLock->Close();
   142 		}
   143 private:
   144 	RHeap* iHeap;
   145 	RFs* iFs;
   146 	RSocketServ* iSs;
   147 	RCommServ* iCs;
   148 	RFastLock* iSsLock;
   149 	RFastLock* iCsLock;
   150 	RFastLock* iDefConnLock;
   151 	RFastLock* iAESelectLock;
   152 	RFastLock* iTzServerLock;
   153 	};
   154 
   155 
   156 class TFileTableCleanup
   157 {
   158 public:
   159 	void SaveUserHeap(RHeap* aHeap)
   160 		{
   161 		iUserHeap = aHeap;
   162 		}
   163 
   164 	~TFileTableCleanup()
   165 	{
   166 		User::SwitchHeap(iUserHeap);
   167 	}
   168 
   169 private:
   170 	RHeap* iUserHeap;
   171 };
   172 
   173 
   174 class CFileTable // codescanner::missingcclass
   175 /*
   176 @internalComponent
   177 */
   178 	{
   179 public:
   180 	CFileTable();
   181 	~CFileTable();
   182 
   183 	TInt Init(RHeap* aHeap = NULL);
   184 	void Close();
   185 	void Default(CFileDescBase* aRedirMedia, CFileDescBase* aStdErrMedia);
   186 	TInt Reserve();
   187 	TInt Attach(TInt aFid, CFileDescBase* aFile);
   188 	TInt Detach(TInt aFid, CFileDescBase*& aDetached);
   189 	TInt At(TInt aFid, CFileDescBase*& aFound) const;
   190 	TInt Reserve(TInt aFids[3]);
   191 	TInt Detach(TInt aFids[3]);
   192 
   193 	int dup (int fid, int& anErrno);
   194 	int dup2 (int fid1, int fid2, int& anErrno);
   195 	int open (const wchar_t* name, int mode, int perms, int& anErrno);
   196 	int close (int fid, int& anErrno);
   197 
   198 	int UserClose (int fid, int& anErrno);
   199 
   200 	off_t lseek (int fid, off_t offset, int whence, int& anErrno);
   201 	int fstat (int fid, struct stat* st, int& anErrno);
   202 	int socket (int family, int style, int protocol, int& anErrno, RSocketServ& aSs);
   203 	int listen (int fd, int n, int& anErrno);
   204 	int bind (int fd, const struct sockaddr* addr, unsigned long size, int& anErrno);
   205 	int sockname (int fd, struct sockaddr* address, unsigned long* aAddrLen,int anEnd, int& anErrno);
   206 	int getsockopt (int fd, int level, int opt, void* buf, unsigned long* len, int& anErrno);
   207 	int setsockopt (int fd, int level, int opt, void* buf, unsigned long len, int& anErrno);
   208 	int ioctlcomplete (int fid, int cmd, void* param, TRequestStatus& aStatus, int& anErrno);
   209 	int ioctlcancel (int fid, int& anErrno);
   210 	int fcntl (int aFid, int aCmd, long anArg, int& anErrno);
   211 
   212 	TInt Asynch (int fid, CFileDescBase*& aFile);
   213 
   214 	int popen (const wchar_t* file, const wchar_t* cmd,const char* mode, int& anErrno);
   215 	int pclose (int aFid, int& anErrno);
   216 	int popen3 (const wchar_t *file, const wchar_t *cmd, wchar_t** envp, int fids[3], int& anErrno);
   217 	int system (const wchar_t* aCmd, const wchar_t* aCmdArg, int& anErrno);
   218 	int waitpid (int pid, int* status, int options, int& anErrno);
   219 
   220 	int posix_spawn (int* pid, const wchar_t* wpath,
   221 					 const posix_spawn_file_actions_t* file_actions,
   222 					 const posix_spawnattr_t* attrp,
   223 					 const wchar_t* wargs, wchar_t** wenvp);
   224 
   225 	void TransferToChild (RProcess& child, TInt aFds[], TInt aWhich, TInt aSlot);
   226 	void PassEnvToChild (RProcess& child, wchar_t** aEnvp);
   227 	void PassFileActionsToChild (RProcess& child,
   228 								 const posix_spawn_file_actions_t* aFileActions);
   229 
   230 #ifdef SYMBIAN_OE_POSIX_SIGNALS
   231 	void TransferSigMaskToChild(RProcess& childproc,TDes& sigmask);
   232 	void CheckOrigins (wchar_t**& wenvp, int& aCount, TUint64 & aSigMask);
   233 #else
   234 	void CheckOrigins (wchar_t**& wenvp, int& aCount);
   235 #endif
   236 	TInt InheritFiles(CFileDesTransferServer* server);
   237 	int pipe (int fildes[2], int& anErrno);
   238 	int mkfifo (const wchar_t *pathname, mode_t mode, int& anErrno, RFs& aFs);
   239 	int CreatePipeDesc (int fildes, RPipe& aHandle, TUint16 aMode = 0);
   240 	void ClosePipe (int fds[2], int& err);
   241 
   242 	static CFileDescBase* FifoOpen (const wchar_t* pathName, mode_t mode, int perms, int& anErrno);
   243 	int link (const wchar_t *oldpathName, const wchar_t *newpathName, int& anErrno, RFs& aFs);
   244 	static CFileDescBase* LinkOpen (const wchar_t* pathName, mode_t mode, int perms, int& anErrno, RFs& aFs);
   245 	int unlink (RFs& aFs, const wchar_t *pathName, int& anErrno);
   246 	int AcceptFileSocket(CFileSocketDesc* pfs, const struct sockaddr_un* addr, int& anErrno, RFs& aFs);
   247 	TInt RConnectionCount();
   248 	TInt RConnectionAt (TInt aRcIndex, RConnection *&aRc);
   249 	TInt AddRConnectionPtr (RConnection *aRc, TInt &aRcIndex);
   250 	void RemoveRConnectionPtrAt (TInt aRcIndex);
   251 	void DeleteRConnectionList();
   252 
   253 	CFileDesTransferServer* NewFDTransferServerL(RSemaphore &sem);
   254 	void StartFDTransferServer(CFileDesTransferServer* pServer);
   255 	void CloseFDTransferServer(CFileDesTransferServer* pServer);
   256 
   257 	int SetEcho(int aFd, TUint8 aEcho, int& aErrno);
   258 
   259 	void DoTransferAll(RSemaphore &sem,TDesC& name);
   260 	TInt GetFileCount() const
   261 		{
   262 		return iFids.Count();
   263 		}
   264 
   265 	void Lock()
   266 		{
   267 		iFidLock.Wait();
   268 		}
   269 
   270 	void Unlock()
   271 		{
   272 		iFidLock.Signal();
   273 		}
   274 
   275 private:
   276 	TFileTableCleanup	iCleanup;
   277 	CArrayPtrSeg<CFileDescBase> iFids;
   278 	// invoked by dup
   279 	TInt Dup(TInt aFid);
   280 	// invoked by both dup2 and fcntl with F_DUPFD
   281 	TInt Dup2(TInt aFid, TInt aFid2, TBool aCloseFid2);
   282 	// the real dup-er.
   283     TInt DupFd(TInt aFid1, TInt aFid2);
   284 	void Release(TInt aFid);
   285 
   286 	//Lock for protecting FIDs across threads
   287 	RFastLock iFidLock;
   288 
   289 	//stores pids of all children of this process
   290 	RArray<TChild> iChildren;
   291 
   292 	//Methods to manipulate iChildren
   293 	void AddChild(const TChild& aChild);
   294 	void RemoveChild(TInt aIdx);
   295 
   296 	void ExpandFTableL(TInt count);
   297 
   298 	const RArray<TChild>& GetChildren() const
   299 		{
   300 		return iChildren;
   301 		}
   302 
   303 	TInt FindChild(const TInt aFid) const
   304 		{
   305 		return iChildren.Find(TChild(aFid),
   306 							  TIdentityRelation<TChild>(TChild::MatchByFid));
   307 		}
   308 
   309 	TInt FindChild(const TProcessId aPid) const
   310 		{
   311 		return iChildren.Find(TChild(-1, aPid),
   312 							  TIdentityRelation<TChild>(TChild::MatchByPid));
   313 		}
   314 
   315 	CArrayPtrSeg<RConnection> iRConnectionList;
   316 	//Lock for protecting iRConnectionList across threads
   317 	RFastLock iRcLock;
   318 
   319 	// A pointer to the private heap
   320 	RHeap* iPrivateHeap;
   321 	friend class CFileDesTransferSession;
   322 	};
   323 
   324 
   325 class PosixFilesystem
   326 /*
   327 @internalComponent
   328 */
   329 	{
   330 public:
   331 	static wchar_t* getcwd(RFs& aFs, wchar_t* buf, unsigned long len, int& anErrno);
   332 	static int chdir(RFs& aFs, const wchar_t* path, int& anErrno);
   333 	static int mkdir(RFs& aFs, const wchar_t* path, int perms, int& anErrno);
   334 	static int rmdir(RFs& aFs, const wchar_t* path, int& anErrno);
   335 	static int chmod(RFs& aFs, const wchar_t* path, int perms, int& anErrno);
   336 	static int reg_unlink(RFs& aFs, const wchar_t* path, int& anErrno);
   337 	static int statbackend(RFs& aFs, const wchar_t* name, struct stat* st, int& anErrno);
   338 	static int rename(RFs& aFs, const wchar_t* oldname, const wchar_t* newname, int& anErrno);
   339 	static TInt ResolvePath(TParse& aResult, const wchar_t* path, TDes* aFilename);
   340 	static TInt SetDefaultDir(RFs& aFs);
   341 	static TInt CreateDefaultPrivatePath(RFs& aFs);
   342 	static int utime(RFs& aFs, const wchar_t* name, const struct utimbuf* filetimes, int& anErrno);
   343 	};
   344 
   345 
   346 class TAccessPointRecord
   347 	{
   348 public:	
   349 	TUint32 iId;
   350 	TBuf<KCommsDbSvrMaxColumnNameLength> iName;
   351 	TUint32 iDialogPref;
   352 	TUint32 iDirection;
   353 	TUint32 iService;
   354 	TBuf<KCommsDbSvrMaxColumnNameLength> iServiceType;
   355 	TUint32 iBearer;
   356 	TBuf<KCommsDbSvrMaxColumnNameLength> iBearerType;
   357 	TUint32 iNetwork;
   358 	TUint32 iNetworkWeighting;
   359 	TUint32 iLocation;
   360 public:
   361 	TAccessPointRecord();
   362 	
   363 	};
   364 
   365 // This structure encapsulates an aselect request
   366 NONSHARABLE_STRUCT ( TaselectRequestInfo )
   367 	{
   368 	TThreadId iId;
   369 	int iMaxFd;
   370 	fd_set* iReadFds;
   371 	fd_set* iWriteFds;
   372 	fd_set* iExceptFds;
   373 	struct timeval *itvptr;
   374 	TRequestStatus* iRequestStatus;
   375 	};
   376 
   377 // Holds the information about aselect requests, this used required
   378 // for cancellation of the aselect requests
   379 NONSHARABLE_CLASS ( TASelectRequest )
   380 	{
   381 public:
   382 	// Thread id of the thread processing the aselect
   383 	TThreadId iThreadId;
   384 	// Request object used for the request
   385 	TRequestStatus* iRequestStatus;
   386 	};
   387 
   388 NONSHARABLE_CLASS(CLocalSystemInterface) : public CBase
   389 /*
   390 @internalComponent
   391 */
   392 	{
   393 public:
   394 
   395 
   396 	IMPORT_C CLocalSystemInterface();
   397 	IMPORT_C ~CLocalSystemInterface();
   398 
   399 #ifdef __SYMBIAN_COMPILE_UNUSED__
   400 	CLocalSystemInterface& Clone();
   401 	void Release();
   402 	void TerminateProcess(int status);
   403 #endif //__SYMBIAN_COMPILE_UNUSED__
   404 
   405 	int AddToDirList(DIR *aDir);
   406 	int RemoveDirFromList(DIR* aDir);
   407 	int FindInDirList(DIR* aDir);
   408 
   409 	void Exit(int code);
   410 
   411 	int dup (int fid, int& anErrno);
   412 	int dup2 (int fid, int fid2, int& anErrno);
   413 	int open (const wchar_t* name, int mode, int perms, int& anErrno);
   414 	int read (int fid, char* buf, unsigned long len, int& anErrno);
   415 	int write (int fid, const char* buf, unsigned long len, int& anErrno);
   416 	int fsync (int fid, int& anErrno);
   417 	int close (int fid, int& anErrno);
   418 	off_t lseek (int fid, off_t offset, int whence, int& anErrno);
   419 	int fstat (int fid, struct stat* st, int& anErrno);
   420 	int ioctl (int fid, int cmd, void* param, int& anErrno);
   421 	int ioctl (int fid, int cmd, void* param, TRequestStatus& aStatus, int& anErrno);
   422 	int ioctl_complete (int fid, int cmd, void* param, TRequestStatus& aStatus, int& anErrno);
   423 
   424 #ifdef __SYMBIAN_COMPILE_UNUSED__
   425 	int ioctl_cancel (int fid, int& anErrno);
   426 #endif //__SYMBIAN_COMPILE_UNUSED__
   427 
   428 	wchar_t* getcwd (wchar_t* buf, unsigned long len, int& anErrno);
   429 
   430 	int chdir (const wchar_t* path, int& anErrno);
   431 	int mkdir (const wchar_t* path, int perms, int& anErrno);
   432 	int rmdir (const wchar_t* path, int& anErrno);
   433 	int chmod (const wchar_t* path, int perms, int& anErrno);
   434 	int stat (const wchar_t* name, struct stat* st, int& anErrno);
   435 	int lstat (const wchar_t* name, struct stat *st, int& anErrno);
   436 	int rename (const wchar_t* oldname, const wchar_t* newname, int& anErrno);
   437 
   438 	IMPORT_C TInt ResolvePath (TParse& aResult, const wchar_t* path, TDes* aFilename);
   439 	int socket (int family, int style, int protocol, int& anErrno);
   440 	int recvfrom (int fd, char* buf, unsigned long cnt, int flags, struct sockaddr* from, unsigned long* fromsize, int& anErrno);
   441 	int sendto (int fd, const char* buf, unsigned long cnt, int flags, struct sockaddr* to, unsigned long tosize, int& anErrno);
   442 	int shutdown (int fd, int how, int& anErrno);
   443 	int listen (int fd, int n, int& anErrno);
   444 	int accept (int fd, struct sockaddr *addr, size_t *size, int& anErrno);
   445 	int bind (int fd, const struct sockaddr* addr, unsigned long size, int& anErrno);
   446 	int connect (int fd, const struct sockaddr* addr, unsigned long size, int& anErrno);
   447 	int sockname (int fd, struct sockaddr* addr, unsigned long* size, int anEnd, int& anErrno);
   448 	int getsockopt (int fd, int level, int opt, void* buf, unsigned long* len, int& anErrno);
   449 	int setsockopt (int fd, int level, int opt, void* buf, unsigned long len, int& anErrno);
   450 
   451 	int popen3 (const wchar_t *file, const wchar_t *cmd, wchar_t** envp, int fids[3], int& anErrno);
   452 	int waitpid (int pid, int* status, int options, int& anErrno);
   453 
   454 	int fcntl (int aFid, int aCmd, long anArg, int& anErrno);
   455 	int utime (const wchar_t* name, const struct utimbuf* filetimes, int& anErrno);
   456 
   457 	int popen (const wchar_t* file, const wchar_t* cmd, const char* mode, int& anErrno);
   458 	int pclose (int aFid, int& anErrno);
   459 	IMPORT_C void CheckOrigins (wchar_t**& wenvp, int& aCount);
   460 #ifdef SYMBIAN_OE_POSIX_SIGNALS
   461 	TInt SignalHandler();
   462 	IMPORT_C void InitSignalHandler();
   463 	void RegisterExitNotification(TProcessId aPid);
   464 
   465 	inline TUint64 GetSigMask()const
   466 	{
   467 		return iSigMask;
   468 	}
   469 
   470 	int kill(pid_t pid, int sig, int& anErrno);
   471 	int raise(int sig, int& anErrno);
   472 	int sigqueue(pid_t pid, int sig, const union sigval val, int& anErrno);
   473 
   474 	int sigfillset(sigset_t *set, int& anErrno);
   475 	int sigaddset(sigset_t *set, int signo, int& anErrno);
   476 	int sigdelset(sigset_t *set, int signo, int& anErrno);
   477 	int sigismember(const sigset_t *set, int signo, int& anErrno);
   478 	int sigandset(sigset_t * set, const sigset_t * left, const sigset_t * right, int& anErrno);
   479 	int sigorset(sigset_t * set, const sigset_t * left, const sigset_t * right, int& anErrno);
   480 	int sigisemptyset(const sigset_t * set, int& anErrno);
   481 	int sigemptyset(sigset_t * set, int& anErrno);
   482 
   483 	int sigprocmask(int how, const sigset_t *set, sigset_t *oset, int& anErrno);
   484 	int sighold(int signo, int& anErrno);
   485 	int sigrelse(int signo, int& anErrno);
   486 	int sigpause(int signo, int& anErrno);
   487 
   488 	int sigwait(const sigset_t *set, int *sig, int& anErrno);
   489 	int sigtimedwait(const sigset_t *set, siginfo_t *info, const struct timespec *timeout, int& anErrno);
   490 	int sigwaitinfo(const sigset_t *set, siginfo_t *info, int& anErrno);
   491 	int sigaction(int sig, const struct sigaction *act, struct sigaction *oact, int& anErrno);
   492 	TSignalHandler sigset(int aSigNum, TSignalHandler disp, int& anErrno);
   493 	TSignalHandler bsd_signal(int aSigNum, TSignalHandler aFunc,int& anErrno);
   494 	TSignalHandler signal(int aSigNum, TSignalHandler aFunc,int& anErrno);
   495 	int sigignore(int aSigNum, int& anErrno);
   496 	unsigned int alarm(unsigned int seconds);
   497 	int sigpending(sigset_t *set,int& anErrno);
   498 
   499 #ifdef SYMBIAN_OE_LIBRT	
   500 	//start timers...
   501 	IMPORT_C TInt AddTimer(const TInt& aTimerId);
   502 	IMPORT_C TInt DeleteTimer(const TInt& aTimerId);
   503 	IMPORT_C TInt IncrementOverrun(const TInt& aTimerId);
   504 	IMPORT_C TInt ResetOverrun(const TInt& aTimerId);
   505 	IMPORT_C TInt Overrun(const TInt& aTimerId);
   506 	IMPORT_C TInt RaiseTimerSignal(int sig, int timerid);
   507 	//end timers
   508 #endif //SYMBIAN_OE_LIBRT
   509 #endif // SYMBIAN_OE_POSIX_SIGNALS
   510     int pipe (int fildes[2], int& anErrno );
   511     int mkfifo (const wchar_t *pathname, mode_t mode, int& anErrno);
   512     int link (const wchar_t *oldpathName, const wchar_t *newpathName, int& anErrno );
   513     int unlink (const wchar_t *pathName, int& anErrno );
   514 
   515 	int select (int maxfd, fd_set* readfds,fd_set* writefds,fd_set* exceptfds, struct timeval* tvptr, int& anErrno);
   516 
   517 	int aselect(int maxfd, fd_set *readfds, fd_set *writefds,fd_set *exceptfds, struct timeval *tvptr, TRequestStatus* requeststatus,int& anErrno);
   518 
   519 	int cancelaselect(TRequestStatus* requeststatus, int& anErrno, TBool perform_cleanup = EFalse);		
   520 		
   521 	int eselect(int maxfd, fd_set *readfds, fd_set *writefds,fd_set *exceptfds, struct timeval *tvptr, int arraycount,TRequestStatus* waitarray,int& anErrno);
   522 	
   523 	static TInt handleaselect(TAny* aPtr);
   524 	
   525 	RArray<TASelectRequest>& ASelectRequest();
   526 	
   527 	RFastLock& ASelectLock();
   528 	
   529 	inline RFastLock& DefConnLock() { return iDefConnLock; }
   530 	
   531 	inline TInt AddSocket(CSocketDesc* aPtr)
   532 	    {
   533         RHeap* oheap = User::SwitchHeap(iPrivateHeap);
   534 	    TInt ret = iSocketArray.InsertInAddressOrder(aPtr);
   535 	    User::SwitchHeap(oheap);	    
   536 	    return ret;
   537 	    }
   538 	
   539 	inline void RemoveSocket(CSocketDesc* aPtr)
   540 	    {
   541 	    TInt index = iSocketArray.FindInAddressOrder(aPtr);
   542 	    if (index != -1)
   543 	        {
   544 			RHeap* oheap = User::SwitchHeap(iPrivateHeap);
   545             iSocketArray.Remove(index);            
   546             iSocketArray.Compress();
   547             User::SwitchHeap(oheap);
   548 	        }
   549 	    }
   550 	
   551 	int system (const wchar_t* aCmd, const wchar_t* aCmdArg, int& anErrno );
   552 	const wchar_t* GetDirName (int aFid);
   553 	IMPORT_C int Truncate (int aFid, off_t anOffset, int& anErrno);
   554 	int fchmod (int fd , mode_t  perms, int& anErrno);
   555 
   556 	int posix_spawn (int* pid, const wchar_t* wpath,
   557 					 const posix_spawn_file_actions_t* file_actions,
   558 					 const posix_spawnattr_t* attrp,
   559 					 const wchar_t* wargs, wchar_t** wenvp);
   560 
   561 	int SetEcho(int aFd, TUint8 aEcho, int& aErrno);
   562 	
   563 	//Set the default interface for network operations
   564 	int setdefaultif(const struct ifreq* aIfReq);
   565 	int unsetdefaultif(TBool allow_bringup = ETrue);
   566 	
   567 	IMPORT_C CFileDescBase* GetDesc(int aFid);
   568 
   569 	// functions returning session to File Server
   570 	IMPORT_C RFs& FileSession();
   571     RCommServ& CommServSession();
   572     RSocketServ& SockServSession();
   573 
   574 	//Dynamic Memory related APIs
   575 	IMPORT_C void* Alloc(size_t aNBytes);
   576 	IMPORT_C void* ReAlloc(void* aPtr, size_t aNBytes);
   577 	IMPORT_C void Free(void* aPtr);
   578 	IMPORT_C void* AllocTLD(TInt aSize);
   579 
   580 	//Return the private Heap memory
   581 	IMPORT_C RHeap* Heap();
   582 
   583 	//Return the Comms Server Lock
   584 	RFastLock& CommsSessionLock();
   585 
   586 	//Return reference to file table
   587 	const CFileTable& FileTable() const;
   588 	
   589 	//Get the default connection (RConnection) instance 
   590 	RConnection& GetDefaultConnection();
   591 	
   592 	//Get the saved session path of the file server session
   593 	TInt GetSessionPath(TDes& aPath);
   594 	
   595 	//Save the file server session path
   596 	TInt SaveSessionPath(const TDesC& aPath);
   597 	
   598 	static void WaitForNRequest(TRequestStatus aStatusArray[], TInt aNum);
   599 
   600 private:
   601 	//Helper function to read a record from the IAP table
   602 	void ReadRecordFromIapTableL(CCommsDbTableView* aView, TAccessPointRecord &aRecord);
   603 	//Get the connection preferences from an IAP name
   604 	TInt GetConnectionPreferencesL(TBuf<KCommsDbSvrMaxColumnNameLength> aIapName, 
   605 			 					   TCommDbConnPref &aConnPref);
   606 
   607 	// Helper function used by "select" api. 
   608 	// The function returns "True", if an ioctl call has been made and it is to be waited upon.
   609 	// The function returns "False", (if an ioctl call is not made)/(an ioctl call was made but is not to be waited upon, and will get cancelled).	
   610 	TBool SelectPollIssueIoctl(fd_set** aInputFds, fd_set** aOutputFds, 
   611 			TRequestStatus* aReqArray, TInt *aReqFds, TInt aNumReqs, TInt& nDescriptorsSet) ;
   612 	TInt CreateaselectThread(int maxfd, fd_set* readfds, fd_set* writefds,
   613 				fd_set* exceptfds, struct timeval* tvptr, 
   614 				TRequestStatus* requeststatus, int& anErrno);
   615 	
   616 	// Helper function for the setdefaultif() API to restart the 
   617 	// default RConnection with the new settings.
   618 	TInt StartDefConnection();
   619 	
   620 	// Helper function for doing an on-demand connection to RTz server
   621 	TInt OnDemandTZServerConnection();
   622 private:
   623 	// NOTE: iCleanup should be the first member of CLSI, since iPrivateHeap
   624 	// will be destroyed from within iCleanup destructor.
   625 	TCLSICleanup	iCleanup;
   626 
   627 	CFileTable 		iFids;
   628 
   629 	//The backend file session for the application
   630 	RFs 			iFs;
   631 	//The current session path of the application.
   632 	TFileName 		iSessionPath;
   633 	//Lock to guard the session path
   634 	RFastLock 		iSessionPathLock;
   635 	
   636 	RSocketServ 	iSs;
   637 	RCommServ 		iCs;
   638 
   639 	//Lock for protecting Socket Session across threads
   640 	RFastLock 		iSSLock;
   641 	//Lock for protecting Comms Session across threads
   642 	RFastLock 		iCSLock;
   643 	
   644 	//Private Heap which will be used by the library for all memory allocation
   645 	RHeap* 			iPrivateHeap;
   646 
   647 	//used for Opendir
   648 	const static TInt KDirGran = 2;
   649 	RPointerArray<TAny> iOpenDirList;
   650 
   651 	//in class consts
   652 	const static TInt KTLDInfoListGran = 4;
   653 	const static TInt KPtrsGran = 2;
   654 
   655 	//Thread-TLS pointers mapping
   656 	class TTLDInfo
   657 		{
   658 	public:
   659 		TTLDInfo(TThreadId id, TAny* ptr) : iTid(id), iPtrs(CLocalSystemInterface::KPtrsGran)
   660 			{
   661 			iPtrs.Append(ptr);
   662 			}
   663 		void Close(RHeap* pHeap)
   664 			{
   665 			TInt nptrs = iPtrs.Count();
   666 			for (TInt j = 0; j < nptrs; ++j)
   667 				{
   668 				pHeap->Free(iPtrs[j]);
   669 				}
   670 			iPtrs.Close();
   671 			}
   672 		TThreadId iTid;
   673 		RPointerArray<TAny> iPtrs;
   674 		};
   675 
   676 	//Array of Thread-TLS pointers mappings
   677 	RArray<TTLDInfo> iTLDInfoList;
   678 
   679 	//Protect the array from concurrent access
   680 	RFastLock	iTLDListLock;
   681 	//The default connection to be used for all network apis. 
   682 	RConnection iDefConnection;
   683 	//Protect the iDefConnection from concurrent GetDefaultConnection calls
   684 	RFastLock   iDefConnLock;
   685 	//Calling unsetdefaultif(), instead of setdefaultif(NULL) ensures that the torn down
   686 	//connection is not brought back up again, by subsequent socket/network calls.
   687 	//This variable enables this facility. It is reset to default (ETrue) by setdefaultif(<pref>)
   688 	TBool iDefConnResurrect;
   689 	//Lock for protecting iASelectRequest across threads
   690 	RFastLock iASelectLock;
   691 		
   692 	// iASelectRequest holds the TASelectRequest objects of the aselect requests
   693 	RArray<TASelectRequest> iASelectRequest;
   694 	
   695 	// Default connection settings, set/cleared using setdefaultif
   696 	TConnPref* iDefConnPref;
   697     RTz     iTzServer;	
   698 	RFastLock iTzServerLock;
   699 	TBool	iIsRTzConnected;
   700     RPointerArray<CSocketDesc> iSocketArray;
   701 #ifdef SYMBIAN_OE_POSIX_SIGNALS
   702 	// Signal handler thread
   703 	RThread 				iSignalHandlerThread;
   704 	RPipe					iSignalReadPipe;
   705 	RPipe					iSignalWritePipe;
   706 	TBool					iSignalLoopRunning;
   707 	RSignalSession			iSignalSession;
   708 	TUint64					iSigMask;
   709 	RArray<TSignalMessage>	iBlockedSigArray;
   710 	RMutex					iBlockedSAMutex;
   711 	TRequestStatus			iPipeReadStatus;
   712 
   713 	// For thread(s) to wait till signals initialization happens
   714 	TInt					iSigInitWaitCount;
   715 	RSemaphore				iSigInitWaitSemaphore;
   716 	RMutex					iSigInitWaitMutex;
   717 	TBool					iSignalsInitialized;
   718 	
   719 	RSemaphore				iSignalInitSemaphore;
   720 
   721 	class TChildWaiter
   722 		{
   723 	public:
   724 		TChildWaiter()
   725 			{
   726 			iWaiterStatus = KRequestPending;
   727 			iWaiterPid = 0;
   728 			}
   729 
   730 		virtual ~TChildWaiter() {}
   731 
   732 		TRequestStatus iWaiterStatus;
   733 		TProcessId iWaiterPid;
   734 
   735 		static TBool MatchByPid(const TChildWaiter& aWaiter1, const TChildWaiter& aWaiter2)
   736 			{
   737 			if((aWaiter1.iWaiterPid == aWaiter2.iWaiterPid) != 0)
   738 				return ETrue;
   739 			else
   740 				return EFalse;
   741 			}
   742 
   743 		static TBool MatchNotPending(const TChildWaiter& aWaiter1, const TChildWaiter& aWaiter2)
   744 			{
   745 			if(aWaiter1.iWaiterStatus == KRequestPending || aWaiter2.iWaiterStatus == KRequestPending)
   746 				return EFalse;
   747 			else
   748 				return ETrue;
   749 			}
   750 		};
   751 
   752 	class TSignalActionInfo
   753 		{
   754 	public:
   755 		TSignalActionInfo()
   756 			{
   757 			iSigHandler = SIG_DFL;
   758 			iSigAction = 0;
   759 			iSigFlags = 0;
   760 			}
   761 
   762 		virtual ~TSignalActionInfo() {}
   763 
   764 		TSignalHandler 	iSigHandler;
   765 		TSignalAction	iSigAction;
   766 		TUint32			iSigFlags;
   767 		};
   768 
   769 	TFixedArray<TSignalActionInfo,SIGRTMAX> iSigActions;
   770 	RMutex          		iSigHandlerMutex;
   771 	RTimer 					iAlarmTimer;
   772     TTime 					iAlarmStartTime;
   773     TRequestStatus 			iAlarmStatus;
   774     TBool 					iIsTimerActive;
   775     TUint					iCurrentTimeout;
   776 
   777     void HandleSignal(TSignalMessage& aMsg, TBool aIsDequeued = EFalse);
   778 	void InitializeSignalHandlers();
   779 	void CallDefaultHandler(int aSigNum);
   780 	void WaitOrHandleSignal(TSignalMessage aMsg);
   781 	TInt SuspendOtherThreads();
   782 	TInt ResumeOtherThreads();
   783 	TInt EnsureSignalsInitialized();
   784 	TInt SigWaitInternal(TUint64 aWaitSet,TSignalMessage* aMsg,TInt aTimeout,TBool aTimeCheckFailed=EFalse);
   785 
   786 	class TSignalWaiter
   787 		{
   788 	public:
   789 		TSignalWaiter(TUint64 aWaitMask, TSignalMessage* aMsgPtr)
   790 			{
   791 			iSigWaitSemaphore.CreateLocal(0);
   792 			iSigWaitMessagePtr = aMsgPtr;
   793 			iSigWaitMask = aWaitMask;
   794 
   795 			RThread	lThread;
   796 			iSigWaitThreadId = lThread.Id();
   797 			lThread.Close();
   798 			}
   799 
   800 		virtual ~TSignalWaiter()
   801 			{
   802 			iSigWaitSemaphore.Close();
   803 			}
   804 
   805 		TUint64				iSigWaitMask;
   806 		RSemaphore			iSigWaitSemaphore;
   807 		TSignalMessage*		iSigWaitMessagePtr;
   808 		TThreadId			iSigWaitThreadId;
   809 
   810 		static TBool MatchByMask(const TSignalWaiter& aWaiter1, const TSignalWaiter& aWaiter2)
   811 			{
   812 			if((aWaiter1.iSigWaitMask & aWaiter2.iSigWaitMask) != 0)
   813 				return ETrue;
   814 			else
   815 				return EFalse;
   816 			}
   817 
   818 		static TBool MatchByThreadId(const TSignalWaiter& aWaiter1, const TSignalWaiter& aWaiter2)
   819 			{
   820 			if(aWaiter1.iSigWaitThreadId == aWaiter2.iSigWaitThreadId)
   821 				return ETrue;
   822 			else
   823 				return EFalse;
   824 			}
   825 		};
   826 
   827 	RArray<TSignalWaiter>	iSignalWaiterArray;
   828 	RArray<TChildWaiter>	iChildWaiterArray;
   829 	RMutex					iSignalWaiterMutex;
   830 
   831 #ifdef SYMBIAN_OE_LIBRT		
   832 	//timers..
   833 	TInt FindTimer(const TInt& aTimerId);
   834 	class TOverrun
   835 		{
   836 		public:
   837 			TOverrun(const TInt& aTimerId):iTimerId(aTimerId), Overruns(0) 
   838 				{
   839 				}
   840 
   841 		TInt iTimerId;
   842 		TInt Overruns;
   843 		};
   844 		
   845 	RArray<TOverrun> 		iTimerOverruns;
   846 	RMutex					iTimerOverrunsMutex;
   847 	//timers..
   848 #endif // SYMBIAN_OE_LIBRT		
   849 	
   850 #endif // SYMBIAN_OE_POSIX_SIGNALS
   851 public:
   852 
   853 	IMPORT_C RTz & TZServer(TInt& aStatus);
   854 	
   855 //ipc server session
   856 RIpcSession iIpcS;
   857 friend class RFileDesTransferSession;
   858 	};
   859 
   860 
   861 /*
   862 * Return global backend object to libraries
   863 */
   864 IMPORT_C CLocalSystemInterface* Backend();
   865 
   866 
   867 
   868 
   869 // LIBC-BACKEND specific Symbian Error Codes
   870 const TInt KErrMaxFdOpened = -1025;
   871 const TInt KErrDirectoryOpen = -1026;
   872 
   873 
   874 
   875 // Directory enumeration
   876 
   877 NONSHARABLE_STRUCT(__EPOC32_WDIR) : public CBase	// aka "class __EPOC32_WDIR with everything public"
   878 	{
   879 public:
   880 	__EPOC32_WDIR() {}
   881 	~__EPOC32_WDIR();
   882 
   883 	TInt Open();
   884 	TInt Open(const TDesC& aPath);
   885 	TInt Open(const wchar_t *_path,int*);
   886 	virtual TInt UpdateNarrow();
   887 	HBufC* iPath;
   888 	CDir* iEntries;
   889 	TInt iIndex;		// counting down, 0 means "finished"
   890 	struct wdirent iCurrent;
   891 	TInt iCount ;
   892 	TBuf16<KMaxFileName> iCurrentName;
   893 	/* *****************************************************************
   894 	Overloading new and delete operators so that they will
   895 	allocate and deallocare memory from/to the private heap of backend
   896 	********************************************************************/
   897 	inline TAny* operator new(TUint aSize, TAny* aBase) __NO_THROW
   898 		{
   899 		Mem::FillZ(aBase, aSize); return aBase;
   900 		}
   901 
   902 	inline TAny* operator new(TUint aSize) __NO_THROW
   903 		{
   904 		return Backend()->Alloc(aSize);
   905 		}
   906 
   907 	inline TAny* operator new(TUint aSize, TLeave)
   908 		{
   909 		TAny* ptr = Backend()->Alloc(aSize);
   910 		if (ptr == NULL)
   911 			{
   912 			User::Leave(KErrNoMemory);
   913 			}
   914 		return ptr;
   915 		}
   916 
   917 	inline TAny* operator new(TUint aSize, TUint aExtraSize) __NO_THROW
   918 		{
   919 		return Backend()->Alloc(aSize + aExtraSize);
   920 		}
   921 
   922 	inline TAny* operator new(TUint aSize, TLeave, TUint aExtraSize)
   923 		{
   924 		TAny* ptr = Backend()->Alloc(aSize + aExtraSize);
   925 		if (ptr == NULL)
   926 			{
   927 			User::Leave(KErrNoMemory);
   928 			}
   929 		return ptr;
   930 		}
   931 
   932 	inline void operator delete(TAny *aPtr) __NO_THROW
   933 		{
   934 		Backend()->Free( aPtr );
   935 		}
   936 	};
   937 
   938 NONSHARABLE_STRUCT(__EPOC32_DIR) : public __EPOC32_WDIR
   939 	{
   940 public:
   941 	__EPOC32_DIR(){}
   942 	~__EPOC32_DIR(){}
   943 
   944 	virtual TInt UpdateNarrow();
   945 	struct dirent iCurrentNarrow;
   946 	TBuf8<KMaxFileName> iCurrentNarrowName;
   947 	};
   948 
   949 NONSHARABLE_STRUCT (_iconv_t)
   950 	{
   951 	TUint toCode;
   952 	TUint fromCode;
   953 	};
   954 
   955 class TUSockAddr : public TSockAddr
   956 /*
   957 Utility class for converting struct sockaddr to and from EPOC32 TSockAddr
   958 @internalComponent
   959 */
   960 	{
   961 public:
   962 	TUSockAddr() : TSockAddr(), iError(0) {}
   963 
   964 	TUSockAddr(TAny* addr);			// constructor form of Prepare
   965 	IMPORT_C TUSockAddr(const TAny* addr, TUint len);	// constructor form of Set
   966 private:
   967 	void Prepare(TAny* addr);
   968 	void Set(const TAny* addr, TUint len);
   969 public:
   970 	IMPORT_C void Get(TAny* addr, unsigned long* len);
   971 public:
   972 	TInt iError; // To store error status of TUSockAddr functions, refers errno.h. Notebly not Symbian error codes.
   973 	};
   974 
   975 
   976 
   977 
   978 
   979 #endif //SYSIF_H
   980