From f1f23b0047bf11eb8f904f4da4e5cc2bb236bf06 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Thu, 15 Sep 2011 23:40:27 +0300 Subject: [PATCH 01/16] Another way of splitting c- and cpp- files --- projects/stargazer/plugins/Makefile.in | 14 +++++++++++--- stglibs/Makefile.in | 19 +++++++++++-------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/projects/stargazer/plugins/Makefile.in b/projects/stargazer/plugins/Makefile.in index 2b255304..faee2ad8 100644 --- a/projects/stargazer/plugins/Makefile.in +++ b/projects/stargazer/plugins/Makefile.in @@ -9,6 +9,7 @@ OBJS = $(notdir $(patsubst %.cpp, %.o, $(patsubst %.c, %.o, $(SRCS)))) LN = ln CXXFLAGS += -fPIC +CFLAGS += -fPIC LDFLAGS += -shared -L$(DIR_LIB) -Wl,-rpath,$(PREFIX)/usr/lib/stg vpath %.a $(DIR_LIB) @@ -38,6 +39,13 @@ endif deps: $(SRCS) ../../../../../Makefile.conf @>deps ;\ for file in $(SRCS); do\ - echo "`$(CC) $(CXXFLAGS) $(DEFS) $(SEARCH_DIRS) -MM $$file` Makefile ../../../../../Makefile.conf" >> deps ;\ - echo -e '\t$$(CC) -c $$< $(CXXFLAGS) $(SEARCH_DIRS) $(DEFS)' >> deps ;\ - done + echo "$$file" | grep ".c$$" > /dev/null;\ + if [ $$? == 0 ];\ + then\ + echo "`$(CC) $(CFLAGS) $(DEFS) $(SEARCH_DIRS) -MM $$file` Makefile ../../../../../Makefile.conf" >> deps ;\ + echo -e '\t$$(CC) -c $$< $(CFLAGS) $(SEARCH_DIRS) $(DEFS)' >> deps ;\ + else\ + echo "`$(CXX) $(CXXFLAGS) $(DEFS) $(SEARCH_DIRS) -MM $$file` Makefile ../../../../../Makefile.conf" >> deps ;\ + echo -e '\t$$(CXX) -c $$< $(CXXFLAGS) $(SEARCH_DIRS) $(DEFS)' >> deps ;\ + fi;\ + done; diff --git a/stglibs/Makefile.in b/stglibs/Makefile.in index d86cac3c..a016fed5 100644 --- a/stglibs/Makefile.in +++ b/stglibs/Makefile.in @@ -64,12 +64,15 @@ endif deps: $(SRCS) ../../Makefile.conf @>deps ;\ - for file in `echo "$(SRCS)" | grep ".c$$"`; do\ - echo "`$(CC) $(CFLAGS) $(SEARCH_DIRS) $(DEFS) -MM $$file` Makefile ../../Makefile.conf" >> deps ;\ - echo -e '\t$$(CC) $(CFLAGS) $(SEARCH_DIRS) $(DEFS) -c $$<' >> deps ;\ - done;\ - for file in `echo "$(SRCS)" | grep ".cpp$$"`; do\ - echo "`$(CXX) $(CXXFLAGS) $(SEARCH_DIRS) $(DEFS) -MM $$file` Makefile ../../Makefile.conf" >> deps ;\ - echo -e '\t$$(CXX) $(CXXFLAGS) $(SEARCH_DIRS) $(DEFS) -c $$<' >> deps ;\ - done\ + for file in $(SRCS); do\ + echo "$$file" | grep ".c$$" > /dev/null;\ + if [ $$? == 0 ];\ + then\ + echo "`$(CC) $(CFLAGS) $(SEARCH_DIRS) $(DEFS) -MM $$file` Makefile ../../Makefile.conf" >> deps ;\ + echo -e '\t$$(CC) $(CFLAGS) $(SEARCH_DIRS) $(DEFS) -c $$<' >> deps ;\ + else\ + echo "`$(CXX) $(CXXFLAGS) $(SEARCH_DIRS) $(DEFS) -MM $$file` Makefile ../../Makefile.conf" >> deps ;\ + echo -e '\t$$(CXX) $(CXXFLAGS) $(SEARCH_DIRS) $(DEFS) -c $$<' >> deps ;\ + fi;\ + done -- 2.44.2 From bfb8d7685e998fbcafcb5315b3c50a77760b7e48 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Thu, 15 Sep 2011 23:41:11 +0300 Subject: [PATCH 02/16] Extra semicolon removed --- projects/stargazer/plugins/Makefile.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/stargazer/plugins/Makefile.in b/projects/stargazer/plugins/Makefile.in index faee2ad8..961ba3aa 100644 --- a/projects/stargazer/plugins/Makefile.in +++ b/projects/stargazer/plugins/Makefile.in @@ -48,4 +48,4 @@ deps: $(SRCS) ../../../../../Makefile.conf echo "`$(CXX) $(CXXFLAGS) $(DEFS) $(SEARCH_DIRS) -MM $$file` Makefile ../../../../../Makefile.conf" >> deps ;\ echo -e '\t$$(CXX) -c $$< $(CXXFLAGS) $(SEARCH_DIRS) $(DEFS)' >> deps ;\ fi;\ - done; + done -- 2.44.2 From 53c1823aaef2eb8d547a8eed8cfe12fe7204ca79 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Fri, 16 Sep 2011 19:11:58 +0300 Subject: [PATCH 03/16] Replace deprecated usleep with POSIX-compliant nanosleep --- projects/rscriptd/listener.cpp | 12 +++++++---- projects/rscriptd/main.cpp | 3 ++- projects/sgauth/main.cpp | 3 ++- projects/sgauth/web.cpp | 3 ++- projects/sgauthstress/main.cpp | 3 ++- .../authorization/inetaccess/inetaccess.cpp | 21 +++++++++++++------ .../plugins/authorization/stress/stress.cpp | 3 ++- .../plugins/capture/cap_nf/cap_nf.cpp | 6 ++++-- .../capture/divert_freebsd/divert_cap.cpp | 3 ++- .../capture/ether_freebsd/ether_cap.cpp | 21 ++++++++++++------- .../plugins/capture/ether_linux/ether_cap.cpp | 8 +++++-- .../plugins/capture/ipq_linux/ipq_cap.cpp | 6 ++++-- .../configuration/rpcconfig/rpcconfig.cpp | 12 +++++++---- .../plugins/configuration/sgconfig/rsconf.cpp | 2 +- .../configuration/sgconfig/stgconfig.cpp | 3 ++- .../stargazer/plugins/other/radius/radius.cpp | 3 ++- .../plugins/other/rscript/rscript.cpp | 3 ++- stglibs/ia.lib/ia.cpp | 6 ++++-- stglibs/pinger.lib/pinger.cpp | 9 +++++--- 19 files changed, 87 insertions(+), 43 deletions(-) diff --git a/projects/rscriptd/listener.cpp b/projects/rscriptd/listener.cpp index 9e03988c..d4f2dc93 100644 --- a/projects/rscriptd/listener.cpp +++ b/projects/rscriptd/listener.cpp @@ -102,14 +102,16 @@ running = false; printfd(__FILE__, "LISTENER::Stop()\n"); -usleep(500000); +struct timespec ts = {0, 500000000}; +nanosleep(&ts, NULL); if (!processorStopped) { //5 seconds to thread stops itself for (int i = 0; i < 25 && !processorStopped; i++) { - usleep(200000); + struct timespec ts = {0, 200000000}; + nanosleep(&ts, NULL); } //after 5 seconds waiting thread still running. now killing it @@ -130,7 +132,8 @@ if (!receiverStopped) //5 seconds to thread stops itself for (int i = 0; i < 25 && !receiverStopped; i++) { - usleep(200000); + struct timespec ts = {0, 200000000}; + nanosleep(&ts, NULL); } //after 5 seconds waiting thread still running. now killing it @@ -196,7 +199,8 @@ processorStopped = false; while (running) { - usleep(500000); + struct timespec ts = {0, 500000000}; + nanosleep(&ts, NULL); if (!pending.empty()) ProcessPending(); ProcessTimeouts(); diff --git a/projects/rscriptd/main.cpp b/projects/rscriptd/main.cpp index 7e7f7f35..2739758c 100644 --- a/projects/rscriptd/main.cpp +++ b/projects/rscriptd/main.cpp @@ -430,7 +430,8 @@ WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++"); while (nonstop.GetStatus()) { - usleep(100000); + struct timespec ts = {0, 100000000}; + nanosleep(&ts, NULL); } listener->Stop(); diff --git a/projects/sgauth/main.cpp b/projects/sgauth/main.cpp index 96c58203..2af78b71 100644 --- a/projects/sgauth/main.cpp +++ b/projects/sgauth/main.cpp @@ -239,7 +239,8 @@ clnp->Connect(); while (1) { - usleep(200000); + struct timespec ts = {0, 200000000}; + nanosleep(&ts, NULL); char state[20]; diff --git a/projects/sgauth/web.cpp b/projects/sgauth/web.cpp index 6e2d4aca..ab169cc4 100644 --- a/projects/sgauth/web.cpp +++ b/projects/sgauth/web.cpp @@ -182,7 +182,8 @@ while (1) #ifdef WIN32 Sleep(1000); #else - usleep(1000000); + struct timespec ts = {1, 0}; + nanosleep(&ts, NULL); #endif exit(0); } diff --git a/projects/sgauthstress/main.cpp b/projects/sgauthstress/main.cpp index 2388254d..b0819c3c 100644 --- a/projects/sgauthstress/main.cpp +++ b/projects/sgauthstress/main.cpp @@ -157,7 +157,8 @@ std::cout << "Successfully loaded " << proto.UserCount() << " users" << std::end running = true; while (running) { - usleep(200000); + struct timespec ts = {0, 200000000}; + nanosleep(&ts, NULL); } proto.Stop(); diff --git a/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp b/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp index 5e468d0f..4986080e 100644 --- a/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp +++ b/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp @@ -30,7 +30,7 @@ #include #include -#include // usleep, close +#include // close #include #include @@ -428,7 +428,8 @@ if (isRunningRun) //5 seconds to thread stops itself for (int i = 0; i < 25 && isRunningRun; i++) { - usleep(200000); + struct timespec ts = {0, 200000000}; + nanosleep(&ts, NULL); } //after 5 seconds waiting thread still running. now killing it @@ -441,7 +442,10 @@ if (isRunningRun) return -1; } for (int i = 0; i < 25 && isRunningRun; ++i) - usleep(200000); + { + struct timespec ts = {0, 200000000}; + nanosleep(&ts, NULL); + } if (isRunningRun) { printfd(__FILE__, "Failed to stop recv thread\n"); @@ -461,7 +465,8 @@ if (isRunningRunTimeouter) //5 seconds to thread stops itself for (int i = 0; i < 25 && isRunningRunTimeouter; i++) { - usleep(200000); + struct timespec ts = {0, 200000000}; + nanosleep(&ts, NULL); } //after 5 seconds waiting thread still running. now killing it @@ -473,7 +478,10 @@ if (isRunningRunTimeouter) return -1; } for (int i = 0; i < 25 && isRunningRunTimeouter; ++i) - usleep(200000); + { + struct timespec ts = {0, 200000000}; + nanosleep(&ts, NULL); + } if (isRunningRunTimeouter) { printfd(__FILE__, "Failed to stop timeouter thread\n"); @@ -525,7 +533,8 @@ int a = -1; string monFile = ia->stgSettings->GetMonitorDir() + "/inetaccess_t"; while (ia->nonstop) { - usleep(20000); + struct timespec ts = {0, 20000000}; + nanosleep(&ts, NULL); ia->Timeouter(); // TODO change counter to timer and MONITOR_TIME_DELAY_SEC if (++a % (50 * 60) == 0 && ia->stgSettings->GetMonitoring()) diff --git a/projects/stargazer/plugins/authorization/stress/stress.cpp b/projects/stargazer/plugins/authorization/stress/stress.cpp index 5d41a91d..7273ed48 100644 --- a/projects/stargazer/plugins/authorization/stress/stress.cpp +++ b/projects/stargazer/plugins/authorization/stress/stress.cpp @@ -176,7 +176,8 @@ if (isRunning) { if (!isRunning) break; - usleep(200000); + struct timespec ts = {0, 200000000}; + nanosleep(&ts, NULL); } //after 5 seconds waiting thread still running. now killing it diff --git a/projects/stargazer/plugins/capture/cap_nf/cap_nf.cpp b/projects/stargazer/plugins/capture/cap_nf/cap_nf.cpp index 9ed50756..fb7d2cc5 100644 --- a/projects/stargazer/plugins/capture/cap_nf/cap_nf.cpp +++ b/projects/stargazer/plugins/capture/cap_nf/cap_nf.cpp @@ -148,7 +148,8 @@ if (portU && !stoppedUDP) CloseUDP(); for (int i = 0; i < 25 && !stoppedUDP; ++i) { - usleep(200000); + struct timespec ts = {0, 200000000}; + nanosleep(&ts, NULL); } if (stoppedUDP) { @@ -170,7 +171,8 @@ if (portT && !stoppedTCP) CloseTCP(); for (int i = 0; i < 25 && !stoppedTCP; ++i) { - usleep(200000); + struct timespec ts = {0, 200000000}; + nanosleep(&ts, NULL); } if (stoppedTCP) { diff --git a/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.cpp b/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.cpp index 98bf1b6c..5927727e 100644 --- a/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.cpp +++ b/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.cpp @@ -130,7 +130,8 @@ for (i = 0; i < 25; i++) if (!isRunning) break; - usleep(200000); + struct timespec ts = {0, 200000000}; + nanosleep(&ts, NULL); } //after 5 seconds waiting thread still running. now killing it diff --git a/projects/stargazer/plugins/capture/ether_freebsd/ether_cap.cpp b/projects/stargazer/plugins/capture/ether_freebsd/ether_cap.cpp index c415e691..108c4e0d 100644 --- a/projects/stargazer/plugins/capture/ether_freebsd/ether_cap.cpp +++ b/projects/stargazer/plugins/capture/ether_freebsd/ether_cap.cpp @@ -180,7 +180,8 @@ for (i = 0; i < 25; i++) if (!isRunning) break; - usleep(200000); + struct timespec ts = {0, 200000000}; + nanosleep(&ts, NULL); } //after 5 seconds waiting thread still running. now killing it @@ -213,12 +214,11 @@ char * iface; while (dc->nonstop) { - dc->BPFCapRead((char*)&hdr, 68 + 14, &iface); + if (dc->BPFCapRead((char*)&hdr, 68 + 14, &iface)) + continue; if (!(hdr[12] == 0x8 && hdr[13] == 0x0)) - { continue; - } dc->traffCnt->Process(*rpp); } @@ -313,12 +313,16 @@ for (unsigned int i = 0; i < polld.size(); i++) { if (polld[i].revents & POLLIN) { - BPFCapRead(buffer, blen, capIface, &bpfData[i]); + if (BPFCapRead(buffer, blen, capIface, &bpfData[i])) + { + polld[i].revents = 0; + continue; + } polld[i].revents = 0; return 0; } } -return 0; +return -1; } //----------------------------------------------------------------------------- int BPF_CAP::BPFCapRead(char * buffer, int blen, char **, BPF_DATA * bd) @@ -328,8 +332,9 @@ if (bd->canRead) bd->r = read(bd->fd, bd->buffer, BUFF_LEN); if (bd->r < 0) { - //printfd(__FILE__, " error read\n"); - usleep(20000); + struct timespec ts = {0, 20000000}; + nanosleep(&ts, NULL); + return -1; } bd->p = bd->buffer; diff --git a/projects/stargazer/plugins/capture/ether_linux/ether_cap.cpp b/projects/stargazer/plugins/capture/ether_linux/ether_cap.cpp index 97e9b171..276c8df0 100644 --- a/projects/stargazer/plugins/capture/ether_linux/ether_cap.cpp +++ b/projects/stargazer/plugins/capture/ether_linux/ether_cap.cpp @@ -115,7 +115,8 @@ nonstop = false; //5 seconds to thread stops itself for (int i = 0; i < 25 && isRunning; i++) { - usleep(200000); + struct timespec ts = {0, 200000000}; + nanosleep(&ts, NULL); } //after 5 seconds waiting thread still running. now killing it if (isRunning) @@ -126,7 +127,10 @@ if (isRunning) return -1; } for (int i = 0; i < 25 && isRunning; ++i) - usleep(200000); + { + struct timespec ts = {0, 200000000}; + nanosleep(&ts, NULL); + } if (isRunning) { errorStr = "ETHER_CAP not stopped."; diff --git a/projects/stargazer/plugins/capture/ipq_linux/ipq_cap.cpp b/projects/stargazer/plugins/capture/ipq_linux/ipq_cap.cpp index 7f60c7eb..84939b4e 100644 --- a/projects/stargazer/plugins/capture/ipq_linux/ipq_cap.cpp +++ b/projects/stargazer/plugins/capture/ipq_linux/ipq_cap.cpp @@ -95,7 +95,8 @@ for (int i = 0; i < 25; i++) { if (!isRunning) break; - usleep(200000); + struct timespec ts = {0, 200000000}; + nanosleep(&ts, NULL); } //after 5 seconds waiting thread still running. now killing it if (isRunning) @@ -107,7 +108,8 @@ if (isRunning) } for (int i = 0; i < 25 && isRunning; ++i) { - usleep(200000); + struct timespec ts = {0, 200000000}; + nanosleep(&ts, NULL); } if (isRunning) { diff --git a/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp b/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp index b82293d4..837c8649 100644 --- a/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp +++ b/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp @@ -1,5 +1,3 @@ -#include // TODO: usleep - #include #include @@ -140,7 +138,10 @@ int RPC_CONFIG::Stop() { running = false; for (int i = 0; i < 5 && !stopped; ++i) - usleep(200000); + { + struct timespec ts = {0, 200000000}; + nanosleep(&ts, NULL); + } //rpcServer->terminate(); if (!stopped) { @@ -150,7 +151,10 @@ if (!stopped) printfd(__FILE__, "Failed to kill thread\n"); } for (int i = 0; i < 25 && !stopped; ++i) - usleep(200000); + { + struct timespec ts = {0, 200000000}; + nanosleep(&ts, NULL); + } if (!stopped) { printfd(__FILE__, "Failed to stop RPC thread\n"); diff --git a/projects/stargazer/plugins/configuration/sgconfig/rsconf.cpp b/projects/stargazer/plugins/configuration/sgconfig/rsconf.cpp index b4cbf544..421fc7bb 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/rsconf.cpp +++ b/projects/stargazer/plugins/configuration/sgconfig/rsconf.cpp @@ -26,7 +26,7 @@ * *******************************************************************/ -#include // cloase, usleep +#include // close #include #include diff --git a/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp b/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp index f36792ae..0a65c0d9 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp +++ b/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp @@ -123,7 +123,8 @@ for (i = 0; i < 25; i++) if (!isRunning) break; - usleep(200000); + struct timespec ts = {0, 200000000}; + nanosleep(&ts, NULL); } //after 5 seconds waiting thread still running. now killing it diff --git a/projects/stargazer/plugins/other/radius/radius.cpp b/projects/stargazer/plugins/other/radius/radius.cpp index 62c9305c..6a02ce4f 100644 --- a/projects/stargazer/plugins/other/radius/radius.cpp +++ b/projects/stargazer/plugins/other/radius/radius.cpp @@ -236,7 +236,8 @@ if (isRunning) //5 seconds to thread stops itself for (int i = 0; i < 25 && isRunning; i++) { - usleep(200000); + struct timespec ts = {0, 200000000}; + nanosleep(&ts, NULL); } //after 5 seconds waiting thread still running. now killing it diff --git a/projects/stargazer/plugins/other/rscript/rscript.cpp b/projects/stargazer/plugins/other/rscript/rscript.cpp index 6fde814a..c1d66051 100644 --- a/projects/stargazer/plugins/other/rscript/rscript.cpp +++ b/projects/stargazer/plugins/other/rscript/rscript.cpp @@ -282,7 +282,8 @@ if (isRunning) //5 seconds to thread stops itself for (int i = 0; i < 25 && isRunning; i++) { - usleep(200000); + struct timespec ts = {0, 200000000}; + nanosleep(&ts, NULL); } //after 5 seconds waiting thread still running. now killing it diff --git a/stglibs/ia.lib/ia.cpp b/stglibs/ia.lib/ia.cpp index 9883dbbb..abecef09 100644 --- a/stglibs/ia.lib/ia.cpp +++ b/stglibs/ia.lib/ia.cpp @@ -69,7 +69,7 @@ static int a = 0; if (a == 0) { - usleep(50000); + Sleep(50); a = 1; } @@ -82,7 +82,9 @@ return NULL; //--------------------------------------------------------------------------- void Sleep(int ms) { -usleep(ms * 1000); +long long res = ms * 1000000; +struct timespec ts = {res / 1000000000, res % 1000000000}; +nanosleep(&ts, NULL); } //--------------------------------------------------------------------------- long GetTickCount() diff --git a/stglibs/pinger.lib/pinger.cpp b/stglibs/pinger.lib/pinger.cpp index f3686806..df3793ab 100644 --- a/stglibs/pinger.lib/pinger.cpp +++ b/stglibs/pinger.lib/pinger.cpp @@ -93,7 +93,8 @@ int STG_PINGER::Stop() if (!isRunningRecver) break; - usleep(200000); + struct timespec ts = {0, 200000000}; + nanosleep(&ts, NULL); } //after 5 seconds waiting thread still running. now killing it @@ -115,7 +116,8 @@ int STG_PINGER::Stop() if (!isRunningSender) break; - usleep(200000); + struct timespec ts = {0, 200000000}; + nanosleep(&ts, NULL); } //after 5 seconds waiting thread still running. now killing it @@ -315,7 +317,8 @@ void * STG_PINGER::RunSendPing(void * d) #else currTime = time(NULL); #endif - usleep(20000); + struct timespec ts = {0, 20000000}; + nanosleep(&ts, NULL); } } -- 2.44.2 From 597f6f31e3801612273886481381df509d8bcd12 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Fri, 16 Sep 2011 20:44:06 +0300 Subject: [PATCH 04/16] Block all signals in threads other than main --- projects/rscriptd/listener.cpp | 8 + projects/sgauth/web.cpp | 15 +- projects/sgauthstress/proto.cpp | 5 + projects/stargazer/eventloop.cpp | 5 + .../authorization/inetaccess/inetaccess.cpp | 8 + .../plugins/capture/cap_nf/cap_nf.cpp | 8 + .../capture/divert_freebsd/divert_cap.cpp | 6 +- .../capture/ether_freebsd/ether_cap.cpp | 19 +- .../plugins/capture/ether_linux/ether_cap.cpp | 17 +- .../plugins/capture/ipq_linux/ipq_cap.cpp | 12 +- .../configuration/rpcconfig/rpcconfig.cpp | 5 + .../configuration/sgconfig/stgconfig.cpp | 4 + .../stargazer/plugins/other/ping/ping.cpp | 6 +- .../stargazer/plugins/other/radius/radius.cpp | 4 + .../plugins/other/rscript/rscript.cpp | 4 + .../stargazer/plugins/other/smux/smux.cpp | 4 + projects/stargazer/stg_timer.cpp | 9 +- projects/stargazer/traffcounter_impl.cpp | 4 + projects/stargazer/user_impl.cpp | 2 +- projects/stargazer/users_impl.cpp | 5 + stglibs/ia.lib/ia.cpp | 4 + stglibs/pinger.lib/pinger.cpp | 426 +++++++++--------- 22 files changed, 345 insertions(+), 235 deletions(-) diff --git a/projects/rscriptd/listener.cpp b/projects/rscriptd/listener.cpp index d4f2dc93..73042674 100644 --- a/projects/rscriptd/listener.cpp +++ b/projects/rscriptd/listener.cpp @@ -165,6 +165,10 @@ return false; //----------------------------------------------------------------------------- void * LISTENER::Run(void * d) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + LISTENER * listener = static_cast(d); listener->Runner(); @@ -186,6 +190,10 @@ receiverStopped = true; //----------------------------------------------------------------------------- void * LISTENER::RunProcessor(void * d) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + LISTENER * listener = static_cast(d); listener->ProcessorRunner(); diff --git a/projects/sgauth/web.cpp b/projects/sgauth/web.cpp index ab169cc4..61cd2b58 100644 --- a/projects/sgauth/web.cpp +++ b/projects/sgauth/web.cpp @@ -23,11 +23,13 @@ $Date: 2010/03/15 12:58:17 $ */ -#include -#include -#include #include +#include +#include +#include +#include + #include "stg/common.h" #include "stg/ia.h" #include "web.h" @@ -42,10 +44,15 @@ extern IA_CLIENT_PROT * clnp; //--------------------------------------------------------------------------- #ifndef WIN32 void * RunWeb(void *) +{ +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + #else unsigned long WINAPI RunWeb(void *) -#endif { +#endif while (1) web->Run(); return NULL; diff --git a/projects/sgauthstress/proto.cpp b/projects/sgauthstress/proto.cpp index 2e97fd2a..dc2bf08c 100644 --- a/projects/sgauthstress/proto.cpp +++ b/projects/sgauthstress/proto.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include #include @@ -77,6 +78,10 @@ pthread_mutex_destroy(&mutex); void * PROTO::Runner(void * data) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + PROTO * protoPtr = static_cast(data); protoPtr->Run(); return NULL; diff --git a/projects/stargazer/eventloop.cpp b/projects/stargazer/eventloop.cpp index dd17de9b..6cc59c94 100644 --- a/projects/stargazer/eventloop.cpp +++ b/projects/stargazer/eventloop.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -53,6 +54,10 @@ return NULL; void EVENT_LOOP::Runner() { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + _stopped = false; printfd(__FILE__, "EVENT_LOOP::Runner - Before start\n"); while (_running) diff --git a/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp b/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp index 4986080e..1eac0ab1 100644 --- a/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp +++ b/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp @@ -500,6 +500,10 @@ return 0; //----------------------------------------------------------------------------- void * AUTH_IA::Run(void * d) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + AUTH_IA * ia = static_cast(d); ia->isRunningRun = true; @@ -525,6 +529,10 @@ return NULL; //----------------------------------------------------------------------------- void * AUTH_IA::RunTimeouter(void * d) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + AUTH_IA * ia = static_cast(d); ia->isRunningRunTimeouter = true; diff --git a/projects/stargazer/plugins/capture/cap_nf/cap_nf.cpp b/projects/stargazer/plugins/capture/cap_nf/cap_nf.cpp index fb7d2cc5..51ba4954 100644 --- a/projects/stargazer/plugins/capture/cap_nf/cap_nf.cpp +++ b/projects/stargazer/plugins/capture/cap_nf/cap_nf.cpp @@ -244,6 +244,10 @@ return false; void * NF_CAP::RunUDP(void * c) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + NF_CAP * cap = static_cast(c); uint8_t buf[BUF_SIZE]; int res; @@ -286,6 +290,10 @@ return NULL; void * NF_CAP::RunTCP(void * c) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + NF_CAP * cap = static_cast(c); uint8_t buf[BUF_SIZE]; int res; diff --git a/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.cpp b/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.cpp index 5927727e..9a9d1543 100644 --- a/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.cpp +++ b/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.cpp @@ -150,7 +150,11 @@ return 0; //----------------------------------------------------------------------------- void * DIVERT_CAP::Run(void * d) { -DIVERT_CAP * dc = (DIVERT_CAP *)d; +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + +DIVERT_CAP * dc = static_cast(d); dc->isRunning = true; char buffer[64]; diff --git a/projects/stargazer/plugins/capture/ether_freebsd/ether_cap.cpp b/projects/stargazer/plugins/capture/ether_freebsd/ether_cap.cpp index 108c4e0d..5835d077 100644 --- a/projects/stargazer/plugins/capture/ether_freebsd/ether_cap.cpp +++ b/projects/stargazer/plugins/capture/ether_freebsd/ether_cap.cpp @@ -39,19 +39,20 @@ $Author: faust $ #include #include #include - -#include #include -#include -#include -#include -#include #include +#include +#include +#include +#include +#include + #include "stg/common.h" #include "stg/raw_ip_packet.h" #include "stg/traffcounter.h" #include "stg/plugin_creator.h" + #include "ether_cap.h" //#define CAP_DEBUG 1 @@ -201,7 +202,11 @@ return 0; //----------------------------------------------------------------------------- void * BPF_CAP::Run(void * d) { -BPF_CAP * dc = (BPF_CAP *)d; +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + +BPF_CAP * dc = static_cast(d); dc->isRunning = true; uint8_t hdr[96]; //68 + 14 + 4(size) + 9(SYS_IFACE) + 1(align to 4) = 96 diff --git a/projects/stargazer/plugins/capture/ether_linux/ether_cap.cpp b/projects/stargazer/plugins/capture/ether_linux/ether_cap.cpp index 276c8df0..632c1846 100644 --- a/projects/stargazer/plugins/capture/ether_linux/ether_cap.cpp +++ b/projects/stargazer/plugins/capture/ether_linux/ether_cap.cpp @@ -32,22 +32,23 @@ $Date: 2009/12/13 13:45:13 $ #include #include #include -#include -#include -#include -#include #include #include #include -#include - #include #include +#include +#include +#include +#include +#include + #include "stg/common.h" #include "stg/raw_ip_packet.h" #include "stg/traffcounter.h" #include "stg/plugin_creator.h" + #include "ether_cap.h" //#define CAP_DEBUG 1 @@ -149,6 +150,10 @@ return 0; //----------------------------------------------------------------------------- void * ETHER_CAP::Run(void * d) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + ETHER_CAP * dc = (ETHER_CAP *)d; dc->isRunning = true; diff --git a/projects/stargazer/plugins/capture/ipq_linux/ipq_cap.cpp b/projects/stargazer/plugins/capture/ipq_linux/ipq_cap.cpp index 84939b4e..d0192e0a 100644 --- a/projects/stargazer/plugins/capture/ipq_linux/ipq_cap.cpp +++ b/projects/stargazer/plugins/capture/ipq_linux/ipq_cap.cpp @@ -18,14 +18,16 @@ * Author : Boris Mikhailenko */ -#include -#include #include #include +#include +#include + #include "stg/raw_ip_packet.h" #include "stg/traffcounter.h" #include "stg/plugin_creator.h" + #include "ipq_cap.h" extern "C" @@ -126,9 +128,13 @@ return 0; //----------------------------------------------------------------------------- void * IPQ_CAP::Run(void * d) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + RAW_PACKET raw_packet; -IPQ_CAP * dc = (IPQ_CAP *)d; +IPQ_CAP * dc = static_cast(d); dc->isRunning = true; memset(&raw_packet, 0, sizeof(raw_packet)); raw_packet.dataLen = -1; diff --git a/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp b/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp index 837c8649..d3fdba55 100644 --- a/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp +++ b/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp @@ -157,6 +157,7 @@ if (!stopped) } if (!stopped) { + running = true; printfd(__FILE__, "Failed to stop RPC thread\n"); errorStr = "Failed to stop RPC thread"; return -1; @@ -171,6 +172,10 @@ return 0; void * RPC_CONFIG::Run(void * rc) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + RPC_CONFIG * config = static_cast(rc); config->stopped = false; diff --git a/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp b/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp index 0a65c0d9..3cd6b901 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp +++ b/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp @@ -145,6 +145,10 @@ return 0; //----------------------------------------------------------------------------- void * STG_CONFIG::Run(void * d) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + STG_CONFIG * stgConf = (STG_CONFIG *)d; stgConf->isRunning = true; diff --git a/projects/stargazer/plugins/other/ping/ping.cpp b/projects/stargazer/plugins/other/ping/ping.cpp index c8240679..f4755c23 100644 --- a/projects/stargazer/plugins/other/ping/ping.cpp +++ b/projects/stargazer/plugins/other/ping/ping.cpp @@ -167,7 +167,11 @@ return isRunning; //----------------------------------------------------------------------------- void * PING::Run(void * d) { -PING * ping = (PING *)d; +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + +PING * ping = static_cast(d); ping->isRunning = true; long delay = (10000000 * ping->pingSettings.GetPingDelay()) / 3 + 50000000; diff --git a/projects/stargazer/plugins/other/radius/radius.cpp b/projects/stargazer/plugins/other/radius/radius.cpp index 6a02ce4f..c827330f 100644 --- a/projects/stargazer/plugins/other/radius/radius.cpp +++ b/projects/stargazer/plugins/other/radius/radius.cpp @@ -258,6 +258,10 @@ return 0; //----------------------------------------------------------------------------- void * RADIUS::Run(void * d) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + RADIUS * rad = (RADIUS *)d; RAD_PACKET packet; diff --git a/projects/stargazer/plugins/other/rscript/rscript.cpp b/projects/stargazer/plugins/other/rscript/rscript.cpp index c1d66051..17598006 100644 --- a/projects/stargazer/plugins/other/rscript/rscript.cpp +++ b/projects/stargazer/plugins/other/rscript/rscript.cpp @@ -198,6 +198,10 @@ pthread_mutex_destroy(&mutex); //----------------------------------------------------------------------------- void * REMOTE_SCRIPT::Run(void * d) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + REMOTE_SCRIPT * rs = static_cast(d); rs->isRunning = true; diff --git a/projects/stargazer/plugins/other/smux/smux.cpp b/projects/stargazer/plugins/other/smux/smux.cpp index 61ed8026..5367e249 100644 --- a/projects/stargazer/plugins/other/smux/smux.cpp +++ b/projects/stargazer/plugins/other/smux/smux.cpp @@ -274,6 +274,10 @@ return 0; void * SMUX::Runner(void * d) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + SMUX * smux = static_cast(d); smux->Run(); diff --git a/projects/stargazer/stg_timer.cpp b/projects/stargazer/stg_timer.cpp index fff79a39..235c669e 100644 --- a/projects/stargazer/stg_timer.cpp +++ b/projects/stargazer/stg_timer.cpp @@ -2,6 +2,7 @@ #include #include +#include #include "stg/common.h" @@ -58,6 +59,10 @@ switch (START_TIME) stgTime = time(NULL); #endif +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + nonstop = 1; isTimerRunning = true; while (nonstop) @@ -65,12 +70,10 @@ while (nonstop) #ifdef STG_TIMER_DEBUG struct timespec ts = {0, 1000000000 / TIME_SPEED}; nanosleep(&ts, NULL); - //usleep(1000000 / TIME_SPEED); stgTime++; #else struct timespec ts = {0, 500000000}; nanosleep(&ts, NULL); - //usleep(500000); stgTime = time(NULL); #endif } @@ -112,11 +115,9 @@ int stgUsleep(unsigned long t) #ifdef STG_TIMER_DEBUG struct timespec ts = {(t / TIME_SPEED) / 1000000, ((t / TIME_SPEED) % 1000000) * 1000}; return nanosleep(&ts, NULL); -//return usleep(t / TIME_SPEED); #else struct timespec ts = {t / 1000000, (t % 1000000) * 1000}; return nanosleep(&ts, NULL); -//return usleep(t); #endif } //----------------------------------------------------------------------------- diff --git a/projects/stargazer/traffcounter_impl.cpp b/projects/stargazer/traffcounter_impl.cpp index ae7f1dfb..a02e3b6d 100644 --- a/projects/stargazer/traffcounter_impl.cpp +++ b/projects/stargazer/traffcounter_impl.cpp @@ -169,6 +169,10 @@ return 0; //----------------------------------------------------------------------------- void * TRAFFCOUNTER_IMPL::Run(void * data) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + TRAFFCOUNTER_IMPL * tc = static_cast(data); tc->stopped = false; int c = 0; diff --git a/projects/stargazer/user_impl.cpp b/projects/stargazer/user_impl.cpp index 6aca9cea..1278afa2 100644 --- a/projects/stargazer/user_impl.cpp +++ b/projects/stargazer/user_impl.cpp @@ -484,7 +484,7 @@ for (int i = 0; i < DIR_NUM; i++) enabledDirs[i] = dirs & (1 << i); } -if (authorizedBy.size()) +if (!authorizedBy.empty()) { if (currIP != ip) { diff --git a/projects/stargazer/users_impl.cpp b/projects/stargazer/users_impl.cpp index 19be5a41..61ee90ff 100644 --- a/projects/stargazer/users_impl.cpp +++ b/projects/stargazer/users_impl.cpp @@ -33,6 +33,7 @@ #endif #include + #include #include #include @@ -353,6 +354,10 @@ return 0; //----------------------------------------------------------------------------- void * USERS_IMPL::Run(void * d) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + printfd(__FILE__, "=====================| pid: %d |===================== \n", getpid()); USERS_IMPL * us = (USERS_IMPL*) d; diff --git a/stglibs/ia.lib/ia.cpp b/stglibs/ia.lib/ia.cpp index abecef09..81bd75fb 100644 --- a/stglibs/ia.lib/ia.cpp +++ b/stglibs/ia.lib/ia.cpp @@ -63,6 +63,10 @@ #include void * RunL(void * data) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + IA_CLIENT_PROT * c = (IA_CLIENT_PROT *)data; static int a = 0; diff --git a/stglibs/pinger.lib/pinger.cpp b/stglibs/pinger.lib/pinger.cpp index df3793ab..8e6370ed 100644 --- a/stglibs/pinger.lib/pinger.cpp +++ b/stglibs/pinger.lib/pinger.cpp @@ -1,23 +1,25 @@ -#include #include -#include #include #include #include #include #include #include -#include #include #include -#include -#include -#include -#include "pinger.h" +#include +#include +#include +#include +#include +#include + #include "stg/common.h" #include "stg/locker.h" +#include "pinger.h" + #ifdef STG_TIME extern volatile time_t stgTime; #endif @@ -40,320 +42,328 @@ STG_PINGER::STG_PINGER(time_t d) ipToDel(), mutex() { - pthread_mutex_init(&mutex, NULL); - memset(&pmSend, 0, sizeof(pmSend)); +pthread_mutex_init(&mutex, NULL); +memset(&pmSend, 0, sizeof(pmSend)); } //----------------------------------------------------------------------------- STG_PINGER::~STG_PINGER() { - pthread_mutex_destroy(&mutex); +pthread_mutex_destroy(&mutex); } //----------------------------------------------------------------------------- int STG_PINGER::Start() { - struct protoent *proto = NULL; - proto = getprotobyname("ICMP"); - sendSocket = socket(PF_INET, SOCK_RAW, proto->p_proto); - recvSocket = socket(PF_INET, SOCK_RAW, proto->p_proto); - nonstop = true; - pid = (int) getpid() % 65535; - if (sendSocket < 0 || recvSocket < 0) - { - errorStr = "Cannot create socket."; - return -1; - } +struct protoent *proto = NULL; +proto = getprotobyname("ICMP"); +sendSocket = socket(PF_INET, SOCK_RAW, proto->p_proto); +recvSocket = socket(PF_INET, SOCK_RAW, proto->p_proto); +nonstop = true; +pid = (int) getpid() % 65535; +if (sendSocket < 0 || recvSocket < 0) + { + errorStr = "Cannot create socket."; + return -1; + } - if (pthread_create(&sendThread, NULL, RunSendPing, this)) - { - errorStr = "Cannot create send thread."; - return -1; - } +if (pthread_create(&sendThread, NULL, RunSendPing, this)) + { + errorStr = "Cannot create send thread."; + return -1; + } - if (pthread_create(&recvThread, NULL, RunRecvPing, this)) - { - errorStr = "Cannot create recv thread."; - return -1; - } +if (pthread_create(&recvThread, NULL, RunRecvPing, this)) + { + errorStr = "Cannot create recv thread."; + return -1; + } - return 0; +return 0; } //----------------------------------------------------------------------------- int STG_PINGER::Stop() { - close(recvSocket); - nonstop = false; - if (isRunningRecver) +close(recvSocket); +nonstop = false; +if (isRunningRecver) + { + //5 seconds to thread stops itself + for (size_t i = 0; i < 25; i++) { - //5 seconds to thread stops itself - for (size_t i = 0; i < 25; i++) - { - if (i % 5 == 0) - SendPing(0x0100007f);//127.0.0.1 + if (i % 5 == 0) + SendPing(0x0100007f);//127.0.0.1 - if (!isRunningRecver) - break; + if (!isRunningRecver) + break; - struct timespec ts = {0, 200000000}; - nanosleep(&ts, NULL); - } + struct timespec ts = {0, 200000000}; + nanosleep(&ts, NULL); + } - //after 5 seconds waiting thread still running. now killing it - if (isRunningRecver) + //after 5 seconds waiting thread still running. now killing it + if (isRunningRecver) + { + if (pthread_kill(recvThread, SIGINT)) { - if (pthread_kill(recvThread, SIGINT)) - { - errorStr = "Cannot kill thread."; - return -1; - } + errorStr = "Cannot kill thread."; + return -1; } } + } - if (isRunningSender) +if (isRunningSender) + { + //5 seconds to thread stops itself + for (size_t i = 0; i < 25; i++) { - //5 seconds to thread stops itself - for (size_t i = 0; i < 25; i++) - { - if (!isRunningSender) - break; + if (!isRunningSender) + break; - struct timespec ts = {0, 200000000}; - nanosleep(&ts, NULL); - } + struct timespec ts = {0, 200000000}; + nanosleep(&ts, NULL); + } - //after 5 seconds waiting thread still running. now killing it - if (isRunningSender) + //after 5 seconds waiting thread still running. now killing it + if (isRunningSender) + { + if (pthread_kill(sendThread, SIGINT)) { - if (pthread_kill(sendThread, SIGINT)) - { - errorStr = "Cannot kill thread."; - return -1; - } + errorStr = "Cannot kill thread."; + return -1; } } + } - close(sendSocket); - return 0; +close(sendSocket); +return 0; } //----------------------------------------------------------------------------- void STG_PINGER::AddIP(uint32_t ip) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - ipToAdd.push_back(ip); +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +ipToAdd.push_back(ip); } //----------------------------------------------------------------------------- void STG_PINGER::DelIP(uint32_t ip) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - ipToDel.push_back(ip); +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +ipToDel.push_back(ip); } //----------------------------------------------------------------------------- void STG_PINGER::RealAddIP() - { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); - std::list::iterator iter; - iter = ipToAdd.begin(); - while (iter != ipToAdd.end()) - { - pingIP.insert(std::make_pair(*iter, 0)); - ++iter; - } - ipToAdd.erase(ipToAdd.begin(), ipToAdd.end()); +std::list::iterator iter; +iter = ipToAdd.begin(); +while (iter != ipToAdd.end()) + { + pingIP.insert(std::make_pair(*iter, 0)); + ++iter; } +ipToAdd.erase(ipToAdd.begin(), ipToAdd.end()); +} //----------------------------------------------------------------------------- void STG_PINGER::RealDelIP() { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex, __FILE__, __LINE__); - std::list::iterator iter; - std::multimap::iterator treeIter; - iter = ipToDel.begin(); - while (iter != ipToDel.end()) - { - treeIter = pingIP.find(*iter); - if (treeIter != pingIP.end()) - pingIP.erase(treeIter); +std::list::iterator iter; +std::multimap::iterator treeIter; +iter = ipToDel.begin(); +while (iter != ipToDel.end()) + { + treeIter = pingIP.find(*iter); + if (treeIter != pingIP.end()) + pingIP.erase(treeIter); - ++iter; - } - ipToDel.erase(ipToDel.begin(), ipToDel.end()); + ++iter; + } +ipToDel.erase(ipToDel.begin(), ipToDel.end()); } //----------------------------------------------------------------------------- int STG_PINGER::GetPingIPNum() const { - return pingIP.size(); +return pingIP.size(); } //----------------------------------------------------------------------------- void STG_PINGER::PrintAllIP() { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - std::multimap::iterator iter; - iter = pingIP.begin(); - while (iter != pingIP.end()) - { - uint32_t ip = iter->first; - time_t t = iter->second; - std::string s; - x2str(t, s); - printf("ip = %s, time = %9s\n", inet_ntostring(ip).c_str(), s.c_str()); - ++iter; - } +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +std::multimap::iterator iter; +iter = pingIP.begin(); +while (iter != pingIP.end()) + { + uint32_t ip = iter->first; + time_t t = iter->second; + std::string s; + x2str(t, s); + printf("ip = %s, time = %9s\n", inet_ntostring(ip).c_str(), s.c_str()); + ++iter; + } } //----------------------------------------------------------------------------- int STG_PINGER::GetIPTime(uint32_t ip, time_t * t) const { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - std::multimap::const_iterator treeIter; +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +std::multimap::const_iterator treeIter; - treeIter = pingIP.find(ip); - if (treeIter == pingIP.end()) - return -1; +treeIter = pingIP.find(ip); +if (treeIter == pingIP.end()) + return -1; - *t = treeIter->second; - return 0; +*t = treeIter->second; +return 0; } //----------------------------------------------------------------------------- uint16_t STG_PINGER::PingCheckSum(void * data, int len) { - unsigned short * buf = (unsigned short *)data; - unsigned int sum = 0; - unsigned short result; +unsigned short * buf = (unsigned short *)data; +unsigned int sum = 0; +unsigned short result; - for ( sum = 0; len > 1; len -= 2 ) - sum += *buf++; +for ( sum = 0; len > 1; len -= 2 ) + sum += *buf++; - if ( len == 1 ) - sum += *(unsigned char*)buf; +if ( len == 1 ) + sum += *(unsigned char*)buf; - sum = (sum >> 16) + (sum & 0xFFFF); - sum += (sum >> 16); - result = ~sum; - return result; +sum = (sum >> 16) + (sum & 0xFFFF); +sum += (sum >> 16); +result = ~sum; +return result; } //----------------------------------------------------------------------------- int STG_PINGER::SendPing(uint32_t ip) { - struct sockaddr_in addr; - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_port = 0; - addr.sin_addr.s_addr = ip; +struct sockaddr_in addr; +memset(&addr, 0, sizeof(addr)); +addr.sin_family = AF_INET; +addr.sin_port = 0; +addr.sin_addr.s_addr = ip; - memset(&pmSend, 0, sizeof(pmSend)); - pmSend.hdr.type = ICMP_ECHO; - pmSend.hdr.un.echo.id = pid; - memcpy(pmSend.msg, &ip, sizeof(ip)); +memset(&pmSend, 0, sizeof(pmSend)); +pmSend.hdr.type = ICMP_ECHO; +pmSend.hdr.un.echo.id = pid; +memcpy(pmSend.msg, &ip, sizeof(ip)); - pmSend.hdr.checksum = PingCheckSum(&pmSend, sizeof(pmSend)); +pmSend.hdr.checksum = PingCheckSum(&pmSend, sizeof(pmSend)); - if (sendto(sendSocket, &pmSend, sizeof(pmSend), 0, (sockaddr *)&addr, sizeof(addr)) <= 0 ) - { - errorStr = "Send ping error: " + std::string(strerror(errno)); - return -1; - } +if (sendto(sendSocket, &pmSend, sizeof(pmSend), 0, (sockaddr *)&addr, sizeof(addr)) <= 0 ) + { + errorStr = "Send ping error: " + std::string(strerror(errno)); + return -1; + } - return 0; +return 0; } //----------------------------------------------------------------------------- uint32_t STG_PINGER::RecvPing() { - struct sockaddr_in addr; - uint32_t ipAddr = 0; +struct sockaddr_in addr; +uint32_t ipAddr = 0; - char buf[128]; - memset(buf, 0, sizeof(buf)); - int bytes; - socklen_t len = sizeof(addr); +char buf[128]; +memset(buf, 0, sizeof(buf)); +int bytes; +socklen_t len = sizeof(addr); - bytes = recvfrom(recvSocket, &buf, sizeof(buf), 0, (struct sockaddr*)&addr, &len); - if (bytes > 0) - { - struct IP_HDR * ip = (struct IP_HDR *)buf; - struct ICMP_HDR *icmp = (struct ICMP_HDR *)(buf + ip->ihl * 4); +bytes = recvfrom(recvSocket, &buf, sizeof(buf), 0, (struct sockaddr*)&addr, &len); +if (bytes > 0) + { + struct IP_HDR * ip = (struct IP_HDR *)buf; + struct ICMP_HDR *icmp = (struct ICMP_HDR *)(buf + ip->ihl * 4); - if (icmp->un.echo.id != pid) - return 0; + if (icmp->un.echo.id != pid) + return 0; - ipAddr = *(uint32_t*)(buf + sizeof(ICMP_HDR) + ip->ihl * 4); - } + ipAddr = *(uint32_t*)(buf + sizeof(ICMP_HDR) + ip->ihl * 4); + } - return ipAddr; +return ipAddr; } //----------------------------------------------------------------------------- void * STG_PINGER::RunSendPing(void * d) { - STG_PINGER * pinger = static_cast(d); +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); - pinger->isRunningSender = true; - time_t lastPing = 0; - while (pinger->nonstop) +STG_PINGER * pinger = static_cast(d); + +pinger->isRunningSender = true; +time_t lastPing = 0; +while (pinger->nonstop) + { + pinger->RealAddIP(); + pinger->RealDelIP(); + + std::multimap::iterator iter; + iter = pinger->pingIP.begin(); + while (iter != pinger->pingIP.end()) { - pinger->RealAddIP(); - pinger->RealDelIP(); + pinger->SendPing(iter->first); + ++iter; + } - std::multimap::iterator iter; - iter = pinger->pingIP.begin(); - while (iter != pinger->pingIP.end()) - { - pinger->SendPing(iter->first); - ++iter; - } + time_t currTime; - time_t currTime; + #ifdef STG_TIME + lastPing = stgTime; + currTime = stgTime; + #else + currTime = lastPing = time(NULL); + #endif + while (currTime - lastPing < pinger->delay && pinger->nonstop) + { #ifdef STG_TIME - lastPing = stgTime; currTime = stgTime; #else - currTime = lastPing = time(NULL); + currTime = time(NULL); #endif - - while (currTime - lastPing < pinger->delay && pinger->nonstop) - { - #ifdef STG_TIME - currTime = stgTime; - #else - currTime = time(NULL); - #endif - struct timespec ts = {0, 20000000}; - nanosleep(&ts, NULL); - } + struct timespec ts = {0, 20000000}; + nanosleep(&ts, NULL); } + } - pinger->isRunningSender = false; +pinger->isRunningSender = false; - return NULL; +return NULL; } //----------------------------------------------------------------------------- void * STG_PINGER::RunRecvPing(void * d) { - STG_PINGER * pinger = static_cast(d); +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); - pinger->isRunningRecver = true; +STG_PINGER * pinger = static_cast(d); - while (pinger->nonstop) - { - uint32_t ip = pinger->RecvPing(); +pinger->isRunningRecver = true; + +while (pinger->nonstop) + { + uint32_t ip = pinger->RecvPing(); - if (ip) + if (ip) + { + std::multimap::iterator treeIterUpper = pinger->pingIP.upper_bound(ip); + std::multimap::iterator treeIterLower = pinger->pingIP.lower_bound(ip); + while (treeIterUpper != treeIterLower) { - std::multimap::iterator treeIterUpper = pinger->pingIP.upper_bound(ip); - std::multimap::iterator treeIterLower = pinger->pingIP.lower_bound(ip); - while (treeIterUpper != treeIterLower) - { - #ifdef STG_TIME - treeIterLower->second = stgTime; - #else - treeIterLower->second = time(NULL); - #endif - ++treeIterLower; - } + #ifdef STG_TIME + treeIterLower->second = stgTime; + #else + treeIterLower->second = time(NULL); + #endif + ++treeIterLower; } - } - pinger->isRunningRecver = false; - return NULL; + + } +pinger->isRunningRecver = false; +return NULL; } //----------------------------------------------------------------------------- -- 2.44.2 From 0fd1cedaf374bd51b529f30a333a47547326d8b9 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Fri, 16 Sep 2011 20:44:39 +0300 Subject: [PATCH 05/16] New signal handling infrastructure. Prevent unloading running module. --- projects/stargazer/main.cpp | 329 +++++++----------------------------- 1 file changed, 59 insertions(+), 270 deletions(-) diff --git a/projects/stargazer/main.cpp b/projects/stargazer/main.cpp index 5b4a06fc..7e83b72d 100644 --- a/projects/stargazer/main.cpp +++ b/projects/stargazer/main.cpp @@ -63,7 +63,6 @@ #include "eventloop.h" using namespace std; -uint32_t eip; #ifdef DEBUG #define MAIN_DEBUG (1) @@ -72,13 +71,9 @@ uint32_t eip; #define START_FILE "/._ST_ART_ED_" -static bool needRulesReloading = false; -static bool childExited = false; -//static pid_t executerPid; set executersPid; static pid_t stgChildPid; - //----------------------------------------------------------------------------- bool StartModCmp(const PLUGIN_RUNNER & lhs, const PLUGIN_RUNNER & rhs) { @@ -90,28 +85,6 @@ bool StopModCmp(const PLUGIN_RUNNER & lhs, const PLUGIN_RUNNER & rhs) return lhs.GetStopPosition() > rhs.GetStopPosition(); } //----------------------------------------------------------------------------- -class STG_STOPPER -{ -public: - STG_STOPPER() : nonstop(true) {} - bool GetStatus() const { return nonstop; } - #ifdef NO_DAEMON - void Stop(const char * __file__, int __line__) - #else - void Stop(const char *, int) - #endif - { - #ifdef NO_DAEMON - printfd(__FILE__, "Stg stopped at %s:%d\n", __file__, __line__); - #endif - nonstop = false; - } -private: - bool nonstop; -}; -//----------------------------------------------------------------------------- -STG_STOPPER nonstop; -//----------------------------------------------------------------------------- static void StartTimer() { STG_LOGGER & WriteServLog = GetStgLogger(); @@ -129,185 +102,6 @@ else } } //----------------------------------------------------------------------------- -void CatchUSR1(int) -{ - -} -//----------------------------------------------------------------------------- -void CatchTERM(int sig) -{ -/* - *Function Name:CatchINT - *Parameters: sig_num - ÎÏÍÅÒ ÓÉÇÎÁÌÁ - *Description: ïÂÒÁÂÏÔÞÉË ÓÉÇÎÁÌÁ INT - *Returns: îÉÞÅÇÏ - */ -STG_LOGGER & WriteServLog = GetStgLogger(); -WriteServLog("Shutting down... %d", sig); - -//nonstop = false; -nonstop.Stop(__FILE__, __LINE__); - -struct sigaction newsa, oldsa; -sigset_t sigmask; - -sigemptyset(&sigmask); -sigaddset(&sigmask, SIGTERM); -newsa.sa_handler = SIG_IGN; -newsa.sa_mask = sigmask; -newsa.sa_flags = 0; -sigaction(SIGTERM, &newsa, &oldsa); - -sigemptyset(&sigmask); -sigaddset(&sigmask, SIGINT); -newsa.sa_handler = SIG_IGN; -newsa.sa_mask = sigmask; -newsa.sa_flags = 0; -sigaction(SIGINT, &newsa, &oldsa); -} -//----------------------------------------------------------------------------- -void CatchPIPE(int) -{ -STG_LOGGER & WriteServLog = GetStgLogger(); -WriteServLog("Broken pipe!"); -} -//----------------------------------------------------------------------------- -void CatchHUP(int) -{ -needRulesReloading = true; -} -//----------------------------------------------------------------------------- -void CatchCHLD(int) -{ -int status; -pid_t childPid; -childPid = waitpid(-1, &status, WNOHANG); - -set::iterator pid; -pid = executersPid.find(childPid); -if (pid != executersPid.end()) - { - executersPid.erase(pid); - if (executersPid.empty() && nonstop.GetStatus()) - { - nonstop.Stop(__FILE__, __LINE__); - } - } -if (childPid == stgChildPid) - { - childExited = true; - } -} -/*//----------------------------------------------------------------------------- -void CatchSEGV(int, siginfo_t *, void *) -{ -char fileName[50]; -sprintf(fileName, "/tmp/stg_segv.%d", getpid()); -FILE * f = fopen(fileName, "wt"); -if (f) - { - fprintf(f, "\nSignal info:\n~~~~~~~~~~~~\n"); - fprintf(f, "numb:\t %d (%d)\n", sinfo->si_signo, sig); - fprintf(f, "errn:\t %d\n", sinfo->si_errno); - fprintf(f, "code:\t %d ", sinfo->si_code); - - switch (sinfo->si_code) - { - case SEGV_MAPERR: - fprintf(f, "(SEGV_MAPERR - address not mapped to object)\n"); - break; - - case SEGV_ACCERR: - fprintf(f, "(SEGV_ACCERR - invalid permissions for mapped object)\n"); - break; - - default: - fprintf(f, "???\n"); - } - - fprintf(f, "addr:\t 0x%.8X\n", - (unsigned int)sinfo->si_addr); - - Dl_info dlinfo; - //asm("movl %eip, eip"); - if (dladdr((void*)CatchCHLD, &dlinfo)) - { - fprintf(f, "SEGV point: %s %s\n", dlinfo.dli_fname, dlinfo.dli_sname); - } - else - { - fprintf(f, "Cannot find SEGV point\n"); - } - - fclose(f); - } - -struct sigaction segv_action, segv_action_old; - -segv_action.sa_handler = SIG_DFL; -segv_action.sa_sigaction = NULL; -segv_action.sa_flags = SA_SIGINFO; -segv_action.sa_restorer = NULL; - -sigaction(SIGSEGV, &segv_action, &segv_action_old); -}*/ -//----------------------------------------------------------------------------- -static void SetSignalHandlers() -{ -struct sigaction newsa, oldsa; -sigset_t sigmask; -/////// -sigemptyset(&sigmask); -sigaddset(&sigmask, SIGTERM); -newsa.sa_handler = CatchTERM; -newsa.sa_mask = sigmask; -newsa.sa_flags = 0; -sigaction(SIGTERM, &newsa, &oldsa); -/////// -sigemptyset(&sigmask); -sigaddset(&sigmask, SIGUSR1); -newsa.sa_handler = CatchUSR1; -newsa.sa_mask = sigmask; -newsa.sa_flags = 0; -sigaction(SIGUSR1, &newsa, &oldsa); -/////// -sigemptyset(&sigmask); -sigaddset(&sigmask, SIGINT); -newsa.sa_handler = CatchTERM; -newsa.sa_mask = sigmask; -newsa.sa_flags = 0; -sigaction(SIGINT, &newsa, &oldsa); -////// -sigemptyset(&sigmask); -sigaddset(&sigmask, SIGPIPE); -newsa.sa_handler = CatchPIPE; -newsa.sa_mask = sigmask; -newsa.sa_flags = 0; -sigaction(SIGPIPE, &newsa, &oldsa); -////// -sigemptyset(&sigmask); -sigaddset(&sigmask, SIGHUP); -newsa.sa_handler = CatchHUP; -newsa.sa_mask = sigmask; -newsa.sa_flags = 0; -sigaction(SIGHUP, &newsa, &oldsa); -////// -sigemptyset(&sigmask); -sigaddset(&sigmask, SIGCHLD); -newsa.sa_handler = CatchCHLD; -newsa.sa_mask = sigmask; -newsa.sa_flags = 0; -sigaction(SIGCHLD, &newsa, &oldsa); - -/*newsa.sa_handler = NULL; -newsa.sa_sigaction = CatchSEGV; -newsa.sa_flags = SA_SIGINFO; -newsa.sa_restorer = NULL; -sigaction(SIGSEGV, &newsa, &oldsa);*/ - -return; -} -//----------------------------------------------------------------------------- int StartScriptExecuter(char * procName, int msgKey, int * msgID, SETTINGS_IMPL * settings) { STG_LOGGER & WriteServLog = GetStgLogger(); @@ -394,11 +188,6 @@ switch (stgChildPid) exit(0); } - if (childExited) - { - unlink(startFile.c_str()); - exit(1); - } nanosleep(&ts, NULL); } unlink(startFile.c_str()); @@ -423,25 +212,6 @@ while (pid != executersPid.end()) //----------------------------------------------------------------------------- int main(int argc, char * argv[]) { - -/* - Initialization order: - - Logger - - Stg timer - - Settings - - Plugins - - Plugins settings - - Read Admins - - Read Tariffs - - Read Users - - Start Users - - Start Traffcounter - - Start Plugins - - Start pinger - - Set signal nandlers - - Fork and exit - * */ - SETTINGS_IMPL * settings = NULL; STORE * dataStore = NULL; TARIFFS_IMPL * tariffs = NULL; @@ -578,7 +348,6 @@ modIter = modules.begin(); while (modIter != modules.end()) { - //Loading modules if (modIter->Load()) { WriteServLog("Error: %s", @@ -588,7 +357,6 @@ while (modIter != modules.end()) ++modIter; } -//Start section if (users->Start()) { goto exitLblNotStarted; @@ -611,30 +379,14 @@ while (modIter != modules.end()) { WriteServLog("Error: %s", modIter->GetStrError().c_str()); - //printfd(__FILE__, "Error: %s\n", capRunner.GetStrError().c_str()); goto exitLbl; } WriteServLog("Module: '%s'. Start successfull.", modIter->GetPlugin()->GetVersion().c_str()); ++modIter; } -SetSignalHandlers(); srandom(stgTime); -/* - * Note that an implementation in which nice returns the new nice value - * can legitimately return -1. To reliably detect an error, set - * errno to 0 before the call, and check its value when nice returns -1. - * - * - * (c) man 2 nice - */ -/*errno = 0; -if (nice(-19) && errno) { - printfd(__FILE__, "nice failed: '%s'\n", strerror(errno)); - WriteServLog("nice failed: '%s'", strerror(errno)); -}*/ - WriteServLog("Stg started successfully."); WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++"); @@ -642,26 +394,58 @@ WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++"); creat(startFile.c_str(), S_IRUSR); #endif -while (nonstop.GetStatus()) +while (true) { - if (needRulesReloading) + sigset_t signalSet; + sigfillset(&signalSet); + int sig = 0; + sigwait(&signalSet, &sig); + bool stop = false; + int status; + pid_t childPid; + set::iterator it; + switch (sig) { - needRulesReloading = false; - traffCnt->Reload(); + case SIGHUP: + traffCnt->Reload(); + modIter = modules.begin(); + for (; modIter != modules.end(); ++modIter) + { + if (modIter->Reload()) + { + WriteServLog("Error reloading %s ('%s')", modIter->GetPlugin()->GetVersion().c_str(), + modIter->GetStrError().c_str()); + printfd(__FILE__, "Error reloading %s ('%s')\n", modIter->GetPlugin()->GetVersion().c_str(), + modIter->GetStrError().c_str()); + } + } + break; + case SIGTERM: + stop = true; + break; + case SIGINT: + stop = true; + break; + case SIGPIPE: + WriteServLog("Broken pipe!"); + break; + case SIGCHLD: + childPid = waitpid(-1, &status, WNOHANG); - modIter = modules.begin(); - for (; modIter != modules.end(); ++modIter) - { - if (modIter->Reload()) + it = executersPid.find(childPid); + if (it != executersPid.end()) { - WriteServLog("Error reloading %s ('%s')", modIter->GetPlugin()->GetVersion().c_str(), - modIter->GetStrError().c_str()); - printfd(__FILE__, "Error reloading %s ('%s')\n", modIter->GetPlugin()->GetVersion().c_str(), - modIter->GetStrError().c_str()); + executersPid.erase(it); + if (executersPid.empty()) + stop = true; } - } + break; + default: + WriteServLog("Ignore signel %d", sig); + break; } - stgUsleep(100000); + if (stop) + break; } exitLbl: @@ -681,8 +465,6 @@ while (modIter != modules.end()) modIter->GetPlugin()->GetVersion().c_str(), modIter->GetStrError().c_str()); printfd(__FILE__, "Failed to stop module '%s'\n", name.c_str()); - //printfd(__FILE__, "Error: %s\n", capRunner.GetStrError().c_str()); - //goto exitLbl; } WriteServLog("Module: \'%s\'. Stop successfull.", modIter->GetPlugin()->GetVersion().c_str()); ++modIter; @@ -699,13 +481,20 @@ modIter = modules.begin(); while (modIter != modules.end()) { std::string name = modIter->GetFileName(); - printfd(__FILE__, "Unloading module '%s'\n", name.c_str()); - if (modIter->Unload()) + if (modIter->IsRunning()) { - WriteServLog("Module \'%s\': Error: %s", - name.c_str(), - modIter->GetStrError().c_str()); - printfd(__FILE__, "Failed to unload module '%s'\n", name.c_str()); + printfd(__FILE__, "Passing module '%s' `cause it's still running\n", name.c_str()); + } + else + { + printfd(__FILE__, "Unloading module '%s'\n", name.c_str()); + if (modIter->Unload()) + { + WriteServLog("Module \'%s\': Error: %s", + name.c_str(), + modIter->GetStrError().c_str()); + printfd(__FILE__, "Failed to unload module '%s'\n", name.c_str()); + } } ++modIter; } -- 2.44.2 From bee1318e72d01e37085e44d9c100926419b3c7ca Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Fri, 16 Sep 2011 20:45:17 +0300 Subject: [PATCH 06/16] Return correct core if stop fails --- projects/stargazer/plugin_runner.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/projects/stargazer/plugin_runner.cpp b/projects/stargazer/plugin_runner.cpp index 0bf159b8..3083352c 100644 --- a/projects/stargazer/plugin_runner.cpp +++ b/projects/stargazer/plugin_runner.cpp @@ -178,9 +178,7 @@ if (!plugin) return -1; } -plugin->Stop(); - -return 0; +return plugin->Stop(); } //----------------------------------------------------------------------------- int PLUGIN_RUNNER::Reload() -- 2.44.2 From 3b856e83ad55125111fe5961d07d7524bd6ee283 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Fri, 16 Sep 2011 20:45:38 +0300 Subject: [PATCH 07/16] Fix raw ip tests --- tests/test_raw_ip.cpp | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/tests/test_raw_ip.cpp b/tests/test_raw_ip.cpp index 3b3a97fa..be20ed01 100644 --- a/tests/test_raw_ip.cpp +++ b/tests/test_raw_ip.cpp @@ -36,17 +36,17 @@ namespace tut set_test_name("Check structure consistency"); RAW_PACKET rp; - rp.header.ipHeader.ip_v = 4; - rp.header.ipHeader.ip_hl = 5; - rp.header.ipHeader.ip_tos = 0; - rp.header.ipHeader.ip_len = htons(40); // 20 of header + 20 of data - rp.header.ipHeader.ip_p = 6; - rp.header.ipHeader.ip_src.s_addr = inet_addr("192.168.0.1"); - rp.header.ipHeader.ip_dst.s_addr = inet_addr("192.168.0.101"); - rp.header.sPort = htons(80); - rp.header.dPort = htons(38546); - - ensure_equals("IP header size (explicitly)", sizeof(rp.header.ipHeader), 20); + rp.rawPacket.header.ipHeader.ip_v = 4; + rp.rawPacket.header.ipHeader.ip_hl = 5; + rp.rawPacket.header.ipHeader.ip_tos = 0; + rp.rawPacket.header.ipHeader.ip_len = htons(40); // 20 of header + 20 of data + rp.rawPacket.header.ipHeader.ip_p = 6; + rp.rawPacket.header.ipHeader.ip_src.s_addr = inet_addr("192.168.0.1"); + rp.rawPacket.header.ipHeader.ip_dst.s_addr = inet_addr("192.168.0.101"); + rp.rawPacket.header.sPort = htons(80); + rp.rawPacket.header.dPort = htons(38546); + + ensure_equals("IP header size (explicitly)", sizeof(rp.rawPacket.header.ipHeader), 20); ensure_equals("IP version", rp.GetIPVersion(), 4); ensure_equals("IP header size (with options)", rp.GetHeaderLen(), 20); ensure_equals("Underlying protocol version", rp.GetProto(), 6); @@ -71,8 +71,8 @@ namespace tut genVector(buf); memcpy(p1.pckt, buf, 68); - memcpy(p2.pckt, buf, 68); - memcpy(p3.pckt, buf, 68); + memcpy(p2.rawPacket.pckt, buf, 68); + memcpy(p3.rawPacket.pckt, buf, 68); ensure_equals("IP versions", p1.GetIPVersion(), p2.GetIPVersion()); ensure_equals("IP headers length", p1.GetHeaderLen(), p2.GetHeaderLen()); @@ -101,8 +101,8 @@ std::ostream & operator<<(std::ostream & stream, const RAW_PACKET & p) { stream.unsetf(std::ios::dec); stream.setf(std::ios::hex); - for (size_t i = 0; i < sizeof(p.pckt); ++i) { - stream << static_cast(p.pckt[i]); + for (size_t i = 0; i < sizeof(p.rawPacket.pckt); ++i) { + stream << static_cast(p.rawPacket.pckt[i]); } stream.unsetf(std::ios::hex); stream.setf(std::ios::dec); -- 2.44.2 From 6c6628047db7319e630e6a8e660c5207ea83748e Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Fri, 16 Sep 2011 20:46:00 +0300 Subject: [PATCH 08/16] Unused methods removed from TEST_ADMIN --- tests/testadmin.h | 7 ------- 1 file changed, 7 deletions(-) diff --git a/tests/testadmin.h b/tests/testadmin.h index 59f81d31..bc11daa9 100644 --- a/tests/testadmin.h +++ b/tests/testadmin.h @@ -7,13 +7,6 @@ class TEST_ADMIN : public ADMIN { public: TEST_ADMIN() : priv(0xffFF), ip(0) {} - ADMIN & operator=(const ADMIN &) { return *this; } - ADMIN & operator=(const ADMIN_CONF &) { return *this; } - bool operator==(const ADMIN & /*rhs*/) const { return true; } - bool operator!=(const ADMIN & /*rhs*/) const { return false; } - bool operator<(const ADMIN & /*rhs*/) const { return true; } - bool operator<=(const ADMIN & /*rhs*/) const { return true; } - const std::string & GetPassword() const { return password; } const std::string & GetLogin() const { return login; } PRIV const * GetPriv() const { return &priv; } -- 2.44.2 From a622b081808494796d3679acaebbf5ee364fe9de Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Fri, 16 Sep 2011 22:34:12 +0300 Subject: [PATCH 09/16] Do not use pthread_kill to stop a thread --- projects/rscriptd/main.cpp | 176 +++--------------- projects/stargazer/main.cpp | 6 +- .../authorization/inetaccess/inetaccess.cpp | 56 +----- .../configuration/sgconfig/stgconfig.cpp | 12 +- .../stargazer/plugins/other/ping/ping.cpp | 16 +- .../stargazer/plugins/other/radius/radius.cpp | 15 +- .../plugins/other/rscript/rscript.cpp | 15 +- .../stargazer/plugins/other/smux/smux.cpp | 22 +-- projects/stargazer/traffcounter_impl.cpp | 11 +- stglibs/pinger.lib/pinger.cpp | 24 +-- 10 files changed, 61 insertions(+), 292 deletions(-) diff --git a/projects/rscriptd/main.cpp b/projects/rscriptd/main.cpp index 2739758c..93e1d605 100644 --- a/projects/rscriptd/main.cpp +++ b/projects/rscriptd/main.cpp @@ -57,143 +57,9 @@ using namespace std; #define START_FILE "/._ST_ART_ED_" -static bool childExited = false; set executersPid; -static pid_t stgChildPid; volatile time_t stgTime = time(NULL); -class STG_STOPPER -{ -public: - STG_STOPPER() { nonstop = true; } - bool GetStatus() const { return nonstop; }; - void Stop(const char * __file__, int __line__) - { - #ifdef NO_DAEMON - printfd(__FILE__, "rscriptd stopped at %s:%d\n", __file__, __line__); - #endif - nonstop = false; - } -private: - bool nonstop; -}; -//----------------------------------------------------------------------------- -STG_STOPPER nonstop; -//----------------------------------------------------------------------------- -void CatchPROF(int) -{ -} -//----------------------------------------------------------------------------- -void CatchUSR1(int) -{ -} -//----------------------------------------------------------------------------- -void CatchTERM(int sig) -{ -/* - *Function Name:CatchINT - *Parameters: sig_num - signal number - *Description: INT signal handler - *Returns: Nothing - */ -STG_LOGGER & WriteServLog = GetStgLogger(); -WriteServLog("Shutting down... %d", sig); - -nonstop.Stop(__FILE__, __LINE__); - -struct sigaction newsa, oldsa; -sigset_t sigmask; - -sigemptyset(&sigmask); -sigaddset(&sigmask, SIGTERM); -newsa.sa_handler = SIG_IGN; -newsa.sa_mask = sigmask; -newsa.sa_flags = 0; -sigaction(SIGTERM, &newsa, &oldsa); - -sigemptyset(&sigmask); -sigaddset(&sigmask, SIGINT); -newsa.sa_handler = SIG_IGN; -newsa.sa_mask = sigmask; -newsa.sa_flags = 0; -sigaction(SIGINT, &newsa, &oldsa); -} -//----------------------------------------------------------------------------- -void CatchPIPE(int) -{ -STG_LOGGER & WriteServLog = GetStgLogger(); -WriteServLog("Broken pipe!"); -} -//----------------------------------------------------------------------------- -void CatchHUP(int) -{ -} -//----------------------------------------------------------------------------- -void CatchCHLD(int) -{ -int status; -pid_t childPid; -childPid = waitpid(-1, &status, WNOHANG); - -set::iterator pid; -pid = executersPid.find(childPid); -if (pid != executersPid.end()) - { - executersPid.erase(pid); - } -if (childPid == stgChildPid) - { - childExited = true; - } -} -//----------------------------------------------------------------------------- -void SetSignalHandlers() -{ -struct sigaction newsa, oldsa; -sigset_t sigmask; - -sigemptyset(&sigmask); -sigaddset(&sigmask, SIGTERM); -newsa.sa_handler = CatchTERM; -newsa.sa_mask = sigmask; -newsa.sa_flags = 0; -sigaction(SIGTERM, &newsa, &oldsa); - -sigemptyset(&sigmask); -sigaddset(&sigmask, SIGUSR1); -newsa.sa_handler = CatchUSR1; -newsa.sa_mask = sigmask; -newsa.sa_flags = 0; -sigaction(SIGUSR1, &newsa, &oldsa); - -sigemptyset(&sigmask); -sigaddset(&sigmask, SIGINT); -newsa.sa_handler = CatchTERM; -newsa.sa_mask = sigmask; -newsa.sa_flags = 0; -sigaction(SIGINT, &newsa, &oldsa); - -sigemptyset(&sigmask); -sigaddset(&sigmask, SIGPIPE); -newsa.sa_handler = CatchPIPE; -newsa.sa_mask = sigmask; -newsa.sa_flags = 0; -sigaction(SIGPIPE, &newsa, &oldsa); - -sigemptyset(&sigmask); -sigaddset(&sigmask, SIGHUP); -newsa.sa_handler = CatchHUP; -newsa.sa_mask = sigmask; -newsa.sa_flags = 0; -sigaction(SIGHUP, &newsa, &oldsa); - -sigemptyset(&sigmask); -sigaddset(&sigmask, SIGCHLD); -newsa.sa_handler = CatchCHLD; -newsa.sa_mask = sigmask; -newsa.sa_flags = 0; -sigaction(SIGCHLD, &newsa, &oldsa); -} //----------------------------------------------------------------------------- void KillExecuters() { @@ -307,9 +173,9 @@ int ForkAndWait(const string & confDir) #endif { #ifndef NO_DAEMON -stgChildPid = fork(); +pid_t childPid = fork(); -switch (stgChildPid) +switch (childPid) { case -1: // Failure return -1; @@ -332,15 +198,6 @@ return 0; //----------------------------------------------------------------------------- int main(int argc, char * argv[]) { - -/* - Initialization order: - - Logger - - Config - - Set signal nandlers - - Fork and exit - */ - CONFIGFILE * cfg = NULL; LISTENER * listener = NULL; int msgID = -11; @@ -385,7 +242,6 @@ cfg->ReadInt("UserTimeout", &userTimeout, 60); cfg->ReadString("ScriptOnConnect", &onConnect, "/etc/rscriptd/OnConnect"); cfg->ReadString("ScriptOnDisconnect", &onDisconnect, "/etc/rscriptd/OnDisconnect"); -SetSignalHandlers(); if (ForkAndWait(confDir) < 0) { STG_LOGGER & WriteServLog = GetStgLogger(); @@ -428,10 +284,32 @@ listener->Start(); WriteServLog("rscriptd started successfully."); WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++"); -while (nonstop.GetStatus()) +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + +while (true) { - struct timespec ts = {0, 100000000}; - nanosleep(&ts, NULL); + sigfillset(&signalSet); + int sig = 0; + printfd(__FILE__, "Before sigwait\n"); + sigwait(&signalSet, &sig); + printfd(__FILE__, "After sigwait. Signal: %d\n", sig); + bool stop = false; + switch (sig) + { + case SIGTERM: + stop = true; + break; + case SIGINT: + stop = true; + break; + default: + WriteServLog("Ignore signel %d", sig); + break; + } + if (stop) + break; } listener->Stop(); diff --git a/projects/stargazer/main.cpp b/projects/stargazer/main.cpp index 7e83b72d..80ec465b 100644 --- a/projects/stargazer/main.cpp +++ b/projects/stargazer/main.cpp @@ -72,7 +72,6 @@ using namespace std; #define START_FILE "/._ST_ART_ED_" set executersPid; -static pid_t stgChildPid; //----------------------------------------------------------------------------- bool StartModCmp(const PLUGIN_RUNNER & lhs, const PLUGIN_RUNNER & rhs) @@ -394,9 +393,12 @@ WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++"); creat(startFile.c_str(), S_IRUSR); #endif +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + while (true) { - sigset_t signalSet; sigfillset(&signalSet); int sig = 0; sigwait(&signalSet, &sig); diff --git a/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp b/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp index 1eac0ab1..a6d2073a 100644 --- a/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp +++ b/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp @@ -431,31 +431,6 @@ if (isRunningRun) struct timespec ts = {0, 200000000}; nanosleep(&ts, NULL); } - - //after 5 seconds waiting thread still running. now killing it - if (isRunningRun) - { - if (pthread_kill(recvThread, SIGINT)) - { - errorStr = "Cannot kill thread."; - printfd(__FILE__, "Cannot kill thread\n"); - return -1; - } - for (int i = 0; i < 25 && isRunningRun; ++i) - { - struct timespec ts = {0, 200000000}; - nanosleep(&ts, NULL); - } - if (isRunningRun) - { - printfd(__FILE__, "Failed to stop recv thread\n"); - } - else - { - pthread_join(recvThread, NULL); - } - printfd(__FILE__, "AUTH_IA killed Run\n"); - } } FinalizeNet(); @@ -468,33 +443,14 @@ if (isRunningRunTimeouter) struct timespec ts = {0, 200000000}; nanosleep(&ts, NULL); } - - //after 5 seconds waiting thread still running. now killing it - if (isRunningRunTimeouter) - { - if (pthread_kill(timeouterThread, SIGINT)) - { - errorStr = "Cannot kill thread."; - return -1; - } - for (int i = 0; i < 25 && isRunningRunTimeouter; ++i) - { - struct timespec ts = {0, 200000000}; - nanosleep(&ts, NULL); - } - if (isRunningRunTimeouter) - { - printfd(__FILE__, "Failed to stop timeouter thread\n"); - } - else - { - pthread_join(timeouterThread, NULL); - } - printfd(__FILE__, "AUTH_IA killed Timeouter\n"); - } } -printfd(__FILE__, "AUTH_IA::Stoped successfully.\n"); + users->DelNotifierUserDel(&onDelUserNotifier); + +if (isRunningRun || isRunningRunTimeouter) + return -1; + +printfd(__FILE__, "AUTH_IA::Stoped successfully.\n"); return 0; } //----------------------------------------------------------------------------- diff --git a/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp b/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp index 3cd6b901..ea71a117 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp +++ b/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp @@ -127,18 +127,8 @@ for (i = 0; i < 25; i++) nanosleep(&ts, NULL); } -//after 5 seconds waiting thread still running. now killing it if (isRunning) - { - //TODO pthread_cancel() - if (pthread_kill(thread, SIGINT)) - { - errorStr = "Cannot kill thread."; - printfd(__FILE__, "Cannot kill thread\n"); - return -1; - } - printfd(__FILE__, "STG_CONFIG killed\n"); - } + return -1; return 0; } diff --git a/projects/stargazer/plugins/other/ping/ping.cpp b/projects/stargazer/plugins/other/ping/ping.cpp index f4755c23..f0868e12 100644 --- a/projects/stargazer/plugins/other/ping/ping.cpp +++ b/projects/stargazer/plugins/other/ping/ping.cpp @@ -133,19 +133,6 @@ for (int i = 0; i < 25; i++) nanosleep(&ts, NULL); } -//after 5 seconds waiting thread still running. now kill it -if (isRunning) - { - printfd(__FILE__, "kill PING thread.\n"); - if (pthread_kill(thread, SIGINT)) - { - errorStr = "Cannot kill PING thread."; - printfd(__FILE__, "Cannot kill PING thread.\n"); - return -1; - } - printfd(__FILE__, "PING killed\n"); - } - users->DelNotifierUserAdd(&onAddUserNotifier); users->DelNotifierUserDel(&onDelUserNotifier); @@ -157,6 +144,9 @@ while (users_iter != usersList.end()) ++users_iter; } +if (isRunning) + return -1; + return 0; } //----------------------------------------------------------------------------- diff --git a/projects/stargazer/plugins/other/radius/radius.cpp b/projects/stargazer/plugins/other/radius/radius.cpp index c827330f..18c18816 100644 --- a/projects/stargazer/plugins/other/radius/radius.cpp +++ b/projects/stargazer/plugins/other/radius/radius.cpp @@ -239,20 +239,11 @@ if (isRunning) struct timespec ts = {0, 200000000}; nanosleep(&ts, NULL); } - - //after 5 seconds waiting thread still running. now killing it - if (isRunning) - { - if (pthread_kill(thread, SIGINT)) - { - errorStr = "Cannot kill thread."; - printfd(__FILE__, "Cannot kill thread\n"); - return -1; - } - printfd(__FILE__, "RADIUS::Stop killed Run\n"); - } } +if (isRunning) + return -1; + return 0; } //----------------------------------------------------------------------------- diff --git a/projects/stargazer/plugins/other/rscript/rscript.cpp b/projects/stargazer/plugins/other/rscript/rscript.cpp index 17598006..c9fcc549 100644 --- a/projects/stargazer/plugins/other/rscript/rscript.cpp +++ b/projects/stargazer/plugins/other/rscript/rscript.cpp @@ -289,23 +289,14 @@ if (isRunning) struct timespec ts = {0, 200000000}; nanosleep(&ts, NULL); } - - //after 5 seconds waiting thread still running. now killing it - if (isRunning) - { - if (pthread_kill(thread, SIGINT)) - { - errorStr = "Cannot kill thread."; - printfd(__FILE__, "Cannot kill thread\n"); - return -1; - } - printfd(__FILE__, "REMOTE_SCRIPT killed Run\n"); - } } users->DelNotifierUserDel(&onDelUserNotifier); users->DelNotifierUserAdd(&onAddUserNotifier); +if (isRunning) + return -1; + return 0; } //----------------------------------------------------------------------------- diff --git a/projects/stargazer/plugins/other/smux/smux.cpp b/projects/stargazer/plugins/other/smux/smux.cpp index 5367e249..fcc0a9c2 100644 --- a/projects/stargazer/plugins/other/smux/smux.cpp +++ b/projects/stargazer/plugins/other/smux/smux.cpp @@ -224,22 +224,10 @@ if (!stopped) struct timespec ts = {0, 200000000}; nanosleep(&ts, NULL); } - - //after 5 seconds waiting thread still running. now killing it - if (!stopped) - { - printfd(__FILE__, "SMUX::Stop() - failed to stop thread, killing it\n"); - if (pthread_kill(thread, SIGINT)) - { - errorStr = "Cannot kill thread."; - printfd(__FILE__, "SMUX::Stop() - Cannot kill thread\n"); - return -1; - } - printfd(__FILE__, "SMUX::Stop() - killed Run\n"); - } } -pthread_join(thread, NULL); +if (stopped) + pthread_join(thread, NULL); ResetNotifiers(); @@ -259,6 +247,12 @@ sensors.erase(sensors.begin(), sensors.end()); close(sock); +if (!stopped) + { + running = true; + return -1; + } + printfd(__FILE__, "SMUX::Stop() - After\n"); return 0; } diff --git a/projects/stargazer/traffcounter_impl.cpp b/projects/stargazer/traffcounter_impl.cpp index a02e3b6d..da64a0b8 100644 --- a/projects/stargazer/traffcounter_impl.cpp +++ b/projects/stargazer/traffcounter_impl.cpp @@ -152,16 +152,9 @@ for (int i = 0; i < 25 && !stopped; i++) nanosleep(&ts, NULL); } -//after 5 seconds waiting thread still running. now kill it if (!stopped) - { - printfd(__FILE__, "kill TRAFFCOUNTER thread.\n"); - if (pthread_kill(thread, SIGINT)) - { - return -1; - } - printfd(__FILE__, "TRAFFCOUNTER killed\n"); - } + return -1; + printfd(__FILE__, "TRAFFCOUNTER::Stop()\n"); return 0; diff --git a/stglibs/pinger.lib/pinger.cpp b/stglibs/pinger.lib/pinger.cpp index 8e6370ed..af761ffa 100644 --- a/stglibs/pinger.lib/pinger.cpp +++ b/stglibs/pinger.lib/pinger.cpp @@ -98,16 +98,6 @@ if (isRunningRecver) struct timespec ts = {0, 200000000}; nanosleep(&ts, NULL); } - - //after 5 seconds waiting thread still running. now killing it - if (isRunningRecver) - { - if (pthread_kill(recvThread, SIGINT)) - { - errorStr = "Cannot kill thread."; - return -1; - } - } } if (isRunningSender) @@ -121,19 +111,13 @@ if (isRunningSender) struct timespec ts = {0, 200000000}; nanosleep(&ts, NULL); } - - //after 5 seconds waiting thread still running. now killing it - if (isRunningSender) - { - if (pthread_kill(sendThread, SIGINT)) - { - errorStr = "Cannot kill thread."; - return -1; - } - } } close(sendSocket); + +if (isRunningSender || isRunningRecver) + return -1; + return 0; } //----------------------------------------------------------------------------- -- 2.44.2 From 900527c952b8cd0140317cb7dc9d4b80eb1239bf Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Fri, 16 Sep 2011 22:34:48 +0300 Subject: [PATCH 10/16] Use external socket to correctly stop XML-RPC server --- .../configuration/rpcconfig/rpcconfig.cpp | 92 +++++++++++++------ .../configuration/rpcconfig/rpcconfig.h | 1 + 2 files changed, 66 insertions(+), 27 deletions(-) diff --git a/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp b/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp index d3fdba55..d4419b40 100644 --- a/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp +++ b/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp @@ -1,6 +1,12 @@ +#include +#include +#include +#include + #include #include - +#include +#include #include #include @@ -80,6 +86,7 @@ RPC_CONFIG::RPC_CONFIG() tariffs(NULL), store(NULL), settings(), + fd(-1), rpcRegistry(), rpcServer(NULL), running(false), @@ -120,17 +127,57 @@ int RPC_CONFIG::Start() { InitiateRegistry(); running = true; + +fd = socket(AF_INET, SOCK_STREAM, 0); +if (fd < 0) + { + errorStr = "Failed to create socket"; + printfd(__FILE__, "Failed to create listening socket: %s\n", strerror(errno)); + return -1; + } + +int flag = 1; + +if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag))) + { + errorStr = "Setsockopt failed."; + printfd(__FILE__, "Setsockopt failed: %s\n", strerror(errno)); + return -1; + } + +struct sockaddr_in addr; +addr.sin_family = AF_INET; +addr.sin_port = htons(rpcConfigSettings.GetPort()); +addr.sin_addr.s_addr = inet_addr("0.0.0.0"); + +if (bind(fd, (struct sockaddr *)&addr, sizeof(addr))) + { + errorStr = "Failed to bind socket"; + printfd(__FILE__, "Failed to bind listening socket: %s\n", strerror(errno)); + return -1; + } + +if (listen(fd, 10)) + { + errorStr = "Failed to listen socket"; + printfd(__FILE__, "Failed to listen listening socket: %s\n", strerror(errno)); + return -1; + } + rpcServer = new xmlrpc_c::serverAbyss( - rpcRegistry, - rpcConfigSettings.GetPort(), - "/var/log/stargazer_rpc.log" + xmlrpc_c::serverAbyss::constrOpt() + .registryP(&rpcRegistry) + .logFileName("/var/log/stargazer_rpc.log") + .socketFd(fd) ); + if (pthread_create(&tid, NULL, Run, this)) { errorStr = "Failed to create RPC thread"; printfd(__FILE__, "Failed to crate RPC thread\n"); return -1; } + return 0; } @@ -142,31 +189,21 @@ for (int i = 0; i < 5 && !stopped; ++i) struct timespec ts = {0, 200000000}; nanosleep(&ts, NULL); } -//rpcServer->terminate(); + if (!stopped) { - if (pthread_kill(tid, SIGTERM)) - { - errorStr = "Failed to kill thread"; - printfd(__FILE__, "Failed to kill thread\n"); - } - for (int i = 0; i < 25 && !stopped; ++i) - { - struct timespec ts = {0, 200000000}; - nanosleep(&ts, NULL); - } - if (!stopped) - { - running = true; - printfd(__FILE__, "Failed to stop RPC thread\n"); - errorStr = "Failed to stop RPC thread"; - return -1; - } - else - { - pthread_join(tid, NULL); - } + running = true; + printfd(__FILE__, "Failed to stop RPC thread\n"); + errorStr = "Failed to stop RPC thread"; + return -1; } +else + { + pthread_join(tid, NULL); + } + +close(fd); + return 0; } @@ -181,7 +218,8 @@ RPC_CONFIG * config = static_cast(rc); config->stopped = false; while (config->running) { - config->rpcServer->runOnce(); + if (WaitPackets(config->fd)) + config->rpcServer->runOnce(); } config->stopped = true; diff --git a/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.h b/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.h index 4e6d9f1f..87ef9470 100644 --- a/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.h +++ b/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.h @@ -101,6 +101,7 @@ private: TARIFFS * tariffs; STORE * store; MODULE_SETTINGS settings; + int fd; xmlrpc_c::registry rpcRegistry; xmlrpc_c::serverAbyss * rpcServer; bool running; -- 2.44.2 From f12f8cc038c4421884a6787e110a517bbaea1953 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Mon, 19 Sep 2011 10:53:02 +0300 Subject: [PATCH 11/16] Block all signals earlie, to inherit by all nested threads --- projects/stargazer/main.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/projects/stargazer/main.cpp b/projects/stargazer/main.cpp index 80ec465b..3839ae8e 100644 --- a/projects/stargazer/main.cpp +++ b/projects/stargazer/main.cpp @@ -286,6 +286,10 @@ for (size_t i = 0; i < settings->GetExecutersNum(); i++) PIDFile pidFile(settings->GetPIDFileName()); +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + StartTimer(); WaitTimer(); if (!IsStgTimerRunning()) @@ -393,10 +397,6 @@ WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++"); creat(startFile.c_str(), S_IRUSR); #endif -sigset_t signalSet; -sigfillset(&signalSet); -pthread_sigmask(SIG_BLOCK, &signalSet, NULL); - while (true) { sigfillset(&signalSet); -- 2.44.2 From 355911106bc27f5504eb904a064367ef1b5f0aba Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Mon, 19 Sep 2011 10:57:28 +0300 Subject: [PATCH 12/16] Make fee charging rules inclusive (2 includes 0 and 3 includes fee) --- projects/stargazer/user_impl.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/projects/stargazer/user_impl.cpp b/projects/stargazer/user_impl.cpp index 1278afa2..ca2e2cae 100644 --- a/projects/stargazer/user_impl.cpp +++ b/projects/stargazer/user_impl.cpp @@ -1243,11 +1243,11 @@ switch (settings->GetFeeChargeType()) property.cash.Set(c - fee, sysAdmin, login, store, "Subscriber fee charge"); break; case 1: - if (c > 0) + if (c >= 0) property.cash.Set(c - fee, sysAdmin, login, store, "Subscriber fee charge"); break; case 2: - if (c > fee) + if (c >= fee) property.cash.Set(c - fee, sysAdmin, login, store, "Subscriber fee charge"); break; } @@ -1297,14 +1297,14 @@ switch (settings->GetFeeChargeType()) SetPrepaidTraff(); break; case 1: - if (c > 0) + if (c >= 0) { property.cash.Set(c - fee, sysAdmin, login, store, "Subscriber fee charge"); SetPrepaidTraff(); } break; case 2: - if (c > fee) + if (c >= fee) { property.cash.Set(c - fee, sysAdmin, login, store, "Subscriber fee charge"); SetPrepaidTraff(); -- 2.44.2 From 261442af210d652fc2c8a3d9197097982701bd16 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Mon, 19 Sep 2011 11:57:52 +0300 Subject: [PATCH 13/16] TUT framework updated to svn version Libs headers moved to its own folders --- stglibs/common.lib/{ => include/stg}/common.h | 0 .../{ => include/stg}/conffiles.h | 0 stglibs/crypto.lib/{ => include/stg}/ag_md5.h | 0 .../crypto.lib/{ => include/stg}/blowfish.h | 0 .../{ => include/stg}/dotconfpp.h | 0 stglibs/ia.lib/{ => include/stg}/ia.h | 0 stglibs/ibpp.lib/{ => include/stg}/ibpp.h | 0 stglibs/locker.lib/{ => include/stg}/locker.h | 0 stglibs/logger.lib/{ => include/stg}/logger.h | 0 stglibs/pinger.lib/{ => include/stg}/pinger.h | 0 .../{ => include/stg}/scriptexecuter.h | 0 stglibs/smux.lib/{ => include/stg}/ANY.h | 0 .../{ => include/stg}/ApplicationSyntax.h | 0 stglibs/smux.lib/{ => include/stg}/AtEntry.h | 0 .../smux.lib/{ => include/stg}/BIT_STRING.h | 0 stglibs/smux.lib/{ => include/stg}/BOOLEAN.h | 0 stglibs/smux.lib/{ => include/stg}/ClosePDU.h | 0 stglibs/smux.lib/{ => include/stg}/Counter.h | 0 .../{ => include/stg}/DisplayString.h | 0 .../{ => include/stg}/EgpNeighEntry.h | 0 stglibs/smux.lib/{ => include/stg}/Gauge.h | 0 .../{ => include/stg}/GetNextRequest-PDU.h | 0 .../{ => include/stg}/GetRequest-PDU.h | 0 .../{ => include/stg}/GetResponse-PDU.h | 0 stglibs/smux.lib/{ => include/stg}/INTEGER.h | 0 stglibs/smux.lib/{ => include/stg}/IfEntry.h | 0 .../smux.lib/{ => include/stg}/IpAddrEntry.h | 0 .../smux.lib/{ => include/stg}/IpAddress.h | 0 .../{ => include/stg}/IpNetToMediaEntry.h | 0 .../smux.lib/{ => include/stg}/IpRouteEntry.h | 0 stglibs/smux.lib/{ => include/stg}/Message.h | 0 stglibs/smux.lib/{ => include/stg}/NULL.h | 0 .../{ => include/stg}/NativeEnumerated.h | 0 .../{ => include/stg}/NativeInteger.h | 0 .../{ => include/stg}/NetworkAddress.h | 0 .../{ => include/stg}/OBJECT_IDENTIFIER.h | 0 .../smux.lib/{ => include/stg}/OCTET_STRING.h | 0 .../smux.lib/{ => include/stg}/ObjectName.h | 0 .../smux.lib/{ => include/stg}/ObjectSyntax.h | 0 stglibs/smux.lib/{ => include/stg}/Opaque.h | 0 stglibs/smux.lib/{ => include/stg}/OpenPDU.h | 0 stglibs/smux.lib/{ => include/stg}/PDU.h | 0 stglibs/smux.lib/{ => include/stg}/PDUs.h | 0 .../smux.lib/{ => include/stg}/PhysAddress.h | 0 stglibs/smux.lib/{ => include/stg}/RReqPDU.h | 0 stglibs/smux.lib/{ => include/stg}/RRspPDU.h | 0 .../smux.lib/{ => include/stg}/SMUX-PDUs.h | 0 stglibs/smux.lib/{ => include/stg}/SOutPDU.h | 0 .../{ => include/stg}/SetRequest-PDU.h | 0 .../smux.lib/{ => include/stg}/SimpleOpen.h | 0 .../smux.lib/{ => include/stg}/SimpleSyntax.h | 0 .../smux.lib/{ => include/stg}/TcpConnEntry.h | 0 .../smux.lib/{ => include/stg}/TimeTicks.h | 0 stglibs/smux.lib/{ => include/stg}/Trap-PDU.h | 0 stglibs/smux.lib/{ => include/stg}/UdpEntry.h | 0 stglibs/smux.lib/{ => include/stg}/VarBind.h | 0 .../smux.lib/{ => include/stg}/VarBindList.h | 0 .../{ => include/stg}/asn_SEQUENCE_OF.h | 0 .../smux.lib/{ => include/stg}/asn_SET_OF.h | 0 .../{ => include/stg}/asn_application.h | 0 .../smux.lib/{ => include/stg}/asn_codecs.h | 0 .../{ => include/stg}/asn_codecs_prim.h | 0 .../smux.lib/{ => include/stg}/asn_internal.h | 0 .../smux.lib/{ => include/stg}/asn_system.h | 0 .../smux.lib/{ => include/stg}/ber_decoder.h | 0 .../{ => include/stg}/ber_tlv_length.h | 0 .../smux.lib/{ => include/stg}/ber_tlv_tag.h | 0 .../{ => include/stg}/constr_CHOICE.h | 0 .../{ => include/stg}/constr_SEQUENCE.h | 0 .../{ => include/stg}/constr_SEQUENCE_OF.h | 0 .../{ => include/stg}/constr_SET_OF.h | 0 .../smux.lib/{ => include/stg}/constr_TYPE.h | 0 .../smux.lib/{ => include/stg}/constraints.h | 0 .../smux.lib/{ => include/stg}/der_encoder.h | 0 .../smux.lib/{ => include/stg}/per_decoder.h | 0 .../smux.lib/{ => include/stg}/per_encoder.h | 0 .../smux.lib/{ => include/stg}/per_support.h | 0 .../smux.lib/{ => include/stg}/xer_decoder.h | 0 .../smux.lib/{ => include/stg}/xer_encoder.h | 0 .../smux.lib/{ => include/stg}/xer_support.h | 0 .../srvconf.lib/{ => include/stg}/netunit.h | 0 .../srvconf.lib/{ => include/stg}/servconf.h | 0 tests/tut.h | 2 - tests/tut/tut.hpp | 136 +++++++---- tests/tut/tut_assert.hpp | 155 ++++++++++-- tests/tut/tut_config.hpp | 6 + tests/tut/tut_console_reporter.hpp | 95 +++++--- tests/tut/tut_cppunit_reporter.hpp | 213 +++++++++-------- tests/tut/tut_exception.hpp | 92 +++++++- tests/tut/tut_fpt.hpp | 185 +++++++++++++++ tests/tut/tut_macros.hpp | 63 +++++ tests/tut/tut_main.hpp | 111 +++++++++ tests/tut/tut_posix.hpp | 37 ++- tests/tut/tut_restartable.hpp | 9 +- tests/tut/tut_result.hpp | 79 +++++-- tests/tut/tut_runner.hpp | 55 +++-- tests/tut/tut_xml_reporter.hpp | 220 +++++++----------- tests/tut_reporter.h | 5 - tests/tut_restartable.h | 2 - 99 files changed, 1061 insertions(+), 404 deletions(-) rename stglibs/common.lib/{ => include/stg}/common.h (100%) rename stglibs/conffiles.lib/{ => include/stg}/conffiles.h (100%) rename stglibs/crypto.lib/{ => include/stg}/ag_md5.h (100%) rename stglibs/crypto.lib/{ => include/stg}/blowfish.h (100%) rename stglibs/dotconfpp.lib/{ => include/stg}/dotconfpp.h (100%) rename stglibs/ia.lib/{ => include/stg}/ia.h (100%) rename stglibs/ibpp.lib/{ => include/stg}/ibpp.h (100%) rename stglibs/locker.lib/{ => include/stg}/locker.h (100%) rename stglibs/logger.lib/{ => include/stg}/logger.h (100%) rename stglibs/pinger.lib/{ => include/stg}/pinger.h (100%) rename stglibs/scriptexecuter.lib/{ => include/stg}/scriptexecuter.h (100%) rename stglibs/smux.lib/{ => include/stg}/ANY.h (100%) rename stglibs/smux.lib/{ => include/stg}/ApplicationSyntax.h (100%) rename stglibs/smux.lib/{ => include/stg}/AtEntry.h (100%) rename stglibs/smux.lib/{ => include/stg}/BIT_STRING.h (100%) rename stglibs/smux.lib/{ => include/stg}/BOOLEAN.h (100%) rename stglibs/smux.lib/{ => include/stg}/ClosePDU.h (100%) rename stglibs/smux.lib/{ => include/stg}/Counter.h (100%) rename stglibs/smux.lib/{ => include/stg}/DisplayString.h (100%) rename stglibs/smux.lib/{ => include/stg}/EgpNeighEntry.h (100%) rename stglibs/smux.lib/{ => include/stg}/Gauge.h (100%) rename stglibs/smux.lib/{ => include/stg}/GetNextRequest-PDU.h (100%) rename stglibs/smux.lib/{ => include/stg}/GetRequest-PDU.h (100%) rename stglibs/smux.lib/{ => include/stg}/GetResponse-PDU.h (100%) rename stglibs/smux.lib/{ => include/stg}/INTEGER.h (100%) rename stglibs/smux.lib/{ => include/stg}/IfEntry.h (100%) rename stglibs/smux.lib/{ => include/stg}/IpAddrEntry.h (100%) rename stglibs/smux.lib/{ => include/stg}/IpAddress.h (100%) rename stglibs/smux.lib/{ => include/stg}/IpNetToMediaEntry.h (100%) rename stglibs/smux.lib/{ => include/stg}/IpRouteEntry.h (100%) rename stglibs/smux.lib/{ => include/stg}/Message.h (100%) rename stglibs/smux.lib/{ => include/stg}/NULL.h (100%) rename stglibs/smux.lib/{ => include/stg}/NativeEnumerated.h (100%) rename stglibs/smux.lib/{ => include/stg}/NativeInteger.h (100%) rename stglibs/smux.lib/{ => include/stg}/NetworkAddress.h (100%) rename stglibs/smux.lib/{ => include/stg}/OBJECT_IDENTIFIER.h (100%) rename stglibs/smux.lib/{ => include/stg}/OCTET_STRING.h (100%) rename stglibs/smux.lib/{ => include/stg}/ObjectName.h (100%) rename stglibs/smux.lib/{ => include/stg}/ObjectSyntax.h (100%) rename stglibs/smux.lib/{ => include/stg}/Opaque.h (100%) rename stglibs/smux.lib/{ => include/stg}/OpenPDU.h (100%) rename stglibs/smux.lib/{ => include/stg}/PDU.h (100%) rename stglibs/smux.lib/{ => include/stg}/PDUs.h (100%) rename stglibs/smux.lib/{ => include/stg}/PhysAddress.h (100%) rename stglibs/smux.lib/{ => include/stg}/RReqPDU.h (100%) rename stglibs/smux.lib/{ => include/stg}/RRspPDU.h (100%) rename stglibs/smux.lib/{ => include/stg}/SMUX-PDUs.h (100%) rename stglibs/smux.lib/{ => include/stg}/SOutPDU.h (100%) rename stglibs/smux.lib/{ => include/stg}/SetRequest-PDU.h (100%) rename stglibs/smux.lib/{ => include/stg}/SimpleOpen.h (100%) rename stglibs/smux.lib/{ => include/stg}/SimpleSyntax.h (100%) rename stglibs/smux.lib/{ => include/stg}/TcpConnEntry.h (100%) rename stglibs/smux.lib/{ => include/stg}/TimeTicks.h (100%) rename stglibs/smux.lib/{ => include/stg}/Trap-PDU.h (100%) rename stglibs/smux.lib/{ => include/stg}/UdpEntry.h (100%) rename stglibs/smux.lib/{ => include/stg}/VarBind.h (100%) rename stglibs/smux.lib/{ => include/stg}/VarBindList.h (100%) rename stglibs/smux.lib/{ => include/stg}/asn_SEQUENCE_OF.h (100%) rename stglibs/smux.lib/{ => include/stg}/asn_SET_OF.h (100%) rename stglibs/smux.lib/{ => include/stg}/asn_application.h (100%) rename stglibs/smux.lib/{ => include/stg}/asn_codecs.h (100%) rename stglibs/smux.lib/{ => include/stg}/asn_codecs_prim.h (100%) rename stglibs/smux.lib/{ => include/stg}/asn_internal.h (100%) rename stglibs/smux.lib/{ => include/stg}/asn_system.h (100%) rename stglibs/smux.lib/{ => include/stg}/ber_decoder.h (100%) rename stglibs/smux.lib/{ => include/stg}/ber_tlv_length.h (100%) rename stglibs/smux.lib/{ => include/stg}/ber_tlv_tag.h (100%) rename stglibs/smux.lib/{ => include/stg}/constr_CHOICE.h (100%) rename stglibs/smux.lib/{ => include/stg}/constr_SEQUENCE.h (100%) rename stglibs/smux.lib/{ => include/stg}/constr_SEQUENCE_OF.h (100%) rename stglibs/smux.lib/{ => include/stg}/constr_SET_OF.h (100%) rename stglibs/smux.lib/{ => include/stg}/constr_TYPE.h (100%) rename stglibs/smux.lib/{ => include/stg}/constraints.h (100%) rename stglibs/smux.lib/{ => include/stg}/der_encoder.h (100%) rename stglibs/smux.lib/{ => include/stg}/per_decoder.h (100%) rename stglibs/smux.lib/{ => include/stg}/per_encoder.h (100%) rename stglibs/smux.lib/{ => include/stg}/per_support.h (100%) rename stglibs/smux.lib/{ => include/stg}/xer_decoder.h (100%) rename stglibs/smux.lib/{ => include/stg}/xer_encoder.h (100%) rename stglibs/smux.lib/{ => include/stg}/xer_support.h (100%) rename stglibs/srvconf.lib/{ => include/stg}/netunit.h (100%) rename stglibs/srvconf.lib/{ => include/stg}/servconf.h (100%) delete mode 100644 tests/tut.h create mode 100644 tests/tut/tut_config.hpp create mode 100644 tests/tut/tut_fpt.hpp create mode 100644 tests/tut/tut_macros.hpp create mode 100644 tests/tut/tut_main.hpp delete mode 100644 tests/tut_reporter.h delete mode 100644 tests/tut_restartable.h diff --git a/stglibs/common.lib/common.h b/stglibs/common.lib/include/stg/common.h similarity index 100% rename from stglibs/common.lib/common.h rename to stglibs/common.lib/include/stg/common.h diff --git a/stglibs/conffiles.lib/conffiles.h b/stglibs/conffiles.lib/include/stg/conffiles.h similarity index 100% rename from stglibs/conffiles.lib/conffiles.h rename to stglibs/conffiles.lib/include/stg/conffiles.h diff --git a/stglibs/crypto.lib/ag_md5.h b/stglibs/crypto.lib/include/stg/ag_md5.h similarity index 100% rename from stglibs/crypto.lib/ag_md5.h rename to stglibs/crypto.lib/include/stg/ag_md5.h diff --git a/stglibs/crypto.lib/blowfish.h b/stglibs/crypto.lib/include/stg/blowfish.h similarity index 100% rename from stglibs/crypto.lib/blowfish.h rename to stglibs/crypto.lib/include/stg/blowfish.h diff --git a/stglibs/dotconfpp.lib/dotconfpp.h b/stglibs/dotconfpp.lib/include/stg/dotconfpp.h similarity index 100% rename from stglibs/dotconfpp.lib/dotconfpp.h rename to stglibs/dotconfpp.lib/include/stg/dotconfpp.h diff --git a/stglibs/ia.lib/ia.h b/stglibs/ia.lib/include/stg/ia.h similarity index 100% rename from stglibs/ia.lib/ia.h rename to stglibs/ia.lib/include/stg/ia.h diff --git a/stglibs/ibpp.lib/ibpp.h b/stglibs/ibpp.lib/include/stg/ibpp.h similarity index 100% rename from stglibs/ibpp.lib/ibpp.h rename to stglibs/ibpp.lib/include/stg/ibpp.h diff --git a/stglibs/locker.lib/locker.h b/stglibs/locker.lib/include/stg/locker.h similarity index 100% rename from stglibs/locker.lib/locker.h rename to stglibs/locker.lib/include/stg/locker.h diff --git a/stglibs/logger.lib/logger.h b/stglibs/logger.lib/include/stg/logger.h similarity index 100% rename from stglibs/logger.lib/logger.h rename to stglibs/logger.lib/include/stg/logger.h diff --git a/stglibs/pinger.lib/pinger.h b/stglibs/pinger.lib/include/stg/pinger.h similarity index 100% rename from stglibs/pinger.lib/pinger.h rename to stglibs/pinger.lib/include/stg/pinger.h diff --git a/stglibs/scriptexecuter.lib/scriptexecuter.h b/stglibs/scriptexecuter.lib/include/stg/scriptexecuter.h similarity index 100% rename from stglibs/scriptexecuter.lib/scriptexecuter.h rename to stglibs/scriptexecuter.lib/include/stg/scriptexecuter.h diff --git a/stglibs/smux.lib/ANY.h b/stglibs/smux.lib/include/stg/ANY.h similarity index 100% rename from stglibs/smux.lib/ANY.h rename to stglibs/smux.lib/include/stg/ANY.h diff --git a/stglibs/smux.lib/ApplicationSyntax.h b/stglibs/smux.lib/include/stg/ApplicationSyntax.h similarity index 100% rename from stglibs/smux.lib/ApplicationSyntax.h rename to stglibs/smux.lib/include/stg/ApplicationSyntax.h diff --git a/stglibs/smux.lib/AtEntry.h b/stglibs/smux.lib/include/stg/AtEntry.h similarity index 100% rename from stglibs/smux.lib/AtEntry.h rename to stglibs/smux.lib/include/stg/AtEntry.h diff --git a/stglibs/smux.lib/BIT_STRING.h b/stglibs/smux.lib/include/stg/BIT_STRING.h similarity index 100% rename from stglibs/smux.lib/BIT_STRING.h rename to stglibs/smux.lib/include/stg/BIT_STRING.h diff --git a/stglibs/smux.lib/BOOLEAN.h b/stglibs/smux.lib/include/stg/BOOLEAN.h similarity index 100% rename from stglibs/smux.lib/BOOLEAN.h rename to stglibs/smux.lib/include/stg/BOOLEAN.h diff --git a/stglibs/smux.lib/ClosePDU.h b/stglibs/smux.lib/include/stg/ClosePDU.h similarity index 100% rename from stglibs/smux.lib/ClosePDU.h rename to stglibs/smux.lib/include/stg/ClosePDU.h diff --git a/stglibs/smux.lib/Counter.h b/stglibs/smux.lib/include/stg/Counter.h similarity index 100% rename from stglibs/smux.lib/Counter.h rename to stglibs/smux.lib/include/stg/Counter.h diff --git a/stglibs/smux.lib/DisplayString.h b/stglibs/smux.lib/include/stg/DisplayString.h similarity index 100% rename from stglibs/smux.lib/DisplayString.h rename to stglibs/smux.lib/include/stg/DisplayString.h diff --git a/stglibs/smux.lib/EgpNeighEntry.h b/stglibs/smux.lib/include/stg/EgpNeighEntry.h similarity index 100% rename from stglibs/smux.lib/EgpNeighEntry.h rename to stglibs/smux.lib/include/stg/EgpNeighEntry.h diff --git a/stglibs/smux.lib/Gauge.h b/stglibs/smux.lib/include/stg/Gauge.h similarity index 100% rename from stglibs/smux.lib/Gauge.h rename to stglibs/smux.lib/include/stg/Gauge.h diff --git a/stglibs/smux.lib/GetNextRequest-PDU.h b/stglibs/smux.lib/include/stg/GetNextRequest-PDU.h similarity index 100% rename from stglibs/smux.lib/GetNextRequest-PDU.h rename to stglibs/smux.lib/include/stg/GetNextRequest-PDU.h diff --git a/stglibs/smux.lib/GetRequest-PDU.h b/stglibs/smux.lib/include/stg/GetRequest-PDU.h similarity index 100% rename from stglibs/smux.lib/GetRequest-PDU.h rename to stglibs/smux.lib/include/stg/GetRequest-PDU.h diff --git a/stglibs/smux.lib/GetResponse-PDU.h b/stglibs/smux.lib/include/stg/GetResponse-PDU.h similarity index 100% rename from stglibs/smux.lib/GetResponse-PDU.h rename to stglibs/smux.lib/include/stg/GetResponse-PDU.h diff --git a/stglibs/smux.lib/INTEGER.h b/stglibs/smux.lib/include/stg/INTEGER.h similarity index 100% rename from stglibs/smux.lib/INTEGER.h rename to stglibs/smux.lib/include/stg/INTEGER.h diff --git a/stglibs/smux.lib/IfEntry.h b/stglibs/smux.lib/include/stg/IfEntry.h similarity index 100% rename from stglibs/smux.lib/IfEntry.h rename to stglibs/smux.lib/include/stg/IfEntry.h diff --git a/stglibs/smux.lib/IpAddrEntry.h b/stglibs/smux.lib/include/stg/IpAddrEntry.h similarity index 100% rename from stglibs/smux.lib/IpAddrEntry.h rename to stglibs/smux.lib/include/stg/IpAddrEntry.h diff --git a/stglibs/smux.lib/IpAddress.h b/stglibs/smux.lib/include/stg/IpAddress.h similarity index 100% rename from stglibs/smux.lib/IpAddress.h rename to stglibs/smux.lib/include/stg/IpAddress.h diff --git a/stglibs/smux.lib/IpNetToMediaEntry.h b/stglibs/smux.lib/include/stg/IpNetToMediaEntry.h similarity index 100% rename from stglibs/smux.lib/IpNetToMediaEntry.h rename to stglibs/smux.lib/include/stg/IpNetToMediaEntry.h diff --git a/stglibs/smux.lib/IpRouteEntry.h b/stglibs/smux.lib/include/stg/IpRouteEntry.h similarity index 100% rename from stglibs/smux.lib/IpRouteEntry.h rename to stglibs/smux.lib/include/stg/IpRouteEntry.h diff --git a/stglibs/smux.lib/Message.h b/stglibs/smux.lib/include/stg/Message.h similarity index 100% rename from stglibs/smux.lib/Message.h rename to stglibs/smux.lib/include/stg/Message.h diff --git a/stglibs/smux.lib/NULL.h b/stglibs/smux.lib/include/stg/NULL.h similarity index 100% rename from stglibs/smux.lib/NULL.h rename to stglibs/smux.lib/include/stg/NULL.h diff --git a/stglibs/smux.lib/NativeEnumerated.h b/stglibs/smux.lib/include/stg/NativeEnumerated.h similarity index 100% rename from stglibs/smux.lib/NativeEnumerated.h rename to stglibs/smux.lib/include/stg/NativeEnumerated.h diff --git a/stglibs/smux.lib/NativeInteger.h b/stglibs/smux.lib/include/stg/NativeInteger.h similarity index 100% rename from stglibs/smux.lib/NativeInteger.h rename to stglibs/smux.lib/include/stg/NativeInteger.h diff --git a/stglibs/smux.lib/NetworkAddress.h b/stglibs/smux.lib/include/stg/NetworkAddress.h similarity index 100% rename from stglibs/smux.lib/NetworkAddress.h rename to stglibs/smux.lib/include/stg/NetworkAddress.h diff --git a/stglibs/smux.lib/OBJECT_IDENTIFIER.h b/stglibs/smux.lib/include/stg/OBJECT_IDENTIFIER.h similarity index 100% rename from stglibs/smux.lib/OBJECT_IDENTIFIER.h rename to stglibs/smux.lib/include/stg/OBJECT_IDENTIFIER.h diff --git a/stglibs/smux.lib/OCTET_STRING.h b/stglibs/smux.lib/include/stg/OCTET_STRING.h similarity index 100% rename from stglibs/smux.lib/OCTET_STRING.h rename to stglibs/smux.lib/include/stg/OCTET_STRING.h diff --git a/stglibs/smux.lib/ObjectName.h b/stglibs/smux.lib/include/stg/ObjectName.h similarity index 100% rename from stglibs/smux.lib/ObjectName.h rename to stglibs/smux.lib/include/stg/ObjectName.h diff --git a/stglibs/smux.lib/ObjectSyntax.h b/stglibs/smux.lib/include/stg/ObjectSyntax.h similarity index 100% rename from stglibs/smux.lib/ObjectSyntax.h rename to stglibs/smux.lib/include/stg/ObjectSyntax.h diff --git a/stglibs/smux.lib/Opaque.h b/stglibs/smux.lib/include/stg/Opaque.h similarity index 100% rename from stglibs/smux.lib/Opaque.h rename to stglibs/smux.lib/include/stg/Opaque.h diff --git a/stglibs/smux.lib/OpenPDU.h b/stglibs/smux.lib/include/stg/OpenPDU.h similarity index 100% rename from stglibs/smux.lib/OpenPDU.h rename to stglibs/smux.lib/include/stg/OpenPDU.h diff --git a/stglibs/smux.lib/PDU.h b/stglibs/smux.lib/include/stg/PDU.h similarity index 100% rename from stglibs/smux.lib/PDU.h rename to stglibs/smux.lib/include/stg/PDU.h diff --git a/stglibs/smux.lib/PDUs.h b/stglibs/smux.lib/include/stg/PDUs.h similarity index 100% rename from stglibs/smux.lib/PDUs.h rename to stglibs/smux.lib/include/stg/PDUs.h diff --git a/stglibs/smux.lib/PhysAddress.h b/stglibs/smux.lib/include/stg/PhysAddress.h similarity index 100% rename from stglibs/smux.lib/PhysAddress.h rename to stglibs/smux.lib/include/stg/PhysAddress.h diff --git a/stglibs/smux.lib/RReqPDU.h b/stglibs/smux.lib/include/stg/RReqPDU.h similarity index 100% rename from stglibs/smux.lib/RReqPDU.h rename to stglibs/smux.lib/include/stg/RReqPDU.h diff --git a/stglibs/smux.lib/RRspPDU.h b/stglibs/smux.lib/include/stg/RRspPDU.h similarity index 100% rename from stglibs/smux.lib/RRspPDU.h rename to stglibs/smux.lib/include/stg/RRspPDU.h diff --git a/stglibs/smux.lib/SMUX-PDUs.h b/stglibs/smux.lib/include/stg/SMUX-PDUs.h similarity index 100% rename from stglibs/smux.lib/SMUX-PDUs.h rename to stglibs/smux.lib/include/stg/SMUX-PDUs.h diff --git a/stglibs/smux.lib/SOutPDU.h b/stglibs/smux.lib/include/stg/SOutPDU.h similarity index 100% rename from stglibs/smux.lib/SOutPDU.h rename to stglibs/smux.lib/include/stg/SOutPDU.h diff --git a/stglibs/smux.lib/SetRequest-PDU.h b/stglibs/smux.lib/include/stg/SetRequest-PDU.h similarity index 100% rename from stglibs/smux.lib/SetRequest-PDU.h rename to stglibs/smux.lib/include/stg/SetRequest-PDU.h diff --git a/stglibs/smux.lib/SimpleOpen.h b/stglibs/smux.lib/include/stg/SimpleOpen.h similarity index 100% rename from stglibs/smux.lib/SimpleOpen.h rename to stglibs/smux.lib/include/stg/SimpleOpen.h diff --git a/stglibs/smux.lib/SimpleSyntax.h b/stglibs/smux.lib/include/stg/SimpleSyntax.h similarity index 100% rename from stglibs/smux.lib/SimpleSyntax.h rename to stglibs/smux.lib/include/stg/SimpleSyntax.h diff --git a/stglibs/smux.lib/TcpConnEntry.h b/stglibs/smux.lib/include/stg/TcpConnEntry.h similarity index 100% rename from stglibs/smux.lib/TcpConnEntry.h rename to stglibs/smux.lib/include/stg/TcpConnEntry.h diff --git a/stglibs/smux.lib/TimeTicks.h b/stglibs/smux.lib/include/stg/TimeTicks.h similarity index 100% rename from stglibs/smux.lib/TimeTicks.h rename to stglibs/smux.lib/include/stg/TimeTicks.h diff --git a/stglibs/smux.lib/Trap-PDU.h b/stglibs/smux.lib/include/stg/Trap-PDU.h similarity index 100% rename from stglibs/smux.lib/Trap-PDU.h rename to stglibs/smux.lib/include/stg/Trap-PDU.h diff --git a/stglibs/smux.lib/UdpEntry.h b/stglibs/smux.lib/include/stg/UdpEntry.h similarity index 100% rename from stglibs/smux.lib/UdpEntry.h rename to stglibs/smux.lib/include/stg/UdpEntry.h diff --git a/stglibs/smux.lib/VarBind.h b/stglibs/smux.lib/include/stg/VarBind.h similarity index 100% rename from stglibs/smux.lib/VarBind.h rename to stglibs/smux.lib/include/stg/VarBind.h diff --git a/stglibs/smux.lib/VarBindList.h b/stglibs/smux.lib/include/stg/VarBindList.h similarity index 100% rename from stglibs/smux.lib/VarBindList.h rename to stglibs/smux.lib/include/stg/VarBindList.h diff --git a/stglibs/smux.lib/asn_SEQUENCE_OF.h b/stglibs/smux.lib/include/stg/asn_SEQUENCE_OF.h similarity index 100% rename from stglibs/smux.lib/asn_SEQUENCE_OF.h rename to stglibs/smux.lib/include/stg/asn_SEQUENCE_OF.h diff --git a/stglibs/smux.lib/asn_SET_OF.h b/stglibs/smux.lib/include/stg/asn_SET_OF.h similarity index 100% rename from stglibs/smux.lib/asn_SET_OF.h rename to stglibs/smux.lib/include/stg/asn_SET_OF.h diff --git a/stglibs/smux.lib/asn_application.h b/stglibs/smux.lib/include/stg/asn_application.h similarity index 100% rename from stglibs/smux.lib/asn_application.h rename to stglibs/smux.lib/include/stg/asn_application.h diff --git a/stglibs/smux.lib/asn_codecs.h b/stglibs/smux.lib/include/stg/asn_codecs.h similarity index 100% rename from stglibs/smux.lib/asn_codecs.h rename to stglibs/smux.lib/include/stg/asn_codecs.h diff --git a/stglibs/smux.lib/asn_codecs_prim.h b/stglibs/smux.lib/include/stg/asn_codecs_prim.h similarity index 100% rename from stglibs/smux.lib/asn_codecs_prim.h rename to stglibs/smux.lib/include/stg/asn_codecs_prim.h diff --git a/stglibs/smux.lib/asn_internal.h b/stglibs/smux.lib/include/stg/asn_internal.h similarity index 100% rename from stglibs/smux.lib/asn_internal.h rename to stglibs/smux.lib/include/stg/asn_internal.h diff --git a/stglibs/smux.lib/asn_system.h b/stglibs/smux.lib/include/stg/asn_system.h similarity index 100% rename from stglibs/smux.lib/asn_system.h rename to stglibs/smux.lib/include/stg/asn_system.h diff --git a/stglibs/smux.lib/ber_decoder.h b/stglibs/smux.lib/include/stg/ber_decoder.h similarity index 100% rename from stglibs/smux.lib/ber_decoder.h rename to stglibs/smux.lib/include/stg/ber_decoder.h diff --git a/stglibs/smux.lib/ber_tlv_length.h b/stglibs/smux.lib/include/stg/ber_tlv_length.h similarity index 100% rename from stglibs/smux.lib/ber_tlv_length.h rename to stglibs/smux.lib/include/stg/ber_tlv_length.h diff --git a/stglibs/smux.lib/ber_tlv_tag.h b/stglibs/smux.lib/include/stg/ber_tlv_tag.h similarity index 100% rename from stglibs/smux.lib/ber_tlv_tag.h rename to stglibs/smux.lib/include/stg/ber_tlv_tag.h diff --git a/stglibs/smux.lib/constr_CHOICE.h b/stglibs/smux.lib/include/stg/constr_CHOICE.h similarity index 100% rename from stglibs/smux.lib/constr_CHOICE.h rename to stglibs/smux.lib/include/stg/constr_CHOICE.h diff --git a/stglibs/smux.lib/constr_SEQUENCE.h b/stglibs/smux.lib/include/stg/constr_SEQUENCE.h similarity index 100% rename from stglibs/smux.lib/constr_SEQUENCE.h rename to stglibs/smux.lib/include/stg/constr_SEQUENCE.h diff --git a/stglibs/smux.lib/constr_SEQUENCE_OF.h b/stglibs/smux.lib/include/stg/constr_SEQUENCE_OF.h similarity index 100% rename from stglibs/smux.lib/constr_SEQUENCE_OF.h rename to stglibs/smux.lib/include/stg/constr_SEQUENCE_OF.h diff --git a/stglibs/smux.lib/constr_SET_OF.h b/stglibs/smux.lib/include/stg/constr_SET_OF.h similarity index 100% rename from stglibs/smux.lib/constr_SET_OF.h rename to stglibs/smux.lib/include/stg/constr_SET_OF.h diff --git a/stglibs/smux.lib/constr_TYPE.h b/stglibs/smux.lib/include/stg/constr_TYPE.h similarity index 100% rename from stglibs/smux.lib/constr_TYPE.h rename to stglibs/smux.lib/include/stg/constr_TYPE.h diff --git a/stglibs/smux.lib/constraints.h b/stglibs/smux.lib/include/stg/constraints.h similarity index 100% rename from stglibs/smux.lib/constraints.h rename to stglibs/smux.lib/include/stg/constraints.h diff --git a/stglibs/smux.lib/der_encoder.h b/stglibs/smux.lib/include/stg/der_encoder.h similarity index 100% rename from stglibs/smux.lib/der_encoder.h rename to stglibs/smux.lib/include/stg/der_encoder.h diff --git a/stglibs/smux.lib/per_decoder.h b/stglibs/smux.lib/include/stg/per_decoder.h similarity index 100% rename from stglibs/smux.lib/per_decoder.h rename to stglibs/smux.lib/include/stg/per_decoder.h diff --git a/stglibs/smux.lib/per_encoder.h b/stglibs/smux.lib/include/stg/per_encoder.h similarity index 100% rename from stglibs/smux.lib/per_encoder.h rename to stglibs/smux.lib/include/stg/per_encoder.h diff --git a/stglibs/smux.lib/per_support.h b/stglibs/smux.lib/include/stg/per_support.h similarity index 100% rename from stglibs/smux.lib/per_support.h rename to stglibs/smux.lib/include/stg/per_support.h diff --git a/stglibs/smux.lib/xer_decoder.h b/stglibs/smux.lib/include/stg/xer_decoder.h similarity index 100% rename from stglibs/smux.lib/xer_decoder.h rename to stglibs/smux.lib/include/stg/xer_decoder.h diff --git a/stglibs/smux.lib/xer_encoder.h b/stglibs/smux.lib/include/stg/xer_encoder.h similarity index 100% rename from stglibs/smux.lib/xer_encoder.h rename to stglibs/smux.lib/include/stg/xer_encoder.h diff --git a/stglibs/smux.lib/xer_support.h b/stglibs/smux.lib/include/stg/xer_support.h similarity index 100% rename from stglibs/smux.lib/xer_support.h rename to stglibs/smux.lib/include/stg/xer_support.h diff --git a/stglibs/srvconf.lib/netunit.h b/stglibs/srvconf.lib/include/stg/netunit.h similarity index 100% rename from stglibs/srvconf.lib/netunit.h rename to stglibs/srvconf.lib/include/stg/netunit.h diff --git a/stglibs/srvconf.lib/servconf.h b/stglibs/srvconf.lib/include/stg/servconf.h similarity index 100% rename from stglibs/srvconf.lib/servconf.h rename to stglibs/srvconf.lib/include/stg/servconf.h diff --git a/tests/tut.h b/tests/tut.h deleted file mode 100644 index 22859cdd..00000000 --- a/tests/tut.h +++ /dev/null @@ -1,2 +0,0 @@ - -#include diff --git a/tests/tut/tut.hpp b/tests/tut/tut.hpp index 93233a6a..52f9643c 100644 --- a/tests/tut/tut.hpp +++ b/tests/tut/tut.hpp @@ -1,5 +1,6 @@ #ifndef TUT_H_GUARD #define TUT_H_GUARD +#include #include #include @@ -9,11 +10,6 @@ #include #include #include -#include - -#if defined(linux) -#define TUT_USE_POSIX -#endif #include "tut_exception.hpp" #include "tut_result.hpp" @@ -35,6 +31,9 @@ namespace tut { +template +class test_group; + /** * Test object. Contains data test run upon and default test method * implementation. Inherited from Data to allow tests to @@ -43,12 +42,29 @@ namespace tut template class test_object : public Data, public test_object_posix { + template + friend class test_group; + + void set_test_group(const char *group) + { + current_test_group_ = group; + } + + void set_test_id(int current_test_id) + { + current_test_id_ = current_test_id; + } + public: /** * Default constructor */ test_object() + : called_method_was_a_dummy_test_(false), + current_test_id_(0), + current_test_name_(), + current_test_group_() { } @@ -62,9 +78,9 @@ public: return current_test_name_; } - void set_test_id(int current_test_id) + const std::string& get_test_group() const { - current_test_id_ = current_test_id; + return current_test_group_; } int get_test_id() const @@ -86,13 +102,17 @@ public: * Used to detect usused test numbers and avoid unnecessary * test object creation which may be time-consuming depending * on operations described in Data::Data() and Data::~Data(). - * TODO: replace with throwing special exception from default test. */ bool called_method_was_a_dummy_test_; + virtual ~test_object() + { + } + private: int current_test_id_; std::string current_test_name_; + std::string current_test_group_; }; @@ -126,6 +146,9 @@ struct tests_registerer template class test_group : public group_base, public test_group_posix { + test_group(const test_group&); + void operator=(const test_group&); + const char* name_; typedef void (test_object::*testmethod)(); @@ -139,13 +162,15 @@ class test_group : public group_base, public test_group_posix tests tests_; tests_iterator current_test_; - enum seh_result - { - SEH_OK, - SEH_CTOR, - SEH_TEST, - SEH_DUMMY - }; + enum seh_result + { + SEH_OK, +#if defined(TUT_USE_SEH) + SEH_CTOR, + SEH_TEST, +#endif + SEH_DUMMY + }; /** * Exception-in-destructor-safe smart-pointer class. @@ -201,11 +226,16 @@ class test_group : public group_base, public test_group_posix { try { +#if defined(TUT_USE_SEH) if (delete_obj() == false) { throw warning("destructor of test object raised" " an SEH exception"); } +#else + bool d = delete_obj(); + assert(d && "delete failed with SEH disabled: runtime bug?"); +#endif } catch (const std::exception& ex) { @@ -268,7 +298,9 @@ public: * Creates and registers test group with specified name. */ test_group(const char* name) - : name_(name) + : name_(name), + tests_(), + current_test_() { // register itself runner.get().register_group(name_,this); @@ -281,7 +313,9 @@ public: * This constructor is used in self-test run only. */ test_group(const char* name, test_runner& another_runner) - : name_(name) + : name_(name), + tests_(), + current_test_() { // register itself another_runner.register_group(name_, this); @@ -367,22 +401,23 @@ public: try { switch (run_test_seh_(ti->second, obj, current_test_name, ti->first)) - { - case SEH_CTOR: - throw bad_ctor("seh"); - break; - - case SEH_TEST: - throw seh("seh"); - break; - - case SEH_DUMMY: - tr.result = test_result::dummy; - break; - - case SEH_OK: - // ok - break; + { +#if defined(TUT_USE_SEH) + case SEH_CTOR: + throw bad_ctor("seh"); + break; + + case SEH_TEST: + throw seh("seh"); + break; +#endif + case SEH_DUMMY: + tr.result = test_result::dummy; + break; + + case SEH_OK: + // ok + break; } } catch (const rethrown& ex) @@ -393,13 +428,13 @@ public: catch (const tut_error& ex) { tr.result = ex.result(); - tr.exception_typeid = typeid(ex).name(); + tr.exception_typeid = ex.type(); tr.message = ex.what(); } catch (const std::exception& ex) { tr.result = test_result::ex; - tr.exception_typeid = typeid(ex).name(); + tr.exception_typeid = type_name(ex); tr.message = ex.what(); } catch (...) @@ -433,12 +468,12 @@ public: __try { #endif - if (obj.get() == 0) - { - reset_holder_(obj); - } + if (obj.get() == 0) + { + reset_holder_(obj); + } - obj->called_method_was_a_dummy_test_ = false; + obj->called_method_was_a_dummy_test_ = false; #if defined(TUT_USE_SEH) @@ -446,6 +481,7 @@ public: { #endif obj.get()->set_test_id(current_test_id); + obj.get()->set_test_group(name_); (obj.get()->*tm)(); #if defined(TUT_USE_SEH) } @@ -456,20 +492,20 @@ public: } #endif - if (obj->called_method_was_a_dummy_test_) - { - // do not call obj.release(); reuse object - return SEH_DUMMY; - } + if (obj->called_method_was_a_dummy_test_) + { + // do not call obj.release(); reuse object + return SEH_DUMMY; + } - current_test_name = obj->get_test_name(); - obj.permit_throw(); - obj.release(); + current_test_name = obj->get_test_name(); + obj.permit_throw(); + obj.release(); #if defined(TUT_USE_SEH) } __except(handle_seh_(::GetExceptionCode())) { - return SEH_CTOR; + return SEH_CTOR; } #endif return SEH_OK; @@ -531,5 +567,5 @@ inline int handle_seh_(DWORD excode) #endif } -#endif +#endif // TUT_H_GUARD diff --git a/tests/tut/tut_assert.hpp b/tests/tut/tut_assert.hpp index 7ee44bc2..c1959012 100644 --- a/tests/tut/tut_assert.hpp +++ b/tests/tut/tut_assert.hpp @@ -1,29 +1,34 @@ #ifndef TUT_ASSERT_H_GUARD #define TUT_ASSERT_H_GUARD +#include -#include "tut_exception.hpp" #include #include +#include +#include +#include #if defined(TUT_USE_POSIX) #include #include #endif +#include "tut_exception.hpp" + namespace tut { namespace detail { template - std::ostream &msg_prefix(std::ostream &str, const M &msg) + std::ostringstream &msg_prefix(std::ostringstream &str, const M &msg) { - std::stringstream ss; + std::ostringstream ss; ss << msg; if(!ss.str().empty()) { - str << ss.rdbuf() << ": "; + str << msg << ": "; } return str; @@ -83,7 +88,7 @@ void ensure_not(const M& msg, bool cond) * Tests two objects for being equal. * Throws if false. * - * NB: both T and Q must have operator << defined somewhere, or + * NB: both LHS and RHS must have operator << defined somewhere, or * client code will not compile at all! */ template @@ -91,42 +96,128 @@ void ensure_equals(const M& msg, const LHS& actual, const RHS& expected) { if (expected != actual) { - std::stringstream ss; + std::ostringstream ss; detail::msg_prefix(ss,msg) - << "expected '" + << "expected `" << expected - << "' actual '" + << "` actual `" << actual - << '\''; + << "`"; throw failure(ss.str()); } } -template -void ensure_equals(const LHS& actual, const RHS& expected) +/** + * Tests two pointers for being equal. + * Throws if false. + * + * NB: both T and Q must have operator << defined somewhere, or + * client code will not compile at all! + */ +template +void ensure_equals(const M& msg, const LHS * const actual, const RHS * const expected) { - ensure_equals("Values are not equal", actual, expected); + if (expected != actual) + { + std::ostringstream ss; + detail::msg_prefix(ss,msg) + << "expected `" + << (void*)expected + << "` actual `" + << (void*)actual + << "`"; + throw failure(ss.str()); + } } template -void ensure_equals(const M& msg, const double& actual, const double& expected, - const double& epsilon = std::numeric_limits::epsilon()) +void ensure_equals(const M& msg, const double& actual, const double& expected, const double& epsilon) { const double diff = actual - expected; - if ( !((diff <= epsilon) && (diff >= -epsilon )) ) + if ( (actual != expected) && !((diff <= epsilon) && (diff >= -epsilon )) ) { - std::stringstream ss; + std::ostringstream ss; detail::msg_prefix(ss,msg) << std::scientific << std::showpoint << std::setprecision(16) - << "expected " << expected - << " actual " << actual - << " with precision " << epsilon; + << "expected `" << expected + << "` actual `" << actual + << "` with precision `" << epsilon << "`"; throw failure(ss.str()); } } + +template +void ensure_equals(const M& msg, const double& actual, const double& expected) +{ + ensure_equals(msg, actual, expected, std::numeric_limits::epsilon()); +} + +template +void ensure_equals(const LHS& actual, const RHS& expected) +{ + ensure_equals("Values are not equal", actual, expected); +} + + +template +void ensure_equals(const std::string &msg, + const LhsIterator &lhs_begin, const LhsIterator &lhs_end, + const RhsIterator &rhs_begin, const RhsIterator &rhs_end) +{ + typename std::iterator_traits::difference_type lhs_size = std::distance(lhs_begin, lhs_end); + typename std::iterator_traits::difference_type rhs_size = std::distance(rhs_begin, rhs_end); + + if(lhs_size < rhs_size) + { + ensure_equals(msg + ": range is too short", lhs_size, rhs_size); + } + + if(lhs_size > rhs_size) + { + ensure_equals(msg + ": range is too long", lhs_size, rhs_size); + } + + assert(lhs_size == rhs_size); + + LhsIterator lhs_i = lhs_begin; + RhsIterator rhs_i = rhs_begin; + while( (lhs_i != lhs_end) && (rhs_i != rhs_end) ) + { + if(*lhs_i != *rhs_i) + { + std::ostringstream ss; + detail::msg_prefix(ss,msg) + << "expected `" << *rhs_i + << "` actual `" << *lhs_i + << "` at offset " << std::distance(lhs_begin, lhs_i); + throw failure(ss.str()); + } + + lhs_i++; + rhs_i++; + } + + assert(lhs_i == lhs_end); + assert(rhs_i == rhs_end); +} + +template +void ensure_equals(const LhsIterator &lhs_begin, const LhsIterator &lhs_end, + const RhsIterator &rhs_begin, const RhsIterator &rhs_end) +{ + ensure_equals("Ranges are not equal", lhs_begin, lhs_end, rhs_begin, rhs_end); +} + +template +void ensure_equals(const LhsType *lhs_begin, const LhsType *lhs_end, + const RhsType *rhs_begin, const RhsType *rhs_end) +{ + ensure_equals("Ranges are not equal", lhs_begin, lhs_end, rhs_begin, rhs_end); +} + /** * Tests two objects for being at most in given distance one from another. * Borders are excluded. @@ -143,15 +234,15 @@ void ensure_distance(const M& msg, const T& actual, const T& expected, const T& { if (expected-distance >= actual || expected+distance <= actual) { - std::stringstream ss; + std::ostringstream ss; detail::msg_prefix(ss,msg) - << " expected (" + << " expected `" << expected-distance - << " - " + << "` - `" << expected+distance - << ") actual '" + << "` actual `" << actual - << '\''; + << "`"; throw failure(ss.str()); } } @@ -169,7 +260,7 @@ void ensure_errno(const M& msg, bool cond) { #if defined(TUT_USE_POSIX) char e[512]; - std::stringstream ss; + std::ostringstream ss; detail::msg_prefix(ss,msg) << strerror_r(errno, e, sizeof(e)); throw failure(ss.str()); @@ -193,6 +284,20 @@ void fail(const M& msg) throw failure(msg); } +/** + * Mark test case as known failure and skip execution. + */ +void skip(const char* msg = "") +{ + throw skipped(msg); +} + +template +void skip(const M& msg) +{ + throw skipped(msg); +} + } // end of namespace } diff --git a/tests/tut/tut_config.hpp b/tests/tut/tut_config.hpp new file mode 100644 index 00000000..d698c1c8 --- /dev/null +++ b/tests/tut/tut_config.hpp @@ -0,0 +1,6 @@ +#ifndef TUT_CONFIG_H_GUARD +#define TUT_CONFIG_H_GUARD + +#define TUT_USE_RTTI 1 + +#endif diff --git a/tests/tut/tut_console_reporter.hpp b/tests/tut/tut_console_reporter.hpp index 3c3739be..33856db8 100644 --- a/tests/tut/tut_console_reporter.hpp +++ b/tests/tut/tut_console_reporter.hpp @@ -1,6 +1,5 @@ #ifndef TUT_CONSOLE_REPORTER #define TUT_CONSOLE_REPORTER - #include #include @@ -17,29 +16,32 @@ std::ostream& operator<<(std::ostream& os, const tut::test_result& tr) { switch(tr.result) { - case tut::test_result::ok: - os << '.'; - break; - case tut::test_result::fail: - os << '[' << tr.test << "=F]"; - break; - case tut::test_result::ex_ctor: - os << '[' << tr.test << "=C]"; - break; - case tut::test_result::ex: - os << '[' << tr.test << "=X]"; - break; - case tut::test_result::warn: - os << '[' << tr.test << "=W]"; - break; - case tut::test_result::term: - os << '[' << tr.test << "=T]"; - break; - case tut::test_result::rethrown: - os << '[' << tr.test << "=P]"; - break; - case tut::test_result::dummy: - assert(!"Should never be called"); + case tut::test_result::ok: + os << '.'; + break; + case tut::test_result::fail: + os << '[' << tr.test << "=F]"; + break; + case tut::test_result::ex_ctor: + os << '[' << tr.test << "=C]"; + break; + case tut::test_result::ex: + os << '[' << tr.test << "=X]"; + break; + case tut::test_result::warn: + os << '[' << tr.test << "=W]"; + break; + case tut::test_result::term: + os << '[' << tr.test << "=T]"; + break; + case tut::test_result::rethrown: + os << '[' << tr.test << "=P]"; + break; + case tut::test_result::skipped: + os << '[' << tr.test << "=S]"; + break; + case tut::test_result::dummy: + throw tut::tut_error("console reporter called for dummy test result"); } return os; @@ -60,6 +62,8 @@ class console_reporter : public tut::callback not_passed_list not_passed; std::ostream& os; + console_reporter(const console_reporter &); + console_reporter &operator=(const console_reporter &); public: int ok_count; @@ -67,15 +71,33 @@ public: int failures_count; int terminations_count; int warnings_count; + int skipped_count; console_reporter() - : os(std::cout) + : current_group(), + not_passed(), + os(std::cout), + ok_count(0), + exceptions_count(0), + failures_count(0), + terminations_count(0), + warnings_count(0), + skipped_count(0) { init(); } console_reporter(std::ostream& out) - : os(out) + : current_group(), + not_passed(), + os(out), + ok_count(0), + exceptions_count(0), + failures_count(0), + terminations_count(0), + warnings_count(0), + skipped_count(0) + { init(); } @@ -114,11 +136,15 @@ public: case test_result::term: terminations_count++; break; + case test_result::skipped: + skipped_count++; + break; case tut::test_result::dummy: - assert(!"Should never be called"); + assert( (tr.result != tut::test_result::dummy) && "Should never be called"); } // switch - if (tr.result != tut::test_result::ok) + if ( (tr.result != tut::test_result::ok) && + (tr.result != tut::test_result::skipped) ) { not_passed.push_back(tr); } @@ -181,12 +207,12 @@ public: { if (tr.result == test_result::fail) { - os << " failed assertion: \"" << tr.message << "\"" + os << " failed assertion: `" << tr.message << "`" << std::endl; } else { - os << " message: \"" << tr.message << "\"" + os << " message: `" << tr.message << "`" << std::endl; } } @@ -214,11 +240,17 @@ public: { os << " warnings:" << warnings_count; } + os << " ok:" << ok_count; + + if(skipped_count > 0) + { + os << " skipped:" << skipped_count; + } os << std::endl; } - bool all_ok() const + virtual bool all_ok() const { return not_passed.empty(); } @@ -232,6 +264,7 @@ private: failures_count = 0; terminations_count = 0; warnings_count = 0; + skipped_count = 0; not_passed.clear(); } }; diff --git a/tests/tut/tut_cppunit_reporter.hpp b/tests/tut/tut_cppunit_reporter.hpp index 3b9f38c1..62646b09 100644 --- a/tests/tut/tut_cppunit_reporter.hpp +++ b/tests/tut/tut_cppunit_reporter.hpp @@ -7,6 +7,7 @@ #include #include #include +#include namespace tut { @@ -16,72 +17,60 @@ namespace tut */ class cppunit_reporter : public tut::callback { - private: - std::vector failed_tests; - std::vector passed_tests; - std::string filename; + std::vector failed_tests_; + std::vector passed_tests_; + const std::string filename_; + std::auto_ptr stream_; - std::string encode(const std::string & text) - { - std::string out; - for (unsigned int i=0; i': - out += ">"; - break; - case '&': - out += "&"; - break; - case '\'': - out += "'"; - break; - case '"': - out += """; - break; - default: - out += c; - } - } - - return out; - } + cppunit_reporter(const cppunit_reporter &); + cppunit_reporter &operator=(const cppunit_reporter &); public: + explicit cppunit_reporter(const std::string &filename = "testResult.xml") + : failed_tests_(), + passed_tests_(), + filename_(filename), + stream_(new std::ofstream(filename_.c_str())) + { + if (!stream_->good()) { + throw tut_error("Cannot open output file `" + filename_ + "`"); + } + } - cppunit_reporter(const std::string & _filename = "") + explicit cppunit_reporter(std::ostream &stream) + : failed_tests_(), + passed_tests_(), + filename_(), + stream_(&stream) { - setFilename(_filename); } - void setFilename(const std::string & _filename) + ~cppunit_reporter() { - if (_filename == "") - { - filename = "testResult.xml"; - } - else + if(filename_.empty()) { - filename = _filename; + stream_.release(); } } void run_started() { - failed_tests.clear(); - passed_tests.clear(); + failed_tests_.clear(); + passed_tests_.clear(); } void test_completed(const tut::test_result& tr) { - if (tr.result == test_result::ok) { - passed_tests.push_back(tr); - } else { - failed_tests.push_back(tr); + assert(tr.result != test_result::dummy ); + if ( (tr.result == test_result::ok) || + (tr.result == test_result::skipped) ) + { + passed_tests_.push_back(tr); + } + else + { + failed_tests_.push_back(tr); } } @@ -91,23 +80,18 @@ public: int failures = 0; std::string failure_type; std::string failure_msg; - std::ofstream xmlfile; - - xmlfile.open(filename.c_str(), std::ios::in | std::ios::trunc); - if (!xmlfile.is_open()) { - throw (std::runtime_error("Cannot open file for output")); - } - /* *********************** header ***************************** */ - xmlfile << "" << std::endl - << "" << std::endl; + *stream_ << "" << std::endl + << "" << std::endl; - /* *********************** failed tests ***************************** */ - if (failed_tests.size() > 0) { - xmlfile << " " << std::endl; + if (failed_tests_.size() > 0) + { + *stream_ << " " << std::endl; - for (unsigned int i=0; i" << std::endl - << " " << encode(failed_tests[i].group) + "::" + encode(failed_tests[i].name) << "" << std::endl - << " " << failure_type << "" << std::endl - << " " << std::endl - << " Unknown" << std::endl - << " Unknown" << std::endl - << " " << std::endl - << " " << encode(failure_msg + failed_tests[i].message) << "" << std::endl - << " " << std::endl; + *stream_ << " " << std::endl + << " " << encode(failed_tests_[i].group) + "::" + encode(failed_tests_[i].name) << "" << std::endl + << " " << failure_type << "" << std::endl + << " " << std::endl + << " Unknown" << std::endl + << " Unknown" << std::endl + << " " << std::endl + << " " << encode(failure_msg + failed_tests_[i].message) << "" << std::endl + << " " << std::endl; } - xmlfile << " " << std::endl; + *stream_ << " " << std::endl; } /* *********************** passed tests ***************************** */ - if (passed_tests.size() > 0) { - xmlfile << " " << std::endl; - - for (unsigned int i=0; i" << std::endl - << " " << encode(passed_tests[i].group) + "::" + encode(passed_tests[i].name) << "" << std::endl - << " " << std::endl; + if (passed_tests_.size() > 0) { + *stream_ << " " << std::endl; + + for (unsigned int i=0; i" << std::endl + << " " << encode(passed_tests_[i].group) + "::" + encode(passed_tests_[i].name) << "" << std::endl + << " " << std::endl; } - xmlfile << " " << std::endl; + *stream_ << " " << std::endl; } /* *********************** statistics ***************************** */ - xmlfile << " " << std::endl - << " " << (failed_tests.size() + passed_tests.size()) << "" << std::endl - << " " << failed_tests.size() << "" << std::endl - << " " << errors << "" << std::endl - << " " << failures << "" << std::endl - << " " << std::endl; + *stream_ << " " << std::endl + << " " << (failed_tests_.size() + passed_tests_.size()) << "" << std::endl + << " " << failed_tests_.size() << "" << std::endl + << " " << errors << "" << std::endl + << " " << failures << "" << std::endl + << " " << std::endl; /* *********************** footer ***************************** */ - xmlfile << "" << std::endl; - - xmlfile.close(); + *stream_ << "" << std::endl; } - bool all_ok() const + virtual bool all_ok() const { - return failed_tests.empty(); - }; + return failed_tests_.empty(); + } + /** + * \brief Encodes text to XML + * XML-reserved characters (e.g. "<") are encoded according to specification + * @param text text to be encoded + * @return encoded string + */ + static std::string encode(const std::string & text) + { + std::string out; + + for (unsigned int i=0; i': + out += ">"; + break; + case '&': + out += "&"; + break; + case '\'': + out += "'"; + break; + case '"': + out += """; + break; + default: + out += c; + } + } + return out; + } }; } diff --git a/tests/tut/tut_exception.hpp b/tests/tut/tut_exception.hpp index c5c88cb2..1ebea341 100644 --- a/tests/tut/tut_exception.hpp +++ b/tests/tut/tut_exception.hpp @@ -12,7 +12,7 @@ namespace tut */ struct tut_error : public std::exception { - tut_error(const std::string& msg) + explicit tut_error(const std::string& msg) : err_msg(msg) { } @@ -22,6 +22,11 @@ struct tut_error : public std::exception return test_result::ex; } + virtual std::string type() const + { + return "tut::tut_error"; + } + const char* what() const throw() { return err_msg.c_str(); @@ -32,8 +37,9 @@ struct tut_error : public std::exception } private: + void operator=(const tut_error &); - std::string err_msg; + const std::string err_msg; }; /** @@ -41,23 +47,48 @@ private: */ struct no_such_group : public tut_error { - no_such_group(const std::string& grp) + explicit no_such_group(const std::string& grp) : tut_error(grp) { } + virtual std::string type() const + { + return "tut::no_such_group"; + } + ~no_such_group() throw() { } }; +/** + * Test not found exception. + */ +struct no_such_test : public tut_error +{ + explicit no_such_test(const std::string& grp) + : tut_error(grp) + { + } + + virtual std::string type() const + { + return "tut::no_such_test"; + } + + ~no_such_test() throw() + { + } +}; + /** * Internal exception to be throwed when * test constructor has failed. */ struct bad_ctor : public tut_error { - bad_ctor(const std::string& msg) + explicit bad_ctor(const std::string& msg) : tut_error(msg) { } @@ -67,6 +98,11 @@ struct bad_ctor : public tut_error return test_result::ex_ctor; } + virtual std::string type() const + { + return "tut::bad_ctor"; + } + ~bad_ctor() throw() { } @@ -77,7 +113,7 @@ struct bad_ctor : public tut_error */ struct failure : public tut_error { - failure(const std::string& msg) + explicit failure(const std::string& msg) : tut_error(msg) { } @@ -87,6 +123,11 @@ struct failure : public tut_error return test_result::fail; } + virtual std::string type() const + { + return "tut::failure"; + } + ~failure() throw() { } @@ -97,7 +138,7 @@ struct failure : public tut_error */ struct warning : public tut_error { - warning(const std::string& msg) + explicit warning(const std::string& msg) : tut_error(msg) { } @@ -107,6 +148,11 @@ struct warning : public tut_error return test_result::warn; } + virtual std::string type() const + { + return "tut::warning"; + } + ~warning() throw() { } @@ -117,7 +163,7 @@ struct warning : public tut_error */ struct seh : public tut_error { - seh(const std::string& msg) + explicit seh(const std::string& msg) : tut_error(msg) { } @@ -127,6 +173,11 @@ struct seh : public tut_error return test_result::term; } + virtual std::string type() const + { + return "tut::seh"; + } + ~seh() throw() { } @@ -147,6 +198,11 @@ struct rethrown : public failure return test_result::rethrown; } + virtual std::string type() const + { + return "tut::rethrown"; + } + ~rethrown() throw() { } @@ -154,6 +210,28 @@ struct rethrown : public failure const test_result tr; }; +struct skipped : public tut_error +{ + explicit skipped(const std::string& msg) + : tut_error(msg) + { + } + + virtual test_result::result_type result() const + { + return test_result::skipped; + } + + virtual std::string type() const + { + return "tut::skipped"; + } + + ~skipped() throw() + { + } +}; + } #endif diff --git a/tests/tut/tut_fpt.hpp b/tests/tut/tut_fpt.hpp new file mode 100644 index 00000000..e8df8b06 --- /dev/null +++ b/tests/tut/tut_fpt.hpp @@ -0,0 +1,185 @@ +/** + * @brief Additional ensures for scientific/engineering applications. + * @author Joerg + * @date 07/04/2008 + */ +#ifndef TUT_Float_H_GUARD +#define TUT_Float_H_GUARD + +#include +#include + +namespace tut +{ + namespace detail + { + template + struct If + { + typedef Else type; + }; + + template + struct If + { + typedef Then type; + }; + + template + struct fpt_traits + { + struct StdNumericLimitsNotAvailable {}; + static const StdNumericLimitsNotAvailable static_check[ std::numeric_limits::is_specialized ]; + + static const T zero; + + typedef typename If::is_integer, + double, + T>::type Result; + + static T abs(const T &arg) + { + if(arg < zero) + return zero - arg; + else + return arg; + } + + static T sig(const T &arg) + { + if(arg < zero) + return -1; + else + return 1; + } + + static inline Result div(const Result &number, const T &divisor) + { + static_cast(static_check); + + if(number == zero && divisor == zero) + return std::numeric_limits::quiet_NaN(); + + if(number == zero) + return zero; + + if(divisor == zero) + return sig(number) * std::numeric_limits::infinity(); + + assert(zero < number); + assert(zero < divisor); + + // Avoid underflow + if(static_cast(1) < abs(divisor)) + { + // number / divisor < min <=> number < min * divisor + if( abs(number) < abs(divisor) * std::numeric_limits::min()) + { + return sig(divisor) * sig(number) * std::numeric_limits::min(); + } + } + + // Avoid overflow + if( abs(divisor) < static_cast(1)) + { + // number / divisor > max <=> number > max * divisor + if( abs(divisor) * std::numeric_limits::max() < abs(number)) + { + return sig(divisor) * sig(number) * std::numeric_limits::max(); + } + } + + return number / divisor; + } + }; + + template + const typename fpt_traits::StdNumericLimitsNotAvailable + fpt_traits::static_check[ std::numeric_limits::is_specialized ] = { {} }; + + template + const T fpt_traits::zero = static_cast(0); + + template + bool check_tolerance(T actual, T expected, U fraction) + { + typename fpt_traits::Result diff = fpt_traits::div( fpt_traits::abs( expected - actual ), + fpt_traits::abs( expected ) ); + + return (diff == fraction) || (diff < fraction); + } + + } // namespace detail + + template + void ensure_close(const char* msg, const T& actual, const T& expected, const U& tolerance ) + { + typedef detail::fpt_traits Traits; + + typename Traits::Result fraction = Traits::div( Traits::abs(static_cast(tolerance)), + static_cast(100) ); + if( !detail::check_tolerance(actual, expected, fraction) ) + { + std::ostringstream ss; + ss << ( msg ? msg : "" ) + << ( msg ? ": " : "" ) + << "expected `" + << expected + << "` and actual `" + << actual + << "` differ more than " + << tolerance + << "%"; + throw failure( ss.str().c_str() ); + } + } + + template + void ensure_close(const T& actual, const T& expected, const Tolerance& tolerance ) + { + ensure_close( 0, actual, expected, tolerance ); + } + + template + void ensure_close_fraction(const char* msg, const T& actual, const T& expected, const U& fraction) + { + typedef char StdNumericLimitsNotAvailable; + const StdNumericLimitsNotAvailable static_check[ std::numeric_limits::is_specialized ] = { 0 }; + static_cast(static_check); + + typedef typename detail::If::is_integer, + double, + U>::type Tolerance; + + if( !detail::check_tolerance(actual, expected, fraction) ) + { + std::ostringstream ss; + ss << ( msg ? msg : "" ) + << ( msg ? ": " : "" ) + << "expected `" + << expected + << "` and actual `" + << actual + << "` differ more than fraction `" + << fraction + << "`"; + throw failure( ss.str().c_str() ); + } + } + + template + void ensure_close_fraction( const char* msg, const T& actual, const T& expected, const int& tolerance ) + { + ensure_close(msg, actual, expected, double(tolerance)); + } + + template< typename T, typename Tolerance> + void ensure_close_fraction(const T& actual, const T& expected, const Tolerance& fraction) + { + ensure_close_fraction( 0, actual, expected, fraction ); + } + +} // namespace tut + +#endif + diff --git a/tests/tut/tut_macros.hpp b/tests/tut/tut_macros.hpp new file mode 100644 index 00000000..ed517d66 --- /dev/null +++ b/tests/tut/tut_macros.hpp @@ -0,0 +1,63 @@ +#ifndef TUT_MACROS_HPP +#define TUT_MACROS_HPP + +#include + +#ifdef ensure_THROW +#error ensure_THROW macro is already defined +#endif + +/** Helper macros to ensure that a call throws exception. + * \code + * #include + * ensure_THROW( this_function_should_throw_bad_alloc(), std::bad_alloc ); + * \endcode + */ +#define ensure_THROW( x, e ) \ +try \ +{ \ + x; \ + fail(#x " has not thrown expected exception " #e); \ +} \ +catch(const e &) \ +{ \ +} \ +catch(const std::exception &ex) \ +{ \ + fail( std::string(#x " has thrown unexpected exception ")+tut::type_name(ex)+": "+ex.what()); \ +} \ +catch(...) \ +{ \ + fail(#x " has thrown unexpected unknown exception"); \ +} + +#ifdef ensure_NO_THROW +#error ensure_NO_THROW macro is already defined +#endif + +/** Helper macro to ensure a call does not throw any exceptions. + * \code + * #include + * ensure_NO_THROW( this_function_should_never_throw() ); + * \endcode + */ +#define ensure_NO_THROW( x ) \ +try \ +{ \ + x; \ +} \ +catch(const std::exception &ex) \ +{ \ + fail( std::string(#x " has thrown unexpected exception ")+tut::type_name(ex)+": "+ex.what()); \ +} \ +catch(...) \ +{ \ + fail(#x " has thrown unexpected unknown exception"); \ +} + +#ifdef __COUNTER__ +#define TUT_TESTCASE(object) template<> template<> void object::test<__COUNTER__>() +#endif + +#endif + diff --git a/tests/tut/tut_main.hpp b/tests/tut/tut_main.hpp new file mode 100644 index 00000000..c6955bb2 --- /dev/null +++ b/tests/tut/tut_main.hpp @@ -0,0 +1,111 @@ +#ifndef TUT_MAIN_H +#define TUT_MAIN_H + +#include +#include +#include +#include +#include + +namespace tut +{ + +/** Helper function to make test binaries simpler. + * + * Example of basic usage follows. + * + * @code + * namespace tut { test_runner_singleton runner; } + * + * int main(int argc, char **argv) + * { + * if( tut_main(argc, argv) ) + * return 0; + * else + * return -1; + * } + * @endcode + * + * It is also possible to do some generic initialization before + * running any tests and cleanup before exiting application. + * Note that tut_main can throw tut::no_such_group or tut::no_such_test. + * + * @code + * namespace tut { test_runner_singleton runner; } + * + * int main(int argc, char **argv) + * { + * tut::xml_reporter reporter; + * tut::runner.get().insert_callback(&reporter); + * + * MyInit(); + * try + * { + * tut_main(argc, argv); + * } + * catch(const tut::tut_error &ex) + * { + * std::cerr << "TUT error: " << ex.what() << std::endl; + * } + * MyCleanup(); + * } + * @endcode + */ +inline bool tut_main(int argc, const char * const * const argv, std::ostream &os = std::cerr) +{ + std::stringstream usage; + usage << "Usage: " << argv[0] << " [group] [testcase]" << std::endl; + groupnames gr = runner.get().list_groups(); + usage << "Available test groups:" << std::endl; + for(groupnames::const_iterator i = gr.begin(); i != gr.end(); ++i) + { + usage << " " << *i << std::endl; + } + + if(argc>1) + { + if(std::string(argv[1]) == "-h" || + std::string(argv[1]) == "--help" || + std::string(argv[1]) == "/?" || + argc > 3) + { + os << usage.rdbuf(); + return false; + } + } + + // Check command line options. + switch(argc) + { + case 1: + runner.get().run_tests(); + break; + + case 2: + runner.get().run_tests(argv[1]); + break; + + case 3: + { + char *end; + int t = strtol(argv[2], &end, 10); + if(end != argv[2] + strlen(argv[2])) + { + throw no_such_test("`" + std::string(argv[2]) + "` should be a number"); + } + + test_result tr; + if(!runner.get().run_test(argv[1], t, tr) || tr.result == test_result::dummy) + { + throw no_such_test("No testcase `" + std::string(argv[2]) + "` in group `" + argv[1] + "`"); + } + } + break; + } + + return true; +} // tut_main() + +} + +#endif diff --git a/tests/tut/tut_posix.hpp b/tests/tut/tut_posix.hpp index 89a71305..c7cfe57a 100644 --- a/tests/tut/tut_posix.hpp +++ b/tests/tut/tut_posix.hpp @@ -1,5 +1,6 @@ #ifndef TUT_FORK_H_GUARD #define TUT_FORK_H_GUARD +#include #if defined(TUT_USE_POSIX) #include @@ -44,7 +45,7 @@ private: if(tr.result != test_result::ok) { - std::stringstream ss; + std::ostringstream ss; ss << int(tr.result) << "\n" << tr.group << "\n" << tr.test << "\n" @@ -57,6 +58,10 @@ private: ensure_errno("write() failed", w == size); } } + + virtual ~test_group_posix() + { + } }; template @@ -126,7 +131,8 @@ public: * Default constructor */ test_object_posix() - : pipe_(-1) + : pids_(), + pipe_(-1) { } @@ -142,7 +148,7 @@ public: if(!pids_.empty()) { - std::stringstream ss; + std::ostringstream ss; // in parent, reap children for(std::map::iterator i = pids_.begin(); i != pids_.end(); ++i) @@ -226,7 +232,7 @@ private: else { // cannot kill, we are in trouble - std::stringstream ss; + std::ostringstream ss; char e[1024]; ss << "child " << pid << " could not be killed with SIGTERM, " << strerror_r(errno, e, sizeof(e)) << std::endl; fail(ss.str()); @@ -257,7 +263,7 @@ private: } else { - std::stringstream ss; + std::ostringstream ss; char e[1024]; ss << "child " << pid << " could not be killed with SIGKILL, " << strerror_r(errno, e, sizeof(e)) << std::endl; fail(ss.str()); @@ -269,7 +275,7 @@ private: ensure_equals("child process exists after SIGKILL", ::kill(pid, 0), -1); - std::stringstream ss; + std::ostringstream ss; ss << "child " << pid << " had to be killed with SIGKILL"; fail(ss.str()); } @@ -361,7 +367,7 @@ private: { if(WIFSIGNALED(status)) { - std::stringstream ss; + std::ostringstream ss; ss << "child killed by signal " << WTERMSIG(status) << ": expected exit with code " << exit_status; @@ -372,7 +378,7 @@ private: { if(WEXITSTATUS(status) != exit_status) { - std::stringstream ss; + std::ostringstream ss; ss << "child exited, expected '" << exit_status << "' actual '" @@ -385,7 +391,7 @@ private: if(WIFSTOPPED(status)) { - std::stringstream ss; + std::ostringstream ss; ss << "child stopped by signal " << WTERMSIG(status) << ": expected exit with code " << exit_status; throw failure(ss.str().c_str()); @@ -398,7 +404,7 @@ private: { if(WTERMSIG(status) != signal) { - std::stringstream ss; + std::ostringstream ss; ss << "child killed by signal, expected '" << signal << "' actual '" @@ -410,7 +416,7 @@ private: if(WIFEXITED(status)) { - std::stringstream ss; + std::ostringstream ss; ss << "child exited with code " << WEXITSTATUS(status) << ": expected signal " << signal; @@ -419,7 +425,7 @@ private: if(WIFSTOPPED(status)) { - std::stringstream ss; + std::ostringstream ss; ss << "child stopped by signal " << WTERMSIG(status) << ": expected kill by signal " << signal; @@ -453,6 +459,9 @@ namespace tut struct test_object_posix { + virtual ~test_object_posix() + { + } }; struct test_group_posix @@ -461,6 +470,10 @@ struct test_group_posix void send_result_(const T*, const test_result &) { } + + virtual ~test_group_posix() + { + } }; } // namespace tut diff --git a/tests/tut/tut_restartable.hpp b/tests/tut/tut_restartable.hpp index eb6eb00e..9f929234 100644 --- a/tests/tut/tut_restartable.hpp +++ b/tests/tut/tut_restartable.hpp @@ -186,7 +186,7 @@ bool deserialize(std::istream& is, tut::test_result& tr) } return true; } -}; +} /** * Restartable test runner wrapper. @@ -207,11 +207,12 @@ public: */ restartable_wrapper(const std::string& dir = ".") : runner_(runner.get()), - dir_(dir) + callbacks_(), + dir_(dir), + log_( dir + '/' + "log.tut" ), + jrn_( dir + '/' + "journal.tut" ) { // dozen: it works, but it would be better to use system path separator - jrn_ = dir_ + '/' + "journal.tut"; - log_ = dir_ + '/' + "log.tut"; } /** diff --git a/tests/tut/tut_result.hpp b/tests/tut/tut_result.hpp index 5731802c..e9607859 100644 --- a/tests/tut/tut_result.hpp +++ b/tests/tut/tut_result.hpp @@ -1,11 +1,42 @@ #ifndef TUT_RESULT_H_GUARD #define TUT_RESULT_H_GUARD +#include #include +#if defined(TUT_USE_RTTI) +#if (defined(_MSC_VER) && !defined(_CPPRTTI)) || (defined(__GNUC__) && !defined(__GXX_RTTI)) +#undef TUT_USE_RTTI +#endif +#endif + +#if defined(TUT_USE_RTTI) +#include +#endif + namespace tut { +#if defined(TUT_USE_RTTI) +template +inline std::string type_name(const T& t) +{ + return typeid(t).name(); +} +#else +template +inline std::string type_name(const T& t) +{ + return "Unknown type, RTTI disabled"; +} + +inline std::string type_name(const std::exception&) +{ + return "Unknown std::exception, RTTI disabled"; +} +#endif + + #if defined(TUT_USE_POSIX) struct test_result_posix { @@ -14,11 +45,18 @@ struct test_result_posix { } + virtual ~test_result_posix() + { + } + pid_t pid; }; #else struct test_result_posix { + virtual ~test_result_posix() + { + } }; #endif @@ -46,22 +84,19 @@ struct test_result : public test_result_posix std::string name; /** - * ok - test finished successfully - * fail - test failed with ensure() or fail() methods - * ex - test throwed an exceptions - * warn - test finished successfully, but test destructor throwed - * term - test forced test application to terminate abnormally + * result of a test */ enum result_type { - ok, - fail, - ex, - warn, - term, - ex_ctor, - rethrown, - dummy + ok, ///< test finished successfully + fail, ///< test failed with ensure() or fail() methods + ex, ///< test throwed an exceptions + warn, ///< test finished successfully, but test destructor throwed + term, ///< test forced test application to terminate abnormally + ex_ctor, ///< + rethrown, ///< + skipped, ///< + dummy ///< }; result_type result; @@ -76,8 +111,12 @@ struct test_result : public test_result_posix * Default constructor. */ test_result() - : test(0), - result(ok) + : group(), + test(0), + name(), + result(ok), + message(), + exception_typeid() { } @@ -89,7 +128,9 @@ struct test_result : public test_result_posix : group(grp), test(pos), name(test_name), - result(res) + result(res), + message(), + exception_typeid() { } @@ -104,7 +145,7 @@ struct test_result : public test_result_posix name(test_name), result(res), message(ex.what()), - exception_typeid(typeid(ex).name()) + exception_typeid(type_name(ex)) { } @@ -122,6 +163,10 @@ struct test_result : public test_result_posix exception_typeid(ex_typeid) { } + + virtual ~test_result() + { + } }; } diff --git a/tests/tut/tut_runner.hpp b/tests/tut/tut_runner.hpp index 33ffe423..3803046e 100644 --- a/tests/tut/tut_runner.hpp +++ b/tests/tut/tut_runner.hpp @@ -62,24 +62,27 @@ struct callback * Called when a group started * @param name Name of the group */ - virtual void group_started(const std::string& /*name*/) + virtual void group_started(const std::string& name) { + (void)name; } /** * Called when a test finished. * @param tr Test results. */ - virtual void test_completed(const test_result& /*tr*/) + virtual void test_completed(const test_result& tr) { + (void)tr; } /** * Called when a group is completed * @param name Name of the group */ - virtual void group_completed(const std::string& /*name*/) + virtual void group_completed(const std::string& name) { + (void)name; } /** @@ -88,6 +91,11 @@ struct callback virtual void run_completed() { } + + virtual bool all_ok() const + { + return true; + } private: callback(const callback &); void operator=(const callback&); @@ -111,11 +119,15 @@ public: * Constructor */ test_runner() + : groups_(), + callbacks_() { } /** * Stores another group for getting by name. + * @param name new group object + * @param gr new callback object */ void register_group(const std::string& name, group_base* gr) { @@ -127,15 +139,16 @@ public: if (groups_.find(name) != groups_.end()) { std::string msg("attempt to add already existent group " + name); - // this exception terminates application so we use cerr also - // TODO: should this message appear in stream? - std::cerr << msg << std::endl; throw tut_error(msg); } groups_.insert( std::make_pair(name, gr) ); } + /** + * Stores one callback object. + * @param cb new callback object + */ void set_callback(callback *cb) { clear_callbacks(); @@ -143,7 +156,8 @@ public: } /** - * Stores callback object. + * Add callback object. + * @param cb new callback object */ void insert_callback(callback* cb) { @@ -153,11 +167,18 @@ public: } } + /** + * Remove callback object. + * @param cb callback to remove + */ void erase_callback(callback* cb) { callbacks_.erase(cb); } + /** + * Remove all callback objects. + */ void clear_callbacks() { callbacks_.clear(); @@ -165,12 +186,17 @@ public: /** * Returns callback list. + * @return callback list */ const callbacks &get_callbacks() const { return callbacks_; } + /** + * Set callback list. + * @param cb new callback list + */ void set_callbacks(const callbacks &cb) { callbacks_ = cb; @@ -178,23 +204,20 @@ public: /** * Returns list of known test groups. + * @return groups list */ const groupnames list_groups() const { groupnames ret; - const_iterator i = groups_.begin(); - const_iterator e = groups_.end(); - while (i != e) + for(const_iterator i = groups_.begin(); i != groups_.end(); ++i) { ret.push_back(i->first); - ++i; } return ret; } /** * Runs all tests in all groups. - * @param callback Callback object if exists; null otherwise */ void run_tests() const { @@ -216,6 +239,7 @@ public: /** * Runs all tests in specified group. + * @param group_name group to test */ void run_tests(const std::string& group_name) const { @@ -236,6 +260,10 @@ public: /** * Runs one test in specified group. + * @param group_name group to test + * @param n run case in test + * @param tr result of this case + * @return true if test is ok, otherwise false */ bool run_test(const std::string& group_name, int n, test_result &tr) const { @@ -356,4 +384,5 @@ extern test_runner_singleton runner; } -#endif +#endif // TUT_RUNNER_H_GUARD + diff --git a/tests/tut/tut_xml_reporter.hpp b/tests/tut/tut_xml_reporter.hpp index 8a9d5dec..d0c301dd 100644 --- a/tests/tut/tut_xml_reporter.hpp +++ b/tests/tut/tut_xml_reporter.hpp @@ -1,16 +1,9 @@ -/* - * tut_xml_reporter.hpp - * - * ECOS Library. IPT CS R&D CET ECOS Copyright 2008 Nokia - * Siemens Networks. All right - * - * - */ - #ifndef TUT_XML_REPORTER #define TUT_XML_REPORTER - +#include #include +#include +#include #include #include #include @@ -26,62 +19,12 @@ namespace tut */ class xml_reporter : public tut::callback { -protected: typedef std::vector TestResults; typedef std::map TestGroups; - TestGroups all_tests; /// holds all test results - std::string filename; /// filename base - - /** - * \brief Initializes object - * Resets counters and clears all stored test results. - */ - virtual void init() - { - ok_count = 0; - exceptions_count = 0; - failures_count = 0; - terminations_count = 0; - warnings_count = 0; - all_tests.clear(); - } - - /** - * \brief Encodes text to XML - * XML-reserved characters (e.g. "<") are encoded according to specification - * @param text text to be encoded - * @return encoded string - */ - virtual std::string encode(const std::string & text) - { - std::string out; - - for (unsigned int i=0; i': - out += ">"; - break; - case '&': - out += "&"; - break; - case '\'': - out += "'"; - break; - case '"': - out += """; - break; - default: - out += c; - } - } - - return out; - } + TestGroups all_tests_; /// holds all test results + const std::string filename_; /// filename base + std::auto_ptr stream_; /** * \brief Builds "testcase" XML entity with given parameters @@ -91,7 +34,7 @@ protected: * @param failure_msg failure message to be reported (empty, if test passed) * @return string with \ entity */ - virtual std::string xml_build_testcase(const tut::test_result & tr, const std::string & failure_type, + std::string xml_build_testcase(const tut::test_result & tr, const std::string & failure_type, const std::string & failure_msg, int pid = 0) { using std::endl; @@ -99,17 +42,18 @@ protected: std::ostringstream out; - if (tr.result == test_result::ok) + if ( (tr.result == test_result::ok) || + (tr.result == test_result::skipped) ) { - out << " "; + out << " "; } else { - string err_msg = encode(failure_msg + tr.message); + string err_msg = cppunit_reporter::encode(failure_msg + tr.message); string tag; // determines tag name: "failure" or "error" if ( tr.result == test_result::fail || tr.result == test_result::warn || - tr.result == test_result::ex || tr.result == test_result::ex_ctor ) + tr.result == test_result::ex || tr.result == test_result::ex_ctor || tr.result == test_result::rethrown ) { tag = "failure"; } @@ -118,16 +62,18 @@ protected: tag = "error"; } - out << " " << endl; - out << " <" << tag << " message=\"" << err_msg << "\"" << " type=\"" << failure_type << "\""; + out << " " << endl; + out << " <" << tag << " message=\"" << err_msg << "\"" << " type=\"" << failure_type << "\""; #if defined(TUT_USE_POSIX) if(pid != getpid()) { out << " child=\"" << pid << "\""; } +#else + (void)pid; #endif out << ">" << err_msg << "" << endl; - out << " "; + out << " "; } return out.str(); @@ -140,17 +86,17 @@ protected: * @param failures number of failures to be reported * @param total total number of tests to be reported * @param name test suite name - * @param testcases encoded XML string containing testcases + * @param testcases cppunit_reporter::encoded XML string containing testcases * @return string with \ entity */ - virtual std::string xml_build_testsuite(int errors, int failures, int total, + std::string xml_build_testsuite(int errors, int failures, int total, const std::string & name, const std::string & testcases) { std::ostringstream out; - out << "" << std::endl; + out << " " << std::endl; out << testcases; - out << ""; + out << " "; return out.str(); } @@ -165,39 +111,39 @@ public: /** * \brief Default constructor * @param filename base filename - * @see setFilenameBase */ - xml_reporter(const std::string & _filename = "") + xml_reporter(const std::string & filename) + : all_tests_(), + filename_(filename), + stream_(new std::ofstream(filename_.c_str())), + ok_count(0), + exceptions_count(0), + failures_count(0), + terminations_count(0), + warnings_count(0) { - init(); - setFilenameBase(_filename); + if (!stream_->good()) { + throw tut_error("Cannot open output file `" + filename_ + "`"); + } } - /** - * \brief Sets filename base for output - * @param _filename filename base - * Example usage: - * @code - * xml_reporter reporter; - * reporter.setFilenameBase("my_xml"); - * @endcode - * The above code will instruct reporter to create my_xml_1.xml file for the first test group, - * my_xml_2.xml file for the second, and so on. - */ - virtual void setFilenameBase(const std::string & _filename) + xml_reporter(std::ostream & stream) + : all_tests_(), + filename_(), + stream_(&stream), + ok_count(0), + exceptions_count(0), + failures_count(0), + terminations_count(0), + warnings_count(0) { - if (_filename == "") - { - filename = "testResult"; - } - else - { - if (_filename.length() > 200) - { - throw(std::runtime_error("Filename too long!")); - } + } - filename = _filename; + ~xml_reporter() + { + if(filename_.empty()) + { + stream_.release(); } } @@ -207,7 +153,12 @@ public: */ virtual void run_started() { - init(); + ok_count = 0; + exceptions_count = 0; + failures_count = 0; + terminations_count = 0; + warnings_count = 0; + all_tests_.clear(); } /** @@ -219,6 +170,7 @@ public: // update global statistics switch (tr.result) { case test_result::ok: + case test_result::skipped: ok_count++; break; case test_result::fail: @@ -235,60 +187,52 @@ public: case test_result::term: terminations_count++; break; + case tut::test_result::dummy: + assert(!"Should never be called"); } // switch // add test result to results table - (all_tests[tr.group]).push_back(tr); + all_tests_[tr.group].push_back(tr); } /** * \brief Callback function * This function is called when all tests are completed. It generates XML output - * to file(s). File name base can be set with \ref setFilenameBase. + * to file(s). File name base can be set with constructor. */ virtual void run_completed() { - using std::endl; - using std::string; - - static int number = 1; // results file sequence number (testResult_.xml) + /* *********************** header ***************************** */ + *stream_ << "" << std::endl; + *stream_ << "" << std::endl; // iterate over all test groups - TestGroups::const_iterator tgi; - for (tgi = all_tests.begin(); tgi != all_tests.end(); ++tgi) { + for (TestGroups::const_iterator tgi = all_tests_.begin(); tgi != all_tests_.end(); ++tgi) + { /* per-group statistics */ int passed = 0; // passed in single group int exceptions = 0; // exceptions in single group int failures = 0; // failures in single group int terminations = 0; // terminations in single group int warnings = 0; // warnings in single group - int errors = 0; // errors in single group + int errors = 0; // errors in single group - /* generate output filename */ - char fn[256]; - sprintf(fn, "%s_%d.xml", filename.c_str(), number++); - - std::ofstream xmlfile; - xmlfile.open(fn, std::ios::in | std::ios::trunc); - if (!xmlfile.is_open()) { - throw (std::runtime_error("Cannot open file for output")); - } - - /* *********************** header ***************************** */ - xmlfile << "" << endl; // output is written to string stream buffer, because JUnit format tag // contains statistics, which aren't known yet std::ostringstream out; // iterate over all test cases in the current test group - TestResults::const_iterator tri; - for (tri = (*tgi).second.begin(); tri != (*tgi).second.end(); ++tri) { - string failure_type; // string describing the failure type - string failure_msg; // a string with failure message + const TestResults &results = tgi->second; + for (TestResults::const_iterator tri = results.begin(); tri != results.end(); ++tri) + { + std::string failure_type; // string describing the failure type + std::string failure_msg; // a string with failure message - switch ((*tri).result) { + switch (tri->result) + { case test_result::ok: + case test_result::skipped: passed++; break; case test_result::fail: @@ -298,7 +242,7 @@ public: break; case test_result::ex: failure_type = "Assertion"; - failure_msg = "Thrown exception: " + (*tri).exception_typeid + '\n'; + failure_msg = "Thrown exception: " + tri->exception_typeid + '\n'; exceptions++; break; case test_result::warn: @@ -313,28 +257,27 @@ public: break; case test_result::ex_ctor: failure_type = "Assertion"; - failure_msg = "Constructor has thrown an exception: " + (*tri).exception_typeid + '\n'; + failure_msg = "Constructor has thrown an exception: " + tri->exception_typeid + ".\n"; exceptions++; break; case test_result::rethrown: failure_type = "Assertion"; - failure_msg = "Child failed"; + failure_msg = "Child failed.\n"; failures++; break; default: failure_type = "Error"; failure_msg = "Unknown test status, this should have never happened. " - "You may just have found a BUG in TUT XML reporter, please report it immediately.\n"; + "You may just have found a bug in TUT, please report it immediately.\n"; errors++; break; } // switch #if defined(TUT_USE_POSIX) - out << xml_build_testcase(*tri, failure_type, failure_msg, (*tri).pid) << endl; + out << xml_build_testcase(*tri, failure_type, failure_msg, tri->pid) << std::endl; #else - out << xml_build_testcase(*tri, failure_type, failure_msg) << endl; + out << xml_build_testcase(*tri, failure_type, failure_msg) << std::endl; #endif - } // iterate over all test cases // calculate per-group statistics @@ -342,9 +285,10 @@ public: int stat_failures = failures + warnings + exceptions; int stat_all = stat_errors + stat_failures + passed; - xmlfile << xml_build_testsuite(stat_errors, stat_failures, stat_all, (*tgi).first/* name */, out.str()/* testcases */) << endl; - xmlfile.close(); + *stream_ << xml_build_testsuite(stat_errors, stat_failures, stat_all, (*tgi).first/* name */, out.str()/* testcases */) << std::endl; } // iterate over all test groups + + *stream_ << "" << std::endl; } /** diff --git a/tests/tut_reporter.h b/tests/tut_reporter.h deleted file mode 100644 index e568635a..00000000 --- a/tests/tut_reporter.h +++ /dev/null @@ -1,5 +0,0 @@ - -#include -#include -#include -#include diff --git a/tests/tut_restartable.h b/tests/tut_restartable.h deleted file mode 100644 index c7897797..00000000 --- a/tests/tut_restartable.h +++ /dev/null @@ -1,2 +0,0 @@ - -#include -- 2.44.2 From 703ad5b9a14dc2a42849669d3618a8ba1136bb92 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Mon, 19 Sep 2011 23:05:03 +0300 Subject: [PATCH 14/16] Keep stglibs headers and libraries "at home" --- include/stg/raw_ip_packet.h | 3 -- include/stg/user_ips.h | 2 +- include/stg/user_property.h | 7 +-- stglibs/Makefile.in | 26 ++++++----- stglibs/common.lib/common.cpp | 28 +---------- stglibs/conffiles.lib/conffiles.cpp | 51 +++------------------ stglibs/crypto.lib/ag_md5.c | 4 +- stglibs/crypto.lib/blowfish.c | 2 +- stglibs/dotconfpp.lib/dotconfpp.cpp | 2 +- stglibs/ia.lib/Makefile | 7 ++- stglibs/ia.lib/ia.cpp | 42 ++++++++--------- stglibs/ibpp.lib/Makefile | 2 +- stglibs/ibpp.lib/_ibpp.h | 2 +- stglibs/locker.lib/locker.cpp | 6 +-- stglibs/logger.lib/logger.cpp | 2 +- stglibs/pinger.lib/Makefile | 3 ++ stglibs/pinger.lib/pinger.cpp | 2 +- stglibs/scriptexecuter.lib/scriptexecuter.c | 5 +- stglibs/smux.lib/Makefile | 6 ++- stglibs/srvconf.lib/Makefile | 3 ++ stglibs/srvconf.lib/netunit.cpp | 2 +- stglibs/srvconf.lib/parser.cpp | 2 +- stglibs/srvconf.lib/servconf.cpp | 2 +- 23 files changed, 77 insertions(+), 134 deletions(-) diff --git a/include/stg/raw_ip_packet.h b/include/stg/raw_ip_packet.h index 16e43402..f07bf228 100644 --- a/include/stg/raw_ip_packet.h +++ b/include/stg/raw_ip_packet.h @@ -10,9 +10,6 @@ #include -#include "const.h" -#include "common.h" - #define IPv4 (2) enum { pcktSize = 68 }; //60(max) ip + 8 udp or tcp (part of tcp or udp header to ports) diff --git a/include/stg/user_ips.h b/include/stg/user_ips.h index 250520b2..17bb59be 100644 --- a/include/stg/user_ips.h +++ b/include/stg/user_ips.h @@ -41,7 +41,7 @@ #include #include -#include "common.h" +#include "stg/common.h" #include "os_int.h" using namespace std; diff --git a/include/stg/user_property.h b/include/stg/user_property.h index 822bc0f9..bdad48b0 100644 --- a/include/stg/user_property.h +++ b/include/stg/user_property.h @@ -15,12 +15,13 @@ $Author: faust $ #include #include +#include "stg/logger.h" +#include "stg/locker.h" +#include "stg/scriptexecuter.h" + #include "store.h" #include "admin.h" #include "notifer.h" -#include "logger.h" -#include "locker.h" -#include "scriptexecuter.h" #include "noncopyable.h" extern const volatile time_t stgTime; diff --git a/stglibs/Makefile.in b/stglibs/Makefile.in index a016fed5..c0b4f2b9 100644 --- a/stglibs/Makefile.in +++ b/stglibs/Makefile.in @@ -6,20 +6,24 @@ include ../../Makefile.conf PROG = lib$(LIB_NAME).a +SEARCH_DIRS = -I $(DIR_INCLUDE) -I . -I ./include + ifeq ($(STG_TIME), yes) DEFS += -DSTG_TIME endif -ifneq ($(ADD_DEFS),) -DEFS += $(ADD_DEFS) +ifneq ($(LIB_DEFS),) +DEFS += $(LIB_DEFS) endif -SEARCH_DIRS = -I $(DIR_INCLUDE) -I ./ +ifneq ($(LIB_INCS),) +SEARCH_DIRS += $(LIB_INCS) +endif OBJS = $(notdir $(patsubst %.cpp, %.o, $(patsubst %.c, %.o, $(SRCS)))) -INST_INCS = $(addprefix $(DIR_INCLUDE)/, $(notdir $(INCS))) -INST_LIBS = $(DIR_LIB)/lib$(LIB_NAME) +#INST_INCS = $(addprefix $(DIR_INCLUDE)/, $(notdir $(INCS))) +#INST_LIBS = $(DIR_LIB)/lib$(LIB_NAME) CXXFLAGS += -fPIC CFLAGS += -fPIC @@ -31,16 +35,16 @@ all: $(PROG) $(PROG): $(OBJS) $(STGLIBS) ar rc lib$(LIB_NAME).a $(OBJS) ranlib lib$(LIB_NAME).a - cp *.a $(DIR_LIB) + #cp *.a $(DIR_LIB) -includes: $(INCS) - cp -p $(INCS) $(DIR_INCLUDE)/stg +#includes: $(INCS) +# cp -p $(INCS) $(DIR_INCLUDE)/stg clean: rm -f deps $(PROG) *.o *.a *.so tags *.*~ - for file in $(INCS); do \ - rm -f $(DIR_INCLUDE)/stg/$$file; \ - done + #for file in $(INCS); do \ + # rm -f $(DIR_INCLUDE)/stg/$$file; \ + #done install: $(PROG) mkdir -m $(DIR_MODE) -p $(PREFIX)/usr/lib/stg diff --git a/stglibs/common.lib/common.cpp b/stglibs/common.lib/common.cpp index 3361adb3..855c71d7 100644 --- a/stglibs/common.lib/common.cpp +++ b/stglibs/common.lib/common.cpp @@ -28,32 +28,6 @@ $Author: faust $ */ - -/*#include -#include -#include -#include -#include -#include -#include - - -#include - - -#include -#include - -#ifdef WIN32 -#include -#else -#include -#include -#include -#include -#include -#endif*/ - #ifdef FREE_BSD #include #endif @@ -76,7 +50,7 @@ #include #include -#include "common.h" +#include "stg/common.h" #ifndef INET_ADDRSTRLEN # define INET_ADDRSTRLEN 16 diff --git a/stglibs/conffiles.lib/conffiles.cpp b/stglibs/conffiles.lib/conffiles.cpp index ea51939a..263c9933 100644 --- a/stglibs/conffiles.lib/conffiles.cpp +++ b/stglibs/conffiles.lib/conffiles.cpp @@ -40,8 +40,7 @@ #include -#include "conffiles.h" -#include "stg/common.h" +#include "stg/conffiles.h" using namespace std; @@ -105,33 +104,13 @@ int e = error; error = 0; return e; } -/*//--------------------------------------------------------------------------- -int CONFIGFILE::ReadString(const string & param, char * str, int * maxLen, const char * defaultVal) const -{ -it = param_val.find(param); -// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ - -if (it != param_val.end()) - { - // þÔÏ-ÔÏ ÓÔÏÉÔ - strncpy(str, param_val[param].c_str(), *maxLen); - *maxLen = param_val[param].size(); - return 0; - } - -strncpy(str, defaultVal, *maxLen); -*maxLen = strlen(defaultVal); -return -1; -}*/ //--------------------------------------------------------------------------- int CONFIGFILE::ReadString(const string & param, string * val, const string & defaultVal) const { const map::const_iterator it(param_val.find(param)); -// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ if (it != param_val.end()) { - // þÔÏ-ÔÏ ÓÔÏÉÔ *val = it->second; return 0; } @@ -169,11 +148,9 @@ return -1; int CONFIGFILE::ReadInt(const string & param, int * val, int defaultVal) const { const map::const_iterator it(param_val.find(param)); -// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ if (it != param_val.end()) { - // þÔÏ-ÔÏ ÓÔÏÉÔ char *res; *val = strtol(it->second.c_str(), &res, 10); if (*res != 0) @@ -191,11 +168,9 @@ return -1; int CONFIGFILE::ReadUInt(const string & param, unsigned int * val, unsigned int defaultVal) const { const map::const_iterator it(param_val.find(param)); -// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ if (it != param_val.end()) { - // þÔÏ-ÔÏ ÓÔÏÉÔ char *res; *val = strtoul(it->second.c_str(), &res, 10); if (*res != 0) @@ -213,11 +188,9 @@ return -1; int CONFIGFILE::ReadLongInt(const string & param, long int * val, long int defaultVal) const { const map::const_iterator it(param_val.find(param)); -// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ if (it != param_val.end()) { - // þÔÏ-ÔÏ ÓÔÏÉÔ char *res; *val = strtol(it->second.c_str(), &res, 10); if (*res != 0) @@ -235,11 +208,9 @@ return -1; int CONFIGFILE::ReadULongInt(const string & param, unsigned long int * val, unsigned long int defaultVal) const { const map::const_iterator it(param_val.find(param)); -// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ if (it != param_val.end()) { - // þÔÏ-ÔÏ ÓÔÏÉÔ char *res; *val = strtoul(it->second.c_str(), &res, 10); if (*res != 0) @@ -257,11 +228,9 @@ return -1; int CONFIGFILE::ReadLongLongInt(const string & param, int64_t * val, int64_t defaultVal) const { const map::const_iterator it(param_val.find(param)); -// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ if (it != param_val.end()) { - // þÔÏ-ÔÏ ÓÔÏÉÔ char *res; *val = strtoll(it->second.c_str(), &res, 10); if (*res != 0) @@ -279,11 +248,9 @@ return -1; int CONFIGFILE::ReadULongLongInt(const string & param, uint64_t * val, uint64_t defaultVal) const { const map::const_iterator it(param_val.find(param)); -// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ if (it != param_val.end()) { - // þÔÏ-ÔÏ ÓÔÏÉÔ char *res; *val = strtoull(it->second.c_str(), &res, 10); if (*res != 0) @@ -301,11 +268,9 @@ return -1; int CONFIGFILE::ReadShortInt(const string & param, short int * val, short int defaultVal) const { const map::const_iterator it(param_val.find(param)); -// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ if (it != param_val.end()) { - // þÔÏ-ÔÏ ÓÔÏÉÔ char *res; *val = (short)strtol(it->second.c_str(), &res, 10); if (*res != 0) @@ -323,11 +288,9 @@ return -1; int CONFIGFILE::ReadUShortInt(const string & param, unsigned short int * val, unsigned short int defaultVal) const { const map::const_iterator it(param_val.find(param)); -// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ if (it != param_val.end()) { - // þÔÏ-ÔÏ ÓÔÏÉÔ char *res; *val = (short)strtoul(it->second.c_str(), &res, 10); if (*res != 0) @@ -344,20 +307,18 @@ return -1; //--------------------------------------------------------------------------- void CONFIGFILE::WriteInt(const string & param, int64_t val) { -string s; -x2str(val, s); -param_val[param] = s; +char buf[32]; +snprintf(buf, sizeof(buf), "%lld", static_cast(val)); +param_val[param] = buf; changed = true; } //--------------------------------------------------------------------------- int CONFIGFILE::ReadDouble(const string & param, double * val, double defaultVal) const { const map::const_iterator it(param_val.find(param)); -// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ if (it != param_val.end()) { - // þÔÏ-ÔÏ ÓÔÏÉÔ char *res; *val = strtod(it->second.c_str(), &res); if (*res != 0) @@ -405,8 +366,8 @@ int CONFIGFILE::Flush() const if (!changed) return 0; -std::string pid; -x2str(getpid(), pid); +char pid[6]; +snprintf(pid, sizeof(pid), "%d", getpid()); if (Flush(fileName + "." + pid)) return -1; diff --git a/stglibs/crypto.lib/ag_md5.c b/stglibs/crypto.lib/ag_md5.c index fef284a5..a638c189 100644 --- a/stglibs/crypto.lib/ag_md5.c +++ b/stglibs/crypto.lib/ag_md5.c @@ -1,4 +1,3 @@ - #ifdef WIN32 #include #include @@ -11,8 +10,8 @@ #include #include #include -#include "ag_md5.h" +#include "stg/ag_md5.h" #define F1(x, y, z) (z ^ (x & (y ^ z))) #define F2(x, y, z) F1(z, x, y) @@ -22,7 +21,6 @@ #define MD5STEP(f, w, x, y, z, data, s) \ ( w += f(x, y, z) + data, w = w<>(32-s), w += x ) - int i64c(int i) { if (i <= 0) diff --git a/stglibs/crypto.lib/blowfish.c b/stglibs/crypto.lib/blowfish.c index 69c467bd..951f4816 100644 --- a/stglibs/crypto.lib/blowfish.c +++ b/stglibs/crypto.lib/blowfish.c @@ -8,7 +8,7 @@ #include #include "stg/const.h" -#include "blowfish.h" +#include "stg/blowfish.h" #define ENCRYPT 0 #define DECRYPT 1 diff --git a/stglibs/dotconfpp.lib/dotconfpp.cpp b/stglibs/dotconfpp.lib/dotconfpp.cpp index 8ac25440..41775256 100644 --- a/stglibs/dotconfpp.lib/dotconfpp.cpp +++ b/stglibs/dotconfpp.lib/dotconfpp.cpp @@ -19,7 +19,7 @@ #include // glob #include -#include "dotconfpp.h" +#include "stg/dotconfpp.h" #include "mempool.h" DOTCONFDocumentNode::DOTCONFDocumentNode():previousNode(NULL), nextNode(NULL), parentNode(NULL), childNode(NULL), diff --git a/stglibs/ia.lib/Makefile b/stglibs/ia.lib/Makefile index 15104cd2..3ab0306a 100644 --- a/stglibs/ia.lib/Makefile +++ b/stglibs/ia.lib/Makefile @@ -8,8 +8,11 @@ SRCS = ia.cpp INCS = ia.h -STGLIBS = -lstgcommon \ - -lstgcrypto +STGLIBS = -lstgcrypto + LIBS = $(LIB_THREAD) +LIB_INCS = -I ../crypto.lib/include \ + -I ../common.lib/include + include ../Makefile.in diff --git a/stglibs/ia.lib/ia.cpp b/stglibs/ia.lib/ia.cpp index 81bd75fb..dc8d553e 100644 --- a/stglibs/ia.lib/ia.cpp +++ b/stglibs/ia.lib/ia.cpp @@ -29,25 +29,26 @@ //--------------------------------------------------------------------------- +#ifdef WIN32 +#include +#include +#include +#include +#else +#include +#include +#include +#include +#include +#endif + #include #include #include #include -#ifdef WIN32 - #include - #include - #include - #include -#else - #include - #include - #include - #include -#endif - #include "stg/common.h" -#include "ia.h" +#include "stg/ia.h" #define IA_NONE (0) #define IA_CONNECT (1) @@ -61,13 +62,19 @@ //--------------------------------------------------------------------------- #ifndef WIN32 #include +void Sleep(int ms) +{ +long long res = ms * 1000000; +struct timespec ts = {res / 1000000000, res % 1000000000}; +nanosleep(&ts, NULL); +} +//--------------------------------------------------------------------------- void * RunL(void * data) { sigset_t signalSet; sigfillset(&signalSet); pthread_sigmask(SIG_BLOCK, &signalSet, NULL); - IA_CLIENT_PROT * c = (IA_CLIENT_PROT *)data; static int a = 0; @@ -84,13 +91,6 @@ while (c->GetNonstop()) return NULL; } //--------------------------------------------------------------------------- -void Sleep(int ms) -{ -long long res = ms * 1000000; -struct timespec ts = {res / 1000000000, res % 1000000000}; -nanosleep(&ts, NULL); -} -//--------------------------------------------------------------------------- long GetTickCount() { struct timeval tv; diff --git a/stglibs/ibpp.lib/Makefile b/stglibs/ibpp.lib/Makefile index b73c56d6..59eded2f 100644 --- a/stglibs/ibpp.lib/Makefile +++ b/stglibs/ibpp.lib/Makefile @@ -26,7 +26,7 @@ SRCS = array.cpp \ INCS = ibpp.h -ADD_DEFS = -DIBPP_LINUX +LIB_DEFS = -DIBPP_LINUX LIBS = -lfbclient diff --git a/stglibs/ibpp.lib/_ibpp.h b/stglibs/ibpp.lib/_ibpp.h index e7af2eaf..8284fe9b 100644 --- a/stglibs/ibpp.lib/_ibpp.h +++ b/stglibs/ibpp.lib/_ibpp.h @@ -31,7 +31,7 @@ #ifndef __INTERNAL_IBPP_H__ #define __INTERNAL_IBPP_H__ -#include "ibpp.h" +#include "stg/ibpp.h" #if defined(__BCPLUSPLUS__) || defined(_MSC_VER) || defined(__DMC__) #define HAS_HDRSTOP diff --git a/stglibs/locker.lib/locker.cpp b/stglibs/locker.lib/locker.cpp index 0d97fcb4..94ef026a 100644 --- a/stglibs/locker.lib/locker.cpp +++ b/stglibs/locker.lib/locker.cpp @@ -24,13 +24,11 @@ $Author: nobunaga $ */ - +#ifdef DEBUG_LOCKER #include -#include "locker.h" - -#ifdef DEBUG_LOCKER +#include "stg/locker.h" long long STG_LOCKER::id = 0; pthread_mutex_t STG_LOCKER::lockerMutex = PTHREAD_MUTEX_INITIALIZER; diff --git a/stglibs/logger.lib/logger.cpp b/stglibs/logger.lib/logger.cpp index 215c6392..d8558199 100644 --- a/stglibs/logger.lib/logger.cpp +++ b/stglibs/logger.lib/logger.cpp @@ -2,7 +2,7 @@ #include #include -#include "logger.h" +#include "stg/logger.h" #ifdef STG_TIME extern const volatile time_t stgTime; diff --git a/stglibs/pinger.lib/Makefile b/stglibs/pinger.lib/Makefile index f0aac6d3..c8c09b3e 100644 --- a/stglibs/pinger.lib/Makefile +++ b/stglibs/pinger.lib/Makefile @@ -10,6 +10,9 @@ INCS = pinger.h LIBS = $(LIB_THREAD) +LIB_INCS = -I ../locker.lib/include \ + -I ../common.lib/include + include ../Makefile.in test: all diff --git a/stglibs/pinger.lib/pinger.cpp b/stglibs/pinger.lib/pinger.cpp index af761ffa..664367e8 100644 --- a/stglibs/pinger.lib/pinger.cpp +++ b/stglibs/pinger.lib/pinger.cpp @@ -18,7 +18,7 @@ #include "stg/common.h" #include "stg/locker.h" -#include "pinger.h" +#include "stg/pinger.h" #ifdef STG_TIME extern volatile time_t stgTime; diff --git a/stglibs/scriptexecuter.lib/scriptexecuter.c b/stglibs/scriptexecuter.lib/scriptexecuter.c index 021407dd..462d310b 100644 --- a/stglibs/scriptexecuter.lib/scriptexecuter.c +++ b/stglibs/scriptexecuter.lib/scriptexecuter.c @@ -1,15 +1,14 @@ #include #include #include + #include #include - #include #include #include -#include "scriptexecuter.h" - +#include "stg/scriptexecuter.h" #define MAX_SCRIPT_LEN (1100) diff --git a/stglibs/smux.lib/Makefile b/stglibs/smux.lib/Makefile index 9663b8cd..c5824be9 100644 --- a/stglibs/smux.lib/Makefile +++ b/stglibs/smux.lib/Makefile @@ -4,9 +4,11 @@ LIB_NAME = stgsmux SRCS = $(wildcard *.c) -INCS = $(wildcard *.h) +INCS = $(wildcard include/stg/*.h) -.PHONY: asn1 +LIB_INCS = -I ./include/stg + +#.PHONY: asn1 #ASN1_SOURCES = RFC1213-MIB.asn1 \ # RFC1155-SMI.asn1 \ diff --git a/stglibs/srvconf.lib/Makefile b/stglibs/srvconf.lib/Makefile index 328d8d33..e3bfa762 100644 --- a/stglibs/srvconf.lib/Makefile +++ b/stglibs/srvconf.lib/Makefile @@ -15,4 +15,7 @@ SRCS = netunit.cpp \ INCS = servconf.h \ netunit.h +LIB_INCS = -I ../common.lib/include \ + -I ../crypto.lib/include + include ../Makefile.in diff --git a/stglibs/srvconf.lib/netunit.cpp b/stglibs/srvconf.lib/netunit.cpp index 7f304e2a..9a653d5b 100644 --- a/stglibs/srvconf.lib/netunit.cpp +++ b/stglibs/srvconf.lib/netunit.cpp @@ -32,7 +32,7 @@ #include #include -#include "netunit.h" +#include "stg/netunit.h" #include "stg/common.h" //--------------------------------------------------------------------------- diff --git a/stglibs/srvconf.lib/parser.cpp b/stglibs/srvconf.lib/parser.cpp index 0038f03d..a87266eb 100644 --- a/stglibs/srvconf.lib/parser.cpp +++ b/stglibs/srvconf.lib/parser.cpp @@ -35,7 +35,7 @@ #include "stg/common.h" #include "stg/const.h" -#include "servconf.h" +#include "stg/servconf.h" using namespace std; diff --git a/stglibs/srvconf.lib/servconf.cpp b/stglibs/srvconf.lib/servconf.cpp index a28e2e86..ab386cf7 100644 --- a/stglibs/srvconf.lib/servconf.cpp +++ b/stglibs/srvconf.lib/servconf.cpp @@ -28,7 +28,7 @@ #include #include "stg/common.h" -#include "servconf.h" +#include "stg/servconf.h" using namespace std; -- 2.44.2 From f4b1c42411d34486600b49584c7a0014ccb1b7bf Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Mon, 19 Sep 2011 23:06:02 +0300 Subject: [PATCH 15/16] Keep d-files and objects in test directory --- tests/Makefile | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/tests/Makefile b/tests/Makefile index 6c52d9ea..dbec16b8 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,5 +1,15 @@ -CFLAGS += -g3 -Wall -W -Wextra -DLINUX -I../include -I../projects/stargazer -I../stglibs/stgconffiles.lib -I. -DUSE_ABSTRACT_SETTINGS -DLINUX -CXXFLAGS = ${CFLAGS} +INCS = -I . \ + -I ../include \ + -I ../stglibs/common.lib/include \ + -I ../stglibs/conffiles.lib/include \ + -I ../stglibs/logger.lib/include \ + -I ../stglibs/locker.lib/include \ + -I ../stglibs/scriptexecuter.lib/include \ + -I ../projects/stargazer +DEFS = -DLINUX \ + -DUSE_ABSTRACT_SETTINGS +CFLAGS += -g3 -Wall -W -Wextra $(INCS) $(DEFS) +CXXFLAGS = $(CFLAGS) LIBS = -lpthread PROG = tests @@ -18,26 +28,28 @@ SOURCES = main.cpp \ ../stglibs/logger.lib/logger.cpp \ ../stglibs/scriptexecuter.lib/scriptexecuter.cpp -.PHONY: all includes clean +.PHONY: all clean -all: includes $(PROG) +all: $(PROG) -includes: - make -C ../stglibs includes - -$(PROG): $(subst .cpp,.o,$(SOURCES)) +$(PROG): $(addsuffix .o,$(basename $(SOURCES))) $(CXX) $(LDFLAGS) $^ $(LIBS) -o $@ clean: - rm -f *.o *.d $(PROG) + rm -f $(addsuffix .o,$(basename $(SOURCES))) $(addsuffix .d,$(SOURCES)) $(PROG) ifneq ($(MAKECMDGOALS),distclean) ifneq ($(MAKECMDGOALS),clean) --include $(subst .cpp,.d,$(SOURCES)) +-include $(addsuffix .d,$(SOURCES)) endif endif -%.d: %.cpp - @$(CC) -MM $(CXXFLAGS) $< > $@.$$$$; \ +%.cpp.d: %.cpp + @$(CXX) -MM $(CXXFLAGS) $< > $@.$$$$; \ + sed 's,\($*\).o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ + rm -f $@.$$$$ + +%.c.d: %.c + @$(CC) -MM $(CFLAGS) $< > $@.$$$$; \ sed 's,\($*\).o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ rm -f $@.$$$$ -- 2.44.2 From 2ea67b39f9cade2b55cfbbd554af1049c1c69fd9 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Mon, 19 Sep 2011 23:06:53 +0300 Subject: [PATCH 16/16] New style tut-headers --- tests/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/main.cpp b/tests/main.cpp index 368c9330..8827d6d4 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -1,5 +1,5 @@ #include "tut/tut.hpp" -#include "tut_reporter.h" +#include "tut/tut_reporter.hpp" namespace tut { -- 2.44.2