mirror of
https://github.com/pocoproject/poco.git
synced 2025-08-13 05:55:42 +02:00

* 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>
123 lines
1.9 KiB
C++
123 lines
1.9 KiB
C++
//
|
|
// PIDFile.cpp
|
|
//
|
|
// Library: Foundation
|
|
// Package: Processes
|
|
// Module: PIDFile
|
|
//
|
|
// Copyright (c) 2023, Applied Informatics Software Engineering GmbH.
|
|
// Aleph ONE Software Engineering d.o.o.,
|
|
// and Contributors.
|
|
//
|
|
// SPDX-License-Identifier: BSL-1.0
|
|
//
|
|
|
|
|
|
#include "Poco/PIDFile.h"
|
|
#include "Poco/Path.h"
|
|
#include "Poco/File.h"
|
|
#include "Poco/Process.h"
|
|
#include "Poco/FileStream.h"
|
|
#include <fstream>
|
|
|
|
|
|
using Poco::Path;
|
|
using Poco::File;
|
|
using Poco::Process;
|
|
using Poco::FileInputStream;
|
|
using Poco::FileOutputStream;
|
|
|
|
|
|
namespace Poco {
|
|
|
|
|
|
PIDFile::PIDFile()
|
|
{
|
|
}
|
|
|
|
|
|
PIDFile::PIDFile(const std::string& fileName, bool write):
|
|
_fileName(fileName)
|
|
{
|
|
if (write) create();
|
|
}
|
|
|
|
|
|
PIDFile::~PIDFile()
|
|
{
|
|
destroy();
|
|
}
|
|
|
|
|
|
void PIDFile::setName(const std::string& fileName)
|
|
{
|
|
destroy();
|
|
_fileName = fileName;
|
|
create();
|
|
}
|
|
|
|
|
|
void PIDFile::create()
|
|
{
|
|
if (!_fileName.empty())
|
|
{
|
|
Path p(getFileName(_fileName));
|
|
if (!File(p.makeParent()).exists())
|
|
File(p).createDirectories();
|
|
_pid = static_cast<int>(Process::id());
|
|
FileOutputStream fos(_fileName);
|
|
fos << _pid; fos.close();
|
|
}
|
|
}
|
|
|
|
|
|
void PIDFile::destroy()
|
|
{
|
|
if (!_fileName.empty())
|
|
{
|
|
File f(_fileName);
|
|
if (f.exists()) f.remove();
|
|
_fileName.clear();
|
|
}
|
|
_pid = INVALID_PID;
|
|
}
|
|
|
|
|
|
bool PIDFile::exists() const
|
|
{
|
|
if (File(_fileName).exists())
|
|
{
|
|
FileInputStream fis(_fileName);
|
|
int fPID = 0;
|
|
if (fis.peek() != std::ifstream::traits_type::eof())
|
|
fis >> fPID;
|
|
return fPID == _pid;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
|
|
bool PIDFile::contains(const std::string& fileName, int pid)
|
|
{
|
|
if (File(fileName).exists())
|
|
{
|
|
FileInputStream fis(fileName);
|
|
int fPID = 0;
|
|
if (fis.peek() != std::ifstream::traits_type::eof())
|
|
fis >> fPID;
|
|
return fPID == pid;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
|
|
std::string& PIDFile::getFileName(std::string& pidFile)
|
|
{
|
|
Path p(pidFile);
|
|
pidFile = p.makeAbsolute().toString();
|
|
return pidFile;
|
|
}
|
|
|
|
|
|
} // namespace Poco
|