cmProcess.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <chrono>
  6. #include <cstddef>
  7. #include <cstdint>
  8. #include <memory>
  9. #include <string>
  10. #include <utility>
  11. #include <vector>
  12. #include <cm3p/uv.h>
  13. #include "cmDuration.h"
  14. #include "cmProcessOutput.h"
  15. #include "cmUVHandlePtr.h"
  16. class cmCTestRunTest;
  17. /** \class cmProcess
  18. * \brief run a process with c++
  19. *
  20. * cmProcess wraps the kwsys process stuff in a c++ class.
  21. */
  22. class cmProcess
  23. {
  24. public:
  25. explicit cmProcess(std::unique_ptr<cmCTestRunTest> runner);
  26. ~cmProcess();
  27. void SetCommand(std::string const& command);
  28. void SetCommandArguments(std::vector<std::string> const& arg);
  29. void SetWorkingDirectory(std::string const& 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, std::vector<size_t>* affinity);
  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() const { return this->Id; }
  48. void SetId(int id) { this->Id = id; }
  49. int64_t GetExitValue() const { 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() const;
  61. std::string GetExitExceptionString() const;
  62. std::unique_ptr<cmCTestRunTest> GetRunner()
  63. {
  64. return std::move(this->Runner);
  65. }
  66. private:
  67. cmDuration Timeout;
  68. std::chrono::steady_clock::time_point StartTime;
  69. cmDuration TotalTime;
  70. bool ReadHandleClosed = false;
  71. bool ProcessHandleClosed = false;
  72. cm::uv_process_ptr Process;
  73. cm::uv_pipe_ptr PipeReader;
  74. cm::uv_timer_ptr Timer;
  75. std::vector<char> Buf;
  76. std::unique_ptr<cmCTestRunTest> Runner;
  77. cmProcessOutput Conv;
  78. int Signal = 0;
  79. cmProcess::State ProcessState = cmProcess::State::Starting;
  80. static void OnExitCB(uv_process_t* process, int64_t exit_status,
  81. int term_signal);
  82. static void OnTimeoutCB(uv_timer_t* timer);
  83. static void OnReadCB(uv_stream_t* stream, ssize_t nread,
  84. const uv_buf_t* buf);
  85. static void OnAllocateCB(uv_handle_t* handle, size_t suggested_size,
  86. uv_buf_t* buf);
  87. void OnExit(int64_t exit_status, int term_signal);
  88. void OnTimeout();
  89. void OnRead(ssize_t nread, const uv_buf_t* buf);
  90. void OnAllocate(size_t suggested_size, uv_buf_t* buf);
  91. void StartTimer();
  92. void Finish();
  93. class Buffer : public std::vector<char>
  94. {
  95. // Half-open index range of partial line already scanned.
  96. size_type First = 0;
  97. size_type Last = 0;
  98. public:
  99. Buffer() = default;
  100. bool GetLine(std::string& line);
  101. bool GetLast(std::string& line);
  102. };
  103. Buffer Output;
  104. std::string Command;
  105. std::string WorkingDirectory;
  106. std::vector<std::string> Arguments;
  107. std::vector<const char*> ProcessArgs;
  108. int Id;
  109. int64_t ExitValue;
  110. };