From a07ffb80bb8c0957ad0ffde82bb3c9d7b9ddc1a5 Mon Sep 17 00:00:00 2001 From: wendajiang Date: Sun, 15 Nov 2020 21:17:25 +0800 Subject: [PATCH 1/3] add conditional macro about macos --- example_poll.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/example_poll.cpp b/example_poll.cpp index eb92f21..f085562 100644 --- a/example_poll.cpp +++ b/example_poll.cpp @@ -38,6 +38,11 @@ #include #endif +#ifdef __APPLE__ +#include +#endif + + using namespace std; struct task_t From 69101c375ecac438485a4f09548e6f503ff24269 Mon Sep 17 00:00:00 2001 From: johnwdjiang Date: Fri, 26 Feb 2021 15:47:00 +0800 Subject: [PATCH 2/3] add #if for gethostbyname_r --- co_hook_sys_call.cpp | 105 ++++++++++++++++++++++--------------------- 1 file changed, 54 insertions(+), 51 deletions(-) diff --git a/co_hook_sys_call.cpp b/co_hook_sys_call.cpp index d41e4d4..78e07cd 100644 --- a/co_hook_sys_call.cpp +++ b/co_hook_sys_call.cpp @@ -3,16 +3,16 @@ * Copyright (C) 2014 THL A29 Limited, a Tencent company. All rights reserved. * -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * -* Unless required by applicable law or agreed to in writing, -* software distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and * limitations under the License. */ @@ -99,7 +99,9 @@ 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); +#if defined( __LINUX__ ) 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); +#endif 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"); @@ -116,7 +118,7 @@ static recv_pfn_t g_sys_recv_func = (recv_pfn_t)dlsym(RTLD_NEXT,"recv"); static poll_pfn_t g_sys_poll_func = (poll_pfn_t)dlsym(RTLD_NEXT,"poll"); -static setsockopt_pfn_t g_sys_setsockopt_func +static setsockopt_pfn_t g_sys_setsockopt_func = (setsockopt_pfn_t)dlsym(RTLD_NEXT,"setsockopt"); static fcntl_pfn_t g_sys_fcntl_func = (fcntl_pfn_t)dlsym(RTLD_NEXT,"fcntl"); @@ -126,25 +128,27 @@ 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"); +#if defined( __LINUX__ ) static gethostbyname_r_pfn_t g_sys_gethostbyname_r_func = (gethostbyname_r_pfn_t)dlsym(RTLD_NEXT, "gethostbyname_r"); +#endif static __poll_pfn_t g_sys___poll_func = (__poll_pfn_t)dlsym(RTLD_NEXT, "__poll"); /* -static pthread_getspecific_pfn_t g_sys_pthread_getspecific_func +static pthread_getspecific_pfn_t g_sys_pthread_getspecific_func = (pthread_getspecific_pfn_t)dlsym(RTLD_NEXT,"pthread_getspecific"); -static pthread_setspecific_pfn_t g_sys_pthread_setspecific_func +static pthread_setspecific_pfn_t g_sys_pthread_setspecific_func = (pthread_setspecific_pfn_t)dlsym(RTLD_NEXT,"pthread_setspecific"); -static pthread_rwlock_rdlock_pfn_t g_sys_pthread_rwlock_rdlock_func +static pthread_rwlock_rdlock_pfn_t g_sys_pthread_rwlock_rdlock_func = (pthread_rwlock_rdlock_pfn_t)dlsym(RTLD_NEXT,"pthread_rwlock_rdlock"); -static pthread_rwlock_wrlock_pfn_t g_sys_pthread_rwlock_wrlock_func +static pthread_rwlock_wrlock_pfn_t g_sys_pthread_rwlock_wrlock_func = (pthread_rwlock_wrlock_pfn_t)dlsym(RTLD_NEXT,"pthread_rwlock_wrlock"); -static pthread_rwlock_unlock_pfn_t g_sys_pthread_rwlock_unlock_func +static pthread_rwlock_unlock_pfn_t g_sys_pthread_rwlock_unlock_func = (pthread_rwlock_unlock_pfn_t)dlsym(RTLD_NEXT,"pthread_rwlock_unlock"); */ @@ -211,7 +215,7 @@ static inline void free_by_fd( int fd ) if( lp ) { g_rpchook_socket_fd[ fd ] = NULL; - free(lp); + free(lp); } } return; @@ -233,7 +237,7 @@ int socket(int domain, int type, int protocol) rpchook_t *lp = alloc_by_fd( fd ); lp->domain = domain; - + fcntl( fd, F_SETFL, g_sys_fcntl_func(fd, F_GETFL,0 ) ); return fd; @@ -268,11 +272,11 @@ int connect(int fd, const struct sockaddr *address, socklen_t address_len) { memcpy( &(lp->dest),address,(int)address_len ); } - if( O_NONBLOCK & lp->user_flag ) + if( O_NONBLOCK & lp->user_flag ) { return ret; } - + if (!(ret < 0 && errno == EINPROGRESS)) { return ret; @@ -320,7 +324,7 @@ int connect(int fd, const struct sockaddr *address, socklen_t address_len) int close(int fd) { HOOK_SYS_FUNC( close ); - + if( !co_is_enable_sys_hook() ) { return g_sys_close_func( fd ); @@ -334,19 +338,19 @@ int close(int fd) ssize_t read( int fd, void *buf, size_t nbyte ) { HOOK_SYS_FUNC( read ); - + if( !co_is_enable_sys_hook() ) { return g_sys_read_func( fd,buf,nbyte ); } rpchook_t *lp = get_by_fd( fd ); - if( !lp || ( O_NONBLOCK & lp->user_flag ) ) + if( !lp || ( O_NONBLOCK & lp->user_flag ) ) { ssize_t ret = g_sys_read_func( fd,buf,nbyte ); return ret; } - int timeout = ( lp->read_timeout.tv_sec * 1000 ) + int timeout = ( lp->read_timeout.tv_sec * 1000 ) + ( lp->read_timeout.tv_usec / 1000 ); struct pollfd pf = { 0 }; @@ -364,12 +368,12 @@ ssize_t read( int fd, void *buf, size_t nbyte ) } return readret; - + } ssize_t write( int fd, const void *buf, size_t nbyte ) { HOOK_SYS_FUNC( write ); - + if( !co_is_enable_sys_hook() ) { return g_sys_write_func( fd,buf,nbyte ); @@ -382,7 +386,7 @@ ssize_t write( int fd, const void *buf, size_t nbyte ) return ret; } size_t wrotelen = 0; - int timeout = ( lp->write_timeout.tv_sec * 1000 ) + int timeout = ( lp->write_timeout.tv_sec * 1000 ) + ( lp->write_timeout.tv_usec / 1000 ); ssize_t writeret = g_sys_write_func( fd,(const char*)buf + wrotelen,nbyte - wrotelen ); @@ -394,7 +398,7 @@ ssize_t write( int fd, const void *buf, size_t nbyte ) if( writeret > 0 ) { - wrotelen += writeret; + wrotelen += writeret; } while( wrotelen < nbyte ) { @@ -405,7 +409,7 @@ ssize_t write( int fd, const void *buf, size_t nbyte ) poll( &pf,1,timeout ); writeret = g_sys_write_func( fd,(const char*)buf + wrotelen,nbyte - wrotelen ); - + if( writeret <= 0 ) { break; @@ -445,7 +449,7 @@ ssize_t sendto(int socket, const void *message, size_t length, ssize_t ret = g_sys_sendto_func( socket,message,length,flags,dest_addr,dest_len ); if( ret < 0 && EAGAIN == errno ) { - int timeout = ( lp->write_timeout.tv_sec * 1000 ) + int timeout = ( lp->write_timeout.tv_sec * 1000 ) + ( lp->write_timeout.tv_usec / 1000 ); @@ -476,7 +480,7 @@ ssize_t recvfrom(int socket, void *buffer, size_t length, return g_sys_recvfrom_func( socket,buffer,length,flags,address,address_len ); } - int timeout = ( lp->read_timeout.tv_sec * 1000 ) + int timeout = ( lp->read_timeout.tv_sec * 1000 ) + ( lp->read_timeout.tv_usec / 1000 ); @@ -492,7 +496,7 @@ ssize_t recvfrom(int socket, void *buffer, size_t length, ssize_t send(int socket, const void *buffer, size_t length, int flags) { HOOK_SYS_FUNC( send ); - + if( !co_is_enable_sys_hook() ) { return g_sys_send_func( socket,buffer,length,flags ); @@ -504,7 +508,7 @@ ssize_t send(int socket, const void *buffer, size_t length, int flags) return g_sys_send_func( socket,buffer,length,flags ); } size_t wrotelen = 0; - int timeout = ( lp->write_timeout.tv_sec * 1000 ) + int timeout = ( lp->write_timeout.tv_sec * 1000 ) + ( lp->write_timeout.tv_usec / 1000 ); ssize_t writeret = g_sys_send_func( socket,buffer,length,flags ); @@ -515,7 +519,7 @@ ssize_t send(int socket, const void *buffer, size_t length, int flags) if( writeret > 0 ) { - wrotelen += writeret; + wrotelen += writeret; } while( wrotelen < length ) { @@ -526,7 +530,7 @@ ssize_t send(int socket, const void *buffer, size_t length, int flags) poll( &pf,1,timeout ); writeret = g_sys_send_func( socket,(const char*)buffer + wrotelen,length - wrotelen,flags ); - + if( writeret <= 0 ) { break; @@ -543,18 +547,18 @@ ssize_t send(int socket, const void *buffer, size_t length, int flags) ssize_t recv( int socket, void *buffer, size_t length, int flags ) { HOOK_SYS_FUNC( recv ); - + if( !co_is_enable_sys_hook() ) { return g_sys_recv_func( socket,buffer,length,flags ); } rpchook_t *lp = get_by_fd( socket ); - if( !lp || ( O_NONBLOCK & lp->user_flag ) ) + if( !lp || ( O_NONBLOCK & lp->user_flag ) ) { return g_sys_recv_func( socket,buffer,length,flags ); } - int timeout = ( lp->read_timeout.tv_sec * 1000 ) + int timeout = ( lp->read_timeout.tv_sec * 1000 ) + ( lp->read_timeout.tv_usec / 1000 ); struct pollfd pf = { 0 }; @@ -572,7 +576,7 @@ ssize_t recv( int socket, void *buffer, size_t length, int flags ) } return readret; - + } extern int co_poll_inner( stCoEpoll_t *ctx,struct pollfd fds[], nfds_t nfds, int timeout, poll_pfn_t pollfunc); @@ -637,7 +641,7 @@ int setsockopt(int fd, int level, int option_name, if( lp && SOL_SOCKET == level ) { struct timeval *val = (struct timeval*)option_value; - if( SO_RCVTIMEO == option_name ) + if( SO_RCVTIMEO == option_name ) { memcpy( &lp->read_timeout,val,sizeof(*val) ); } @@ -744,7 +748,7 @@ int fcntl(int fildes, int cmd, ...) struct stCoSysEnv_t { - char *name; + char *name; char *value; }; struct stCoSysEnvArr_t @@ -754,7 +758,7 @@ struct stCoSysEnvArr_t }; static stCoSysEnvArr_t *dup_co_sysenv_arr( stCoSysEnvArr_t * arr ) { - stCoSysEnvArr_t *lp = (stCoSysEnvArr_t*)calloc( sizeof(stCoSysEnvArr_t),1 ); + stCoSysEnvArr_t *lp = (stCoSysEnvArr_t*)calloc( sizeof(stCoSysEnvArr_t),1 ); if( arr->cnt ) { lp->data = (stCoSysEnv_t*)calloc( sizeof(stCoSysEnv_t) * arr->cnt,1 ); @@ -766,12 +770,12 @@ static stCoSysEnvArr_t *dup_co_sysenv_arr( stCoSysEnvArr_t * arr ) static int co_sysenv_comp(const void *a, const void *b) { - return strcmp(((stCoSysEnv_t*)a)->name, ((stCoSysEnv_t*)b)->name); + return strcmp(((stCoSysEnv_t*)a)->name, ((stCoSysEnv_t*)b)->name); } static stCoSysEnvArr_t g_co_sysenv = { 0 }; - + void co_set_env_list( const char *name[],size_t cnt) { if( g_co_sysenv.data ) @@ -917,12 +921,14 @@ struct hostent *gethostbyname(const char *name) } + +#if defined( __LINUX__ ) 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; + static __thread clsCoMutex* tls_leaky_dns_lock = NULL; if(tls_leaky_dns_lock == NULL) { tls_leaky_dns_lock = new clsCoMutex(); } @@ -938,9 +944,6 @@ int gethostbyname_r(const char* __restrict name, 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); @@ -948,8 +951,8 @@ int gethostbyname_r(const char* __restrict name, return co_gethostbyname_r(name, __result_buf, __buf, __buflen, __result, __h_errnop); -#endif } +#endif struct res_state_wrap { @@ -959,11 +962,11 @@ CO_ROUTINE_SPECIFIC(res_state_wrap, __co_state_wrap); extern "C" { - res_state __res_state() + res_state __res_state() { HOOK_SYS_FUNC(__res_state); - if (!co_is_enable_sys_hook()) + if (!co_is_enable_sys_hook()) { return g_sys___res_state_func(); } @@ -976,7 +979,7 @@ extern "C" } } -struct hostbuf_wrap +struct hostbuf_wrap { struct hostent host; char* buffer; @@ -1010,8 +1013,8 @@ struct hostent *co_gethostbyname(const char *name) int *h_errnop = &(__co_hostbuf_wrap->host_errno); int ret = -1; - while (ret = gethostbyname_r(name, host, __co_hostbuf_wrap->buffer, - __co_hostbuf_wrap->iBufferSize, &result, h_errnop) == ERANGE && + while (ret = gethostbyname_r(name, host, __co_hostbuf_wrap->buffer, + __co_hostbuf_wrap->iBufferSize, &result, h_errnop) == ERANGE && *h_errnop == NETDB_INTERNAL ) { free(__co_hostbuf_wrap->buffer); @@ -1019,7 +1022,7 @@ struct hostent *co_gethostbyname(const char *name) __co_hostbuf_wrap->buffer = (char*)malloc(__co_hostbuf_wrap->iBufferSize); } - if (ret == 0 && (host == result)) + if (ret == 0 && (host == result)) { return host; } From 5e49159bb6aa4beb25fc0f708d6242e2754ba512 Mon Sep 17 00:00:00 2001 From: johnwdjiang Date: Fri, 26 Feb 2021 16:00:16 +0800 Subject: [PATCH 3/3] add include string.h --- example_echosvr.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/example_echosvr.cpp b/example_echosvr.cpp index 91131d9..59248f9 100644 --- a/example_echosvr.cpp +++ b/example_echosvr.cpp @@ -19,6 +19,7 @@ #include "co_routine.h" +#include #include #include