cmProcess.h 3.0 KB

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