1 /*
2 * Copyright (c) 1997, 2012, 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
676 len = SOCKADDR_LEN;
677
678 if (IS_NULL(fdObj)) {
679 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
680 "Socket closed");
681 return;
682 } else {
683 fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
684 }
685 if (IS_NULL(socket)) {
686 JNU_ThrowNullPointerException(env, "socket is null");
687 return;
688 }
689
690 /*
691 * accept connection but ignore ECONNABORTED indicating that
692 * connection was eagerly accepted by the OS but was reset
693 * before accept() was called.
694 *
695 * If accept timeout in place and timeout is adjusted with
696 * each ECONNABORTED or EWOULDBLOCK to ensure that semantics
697 * of timeout are preserved.
698 */
699 for (;;) {
700 int ret;
701
702 /* first usage pick up current time */
703 if (prevTime == 0 && timeout > 0) {
704 prevTime = JVM_CurrentTimeMillis(env, 0);
705 }
706
707 /* passing a timeout of 0 to poll will return immediately,
708 but in the case of ServerSocket 0 means infinite. */
709 if (timeout <= 0) {
710 ret = NET_Timeout(fd, -1);
711 } else {
712 ret = NET_Timeout(fd, timeout);
713 }
714 if (ret == 0) {
715 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
716 "Accept timed out");
717 return;
721 } else if (errno == ENOMEM) {
722 JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed");
723 } else {
724 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Accept failed");
725 }
726 return;
727 } else if (ret == JVM_IO_INTR) {
728 JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
729 "operation interrupted");
730 return;
731 }
732
733 newfd = NET_Accept(fd, (struct sockaddr *)&him, (jint*)&len);
734
735 /* connection accepted */
736 if (newfd >= 0) {
737 SET_BLOCKING(newfd);
738 break;
739 }
740
741 /* non (ECONNABORTED or EWOULDBLOCK) error */
742 if (!(errno == ECONNABORTED || errno == EWOULDBLOCK)) {
743 break;
744 }
745
746 /* ECONNABORTED or EWOULDBLOCK error so adjust timeout if there is one. */
747 if (timeout) {
748 jlong currTime = JVM_CurrentTimeMillis(env, 0);
749 timeout -= (currTime - prevTime);
750
751 if (timeout <= 0) {
752 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
753 "Accept timed out");
754 return;
755 }
756 prevTime = currTime;
757 }
758 }
759
760 if (newfd < 0) {
761 if (newfd == -2) {
762 JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
763 "operation interrupted");
764 } else {
765 if (errno == EINVAL) {
766 errno = EBADF;
|
1 /*
2 * Copyright (c) 1997, 2018, 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
676 len = SOCKADDR_LEN;
677
678 if (IS_NULL(fdObj)) {
679 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
680 "Socket closed");
681 return;
682 } else {
683 fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
684 }
685 if (IS_NULL(socket)) {
686 JNU_ThrowNullPointerException(env, "socket is null");
687 return;
688 }
689
690 /*
691 * accept connection but ignore ECONNABORTED indicating that
692 * connection was eagerly accepted by the OS but was reset
693 * before accept() was called.
694 *
695 * If accept timeout in place and timeout is adjusted with
696 * each ECONNABORTED or EWOULDBLOCK or EAGAIN to ensure that
697 * semantics of timeout are preserved.
698 */
699 for (;;) {
700 int ret;
701
702 /* first usage pick up current time */
703 if (prevTime == 0 && timeout > 0) {
704 prevTime = JVM_CurrentTimeMillis(env, 0);
705 }
706
707 /* passing a timeout of 0 to poll will return immediately,
708 but in the case of ServerSocket 0 means infinite. */
709 if (timeout <= 0) {
710 ret = NET_Timeout(fd, -1);
711 } else {
712 ret = NET_Timeout(fd, timeout);
713 }
714 if (ret == 0) {
715 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
716 "Accept timed out");
717 return;
721 } else if (errno == ENOMEM) {
722 JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed");
723 } else {
724 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Accept failed");
725 }
726 return;
727 } else if (ret == JVM_IO_INTR) {
728 JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
729 "operation interrupted");
730 return;
731 }
732
733 newfd = NET_Accept(fd, (struct sockaddr *)&him, (jint*)&len);
734
735 /* connection accepted */
736 if (newfd >= 0) {
737 SET_BLOCKING(newfd);
738 break;
739 }
740
741 /* non (ECONNABORTED or EWOULDBLOCK or EAGAIN) error */
742 if (!(errno == ECONNABORTED || errno == EWOULDBLOCK || errno == EAGAIN)) {
743 break;
744 }
745
746 /* ECONNABORTED or EWOULDBLOCK or EAGAIN error so adjust timeout if there is one. */
747 if (timeout) {
748 jlong currTime = JVM_CurrentTimeMillis(env, 0);
749 timeout -= (currTime - prevTime);
750
751 if (timeout <= 0) {
752 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
753 "Accept timed out");
754 return;
755 }
756 prevTime = currTime;
757 }
758 }
759
760 if (newfd < 0) {
761 if (newfd == -2) {
762 JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
763 "operation interrupted");
764 } else {
765 if (errno == EINVAL) {
766 errno = EBADF;
|