• 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 // Also use SIGRTMAX instead of __SIGRTMAX, which is not defined by musl.
52 #if !defined (__BIONIC__)
53 static int sigWakeup = (SIGRTMAX - 2);
54 #else
55 static int sigWakeup = (__SIGRTMIN + 2);
56 #endif
57 
58 /*
59  * Close or dup2 a file descriptor ensuring that all threads blocked on
60  * the file descriptor are notified via a wakeup signal.
61  *
62  *      fd1 < 0    => close(fd2)
63  *      fd1 >= 0   => dup2(fd1, fd2)
64  *
65  * Returns -1 with errno set if operation fails.
66  */
closefd(int fd1,int fd2)67 static int closefd(int fd1, int fd2) {
68     int rv, orig_errno;
69 
70     AsynchronousCloseMonitor::signalBlockedThreads(fd2);
71 
72     /*
73      * And close/dup the file descriptor
74      * (restart if interrupted by signal)
75      */
76     do {
77       if (fd1 < 0) {
78         rv = close(fd2);
79       } else {
80         rv = dup2(fd1, fd2);
81       }
82     } while (rv == -1 && errno == EINTR);
83     return rv;
84 }
85 
86 /*
87  * Wrapper for dup2 - same semantics as dup2 system call except
88  * that any threads blocked in an I/O system call on fd2 will be
89  * preempted and return -1/EBADF;
90  */
NET_Dup2(int fd,int fd2)91 int NET_Dup2(int fd, int fd2) {
92     if (fd < 0) {
93         errno = EBADF;
94         return -1;
95     }
96     return closefd(fd, fd2);
97 }
98 
99 /*
100  * Wrapper for close - same semantics as close system call
101  * except that any threads blocked in an I/O on fd will be
102  * preempted and the I/O system call will return -1/EBADF.
103  */
NET_SocketClose(int fd)104 int NET_SocketClose(int fd) {
105     return closefd(-1, fd);
106 }
107 
108 /************** Basic I/O operations here ***************/
109 
110 /*
111  * Macro to perform a blocking IO operation. Restarts
112  * automatically if interrupted by signal (other than
113  * our wakeup signal)
114  */
115 #define BLOCKING_IO_RETURN_INT(FD, FUNC) {      \
116     int ret;                                    \
117     int _syscallErrno; \
118     do {                                        \
119         bool _wasSignaled; \
120         {                                       \
121             AsynchronousCloseMonitor _monitor(FD); \
122             ret = FUNC;                            \
123             _syscallErrno = errno; \
124             _wasSignaled = _monitor.wasSignaled(); \
125         } \
126         errno = _wasSignaled ? EBADF : _syscallErrno; \
127     } while (ret == -1 && errno == EINTR);      \
128     return ret;                                 \
129 }
130 
131 
NET_Read(int s,void * buf,size_t len)132 int NET_Read(int s, void* buf, size_t len) {
133     BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, 0) );
134 }
135 
NET_ReadV(int s,const struct iovec * vector,int count)136 int NET_ReadV(int s, const struct iovec * vector, int count) {
137     BLOCKING_IO_RETURN_INT( s, readv(s, vector, count) );
138 }
139 
NET_RecvFrom(int s,void * buf,int len,unsigned int flags,struct sockaddr * from,int * fromlen)140 int NET_RecvFrom(int s, void *buf, int len, unsigned int flags,
141        struct sockaddr *from, int *fromlen) {
142     socklen_t socklen = *fromlen;
143     BLOCKING_IO_RETURN_INT( s, recvfrom(s, buf, len, flags, from, &socklen) );
144     *fromlen = socklen;
145 }
146 
NET_Send(int s,void * msg,int len,unsigned int flags)147 int NET_Send(int s, void *msg, int len, unsigned int flags) {
148     BLOCKING_IO_RETURN_INT( s, send(s, msg, len, flags) );
149 }
150 
NET_WriteV(int s,const struct iovec * vector,int count)151 int NET_WriteV(int s, const struct iovec * vector, int count) {
152     BLOCKING_IO_RETURN_INT( s, writev(s, vector, count) );
153 }
154 
NET_SendTo(int s,const void * msg,int len,unsigned int flags,const struct sockaddr * to,int tolen)155 int NET_SendTo(int s, const void *msg, int len,  unsigned  int
156        flags, const struct sockaddr *to, int tolen) {
157     BLOCKING_IO_RETURN_INT( s, sendto(s, msg, len, flags, to, tolen) );
158 }
159 
NET_Accept(int s,struct sockaddr * addr,int * addrlen)160 int NET_Accept(int s, struct sockaddr *addr, int *addrlen) {
161     socklen_t socklen = *addrlen;
162     BLOCKING_IO_RETURN_INT( s, accept(s, addr, &socklen) );
163     *addrlen = socklen;
164 }
165 
NET_Connect(int s,struct sockaddr * addr,int addrlen)166 int NET_Connect(int s, struct sockaddr *addr, int addrlen) {
167     BLOCKING_IO_RETURN_INT( s, connect(s, addr, addrlen) );
168 }
169 
170 #ifndef USE_SELECT
NET_Poll(struct pollfd * ufds,unsigned int nfds,int timeout)171 int NET_Poll(struct pollfd *ufds, unsigned int nfds, int timeout) {
172     BLOCKING_IO_RETURN_INT( ufds[0].fd, poll(ufds, nfds, timeout) );
173 }
174 #else
NET_Select(int s,fd_set * readfds,fd_set * writefds,fd_set * exceptfds,struct timeval * timeout)175 int NET_Select(int s, fd_set *readfds, fd_set *writefds,
176                fd_set *exceptfds, struct timeval *timeout) {
177     BLOCKING_IO_RETURN_INT( s-1,
178                             select(s, readfds, writefds, exceptfds, timeout) );
179 }
180 #endif
181 
182 /*
183  * Wrapper for poll(s, timeout).
184  * Auto restarts with adjusted timeout if interrupted by
185  * signal other than our wakeup signal.
186  *
187  * If s < 0, exits early rather than delegating to poll().
188  * TODO: Investigate whether it'd be better to handle this
189  * case at the caller so that this function is never called
190  * for s < 0.
191  */
NET_Timeout(int s,long timeout)192 int NET_Timeout(int s, long timeout) {
193     long prevtime = 0, newtime;
194     struct timeval t;
195 
196     /*
197      * b/27763633
198      * Avoid blocking calls to poll() for invalid sockets, e.g. when
199      * called from PlainSocketImpl_socketAccept with fd == -1.
200      */
201     if (s < 0) {
202         errno = EBADF;
203         return -1;
204     }
205 
206     /*
207      * Pick up current time as may need to adjust timeout
208      */
209     if (timeout > 0) {
210         gettimeofday(&t, NULL);
211         prevtime = t.tv_sec * 1000  +  t.tv_usec / 1000;
212     }
213 
214     for(;;) {
215         struct pollfd pfd;
216         int rv;
217 
218         /*
219          * Poll the fd. If interrupted by our wakeup signal
220          * errno will be set to EBADF.
221          */
222         pfd.fd = s;
223         pfd.events = POLLIN | POLLERR;
224 
225         bool wasSignaled;
226         int syscallErrno;
227         {
228           AsynchronousCloseMonitor monitor(s);
229           rv = poll(&pfd, 1, timeout);
230           syscallErrno = errno;
231           wasSignaled = monitor.wasSignaled();
232         }
233         errno = wasSignaled ? EBADF : syscallErrno;
234 
235         /*
236          * If interrupted then adjust timeout. If timeout
237          * has expired return 0 (indicating timeout expired).
238          */
239         if (rv < 0 && errno == EINTR) {
240             if (timeout > 0) {
241                 gettimeofday(&t, NULL);
242                 newtime = t.tv_sec * 1000  +  t.tv_usec / 1000;
243                 timeout -= newtime - prevtime;
244                 if (timeout <= 0) {
245                     return 0;
246                 }
247                 prevtime = newtime;
248             }
249         } else {
250             return rv;
251         }
252 
253     }
254 }
255 
256 }
257