src/os/linux/vm/hpi_linux.hpp

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 435
a61af66fc99e
child 657
2a1a77d3458f
permissions
-rw-r--r--

Initial load

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

mercurial