Process.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Util/Array.h>
  7. #include <AnKi/Util/String.h>
  8. #include <AnKi/Util/WeakArray.h>
  9. #include <AnKi/Util/Enum.h>
  10. // Forward
  11. struct reproc_t;
  12. namespace anki {
  13. /// @addtogroup util_system
  14. /// @{
  15. /// @memberof Process
  16. enum class ProcessStatus : U8
  17. {
  18. kRunning,
  19. kNotRunning
  20. };
  21. /// @memberof Process
  22. enum class ProcessKillSignal : U8
  23. {
  24. kNormal,
  25. kForce
  26. };
  27. /// @memberof Process
  28. enum class ProcessOptions : U8
  29. {
  30. kNone = 0,
  31. kOpenStdout = 1 << 0,
  32. kOpenStderr = 1 << 1,
  33. };
  34. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(ProcessOptions)
  35. /// Executes external processes.
  36. class Process
  37. {
  38. public:
  39. Process() = default;
  40. ~Process();
  41. Process(const Process&) = delete;
  42. Process& operator=(const Process&) = delete;
  43. /// Start a process.
  44. /// @param executable The executable to start.
  45. /// @param arguments The command line arguments.
  46. /// @param environment The environment variables.
  47. Error start(CString executable, ConstWeakArray<CString> arguments = {}, ConstWeakArray<CString> environment = {},
  48. ProcessOptions options = ProcessOptions::kOpenStderr | ProcessOptions::kOpenStdout);
  49. /// Same as the other start().
  50. Error start(CString executable, const DynamicArray<String>& arguments, const DynamicArray<String>& environment,
  51. ProcessOptions options = ProcessOptions::kOpenStderr | ProcessOptions::kOpenStdout);
  52. /// Wait for the process to finish.
  53. /// @param timeout The time to wait. If it's negative wait forever.
  54. /// @param[out] status The exit status
  55. /// @param[out] exitCode The exit code if the process has finished.
  56. Error wait(Second timeout = -1.0, ProcessStatus* status = nullptr, I32* exitCode = nullptr);
  57. /// Get the status.
  58. Error getStatus(ProcessStatus& status);
  59. /// Kill the process. Need to call wait after killing the process.
  60. Error kill(ProcessKillSignal k);
  61. /// Read from stdout.
  62. Error readFromStdout(String& text);
  63. /// Read from stderr.
  64. Error readFromStderr(String& text);
  65. /// Cleanup a finished process. Call this if you want to start a new process again. Need to have waited before
  66. /// calling destroy.
  67. void destroy();
  68. /// Call a process and wait.
  69. /// @param executable The executable to start.
  70. /// @param arguments The command line arguments.
  71. /// @param stdOut Optional stdout.
  72. /// @param stdErr Optional stderr.
  73. /// @param exitCode Exit code.
  74. static Error callProcess(CString executable, ConstWeakArray<CString> arguments, String* stdOut, String* stdErr, I32& exitCode);
  75. private:
  76. static constexpr U32 kMaxArgs = 64;
  77. static constexpr U32 kMaxEnv = 32;
  78. reproc_t* m_handle = nullptr;
  79. Error readCommon(I32 reprocStream, String& text);
  80. Error startInternal(const Char* arguments[], const Char* environment[], ProcessOptions options);
  81. };
  82. /// Get the current process ID.
  83. ANKI_PURE U32 getCurrentProcessId();
  84. /// @}
  85. } // end namespace anki