os/ossrv/genericopenlibs/cstdlib/INC/POSIXIF.H
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
#include "SYSIF.H"
sl@0
    17
#include <estlib.h>
sl@0
    18
#include "FDESC.H"
sl@0
    19
#include "PIPEDESC.H"
sl@0
    20
#include <stdlib.h>
sl@0
    21
sl@0
    22
/**
sl@0
    23
@internalComponent
sl@0
    24
*/
sl@0
    25
const TInt KCPosixMajorVersionNumber=2;
sl@0
    26
const TInt KCPosixMinorVersionNumber=0;
sl@0
    27
/**
sl@0
    28
@internalComponent
sl@0
    29
*/
sl@0
    30
enum TPosixServerPanic 
sl@0
    31
	{
sl@0
    32
	EPosix_BadWaitCompletion=1,
sl@0
    33
	EPosix_NoPendingIO=2,
sl@0
    34
	};
sl@0
    35
/**
sl@0
    36
@internalComponent
sl@0
    37
*/
sl@0
    38
enum PosixMessage {
sl@0
    39
	// asynchronous operations which need SetActive/RunL
sl@0
    40
	PMread, PMwrite, PMfsync, 
sl@0
    41
	PMsendto, PMrecvfrom, PMconnect, PMshutdown, PMaccept,
sl@0
    42
//NOTIFY PROBLEM
sl@0
    43
//	PMioctl, 
sl@0
    44
	PMioctl, PMioctlN,
sl@0
    45
	// asynchronous operations which just delay completion
sl@0
    46
	PMwaitpid,
sl@0
    47
	// synchronous operations
sl@0
    48
	PMdup, PMdup2,
sl@0
    49
	PMopen, PMclose, PMlseek, PMfstat,
sl@0
    50
	PMgetcwd,
sl@0
    51
	PMchdir, PMmkdir, PMrmdir, 
sl@0
    52
	PMchmod, PMunlink, PMstat, PMrename,
sl@0
    53
	PMResolvePath,
sl@0
    54
	PMsocket, PMbind, PMlisten, PMsockname, PMgetsockopt, PMsetsockopt,
sl@0
    55
	PMioctlcomplete, PMcancel,
sl@0
    56
	PMTerminateProcess,
sl@0
    57
	PMgetenv, PMsetenv, PMunsetenv,
sl@0
    58
	PMpopen3, 
sl@0
    59
	// inter-process operations
sl@0
    60
	PMAreYouMyMother, PMHelloMum, PMPipeWrite, PMPipeRead, PMPipeIoctl, PMPipeClose, PMPipeCancel
sl@0
    61
	};
sl@0
    62
/**
sl@0
    63
@internalComponent
sl@0
    64
*/
sl@0
    65
struct PosixParams
sl@0
    66
	{
sl@0
    67
	int ret;
sl@0
    68
	int fid;
sl@0
    69
	int pint[3];
sl@0
    70
	const char* cptr[2];
sl@0
    71
	char* ptr[2];
sl@0
    72
	const wchar_t* cwptr[2];
sl@0
    73
	wchar_t* wptr[2];
sl@0
    74
	wchar_t** eptr[1];
sl@0
    75
	unsigned long len[1];
sl@0
    76
	unsigned long* lenp[1];
sl@0
    77
	TUSockAddr addr;
sl@0
    78
	};
sl@0
    79
sl@0
    80
/**
sl@0
    81
Package which holds client's process Id.
sl@0
    82
@internalComponent
sl@0
    83
*/
sl@0
    84
typedef TPckgBuf<TInt> TPosixIPCPid;
sl@0
    85
sl@0
    86
/**
sl@0
    87
Package used by server to pass data to client.
sl@0
    88
@internalComponent
sl@0
    89
*/
sl@0
    90
struct PosixIPCReply
sl@0
    91
	{
sl@0
    92
	TUint iVarCount;
sl@0
    93
	TUint iEnvironmentSize;
sl@0
    94
	TUint iWorkingDirectorySize;
sl@0
    95
	TUint iPipeMask;
sl@0
    96
	};
