process_spawn.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright (C) 2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef PROCESS_SPAWN_H
  15. #define PROCESS_SPAWN_H
  16. #include <exceptions/exceptions.h>
  17. #include <string>
  18. #include <vector>
  19. namespace isc {
  20. namespace util {
  21. /// @brief Exception thrown when error occurs during spawning a process.
  22. class ProcessSpawnError : public Exception {
  23. public:
  24. ProcessSpawnError(const char* file, size_t line, const char* what) :
  25. isc::Exception(file, line, what) { };
  26. };
  27. /// @brief Forward declaration to the implementation of the @c ProcessSpawn
  28. /// class.
  29. class ProcessSpawnImpl;
  30. /// @brief Type of the container holding arguments of the executable
  31. /// being run as a background process.
  32. typedef std::vector<std::string> ProcessArgs;
  33. /// @brief Utility class for spawning new processes.
  34. ///
  35. /// This class is used to spawn new process by Kea. It forks the current
  36. /// process and then uses the @c execvp function to execute the specified
  37. /// binary with parameters. The @c ProcessSpawn installs the handler for
  38. /// the SIGCHLD signal, which is executed when the child process ends.
  39. /// The handler checks the exit code returned by the process and records
  40. /// it. The exit code can be retrieved by the caller using the
  41. /// @c ProcessSpawn::getExitStatus method.
  42. ///
  43. /// @warning It is not recommended to use multiple instances of the
  44. /// @c ProcessSpawn classes at the same time, because each newly
  45. /// created instance would replace the SIGCHLD handler. This means that
  46. /// it would not be possible to gather the exit code of all but one
  47. /// (newly created) process, because the SIGCHLD handler is responsible
  48. /// for checking the exit code of the process.
  49. class ProcessSpawn {
  50. public:
  51. /// @brief Constructor.
  52. ///
  53. /// @param executable A path to the program to be executed.
  54. /// @param args Arguments for the program to be executed.
  55. ProcessSpawn(const std::string& executable,
  56. const ProcessArgs& args = ProcessArgs());
  57. /// @brief Destructor.
  58. ~ProcessSpawn();
  59. /// @brief Returns full command line, including arguments, for the process.
  60. std::string getCommandLine() const;
  61. /// @brief Spawn the new process.
  62. ///
  63. /// This method forks the current process and execues the specified
  64. /// binary with arguments within the child process.
  65. ///
  66. /// The child process will return EXIT_FAILURE if the method was unable
  67. /// to start the exuctable, e.g. as a result of insufficient permissions
  68. /// or when the executable does not exist. If the process ends successfully
  69. /// the EXIT_SUCCESS is returned.
  70. ///
  71. /// @throw ProcessSpawnError if forking a current process failed.
  72. void spawn();
  73. /// @brief Checks if the process is still running.
  74. ///
  75. /// @return true if the child process is running, false otherwise.
  76. bool isRunning() const;
  77. /// @brief Returns exit status of the process.
  78. ///
  79. /// If the process is still running, the previous status is returned
  80. /// or 0, if the process is being ran for the first time.
  81. ///
  82. /// @return Exit code of the process.
  83. int getExitStatus() const;
  84. private:
  85. /// @brief A pointer to the implementation of this class.
  86. ProcessSpawnImpl* impl_;
  87. };
  88. }
  89. }
  90. #endif // PROCESS_SPAWN_H