00001
00002
00003 #include <fesa-core/Utilities/ProcessRunUtils.h>
00004
00005 #include <fesa-core/Core/FesaDefs.h>
00006 #include <fesa-core/Exception/FesaException.h>
00007
00008 #include <cmw-log/Logger.h>
00009
00010 #include <cerrno>
00011 #include <csignal>
00012 #include <cstring>
00013 #include <iostream>
00014 #include <sstream>
00015 #include <unistd.h>
00016
00017
00018 namespace
00019 {
00020
00021 CMW::Log::Logger& logger = CMW::Log::LoggerFactory::getLogger("FESA.FWK.fesa-core.Core.ProcessRunUtils");
00022
00023 }
00024
00025
00026 namespace fesa
00027 {
00028
00029 namespace ProcessRunUtils
00030 {
00031
00032 void
00033 maskSignals()
00034 {
00035 sigset_t sigset;
00036 int rc = sigemptyset(&sigset);
00037 if (rc == -1)
00038 {
00039 std::ostringstream errorStringStream;
00040 errorStringStream << "sigemptyset(): " << std::strerror(errno);
00041 throw FesaException(__FILE__, __LINE__, errorStringStream.str());
00042 }
00043
00044 rc = sigaddset(&sigset, SIGHUP);
00045 if (rc == -1)
00046 {
00047 std::ostringstream errorStringStream;
00048 errorStringStream << "sigaddset(): " << std::strerror(errno);
00049 throw FesaException(__FILE__, __LINE__, errorStringStream.str());
00050 }
00051 rc = sigaddset(&sigset, SIGINT);
00052 if (rc == -1)
00053 {
00054 std::ostringstream errorStringStream;
00055 errorStringStream << "sigaddset(): " << std::strerror(errno);
00056 throw FesaException(__FILE__, __LINE__, errorStringStream.str());
00057 }
00058 rc = sigaddset(&sigset, SIGQUIT);
00059 if (rc == -1)
00060 {
00061 std::ostringstream errorStringStream;
00062 errorStringStream << "sigaddset(): " << std::strerror(errno);
00063 throw FesaException(__FILE__, __LINE__, errorStringStream.str());
00064 }
00065 rc = sigaddset(&sigset, SIGILL);
00066 if (rc == -1)
00067 {
00068 std::ostringstream errorStringStream;
00069 errorStringStream << "sigaddset(): " << std::strerror(errno);
00070 throw FesaException(__FILE__, __LINE__, errorStringStream.str());
00071 }
00072 rc = sigaddset(&sigset, SIGTRAP);
00073 if (rc == -1)
00074 {
00075 std::ostringstream errorStringStream;
00076 errorStringStream << "sigaddset(): " << std::strerror(errno);
00077 throw FesaException(__FILE__, __LINE__, errorStringStream.str());
00078 }
00079 rc = sigaddset(&sigset, SIGABRT);
00080 if (rc == -1)
00081 {
00082 std::ostringstream errorStringStream;
00083 errorStringStream << "sigaddset(): " << std::strerror(errno);
00084 throw FesaException(__FILE__, __LINE__, errorStringStream.str());
00085 }
00086 rc = sigaddset(&sigset, SIGBUS);
00087 if (rc == -1)
00088 {
00089 std::ostringstream errorStringStream;
00090 errorStringStream << "sigaddset(): " << std::strerror(errno);
00091 throw FesaException(__FILE__, __LINE__, errorStringStream.str());
00092 }
00093 rc = sigaddset(&sigset, SIGFPE);
00094 if (rc == -1)
00095 {
00096 std::ostringstream errorStringStream;
00097 errorStringStream << "sigaddset(): " << std::strerror(errno);
00098 throw FesaException(__FILE__, __LINE__, errorStringStream.str());
00099 }
00100 rc = sigaddset(&sigset, SIGSEGV);
00101 if (rc == -1)
00102 {
00103 std::ostringstream errorStringStream;
00104 errorStringStream << "sigaddset(): " << std::strerror(errno);
00105 throw FesaException(__FILE__, __LINE__, errorStringStream.str());
00106 }
00107 rc = sigaddset(&sigset, SIGTERM);
00108 if (rc == -1)
00109 {
00110 std::ostringstream errorStringStream;
00111 errorStringStream << "sigaddset(): " << std::strerror(errno);
00112 throw FesaException(__FILE__, __LINE__, errorStringStream.str());
00113 }
00114 rc = sigaddset(&sigset, SIGSTKFLT);
00115 if (rc == -1)
00116 {
00117 std::ostringstream errorStringStream;
00118 errorStringStream << "sigaddset(): " << std::strerror(errno);
00119 throw FesaException(__FILE__, __LINE__, errorStringStream.str());
00120 }
00121 rc = sigaddset(&sigset, SIGSTOP);
00122 if (rc == -1)
00123 {
00124 std::ostringstream errorStringStream;
00125 errorStringStream << "sigaddset(): " << std::strerror(errno);
00126 throw FesaException(__FILE__, __LINE__, errorStringStream.str());
00127 }
00128 rc = sigaddset(&sigset, SIGTSTP);
00129 if (rc == -1)
00130 {
00131 std::ostringstream errorStringStream;
00132 errorStringStream << "sigaddset(): " << std::strerror(errno);
00133 throw FesaException(__FILE__, __LINE__, errorStringStream.str());
00134 }
00135 rc = sigaddset(&sigset, SIGTTIN);
00136 if (rc == -1)
00137 {
00138 std::ostringstream errorStringStream;
00139 errorStringStream << "sigaddset(): " << std::strerror(errno);
00140 throw FesaException(__FILE__, __LINE__, errorStringStream.str());
00141 }
00142 rc = sigaddset(&sigset, SIGTTOU);
00143 if (rc == -1)
00144 {
00145 std::ostringstream errorStringStream;
00146 errorStringStream << "sigaddset(): " << std::strerror(errno);
00147 throw FesaException(__FILE__, __LINE__, errorStringStream.str());
00148 }
00149
00150 rc = sigaddset(&sigset, fesa::SIGFESA_RT_DOWN);
00151 if (rc == -1)
00152 {
00153 std::ostringstream errorStringStream;
00154 errorStringStream << "sigaddset(): " << std::strerror(errno);
00155 throw FesaException(__FILE__, __LINE__, errorStringStream.str());
00156 }
00157 rc = sigaddset(&sigset, fesa::SIGFESA_NOTIFICATION_FAILURE);
00158 if (rc == -1)
00159 {
00160 std::ostringstream errorStringStream;
00161 errorStringStream << "sigaddset(): " << std::strerror(errno);
00162 throw FesaException(__FILE__, __LINE__, errorStringStream.str());
00163 }
00164 rc = sigaddset(&sigset, fesa::SIGFESA_CHECK_PROCESS_ALIVE);
00165 if (rc == -1)
00166 {
00167 std::ostringstream errorStringStream;
00168 errorStringStream << "sigaddset(): " << std::strerror(errno);
00169 throw FesaException(__FILE__, __LINE__, errorStringStream.str());
00170 }
00171
00172 rc = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
00173 if (rc == -1)
00174 {
00175 std::ostringstream errorStringStream;
00176 errorStringStream << "pthread_sigmask(): " << std::strerror(errno);
00177 throw FesaException(__FILE__, __LINE__, errorStringStream.str());
00178 }
00179 }
00180
00181
00182 void
00183 shutDown()
00184 {
00185 LOG_WARNING_IF(logger, "Reacting to incoming signal. Shutting down the framework");
00186 std::exit(0);
00187 }
00188
00189
00190 void
00191 detachFromTerminal()
00192 {
00193 int rc = setsid();
00194 if (rc == -1)
00195 {
00196 std::ostringstream errorStringStream;
00197 errorStringStream << "setsid(): " << std::strerror(errno);
00198 std::cerr << "WARNING: Detaching from terminal not successful. " << errorStringStream.str() << std::endl;
00199 }
00200 }
00201
00202 }
00203
00204 }