blob: 2d9d5b4e47ffc81a230c304de54033d0b3f506a0 [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
ksriniac5e02f2012-01-11 08:14:47 -08002 * Copyright (c) 1995, 2012, 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
ksrini7d9872e2011-03-20 08:41:33 -070064/* we always print to stderr */
65#define USE_STDERR JNI_TRUE
66
duke6e45e102007-12-01 00:00:00 +000067static jboolean printVersion = JNI_FALSE; /* print and exit */
68static jboolean showVersion = JNI_FALSE; /* print but continue */
69static jboolean printUsage = JNI_FALSE; /* print and exit*/
70static jboolean printXUsage = JNI_FALSE; /* print and exit*/
ksrini8e20e1c2010-11-23 16:52:39 -080071static char *showSettings = NULL; /* print but continue */
duke6e45e102007-12-01 00:00:00 +000072
73static const char *_program_name;
74static const char *_launcher_name;
75static jboolean _is_java_args = JNI_FALSE;
76static const char *_fVersion;
77static const char *_dVersion;
78static jboolean _wc_enabled = JNI_FALSE;
79static jint _ergo_policy = DEFAULT_POLICY;
80
81/*
82 * Entries for splash screen environment variables.
83 * putenv is performed in SelectVersion. We need
84 * them in memory until UnsetEnv, so they are made static
85 * global instead of auto local.
86 */
87static char* splash_file_entry = NULL;
88static char* splash_jar_entry = NULL;
89
90/*
91 * List of VM options to be specified when the VM is created.
92 */
93static JavaVMOption *options;
94static int numOptions, maxOptions;
95
96/*
97 * Prototypes for functions internal to launcher.
98 */
99static void SetClassPath(const char *s);
100static void SelectVersion(int argc, char **argv, char **main_class);
mchungea290e22011-01-21 09:43:57 -0800101static jboolean ParseArguments(int *pargc, char ***pargv,
102 int *pmode, char **pwhat,
103 int *pret, const char *jrepath);
duke6e45e102007-12-01 00:00:00 +0000104static jboolean InitializeJVM(JavaVM **pvm, JNIEnv **penv,
105 InvocationFunctions *ifn);
106static jstring NewPlatformString(JNIEnv *env, char *s);
mchungea290e22011-01-21 09:43:57 -0800107static jclass LoadMainClass(JNIEnv *env, int mode, char *name);
duke6e45e102007-12-01 00:00:00 +0000108
109static void TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv);
110static jboolean AddApplicationOptions(int cpathc, const char **cpathv);
111static void SetApplicationClassPath(const char**);
112
113static void PrintJavaVersion(JNIEnv *env, jboolean extraLF);
114static void PrintUsage(JNIEnv* env, jboolean doXUsage);
ksrini8e20e1c2010-11-23 16:52:39 -0800115static void ShowSettings(JNIEnv* env, char *optString);
duke6e45e102007-12-01 00:00:00 +0000116
117static void SetPaths(int argc, char **argv);
118
119static void DumpState();
120static jboolean RemovableOption(char *option);
121
122/* Maximum supported entries from jvm.cfg. */
123#define INIT_MAX_KNOWN_VMS 10
124
125/* Values for vmdesc.flag */
126enum vmdesc_flag {
127 VM_UNKNOWN = -1,
128 VM_KNOWN,
129 VM_ALIASED_TO,
130 VM_WARN,
131 VM_ERROR,
132 VM_IF_SERVER_CLASS,
133 VM_IGNORE
134};
135
136struct vmdesc {
137 char *name;
138 int flag;
139 char *alias;
140 char *server_class;
141};
142static struct vmdesc *knownVMs = NULL;
143static int knownVMsCount = 0;
144static int knownVMsLimit = 0;
145
146static void GrowKnownVMs();
147static int KnownVMIndex(const char* name);
148static void FreeKnownVMs();
duke6e45e102007-12-01 00:00:00 +0000149static jboolean IsWildCardEnabled();
150
151#define ARG_CHECK(n, f, a) if (n < 1) { \
ksrini0e817162008-08-26 10:21:20 -0700152 JLI_ReportErrorMessage(f, a); \
duke6e45e102007-12-01 00:00:00 +0000153 printUsage = JNI_TRUE; \
154 *pret = 1; \
155 return JNI_TRUE; \
156}
157
158/*
159 * Running Java code in primordial thread caused many problems. We will
160 * create a new thread to invoke JVM. See 6316197 for more information.
161 */
ksrinie97d4002012-07-31 06:14:28 -0700162static jlong threadStackSize = 0; /* stack size of the new thread */
ksrini6d575f82010-12-23 13:51:30 -0800163static jlong maxHeapSize = 0; /* max heap size */
164static jlong initialHeapSize = 0; /* inital heap size */
duke6e45e102007-12-01 00:00:00 +0000165
duke6e45e102007-12-01 00:00:00 +0000166/*
167 * Entry point.
168 */
169int
170JLI_Launch(int argc, char ** argv, /* main argc, argc */
171 int jargc, const char** jargv, /* java args */
172 int appclassc, const char** appclassv, /* app classpath */
173 const char* fullversion, /* full version defined */
174 const char* dotversion, /* dot version defined */
175 const char* pname, /* program name */
176 const char* lname, /* launcher name */
177 jboolean javaargs, /* JAVA_ARGS */
178 jboolean cpwildcard, /* classpath wildcard*/
179 jboolean javaw, /* windows-only javaw */
180 jint ergo /* ergonomics class policy */
181)
182{
mchungea290e22011-01-21 09:43:57 -0800183 int mode = LM_UNKNOWN;
184 char *what = NULL;
duke6e45e102007-12-01 00:00:00 +0000185 char *cpath = 0;
186 char *main_class = NULL;
187 int ret;
188 InvocationFunctions ifn;
189 jlong start, end;
ksrinie3ec45d2010-07-09 11:04:34 -0700190 char jvmpath[MAXPATHLEN];
191 char jrepath[MAXPATHLEN];
michaelm5ac8c152012-03-06 20:34:38 +0000192 char jvmcfg[MAXPATHLEN];
duke6e45e102007-12-01 00:00:00 +0000193
194 _fVersion = fullversion;
195 _dVersion = dotversion;
196 _launcher_name = lname;
197 _program_name = pname;
198 _is_java_args = javaargs;
199 _wc_enabled = cpwildcard;
200 _ergo_policy = ergo;
201
ksrini52cded22008-03-06 07:51:28 -0800202 InitLauncher(javaw);
duke6e45e102007-12-01 00:00:00 +0000203 DumpState();
ksrinie97d4002012-07-31 06:14:28 -0700204 if (JLI_IsTraceLauncher()) {
205 int i;
206 printf("Command line args:\n");
207 for (i = 0; i < argc ; i++) {
208 printf("argv[%d] = %s\n", i, argv[i]);
209 }
210 AddOption("-Dsun.java.launcher.diag=true", NULL);
211 }
duke6e45e102007-12-01 00:00:00 +0000212
213 /*
214 * Make sure the specified version of the JRE is running.
215 *
216 * There are three things to note about the SelectVersion() routine:
217 * 1) If the version running isn't correct, this routine doesn't
218 * return (either the correct version has been exec'd or an error
219 * was issued).
220 * 2) Argc and Argv in this scope are *not* altered by this routine.
221 * It is the responsibility of subsequent code to ignore the
222 * arguments handled by this routine.
223 * 3) As a side-effect, the variable "main_class" is guaranteed to
224 * be set (if it should ever be set). This isn't exactly the
225 * poster child for structured programming, but it is a small
226 * price to pay for not processing a jar file operand twice.
227 * (Note: This side effect has been disabled. See comment on
228 * bugid 5030265 below.)
229 */
230 SelectVersion(argc, argv, &main_class);
231
duke6e45e102007-12-01 00:00:00 +0000232 CreateExecutionEnvironment(&argc, &argv,
233 jrepath, sizeof(jrepath),
michaelm5ac8c152012-03-06 20:34:38 +0000234 jvmpath, sizeof(jvmpath),
235 jvmcfg, sizeof(jvmcfg));
duke6e45e102007-12-01 00:00:00 +0000236
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
mchungea290e22011-01-21 09:43:57 -0800273 /* Parse command line options; if the return value of
274 * ParseArguments is false, the program should exit.
duke6e45e102007-12-01 00:00:00 +0000275 */
mchungea290e22011-01-21 09:43:57 -0800276 if (!ParseArguments(&argc, &argv, &mode, &what, &ret, jrepath))
277 {
duke6e45e102007-12-01 00:00:00 +0000278 return(ret);
279 }
280
281 /* Override class path if -jar flag was specified */
mchungea290e22011-01-21 09:43:57 -0800282 if (mode == LM_JAR) {
283 SetClassPath(what); /* Override class path */
duke6e45e102007-12-01 00:00:00 +0000284 }
285
286 /* set the -Dsun.java.command pseudo property */
mchungea290e22011-01-21 09:43:57 -0800287 SetJavaCommandLineProp(what, argc, argv);
duke6e45e102007-12-01 00:00:00 +0000288
289 /* Set the -Dsun.java.launcher pseudo property */
290 SetJavaLauncherProp();
291
292 /* set the -Dsun.java.launcher.* platform properties */
293 SetJavaLauncherPlatformProps();
294
michaelm5ac8c152012-03-06 20:34:38 +0000295 return JVMInit(&ifn, threadStackSize, argc, argv, mode, what, ret);
duke6e45e102007-12-01 00:00:00 +0000296}
ksrinie3ec45d2010-07-09 11:04:34 -0700297/*
298 * Always detach the main thread so that it appears to have ended when
299 * the application's main method exits. This will invoke the
300 * uncaught exception handler machinery if main threw an
301 * exception. An uncaught exception handler cannot change the
302 * launcher's return code except by calling System.exit.
303 *
304 * Wait for all non-daemon threads to end, then destroy the VM.
305 * This will actually create a trivial new Java waiter thread
306 * named "DestroyJavaVM", but this will be seen as a different
307 * thread from the one that executed main, even though they are
308 * the same C thread. This allows mainThread.join() and
309 * mainThread.isAlive() to work as expected.
310 */
311#define LEAVE() \
312 if ((*vm)->DetachCurrentThread(vm) != 0) { \
313 JLI_ReportErrorMessage(JVM_ERROR2); \
314 ret = 1; \
315 } \
316 (*vm)->DestroyJavaVM(vm); \
317 return ret \
duke6e45e102007-12-01 00:00:00 +0000318
ksrini20a64b22008-09-24 15:07:41 -0700319#define CHECK_EXCEPTION_NULL_LEAVE(e) \
320 if ((*env)->ExceptionOccurred(env)) { \
321 JLI_ReportExceptionDescription(env); \
ksrinie3ec45d2010-07-09 11:04:34 -0700322 LEAVE(); \
ksrini20a64b22008-09-24 15:07:41 -0700323 } \
324 if ((e) == NULL) { \
325 JLI_ReportErrorMessage(JNI_ERROR); \
ksrinie3ec45d2010-07-09 11:04:34 -0700326 LEAVE(); \
ksrini20a64b22008-09-24 15:07:41 -0700327 }
328
329#define CHECK_EXCEPTION_LEAVE(rv) \
330 if ((*env)->ExceptionOccurred(env)) { \
331 JLI_ReportExceptionDescription(env); \
332 ret = (rv); \
ksrinie3ec45d2010-07-09 11:04:34 -0700333 LEAVE(); \
ksrini20a64b22008-09-24 15:07:41 -0700334 }
duke6e45e102007-12-01 00:00:00 +0000335
336int JNICALL
337JavaMain(void * _args)
338{
339 JavaMainArgs *args = (JavaMainArgs *)_args;
340 int argc = args->argc;
341 char **argv = args->argv;
mchungea290e22011-01-21 09:43:57 -0800342 int mode = args->mode;
343 char *what = args->what;
duke6e45e102007-12-01 00:00:00 +0000344 InvocationFunctions ifn = args->ifn;
345
346 JavaVM *vm = 0;
347 JNIEnv *env = 0;
mchungea290e22011-01-21 09:43:57 -0800348 jclass mainClass = NULL;
duke6e45e102007-12-01 00:00:00 +0000349 jmethodID mainID;
350 jobjectArray mainArgs;
351 int ret = 0;
352 jlong start, end;
353
michaelm5ac8c152012-03-06 20:34:38 +0000354 RegisterThread();
355
duke6e45e102007-12-01 00:00:00 +0000356 /* Initialize the virtual machine */
duke6e45e102007-12-01 00:00:00 +0000357 start = CounterGet();
358 if (!InitializeJVM(&vm, &env, &ifn)) {
ksrini0e817162008-08-26 10:21:20 -0700359 JLI_ReportErrorMessage(JVM_ERROR1);
duke6e45e102007-12-01 00:00:00 +0000360 exit(1);
361 }
362
ksrini05fdd5a2012-01-03 08:27:37 -0800363 if (showSettings != NULL) {
364 ShowSettings(env, showSettings);
365 CHECK_EXCEPTION_LEAVE(1);
366 }
367
duke6e45e102007-12-01 00:00:00 +0000368 if (printVersion || showVersion) {
369 PrintJavaVersion(env, showVersion);
ksrini20a64b22008-09-24 15:07:41 -0700370 CHECK_EXCEPTION_LEAVE(0);
duke6e45e102007-12-01 00:00:00 +0000371 if (printVersion) {
ksrinie3ec45d2010-07-09 11:04:34 -0700372 LEAVE();
duke6e45e102007-12-01 00:00:00 +0000373 }
374 }
375
376 /* If the user specified neither a class name nor a JAR file */
mchungea290e22011-01-21 09:43:57 -0800377 if (printXUsage || printUsage || what == 0 || mode == LM_UNKNOWN) {
duke6e45e102007-12-01 00:00:00 +0000378 PrintUsage(env, printXUsage);
ksrini20a64b22008-09-24 15:07:41 -0700379 CHECK_EXCEPTION_LEAVE(1);
ksrinie3ec45d2010-07-09 11:04:34 -0700380 LEAVE();
duke6e45e102007-12-01 00:00:00 +0000381 }
382
383 FreeKnownVMs(); /* after last possible PrintUsage() */
384
385 if (JLI_IsTraceLauncher()) {
386 end = CounterGet();
387 JLI_TraceLauncher("%ld micro seconds to InitializeJVM\n",
388 (long)(jint)Counter2Micros(end-start));
389 }
390
mchungea290e22011-01-21 09:43:57 -0800391 /* At this stage, argc/argv have the application's arguments */
duke6e45e102007-12-01 00:00:00 +0000392 if (JLI_IsTraceLauncher()){
393 int i;
mchungea290e22011-01-21 09:43:57 -0800394 printf("%s is '%s'\n", launchModeNames[mode], what);
395 printf("App's argc is %d\n", argc);
duke6e45e102007-12-01 00:00:00 +0000396 for (i=0; i < argc; i++) {
397 printf(" argv[%2d] = '%s'\n", i, argv[i]);
398 }
399 }
400
401 ret = 1;
402
403 /*
404 * Get the application's main class.
405 *
406 * See bugid 5030265. The Main-Class name has already been parsed
407 * from the manifest, but not parsed properly for UTF-8 support.
408 * Hence the code here ignores the value previously extracted and
409 * uses the pre-existing code to reextract the value. This is
410 * possibly an end of release cycle expedient. However, it has
411 * also been discovered that passing some character sets through
412 * the environment has "strange" behavior on some variants of
413 * Windows. Hence, maybe the manifest parsing code local to the
414 * launcher should never be enhanced.
415 *
416 * Hence, future work should either:
417 * 1) Correct the local parsing code and verify that the
418 * Main-Class attribute gets properly passed through
419 * all environments,
420 * 2) Remove the vestages of maintaining main_class through
421 * the environment (and remove these comments).
422 */
mchungea290e22011-01-21 09:43:57 -0800423 mainClass = LoadMainClass(env, mode, what);
ksrini20a64b22008-09-24 15:07:41 -0700424 CHECK_EXCEPTION_NULL_LEAVE(mainClass);
michaelm5ac8c152012-03-06 20:34:38 +0000425 PostJVMInit(env, mainClass, vm);
ksrini20a64b22008-09-24 15:07:41 -0700426 /*
427 * The LoadMainClass not only loads the main class, it will also ensure
428 * that the main method's signature is correct, therefore further checking
429 * is not required. The main method is invoked here so that extraneous java
430 * stacks are not in the application stack trace.
431 */
duke6e45e102007-12-01 00:00:00 +0000432 mainID = (*env)->GetStaticMethodID(env, mainClass, "main",
433 "([Ljava/lang/String;)V");
ksrini20a64b22008-09-24 15:07:41 -0700434 CHECK_EXCEPTION_NULL_LEAVE(mainID);
duke6e45e102007-12-01 00:00:00 +0000435
ksrinie97d4002012-07-31 06:14:28 -0700436 /* Build platform specific argument array */
437 mainArgs = CreateApplicationArgs(env, argv, argc);
ksrini20a64b22008-09-24 15:07:41 -0700438 CHECK_EXCEPTION_NULL_LEAVE(mainArgs);
duke6e45e102007-12-01 00:00:00 +0000439
440 /* Invoke main method. */
441 (*env)->CallStaticVoidMethod(env, mainClass, mainID, mainArgs);
442
443 /*
444 * The launcher's exit code (in the absence of calls to
445 * System.exit) will be non-zero if main threw an exception.
446 */
447 ret = (*env)->ExceptionOccurred(env) == NULL ? 0 : 1;
ksrinie3ec45d2010-07-09 11:04:34 -0700448 LEAVE();
duke6e45e102007-12-01 00:00:00 +0000449}
450
duke6e45e102007-12-01 00:00:00 +0000451/*
452 * Checks the command line options to find which JVM type was
453 * specified. If no command line option was given for the JVM type,
454 * the default type is used. The environment variable
455 * JDK_ALTERNATE_VM and the command line option -XXaltjvm= are also
456 * checked as ways of specifying which JVM type to invoke.
457 */
458char *
459CheckJvmType(int *pargc, char ***argv, jboolean speculative) {
460 int i, argi;
461 int argc;
462 char **newArgv;
463 int newArgvIdx = 0;
464 int isVMType;
465 int jvmidx = -1;
466 char *jvmtype = getenv("JDK_ALTERNATE_VM");
467
468 argc = *pargc;
469
470 /* To make things simpler we always copy the argv array */
471 newArgv = JLI_MemAlloc((argc + 1) * sizeof(char *));
472
473 /* The program name is always present */
474 newArgv[newArgvIdx++] = (*argv)[0];
475
476 for (argi = 1; argi < argc; argi++) {
477 char *arg = (*argv)[argi];
478 isVMType = 0;
479
480 if (IsJavaArgs()) {
481 if (arg[0] != '-') {
482 newArgv[newArgvIdx++] = arg;
483 continue;
484 }
485 } else {
486 if (JLI_StrCmp(arg, "-classpath") == 0 ||
487 JLI_StrCmp(arg, "-cp") == 0) {
488 newArgv[newArgvIdx++] = arg;
489 argi++;
490 if (argi < argc) {
491 newArgv[newArgvIdx++] = (*argv)[argi];
492 }
493 continue;
494 }
495 if (arg[0] != '-') break;
496 }
497
498 /* Did the user pass an explicit VM type? */
499 i = KnownVMIndex(arg);
500 if (i >= 0) {
501 jvmtype = knownVMs[jvmidx = i].name + 1; /* skip the - */
502 isVMType = 1;
503 *pargc = *pargc - 1;
504 }
505
506 /* Did the user specify an "alternate" VM? */
507 else if (JLI_StrCCmp(arg, "-XXaltjvm=") == 0 || JLI_StrCCmp(arg, "-J-XXaltjvm=") == 0) {
508 isVMType = 1;
509 jvmtype = arg+((arg[1]=='X')? 10 : 12);
510 jvmidx = -1;
511 }
512
513 if (!isVMType) {
514 newArgv[newArgvIdx++] = arg;
515 }
516 }
517
518 /*
519 * Finish copying the arguments if we aborted the above loop.
520 * NOTE that if we aborted via "break" then we did NOT copy the
521 * last argument above, and in addition argi will be less than
522 * argc.
523 */
524 while (argi < argc) {
525 newArgv[newArgvIdx++] = (*argv)[argi];
526 argi++;
527 }
528
529 /* argv is null-terminated */
530 newArgv[newArgvIdx] = 0;
531
532 /* Copy back argv */
533 *argv = newArgv;
534 *pargc = newArgvIdx;
535
536 /* use the default VM type if not specified (no alias processing) */
537 if (jvmtype == NULL) {
538 char* result = knownVMs[0].name+1;
539 /* Use a different VM type if we are on a server class machine? */
540 if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) &&
541 (ServerClassMachine() == JNI_TRUE)) {
542 result = knownVMs[0].server_class+1;
543 }
544 JLI_TraceLauncher("Default VM: %s\n", result);
545 return result;
546 }
547
548 /* if using an alternate VM, no alias processing */
549 if (jvmidx < 0)
550 return jvmtype;
551
552 /* Resolve aliases first */
553 {
554 int loopCount = 0;
555 while (knownVMs[jvmidx].flag == VM_ALIASED_TO) {
556 int nextIdx = KnownVMIndex(knownVMs[jvmidx].alias);
557
558 if (loopCount > knownVMsCount) {
559 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700560 JLI_ReportErrorMessage(CFG_ERROR1);
duke6e45e102007-12-01 00:00:00 +0000561 exit(1);
562 } else {
563 return "ERROR";
564 /* break; */
565 }
566 }
567
568 if (nextIdx < 0) {
569 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700570 JLI_ReportErrorMessage(CFG_ERROR2, knownVMs[jvmidx].alias);
duke6e45e102007-12-01 00:00:00 +0000571 exit(1);
572 } else {
573 return "ERROR";
574 }
575 }
576 jvmidx = nextIdx;
577 jvmtype = knownVMs[jvmidx].name+1;
578 loopCount++;
579 }
580 }
581
582 switch (knownVMs[jvmidx].flag) {
583 case VM_WARN:
584 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700585 JLI_ReportErrorMessage(CFG_WARN1, jvmtype, knownVMs[0].name + 1);
duke6e45e102007-12-01 00:00:00 +0000586 }
587 /* fall through */
588 case VM_IGNORE:
589 jvmtype = knownVMs[jvmidx=0].name + 1;
590 /* fall through */
591 case VM_KNOWN:
592 break;
593 case VM_ERROR:
594 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700595 JLI_ReportErrorMessage(CFG_ERROR3, jvmtype);
duke6e45e102007-12-01 00:00:00 +0000596 exit(1);
597 } else {
598 return "ERROR";
599 }
600 }
601
602 return jvmtype;
603}
604
605/* copied from HotSpot function "atomll()" */
606static int
ksrini8e20e1c2010-11-23 16:52:39 -0800607parse_size(const char *s, jlong *result) {
duke6e45e102007-12-01 00:00:00 +0000608 jlong n = 0;
609 int args_read = sscanf(s, jlong_format_specifier(), &n);
610 if (args_read != 1) {
611 return 0;
612 }
613 while (*s != '\0' && *s >= '0' && *s <= '9') {
614 s++;
615 }
616 // 4705540: illegal if more characters are found after the first non-digit
617 if (JLI_StrLen(s) > 1) {
618 return 0;
619 }
620 switch (*s) {
621 case 'T': case 't':
622 *result = n * GB * KB;
623 return 1;
624 case 'G': case 'g':
625 *result = n * GB;
626 return 1;
627 case 'M': case 'm':
628 *result = n * MB;
629 return 1;
630 case 'K': case 'k':
631 *result = n * KB;
632 return 1;
633 case '\0':
634 *result = n;
635 return 1;
636 default:
637 /* Create JVM with default stack and let VM handle malformed -Xss string*/
638 return 0;
639 }
640}
641
642/*
643 * Adds a new VM option with the given given name and value.
644 */
645void
646AddOption(char *str, void *info)
647{
648 /*
649 * Expand options array if needed to accommodate at least one more
650 * VM option.
651 */
652 if (numOptions >= maxOptions) {
653 if (options == 0) {
654 maxOptions = 4;
655 options = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
656 } else {
657 JavaVMOption *tmp;
658 maxOptions *= 2;
659 tmp = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
660 memcpy(tmp, options, numOptions * sizeof(JavaVMOption));
661 JLI_MemFree(options);
662 options = tmp;
663 }
664 }
665 options[numOptions].optionString = str;
666 options[numOptions++].extraInfo = info;
667
668 if (JLI_StrCCmp(str, "-Xss") == 0) {
ksrini8e20e1c2010-11-23 16:52:39 -0800669 jlong tmp;
670 if (parse_size(str + 4, &tmp)) {
671 threadStackSize = tmp;
672 }
673 }
674
675 if (JLI_StrCCmp(str, "-Xmx") == 0) {
676 jlong tmp;
677 if (parse_size(str + 4, &tmp)) {
ksrini6d575f82010-12-23 13:51:30 -0800678 maxHeapSize = tmp;
679 }
680 }
681
682 if (JLI_StrCCmp(str, "-Xms") == 0) {
683 jlong tmp;
684 if (parse_size(str + 4, &tmp)) {
mchungea290e22011-01-21 09:43:57 -0800685 initialHeapSize = tmp;
ksrini8e20e1c2010-11-23 16:52:39 -0800686 }
duke6e45e102007-12-01 00:00:00 +0000687 }
688}
689
690static void
691SetClassPath(const char *s)
692{
693 char *def;
martinc0ca3352009-06-22 16:41:27 -0700694 const char *orig = s;
695 static const char format[] = "-Djava.class.path=%s";
ksrinie1a34382012-04-24 10:37:01 -0700696 /*
697 * usually we should not get a null pointer, but there are cases where
698 * we might just get one, in which case we simply ignore it, and let the
699 * caller deal with it
700 */
701 if (s == NULL)
702 return;
duke6e45e102007-12-01 00:00:00 +0000703 s = JLI_WildcardExpandClasspath(s);
martinc0ca3352009-06-22 16:41:27 -0700704 def = JLI_MemAlloc(sizeof(format)
705 - 2 /* strlen("%s") */
706 + JLI_StrLen(s));
707 sprintf(def, format, s);
duke6e45e102007-12-01 00:00:00 +0000708 AddOption(def, NULL);
martinc0ca3352009-06-22 16:41:27 -0700709 if (s != orig)
710 JLI_MemFree((char *) s);
duke6e45e102007-12-01 00:00:00 +0000711}
712
713/*
714 * The SelectVersion() routine ensures that an appropriate version of
715 * the JRE is running. The specification for the appropriate version
716 * is obtained from either the manifest of a jar file (preferred) or
717 * from command line options.
718 * The routine also parses splash screen command line options and
719 * passes on their values in private environment variables.
720 */
721static void
722SelectVersion(int argc, char **argv, char **main_class)
723{
724 char *arg;
725 char **new_argv;
726 char **new_argp;
727 char *operand;
728 char *version = NULL;
729 char *jre = NULL;
730 int jarflag = 0;
731 int headlessflag = 0;
732 int restrict_search = -1; /* -1 implies not known */
733 manifest_info info;
734 char env_entry[MAXNAMELEN + 24] = ENV_ENTRY "=";
735 char *splash_file_name = NULL;
736 char *splash_jar_name = NULL;
737 char *env_in;
738 int res;
739
740 /*
741 * If the version has already been selected, set *main_class
742 * with the value passed through the environment (if any) and
743 * simply return.
744 */
745 if ((env_in = getenv(ENV_ENTRY)) != NULL) {
746 if (*env_in != '\0')
747 *main_class = JLI_StringDup(env_in);
748 return;
749 }
750
751 /*
752 * Scan through the arguments for options relevant to multiple JRE
753 * support. For reference, the command line syntax is defined as:
754 *
755 * SYNOPSIS
756 * java [options] class [argument...]
757 *
758 * java [options] -jar file.jar [argument...]
759 *
760 * As the scan is performed, make a copy of the argument list with
761 * the version specification options (new to 1.5) removed, so that
762 * a version less than 1.5 can be exec'd.
763 *
764 * Note that due to the syntax of the native Windows interface
765 * CreateProcess(), processing similar to the following exists in
766 * the Windows platform specific routine ExecJRE (in java_md.c).
767 * Changes here should be reproduced there.
768 */
769 new_argv = JLI_MemAlloc((argc + 1) * sizeof(char*));
770 new_argv[0] = argv[0];
771 new_argp = &new_argv[1];
772 argc--;
773 argv++;
774 while ((arg = *argv) != 0 && *arg == '-') {
775 if (JLI_StrCCmp(arg, "-version:") == 0) {
776 version = arg + 9;
777 } else if (JLI_StrCmp(arg, "-jre-restrict-search") == 0) {
778 restrict_search = 1;
779 } else if (JLI_StrCmp(arg, "-no-jre-restrict-search") == 0) {
780 restrict_search = 0;
781 } else {
782 if (JLI_StrCmp(arg, "-jar") == 0)
783 jarflag = 1;
784 /* deal with "unfortunate" classpath syntax */
785 if ((JLI_StrCmp(arg, "-classpath") == 0 || JLI_StrCmp(arg, "-cp") == 0) &&
786 (argc >= 2)) {
787 *new_argp++ = arg;
788 argc--;
789 argv++;
790 arg = *argv;
791 }
792
793 /*
794 * Checking for headless toolkit option in the some way as AWT does:
795 * "true" means true and any other value means false
796 */
797 if (JLI_StrCmp(arg, "-Djava.awt.headless=true") == 0) {
798 headlessflag = 1;
799 } else if (JLI_StrCCmp(arg, "-Djava.awt.headless=") == 0) {
800 headlessflag = 0;
801 } else if (JLI_StrCCmp(arg, "-splash:") == 0) {
802 splash_file_name = arg+8;
803 }
804 *new_argp++ = arg;
805 }
806 argc--;
807 argv++;
808 }
809 if (argc <= 0) { /* No operand? Possibly legit with -[full]version */
810 operand = NULL;
811 } else {
812 argc--;
813 *new_argp++ = operand = *argv++;
814 }
815 while (argc-- > 0) /* Copy over [argument...] */
816 *new_argp++ = *argv++;
817 *new_argp = NULL;
818
819 /*
820 * If there is a jar file, read the manifest. If the jarfile can't be
821 * read, the manifest can't be read from the jar file, or the manifest
822 * is corrupt, issue the appropriate error messages and exit.
823 *
824 * Even if there isn't a jar file, construct a manifest_info structure
825 * containing the command line information. It's a convenient way to carry
826 * this data around.
827 */
828 if (jarflag && operand) {
829 if ((res = JLI_ParseManifest(operand, &info)) != 0) {
830 if (res == -1)
ksrini0e817162008-08-26 10:21:20 -0700831 JLI_ReportErrorMessage(JAR_ERROR2, operand);
duke6e45e102007-12-01 00:00:00 +0000832 else
ksrini0e817162008-08-26 10:21:20 -0700833 JLI_ReportErrorMessage(JAR_ERROR3, operand);
duke6e45e102007-12-01 00:00:00 +0000834 exit(1);
835 }
836
837 /*
838 * Command line splash screen option should have precedence
839 * over the manifest, so the manifest data is used only if
840 * splash_file_name has not been initialized above during command
841 * line parsing
842 */
843 if (!headlessflag && !splash_file_name && info.splashscreen_image_file_name) {
844 splash_file_name = info.splashscreen_image_file_name;
845 splash_jar_name = operand;
846 }
847 } else {
848 info.manifest_version = NULL;
849 info.main_class = NULL;
850 info.jre_version = NULL;
851 info.jre_restrict_search = 0;
852 }
853
854 /*
855 * Passing on splash screen info in environment variables
856 */
857 if (splash_file_name && !headlessflag) {
858 char* splash_file_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_FILE_ENV_ENTRY "=")+JLI_StrLen(splash_file_name)+1);
859 JLI_StrCpy(splash_file_entry, SPLASH_FILE_ENV_ENTRY "=");
860 JLI_StrCat(splash_file_entry, splash_file_name);
861 putenv(splash_file_entry);
862 }
863 if (splash_jar_name && !headlessflag) {
864 char* splash_jar_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_JAR_ENV_ENTRY "=")+JLI_StrLen(splash_jar_name)+1);
865 JLI_StrCpy(splash_jar_entry, SPLASH_JAR_ENV_ENTRY "=");
866 JLI_StrCat(splash_jar_entry, splash_jar_name);
867 putenv(splash_jar_entry);
868 }
869
870 /*
871 * The JRE-Version and JRE-Restrict-Search values (if any) from the
872 * manifest are overwritten by any specified on the command line.
873 */
874 if (version != NULL)
875 info.jre_version = version;
876 if (restrict_search != -1)
877 info.jre_restrict_search = restrict_search;
878
879 /*
880 * "Valid" returns (other than unrecoverable errors) follow. Set
881 * main_class as a side-effect of this routine.
882 */
883 if (info.main_class != NULL)
884 *main_class = JLI_StringDup(info.main_class);
885
886 /*
887 * If no version selection information is found either on the command
888 * line or in the manifest, simply return.
889 */
890 if (info.jre_version == NULL) {
891 JLI_FreeManifest();
892 JLI_MemFree(new_argv);
893 return;
894 }
895
896 /*
897 * Check for correct syntax of the version specification (JSR 56).
898 */
899 if (!JLI_ValidVersionString(info.jre_version)) {
ksrini0e817162008-08-26 10:21:20 -0700900 JLI_ReportErrorMessage(SPC_ERROR1, info.jre_version);
duke6e45e102007-12-01 00:00:00 +0000901 exit(1);
902 }
903
904 /*
905 * Find the appropriate JVM on the system. Just to be as forgiving as
906 * possible, if the standard algorithms don't locate an appropriate
907 * jre, check to see if the one running will satisfy the requirements.
908 * This can happen on systems which haven't been set-up for multiple
909 * JRE support.
910 */
911 jre = LocateJRE(&info);
912 JLI_TraceLauncher("JRE-Version = %s, JRE-Restrict-Search = %s Selected = %s\n",
913 (info.jre_version?info.jre_version:"null"),
914 (info.jre_restrict_search?"true":"false"), (jre?jre:"null"));
915
916 if (jre == NULL) {
917 if (JLI_AcceptableRelease(GetFullVersion(), info.jre_version)) {
918 JLI_FreeManifest();
919 JLI_MemFree(new_argv);
920 return;
921 } else {
ksrini0e817162008-08-26 10:21:20 -0700922 JLI_ReportErrorMessage(CFG_ERROR4, info.jre_version);
duke6e45e102007-12-01 00:00:00 +0000923 exit(1);
924 }
925 }
926
927 /*
928 * If I'm not the chosen one, exec the chosen one. Returning from
929 * ExecJRE indicates that I am indeed the chosen one.
930 *
931 * The private environment variable _JAVA_VERSION_SET is used to
932 * prevent the chosen one from re-reading the manifest file and
933 * using the values found within to override the (potential) command
934 * line flags stripped from argv (because the target may not
935 * understand them). Passing the MainClass value is an optimization
936 * to avoid locating, expanding and parsing the manifest extra
937 * times.
938 */
ksrini8760ca92008-09-04 09:43:32 -0700939 if (info.main_class != NULL) {
940 if (JLI_StrLen(info.main_class) <= MAXNAMELEN) {
941 (void)JLI_StrCat(env_entry, info.main_class);
942 } else {
asaha80ccd422009-04-16 21:08:04 -0700943 JLI_ReportErrorMessage(CLS_ERROR5, MAXNAMELEN);
ksrini8760ca92008-09-04 09:43:32 -0700944 exit(1);
945 }
946 }
duke6e45e102007-12-01 00:00:00 +0000947 (void)putenv(env_entry);
948 ExecJRE(jre, new_argv);
949 JLI_FreeManifest();
950 JLI_MemFree(new_argv);
951 return;
952}
953
954/*
955 * Parses command line arguments. Returns JNI_FALSE if launcher
956 * should exit without starting vm, returns JNI_TRUE if vm needs
mchungea290e22011-01-21 09:43:57 -0800957 * to be started to process given options. *pret (the launcher
duke6e45e102007-12-01 00:00:00 +0000958 * process return value) is set to 0 for a normal exit.
959 */
960static jboolean
mchungea290e22011-01-21 09:43:57 -0800961ParseArguments(int *pargc, char ***pargv,
962 int *pmode, char **pwhat,
963 int *pret, const char *jrepath)
duke6e45e102007-12-01 00:00:00 +0000964{
965 int argc = *pargc;
966 char **argv = *pargv;
mchungea290e22011-01-21 09:43:57 -0800967 int mode = LM_UNKNOWN;
duke6e45e102007-12-01 00:00:00 +0000968 char *arg;
969
970 *pret = 0;
971
972 while ((arg = *argv) != 0 && *arg == '-') {
973 argv++; --argc;
974 if (JLI_StrCmp(arg, "-classpath") == 0 || JLI_StrCmp(arg, "-cp") == 0) {
975 ARG_CHECK (argc, ARG_ERROR1, arg);
976 SetClassPath(*argv);
mchungea290e22011-01-21 09:43:57 -0800977 mode = LM_CLASS;
duke6e45e102007-12-01 00:00:00 +0000978 argv++; --argc;
979 } else if (JLI_StrCmp(arg, "-jar") == 0) {
980 ARG_CHECK (argc, ARG_ERROR2, arg);
mchungea290e22011-01-21 09:43:57 -0800981 mode = LM_JAR;
duke6e45e102007-12-01 00:00:00 +0000982 } else if (JLI_StrCmp(arg, "-help") == 0 ||
983 JLI_StrCmp(arg, "-h") == 0 ||
984 JLI_StrCmp(arg, "-?") == 0) {
985 printUsage = JNI_TRUE;
986 return JNI_TRUE;
987 } else if (JLI_StrCmp(arg, "-version") == 0) {
988 printVersion = JNI_TRUE;
989 return JNI_TRUE;
990 } else if (JLI_StrCmp(arg, "-showversion") == 0) {
991 showVersion = JNI_TRUE;
992 } else if (JLI_StrCmp(arg, "-X") == 0) {
993 printXUsage = JNI_TRUE;
994 return JNI_TRUE;
995/*
ksrini8e20e1c2010-11-23 16:52:39 -0800996 * The following case checks for -XshowSettings OR -XshowSetting:SUBOPT.
997 * In the latter case, any SUBOPT value not recognized will default to "all"
998 */
999 } else if (JLI_StrCmp(arg, "-XshowSettings") == 0 ||
1000 JLI_StrCCmp(arg, "-XshowSettings:") == 0) {
1001 showSettings = arg;
ksrini9636e2e2011-02-03 15:41:23 -08001002 } else if (JLI_StrCmp(arg, "-Xdiag") == 0) {
1003 AddOption("-Dsun.java.launcher.diag=true", NULL);
ksrini8e20e1c2010-11-23 16:52:39 -08001004/*
duke6e45e102007-12-01 00:00:00 +00001005 * The following case provide backward compatibility with old-style
1006 * command line options.
1007 */
1008 } else if (JLI_StrCmp(arg, "-fullversion") == 0) {
ksrini0e817162008-08-26 10:21:20 -07001009 JLI_ReportMessage("%s full version \"%s\"", _launcher_name, GetFullVersion());
duke6e45e102007-12-01 00:00:00 +00001010 return JNI_FALSE;
1011 } else if (JLI_StrCmp(arg, "-verbosegc") == 0) {
1012 AddOption("-verbose:gc", NULL);
1013 } else if (JLI_StrCmp(arg, "-t") == 0) {
1014 AddOption("-Xt", NULL);
1015 } else if (JLI_StrCmp(arg, "-tm") == 0) {
1016 AddOption("-Xtm", NULL);
1017 } else if (JLI_StrCmp(arg, "-debug") == 0) {
1018 AddOption("-Xdebug", NULL);
1019 } else if (JLI_StrCmp(arg, "-noclassgc") == 0) {
1020 AddOption("-Xnoclassgc", NULL);
1021 } else if (JLI_StrCmp(arg, "-Xfuture") == 0) {
1022 AddOption("-Xverify:all", NULL);
1023 } else if (JLI_StrCmp(arg, "-verify") == 0) {
1024 AddOption("-Xverify:all", NULL);
1025 } else if (JLI_StrCmp(arg, "-verifyremote") == 0) {
1026 AddOption("-Xverify:remote", NULL);
1027 } else if (JLI_StrCmp(arg, "-noverify") == 0) {
1028 AddOption("-Xverify:none", NULL);
1029 } else if (JLI_StrCCmp(arg, "-prof") == 0) {
1030 char *p = arg + 5;
1031 char *tmp = JLI_MemAlloc(JLI_StrLen(arg) + 50);
1032 if (*p) {
1033 sprintf(tmp, "-Xrunhprof:cpu=old,file=%s", p + 1);
1034 } else {
1035 sprintf(tmp, "-Xrunhprof:cpu=old,file=java.prof");
1036 }
1037 AddOption(tmp, NULL);
1038 } else if (JLI_StrCCmp(arg, "-ss") == 0 ||
1039 JLI_StrCCmp(arg, "-oss") == 0 ||
1040 JLI_StrCCmp(arg, "-ms") == 0 ||
1041 JLI_StrCCmp(arg, "-mx") == 0) {
1042 char *tmp = JLI_MemAlloc(JLI_StrLen(arg) + 6);
1043 sprintf(tmp, "-X%s", arg + 1); /* skip '-' */
1044 AddOption(tmp, NULL);
1045 } else if (JLI_StrCmp(arg, "-checksource") == 0 ||
1046 JLI_StrCmp(arg, "-cs") == 0 ||
1047 JLI_StrCmp(arg, "-noasyncgc") == 0) {
1048 /* No longer supported */
ksrini0e817162008-08-26 10:21:20 -07001049 JLI_ReportErrorMessage(ARG_WARN, arg);
duke6e45e102007-12-01 00:00:00 +00001050 } else if (JLI_StrCCmp(arg, "-version:") == 0 ||
1051 JLI_StrCmp(arg, "-no-jre-restrict-search") == 0 ||
1052 JLI_StrCmp(arg, "-jre-restrict-search") == 0 ||
1053 JLI_StrCCmp(arg, "-splash:") == 0) {
1054 ; /* Ignore machine independent options already handled */
michaelm5ac8c152012-03-06 20:34:38 +00001055 } else if (ProcessPlatformOption(arg)) {
1056 ; /* Processing of platform dependent options */
1057 } else if (RemovableOption(arg)) {
duke6e45e102007-12-01 00:00:00 +00001058 ; /* Do not pass option to vm. */
1059 } else {
1060 AddOption(arg, NULL);
1061 }
1062 }
1063
1064 if (--argc >= 0) {
mchungea290e22011-01-21 09:43:57 -08001065 *pwhat = *argv++;
1066 }
1067
1068 if (*pwhat == NULL) {
1069 *pret = 1;
1070 } else if (mode == LM_UNKNOWN) {
1071 /* default to LM_CLASS if -jar and -cp option are
1072 * not specified */
1073 mode = LM_CLASS;
1074 }
1075
1076 if (argc >= 0) {
duke6e45e102007-12-01 00:00:00 +00001077 *pargc = argc;
1078 *pargv = argv;
1079 }
mchungea290e22011-01-21 09:43:57 -08001080
1081 *pmode = mode;
1082
duke6e45e102007-12-01 00:00:00 +00001083 return JNI_TRUE;
1084}
1085
1086/*
1087 * Initializes the Java Virtual Machine. Also frees options array when
1088 * finished.
1089 */
1090static jboolean
1091InitializeJVM(JavaVM **pvm, JNIEnv **penv, InvocationFunctions *ifn)
1092{
1093 JavaVMInitArgs args;
1094 jint r;
1095
1096 memset(&args, 0, sizeof(args));
1097 args.version = JNI_VERSION_1_2;
1098 args.nOptions = numOptions;
1099 args.options = options;
1100 args.ignoreUnrecognized = JNI_FALSE;
1101
1102 if (JLI_IsTraceLauncher()) {
1103 int i = 0;
1104 printf("JavaVM args:\n ");
1105 printf("version 0x%08lx, ", (long)args.version);
1106 printf("ignoreUnrecognized is %s, ",
1107 args.ignoreUnrecognized ? "JNI_TRUE" : "JNI_FALSE");
1108 printf("nOptions is %ld\n", (long)args.nOptions);
1109 for (i = 0; i < numOptions; i++)
1110 printf(" option[%2d] = '%s'\n",
1111 i, args.options[i].optionString);
1112 }
1113
1114 r = ifn->CreateJavaVM(pvm, (void **)penv, &args);
1115 JLI_MemFree(options);
1116 return r == JNI_OK;
1117}
1118
ksrini7d9872e2011-03-20 08:41:33 -07001119static jclass helperClass = NULL;
1120
ksrinie97d4002012-07-31 06:14:28 -07001121jclass
1122GetLauncherHelperClass(JNIEnv *env)
1123{
ksrini7d9872e2011-03-20 08:41:33 -07001124 if (helperClass == NULL) {
1125 NULL_CHECK0(helperClass = FindBootStrapClass(env,
1126 "sun/launcher/LauncherHelper"));
duke6e45e102007-12-01 00:00:00 +00001127 }
ksrini7d9872e2011-03-20 08:41:33 -07001128 return helperClass;
duke6e45e102007-12-01 00:00:00 +00001129}
1130
ksrini7d9872e2011-03-20 08:41:33 -07001131static jmethodID makePlatformStringMID = NULL;
duke6e45e102007-12-01 00:00:00 +00001132/*
1133 * Returns a new Java string object for the specified platform string.
1134 */
1135static jstring
1136NewPlatformString(JNIEnv *env, char *s)
1137{
1138 int len = (int)JLI_StrLen(s);
duke6e45e102007-12-01 00:00:00 +00001139 jbyteArray ary;
ksrini7d9872e2011-03-20 08:41:33 -07001140 jclass cls = GetLauncherHelperClass(env);
1141 NULL_CHECK0(cls);
duke6e45e102007-12-01 00:00:00 +00001142 if (s == NULL)
1143 return 0;
duke6e45e102007-12-01 00:00:00 +00001144
1145 ary = (*env)->NewByteArray(env, len);
1146 if (ary != 0) {
1147 jstring str = 0;
1148 (*env)->SetByteArrayRegion(env, ary, 0, len, (jbyte *)s);
1149 if (!(*env)->ExceptionOccurred(env)) {
ksrini7d9872e2011-03-20 08:41:33 -07001150 if (makePlatformStringMID == NULL) {
1151 NULL_CHECK0(makePlatformStringMID = (*env)->GetStaticMethodID(env,
1152 cls, "makePlatformString", "(Z[B)Ljava/lang/String;"));
duke6e45e102007-12-01 00:00:00 +00001153 }
ksrini7d9872e2011-03-20 08:41:33 -07001154 str = (*env)->CallStaticObjectMethod(env, cls,
1155 makePlatformStringMID, USE_STDERR, ary);
duke6e45e102007-12-01 00:00:00 +00001156 (*env)->DeleteLocalRef(env, ary);
1157 return str;
1158 }
1159 }
1160 return 0;
1161}
1162
1163/*
1164 * Returns a new array of Java string objects for the specified
1165 * array of platform strings.
1166 */
ksrinie97d4002012-07-31 06:14:28 -07001167jobjectArray
duke6e45e102007-12-01 00:00:00 +00001168NewPlatformStringArray(JNIEnv *env, char **strv, int strc)
1169{
1170 jarray cls;
1171 jarray ary;
1172 int i;
1173
ksrini20a64b22008-09-24 15:07:41 -07001174 NULL_CHECK0(cls = FindBootStrapClass(env, "java/lang/String"));
duke6e45e102007-12-01 00:00:00 +00001175 NULL_CHECK0(ary = (*env)->NewObjectArray(env, strc, cls, 0));
1176 for (i = 0; i < strc; i++) {
1177 jstring str = NewPlatformString(env, *strv++);
1178 NULL_CHECK0(str);
1179 (*env)->SetObjectArrayElement(env, ary, i, str);
1180 (*env)->DeleteLocalRef(env, str);
1181 }
1182 return ary;
1183}
1184
1185/*
ksrini20a64b22008-09-24 15:07:41 -07001186 * Loads a class and verifies that the main class is present and it is ok to
1187 * call it for more details refer to the java implementation.
duke6e45e102007-12-01 00:00:00 +00001188 */
1189static jclass
mchungea290e22011-01-21 09:43:57 -08001190LoadMainClass(JNIEnv *env, int mode, char *name)
duke6e45e102007-12-01 00:00:00 +00001191{
ksrini20a64b22008-09-24 15:07:41 -07001192 jmethodID mid;
1193 jstring str;
1194 jobject result;
duke6e45e102007-12-01 00:00:00 +00001195 jlong start, end;
ksrini7d9872e2011-03-20 08:41:33 -07001196 jclass cls = GetLauncherHelperClass(env);
1197 NULL_CHECK0(cls);
ksrini20a64b22008-09-24 15:07:41 -07001198 if (JLI_IsTraceLauncher()) {
duke6e45e102007-12-01 00:00:00 +00001199 start = CounterGet();
ksrini20a64b22008-09-24 15:07:41 -07001200 }
ksrini7d9872e2011-03-20 08:41:33 -07001201 NULL_CHECK0(mid = (*env)->GetStaticMethodID(env, cls,
1202 "checkAndLoadMain",
1203 "(ZILjava/lang/String;)Ljava/lang/Class;"));
1204
ksriniac5e02f2012-01-11 08:14:47 -08001205 str = NewPlatformString(env, name);
ksrini7d9872e2011-03-20 08:41:33 -07001206 result = (*env)->CallStaticObjectMethod(env, cls, mid, USE_STDERR, mode, str);
duke6e45e102007-12-01 00:00:00 +00001207
1208 if (JLI_IsTraceLauncher()) {
1209 end = CounterGet();
1210 printf("%ld micro seconds to load main class\n",
1211 (long)(jint)Counter2Micros(end-start));
ksrinie97d4002012-07-31 06:14:28 -07001212 printf("----%s----\n", JLDEBUG_ENV_ENTRY);
duke6e45e102007-12-01 00:00:00 +00001213 }
1214
ksrini20a64b22008-09-24 15:07:41 -07001215 return (jclass)result;
duke6e45e102007-12-01 00:00:00 +00001216}
1217
duke6e45e102007-12-01 00:00:00 +00001218/*
1219 * For tools, convert command line args thus:
1220 * javac -cp foo:foo/"*" -J-ms32m ...
1221 * java -ms32m -cp JLI_WildcardExpandClasspath(foo:foo/"*") ...
1222 *
1223 * Takes 4 parameters, and returns the populated arguments
1224 */
1225static void
1226TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv)
1227{
1228 int argc = *pargc;
1229 char **argv = *pargv;
1230 int nargc = argc + jargc;
1231 char **nargv = JLI_MemAlloc((nargc + 1) * sizeof(char *));
1232 int i;
1233
1234 *pargc = nargc;
1235 *pargv = nargv;
1236
1237 /* Copy the VM arguments (i.e. prefixed with -J) */
1238 for (i = 0; i < jargc; i++) {
1239 const char *arg = jargv[i];
1240 if (arg[0] == '-' && arg[1] == 'J') {
1241 *nargv++ = ((arg + 2) == NULL) ? NULL : JLI_StringDup(arg + 2);
1242 }
1243 }
1244
1245 for (i = 0; i < argc; i++) {
1246 char *arg = argv[i];
1247 if (arg[0] == '-' && arg[1] == 'J') {
1248 if (arg[2] == '\0') {
ksrini0e817162008-08-26 10:21:20 -07001249 JLI_ReportErrorMessage(ARG_ERROR3);
duke6e45e102007-12-01 00:00:00 +00001250 exit(1);
1251 }
1252 *nargv++ = arg + 2;
1253 }
1254 }
1255
1256 /* Copy the rest of the arguments */
1257 for (i = 0; i < jargc ; i++) {
1258 const char *arg = jargv[i];
1259 if (arg[0] != '-' || arg[1] != 'J') {
1260 *nargv++ = (arg == NULL) ? NULL : JLI_StringDup(arg);
1261 }
1262 }
1263 for (i = 0; i < argc; i++) {
1264 char *arg = argv[i];
1265 if (arg[0] == '-') {
1266 if (arg[1] == 'J')
1267 continue;
1268 if (IsWildCardEnabled() && arg[1] == 'c'
1269 && (JLI_StrCmp(arg, "-cp") == 0 ||
1270 JLI_StrCmp(arg, "-classpath") == 0)
1271 && i < argc - 1) {
1272 *nargv++ = arg;
1273 *nargv++ = (char *) JLI_WildcardExpandClasspath(argv[i+1]);
1274 i++;
1275 continue;
1276 }
1277 }
1278 *nargv++ = arg;
1279 }
1280 *nargv = 0;
1281}
1282
1283/*
1284 * For our tools, we try to add 3 VM options:
1285 * -Denv.class.path=<envcp>
1286 * -Dapplication.home=<apphome>
1287 * -Djava.class.path=<appcp>
1288 * <envcp> is the user's setting of CLASSPATH -- for instance the user
1289 * tells javac where to find binary classes through this environment
1290 * variable. Notice that users will be able to compile against our
1291 * tools classes (sun.tools.javac.Main) only if they explicitly add
1292 * tools.jar to CLASSPATH.
1293 * <apphome> is the directory where the application is installed.
1294 * <appcp> is the classpath to where our apps' classfiles are.
1295 */
1296static jboolean
1297AddApplicationOptions(int cpathc, const char **cpathv)
1298{
1299 char *envcp, *appcp, *apphome;
1300 char home[MAXPATHLEN]; /* application home */
1301 char separator[] = { PATH_SEPARATOR, '\0' };
1302 int size, i;
1303
1304 {
1305 const char *s = getenv("CLASSPATH");
1306 if (s) {
1307 s = (char *) JLI_WildcardExpandClasspath(s);
1308 /* 40 for -Denv.class.path= */
1309 envcp = (char *)JLI_MemAlloc(JLI_StrLen(s) + 40);
1310 sprintf(envcp, "-Denv.class.path=%s", s);
1311 AddOption(envcp, NULL);
1312 }
1313 }
1314
1315 if (!GetApplicationHome(home, sizeof(home))) {
ksrini0e817162008-08-26 10:21:20 -07001316 JLI_ReportErrorMessage(CFG_ERROR5);
duke6e45e102007-12-01 00:00:00 +00001317 return JNI_FALSE;
1318 }
1319
1320 /* 40 for '-Dapplication.home=' */
1321 apphome = (char *)JLI_MemAlloc(JLI_StrLen(home) + 40);
1322 sprintf(apphome, "-Dapplication.home=%s", home);
1323 AddOption(apphome, NULL);
1324
1325 /* How big is the application's classpath? */
1326 size = 40; /* 40: "-Djava.class.path=" */
1327 for (i = 0; i < cpathc; i++) {
1328 size += (int)JLI_StrLen(home) + (int)JLI_StrLen(cpathv[i]) + 1; /* 1: separator */
1329 }
1330 appcp = (char *)JLI_MemAlloc(size + 1);
1331 JLI_StrCpy(appcp, "-Djava.class.path=");
1332 for (i = 0; i < cpathc; i++) {
1333 JLI_StrCat(appcp, home); /* c:\program files\myapp */
1334 JLI_StrCat(appcp, cpathv[i]); /* \lib\myapp.jar */
1335 JLI_StrCat(appcp, separator); /* ; */
1336 }
1337 appcp[JLI_StrLen(appcp)-1] = '\0'; /* remove trailing path separator */
1338 AddOption(appcp, NULL);
1339 return JNI_TRUE;
1340}
1341
1342/*
1343 * inject the -Dsun.java.command pseudo property into the args structure
1344 * this pseudo property is used in the HotSpot VM to expose the
1345 * Java class name and arguments to the main method to the VM. The
1346 * HotSpot VM uses this pseudo property to store the Java class name
1347 * (or jar file name) and the arguments to the class's main method
1348 * to the instrumentation memory region. The sun.java.command pseudo
1349 * property is not exported by HotSpot to the Java layer.
1350 */
1351void
mchungea290e22011-01-21 09:43:57 -08001352SetJavaCommandLineProp(char *what, int argc, char **argv)
duke6e45e102007-12-01 00:00:00 +00001353{
1354
1355 int i = 0;
1356 size_t len = 0;
1357 char* javaCommand = NULL;
1358 char* dashDstr = "-Dsun.java.command=";
1359
mchungea290e22011-01-21 09:43:57 -08001360 if (what == NULL) {
duke6e45e102007-12-01 00:00:00 +00001361 /* unexpected, one of these should be set. just return without
1362 * setting the property
1363 */
1364 return;
1365 }
1366
duke6e45e102007-12-01 00:00:00 +00001367 /* determine the amount of memory to allocate assuming
1368 * the individual components will be space separated
1369 */
mchungea290e22011-01-21 09:43:57 -08001370 len = JLI_StrLen(what);
duke6e45e102007-12-01 00:00:00 +00001371 for (i = 0; i < argc; i++) {
1372 len += JLI_StrLen(argv[i]) + 1;
1373 }
1374
1375 /* allocate the memory */
1376 javaCommand = (char*) JLI_MemAlloc(len + JLI_StrLen(dashDstr) + 1);
1377
1378 /* build the -D string */
1379 *javaCommand = '\0';
1380 JLI_StrCat(javaCommand, dashDstr);
mchungea290e22011-01-21 09:43:57 -08001381 JLI_StrCat(javaCommand, what);
duke6e45e102007-12-01 00:00:00 +00001382
1383 for (i = 0; i < argc; i++) {
1384 /* the components of the string are space separated. In
1385 * the case of embedded white space, the relationship of
1386 * the white space separated components to their true
1387 * positional arguments will be ambiguous. This issue may
1388 * be addressed in a future release.
1389 */
1390 JLI_StrCat(javaCommand, " ");
1391 JLI_StrCat(javaCommand, argv[i]);
1392 }
1393
1394 AddOption(javaCommand, NULL);
1395}
1396
1397/*
1398 * JVM would like to know if it's created by a standard Sun launcher, or by
1399 * user native application, the following property indicates the former.
1400 */
mchungea290e22011-01-21 09:43:57 -08001401void
1402SetJavaLauncherProp() {
duke6e45e102007-12-01 00:00:00 +00001403 AddOption("-Dsun.java.launcher=SUN_STANDARD", NULL);
1404}
1405
1406/*
1407 * Prints the version information from the java.version and other properties.
1408 */
1409static void
1410PrintJavaVersion(JNIEnv *env, jboolean extraLF)
1411{
1412 jclass ver;
1413 jmethodID print;
1414
ksrini20a64b22008-09-24 15:07:41 -07001415 NULL_CHECK(ver = FindBootStrapClass(env, "sun/misc/Version"));
duke6e45e102007-12-01 00:00:00 +00001416 NULL_CHECK(print = (*env)->GetStaticMethodID(env,
1417 ver,
1418 (extraLF == JNI_TRUE) ? "println" : "print",
1419 "()V"
1420 )
1421 );
1422
1423 (*env)->CallStaticVoidMethod(env, ver, print);
1424}
1425
1426/*
ksrini8e20e1c2010-11-23 16:52:39 -08001427 * Prints all the Java settings, see the java implementation for more details.
1428 */
1429static void
1430ShowSettings(JNIEnv *env, char *optString)
1431{
ksrini8e20e1c2010-11-23 16:52:39 -08001432 jmethodID showSettingsID;
1433 jstring joptString;
ksrini7d9872e2011-03-20 08:41:33 -07001434 jclass cls = GetLauncherHelperClass(env);
1435 NULL_CHECK(cls);
ksrini8e20e1c2010-11-23 16:52:39 -08001436 NULL_CHECK(showSettingsID = (*env)->GetStaticMethodID(env, cls,
ksrini6d575f82010-12-23 13:51:30 -08001437 "showSettings", "(ZLjava/lang/String;JJJZ)V"));
ksrini8e20e1c2010-11-23 16:52:39 -08001438 joptString = (*env)->NewStringUTF(env, optString);
1439 (*env)->CallStaticVoidMethod(env, cls, showSettingsID,
ksrini7d9872e2011-03-20 08:41:33 -07001440 USE_STDERR,
ksrini8e20e1c2010-11-23 16:52:39 -08001441 joptString,
ksrini6d575f82010-12-23 13:51:30 -08001442 (jlong)initialHeapSize,
1443 (jlong)maxHeapSize,
ksrini8e20e1c2010-11-23 16:52:39 -08001444 (jlong)threadStackSize,
1445 ServerClassMachine());
1446}
1447
1448/*
ksrini20a64b22008-09-24 15:07:41 -07001449 * Prints default usage or the Xusage message, see sun.launcher.LauncherHelper.java
duke6e45e102007-12-01 00:00:00 +00001450 */
1451static void
1452PrintUsage(JNIEnv* env, jboolean doXUsage)
1453{
duke6e45e102007-12-01 00:00:00 +00001454 jmethodID initHelp, vmSelect, vmSynonym, vmErgo, printHelp, printXUsageMessage;
1455 jstring jprogname, vm1, vm2;
1456 int i;
ksrini7d9872e2011-03-20 08:41:33 -07001457 jclass cls = GetLauncherHelperClass(env);
1458 NULL_CHECK(cls);
duke6e45e102007-12-01 00:00:00 +00001459 if (doXUsage) {
1460 NULL_CHECK(printXUsageMessage = (*env)->GetStaticMethodID(env, cls,
1461 "printXUsageMessage", "(Z)V"));
ksrini7d9872e2011-03-20 08:41:33 -07001462 (*env)->CallStaticVoidMethod(env, cls, printXUsageMessage, USE_STDERR);
duke6e45e102007-12-01 00:00:00 +00001463 } else {
1464 NULL_CHECK(initHelp = (*env)->GetStaticMethodID(env, cls,
1465 "initHelpMessage", "(Ljava/lang/String;)V"));
1466
1467 NULL_CHECK(vmSelect = (*env)->GetStaticMethodID(env, cls, "appendVmSelectMessage",
1468 "(Ljava/lang/String;Ljava/lang/String;)V"));
1469
1470 NULL_CHECK(vmSynonym = (*env)->GetStaticMethodID(env, cls,
1471 "appendVmSynonymMessage",
1472 "(Ljava/lang/String;Ljava/lang/String;)V"));
1473 NULL_CHECK(vmErgo = (*env)->GetStaticMethodID(env, cls,
1474 "appendVmErgoMessage", "(ZLjava/lang/String;)V"));
1475
1476 NULL_CHECK(printHelp = (*env)->GetStaticMethodID(env, cls,
1477 "printHelpMessage", "(Z)V"));
1478
1479 jprogname = (*env)->NewStringUTF(env, _program_name);
1480
1481 /* Initialize the usage message with the usual preamble */
1482 (*env)->CallStaticVoidMethod(env, cls, initHelp, jprogname);
1483
1484
1485 /* Assemble the other variant part of the usage */
1486 if ((knownVMs[0].flag == VM_KNOWN) ||
1487 (knownVMs[0].flag == VM_IF_SERVER_CLASS)) {
1488 vm1 = (*env)->NewStringUTF(env, knownVMs[0].name);
1489 vm2 = (*env)->NewStringUTF(env, knownVMs[0].name+1);
1490 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1491 }
1492 for (i=1; i<knownVMsCount; i++) {
1493 if (knownVMs[i].flag == VM_KNOWN) {
1494 vm1 = (*env)->NewStringUTF(env, knownVMs[i].name);
1495 vm2 = (*env)->NewStringUTF(env, knownVMs[i].name+1);
1496 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1497 }
1498 }
1499 for (i=1; i<knownVMsCount; i++) {
1500 if (knownVMs[i].flag == VM_ALIASED_TO) {
1501 vm1 = (*env)->NewStringUTF(env, knownVMs[i].name);
1502 vm2 = (*env)->NewStringUTF(env, knownVMs[i].alias+1);
1503 (*env)->CallStaticVoidMethod(env, cls, vmSynonym, vm1, vm2);
1504 }
1505 }
1506
1507 /* The first known VM is the default */
1508 {
1509 jboolean isServerClassMachine = ServerClassMachine();
1510
1511 const char* defaultVM = knownVMs[0].name+1;
1512 if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) && isServerClassMachine) {
1513 defaultVM = knownVMs[0].server_class+1;
1514 }
1515
1516 vm1 = (*env)->NewStringUTF(env, defaultVM);
1517 (*env)->CallStaticVoidMethod(env, cls, vmErgo, isServerClassMachine, vm1);
1518 }
1519
1520 /* Complete the usage message and print to stderr*/
ksrini7d9872e2011-03-20 08:41:33 -07001521 (*env)->CallStaticVoidMethod(env, cls, printHelp, USE_STDERR);
duke6e45e102007-12-01 00:00:00 +00001522 }
1523 return;
1524}
1525
1526/*
1527 * Read the jvm.cfg file and fill the knownJVMs[] array.
1528 *
1529 * The functionality of the jvm.cfg file is subject to change without
1530 * notice and the mechanism will be removed in the future.
1531 *
1532 * The lexical structure of the jvm.cfg file is as follows:
1533 *
1534 * jvmcfg := { vmLine }
1535 * vmLine := knownLine
1536 * | aliasLine
1537 * | warnLine
1538 * | ignoreLine
1539 * | errorLine
1540 * | predicateLine
1541 * | commentLine
1542 * knownLine := flag "KNOWN" EOL
1543 * warnLine := flag "WARN" EOL
1544 * ignoreLine := flag "IGNORE" EOL
1545 * errorLine := flag "ERROR" EOL
1546 * aliasLine := flag "ALIASED_TO" flag EOL
1547 * predicateLine := flag "IF_SERVER_CLASS" flag EOL
1548 * commentLine := "#" text EOL
1549 * flag := "-" identifier
1550 *
1551 * The semantics are that when someone specifies a flag on the command line:
1552 * - if the flag appears on a knownLine, then the identifier is used as
1553 * the name of the directory holding the JVM library (the name of the JVM).
1554 * - if the flag appears as the first flag on an aliasLine, the identifier
1555 * of the second flag is used as the name of the JVM.
1556 * - if the flag appears on a warnLine, the identifier is used as the
1557 * name of the JVM, but a warning is generated.
1558 * - if the flag appears on an ignoreLine, the identifier is recognized as the
1559 * name of a JVM, but the identifier is ignored and the default vm used
1560 * - if the flag appears on an errorLine, an error is generated.
1561 * - if the flag appears as the first flag on a predicateLine, and
1562 * the machine on which you are running passes the predicate indicated,
1563 * then the identifier of the second flag is used as the name of the JVM,
1564 * otherwise the identifier of the first flag is used as the name of the JVM.
1565 * If no flag is given on the command line, the first vmLine of the jvm.cfg
1566 * file determines the name of the JVM.
1567 * PredicateLines are only interpreted on first vmLine of a jvm.cfg file,
1568 * since they only make sense if someone hasn't specified the name of the
1569 * JVM on the command line.
1570 *
1571 * The intent of the jvm.cfg file is to allow several JVM libraries to
1572 * be installed in different subdirectories of a single JRE installation,
1573 * for space-savings and convenience in testing.
1574 * The intent is explicitly not to provide a full aliasing or predicate
1575 * mechanism.
1576 */
1577jint
michaelm5ac8c152012-03-06 20:34:38 +00001578ReadKnownVMs(const char *jvmCfgName, jboolean speculative)
duke6e45e102007-12-01 00:00:00 +00001579{
1580 FILE *jvmCfg;
duke6e45e102007-12-01 00:00:00 +00001581 char line[MAXPATHLEN+20];
1582 int cnt = 0;
1583 int lineno = 0;
1584 jlong start, end;
1585 int vmType;
1586 char *tmpPtr;
1587 char *altVMName = NULL;
1588 char *serverClassVMName = NULL;
1589 static char *whiteSpace = " \t";
1590 if (JLI_IsTraceLauncher()) {
1591 start = CounterGet();
1592 }
duke6e45e102007-12-01 00:00:00 +00001593
1594 jvmCfg = fopen(jvmCfgName, "r");
1595 if (jvmCfg == NULL) {
1596 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -07001597 JLI_ReportErrorMessage(CFG_ERROR6, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001598 exit(1);
1599 } else {
1600 return -1;
1601 }
1602 }
1603 while (fgets(line, sizeof(line), jvmCfg) != NULL) {
1604 vmType = VM_UNKNOWN;
1605 lineno++;
1606 if (line[0] == '#')
1607 continue;
1608 if (line[0] != '-') {
ksrini0e817162008-08-26 10:21:20 -07001609 JLI_ReportErrorMessage(CFG_WARN2, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001610 }
1611 if (cnt >= knownVMsLimit) {
1612 GrowKnownVMs(cnt);
1613 }
1614 line[JLI_StrLen(line)-1] = '\0'; /* remove trailing newline */
1615 tmpPtr = line + JLI_StrCSpn(line, whiteSpace);
1616 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001617 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001618 } else {
1619 /* Null-terminate this string for JLI_StringDup below */
1620 *tmpPtr++ = 0;
1621 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1622 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001623 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001624 } else {
1625 if (!JLI_StrCCmp(tmpPtr, "KNOWN")) {
1626 vmType = VM_KNOWN;
1627 } else if (!JLI_StrCCmp(tmpPtr, "ALIASED_TO")) {
1628 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1629 if (*tmpPtr != 0) {
1630 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1631 }
1632 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001633 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001634 } else {
1635 /* Null terminate altVMName */
1636 altVMName = tmpPtr;
1637 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1638 *tmpPtr = 0;
1639 vmType = VM_ALIASED_TO;
1640 }
1641 } else if (!JLI_StrCCmp(tmpPtr, "WARN")) {
1642 vmType = VM_WARN;
1643 } else if (!JLI_StrCCmp(tmpPtr, "IGNORE")) {
1644 vmType = VM_IGNORE;
1645 } else if (!JLI_StrCCmp(tmpPtr, "ERROR")) {
1646 vmType = VM_ERROR;
1647 } else if (!JLI_StrCCmp(tmpPtr, "IF_SERVER_CLASS")) {
1648 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1649 if (*tmpPtr != 0) {
1650 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1651 }
1652 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001653 JLI_ReportErrorMessage(CFG_WARN4, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001654 } else {
1655 /* Null terminate server class VM name */
1656 serverClassVMName = tmpPtr;
1657 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1658 *tmpPtr = 0;
1659 vmType = VM_IF_SERVER_CLASS;
1660 }
1661 } else {
ksrini0e817162008-08-26 10:21:20 -07001662 JLI_ReportErrorMessage(CFG_WARN5, lineno, &jvmCfgName[0]);
duke6e45e102007-12-01 00:00:00 +00001663 vmType = VM_KNOWN;
1664 }
1665 }
1666 }
1667
1668 JLI_TraceLauncher("jvm.cfg[%d] = ->%s<-\n", cnt, line);
1669 if (vmType != VM_UNKNOWN) {
1670 knownVMs[cnt].name = JLI_StringDup(line);
1671 knownVMs[cnt].flag = vmType;
1672 switch (vmType) {
1673 default:
1674 break;
1675 case VM_ALIASED_TO:
1676 knownVMs[cnt].alias = JLI_StringDup(altVMName);
1677 JLI_TraceLauncher(" name: %s vmType: %s alias: %s\n",
1678 knownVMs[cnt].name, "VM_ALIASED_TO", knownVMs[cnt].alias);
1679 break;
1680 case VM_IF_SERVER_CLASS:
1681 knownVMs[cnt].server_class = JLI_StringDup(serverClassVMName);
1682 JLI_TraceLauncher(" name: %s vmType: %s server_class: %s\n",
1683 knownVMs[cnt].name, "VM_IF_SERVER_CLASS", knownVMs[cnt].server_class);
1684 break;
1685 }
1686 cnt++;
1687 }
1688 }
1689 fclose(jvmCfg);
1690 knownVMsCount = cnt;
1691
1692 if (JLI_IsTraceLauncher()) {
1693 end = CounterGet();
1694 printf("%ld micro seconds to parse jvm.cfg\n",
1695 (long)(jint)Counter2Micros(end-start));
1696 }
1697
1698 return cnt;
1699}
1700
1701
1702static void
1703GrowKnownVMs(int minimum)
1704{
1705 struct vmdesc* newKnownVMs;
1706 int newMax;
1707
1708 newMax = (knownVMsLimit == 0 ? INIT_MAX_KNOWN_VMS : (2 * knownVMsLimit));
1709 if (newMax <= minimum) {
1710 newMax = minimum;
1711 }
1712 newKnownVMs = (struct vmdesc*) JLI_MemAlloc(newMax * sizeof(struct vmdesc));
1713 if (knownVMs != NULL) {
1714 memcpy(newKnownVMs, knownVMs, knownVMsLimit * sizeof(struct vmdesc));
1715 }
1716 JLI_MemFree(knownVMs);
1717 knownVMs = newKnownVMs;
1718 knownVMsLimit = newMax;
1719}
1720
1721
1722/* Returns index of VM or -1 if not found */
1723static int
1724KnownVMIndex(const char* name)
1725{
1726 int i;
1727 if (JLI_StrCCmp(name, "-J") == 0) name += 2;
1728 for (i = 0; i < knownVMsCount; i++) {
1729 if (!JLI_StrCmp(name, knownVMs[i].name)) {
1730 return i;
1731 }
1732 }
1733 return -1;
1734}
1735
1736static void
1737FreeKnownVMs()
1738{
1739 int i;
1740 for (i = 0; i < knownVMsCount; i++) {
1741 JLI_MemFree(knownVMs[i].name);
1742 knownVMs[i].name = NULL;
1743 }
1744 JLI_MemFree(knownVMs);
1745}
1746
duke6e45e102007-12-01 00:00:00 +00001747/*
1748 * Displays the splash screen according to the jar file name
1749 * and image file names stored in environment variables
1750 */
michaelm5ac8c152012-03-06 20:34:38 +00001751void
duke6e45e102007-12-01 00:00:00 +00001752ShowSplashScreen()
1753{
1754 const char *jar_name = getenv(SPLASH_JAR_ENV_ENTRY);
1755 const char *file_name = getenv(SPLASH_FILE_ENV_ENTRY);
1756 int data_size;
1757 void *image_data;
1758 if (jar_name) {
1759 image_data = JLI_JarUnpackFile(jar_name, file_name, &data_size);
1760 if (image_data) {
1761 DoSplashInit();
1762 DoSplashLoadMemory(image_data, data_size);
1763 JLI_MemFree(image_data);
1764 }
1765 } else if (file_name) {
1766 DoSplashInit();
1767 DoSplashLoadFile(file_name);
1768 } else {
1769 return;
1770 }
1771 DoSplashSetFileJarName(file_name, jar_name);
1772
1773 /*
1774 * Done with all command line processing and potential re-execs so
1775 * clean up the environment.
1776 */
1777 (void)UnsetEnv(ENV_ENTRY);
1778 (void)UnsetEnv(SPLASH_FILE_ENV_ENTRY);
1779 (void)UnsetEnv(SPLASH_JAR_ENV_ENTRY);
1780
1781 JLI_MemFree(splash_jar_entry);
1782 JLI_MemFree(splash_file_entry);
1783
1784}
1785
1786const char*
1787GetDotVersion()
1788{
1789 return _dVersion;
1790}
1791
1792const char*
1793GetFullVersion()
1794{
1795 return _fVersion;
1796}
1797
1798const char*
1799GetProgramName()
1800{
1801 return _program_name;
1802}
1803
1804const char*
1805GetLauncherName()
1806{
1807 return _launcher_name;
1808}
1809
1810jint
1811GetErgoPolicy()
1812{
1813 return _ergo_policy;
1814}
1815
1816jboolean
1817IsJavaArgs()
1818{
1819 return _is_java_args;
1820}
1821
1822static jboolean
1823IsWildCardEnabled()
1824{
1825 return _wc_enabled;
1826}
1827
michaelm5ac8c152012-03-06 20:34:38 +00001828int
1829ContinueInNewThread(InvocationFunctions* ifn, jlong threadStackSize,
1830 int argc, char **argv,
mchungea290e22011-01-21 09:43:57 -08001831 int mode, char *what, int ret)
duke6e45e102007-12-01 00:00:00 +00001832{
1833
1834 /*
1835 * If user doesn't specify stack size, check if VM has a preference.
1836 * Note that HotSpot no longer supports JNI_VERSION_1_1 but it will
1837 * return its default stack size through the init args structure.
1838 */
1839 if (threadStackSize == 0) {
1840 struct JDK1_1InitArgs args1_1;
1841 memset((void*)&args1_1, 0, sizeof(args1_1));
1842 args1_1.version = JNI_VERSION_1_1;
1843 ifn->GetDefaultJavaVMInitArgs(&args1_1); /* ignore return value */
1844 if (args1_1.javaStackSize > 0) {
1845 threadStackSize = args1_1.javaStackSize;
1846 }
1847 }
1848
1849 { /* Create a new thread to create JVM and invoke main method */
1850 JavaMainArgs args;
1851 int rslt;
1852
1853 args.argc = argc;
1854 args.argv = argv;
mchungea290e22011-01-21 09:43:57 -08001855 args.mode = mode;
1856 args.what = what;
duke6e45e102007-12-01 00:00:00 +00001857 args.ifn = *ifn;
1858
1859 rslt = ContinueInNewThread0(JavaMain, threadStackSize, (void*)&args);
1860 /* If the caller has deemed there is an error we
1861 * simply return that, otherwise we return the value of
1862 * the callee
1863 */
1864 return (ret != 0) ? ret : rslt;
1865 }
1866}
1867
1868static void
1869DumpState()
1870{
1871 if (!JLI_IsTraceLauncher()) return ;
1872 printf("Launcher state:\n");
1873 printf("\tdebug:%s\n", (JLI_IsTraceLauncher() == JNI_TRUE) ? "on" : "off");
1874 printf("\tjavargs:%s\n", (_is_java_args == JNI_TRUE) ? "on" : "off");
1875 printf("\tprogram name:%s\n", GetProgramName());
1876 printf("\tlauncher name:%s\n", GetLauncherName());
1877 printf("\tjavaw:%s\n", (IsJavaw() == JNI_TRUE) ? "on" : "off");
1878 printf("\tfullversion:%s\n", GetFullVersion());
1879 printf("\tdotversion:%s\n", GetDotVersion());
1880 printf("\tergo_policy:");
1881 switch(GetErgoPolicy()) {
1882 case NEVER_SERVER_CLASS:
1883 printf("NEVER_ACT_AS_A_SERVER_CLASS_MACHINE\n");
1884 break;
1885 case ALWAYS_SERVER_CLASS:
1886 printf("ALWAYS_ACT_AS_A_SERVER_CLASS_MACHINE\n");
1887 break;
1888 default:
1889 printf("DEFAULT_ERGONOMICS_POLICY\n");
1890 }
1891}
1892
1893/*
1894 * Return JNI_TRUE for an option string that has no effect but should
1895 * _not_ be passed on to the vm; return JNI_FALSE otherwise. On
1896 * Solaris SPARC, this screening needs to be done if:
ksrini11e7f1b2009-11-20 11:01:32 -08001897 * -d32 or -d64 is passed to a binary with an unmatched data model
1898 * (the exec in CreateExecutionEnvironment removes -d<n> options and points the
1899 * exec to the proper binary). In the case of when the data model and the
1900 * requested version is matched, an exec would not occur, and these options
1901 * were erroneously passed to the vm.
duke6e45e102007-12-01 00:00:00 +00001902 */
1903jboolean
1904RemovableOption(char * option)
1905{
1906 /*
1907 * Unconditionally remove both -d32 and -d64 options since only
1908 * the last such options has an effect; e.g.
1909 * java -d32 -d64 -d32 -version
1910 * is equivalent to
1911 * java -d32 -version
1912 */
1913
1914 if( (JLI_StrCCmp(option, "-d32") == 0 ) ||
1915 (JLI_StrCCmp(option, "-d64") == 0 ) )
1916 return JNI_TRUE;
1917 else
1918 return JNI_FALSE;
1919}
1920
1921/*
1922 * A utility procedure to always print to stderr
1923 */
1924void
ksrini0e817162008-08-26 10:21:20 -07001925JLI_ReportMessage(const char* fmt, ...)
duke6e45e102007-12-01 00:00:00 +00001926{
1927 va_list vl;
1928 va_start(vl, fmt);
1929 vfprintf(stderr, fmt, vl);
1930 fprintf(stderr, "\n");
1931 va_end(vl);
1932}