Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add conditional macro about macos #153

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 54 additions & 51 deletions co_hook_sys_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/

Expand Down Expand Up @@ -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");
Expand All @@ -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");

Expand All @@ -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");
*/

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
Expand All @@ -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 };
Expand All @@ -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 );
Expand All @@ -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 );
Expand All @@ -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 )
{
Expand All @@ -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;
Expand Down Expand Up @@ -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 );


Expand Down Expand Up @@ -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 );


Expand All @@ -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 );
Expand All @@ -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 );
Expand All @@ -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 )
{
Expand All @@ -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;
Expand All @@ -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 };
Expand All @@ -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);
Expand Down Expand Up @@ -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) );
}
Expand Down Expand Up @@ -744,7 +748,7 @@ int fcntl(int fildes, int cmd, ...)

struct stCoSysEnv_t
{
char *name;
char *name;
char *value;
};
struct stCoSysEnvArr_t
Expand All @@ -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 );
Expand All @@ -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 )
Expand Down Expand Up @@ -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();
}
Expand All @@ -938,18 +944,15 @@ 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);
}

return co_gethostbyname_r(name, __result_buf, __buf, __buflen, __result,
__h_errnop);
#endif
}
#endif

struct res_state_wrap
{
Expand All @@ -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();
}
Expand All @@ -976,7 +979,7 @@ extern "C"
}
}

struct hostbuf_wrap
struct hostbuf_wrap
{
struct hostent host;
char* buffer;
Expand Down Expand Up @@ -1010,16 +1013,16 @@ 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);
__co_hostbuf_wrap->iBufferSize = __co_hostbuf_wrap->iBufferSize * 2;
__co_hostbuf_wrap->buffer = (char*)malloc(__co_hostbuf_wrap->iBufferSize);
}

if (ret == 0 && (host == result))
if (ret == 0 && (host == result))
{
return host;
}
Expand Down
1 change: 1 addition & 0 deletions example_echosvr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@


#include "co_routine.h"
#include <string.h>

#include <stdio.h>
#include <stdlib.h>
Expand Down
Loading