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