added a very simple App::guardProcess() function

This commit is contained in:
hyung-hwan 2018-09-05 14:52:51 +00:00
parent 6f314b0dd8
commit e8804b20ec
5 changed files with 62 additions and 21 deletions

View File

@ -53,6 +53,23 @@ public:
virtual void on_signal (int sig) { }
int subscribeToSignal (int sig, bool accept);
void unsubscribeFromSignal (int sig);
typedef void (*SignalHandler) (int sig);
static qse_size_t _sighrs[2][QSE_NSIGS];
// You may set a global signal handler with setSignalHandler().
// If an application is subscribing to a single with subscribeToSignal(),
// this function is doomed to fail. If a successful call to
// setSignalHandler() has been made withoutut unsetSingalHandler() called
// yet, a subsequence call to subscribeToSignal() is doomed to fail too.
// These two different interfaces are mutually exclusive.
static int setSignalHandler (int sig, SignalHandler sighr);
static int unsetSignalHandler (int sig);
int guardProcess (const qse_mchar_t* proc_name);
protected:
bool _root_only;
@ -78,21 +95,6 @@ public:
_SigLink _sig[QSE_NSIGS];
typedef void (*SignalHandler) (int sig);
static qse_size_t _sighrs[2][QSE_NSIGS];
int subscribeToSignal (int sig, bool accept);
void unsubscribeFromSignal (int sig);
// You may set a global signal handler with setSignalHandler().
// If an application is subscribing to a single with subscribeToSignal(),
// this function is doomed to fail. If a successful call to
// setSignalHandler() has been made withoutut unsetSingalHandler() called
// yet, a subsequence call to subscribeToSignal() is doomed to fail too.
// These two different interfaces are mutually exclusive.
static int setSignalHandler (int sig, SignalHandler sighr);
static int unsetSignalHandler (int sig);
protected:
static int set_signal_handler_no_mutex (int sig, SignalHandler sighr);
static int unset_signal_handler_no_mutex (int sig);

View File

@ -382,6 +382,27 @@ void App::unsubscribe_from_all_signals_no_mutex()
}
}
int App::guardProcess (const qse_mchar_t* proc_name)
{
// TODO: enhance it
while (1)
{
pid_t pid = ::fork();
if (pid == -1) return -1;
if (pid == 0) break; // child
int status;
while (::waitpid(pid, &status, 0) != pid);
if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
{
return 0;
}
}
// TODO: if (proc_name) qse_set_proc_name (proc_name);
return 1; // the caller must execute the actual work.
}
/////////////////////////////////
QSE_END_NAMESPACE(QSE)
/////////////////////////////////

View File

@ -53,3 +53,10 @@ void qse_sleep (const qse_ntime_t* interval)
#endif
}
/*
TODO:
int qse_set_proc_name (const qse_char_t* name)
{
::prctl(PR_SET_NAME, name, 0, 0, 0);
}*/

View File

@ -3,6 +3,7 @@
#include <qse/si/sio.h>
#include <qse/si/os.h>
#include <qse/cmn/mem.h>
#include <qse/cmn/str.h>
#include <qse/cmn/HeapMmgr.hpp>
#include <qse/si/App.hpp>
@ -12,10 +13,14 @@
# include <windows.h>
#endif
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/prctl.h>
#include <unistd.h>
class ClientHandler
{
public:
@ -70,8 +75,13 @@ public:
int run ()
{
this->server.setThreadStackSize (256000);
return this->server.start (QSE_T("[::]:9998,0.0.0.0:9998"));
if (this->guardProcess("myapp") > 0)
{
qse_printf (QSE_T("Stareting workd\n"));
this->server.setThreadStackSize (256000);
return this->server.start (QSE_T("[::]:9998,0.0.0.0:9998"));
}
return -1;
}
protected:
@ -87,7 +97,7 @@ MyApp app2 (&heap_mmgr);
MyApp app3 (&heap_mmgr);
MyApp app4 (&heap_mmgr);
app.subscribeToSignal (SIGINT, true) ;
app.subscribeToSignal (SIGINT, true);
app.subscribeToSignal (SIGTERM, true);
app4.subscribeToSignal (SIGINT, true);
@ -205,7 +215,7 @@ int main ()
}
else
{
sprintf (locale, ".%u", (unsigned int)codepage);
qse_mbsxfmt (locale, QSE_COUNTOF(locale), ".%u", (unsigned int)codepage);
setlocale (LC_ALL, locale);
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
}

View File

@ -3,6 +3,7 @@
#include <qse/si/sio.h>
#include <qse/si/os.h>
#include <qse/cmn/mem.h>
#include <qse/cmn/str.h>
#include <qse/cmn/HeapMmgr.hpp>
#include <qse/sttp/Sttp.hpp>
#include <qse/si/App.hpp>
@ -130,7 +131,7 @@ int main ()
}
else
{
sprintf (locale, ".%u", (unsigned int)codepage);
qse_mbsxfmt (locale, QSE_COUNTOF(locale), ".%u", (unsigned int)codepage);
setlocale (LC_ALL, locale);
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
}