diff --git a/commonapicontrollableprocess/Makefile b/commonapicontrollableprocess/Makefile index be3a5ad..716d023 100644 --- a/commonapicontrollableprocess/Makefile +++ b/commonapicontrollableprocess/Makefile @@ -3,14 +3,14 @@ CXXFLAGS = -Wall -Wextra -pthread -O2 -fPIC -std=c++17 LIBNAME = libcommonpicontrollableprocess -INCLUDES = -Iinclude/comapi/controllableProcess -Iinclude/comApi -Iinclude/plugin -Iplugins/commonapicontrollableprocess +INCLUDES = -Iinclude/comapi/controllableProcess -I../include SRCS = src/controllableProcess.cpp OBJS = $(SRCS:.cpp=.o) -STATIC_LIB = lib$(LIBNAME).a -SHARED_LIB = lib$(LIBNAME).so +STATIC_LIB = $(LIBNAME).a +SHARED_LIB = $(LIBNAME).so all: $(STATIC_LIB) $(SHARED_LIB) diff --git a/commonapicontrollableprocess/include/comapi/controllableProcess/controllableProcess.hpp b/commonapicontrollableprocess/include/comapi/controllableProcess/controllableProcess.hpp index 2673a08..8c18ed0 100644 --- a/commonapicontrollableprocess/include/comapi/controllableProcess/controllableProcess.hpp +++ b/commonapicontrollableprocess/include/comapi/controllableProcess/controllableProcess.hpp @@ -1,10 +1,10 @@ -#ifndef COMMON_API_CONTROLLABLE_PROCESS_HPP_ -#define COMMON_API_CONTROLLABLE_PROCESS_HPP_ +#ifndef COMMON_API_CONTROLLABLE_PROCESS_HPP +#define COMMON_API_CONTROLLABLE_PROCESS_HPP #include #include -#include "plugin.hpp" +#include "plugin/plugin.hpp" namespace reactorFramework { diff --git a/commonapicontrollableprocess/include/comapi/controllableProcess/controllableProcessPlugin.hpp b/commonapicontrollableprocess/include/comapi/controllableProcess/controllableProcessPlugin.hpp index e723829..d2db0d9 100644 --- a/commonapicontrollableprocess/include/comapi/controllableProcess/controllableProcessPlugin.hpp +++ b/commonapicontrollableprocess/include/comapi/controllableProcess/controllableProcessPlugin.hpp @@ -1,8 +1,7 @@ #ifndef COMMON_API_CONTROLLABLE_PROCESS_PLUGIN_HPP #define COMMON_API_CONTROLLABLE_PROCESS_PLUGIN_HPP - -#include "defines.hpp" +#include "plugin/defines.hpp" #include "controllableProcess.hpp" #define COMMONAPICONTROLLABLEPROCESS_PLUGIN_CREATOR_NAME commonapi_controllable_process_create_plugin diff --git a/commonapidaemon/Makefile b/commonapidaemon/Makefile new file mode 100644 index 0000000..9a35a46 --- /dev/null +++ b/commonapidaemon/Makefile @@ -0,0 +1,33 @@ +CXX = g++ +CXXFLAGS = -Wall -Wextra -pthread -O2 -fPIC -std=c++17 + +LIBNAME = libcommonpidaemon + +INCLUDES = -Iinclude/ -I../include/ -I../commonapicontrollableprocess/include + +SRCS = src/Plugin.cpp + +OBJS = $(SRCS:.cpp=.o) + +STATIC_LIB = $(LIBNAME).a +SHARED_LIB = $(LIBNAME).so + +all: $(STATIC_LIB) $(SHARED_LIB) + +$(STATIC_LIB): $(OBJS) + @echo "Creating static library $@" + ar rcs $@ $(OBJS) + +$(SHARED_LIB): $(OBJS) + @echo "Creating shared library $@" + $(CXX) -shared -o $@ $(OBJS) + +src/%.o: src/%.cpp + @echo "Compiling $< into $@" + $(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@ + +clean: + @echo "Cleaning up" + rm -f $(OBJS) $(STATIC_LIB) $(SHARED_LIB) + +.PHONY: all clean diff --git a/commonapidaemon/include/Plugin.hpp b/commonapidaemon/include/Plugin.hpp new file mode 100644 index 0000000..6ed8e74 --- /dev/null +++ b/commonapidaemon/include/Plugin.hpp @@ -0,0 +1,43 @@ +#ifndef COMMON_API_DAEMON_PLUGIN_HPP +#define COMMON_API_DAEMON_PLUGIN_HPP + +#include "comapi/controllableProcess/controllableProcess.hpp" + +#include "plugin/pluginServices.hpp" + +#include + +namespace commonapidaemon +{ + +class Plugin : public std::enable_shared_from_this, + public reactorFramework::controllableprocess::ControllableProcess +{ +public: + Plugin(std::shared_ptr); + ~Plugin(); + + void setTerminateCb(const TerminateCb& cb) override; +private: + void handleSigTerm(); + void callTerminateCb(); + + enum class SignalState + { + NONE, + SIGNAL_RECEIVED, + SIGNAL_HANDLED, + }; + + std::shared_ptr pluginServices; + reactorFramework::FdMonitor& fdMonitor; + reactorFramework::signalMonitorService& signalMonitor; + reactorFramework::CallbackQueueService& callbackQueue; + + SignalState signalState; + TerminateCb terminateCb; +}; + +} + +#endif diff --git a/commonapidaemon/src/Plugin.cpp b/commonapidaemon/src/Plugin.cpp new file mode 100644 index 0000000..05c2ca8 --- /dev/null +++ b/commonapidaemon/src/Plugin.cpp @@ -0,0 +1,94 @@ +#include "Plugin.hpp" + +#include "plugin/callbackQueueService.hpp" +#include "plugin/fdMonitor.hpp" +#include "plugin/signalMonitorService.hpp" + +#include +#include +#include +#include +#include +#include +#include + +using namespace commonapidaemon; + +namespace +{ + std::once_flag once; + void unblockSigTerm() noexcept + { + sigset_t sset; + sigemptyset(&sset); + sigaddset(&sset, SIGTERM); + pthread_sigmask(SIG_UNBLOCK, &sset, nullptr); + } + + void unblockSigTermAtFork() noexcept + { + pthread_atfork(nullptr, nullptr, unblockSigTerm); + } +} + +Plugin::Plugin(std::shared_ptr pluginServices): + pluginServices(pluginServices), + fdMonitor(pluginServices->getFdMonitor()), + signalMonitor(pluginServices->getSignalMonitor()), + callbackQueue(pluginServices->getEventCallbackQueue()), + signalState(SignalState::NONE) +{ + + signalMonitor.add(SIGTERM, std::bind(&Plugin::handleSigTerm, this)); + std::call_once(once, unblockSigTermAtFork); +} + + +Plugin::~Plugin() +{ + signalMonitor.del(SIGTERM); +} + +void Plugin::setTerminateCb(const TerminateCb& cb) +{ + terminateCb = cb; + if(SignalState::SIGNAL_RECEIVED != signalState) + { + return; + } + std::weak_ptr weak(shared_from_this()); + + callbackQueue.post( + [weak]() + { + if(auto plugin = weak.lock()) + { + plugin->callTerminateCb(); + } + } + ); +} + + +void Plugin::handleSigTerm() +{ + if (signalState != SignalState::NONE) + { + return; + } + + signalState = SignalState::SIGNAL_RECEIVED; + + callTerminateCb(); +} + +void Plugin::callTerminateCb() +{ + if(!terminateCb) + { + return; + } + + signalState = SignalState::SIGNAL_HANDLED; + terminateCb(); +}