Process.h 2.1 KB

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