1 /*
   2  * Copyright (c) 1999, 2013, 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_LINUX_VM_OS_LINUX_INLINE_HPP
  26 #define OS_LINUX_VM_OS_LINUX_INLINE_HPP
  27 
  28 #include "runtime/atomic.inline.hpp"
  29 #include "runtime/orderAccess.inline.hpp"
  30 #include "runtime/os.hpp"
  31 
  32 // System includes
  33 
  34 #include <unistd.h>
  35 #include <sys/socket.h>
  36 #include <sys/poll.h>
  37 #include <netdb.h>
  38 
  39 inline void* os::thread_local_storage_at(int index) {
  40   return pthread_getspecific((pthread_key_t)index);
  41 }
  42 
  43 inline const char* os::file_separator() {
  44   return "/";
  45 }
  46 
  47 inline const char* os::line_separator() {
  48   return "\n";
  49 }
  50 
  51 inline const char* os::path_separator() {
  52   return ":";
  53 }
  54 
  55 // File names are case-sensitive on windows only
  56 inline int os::file_name_strcmp(const char* s1, const char* s2) {
  57   return strcmp(s1, s2);
  58 }
  59 
  60 inline bool os::obsolete_option(const JavaVMOption *option) {
  61   return false;
  62 }
  63 
  64 inline bool os::uses_stack_guard_pages() {
  65   return true;
  66 }
  67 
  68 inline bool os::allocate_stack_guard_pages() {
  69   assert(uses_stack_guard_pages(), "sanity check");
  70   return true;
  71 }
  72 
  73 
  74 // On Linux, reservations are made on a page by page basis, nothing to do.
  75 inline void os::pd_split_reserved_memory(char *base, size_t size,
  76                                       size_t split, bool realloc) {
  77 }
  78 
  79 
  80 // Bang the shadow pages if they need to be touched to be mapped.
  81 inline void os::bang_stack_shadow_pages() {
  82 }
  83 
  84 inline void os::dll_unload(void *lib) {
  85   ::dlclose(lib);
  86 }
  87 
  88 inline const int os::default_file_open_flags() { return 0;}
  89 
  90 inline jlong os::lseek(int fd, jlong offset, int whence) {
  91   return (jlong) ::lseek64(fd, offset, whence);
  92 }
  93 
  94 inline int os::fsync(int fd) {
  95   return ::fsync(fd);
  96 }
  97 
  98 inline char* os::native_path(char *path) {
  99   return path;
 100 }
 101 
 102 inline int os::ftruncate(int fd, jlong length) {
 103   return ::ftruncate64(fd, length);
 104 }
 105 
 106 // macros for restartable system calls
 107 
 108 #define RESTARTABLE(_cmd, _result) do { \
 109     _result = _cmd; \
 110   } while(((int)_result == OS_ERR) && (errno == EINTR))
 111 
 112 #define RESTARTABLE_RETURN_INT(_cmd) do { \
 113   int _result; \
 114   RESTARTABLE(_cmd, _result); \
 115   return _result; \
 116 } while(false)
 117 
 118 inline bool os::numa_has_static_binding()   { return true; }
 119 inline bool os::numa_has_group_homing()     { return false;  }
 120 
 121 inline size_t os::restartable_read(int fd, void *buf, unsigned int nBytes) {
 122   size_t res;
 123   RESTARTABLE( (size_t) ::read(fd, buf, (size_t) nBytes), res);
 124   return res;
 125 }
 126 
 127 inline size_t os::write(int fd, const void *buf, unsigned int nBytes) {
 128   size_t res;
 129   RESTARTABLE((size_t) ::write(fd, buf, (size_t) nBytes), res);
 130   return res;
 131 }
 132 
 133 inline int os::close(int fd) {
 134   return ::close(fd);
 135 }
 136 
 137 inline int os::socket_close(int fd) {
 138   return ::close(fd);
 139 }
 140 
 141 inline int os::socket(int domain, int type, int protocol) {
 142   return ::socket(domain, type, protocol);
 143 }
 144 
 145 inline int os::recv(int fd, char* buf, size_t nBytes, uint flags) {
 146   RESTARTABLE_RETURN_INT(::recv(fd, buf, nBytes, flags));
 147 }
 148 
 149 inline int os::send(int fd, char* buf, size_t nBytes, uint flags) {
 150   RESTARTABLE_RETURN_INT(::send(fd, buf, nBytes, flags));
 151 }
 152 
 153 inline int os::raw_send(int fd, char* buf, size_t nBytes, uint flags) {
 154   return os::send(fd, buf, nBytes, flags);
 155 }
 156 
 157 inline int os::timeout(int fd, long timeout) {
 158   julong prevtime,newtime;
 159   struct timeval t;
 160 
 161   gettimeofday(&t, NULL);
 162   prevtime = ((julong)t.tv_sec * 1000)  +  t.tv_usec / 1000;
 163 
 164   for(;;) {
 165     struct pollfd pfd;
 166 
 167     pfd.fd = fd;
 168     pfd.events = POLLIN | POLLERR;
 169 
 170     int res = ::poll(&pfd, 1, timeout);
 171 
 172     if (res == OS_ERR && errno == EINTR) {
 173 
 174       // On Linux any value < 0 means "forever"
 175 
 176       if(timeout >= 0) {
 177         gettimeofday(&t, NULL);
 178         newtime = ((julong)t.tv_sec * 1000)  +  t.tv_usec / 1000;
 179         timeout -= newtime - prevtime;
 180         if(timeout <= 0)
 181           return OS_OK;
 182         prevtime = newtime;
 183       }
 184     } else
 185       return res;
 186   }
 187 }
 188 
 189 inline int os::listen(int fd, int count) {
 190   return ::listen(fd, count);
 191 }
 192 
 193 inline int os::connect(int fd, struct sockaddr* him, socklen_t len) {
 194   RESTARTABLE_RETURN_INT(::connect(fd, him, len));
 195 }
 196 
 197 inline int os::accept(int fd, struct sockaddr* him, socklen_t* len) {
 198   // Linux doc says this can't return EINTR, unlike accept() on Solaris.
 199   // But see attachListener_linux.cpp, LinuxAttachListener::dequeue().
 200   return (int)::accept(fd, him, len);
 201 }
 202 
 203 inline int os::recvfrom(int fd, char* buf, size_t nBytes, uint flags,
 204                         sockaddr* from, socklen_t* fromlen) {
 205   RESTARTABLE_RETURN_INT((int)::recvfrom(fd, buf, nBytes, flags, from, fromlen));
 206 }
 207 
 208 inline int os::sendto(int fd, char* buf, size_t len, uint flags,
 209                       struct sockaddr* to, socklen_t tolen) {
 210   RESTARTABLE_RETURN_INT((int)::sendto(fd, buf, len, flags, to, tolen));
 211 }
 212 
 213 inline int os::socket_shutdown(int fd, int howto) {
 214   return ::shutdown(fd, howto);
 215 }
 216 
 217 inline int os::bind(int fd, struct sockaddr* him, socklen_t len) {
 218   return ::bind(fd, him, len);
 219 }
 220 
 221 inline int os::get_sock_name(int fd, struct sockaddr* him, socklen_t* len) {
 222   return ::getsockname(fd, him, len);
 223 }
 224 
 225 inline int os::get_host_name(char* name, int namelen) {
 226   return ::gethostname(name, namelen);
 227 }
 228 
 229 inline struct hostent* os::get_host_by_name(char* name) {
 230   return ::gethostbyname(name);
 231 }
 232 
 233 inline int os::get_sock_opt(int fd, int level, int optname,
 234                             char* optval, socklen_t* optlen) {
 235   return ::getsockopt(fd, level, optname, optval, optlen);
 236 }
 237 
 238 inline int os::set_sock_opt(int fd, int level, int optname,
 239                             const char* optval, socklen_t optlen) {
 240   return ::setsockopt(fd, level, optname, optval, optlen);
 241 }
 242 
 243 #endif // OS_LINUX_VM_OS_LINUX_INLINE_HPP