sl@0
    97
/**
sl@0
    98
@internalComponent
sl@0
    99
*/
sl@0
   100
typedef TPckgBuf<PosixIPCReply> TPosixIPCReply;
sl@0
   101
sl@0
   102
sl@0
   103
//
sl@0
   104
class RPosixSession : public RSessionBase
sl@0
   105
/**
sl@0
   106
@internalComponent
sl@0
   107
*/
sl@0
   108
	{
sl@0
   109
public:
sl@0
   110
	TInt Connect();
sl@0
   111
	TInt Connect(TDesC& aServerName);
sl@0
   112
	
sl@0
   113
	// for specific posix functions
sl@0
   114
	int Request(TInt aFunction, int& anErrno, PosixParams& params) const;
sl@0
   115
	void Request(TInt aFunction, int& anErrno, PosixParams& params, TRequestStatus& aStatus) const;
sl@0
   116
	// for generic messages
sl@0
   117
	TInt Request(TInt aFunction, const TIpcArgs& aArg) const;
sl@0
   118
	void Request(TInt aFunction, const TIpcArgs& aArg, TRequestStatus &aStatus) const;
sl@0
   119
	
sl@0
   120
	static TVersion Version();
sl@0
   121
	};
sl@0
   122
sl@0
   123
sl@0
   124
//
sl@0
   125
class CPosixRequest;
sl@0
   126
class CPosixServer;
sl@0
   127
class CPosixIPCSession;
sl@0
   128
NONSHARABLE_CLASS(CPosixProcess) : public CActive
sl@0
   129
/**
sl@0
   130
@internalComponent
sl@0
   131
*/
sl@0
   132
	{
sl@0
   133
public:
sl@0
   134
	CPosixProcess(CPosixServer& aServer);
sl@0
   135
	~CPosixProcess();
sl@0
   136
	void POpen3L(PosixParams* aParams);
sl@0
   137
	static CPosixProcess* Find(CPosixProcess* head, TInt pid);
sl@0
   138
	static void Release(CPosixProcess** aHead, CPosixProcess* aProc);
sl@0
   139
	void Sizes(TPosixIPCReply& aReply) const;
sl@0
   140
	void CopyToChildL(const RMessage2& aMessage);
sl@0
   141
	inline TInt IsAlive() const {return IsActive();}
sl@0
   142
	inline void Queue(CPosixRequest* aWaiter); 
sl@0
   143
protected:
sl@0
   144
	void RunL();
sl@0
   145
	void DoCancel();
sl@0
   146
sl@0
   147
private:
sl@0
   148
	inline CEnvironment& Env() const;
sl@0
   149
	inline CFileTable& Fids() const;
sl@0
   150
	inline RFs& Fs() const;
sl@0
   151
	inline RCommServ& Cs() const;
sl@0
   152
sl@0
   153
public:
sl@0
   154
	CPosixProcess* iNextProcess;
sl@0
   155
	TInt iPid;
sl@0
   156
	TInt iExitReason;
sl@0
   157
private:
sl@0
   158
	CPosixServer& iServer;
sl@0
   159
sl@0
   160
	RProcess iChild;
sl@0
   161
	CPosixRequest* iWaiters;
sl@0
   162
sl@0
   163
	TUint iVarCount;
sl@0
   164
	HBufC16* iEnvironment;
sl@0
   165
	HBufC* iWorkingDirectory;
sl@0
   166
	CPipeDesc* iPipes[3];
sl@0
   167
	};
sl@0
   168
//
sl@0
   169
class CPosixRequest;
sl@0
   170
sl@0
   171
/**
sl@0
   172
@internalComponent
sl@0
   173
*/
sl@0
   174
