36 Process::Id read( SignalSafe ,
const char * path ) ;
37 bool cleanup(
const Cleanup::Arg & ) noexcept ;
38 bool cleanup(
const Path & ) noexcept ;
48 m_path((!path.empty()&&path.isRelative())?
Path::join(
Process::cwd(),path):path)
54 static_assert(
noexcept(m_path.cstr()) ,
"" ) ;
55 static_assert(
noexcept(m_path.empty()) ,
"" ) ;
56 static_assert(
noexcept(PidFileImp::cleanup(m_path)),
"" ) ;
60 bool done = PidFileImp::cleanup( m_path ) ;
64 PidFileImp::cleanup( m_path ) ;
75void G::PidFile::create(
const Path & pid_file )
77 if( !pid_file.
empty() )
86 file << pid.str() << std::endl ;
89 throw Error(
"cannot write file" , pid_file.
str() ) ;
116bool G::PidFileImp::cleanup(
const Path & path )
noexcept
120 int fd =
File::open( path , File::InOutAppend::In ) ;
124 constexpr std::size_t buffer_size = 11U ;
125 std::array<char,buffer_size> buffer {} ;
128 ssize_t rc =
File::read( fd , buffer.data() , buffer_size-1U ) ;
133 Process::Id file_pid( buffer.data() , buffer.data()+
static_cast<std::size_t
>(rc) ) ;
135 Process::Id this_pid ;
136 if( this_pid != file_pid )
147bool G::PidFileImp::cleanup(
const Cleanup::Arg & arg )
noexcept
151 return cleanup( Path(arg.str()) ) ;
static Arg arg(const char *)
Duplicates a c-string for add().
static void add(Fn, Arg arg)
Adds the given handler to the list of handlers that are to be called when the process terminates abno...
An overload discriminator for G::File::open().
static void open(std::ofstream &, const Path &)
Calls open() on the given output file stream.
static void close(int fd) noexcept
Calls ::close() or equivalent.
static bool remove(const Path &path, std::nothrow_t) noexcept
Deletes the file or directory. Returns false on error.
static ssize_t read(int fd, char *, std::size_t) noexcept
Calls read() or equivalent.
static bool mkdir(const Path &dir, std::nothrow_t)
Creates a directory.
A Path object represents a file system path.
std::string str() const
Returns the path string.
bool empty() const noexcept
Returns true if the path is empty.
bool committed() const
Returns true if commit() has been called with a valid path().
void commit()
Creates the pid file if a path has been defined.
PidFile()
Default constructor.
~PidFile()
Destructor. Deletes the file.
void mkdir()
Creates the directory if it does not already exist.
Path path() const
Returns the full path of the file.
A static interface for doing things with processes.
static std::string strerror(int errno_)
Translates an 'errno' value into a meaningful diagnostic string.
static int errno_(const SignalSafe &=G::SignalSafe()) noexcept
Returns the process's current 'errno' value.
static void atExit() noexcept
Re-acquires special privileges just before process exit.