#include "pidfile.h" #include "p2daemonAppEngine.h" #include #include #include #include SimpleMessageException(ChownFailed); PidFile::PidFile(const boost::filesystem::path & path) : Path(path) { auto pidDir = Path.parent_path(); if (!boost::filesystem::exists(pidDir)) { boost::filesystem::create_directories(pidDir); if (DaemonAppEngine::setGroup || DaemonAppEngine::setUser) { if (chown(pidDir.string().c_str(), DaemonAppEngine::setUser ? *DaemonAppEngine::setUser : -1, DaemonAppEngine::setGroup ? *DaemonAppEngine::setGroup : -1)) { boost::filesystem::remove(pidDir); throw ChownFailed(strerror(errno)); } } } boost::filesystem::ofstream pf(Path); pf << getpid(); if (DaemonAppEngine::setGroup || DaemonAppEngine::setUser) { if (chown(Path.string().c_str(), DaemonAppEngine::setUser ? *DaemonAppEngine::setUser : -1, DaemonAppEngine::setGroup ? *DaemonAppEngine::setGroup : -1)) { boost::filesystem::remove(Path); throw ChownFailed(strerror(errno)); } } } PidFile::~PidFile() { boost::filesystem::remove(Path); }