process.inc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Michael Van Canneyt
  5. Linux specific part of TProcess.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. uses linux;
  13. Function TProcess.GetRunning : Boolean;
  14. begin
  15. IF FRunning then
  16. FRunning:=GetExitStatus=-1;
  17. Result:=FRunning;
  18. end;
  19. Procedure TProcess.Execute;
  20. begin
  21. FreeStreams;
  22. CreatePipeStreams (FChildInputSTream,FParentOutPutStream);
  23. CreatePipeStreams (FParentInputStream,FChildOutPutStream);
  24. If poUsePipes in FCreateOptions then
  25. begin
  26. if poStdErrToOutPut in FCreateOptions then
  27. CreatePipeStreams (FParentErrorStream,FChildErrorStream)
  28. else
  29. begin
  30. FChildErrorStream:=FChildOutPutStream;
  31. FParentErrorStream:=FParentInputStream;
  32. end;
  33. end
  34. else
  35. CreatePipeStreams (FParentErrorStream,FChildErrorStream);
  36. If FCurrentDirectory<>'' then
  37. Chdir(FCurrentDirectory);
  38. FHandle:=fork();
  39. if FHandle=0 then
  40. begin
  41. // Child
  42. fdClose(0);
  43. fdClose(1);
  44. fdclose(2);
  45. dup2(FChildInputStream.Handle,0);
  46. dup2(FCHildOutputStream.Handle,1);
  47. dup2(FChildErrorStream.Handle,2);
  48. execl(FCommandline);
  49. halt(127);
  50. end
  51. else
  52. begin
  53. // Parent
  54. FPID:=FHandle;
  55. FThreadHandle:=FHandle;
  56. fdclose(FChildOutputStream.Handle);
  57. fdclose(FChildInputStream.Handle);
  58. fdclose(FChildErrorStream.Handle);
  59. FRunning:=True;
  60. if (poWaitOnExit in FCreateOptions) and
  61. not (poRunSuspended in FCreateOptions) then
  62. WaitOnExit;
  63. end;
  64. end;
  65. Function TProcess.WaitOnExit : Dword;
  66. begin
  67. waitpid(FPID, nil, 0);
  68. {
  69. Result:=WaitForSingleObject (FprocessInformation.hProcess,Infinite);
  70. If Result<>Wait_Failed then
  71. GetExitStatus;
  72. } FRunning:=False;
  73. Result := 0;
  74. end;
  75. Function TProcess.Suspend : Longint;
  76. begin
  77. Result:=Kill(Handle,SIGSTOP);
  78. end;
  79. Function TProcess.Resume : LongInt;
  80. begin
  81. Result:=Kill(FHandle,SIGCONT);
  82. end;
  83. Function TProcess.Terminate(AExitCode : Integer) : Boolean;
  84. begin
  85. Result:=False;
  86. If ExitStatus=-1 then
  87. Result:=Kill(FHandle,SIGTERM)=0;
  88. end;
  89. {
  90. $Log$
  91. Revision 1.2 2000-07-13 11:33:01 michael
  92. + removed logs
  93. }