process.inc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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
  13. {$ifdef ver1_0}
  14. Linux
  15. {$else}
  16. Unix
  17. {$endif}
  18. ;
  19. Function TProcess.GetRunning : Boolean;
  20. begin
  21. IF FRunning then
  22. FRunning:=GetExitStatus=-1;
  23. Result:=FRunning;
  24. end;
  25. Procedure TProcess.Execute;
  26. begin
  27. FreeStreams;
  28. CreatePipeStreams (FChildInputSTream,FParentOutPutStream);
  29. CreatePipeStreams (FParentInputStream,FChildOutPutStream);
  30. If poUsePipes in FCreateOptions then
  31. begin
  32. if poStdErrToOutPut in FCreateOptions then
  33. CreatePipeStreams (FParentErrorStream,FChildErrorStream)
  34. else
  35. begin
  36. FChildErrorStream:=FChildOutPutStream;
  37. FParentErrorStream:=FParentInputStream;
  38. end;
  39. end
  40. else
  41. CreatePipeStreams (FParentErrorStream,FChildErrorStream);
  42. If FCurrentDirectory<>'' then
  43. Chdir(FCurrentDirectory);
  44. FHandle:=fork();
  45. if FHandle=0 then
  46. begin
  47. // Child
  48. fdClose(0);
  49. fdClose(1);
  50. fdclose(2);
  51. dup2(FChildInputStream.Handle,0);
  52. dup2(FCHildOutputStream.Handle,1);
  53. dup2(FChildErrorStream.Handle,2);
  54. execl(FCommandline);
  55. halt(127);
  56. end
  57. else
  58. begin
  59. // Parent
  60. FPID:=FHandle;
  61. FThreadHandle:=FHandle;
  62. fdclose(FChildOutputStream.Handle);
  63. fdclose(FChildInputStream.Handle);
  64. fdclose(FChildErrorStream.Handle);
  65. FRunning:=True;
  66. if (poWaitOnExit in FCreateOptions) and
  67. not (poRunSuspended in FCreateOptions) then
  68. WaitOnExit;
  69. end;
  70. end;
  71. Function TProcess.WaitOnExit : Dword;
  72. begin
  73. waitpid(FPID, nil, 0);
  74. {
  75. Result:=WaitForSingleObject (FprocessInformation.hProcess,Infinite);
  76. If Result<>Wait_Failed then
  77. GetExitStatus;
  78. } FRunning:=False;
  79. Result := 0;
  80. end;
  81. Function TProcess.Suspend : Longint;
  82. begin
  83. Result:=Kill(Handle,SIGSTOP);
  84. end;
  85. Function TProcess.Resume : LongInt;
  86. begin
  87. Result:=Kill(FHandle,SIGCONT);
  88. end;
  89. Function TProcess.Terminate(AExitCode : Integer) : Boolean;
  90. begin
  91. Result:=False;
  92. If ExitStatus=-1 then
  93. Result:=Kill(FHandle,SIGTERM)=0;
  94. end;
  95. {
  96. $Log$
  97. Revision 1.2 2002-09-07 15:15:27 peter
  98. * old logs removed and tabs fixed
  99. }