39 cleanup(
SignalSafe() , m_path.str().c_str() ) ;
53 void G::PidFile::create(
const Path & pid_file )
55 if( pid_file !=
Path() )
57 G_DEBUG(
"G::PidFile::create: \"" << pid_file <<
"\"" ) ;
62 file.open( pid_file.
str().c_str() , std::ios_base::out | std::ios_base::trunc ) ;
65 file << pid.str() << std::endl ;
68 throw Error(std::string(
"cannot create file: ")+pid_file.
str()) ;
69 Cleanup::add( cleanup , new_string_ignore_leak(pid_file.
str())->c_str() ) ;
73 bool G::PidFile::mine( SignalSafe safe ,
const char * path )
76 Process::Id this_pid ;
77 Process::Id file_pid( safe , path ) ;
78 return this_pid == file_pid ;
87 if( path && *path && mine(safe,path) )
98 if( valid() && ! m_path.isAbsolute() )
99 throw Error(std::string(
"must be an absolute path: ")+m_path.str()) ;
121 bool G::PidFile::valid()
const
123 return m_path !=
Path() ;
126 std::string * G::PidFile::new_string_ignore_leak(
const std::string & s )
128 return new std::string( s ) ;
void init(const Path &pid_file_path)
Used after default construction to make the object active.
std::string str() const
Returns the path string.
void check()
Throws an exception if the path is not absolute.
An empty structure that is used to indicate a signal-safe, reentrant implementation.
Path path() const
Returns the path as supplied to the constructor or init().
A combination of user-id and group-id, with a very low-level interface to the get/set/e/uid/gid funct...
~PidFile()
Destructor. Calls cleanup() to delete the file.
static Identity start(SignalSafe)
A signal-safe alternative to construction.
static void stop(SignalSafe, Identity)
A signal-safe alternative to destruction.
PidFile()
Default constructor.
static void cleanup(SignalSafe, const char *path)
Deletes the specified pid file if it contains this process's id.
bool committed() const
Returns true if commit() has been called with a valid path().
void commit()
Creates the file and installs signal handlers to cleanup() the file on abnormal process termination...
A Path object represents a file system path.
static void add(void(*fn)(SignalSafe, const char *), const char *arg)
Adds the given handler to the list of handlers that are to be called when the process terminates abno...