diff --git a/qse/include/qse/si/App.hpp b/qse/include/qse/si/App.hpp index a4729bd4..ba04c568 100644 --- a/qse/include/qse/si/App.hpp +++ b/qse/include/qse/si/App.hpp @@ -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); diff --git a/qse/lib/si/App.cpp b/qse/lib/si/App.cpp index 491c2821..e662480d 100644 --- a/qse/lib/si/App.cpp +++ b/qse/lib/si/App.cpp @@ -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) ///////////////////////////////// diff --git a/qse/lib/si/os.c b/qse/lib/si/os.c index 7be851e6..b28bd0ca 100644 --- a/qse/lib/si/os.c +++ b/qse/lib/si/os.c @@ -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); +}*/ + diff --git a/qse/samples/si/tcpsvr01.cpp b/qse/samples/si/tcpsvr01.cpp index d13ba10d..66e8b1f7 100644 --- a/qse/samples/si/tcpsvr01.cpp +++ b/qse/samples/si/tcpsvr01.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -12,10 +13,14 @@ # include #endif -#include #include #include + +#include +#include +#include + 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);*/ } diff --git a/qse/samples/si/tcpsvr02.cpp b/qse/samples/si/tcpsvr02.cpp index bc229630..fa69d73a 100644 --- a/qse/samples/si/tcpsvr02.cpp +++ b/qse/samples/si/tcpsvr02.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -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);*/ }