NONSHARABLE_CLASS(CPosixServer) : public CServer2
sl@0
   175
	{
sl@0
   176
public:
sl@0
   177
	static TInt ThreadFunction(TAny* aPtr);
sl@0
   178
	static void InitL(TInt aPriority);
sl@0
   179
	RFs& Fs()           { return iFs; }
sl@0
   180
	RCommServ& Cs()     { return iCs; }
sl@0
   181
	CFileTable& Fids()  { return iFids; }
sl@0
   182
	RSocketServ& Ss()   { return iSs; }
sl@0
   183
	CEnvironment& Env() { return iEnv; }
sl@0
   184
sl@0
   185
	int POpen3(PosixParams* aParams, int& anErrno);
sl@0
   186
	CPosixProcess* Child(TInt pid)	{ return CPosixProcess::Find(iChildren, pid); }
sl@0
   187
	void Release(CPosixProcess* aChild) { CPosixProcess::Release(&iChildren, aChild); }
sl@0
   188
	inline void WaitForAnyChild(CPosixRequest* aWaiter);
sl@0
   189
	CPosixRequest* Waiters();
sl@0
   190
	static void ServerPanic(TPosixServerPanic aPanic);
sl@0
   191
sl@0
   192
protected:
sl@0
   193
	CPosixServer(TInt aPriority);
sl@0
   194
	CSession2* NewSessionL(const TVersion& aVersion, const RMessage2& aMessage) const;
sl@0
   195
	void FindParentL();
sl@0
   196
	void DefaultConsoleL();
sl@0
   197
private:
sl@0
   198
	RFs iFs;
sl@0
   199
	CFileTable iFids;
sl@0
   200
	RSocketServ iSs;
sl@0
   201
	RCommServ	iCs;
sl@0
   202
sl@0
   203
	CEnvironment iEnv;
sl@0
   204
	RPosixSession iParent;
sl@0
   205
	CPosixProcess* iChildren;
sl@0
   206
	CPosixRequest* iWaitAnyQueue;
sl@0
   207
	};
sl@0
   208
sl@0
   209
//
sl@0
   210
class CPosixSession;
sl@0
   211
NONSHARABLE_CLASS(CPosixRequest) : public CActive
sl@0
   212
/**
sl@0
   213
@internalComponent
sl@0
   214
*/
sl@0
   215
	{
sl@0
   216
public:
sl@0
   217
	CPosixRequest(CPosixServer& aServer);
sl@0
   218
	~CPosixRequest();
sl@0
   219
	void Service(const RMessage2& aMessage);
sl@0
   220
	void EnList(CPosixRequest*& aHead);	// simple single linked list
sl@0
   221
	void WaitCompleted(TInt aPid, TInt aReason);
sl@0
   222
protected:
sl@0
   223
	void RunL();
sl@0
   224
	void DoCancel();
sl@0
   225
private:
sl@0
   226
	RFs& Fs() const           { return iServer.Fs(); }
sl@0
   227
	RCommServ& Cs() const           { return iServer.Cs(); }
sl@0
   228
	CFileTable& Fids() const  { return iServer.Fids(); }
sl@0
   229
	RSocketServ& Ss() const   { return iServer.Ss(); }
sl@0
   230
	CEnvironment& Env() const { return iServer.Env(); }
sl@0
   231
private:
sl@0
   232
	void QueueAsynch(const RMessage2& aMessage);
sl@0
   233
	void StartAsynch();
sl@0
   234
	void EndAsynch(TInt aResult);
sl@0
   235
sl@0
   236
	CPosixServer& iServer;
sl@0
   237
sl@0
   238
	RMessage2 iMessage;
sl@0
   239
sl@0
   240
	CFileDescBase* iFile;
sl@0
   241
	CSocketDesc* iNewF;
sl@0
   242
	int iNewFid;
sl@0
   243
	TPtr8 iPtr;
sl@0
   244
sl@0
   245
	CPosixRequest* iNext;	// for Enlist
sl@0
   246
	TSglQueLink iLink;
sl@0
   247
	enum CFileDescBase::IOQueues iQueue;
sl@0
   248
sl@0
   249
	friend class TPosixRequestQueue;
sl@0
   250
	friend class CFileDescBase;		// friend functions AddLast & Remove perhaps? 
sl@0
   251
	};
