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