|
1 /* |
|
2 * Copyright 1997-2007 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. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any questions. |
|
24 */ |
|
25 |
|
26 #include <windows.h> |
|
27 #include <io.h> |
|
28 #include <process.h> |
|
29 #include <stdlib.h> |
|
30 #include <stdio.h> |
|
31 #include <stdarg.h> |
|
32 #include <string.h> |
|
33 #include <sys/types.h> |
|
34 #include <sys/stat.h> |
|
35 #include <wtypes.h> |
|
36 |
|
37 #include <jni.h> |
|
38 #include "java.h" |
|
39 #include "version_comp.h" |
|
40 |
|
41 #define JVM_DLL "jvm.dll" |
|
42 #define JAVA_DLL "java.dll" |
|
43 #define CRT_DLL "msvcr71.dll" |
|
44 |
|
45 /* |
|
46 * Prototypes. |
|
47 */ |
|
48 static jboolean GetPublicJREHome(char *path, jint pathsize); |
|
49 static jboolean GetJVMPath(const char *jrepath, const char *jvmtype, |
|
50 char *jvmpath, jint jvmpathsize); |
|
51 static jboolean GetJREPath(char *path, jint pathsize); |
|
52 |
|
53 static jboolean _isjavaw = JNI_FALSE; |
|
54 |
|
55 void |
|
56 SetJavaw() |
|
57 { |
|
58 _isjavaw = JNI_TRUE; |
|
59 } |
|
60 |
|
61 jboolean |
|
62 IsJavaw() |
|
63 { |
|
64 return _isjavaw; |
|
65 } |
|
66 |
|
67 /* |
|
68 * Returns the arch path, to get the current arch use the |
|
69 * macro GetArch, nbits here is ignored for now. |
|
70 */ |
|
71 const char * |
|
72 GetArchPath(int nbits) |
|
73 { |
|
74 #ifdef _M_AMD64 |
|
75 return "amd64"; |
|
76 #elif defined(_M_IA64) |
|
77 return "ia64"; |
|
78 #else |
|
79 return "i386"; |
|
80 #endif |
|
81 } |
|
82 |
|
83 /* |
|
84 * |
|
85 */ |
|
86 void |
|
87 CreateExecutionEnvironment(int *_argc, |
|
88 char ***_argv, |
|
89 char jrepath[], |
|
90 jint so_jrepath, |
|
91 char jvmpath[], |
|
92 jint so_jvmpath, |
|
93 char **original_argv) { |
|
94 char * jvmtype; |
|
95 int i = 0; |
|
96 char** pargv = *_argv; |
|
97 int running = CURRENT_DATA_MODEL; |
|
98 |
|
99 int wanted = running; |
|
100 |
|
101 for (i = 0; i < *_argc ; i++) { |
|
102 if (JLI_StrCmp(pargv[i], "-J-d64") == 0 || JLI_StrCmp(pargv[i], "-d64") == 0) { |
|
103 wanted = 64; |
|
104 continue; |
|
105 } |
|
106 if (JLI_StrCmp(pargv[i], "-J-d32") == 0 || JLI_StrCmp(pargv[i], "-d32") == 0) { |
|
107 wanted = 32; |
|
108 continue; |
|
109 } |
|
110 } |
|
111 if (running != wanted) { |
|
112 ReportErrorMessage(JRE_ERROR2, wanted); |
|
113 exit(1); |
|
114 } |
|
115 |
|
116 /* Find out where the JRE is that we will be using. */ |
|
117 if (!GetJREPath(jrepath, so_jrepath)) { |
|
118 ReportErrorMessage(JRE_ERROR1); |
|
119 exit(2); |
|
120 } |
|
121 |
|
122 /* Find the specified JVM type */ |
|
123 if (ReadKnownVMs(jrepath, (char*)GetArch(), JNI_FALSE) < 1) { |
|
124 ReportErrorMessage(CFG_ERROR7); |
|
125 exit(1); |
|
126 } |
|
127 jvmtype = CheckJvmType(_argc, _argv, JNI_FALSE); |
|
128 |
|
129 jvmpath[0] = '\0'; |
|
130 if (!GetJVMPath(jrepath, jvmtype, jvmpath, so_jvmpath)) { |
|
131 ReportErrorMessage(CFG_ERROR8, jvmtype, jvmpath); |
|
132 exit(4); |
|
133 } |
|
134 /* If we got here, jvmpath has been correctly initialized. */ |
|
135 |
|
136 } |
|
137 |
|
138 /* |
|
139 * Find path to JRE based on .exe's location or registry settings. |
|
140 */ |
|
141 jboolean |
|
142 GetJREPath(char *path, jint pathsize) |
|
143 { |
|
144 char javadll[MAXPATHLEN]; |
|
145 struct stat s; |
|
146 |
|
147 if (GetApplicationHome(path, pathsize)) { |
|
148 /* Is JRE co-located with the application? */ |
|
149 sprintf(javadll, "%s\\bin\\" JAVA_DLL, path); |
|
150 if (stat(javadll, &s) == 0) { |
|
151 goto found; |
|
152 } |
|
153 |
|
154 /* Does this app ship a private JRE in <apphome>\jre directory? */ |
|
155 sprintf(javadll, "%s\\jre\\bin\\" JAVA_DLL, path); |
|
156 if (stat(javadll, &s) == 0) { |
|
157 JLI_StrCat(path, "\\jre"); |
|
158 goto found; |
|
159 } |
|
160 } |
|
161 |
|
162 /* Look for a public JRE on this machine. */ |
|
163 if (GetPublicJREHome(path, pathsize)) { |
|
164 goto found; |
|
165 } |
|
166 |
|
167 ReportErrorMessage(JRE_ERROR8 JAVA_DLL); |
|
168 return JNI_FALSE; |
|
169 |
|
170 found: |
|
171 JLI_TraceLauncher("JRE path is %s\n", path); |
|
172 return JNI_TRUE; |
|
173 } |
|
174 |
|
175 /* |
|
176 * Given a JRE location and a JVM type, construct what the name the |
|
177 * JVM shared library will be. Return true, if such a library |
|
178 * exists, false otherwise. |
|
179 */ |
|
180 static jboolean |
|
181 GetJVMPath(const char *jrepath, const char *jvmtype, |
|
182 char *jvmpath, jint jvmpathsize) |
|
183 { |
|
184 struct stat s; |
|
185 if (JLI_StrChr(jvmtype, '/') || JLI_StrChr(jvmtype, '\\')) { |
|
186 sprintf(jvmpath, "%s\\" JVM_DLL, jvmtype); |
|
187 } else { |
|
188 sprintf(jvmpath, "%s\\bin\\%s\\" JVM_DLL, jrepath, jvmtype); |
|
189 } |
|
190 if (stat(jvmpath, &s) == 0) { |
|
191 return JNI_TRUE; |
|
192 } else { |
|
193 return JNI_FALSE; |
|
194 } |
|
195 } |
|
196 |
|
197 /* |
|
198 * Load a jvm from "jvmpath" and initialize the invocation functions. |
|
199 */ |
|
200 jboolean |
|
201 LoadJavaVM(const char *jvmpath, InvocationFunctions *ifn) |
|
202 { |
|
203 HINSTANCE handle; |
|
204 char crtpath[MAXPATHLEN]; |
|
205 |
|
206 JLI_TraceLauncher("JVM path is %s\n", jvmpath); |
|
207 |
|
208 /* |
|
209 * The Microsoft C Runtime Library needs to be loaded first. A copy is |
|
210 * assumed to be present in the "JRE path" directory. If it is not found |
|
211 * there (or "JRE path" fails to resolve), skip the explicit load and let |
|
212 * nature take its course, which is likely to be a failure to execute. |
|
213 */ |
|
214 if (GetJREPath(crtpath, MAXPATHLEN)) { |
|
215 (void)JLI_StrCat(crtpath, "\\bin\\" CRT_DLL); /* Add crt dll */ |
|
216 JLI_TraceLauncher("CRT path is %s\n", crtpath); |
|
217 if (_access(crtpath, 0) == 0) { |
|
218 if (LoadLibrary(crtpath) == 0) { |
|
219 ReportErrorMessage(DLL_ERROR4, crtpath); |
|
220 return JNI_FALSE; |
|
221 } |
|
222 } |
|
223 } |
|
224 |
|
225 /* Load the Java VM DLL */ |
|
226 if ((handle = LoadLibrary(jvmpath)) == 0) { |
|
227 ReportErrorMessage(DLL_ERROR4, (char *)jvmpath); |
|
228 return JNI_FALSE; |
|
229 } |
|
230 |
|
231 /* Now get the function addresses */ |
|
232 ifn->CreateJavaVM = |
|
233 (void *)GetProcAddress(handle, "JNI_CreateJavaVM"); |
|
234 ifn->GetDefaultJavaVMInitArgs = |
|
235 (void *)GetProcAddress(handle, "JNI_GetDefaultJavaVMInitArgs"); |
|
236 if (ifn->CreateJavaVM == 0 || ifn->GetDefaultJavaVMInitArgs == 0) { |
|
237 ReportErrorMessage(JNI_ERROR1, (char *)jvmpath); |
|
238 return JNI_FALSE; |
|
239 } |
|
240 |
|
241 return JNI_TRUE; |
|
242 } |
|
243 |
|
244 /* |
|
245 * If app is "c:\foo\bin\javac", then put "c:\foo" into buf. |
|
246 */ |
|
247 jboolean |
|
248 GetApplicationHome(char *buf, jint bufsize) |
|
249 { |
|
250 char *cp; |
|
251 GetModuleFileName(0, buf, bufsize); |
|
252 *JLI_StrRChr(buf, '\\') = '\0'; /* remove .exe file name */ |
|
253 if ((cp = JLI_StrRChr(buf, '\\')) == 0) { |
|
254 /* This happens if the application is in a drive root, and |
|
255 * there is no bin directory. */ |
|
256 buf[0] = '\0'; |
|
257 return JNI_FALSE; |
|
258 } |
|
259 *cp = '\0'; /* remove the bin\ part */ |
|
260 return JNI_TRUE; |
|
261 } |
|
262 |
|
263 /* |
|
264 * Helpers to look in the registry for a public JRE. |
|
265 */ |
|
266 /* Same for 1.5.0, 1.5.1, 1.5.2 etc. */ |
|
267 #define JRE_KEY "Software\\JavaSoft\\Java Runtime Environment" |
|
268 |
|
269 static jboolean |
|
270 GetStringFromRegistry(HKEY key, const char *name, char *buf, jint bufsize) |
|
271 { |
|
272 DWORD type, size; |
|
273 |
|
274 if (RegQueryValueEx(key, name, 0, &type, 0, &size) == 0 |
|
275 && type == REG_SZ |
|
276 && (size < (unsigned int)bufsize)) { |
|
277 if (RegQueryValueEx(key, name, 0, 0, buf, &size) == 0) { |
|
278 return JNI_TRUE; |
|
279 } |
|
280 } |
|
281 return JNI_FALSE; |
|
282 } |
|
283 |
|
284 static jboolean |
|
285 GetPublicJREHome(char *buf, jint bufsize) |
|
286 { |
|
287 HKEY key, subkey; |
|
288 char version[MAXPATHLEN]; |
|
289 |
|
290 /* |
|
291 * Note: There is a very similar implementation of the following |
|
292 * registry reading code in the Windows java control panel (javacp.cpl). |
|
293 * If there are bugs here, a similar bug probably exists there. Hence, |
|
294 * changes here require inspection there. |
|
295 */ |
|
296 |
|
297 /* Find the current version of the JRE */ |
|
298 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, JRE_KEY, 0, KEY_READ, &key) != 0) { |
|
299 ReportErrorMessage(REG_ERROR1, JRE_KEY); |
|
300 return JNI_FALSE; |
|
301 } |
|
302 |
|
303 if (!GetStringFromRegistry(key, "CurrentVersion", |
|
304 version, sizeof(version))) { |
|
305 ReportErrorMessage(REG_ERROR2, JRE_KEY); |
|
306 RegCloseKey(key); |
|
307 return JNI_FALSE; |
|
308 } |
|
309 |
|
310 if (JLI_StrCmp(version, GetDotVersion()) != 0) { |
|
311 ReportErrorMessage(REG_ERROR3, JRE_KEY, version, GetDotVersion() |
|
312 ); |
|
313 RegCloseKey(key); |
|
314 return JNI_FALSE; |
|
315 } |
|
316 |
|
317 /* Find directory where the current version is installed. */ |
|
318 if (RegOpenKeyEx(key, version, 0, KEY_READ, &subkey) != 0) { |
|
319 ReportErrorMessage(REG_ERROR1, JRE_KEY, version); |
|
320 RegCloseKey(key); |
|
321 return JNI_FALSE; |
|
322 } |
|
323 |
|
324 if (!GetStringFromRegistry(subkey, "JavaHome", buf, bufsize)) { |
|
325 ReportErrorMessage(REG_ERROR4, JRE_KEY, version); |
|
326 RegCloseKey(key); |
|
327 RegCloseKey(subkey); |
|
328 return JNI_FALSE; |
|
329 } |
|
330 |
|
331 if (JLI_IsTraceLauncher()) { |
|
332 char micro[MAXPATHLEN]; |
|
333 if (!GetStringFromRegistry(subkey, "MicroVersion", micro, |
|
334 sizeof(micro))) { |
|
335 printf("Warning: Can't read MicroVersion\n"); |
|
336 micro[0] = '\0'; |
|
337 } |
|
338 printf("Version major.minor.micro = %s.%s\n", version, micro); |
|
339 } |
|
340 |
|
341 RegCloseKey(key); |
|
342 RegCloseKey(subkey); |
|
343 return JNI_TRUE; |
|
344 } |
|
345 |
|
346 /* |
|
347 * Support for doing cheap, accurate interval timing. |
|
348 */ |
|
349 static jboolean counterAvailable = JNI_FALSE; |
|
350 static jboolean counterInitialized = JNI_FALSE; |
|
351 static LARGE_INTEGER counterFrequency; |
|
352 |
|
353 jlong CounterGet() |
|
354 { |
|
355 LARGE_INTEGER count; |
|
356 |
|
357 if (!counterInitialized) { |
|
358 counterAvailable = QueryPerformanceFrequency(&counterFrequency); |
|
359 counterInitialized = JNI_TRUE; |
|
360 } |
|
361 if (!counterAvailable) { |
|
362 return 0; |
|
363 } |
|
364 QueryPerformanceCounter(&count); |
|
365 return (jlong)(count.QuadPart); |
|
366 } |
|
367 |
|
368 jlong Counter2Micros(jlong counts) |
|
369 { |
|
370 if (!counterAvailable || !counterInitialized) { |
|
371 return 0; |
|
372 } |
|
373 return (counts * 1000 * 1000)/counterFrequency.QuadPart; |
|
374 } |
|
375 |
|
376 void |
|
377 ReportErrorMessage(const char* fmt, ...) { |
|
378 va_list vl; |
|
379 va_start(vl,fmt); |
|
380 |
|
381 if (IsJavaw()) { |
|
382 char *message; |
|
383 |
|
384 /* get the length of the string we need */ |
|
385 int n = _vscprintf(fmt, vl); |
|
386 |
|
387 message = (char *)JLI_MemAlloc(n + 1); |
|
388 _vsnprintf(message, n, fmt, vl); |
|
389 message[n]='\0'; |
|
390 MessageBox(NULL, message, "Java Virtual Machine Launcher", |
|
391 (MB_OK|MB_ICONSTOP|MB_APPLMODAL)); |
|
392 JLI_MemFree(message); |
|
393 } else { |
|
394 vfprintf(stderr, fmt, vl); |
|
395 fprintf(stderr, "\n"); |
|
396 } |
|
397 va_end(vl); |
|
398 } |
|
399 |
|
400 /* |
|
401 * Just like ReportErrorMessage, except that it concatenates the system |
|
402 * error message if any, its upto the calling routine to correctly |
|
403 * format the separation of the messages. |
|
404 */ |
|
405 void |
|
406 ReportErrorMessageSys(const char *fmt, ...) |
|
407 { |
|
408 va_list vl; |
|
409 |
|
410 int save_errno = errno; |
|
411 DWORD errval; |
|
412 jboolean freeit = JNI_FALSE; |
|
413 char *errtext = NULL; |
|
414 |
|
415 va_start(vl, fmt); |
|
416 |
|
417 if ((errval = GetLastError()) != 0) { /* Platform SDK / DOS Error */ |
|
418 int n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM| |
|
419 FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_ALLOCATE_BUFFER, |
|
420 NULL, errval, 0, (LPTSTR)&errtext, 0, NULL); |
|
421 if (errtext == NULL || n == 0) { /* Paranoia check */ |
|
422 errtext = ""; |
|
423 n = 0; |
|
424 } else { |
|
425 freeit = JNI_TRUE; |
|
426 if (n > 2) { /* Drop final CR, LF */ |
|
427 if (errtext[n - 1] == '\n') n--; |
|
428 if (errtext[n - 1] == '\r') n--; |
|
429 errtext[n] = '\0'; |
|
430 } |
|
431 } |
|
432 } else { /* C runtime error that has no corresponding DOS error code */ |
|
433 errtext = strerror(save_errno); |
|
434 } |
|
435 |
|
436 if (IsJavaw()) { |
|
437 char *message; |
|
438 int mlen; |
|
439 /* get the length of the string we need */ |
|
440 int len = mlen = _vscprintf(fmt, vl) + 1; |
|
441 if (freeit) { |
|
442 mlen += JLI_StrLen(errtext); |
|
443 } |
|
444 |
|
445 message = (char *)JLI_MemAlloc(mlen); |
|
446 _vsnprintf(message, len, fmt, vl); |
|
447 message[len]='\0'; |
|
448 |
|
449 if (freeit) { |
|
450 JLI_StrCat(message, errtext); |
|
451 } |
|
452 |
|
453 MessageBox(NULL, message, "Java Virtual Machine Launcher", |
|
454 (MB_OK|MB_ICONSTOP|MB_APPLMODAL)); |
|
455 |
|
456 JLI_MemFree(message); |
|
457 } else { |
|
458 vfprintf(stderr, fmt, vl); |
|
459 if (freeit) { |
|
460 fprintf(stderr, "%s", errtext); |
|
461 } |
|
462 } |
|
463 if (freeit) { |
|
464 (void)LocalFree((HLOCAL)errtext); |
|
465 } |
|
466 va_end(vl); |
|
467 } |
|
468 |
|
469 void ReportExceptionDescription(JNIEnv * env) { |
|
470 if (IsJavaw()) { |
|
471 /* |
|
472 * This code should be replaced by code which opens a window with |
|
473 * the exception detail message, for now atleast put a dialog up. |
|
474 */ |
|
475 MessageBox(NULL, "A Java Exception has occurred.", "Java Virtual Machine Launcher", |
|
476 (MB_OK|MB_ICONSTOP|MB_APPLMODAL)); |
|
477 } else { |
|
478 (*env)->ExceptionDescribe(env); |
|
479 } |
|
480 } |
|
481 |
|
482 jboolean |
|
483 ServerClassMachine() { |
|
484 return (GetErgoPolicy() == ALWAYS_SERVER_CLASS) ? JNI_TRUE : JNI_FALSE; |
|
485 } |
|
486 |
|
487 /* |
|
488 * Determine if there is an acceptable JRE in the registry directory top_key. |
|
489 * Upon locating the "best" one, return a fully qualified path to it. |
|
490 * "Best" is defined as the most advanced JRE meeting the constraints |
|
491 * contained in the manifest_info. If no JRE in this directory meets the |
|
492 * constraints, return NULL. |
|
493 * |
|
494 * It doesn't matter if we get an error reading the registry, or we just |
|
495 * don't find anything interesting in the directory. We just return NULL |
|
496 * in either case. |
|
497 */ |
|
498 static char * |
|
499 ProcessDir(manifest_info* info, HKEY top_key) { |
|
500 DWORD index = 0; |
|
501 HKEY ver_key; |
|
502 char name[MAXNAMELEN]; |
|
503 int len; |
|
504 char *best = NULL; |
|
505 |
|
506 /* |
|
507 * Enumerate "<top_key>/SOFTWARE/JavaSoft/Java Runtime Environment" |
|
508 * searching for the best available version. |
|
509 */ |
|
510 while (RegEnumKey(top_key, index, name, MAXNAMELEN) == ERROR_SUCCESS) { |
|
511 index++; |
|
512 if (JLI_AcceptableRelease(name, info->jre_version)) |
|
513 if ((best == NULL) || (JLI_ExactVersionId(name, best) > 0)) { |
|
514 if (best != NULL) |
|
515 JLI_MemFree(best); |
|
516 best = JLI_StringDup(name); |
|
517 } |
|
518 } |
|
519 |
|
520 /* |
|
521 * Extract "JavaHome" from the "best" registry directory and return |
|
522 * that path. If no appropriate version was located, or there is an |
|
523 * error in extracting the "JavaHome" string, return null. |
|
524 */ |
|
525 if (best == NULL) |
|
526 return (NULL); |
|
527 else { |
|
528 if (RegOpenKeyEx(top_key, best, 0, KEY_READ, &ver_key) |
|
529 != ERROR_SUCCESS) { |
|
530 JLI_MemFree(best); |
|
531 if (ver_key != NULL) |
|
532 RegCloseKey(ver_key); |
|
533 return (NULL); |
|
534 } |
|
535 JLI_MemFree(best); |
|
536 len = MAXNAMELEN; |
|
537 if (RegQueryValueEx(ver_key, "JavaHome", NULL, NULL, (LPBYTE)name, &len) |
|
538 != ERROR_SUCCESS) { |
|
539 if (ver_key != NULL) |
|
540 RegCloseKey(ver_key); |
|
541 return (NULL); |
|
542 } |
|
543 if (ver_key != NULL) |
|
544 RegCloseKey(ver_key); |
|
545 return (JLI_StringDup(name)); |
|
546 } |
|
547 } |
|
548 |
|
549 /* |
|
550 * This is the global entry point. It examines the host for the optimal |
|
551 * JRE to be used by scanning a set of registry entries. This set of entries |
|
552 * is hardwired on Windows as "Software\JavaSoft\Java Runtime Environment" |
|
553 * under the set of roots "{ HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE }". |
|
554 * |
|
555 * This routine simply opens each of these registry directories before passing |
|
556 * control onto ProcessDir(). |
|
557 */ |
|
558 char * |
|
559 LocateJRE(manifest_info* info) { |
|
560 HKEY key = NULL; |
|
561 char *path; |
|
562 int key_index; |
|
563 HKEY root_keys[2] = { HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE }; |
|
564 |
|
565 for (key_index = 0; key_index <= 1; key_index++) { |
|
566 if (RegOpenKeyEx(root_keys[key_index], JRE_KEY, 0, KEY_READ, &key) |
|
567 == ERROR_SUCCESS) |
|
568 if ((path = ProcessDir(info, key)) != NULL) { |
|
569 if (key != NULL) |
|
570 RegCloseKey(key); |
|
571 return (path); |
|
572 } |
|
573 if (key != NULL) |
|
574 RegCloseKey(key); |
|
575 } |
|
576 return NULL; |
|
577 } |
|
578 |
|
579 /* |
|
580 * Local helper routine to isolate a single token (option or argument) |
|
581 * from the command line. |
|
582 * |
|
583 * This routine accepts a pointer to a character pointer. The first |
|
584 * token (as defined by MSDN command-line argument syntax) is isolated |
|
585 * from that string. |
|
586 * |
|
587 * Upon return, the input character pointer pointed to by the parameter s |
|
588 * is updated to point to the remainding, unscanned, portion of the string, |
|
589 * or to a null character if the entire string has been consummed. |
|
590 * |
|
591 * This function returns a pointer to a null-terminated string which |
|
592 * contains the isolated first token, or to the null character if no |
|
593 * token could be isolated. |
|
594 * |
|
595 * Note the side effect of modifying the input string s by the insertion |
|
596 * of a null character, making it two strings. |
|
597 * |
|
598 * See "Parsing C Command-Line Arguments" in the MSDN Library for the |
|
599 * parsing rule details. The rule summary from that specification is: |
|
600 * |
|
601 * * Arguments are delimited by white space, which is either a space or a tab. |
|
602 * |
|
603 * * A string surrounded by double quotation marks is interpreted as a single |
|
604 * argument, regardless of white space contained within. A quoted string can |
|
605 * be embedded in an argument. Note that the caret (^) is not recognized as |
|
606 * an escape character or delimiter. |
|
607 * |
|
608 * * A double quotation mark preceded by a backslash, \", is interpreted as a |
|
609 * literal double quotation mark ("). |
|
610 * |
|
611 * * Backslashes are interpreted literally, unless they immediately precede a |
|
612 * double quotation mark. |
|
613 * |
|
614 * * If an even number of backslashes is followed by a double quotation mark, |
|
615 * then one backslash (\) is placed in the argv array for every pair of |
|
616 * backslashes (\\), and the double quotation mark (") is interpreted as a |
|
617 * string delimiter. |
|
618 * |
|
619 * * If an odd number of backslashes is followed by a double quotation mark, |
|
620 * then one backslash (\) is placed in the argv array for every pair of |
|
621 * backslashes (\\) and the double quotation mark is interpreted as an |
|
622 * escape sequence by the remaining backslash, causing a literal double |
|
623 * quotation mark (") to be placed in argv. |
|
624 */ |
|
625 static char* |
|
626 nextarg(char** s) { |
|
627 char *p = *s; |
|
628 char *head; |
|
629 int slashes = 0; |
|
630 int inquote = 0; |
|
631 |
|
632 /* |
|
633 * Strip leading whitespace, which MSDN defines as only space or tab. |
|
634 * (Hence, no locale specific "isspace" here.) |
|
635 */ |
|
636 while (*p != (char)0 && (*p == ' ' || *p == '\t')) |
|
637 p++; |
|
638 head = p; /* Save the start of the token to return */ |
|
639 |
|
640 /* |
|
641 * Isolate a token from the command line. |
|
642 */ |
|
643 while (*p != (char)0 && (inquote || !(*p == ' ' || *p == '\t'))) { |
|
644 if (*p == '\\' && *(p+1) == '"' && slashes % 2 == 0) |
|
645 p++; |
|
646 else if (*p == '"') |
|
647 inquote = !inquote; |
|
648 slashes = (*p++ == '\\') ? slashes + 1 : 0; |
|
649 } |
|
650 |
|
651 /* |
|
652 * If the token isolated isn't already terminated in a "char zero", |
|
653 * then replace the whitespace character with one and move to the |
|
654 * next character. |
|
655 */ |
|
656 if (*p != (char)0) |
|
657 *p++ = (char)0; |
|
658 |
|
659 /* |
|
660 * Update the parameter to point to the head of the remaining string |
|
661 * reflecting the command line and return a pointer to the leading |
|
662 * token which was isolated from the command line. |
|
663 */ |
|
664 *s = p; |
|
665 return (head); |
|
666 } |
|
667 |
|
668 /* |
|
669 * Local helper routine to return a string equivalent to the input string |
|
670 * s, but with quotes removed so the result is a string as would be found |
|
671 * in argv[]. The returned string should be freed by a call to JLI_MemFree(). |
|
672 * |
|
673 * The rules for quoting (and escaped quotes) are: |
|
674 * |
|
675 * 1 A double quotation mark preceded by a backslash, \", is interpreted as a |
|
676 * literal double quotation mark ("). |
|
677 * |
|
678 * 2 Backslashes are interpreted literally, unless they immediately precede a |
|
679 * double quotation mark. |
|
680 * |
|
681 * 3 If an even number of backslashes is followed by a double quotation mark, |
|
682 * then one backslash (\) is placed in the argv array for every pair of |
|
683 * backslashes (\\), and the double quotation mark (") is interpreted as a |
|
684 * string delimiter. |
|
685 * |
|
686 * 4 If an odd number of backslashes is followed by a double quotation mark, |
|
687 * then one backslash (\) is placed in the argv array for every pair of |
|
688 * backslashes (\\) and the double quotation mark is interpreted as an |
|
689 * escape sequence by the remaining backslash, causing a literal double |
|
690 * quotation mark (") to be placed in argv. |
|
691 */ |
|
692 static char* |
|
693 unquote(const char *s) { |
|
694 const char *p = s; /* Pointer to the tail of the original string */ |
|
695 char *un = (char*)JLI_MemAlloc(JLI_StrLen(s) + 1); /* Ptr to unquoted string */ |
|
696 char *pun = un; /* Pointer to the tail of the unquoted string */ |
|
697 |
|
698 while (*p != '\0') { |
|
699 if (*p == '"') { |
|
700 p++; |
|
701 } else if (*p == '\\') { |
|
702 const char *q = p + JLI_StrSpn(p,"\\"); |
|
703 if (*q == '"') |
|
704 do { |
|
705 *pun++ = '\\'; |
|
706 p += 2; |
|
707 } while (*p == '\\' && p < q); |
|
708 else |
|
709 while (p < q) |
|
710 *pun++ = *p++; |
|
711 } else { |
|
712 *pun++ = *p++; |
|
713 } |
|
714 } |
|
715 *pun = '\0'; |
|
716 return un; |
|
717 } |
|
718 |
|
719 /* |
|
720 * Given a path to a jre to execute, this routine checks if this process |
|
721 * is indeed that jre. If not, it exec's that jre. |
|
722 * |
|
723 * We want to actually check the paths rather than just the version string |
|
724 * built into the executable, so that given version specification will yield |
|
725 * the exact same Java environment, regardless of the version of the arbitrary |
|
726 * launcher we start with. |
|
727 */ |
|
728 void |
|
729 ExecJRE(char *jre, char **argv) { |
|
730 int len; |
|
731 char path[MAXPATHLEN + 1]; |
|
732 |
|
733 const char *progname = GetProgramName(); |
|
734 |
|
735 /* |
|
736 * Resolve the real path to the currently running launcher. |
|
737 */ |
|
738 len = GetModuleFileName(NULL, path, MAXPATHLEN + 1); |
|
739 if (len == 0 || len > MAXPATHLEN) { |
|
740 ReportErrorMessageSys(JRE_ERROR9, progname); |
|
741 exit(1); |
|
742 } |
|
743 |
|
744 JLI_TraceLauncher("ExecJRE: old: %s\n", path); |
|
745 JLI_TraceLauncher("ExecJRE: new: %s\n", jre); |
|
746 |
|
747 /* |
|
748 * If the path to the selected JRE directory is a match to the initial |
|
749 * portion of the path to the currently executing JRE, we have a winner! |
|
750 * If so, just return. |
|
751 */ |
|
752 if (JLI_StrNCaseCmp(jre, path, JLI_StrLen(jre)) == 0) |
|
753 return; /* I am the droid you were looking for */ |
|
754 |
|
755 /* |
|
756 * If this isn't the selected version, exec the selected version. |
|
757 */ |
|
758 (void)JLI_StrCat(JLI_StrCat(JLI_StrCpy(path, jre), "\\bin\\"), progname); |
|
759 (void)JLI_StrCat(path, ".exe"); |
|
760 |
|
761 /* |
|
762 * Although Windows has an execv() entrypoint, it doesn't actually |
|
763 * overlay a process: it can only create a new process and terminate |
|
764 * the old process. Therefore, any processes waiting on the initial |
|
765 * process wake up and they shouldn't. Hence, a chain of pseudo-zombie |
|
766 * processes must be retained to maintain the proper wait semantics. |
|
767 * Fortunately the image size of the launcher isn't too large at this |
|
768 * time. |
|
769 * |
|
770 * If it weren't for this semantic flaw, the code below would be ... |
|
771 * |
|
772 * execv(path, argv); |
|
773 * ReportErrorMessage("Error: Exec of %s failed\n", path); |
|
774 * exit(1); |
|
775 * |
|
776 * The incorrect exec semantics could be addressed by: |
|
777 * |
|
778 * exit((int)spawnv(_P_WAIT, path, argv)); |
|
779 * |
|
780 * Unfortunately, a bug in Windows spawn/exec impementation prevents |
|
781 * this from completely working. All the Windows POSIX process creation |
|
782 * interfaces are implemented as wrappers around the native Windows |
|
783 * function CreateProcess(). CreateProcess() takes a single string |
|
784 * to specify command line options and arguments, so the POSIX routine |
|
785 * wrappers build a single string from the argv[] array and in the |
|
786 * process, any quoting information is lost. |
|
787 * |
|
788 * The solution to this to get the original command line, to process it |
|
789 * to remove the new multiple JRE options (if any) as was done for argv |
|
790 * in the common SelectVersion() routine and finally to pass it directly |
|
791 * to the native CreateProcess() Windows process control interface. |
|
792 */ |
|
793 { |
|
794 char *cmdline; |
|
795 char *p; |
|
796 char *np; |
|
797 char *ocl; |
|
798 char *ccl; |
|
799 char *unquoted; |
|
800 DWORD exitCode; |
|
801 STARTUPINFO si; |
|
802 PROCESS_INFORMATION pi; |
|
803 |
|
804 /* |
|
805 * The following code block gets and processes the original command |
|
806 * line, replacing the argv[0] equivalent in the command line with |
|
807 * the path to the new executable and removing the appropriate |
|
808 * Multiple JRE support options. Note that similar logic exists |
|
809 * in the platform independent SelectVersion routine, but is |
|
810 * replicated here due to the syntax of CreateProcess(). |
|
811 * |
|
812 * The magic "+ 4" characters added to the command line length are |
|
813 * 2 possible quotes around the path (argv[0]), a space after the |
|
814 * path and a terminating null character. |
|
815 */ |
|
816 ocl = GetCommandLine(); |
|
817 np = ccl = JLI_StringDup(ocl); |
|
818 p = nextarg(&np); /* Discard argv[0] */ |
|
819 cmdline = (char *)JLI_MemAlloc(JLI_StrLen(path) + JLI_StrLen(np) + 4); |
|
820 if (JLI_StrChr(path, (int)' ') == NULL && JLI_StrChr(path, (int)'\t') == NULL) |
|
821 cmdline = JLI_StrCpy(cmdline, path); |
|
822 else |
|
823 cmdline = JLI_StrCat(JLI_StrCat(JLI_StrCpy(cmdline, "\""), path), "\""); |
|
824 |
|
825 while (*np != (char)0) { /* While more command-line */ |
|
826 p = nextarg(&np); |
|
827 if (*p != (char)0) { /* If a token was isolated */ |
|
828 unquoted = unquote(p); |
|
829 if (*unquoted == '-') { /* Looks like an option */ |
|
830 if (JLI_StrCmp(unquoted, "-classpath") == 0 || |
|
831 JLI_StrCmp(unquoted, "-cp") == 0) { /* Unique cp syntax */ |
|
832 cmdline = JLI_StrCat(JLI_StrCat(cmdline, " "), p); |
|
833 p = nextarg(&np); |
|
834 if (*p != (char)0) /* If a token was isolated */ |
|
835 cmdline = JLI_StrCat(JLI_StrCat(cmdline, " "), p); |
|
836 } else if (JLI_StrNCmp(unquoted, "-version:", 9) != 0 && |
|
837 JLI_StrCmp(unquoted, "-jre-restrict-search") != 0 && |
|
838 JLI_StrCmp(unquoted, "-no-jre-restrict-search") != 0) { |
|
839 cmdline = JLI_StrCat(JLI_StrCat(cmdline, " "), p); |
|
840 } |
|
841 } else { /* End of options */ |
|
842 cmdline = JLI_StrCat(JLI_StrCat(cmdline, " "), p); |
|
843 cmdline = JLI_StrCat(JLI_StrCat(cmdline, " "), np); |
|
844 JLI_MemFree((void *)unquoted); |
|
845 break; |
|
846 } |
|
847 JLI_MemFree((void *)unquoted); |
|
848 } |
|
849 } |
|
850 JLI_MemFree((void *)ccl); |
|
851 |
|
852 if (JLI_IsTraceLauncher()) { |
|
853 np = ccl = JLI_StringDup(cmdline); |
|
854 p = nextarg(&np); |
|
855 printf("ReExec Command: %s (%s)\n", path, p); |
|
856 printf("ReExec Args: %s\n", np); |
|
857 JLI_MemFree((void *)ccl); |
|
858 } |
|
859 (void)fflush(stdout); |
|
860 (void)fflush(stderr); |
|
861 |
|
862 /* |
|
863 * The following code is modeled after a model presented in the |
|
864 * Microsoft Technical Article "Moving Unix Applications to |
|
865 * Windows NT" (March 6, 1994) and "Creating Processes" on MSDN |
|
866 * (Februrary 2005). It approximates UNIX spawn semantics with |
|
867 * the parent waiting for termination of the child. |
|
868 */ |
|
869 memset(&si, 0, sizeof(si)); |
|
870 si.cb =sizeof(STARTUPINFO); |
|
871 memset(&pi, 0, sizeof(pi)); |
|
872 |
|
873 if (!CreateProcess((LPCTSTR)path, /* executable name */ |
|
874 (LPTSTR)cmdline, /* command line */ |
|
875 (LPSECURITY_ATTRIBUTES)NULL, /* process security attr. */ |
|
876 (LPSECURITY_ATTRIBUTES)NULL, /* thread security attr. */ |
|
877 (BOOL)TRUE, /* inherits system handles */ |
|
878 (DWORD)0, /* creation flags */ |
|
879 (LPVOID)NULL, /* environment block */ |
|
880 (LPCTSTR)NULL, /* current directory */ |
|
881 (LPSTARTUPINFO)&si, /* (in) startup information */ |
|
882 (LPPROCESS_INFORMATION)&pi)) { /* (out) process information */ |
|
883 ReportErrorMessageSys(SYS_ERROR1, path); |
|
884 exit(1); |
|
885 } |
|
886 |
|
887 if (WaitForSingleObject(pi.hProcess, INFINITE) != WAIT_FAILED) { |
|
888 if (GetExitCodeProcess(pi.hProcess, &exitCode) == FALSE) |
|
889 exitCode = 1; |
|
890 } else { |
|
891 ReportErrorMessage(SYS_ERROR2); |
|
892 exitCode = 1; |
|
893 } |
|
894 |
|
895 CloseHandle(pi.hThread); |
|
896 CloseHandle(pi.hProcess); |
|
897 |
|
898 exit(exitCode); |
|
899 } |
|
900 |
|
901 } |
|
902 |
|
903 /* |
|
904 * Wrapper for platform dependent unsetenv function. |
|
905 */ |
|
906 int |
|
907 UnsetEnv(char *name) |
|
908 { |
|
909 int ret; |
|
910 char *buf = JLI_MemAlloc(JLI_StrLen(name) + 2); |
|
911 buf = JLI_StrCat(JLI_StrCpy(buf, name), "="); |
|
912 ret = _putenv(buf); |
|
913 JLI_MemFree(buf); |
|
914 return (ret); |
|
915 } |
|
916 |
|
917 /* --- Splash Screen shared library support --- */ |
|
918 |
|
919 static const char* SPLASHSCREEN_SO = "\\bin\\splashscreen.dll"; |
|
920 |
|
921 static HMODULE hSplashLib = NULL; |
|
922 |
|
923 void* SplashProcAddress(const char* name) { |
|
924 char libraryPath[MAXPATHLEN]; /* some extra space for JLI_StrCat'ing SPLASHSCREEN_SO */ |
|
925 |
|
926 if (!GetJREPath(libraryPath, MAXPATHLEN)) { |
|
927 return NULL; |
|
928 } |
|
929 if (JLI_StrLen(libraryPath)+JLI_StrLen(SPLASHSCREEN_SO) >= MAXPATHLEN) { |
|
930 return NULL; |
|
931 } |
|
932 JLI_StrCat(libraryPath, SPLASHSCREEN_SO); |
|
933 |
|
934 if (!hSplashLib) { |
|
935 hSplashLib = LoadLibrary(libraryPath); |
|
936 } |
|
937 if (hSplashLib) { |
|
938 return GetProcAddress(hSplashLib, name); |
|
939 } else { |
|
940 return NULL; |
|
941 } |
|
942 } |
|
943 |
|
944 void SplashFreeLibrary() { |
|
945 if (hSplashLib) { |
|
946 FreeLibrary(hSplashLib); |
|
947 hSplashLib = NULL; |
|
948 } |
|
949 } |
|
950 |
|
951 const char * |
|
952 jlong_format_specifier() { |
|
953 return "%I64d"; |
|
954 } |
|
955 |
|
956 /* |
|
957 * Block current thread and continue execution in a new thread |
|
958 */ |
|
959 int |
|
960 ContinueInNewThread0(int (JNICALL *continuation)(void *), jlong stack_size, void * args) { |
|
961 int rslt = 0; |
|
962 unsigned thread_id; |
|
963 |
|
964 #ifndef STACK_SIZE_PARAM_IS_A_RESERVATION |
|
965 #define STACK_SIZE_PARAM_IS_A_RESERVATION (0x10000) |
|
966 #endif |
|
967 |
|
968 /* |
|
969 * STACK_SIZE_PARAM_IS_A_RESERVATION is what we want, but it's not |
|
970 * supported on older version of Windows. Try first with the flag; and |
|
971 * if that fails try again without the flag. See MSDN document or HotSpot |
|
972 * source (os_win32.cpp) for details. |
|
973 */ |
|
974 HANDLE thread_handle = |
|
975 (HANDLE)_beginthreadex(NULL, |
|
976 (unsigned)stack_size, |
|
977 continuation, |
|
978 args, |
|
979 STACK_SIZE_PARAM_IS_A_RESERVATION, |
|
980 &thread_id); |
|
981 if (thread_handle == NULL) { |
|
982 thread_handle = |
|
983 (HANDLE)_beginthreadex(NULL, |
|
984 (unsigned)stack_size, |
|
985 continuation, |
|
986 args, |
|
987 0, |
|
988 &thread_id); |
|
989 } |
|
990 if (thread_handle) { |
|
991 WaitForSingleObject(thread_handle, INFINITE); |
|
992 GetExitCodeThread(thread_handle, &rslt); |
|
993 CloseHandle(thread_handle); |
|
994 } else { |
|
995 rslt = continuation(args); |
|
996 } |
|
997 return rslt; |
|
998 } |
|
999 |
|
1000 /* Linux only, empty on windows. */ |
|
1001 void SetJavaLauncherPlatformProps() {} |