Process.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright (C) 2009-2021, 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. namespace anki {
  10. /// @addtogroup util_system
  11. /// @{
  12. /// @memberof Process
  13. enum class ProcessStatus : U8
  14. {
  15. RUNNING,
  16. NOT_RUNNING,
  17. NORMAL_EXIT,
  18. CRASH_EXIT
  19. };
  20. /// @memberof Process
  21. enum class ProcessKillSignal : U8
  22. {
  23. NORMAL,
  24. FORCE
  25. };
  26. /// Executes external processes.
  27. class Process
  28. {
  29. public:
  30. Process() = default;
  31. ~Process();
  32. Process(const Process&) = delete;
  33. Process& operator=(const Process&) = delete;
  34. /// Start a process.
  35. /// @param executable The executable to start.
  36. /// @param arguments The command line arguments.
  37. /// @param environment The environment variables.
  38. ANKI_USE_RESULT Error start(CString executable, ConstWeakArray<CString> arguments,
  39. ConstWeakArray<CString> environment);
  40. /// Wait for the process to finish.
  41. /// @param timeout The time to wait. If 0.0 wait forever.
  42. /// @param[out] status The exit status
  43. /// @param[out] exitCode The exit code if the process has finished.
  44. ANKI_USE_RESULT Error wait(Second timeout = 0.0, ProcessStatus* status = nullptr, I32* exitCode = nullptr);
  45. /// Get the status.
  46. ANKI_USE_RESULT Error getStatus(ProcessStatus& status);
  47. /// Kill the process.
  48. ANKI_USE_RESULT Error kill(ProcessKillSignal k);
  49. /// Read from stdout.
  50. ANKI_USE_RESULT Error readFromStdout(StringAuto& text);
  51. /// Read from stderr.
  52. ANKI_USE_RESULT Error readFromStderr(StringAuto& text);
  53. private:
  54. #if ANKI_POSIX
  55. static constexpr int DEFAULT_EXIT_CODE = -1;
  56. int m_pid = -1;
  57. int m_exitCode = DEFAULT_EXIT_CODE;
  58. ProcessStatus m_status = ProcessStatus::NOT_RUNNING;
  59. Array<int, 2> m_stdoutPipe = {-1, -1};
  60. Array<int, 2> m_stderrPipe = {-1, -1};
  61. void destroyPipes();
  62. ANKI_USE_RESULT Error createPipes();
  63. ANKI_USE_RESULT Error readFromFd(int fd, StringAuto& text) const;
  64. /// Update some members.
  65. ANKI_USE_RESULT Error refresh(int waitpidOptions);
  66. #else
  67. // TODO
  68. #endif
  69. };
  70. /// @}
  71. } // end namespace anki