Skip to content

Commit

Permalink
fix: fix hook poll bug for curl post request on the same fd
Browse files Browse the repository at this point in the history
  • Loading branch information
leiffyli committed Feb 18, 2019
1 parent 6d49ec7 commit 83d2343
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
44 changes: 39 additions & 5 deletions co_hook_sys_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <netdb.h>

#include <time.h>
#include <map>
#include "co_routine.h"
#include "co_routine_inner.h"
#include "co_routine_specific.h"
Expand Down Expand Up @@ -575,15 +576,48 @@ extern int co_poll_inner( stCoEpoll_t *ctx,struct pollfd fds[], nfds_t nfds, int

int poll(struct pollfd fds[], nfds_t nfds, int timeout)
{

HOOK_SYS_FUNC( poll );

if( !co_is_enable_sys_hook() )
{
return g_sys_poll_func( fds,nfds,timeout );
if (!co_is_enable_sys_hook() || timeout == 0) {
return g_sys_poll_func(fds, nfds, timeout);
}
pollfd *fds_merge = NULL;
nfds_t nfds_merge = 0;
std::map<int, int> m; // fd --> idx
std::map<int, int>::iterator it;
if (nfds > 1) {
fds_merge = (pollfd *)malloc(sizeof(pollfd) * nfds);
for (size_t i = 0; i < nfds; i++) {
if ((it = m.find(fds[i].fd)) == m.end()) {
fds_merge[nfds_merge] = fds[i];
m[fds[i].fd] = nfds_merge;
nfds_merge++;
} else {
int j = it->second;
fds_merge[j].events |= fds[i].events; // merge in j slot
}
}
}

return co_poll_inner( co_get_epoll_ct(),fds,nfds,timeout, g_sys_poll_func);
int ret = 0;
if (nfds_merge == nfds || nfds == 1) {
ret = co_poll_inner(co_get_epoll_ct(), fds, nfds, timeout, g_sys_poll_func);
} else {
ret = co_poll_inner(co_get_epoll_ct(), fds_merge, nfds_merge, timeout,
g_sys_poll_func);
if (ret > 0) {
for (size_t i = 0; i < nfds; i++) {
it = m.find(fds[i].fd);
if (it != m.end()) {
int j = it->second;
fds[i].revents = fds_merge[j].revents & fds[i].events;
}
}
}
}
free(fds_merge);
return ret;


}
int setsockopt(int fd, int level, int option_name,
Expand Down
2 changes: 1 addition & 1 deletion co_routine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ static unsigned long long GetTickMS()
#endif
}

/* no longer use
static pid_t GetPid()
{
static __thread pid_t pid = 0;
Expand Down Expand Up @@ -141,7 +142,6 @@ static pid_t GetPid()
return tid;
}
/*
static pid_t GetPid()
{
char **p = (char**)pthread_self();
Expand Down

0 comments on commit 83d2343

Please sign in to comment.