added a very simple App::guardProcess() function
This commit is contained in:
		| @ -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); | ||||
|  | ||||
| @ -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) | ||||
| ///////////////////////////////// | ||||
|  | ||||
| @ -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); | ||||
| }*/ | ||||
|  | ||||
|  | ||||
| @ -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,9 +75,14 @@ public: | ||||
|  | ||||
| 	int run () | ||||
| 	{ | ||||
| 		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: | ||||
| 	QSE::TcpServerF<ClientHandler> server; | ||||
| @ -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);*/ | ||||
| 	} | ||||
|  | ||||
| @ -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);*/ | ||||
| 	} | ||||
|  | ||||
		Reference in New Issue
	
	Block a user