/* De-Initialize TCP listner sockets. * This function deinitializes everything, including freeing the * session table. No TCP listen receive operations are permitted * unless the subsystem is reinitialized. * rgerhards, 2007-06-21 */ static void deinit_tcp_listener(tcpsrv_t *pThis) { int i; tcpLstnPortList_t *pEntry; tcpLstnPortList_t *pDel; ISOBJ_TYPE_assert(pThis, tcpsrv); if(pThis->pSessions != NULL) { /* close all TCP connections! */ if(!pThis->bUsingEPoll) { i = TCPSessGetNxtSess(pThis, -1); while(i != -1) { tcps_sess.Destruct(&pThis->pSessions[i]); /* now get next... */ i = TCPSessGetNxtSess(pThis, i); } } /* we are done with the session table - so get rid of it... */ free(pThis->pSessions); pThis->pSessions = NULL; /* just to make sure... */ } /* free list of tcp listen ports */ pEntry = pThis->pLstnPorts; while(pEntry != NULL) { free(pEntry->pszPort); prop.Destruct(&pEntry->pInputName); ratelimitDestruct(pEntry->ratelimiter); statsobj.Destruct(&(pEntry->stats)); pDel = pEntry; pEntry = pEntry->pNext; free(pDel); } /* finally close our listen streams */ for(i = 0 ; i < pThis->iLstnCurr ; ++i) { netstrm.Destruct(pThis->ppLstn + i); } }
static inline rsRetVal RunSelect(tcpsrv_t *pThis, nsd_epworkset_t workset[], size_t sizeWorkset) { DEFiRet; int nfds; int i; int iWorkset; int iTCPSess; int bIsReady; nssel_t *pSel = NULL; rsRetVal localRet; ISOBJ_TYPE_assert(pThis, tcpsrv); /* this is an endless loop - it is terminated by the framework canelling * this thread. Thus, we also need to instantiate a cancel cleanup handler * to prevent us from leaking anything. -- rgerhards, 20080-04-24 */ pthread_cleanup_push(RunCancelCleanup, (void*) &pSel); while(1) { CHKiRet(nssel.Construct(&pSel)); if(pThis->pszDrvrName != NULL) CHKiRet(nssel.SetDrvrName(pSel, pThis->pszDrvrName)); CHKiRet(nssel.ConstructFinalize(pSel)); /* Add the TCP listen sockets to the list of read descriptors. */ for(i = 0 ; i < pThis->iLstnCurr ; ++i) { CHKiRet(nssel.Add(pSel, pThis->ppLstn[i], NSDSEL_RD)); } /* do the sessions */ iTCPSess = TCPSessGetNxtSess(pThis, -1); while(iTCPSess != -1) { /* TODO: access to pNsd is NOT really CLEAN, use method... */ CHKiRet(nssel.Add(pSel, pThis->pSessions[iTCPSess]->pStrm, NSDSEL_RD)); /* now get next... */ iTCPSess = TCPSessGetNxtSess(pThis, iTCPSess); } /* wait for io to become ready */ CHKiRet(nssel.Wait(pSel, &nfds)); if(glbl.GetGlobalInputTermState() == 1) break; /* terminate input! */ iWorkset = 0; for(i = 0 ; i < pThis->iLstnCurr ; ++i) { if(glbl.GetGlobalInputTermState() == 1) ABORT_FINALIZE(RS_RET_FORCE_TERM); CHKiRet(nssel.IsReady(pSel, pThis->ppLstn[i], NSDSEL_RD, &bIsReady, &nfds)); if(bIsReady) { workset[iWorkset].id = i; workset[iWorkset].pUsr = (void*) pThis->ppLstn; /* this is a flag to indicate listen sock */ ++iWorkset; if(iWorkset >= (int) sizeWorkset) { processWorkset(pThis, NULL, iWorkset, workset); iWorkset = 0; } //DBGPRINTF("New connect on NSD %p.\n", pThis->ppLstn[i]); //SessAccept(pThis, pThis->ppLstnPort[i], &pNewSess, pThis->ppLstn[i]); --nfds; /* indicate we have processed one */ } } /* now check the sessions */ iTCPSess = TCPSessGetNxtSess(pThis, -1); while(nfds && iTCPSess != -1) { if(glbl.GetGlobalInputTermState() == 1) ABORT_FINALIZE(RS_RET_FORCE_TERM); localRet = nssel.IsReady(pSel, pThis->pSessions[iTCPSess]->pStrm, NSDSEL_RD, &bIsReady, &nfds); if(bIsReady || localRet != RS_RET_OK) { workset[iWorkset].id = iTCPSess; workset[iWorkset].pUsr = (void*) pThis->pSessions[iTCPSess]; ++iWorkset; if(iWorkset >= (int) sizeWorkset) { processWorkset(pThis, NULL, iWorkset, workset); iWorkset = 0; } --nfds; /* indicate we have processed one */ } iTCPSess = TCPSessGetNxtSess(pThis, iTCPSess); } if(iWorkset > 0) processWorkset(pThis, NULL, iWorkset, workset); /* we need to copy back close descriptors */ CHKiRet(nssel.Destruct(&pSel)); finalize_it: /* this is a very special case - this time only we do not exit the function, * because that would not help us either. So we simply retry it. Let's see * if that actually is a better idea. Exiting the loop wasn't we always * crashed, which made sense (the rest of the engine was not prepared for * that) -- rgerhards, 2008-05-19 */ if(pSel != NULL) { /* cleanup missing? happens during err exit! */ nssel.Destruct(&pSel); } } /* note that this point is usually not reached */ pthread_cleanup_pop(1); /* remove cleanup handler */ RETiRet; }