1 /*
   2  * Copyright (c) 1998, 2010, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef OS_SOLARIS_VM_HPI_SOLARIS_HPP
  26 #define OS_SOLARIS_VM_HPI_SOLARIS_HPP
  27 
  28 //
  29 // Parts of the HPI interface for which the HotSparc does not use the
  30 // HPI (because the interruptible IO mechanims used are different).
  31 //
  32 
  33 #include <sys/socket.h>
  34 #include <sys/poll.h>
  35 #include <sys/filio.h>
  36 #include <unistd.h>
  37 #include <netdb.h>
  38 #include <setjmp.h>
  39 
  40 // HPI_FileInterface
  41 
  42 // Many system calls can be interrupted by signals and must be restarted.
  43 // Restart support was added without disturbing the extent of thread
  44 // interruption support.
  45 
  46 inline int    hpi::close(int fd) {
  47   RESTARTABLE_RETURN_INT(::close(fd));
  48 }
  49 
  50 inline size_t hpi::read(int fd, void *buf, unsigned int nBytes) {
  51   INTERRUPTIBLE_RETURN_INT(::read(fd, buf, nBytes), os::Solaris::clear_interrupted);
  52 }
  53 
  54 inline size_t hpi::write(int fd, const void *buf, unsigned int nBytes) {
  55   INTERRUPTIBLE_RETURN_INT(::write(fd, buf, nBytes), os::Solaris::clear_interrupted);
  56 }
  57 
  58 
  59 // HPI_SocketInterface
  60 
  61 inline int    hpi::socket_close(int fd) {
  62   RESTARTABLE_RETURN_INT(::close(fd));
  63 }
  64 
  65 inline int    hpi::socket(int domain, int type, int protocol) {
  66   return ::socket(domain, type, protocol);
  67 }
  68 
  69 inline int    hpi::recv(int fd, char *buf, int nBytes, int flags) {
  70   INTERRUPTIBLE_RETURN_INT(::recv(fd, buf, nBytes, flags), os::Solaris::clear_interrupted);
  71 }
  72 
  73 inline int    hpi::send(int fd, char *buf, int nBytes, int flags) {
  74   INTERRUPTIBLE_RETURN_INT(::send(fd, buf, nBytes, flags), os::Solaris::clear_interrupted);
  75 }
  76 
  77 inline int    hpi::raw_send(int fd, char *buf, int nBytes, int flags) {
  78   RESTARTABLE_RETURN_INT(::send(fd, buf, nBytes, flags));
  79 }
  80 
  81 // As both poll and select can be interrupted by signals, we have to be
  82 // prepared to restart the system call after updating the timeout, unless
  83 // a poll() is done with timeout == -1, in which case we repeat with this
  84 // "wait forever" value.
  85 
  86 inline int    hpi::timeout(int fd, long timeout) {
  87   int res;
  88   struct timeval t;
  89   julong prevtime, newtime;
  90   static const char* aNull = 0;
  91 
  92   struct pollfd pfd;
  93   pfd.fd = fd;
  94   pfd.events = POLLIN;
  95 
  96   gettimeofday(&t, &aNull);
  97   prevtime = ((julong)t.tv_sec * 1000)  +  t.tv_usec / 1000;
  98 
  99   for(;;) {
 100     INTERRUPTIBLE_NORESTART(::poll(&pfd, 1, timeout), res, os::Solaris::clear_interrupted);
 101     if(res == OS_ERR && errno == EINTR) {
 102         if(timeout != -1) {
 103             gettimeofday(&t, &aNull);
 104             newtime = ((julong)t.tv_sec * 1000)  +  t.tv_usec /1000;
 105             timeout -= newtime - prevtime;
 106             if(timeout <= 0)
 107               return OS_OK;
 108             prevtime = newtime;
 109         }
 110     } else
 111       return res;
 112   }
 113 }
 114 
 115 inline int    hpi::listen(int fd, int count) {
 116   if (fd < 0)
 117     return OS_ERR;
 118 
 119   return ::listen(fd, count);
 120 }
 121 
 122 inline int
 123 hpi::connect(int fd, struct sockaddr *him, int len) {
 124   do {
 125     int _result;
 126     INTERRUPTIBLE_NORESTART(::connect(fd, him, len), _result,
 127                             os::Solaris::clear_interrupted);
 128 
 129     // Depending on when thread interruption is reset, _result could be
 130     // one of two values when errno == EINTR
 131 
 132     if (((_result == OS_INTRPT) || (_result == OS_ERR)) && (errno == EINTR)) {
 133       /* restarting a connect() changes its errno semantics */
 134       INTERRUPTIBLE(::connect(fd, him, len), _result,
 135                       os::Solaris::clear_interrupted);
 136       /* undo these changes */
 137       if (_result == OS_ERR) {
 138         if (errno == EALREADY) errno = EINPROGRESS; /* fall through */
 139         else if (errno == EISCONN) { errno = 0; return OS_OK; }
 140       }
 141     }
 142     return _result;
 143   } while(false);
 144 }
 145 
 146 inline int    hpi::accept(int fd, struct sockaddr *him, int *len) {
 147   if (fd < 0)
 148     return OS_ERR;
 149   INTERRUPTIBLE_RETURN_INT((int)::accept(fd, him, (socklen_t*) len), os::Solaris::clear_interrupted);
 150 }
 151 
 152 inline int    hpi::recvfrom(int fd, char *buf, int nBytes, int flags,
 153                             sockaddr *from, int *fromlen) {
 154   //%%note jvm_r11
 155   INTERRUPTIBLE_RETURN_INT((int)::recvfrom(fd, buf, nBytes, (unsigned int) flags, from, (socklen_t *)fromlen), os::Solaris::clear_interrupted);
 156 }
 157 
 158 inline int    hpi::sendto(int fd, char *buf, int len, int flags,
 159                           struct sockaddr *to, int tolen) {
 160   //%%note jvm_r11
 161   INTERRUPTIBLE_RETURN_INT((int)::sendto(fd, buf, len, (unsigned int) flags, to, tolen),os::Solaris::clear_interrupted);
 162 }
 163 
 164 inline int    hpi::socket_available(int fd, jint *pbytes) {
 165   if (fd < 0)
 166     return OS_OK;
 167 
 168   int ret;
 169 
 170   RESTARTABLE(::ioctl(fd, FIONREAD, pbytes), ret);
 171 
 172   //%% note ioctl can return 0 when successful, JVM_SocketAvailable
 173   // is expected to return 0 on failure and 1 on success to the jdk.
 174 
 175   return (ret == OS_ERR) ? 0 : 1;
 176 }
 177 
 178 
 179 /*
 180 HPIDECL(socket_shutdown, "socket_shutdown", _socket, SocketShutdown,
 181         int, "%d",
 182         (int fd, int howto),
 183         ("fd = %d, howto = %d", fd, howto),
 184         (fd, howto));
 185         */
 186 inline int hpi::socket_shutdown(int fd, int howto){
 187   return ::shutdown(fd, howto);
 188 }
 189 
 190 /*
 191 HPIDECL(bind, "bind", _socket, Bind,
 192         int, "%d",
 193         (int fd, struct sockaddr *him, int len),
 194         ("fd = %d, him = %p, len = %d",
 195          fd, him, len),
 196         (fd, him, len));
 197 */
 198 inline int hpi::bind(int fd, struct sockaddr *him, int len){
 199   INTERRUPTIBLE_RETURN_INT_NORESTART(::bind(fd, him, len),os::Solaris::clear_interrupted);
 200 }
 201 
 202 /*
 203 HPIDECL(get_sock_name, "get_sock_name", _socket, GetSocketName,
 204         int, "%d",
 205         (int fd, struct sockaddr *him, int *len),
 206         ("fd = %d, him = %p, len = %p",
 207          fd, him, len),
 208         (fd, him, len));
 209         */
 210 inline int hpi::get_sock_name(int fd, struct sockaddr *him, int *len){
 211   return ::getsockname(fd, him, (socklen_t*) len);
 212 }
 213 
 214 /*
 215 HPIDECL(get_host_name, "get_host_name", _socket, GetHostName, int, "%d",
 216         (char *hostname, int namelen),
 217         ("hostname = %p, namelen = %d",
 218          hostname, namelen),
 219         (hostname, namelen));
 220         */
 221 inline int hpi::get_host_name(char* name, int namelen){
 222   return ::gethostname(name, namelen);
 223 }
 224 
 225 /*
 226 HPIDECL(get_sock_opt, "get_sock_opt", _socket, SocketGetOption, int, "%d",
 227         (int fd, int level, int optname, char *optval, int* optlen),
 228         ("fd = %d, level = %d, optname = %d, optval = %p, optlen = %p",
 229          fd, level, optname, optval, optlen),
 230         (fd, level, optname, optval, optlen));
 231         */
 232 inline int hpi::get_sock_opt(int fd, int level, int optname,
 233                              char *optval, int* optlen){
 234   return ::getsockopt(fd, level, optname, optval, (socklen_t*) optlen);
 235 }
 236 
 237 /*
 238 HPIDECL(set_sock_opt, "set_sock_opt", _socket, SocketSetOption, int, "%d",
 239         (int fd, int level, int optname, const char *optval, int optlen),
 240         ("fd = %d, level = %d, optname = %d, optval = %p, optlen = %d",
 241          fd, level, optname, optval, optlen),
 242         (fd, level, optname, optval, optlen));
 243         */
 244 inline int hpi::set_sock_opt(int fd, int level, int optname,
 245                              const char *optval, int optlen){
 246   return ::setsockopt(fd, level, optname, optval, optlen);
 247 }
 248 
 249 //Reconciliation History
 250 // 1.3 98/10/21 18:17:14 hpi_win32.hpp
 251 // 1.6 99/06/28 11:01:36 hpi_win32.hpp
 252 //End
 253 
 254 #endif // OS_SOLARIS_VM_HPI_SOLARIS_HPP