提交 dc6aafcc 编写于 作者: P princewen

Add hook for gethostbyname_r

上级 69339494
......@@ -35,7 +35,7 @@ else
LINKS += -g -L./lib -lcolib -lpthread -ldl
endif
COLIB_OBJS=co_epoll.o co_routine.o co_hook_sys_call.o coctx_swap.o coctx.o
COLIB_OBJS=co_epoll.o co_routine.o co_hook_sys_call.o coctx_swap.o coctx.o co_comm.o
#co_swapcontext.o
PROGS = colib example_poll example_echosvr example_echocli example_thread example_cond example_specific example_copystack example_closure example_setenv
......
#include "co_comm.h"
clsCoMutex::clsCoMutex() {
m_ptCondSignal = co_cond_alloc();
m_iWaitItemCnt = 0;
}
clsCoMutex::~clsCoMutex() { co_cond_free(m_ptCondSignal); }
void clsCoMutex::CoLock() {
if (m_iWaitItemCnt > 0) {
m_iWaitItemCnt++;
co_cond_timedwait(m_ptCondSignal, -1);
} else {
m_iWaitItemCnt++;
}
}
void clsCoMutex::CoUnLock() {
m_iWaitItemCnt--;
co_cond_signal(m_ptCondSignal);
}
#pragma once
#include "co_routine.h"
class clsCoMutex {
public:
clsCoMutex();
~clsCoMutex();
void CoLock();
void CoUnLock();
private:
stCoCond_t* m_ptCondSignal;
int m_iWaitItemCnt;
};
class clsSmartLock {
public:
clsSmartLock(clsCoMutex* m) {
m_ptMutex = m;
m_ptMutex->CoLock();
}
~clsSmartLock() { m_ptMutex->CoUnLock(); }
private:
clsCoMutex* m_ptMutex;
};
......@@ -45,6 +45,7 @@
#include "co_routine.h"
#include "co_routine_inner.h"
#include "co_routine_specific.h"
#include "co_comm.h"
typedef long long ll64_t;
......@@ -98,6 +99,7 @@ typedef char *(*getenv_pfn_t)(const char *name);
typedef hostent* (*gethostbyname_pfn_t)(const char *name);
typedef res_state (*__res_state_pfn_t)();
typedef int (*__poll_pfn_t)(struct pollfd fds[], nfds_t nfds, int timeout);
typedef int (*gethostbyname_r_pfn_t)(const char* __restrict name, struct hostent* __restrict __result_buf, char* __restrict __buf, size_t __buflen, struct hostent** __restrict __result, int* __restrict __h_errnop);
static socket_pfn_t g_sys_socket_func = (socket_pfn_t)dlsym(RTLD_NEXT,"socket");
static connect_pfn_t g_sys_connect_func = (connect_pfn_t)dlsym(RTLD_NEXT,"connect");
......@@ -124,6 +126,7 @@ static getenv_pfn_t g_sys_getenv_func = (getenv_pfn_t)dlsym(RTLD_NEXT,"getenv
static __res_state_pfn_t g_sys___res_state_func = (__res_state_pfn_t)dlsym(RTLD_NEXT,"__res_state");
static gethostbyname_pfn_t g_sys_gethostbyname_func = (gethostbyname_pfn_t)dlsym(RTLD_NEXT, "gethostbyname");
static gethostbyname_r_pfn_t g_sys_gethostbyname_r_func = (gethostbyname_r_pfn_t)dlsym(RTLD_NEXT, "gethostbyname_r");
static __poll_pfn_t g_sys___poll_func = (__poll_pfn_t)dlsym(RTLD_NEXT, "__poll");
......@@ -914,6 +917,39 @@ struct hostent *gethostbyname(const char *name)
}
int co_gethostbyname_r(const char* __restrict name,
struct hostent* __restrict __result_buf,
char* __restrict __buf, size_t __buflen,
struct hostent** __restrict __result,
int* __restrict __h_errnop) {
static __thread clsCoMutex* tls_leaky_dns_lock = NULL;
if(tls_leaky_dns_lock == NULL) {
tls_leaky_dns_lock = new clsCoMutex();
}
clsSmartLock auto_lock(tls_leaky_dns_lock);
return g_sys_gethostbyname_r_func(name, __result_buf, __buf, __buflen,
__result, __h_errnop);
}
int gethostbyname_r(const char* __restrict name,
struct hostent* __restrict __result_buf,
char* __restrict __buf, size_t __buflen,
struct hostent** __restrict __result,
int* __restrict __h_errnop) {
HOOK_SYS_FUNC(gethostbyname_r);
#if defined( __APPLE__ ) || defined( __FreeBSD__ )
return g_sys_gethostbyname_r_func( name );
#else
if (!co_is_enable_sys_hook()) {
return g_sys_gethostbyname_r_func(name, __result_buf, __buf, __buflen,
__result, __h_errnop);
}
return co_gethostbyname_r(name, __result_buf, __buf, __buflen, __result,
__h_errnop);
#endif
}
struct res_state_wrap
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册