cmProcess.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmProcess_h
  4. #define cmProcess_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include "cmDuration.h"
  7. #include "cmProcessOutput.h"
  8. #include "cmUVHandlePtr.h"
  9. #include "cm_uv.h"
  10. #include <chrono>
  11. #include <stddef.h>
  12. #include <stdint.h>
  13. #include <string>
  14. #include <vector>
  15. class cmCTestRunTest;
  16. /** \class cmProcess
  17. * \brief run a process with c++
  18. *
  19. * cmProcess wraps the kwsys process stuff in a c++ class.
  20. */
  21. class cmProcess
  22. {
  23. public:
  24. explicit cmProcess(cmCTestRunTest& runner);
  25. ~cmProcess();
  26. const char* GetCommand() { return this->Command.c_str(); }
  27. void SetCommand(const char* command);
  28. void SetCommandArguments(std::vector<std::string> const& arg);
  29. void SetWorkingDirectory(const char* dir) { this->WorkingDirectory = dir; }
  30. void SetTimeout(cmDuration t) { this->Timeout = t; }
  31. void ChangeTimeout(cmDuration t);
  32. void ResetStartTime();
  33. // Return true if the process starts
  34. bool StartProcess(uv_loop_t& loop);
  35. enum class State
  36. {
  37. Starting,
  38. Error,
  39. Exception,
  40. Executing,
  41. Exited,
  42. Expired,
  43. Killed,
  44. Disowned
  45. };
  46. State GetProcessStatus();
  47. int GetId() { return this->Id; }
  48. void SetId(int id) { this->Id = id; }
  49. int GetExitValue() { return this->ExitValue; }
  50. cmDuration GetTotalTime() { return this->TotalTime; }
  51. enum class Exception
  52. {
  53. None,
  54. Fault,
  55. Illegal,
  56. Interrupt,
  57. Numerical,
  58. Other
  59. };
  60. Exception GetExitException();
  61. std::string GetExitExceptionString();
  62. private:
  63. cmDuration Timeout;
  64. std::chrono::steady_clock::time_point StartTime;
  65. cmDuration TotalTime;
  66. bool ReadHandleClosed = false;
  67. bool ProcessHandleClosed = false;
  68. cm::uv_process_ptr Process;
  69. cm::uv_pipe_ptr PipeReader;
  70. cm::uv_timer_ptr Timer;
  71. std::vector<char> Buf;
  72. cmCTestRunTest& Runner;
  73. cmProcessOutput Conv;
  74. int Signal = 0;
  75. cmProcess::State ProcessState = cmProcess::State::Starting;
  76. static void OnExitCB(uv_process_t* process, int64_t exit_status,
  77. int term_signal);
  78. static void OnTimeoutCB(uv_timer_t* timer);
  79. static void OnReadCB(uv_stream_t* stream, ssize_t nread,
  80. const uv_buf_t* buf);
  81. static void OnAllocateCB(uv_handle_t* handle, size_t suggested_size,
  82. uv_buf_t* buf);
  83. void OnExit(int64_t exit_status, int term_signal);
  84. void OnTimeout();
  85. void OnRead(ssize_t nread, const uv_buf_t* buf);
  86. void OnAllocate(size_t suggested_size, uv_buf_t* buf);
  87. void StartTimer();
  88. class Buffer : public std::vector<char>
  89. {
  90. // Half-open index range of partial line already scanned.
  91. size_type First;
  92. size_type Last;
  93. public:
  94. Buffer()
  95. : First(0)
  96. , Last(0)
  97. {
  98. }
  99. bool GetLine(std::string& line);
  100. bool GetLast(std::string& line);
  101. };
  102. Buffer Output;
  103. std::string Command;
  104. std::string WorkingDirectory;
  105. std::vector<std::string> Arguments;
  106. std::vector<const char*> ProcessArgs;
  107. int Id;
  108. int ExitValue;
  109. };
  110. #endif