2
|
1 |
/*
|
|
2 |
* Copyright 2003-2005 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
*
|
|
4 |
* Redistribution and use in source and binary forms, with or without
|
|
5 |
* modification, are permitted provided that the following conditions
|
|
6 |
* are met:
|
|
7 |
*
|
|
8 |
* - Redistributions of source code must retain the above copyright
|
|
9 |
* notice, this list of conditions and the following disclaimer.
|
|
10 |
*
|
|
11 |
* - Redistributions in binary form must reproduce the above copyright
|
|
12 |
* notice, this list of conditions and the following disclaimer in the
|
|
13 |
* documentation and/or other materials provided with the distribution.
|
|
14 |
*
|
|
15 |
* - Neither the name of Sun Microsystems nor the names of its
|
|
16 |
* contributors may be used to endorse or promote products derived
|
|
17 |
* from this software without specific prior written permission.
|
|
18 |
*
|
|
19 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
|
20 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
21 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
22 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
23 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
24 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
25 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
26 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
27 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
28 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
29 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
30 |
*/
|
|
31 |
|
|
32 |
#include <windows.h>
|
|
33 |
#include <io.h>
|
|
34 |
#include <sys/types.h>
|
|
35 |
#include <sys/stat.h>
|
|
36 |
#include <mmsystem.h>
|
|
37 |
#include <winsock2.h>
|
|
38 |
#include <fcntl.h>
|
|
39 |
#include <process.h>
|
|
40 |
|
|
41 |
#include "jni.h"
|
|
42 |
#include "hprof.h"
|
|
43 |
|
|
44 |
int
|
|
45 |
md_getpid(void)
|
|
46 |
{
|
|
47 |
static int pid = -1;
|
|
48 |
|
|
49 |
if ( pid >= 0 ) {
|
|
50 |
return pid;
|
|
51 |
}
|
|
52 |
pid = getpid();
|
|
53 |
return pid;
|
|
54 |
}
|
|
55 |
|
|
56 |
void
|
|
57 |
md_sleep(unsigned seconds)
|
|
58 |
{
|
|
59 |
Sleep((DWORD)seconds*1000);
|
|
60 |
}
|
|
61 |
|
|
62 |
void
|
|
63 |
md_init(void)
|
|
64 |
{
|
|
65 |
}
|
|
66 |
|
|
67 |
int
|
|
68 |
md_connect(char *hostname, unsigned short port)
|
|
69 |
{
|
|
70 |
struct hostent *hentry;
|
|
71 |
struct sockaddr_in s;
|
|
72 |
int fd;
|
|
73 |
|
|
74 |
/* create a socket */
|
|
75 |
fd = (int)socket(AF_INET, SOCK_STREAM, 0);
|
|
76 |
|
|
77 |
/* find remote host's addr from name */
|
|
78 |
if ((hentry = gethostbyname(hostname)) == NULL) {
|
|
79 |
return -1;
|
|
80 |
}
|
|
81 |
(void)memset((char *)&s, 0, sizeof(s));
|
|
82 |
/* set remote host's addr; its already in network byte order */
|
|
83 |
(void)memcpy(&s.sin_addr.s_addr, *(hentry->h_addr_list),
|
|
84 |
(int)sizeof(s.sin_addr.s_addr));
|
|
85 |
/* set remote host's port */
|
|
86 |
s.sin_port = htons(port);
|
|
87 |
s.sin_family = AF_INET;
|
|
88 |
|
|
89 |
/* now try connecting */
|
|
90 |
if (-1 == connect(fd, (struct sockaddr*)&s, sizeof(s))) {
|
|
91 |
return 0;
|
|
92 |
}
|
|
93 |
return fd;
|
|
94 |
}
|
|
95 |
|
|
96 |
int
|
|
97 |
md_recv(int f, char *buf, int len, int option)
|
|
98 |
{
|
|
99 |
return recv(f, buf, len, option);
|
|
100 |
}
|
|
101 |
|
|
102 |
int
|
|
103 |
md_shutdown(int filedes, int option)
|
|
104 |
{
|
|
105 |
return shutdown(filedes, option);
|
|
106 |
}
|
|
107 |
|
|
108 |
int
|
|
109 |
md_open(const char *filename)
|
|
110 |
{
|
|
111 |
return open(filename, O_RDONLY);
|
|
112 |
}
|
|
113 |
|
|
114 |
int
|
|
115 |
md_open_binary(const char *filename)
|
|
116 |
{
|
|
117 |
return open(filename, O_RDONLY|O_BINARY);
|
|
118 |
}
|
|
119 |
|
|
120 |
int
|
|
121 |
md_creat(const char *filename)
|
|
122 |
{
|
|
123 |
return open(filename, O_CREAT | O_WRONLY | O_TRUNC,
|
|
124 |
_S_IREAD | _S_IWRITE);
|
|
125 |
}
|
|
126 |
|
|
127 |
int
|
|
128 |
md_creat_binary(const char *filename)
|
|
129 |
{
|
|
130 |
return open(filename, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY,
|
|
131 |
_S_IREAD | _S_IWRITE);
|
|
132 |
}
|
|
133 |
|
|
134 |
jlong
|
|
135 |
md_seek(int filedes, jlong pos)
|
|
136 |
{
|
|
137 |
jlong new_pos;
|
|
138 |
|
|
139 |
if ( pos == (jlong)-1 ) {
|
|
140 |
new_pos = _lseeki64(filedes, 0L, SEEK_END);
|
|
141 |
} else {
|
|
142 |
new_pos = _lseeki64(filedes, pos, SEEK_SET);
|
|
143 |
}
|
|
144 |
return new_pos;
|
|
145 |
}
|
|
146 |
|
|
147 |
void
|
|
148 |
md_close(int filedes)
|
|
149 |
{
|
|
150 |
(void)close(filedes);
|
|
151 |
}
|
|
152 |
|
|
153 |
int
|
|
154 |
md_send(int s, const char *msg, int len, int flags)
|
|
155 |
{
|
|
156 |
return send(s, msg, len, flags);
|
|
157 |
}
|
|
158 |
|
|
159 |
int
|
|
160 |
md_read(int filedes, void *buf, int nbyte)
|
|
161 |
{
|
|
162 |
return read(filedes, buf, nbyte);
|
|
163 |
}
|
|
164 |
|
|
165 |
int
|
|
166 |
md_write(int filedes, const void *buf, int nbyte)
|
|
167 |
{
|
|
168 |
return write(filedes, buf, nbyte);
|
|
169 |
}
|
|
170 |
|
|
171 |
jlong
|
|
172 |
md_get_microsecs(void)
|
|
173 |
{
|
|
174 |
return (jlong)(timeGetTime())*(jlong)1000;
|
|
175 |
}
|
|
176 |
|
|
177 |
#define FT2JLONG(ft) \
|
|
178 |
((jlong)(ft).dwHighDateTime << 32 | (jlong)(ft).dwLowDateTime)
|
|
179 |
|
|
180 |
jlong
|
|
181 |
md_get_timemillis(void)
|
|
182 |
{
|
|
183 |
static jlong fileTime_1_1_70 = 0;
|
|
184 |
SYSTEMTIME st0;
|
|
185 |
FILETIME ft0;
|
|
186 |
|
|
187 |
if (fileTime_1_1_70 == 0) {
|
|
188 |
/* Initialize fileTime_1_1_70 -- the Win32 file time of midnight
|
|
189 |
* 1/1/70.
|
|
190 |
*/
|
|
191 |
|
|
192 |
memset(&st0, 0, sizeof(st0));
|
|
193 |
st0.wYear = 1970;
|
|
194 |
st0.wMonth = 1;
|
|
195 |
st0.wDay = 1;
|
|
196 |
SystemTimeToFileTime(&st0, &ft0);
|
|
197 |
fileTime_1_1_70 = FT2JLONG(ft0);
|
|
198 |
}
|
|
199 |
|
|
200 |
GetSystemTime(&st0);
|
|
201 |
SystemTimeToFileTime(&st0, &ft0);
|
|
202 |
|
|
203 |
return (FT2JLONG(ft0) - fileTime_1_1_70) / 10000;
|
|
204 |
}
|
|
205 |
|
|
206 |
jlong
|
|
207 |
md_get_thread_cpu_timemillis(void)
|
|
208 |
{
|
|
209 |
return md_get_timemillis();
|
|
210 |
}
|
|
211 |
|
|
212 |
HINSTANCE hJavaInst;
|
|
213 |
static int nError = 0;
|
|
214 |
|
|
215 |
BOOL WINAPI
|
|
216 |
DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved)
|
|
217 |
{
|
|
218 |
WSADATA wsaData;
|
|
219 |
switch (reason) {
|
|
220 |
case DLL_PROCESS_ATTACH:
|
|
221 |
hJavaInst = hinst;
|
|
222 |
nError = WSAStartup(MAKEWORD(2,0), &wsaData);
|
|
223 |
break;
|
|
224 |
case DLL_PROCESS_DETACH:
|
|
225 |
WSACleanup();
|
|
226 |
hJavaInst = NULL;
|
|
227 |
default:
|
|
228 |
break;
|
|
229 |
}
|
|
230 |
return TRUE;
|
|
231 |
}
|
|
232 |
|
|
233 |
void
|
|
234 |
md_get_prelude_path(char *path, int path_len, char *filename)
|
|
235 |
{
|
|
236 |
char libdir[FILENAME_MAX+1];
|
|
237 |
char *lastSlash;
|
|
238 |
|
|
239 |
GetModuleFileName(hJavaInst, libdir, FILENAME_MAX);
|
|
240 |
|
|
241 |
/* This is actually in the bin directory, so move above bin for lib */
|
|
242 |
lastSlash = strrchr(libdir, '\\');
|
|
243 |
if ( lastSlash != NULL ) {
|
|
244 |
*lastSlash = '\0';
|
|
245 |
}
|
|
246 |
lastSlash = strrchr(libdir, '\\');
|
|
247 |
if ( lastSlash != NULL ) {
|
|
248 |
*lastSlash = '\0';
|
|
249 |
}
|
|
250 |
(void)md_snprintf(path, path_len, "%s\\lib\\%s", libdir, filename);
|
|
251 |
}
|
|
252 |
|
|
253 |
int
|
|
254 |
md_vsnprintf(char *s, int n, const char *format, va_list ap)
|
|
255 |
{
|
|
256 |
return _vsnprintf(s, n, format, ap);
|
|
257 |
}
|
|
258 |
|
|
259 |
int
|
|
260 |
md_snprintf(char *s, int n, const char *format, ...)
|
|
261 |
{
|
|
262 |
int ret;
|
|
263 |
va_list ap;
|
|
264 |
|
|
265 |
va_start(ap, format);
|
|
266 |
ret = md_vsnprintf(s, n, format, ap);
|
|
267 |
va_end(ap);
|
|
268 |
return ret;
|
|
269 |
}
|
|
270 |
|
|
271 |
void
|
|
272 |
md_system_error(char *buf, int len)
|
|
273 |
{
|
|
274 |
long errval;
|
|
275 |
|
|
276 |
errval = GetLastError();
|
|
277 |
buf[0] = '\0';
|
|
278 |
if (errval != 0) {
|
|
279 |
int n;
|
|
280 |
|
|
281 |
n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
282 |
NULL, errval,
|
|
283 |
0, buf, len, NULL);
|
|
284 |
if (n > 3) {
|
|
285 |
/* Drop final '.', CR, LF */
|
|
286 |
if (buf[n - 1] == '\n') n--;
|
|
287 |
if (buf[n - 1] == '\r') n--;
|
|
288 |
if (buf[n - 1] == '.') n--;
|
|
289 |
buf[n] = '\0';
|
|
290 |
}
|
|
291 |
}
|
|
292 |
}
|
|
293 |
|
|
294 |
unsigned
|
|
295 |
md_htons(unsigned short s)
|
|
296 |
{
|
|
297 |
return htons(s);
|
|
298 |
}
|
|
299 |
|
|
300 |
unsigned
|
|
301 |
md_htonl(unsigned l)
|
|
302 |
{
|
|
303 |
return htonl(l);
|
|
304 |
}
|
|
305 |
|
|
306 |
unsigned
|
|
307 |
md_ntohs(unsigned short s)
|
|
308 |
{
|
|
309 |
return ntohs(s);
|
|
310 |
}
|
|
311 |
|
|
312 |
unsigned
|
|
313 |
md_ntohl(unsigned l)
|
|
314 |
{
|
|
315 |
return ntohl(l);
|
|
316 |
}
|
|
317 |
|
|
318 |
static int
|
|
319 |
get_last_error_string(char *buf, int len)
|
|
320 |
{
|
|
321 |
long errval;
|
|
322 |
|
|
323 |
errval = GetLastError();
|
|
324 |
if (errval != 0) {
|
|
325 |
/* DOS error */
|
|
326 |
int n;
|
|
327 |
|
|
328 |
n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
329 |
NULL, errval,
|
|
330 |
0, buf, len, NULL);
|
|
331 |
if (n > 3) {
|
|
332 |
/* Drop final '.', CR, LF */
|
|
333 |
if (buf[n - 1] == '\n') n--;
|
|
334 |
if (buf[n - 1] == '\r') n--;
|
|
335 |
if (buf[n - 1] == '.') n--;
|
|
336 |
buf[n] = '\0';
|
|
337 |
}
|
|
338 |
return n;
|
|
339 |
}
|
|
340 |
|
|
341 |
if (errno != 0) {
|
|
342 |
/* C runtime error that has no corresponding DOS error code */
|
|
343 |
const char *s;
|
|
344 |
int n;
|
|
345 |
|
|
346 |
s = strerror(errno);
|
|
347 |
n = (int)strlen(s);
|
|
348 |
if (n >= len) {
|
|
349 |
n = len - 1;
|
|
350 |
}
|
|
351 |
(void)strncpy(buf, s, n);
|
|
352 |
buf[n] = '\0';
|
|
353 |
return n;
|
|
354 |
}
|
|
355 |
|
|
356 |
return 0;
|
|
357 |
}
|
|
358 |
|
|
359 |
/* Build a machine dependent library name out of a path and file name. */
|
|
360 |
void
|
|
361 |
md_build_library_name(char *holder, int holderlen, char *pname, char *fname)
|
|
362 |
{
|
|
363 |
int pnamelen;
|
|
364 |
char c;
|
|
365 |
|
|
366 |
pnamelen = pname ? (int)strlen(pname) : 0;
|
|
367 |
c = (pnamelen > 0) ? pname[pnamelen-1] : 0;
|
|
368 |
|
|
369 |
/* Quietly truncates on buffer overflow. Should be an error. */
|
|
370 |
if (pnamelen + strlen(fname) + 10 > (unsigned int)holderlen) {
|
|
371 |
*holder = '\0';
|
|
372 |
return;
|
|
373 |
}
|
|
374 |
|
|
375 |
if (pnamelen == 0) {
|
|
376 |
sprintf(holder, "%s.dll", fname);
|
|
377 |
} else if (c == ':' || c == '\\') {
|
|
378 |
sprintf(holder, "%s%s.dll", pname, fname);
|
|
379 |
} else {
|
|
380 |
sprintf(holder, "%s\\%s.dll", pname, fname);
|
|
381 |
}
|
|
382 |
}
|
|
383 |
|
|
384 |
void *
|
|
385 |
md_load_library(const char * name, char *err_buf, int err_buflen)
|
|
386 |
{
|
|
387 |
void *result;
|
|
388 |
|
|
389 |
result = LoadLibrary(name);
|
|
390 |
if (result == NULL) {
|
|
391 |
/* Error message is pretty lame, try to make a better guess. */
|
|
392 |
long errcode;
|
|
393 |
|
|
394 |
errcode = GetLastError();
|
|
395 |
if (errcode == ERROR_MOD_NOT_FOUND) {
|
|
396 |
strncpy(err_buf, "Can't find dependent libraries", err_buflen-2);
|
|
397 |
err_buf[err_buflen-1] = '\0';
|
|
398 |
} else {
|
|
399 |
get_last_error_string(err_buf, err_buflen);
|
|
400 |
}
|
|
401 |
}
|
|
402 |
return result;
|
|
403 |
}
|
|
404 |
|
|
405 |
void
|
|
406 |
md_unload_library(void *handle)
|
|
407 |
{
|
|
408 |
FreeLibrary(handle);
|
|
409 |
}
|
|
410 |
|
|
411 |
void *
|
|
412 |
md_find_library_entry(void *handle, const char *name)
|
|
413 |
{
|
|
414 |
return GetProcAddress(handle, name);
|
|
415 |
}
|