blob: f7cbcdc95bce46ab467dd1af347b882dd9ac5c95 [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
ksrini16238472008-04-10 09:02:22 -07002 * Copyright 1995-2008 Sun Microsystems, Inc. All Rights Reserved.
duke6e45e102007-12-01 00:00:00 +00003 * 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/*
27 * Shared source for 'java' command line tool.
28 *
29 * If JAVA_ARGS is defined, then acts as a launcher for applications. For
30 * instance, the JDK command line tools such as javac and javadoc (see
31 * makefiles for more details) are built with this program. Any arguments
32 * prefixed with '-J' will be passed directly to the 'java' command.
33 */
34
35/*
36 * One job of the launcher is to remove command line options which the
37 * vm does not understand and will not process. These options include
38 * options which select which style of vm is run (e.g. -client and
39 * -server) as well as options which select the data model to use.
40 * Additionally, for tools which invoke an underlying vm "-J-foo"
41 * options are turned into "-foo" options to the vm. This option
42 * filtering is handled in a number of places in the launcher, some of
43 * it in machine-dependent code. In this file, the function
44 * CheckJVMType removes vm style options and TranslateApplicationArgs
45 * removes "-J" prefixes. On unix platforms, the
46 * CreateExecutionEnvironment function from the unix java_md.c file
47 * processes and removes -d<n> options. However, in case
48 * CreateExecutionEnvironment does not need to exec because
49 * LD_LIBRARY_PATH is set acceptably and the data model does not need
50 * to be changed, ParseArguments will screen out the redundant -d<n>
51 * options and prevent them from being passed to the vm; this is done
52 * by RemovableOption.
53 */
54
55
56#include "java.h"
57
58/*
59 * A NOTE TO DEVELOPERS: For performance reasons it is important that
60 * the program image remain relatively small until after SelectVersion
61 * CreateExecutionEnvironment have finished their possibly recursive
62 * processing. Watch everything, but resist all temptations to use Java
63 * interfaces.
64 */
65
66static jboolean printVersion = JNI_FALSE; /* print and exit */
67static jboolean showVersion = JNI_FALSE; /* print but continue */
68static jboolean printUsage = JNI_FALSE; /* print and exit*/
69static jboolean printXUsage = JNI_FALSE; /* print and exit*/
70
71static const char *_program_name;
72static const char *_launcher_name;
73static jboolean _is_java_args = JNI_FALSE;
74static const char *_fVersion;
75static const char *_dVersion;
76static jboolean _wc_enabled = JNI_FALSE;
77static jint _ergo_policy = DEFAULT_POLICY;
78
79/*
80 * Entries for splash screen environment variables.
81 * putenv is performed in SelectVersion. We need
82 * them in memory until UnsetEnv, so they are made static
83 * global instead of auto local.
84 */
85static char* splash_file_entry = NULL;
86static char* splash_jar_entry = NULL;
87
88/*
89 * List of VM options to be specified when the VM is created.
90 */
91static JavaVMOption *options;
92static int numOptions, maxOptions;
93
94/*
95 * Prototypes for functions internal to launcher.
96 */
97static void SetClassPath(const char *s);
98static void SelectVersion(int argc, char **argv, char **main_class);
99static jboolean ParseArguments(int *pargc, char ***pargv, char **pjarfile,
100 char **pclassname, int *pret, const char *jvmpath);
101static jboolean InitializeJVM(JavaVM **pvm, JNIEnv **penv,
102 InvocationFunctions *ifn);
103static jstring NewPlatformString(JNIEnv *env, char *s);
104static jobjectArray NewPlatformStringArray(JNIEnv *env, char **strv, int strc);
105static jclass LoadClass(JNIEnv *env, char *name);
106static jstring GetMainClassName(JNIEnv *env, char *jarname);
107
108static void TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv);
109static jboolean AddApplicationOptions(int cpathc, const char **cpathv);
110static void SetApplicationClassPath(const char**);
111
112static void PrintJavaVersion(JNIEnv *env, jboolean extraLF);
113static void PrintUsage(JNIEnv* env, jboolean doXUsage);
114
115static void SetPaths(int argc, char **argv);
116
117static void DumpState();
118static jboolean RemovableOption(char *option);
119
120/* Maximum supported entries from jvm.cfg. */
121#define INIT_MAX_KNOWN_VMS 10
122
123/* Values for vmdesc.flag */
124enum vmdesc_flag {
125 VM_UNKNOWN = -1,
126 VM_KNOWN,
127 VM_ALIASED_TO,
128 VM_WARN,
129 VM_ERROR,
130 VM_IF_SERVER_CLASS,
131 VM_IGNORE
132};
133
134struct vmdesc {
135 char *name;
136 int flag;
137 char *alias;
138 char *server_class;
139};
140static struct vmdesc *knownVMs = NULL;
141static int knownVMsCount = 0;
142static int knownVMsLimit = 0;
143
144static void GrowKnownVMs();
145static int KnownVMIndex(const char* name);
146static void FreeKnownVMs();
147static void ShowSplashScreen();
148static jboolean IsWildCardEnabled();
149
150#define ARG_CHECK(n, f, a) if (n < 1) { \
151 ReportErrorMessage(f, a); \
152 printUsage = JNI_TRUE; \
153 *pret = 1; \
154 return JNI_TRUE; \
155}
156
157/*
158 * Running Java code in primordial thread caused many problems. We will
159 * create a new thread to invoke JVM. See 6316197 for more information.
160 */
161static jlong threadStackSize = 0; /* stack size of the new thread */
162
163int JNICALL JavaMain(void * args); /* entry point */
164
165typedef struct {
166 int argc;
167 char ** argv;
168 char * jarfile;
169 char * classname;
170 InvocationFunctions ifn;
171} JavaMainArgs;
172
173/*
174 * Entry point.
175 */
176int
177JLI_Launch(int argc, char ** argv, /* main argc, argc */
178 int jargc, const char** jargv, /* java args */
179 int appclassc, const char** appclassv, /* app classpath */
180 const char* fullversion, /* full version defined */
181 const char* dotversion, /* dot version defined */
182 const char* pname, /* program name */
183 const char* lname, /* launcher name */
184 jboolean javaargs, /* JAVA_ARGS */
185 jboolean cpwildcard, /* classpath wildcard*/
186 jboolean javaw, /* windows-only javaw */
187 jint ergo /* ergonomics class policy */
188)
189{
190 char *jarfile = 0;
191 char *classname = 0;
192 char *cpath = 0;
193 char *main_class = NULL;
194 int ret;
195 InvocationFunctions ifn;
196 jlong start, end;
197 char jrepath[MAXPATHLEN], jvmpath[MAXPATHLEN];
198 char ** original_argv = argv;
199
200 _fVersion = fullversion;
201 _dVersion = dotversion;
202 _launcher_name = lname;
203 _program_name = pname;
204 _is_java_args = javaargs;
205 _wc_enabled = cpwildcard;
206 _ergo_policy = ergo;
207
ksrini52cded22008-03-06 07:51:28 -0800208 InitLauncher(javaw);
duke6e45e102007-12-01 00:00:00 +0000209 DumpState();
210
211 /*
212 * Make sure the specified version of the JRE is running.
213 *
214 * There are three things to note about the SelectVersion() routine:
215 * 1) If the version running isn't correct, this routine doesn't
216 * return (either the correct version has been exec'd or an error
217 * was issued).
218 * 2) Argc and Argv in this scope are *not* altered by this routine.
219 * It is the responsibility of subsequent code to ignore the
220 * arguments handled by this routine.
221 * 3) As a side-effect, the variable "main_class" is guaranteed to
222 * be set (if it should ever be set). This isn't exactly the
223 * poster child for structured programming, but it is a small
224 * price to pay for not processing a jar file operand twice.
225 * (Note: This side effect has been disabled. See comment on
226 * bugid 5030265 below.)
227 */
228 SelectVersion(argc, argv, &main_class);
229
230 /* copy original argv */
231 JLI_TraceLauncher("Command line Args:\n");
232 original_argv = (JLI_CopyArgs(argc, (const char**)argv));
233
234 CreateExecutionEnvironment(&argc, &argv,
235 jrepath, sizeof(jrepath),
236 jvmpath, sizeof(jvmpath),
237 original_argv);
238
239 ifn.CreateJavaVM = 0;
240 ifn.GetDefaultJavaVMInitArgs = 0;
241
242 if (JLI_IsTraceLauncher()) {
243 start = CounterGet();
244 }
245
246 if (!LoadJavaVM(jvmpath, &ifn)) {
247 return(6);
248 }
249
250 if (JLI_IsTraceLauncher()) {
251 end = CounterGet();
252 }
253
254 JLI_TraceLauncher("%ld micro seconds to LoadJavaVM\n",
255 (long)(jint)Counter2Micros(end-start));
256
257 ++argv;
258 --argc;
259
260 if (IsJavaArgs()) {
261 /* Preprocess wrapper arguments */
262 TranslateApplicationArgs(jargc, jargv, &argc, &argv);
263 if (!AddApplicationOptions(appclassc, appclassv)) {
264 return(1);
265 }
266 } else {
267 /* Set default CLASSPATH */
268 cpath = getenv("CLASSPATH");
269 if (cpath == NULL) {
270 cpath = ".";
271 }
272 SetClassPath(cpath);
273 }
274
275 /*
276 * Parse command line options; if the return value of
277 * ParseArguments is false, the program should exit.
278 */
279 if (!ParseArguments(&argc, &argv, &jarfile, &classname, &ret, jvmpath)) {
280 return(ret);
281 }
282
283 /* Override class path if -jar flag was specified */
284 if (jarfile != 0) {
285 SetClassPath(jarfile);
286 }
287
288 /* set the -Dsun.java.command pseudo property */
289 SetJavaCommandLineProp(classname, jarfile, argc, argv);
290
291 /* Set the -Dsun.java.launcher pseudo property */
292 SetJavaLauncherProp();
293
294 /* set the -Dsun.java.launcher.* platform properties */
295 SetJavaLauncherPlatformProps();
296
297 /* Show the splash screen if needed */
298 ShowSplashScreen();
299
300 return ContinueInNewThread(&ifn, argc, argv, jarfile, classname, ret);
301
302}
303
304
305int JNICALL
306JavaMain(void * _args)
307{
308 JavaMainArgs *args = (JavaMainArgs *)_args;
309 int argc = args->argc;
310 char **argv = args->argv;
311 char *jarfile = args->jarfile;
312 char *classname = args->classname;
313 InvocationFunctions ifn = args->ifn;
314
315 JavaVM *vm = 0;
316 JNIEnv *env = 0;
317 jstring mainClassName;
318 jclass mainClass;
319 jmethodID mainID;
320 jobjectArray mainArgs;
321 int ret = 0;
322 jlong start, end;
323
324
325 /* Initialize the virtual machine */
326
327 start = CounterGet();
328 if (!InitializeJVM(&vm, &env, &ifn)) {
329 ReportErrorMessage(JVM_ERROR1);
330 exit(1);
331 }
332
333 if (printVersion || showVersion) {
334 PrintJavaVersion(env, showVersion);
335 if ((*env)->ExceptionOccurred(env)) {
336 ReportExceptionDescription(env);
337 ReportErrorMessage(JNI_ERROR);
338 goto leave;
339 }
340 if (printVersion) {
341 ret = 0;
342 goto leave;
343 }
344 }
345
346 /* If the user specified neither a class name nor a JAR file */
347 if (printXUsage || printUsage || (jarfile == 0 && classname == 0)) {
348 PrintUsage(env, printXUsage);
349 if ((*env)->ExceptionOccurred(env)) {
350 ReportExceptionDescription(env);
351 ReportErrorMessage(JNI_ERROR);
352 ret=1;
353 }
354 goto leave;
355 }
356
357 FreeKnownVMs(); /* after last possible PrintUsage() */
358
359 if (JLI_IsTraceLauncher()) {
360 end = CounterGet();
361 JLI_TraceLauncher("%ld micro seconds to InitializeJVM\n",
362 (long)(jint)Counter2Micros(end-start));
363 }
364
365 /* At this stage, argc/argv have the applications' arguments */
366 if (JLI_IsTraceLauncher()){
367 int i;
368 printf("Main-Class is '%s'\n", classname ? classname : "");
369 printf("Apps' argc is %d\n", argc);
370 for (i=0; i < argc; i++) {
371 printf(" argv[%2d] = '%s'\n", i, argv[i]);
372 }
373 }
374
375 ret = 1;
376
377 /*
378 * Get the application's main class.
379 *
380 * See bugid 5030265. The Main-Class name has already been parsed
381 * from the manifest, but not parsed properly for UTF-8 support.
382 * Hence the code here ignores the value previously extracted and
383 * uses the pre-existing code to reextract the value. This is
384 * possibly an end of release cycle expedient. However, it has
385 * also been discovered that passing some character sets through
386 * the environment has "strange" behavior on some variants of
387 * Windows. Hence, maybe the manifest parsing code local to the
388 * launcher should never be enhanced.
389 *
390 * Hence, future work should either:
391 * 1) Correct the local parsing code and verify that the
392 * Main-Class attribute gets properly passed through
393 * all environments,
394 * 2) Remove the vestages of maintaining main_class through
395 * the environment (and remove these comments).
396 */
397 if (jarfile != 0) {
398 mainClassName = GetMainClassName(env, jarfile);
399 if ((*env)->ExceptionOccurred(env)) {
400 ReportExceptionDescription(env);
401 ReportErrorMessage(JNI_ERROR);
402 goto leave;
403 }
404 if (mainClassName == NULL) {
405 ReportErrorMessage(JAR_ERROR1,jarfile, GEN_ERROR);
406 goto leave;
407 }
408 classname = (char *)(*env)->GetStringUTFChars(env, mainClassName, 0);
409 if (classname == NULL) {
410 ReportExceptionDescription(env);
411 ReportErrorMessage(JNI_ERROR);
412 goto leave;
413 }
414 mainClass = LoadClass(env, classname);
415 if(mainClass == NULL) { /* exception occured */
416 ReportExceptionDescription(env);
ksrini16238472008-04-10 09:02:22 -0700417 ReportErrorMessage(CLS_ERROR1, classname);
duke6e45e102007-12-01 00:00:00 +0000418 goto leave;
419 }
420 (*env)->ReleaseStringUTFChars(env, mainClassName, classname);
421 } else {
422 mainClassName = NewPlatformString(env, classname);
423 if (mainClassName == NULL) {
424 ReportErrorMessage(CLS_ERROR2, classname, GEN_ERROR);
425 goto leave;
426 }
427 classname = (char *)(*env)->GetStringUTFChars(env, mainClassName, 0);
428 if (classname == NULL) {
429 ReportExceptionDescription(env);
430 ReportErrorMessage(JNI_ERROR);
431 goto leave;
432 }
433 mainClass = LoadClass(env, classname);
434 if(mainClass == NULL) { /* exception occured */
435 ReportExceptionDescription(env);
ksrini16238472008-04-10 09:02:22 -0700436 ReportErrorMessage(CLS_ERROR1, classname);
duke6e45e102007-12-01 00:00:00 +0000437 goto leave;
438 }
439 (*env)->ReleaseStringUTFChars(env, mainClassName, classname);
440 }
441
442 /* Get the application's main method */
443 mainID = (*env)->GetStaticMethodID(env, mainClass, "main",
444 "([Ljava/lang/String;)V");
445 if (mainID == NULL) {
446 if ((*env)->ExceptionOccurred(env)) {
447 ReportExceptionDescription(env);
448 ReportErrorMessage(JNI_ERROR);
449 } else {
450 ReportErrorMessage(CLS_ERROR3);
451 }
452 goto leave;
453 }
454
455 { /* Make sure the main method is public */
456 jint mods;
457 jmethodID mid;
458 jobject obj = (*env)->ToReflectedMethod(env, mainClass,
459 mainID, JNI_TRUE);
460
461 if( obj == NULL) { /* exception occurred */
462 ReportExceptionDescription(env);
463 ReportErrorMessage(JNI_ERROR);
464 goto leave;
465 }
466
467 mid =
468 (*env)->GetMethodID(env,
469 (*env)->GetObjectClass(env, obj),
470 "getModifiers", "()I");
471 if ((*env)->ExceptionOccurred(env)) {
472 ReportExceptionDescription(env);
473 ReportErrorMessage(JNI_ERROR);
474 goto leave;
475 }
476
477 mods = (*env)->CallIntMethod(env, obj, mid);
478 if ((mods & 1) == 0) { /* if (!Modifier.isPublic(mods)) ... */
479 ReportErrorMessage(CLS_ERROR4);
480 goto leave;
481 }
482 }
483
484 /* Build argument array */
485 mainArgs = NewPlatformStringArray(env, argv, argc);
486 if (mainArgs == NULL) {
487 ReportExceptionDescription(env);
488 ReportErrorMessage(JNI_ERROR);
489 goto leave;
490 }
491
492 /* Invoke main method. */
493 (*env)->CallStaticVoidMethod(env, mainClass, mainID, mainArgs);
494
495 /*
496 * The launcher's exit code (in the absence of calls to
497 * System.exit) will be non-zero if main threw an exception.
498 */
499 ret = (*env)->ExceptionOccurred(env) == NULL ? 0 : 1;
500
501 /*
502 * Detach the main thread so that it appears to have ended when
503 * the application's main method exits. This will invoke the
504 * uncaught exception handler machinery if main threw an
505 * exception. An uncaught exception handler cannot change the
506 * launcher's return code except by calling System.exit.
507 */
508 if ((*vm)->DetachCurrentThread(vm) != 0) {
509 ReportErrorMessage(JVM_ERROR2);
510 ret = 1;
511 goto leave;
512 }
513
514 leave:
515 /*
516 * Wait for all non-daemon threads to end, then destroy the VM.
517 * This will actually create a trivial new Java waiter thread
518 * named "DestroyJavaVM", but this will be seen as a different
519 * thread from the one that executed main, even though they are
520 * the same C thread. This allows mainThread.join() and
521 * mainThread.isAlive() to work as expected.
522 */
523 (*vm)->DestroyJavaVM(vm);
524
525 return ret;
526}
527
528
529/*
530 * Checks the command line options to find which JVM type was
531 * specified. If no command line option was given for the JVM type,
532 * the default type is used. The environment variable
533 * JDK_ALTERNATE_VM and the command line option -XXaltjvm= are also
534 * checked as ways of specifying which JVM type to invoke.
535 */
536char *
537CheckJvmType(int *pargc, char ***argv, jboolean speculative) {
538 int i, argi;
539 int argc;
540 char **newArgv;
541 int newArgvIdx = 0;
542 int isVMType;
543 int jvmidx = -1;
544 char *jvmtype = getenv("JDK_ALTERNATE_VM");
545
546 argc = *pargc;
547
548 /* To make things simpler we always copy the argv array */
549 newArgv = JLI_MemAlloc((argc + 1) * sizeof(char *));
550
551 /* The program name is always present */
552 newArgv[newArgvIdx++] = (*argv)[0];
553
554 for (argi = 1; argi < argc; argi++) {
555 char *arg = (*argv)[argi];
556 isVMType = 0;
557
558 if (IsJavaArgs()) {
559 if (arg[0] != '-') {
560 newArgv[newArgvIdx++] = arg;
561 continue;
562 }
563 } else {
564 if (JLI_StrCmp(arg, "-classpath") == 0 ||
565 JLI_StrCmp(arg, "-cp") == 0) {
566 newArgv[newArgvIdx++] = arg;
567 argi++;
568 if (argi < argc) {
569 newArgv[newArgvIdx++] = (*argv)[argi];
570 }
571 continue;
572 }
573 if (arg[0] != '-') break;
574 }
575
576 /* Did the user pass an explicit VM type? */
577 i = KnownVMIndex(arg);
578 if (i >= 0) {
579 jvmtype = knownVMs[jvmidx = i].name + 1; /* skip the - */
580 isVMType = 1;
581 *pargc = *pargc - 1;
582 }
583
584 /* Did the user specify an "alternate" VM? */
585 else if (JLI_StrCCmp(arg, "-XXaltjvm=") == 0 || JLI_StrCCmp(arg, "-J-XXaltjvm=") == 0) {
586 isVMType = 1;
587 jvmtype = arg+((arg[1]=='X')? 10 : 12);
588 jvmidx = -1;
589 }
590
591 if (!isVMType) {
592 newArgv[newArgvIdx++] = arg;
593 }
594 }
595
596 /*
597 * Finish copying the arguments if we aborted the above loop.
598 * NOTE that if we aborted via "break" then we did NOT copy the
599 * last argument above, and in addition argi will be less than
600 * argc.
601 */
602 while (argi < argc) {
603 newArgv[newArgvIdx++] = (*argv)[argi];
604 argi++;
605 }
606
607 /* argv is null-terminated */
608 newArgv[newArgvIdx] = 0;
609
610 /* Copy back argv */
611 *argv = newArgv;
612 *pargc = newArgvIdx;
613
614 /* use the default VM type if not specified (no alias processing) */
615 if (jvmtype == NULL) {
616 char* result = knownVMs[0].name+1;
617 /* Use a different VM type if we are on a server class machine? */
618 if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) &&
619 (ServerClassMachine() == JNI_TRUE)) {
620 result = knownVMs[0].server_class+1;
621 }
622 JLI_TraceLauncher("Default VM: %s\n", result);
623 return result;
624 }
625
626 /* if using an alternate VM, no alias processing */
627 if (jvmidx < 0)
628 return jvmtype;
629
630 /* Resolve aliases first */
631 {
632 int loopCount = 0;
633 while (knownVMs[jvmidx].flag == VM_ALIASED_TO) {
634 int nextIdx = KnownVMIndex(knownVMs[jvmidx].alias);
635
636 if (loopCount > knownVMsCount) {
637 if (!speculative) {
638 ReportErrorMessage(CFG_ERROR1);
639 exit(1);
640 } else {
641 return "ERROR";
642 /* break; */
643 }
644 }
645
646 if (nextIdx < 0) {
647 if (!speculative) {
648 ReportErrorMessage(CFG_ERROR2, knownVMs[jvmidx].alias);
649 exit(1);
650 } else {
651 return "ERROR";
652 }
653 }
654 jvmidx = nextIdx;
655 jvmtype = knownVMs[jvmidx].name+1;
656 loopCount++;
657 }
658 }
659
660 switch (knownVMs[jvmidx].flag) {
661 case VM_WARN:
662 if (!speculative) {
663 ReportErrorMessage(CFG_WARN1, jvmtype, knownVMs[0].name + 1);
664 }
665 /* fall through */
666 case VM_IGNORE:
667 jvmtype = knownVMs[jvmidx=0].name + 1;
668 /* fall through */
669 case VM_KNOWN:
670 break;
671 case VM_ERROR:
672 if (!speculative) {
673 ReportErrorMessage(CFG_ERROR3, jvmtype);
674 exit(1);
675 } else {
676 return "ERROR";
677 }
678 }
679
680 return jvmtype;
681}
682
683/* copied from HotSpot function "atomll()" */
684static int
685parse_stack_size(const char *s, jlong *result) {
686 jlong n = 0;
687 int args_read = sscanf(s, jlong_format_specifier(), &n);
688 if (args_read != 1) {
689 return 0;
690 }
691 while (*s != '\0' && *s >= '0' && *s <= '9') {
692 s++;
693 }
694 // 4705540: illegal if more characters are found after the first non-digit
695 if (JLI_StrLen(s) > 1) {
696 return 0;
697 }
698 switch (*s) {
699 case 'T': case 't':
700 *result = n * GB * KB;
701 return 1;
702 case 'G': case 'g':
703 *result = n * GB;
704 return 1;
705 case 'M': case 'm':
706 *result = n * MB;
707 return 1;
708 case 'K': case 'k':
709 *result = n * KB;
710 return 1;
711 case '\0':
712 *result = n;
713 return 1;
714 default:
715 /* Create JVM with default stack and let VM handle malformed -Xss string*/
716 return 0;
717 }
718}
719
720/*
721 * Adds a new VM option with the given given name and value.
722 */
723void
724AddOption(char *str, void *info)
725{
726 /*
727 * Expand options array if needed to accommodate at least one more
728 * VM option.
729 */
730 if (numOptions >= maxOptions) {
731 if (options == 0) {
732 maxOptions = 4;
733 options = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
734 } else {
735 JavaVMOption *tmp;
736 maxOptions *= 2;
737 tmp = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
738 memcpy(tmp, options, numOptions * sizeof(JavaVMOption));
739 JLI_MemFree(options);
740 options = tmp;
741 }
742 }
743 options[numOptions].optionString = str;
744 options[numOptions++].extraInfo = info;
745
746 if (JLI_StrCCmp(str, "-Xss") == 0) {
747 jlong tmp;
748 if (parse_stack_size(str + 4, &tmp)) {
749 threadStackSize = tmp;
750 }
751 }
752}
753
754static void
755SetClassPath(const char *s)
756{
757 char *def;
758 s = JLI_WildcardExpandClasspath(s);
759 def = JLI_MemAlloc(JLI_StrLen(s) + 40);
760 sprintf(def, "-Djava.class.path=%s", s);
761 AddOption(def, NULL);
762}
763
764/*
765 * The SelectVersion() routine ensures that an appropriate version of
766 * the JRE is running. The specification for the appropriate version
767 * is obtained from either the manifest of a jar file (preferred) or
768 * from command line options.
769 * The routine also parses splash screen command line options and
770 * passes on their values in private environment variables.
771 */
772static void
773SelectVersion(int argc, char **argv, char **main_class)
774{
775 char *arg;
776 char **new_argv;
777 char **new_argp;
778 char *operand;
779 char *version = NULL;
780 char *jre = NULL;
781 int jarflag = 0;
782 int headlessflag = 0;
783 int restrict_search = -1; /* -1 implies not known */
784 manifest_info info;
785 char env_entry[MAXNAMELEN + 24] = ENV_ENTRY "=";
786 char *splash_file_name = NULL;
787 char *splash_jar_name = NULL;
788 char *env_in;
789 int res;
790
791 /*
792 * If the version has already been selected, set *main_class
793 * with the value passed through the environment (if any) and
794 * simply return.
795 */
796 if ((env_in = getenv(ENV_ENTRY)) != NULL) {
797 if (*env_in != '\0')
798 *main_class = JLI_StringDup(env_in);
799 return;
800 }
801
802 /*
803 * Scan through the arguments for options relevant to multiple JRE
804 * support. For reference, the command line syntax is defined as:
805 *
806 * SYNOPSIS
807 * java [options] class [argument...]
808 *
809 * java [options] -jar file.jar [argument...]
810 *
811 * As the scan is performed, make a copy of the argument list with
812 * the version specification options (new to 1.5) removed, so that
813 * a version less than 1.5 can be exec'd.
814 *
815 * Note that due to the syntax of the native Windows interface
816 * CreateProcess(), processing similar to the following exists in
817 * the Windows platform specific routine ExecJRE (in java_md.c).
818 * Changes here should be reproduced there.
819 */
820 new_argv = JLI_MemAlloc((argc + 1) * sizeof(char*));
821 new_argv[0] = argv[0];
822 new_argp = &new_argv[1];
823 argc--;
824 argv++;
825 while ((arg = *argv) != 0 && *arg == '-') {
826 if (JLI_StrCCmp(arg, "-version:") == 0) {
827 version = arg + 9;
828 } else if (JLI_StrCmp(arg, "-jre-restrict-search") == 0) {
829 restrict_search = 1;
830 } else if (JLI_StrCmp(arg, "-no-jre-restrict-search") == 0) {
831 restrict_search = 0;
832 } else {
833 if (JLI_StrCmp(arg, "-jar") == 0)
834 jarflag = 1;
835 /* deal with "unfortunate" classpath syntax */
836 if ((JLI_StrCmp(arg, "-classpath") == 0 || JLI_StrCmp(arg, "-cp") == 0) &&
837 (argc >= 2)) {
838 *new_argp++ = arg;
839 argc--;
840 argv++;
841 arg = *argv;
842 }
843
844 /*
845 * Checking for headless toolkit option in the some way as AWT does:
846 * "true" means true and any other value means false
847 */
848 if (JLI_StrCmp(arg, "-Djava.awt.headless=true") == 0) {
849 headlessflag = 1;
850 } else if (JLI_StrCCmp(arg, "-Djava.awt.headless=") == 0) {
851 headlessflag = 0;
852 } else if (JLI_StrCCmp(arg, "-splash:") == 0) {
853 splash_file_name = arg+8;
854 }
855 *new_argp++ = arg;
856 }
857 argc--;
858 argv++;
859 }
860 if (argc <= 0) { /* No operand? Possibly legit with -[full]version */
861 operand = NULL;
862 } else {
863 argc--;
864 *new_argp++ = operand = *argv++;
865 }
866 while (argc-- > 0) /* Copy over [argument...] */
867 *new_argp++ = *argv++;
868 *new_argp = NULL;
869
870 /*
871 * If there is a jar file, read the manifest. If the jarfile can't be
872 * read, the manifest can't be read from the jar file, or the manifest
873 * is corrupt, issue the appropriate error messages and exit.
874 *
875 * Even if there isn't a jar file, construct a manifest_info structure
876 * containing the command line information. It's a convenient way to carry
877 * this data around.
878 */
879 if (jarflag && operand) {
880 if ((res = JLI_ParseManifest(operand, &info)) != 0) {
881 if (res == -1)
882 ReportErrorMessage(JAR_ERROR2, operand);
883 else
884 ReportErrorMessage(JAR_ERROR3, operand);
885 exit(1);
886 }
887
888 /*
889 * Command line splash screen option should have precedence
890 * over the manifest, so the manifest data is used only if
891 * splash_file_name has not been initialized above during command
892 * line parsing
893 */
894 if (!headlessflag && !splash_file_name && info.splashscreen_image_file_name) {
895 splash_file_name = info.splashscreen_image_file_name;
896 splash_jar_name = operand;
897 }
898 } else {
899 info.manifest_version = NULL;
900 info.main_class = NULL;
901 info.jre_version = NULL;
902 info.jre_restrict_search = 0;
903 }
904
905 /*
906 * Passing on splash screen info in environment variables
907 */
908 if (splash_file_name && !headlessflag) {
909 char* splash_file_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_FILE_ENV_ENTRY "=")+JLI_StrLen(splash_file_name)+1);
910 JLI_StrCpy(splash_file_entry, SPLASH_FILE_ENV_ENTRY "=");
911 JLI_StrCat(splash_file_entry, splash_file_name);
912 putenv(splash_file_entry);
913 }
914 if (splash_jar_name && !headlessflag) {
915 char* splash_jar_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_JAR_ENV_ENTRY "=")+JLI_StrLen(splash_jar_name)+1);
916 JLI_StrCpy(splash_jar_entry, SPLASH_JAR_ENV_ENTRY "=");
917 JLI_StrCat(splash_jar_entry, splash_jar_name);
918 putenv(splash_jar_entry);
919 }
920
921 /*
922 * The JRE-Version and JRE-Restrict-Search values (if any) from the
923 * manifest are overwritten by any specified on the command line.
924 */
925 if (version != NULL)
926 info.jre_version = version;
927 if (restrict_search != -1)
928 info.jre_restrict_search = restrict_search;
929
930 /*
931 * "Valid" returns (other than unrecoverable errors) follow. Set
932 * main_class as a side-effect of this routine.
933 */
934 if (info.main_class != NULL)
935 *main_class = JLI_StringDup(info.main_class);
936
937 /*
938 * If no version selection information is found either on the command
939 * line or in the manifest, simply return.
940 */
941 if (info.jre_version == NULL) {
942 JLI_FreeManifest();
943 JLI_MemFree(new_argv);
944 return;
945 }
946
947 /*
948 * Check for correct syntax of the version specification (JSR 56).
949 */
950 if (!JLI_ValidVersionString(info.jre_version)) {
951 ReportErrorMessage(SPC_ERROR1, info.jre_version);
952 exit(1);
953 }
954
955 /*
956 * Find the appropriate JVM on the system. Just to be as forgiving as
957 * possible, if the standard algorithms don't locate an appropriate
958 * jre, check to see if the one running will satisfy the requirements.
959 * This can happen on systems which haven't been set-up for multiple
960 * JRE support.
961 */
962 jre = LocateJRE(&info);
963 JLI_TraceLauncher("JRE-Version = %s, JRE-Restrict-Search = %s Selected = %s\n",
964 (info.jre_version?info.jre_version:"null"),
965 (info.jre_restrict_search?"true":"false"), (jre?jre:"null"));
966
967 if (jre == NULL) {
968 if (JLI_AcceptableRelease(GetFullVersion(), info.jre_version)) {
969 JLI_FreeManifest();
970 JLI_MemFree(new_argv);
971 return;
972 } else {
973 ReportErrorMessage(CFG_ERROR4, info.jre_version);
974 exit(1);
975 }
976 }
977
978 /*
979 * If I'm not the chosen one, exec the chosen one. Returning from
980 * ExecJRE indicates that I am indeed the chosen one.
981 *
982 * The private environment variable _JAVA_VERSION_SET is used to
983 * prevent the chosen one from re-reading the manifest file and
984 * using the values found within to override the (potential) command
985 * line flags stripped from argv (because the target may not
986 * understand them). Passing the MainClass value is an optimization
987 * to avoid locating, expanding and parsing the manifest extra
988 * times.
989 */
990 if (info.main_class != NULL)
991 (void)JLI_StrCat(env_entry, info.main_class);
992 (void)putenv(env_entry);
993 ExecJRE(jre, new_argv);
994 JLI_FreeManifest();
995 JLI_MemFree(new_argv);
996 return;
997}
998
999/*
1000 * Parses command line arguments. Returns JNI_FALSE if launcher
1001 * should exit without starting vm, returns JNI_TRUE if vm needs
1002 * to be started to process given options. *pret (the launcher
1003 * process return value) is set to 0 for a normal exit.
1004 */
1005static jboolean
1006ParseArguments(int *pargc, char ***pargv, char **pjarfile,
1007 char **pclassname, int *pret, const char *jvmpath)
1008{
1009 int argc = *pargc;
1010 char **argv = *pargv;
1011 jboolean jarflag = JNI_FALSE;
1012 char *arg;
1013
1014 *pret = 0;
1015
1016 while ((arg = *argv) != 0 && *arg == '-') {
1017 argv++; --argc;
1018 if (JLI_StrCmp(arg, "-classpath") == 0 || JLI_StrCmp(arg, "-cp") == 0) {
1019 ARG_CHECK (argc, ARG_ERROR1, arg);
1020 SetClassPath(*argv);
1021 argv++; --argc;
1022 } else if (JLI_StrCmp(arg, "-jar") == 0) {
1023 ARG_CHECK (argc, ARG_ERROR2, arg);
1024 jarflag = JNI_TRUE;
1025 } else if (JLI_StrCmp(arg, "-help") == 0 ||
1026 JLI_StrCmp(arg, "-h") == 0 ||
1027 JLI_StrCmp(arg, "-?") == 0) {
1028 printUsage = JNI_TRUE;
1029 return JNI_TRUE;
1030 } else if (JLI_StrCmp(arg, "-version") == 0) {
1031 printVersion = JNI_TRUE;
1032 return JNI_TRUE;
1033 } else if (JLI_StrCmp(arg, "-showversion") == 0) {
1034 showVersion = JNI_TRUE;
1035 } else if (JLI_StrCmp(arg, "-X") == 0) {
1036 printXUsage = JNI_TRUE;
1037 return JNI_TRUE;
1038/*
1039 * The following case provide backward compatibility with old-style
1040 * command line options.
1041 */
1042 } else if (JLI_StrCmp(arg, "-fullversion") == 0) {
1043 ReportMessage("%s full version \"%s\"", _launcher_name, GetFullVersion());
1044 return JNI_FALSE;
1045 } else if (JLI_StrCmp(arg, "-verbosegc") == 0) {
1046 AddOption("-verbose:gc", NULL);
1047 } else if (JLI_StrCmp(arg, "-t") == 0) {
1048 AddOption("-Xt", NULL);
1049 } else if (JLI_StrCmp(arg, "-tm") == 0) {
1050 AddOption("-Xtm", NULL);
1051 } else if (JLI_StrCmp(arg, "-debug") == 0) {
1052 AddOption("-Xdebug", NULL);
1053 } else if (JLI_StrCmp(arg, "-noclassgc") == 0) {
1054 AddOption("-Xnoclassgc", NULL);
1055 } else if (JLI_StrCmp(arg, "-Xfuture") == 0) {
1056 AddOption("-Xverify:all", NULL);
1057 } else if (JLI_StrCmp(arg, "-verify") == 0) {
1058 AddOption("-Xverify:all", NULL);
1059 } else if (JLI_StrCmp(arg, "-verifyremote") == 0) {
1060 AddOption("-Xverify:remote", NULL);
1061 } else if (JLI_StrCmp(arg, "-noverify") == 0) {
1062 AddOption("-Xverify:none", NULL);
1063 } else if (JLI_StrCCmp(arg, "-prof") == 0) {
1064 char *p = arg + 5;
1065 char *tmp = JLI_MemAlloc(JLI_StrLen(arg) + 50);
1066 if (*p) {
1067 sprintf(tmp, "-Xrunhprof:cpu=old,file=%s", p + 1);
1068 } else {
1069 sprintf(tmp, "-Xrunhprof:cpu=old,file=java.prof");
1070 }
1071 AddOption(tmp, NULL);
1072 } else if (JLI_StrCCmp(arg, "-ss") == 0 ||
1073 JLI_StrCCmp(arg, "-oss") == 0 ||
1074 JLI_StrCCmp(arg, "-ms") == 0 ||
1075 JLI_StrCCmp(arg, "-mx") == 0) {
1076 char *tmp = JLI_MemAlloc(JLI_StrLen(arg) + 6);
1077 sprintf(tmp, "-X%s", arg + 1); /* skip '-' */
1078 AddOption(tmp, NULL);
1079 } else if (JLI_StrCmp(arg, "-checksource") == 0 ||
1080 JLI_StrCmp(arg, "-cs") == 0 ||
1081 JLI_StrCmp(arg, "-noasyncgc") == 0) {
1082 /* No longer supported */
1083 ReportErrorMessage(ARG_WARN, arg);
1084 } else if (JLI_StrCCmp(arg, "-version:") == 0 ||
1085 JLI_StrCmp(arg, "-no-jre-restrict-search") == 0 ||
1086 JLI_StrCmp(arg, "-jre-restrict-search") == 0 ||
1087 JLI_StrCCmp(arg, "-splash:") == 0) {
1088 ; /* Ignore machine independent options already handled */
1089 } else if (RemovableOption(arg) ) {
1090 ; /* Do not pass option to vm. */
1091 } else {
1092 AddOption(arg, NULL);
1093 }
1094 }
1095
1096 if (--argc >= 0) {
1097 if (jarflag) {
1098 *pjarfile = *argv++;
1099 *pclassname = 0;
1100 } else {
1101 *pjarfile = 0;
1102 *pclassname = *argv++;
1103 }
1104 *pargc = argc;
1105 *pargv = argv;
1106 }
1107
1108 return JNI_TRUE;
1109}
1110
1111/*
1112 * Initializes the Java Virtual Machine. Also frees options array when
1113 * finished.
1114 */
1115static jboolean
1116InitializeJVM(JavaVM **pvm, JNIEnv **penv, InvocationFunctions *ifn)
1117{
1118 JavaVMInitArgs args;
1119 jint r;
1120
1121 memset(&args, 0, sizeof(args));
1122 args.version = JNI_VERSION_1_2;
1123 args.nOptions = numOptions;
1124 args.options = options;
1125 args.ignoreUnrecognized = JNI_FALSE;
1126
1127 if (JLI_IsTraceLauncher()) {
1128 int i = 0;
1129 printf("JavaVM args:\n ");
1130 printf("version 0x%08lx, ", (long)args.version);
1131 printf("ignoreUnrecognized is %s, ",
1132 args.ignoreUnrecognized ? "JNI_TRUE" : "JNI_FALSE");
1133 printf("nOptions is %ld\n", (long)args.nOptions);
1134 for (i = 0; i < numOptions; i++)
1135 printf(" option[%2d] = '%s'\n",
1136 i, args.options[i].optionString);
1137 }
1138
1139 r = ifn->CreateJavaVM(pvm, (void **)penv, &args);
1140 JLI_MemFree(options);
1141 return r == JNI_OK;
1142}
1143
1144
1145#define NULL_CHECK0(e) if ((e) == 0) { \
1146 ReportErrorMessage(JNI_ERROR); \
1147 return 0; \
1148 }
1149
1150#define NULL_CHECK(e) if ((e) == 0) { \
1151 ReportErrorMessage(JNI_ERROR); \
1152 return; \
1153 }
1154
1155static jstring platformEncoding = NULL;
1156static jstring getPlatformEncoding(JNIEnv *env) {
1157 if (platformEncoding == NULL) {
1158 jstring propname = (*env)->NewStringUTF(env, "sun.jnu.encoding");
1159 if (propname) {
1160 jclass cls;
1161 jmethodID mid;
1162 NULL_CHECK0 (cls = (*env)->FindClass(env, "java/lang/System"));
1163 NULL_CHECK0 (mid = (*env)->GetStaticMethodID(
1164 env, cls,
1165 "getProperty",
1166 "(Ljava/lang/String;)Ljava/lang/String;"));
1167 platformEncoding = (*env)->CallStaticObjectMethod (
1168 env, cls, mid, propname);
1169 }
1170 }
1171 return platformEncoding;
1172}
1173
1174static jboolean isEncodingSupported(JNIEnv *env, jstring enc) {
1175 jclass cls;
1176 jmethodID mid;
1177 NULL_CHECK0 (cls = (*env)->FindClass(env, "java/nio/charset/Charset"));
1178 NULL_CHECK0 (mid = (*env)->GetStaticMethodID(
1179 env, cls,
1180 "isSupported",
1181 "(Ljava/lang/String;)Z"));
1182 return (*env)->CallStaticBooleanMethod(env, cls, mid, enc);
1183}
1184
1185/*
1186 * Returns a new Java string object for the specified platform string.
1187 */
1188static jstring
1189NewPlatformString(JNIEnv *env, char *s)
1190{
1191 int len = (int)JLI_StrLen(s);
1192 jclass cls;
1193 jmethodID mid;
1194 jbyteArray ary;
1195 jstring enc;
1196
1197 if (s == NULL)
1198 return 0;
1199 enc = getPlatformEncoding(env);
1200
1201 ary = (*env)->NewByteArray(env, len);
1202 if (ary != 0) {
1203 jstring str = 0;
1204 (*env)->SetByteArrayRegion(env, ary, 0, len, (jbyte *)s);
1205 if (!(*env)->ExceptionOccurred(env)) {
1206 if (isEncodingSupported(env, enc) == JNI_TRUE) {
1207 NULL_CHECK0(cls = (*env)->FindClass(env, "java/lang/String"));
1208 NULL_CHECK0(mid = (*env)->GetMethodID(env, cls, "<init>",
1209 "([BLjava/lang/String;)V"));
1210 str = (*env)->NewObject(env, cls, mid, ary, enc);
1211 } else {
1212 /*If the encoding specified in sun.jnu.encoding is not
1213 endorsed by "Charset.isSupported" we have to fall back
1214 to use String(byte[]) explicitly here without specifying
1215 the encoding name, in which the StringCoding class will
1216 pickup the iso-8859-1 as the fallback converter for us.
1217 */
1218 NULL_CHECK0(cls = (*env)->FindClass(env, "java/lang/String"));
1219 NULL_CHECK0(mid = (*env)->GetMethodID(env, cls, "<init>",
1220 "([B)V"));
1221 str = (*env)->NewObject(env, cls, mid, ary);
1222 }
1223 (*env)->DeleteLocalRef(env, ary);
1224 return str;
1225 }
1226 }
1227 return 0;
1228}
1229
1230/*
1231 * Returns a new array of Java string objects for the specified
1232 * array of platform strings.
1233 */
1234static jobjectArray
1235NewPlatformStringArray(JNIEnv *env, char **strv, int strc)
1236{
1237 jarray cls;
1238 jarray ary;
1239 int i;
1240
1241 NULL_CHECK0(cls = (*env)->FindClass(env, "java/lang/String"));
1242 NULL_CHECK0(ary = (*env)->NewObjectArray(env, strc, cls, 0));
1243 for (i = 0; i < strc; i++) {
1244 jstring str = NewPlatformString(env, *strv++);
1245 NULL_CHECK0(str);
1246 (*env)->SetObjectArrayElement(env, ary, i, str);
1247 (*env)->DeleteLocalRef(env, str);
1248 }
1249 return ary;
1250}
1251
1252/*
1253 * Loads a class, convert the '.' to '/'.
1254 */
1255static jclass
1256LoadClass(JNIEnv *env, char *name)
1257{
1258 char *buf = JLI_MemAlloc(JLI_StrLen(name) + 1);
1259 char *s = buf, *t = name, c;
1260 jclass cls;
1261 jlong start, end;
1262
1263 if (JLI_IsTraceLauncher())
1264 start = CounterGet();
1265
1266 do {
1267 c = *t++;
1268 *s++ = (c == '.') ? '/' : c;
1269 } while (c != '\0');
1270 cls = (*env)->FindClass(env, buf);
1271 JLI_MemFree(buf);
1272
1273 if (JLI_IsTraceLauncher()) {
1274 end = CounterGet();
1275 printf("%ld micro seconds to load main class\n",
1276 (long)(jint)Counter2Micros(end-start));
1277 printf("----_JAVA_LAUNCHER_DEBUG----\n");
1278 }
1279
1280 return cls;
1281}
1282
1283
1284/*
1285 * Returns the main class name for the specified jar file.
1286 */
1287static jstring
1288GetMainClassName(JNIEnv *env, char *jarname)
1289{
1290#define MAIN_CLASS "Main-Class"
1291 jclass cls;
1292 jmethodID mid;
1293 jobject jar, man, attr;
1294 jstring str, result = 0;
1295
1296 NULL_CHECK0(cls = (*env)->FindClass(env, "java/util/jar/JarFile"));
1297 NULL_CHECK0(mid = (*env)->GetMethodID(env, cls, "<init>",
1298 "(Ljava/lang/String;)V"));
1299 NULL_CHECK0(str = NewPlatformString(env, jarname));
1300 NULL_CHECK0(jar = (*env)->NewObject(env, cls, mid, str));
1301 NULL_CHECK0(mid = (*env)->GetMethodID(env, cls, "getManifest",
1302 "()Ljava/util/jar/Manifest;"));
1303 man = (*env)->CallObjectMethod(env, jar, mid);
1304 if (man != 0) {
1305 NULL_CHECK0(mid = (*env)->GetMethodID(env,
1306 (*env)->GetObjectClass(env, man),
1307 "getMainAttributes",
1308 "()Ljava/util/jar/Attributes;"));
1309 attr = (*env)->CallObjectMethod(env, man, mid);
1310 if (attr != 0) {
1311 NULL_CHECK0(mid = (*env)->GetMethodID(env,
1312 (*env)->GetObjectClass(env, attr),
1313 "getValue",
1314 "(Ljava/lang/String;)Ljava/lang/String;"));
1315 NULL_CHECK0(str = NewPlatformString(env, MAIN_CLASS));
1316 result = (*env)->CallObjectMethod(env, attr, mid, str);
1317 }
1318 }
1319 return result;
1320}
1321
1322
1323/*
1324 * For tools, convert command line args thus:
1325 * javac -cp foo:foo/"*" -J-ms32m ...
1326 * java -ms32m -cp JLI_WildcardExpandClasspath(foo:foo/"*") ...
1327 *
1328 * Takes 4 parameters, and returns the populated arguments
1329 */
1330static void
1331TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv)
1332{
1333 int argc = *pargc;
1334 char **argv = *pargv;
1335 int nargc = argc + jargc;
1336 char **nargv = JLI_MemAlloc((nargc + 1) * sizeof(char *));
1337 int i;
1338
1339 *pargc = nargc;
1340 *pargv = nargv;
1341
1342 /* Copy the VM arguments (i.e. prefixed with -J) */
1343 for (i = 0; i < jargc; i++) {
1344 const char *arg = jargv[i];
1345 if (arg[0] == '-' && arg[1] == 'J') {
1346 *nargv++ = ((arg + 2) == NULL) ? NULL : JLI_StringDup(arg + 2);
1347 }
1348 }
1349
1350 for (i = 0; i < argc; i++) {
1351 char *arg = argv[i];
1352 if (arg[0] == '-' && arg[1] == 'J') {
1353 if (arg[2] == '\0') {
1354 ReportErrorMessage(ARG_ERROR3);
1355 exit(1);
1356 }
1357 *nargv++ = arg + 2;
1358 }
1359 }
1360
1361 /* Copy the rest of the arguments */
1362 for (i = 0; i < jargc ; i++) {
1363 const char *arg = jargv[i];
1364 if (arg[0] != '-' || arg[1] != 'J') {
1365 *nargv++ = (arg == NULL) ? NULL : JLI_StringDup(arg);
1366 }
1367 }
1368 for (i = 0; i < argc; i++) {
1369 char *arg = argv[i];
1370 if (arg[0] == '-') {
1371 if (arg[1] == 'J')
1372 continue;
1373 if (IsWildCardEnabled() && arg[1] == 'c'
1374 && (JLI_StrCmp(arg, "-cp") == 0 ||
1375 JLI_StrCmp(arg, "-classpath") == 0)
1376 && i < argc - 1) {
1377 *nargv++ = arg;
1378 *nargv++ = (char *) JLI_WildcardExpandClasspath(argv[i+1]);
1379 i++;
1380 continue;
1381 }
1382 }
1383 *nargv++ = arg;
1384 }
1385 *nargv = 0;
1386}
1387
1388/*
1389 * For our tools, we try to add 3 VM options:
1390 * -Denv.class.path=<envcp>
1391 * -Dapplication.home=<apphome>
1392 * -Djava.class.path=<appcp>
1393 * <envcp> is the user's setting of CLASSPATH -- for instance the user
1394 * tells javac where to find binary classes through this environment
1395 * variable. Notice that users will be able to compile against our
1396 * tools classes (sun.tools.javac.Main) only if they explicitly add
1397 * tools.jar to CLASSPATH.
1398 * <apphome> is the directory where the application is installed.
1399 * <appcp> is the classpath to where our apps' classfiles are.
1400 */
1401static jboolean
1402AddApplicationOptions(int cpathc, const char **cpathv)
1403{
1404 char *envcp, *appcp, *apphome;
1405 char home[MAXPATHLEN]; /* application home */
1406 char separator[] = { PATH_SEPARATOR, '\0' };
1407 int size, i;
1408
1409 {
1410 const char *s = getenv("CLASSPATH");
1411 if (s) {
1412 s = (char *) JLI_WildcardExpandClasspath(s);
1413 /* 40 for -Denv.class.path= */
1414 envcp = (char *)JLI_MemAlloc(JLI_StrLen(s) + 40);
1415 sprintf(envcp, "-Denv.class.path=%s", s);
1416 AddOption(envcp, NULL);
1417 }
1418 }
1419
1420 if (!GetApplicationHome(home, sizeof(home))) {
1421 ReportErrorMessage(CFG_ERROR5);
1422 return JNI_FALSE;
1423 }
1424
1425 /* 40 for '-Dapplication.home=' */
1426 apphome = (char *)JLI_MemAlloc(JLI_StrLen(home) + 40);
1427 sprintf(apphome, "-Dapplication.home=%s", home);
1428 AddOption(apphome, NULL);
1429
1430 /* How big is the application's classpath? */
1431 size = 40; /* 40: "-Djava.class.path=" */
1432 for (i = 0; i < cpathc; i++) {
1433 size += (int)JLI_StrLen(home) + (int)JLI_StrLen(cpathv[i]) + 1; /* 1: separator */
1434 }
1435 appcp = (char *)JLI_MemAlloc(size + 1);
1436 JLI_StrCpy(appcp, "-Djava.class.path=");
1437 for (i = 0; i < cpathc; i++) {
1438 JLI_StrCat(appcp, home); /* c:\program files\myapp */
1439 JLI_StrCat(appcp, cpathv[i]); /* \lib\myapp.jar */
1440 JLI_StrCat(appcp, separator); /* ; */
1441 }
1442 appcp[JLI_StrLen(appcp)-1] = '\0'; /* remove trailing path separator */
1443 AddOption(appcp, NULL);
1444 return JNI_TRUE;
1445}
1446
1447/*
1448 * inject the -Dsun.java.command pseudo property into the args structure
1449 * this pseudo property is used in the HotSpot VM to expose the
1450 * Java class name and arguments to the main method to the VM. The
1451 * HotSpot VM uses this pseudo property to store the Java class name
1452 * (or jar file name) and the arguments to the class's main method
1453 * to the instrumentation memory region. The sun.java.command pseudo
1454 * property is not exported by HotSpot to the Java layer.
1455 */
1456void
1457SetJavaCommandLineProp(char *classname, char *jarfile,
1458 int argc, char **argv)
1459{
1460
1461 int i = 0;
1462 size_t len = 0;
1463 char* javaCommand = NULL;
1464 char* dashDstr = "-Dsun.java.command=";
1465
1466 if (classname == NULL && jarfile == NULL) {
1467 /* unexpected, one of these should be set. just return without
1468 * setting the property
1469 */
1470 return;
1471 }
1472
1473 /* if the class name is not set, then use the jarfile name */
1474 if (classname == NULL) {
1475 classname = jarfile;
1476 }
1477
1478 /* determine the amount of memory to allocate assuming
1479 * the individual components will be space separated
1480 */
1481 len = JLI_StrLen(classname);
1482 for (i = 0; i < argc; i++) {
1483 len += JLI_StrLen(argv[i]) + 1;
1484 }
1485
1486 /* allocate the memory */
1487 javaCommand = (char*) JLI_MemAlloc(len + JLI_StrLen(dashDstr) + 1);
1488
1489 /* build the -D string */
1490 *javaCommand = '\0';
1491 JLI_StrCat(javaCommand, dashDstr);
1492 JLI_StrCat(javaCommand, classname);
1493
1494 for (i = 0; i < argc; i++) {
1495 /* the components of the string are space separated. In
1496 * the case of embedded white space, the relationship of
1497 * the white space separated components to their true
1498 * positional arguments will be ambiguous. This issue may
1499 * be addressed in a future release.
1500 */
1501 JLI_StrCat(javaCommand, " ");
1502 JLI_StrCat(javaCommand, argv[i]);
1503 }
1504
1505 AddOption(javaCommand, NULL);
1506}
1507
1508/*
1509 * JVM would like to know if it's created by a standard Sun launcher, or by
1510 * user native application, the following property indicates the former.
1511 */
1512void SetJavaLauncherProp() {
1513 AddOption("-Dsun.java.launcher=SUN_STANDARD", NULL);
1514}
1515
1516/*
1517 * Prints the version information from the java.version and other properties.
1518 */
1519static void
1520PrintJavaVersion(JNIEnv *env, jboolean extraLF)
1521{
1522 jclass ver;
1523 jmethodID print;
1524
1525 NULL_CHECK(ver = (*env)->FindClass(env, "sun/misc/Version"));
1526 NULL_CHECK(print = (*env)->GetStaticMethodID(env,
1527 ver,
1528 (extraLF == JNI_TRUE) ? "println" : "print",
1529 "()V"
1530 )
1531 );
1532
1533 (*env)->CallStaticVoidMethod(env, ver, print);
1534}
1535
1536/*
1537 * Prints default usage or the Xusage message, see sun.launcher.LauncherHelp.java
1538 */
1539static void
1540PrintUsage(JNIEnv* env, jboolean doXUsage)
1541{
1542 jclass cls;
1543 jmethodID initHelp, vmSelect, vmSynonym, vmErgo, printHelp, printXUsageMessage;
1544 jstring jprogname, vm1, vm2;
1545 int i;
1546
1547 NULL_CHECK(cls = (*env)->FindClass(env, "sun/launcher/LauncherHelp"));
1548
1549
1550 if (doXUsage) {
1551 NULL_CHECK(printXUsageMessage = (*env)->GetStaticMethodID(env, cls,
1552 "printXUsageMessage", "(Z)V"));
1553 (*env)->CallStaticVoidMethod(env, cls, printXUsageMessage, JNI_TRUE);
1554 } else {
1555 NULL_CHECK(initHelp = (*env)->GetStaticMethodID(env, cls,
1556 "initHelpMessage", "(Ljava/lang/String;)V"));
1557
1558 NULL_CHECK(vmSelect = (*env)->GetStaticMethodID(env, cls, "appendVmSelectMessage",
1559 "(Ljava/lang/String;Ljava/lang/String;)V"));
1560
1561 NULL_CHECK(vmSynonym = (*env)->GetStaticMethodID(env, cls,
1562 "appendVmSynonymMessage",
1563 "(Ljava/lang/String;Ljava/lang/String;)V"));
1564 NULL_CHECK(vmErgo = (*env)->GetStaticMethodID(env, cls,
1565 "appendVmErgoMessage", "(ZLjava/lang/String;)V"));
1566
1567 NULL_CHECK(printHelp = (*env)->GetStaticMethodID(env, cls,
1568 "printHelpMessage", "(Z)V"));
1569
1570 jprogname = (*env)->NewStringUTF(env, _program_name);
1571
1572 /* Initialize the usage message with the usual preamble */
1573 (*env)->CallStaticVoidMethod(env, cls, initHelp, jprogname);
1574
1575
1576 /* Assemble the other variant part of the usage */
1577 if ((knownVMs[0].flag == VM_KNOWN) ||
1578 (knownVMs[0].flag == VM_IF_SERVER_CLASS)) {
1579 vm1 = (*env)->NewStringUTF(env, knownVMs[0].name);
1580 vm2 = (*env)->NewStringUTF(env, knownVMs[0].name+1);
1581 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1582 }
1583 for (i=1; i<knownVMsCount; i++) {
1584 if (knownVMs[i].flag == VM_KNOWN) {
1585 vm1 = (*env)->NewStringUTF(env, knownVMs[i].name);
1586 vm2 = (*env)->NewStringUTF(env, knownVMs[i].name+1);
1587 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1588 }
1589 }
1590 for (i=1; i<knownVMsCount; i++) {
1591 if (knownVMs[i].flag == VM_ALIASED_TO) {
1592 vm1 = (*env)->NewStringUTF(env, knownVMs[i].name);
1593 vm2 = (*env)->NewStringUTF(env, knownVMs[i].alias+1);
1594 (*env)->CallStaticVoidMethod(env, cls, vmSynonym, vm1, vm2);
1595 }
1596 }
1597
1598 /* The first known VM is the default */
1599 {
1600 jboolean isServerClassMachine = ServerClassMachine();
1601
1602 const char* defaultVM = knownVMs[0].name+1;
1603 if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) && isServerClassMachine) {
1604 defaultVM = knownVMs[0].server_class+1;
1605 }
1606
1607 vm1 = (*env)->NewStringUTF(env, defaultVM);
1608 (*env)->CallStaticVoidMethod(env, cls, vmErgo, isServerClassMachine, vm1);
1609 }
1610
1611 /* Complete the usage message and print to stderr*/
1612 (*env)->CallStaticVoidMethod(env, cls, printHelp, JNI_TRUE);
1613 }
1614 return;
1615}
1616
1617/*
1618 * Read the jvm.cfg file and fill the knownJVMs[] array.
1619 *
1620 * The functionality of the jvm.cfg file is subject to change without
1621 * notice and the mechanism will be removed in the future.
1622 *
1623 * The lexical structure of the jvm.cfg file is as follows:
1624 *
1625 * jvmcfg := { vmLine }
1626 * vmLine := knownLine
1627 * | aliasLine
1628 * | warnLine
1629 * | ignoreLine
1630 * | errorLine
1631 * | predicateLine
1632 * | commentLine
1633 * knownLine := flag "KNOWN" EOL
1634 * warnLine := flag "WARN" EOL
1635 * ignoreLine := flag "IGNORE" EOL
1636 * errorLine := flag "ERROR" EOL
1637 * aliasLine := flag "ALIASED_TO" flag EOL
1638 * predicateLine := flag "IF_SERVER_CLASS" flag EOL
1639 * commentLine := "#" text EOL
1640 * flag := "-" identifier
1641 *
1642 * The semantics are that when someone specifies a flag on the command line:
1643 * - if the flag appears on a knownLine, then the identifier is used as
1644 * the name of the directory holding the JVM library (the name of the JVM).
1645 * - if the flag appears as the first flag on an aliasLine, the identifier
1646 * of the second flag is used as the name of the JVM.
1647 * - if the flag appears on a warnLine, the identifier is used as the
1648 * name of the JVM, but a warning is generated.
1649 * - if the flag appears on an ignoreLine, the identifier is recognized as the
1650 * name of a JVM, but the identifier is ignored and the default vm used
1651 * - if the flag appears on an errorLine, an error is generated.
1652 * - if the flag appears as the first flag on a predicateLine, and
1653 * the machine on which you are running passes the predicate indicated,
1654 * then the identifier of the second flag is used as the name of the JVM,
1655 * otherwise the identifier of the first flag is used as the name of the JVM.
1656 * If no flag is given on the command line, the first vmLine of the jvm.cfg
1657 * file determines the name of the JVM.
1658 * PredicateLines are only interpreted on first vmLine of a jvm.cfg file,
1659 * since they only make sense if someone hasn't specified the name of the
1660 * JVM on the command line.
1661 *
1662 * The intent of the jvm.cfg file is to allow several JVM libraries to
1663 * be installed in different subdirectories of a single JRE installation,
1664 * for space-savings and convenience in testing.
1665 * The intent is explicitly not to provide a full aliasing or predicate
1666 * mechanism.
1667 */
1668jint
1669ReadKnownVMs(const char *jrepath, const char * arch, jboolean speculative)
1670{
1671 FILE *jvmCfg;
1672 char jvmCfgName[MAXPATHLEN+20];
1673 char line[MAXPATHLEN+20];
1674 int cnt = 0;
1675 int lineno = 0;
1676 jlong start, end;
1677 int vmType;
1678 char *tmpPtr;
1679 char *altVMName = NULL;
1680 char *serverClassVMName = NULL;
1681 static char *whiteSpace = " \t";
1682 if (JLI_IsTraceLauncher()) {
1683 start = CounterGet();
1684 }
1685
1686 JLI_StrCpy(jvmCfgName, jrepath);
1687 JLI_StrCat(jvmCfgName, FILESEP "lib" FILESEP);
1688 JLI_StrCat(jvmCfgName, arch);
1689 JLI_StrCat(jvmCfgName, FILESEP "jvm.cfg");
1690
1691 jvmCfg = fopen(jvmCfgName, "r");
1692 if (jvmCfg == NULL) {
1693 if (!speculative) {
1694 ReportErrorMessage(CFG_ERROR6, jvmCfgName);
1695 exit(1);
1696 } else {
1697 return -1;
1698 }
1699 }
1700 while (fgets(line, sizeof(line), jvmCfg) != NULL) {
1701 vmType = VM_UNKNOWN;
1702 lineno++;
1703 if (line[0] == '#')
1704 continue;
1705 if (line[0] != '-') {
1706 ReportErrorMessage(CFG_WARN2, lineno, jvmCfgName);
1707 }
1708 if (cnt >= knownVMsLimit) {
1709 GrowKnownVMs(cnt);
1710 }
1711 line[JLI_StrLen(line)-1] = '\0'; /* remove trailing newline */
1712 tmpPtr = line + JLI_StrCSpn(line, whiteSpace);
1713 if (*tmpPtr == 0) {
1714 ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
1715 } else {
1716 /* Null-terminate this string for JLI_StringDup below */
1717 *tmpPtr++ = 0;
1718 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1719 if (*tmpPtr == 0) {
1720 ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
1721 } else {
1722 if (!JLI_StrCCmp(tmpPtr, "KNOWN")) {
1723 vmType = VM_KNOWN;
1724 } else if (!JLI_StrCCmp(tmpPtr, "ALIASED_TO")) {
1725 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1726 if (*tmpPtr != 0) {
1727 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1728 }
1729 if (*tmpPtr == 0) {
1730 ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
1731 } else {
1732 /* Null terminate altVMName */
1733 altVMName = tmpPtr;
1734 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1735 *tmpPtr = 0;
1736 vmType = VM_ALIASED_TO;
1737 }
1738 } else if (!JLI_StrCCmp(tmpPtr, "WARN")) {
1739 vmType = VM_WARN;
1740 } else if (!JLI_StrCCmp(tmpPtr, "IGNORE")) {
1741 vmType = VM_IGNORE;
1742 } else if (!JLI_StrCCmp(tmpPtr, "ERROR")) {
1743 vmType = VM_ERROR;
1744 } else if (!JLI_StrCCmp(tmpPtr, "IF_SERVER_CLASS")) {
1745 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1746 if (*tmpPtr != 0) {
1747 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1748 }
1749 if (*tmpPtr == 0) {
1750 ReportErrorMessage(CFG_WARN4, lineno, jvmCfgName);
1751 } else {
1752 /* Null terminate server class VM name */
1753 serverClassVMName = tmpPtr;
1754 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1755 *tmpPtr = 0;
1756 vmType = VM_IF_SERVER_CLASS;
1757 }
1758 } else {
1759 ReportErrorMessage(CFG_WARN5, lineno, &jvmCfgName[0]);
1760 vmType = VM_KNOWN;
1761 }
1762 }
1763 }
1764
1765 JLI_TraceLauncher("jvm.cfg[%d] = ->%s<-\n", cnt, line);
1766 if (vmType != VM_UNKNOWN) {
1767 knownVMs[cnt].name = JLI_StringDup(line);
1768 knownVMs[cnt].flag = vmType;
1769 switch (vmType) {
1770 default:
1771 break;
1772 case VM_ALIASED_TO:
1773 knownVMs[cnt].alias = JLI_StringDup(altVMName);
1774 JLI_TraceLauncher(" name: %s vmType: %s alias: %s\n",
1775 knownVMs[cnt].name, "VM_ALIASED_TO", knownVMs[cnt].alias);
1776 break;
1777 case VM_IF_SERVER_CLASS:
1778 knownVMs[cnt].server_class = JLI_StringDup(serverClassVMName);
1779 JLI_TraceLauncher(" name: %s vmType: %s server_class: %s\n",
1780 knownVMs[cnt].name, "VM_IF_SERVER_CLASS", knownVMs[cnt].server_class);
1781 break;
1782 }
1783 cnt++;
1784 }
1785 }
1786 fclose(jvmCfg);
1787 knownVMsCount = cnt;
1788
1789 if (JLI_IsTraceLauncher()) {
1790 end = CounterGet();
1791 printf("%ld micro seconds to parse jvm.cfg\n",
1792 (long)(jint)Counter2Micros(end-start));
1793 }
1794
1795 return cnt;
1796}
1797
1798
1799static void
1800GrowKnownVMs(int minimum)
1801{
1802 struct vmdesc* newKnownVMs;
1803 int newMax;
1804
1805 newMax = (knownVMsLimit == 0 ? INIT_MAX_KNOWN_VMS : (2 * knownVMsLimit));
1806 if (newMax <= minimum) {
1807 newMax = minimum;
1808 }
1809 newKnownVMs = (struct vmdesc*) JLI_MemAlloc(newMax * sizeof(struct vmdesc));
1810 if (knownVMs != NULL) {
1811 memcpy(newKnownVMs, knownVMs, knownVMsLimit * sizeof(struct vmdesc));
1812 }
1813 JLI_MemFree(knownVMs);
1814 knownVMs = newKnownVMs;
1815 knownVMsLimit = newMax;
1816}
1817
1818
1819/* Returns index of VM or -1 if not found */
1820static int
1821KnownVMIndex(const char* name)
1822{
1823 int i;
1824 if (JLI_StrCCmp(name, "-J") == 0) name += 2;
1825 for (i = 0; i < knownVMsCount; i++) {
1826 if (!JLI_StrCmp(name, knownVMs[i].name)) {
1827 return i;
1828 }
1829 }
1830 return -1;
1831}
1832
1833static void
1834FreeKnownVMs()
1835{
1836 int i;
1837 for (i = 0; i < knownVMsCount; i++) {
1838 JLI_MemFree(knownVMs[i].name);
1839 knownVMs[i].name = NULL;
1840 }
1841 JLI_MemFree(knownVMs);
1842}
1843
1844
1845/*
1846 * Displays the splash screen according to the jar file name
1847 * and image file names stored in environment variables
1848 */
1849static void
1850ShowSplashScreen()
1851{
1852 const char *jar_name = getenv(SPLASH_JAR_ENV_ENTRY);
1853 const char *file_name = getenv(SPLASH_FILE_ENV_ENTRY);
1854 int data_size;
1855 void *image_data;
1856 if (jar_name) {
1857 image_data = JLI_JarUnpackFile(jar_name, file_name, &data_size);
1858 if (image_data) {
1859 DoSplashInit();
1860 DoSplashLoadMemory(image_data, data_size);
1861 JLI_MemFree(image_data);
1862 }
1863 } else if (file_name) {
1864 DoSplashInit();
1865 DoSplashLoadFile(file_name);
1866 } else {
1867 return;
1868 }
1869 DoSplashSetFileJarName(file_name, jar_name);
1870
1871 /*
1872 * Done with all command line processing and potential re-execs so
1873 * clean up the environment.
1874 */
1875 (void)UnsetEnv(ENV_ENTRY);
1876 (void)UnsetEnv(SPLASH_FILE_ENV_ENTRY);
1877 (void)UnsetEnv(SPLASH_JAR_ENV_ENTRY);
1878
1879 JLI_MemFree(splash_jar_entry);
1880 JLI_MemFree(splash_file_entry);
1881
1882}
1883
1884const char*
1885GetDotVersion()
1886{
1887 return _dVersion;
1888}
1889
1890const char*
1891GetFullVersion()
1892{
1893 return _fVersion;
1894}
1895
1896const char*
1897GetProgramName()
1898{
1899 return _program_name;
1900}
1901
1902const char*
1903GetLauncherName()
1904{
1905 return _launcher_name;
1906}
1907
1908jint
1909GetErgoPolicy()
1910{
1911 return _ergo_policy;
1912}
1913
1914jboolean
1915IsJavaArgs()
1916{
1917 return _is_java_args;
1918}
1919
1920static jboolean
1921IsWildCardEnabled()
1922{
1923 return _wc_enabled;
1924}
1925
1926static int
1927ContinueInNewThread(InvocationFunctions* ifn, int argc,
1928 char **argv, char *jarfile, char *classname, int ret)
1929{
1930
1931 /*
1932 * If user doesn't specify stack size, check if VM has a preference.
1933 * Note that HotSpot no longer supports JNI_VERSION_1_1 but it will
1934 * return its default stack size through the init args structure.
1935 */
1936 if (threadStackSize == 0) {
1937 struct JDK1_1InitArgs args1_1;
1938 memset((void*)&args1_1, 0, sizeof(args1_1));
1939 args1_1.version = JNI_VERSION_1_1;
1940 ifn->GetDefaultJavaVMInitArgs(&args1_1); /* ignore return value */
1941 if (args1_1.javaStackSize > 0) {
1942 threadStackSize = args1_1.javaStackSize;
1943 }
1944 }
1945
1946 { /* Create a new thread to create JVM and invoke main method */
1947 JavaMainArgs args;
1948 int rslt;
1949
1950 args.argc = argc;
1951 args.argv = argv;
1952 args.jarfile = jarfile;
1953 args.classname = classname;
1954 args.ifn = *ifn;
1955
1956 rslt = ContinueInNewThread0(JavaMain, threadStackSize, (void*)&args);
1957 /* If the caller has deemed there is an error we
1958 * simply return that, otherwise we return the value of
1959 * the callee
1960 */
1961 return (ret != 0) ? ret : rslt;
1962 }
1963}
1964
1965static void
1966DumpState()
1967{
1968 if (!JLI_IsTraceLauncher()) return ;
1969 printf("Launcher state:\n");
1970 printf("\tdebug:%s\n", (JLI_IsTraceLauncher() == JNI_TRUE) ? "on" : "off");
1971 printf("\tjavargs:%s\n", (_is_java_args == JNI_TRUE) ? "on" : "off");
1972 printf("\tprogram name:%s\n", GetProgramName());
1973 printf("\tlauncher name:%s\n", GetLauncherName());
1974 printf("\tjavaw:%s\n", (IsJavaw() == JNI_TRUE) ? "on" : "off");
1975 printf("\tfullversion:%s\n", GetFullVersion());
1976 printf("\tdotversion:%s\n", GetDotVersion());
1977 printf("\tergo_policy:");
1978 switch(GetErgoPolicy()) {
1979 case NEVER_SERVER_CLASS:
1980 printf("NEVER_ACT_AS_A_SERVER_CLASS_MACHINE\n");
1981 break;
1982 case ALWAYS_SERVER_CLASS:
1983 printf("ALWAYS_ACT_AS_A_SERVER_CLASS_MACHINE\n");
1984 break;
1985 default:
1986 printf("DEFAULT_ERGONOMICS_POLICY\n");
1987 }
1988}
1989
1990/*
1991 * Return JNI_TRUE for an option string that has no effect but should
1992 * _not_ be passed on to the vm; return JNI_FALSE otherwise. On
1993 * Solaris SPARC, this screening needs to be done if:
1994 * 1) LD_LIBRARY_PATH does _not_ need to be reset and
1995 * 2) -d32 or -d64 is passed to a binary with a matching data model
1996 * (the exec in SetLibraryPath removes -d<n> options and points the
1997 * exec to the proper binary). When this exec is not done, these options
1998 * would end up getting passed onto the vm.
1999 */
2000jboolean
2001RemovableOption(char * option)
2002{
2003 /*
2004 * Unconditionally remove both -d32 and -d64 options since only
2005 * the last such options has an effect; e.g.
2006 * java -d32 -d64 -d32 -version
2007 * is equivalent to
2008 * java -d32 -version
2009 */
2010
2011 if( (JLI_StrCCmp(option, "-d32") == 0 ) ||
2012 (JLI_StrCCmp(option, "-d64") == 0 ) )
2013 return JNI_TRUE;
2014 else
2015 return JNI_FALSE;
2016}
2017
2018/*
2019 * A utility procedure to always print to stderr
2020 */
2021void
2022ReportMessage(const char* fmt, ...)
2023{
2024 va_list vl;
2025 va_start(vl, fmt);
2026 vfprintf(stderr, fmt, vl);
2027 fprintf(stderr, "\n");
2028 va_end(vl);
2029}