From 24b3afa684e57176f5068fd5896679ae0fa047ad Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Mon, 9 Jan 2023 11:29:22 +0100 Subject: Add process.h: is_running() --- Makefile | 41 ++++++++++++++++++++++++++++++++++++++++- debian/changelog | 6 ++++++ debian/control | 2 +- process.cpp | 27 +++++++++++++++++++++++++++ process.h | 7 +++++++ tests/Makefile | 5 +++++ tests/test-process.cpp | 34 ++++++++++++++++++++++++++++++++++ 7 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 process.cpp create mode 100644 process.h create mode 100644 tests/test-process.cpp diff --git a/Makefile b/Makefile index d7cbabb..2f30ad3 100644 --- a/Makefile +++ b/Makefile @@ -23,6 +23,7 @@ PROGSRC=\ file.cpp \ mime.cpp \ os.cpp \ + process.cpp \ stringhelper.cpp \ tempfile.cpp \ url.cpp @@ -90,7 +91,45 @@ ADD_DEP=Makefile DISTFILES=$(shell git ls-files 2>/dev/null) ifeq ($(DISTFILES),) DISTFILES= \ - +Makefile \ +archive.h \ +base64.cpp \ +base64.h \ +common.mk \ +debian/README.Debian \ +debian/changelog \ +debian/compat \ +debian/control \ +debian/copyright \ +debian/libreichwein-dev.install \ +debian/libreichwein0.install \ +debian/rules \ +debian/source/format \ +file.cpp \ +file.h \ +mime.cpp \ +mime.h \ +os.cpp \ +os.h \ +process.cpp \ +process.h \ +remote-install.sh \ +stringhelper.cpp \ +stringhelper.h \ +tempfile.cpp \ +tempfile.h \ +tests/Makefile \ +tests/test-archive.cpp \ +tests/test-base64.cpp \ +tests/test-file.cpp \ +tests/test-mime.cpp \ +tests/test-os.cpp \ +tests/test-process.cpp \ +tests/test-stringhelper.cpp \ +tests/test-tempfile.cpp \ +tests/test-url.cpp \ +url.cpp \ +url.h endif dist: clean diff --git a/debian/changelog b/debian/changelog index 1752e59..9c72966 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +libreichwein (1.2) UNRELEASED; urgency=medium + + * Added process.h+cpp: Reichwein::Process::is_running() + + -- Roland Reichwein Sat, 07 Jan 2023 19:15:07 +0100 + libreichwein (1.1) unstable; urgency=medium * Shared and static lib diff --git a/debian/control b/debian/control index 7e9d5cd..83988c8 100644 --- a/debian/control +++ b/debian/control @@ -2,7 +2,7 @@ Source: libreichwein Section: devel Priority: optional Maintainer: Roland Reichwein -Build-Depends: debhelper (>= 12), libboost-all-dev | libboost1.71-all-dev, clang | g++-9, llvm | g++-9, lld | g++-9, pkg-config, googletest, gcovr +Build-Depends: debhelper (>= 12), libboost-all-dev | libboost1.71-all-dev, clang | g++-9, llvm | g++-9, lld | g++-9, googletest, gcovr Standards-Version: 4.5.0 Homepage: http://www.reichwein.it/libreichwein/ diff --git a/process.cpp b/process.cpp new file mode 100644 index 0000000..b30ef3d --- /dev/null +++ b/process.cpp @@ -0,0 +1,27 @@ +#include "process.h" + +#include +#include + +#include "file.h" + +namespace fs = std::filesystem; + +bool Reichwein::Process::is_running(pid_t pid) +{ + fs::path pid_file{"/proc/" + std::to_string(pid) + "/stat"}; + if (!fs::exists(pid_file)) + return false; + + std::string s{Reichwein::File::getFile(pid_file)}; + + auto pos0{s.find(' ', 0)}; + pos0 = s.find(' ', pos0 + 1); + pos0++; + + auto pos1{s.find(' ', pos0 + 1)}; + + std::string state{s.substr(pos0, pos1 - pos0)}; + + return state == "R" || state == "S"; +} diff --git a/process.h b/process.h new file mode 100644 index 0000000..a47dd6c --- /dev/null +++ b/process.h @@ -0,0 +1,7 @@ +#pragma once + +#include + +namespace Reichwein::Process { + bool is_running(pid_t pid); +} // namespace diff --git a/tests/Makefile b/tests/Makefile index 6ae6f12..b95c91a 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -16,6 +16,7 @@ UNITS=\ file.cpp \ mime.cpp \ os.cpp \ + process.cpp \ stringhelper.cpp \ tempfile.cpp \ url.cpp @@ -26,6 +27,7 @@ UNITTESTS=\ test-file.cpp \ test-mime.cpp \ test-os.cpp \ + test-process.cpp \ test-stringhelper.cpp \ test-tempfile.cpp \ test-url.cpp @@ -69,6 +71,9 @@ mime.o: ../mime.cpp os.o: ../os.cpp $(CXX) $(CXXFLAGS) -o $@ -c $< +process.o: ../process.cpp + $(CXX) $(CXXFLAGS) -o $@ -c $< + stringhelper.o: ../stringhelper.cpp $(CXX) $(CXXFLAGS) -o $@ -c $< diff --git a/tests/test-process.cpp b/tests/test-process.cpp new file mode 100644 index 0000000..dfee83f --- /dev/null +++ b/tests/test-process.cpp @@ -0,0 +1,34 @@ +#include + +#include "process.h" + +#include + +class ProcessTest: public ::testing::Test +{ +protected: + ProcessTest(){ + } + + ~ProcessTest() override{ + } + + void SetUp() override + { + } + + void TearDown() override + { + } + +}; + +TEST_F(ProcessTest,is_running) { + auto pid{::getpid()}; + + EXPECT_NE(pid, -1); + + EXPECT_EQ(Reichwein::Process::is_running(pid), true); + + EXPECT_EQ(Reichwein::Process::is_running(999999999), false); +} -- cgit v1.2.3