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