sl@0
   252
//
sl@0
   253
inline CEnvironment& CPosixProcess::Env() const { return iServer.Env(); }
sl@0
   254
inline CFileTable& CPosixProcess::Fids() const  { return iServer.Fids(); }
sl@0
   255
inline RFs& CPosixProcess::Fs() const           { return iServer.Fs(); }
sl@0
   256
inline RCommServ& CPosixProcess::Cs() const           { return iServer.Cs(); }
sl@0
   257
inline void CPosixProcess::Queue(CPosixRequest* aWaiter)          { aWaiter->EnList(iWaiters); }
sl@0
   258
inline void CPosixServer::WaitForAnyChild(CPosixRequest* aWaiter) { aWaiter->EnList(iWaitAnyQueue); }
sl@0
   259
sl@0
   260
//
sl@0
   261
/**
sl@0
   262
@internalComponent
sl@0
   263
*/
sl@0
   264
NONSHARABLE_CLASS(CPosixSession) : public CSession2
sl@0
   265
	{
sl@0
   266
public:
sl@0
   267
	CPosixSession(CPosixServer& aServer);
sl@0
   268
	virtual void ServiceL(const RMessage2& aMessage);
sl@0
   269
protected:
sl@0
   270
	CPosixRequest iActive;
sl@0
   271
	};
sl@0
   272
sl@0
   273
NONSHARABLE_CLASS(CPosixIPCSession) : public CPosixSession
sl@0
   274
/**
sl@0
   275
@internalComponent
sl@0
   276
*/
sl@0
   277
	{
sl@0
   278
public:
sl@0
   279
	inline CPosixIPCSession(CPosixServer& aServer) 
sl@0
   280
		: CPosixSession(aServer) {}
sl@0
   281
	virtual void ServiceL(const RMessage2& aMessage);	 // override the local ServiceL
sl@0
   282
	void SetPipes(CPipeDesc* aPipes[3]);
sl@0
   283
	~CPosixIPCSession();
sl@0
   284
sl@0
   285
private:
sl@0
   286
	TInt AreYouMyMotherL(const RMessage2& aMessage);
sl@0
   287
	TInt HelloMumL(const RMessage2& aMessage);
sl@0
   288
	void PipeRead(const RMessage2& aMessage);
sl@0
   289
	void PipeWrite(const RMessage2& aMessage);
sl@0
   290
	void PipeIoctl(const RMessage2& aMessage);
sl@0
   291
	void PipeClose(const RMessage2& aMessage);
sl@0
   292
	void PipeCancel(const RMessage2& aMessage);
sl@0
   293
sl@0
   294
	CPipeDesc* iPipes[3];
sl@0
   295
	};
sl@0
   296
//
sl@0
   297
NONSHARABLE_CLASS(CProcessSystemInterface) : public MSystemInterface, public CBase
sl@0
   298
