First public contribution.
1 // Copyright (c) 1997-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
25 const TInt KCPosixMajorVersionNumber=2;
26 const TInt KCPosixMinorVersionNumber=0;
30 enum TPosixServerPanic
32 EPosix_BadWaitCompletion=1,
39 // asynchronous operations which need SetActive/RunL
40 PMread, PMwrite, PMfsync,
41 PMsendto, PMrecvfrom, PMconnect, PMshutdown, PMaccept,
45 // asynchronous operations which just delay completion
47 // synchronous operations
49 PMopen, PMclose, PMlseek, PMfstat,
51 PMchdir, PMmkdir, PMrmdir,
52 PMchmod, PMunlink, PMstat, PMrename,
54 PMsocket, PMbind, PMlisten, PMsockname, PMgetsockopt, PMsetsockopt,
55 PMioctlcomplete, PMcancel,
57 PMgetenv, PMsetenv, PMunsetenv,
59 // inter-process operations
60 PMAreYouMyMother, PMHelloMum, PMPipeWrite, PMPipeRead, PMPipeIoctl, PMPipeClose, PMPipeCancel
72 const wchar_t* cwptr[2];
76 unsigned long* lenp[1];
81 Package which holds client's process Id.
84 typedef TPckgBuf<TInt> TPosixIPCPid;
87 Package used by server to pass data to client.
93 TUint iEnvironmentSize;
94 TUint iWorkingDirectorySize;
100 typedef TPckgBuf<PosixIPCReply> TPosixIPCReply;
104 class RPosixSession : public RSessionBase
111 TInt Connect(TDesC& aServerName);
113 // for specific posix functions
114 int Request(TInt aFunction, int& anErrno, PosixParams& params) const;
115 void Request(TInt aFunction, int& anErrno, PosixParams& params, TRequestStatus& aStatus) const;
116 // for generic messages
117 TInt Request(TInt aFunction, const TIpcArgs& aArg) const;
118 void Request(TInt aFunction, const TIpcArgs& aArg, TRequestStatus &aStatus) const;
120 static TVersion Version();
127 class CPosixIPCSession;
128 NONSHARABLE_CLASS(CPosixProcess) : public CActive
134 CPosixProcess(CPosixServer& aServer);
136 void POpen3L(PosixParams* aParams);
137 static CPosixProcess* Find(CPosixProcess* head, TInt pid);
138 static void Release(CPosixProcess** aHead, CPosixProcess* aProc);
139 void Sizes(TPosixIPCReply& aReply) const;
140 void CopyToChildL(const RMessage2& aMessage);
141 inline TInt IsAlive() const {return IsActive();}
142 inline void Queue(CPosixRequest* aWaiter);
148 inline CEnvironment& Env() const;
149 inline CFileTable& Fids() const;
150 inline RFs& Fs() const;
151 inline RCommServ& Cs() const;
154 CPosixProcess* iNextProcess;
158 CPosixServer& iServer;
161 CPosixRequest* iWaiters;
164 HBufC16* iEnvironment;
165 HBufC* iWorkingDirectory;
166 CPipeDesc* iPipes[3];
174 NONSHARABLE_CLASS(CPosixServer) : public CServer2
177 static TInt ThreadFunction(TAny* aPtr);
178 static void InitL(TInt aPriority);
179 RFs& Fs() { return iFs; }
180 RCommServ& Cs() { return iCs; }
181 CFileTable& Fids() { return iFids; }
182 RSocketServ& Ss() { return iSs; }
183 CEnvironment& Env() { return iEnv; }
185 int POpen3(PosixParams* aParams, int& anErrno);
186 CPosixProcess* Child(TInt pid) { return CPosixProcess::Find(iChildren, pid); }
187 void Release(CPosixProcess* aChild) { CPosixProcess::Release(&iChildren, aChild); }
188 inline void WaitForAnyChild(CPosixRequest* aWaiter);
189 CPosixRequest* Waiters();
190 static void ServerPanic(TPosixServerPanic aPanic);
193 CPosixServer(TInt aPriority);
194 CSession2* NewSessionL(const TVersion& aVersion, const RMessage2& aMessage) const;
196 void DefaultConsoleL();
204 RPosixSession iParent;
205 CPosixProcess* iChildren;
206 CPosixRequest* iWaitAnyQueue;
211 NONSHARABLE_CLASS(CPosixRequest) : public CActive
217 CPosixRequest(CPosixServer& aServer);
219 void Service(const RMessage2& aMessage);
220 void EnList(CPosixRequest*& aHead); // simple single linked list
221 void WaitCompleted(TInt aPid, TInt aReason);
226 RFs& Fs() const { return iServer.Fs(); }
227 RCommServ& Cs() const { return iServer.Cs(); }
228 CFileTable& Fids() const { return iServer.Fids(); }
229 RSocketServ& Ss() const { return iServer.Ss(); }
230 CEnvironment& Env() const { return iServer.Env(); }
232 void QueueAsynch(const RMessage2& aMessage);
234 void EndAsynch(TInt aResult);
236 CPosixServer& iServer;
240 CFileDescBase* iFile;
245 CPosixRequest* iNext; // for Enlist
247 enum CFileDescBase::IOQueues iQueue;
249 friend class TPosixRequestQueue;
250 friend class CFileDescBase; // friend functions AddLast & Remove perhaps?
253 inline CEnvironment& CPosixProcess::Env() const { return iServer.Env(); }
254 inline CFileTable& CPosixProcess::Fids() const { return iServer.Fids(); }
255 inline RFs& CPosixProcess::Fs() const { return iServer.Fs(); }
256 inline RCommServ& CPosixProcess::Cs() const { return iServer.Cs(); }
257 inline void CPosixProcess::Queue(CPosixRequest* aWaiter) { aWaiter->EnList(iWaiters); }
258 inline void CPosixServer::WaitForAnyChild(CPosixRequest* aWaiter) { aWaiter->EnList(iWaitAnyQueue); }
264 NONSHARABLE_CLASS(CPosixSession) : public CSession2
267 CPosixSession(CPosixServer& aServer);
268 virtual void ServiceL(const RMessage2& aMessage);
270 CPosixRequest iActive;
273 NONSHARABLE_CLASS(CPosixIPCSession) : public CPosixSession
279 inline CPosixIPCSession(CPosixServer& aServer)
280 : CPosixSession(aServer) {}
281 virtual void ServiceL(const RMessage2& aMessage); // override the local ServiceL
282 void SetPipes(CPipeDesc* aPipes[3]);
286 TInt AreYouMyMotherL(const RMessage2& aMessage);
287 TInt HelloMumL(const RMessage2& aMessage);
288 void PipeRead(const RMessage2& aMessage);
289 void PipeWrite(const RMessage2& aMessage);
290 void PipeIoctl(const RMessage2& aMessage);
291 void PipeClose(const RMessage2& aMessage);
292 void PipeCancel(const RMessage2& aMessage);
294 CPipeDesc* iPipes[3];
297 NONSHARABLE_CLASS(CProcessSystemInterface) : public MSystemInterface, public CBase
303 CProcessSystemInterface();
304 ~CProcessSystemInterface();
307 virtual MSystemInterface& Clone();
308 virtual void Release();
309 virtual void TerminateProcess(int status);
311 virtual int dup (int fid, int& anErrno);
312 virtual int dup2 (int fid, int fid2, int& anErrno);
313 virtual int open (const wchar_t* name, int mode, int perms, int& anErrno);
314 virtual int read (int fid, char* buf, unsigned long len, int& anErrno);
315 virtual int write (int fid, const char* buf, unsigned long len, int& anErrno);
316 virtual int fsync (int fid, int& anErrno);
317 virtual int close (int fid, int& anErrno);
318 virtual int lseek (int fid, int offset, int whence, int& anErrno);
319 virtual int fstat (int fid, struct stat *st, int& anErrno);
320 virtual int ioctl (int fid, int cmd, void* param, int& anErrno);
321 virtual int ioctl (int fid, int cmd, void* param, TRequestStatus& aStatus, int& anErrno);
322 virtual int ioctl_complete (int fid, int cmd, void* param, TRequestStatus& aStatus, int& anErrno);
323 virtual int ioctl_cancel (int fid, int& anErrno);
325 virtual wchar_t * getcwd (wchar_t * buf, unsigned long len, int& anErrno);
327 virtual int chdir (const wchar_t* path, int& anErrno);
328 virtual int mkdir (const wchar_t* path, int perms, int& anErrno);
329 virtual int rmdir (const wchar_t* path, int& anErrno);
330 virtual int chmod (const wchar_t* path, int perms, int& anErrno);
331 virtual int unlink (const wchar_t* path, int& anErrno);
332 virtual int stat (const wchar_t* name, struct stat *st, int& anErrno);
333 virtual int rename (const wchar_t* oldname, const wchar_t* newname, int& anErrno);
335 virtual TInt ResolvePath (TParse& aResult, const wchar_t* path, TDes* aFilename);
337 virtual int socket (int family, int style, int protocol, int& anErrno);
338 virtual int recvfrom (int fd, char* buf, unsigned long cnt, int flags, struct sockaddr* from, unsigned long* fromsize, int& anErrno);
339 virtual int sendto (int fd, const char* buf, unsigned long cnt, int flags, struct sockaddr* to, unsigned long tosize, int& anErrno);
340 virtual int shutdown (int fd, int how, int& anErrno);
341 virtual int listen (int fd, int n, int& anErrno);
342 virtual int accept (int fd, int& anErrno);
343 virtual int bind (int fd, struct sockaddr* addr, unsigned long size, int& anErrno);
344 virtual int connect (int fd, struct sockaddr* addr, unsigned long size, int& anErrno);
345 virtual int sockname (int fd, struct sockaddr* addr, unsigned long* size, int anEnd, int& anErrno);
346 virtual int getsockopt (int fd, int level, int opt, void* buf, unsigned long* len, int& anErrno);
347 virtual int setsockopt (int fd, int level, int opt, void* buf, unsigned long len, int& anErrno);
349 virtual wchar_t* getenv (const wchar_t* name);
350 virtual void unsetenv (const wchar_t* name);
351 virtual int setenv (const wchar_t* name, const wchar_t* value, int rewrite, int& anErrno);
353 virtual int popen3 (const wchar_t *file, const wchar_t *cmd, const wchar_t *mode, wchar_t** envp, int fids[3], int& anErrno);
354 virtual int waitpid (int pid, int* status, int options, int& anErrno);
357 int Request(TInt aFunction, int& anErrno);
358 void Request(TInt aFunction, int& anErrno, TRequestStatus& aStatus);
359 RPosixSession iSession;