process.inc 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. uses linux;
  2. Function TProcess.GetRunning : Boolean;
  3. begin
  4. IF FRunning then
  5. FRunning:=GetExitStatus=-1;
  6. Result:=FRunning;
  7. end;
  8. Procedure TProcess.Execute;
  9. begin
  10. If poUsePipes in FCreateOptions then
  11. begin
  12. FreeStreams;
  13. CreatePipeStreams (FChildInputSTream,FParentOutPutStream);
  14. CreatePipeStreams (FParentInputStream,FChildOutPutStream);
  15. if poStdErrToOutPut in FCreateOptions then
  16. CreatePipeStreams (FParentErrorStream,FChildErrorStream)
  17. else
  18. begin
  19. FChildErrorStream:=FChildOutPutStream;
  20. FParentErrorStream:=FParentInputStream;
  21. end;
  22. end;
  23. If FCurrentDirectory<>'' then
  24. Chdir(FCurrentDirectory);
  25. FHandle:=fork();
  26. if FHandle=0 then
  27. begin
  28. // Child
  29. fdClose(0);
  30. fdClose(1);
  31. fdclose(2);
  32. dup2(FChildInputStream.Handle,0);
  33. dup2(FCHildOutputStream.Handle,1);
  34. dup2(FChildErrorStream.Handle,2);
  35. execl(FCommandline);
  36. halt(127);
  37. end
  38. else
  39. begin
  40. // Parent
  41. FPID:=FHandle;
  42. FThreadHandle:=FHandle;
  43. fdclose(FChildOutputStream.Handle);
  44. fdclose(FChildInputStream.Handle);
  45. fdclose(FChildErrorStream.Handle);
  46. FRunning:=True;
  47. if (poWaitOnExit in FCreateOptions) and
  48. not (poRunSuspended in FCreateOptions) then
  49. WaitOnExit;
  50. end;
  51. end;
  52. Function TProcess.WaitOnExit : Dword;
  53. begin
  54. {
  55. Result:=WaitForSingleObject (FprocessInformation.hProcess,Infinite);
  56. If Result<>Wait_Failed then
  57. GetExitStatus;
  58. } FRunning:=False;
  59. end;
  60. Function TProcess.Suspend : Longint;
  61. begin
  62. Result:=Kill(Handle,SIGSTOP);
  63. end;
  64. Function TProcess.Resume : LongInt;
  65. begin
  66. Result:=Kill(FHandle,SIGCONT);
  67. end;
  68. Function TProcess.Terminate(AExitCode : Integer) : Boolean;
  69. begin
  70. Result:=False;
  71. If ExitStatus=-1 then
  72. Result:=Kill(FHandle,SIGTERM)=0;
  73. end;