author | nloodin |
Wed, 31 Aug 2011 13:48:04 +0200 | |
changeset 10292 | ed7db6a12c2a |
parent 9035 | 1255eb81cc2f |
child 14698 | 9294fcf94c46 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
7999
diff
changeset
|
2 |
* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 15 |
* - Neither the name of Oracle nor the names of its |
2 | 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 |
||
10292
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9035
diff
changeset
|
32 |
/* |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9035
diff
changeset
|
33 |
* This source code is provided to illustrate the usage of a given feature |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9035
diff
changeset
|
34 |
* or technique and has been deliberately simplified. Additional steps |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9035
diff
changeset
|
35 |
* required for a production-quality application, such as security checks, |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9035
diff
changeset
|
36 |
* input validation and proper error handling, might not be present in |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9035
diff
changeset
|
37 |
* this sample code. |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9035
diff
changeset
|
38 |
*/ |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9035
diff
changeset
|
39 |
|
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9035
diff
changeset
|
40 |
|
7999
059b8737ff55
6983248: net/net001 and net/net003 fail on WinXP with JDK7-B108
zgu
parents:
5506
diff
changeset
|
41 |
// To ensure winsock2.h is used, it has to be included ahead of |
059b8737ff55
6983248: net/net001 and net/net003 fail on WinXP with JDK7-B108
zgu
parents:
5506
diff
changeset
|
42 |
// windows.h, which includes winsock.h by default. |
059b8737ff55
6983248: net/net001 and net/net003 fail on WinXP with JDK7-B108
zgu
parents:
5506
diff
changeset
|
43 |
#include <winsock2.h> |
2 | 44 |
#include <windows.h> |
45 |
#include <io.h> |
|
46 |
#include <sys/types.h> |
|
47 |
#include <sys/stat.h> |
|
48 |
#include <mmsystem.h> |
|
49 |
#include <fcntl.h> |
|
50 |
#include <process.h> |
|
51 |
||
52 |
#include "jni.h" |
|
53 |
#include "hprof.h" |
|
54 |
||
55 |
int |
|
56 |
md_getpid(void) |
|
57 |
{ |
|
58 |
static int pid = -1; |
|
59 |
||
60 |
if ( pid >= 0 ) { |
|
61 |
return pid; |
|
62 |
} |
|
63 |
pid = getpid(); |
|
64 |
return pid; |
|
65 |
} |
|
66 |
||
67 |
void |
|
68 |
md_sleep(unsigned seconds) |
|
69 |
{ |
|
70 |
Sleep((DWORD)seconds*1000); |
|
71 |
} |
|
72 |
||
73 |
void |
|
74 |
md_init(void) |
|
75 |
{ |
|
76 |
} |
|
77 |
||
78 |
int |
|
79 |
md_connect(char *hostname, unsigned short port) |
|
80 |
{ |
|
81 |
struct hostent *hentry; |
|
82 |
struct sockaddr_in s; |
|
83 |
int fd; |
|
84 |
||
85 |
/* create a socket */ |
|
86 |
fd = (int)socket(AF_INET, SOCK_STREAM, 0); |
|
87 |
||
88 |
/* find remote host's addr from name */ |
|
89 |
if ((hentry = gethostbyname(hostname)) == NULL) { |
|
90 |
return -1; |
|
91 |
} |
|
92 |
(void)memset((char *)&s, 0, sizeof(s)); |
|
93 |
/* set remote host's addr; its already in network byte order */ |
|
94 |
(void)memcpy(&s.sin_addr.s_addr, *(hentry->h_addr_list), |
|
95 |
(int)sizeof(s.sin_addr.s_addr)); |
|
96 |
/* set remote host's port */ |
|
97 |
s.sin_port = htons(port); |
|
98 |
s.sin_family = AF_INET; |
|
99 |
||
100 |
/* now try connecting */ |
|
101 |
if (-1 == connect(fd, (struct sockaddr*)&s, sizeof(s))) { |
|
102 |
return 0; |
|
103 |
} |
|
104 |
return fd; |
|
105 |
} |
|
106 |
||
107 |
int |
|
108 |
md_recv(int f, char *buf, int len, int option) |
|
109 |
{ |
|
110 |
return recv(f, buf, len, option); |
|
111 |
} |
|
112 |
||
113 |
int |
|
114 |
md_shutdown(int filedes, int option) |
|
115 |
{ |
|
116 |
return shutdown(filedes, option); |
|
117 |
} |
|
118 |
||
119 |
int |
|
120 |
md_open(const char *filename) |
|
121 |
{ |
|
122 |
return open(filename, O_RDONLY); |
|
123 |
} |
|
124 |
||
125 |
int |
|
126 |
md_open_binary(const char *filename) |
|
127 |
{ |
|
128 |
return open(filename, O_RDONLY|O_BINARY); |
|
129 |
} |
|
130 |
||
131 |
int |
|
132 |
md_creat(const char *filename) |
|
133 |
{ |
|
134 |
return open(filename, O_CREAT | O_WRONLY | O_TRUNC, |
|
135 |
_S_IREAD | _S_IWRITE); |
|
136 |
} |
|
137 |
||
138 |
int |
|
139 |
md_creat_binary(const char *filename) |
|
140 |
{ |
|
141 |
return open(filename, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, |
|
142 |
_S_IREAD | _S_IWRITE); |
|
143 |
} |
|
144 |
||
145 |
jlong |
|
146 |
md_seek(int filedes, jlong pos) |
|
147 |
{ |
|
148 |
jlong new_pos; |
|
149 |
||
150 |
if ( pos == (jlong)-1 ) { |
|
151 |
new_pos = _lseeki64(filedes, 0L, SEEK_END); |
|
152 |
} else { |
|
153 |
new_pos = _lseeki64(filedes, pos, SEEK_SET); |
|
154 |
} |
|
155 |
return new_pos; |
|
156 |
} |
|
157 |
||
158 |
void |
|
159 |
md_close(int filedes) |
|
160 |
{ |
|
7999
059b8737ff55
6983248: net/net001 and net/net003 fail on WinXP with JDK7-B108
zgu
parents:
5506
diff
changeset
|
161 |
(void)closesocket(filedes); |
2 | 162 |
} |
163 |
||
164 |
int |
|
165 |
md_send(int s, const char *msg, int len, int flags) |
|
166 |
{ |
|
167 |
return send(s, msg, len, flags); |
|
168 |
} |
|
169 |
||
170 |
int |
|
171 |
md_read(int filedes, void *buf, int nbyte) |
|
172 |
{ |
|
173 |
return read(filedes, buf, nbyte); |
|
174 |
} |
|
175 |
||
176 |
int |
|
177 |
md_write(int filedes, const void *buf, int nbyte) |
|
178 |
{ |
|
179 |
return write(filedes, buf, nbyte); |
|
180 |
} |
|
181 |
||
182 |
jlong |
|
183 |
md_get_microsecs(void) |
|
184 |
{ |
|
185 |
return (jlong)(timeGetTime())*(jlong)1000; |
|
186 |
} |
|
187 |
||
188 |
#define FT2JLONG(ft) \ |
|
189 |
((jlong)(ft).dwHighDateTime << 32 | (jlong)(ft).dwLowDateTime) |
|
190 |
||
191 |
jlong |
|
192 |
md_get_timemillis(void) |
|
193 |
{ |
|
194 |
static jlong fileTime_1_1_70 = 0; |
|
195 |
SYSTEMTIME st0; |
|
196 |
FILETIME ft0; |
|
197 |
||
198 |
if (fileTime_1_1_70 == 0) { |
|
199 |
/* Initialize fileTime_1_1_70 -- the Win32 file time of midnight |
|
200 |
* 1/1/70. |
|
201 |
*/ |
|
202 |
||
203 |
memset(&st0, 0, sizeof(st0)); |
|
204 |
st0.wYear = 1970; |
|
205 |
st0.wMonth = 1; |
|
206 |
st0.wDay = 1; |
|
207 |
SystemTimeToFileTime(&st0, &ft0); |
|
208 |
fileTime_1_1_70 = FT2JLONG(ft0); |
|
209 |
} |
|
210 |
||
211 |
GetSystemTime(&st0); |
|
212 |
SystemTimeToFileTime(&st0, &ft0); |
|
213 |
||
214 |
return (FT2JLONG(ft0) - fileTime_1_1_70) / 10000; |
|
215 |
} |
|
216 |
||
217 |
jlong |
|
218 |
md_get_thread_cpu_timemillis(void) |
|
219 |
{ |
|
220 |
return md_get_timemillis(); |
|
221 |
} |
|
222 |
||
223 |
HINSTANCE hJavaInst; |
|
224 |
static int nError = 0; |
|
225 |
||
226 |
BOOL WINAPI |
|
227 |
DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved) |
|
228 |
{ |
|
229 |
WSADATA wsaData; |
|
230 |
switch (reason) { |
|
231 |
case DLL_PROCESS_ATTACH: |
|
232 |
hJavaInst = hinst; |
|
233 |
nError = WSAStartup(MAKEWORD(2,0), &wsaData); |
|
234 |
break; |
|
235 |
case DLL_PROCESS_DETACH: |
|
236 |
WSACleanup(); |
|
237 |
hJavaInst = NULL; |
|
238 |
default: |
|
239 |
break; |
|
240 |
} |
|
241 |
return TRUE; |
|
242 |
} |
|
243 |
||
244 |
void |
|
245 |
md_get_prelude_path(char *path, int path_len, char *filename) |
|
246 |
{ |
|
247 |
char libdir[FILENAME_MAX+1]; |
|
248 |
char *lastSlash; |
|
249 |
||
250 |
GetModuleFileName(hJavaInst, libdir, FILENAME_MAX); |
|
251 |
||
252 |
/* This is actually in the bin directory, so move above bin for lib */ |
|
253 |
lastSlash = strrchr(libdir, '\\'); |
|
254 |
if ( lastSlash != NULL ) { |
|
255 |
*lastSlash = '\0'; |
|
256 |
} |
|
257 |
lastSlash = strrchr(libdir, '\\'); |
|
258 |
if ( lastSlash != NULL ) { |
|
259 |
*lastSlash = '\0'; |
|
260 |
} |
|
261 |
(void)md_snprintf(path, path_len, "%s\\lib\\%s", libdir, filename); |
|
262 |
} |
|
263 |
||
264 |
int |
|
265 |
md_vsnprintf(char *s, int n, const char *format, va_list ap) |
|
266 |
{ |
|
267 |
return _vsnprintf(s, n, format, ap); |
|
268 |
} |
|
269 |
||
270 |
int |
|
271 |
md_snprintf(char *s, int n, const char *format, ...) |
|
272 |
{ |
|
273 |
int ret; |
|
274 |
va_list ap; |
|
275 |
||
276 |
va_start(ap, format); |
|
277 |
ret = md_vsnprintf(s, n, format, ap); |
|
278 |
va_end(ap); |
|
279 |
return ret; |
|
280 |
} |
|
281 |
||
282 |
void |
|
283 |
md_system_error(char *buf, int len) |
|
284 |
{ |
|
285 |
long errval; |
|
286 |
||
287 |
errval = GetLastError(); |
|
288 |
buf[0] = '\0'; |
|
289 |
if (errval != 0) { |
|
290 |
int n; |
|
291 |
||
292 |
n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, |
|
293 |
NULL, errval, |
|
294 |
0, buf, len, NULL); |
|
295 |
if (n > 3) { |
|
296 |
/* Drop final '.', CR, LF */ |
|
297 |
if (buf[n - 1] == '\n') n--; |
|
298 |
if (buf[n - 1] == '\r') n--; |
|
299 |
if (buf[n - 1] == '.') n--; |
|
300 |
buf[n] = '\0'; |
|
301 |
} |
|
302 |
} |
|
303 |
} |
|
304 |
||
305 |
unsigned |
|
306 |
md_htons(unsigned short s) |
|
307 |
{ |
|
308 |
return htons(s); |
|
309 |
} |
|
310 |
||
311 |
unsigned |
|
312 |
md_htonl(unsigned l) |
|
313 |
{ |
|
314 |
return htonl(l); |
|
315 |
} |
|
316 |
||
317 |
unsigned |
|
318 |
md_ntohs(unsigned short s) |
|
319 |
{ |
|
320 |
return ntohs(s); |
|
321 |
} |
|
322 |
||
323 |
unsigned |
|
324 |
md_ntohl(unsigned l) |
|
325 |
{ |
|
326 |
return ntohl(l); |
|
327 |
} |
|
328 |
||
329 |
static int |
|
330 |
get_last_error_string(char *buf, int len) |
|
331 |
{ |
|
332 |
long errval; |
|
333 |
||
334 |
errval = GetLastError(); |
|
335 |
if (errval != 0) { |
|
336 |
/* DOS error */ |
|
337 |
int n; |
|
338 |
||
339 |
n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, |
|
340 |
NULL, errval, |
|
341 |
0, buf, len, NULL); |
|
342 |
if (n > 3) { |
|
343 |
/* Drop final '.', CR, LF */ |
|
344 |
if (buf[n - 1] == '\n') n--; |
|
345 |
if (buf[n - 1] == '\r') n--; |
|
346 |
if (buf[n - 1] == '.') n--; |
|
347 |
buf[n] = '\0'; |
|
348 |
} |
|
349 |
return n; |
|
350 |
} |
|
351 |
||
352 |
if (errno != 0) { |
|
353 |
/* C runtime error that has no corresponding DOS error code */ |
|
354 |
const char *s; |
|
355 |
int n; |
|
356 |
||
357 |
s = strerror(errno); |
|
358 |
n = (int)strlen(s); |
|
359 |
if (n >= len) { |
|
360 |
n = len - 1; |
|
361 |
} |
|
362 |
(void)strncpy(buf, s, n); |
|
363 |
buf[n] = '\0'; |
|
364 |
return n; |
|
365 |
} |
|
366 |
||
367 |
return 0; |
|
368 |
} |
|
369 |
||
370 |
/* Build a machine dependent library name out of a path and file name. */ |
|
371 |
void |
|
372 |
md_build_library_name(char *holder, int holderlen, char *pname, char *fname) |
|
373 |
{ |
|
374 |
int pnamelen; |
|
375 |
char c; |
|
376 |
||
377 |
pnamelen = pname ? (int)strlen(pname) : 0; |
|
378 |
c = (pnamelen > 0) ? pname[pnamelen-1] : 0; |
|
379 |
||
380 |
/* Quietly truncates on buffer overflow. Should be an error. */ |
|
381 |
if (pnamelen + strlen(fname) + 10 > (unsigned int)holderlen) { |
|
382 |
*holder = '\0'; |
|
383 |
return; |
|
384 |
} |
|
385 |
||
386 |
if (pnamelen == 0) { |
|
387 |
sprintf(holder, "%s.dll", fname); |
|
388 |
} else if (c == ':' || c == '\\') { |
|
389 |
sprintf(holder, "%s%s.dll", pname, fname); |
|
390 |
} else { |
|
391 |
sprintf(holder, "%s\\%s.dll", pname, fname); |
|
392 |
} |
|
393 |
} |
|
394 |
||
395 |
void * |
|
396 |
md_load_library(const char * name, char *err_buf, int err_buflen) |
|
397 |
{ |
|
398 |
void *result; |
|
399 |
||
400 |
result = LoadLibrary(name); |
|
401 |
if (result == NULL) { |
|
402 |
/* Error message is pretty lame, try to make a better guess. */ |
|
403 |
long errcode; |
|
404 |
||
405 |
errcode = GetLastError(); |
|
406 |
if (errcode == ERROR_MOD_NOT_FOUND) { |
|
407 |
strncpy(err_buf, "Can't find dependent libraries", err_buflen-2); |
|
408 |
err_buf[err_buflen-1] = '\0'; |
|
409 |
} else { |
|
410 |
get_last_error_string(err_buf, err_buflen); |
|
411 |
} |
|
412 |
} |
|
413 |
return result; |
|
414 |
} |
|
415 |
||
416 |
void |
|
417 |
md_unload_library(void *handle) |
|
418 |
{ |
|
419 |
FreeLibrary(handle); |
|
420 |
} |
|
421 |
||
422 |
void * |
|
423 |
md_find_library_entry(void *handle, const char *name) |
|
424 |
{ |
|
425 |
return GetProcAddress(handle, name); |
|
426 |
} |