1 /*
   2  * Copyright (c) 1998, 2009, 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 //
  26 // C++ wrapper to HPI.
  27 //
  28 
  29 class hpi : AllStatic {
  30 
  31 private:
  32   static GetInterfaceFunc       _get_interface;
  33   static HPI_FileInterface*     _file;
  34   static HPI_SocketInterface*   _socket;
  35   static HPI_LibraryInterface*  _library;
  36   static HPI_SystemInterface*   _system;
  37 
  38 private:
  39   static void initialize_get_interface(vm_calls_t *callbacks);
  40 
  41 public:
  42   // Load and initialize everything except sockets.
  43   static jint initialize();
  44 
  45   // Socket library needs to be lazy intialized because eagerly
  46   // loading Winsock is known to cause "connect to your ISP"
  47   // dialog to show up.  Or so goes the legend.
  48   static jint initialize_socket_library();
  49 
  50   // HPI_FileInterface
  51   static inline char*  native_path(char *path);
  52   static inline int    file_type(const char *path);
  53   static inline int    open(const char *name, int mode, int perm);
  54   static inline int    close(int fd);
  55   static inline jlong  lseek(int fd, jlong off, int whence);
  56   static inline int    ftruncate(int fd, jlong length);
  57   static inline int    fsync(int fd);
  58   static inline int    available(int fd, jlong *bytes);
  59   static inline size_t read(int fd, void *buf, unsigned int nBytes);
  60   static inline size_t write(int fd, const void *buf, unsigned int nBytes);
  61   static inline int    fsize(int fd, jlong *size);
  62 
  63   // HPI_SocketInterface
  64   static inline int    socket(int domain, int type, int protocol);
  65   static inline int    socket_close(int fd);
  66   static inline int    socket_shutdown(int fd, int howto);
  67   static inline int    recv(int fd, char *buf, int nBytes, int flags);
  68   static inline int    send(int fd, char *buf, int nBytes, int flags);
  69   // Variant of send that doesn't support interruptible I/O
  70   static inline int    raw_send(int fd, char *buf, int nBytes, int flags);
  71   static inline int    timeout(int fd, long timeout);
  72   static inline int    listen(int fd, int count);
  73   static inline int    connect(int fd, struct sockaddr *him, int len);
  74   static inline int    bind(int fd, struct sockaddr *him, int len);
  75   static inline int    accept(int fd, struct sockaddr *him, int *len);
  76   static inline int    recvfrom(int fd, char *buf, int nbytes, int flags,
  77                                 struct sockaddr *from, int *fromlen);
  78   static inline int    get_sock_name(int fd, struct sockaddr *him, int *len);
  79   static inline int    sendto(int fd, char *buf, int len, int flags,
  80                               struct sockaddr *to, int tolen);
  81   static inline int    socket_available(int fd, jint *pbytes);
  82 
  83   static inline int    get_sock_opt(int fd, int level, int optname,
  84                               char *optval, int* optlen);
  85   static inline int    set_sock_opt(int fd, int level, int optname,
  86                               const char *optval, int optlen);
  87   static inline int    get_host_name(char* name, int namelen);
  88   static inline struct hostent*  get_host_by_addr(const char* name, int len, int type);
  89   static inline struct hostent*  get_host_by_name(char* name);
  90   static inline struct protoent* get_proto_by_name(char* name);
  91 
  92   // HPI_LibraryInterface
  93   static inline void   dll_build_name(char *buf, int buf_len, const char* path,
  94                                       const char *name);
  95   static inline void*  dll_load(const char *name, char *ebuf, int ebuflen);
  96   static inline void   dll_unload(void *lib);
  97   static inline void*  dll_lookup(void *lib, const char *name);
  98 
  99   // HPI_SystemInterface
 100   static inline int    lasterror(char *buf, int len);
 101 };
 102 
 103 //
 104 // Macros that provide inline bodies for the functions.
 105 //
 106 
 107 #define HPIDECL(name, names, intf, func, ret_type, ret_fmt, arg_type, arg_print, arg)   \
 108   inline ret_type hpi::name arg_type {        \
 109     if (TraceHPI) {                           \
 110       tty->print("hpi::" names "(");          \
 111       tty->print arg_print ;                  \
 112       tty->print(") = ");                     \
 113     }                                         \
 114     ret_type result = (*intf->func) arg ;     \
 115     if (TraceHPI) {                           \
 116       tty->print_cr(ret_fmt, result);         \
 117     }                                         \
 118     return result;                            \
 119   }
 120 
 121 // Macro to facilitate moving HPI functionality into the vm.
 122 // See bug 6348631.  The only difference between this macro and
 123 // HPIDECL is that we call a vm method rather than use the HPI
 124 // transfer vector.  Ultimately, we'll replace HPIDECL with
 125 // VM_HPIDECL for all hpi methods.
 126 #define VM_HPIDECL(name, names, func, ret_type, ret_fmt, arg_type,arg_print, arg)   \
 127   inline ret_type hpi::name arg_type {        \
 128     if (TraceHPI) {                           \
 129       tty->print("hpi::" names "(");          \
 130       tty->print arg_print ;                  \
 131       tty->print(") = ");                     \
 132     }                                         \
 133     ret_type result = func arg ;              \
 134     if (TraceHPI) {                           \
 135       tty->print_cr(ret_fmt, result);         \
 136     }                                         \
 137     return result;                            \
 138   }
 139 
 140 #define VM_HPIDECL_VOID(name, names, func, arg_type, arg_print, arg)   \
 141   inline void  hpi::name arg_type {           \
 142     if (TraceHPI) {                           \
 143       tty->print("hpi::" names "(");          \
 144       tty->print arg_print;                   \
 145       tty->print(") = ");                     \
 146     }                                         \
 147     func arg;                                 \
 148   }
 149 
 150 #define HPIDECL_VOID(name, names, intf, func, arg_type, arg_print, arg) \
 151   inline void hpi::name arg_type {            \
 152     if (TraceHPI) {                           \
 153       tty->print("hpi::" names "(");          \
 154       tty->print arg_print ;                  \
 155       tty->print_cr(") = void");              \
 156     }                                         \
 157     (*intf->func) arg ;                       \
 158   }
 159 
 160 
 161 // The macro calls below realize into
 162 //          inline char * hpi::native_path(...) {  inlined_body; }
 163 // etc.
 164 
 165 // HPI_FileInterface
 166 
 167 HPIDECL(native_path, "native_path", _file, NativePath, char *, "%s",
 168         (char *path),
 169         ("path = %s", path),
 170         (path));
 171 
 172 HPIDECL(file_type, "file_type", _file, FileType, int, "%d",
 173         (const char *path),
 174         ("path = %s", path),
 175         (path));
 176 
 177 HPIDECL(open, "open", _file, Open, int, "%d",
 178         (const char *name, int mode, int perm),
 179         ("name = %s, mode = %d, perm = %d", name, mode, perm),
 180         (name, mode, perm));
 181 
 182 HPIDECL(lseek, "seek", _file, Seek, jlong, "(a jlong)",
 183         (int fd, jlong off, int whence),
 184         ("fd = %d, off = (a jlong), whence = %d", fd, /* off, */ whence),
 185         (fd, off, whence));
 186 
 187 HPIDECL(ftruncate, "ftruncate", _file, SetLength, int, "%d",
 188         (int fd, jlong length),
 189         ("fd = %d, length = (a jlong)", fd /*, length */),
 190         (fd, length));
 191 
 192 HPIDECL(fsync, "fsync", _file, Sync, int, "%d",
 193         (int fd),
 194         ("fd = %d", fd),
 195         (fd));
 196 
 197 HPIDECL(available, "available", _file, Available, int, "%d",
 198         (int fd, jlong *bytes),
 199         ("fd = %d, bytes = %p", fd, bytes),
 200         (fd, bytes));
 201 
 202 HPIDECL(fsize, "fsize", _file, FileSizeFD, int, "%d",
 203         (int fd, jlong *size),
 204         ("fd = %d, size = %p", fd, size),
 205         (fd, size));
 206 
 207 // HPI_LibraryInterface
 208 VM_HPIDECL_VOID(dll_build_name, "dll_build_name", os::dll_build_name,
 209                (char *buf, int buf_len, const char *path, const char *name),
 210                ("buf = %p, buflen = %d, path = %s, name = %s",
 211                 buf, buf_len, path, name),
 212                (buf, buf_len, path, name));
 213 
 214 VM_HPIDECL(dll_load, "dll_load", os::dll_load,
 215         void *, "(void *)%p",
 216         (const char *name, char *ebuf, int ebuflen),
 217         ("name = %s, ebuf = %p, ebuflen = %d", name, ebuf, ebuflen),
 218         (name, ebuf, ebuflen));
 219 
 220 HPIDECL_VOID(dll_unload, "dll_unload", _library, UnloadLibrary,
 221              (void *lib),
 222              ("lib = %p", lib),
 223              (lib));
 224 
 225 HPIDECL(dll_lookup, "dll_lookup", _library, FindLibraryEntry, void *, "%p",
 226         (void *lib, const char *name),
 227         ("lib = %p, name = %s", lib, name),
 228         (lib, name));
 229 
 230 // HPI_SystemInterface
 231 HPIDECL(lasterror, "lasterror", _system, GetLastErrorString, int, "%d",
 232         (char *buf, int len),
 233         ("buf = %p, len = %d", buf, len),
 234         (buf, len));