pid.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 PID_H
  15. #define PID_H
  16. #include <exceptions/exceptions.h>
  17. #include <boost/lexical_cast.hpp>
  18. #include <boost/shared_ptr.hpp>
  19. #include <fstream>
  20. #include <ostream>
  21. #include <string>
  22. namespace isc {
  23. namespace util {
  24. /// @brief Exception thrown when an error occurs during PID file processing.
  25. class pidFileError : public Exception {
  26. public:
  27. pidFileError(const char* file, size_t line, const char* what) :
  28. isc::Exception(file, line, what) { };
  29. };
  30. /// @brief Class to help with processing PID files
  31. ///
  32. /// This is a utility class to manipulate PID files. It provides
  33. /// functiosn for writing and deleting a file containing a PID as
  34. /// well as for extracting a PID from a file and checking if the
  35. /// process is still running.
  36. class pidFile {
  37. public:
  38. /// @brief Constructor
  39. ///
  40. /// @param filename PID filename.
  41. pidFile(const std::string& filename);
  42. /// @brief Destructor
  43. ~pidFile();
  44. /// @brief Read the PID in from the file and check it.
  45. ///
  46. /// Read the PID in from the file then use it to see if
  47. /// a process with that PID exists. If the file doesn't
  48. /// exist treat it as the process not being there.
  49. /// If the file exists but can't be read or it doesn't
  50. /// the proper format treat it as the process existing.
  51. ///
  52. /// @return true if the PID is in use, false otherwise
  53. bool check();
  54. /// @brief Write the PID to the file.
  55. void write(int);
  56. /// @brief Get PID of the proces and write it to the file.
  57. void write();
  58. /// @brief Delete the file.
  59. void deleteFile();
  60. /// @brief Returns the path to the PID file.
  61. std::string getFilename() const {
  62. return (filename_);
  63. }
  64. private:
  65. /// @brief PID filename
  66. std::string filename_;
  67. };
  68. } // namespace isc::util
  69. } // namespace isc
  70. #endif // PID_H