os/ossrv/genericopenlibs/cstdlib/INC/POSIXIF.H
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/INC/POSIXIF.H	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,361 @@
     1.4 +// Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include "SYSIF.H"
    1.20 +#include <estlib.h>
    1.21 +#include "FDESC.H"
    1.22 +#include "PIPEDESC.H"
    1.23 +#include <stdlib.h>
    1.24 +
    1.25 +/**
    1.26 +@internalComponent
    1.27 +*/
    1.28 +const TInt KCPosixMajorVersionNumber=2;
    1.29 +const TInt KCPosixMinorVersionNumber=0;
    1.30 +/**
    1.31 +@internalComponent
    1.32 +*/
    1.33 +enum TPosixServerPanic 
    1.34 +	{
    1.35 +	EPosix_BadWaitCompletion=1,
    1.36 +	EPosix_NoPendingIO=2,
    1.37 +	};
    1.38 +/**
    1.39 +@internalComponent
    1.40 +*/
    1.41 +enum PosixMessage {
    1.42 +	// asynchronous operations which need SetActive/RunL
    1.43 +	PMread, PMwrite, PMfsync, 
    1.44 +	PMsendto, PMrecvfrom, PMconnect, PMshutdown, PMaccept,
    1.45 +//NOTIFY PROBLEM
    1.46 +//	PMioctl, 
    1.47 +	PMioctl, PMioctlN,
    1.48 +	// asynchronous operations which just delay completion
    1.49 +	PMwaitpid,
    1.50 +	// synchronous operations
    1.51 +	PMdup, PMdup2,
    1.52 +	PMopen, PMclose, PMlseek, PMfstat,
    1.53 +	PMgetcwd,
    1.54 +	PMchdir, PMmkdir, PMrmdir, 
    1.55 +	PMchmod, PMunlink, PMstat, PMrename,
    1.56 +	PMResolvePath,
    1.57 +	PMsocket, PMbind, PMlisten, PMsockname, PMgetsockopt, PMsetsockopt,
    1.58 +	PMioctlcomplete, PMcancel,
    1.59 +	PMTerminateProcess,
    1.60 +	PMgetenv, PMsetenv, PMunsetenv,
    1.61 +	PMpopen3, 
    1.62 +	// inter-process operations
    1.63 +	PMAreYouMyMother, PMHelloMum, PMPipeWrite, PMPipeRead, PMPipeIoctl, PMPipeClose, PMPipeCancel
    1.64 +	};
    1.65 +/**
    1.66 +@internalComponent
    1.67 +*/
    1.68 +struct PosixParams
    1.69 +	{
    1.70 +	int ret;
    1.71 +	int fid;
    1.72 +	int pint[3];
    1.73 +	const char* cptr[2];
    1.74 +	char* ptr[2];
    1.75 +	const wchar_t* cwptr[2];
    1.76 +	wchar_t* wptr[2];
    1.77 +	wchar_t** eptr[1];
    1.78 +	unsigned long len[1];
    1.79 +	unsigned long* lenp[1];
    1.80 +	TUSockAddr addr;
    1.81 +	};
    1.82 +
    1.83 +/**
    1.84 +Package which holds client's process Id.
    1.85 +@internalComponent
    1.86 +*/
    1.87 +typedef TPckgBuf<TInt> TPosixIPCPid;
    1.88 +
    1.89 +/**
    1.90 +Package used by server to pass data to client.
    1.91 +@internalComponent
    1.92 +*/
    1.93 +struct PosixIPCReply
    1.94 +	{
    1.95 +	TUint iVarCount;
    1.96 +	TUint iEnvironmentSize;
    1.97 +	TUint iWorkingDirectorySize;
    1.98 +	TUint iPipeMask;
    1.99 +	};
   1.100 +/**
   1.101 +@internalComponent
   1.102 +*/
   1.103 +typedef TPckgBuf<PosixIPCReply> TPosixIPCReply;
   1.104 +
   1.105 +
   1.106 +//
   1.107 +class RPosixSession : public RSessionBase
   1.108 +/**
   1.109 +@internalComponent
   1.110 +*/
   1.111 +	{
   1.112 +public:
   1.113 +	TInt Connect();
   1.114 +	TInt Connect(TDesC& aServerName);
   1.115 +	
   1.116 +	// for specific posix functions
   1.117 +	int Request(TInt aFunction, int& anErrno, PosixParams& params) const;
   1.118 +	void Request(TInt aFunction, int& anErrno, PosixParams& params, TRequestStatus& aStatus) const;
   1.119 +	// for generic messages
   1.120 +	TInt Request(TInt aFunction, const TIpcArgs& aArg) const;
   1.121 +	void Request(TInt aFunction, const TIpcArgs& aArg, TRequestStatus &aStatus) const;
   1.122 +	
   1.123 +	static TVersion Version();
   1.124 +	};
   1.125 +
   1.126 +
   1.127 +//
   1.128 +class CPosixRequest;
   1.129 +class CPosixServer;
   1.130 +class CPosixIPCSession;
   1.131 +NONSHARABLE_CLASS(CPosixProcess) : public CActive
   1.132 +/**
   1.133 +@internalComponent
   1.134 +*/
   1.135 +	{
   1.136 +public:
   1.137 +	CPosixProcess(CPosixServer& aServer);
   1.138 +	~CPosixProcess();
   1.139 +	void POpen3L(PosixParams* aParams);
   1.140 +	static CPosixProcess* Find(CPosixProcess* head, TInt pid);
   1.141 +	static void Release(CPosixProcess** aHead, CPosixProcess* aProc);
   1.142 +	void Sizes(TPosixIPCReply& aReply) const;
   1.143 +	void CopyToChildL(const RMessage2& aMessage);
   1.144 +	inline TInt IsAlive() const {return IsActive();}
   1.145 +	inline void Queue(CPosixRequest* aWaiter); 
   1.146 +protected:
   1.147 +	void RunL();
   1.148 +	void DoCancel();
   1.149 +
   1.150 +private:
   1.151 +	inline CEnvironment& Env() const;
   1.152 +	inline CFileTable& Fids() const;
   1.153 +	inline RFs& Fs() const;
   1.154 +	inline RCommServ& Cs() const;
   1.155 +
   1.156 +public:
   1.157 +	CPosixProcess* iNextProcess;
   1.158 +	TInt iPid;
   1.159 +	TInt iExitReason;
   1.160 +private:
   1.161 +	CPosixServer& iServer;
   1.162 +
   1.163 +	RProcess iChild;
   1.164 +	CPosixRequest* iWaiters;
   1.165 +
   1.166 +	TUint iVarCount;
   1.167 +	HBufC16* iEnvironment;
   1.168 +	HBufC* iWorkingDirectory;
   1.169 +	CPipeDesc* iPipes[3];
   1.170 +	};
   1.171 +//
   1.172 +class CPosixRequest;
   1.173 +
   1.174 +/**
   1.175 +@internalComponent
   1.176 +*/
   1.177 +NONSHARABLE_CLASS(CPosixServer) : public CServer2
   1.178 +	{
   1.179 +public:
   1.180 +	static TInt ThreadFunction(TAny* aPtr);
   1.181 +	static void InitL(TInt aPriority);
   1.182 +	RFs& Fs()           { return iFs; }
   1.183 +	RCommServ& Cs()     { return iCs; }
   1.184 +	CFileTable& Fids()  { return iFids; }
   1.185 +	RSocketServ& Ss()   { return iSs; }
   1.186 +	CEnvironment& Env() { return iEnv; }
   1.187 +
   1.188 +	int POpen3(PosixParams* aParams, int& anErrno);
   1.189 +	CPosixProcess* Child(TInt pid)	{ return CPosixProcess::Find(iChildren, pid); }
   1.190 +	void Release(CPosixProcess* aChild) { CPosixProcess::Release(&iChildren, aChild); }
   1.191 +	inline void WaitForAnyChild(CPosixRequest* aWaiter);
   1.192 +	CPosixRequest* Waiters();
   1.193 +	static void ServerPanic(TPosixServerPanic aPanic);
   1.194 +
   1.195 +protected:
   1.196 +	CPosixServer(TInt aPriority);
   1.197 +	CSession2* NewSessionL(const TVersion& aVersion, const RMessage2& aMessage) const;
   1.198 +	void FindParentL();
   1.199 +	void DefaultConsoleL();
   1.200 +private:
   1.201 +	RFs iFs;
   1.202 +	CFileTable iFids;
   1.203 +	RSocketServ iSs;
   1.204 +	RCommServ	iCs;
   1.205 +
   1.206 +	CEnvironment iEnv;
   1.207 +	RPosixSession iParent;
   1.208 +	CPosixProcess* iChildren;
   1.209 +	CPosixRequest* iWaitAnyQueue;
   1.210 +	};
   1.211 +
   1.212 +//
   1.213 +class CPosixSession;
   1.214 +NONSHARABLE_CLASS(CPosixRequest) : public CActive
   1.215 +/**
   1.216 +@internalComponent
   1.217 +*/
   1.218 +	{
   1.219 +public:
   1.220 +	CPosixRequest(CPosixServer& aServer);
   1.221 +	~CPosixRequest();
   1.222 +	void Service(const RMessage2& aMessage);
   1.223 +	void EnList(CPosixRequest*& aHead);	// simple single linked list
   1.224 +	void WaitCompleted(TInt aPid, TInt aReason);
   1.225 +protected:
   1.226 +	void RunL();
   1.227 +	void DoCancel();
   1.228 +private:
   1.229 +	RFs& Fs() const           { return iServer.Fs(); }
   1.230 +	RCommServ& Cs() const           { return iServer.Cs(); }
   1.231 +	CFileTable& Fids() const  { return iServer.Fids(); }
   1.232 +	RSocketServ& Ss() const   { return iServer.Ss(); }
   1.233 +	CEnvironment& Env() const { return iServer.Env(); }
   1.234 +private:
   1.235 +	void QueueAsynch(const RMessage2& aMessage);
   1.236 +	void StartAsynch();
   1.237 +	void EndAsynch(TInt aResult);
   1.238 +
   1.239 +	CPosixServer& iServer;
   1.240 +
   1.241 +	RMessage2 iMessage;
   1.242 +
   1.243 +	CFileDescBase* iFile;
   1.244 +	CSocketDesc* iNewF;
   1.245 +	int iNewFid;
   1.246 +	TPtr8 iPtr;
   1.247 +
   1.248 +	CPosixRequest* iNext;	// for Enlist
   1.249 +	TSglQueLink iLink;
   1.250 +	enum CFileDescBase::IOQueues iQueue;
   1.251 +
   1.252 +	friend class TPosixRequestQueue;
   1.253 +	friend class CFileDescBase;		// friend functions AddLast & Remove perhaps? 
   1.254 +	};
   1.255 +//
   1.256 +inline CEnvironment& CPosixProcess::Env() const { return iServer.Env(); }
   1.257 +inline CFileTable& CPosixProcess::Fids() const  { return iServer.Fids(); }
   1.258 +inline RFs& CPosixProcess::Fs() const           { return iServer.Fs(); }
   1.259 +inline RCommServ& CPosixProcess::Cs() const           { return iServer.Cs(); }
   1.260 +inline void CPosixProcess::Queue(CPosixRequest* aWaiter)          { aWaiter->EnList(iWaiters); }
   1.261 +inline void CPosixServer::WaitForAnyChild(CPosixRequest* aWaiter) { aWaiter->EnList(iWaitAnyQueue); }
   1.262 +
   1.263 +//
   1.264 +/**
   1.265 +@internalComponent
   1.266 +*/
   1.267 +NONSHARABLE_CLASS(CPosixSession) : public CSession2
   1.268 +	{
   1.269 +public:
   1.270 +	CPosixSession(CPosixServer& aServer);
   1.271 +	virtual void ServiceL(const RMessage2& aMessage);
   1.272 +protected:
   1.273 +	CPosixRequest iActive;
   1.274 +	};
   1.275 +
   1.276 +NONSHARABLE_CLASS(CPosixIPCSession) : public CPosixSession
   1.277 +/**
   1.278 +@internalComponent
   1.279 +*/
   1.280 +	{
   1.281 +public:
   1.282 +	inline CPosixIPCSession(CPosixServer& aServer) 
   1.283 +		: CPosixSession(aServer) {}
   1.284 +	virtual void ServiceL(const RMessage2& aMessage);	 // override the local ServiceL
   1.285 +	void SetPipes(CPipeDesc* aPipes[3]);
   1.286 +	~CPosixIPCSession();
   1.287 +
   1.288 +private:
   1.289 +	TInt AreYouMyMotherL(const RMessage2& aMessage);
   1.290 +	TInt HelloMumL(const RMessage2& aMessage);
   1.291 +	void PipeRead(const RMessage2& aMessage);
   1.292 +	void PipeWrite(const RMessage2& aMessage);
   1.293 +	void PipeIoctl(const RMessage2& aMessage);
   1.294 +	void PipeClose(const RMessage2& aMessage);
   1.295 +	void PipeCancel(const RMessage2& aMessage);
   1.296 +
   1.297 +	CPipeDesc* iPipes[3];
   1.298 +	};
   1.299 +//
   1.300 +NONSHARABLE_CLASS(CProcessSystemInterface) : public MSystemInterface, public CBase
   1.301 +/**
   1.302 +@internalComponent
   1.303 +*/
   1.304 +	{
   1.305 +public:
   1.306 +	CProcessSystemInterface();
   1.307 +	~CProcessSystemInterface();
   1.308 +
   1.309 +	TInt Connect();
   1.310 +	virtual MSystemInterface& Clone();
   1.311 +	virtual void Release();
   1.312 +	virtual void TerminateProcess(int status);
   1.313 +
   1.314 +	virtual int dup (int fid, int& anErrno);
   1.315 +	virtual int dup2 (int fid, int fid2, int& anErrno);
   1.316 +	virtual int open (const wchar_t* name, int mode, int perms, int& anErrno);
   1.317 +	virtual int read (int fid, char* buf, unsigned long len, int& anErrno);
   1.318 +	virtual int write (int fid, const char* buf, unsigned long len, int& anErrno);
   1.319 +	virtual int fsync (int fid, int& anErrno);
   1.320 +	virtual int close (int fid, int& anErrno);
   1.321 +	virtual int lseek (int fid, int offset, int whence, int& anErrno);
   1.322 +	virtual int fstat (int fid, struct stat *st, int& anErrno);
   1.323 +	virtual int ioctl (int fid, int cmd, void* param, int& anErrno);
   1.324 +	virtual int ioctl (int fid, int cmd, void* param, TRequestStatus& aStatus, int& anErrno);
   1.325 +	virtual int ioctl_complete (int fid, int cmd, void* param, TRequestStatus& aStatus, int& anErrno);
   1.326 +	virtual int ioctl_cancel (int fid, int& anErrno);
   1.327 +
   1.328 +	virtual wchar_t * getcwd (wchar_t * buf, unsigned long len, int& anErrno);
   1.329 +
   1.330 +	virtual int chdir (const wchar_t* path, int& anErrno);
   1.331 +	virtual int mkdir (const wchar_t* path, int perms, int& anErrno);
   1.332 +	virtual int rmdir (const wchar_t* path, int& anErrno);
   1.333 +	virtual int chmod (const wchar_t* path, int perms, int& anErrno);
   1.334 +	virtual int unlink (const wchar_t* path, int& anErrno);
   1.335 +	virtual int stat (const wchar_t* name, struct stat *st, int& anErrno);
   1.336 +	virtual int rename (const wchar_t* oldname, const wchar_t* newname, int& anErrno);
   1.337 +
   1.338 +	virtual TInt ResolvePath (TParse& aResult, const wchar_t* path, TDes* aFilename);
   1.339 +
   1.340 +	virtual int socket (int family, int style, int protocol, int& anErrno);
   1.341 +	virtual int recvfrom (int fd, char* buf, unsigned long cnt, int flags, struct sockaddr* from, unsigned long* fromsize, int& anErrno);
   1.342 +	virtual int sendto (int fd, const char* buf, unsigned long cnt, int flags, struct sockaddr* to, unsigned long tosize, int& anErrno);
   1.343 +	virtual int shutdown (int fd, int how, int& anErrno);
   1.344 +	virtual int listen (int fd, int n, int& anErrno);
   1.345 +	virtual int accept (int fd, int& anErrno);
   1.346 +	virtual int bind (int fd, struct sockaddr* addr, unsigned long size, int& anErrno);
   1.347 +	virtual int connect (int fd, struct sockaddr* addr, unsigned long size, int& anErrno);
   1.348 +	virtual int sockname (int fd, struct sockaddr* addr, unsigned long* size, int anEnd, int& anErrno);
   1.349 +	virtual int getsockopt (int fd, int level, int opt, void* buf, unsigned long* len, int& anErrno);
   1.350 +	virtual int setsockopt (int fd, int level, int opt, void* buf, unsigned long len, int& anErrno);
   1.351 +
   1.352 +	virtual wchar_t* getenv (const wchar_t* name); 
   1.353 +	virtual void unsetenv (const wchar_t* name);
   1.354 +	virtual int setenv (const wchar_t* name, const wchar_t* value, int rewrite, int& anErrno);
   1.355 +
   1.356 +	virtual int popen3 (const wchar_t *file, const wchar_t *cmd, const wchar_t *mode, wchar_t** envp, int fids[3], int& anErrno);
   1.357 +	virtual int waitpid (int pid, int* status, int options, int& anErrno);
   1.358 +
   1.359 +private:
   1.360 +	int Request(TInt aFunction, int& anErrno);
   1.361 +	void Request(TInt aFunction, int& anErrno, TRequestStatus& aStatus);
   1.362 +	RPosixSession iSession;
   1.363 +	PosixParams iParams;
   1.364 +	};