cmProcess.h 3.1 KB

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