void ConnectionProvider::CloseConnections() { for (ConnectionVector::iterator i = connections_.begin(); i != connections_.end(); ++i) { Connection* connection = *i; connection->Close(); } connections_.clear(); }
void ConnectionProvider::DeleteConnections() { for (ConnectionVector::iterator i = connections_.begin(); i != connections_.end(); ++i) { Connection* connection = *i; //! @todo Check that there is enough time to actual close the connection to servers connection->Close(); // <--- CRASH HERE when login is failed SAFE_DELETE(connection); } connections_.clear(); }
int main(int argc, char** argv) { CSCGlobal::gAgentName = "failover_test"; // get the config CConfig::load(); // process the version info if ((argc == 2) && (strcmp("-v", argv[1]) == 0)) { printf("%s %s\r\n", argv[0], SC_VER_STR); exit(0); } if (argc == 2) { sleepTime = atol(argv[1]); Connection* connection; Result* result; char query[10240]; long counter = 0; time_t now; struct tm* timeStruct; char timeNowStr[32]; while (1) { ++counter; now = time(NULL); timeStruct = localtime(&now); /* Convert to local time. */ sprintf(timeNowStr, "%04d-%02d-%02d %02d:%02d:%02d", timeStruct->tm_year + 1900, timeStruct->tm_mon + 1, timeStruct->tm_mday, timeStruct->tm_hour, timeStruct->tm_min, timeStruct->tm_sec ) ; try { printf("\r\nConnecting..."); // connect to the database Trace("Connecting to db", "", 0); connection = new Connection(CConfig::getDBServer(), CConfig::getDBDatabase(), CConfig::getDBUser(), CConfig::getDBPass(), CConfig::getDBRetryTime()); printf("OK. Inserting ID: %ld", counter); // run all the queries here... sprintf(query, "insert into Testing (ID,AgentTime,DBTime) values (%ld,'%s',now())", counter, timeNowStr); result = connection->Query(query); // close the db connection connection->Close(); delete connection; connection = NULL; Trace("Disonnected from db", "", 0); } catch (const std::exception &e) { LogError("Exception caught", e.what(), 0); printf("Exception: %s\n", e.what()); } Trace("Sleeping", "", sleepTime); sleep(sleepTime); } } else { Usage(); } exit(0); }
int omain(int argc, oarg* argv[]) { ostring home; ostring dbs; ostring usr; ostring pwd; size_t i; /* CHECK COMMAND LINE --------------------------------------------------- */ if (argc < (ArgCount - 1)) { return EXIT_FAILURE; } /* GET ARGUMENTS ---------------------------------------------------------*/ dbs = GetArg(argv[ArgDatabase]); usr = GetArg(argv[ArgUser]); pwd = GetArg(argv[ArgPassword]); if (argc == ArgCount) { home = GetArg(argv[ArgHome]); } try { Environment::Initialize(Environment::Default | Environment::Threaded, home); Environment::EnableWarnings(true); ocout << otext("Connecting to ") << usr << otext("/") << pwd << otext("@") << dbs << oendl << oendl; con.Open(dbs, usr, pwd, Environment::SessionDefault); print_version(); create_tables(); /* execute tests */ for (i = 0; i < ARRAY_COUNT(tab_test); i++) { if (tab_test[i].execute) tab_test[i].proc(); } drop_tables(); con.Close(); } catch (std::exception &ex) { ocout << ex.what() << oendl; } if (con) { drop_tables(); con.Close(); } Environment::Cleanup(); ocout << otext("\nPress any key to exit..."); getchar(); return EXIT_SUCCESS; }
void* startThread(void* arg) { /* SELECT W.PageID,PageType,URL,ResetCookies,AuthUser,AuthPass,DownloadThreshold,HackingString,ResetCache,ServiceType,SecurityID,WPS.CurrentErrorRef,WPS.AlertCount,AlertLevel2Count,AlertLevel3Count,WPS.ErrorStatus,W.Status,W.IgnoreRudeWords,W.CustomerNo,W.HTTPMethod,W.IgnoreContentErrors,W.DownloadSpeed,W.Referer,W.StoreResultDetail,W.MinSize,W.MaxSize,C.AlertCount,W.CertID,W.StepName,W.AgentString,W.sm_processScript,WPS.OverallAlertCount,W.StoreRequest,W.StoreHeaders,W.StoreHTML,W.StoreDiag,W.ContentType,W.Header1,W.Header2,W.GzipEncoding from WebPage W, Customer C, WebPageStatus WPS where W.PageID=13714 and WPS.PageID=W.PageID and W.CustomerNo=C.CustomerNo and (W.Status=1 or W.Status=3) select VariableName, VariableValue, Encode from FormVariables where PageID=13714 order by OrderNo asc SELECT Phrase, ID from ContentError SELECT Word, ID from ObsceneWord SELECT Phrase, ErrorCode from WebPagePhrase where PageID=13714 order by PhraseOrder select CookieValue, CookieDomain from Cookies where PageID=13714 select URL, RequiredText from WebPageExtraFiles where PageID=13714 and Type=0 select URL, RequiredText from WebPageExtraFiles where PageID=13714 and Type=1 select URL, RequiredText from WebPageExtraFiles where PageID=13714 and Type=2 INSERT INTO ResultHeader040303 (ResultID,PageID,TestServer,StartDateTime,ResultCode,DNSTime,FirstByteTime,CompleteTime,TotalBytes,RunID,BatchID,FirstDataTime) VALUES (0,13714,10,'2004-03-03 14:39:20',1,8,9,5309,25291,0,0,15) SELECT LAST_INSERT_ID() from ResultHeader040303 where ResultID=LAST_INSERT_ID() INSERT INTO ResultDetail040303 (ResultID,StartDateTime,StartOffset,DNSTime,FirstByteTime,FirstDataTime,CompleteTime,ResultCode,TotalBytes,FileName,ComponentNo) VALUES (40,'2004-03-03 14:39:20',0,8,9,15,892,200,4338,'http://192.168.1.5',1) INSERT INTO ResultDetail040303 (ResultID,StartDateTime,StartOffset,DNSTime,FirstByteTime,FirstDataTime,CompleteTime,ResultCode,TotalBytes,FileName,ComponentNo) VALUES (40,'2004-03-03 14:39:21',1012,1,1,7,4188,200,6543,'http://192.168.1.5/g/p10.png',2) INSERT INTO ResultDetail040303 (ResultID,StartDateTime,StartOffset,DNSTime,FirstByteTime,FirstDataTime,CompleteTime,ResultCode,TotalBytes,FileName,ComponentNo) VALUES (40,'2004-03-03 14:39:21',1025,0,2,3,4185,200,12237,'http://192.168.1.5/g/MYSQLAuthCSP_small.png',3) INSERT INTO ResultDetail040303 (ResultID,StartDateTime,StartOffset,DNSTime,FirstByteTime,FirstDataTime,CompleteTime,ResultCode,TotalBytes,FileName,ComponentNo) VALUES (40,'2004-03-03 14:39:21',1008,1,2,6,4301,200,2173,'http://192.168.1.5/g/newlogo.png',4) SELECT PageID from WebPageStatus where PageID=13714 UPDATE WebPageStatus set LastResultID=40,StartDateTime='2004-03-03 14:39:20',LastResultCode=1,CompleteTime=5309 where PageID=13714 SELECT WS.ErrorStatus,WS.CurrentErrorRef,WS.AlertCount,C.AlertLevel2Count,C.AlertLevel3Count,C.AlertCount,W.OverallAlertCount from Customer C, WebPage W, WebPageStatus WS WHERE C.CustomerNo=W.CustomerNo and W.PageID=WS.PageID and W.PageID=13714 UPDATE WebPageStatus SET ErrorStatus=0,CurrentErrorRef=0,AlertCount=0 WHERE PageID=13714 UPDATE WebPageStatus SET OverallAlertCount=0 WHERE PageID=13714 select ErrorRef from WebPageError where PageID=13714 and Status<>2 */ mysql_thread_init(); Connection* connection; Result* result; char query[10240]; for (int repNo = 0; repNo < numRepsPerThreads; ++repNo) { try { Trace("Connecting to db", "", repNo); connection = new Connection(CConfig::getDBServer(), CConfig::getDBDatabase(), CConfig::getDBUser(), CConfig::getDBPass(), CConfig::getDBRetryTime()); // run all the queries here... result = connection->Query("SELECT W.PageID,PageType,URL,ResetCookies,AuthUser,AuthPass,DownloadThreshold,HackingString,ResetCache,ServiceType,SecurityID,WPS.CurrentErrorRef,WPS.AlertCount,AlertLevel2Count,AlertLevel3Count,WPS.ErrorStatus,W.Status,W.IgnoreRudeWords,W.CustomerNo,W.HTTPMethod,W.IgnoreContentErrors,W.DownloadSpeed,W.Referer,W.StoreResultDetail,W.MinSize,W.MaxSize,C.AlertCount,W.CertID,W.StepName,W.AgentString,W.sm_processScript,WPS.OverallAlertCount,W.StoreRequest,W.StoreHeaders,W.StoreHTML,W.StoreDiag,W.ContentType,W.Header1,W.Header2,W.GzipEncoding from WebPage W, Customer C, WebPageStatus WPS where W.PageID=13714 and WPS.PageID=W.PageID and W.CustomerNo=C.CustomerNo and (W.Status=1 or W.Status=3)"); result = connection->Query("select VariableName, VariableValue, Encode from FormVariables where PageID=13714 order by OrderNo asc"); result = connection->Query("SELECT Phrase, ID from ContentError"); result = connection->Query("SELECT Word, ID from ObsceneWord"); result = connection->Query("SELECT Phrase, ErrorCode from WebPagePhrase where PageID=13714 order by PhraseOrder"); result = connection->Query("select CookieValue, CookieDomain from Cookies where PageID=13714"); result = connection->Query("select URL, RequiredText from WebPageExtraFiles where PageID=13714 and Type=0"); result = connection->Query("select URL, RequiredText from WebPageExtraFiles where PageID=13714 and Type=1"); result = connection->Query("select URL, RequiredText from WebPageExtraFiles where PageID=13714 and Type=2"); // close the db connection connection->Close(); delete connection; connection = NULL; Trace("Disonnected from db", "", repNo); Trace("Connecting to db", "", repNo); connection = new Connection(CConfig::getDBServer(), CConfig::getDBDatabase(), CConfig::getDBUser(), CConfig::getDBPass(), CConfig::getDBRetryTime()); result = connection->Query("INSERT INTO ResultHeader040303 (ResultID,PageID,TestServer,StartDateTime,ResultCode,DNSTime,FirstByteTime,CompleteTime,TotalBytes,RunID,BatchID,FirstDataTime) VALUES (0,13714,10,'2004-03-03 14:39:20',1,8,9,5309,25291,0,0,15)"); result = connection->Query("SELECT LAST_INSERT_ID() from ResultHeader040303 where ResultID=LAST_INSERT_ID()"); result = connection->Query("INSERT INTO ResultDetail040303 (ResultID,StartDateTime,StartOffset,DNSTime,FirstByteTime,FirstDataTime,CompleteTime,ResultCode,TotalBytes,FileName,ComponentNo) VALUES (40,'2004-03-03 14:39:20',0,8,9,15,892,200,4338,'http://192.168.1.5',1)"); result = connection->Query("INSERT INTO ResultDetail040303 (ResultID,StartDateTime,StartOffset,DNSTime,FirstByteTime,FirstDataTime,CompleteTime,ResultCode,TotalBytes,FileName,ComponentNo) VALUES (40,'2004-03-03 14:39:21',1012,1,1,7,4188,200,6543,'http://192.168.1.5/g/p10.png',2)"); result = connection->Query("INSERT INTO ResultDetail040303 (ResultID,StartDateTime,StartOffset,DNSTime,FirstByteTime,FirstDataTime,CompleteTime,ResultCode,TotalBytes,FileName,ComponentNo) VALUES (40,'2004-03-03 14:39:21',1025,0,2,3,4185,200,12237,'http://192.168.1.5/g/MYSQLAuthCSP_small.png',3)"); result = connection->Query("INSERT INTO ResultDetail040303 (ResultID,StartDateTime,StartOffset,DNSTime,FirstByteTime,FirstDataTime,CompleteTime,ResultCode,TotalBytes,FileName,ComponentNo) VALUES (40,'2004-03-03 14:39:21',1008,1,2,6,4301,200,2173,'http://192.168.1.5/g/newlogo.png',4)"); result = connection->Query("SELECT PageID from WebPageStatus where PageID=13714"); result = connection->Query("UPDATE WebPageStatus set LastResultID=40,StartDateTime='2004-03-03 14:39:20',LastResultCode=1,CompleteTime=5309 where PageID=13714"); result = connection->Query("SELECT WS.ErrorStatus,WS.CurrentErrorRef,WS.AlertCount,C.AlertLevel2Count,C.AlertLevel3Count,C.AlertCount,W.OverallAlertCount from Customer C, WebPage W, WebPageStatus WS WHERE C.CustomerNo=W.CustomerNo and W.PageID=WS.PageID and W.PageID=13714"); result = connection->Query("UPDATE WebPageStatus SET ErrorStatus=0,CurrentErrorRef=0,AlertCount=0 WHERE PageID=13714"); result = connection->Query("UPDATE WebPageStatus SET OverallAlertCount=0 WHERE PageID=13714"); result = connection->Query("select ErrorRef from WebPageError where PageID=13714 and Status<>2"); // close the db connection connection->Close(); delete connection; connection = NULL; Trace("Disonnected from db", "", repNo); } catch (const std::exception &e) { printf("Exception caught: - %s\r\n", e.what()); // all done - exit the thread // pthread_exit(NULL); } Trace("Sleeping", "", 100 * sleepTime); usleep(100 * sleepTime); } mysql_thread_end(); // all done - exit the thread pthread_exit(NULL); Trace("After Thread end", "", 0); }
int server::listenAndserve() { char * sfdstr = getenv("sfd"); char * epollfdstr = getenv("epollfd"); //return 0; if(sfdstr){ sfd = atoi(sfdstr); } if(epollfdstr) { //epollfd = atoi(epollfdstr); } if(sfd == 0) { sfd = socket(AF_INET,SOCK_STREAM,0); int flag = fcntl(sfd,F_GETFL,0); int result = fcntl(sfd,F_SETFL,flag | O_NONBLOCK); if(sfd < 0) { printf("create socket error\n"); exit(-1); //closeProc(); } struct sockaddr_in addr; bzero(&addr,sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(10001); printf("port is %d\n",addr.sin_port); int on; on = 1; setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&on, sizeof(on)); int bid = bind(sfd,(struct sockaddr*)&addr,sizeof(struct sockaddr)); if(bid < 0) { printf("bind err\n"); printError(errno,__LINE__); exit(-1); //closeProc(); } int lister = listen(sfd,10); if(lister < 0){ printError(errno,__LINE__); exit(-1); //closeProc(); }else { printf("listen sucess\n"); } } if(epollfd == 0) { epollfd = epoll_create1(0); printf("epofd = %d\n",epollfd); } { Connection * sfdConnect = new Connection(sfd,NULL); int add = EventProcess(epollfd,sfd,EPOLL_CTL_ADD,EPOLLET|EPOLLIN|EPOLLOUT,sfdConnect); if(add < 0) { printError(errno,__LINE__); exit(-1); //closeProc(); } } printf("------------------\n"); logger->Loginfo("sfd = %d sfd = %d",sfd,epollfd); { int x = pipe(loopSwitch); if(x < 0) { printf("create pipe err\n"); } //loopSwitch = open("./loopSwitch",O_CREAT|O_RDWR,0644); Connection * sfdConnect = new Connection(loopSwitch[0],NULL); int add = EventProcess(epollfd,loopSwitch[0],EPOLL_CTL_ADD,EPOLLET|EPOLLIN|EPOLLOUT,sfdConnect); // if(add < 0) { // printf("add looperr errno %d\n",errno); // } if(add < 0) { printError(errno,__LINE__); exit(-1); //closeProc(); } } int subfd = 0; struct sockaddr_in remoteaddr; socklen_t len; struct epoll_event *eventArry = (struct epoll_event*)malloc(sizeof(struct epoll_event) * MAXEVENTS); int num = 0,index = 0; while(true) { bzero(eventArry,sizeof(struct epoll_event) * MAXEVENTS); struct sigaction act; int sig = SIGSEGV; sigemptyset(&act.sa_mask); sigaddset(&act.sa_mask,SIGUSR1); sigaddset(&act.sa_mask,SIGUSR2); num = epoll_pwait(epollfd,eventArry,MAXEVENTS,-1,NULL); //printf("wait success num = %d err %d pid = %d\n",num,errno,getpid()); int xx = errno; if(num == -1) { switch(xx) { case EBADF:{ break; } case EFAULT:{ break; } case EINTR:{ break; } case EINVAL:{ break; } } continue; } // EBADF epfd is not a valid file descriptor. // // EFAULT The memory area pointed to by events is not accessible with write permis‐ // sions. // // EINTR The call was interrupted by a signal handler before either (1) any of the // requested events occurred or (2) the timeout expired; see signal(7). // // EINVAL epfd is not an epoll file descriptor, or maxevents is less than or equal to // zero. for(index = 0;index < num; index++) { Connection * context = (Connection*)eventArry[index].data.ptr; int aFd = context->fd; int subfd = -1; if(aFd == sfd) { if(accepton) { while((subfd = accept(sfd,(struct sockaddr*)&remoteaddr,&len)) > 0){ Connection * connection = new Connection(subfd,&remoteaddr); //printf("172 addr %x\n",connection); EventProcess(epollfd,subfd,EPOLL_CTL_ADD,EPOLLET|EPOLLIN ,connection); //handleConnect(connection); connection->readHandler = &readHandler; } } }else if( aFd == loopSwitch[0] ||aFd == loopSwitch[1]){ printf("pipe closed\n"); return 0; } else { Connection * connection = (Connection*)eventArry[index].data.ptr; //printf("179 addr %x\n",connection); if(eventArry[index].events & EPOLLIN) { if(connection->readHandler != NULL){ int x = connection->readHandler(connection); if(x == 0) { EventProcess(epollfd,aFd,EPOLL_CTL_DEL,0,NULL); connection->Close(); delete(connection); } } } } } // subfd = accept(sfd,(struct sockaddr*)&remoteaddr,&len); //printf("remoate port %d\n",remoteaddr.sin_port); //close(subfd); } }