35 static int (*libc_epoll_create1)(
int flags) = NULL;
36 static int (*libc_epoll_ctl)(
int epfd,
int op,
int fd,
37 struct epoll_event *event) = NULL;
38 static int (*libc_epoll_wait)(
int epfd,
struct epoll_event *events,
39 int maxevents,
int timeout) = NULL;
40 static int (*libc_poll)(
struct pollfd *fds, nfds_t nfds,
int timeout);
41 static int (*libc_close)(
int fd);
42 static int (*libc_dup)(
int oldfd);
43 static int (*libc_dup2)(
int oldfd,
int newfd);
44 static int (*libc_dup3)(
int oldfd,
int newfd,
int flags);
46 static inline void ensure_init(
void);
49 int tas_libc_epoll_create1(
int flags)
52 return libc_epoll_create1(flags);
55 int tas_libc_epoll_ctl(
int epfd,
int op,
int fd,
56 struct epoll_event *event)
59 return libc_epoll_ctl(epfd, op, fd, event);
62 int tas_libc_epoll_wait(
int epfd,
struct epoll_event *events,
66 return libc_epoll_wait(epfd, events, maxevents, timeout);
69 int tas_libc_poll(
struct pollfd *fds, nfds_t nfds,
int timeout)
72 return libc_poll(fds, nfds, timeout);
75 int tas_libc_close(
int fd)
78 return libc_close(fd);
81 int tas_libc_dup(
int oldfd)
84 return libc_dup(oldfd);
87 int tas_libc_dup2(
int oldfd,
int newfd)
90 return libc_dup2(oldfd, newfd);
93 int tas_libc_dup3(
int oldfd,
int newfd,
int flags)
96 return libc_dup3(oldfd, newfd, flags);
103 static void *bind_symbol(
void *handle,
const char *sym)
106 if ((ptr = dlsym(handle, sym)) == NULL) {
107 fprintf(stderr,
"tas libc lookup: dlsym failed (%s)\n", sym);
113 static void init(
void)
117 if ((handle = dlopen(
"libc.so.6", RTLD_LAZY)) == NULL) {
118 perror(
"tas libc lookup: dlopen on libc failed");
122 libc_close = bind_symbol(handle,
"close");
123 libc_epoll_create1 = bind_symbol(handle,
"epoll_create1");
124 libc_epoll_ctl = bind_symbol(handle,
"epoll_ctl");
125 libc_epoll_wait = bind_symbol(handle,
"epoll_wait");
126 libc_poll = bind_symbol(handle,
"poll");
127 libc_dup = bind_symbol(handle,
"dup");
128 libc_dup2 = bind_symbol(handle,
"dup2");
129 libc_dup3 = bind_symbol(handle,
"dup3");
132 static inline void ensure_init(
void)
134 static volatile uint32_t init_cnt = 0;
135 static volatile uint8_t init_done = 0;
136 static __thread uint8_t in_init = 0;
138 if (init_done == 0) {
145 if (__sync_fetch_and_add(&init_cnt, 1) == 0) {
152 while (init_done == 0) {