|
1 /* |
|
2 * Copyright 1998-2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any 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 static inline int timeout(int fd, long timeout); |
|
70 static inline int listen(int fd, int count); |
|
71 static inline int connect(int fd, struct sockaddr *him, int len); |
|
72 static inline int bind(int fd, struct sockaddr *him, int len); |
|
73 static inline int accept(int fd, struct sockaddr *him, int *len); |
|
74 static inline int recvfrom(int fd, char *buf, int nbytes, int flags, |
|
75 struct sockaddr *from, int *fromlen); |
|
76 static inline int get_sock_name(int fd, struct sockaddr *him, int *len); |
|
77 static inline int sendto(int fd, char *buf, int len, int flags, |
|
78 struct sockaddr *to, int tolen); |
|
79 static inline int socket_available(int fd, jint *pbytes); |
|
80 |
|
81 static inline int get_sock_opt(int fd, int level, int optname, |
|
82 char *optval, int* optlen); |
|
83 static inline int set_sock_opt(int fd, int level, int optname, |
|
84 const char *optval, int optlen); |
|
85 static inline int get_host_name(char* name, int namelen); |
|
86 static inline struct hostent* get_host_by_addr(const char* name, int len, int type); |
|
87 static inline struct hostent* get_host_by_name(char* name); |
|
88 static inline struct protoent* get_proto_by_name(char* name); |
|
89 |
|
90 // HPI_LibraryInterface |
|
91 static inline void dll_build_name(char *buf, int buf_len, char* path, |
|
92 const char *name); |
|
93 static inline void* dll_load(const char *name, char *ebuf, int ebuflen); |
|
94 static inline void dll_unload(void *lib); |
|
95 static inline void* dll_lookup(void *lib, const char *name); |
|
96 |
|
97 // HPI_SystemInterface |
|
98 static inline int lasterror(char *buf, int len); |
|
99 }; |
|
100 |
|
101 // |
|
102 // Macros that provide inline bodies for the functions. |
|
103 // |
|
104 |
|
105 #define HPIDECL(name, names, intf, func, ret_type, ret_fmt, arg_type, arg_print, arg) \ |
|
106 inline ret_type hpi::name arg_type { \ |
|
107 if (TraceHPI) { \ |
|
108 tty->print("hpi::" names "("); \ |
|
109 tty->print arg_print ; \ |
|
110 tty->print(") = "); \ |
|
111 } \ |
|
112 ret_type result = (*intf->func) arg ; \ |
|
113 if (TraceHPI) { \ |
|
114 tty->print_cr(ret_fmt, result); \ |
|
115 } \ |
|
116 return result; \ |
|
117 } |
|
118 |
|
119 // Macro to facilitate moving HPI functionality into the vm. |
|
120 // See bug 6348631. The only difference between this macro and |
|
121 // HPIDECL is that we call a vm method rather than use the HPI |
|
122 // transfer vector. Ultimately, we'll replace HPIDECL with |
|
123 // VM_HPIDECL for all hpi methods. |
|
124 #define VM_HPIDECL(name, names, func, ret_type, ret_fmt, arg_type,arg_print, arg) \ |
|
125 inline ret_type hpi::name arg_type { \ |
|
126 if (TraceHPI) { \ |
|
127 tty->print("hpi::" names "("); \ |
|
128 tty->print arg_print ; \ |
|
129 tty->print(") = "); \ |
|
130 } \ |
|
131 ret_type result = func arg ; \ |
|
132 if (TraceHPI) { \ |
|
133 tty->print_cr(ret_fmt, result); \ |
|
134 } \ |
|
135 return result; \ |
|
136 } |
|
137 |
|
138 |
|
139 |
|
140 #define HPIDECL_VOID(name, names, intf, 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_cr(") = void"); \ |
|
146 } \ |
|
147 (*intf->func) arg ; \ |
|
148 } |
|
149 |
|
150 |
|
151 // The macro calls below realize into |
|
152 // inline char * hpi::native_path(...) { inlined_body; } |
|
153 // etc. |
|
154 |
|
155 // HPI_FileInterface |
|
156 |
|
157 HPIDECL(native_path, "native_path", _file, NativePath, char *, "%s", |
|
158 (char *path), |
|
159 ("path = %s", path), |
|
160 (path)); |
|
161 |
|
162 HPIDECL(file_type, "file_type", _file, FileType, int, "%d", |
|
163 (const char *path), |
|
164 ("path = %s", path), |
|
165 (path)); |
|
166 |
|
167 HPIDECL(open, "open", _file, Open, int, "%d", |
|
168 (const char *name, int mode, int perm), |
|
169 ("name = %s, mode = %d, perm = %d", name, mode, perm), |
|
170 (name, mode, perm)); |
|
171 |
|
172 HPIDECL(lseek, "seek", _file, Seek, jlong, "(a jlong)", |
|
173 (int fd, jlong off, int whence), |
|
174 ("fd = %d, off = (a jlong), whence = %d", fd, /* off, */ whence), |
|
175 (fd, off, whence)); |
|
176 |
|
177 HPIDECL(ftruncate, "ftruncate", _file, SetLength, int, "%d", |
|
178 (int fd, jlong length), |
|
179 ("fd = %d, length = (a jlong)", fd /*, length */), |
|
180 (fd, length)); |
|
181 |
|
182 HPIDECL(fsync, "fsync", _file, Sync, int, "%d", |
|
183 (int fd), |
|
184 ("fd = %d", fd), |
|
185 (fd)); |
|
186 |
|
187 HPIDECL(available, "available", _file, Available, int, "%d", |
|
188 (int fd, jlong *bytes), |
|
189 ("fd = %d, bytes = %p", fd, bytes), |
|
190 (fd, bytes)); |
|
191 |
|
192 HPIDECL(fsize, "fsize", _file, FileSizeFD, int, "%d", |
|
193 (int fd, jlong *size), |
|
194 ("fd = %d, size = %p", fd, size), |
|
195 (fd, size)); |
|
196 |
|
197 // HPI_LibraryInterface |
|
198 HPIDECL_VOID(dll_build_name, "dll_build_name", _library, BuildLibName, |
|
199 (char *buf, int buf_len, char *path, const char *name), |
|
200 ("buf = %p, buflen = %d, path = %s, name = %s", |
|
201 buf, buf_len, path, name), |
|
202 (buf, buf_len, path, name)); |
|
203 |
|
204 VM_HPIDECL(dll_load, "dll_load", os::dll_load, |
|
205 void *, "(void *)%p", |
|
206 (const char *name, char *ebuf, int ebuflen), |
|
207 ("name = %s, ebuf = %p, ebuflen = %d", name, ebuf, ebuflen), |
|
208 (name, ebuf, ebuflen)); |
|
209 |
|
210 HPIDECL_VOID(dll_unload, "dll_unload", _library, UnloadLibrary, |
|
211 (void *lib), |
|
212 ("lib = %p", lib), |
|
213 (lib)); |
|
214 |
|
215 HPIDECL(dll_lookup, "dll_lookup", _library, FindLibraryEntry, void *, "%p", |
|
216 (void *lib, const char *name), |
|
217 ("lib = %p, name = %s", lib, name), |
|
218 (lib, name)); |
|
219 |
|
220 // HPI_SystemInterface |
|
221 HPIDECL(lasterror, "lasterror", _system, GetLastErrorString, int, "%d", |
|
222 (char *buf, int len), |
|
223 ("buf = %p, len = %d", buf, len), |
|
224 (buf, len)); |