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 DIR* os::opendir(const char* dirname)
  91 {
  92   assert(dirname != NULL, "just checking");
  93   return ::opendir(dirname);
  94 }
  95 
  96 inline int os::readdir_buf_size(const char *path)
  97 {
  98   return NAME_MAX + sizeof(dirent) + 1;
  99 }
 100 
 101 inline jlong os::lseek(int fd, jlong offset, int whence) {
 102   return (jlong) ::lseek64(fd, offset, whence);
 103 }
 104 
 105 inline int os::fsync(int fd) {
 106   return ::fsync(fd);
 107 }
 108 
 109 inline char* os::native_path(char *path) {
 110   return path;
 111 }
 112 
 113 inline int os::ftruncate(int fd, jlong length) {
 114   return ::ftruncate64(fd, length);
 115 }
 116 
 117 inline struct dirent* os::readdir(DIR* dirp, dirent *dbuf)
 118 {
 119   dirent* p;
 120   int status;
 121   assert(dirp != NULL, "just checking");
 122 
 123   // NOTE: Linux readdir_r (on RH 6.2 and 7.2 at least) is NOT like the POSIX
 124   // version. Here is the doc for this function:
 125   // http://www.gnu.org/manual/glibc-2.2.3/html_node/libc_262.html
 126 
 127   if((status = ::readdir_r(dirp, dbuf, &p)) != 0) {
 128     errno = status;
 129     return NULL;
 130   } else
 131     return p;
 132 }
 133 
 134 inline int os::closedir(DIR *dirp) {
 135   assert(dirp != NULL, "argument is NULL");
 136   return ::closedir(dirp);
 137 }
 138 
 139 // macros for restartable system calls
 140 
 141 #define RESTARTABLE(_cmd, _result) do { \
 142     _result = _cmd; \
 143   } while(((int)_result == OS_ERR) && (errno == EINTR))
 144 
 145 #define RESTARTABLE_RETURN_INT(_cmd) do { \
 146   int _result; \
 147   RESTARTABLE(_cmd, _result); \
 148   return _result; \
 149 } while(false)
 150 
 151 inline bool os::numa_has_static_binding()   { return true; }
 152 inline bool os::numa_has_group_homing()     { return false;  }
 153 
 154 inline size_t os::restartable_read(int fd, void *buf, unsigned int nBytes) {
 155   size_t res;
 156   RESTARTABLE( (size_t) ::read(fd, buf, (size_t) nBytes), res);
 157   return res;
 158 }
 159 
 160 inline size_t os::write(int fd, const void *buf, unsigned int nBytes) {
 161   size_t res;
 162   RESTARTABLE((size_t) ::write(fd, buf, (size_t) nBytes), res);
 163   return res;
 164 }
 165 
 166 inline int os::close(int fd) {
 167   return ::close(fd);
 168 }
 169 
 170 inline int os::socket_close(int fd) {
 171   return ::close(fd);
 172 }
 173 
 174 inline int os::socket(int domain, int type, int protocol) {
 175   return ::socket(domain, type, protocol);
 176 }
 177 
 178 inline int os::recv(int fd, char* buf, size_t nBytes, uint flags) {
 179   RESTARTABLE_RETURN_INT(::recv(fd, buf, nBytes, flags));
 180 }
 181 
 182 inline int os::send(int fd, char* buf, size_t nBytes, uint flags) {
 183   RESTARTABLE_RETURN_INT(::send(fd, buf, nBytes, flags));
 184 }
 185 
 186 inline int os::raw_send(int fd, char* buf, size_t nBytes, uint flags) {
 187   return os::send(fd, buf, nBytes, flags);
 188 }
 189 
 190 inline int os::timeout(int fd, long timeout) {
 191   julong prevtime,newtime;
 192   struct timeval t;
 193 
 194   gettimeofday(&t, NULL);
 195   prevtime = ((julong)t.tv_sec * 1000)  +  t.tv_usec / 1000;
 196 
 197   for(;;) {
 198     struct pollfd pfd;
 199 
 200     pfd.fd = fd;
 201     pfd.events = POLLIN | POLLERR;
 202 
 203     int res = ::poll(&pfd, 1, timeout);
 204 
 205     if (res == OS_ERR && errno == EINTR) {
 206 
 207       // On Linux any value < 0 means "forever"
 208 
 209       if(timeout >= 0) {
 210         gettimeofday(&t, NULL);
 211         newtime = ((julong)t.tv_sec * 1000)  +  t.tv_usec / 1000;
 212         timeout -= newtime - prevtime;
 213         if(timeout <= 0)
 214           return OS_OK;
 215         prevtime = newtime;
 216       }
 217     } else
 218       return res;
 219   }
 220 }
 221 
 222 inline int os::listen(int fd, int count) {
 223   return ::listen(fd, count);
 224 }
 225 
 226 inline int os::connect(int fd, struct sockaddr* him, socklen_t len) {
 227   RESTARTABLE_RETURN_INT(::connect(fd, him, len));
 228 }
 229 
 230 inline int os::accept(int fd, struct sockaddr* him, socklen_t* len) {
 231   // Linux doc says this can't return EINTR, unlike accept() on Solaris.
 232   // But see attachListener_linux.cpp, LinuxAttachListener::dequeue().
 233   return (int)::accept(fd, him, len);
 234 }
 235 
 236 inline int os::recvfrom(int fd, char* buf, size_t nBytes, uint flags,
 237                         sockaddr* from, socklen_t* fromlen) {
 238   RESTARTABLE_RETURN_INT((int)::recvfrom(fd, buf, nBytes, flags, from, fromlen));
 239 }
 240 
 241 inline int os::sendto(int fd, char* buf, size_t len, uint flags,
 242                       struct sockaddr* to, socklen_t tolen) {
 243   RESTARTABLE_RETURN_INT((int)::sendto(fd, buf, len, flags, to, tolen));
 244 }
 245 
 246 inline int os::socket_shutdown(int fd, int howto) {
 247   return ::shutdown(fd, howto);
 248 }
 249 
 250 inline int os::bind(int fd, struct sockaddr* him, socklen_t len) {
 251   return ::bind(fd, him, len);
 252 }
 253 
 254 inline int os::get_sock_name(int fd, struct sockaddr* him, socklen_t* len) {
 255   return ::getsockname(fd, him, len);
 256 }
 257 
 258 inline int os::get_host_name(char* name, int namelen) {
 259   return ::gethostname(name, namelen);
 260 }
 261 
 262 inline struct hostent* os::get_host_by_name(char* name) {
 263   return ::gethostbyname(name);
 264 }
 265 
 266 inline int os::get_sock_opt(int fd, int level, int optname,
 267                             char* optval, socklen_t* optlen) {
 268   return ::getsockopt(fd, level, optname, optval, optlen);
 269 }
 270 
 271 inline int os::set_sock_opt(int fd, int level, int optname,
 272                             const char* optval, socklen_t optlen) {
 273   return ::setsockopt(fd, level, optname, optval, optlen);
 274 }
 275 
 276 #endif // OS_LINUX_VM_OS_LINUX_INLINE_HPP