• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <signal.h>
29 #include <pthread.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <sys/time.h>
33 #include <sys/resource.h>
34 #include <sys/uio.h>
35 #include <unistd.h>
36 #include <errno.h>
37 
38 // Android-changed: Fix poll.h include location
39 // #include <sys/poll.h>
40 #include <poll.h>
41 
42 #include <AsynchronousCloseMonitor.h>
43 
44 extern "C" {
45 
46 /*
47  * Signal to unblock thread
48  */
49 // Android-changed: Bionic (and AsynchronousCloseMonitor) expects libcore to use
50 // __SIGRTMIN + 2, not __SIGRTMAX - 2
51 static int sigWakeup = (__SIGRTMIN + 2);
52 
53 /*
54  * Close or dup2 a file descriptor ensuring that all threads blocked on
55  * the file descriptor are notified via a wakeup signal.
56  *
57  *      fd1 < 0    => close(fd2)
58  *      fd1 >= 0   => dup2(fd1, fd2)
59  *
60  * Returns -1 with errno set if operation fails.
61  */
closefd(int fd1,int fd2)62 static int closefd(int fd1, int fd2) {
63     int rv, orig_errno;
64 
65     AsynchronousCloseMonitor::signalBlockedThreads(fd2);
66 
67     /*
68      * And close/dup the file descriptor
69      * (restart if interrupted by signal)
70      */
71     do {
72       if (fd1 < 0) {
73         rv = close(fd2);
74       } else {
75         rv = dup2(fd1, fd2);
76       }
77     } while (rv == -1 && errno == EINTR);
78     return rv;
79 }
80 
81 /*
82  * Wrapper for dup2 - same semantics as dup2 system call except
83  * that any threads blocked in an I/O system call on fd2 will be
84  * preempted and return -1/EBADF;
85  */
NET_Dup2(int fd,int fd2)86 int NET_Dup2(int fd, int fd2) {
87     if (fd < 0) {
88         errno = EBADF;
89         return -1;
90     }
91     return closefd(fd, fd2);
92 }
93 
94 /*
95  * Wrapper for close - same semantics as close system call
96  * except that any threads blocked in an I/O on fd will be
97  * preempted and the I/O system call will return -1/EBADF.
98  */
NET_SocketClose(int fd)99 int NET_SocketClose(int fd) {
100     return closefd(-1, fd);
101 }
102 
103 /************** Basic I/O operations here ***************/
104 
105 /*
106  * Macro to perform a blocking IO operation. Restarts
107  * automatically if interrupted by signal (other than
108  * our wakeup signal)
109  */
110 #define BLOCKING_IO_RETURN_INT(FD, FUNC) {      \
111     int ret;                                    \
112     int _syscallErrno; \
113     do {                                        \
114         bool _wasSignaled; \
115         {                                       \
116             AsynchronousCloseMonitor _monitor(FD); \
117             ret = FUNC;                            \
118             _syscallErrno = errno; \
119             _wasSignaled = _monitor.wasSignaled(); \
120         } \
121         errno = _wasSignaled ? EBADF : _syscallErrno; \
122     } while (ret == -1 && errno == EINTR);      \
123     return ret;                                 \
124 }
125 
126 
NET_Read(int s,void * buf,size_t len)127 int NET_Read(int s, void* buf, size_t len) {
128     BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, 0) );
129 }
130 
NET_ReadV(int s,const struct iovec * vector,int count)131 int NET_ReadV(int s, const struct iovec * vector, int count) {
132     BLOCKING_IO_RETURN_INT( s, readv(s, vector, count) );
133 }
134 
NET_RecvFrom(int s,void * buf,int len,unsigned int flags,struct sockaddr * from,int * fromlen)135 int NET_RecvFrom(int s, void *buf, int len, unsigned int flags,
136        struct sockaddr *from, int *fromlen) {
137     socklen_t socklen = *fromlen;
138     BLOCKING_IO_RETURN_INT( s, recvfrom(s, buf, len, flags, from, &socklen) );
139     *fromlen = socklen;
140 }
141 
NET_Send(int s,void * msg,int len,unsigned int flags)142 int NET_Send(int s, void *msg, int len, unsigned int flags) {
143     BLOCKING_IO_RETURN_INT( s, send(s, msg, len, flags) );
144 }
145 
NET_WriteV(int s,const struct iovec * vector,int count)146 int NET_WriteV(int s, const struct iovec * vector, int count) {
147     BLOCKING_IO_RETURN_INT( s, writev(s, vector, count) );
148 }
149 
NET_SendTo(int s,const void * msg,int len,unsigned int flags,const struct sockaddr * to,int tolen)150 int NET_SendTo(int s, const void *msg, int len,  unsigned  int
151        flags, const struct sockaddr *to, int tolen) {
152     BLOCKING_IO_RETURN_INT( s, sendto(s, msg, len, flags, to, tolen) );
153 }
154 
NET_Accept(int s,struct sockaddr * addr,int * addrlen)155 int NET_Accept(int s, struct sockaddr *addr, int *addrlen) {
156     socklen_t socklen = *addrlen;
157     BLOCKING_IO_RETURN_INT( s, accept(s, addr, &socklen) );
158     *addrlen = socklen;
159 }
160 
NET_Connect(int s,struct sockaddr * addr,int addrlen)161 int NET_Connect(int s, struct sockaddr *addr, int addrlen) {
162     BLOCKING_IO_RETURN_INT( s, connect(s, addr, addrlen) );
163 }
164 
165 #ifndef USE_SELECT
NET_Poll(struct pollfd * ufds,unsigned int nfds,int timeout)166 int NET_Poll(struct pollfd *ufds, unsigned int nfds, int timeout) {
167     BLOCKING_IO_RETURN_INT( ufds[0].fd, poll(ufds, nfds, timeout) );
168 }
169 #else
NET_Select(int s,fd_set * readfds,fd_set * writefds,fd_set * exceptfds,struct timeval * timeout)170 int NET_Select(int s, fd_set *readfds, fd_set *writefds,
171                fd_set *exceptfds, struct timeval *timeout) {
172     BLOCKING_IO_RETURN_INT( s-1,
173                             select(s, readfds, writefds, exceptfds, timeout) );
174 }
175 #endif
176 
177 /*
178  * Wrapper for poll(s, timeout).
179  * Auto restarts with adjusted timeout if interrupted by
180  * signal other than our wakeup signal.
181  *
182  * If s < 0, exits early rather than delegating to poll().
183  * TODO: Investigate whether it'd be better to handle this
184  * case at the caller so that this function is never called
185  * for s < 0.
186  */
NET_Timeout(int s,long timeout)187 int NET_Timeout(int s, long timeout) {
188     long prevtime = 0, newtime;
189     struct timeval t;
190 
191     /*
192      * b/27763633
193      * Avoid blocking calls to poll() for invalid sockets, e.g. when
194      * called from PlainSocketImpl_socketAccept with fd == -1.
195      */
196     if (s < 0) {
197         errno = EBADF;
198         return -1;
199     }
200 
201     /*
202      * Pick up current time as may need to adjust timeout
203      */
204     if (timeout > 0) {
205         gettimeofday(&t, NULL);
206         prevtime = t.tv_sec * 1000  +  t.tv_usec / 1000;
207     }
208 
209     for(;;) {
210         struct pollfd pfd;
211         int rv;
212 
213         /*
214          * Poll the fd. If interrupted by our wakeup signal
215          * errno will be set to EBADF.
216          */
217         pfd.fd = s;
218         pfd.events = POLLIN | POLLERR;
219 
220         bool wasSignaled;
221         int syscallErrno;
222         {
223           AsynchronousCloseMonitor monitor(s);
224           rv = poll(&pfd, 1, timeout);
225           syscallErrno = errno;
226           wasSignaled = monitor.wasSignaled();
227         }
228         errno = wasSignaled ? EBADF : syscallErrno;
229 
230         /*
231          * If interrupted then adjust timeout. If timeout
232          * has expired return 0 (indicating timeout expired).
233          */
234         if (rv < 0 && errno == EINTR) {
235             if (timeout > 0) {
236                 gettimeofday(&t, NULL);
237                 newtime = t.tv_sec * 1000  +  t.tv_usec / 1000;
238                 timeout -= newtime - prevtime;
239                 if (timeout <= 0) {
240                     return 0;
241                 }
242                 prevtime = newtime;
243             }
244         } else {
245             return rv;
246         }
247 
248     }
249 }
250 
251 }
252