X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=lib%2Fnet.c;h=df2551f755b1fd1de150d65d5670720d71bf2fb9;hb=7c3d7b13860a5d725a300b71d273cdba5bcd8687;hp=48d0a5fec0b7e62a3977e2e54116d022a894ee09;hpb=31ff1f23dedbfe6e2dccf9d3c7c294a1cd761266;p=android-x86%2Fexternal-toybox.git diff --git a/lib/net.c b/lib/net.c index 48d0a5fe..df2551f7 100644 --- a/lib/net.c +++ b/lib/net.c @@ -5,6 +5,8 @@ int xsocket(int domain, int type, int protocol) int fd = socket(domain, type, protocol); if (fd < 0) perror_exit("socket %x %x", type, protocol); + fcntl(fd, F_SETFD, FD_CLOEXEC); + return fd; } @@ -35,10 +37,60 @@ int xconnect(char *host, char *port, int family, int socktype, int protocol, fd = (ai->ai_next ? socket : xsocket)(ai->ai_family, ai->ai_socktype, ai->ai_protocol); if (!connect(fd, ai->ai_addr, ai->ai_addrlen)) break; - else if (!ai2->ai_next) perror_exit("connect"); + else if (!ai->ai_next) perror_exit("connect"); close(fd); } freeaddrinfo(ai2); return fd; } + +int xpoll(struct pollfd *fds, int nfds, int timeout) +{ + int i; + + for (;;) { + if (0>(i = poll(fds, nfds, timeout))) { + if (toys.signal) return i; + if (errno != EINTR && errno != ENOMEM) perror_exit("xpoll"); + else if (timeout>0) timeout--; + } else return i; + } +} + +// Loop forwarding data from in1 to out1 and in2 to out2, handling +// half-connection shutdown. timeouts return if no data for X miliseconds. +// Returns 0: both closed, 1 shutdown_timeout, 2 timeout +int pollinate(int in1, int in2, int out1, int out2, int timeout, int shutdown_timeout) +{ + struct pollfd pollfds[2]; + int i, pollcount = 2; + + memset(pollfds, 0, 2*sizeof(struct pollfd)); + pollfds[0].events = pollfds[1].events = POLLIN; + pollfds[0].fd = in1; + pollfds[1].fd = in2; + + // Poll loop copying data from each fd to the other one. + for (;;) { + if (!xpoll(pollfds, pollcount, timeout)) return pollcount; + + for (i=0; i