From 7755914a2c7bbb36bf11fd7a90edb007c7352933 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Fri, 20 Jul 2018 08:48:33 +0000 Subject: [PATCH] renamed AppRoot to App added App::setSignalHandler() and App::unsetSignalHandler() --- qse/configure | 33 +++++++ qse/configure.ac | 3 + qse/include/qse/config.h.in | 6 ++ qse/include/qse/si/{AppRoot.hpp => App.hpp} | 23 +++-- qse/include/qse/si/Makefile.am | 2 +- qse/include/qse/si/Makefile.in | 4 +- qse/lib/si/{AppRoot.cpp => App.cpp} | 99 +++++++++++++++++---- qse/lib/si/Makefile.am | 2 +- qse/lib/si/Makefile.in | 13 ++- qse/samples/si/tcpsvr01.cpp | 35 ++------ 10 files changed, 150 insertions(+), 70 deletions(-) rename qse/include/qse/si/{AppRoot.hpp => App.hpp} (83%) rename qse/lib/si/{AppRoot.cpp => App.cpp} (61%) diff --git a/qse/configure b/qse/configure index 0c464a52..eaf75aec 100755 --- a/qse/configure +++ b/qse/configure @@ -20926,6 +20926,33 @@ _ACEOF + { $as_echo "$as_me:${as_lineno-$LINENO}: checking numeric value of NSIG" >&5 +$as_echo_n "checking numeric value of NSIG... " >&6; } +if ${ax_cv_numvalof_NSIG+:} false; then : + $as_echo_n "(cached) " >&6 +else + if ac_fn_c_compute_int "$LINENO" "(NSIG)" "ax_cv_numvalof_NSIG" "#include +"; then : + +else + ax_cv_numvalof_NSIG=32 +fi + +fi +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ax_cv_numvalof_NSIG" >&5 +$as_echo "$ax_cv_numvalof_NSIG" >&6; } + + + +cat >>confdefs.h <<_ACEOF +#define NUMVALOF_NSIG $ax_cv_numvalof_NSIG +_ACEOF + + + + + + { $as_echo "$as_me:${as_lineno-$LINENO}: checking numeric value of SOCK_STREAM" >&5 $as_echo_n "checking numeric value of SOCK_STREAM... " >&6; } @@ -22433,6 +22460,12 @@ _ACEOF fi + +cat >>confdefs.h <<_ACEOF +#define QSE_NSIGS ${ax_cv_numvalof_NSIG} +_ACEOF + + if test ${ax_cv_numvalof_SOCK_STREAM} -ge 0 then diff --git a/qse/configure.ac b/qse/configure.ac index 0a24c34f..94ab8324 100644 --- a/qse/configure.ac +++ b/qse/configure.ac @@ -418,6 +418,7 @@ fi AC_CHECK_SIZEOF(mbstate_t,,[#include ]) AX_CHECK_NUMVALOF(MB_LEN_MAX,[32],[#include ]) AX_CHECK_NUMVALOF(PATH_MAX,[0],[#include ]) +AX_CHECK_NUMVALOF(NSIG,[32],[#include ]) AX_CHECK_NUMVALOF(SOCK_STREAM, [-1], [#include #include @@ -711,6 +712,8 @@ then AC_DEFINE_UNQUOTED(QSE_PATH_MAX, ${ax_cv_numvalof_PATH_MAX}, [PATH_MAX]) fi +AC_DEFINE_UNQUOTED(QSE_NSIGS, ${ax_cv_numvalof_NSIG}, [NSIG]) + if test ${ax_cv_numvalof_SOCK_STREAM} -ge 0 then AC_DEFINE_UNQUOTED(QSE_SOCK_STREAM, ${ax_cv_numvalof_SOCK_STREAM}, [SOCK_STREAM]) diff --git a/qse/include/qse/config.h.in b/qse/include/qse/config.h.in index 73d391b6..be617406 100644 --- a/qse/include/qse/config.h.in +++ b/qse/include/qse/config.h.in @@ -786,6 +786,9 @@ /* The size of `MB_LEN_MAX', as computed by valueof. */ #undef NUMVALOF_MB_LEN_MAX +/* The size of `NSIG', as computed by valueof. */ +#undef NUMVALOF_NSIG + /* The size of `PATH_MAX', as computed by valueof. */ #undef NUMVALOF_PATH_MAX @@ -871,6 +874,9 @@ /* Define if mode_t is signed */ #undef QSE_MODE_T_IS_SIGNED +/* NSIG */ +#undef QSE_NSIGS + /* offsetof(struct iovec, iov_base) */ #undef QSE_OFFSETOF_IOV_BASE diff --git a/qse/include/qse/si/AppRoot.hpp b/qse/include/qse/si/App.hpp similarity index 83% rename from qse/include/qse/si/AppRoot.hpp rename to qse/include/qse/si/App.hpp index a720a9b4..7c35cfc0 100644 --- a/qse/include/qse/si/AppRoot.hpp +++ b/qse/include/qse/si/App.hpp @@ -24,8 +24,8 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef _QSE_SI_APPROOT_H_ -#define _QSE_SI_APPROOT_H_ +#ifndef _QSE_SI_APP_H_ +#define _QSE_SI_APP_H_ #include #include @@ -35,11 +35,11 @@ QSE_BEGIN_NAMESPACE(QSE) ///////////////////////////////// -class AppRoot: public Uncopyable, public Types, public Mmged +class App: public Uncopyable, public Types, public Mmged { public: - AppRoot (Mmgr* mmgr): Mmged(mmgr), _root_only(false) {} - virtual ~AppRoot () {} + App (Mmgr* mmgr) QSE_CPP_NOEXCEPT: Mmged(mmgr), _root_only(false) {} + ~App () QSE_CPP_NOEXCEPT {} int daemonize (bool chdir_to_root = true, int fork_count = 1) QSE_CPP_NOEXCEPT; @@ -53,15 +53,12 @@ public: protected: bool _root_only; -#if 0 - uid_t saved_uid; - gid_t saved_gid; - gid_t saved_groups[NGROUPS_MAX]; - qse_size_t saved_ngroups; - void on_signal () QSE_CPP_NOEXCEPT; - void on_signal () QSE_CPP_NOEXCEPT; -#endif +public: + typedef void (*SignalHandler) (int sig); + static int setSignalHandler (int sig, SignalHandler sighr); + static int unsetSignalHandler (int sig); + static qse_size_t _sighrs[2][QSE_NSIGS]; }; ///////////////////////////////// diff --git a/qse/include/qse/si/Makefile.am b/qse/include/qse/si/Makefile.am index 448d742c..e00af158 100644 --- a/qse/include/qse/si/Makefile.am +++ b/qse/include/qse/si/Makefile.am @@ -29,7 +29,7 @@ pkginclude_HEADERS = \ if ENABLE_CXX pkginclude_HEADERS += \ - AppRoot.hpp \ + App.hpp \ Condition.hpp \ Mutex.hpp \ SocketAddress.hpp \ diff --git a/qse/include/qse/si/Makefile.in b/qse/include/qse/si/Makefile.in index 933240c4..856529e9 100644 --- a/qse/include/qse/si/Makefile.in +++ b/qse/include/qse/si/Makefile.in @@ -88,7 +88,7 @@ POST_UNINSTALL = : build_triplet = @build@ host_triplet = @host@ @ENABLE_CXX_TRUE@am__append_1 = \ -@ENABLE_CXX_TRUE@ AppRoot.hpp \ +@ENABLE_CXX_TRUE@ App.hpp \ @ENABLE_CXX_TRUE@ Condition.hpp \ @ENABLE_CXX_TRUE@ Mutex.hpp \ @ENABLE_CXX_TRUE@ SocketAddress.hpp \ @@ -136,7 +136,7 @@ am__can_run_installinfo = \ am__pkginclude_HEADERS_DIST = aio.h aio-pro.h aio-sck.h cnd.h dir.h \ fio.h fs.h glob.h intr.h log.h mtx.h mux.h nwad.h nwif.h \ nwio.h os.h pio.h rwl.h sck.h sinfo.h sio.h spl.h task.h thr.h \ - tio.h AppRoot.hpp Condition.hpp Mutex.hpp SocketAddress.hpp \ + tio.h App.hpp Condition.hpp Mutex.hpp SocketAddress.hpp \ Socket.hpp SpinLock.hpp TcpServer.hpp Thread.hpp am__vpath_adj_setup = srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`; am__vpath_adj = case $$p in \ diff --git a/qse/lib/si/AppRoot.cpp b/qse/lib/si/App.cpp similarity index 61% rename from qse/lib/si/AppRoot.cpp rename to qse/lib/si/App.cpp index 22dc9e91..25ca1d0b 100644 --- a/qse/lib/si/AppRoot.cpp +++ b/qse/lib/si/App.cpp @@ -24,7 +24,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include +#include #include #include "../cmn/syscall.h" #include @@ -33,7 +33,7 @@ QSE_BEGIN_NAMESPACE(QSE) ///////////////////////////////// -int AppRoot::daemonize (bool chdir_to_root, int fork_count) QSE_CPP_NOEXCEPT +int App::daemonize (bool chdir_to_root, int fork_count) QSE_CPP_NOEXCEPT { if (this->_root_only && QSE_GETEUID() != 0) return -1; @@ -103,23 +103,23 @@ int AppRoot::daemonize (bool chdir_to_root, int fork_count) QSE_CPP_NOEXCEPT -int AppRoot::chroot (const qse_mchar_t* mpath) QSE_CPP_NOEXCEPT +int App::chroot (const qse_mchar_t* mpath) QSE_CPP_NOEXCEPT { return QSE_CHROOT (mpath); } -int AppRoot::chroot (const qse_wchar_t* wpath) QSE_CPP_NOEXCEPT +int App::chroot (const qse_wchar_t* wpath) QSE_CPP_NOEXCEPT { qse_mchar_t* mpath; mpath = qse_wcstombsdup (wpath, QSE_NULL, this->getMmgr()); if (!mpath) return -1; - int n = AppRoot::chroot ((const qse_mchar_t*)mpath); + int n = App::chroot ((const qse_mchar_t*)mpath); this->getMmgr()->dispose (mpath); return n; } #if 0 -int AppRoot::switchPrivilege (int gid, int uid, bool permanently) +int App::switchPrivilege (int gid, int uid, bool permanently) { gid = QSE_GETGID(); uid = QSE_GETUID(); @@ -140,7 +140,7 @@ int AppRoot::switchPrivilege (int gid, int uid, bool permanently) } } -int AppRoot::restorePrivilege () +int App::restorePrivilege () { if (QSE_GETEUID() != this->saved_euid) seteuid (this->saved_euid); if (QSE_GETEGID() != this->saved_egid) setegid (this->saved_egid); @@ -149,22 +149,87 @@ int AppRoot::restorePrivilege () } #endif -#if 0 -int main () +qse_size_t App::_sighrs[2][QSE_NSIGS] = { { 0, }, }; + +static void dispatch_signal (int sig) { - AppRoot app; + ((App::SignalHandler)App::_sighrs[0][sig]) (sig); + if (App::_sighrs[1][sig] && App::_sighrs[1][sig] != (qse_size_t)SIG_IGN && App::_sighrs[1][sig] != (qse_size_t)SIG_DFL) + { + ((App::SignalHandler)App::_sighrs[1][sig]) (sig); + } +} - app.daemonize(); - app.chuser (); - app.chgroup (); - app.chroot (); +static void dispatch_siginfo (int sig, siginfo_t* si, void* ctx) +{ + ((App::SignalHandler)App::_sighrs[0][sig]) (sig); + if (App::_sighrs[1][sig] && App::_sighrs[1][sig] != (qse_size_t)SIG_IGN && App::_sighrs[1][sig] != (qse_size_t)SIG_DFL) + { + ((void(*)(int, siginfo_t*, void*))App::_sighrs[1][sig]) (sig, si, ctx); + } +} - app.catchSignal (SIGINT, xxxxx); - app.catchSignal (SIGTERM, xxx); +// i don't want to use sigset_t in App.hpp. +// so i place oldsi here as a static variable instead of +// static member variable of App +static struct +{ + sigset_t sa_mask; + int sa_flags; +} oldsi[QSE_NSIGS] = { { 0, 0 }, }; + +int App::setSignalHandler (int sig, SignalHandler sighr) +{ + if (App::_sighrs[0][sig]) return -1; // already set + + struct sigaction sa, oldsa; + + if (::sigaction(sig, QSE_NULL, &oldsa) == -1) return -1; + + if (oldsa.sa_flags & SA_SIGINFO) + { + sa.sa_sigaction = dispatch_siginfo; + sigemptyset (&sa.sa_mask); + sa.sa_flags |= SA_SIGINFO; + } + else + { + sa.sa_handler = dispatch_signal; + sigemptyset (&sa.sa_mask); + sa.sa_flags = 0; + //sa.sa_flags |= SA_INTERUPT; + //sa.sa_flags |= SA_RESTART; + } + + if (::sigaction(sig, &sa, QSE_NULL) == -1) return -1; + + App::_sighrs[0][sig] = (qse_size_t)sighr; + App::_sighrs[1][sig] = (qse_size_t)oldsa.sa_handler; + oldsi[sig].sa_mask = oldsa.sa_mask; + oldsi[sig].sa_flags = oldsa.sa_flags; return 0; } -#endif + +int App::unsetSignalHandler (int sig) +{ + if (!App::_sighrs[0][sig]) return -1; + + struct sigaction sa; + + sa.sa_mask = oldsi[sig].sa_mask; + sa.sa_flags = oldsi[sig].sa_flags; + if (sa.sa_flags & SA_SIGINFO) + sa.sa_sigaction = (void(*)(int,siginfo_t*,void*))App::_sighrs[1][sig]; + else + sa.sa_handler = (SignalHandler)App::_sighrs[1][sig]; + + if (sigaction (sig, &sa, QSE_NULL) <= -1) return -1; + + App::_sighrs[0][sig] = 0; + App::_sighrs[1][sig] = 0; + return 0; +} ///////////////////////////////// QSE_END_NAMESPACE(QSE) diff --git a/qse/lib/si/Makefile.am b/qse/lib/si/Makefile.am index cdabc2ac..5b8be9d6 100644 --- a/qse/lib/si/Makefile.am +++ b/qse/lib/si/Makefile.am @@ -53,7 +53,7 @@ libqsesi_la_LIBADD = -lqsecmn $(PTHREAD_LIBS) $(SSL_LIBS) if ENABLE_CXX lib_LTLIBRARIES += libqsesixx.la libqsesixx_la_SOURCES = \ - AppRoot.cpp \ + App.cpp \ SocketAddress.cpp \ Socket.cpp \ TcpServer.cpp \ diff --git a/qse/lib/si/Makefile.in b/qse/lib/si/Makefile.in index cdf57577..8b587d0c 100644 --- a/qse/lib/si/Makefile.in +++ b/qse/lib/si/Makefile.in @@ -161,11 +161,10 @@ libqsesi_la_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ $(LIBTOOLFLAGS) --mode=link $(CCLD) $(libqsesi_la_CFLAGS) \ $(CFLAGS) $(libqsesi_la_LDFLAGS) $(LDFLAGS) -o $@ libqsesixx_la_DEPENDENCIES = -am__libqsesixx_la_SOURCES_DIST = AppRoot.cpp SocketAddress.cpp \ - Socket.cpp TcpServer.cpp Thread.cpp -@ENABLE_CXX_TRUE@am_libqsesixx_la_OBJECTS = AppRoot.lo \ -@ENABLE_CXX_TRUE@ SocketAddress.lo Socket.lo TcpServer.lo \ -@ENABLE_CXX_TRUE@ Thread.lo +am__libqsesixx_la_SOURCES_DIST = App.cpp SocketAddress.cpp Socket.cpp \ + TcpServer.cpp Thread.cpp +@ENABLE_CXX_TRUE@am_libqsesixx_la_OBJECTS = App.lo SocketAddress.lo \ +@ENABLE_CXX_TRUE@ Socket.lo TcpServer.lo Thread.lo libqsesixx_la_OBJECTS = $(am_libqsesixx_la_OBJECTS) libqsesixx_la_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CXX \ $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=link $(CXXLD) \ @@ -470,7 +469,7 @@ libqsesi_la_CFLAGS = $(PTHREAD_CFLAGS) libqsesi_la_LDFLAGS = -L../cmn -version-info 1:0:0 -no-undefined libqsesi_la_LIBADD = -lqsecmn $(PTHREAD_LIBS) $(SSL_LIBS) @ENABLE_CXX_TRUE@libqsesixx_la_SOURCES = \ -@ENABLE_CXX_TRUE@ AppRoot.cpp \ +@ENABLE_CXX_TRUE@ App.cpp \ @ENABLE_CXX_TRUE@ SocketAddress.cpp \ @ENABLE_CXX_TRUE@ Socket.cpp \ @ENABLE_CXX_TRUE@ TcpServer.cpp \ @@ -559,7 +558,7 @@ mostlyclean-compile: distclean-compile: -rm -f *.tab.c -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/AppRoot.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/App.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Socket.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SocketAddress.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TcpServer.Plo@am__quote@ diff --git a/qse/samples/si/tcpsvr01.cpp b/qse/samples/si/tcpsvr01.cpp index 01690bad..e732dae4 100644 --- a/qse/samples/si/tcpsvr01.cpp +++ b/qse/samples/si/tcpsvr01.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include @@ -110,35 +111,10 @@ static int test1 (void) return 0; } -static void handle_sigint (int sig, siginfo_t* siginfo, void* ctx) +static void handle_sigint (int sig) { if (g_server) g_server->stop (); } - -static void set_signal (int sig, void(*handler)(int, siginfo_t*, void*)) -{ - struct sigaction sa; - - memset (&sa, 0, sizeof(sa)); - /*sa.sa_handler = handler;*/ - sa.sa_flags = SA_SIGINFO; - sa.sa_sigaction = handler; - sigemptyset (&sa.sa_mask); - - sigaction (sig, &sa, NULL); -} - -static void set_signal_to_default (int sig) -{ - struct sigaction sa; - - memset (&sa, 0, sizeof(sa)); - sa.sa_handler = SIG_DFL; - sa.sa_flags = 0; - sigemptyset (&sa.sa_mask); - - sigaction (sig, &sa, NULL); -} int main () { @@ -161,13 +137,14 @@ int main () /*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ #endif - set_signal (SIGINT, handle_sigint); - qse_open_stdsios (); + + QSE::App::setSignalHandler (SIGINT, handle_sigint); test1(); + QSE::App::unsetSignalHandler (SIGINT); + qse_close_stdsios (); - set_signal_to_default (SIGINT); return 0; }