blob: 2d88dc9f751615934c166054870725860bf154c2 [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);
107static jobjectArray NewPlatformStringArray(JNIEnv *env, char **strv, int strc);
mchungea290e22011-01-21 09:43:57 -0800108static jclass LoadMainClass(JNIEnv *env, int mode, char *name);
duke6e45e102007-12-01 00:00:00 +0000109
110static void TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv);
111static jboolean AddApplicationOptions(int cpathc, const char **cpathv);
112static void SetApplicationClassPath(const char**);
113
114static void PrintJavaVersion(JNIEnv *env, jboolean extraLF);
115static void PrintUsage(JNIEnv* env, jboolean doXUsage);
ksrini8e20e1c2010-11-23 16:52:39 -0800116static void ShowSettings(JNIEnv* env, char *optString);
duke6e45e102007-12-01 00:00:00 +0000117
118static void SetPaths(int argc, char **argv);
119
120static void DumpState();
121static jboolean RemovableOption(char *option);
122
123/* Maximum supported entries from jvm.cfg. */
124#define INIT_MAX_KNOWN_VMS 10
125
126/* Values for vmdesc.flag */
127enum vmdesc_flag {
128 VM_UNKNOWN = -1,
129 VM_KNOWN,
130 VM_ALIASED_TO,
131 VM_WARN,
132 VM_ERROR,
133 VM_IF_SERVER_CLASS,
134 VM_IGNORE
135};
136
137struct vmdesc {
138 char *name;
139 int flag;
140 char *alias;
141 char *server_class;
142};
143static struct vmdesc *knownVMs = NULL;
144static int knownVMsCount = 0;
145static int knownVMsLimit = 0;
146
147static void GrowKnownVMs();
148static int KnownVMIndex(const char* name);
149static void FreeKnownVMs();
150static void ShowSplashScreen();
151static jboolean IsWildCardEnabled();
152
153#define ARG_CHECK(n, f, a) if (n < 1) { \
ksrini0e817162008-08-26 10:21:20 -0700154 JLI_ReportErrorMessage(f, a); \
duke6e45e102007-12-01 00:00:00 +0000155 printUsage = JNI_TRUE; \
156 *pret = 1; \
157 return JNI_TRUE; \
158}
159
160/*
161 * Running Java code in primordial thread caused many problems. We will
162 * create a new thread to invoke JVM. See 6316197 for more information.
163 */
mchungea290e22011-01-21 09:43:57 -0800164static jlong threadStackSize = 0; /* stack size of the new thread */
ksrini6d575f82010-12-23 13:51:30 -0800165static jlong maxHeapSize = 0; /* max heap size */
166static jlong initialHeapSize = 0; /* inital heap size */
duke6e45e102007-12-01 00:00:00 +0000167
168int JNICALL JavaMain(void * args); /* entry point */
169
mchungea290e22011-01-21 09:43:57 -0800170enum LaunchMode { // cf. sun.launcher.LauncherHelper
171 LM_UNKNOWN = 0,
172 LM_CLASS,
173 LM_JAR
174};
175
176static const char *launchModeNames[]
177 = { "Unknown", "Main class", "JAR file" };
178
duke6e45e102007-12-01 00:00:00 +0000179typedef struct {
mchungea290e22011-01-21 09:43:57 -0800180 int argc;
181 char **argv;
182 int mode;
183 char *what;
184 InvocationFunctions ifn;
duke6e45e102007-12-01 00:00:00 +0000185} JavaMainArgs;
186
187/*
188 * Entry point.
189 */
190int
191JLI_Launch(int argc, char ** argv, /* main argc, argc */
192 int jargc, const char** jargv, /* java args */
193 int appclassc, const char** appclassv, /* app classpath */
194 const char* fullversion, /* full version defined */
195 const char* dotversion, /* dot version defined */
196 const char* pname, /* program name */
197 const char* lname, /* launcher name */
198 jboolean javaargs, /* JAVA_ARGS */
199 jboolean cpwildcard, /* classpath wildcard*/
200 jboolean javaw, /* windows-only javaw */
201 jint ergo /* ergonomics class policy */
202)
203{
mchungea290e22011-01-21 09:43:57 -0800204 int mode = LM_UNKNOWN;
205 char *what = NULL;
duke6e45e102007-12-01 00:00:00 +0000206 char *cpath = 0;
207 char *main_class = NULL;
208 int ret;
209 InvocationFunctions ifn;
210 jlong start, end;
ksrinie3ec45d2010-07-09 11:04:34 -0700211 char jvmpath[MAXPATHLEN];
212 char jrepath[MAXPATHLEN];
duke6e45e102007-12-01 00:00:00 +0000213
214 _fVersion = fullversion;
215 _dVersion = dotversion;
216 _launcher_name = lname;
217 _program_name = pname;
218 _is_java_args = javaargs;
219 _wc_enabled = cpwildcard;
220 _ergo_policy = ergo;
221
ksrini52cded22008-03-06 07:51:28 -0800222 InitLauncher(javaw);
duke6e45e102007-12-01 00:00:00 +0000223 DumpState();
224
225 /*
226 * Make sure the specified version of the JRE is running.
227 *
228 * There are three things to note about the SelectVersion() routine:
229 * 1) If the version running isn't correct, this routine doesn't
230 * return (either the correct version has been exec'd or an error
231 * was issued).
232 * 2) Argc and Argv in this scope are *not* altered by this routine.
233 * It is the responsibility of subsequent code to ignore the
234 * arguments handled by this routine.
235 * 3) As a side-effect, the variable "main_class" is guaranteed to
236 * be set (if it should ever be set). This isn't exactly the
237 * poster child for structured programming, but it is a small
238 * price to pay for not processing a jar file operand twice.
239 * (Note: This side effect has been disabled. See comment on
240 * bugid 5030265 below.)
241 */
242 SelectVersion(argc, argv, &main_class);
243
ksrinie3ec45d2010-07-09 11:04:34 -0700244 if (JLI_IsTraceLauncher()) {
245 int i;
246 printf("Command line args:\n");
247 for (i = 0; i < argc ; i++) {
248 printf("argv[%d] = %s\n", i, argv[i]);
249 }
ksrini9636e2e2011-02-03 15:41:23 -0800250 AddOption("-Dsun.java.launcher.diag=true", NULL);
ksrinie3ec45d2010-07-09 11:04:34 -0700251 }
duke6e45e102007-12-01 00:00:00 +0000252
253 CreateExecutionEnvironment(&argc, &argv,
254 jrepath, sizeof(jrepath),
ksrinie3ec45d2010-07-09 11:04:34 -0700255 jvmpath, sizeof(jvmpath));
duke6e45e102007-12-01 00:00:00 +0000256
257 ifn.CreateJavaVM = 0;
258 ifn.GetDefaultJavaVMInitArgs = 0;
259
260 if (JLI_IsTraceLauncher()) {
261 start = CounterGet();
262 }
263
264 if (!LoadJavaVM(jvmpath, &ifn)) {
265 return(6);
266 }
267
268 if (JLI_IsTraceLauncher()) {
269 end = CounterGet();
270 }
271
272 JLI_TraceLauncher("%ld micro seconds to LoadJavaVM\n",
273 (long)(jint)Counter2Micros(end-start));
274
275 ++argv;
276 --argc;
277
278 if (IsJavaArgs()) {
279 /* Preprocess wrapper arguments */
280 TranslateApplicationArgs(jargc, jargv, &argc, &argv);
281 if (!AddApplicationOptions(appclassc, appclassv)) {
282 return(1);
283 }
284 } else {
285 /* Set default CLASSPATH */
286 cpath = getenv("CLASSPATH");
287 if (cpath == NULL) {
288 cpath = ".";
289 }
290 SetClassPath(cpath);
291 }
292
mchungea290e22011-01-21 09:43:57 -0800293 /* Parse command line options; if the return value of
294 * ParseArguments is false, the program should exit.
duke6e45e102007-12-01 00:00:00 +0000295 */
mchungea290e22011-01-21 09:43:57 -0800296 if (!ParseArguments(&argc, &argv, &mode, &what, &ret, jrepath))
297 {
duke6e45e102007-12-01 00:00:00 +0000298 return(ret);
299 }
300
301 /* Override class path if -jar flag was specified */
mchungea290e22011-01-21 09:43:57 -0800302 if (mode == LM_JAR) {
303 SetClassPath(what); /* Override class path */
duke6e45e102007-12-01 00:00:00 +0000304 }
305
306 /* set the -Dsun.java.command pseudo property */
mchungea290e22011-01-21 09:43:57 -0800307 SetJavaCommandLineProp(what, argc, argv);
duke6e45e102007-12-01 00:00:00 +0000308
309 /* Set the -Dsun.java.launcher pseudo property */
310 SetJavaLauncherProp();
311
312 /* set the -Dsun.java.launcher.* platform properties */
313 SetJavaLauncherPlatformProps();
314
315 /* Show the splash screen if needed */
316 ShowSplashScreen();
317
mchungea290e22011-01-21 09:43:57 -0800318 return ContinueInNewThread(&ifn, argc, argv, mode, what, ret);
duke6e45e102007-12-01 00:00:00 +0000319
320}
ksrinie3ec45d2010-07-09 11:04:34 -0700321/*
322 * Always detach the main thread so that it appears to have ended when
323 * the application's main method exits. This will invoke the
324 * uncaught exception handler machinery if main threw an
325 * exception. An uncaught exception handler cannot change the
326 * launcher's return code except by calling System.exit.
327 *
328 * Wait for all non-daemon threads to end, then destroy the VM.
329 * This will actually create a trivial new Java waiter thread
330 * named "DestroyJavaVM", but this will be seen as a different
331 * thread from the one that executed main, even though they are
332 * the same C thread. This allows mainThread.join() and
333 * mainThread.isAlive() to work as expected.
334 */
335#define LEAVE() \
336 if ((*vm)->DetachCurrentThread(vm) != 0) { \
337 JLI_ReportErrorMessage(JVM_ERROR2); \
338 ret = 1; \
339 } \
340 (*vm)->DestroyJavaVM(vm); \
341 return ret \
duke6e45e102007-12-01 00:00:00 +0000342
ksrini20a64b22008-09-24 15:07:41 -0700343#define CHECK_EXCEPTION_NULL_LEAVE(e) \
344 if ((*env)->ExceptionOccurred(env)) { \
345 JLI_ReportExceptionDescription(env); \
ksrinie3ec45d2010-07-09 11:04:34 -0700346 LEAVE(); \
ksrini20a64b22008-09-24 15:07:41 -0700347 } \
348 if ((e) == NULL) { \
349 JLI_ReportErrorMessage(JNI_ERROR); \
ksrinie3ec45d2010-07-09 11:04:34 -0700350 LEAVE(); \
ksrini20a64b22008-09-24 15:07:41 -0700351 }
352
353#define CHECK_EXCEPTION_LEAVE(rv) \
354 if ((*env)->ExceptionOccurred(env)) { \
355 JLI_ReportExceptionDescription(env); \
356 ret = (rv); \
ksrinie3ec45d2010-07-09 11:04:34 -0700357 LEAVE(); \
ksrini20a64b22008-09-24 15:07:41 -0700358 }
duke6e45e102007-12-01 00:00:00 +0000359
360int JNICALL
361JavaMain(void * _args)
362{
363 JavaMainArgs *args = (JavaMainArgs *)_args;
364 int argc = args->argc;
365 char **argv = args->argv;
mchungea290e22011-01-21 09:43:57 -0800366 int mode = args->mode;
367 char *what = args->what;
duke6e45e102007-12-01 00:00:00 +0000368 InvocationFunctions ifn = args->ifn;
369
370 JavaVM *vm = 0;
371 JNIEnv *env = 0;
mchungea290e22011-01-21 09:43:57 -0800372 jclass mainClass = NULL;
duke6e45e102007-12-01 00:00:00 +0000373 jmethodID mainID;
374 jobjectArray mainArgs;
375 int ret = 0;
376 jlong start, end;
377
duke6e45e102007-12-01 00:00:00 +0000378 /* Initialize the virtual machine */
duke6e45e102007-12-01 00:00:00 +0000379 start = CounterGet();
380 if (!InitializeJVM(&vm, &env, &ifn)) {
ksrini0e817162008-08-26 10:21:20 -0700381 JLI_ReportErrorMessage(JVM_ERROR1);
duke6e45e102007-12-01 00:00:00 +0000382 exit(1);
383 }
384
ksrini05fdd5a2012-01-03 08:27:37 -0800385 if (showSettings != NULL) {
386 ShowSettings(env, showSettings);
387 CHECK_EXCEPTION_LEAVE(1);
388 }
389
duke6e45e102007-12-01 00:00:00 +0000390 if (printVersion || showVersion) {
391 PrintJavaVersion(env, showVersion);
ksrini20a64b22008-09-24 15:07:41 -0700392 CHECK_EXCEPTION_LEAVE(0);
duke6e45e102007-12-01 00:00:00 +0000393 if (printVersion) {
ksrinie3ec45d2010-07-09 11:04:34 -0700394 LEAVE();
duke6e45e102007-12-01 00:00:00 +0000395 }
396 }
397
398 /* If the user specified neither a class name nor a JAR file */
mchungea290e22011-01-21 09:43:57 -0800399 if (printXUsage || printUsage || what == 0 || mode == LM_UNKNOWN) {
duke6e45e102007-12-01 00:00:00 +0000400 PrintUsage(env, printXUsage);
ksrini20a64b22008-09-24 15:07:41 -0700401 CHECK_EXCEPTION_LEAVE(1);
ksrinie3ec45d2010-07-09 11:04:34 -0700402 LEAVE();
duke6e45e102007-12-01 00:00:00 +0000403 }
404
405 FreeKnownVMs(); /* after last possible PrintUsage() */
406
407 if (JLI_IsTraceLauncher()) {
408 end = CounterGet();
409 JLI_TraceLauncher("%ld micro seconds to InitializeJVM\n",
410 (long)(jint)Counter2Micros(end-start));
411 }
412
mchungea290e22011-01-21 09:43:57 -0800413 /* At this stage, argc/argv have the application's arguments */
duke6e45e102007-12-01 00:00:00 +0000414 if (JLI_IsTraceLauncher()){
415 int i;
mchungea290e22011-01-21 09:43:57 -0800416 printf("%s is '%s'\n", launchModeNames[mode], what);
417 printf("App's argc is %d\n", argc);
duke6e45e102007-12-01 00:00:00 +0000418 for (i=0; i < argc; i++) {
419 printf(" argv[%2d] = '%s'\n", i, argv[i]);
420 }
421 }
422
423 ret = 1;
424
425 /*
426 * Get the application's main class.
427 *
428 * See bugid 5030265. The Main-Class name has already been parsed
429 * from the manifest, but not parsed properly for UTF-8 support.
430 * Hence the code here ignores the value previously extracted and
431 * uses the pre-existing code to reextract the value. This is
432 * possibly an end of release cycle expedient. However, it has
433 * also been discovered that passing some character sets through
434 * the environment has "strange" behavior on some variants of
435 * Windows. Hence, maybe the manifest parsing code local to the
436 * launcher should never be enhanced.
437 *
438 * Hence, future work should either:
439 * 1) Correct the local parsing code and verify that the
440 * Main-Class attribute gets properly passed through
441 * all environments,
442 * 2) Remove the vestages of maintaining main_class through
443 * the environment (and remove these comments).
444 */
mchungea290e22011-01-21 09:43:57 -0800445 mainClass = LoadMainClass(env, mode, what);
ksrini20a64b22008-09-24 15:07:41 -0700446 CHECK_EXCEPTION_NULL_LEAVE(mainClass);
duke6e45e102007-12-01 00:00:00 +0000447
ksrini20a64b22008-09-24 15:07:41 -0700448 /*
449 * The LoadMainClass not only loads the main class, it will also ensure
450 * that the main method's signature is correct, therefore further checking
451 * is not required. The main method is invoked here so that extraneous java
452 * stacks are not in the application stack trace.
453 */
duke6e45e102007-12-01 00:00:00 +0000454 mainID = (*env)->GetStaticMethodID(env, mainClass, "main",
455 "([Ljava/lang/String;)V");
ksrini20a64b22008-09-24 15:07:41 -0700456 CHECK_EXCEPTION_NULL_LEAVE(mainID);
duke6e45e102007-12-01 00:00:00 +0000457
458 /* Build argument array */
459 mainArgs = NewPlatformStringArray(env, argv, argc);
ksrini20a64b22008-09-24 15:07:41 -0700460 CHECK_EXCEPTION_NULL_LEAVE(mainArgs);
duke6e45e102007-12-01 00:00:00 +0000461
462 /* Invoke main method. */
463 (*env)->CallStaticVoidMethod(env, mainClass, mainID, mainArgs);
464
465 /*
466 * The launcher's exit code (in the absence of calls to
467 * System.exit) will be non-zero if main threw an exception.
468 */
469 ret = (*env)->ExceptionOccurred(env) == NULL ? 0 : 1;
ksrinie3ec45d2010-07-09 11:04:34 -0700470 LEAVE();
duke6e45e102007-12-01 00:00:00 +0000471}
472
duke6e45e102007-12-01 00:00:00 +0000473/*
474 * Checks the command line options to find which JVM type was
475 * specified. If no command line option was given for the JVM type,
476 * the default type is used. The environment variable
477 * JDK_ALTERNATE_VM and the command line option -XXaltjvm= are also
478 * checked as ways of specifying which JVM type to invoke.
479 */
480char *
481CheckJvmType(int *pargc, char ***argv, jboolean speculative) {
482 int i, argi;
483 int argc;
484 char **newArgv;
485 int newArgvIdx = 0;
486 int isVMType;
487 int jvmidx = -1;
488 char *jvmtype = getenv("JDK_ALTERNATE_VM");
489
490 argc = *pargc;
491
492 /* To make things simpler we always copy the argv array */
493 newArgv = JLI_MemAlloc((argc + 1) * sizeof(char *));
494
495 /* The program name is always present */
496 newArgv[newArgvIdx++] = (*argv)[0];
497
498 for (argi = 1; argi < argc; argi++) {
499 char *arg = (*argv)[argi];
500 isVMType = 0;
501
502 if (IsJavaArgs()) {
503 if (arg[0] != '-') {
504 newArgv[newArgvIdx++] = arg;
505 continue;
506 }
507 } else {
508 if (JLI_StrCmp(arg, "-classpath") == 0 ||
509 JLI_StrCmp(arg, "-cp") == 0) {
510 newArgv[newArgvIdx++] = arg;
511 argi++;
512 if (argi < argc) {
513 newArgv[newArgvIdx++] = (*argv)[argi];
514 }
515 continue;
516 }
517 if (arg[0] != '-') break;
518 }
519
520 /* Did the user pass an explicit VM type? */
521 i = KnownVMIndex(arg);
522 if (i >= 0) {
523 jvmtype = knownVMs[jvmidx = i].name + 1; /* skip the - */
524 isVMType = 1;
525 *pargc = *pargc - 1;
526 }
527
528 /* Did the user specify an "alternate" VM? */
529 else if (JLI_StrCCmp(arg, "-XXaltjvm=") == 0 || JLI_StrCCmp(arg, "-J-XXaltjvm=") == 0) {
530 isVMType = 1;
531 jvmtype = arg+((arg[1]=='X')? 10 : 12);
532 jvmidx = -1;
533 }
534
535 if (!isVMType) {
536 newArgv[newArgvIdx++] = arg;
537 }
538 }
539
540 /*
541 * Finish copying the arguments if we aborted the above loop.
542 * NOTE that if we aborted via "break" then we did NOT copy the
543 * last argument above, and in addition argi will be less than
544 * argc.
545 */
546 while (argi < argc) {
547 newArgv[newArgvIdx++] = (*argv)[argi];
548 argi++;
549 }
550
551 /* argv is null-terminated */
552 newArgv[newArgvIdx] = 0;
553
554 /* Copy back argv */
555 *argv = newArgv;
556 *pargc = newArgvIdx;
557
558 /* use the default VM type if not specified (no alias processing) */
559 if (jvmtype == NULL) {
560 char* result = knownVMs[0].name+1;
561 /* Use a different VM type if we are on a server class machine? */
562 if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) &&
563 (ServerClassMachine() == JNI_TRUE)) {
564 result = knownVMs[0].server_class+1;
565 }
566 JLI_TraceLauncher("Default VM: %s\n", result);
567 return result;
568 }
569
570 /* if using an alternate VM, no alias processing */
571 if (jvmidx < 0)
572 return jvmtype;
573
574 /* Resolve aliases first */
575 {
576 int loopCount = 0;
577 while (knownVMs[jvmidx].flag == VM_ALIASED_TO) {
578 int nextIdx = KnownVMIndex(knownVMs[jvmidx].alias);
579
580 if (loopCount > knownVMsCount) {
581 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700582 JLI_ReportErrorMessage(CFG_ERROR1);
duke6e45e102007-12-01 00:00:00 +0000583 exit(1);
584 } else {
585 return "ERROR";
586 /* break; */
587 }
588 }
589
590 if (nextIdx < 0) {
591 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700592 JLI_ReportErrorMessage(CFG_ERROR2, knownVMs[jvmidx].alias);
duke6e45e102007-12-01 00:00:00 +0000593 exit(1);
594 } else {
595 return "ERROR";
596 }
597 }
598 jvmidx = nextIdx;
599 jvmtype = knownVMs[jvmidx].name+1;
600 loopCount++;
601 }
602 }
603
604 switch (knownVMs[jvmidx].flag) {
605 case VM_WARN:
606 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700607 JLI_ReportErrorMessage(CFG_WARN1, jvmtype, knownVMs[0].name + 1);
duke6e45e102007-12-01 00:00:00 +0000608 }
609 /* fall through */
610 case VM_IGNORE:
611 jvmtype = knownVMs[jvmidx=0].name + 1;
612 /* fall through */
613 case VM_KNOWN:
614 break;
615 case VM_ERROR:
616 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700617 JLI_ReportErrorMessage(CFG_ERROR3, jvmtype);
duke6e45e102007-12-01 00:00:00 +0000618 exit(1);
619 } else {
620 return "ERROR";
621 }
622 }
623
624 return jvmtype;
625}
626
627/* copied from HotSpot function "atomll()" */
628static int
ksrini8e20e1c2010-11-23 16:52:39 -0800629parse_size(const char *s, jlong *result) {
duke6e45e102007-12-01 00:00:00 +0000630 jlong n = 0;
631 int args_read = sscanf(s, jlong_format_specifier(), &n);
632 if (args_read != 1) {
633 return 0;
634 }
635 while (*s != '\0' && *s >= '0' && *s <= '9') {
636 s++;
637 }
638 // 4705540: illegal if more characters are found after the first non-digit
639 if (JLI_StrLen(s) > 1) {
640 return 0;
641 }
642 switch (*s) {
643 case 'T': case 't':
644 *result = n * GB * KB;
645 return 1;
646 case 'G': case 'g':
647 *result = n * GB;
648 return 1;
649 case 'M': case 'm':
650 *result = n * MB;
651 return 1;
652 case 'K': case 'k':
653 *result = n * KB;
654 return 1;
655 case '\0':
656 *result = n;
657 return 1;
658 default:
659 /* Create JVM with default stack and let VM handle malformed -Xss string*/
660 return 0;
661 }
662}
663
664/*
665 * Adds a new VM option with the given given name and value.
666 */
667void
668AddOption(char *str, void *info)
669{
670 /*
671 * Expand options array if needed to accommodate at least one more
672 * VM option.
673 */
674 if (numOptions >= maxOptions) {
675 if (options == 0) {
676 maxOptions = 4;
677 options = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
678 } else {
679 JavaVMOption *tmp;
680 maxOptions *= 2;
681 tmp = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
682 memcpy(tmp, options, numOptions * sizeof(JavaVMOption));
683 JLI_MemFree(options);
684 options = tmp;
685 }
686 }
687 options[numOptions].optionString = str;
688 options[numOptions++].extraInfo = info;
689
690 if (JLI_StrCCmp(str, "-Xss") == 0) {
ksrini8e20e1c2010-11-23 16:52:39 -0800691 jlong tmp;
692 if (parse_size(str + 4, &tmp)) {
693 threadStackSize = tmp;
694 }
695 }
696
697 if (JLI_StrCCmp(str, "-Xmx") == 0) {
698 jlong tmp;
699 if (parse_size(str + 4, &tmp)) {
ksrini6d575f82010-12-23 13:51:30 -0800700 maxHeapSize = tmp;
701 }
702 }
703
704 if (JLI_StrCCmp(str, "-Xms") == 0) {
705 jlong tmp;
706 if (parse_size(str + 4, &tmp)) {
mchungea290e22011-01-21 09:43:57 -0800707 initialHeapSize = tmp;
ksrini8e20e1c2010-11-23 16:52:39 -0800708 }
duke6e45e102007-12-01 00:00:00 +0000709 }
710}
711
712static void
713SetClassPath(const char *s)
714{
715 char *def;
martinc0ca3352009-06-22 16:41:27 -0700716 const char *orig = s;
717 static const char format[] = "-Djava.class.path=%s";
duke6e45e102007-12-01 00:00:00 +0000718 s = JLI_WildcardExpandClasspath(s);
martinc0ca3352009-06-22 16:41:27 -0700719 def = JLI_MemAlloc(sizeof(format)
720 - 2 /* strlen("%s") */
721 + JLI_StrLen(s));
722 sprintf(def, format, s);
duke6e45e102007-12-01 00:00:00 +0000723 AddOption(def, NULL);
martinc0ca3352009-06-22 16:41:27 -0700724 if (s != orig)
725 JLI_MemFree((char *) s);
duke6e45e102007-12-01 00:00:00 +0000726}
727
728/*
729 * The SelectVersion() routine ensures that an appropriate version of
730 * the JRE is running. The specification for the appropriate version
731 * is obtained from either the manifest of a jar file (preferred) or
732 * from command line options.
733 * The routine also parses splash screen command line options and
734 * passes on their values in private environment variables.
735 */
736static void
737SelectVersion(int argc, char **argv, char **main_class)
738{
739 char *arg;
740 char **new_argv;
741 char **new_argp;
742 char *operand;
743 char *version = NULL;
744 char *jre = NULL;
745 int jarflag = 0;
746 int headlessflag = 0;
747 int restrict_search = -1; /* -1 implies not known */
748 manifest_info info;
749 char env_entry[MAXNAMELEN + 24] = ENV_ENTRY "=";
750 char *splash_file_name = NULL;
751 char *splash_jar_name = NULL;
752 char *env_in;
753 int res;
754
755 /*
756 * If the version has already been selected, set *main_class
757 * with the value passed through the environment (if any) and
758 * simply return.
759 */
760 if ((env_in = getenv(ENV_ENTRY)) != NULL) {
761 if (*env_in != '\0')
762 *main_class = JLI_StringDup(env_in);
763 return;
764 }
765
766 /*
767 * Scan through the arguments for options relevant to multiple JRE
768 * support. For reference, the command line syntax is defined as:
769 *
770 * SYNOPSIS
771 * java [options] class [argument...]
772 *
773 * java [options] -jar file.jar [argument...]
774 *
775 * As the scan is performed, make a copy of the argument list with
776 * the version specification options (new to 1.5) removed, so that
777 * a version less than 1.5 can be exec'd.
778 *
779 * Note that due to the syntax of the native Windows interface
780 * CreateProcess(), processing similar to the following exists in
781 * the Windows platform specific routine ExecJRE (in java_md.c).
782 * Changes here should be reproduced there.
783 */
784 new_argv = JLI_MemAlloc((argc + 1) * sizeof(char*));
785 new_argv[0] = argv[0];
786 new_argp = &new_argv[1];
787 argc--;
788 argv++;
789 while ((arg = *argv) != 0 && *arg == '-') {
790 if (JLI_StrCCmp(arg, "-version:") == 0) {
791 version = arg + 9;
792 } else if (JLI_StrCmp(arg, "-jre-restrict-search") == 0) {
793 restrict_search = 1;
794 } else if (JLI_StrCmp(arg, "-no-jre-restrict-search") == 0) {
795 restrict_search = 0;
796 } else {
797 if (JLI_StrCmp(arg, "-jar") == 0)
798 jarflag = 1;
799 /* deal with "unfortunate" classpath syntax */
800 if ((JLI_StrCmp(arg, "-classpath") == 0 || JLI_StrCmp(arg, "-cp") == 0) &&
801 (argc >= 2)) {
802 *new_argp++ = arg;
803 argc--;
804 argv++;
805 arg = *argv;
806 }
807
808 /*
809 * Checking for headless toolkit option in the some way as AWT does:
810 * "true" means true and any other value means false
811 */
812 if (JLI_StrCmp(arg, "-Djava.awt.headless=true") == 0) {
813 headlessflag = 1;
814 } else if (JLI_StrCCmp(arg, "-Djava.awt.headless=") == 0) {
815 headlessflag = 0;
816 } else if (JLI_StrCCmp(arg, "-splash:") == 0) {
817 splash_file_name = arg+8;
818 }
819 *new_argp++ = arg;
820 }
821 argc--;
822 argv++;
823 }
824 if (argc <= 0) { /* No operand? Possibly legit with -[full]version */
825 operand = NULL;
826 } else {
827 argc--;
828 *new_argp++ = operand = *argv++;
829 }
830 while (argc-- > 0) /* Copy over [argument...] */
831 *new_argp++ = *argv++;
832 *new_argp = NULL;
833
834 /*
835 * If there is a jar file, read the manifest. If the jarfile can't be
836 * read, the manifest can't be read from the jar file, or the manifest
837 * is corrupt, issue the appropriate error messages and exit.
838 *
839 * Even if there isn't a jar file, construct a manifest_info structure
840 * containing the command line information. It's a convenient way to carry
841 * this data around.
842 */
843 if (jarflag && operand) {
844 if ((res = JLI_ParseManifest(operand, &info)) != 0) {
845 if (res == -1)
ksrini0e817162008-08-26 10:21:20 -0700846 JLI_ReportErrorMessage(JAR_ERROR2, operand);
duke6e45e102007-12-01 00:00:00 +0000847 else
ksrini0e817162008-08-26 10:21:20 -0700848 JLI_ReportErrorMessage(JAR_ERROR3, operand);
duke6e45e102007-12-01 00:00:00 +0000849 exit(1);
850 }
851
852 /*
853 * Command line splash screen option should have precedence
854 * over the manifest, so the manifest data is used only if
855 * splash_file_name has not been initialized above during command
856 * line parsing
857 */
858 if (!headlessflag && !splash_file_name && info.splashscreen_image_file_name) {
859 splash_file_name = info.splashscreen_image_file_name;
860 splash_jar_name = operand;
861 }
862 } else {
863 info.manifest_version = NULL;
864 info.main_class = NULL;
865 info.jre_version = NULL;
866 info.jre_restrict_search = 0;
867 }
868
869 /*
870 * Passing on splash screen info in environment variables
871 */
872 if (splash_file_name && !headlessflag) {
873 char* splash_file_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_FILE_ENV_ENTRY "=")+JLI_StrLen(splash_file_name)+1);
874 JLI_StrCpy(splash_file_entry, SPLASH_FILE_ENV_ENTRY "=");
875 JLI_StrCat(splash_file_entry, splash_file_name);
876 putenv(splash_file_entry);
877 }
878 if (splash_jar_name && !headlessflag) {
879 char* splash_jar_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_JAR_ENV_ENTRY "=")+JLI_StrLen(splash_jar_name)+1);
880 JLI_StrCpy(splash_jar_entry, SPLASH_JAR_ENV_ENTRY "=");
881 JLI_StrCat(splash_jar_entry, splash_jar_name);
882 putenv(splash_jar_entry);
883 }
884
885 /*
886 * The JRE-Version and JRE-Restrict-Search values (if any) from the
887 * manifest are overwritten by any specified on the command line.
888 */
889 if (version != NULL)
890 info.jre_version = version;
891 if (restrict_search != -1)
892 info.jre_restrict_search = restrict_search;
893
894 /*
895 * "Valid" returns (other than unrecoverable errors) follow. Set
896 * main_class as a side-effect of this routine.
897 */
898 if (info.main_class != NULL)
899 *main_class = JLI_StringDup(info.main_class);
900
901 /*
902 * If no version selection information is found either on the command
903 * line or in the manifest, simply return.
904 */
905 if (info.jre_version == NULL) {
906 JLI_FreeManifest();
907 JLI_MemFree(new_argv);
908 return;
909 }
910
911 /*
912 * Check for correct syntax of the version specification (JSR 56).
913 */
914 if (!JLI_ValidVersionString(info.jre_version)) {
ksrini0e817162008-08-26 10:21:20 -0700915 JLI_ReportErrorMessage(SPC_ERROR1, info.jre_version);
duke6e45e102007-12-01 00:00:00 +0000916 exit(1);
917 }
918
919 /*
920 * Find the appropriate JVM on the system. Just to be as forgiving as
921 * possible, if the standard algorithms don't locate an appropriate
922 * jre, check to see if the one running will satisfy the requirements.
923 * This can happen on systems which haven't been set-up for multiple
924 * JRE support.
925 */
926 jre = LocateJRE(&info);
927 JLI_TraceLauncher("JRE-Version = %s, JRE-Restrict-Search = %s Selected = %s\n",
928 (info.jre_version?info.jre_version:"null"),
929 (info.jre_restrict_search?"true":"false"), (jre?jre:"null"));
930
931 if (jre == NULL) {
932 if (JLI_AcceptableRelease(GetFullVersion(), info.jre_version)) {
933 JLI_FreeManifest();
934 JLI_MemFree(new_argv);
935 return;
936 } else {
ksrini0e817162008-08-26 10:21:20 -0700937 JLI_ReportErrorMessage(CFG_ERROR4, info.jre_version);
duke6e45e102007-12-01 00:00:00 +0000938 exit(1);
939 }
940 }
941
942 /*
943 * If I'm not the chosen one, exec the chosen one. Returning from
944 * ExecJRE indicates that I am indeed the chosen one.
945 *
946 * The private environment variable _JAVA_VERSION_SET is used to
947 * prevent the chosen one from re-reading the manifest file and
948 * using the values found within to override the (potential) command
949 * line flags stripped from argv (because the target may not
950 * understand them). Passing the MainClass value is an optimization
951 * to avoid locating, expanding and parsing the manifest extra
952 * times.
953 */
ksrini8760ca92008-09-04 09:43:32 -0700954 if (info.main_class != NULL) {
955 if (JLI_StrLen(info.main_class) <= MAXNAMELEN) {
956 (void)JLI_StrCat(env_entry, info.main_class);
957 } else {
asaha80ccd422009-04-16 21:08:04 -0700958 JLI_ReportErrorMessage(CLS_ERROR5, MAXNAMELEN);
ksrini8760ca92008-09-04 09:43:32 -0700959 exit(1);
960 }
961 }
duke6e45e102007-12-01 00:00:00 +0000962 (void)putenv(env_entry);
963 ExecJRE(jre, new_argv);
964 JLI_FreeManifest();
965 JLI_MemFree(new_argv);
966 return;
967}
968
969/*
970 * Parses command line arguments. Returns JNI_FALSE if launcher
971 * should exit without starting vm, returns JNI_TRUE if vm needs
mchungea290e22011-01-21 09:43:57 -0800972 * to be started to process given options. *pret (the launcher
duke6e45e102007-12-01 00:00:00 +0000973 * process return value) is set to 0 for a normal exit.
974 */
975static jboolean
mchungea290e22011-01-21 09:43:57 -0800976ParseArguments(int *pargc, char ***pargv,
977 int *pmode, char **pwhat,
978 int *pret, const char *jrepath)
duke6e45e102007-12-01 00:00:00 +0000979{
980 int argc = *pargc;
981 char **argv = *pargv;
mchungea290e22011-01-21 09:43:57 -0800982 int mode = LM_UNKNOWN;
duke6e45e102007-12-01 00:00:00 +0000983 char *arg;
984
985 *pret = 0;
986
987 while ((arg = *argv) != 0 && *arg == '-') {
988 argv++; --argc;
989 if (JLI_StrCmp(arg, "-classpath") == 0 || JLI_StrCmp(arg, "-cp") == 0) {
990 ARG_CHECK (argc, ARG_ERROR1, arg);
991 SetClassPath(*argv);
mchungea290e22011-01-21 09:43:57 -0800992 mode = LM_CLASS;
duke6e45e102007-12-01 00:00:00 +0000993 argv++; --argc;
994 } else if (JLI_StrCmp(arg, "-jar") == 0) {
995 ARG_CHECK (argc, ARG_ERROR2, arg);
mchungea290e22011-01-21 09:43:57 -0800996 mode = LM_JAR;
duke6e45e102007-12-01 00:00:00 +0000997 } else if (JLI_StrCmp(arg, "-help") == 0 ||
998 JLI_StrCmp(arg, "-h") == 0 ||
999 JLI_StrCmp(arg, "-?") == 0) {
1000 printUsage = JNI_TRUE;
1001 return JNI_TRUE;
1002 } else if (JLI_StrCmp(arg, "-version") == 0) {
1003 printVersion = JNI_TRUE;
1004 return JNI_TRUE;
1005 } else if (JLI_StrCmp(arg, "-showversion") == 0) {
1006 showVersion = JNI_TRUE;
1007 } else if (JLI_StrCmp(arg, "-X") == 0) {
1008 printXUsage = JNI_TRUE;
1009 return JNI_TRUE;
1010/*
ksrini8e20e1c2010-11-23 16:52:39 -08001011 * The following case checks for -XshowSettings OR -XshowSetting:SUBOPT.
1012 * In the latter case, any SUBOPT value not recognized will default to "all"
1013 */
1014 } else if (JLI_StrCmp(arg, "-XshowSettings") == 0 ||
1015 JLI_StrCCmp(arg, "-XshowSettings:") == 0) {
1016 showSettings = arg;
ksrini9636e2e2011-02-03 15:41:23 -08001017 } else if (JLI_StrCmp(arg, "-Xdiag") == 0) {
1018 AddOption("-Dsun.java.launcher.diag=true", NULL);
ksrini8e20e1c2010-11-23 16:52:39 -08001019/*
duke6e45e102007-12-01 00:00:00 +00001020 * The following case provide backward compatibility with old-style
1021 * command line options.
1022 */
1023 } else if (JLI_StrCmp(arg, "-fullversion") == 0) {
ksrini0e817162008-08-26 10:21:20 -07001024 JLI_ReportMessage("%s full version \"%s\"", _launcher_name, GetFullVersion());
duke6e45e102007-12-01 00:00:00 +00001025 return JNI_FALSE;
1026 } else if (JLI_StrCmp(arg, "-verbosegc") == 0) {
1027 AddOption("-verbose:gc", NULL);
1028 } else if (JLI_StrCmp(arg, "-t") == 0) {
1029 AddOption("-Xt", NULL);
1030 } else if (JLI_StrCmp(arg, "-tm") == 0) {
1031 AddOption("-Xtm", NULL);
1032 } else if (JLI_StrCmp(arg, "-debug") == 0) {
1033 AddOption("-Xdebug", NULL);
1034 } else if (JLI_StrCmp(arg, "-noclassgc") == 0) {
1035 AddOption("-Xnoclassgc", NULL);
1036 } else if (JLI_StrCmp(arg, "-Xfuture") == 0) {
1037 AddOption("-Xverify:all", NULL);
1038 } else if (JLI_StrCmp(arg, "-verify") == 0) {
1039 AddOption("-Xverify:all", NULL);
1040 } else if (JLI_StrCmp(arg, "-verifyremote") == 0) {
1041 AddOption("-Xverify:remote", NULL);
1042 } else if (JLI_StrCmp(arg, "-noverify") == 0) {
1043 AddOption("-Xverify:none", NULL);
1044 } else if (JLI_StrCCmp(arg, "-prof") == 0) {
1045 char *p = arg + 5;
1046 char *tmp = JLI_MemAlloc(JLI_StrLen(arg) + 50);
1047 if (*p) {
1048 sprintf(tmp, "-Xrunhprof:cpu=old,file=%s", p + 1);
1049 } else {
1050 sprintf(tmp, "-Xrunhprof:cpu=old,file=java.prof");
1051 }
1052 AddOption(tmp, NULL);
1053 } else if (JLI_StrCCmp(arg, "-ss") == 0 ||
1054 JLI_StrCCmp(arg, "-oss") == 0 ||
1055 JLI_StrCCmp(arg, "-ms") == 0 ||
1056 JLI_StrCCmp(arg, "-mx") == 0) {
1057 char *tmp = JLI_MemAlloc(JLI_StrLen(arg) + 6);
1058 sprintf(tmp, "-X%s", arg + 1); /* skip '-' */
1059 AddOption(tmp, NULL);
1060 } else if (JLI_StrCmp(arg, "-checksource") == 0 ||
1061 JLI_StrCmp(arg, "-cs") == 0 ||
1062 JLI_StrCmp(arg, "-noasyncgc") == 0) {
1063 /* No longer supported */
ksrini0e817162008-08-26 10:21:20 -07001064 JLI_ReportErrorMessage(ARG_WARN, arg);
duke6e45e102007-12-01 00:00:00 +00001065 } else if (JLI_StrCCmp(arg, "-version:") == 0 ||
1066 JLI_StrCmp(arg, "-no-jre-restrict-search") == 0 ||
1067 JLI_StrCmp(arg, "-jre-restrict-search") == 0 ||
1068 JLI_StrCCmp(arg, "-splash:") == 0) {
1069 ; /* Ignore machine independent options already handled */
1070 } else if (RemovableOption(arg) ) {
1071 ; /* Do not pass option to vm. */
1072 } else {
1073 AddOption(arg, NULL);
1074 }
1075 }
1076
1077 if (--argc >= 0) {
mchungea290e22011-01-21 09:43:57 -08001078 *pwhat = *argv++;
1079 }
1080
1081 if (*pwhat == NULL) {
1082 *pret = 1;
1083 } else if (mode == LM_UNKNOWN) {
1084 /* default to LM_CLASS if -jar and -cp option are
1085 * not specified */
1086 mode = LM_CLASS;
1087 }
1088
1089 if (argc >= 0) {
duke6e45e102007-12-01 00:00:00 +00001090 *pargc = argc;
1091 *pargv = argv;
1092 }
mchungea290e22011-01-21 09:43:57 -08001093
1094 *pmode = mode;
1095
duke6e45e102007-12-01 00:00:00 +00001096 return JNI_TRUE;
1097}
1098
1099/*
1100 * Initializes the Java Virtual Machine. Also frees options array when
1101 * finished.
1102 */
1103static jboolean
1104InitializeJVM(JavaVM **pvm, JNIEnv **penv, InvocationFunctions *ifn)
1105{
1106 JavaVMInitArgs args;
1107 jint r;
1108
1109 memset(&args, 0, sizeof(args));
1110 args.version = JNI_VERSION_1_2;
1111 args.nOptions = numOptions;
1112 args.options = options;
1113 args.ignoreUnrecognized = JNI_FALSE;
1114
1115 if (JLI_IsTraceLauncher()) {
1116 int i = 0;
1117 printf("JavaVM args:\n ");
1118 printf("version 0x%08lx, ", (long)args.version);
1119 printf("ignoreUnrecognized is %s, ",
1120 args.ignoreUnrecognized ? "JNI_TRUE" : "JNI_FALSE");
1121 printf("nOptions is %ld\n", (long)args.nOptions);
1122 for (i = 0; i < numOptions; i++)
1123 printf(" option[%2d] = '%s'\n",
1124 i, args.options[i].optionString);
1125 }
1126
1127 r = ifn->CreateJavaVM(pvm, (void **)penv, &args);
1128 JLI_MemFree(options);
1129 return r == JNI_OK;
1130}
1131
1132
1133#define NULL_CHECK0(e) if ((e) == 0) { \
ksrini0e817162008-08-26 10:21:20 -07001134 JLI_ReportErrorMessage(JNI_ERROR); \
duke6e45e102007-12-01 00:00:00 +00001135 return 0; \
1136 }
1137
1138#define NULL_CHECK(e) if ((e) == 0) { \
ksrini0e817162008-08-26 10:21:20 -07001139 JLI_ReportErrorMessage(JNI_ERROR); \
duke6e45e102007-12-01 00:00:00 +00001140 return; \
1141 }
1142
ksrini7d9872e2011-03-20 08:41:33 -07001143static jclass helperClass = NULL;
1144
1145static jclass
1146GetLauncherHelperClass(JNIEnv *env) {
1147 if (helperClass == NULL) {
1148 NULL_CHECK0(helperClass = FindBootStrapClass(env,
1149 "sun/launcher/LauncherHelper"));
duke6e45e102007-12-01 00:00:00 +00001150 }
ksrini7d9872e2011-03-20 08:41:33 -07001151 return helperClass;
duke6e45e102007-12-01 00:00:00 +00001152}
1153
ksrini7d9872e2011-03-20 08:41:33 -07001154static jmethodID makePlatformStringMID = NULL;
duke6e45e102007-12-01 00:00:00 +00001155/*
1156 * Returns a new Java string object for the specified platform string.
1157 */
1158static jstring
1159NewPlatformString(JNIEnv *env, char *s)
1160{
1161 int len = (int)JLI_StrLen(s);
duke6e45e102007-12-01 00:00:00 +00001162 jbyteArray ary;
ksrini7d9872e2011-03-20 08:41:33 -07001163 jclass cls = GetLauncherHelperClass(env);
1164 NULL_CHECK0(cls);
duke6e45e102007-12-01 00:00:00 +00001165 if (s == NULL)
1166 return 0;
duke6e45e102007-12-01 00:00:00 +00001167
1168 ary = (*env)->NewByteArray(env, len);
1169 if (ary != 0) {
1170 jstring str = 0;
1171 (*env)->SetByteArrayRegion(env, ary, 0, len, (jbyte *)s);
1172 if (!(*env)->ExceptionOccurred(env)) {
ksrini7d9872e2011-03-20 08:41:33 -07001173 if (makePlatformStringMID == NULL) {
1174 NULL_CHECK0(makePlatformStringMID = (*env)->GetStaticMethodID(env,
1175 cls, "makePlatformString", "(Z[B)Ljava/lang/String;"));
duke6e45e102007-12-01 00:00:00 +00001176 }
ksrini7d9872e2011-03-20 08:41:33 -07001177 str = (*env)->CallStaticObjectMethod(env, cls,
1178 makePlatformStringMID, USE_STDERR, ary);
duke6e45e102007-12-01 00:00:00 +00001179 (*env)->DeleteLocalRef(env, ary);
1180 return str;
1181 }
1182 }
1183 return 0;
1184}
1185
1186/*
1187 * Returns a new array of Java string objects for the specified
1188 * array of platform strings.
1189 */
1190static jobjectArray
1191NewPlatformStringArray(JNIEnv *env, char **strv, int strc)
1192{
1193 jarray cls;
1194 jarray ary;
1195 int i;
1196
ksrini20a64b22008-09-24 15:07:41 -07001197 NULL_CHECK0(cls = FindBootStrapClass(env, "java/lang/String"));
duke6e45e102007-12-01 00:00:00 +00001198 NULL_CHECK0(ary = (*env)->NewObjectArray(env, strc, cls, 0));
1199 for (i = 0; i < strc; i++) {
1200 jstring str = NewPlatformString(env, *strv++);
1201 NULL_CHECK0(str);
1202 (*env)->SetObjectArrayElement(env, ary, i, str);
1203 (*env)->DeleteLocalRef(env, str);
1204 }
1205 return ary;
1206}
1207
1208/*
ksrini20a64b22008-09-24 15:07:41 -07001209 * Loads a class and verifies that the main class is present and it is ok to
1210 * call it for more details refer to the java implementation.
duke6e45e102007-12-01 00:00:00 +00001211 */
1212static jclass
mchungea290e22011-01-21 09:43:57 -08001213LoadMainClass(JNIEnv *env, int mode, char *name)
duke6e45e102007-12-01 00:00:00 +00001214{
ksrini20a64b22008-09-24 15:07:41 -07001215 jmethodID mid;
1216 jstring str;
1217 jobject result;
duke6e45e102007-12-01 00:00:00 +00001218 jlong start, end;
ksrini7d9872e2011-03-20 08:41:33 -07001219 jclass cls = GetLauncherHelperClass(env);
1220 NULL_CHECK0(cls);
ksrini20a64b22008-09-24 15:07:41 -07001221 if (JLI_IsTraceLauncher()) {
duke6e45e102007-12-01 00:00:00 +00001222 start = CounterGet();
ksrini20a64b22008-09-24 15:07:41 -07001223 }
ksrini7d9872e2011-03-20 08:41:33 -07001224 NULL_CHECK0(mid = (*env)->GetStaticMethodID(env, cls,
1225 "checkAndLoadMain",
1226 "(ZILjava/lang/String;)Ljava/lang/Class;"));
1227
ksriniac5e02f2012-01-11 08:14:47 -08001228 str = NewPlatformString(env, name);
ksrini7d9872e2011-03-20 08:41:33 -07001229 result = (*env)->CallStaticObjectMethod(env, cls, mid, USE_STDERR, mode, str);
duke6e45e102007-12-01 00:00:00 +00001230
1231 if (JLI_IsTraceLauncher()) {
1232 end = CounterGet();
1233 printf("%ld micro seconds to load main class\n",
1234 (long)(jint)Counter2Micros(end-start));
1235 printf("----_JAVA_LAUNCHER_DEBUG----\n");
1236 }
1237
ksrini20a64b22008-09-24 15:07:41 -07001238 return (jclass)result;
duke6e45e102007-12-01 00:00:00 +00001239}
1240
duke6e45e102007-12-01 00:00:00 +00001241/*
1242 * For tools, convert command line args thus:
1243 * javac -cp foo:foo/"*" -J-ms32m ...
1244 * java -ms32m -cp JLI_WildcardExpandClasspath(foo:foo/"*") ...
1245 *
1246 * Takes 4 parameters, and returns the populated arguments
1247 */
1248static void
1249TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv)
1250{
1251 int argc = *pargc;
1252 char **argv = *pargv;
1253 int nargc = argc + jargc;
1254 char **nargv = JLI_MemAlloc((nargc + 1) * sizeof(char *));
1255 int i;
1256
1257 *pargc = nargc;
1258 *pargv = nargv;
1259
1260 /* Copy the VM arguments (i.e. prefixed with -J) */
1261 for (i = 0; i < jargc; i++) {
1262 const char *arg = jargv[i];
1263 if (arg[0] == '-' && arg[1] == 'J') {
1264 *nargv++ = ((arg + 2) == NULL) ? NULL : JLI_StringDup(arg + 2);
1265 }
1266 }
1267
1268 for (i = 0; i < argc; i++) {
1269 char *arg = argv[i];
1270 if (arg[0] == '-' && arg[1] == 'J') {
1271 if (arg[2] == '\0') {
ksrini0e817162008-08-26 10:21:20 -07001272 JLI_ReportErrorMessage(ARG_ERROR3);
duke6e45e102007-12-01 00:00:00 +00001273 exit(1);
1274 }
1275 *nargv++ = arg + 2;
1276 }
1277 }
1278
1279 /* Copy the rest of the arguments */
1280 for (i = 0; i < jargc ; i++) {
1281 const char *arg = jargv[i];
1282 if (arg[0] != '-' || arg[1] != 'J') {
1283 *nargv++ = (arg == NULL) ? NULL : JLI_StringDup(arg);
1284 }
1285 }
1286 for (i = 0; i < argc; i++) {
1287 char *arg = argv[i];
1288 if (arg[0] == '-') {
1289 if (arg[1] == 'J')
1290 continue;
1291 if (IsWildCardEnabled() && arg[1] == 'c'
1292 && (JLI_StrCmp(arg, "-cp") == 0 ||
1293 JLI_StrCmp(arg, "-classpath") == 0)
1294 && i < argc - 1) {
1295 *nargv++ = arg;
1296 *nargv++ = (char *) JLI_WildcardExpandClasspath(argv[i+1]);
1297 i++;
1298 continue;
1299 }
1300 }
1301 *nargv++ = arg;
1302 }
1303 *nargv = 0;
1304}
1305
1306/*
1307 * For our tools, we try to add 3 VM options:
1308 * -Denv.class.path=<envcp>
1309 * -Dapplication.home=<apphome>
1310 * -Djava.class.path=<appcp>
1311 * <envcp> is the user's setting of CLASSPATH -- for instance the user
1312 * tells javac where to find binary classes through this environment
1313 * variable. Notice that users will be able to compile against our
1314 * tools classes (sun.tools.javac.Main) only if they explicitly add
1315 * tools.jar to CLASSPATH.
1316 * <apphome> is the directory where the application is installed.
1317 * <appcp> is the classpath to where our apps' classfiles are.
1318 */
1319static jboolean
1320AddApplicationOptions(int cpathc, const char **cpathv)
1321{
1322 char *envcp, *appcp, *apphome;
1323 char home[MAXPATHLEN]; /* application home */
1324 char separator[] = { PATH_SEPARATOR, '\0' };
1325 int size, i;
1326
1327 {
1328 const char *s = getenv("CLASSPATH");
1329 if (s) {
1330 s = (char *) JLI_WildcardExpandClasspath(s);
1331 /* 40 for -Denv.class.path= */
1332 envcp = (char *)JLI_MemAlloc(JLI_StrLen(s) + 40);
1333 sprintf(envcp, "-Denv.class.path=%s", s);
1334 AddOption(envcp, NULL);
1335 }
1336 }
1337
1338 if (!GetApplicationHome(home, sizeof(home))) {
ksrini0e817162008-08-26 10:21:20 -07001339 JLI_ReportErrorMessage(CFG_ERROR5);
duke6e45e102007-12-01 00:00:00 +00001340 return JNI_FALSE;
1341 }
1342
1343 /* 40 for '-Dapplication.home=' */
1344 apphome = (char *)JLI_MemAlloc(JLI_StrLen(home) + 40);
1345 sprintf(apphome, "-Dapplication.home=%s", home);
1346 AddOption(apphome, NULL);
1347
1348 /* How big is the application's classpath? */
1349 size = 40; /* 40: "-Djava.class.path=" */
1350 for (i = 0; i < cpathc; i++) {
1351 size += (int)JLI_StrLen(home) + (int)JLI_StrLen(cpathv[i]) + 1; /* 1: separator */
1352 }
1353 appcp = (char *)JLI_MemAlloc(size + 1);
1354 JLI_StrCpy(appcp, "-Djava.class.path=");
1355 for (i = 0; i < cpathc; i++) {
1356 JLI_StrCat(appcp, home); /* c:\program files\myapp */
1357 JLI_StrCat(appcp, cpathv[i]); /* \lib\myapp.jar */
1358 JLI_StrCat(appcp, separator); /* ; */
1359 }
1360 appcp[JLI_StrLen(appcp)-1] = '\0'; /* remove trailing path separator */
1361 AddOption(appcp, NULL);
1362 return JNI_TRUE;
1363}
1364
1365/*
1366 * inject the -Dsun.java.command pseudo property into the args structure
1367 * this pseudo property is used in the HotSpot VM to expose the
1368 * Java class name and arguments to the main method to the VM. The
1369 * HotSpot VM uses this pseudo property to store the Java class name
1370 * (or jar file name) and the arguments to the class's main method
1371 * to the instrumentation memory region. The sun.java.command pseudo
1372 * property is not exported by HotSpot to the Java layer.
1373 */
1374void
mchungea290e22011-01-21 09:43:57 -08001375SetJavaCommandLineProp(char *what, int argc, char **argv)
duke6e45e102007-12-01 00:00:00 +00001376{
1377
1378 int i = 0;
1379 size_t len = 0;
1380 char* javaCommand = NULL;
1381 char* dashDstr = "-Dsun.java.command=";
1382
mchungea290e22011-01-21 09:43:57 -08001383 if (what == NULL) {
duke6e45e102007-12-01 00:00:00 +00001384 /* unexpected, one of these should be set. just return without
1385 * setting the property
1386 */
1387 return;
1388 }
1389
duke6e45e102007-12-01 00:00:00 +00001390 /* determine the amount of memory to allocate assuming
1391 * the individual components will be space separated
1392 */
mchungea290e22011-01-21 09:43:57 -08001393 len = JLI_StrLen(what);
duke6e45e102007-12-01 00:00:00 +00001394 for (i = 0; i < argc; i++) {
1395 len += JLI_StrLen(argv[i]) + 1;
1396 }
1397
1398 /* allocate the memory */
1399 javaCommand = (char*) JLI_MemAlloc(len + JLI_StrLen(dashDstr) + 1);
1400
1401 /* build the -D string */
1402 *javaCommand = '\0';
1403 JLI_StrCat(javaCommand, dashDstr);
mchungea290e22011-01-21 09:43:57 -08001404 JLI_StrCat(javaCommand, what);
duke6e45e102007-12-01 00:00:00 +00001405
1406 for (i = 0; i < argc; i++) {
1407 /* the components of the string are space separated. In
1408 * the case of embedded white space, the relationship of
1409 * the white space separated components to their true
1410 * positional arguments will be ambiguous. This issue may
1411 * be addressed in a future release.
1412 */
1413 JLI_StrCat(javaCommand, " ");
1414 JLI_StrCat(javaCommand, argv[i]);
1415 }
1416
1417 AddOption(javaCommand, NULL);
1418}
1419
1420/*
1421 * JVM would like to know if it's created by a standard Sun launcher, or by
1422 * user native application, the following property indicates the former.
1423 */
mchungea290e22011-01-21 09:43:57 -08001424void
1425SetJavaLauncherProp() {
duke6e45e102007-12-01 00:00:00 +00001426 AddOption("-Dsun.java.launcher=SUN_STANDARD", NULL);
1427}
1428
1429/*
1430 * Prints the version information from the java.version and other properties.
1431 */
1432static void
1433PrintJavaVersion(JNIEnv *env, jboolean extraLF)
1434{
1435 jclass ver;
1436 jmethodID print;
1437
ksrini20a64b22008-09-24 15:07:41 -07001438 NULL_CHECK(ver = FindBootStrapClass(env, "sun/misc/Version"));
duke6e45e102007-12-01 00:00:00 +00001439 NULL_CHECK(print = (*env)->GetStaticMethodID(env,
1440 ver,
1441 (extraLF == JNI_TRUE) ? "println" : "print",
1442 "()V"
1443 )
1444 );
1445
1446 (*env)->CallStaticVoidMethod(env, ver, print);
1447}
1448
1449/*
ksrini8e20e1c2010-11-23 16:52:39 -08001450 * Prints all the Java settings, see the java implementation for more details.
1451 */
1452static void
1453ShowSettings(JNIEnv *env, char *optString)
1454{
ksrini8e20e1c2010-11-23 16:52:39 -08001455 jmethodID showSettingsID;
1456 jstring joptString;
ksrini7d9872e2011-03-20 08:41:33 -07001457 jclass cls = GetLauncherHelperClass(env);
1458 NULL_CHECK(cls);
ksrini8e20e1c2010-11-23 16:52:39 -08001459 NULL_CHECK(showSettingsID = (*env)->GetStaticMethodID(env, cls,
ksrini6d575f82010-12-23 13:51:30 -08001460 "showSettings", "(ZLjava/lang/String;JJJZ)V"));
ksrini8e20e1c2010-11-23 16:52:39 -08001461 joptString = (*env)->NewStringUTF(env, optString);
1462 (*env)->CallStaticVoidMethod(env, cls, showSettingsID,
ksrini7d9872e2011-03-20 08:41:33 -07001463 USE_STDERR,
ksrini8e20e1c2010-11-23 16:52:39 -08001464 joptString,
ksrini6d575f82010-12-23 13:51:30 -08001465 (jlong)initialHeapSize,
1466 (jlong)maxHeapSize,
ksrini8e20e1c2010-11-23 16:52:39 -08001467 (jlong)threadStackSize,
1468 ServerClassMachine());
1469}
1470
1471/*
ksrini20a64b22008-09-24 15:07:41 -07001472 * Prints default usage or the Xusage message, see sun.launcher.LauncherHelper.java
duke6e45e102007-12-01 00:00:00 +00001473 */
1474static void
1475PrintUsage(JNIEnv* env, jboolean doXUsage)
1476{
duke6e45e102007-12-01 00:00:00 +00001477 jmethodID initHelp, vmSelect, vmSynonym, vmErgo, printHelp, printXUsageMessage;
1478 jstring jprogname, vm1, vm2;
1479 int i;
ksrini7d9872e2011-03-20 08:41:33 -07001480 jclass cls = GetLauncherHelperClass(env);
1481 NULL_CHECK(cls);
duke6e45e102007-12-01 00:00:00 +00001482 if (doXUsage) {
1483 NULL_CHECK(printXUsageMessage = (*env)->GetStaticMethodID(env, cls,
1484 "printXUsageMessage", "(Z)V"));
ksrini7d9872e2011-03-20 08:41:33 -07001485 (*env)->CallStaticVoidMethod(env, cls, printXUsageMessage, USE_STDERR);
duke6e45e102007-12-01 00:00:00 +00001486 } else {
1487 NULL_CHECK(initHelp = (*env)->GetStaticMethodID(env, cls,
1488 "initHelpMessage", "(Ljava/lang/String;)V"));
1489
1490 NULL_CHECK(vmSelect = (*env)->GetStaticMethodID(env, cls, "appendVmSelectMessage",
1491 "(Ljava/lang/String;Ljava/lang/String;)V"));
1492
1493 NULL_CHECK(vmSynonym = (*env)->GetStaticMethodID(env, cls,
1494 "appendVmSynonymMessage",
1495 "(Ljava/lang/String;Ljava/lang/String;)V"));
1496 NULL_CHECK(vmErgo = (*env)->GetStaticMethodID(env, cls,
1497 "appendVmErgoMessage", "(ZLjava/lang/String;)V"));
1498
1499 NULL_CHECK(printHelp = (*env)->GetStaticMethodID(env, cls,
1500 "printHelpMessage", "(Z)V"));
1501
1502 jprogname = (*env)->NewStringUTF(env, _program_name);
1503
1504 /* Initialize the usage message with the usual preamble */
1505 (*env)->CallStaticVoidMethod(env, cls, initHelp, jprogname);
1506
1507
1508 /* Assemble the other variant part of the usage */
1509 if ((knownVMs[0].flag == VM_KNOWN) ||
1510 (knownVMs[0].flag == VM_IF_SERVER_CLASS)) {
1511 vm1 = (*env)->NewStringUTF(env, knownVMs[0].name);
1512 vm2 = (*env)->NewStringUTF(env, knownVMs[0].name+1);
1513 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1514 }
1515 for (i=1; i<knownVMsCount; i++) {
1516 if (knownVMs[i].flag == VM_KNOWN) {
1517 vm1 = (*env)->NewStringUTF(env, knownVMs[i].name);
1518 vm2 = (*env)->NewStringUTF(env, knownVMs[i].name+1);
1519 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1520 }
1521 }
1522 for (i=1; i<knownVMsCount; i++) {
1523 if (knownVMs[i].flag == VM_ALIASED_TO) {
1524 vm1 = (*env)->NewStringUTF(env, knownVMs[i].name);
1525 vm2 = (*env)->NewStringUTF(env, knownVMs[i].alias+1);
1526 (*env)->CallStaticVoidMethod(env, cls, vmSynonym, vm1, vm2);
1527 }
1528 }
1529
1530 /* The first known VM is the default */
1531 {
1532 jboolean isServerClassMachine = ServerClassMachine();
1533
1534 const char* defaultVM = knownVMs[0].name+1;
1535 if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) && isServerClassMachine) {
1536 defaultVM = knownVMs[0].server_class+1;
1537 }
1538
1539 vm1 = (*env)->NewStringUTF(env, defaultVM);
1540 (*env)->CallStaticVoidMethod(env, cls, vmErgo, isServerClassMachine, vm1);
1541 }
1542
1543 /* Complete the usage message and print to stderr*/
ksrini7d9872e2011-03-20 08:41:33 -07001544 (*env)->CallStaticVoidMethod(env, cls, printHelp, USE_STDERR);
duke6e45e102007-12-01 00:00:00 +00001545 }
1546 return;
1547}
1548
1549/*
1550 * Read the jvm.cfg file and fill the knownJVMs[] array.
1551 *
1552 * The functionality of the jvm.cfg file is subject to change without
1553 * notice and the mechanism will be removed in the future.
1554 *
1555 * The lexical structure of the jvm.cfg file is as follows:
1556 *
1557 * jvmcfg := { vmLine }
1558 * vmLine := knownLine
1559 * | aliasLine
1560 * | warnLine
1561 * | ignoreLine
1562 * | errorLine
1563 * | predicateLine
1564 * | commentLine
1565 * knownLine := flag "KNOWN" EOL
1566 * warnLine := flag "WARN" EOL
1567 * ignoreLine := flag "IGNORE" EOL
1568 * errorLine := flag "ERROR" EOL
1569 * aliasLine := flag "ALIASED_TO" flag EOL
1570 * predicateLine := flag "IF_SERVER_CLASS" flag EOL
1571 * commentLine := "#" text EOL
1572 * flag := "-" identifier
1573 *
1574 * The semantics are that when someone specifies a flag on the command line:
1575 * - if the flag appears on a knownLine, then the identifier is used as
1576 * the name of the directory holding the JVM library (the name of the JVM).
1577 * - if the flag appears as the first flag on an aliasLine, the identifier
1578 * of the second flag is used as the name of the JVM.
1579 * - if the flag appears on a warnLine, the identifier is used as the
1580 * name of the JVM, but a warning is generated.
1581 * - if the flag appears on an ignoreLine, the identifier is recognized as the
1582 * name of a JVM, but the identifier is ignored and the default vm used
1583 * - if the flag appears on an errorLine, an error is generated.
1584 * - if the flag appears as the first flag on a predicateLine, and
1585 * the machine on which you are running passes the predicate indicated,
1586 * then the identifier of the second flag is used as the name of the JVM,
1587 * otherwise the identifier of the first flag is used as the name of the JVM.
1588 * If no flag is given on the command line, the first vmLine of the jvm.cfg
1589 * file determines the name of the JVM.
1590 * PredicateLines are only interpreted on first vmLine of a jvm.cfg file,
1591 * since they only make sense if someone hasn't specified the name of the
1592 * JVM on the command line.
1593 *
1594 * The intent of the jvm.cfg file is to allow several JVM libraries to
1595 * be installed in different subdirectories of a single JRE installation,
1596 * for space-savings and convenience in testing.
1597 * The intent is explicitly not to provide a full aliasing or predicate
1598 * mechanism.
1599 */
1600jint
1601ReadKnownVMs(const char *jrepath, const char * arch, jboolean speculative)
1602{
1603 FILE *jvmCfg;
1604 char jvmCfgName[MAXPATHLEN+20];
1605 char line[MAXPATHLEN+20];
1606 int cnt = 0;
1607 int lineno = 0;
1608 jlong start, end;
1609 int vmType;
1610 char *tmpPtr;
1611 char *altVMName = NULL;
1612 char *serverClassVMName = NULL;
1613 static char *whiteSpace = " \t";
1614 if (JLI_IsTraceLauncher()) {
1615 start = CounterGet();
1616 }
ksrini01740ec2010-09-09 11:50:40 -07001617 JLI_Snprintf(jvmCfgName, sizeof(jvmCfgName), "%s%slib%s%s%sjvm.cfg",
1618 jrepath, FILESEP, FILESEP, arch, FILESEP);
duke6e45e102007-12-01 00:00:00 +00001619
1620 jvmCfg = fopen(jvmCfgName, "r");
1621 if (jvmCfg == NULL) {
1622 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -07001623 JLI_ReportErrorMessage(CFG_ERROR6, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001624 exit(1);
1625 } else {
1626 return -1;
1627 }
1628 }
1629 while (fgets(line, sizeof(line), jvmCfg) != NULL) {
1630 vmType = VM_UNKNOWN;
1631 lineno++;
1632 if (line[0] == '#')
1633 continue;
1634 if (line[0] != '-') {
ksrini0e817162008-08-26 10:21:20 -07001635 JLI_ReportErrorMessage(CFG_WARN2, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001636 }
1637 if (cnt >= knownVMsLimit) {
1638 GrowKnownVMs(cnt);
1639 }
1640 line[JLI_StrLen(line)-1] = '\0'; /* remove trailing newline */
1641 tmpPtr = line + JLI_StrCSpn(line, whiteSpace);
1642 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001643 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001644 } else {
1645 /* Null-terminate this string for JLI_StringDup below */
1646 *tmpPtr++ = 0;
1647 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1648 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001649 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001650 } else {
1651 if (!JLI_StrCCmp(tmpPtr, "KNOWN")) {
1652 vmType = VM_KNOWN;
1653 } else if (!JLI_StrCCmp(tmpPtr, "ALIASED_TO")) {
1654 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1655 if (*tmpPtr != 0) {
1656 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1657 }
1658 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001659 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001660 } else {
1661 /* Null terminate altVMName */
1662 altVMName = tmpPtr;
1663 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1664 *tmpPtr = 0;
1665 vmType = VM_ALIASED_TO;
1666 }
1667 } else if (!JLI_StrCCmp(tmpPtr, "WARN")) {
1668 vmType = VM_WARN;
1669 } else if (!JLI_StrCCmp(tmpPtr, "IGNORE")) {
1670 vmType = VM_IGNORE;
1671 } else if (!JLI_StrCCmp(tmpPtr, "ERROR")) {
1672 vmType = VM_ERROR;
1673 } else if (!JLI_StrCCmp(tmpPtr, "IF_SERVER_CLASS")) {
1674 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1675 if (*tmpPtr != 0) {
1676 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1677 }
1678 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001679 JLI_ReportErrorMessage(CFG_WARN4, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001680 } else {
1681 /* Null terminate server class VM name */
1682 serverClassVMName = tmpPtr;
1683 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1684 *tmpPtr = 0;
1685 vmType = VM_IF_SERVER_CLASS;
1686 }
1687 } else {
ksrini0e817162008-08-26 10:21:20 -07001688 JLI_ReportErrorMessage(CFG_WARN5, lineno, &jvmCfgName[0]);
duke6e45e102007-12-01 00:00:00 +00001689 vmType = VM_KNOWN;
1690 }
1691 }
1692 }
1693
1694 JLI_TraceLauncher("jvm.cfg[%d] = ->%s<-\n", cnt, line);
1695 if (vmType != VM_UNKNOWN) {
1696 knownVMs[cnt].name = JLI_StringDup(line);
1697 knownVMs[cnt].flag = vmType;
1698 switch (vmType) {
1699 default:
1700 break;
1701 case VM_ALIASED_TO:
1702 knownVMs[cnt].alias = JLI_StringDup(altVMName);
1703 JLI_TraceLauncher(" name: %s vmType: %s alias: %s\n",
1704 knownVMs[cnt].name, "VM_ALIASED_TO", knownVMs[cnt].alias);
1705 break;
1706 case VM_IF_SERVER_CLASS:
1707 knownVMs[cnt].server_class = JLI_StringDup(serverClassVMName);
1708 JLI_TraceLauncher(" name: %s vmType: %s server_class: %s\n",
1709 knownVMs[cnt].name, "VM_IF_SERVER_CLASS", knownVMs[cnt].server_class);
1710 break;
1711 }
1712 cnt++;
1713 }
1714 }
1715 fclose(jvmCfg);
1716 knownVMsCount = cnt;
1717
1718 if (JLI_IsTraceLauncher()) {
1719 end = CounterGet();
1720 printf("%ld micro seconds to parse jvm.cfg\n",
1721 (long)(jint)Counter2Micros(end-start));
1722 }
1723
1724 return cnt;
1725}
1726
1727
1728static void
1729GrowKnownVMs(int minimum)
1730{
1731 struct vmdesc* newKnownVMs;
1732 int newMax;
1733
1734 newMax = (knownVMsLimit == 0 ? INIT_MAX_KNOWN_VMS : (2 * knownVMsLimit));
1735 if (newMax <= minimum) {
1736 newMax = minimum;
1737 }
1738 newKnownVMs = (struct vmdesc*) JLI_MemAlloc(newMax * sizeof(struct vmdesc));
1739 if (knownVMs != NULL) {
1740 memcpy(newKnownVMs, knownVMs, knownVMsLimit * sizeof(struct vmdesc));
1741 }
1742 JLI_MemFree(knownVMs);
1743 knownVMs = newKnownVMs;
1744 knownVMsLimit = newMax;
1745}
1746
1747
1748/* Returns index of VM or -1 if not found */
1749static int
1750KnownVMIndex(const char* name)
1751{
1752 int i;
1753 if (JLI_StrCCmp(name, "-J") == 0) name += 2;
1754 for (i = 0; i < knownVMsCount; i++) {
1755 if (!JLI_StrCmp(name, knownVMs[i].name)) {
1756 return i;
1757 }
1758 }
1759 return -1;
1760}
1761
1762static void
1763FreeKnownVMs()
1764{
1765 int i;
1766 for (i = 0; i < knownVMsCount; i++) {
1767 JLI_MemFree(knownVMs[i].name);
1768 knownVMs[i].name = NULL;
1769 }
1770 JLI_MemFree(knownVMs);
1771}
1772
1773
1774/*
1775 * Displays the splash screen according to the jar file name
1776 * and image file names stored in environment variables
1777 */
1778static void
1779ShowSplashScreen()
1780{
1781 const char *jar_name = getenv(SPLASH_JAR_ENV_ENTRY);
1782 const char *file_name = getenv(SPLASH_FILE_ENV_ENTRY);
1783 int data_size;
1784 void *image_data;
1785 if (jar_name) {
1786 image_data = JLI_JarUnpackFile(jar_name, file_name, &data_size);
1787 if (image_data) {
1788 DoSplashInit();
1789 DoSplashLoadMemory(image_data, data_size);
1790 JLI_MemFree(image_data);
1791 }
1792 } else if (file_name) {
1793 DoSplashInit();
1794 DoSplashLoadFile(file_name);
1795 } else {
1796 return;
1797 }
1798 DoSplashSetFileJarName(file_name, jar_name);
1799
1800 /*
1801 * Done with all command line processing and potential re-execs so
1802 * clean up the environment.
1803 */
1804 (void)UnsetEnv(ENV_ENTRY);
1805 (void)UnsetEnv(SPLASH_FILE_ENV_ENTRY);
1806 (void)UnsetEnv(SPLASH_JAR_ENV_ENTRY);
1807
1808 JLI_MemFree(splash_jar_entry);
1809 JLI_MemFree(splash_file_entry);
1810
1811}
1812
1813const char*
1814GetDotVersion()
1815{
1816 return _dVersion;
1817}
1818
1819const char*
1820GetFullVersion()
1821{
1822 return _fVersion;
1823}
1824
1825const char*
1826GetProgramName()
1827{
1828 return _program_name;
1829}
1830
1831const char*
1832GetLauncherName()
1833{
1834 return _launcher_name;
1835}
1836
1837jint
1838GetErgoPolicy()
1839{
1840 return _ergo_policy;
1841}
1842
1843jboolean
1844IsJavaArgs()
1845{
1846 return _is_java_args;
1847}
1848
1849static jboolean
1850IsWildCardEnabled()
1851{
1852 return _wc_enabled;
1853}
1854
1855static int
mchungea290e22011-01-21 09:43:57 -08001856ContinueInNewThread(InvocationFunctions* ifn, int argc, char **argv,
1857 int mode, char *what, int ret)
duke6e45e102007-12-01 00:00:00 +00001858{
1859
1860 /*
1861 * If user doesn't specify stack size, check if VM has a preference.
1862 * Note that HotSpot no longer supports JNI_VERSION_1_1 but it will
1863 * return its default stack size through the init args structure.
1864 */
1865 if (threadStackSize == 0) {
1866 struct JDK1_1InitArgs args1_1;
1867 memset((void*)&args1_1, 0, sizeof(args1_1));
1868 args1_1.version = JNI_VERSION_1_1;
1869 ifn->GetDefaultJavaVMInitArgs(&args1_1); /* ignore return value */
1870 if (args1_1.javaStackSize > 0) {
1871 threadStackSize = args1_1.javaStackSize;
1872 }
1873 }
1874
1875 { /* Create a new thread to create JVM and invoke main method */
1876 JavaMainArgs args;
1877 int rslt;
1878
1879 args.argc = argc;
1880 args.argv = argv;
mchungea290e22011-01-21 09:43:57 -08001881 args.mode = mode;
1882 args.what = what;
duke6e45e102007-12-01 00:00:00 +00001883 args.ifn = *ifn;
1884
1885 rslt = ContinueInNewThread0(JavaMain, threadStackSize, (void*)&args);
1886 /* If the caller has deemed there is an error we
1887 * simply return that, otherwise we return the value of
1888 * the callee
1889 */
1890 return (ret != 0) ? ret : rslt;
1891 }
1892}
1893
1894static void
1895DumpState()
1896{
1897 if (!JLI_IsTraceLauncher()) return ;
1898 printf("Launcher state:\n");
1899 printf("\tdebug:%s\n", (JLI_IsTraceLauncher() == JNI_TRUE) ? "on" : "off");
1900 printf("\tjavargs:%s\n", (_is_java_args == JNI_TRUE) ? "on" : "off");
1901 printf("\tprogram name:%s\n", GetProgramName());
1902 printf("\tlauncher name:%s\n", GetLauncherName());
1903 printf("\tjavaw:%s\n", (IsJavaw() == JNI_TRUE) ? "on" : "off");
1904 printf("\tfullversion:%s\n", GetFullVersion());
1905 printf("\tdotversion:%s\n", GetDotVersion());
1906 printf("\tergo_policy:");
1907 switch(GetErgoPolicy()) {
1908 case NEVER_SERVER_CLASS:
1909 printf("NEVER_ACT_AS_A_SERVER_CLASS_MACHINE\n");
1910 break;
1911 case ALWAYS_SERVER_CLASS:
1912 printf("ALWAYS_ACT_AS_A_SERVER_CLASS_MACHINE\n");
1913 break;
1914 default:
1915 printf("DEFAULT_ERGONOMICS_POLICY\n");
1916 }
1917}
1918
1919/*
1920 * Return JNI_TRUE for an option string that has no effect but should
1921 * _not_ be passed on to the vm; return JNI_FALSE otherwise. On
1922 * Solaris SPARC, this screening needs to be done if:
ksrini11e7f1b2009-11-20 11:01:32 -08001923 * -d32 or -d64 is passed to a binary with an unmatched data model
1924 * (the exec in CreateExecutionEnvironment removes -d<n> options and points the
1925 * exec to the proper binary). In the case of when the data model and the
1926 * requested version is matched, an exec would not occur, and these options
1927 * were erroneously passed to the vm.
duke6e45e102007-12-01 00:00:00 +00001928 */
1929jboolean
1930RemovableOption(char * option)
1931{
1932 /*
1933 * Unconditionally remove both -d32 and -d64 options since only
1934 * the last such options has an effect; e.g.
1935 * java -d32 -d64 -d32 -version
1936 * is equivalent to
1937 * java -d32 -version
1938 */
1939
1940 if( (JLI_StrCCmp(option, "-d32") == 0 ) ||
1941 (JLI_StrCCmp(option, "-d64") == 0 ) )
1942 return JNI_TRUE;
1943 else
1944 return JNI_FALSE;
1945}
1946
1947/*
1948 * A utility procedure to always print to stderr
1949 */
1950void
ksrini0e817162008-08-26 10:21:20 -07001951JLI_ReportMessage(const char* fmt, ...)
duke6e45e102007-12-01 00:00:00 +00001952{
1953 va_list vl;
1954 va_start(vl, fmt);
1955 vfprintf(stderr, fmt, vl);
1956 fprintf(stderr, "\n");
1957 va_end(vl);
1958}