1 #ifdef USE_PRAGMA_IDENT_HDR 2 #pragma ident "@(#)hpi_linux.hpp 1.18 07/05/05 17:04:34 JVM" 3 #endif 4 /* 5 * Copyright 1999-2005 Sun Microsystems, Inc. All Rights Reserved. 6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 7 * 8 * This code is free software; you can redistribute it and/or modify it 9 * under the terms of the GNU General Public License version 2 only, as 10 * published by the Free Software Foundation. 11 * 12 * This code is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 * version 2 for more details (a copy is included in the LICENSE file that 16 * accompanied this code). 17 * 18 * You should have received a copy of the GNU General Public License version 19 * 2 along with this work; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 * 22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 23 * CA 95054 USA or visit www.sun.com if you need additional information or 24 * have any questions. 25 * 26 */ 27 28 // 29 // Because the interruptible IO has been dropped for HotSpot/Linux, 30 // the following HPI interface is very different from HotSparc. 31 // 32 33 #include <unistd.h> 34 #include <sys/socket.h> 35 #include <sys/poll.h> 36 #include <sys/ioctl.h> 37 #include <netdb.h> 38 39 // HPI_FileInterface 40 41 inline int hpi::close(int fd) { 42 return ::close(fd); 43 } 44 45 inline size_t hpi::read(int fd, void *buf, unsigned int nBytes) { 46 size_t res; 47 RESTARTABLE( (size_t) ::read(fd, buf, (size_t) nBytes), res); 48 return res; 49 } 50 51 inline size_t hpi::write(int fd, const void *buf, unsigned int nBytes) { 52 size_t res; 53 RESTARTABLE((size_t) ::write(fd, buf, (size_t) nBytes), res); 54 return res; 55 } 56 57 58 // HPI_SocketInterface 59 60 inline int hpi::socket_close(int fd) { 61 return ::close(fd); 62 } 63 64 inline int hpi::socket(int domain, int type, int protocol) { 65 return ::socket(domain, type, protocol); 66 } 67 68 inline int hpi::recv(int fd, char *buf, int nBytes, int flags) { 69 RESTARTABLE_RETURN_INT(::recv(fd, buf, nBytes, (unsigned int) flags)); 70 } 71 72 inline int hpi::send(int fd, char *buf, int nBytes, int flags) { 73 RESTARTABLE_RETURN_INT(::send(fd, buf, nBytes, (unsigned int) flags)); 74 } 75 76 inline int hpi::raw_send(int fd, char *buf, int nBytes, int flags) { 77 return send(fd, buf, nBytes, flags); 78 } 79 80 inline int hpi::timeout(int fd, long timeout) { 81 julong prevtime,newtime; 82 struct timeval t; 83 84 gettimeofday(&t, NULL); 85 prevtime = ((julong)t.tv_sec * 1000) + t.tv_usec / 1000; 86 87 for(;;) { 88 struct pollfd pfd; 89 90 pfd.fd = fd; 91 pfd.events = POLLIN | POLLERR; 92 93 int res = ::poll(&pfd, 1, timeout); 94 95 if (res == OS_ERR && errno == EINTR) { 96 97 // On Linux any value < 0 means "forever" 98 99 if(timeout >= 0) { 100 gettimeofday(&t, NULL); 101 newtime = ((julong)t.tv_sec * 1000) + t.tv_usec / 1000; 102 timeout -= newtime - prevtime; 103 if(timeout <= 0) 104 return OS_OK; 105 prevtime = newtime; 106 } 107 } else 108 return res; 109 } 110 } 111 112 inline int hpi::listen(int fd, int count) { 113 return ::listen(fd, count); 114 } 115 116 inline int hpi::connect(int fd, struct sockaddr *him, int len) { 117 RESTARTABLE_RETURN_INT(::connect(fd, him, len)); 118 } 119 120 inline int hpi::accept(int fd, struct sockaddr *him, int *len) { 121 // This cast is from int to unsigned int on linux. Since we 122 // only pass the parameter "len" around the vm and don't try to 123 // fetch it's value, this cast is safe for now. The java.net group 124 // may need and want to change this interface someday if socklen_t goes 125 // to 64 bits on some platform that we support. 126 // Linux doc says this can't return EINTR, unlike accept() on Solaris 127 128 return ::accept(fd, him, (socklen_t *)len); 129 } 130 131 inline int hpi::recvfrom(int fd, char *buf, int nBytes, int flags, 132 sockaddr *from, int *fromlen) { 133 RESTARTABLE_RETURN_INT(::recvfrom(fd, buf, nBytes, (unsigned int) flags, from, (socklen_t *)fromlen)); 134 } 135 136 inline int hpi::sendto(int fd, char *buf, int len, int flags, 137 struct sockaddr *to, int tolen) { 138 RESTARTABLE_RETURN_INT(::sendto(fd, buf, len, (unsigned int) flags, to, tolen)); 139 } 140 141 inline int hpi::socket_available(int fd, jint *pbytes) { 142 // Linux doc says EINTR not returned, unlike Solaris 143 int ret = ::ioctl(fd, FIONREAD, pbytes); 144 145 //%% note ioctl can return 0 when successful, JVM_SocketAvailable 146 // is expected to return 0 on failure and 1 on success to the jdk. 147 return (ret < 0) ? 0 : 1; 148 } 149 150 151 // following methods have been updated to avoid problems in 152 // hpi's sockets calls based on sys_api_td.c (JDK1.3) 153 154 /* 155 HPIDECL(socket_shutdown, "socket_shutdown", _socket, SocketShutdown, 156 int, "%d", 157 (int fd, int howto), 158 ("fd = %d, howto = %d", fd, howto), 159 (fd, howto)); 160 */ 161 inline int hpi::socket_shutdown(int fd, int howto){ 162 return ::shutdown(fd, howto); 163 } 164 165 /* 166 HPIDECL(bind, "bind", _socket, Bind, 167 int, "%d", 168 (int fd, struct sockaddr *him, int len), 169 ("fd = %d, him = %p, len = %d", 170 fd, him, len), 171 (fd, him, len)); 172 */ 173 inline int hpi::bind(int fd, struct sockaddr *him, int len){ 174 return ::bind(fd, him, len); 175 } 176 177 /* 178 HPIDECL(get_sock_name, "get_sock_name", _socket, GetSocketName, 179 int, "%d", 180 (int fd, struct sockaddr *him, int *len), 181 ("fd = %d, him = %p, len = %p", 182 fd, him, len), 183 (fd, him, len)); 184 */ 185 inline int hpi::get_sock_name(int fd, struct sockaddr *him, int *len){ 186 return ::getsockname(fd, him, (socklen_t *)len); 187 } 188 189 /* 190 HPIDECL(get_host_name, "get_host_name", _socket, GetHostName, int, "%d", 191 (char *hostname, int namelen), 192 ("hostname = %p, namelen = %d", 193 hostname, namelen), 194 (hostname, namelen)); 195 */ 196 inline int hpi::get_host_name(char* name, int namelen){ 197 return ::gethostname(name, namelen); 198 } 199 200 /* 201 HPIDECL(get_sock_opt, "get_sock_opt", _socket, SocketGetOption, int, "%d", 202 (int fd, int level, int optname, char *optval, int* optlen), 203 ("fd = %d, level = %d, optname = %d, optval = %p, optlen = %p", 204 fd, level, optname, optval, optlen), 205 (fd, level, optname, optval, optlen)); 206 */ 207 inline int hpi::get_sock_opt(int fd, int level, int optname, 208 char *optval, int* optlen){ 209 return ::getsockopt(fd, level, optname, optval, (socklen_t *)optlen); 210 } 211 212 /* 213 HPIDECL(set_sock_opt, "set_sock_opt", _socket, SocketSetOption, int, "%d", 214 (int fd, int level, int optname, const char *optval, int optlen), 215 ("fd = %d, level = %d, optname = %d, optval = %p, optlen = %d", 216 fd, level, optname, optval, optlen), 217 (fd, level, optname, optval, optlen)); 218 */ 219 inline int hpi::set_sock_opt(int fd, int level, int optname, 220 const char *optval, int optlen){ 221 return ::setsockopt(fd, level, optname, optval, optlen); 222 } 223 224 225 // Reconciliation History 226 // hpi_solaris.hpp 1.9 99/08/30 16:31:23 227 // End