31 #include <sys/eventfd.h> 36 #define MAXSOCK 1024 * 1024 54 int flextcp_fd_init(
void)
60 int flextcp_fd_salloc(
struct socket **ps)
65 if ((s = calloc(1,
sizeof(*s))) == NULL) {
71 if ((fd = eventfd(0, 0)) < 0) {
84 s->type = SOCK_SOCKET;
89 fhs[fd].type = FH_SOCKET;
96 int flextcp_fd_slookup(
int fd,
struct socket **ps)
100 if (fd >= MAXSOCK || fhs[fd].type != FH_SOCKET) {
111 int flextcp_fd_ealloc(
struct epoll **pe,
int fd)
121 assert(fhs[fd].type == FH_UNUSED);
123 if ((e = calloc(1,
sizeof(*e))) == NULL) {
132 fhs[fd].type = FH_EPOLL;
139 int flextcp_fd_elookup(
int fd,
struct epoll **pe)
143 if (fd >= MAXSOCK || fhs[fd].type != FH_EPOLL) {
154 void flextcp_fd_srelease(
int fd,
struct socket *s)
159 void flextcp_fd_erelease(
int fd,
struct epoll *e)
164 void flextcp_fd_close(
int fd)
166 assert(fhs[fd].type == FH_SOCKET || fhs[fd].type == FH_EPOLL);
167 if (fhs[fd].type == FH_SOCKET) {
168 fhs[fd].data.s->refcnt--;
169 fhs[fd].data.s = NULL;
170 }
else if (fhs[fd].type == FH_EPOLL) {
171 fhs[fd].data.e->refcnt--;
172 fhs[fd].data.e = NULL;
174 fprintf(stderr,
"flextcp_fd_close: trying to close non-opened tas fd\n");
178 fhs[fd].type = FH_UNUSED;
185 static inline int internal_dup3(
int oldfd,
int newfd,
int flags)
192 if (newfd >= MAXSOCK) {
193 fprintf(stderr,
"tas_dup: failed because new fd is larger than MAXSOCK\n");
198 if (fhs[newfd].type == FH_SOCKET) {
199 s = fhs[newfd].data.s;
209 fhs[newfd].data.s = NULL;
210 fhs[newfd].type = FH_UNUSED;
211 }
else if (fhs[newfd].type == FH_EPOLL) {
212 ep = fhs[newfd].data.e;
218 flextcp_epoll_destroy(ep);
222 fhs[newfd].data.e = NULL;
223 fhs[newfd].type = FH_UNUSED;
227 if (flextcp_fd_slookup(oldfd, &s) == 0) {
229 fhs[newfd].type = FH_SOCKET;
230 fhs[newfd].data.s = s;
234 flextcp_fd_srelease(oldfd, s);
235 }
else if (flextcp_fd_elookup(oldfd, &ep) == 0) {
237 fhs[newfd].type = FH_EPOLL;
238 fhs[newfd].data.e = ep;
242 flextcp_fd_srelease(oldfd, s);
248 int tas_dup(
int oldfd)
253 newfd = tas_libc_dup(oldfd);
257 return internal_dup3(oldfd, newfd, 0);
260 int tas_dup2(
int oldfd,
int newfd)
263 newfd = tas_libc_dup2(oldfd, newfd);
271 return internal_dup3(oldfd, newfd, 0);
274 int tas_dup3(
int oldfd,
int newfd,
int flags)
277 newfd = tas_libc_dup3(oldfd, newfd, flags);
281 return internal_dup3(oldfd, newfd, flags);