59void G::PidFile::create(
const Path & pid_file )
61 if( !pid_file.
empty() )
70 file << pid.str() << std::endl ;
73 throw Error(
"cannot write file" , pid_file.
str() ) ;
76 static constexpr std::size_t buffer_size = 60U ;
77 static std::array<char,buffer_size> buffer ;
78 const char * cleanup_arg = &buffer[0] ;
79 if( buffer[0] ==
'\0' && pid_file.
size() < buffer.size() )
88G::Process::Id G::PidFile::read( SignalSafe ,
const char * path )
noexcept
90 int fd =
File::open( path , File::InOutAppend::In ) ;
92 return Process::Id::invalid() ;
94 constexpr std::size_t buffer_size = 11U ;
95 std::array<char,buffer_size> buffer ;
98 ssize_t rc =
File::read( fd , &buffer[0] , buffer_size-1U ) ;
101 return Process::Id::invalid() ;
103 return Process::Id( &buffer[0] , &buffer[0]+
static_cast<std::size_t
>(rc) ) ;
110 if( path ==
nullptr || *path ==
'\0' )
115 if( this_pid != file_pid )
118 return 0 == std::remove( path ) ;
128 if( valid() && ! m_path.isAbsolute() )
129 throw Error(std::string(
"must be an absolute path: ")+m_path.str()) ;
151bool G::PidFile::valid()
const
153 return !m_path.empty() ;
static const char * strdup(const char *)
A strdup() function that makes it clear in the stack trace that leaks are expected.
static void add(bool(*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...
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 ssize_t read(int fd, char *, std::size_t) noexcept
Calls ::read() or equivalent.
A Path object represents a file system path.
const char * cstr() const noexcept
Returns the path string.
std::size_t size() const noexcept
Returns the length of the path string.
std::string str() const
Returns the path string.
bool empty() const noexcept
Returns true if size() is zero.
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.
PidFile()
Default constructor.
void check()
Throws an exception if the path is not absolute.
~PidFile()
Destructor. Calls cleanup() to delete the file.
Path path() const
Returns the path as supplied to the constructor or init().
static bool cleanup(SignalSafe, const char *path) noexcept
Deletes the specified pid file if it contains this process's id.
void init(const Path &pid_file_path)
Used after default construction to make the object active.
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.
An empty structure that is used to indicate a signal-safe, reentrant implementation.
static errno_t strncpy_s(char *dst, std::size_t n_dst, const char *src, std::size_t count) noexcept
Does the same as windows strncpy_s().