poco/Foundation/include/Poco/PIDFile.h
Pavle Dragisic 70bb3a40de
Add ProcessRunner and PIDFile (#4225)
* feat(Foundation): PIDFile and ProcessRunner #4064
* feat(Thread): optional signal blocking on POSIX #2978
* fix(ProcessRunner):remove logger, code enhancement #4225
* feat(Foundation): add PIDFile and ProcessRunner Tests #4064
* fix(Foundation): failing ProcessRunner Test #4064
* fix(PIDFile): remove append argument #4064
* remove Windows TODO from ProcessRunner #4064
* feat(ProcessRunnerTest): add line to checkTimeout #4064
* fix(ProcessRunner): add done flag to run() #4064
* fix(ProcessRunnerTest): add missing pidFile argument #4064
* chore(ProcessRunner): remove comments #4064
* fix(ProcessRunner): add runCount flag #4064
* fix(test): SharedLibrary and Class tests paths
* fix(ProcessRunner): thread sanitizer reported data races #4064
* fix(build): pass env var to testrunner #4064
* chore(PIDFile): remove ; in comments #4064
* feat(ProcessRunner): add Win argument format #4064
* fix(Tests): add ProcessRunnerTest to vcxproj #4064
* fix(Tests): change path to TestApp #4064
* feat(Tests): windows processrunner tests #4064
* fix(Tests): duplicate  ProcessRunnerTest in TestSuite vcxproj  #4064
* fix(CodeQL): sw declaration hides variable  #4064
* fix test binaries path for cmake
* fix(Build): missing include/PIDFile.h buildWin #4064
* fix(Build): add PocoFoundation depend in buildWin #4064
* feat(ProcessRunner): test process launching multiple threads #2976

---------

Co-authored-by: Pavle <pavle@debian-gnu-linux-11.localdomain>
Co-authored-by: Alex Fabijanic <alex@pocoproject.org>
2023-11-24 20:22:01 +01:00

102 lines
1.9 KiB
C++

//
// PIDFile.h
//
// Library: Foundation
// Package: Processes
// Module: PIDFile
//
// Definition of the PIDFile class.
//
// Copyright (c) 2023, Applied Informatics Software Engineering GmbH.
// Aleph ONE Software Engineering d.o.o.,
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef Foundation_PIDFile_INCLUDED
#define Foundation_PIDFile_INCLUDED
#include "Poco/Foundation.h"
#include <memory>
namespace Poco {
class Foundation_API PIDFile
/// A utility class, creating process ID file on
/// construction and deleting it on destruction.
{
public:
using Ptr = std::unique_ptr<PIDFile>;
static const int INVALID_PID = -1;
PIDFile();
/// Creates the PIDFile.
PIDFile(const std::string& fileName, bool write = true);
/// Creates the PIDFile.
/// If `fileName` is not empty, creates the PID file.
/// If `write` is true, the file is written.
~PIDFile();
/// Destroys the PIDFile.
/// If fileName is not empty, deletes the PID file.
const std::string& getName() const;
/// Returns the file name.
void setName(const std::string& fileName);
/// Sets the file name.
void create();
/// Creates the file and writes PID into it.
void destroy();
/// Deletes the PID file and invalidates the held PID.
int getPID() const;
/// Returns the PID.
bool exists() const;
/// Returns true if PID file exists and its content is
/// equal to the held PID.
static bool contains(const std::string& fileName, int pid);
/// Returns true if the `fileName` contains the given `pid`.
static std::string& getFileName(std::string& pidFile);
/// Returns the file name.
private:
std::string _fileName;
int _pid = INVALID_PID;
};
//
// inlines
//
inline const std::string& PIDFile::getName() const
{
return _fileName;
}
inline int PIDFile::getPID() const
{
return _pid;
}
} // namespace Poco
#endif // Foundation_ProcessRunner_INCLUDED