39 m_path((!path.empty()&&path.isRelative())?
Path::join(
Process::cwd(),path):path)
47 bool done = cleanup(
SignalSafe() , m_path.cstr() ) ;
62void G::PidFile::create(
const Path & pid_file )
64 if( !pid_file.
empty() )
73 file << pid.str() << std::endl ;
76 throw Error(
"cannot write file" , pid_file.
str() ) ;
79 static constexpr std::size_t buffer_size = 60U ;
80 static std::array<char,buffer_size> buffer {} ;
81 const char * cleanup_arg = &buffer[0] ;
82 if( buffer[0] ==
'\0' && pid_file.
size() < buffer.size() )
91G::Process::Id G::PidFile::read( SignalSafe ,
const char * path )
noexcept
93 int fd =
File::open( path , File::InOutAppend::In ) ;
95 return Process::Id::invalid() ;
97 constexpr std::size_t buffer_size = 11U ;
98 std::array<char,buffer_size> buffer {} ;
101 ssize_t rc =
File::read( fd , &buffer[0] , buffer_size-1U ) ;
104 return Process::Id::invalid() ;
106 return Process::Id( &buffer[0] , &buffer[0]+
static_cast<std::size_t
>(rc) ) ;
113 if( path ==
nullptr || *path ==
'\0' )
118 if( this_pid != file_pid )
121 return 0 == std::remove( path ) ;
131 if( !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.
static bool mkdir(const Path &dir, std::nothrow_t)
Creates a directory.
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 pid file if a path has been defined.
PidFile()
Default constructor.
~PidFile()
Destructor. Calls cleanup() to delete the file.
void mkdir()
Creates the directory if it does not already exist.
Path path() const
Returns the full path of the file.
static bool cleanup(SignalSafe, const char *path) noexcept
Deletes the specified pid file if it contains this process's id.
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.
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().