/**
sl@0
   299
@internalComponent
sl@0
   300
*/
sl@0
   301
	{
sl@0
   302
public:
sl@0
   303
	CProcessSystemInterface();
sl@0
   304
	~CProcessSystemInterface();
sl@0
   305
sl@0
   306
	TInt Connect();
sl@0
   307
	virtual MSystemInterface& Clone();
sl@0
   308
	virtual void Release();
sl@0
   309
	virtual void TerminateProcess(int status);
sl@0
   310
sl@0
   311
	virtual int dup (int fid, int& anErrno);
sl@0
   312
	virtual int dup2 (int fid, int fid2, int& anErrno);
sl@0
   313
	virtual int open (const wchar_t* name, int mode, int perms, int& anErrno);
sl@0
   314
	virtual int read (int fid, char* buf, unsigned long len, int& anErrno);
sl@0
   315
	virtual int write (int fid, const char* buf, unsigned long len, int& anErrno);
sl@0
   316
	virtual int fsync (int fid, int& anErrno);
sl@0
   317
	virtual int close (int fid, int& anErrno);
sl@0
   318
	virtual int lseek (int fid, int offset, int whence, int& anErrno);
sl@0
   319
	virtual int fstat (int fid, struct stat *st, int& anErrno);
sl@0
   320
	virtual int ioctl (int fid, int cmd, void* param, int& anErrno);
sl@0
   321
	virtual int ioctl (int fid, int cmd, void* param, TRequestStatus& aStatus, int& anErrno);
sl@0
   322
	virtual int ioctl_complete (int fid, int cmd, void* param, TRequestStatus& aStatus, int& anErrno);
sl@0
   323
	virtual int ioctl_cancel (int fid, int& anErrno);
sl@0
   324
sl@0
   325
	virtual wchar_t * getcwd (wchar_t * buf, unsigned long len, int& anErrno);
sl@0
   326
sl@0
   327
	virtual int chdir (const wchar_t* path, int& anErrno);
sl@0
   328
	virtual int mkdir (const wchar_t* path, int perms, int& anErrno);
sl@0
   329
	virtual int rmdir (const wchar_t* path, int& anErrno);
sl@0
   330
	virtual int chmod (const wchar_t* path, int perms, int& anErrno);
sl@0
   331
	virtual int unlink (const wchar_t* path, int& anErrno);
sl@0
   332
	virtual int stat (const wchar_t* name, struct stat *st, int& anErrno);
sl@0
   333
	virtual int rename (const wchar_t* oldname, const wchar_t* newname, int& anErrno);
sl@0
   334
sl@0
   335
	virtual TInt ResolvePath (TParse& aResult, const wchar_t* path, TDes* aFilename);
sl@0
   336
sl@0
   337
	virtual int socket (int family, int style, int protocol, int& anErrno);
sl@0
   338
	virtual int recvfrom (int fd, char* buf, unsigned long cnt, int flags, struct sockaddr* from, unsigned long* fromsize, int& anErrno);
sl@0
   339
	virtual int sendto (int fd, const char* buf, unsigned long cnt, int flags, struct sockaddr* to, unsigned long tosize, int& anErrno);
sl@0
   340
	virtual int shutdown (int fd, int how, int& anErrno);
sl@0
   341
	virtual int listen (int fd, int n, int& anErrno);
sl@0
   342
	virtual int accept (int fd, int& anErrno);
sl@0
   343
	virtual int bind (int fd, struct sockaddr* addr, unsigned long size, int& anErrno);
sl@0
   344
	virtual int connect (int fd, struct sockaddr* addr, unsigned long size, int& anErrno);
sl@0
   345
	virtual int sockname (int fd, struct sockaddr* addr, unsigned long* size, int anEnd, int& anErrno);
sl@0
   346
	virtual int getsockopt (int fd, int level, int opt, void* buf, unsigned long* len, int& anErrno);
sl@0
   347
	virtual int setsockopt (int fd, int level, int opt, void* buf, unsigned long len, int& anErrno);
sl@0
   348
sl@0
   349
	virtual wchar_t* getenv (const wchar_t* name); 
sl@0
   350
	virtual void unsetenv (const wchar_t* name);
sl@0
   351
	virtual int setenv (const wchar_t* name, const wchar_t* value, int rewrite, int& anErrno);
sl@0
   352
sl@0
   353
	virtual int popen3 (const wchar_t *file, const wchar_t *cmd, const wchar_t *mode, wchar_t** envp, int fids[3], int& anErrno);
sl@0
   354
	virtual int waitpid (int pid, int* status, int options, int& anErrno);
sl@0
   355
sl@0
   356
private:
sl@0
   357
	int Request(TInt aFunction, int& anErrno);
sl@0
   358
	void Request(TInt aFunction, int& anErrno, TRequestStatus& aStatus);
sl@0
   359
	RPosixSession iSession;
sl@0
   360
	PosixParams iParams;
sl@0
   361
	};