예제 #1
0
void restart_child_process(pid_t pid)
{
	bool process_is_too_short = false;
	processLock.Lock();
	std::map<pid_t,time_t>::iterator it = processes.find(pid);
	if (it!=processes.end()) {
		if (time(NULL) - (*it).second < 5) {
			process_is_too_short = true;
		}
		processes.erase(it);
	}
	processLock.Unlock();
#ifdef _WIN32
	CloseHandle(pid);
#endif
	if (process_is_too_short) {
		sleep(1);
	}
	if(program_quit){
		return;
	}
	if(!::createProcess(&ls, NULL, argv, NULL, RDSTD_INPUT)){
                debug("cann't create process\n");
                return ;
        }
        pid = ls.process.stealPid();
	processLock.Lock();
        processes.insert(pair<pid_t,time_t>(pid,time(NULL)));
	processLock.Unlock();
	return;	
}
예제 #2
0
int checkResponse(KHttpRequest *rq,KHttpObject *obj)
{
	int action = kaccess[RESPONSE].check(rq,obj);
#ifdef ENABLE_KSAPI_FILTER
	if (action == JUMP_ALLOW && conf.gvm->globalVh.hfm) {
		action = conf.gvm->globalVh.hfm->check_response(rq);
	}
#endif
#ifndef HTTP_PROXY
#ifdef ENABLE_USER_ACCESS
	if (action == JUMP_ALLOW && rq->svh) {
		action = rq->svh->vh->checkResponse(rq);
#ifdef ENABLE_KSAPI_FILTER
		if (action==JUMP_ALLOW && rq->svh->vh->hfm) {
			action = rq->svh->vh->hfm->check_response(rq);
		}
#endif
	}
#endif
#endif
	if (action == JUMP_DENY) {
		KMutex *lock = obj->getLock();
		lock->Lock();
		if (obj->refs == 1) {
			KBuffer::destroy(obj->data->bodys);
			obj->data->bodys = NULL;
			set_obj_size(obj, 0);
		}
		lock->Unlock();
	}
	return action;
}
예제 #3
0
int del_filter_time()
{
	int uid=0;
	unsigned src_ip;
	int recv_size;
	int send_size;
	pthread_t pid=pthread_self();
	filter_time_map_t::iterator it;
	filter_time_lock.Lock();
	it=filter_time_list.find(pid);
	if(it==filter_time_list.end())
		goto done;
	uid=(*it).second.m_state.uid;
	src_ip=(*it).second.m_state.src_ip;
	recv_size=(*it).second.server->recv_size;
	send_size=(*it).second.server->send_size;
	filter_time_list.erase(it);
	filter_time_lock.Unlock();
	#ifndef DISABLE_USER
	m_user.UpdateSendRecvSize(src_ip,recv_size,send_size,uid);
	#endif
	return uid;
done:
	filter_time_lock.Unlock();
	return uid;
	
}
예제 #4
0
void add_filter_time(filter_time &m_filter_time)
{
	m_filter_time.start_time=time(NULL);
	pthread_t pid=pthread_self();
	
	filter_time_lock.Lock();
	filter_time_list[pid]=m_filter_time;
	filter_time_lock.Unlock();
	
}
예제 #5
0
static KApiDso *getApiRedirect(u_short id) {
	KApiDso *rd = NULL;
	std::map<u_short, KApiDso *>::iterator it;
	lock.Lock();
	it = apis.find(id);
	if (it != apis.end()) {
		rd = (*it).second;
		//rd->addRef();
	}
	lock.Unlock();
	return rd;
}
예제 #6
0
static bool loadApiRedirect(const char *path,u_short id) {
	
	KApiDso *rd = new KApiDso;
	rd->path = path;
	if (!rd->load()) {
		delete rd;
		return false;
	}
	lock.Lock();
	apis[id] = rd;
	lock.Unlock();
	return true;
}
예제 #7
0
FUNC_TYPE FUNC_CALL swapin_thread(void *param)
{
	for (;;) {
		KHttpRequest *rq = NULL;
		swapinQueueLock.Lock();
		if (swapinQueue.size()<=0) {
			current_swapin_worker--;
			swapinQueueLock.Unlock();
			break;
		}
		rq = *swapinQueue.begin();
		swapinQueue.pop_front();
		swapinQueueLock.Unlock();
		handle_request_swapin(rq);
	}
	KTHREAD_RETURN;
}
예제 #8
0
/*
异步发送物件
rq->filter_flags=RQ_SWAP_OLD_OBJ则发送rq->ctx->old_obj
否则就发送rq->ctx->obj
同步函数为:sendHttpObject,完成相同的功能
*/
bool asyncSendHttpObject(KHttpRequest *rq)
{
	//debug("asyncSendHttpObject %p\n",rq);
	kassert(!TEST(rq->flags,RQ_SYNC));
	//assert(rq->op != STAGE_OP_SYNC);
	if (TEST(rq->filter_flags,RQ_SWAP_OLD_OBJ)) {
		rq->ctx->popObj();
		CLR(rq->filter_flags,RQ_SWAP_OLD_OBJ);
	}
	KHttpObject *obj = rq->ctx->obj;
	assert(obj);
	//assert(rq->fetchObj || TEST(rq->workModel,WORK_MODEL_MANAGE) || rq->svh);
	
	if (!TEST(obj->index.flags,FLAG_IN_MEM)) {
		KMutex *lock = obj->getLock();
		rq->c->removeRequest(rq);
		lock->Lock();
		if (obj->data==NULL) {
#ifdef ENABLE_DISK_CACHE
			obj->data = new KHttpObjectBody();
			obj->data->type = SWAPING_OBJECT;
			obj->data->os = new KHttpObjectSwaping;
			lock->Unlock();
			stage_swapin(rq);
			return false;
#else
			lock->Unlock();
			SET(obj->index.flags,FLAG_DEAD);
			klog(KLOG_ERR,"BUG!! obj is not in memory.");
			assert(false);
			stageEndRequest(rq);
			return false;
#endif
		} else if(obj->data->type == SWAPING_OBJECT) {
			//已经有其它线程在swap
			KHttpObjectSwaping *os = obj->data->os;
			assert(os);			
			os->queue.push_back(rq);
			lock->Unlock();
			return false;
		}
		lock->Unlock();		
	}
	sendMemoryObject(rq,obj);
	return true;
}
예제 #9
0
파일: mysocket.cpp 프로젝트: dolfly/kingate
void make_ip(unsigned long ip,char *ips,bool mask)
{

	struct in_addr s;
	memset(ips,0,18);
	ips[0]='/';
	int skip=1;
	if(!mask)
		skip=0;
	s.s_addr=ip;
	if(ip==0)
		strcpy(ips,"*");
	else if(ip==~0)
		ips[0]=0;
	else if(ip==1)
		strcpy(ips,"localhost");
	else{	
		m_make_ip_lock.Lock();
		strncpy(ips+skip,inet_ntoa(s),16);
		m_make_ip_lock.Unlock();
	}
}
예제 #10
0
KTHREAD_FUNCTION handle_request_swapin(void *param)
{
	KHttpRequest *rq = (KHttpRequest *)param;
	KHttpObject *obj = rq->ctx->obj;
	assert(obj);
	assert(obj->data->type == SWAPING_OBJECT);
	KHttpObjectBody *osData = obj->data;
	assert(osData->os);
	KHttpObjectBody *data = new KHttpObjectBody();
	bool result = obj->swapin(data);
	/**
	* swap in进来的时候是不锁的,这样尽可能的保证畅通,因为swap in可能会比较长时间
	*/
	KMutex *lock = obj->getLock();
	lock->Lock();
	obj->data = data;
	if (!result) {
		SET(obj->index.flags,FLAG_DEAD|OBJ_INDEX_UPDATE);
	} else {
		SET(obj->index.flags,FLAG_IN_MEM);
	}
	lock->Unlock();
	if (result) {
		/////////[144]
			cache.getHash(obj->h)->incSize(obj->index.content_length);
	}
	assert(osData);
	handle_swapin_result(rq,result);
	KHttpObjectSwaping *os = osData->os;
	std::list<KHttpRequest *>::iterator it;
	for (it=os->queue.begin();it!=os->queue.end();it++) {
		handle_swapin_result((*it),result);
	}
	os->queue.clear();
	delete osData;
	KTHREAD_RETURN;
}
예제 #11
0
파일: KGLog.cpp 프로젝트: viticm/pap2
int KGLogPrintf(KGLOG_PRIORITY Priority, const char cszFormat[], ...)
{
    if ((KGLOG_MASK(KGLOG_PRI(Priority)) & gs_nPriorityMask) == 0)
        return true;

    int nResult     = false;
    int nRetCode    = false;
    int nLockFlag   = false;
    int nBufferLen  = 0;
    char szBuffer[1024];

    time_t tmtNow = 0;
    struct tm tmNow;


    tmtNow = time(NULL);

    localtime_r(&tmtNow, &tmNow);

    nRetCode = _snprintf(
                   szBuffer, sizeof(szBuffer) - 1,
                   "%d%2.2d%2.2d-%2.2d%2.2d%2.2d<%s:%p>: ",
                   tmNow.tm_year + 1900,
                   tmNow.tm_mon + 1,
                   tmNow.tm_mday,
                   tmNow.tm_hour,
                   tmNow.tm_min,
                   tmNow.tm_sec,
                   gs_caszPriorityString[KGLOG_PRI(Priority)],
                   KGThread_GetSelfId()
               );
    szBuffer[sizeof(szBuffer) - 1] = '\0';

    if ((nRetCode != -1) && (nRetCode < (sizeof(szBuffer) - 1)))
        nBufferLen = nRetCode;
    else
        nBufferLen = sizeof(szBuffer) - 1;

    va_list marker;
    va_start(marker, cszFormat);

    nRetCode = vsnprintf(szBuffer + nBufferLen, sizeof(szBuffer) - 1 - nBufferLen, cszFormat, marker);

    va_end(marker);

    szBuffer[sizeof(szBuffer) - 1] = '\0';

    if ((nRetCode != -1) && (nRetCode < (sizeof(szBuffer) - 1)))
        nBufferLen += nRetCode;
    else
        nBufferLen = sizeof(szBuffer) - 1;

    if (
        (szBuffer[nBufferLen - 1] != '\n') &&
        (szBuffer[nBufferLen - 1] != '\r')
    )
    {
        if (nBufferLen >= (sizeof(szBuffer) - 1))   // if full
            nBufferLen--;

        szBuffer[nBufferLen] = '\n';
        szBuffer[nBufferLen + 1] = '\0';
        nBufferLen++;
    }

    if (gs_LogParam.Options & KGLOG_OPTION_CONSOLE)
    {
        fputs(szBuffer, stdout);
    }
    if (gs_LogParam.Options & KGLOG_OPTION_STDERR)
    {
        fputs(szBuffer, stderr);
    }

    if (gs_LogParam.Options & KGLOG_OPTION_FILE)
    {
        gs_Lock.Lock();
        nLockFlag = true;

        if (
            gs_nIsFirstTimeToWriteLog ||
            (gs_nCurrentLine >= gs_LogParam.nMaxLineEachFile) ||
            (!(
                 (gs_tmLastChangeFile.tm_mday == tmNow.tm_mday) &&
                 (gs_tmLastChangeFile.tm_mon  == tmNow.tm_mon)  &&
                 (gs_tmLastChangeFile.tm_year == tmNow.tm_year)
             ))
        )
        {
            gs_nIsFirstTimeToWriteLog = false;
            gs_tmLastChangeFile = tmNow;

            nRetCode = _ChangeLogFile();
            KGLOG_PROCESS_ERROR(nRetCode);
            gs_nCurrentLine = 0;
        }

        if (gs_nLogFile != -1)
        {
            nRetCode = _write(gs_nLogFile, szBuffer, nBufferLen);
            if (nRetCode == -1)
            {
                puts("warning : error on write log file, please check your hard disk!");
            }
        }
        ++gs_nCurrentLine;
    }

    nResult = true;
Exit0:
    if (nLockFlag)
    {
        gs_Lock.Unlock();
        nLockFlag = false;
    }
    return nResult;
}
예제 #12
0
bool sendHttpObject(KHttpRequest *rq, KHttpObject *obj) {
	rq->state = STATE_RECV;
	//assert(rq->op == STAGE_OP_SYNC);
	buff * send_buffer = NULL;//obj->container;
	buff *hdrs_to_send = NULL;//alloc_buff(CHUNK_SIZE); /* most headers fit this (?) */
	//bool send_unknow_length = false;
	INT64 send_len = 0;//obj->index.content_length;
	INT64 start = 0;
	unsigned this_send_len = 0;
	//INT64 body_size = 0;
	//	bool chunked_and_http_1_0=false;
	bool result = true;
	//	status_code = obj->data->status_code;
	INT64 content_len = 0;
	assert(!TEST(obj->index.flags,ANSW_CHUNKED));
	KMutex *lock = obj->getLock();
	lock->Lock();
#ifdef ENABLE_DISK_CACHE
	if (obj->data==NULL) {
		assert(TEST(obj->index.flags,FLAG_IN_DISK));
		KHttpObjectBody *data = new KHttpObjectBody();
		obj->data = data;
		if (!obj->swapin(data)) {
			SET(obj->index.flags,FLAG_DEAD|OBJ_INDEX_UPDATE);
			lock->Unlock();
			cache.dead(obj);
			//objList[obj->list_state].dead(obj);
			return send_error(rq, NULL, STATUS_SERVER_ERROR,
					"Cann't swap in object!");
		} else {
			SET(obj->index.flags,FLAG_IN_MEM);
			/////////[184]
				cache.getHash(obj->h)->incSize(obj->index.content_length);
			
		}
	}
#endif	
	send_buffer = obj->data->bodys;
	content_len = obj->index.content_length;
	lock->Unlock();
	send_len = content_len;
	KBuffer headerBuffer(2048);
	build_obj_header(rq, obj, content_len,start, send_len);
	hdrs_to_send = headerBuffer.stealBuffFast();
	result = send_buff(rq, hdrs_to_send) == STREAM_WRITE_SUCCESS;
	KBuffer::destroy(hdrs_to_send);
	if (TEST(obj->index.flags,FLAG_NO_BODY) || rq->meth == METH_HEAD || !result
			|| start == -1) {
		/*
		 * do not need send body
		 */
		return result;
	}
	while (send_buffer != NULL) {
		if ((INT64) send_buffer->used <= start) {
			start -= send_buffer->used;
			goto next_buffer;
		}
		//if (!send_unknow_length) {
		if (send_len <= 0){
				break;
		}
		if (send_len > (INT64) send_buffer->used - start) {
			this_send_len = send_buffer->used - (unsigned) start;
		} else {
			this_send_len = (int) send_len;
		}
		if (!rq->write_all(send_buffer->data + start, this_send_len)) {
			result = false;
			break;
		}
		//rq->send_size += this_send_len;
		start = 0;
		//if (!send_unknow_length){
		send_len -= this_send_len;
		//}
		next_buffer: send_buffer = send_buffer->next;
	}
	if (result) {
		assert(send_len==0);
	}
	return result;
}
예제 #13
0
unsigned KDnsCache::GetName(const char *name)
{
	struct hostent	he_b;
	int		he_errno=0;
	int		rc = 0;
	struct hostent	*he_x=NULL;
	struct hostent *h=NULL;
	unsigned long ip4_addr;
	DNSMAP_T::iterator it;
	bool allnum=true;
	for(int i=0;i<strlen(name);i++){
		if(name[i]!='.' && (name[i]<'0' || name[i]>'9')){
			allnum=false;
			break;
		}
	}
	if(allnum){
	//	printf("name=%s is allnum.\n",name);
		return (unsigned)inet_addr(name);
	//	return (unsigned)s.s_addr;
	}
	m_lock.Lock();
	it=m_dns.find(name);
	if(it!=m_dns.end()){
		ip4_addr=(*it).second.ip;
		m_lock.Unlock();
	//	printf("get name in cache:%s\n",name);
/*		if((*it).second.ip<=0){
		#ifdef HAVE_SYSLOG_H
			syslog(LOG_ERR,"[KDNSCACHE]name :%s in cache value=%d",name,(*it).second.ip);
		#endif	
		}
	*/		

		return ip4_addr;//(*it).second.ip;
	}
	m_lock.Unlock();
	//h=gethostbyname(name);
	//printf("call gethostbyname_r for look %s\n",name);
	DNS_T m_dns_tmp;
#ifdef  HAVE_GETHOSTBYNAME_R
	char	he_strb[2048];
	rc = gethostbyname_r(name, &he_b, he_strb, sizeof(he_strb),
				&he_x,
				&he_errno);
	if(rc!=0||he_errno!=0){
		
		syslog(LOG_ERR,"[KDNSCACHE]name :%s is error,rc=%d,he_errno=%d.\n",name,rc,he_errno);
	
		return 0;
	}
	m_dns_tmp.ip=*((unsigned long *)he_b.h_addr);
#else
#ifndef _WIN32
	m_gethostbyname_lock.Lock();
#endif
	h=gethostbyname(name);
	if(h==NULL){
		m_gethostbyname_lock.Unlock();	
		return 0;	
	}
	m_dns_tmp.ip=*((unsigned long *)h->h_addr);
#ifndef _WIN32
	m_gethostbyname_lock.Unlock();
#endif
#endif
	m_dns_tmp.create_time=time(NULL);
	m_lock.Lock();
	m_dns.insert(make_pair<string,DNS_T>(name,m_dns_tmp));
	m_lock.Unlock();
	return m_dns_tmp.ip;
err:
	m_lock.Unlock();
	return 0;
}
예제 #14
0
파일: KIDSequence.cpp 프로젝트: viticm/pap2
BOOL KIDSequence::GenerateID(const char cszSequenceName[], int nPrefetch, uint64_t* puRetID)
{
    BOOL                                            bResult     = false;    
    int                                             nRetCode    = false;
    KID_PREFETCH_INFO*                              pPrefetch   = NULL;
    MYSQL_RES*                                      pQueryRes   = NULL;
    uint64_t                                        uStartID    = 0;
    my_ulonglong                                    uRowCount   = 0;
    MYSQL_ROW                                       Row;
    char                                            szQuery[256];
    KID_PREFETCH_TABLE::iterator                    it;
    std::pair<KID_PREFETCH_TABLE::iterator, BOOL>   InsRet;

    assert(cszSequenceName);
    assert(puRetID);
    assert(m_pDBHandle);
    assert(nPrefetch >= 1);

    m_Mutex.Lock();
    
    it = m_PrefetchTable.find(cszSequenceName);
    if (it != m_PrefetchTable.end())
    {
        pPrefetch = &it->second;
    }
    else
    {
        InsRet = m_PrefetchTable.insert(std::make_pair(cszSequenceName, KID_PREFETCH_INFO()));
        KGLOG_PROCESS_ERROR(InsRet.second);
        
        pPrefetch = &InsRet.first->second;

        pPrefetch->uIDEnd   = 0;
        pPrefetch->uNextID  = 0;
    }

    if (pPrefetch->uNextID < pPrefetch->uIDEnd)
        goto Exit1;

    while(true)
    { 
        nRetCode = snprintf(
            szQuery,  
            sizeof(szQuery),
            "select %s from %s where %s = '%s'",
            SEQ_CURRENT_ID, SEQ_TABLE_NAME, SEQ_NAME,
            cszSequenceName
        );
        KGLOG_PROCESS_ERROR(nRetCode > 0 && nRetCode < (int)sizeof(szQuery));

        nRetCode = DoQuery(szQuery);
        KGLOG_PROCESS_ERROR(nRetCode);

        if (pQueryRes)
        {
            mysql_free_result(pQueryRes);
            pQueryRes = NULL;
        }

        pQueryRes = mysql_store_result(m_pDBHandle);
        KGLOG_PROCESS_ERROR(pQueryRes);

        uRowCount = mysql_num_rows(pQueryRes);
        if (uRowCount == 0)
        {
            nRetCode = snprintf(
                szQuery, sizeof(szQuery),
                "insert into %s (%s, %s) values('%s', 1)",
                SEQ_TABLE_NAME, SEQ_NAME, SEQ_CURRENT_ID,
                cszSequenceName
            );
            KGLOG_PROCESS_ERROR(nRetCode > 0 && nRetCode < (int)sizeof(szQuery));

            nRetCode = DoQuery(szQuery);
            KGLOG_PROCESS_ERROR(nRetCode);

            continue;
        }

        Row = mysql_fetch_row(pQueryRes);
        KGLOG_PROCESS_ERROR(Row);
        
        assert(Row[0]);

        uStartID = (uint64_t)strtoull(Row[0], NULL, 10);
        
        KGLOG_PROCESS_ERROR(uStartID < ULLONG_MAX - nPrefetch);

        nRetCode = snprintf(
            szQuery,
            sizeof(szQuery),
            "update %s set %s = %s + %d where %s = '%s' and %s = %llu",
            SEQ_TABLE_NAME, SEQ_CURRENT_ID, SEQ_CURRENT_ID, nPrefetch,
            SEQ_NAME, cszSequenceName, SEQ_CURRENT_ID, uStartID
        );
        KGLOG_PROCESS_ERROR(nRetCode > 0 && nRetCode < (int)sizeof(szQuery));

        nRetCode = DoQuery(szQuery);
        KGLOG_PROCESS_ERROR(nRetCode);

        nRetCode = (int)mysql_affected_rows(m_pDBHandle);
        if (nRetCode == 1)
            break;
    }
    
    pPrefetch->uIDEnd   = uStartID + nPrefetch;
    pPrefetch->uNextID  = uStartID;

Exit1:
    *puRetID = pPrefetch->uNextID++;

    bResult = true;
Exit0:
    if (pQueryRes)
    {
        mysql_free_result(pQueryRes);
        pQueryRes = NULL;
    }

    m_Mutex.Unlock();
    return bResult;
}