process_spawn.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 <sys/types.h>
  19. #include <vector>
  20. namespace isc {
  21. namespace util {
  22. /// @brief Exception thrown when error occurs during spawning a process.
  23. class ProcessSpawnError : public Exception {
  24. public:
  25. ProcessSpawnError(const char* file, size_t line, const char* what) :
  26. isc::Exception(file, line, what) { };
  27. };
  28. /// @brief Forward declaration to the implementation of the @c ProcessSpawn
  29. /// class.
  30. class ProcessSpawnImpl;
  31. /// @brief Type of the container holding arguments of the executable
  32. /// being run as a background process.
  33. typedef std::vector<std::string> ProcessArgs;
  34. /// @brief Utility class for spawning new processes.
  35. ///
  36. /// This class is used to spawn new process by Kea. It forks the current
  37. /// process and then uses the @c execvp function to execute the specified
  38. /// binary with parameters. The @c ProcessSpawn installs the handler for
  39. /// the SIGCHLD signal, which is executed when the child process ends.
  40. /// The handler checks the exit code returned by the process and records
  41. /// it. The exit code can be retrieved by the caller using the
  42. /// @c ProcessSpawn::getExitStatus method.
  43. ///
  44. /// @warning Only one instance of the @c ProcessSpawn class may exist
  45. /// at the given time. Creating additional instance would cause an
  46. /// attempt to register a new SIGCHLD signal handler and, as a
  47. /// consequence, the new @c ProcessSpawn object will fail to create.
  48. ///
  49. /// @todo The SIGCHLD handling logic should be moved to the @c SignalSet
  50. /// class so as multiple instances of the @c ProcessSpawn use the same
  51. /// SIGCHLD signal handler.
  52. class ProcessSpawn {
  53. public:
  54. /// @brief Constructor.
  55. ///
  56. /// @param executable A path to the program to be executed.
  57. /// @param args Arguments for the program to be executed.
  58. ProcessSpawn(const std::string& executable,
  59. const ProcessArgs& args = ProcessArgs());
  60. /// @brief Destructor.
  61. ~ProcessSpawn();
  62. /// @brief Returns full command line, including arguments, for the process.
  63. std::string getCommandLine() const;
  64. /// @brief Spawn the new process.
  65. ///
  66. /// This method forks the current process and executes the specified
  67. /// binary with arguments within the child process.
  68. ///
  69. /// The child process will return EXIT_FAILURE if the method was unable
  70. /// to start the executable, e.g. as a result of insufficient permissions
  71. /// or when the executable does not exist. If the process ends successfully
  72. /// the EXIT_SUCCESS is returned.
  73. ///
  74. /// @throw ProcessSpawnError if forking a current process failed.
  75. pid_t spawn();
  76. /// @brief Checks if the process is still running.
  77. ///
  78. /// @param pid ID of the child processes for which state should be checked.
  79. ///
  80. /// @return true if the child process is running, false otherwise.
  81. bool isRunning(const pid_t pid) const;
  82. /// @brief Checks if any of the spawned processes is still running.
  83. ///
  84. /// @return true if at least one child process is still running.
  85. bool isAnyRunning() const;
  86. /// @brief Returns exit status of the process.
  87. ///
  88. /// If the process is still running, the previous status is returned
  89. /// or 0, if the process is being ran for the first time.
  90. ///
  91. /// @param pid ID of the child process for which exit status should be
  92. /// returned.
  93. ///
  94. /// @return Exit code of the process.
  95. int getExitStatus(const pid_t pid) const;
  96. /// @brief Removes the status of the process with a specified PID.
  97. ///
  98. /// This method removes the status of the process with a specified PID.
  99. /// If the process is still running, the status is not removed and the
  100. /// exception is thrown.
  101. ///
  102. /// @param pid A process pid.
  103. void clearStatus(const pid_t pid);
  104. private:
  105. /// @brief A pointer to the implementation of this class.
  106. ProcessSpawnImpl* impl_;
  107. };
  108. }
  109. }
  110. #endif // PROCESS_SPAWN_H