blob: 2d339547761d1f510b354c188f162632fa942657 [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
ksrinie3ec45d2010-07-09 11:04:34 -07002 * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
duke6e45e102007-12-01 00:00:00 +00003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
ohair2283b9d2010-05-25 15:58:33 -07007 * published by the Free Software Foundation. Oracle designates this
duke6e45e102007-12-01 00:00:00 +00008 * particular file as subject to the "Classpath" exception as provided
ohair2283b9d2010-05-25 15:58:33 -07009 * by Oracle in the LICENSE file that accompanied this code.
duke6e45e102007-12-01 00:00:00 +000010 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
ohair2283b9d2010-05-25 15:58:33 -070021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
duke6e45e102007-12-01 00:00:00 +000024 */
25
26/*
27 * Shared source for 'java' command line tool.
28 *
29 * If JAVA_ARGS is defined, then acts as a launcher for applications. For
30 * instance, the JDK command line tools such as javac and javadoc (see
31 * makefiles for more details) are built with this program. Any arguments
32 * prefixed with '-J' will be passed directly to the 'java' command.
33 */
34
35/*
36 * One job of the launcher is to remove command line options which the
37 * vm does not understand and will not process. These options include
38 * options which select which style of vm is run (e.g. -client and
39 * -server) as well as options which select the data model to use.
40 * Additionally, for tools which invoke an underlying vm "-J-foo"
41 * options are turned into "-foo" options to the vm. This option
42 * filtering is handled in a number of places in the launcher, some of
43 * it in machine-dependent code. In this file, the function
ksrini11e7f1b2009-11-20 11:01:32 -080044 * CheckJvmType removes vm style options and TranslateApplicationArgs
45 * removes "-J" prefixes. The CreateExecutionEnvironment function processes
46 * and removes -d<n> options. On unix, there is a possibility that the running
47 * data model may not match to the desired data model, in this case an exec is
48 * required to start the desired model. If the data models match, then
49 * ParseArguments will remove the -d<n> flags. If the data models do not match
50 * the CreateExecutionEnviroment will remove the -d<n> flags.
duke6e45e102007-12-01 00:00:00 +000051 */
52
53
54#include "java.h"
55
56/*
57 * A NOTE TO DEVELOPERS: For performance reasons it is important that
58 * the program image remain relatively small until after SelectVersion
59 * CreateExecutionEnvironment have finished their possibly recursive
60 * processing. Watch everything, but resist all temptations to use Java
61 * interfaces.
62 */
63
64static jboolean printVersion = JNI_FALSE; /* print and exit */
65static jboolean showVersion = JNI_FALSE; /* print but continue */
66static jboolean printUsage = JNI_FALSE; /* print and exit*/
67static jboolean printXUsage = JNI_FALSE; /* print and exit*/
ksrini8e20e1c2010-11-23 16:52:39 -080068static char *showSettings = NULL; /* print but continue */
duke6e45e102007-12-01 00:00:00 +000069
70static const char *_program_name;
71static const char *_launcher_name;
72static jboolean _is_java_args = JNI_FALSE;
73static const char *_fVersion;
74static const char *_dVersion;
75static jboolean _wc_enabled = JNI_FALSE;
76static jint _ergo_policy = DEFAULT_POLICY;
77
78/*
79 * Entries for splash screen environment variables.
80 * putenv is performed in SelectVersion. We need
81 * them in memory until UnsetEnv, so they are made static
82 * global instead of auto local.
83 */
84static char* splash_file_entry = NULL;
85static char* splash_jar_entry = NULL;
86
87/*
88 * List of VM options to be specified when the VM is created.
89 */
90static JavaVMOption *options;
91static int numOptions, maxOptions;
92
93/*
94 * Prototypes for functions internal to launcher.
95 */
96static void SetClassPath(const char *s);
97static void SelectVersion(int argc, char **argv, char **main_class);
mchungea290e22011-01-21 09:43:57 -080098static jboolean ParseArguments(int *pargc, char ***pargv,
99 int *pmode, char **pwhat,
100 int *pret, const char *jrepath);
duke6e45e102007-12-01 00:00:00 +0000101static jboolean InitializeJVM(JavaVM **pvm, JNIEnv **penv,
102 InvocationFunctions *ifn);
103static jstring NewPlatformString(JNIEnv *env, char *s);
104static jobjectArray NewPlatformStringArray(JNIEnv *env, char **strv, int strc);
mchungea290e22011-01-21 09:43:57 -0800105static jclass LoadMainClass(JNIEnv *env, int mode, char *name);
duke6e45e102007-12-01 00:00:00 +0000106
107static void TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv);
108static jboolean AddApplicationOptions(int cpathc, const char **cpathv);
109static void SetApplicationClassPath(const char**);
110
111static void PrintJavaVersion(JNIEnv *env, jboolean extraLF);
112static void PrintUsage(JNIEnv* env, jboolean doXUsage);
ksrini8e20e1c2010-11-23 16:52:39 -0800113static void ShowSettings(JNIEnv* env, char *optString);
duke6e45e102007-12-01 00:00:00 +0000114
115static void SetPaths(int argc, char **argv);
116
117static void DumpState();
118static jboolean RemovableOption(char *option);
119
120/* Maximum supported entries from jvm.cfg. */
121#define INIT_MAX_KNOWN_VMS 10
122
123/* Values for vmdesc.flag */
124enum vmdesc_flag {
125 VM_UNKNOWN = -1,
126 VM_KNOWN,
127 VM_ALIASED_TO,
128 VM_WARN,
129 VM_ERROR,
130 VM_IF_SERVER_CLASS,
131 VM_IGNORE
132};
133
134struct vmdesc {
135 char *name;
136 int flag;
137 char *alias;
138 char *server_class;
139};
140static struct vmdesc *knownVMs = NULL;
141static int knownVMsCount = 0;
142static int knownVMsLimit = 0;
143
144static void GrowKnownVMs();
145static int KnownVMIndex(const char* name);
146static void FreeKnownVMs();
147static void ShowSplashScreen();
148static jboolean IsWildCardEnabled();
149
150#define ARG_CHECK(n, f, a) if (n < 1) { \
ksrini0e817162008-08-26 10:21:20 -0700151 JLI_ReportErrorMessage(f, a); \
duke6e45e102007-12-01 00:00:00 +0000152 printUsage = JNI_TRUE; \
153 *pret = 1; \
154 return JNI_TRUE; \
155}
156
157/*
158 * Running Java code in primordial thread caused many problems. We will
159 * create a new thread to invoke JVM. See 6316197 for more information.
160 */
mchungea290e22011-01-21 09:43:57 -0800161static jlong threadStackSize = 0; /* stack size of the new thread */
ksrini6d575f82010-12-23 13:51:30 -0800162static jlong maxHeapSize = 0; /* max heap size */
163static jlong initialHeapSize = 0; /* inital heap size */
duke6e45e102007-12-01 00:00:00 +0000164
165int JNICALL JavaMain(void * args); /* entry point */
166
mchungea290e22011-01-21 09:43:57 -0800167enum LaunchMode { // cf. sun.launcher.LauncherHelper
168 LM_UNKNOWN = 0,
169 LM_CLASS,
170 LM_JAR
171};
172
173static const char *launchModeNames[]
174 = { "Unknown", "Main class", "JAR file" };
175
duke6e45e102007-12-01 00:00:00 +0000176typedef struct {
mchungea290e22011-01-21 09:43:57 -0800177 int argc;
178 char **argv;
179 int mode;
180 char *what;
181 InvocationFunctions ifn;
duke6e45e102007-12-01 00:00:00 +0000182} JavaMainArgs;
183
184/*
185 * Entry point.
186 */
187int
188JLI_Launch(int argc, char ** argv, /* main argc, argc */
189 int jargc, const char** jargv, /* java args */
190 int appclassc, const char** appclassv, /* app classpath */
191 const char* fullversion, /* full version defined */
192 const char* dotversion, /* dot version defined */
193 const char* pname, /* program name */
194 const char* lname, /* launcher name */
195 jboolean javaargs, /* JAVA_ARGS */
196 jboolean cpwildcard, /* classpath wildcard*/
197 jboolean javaw, /* windows-only javaw */
198 jint ergo /* ergonomics class policy */
199)
200{
mchungea290e22011-01-21 09:43:57 -0800201 int mode = LM_UNKNOWN;
202 char *what = NULL;
duke6e45e102007-12-01 00:00:00 +0000203 char *cpath = 0;
204 char *main_class = NULL;
205 int ret;
206 InvocationFunctions ifn;
207 jlong start, end;
ksrinie3ec45d2010-07-09 11:04:34 -0700208 char jvmpath[MAXPATHLEN];
209 char jrepath[MAXPATHLEN];
duke6e45e102007-12-01 00:00:00 +0000210
211 _fVersion = fullversion;
212 _dVersion = dotversion;
213 _launcher_name = lname;
214 _program_name = pname;
215 _is_java_args = javaargs;
216 _wc_enabled = cpwildcard;
217 _ergo_policy = ergo;
218
ksrini52cded22008-03-06 07:51:28 -0800219 InitLauncher(javaw);
duke6e45e102007-12-01 00:00:00 +0000220 DumpState();
221
222 /*
223 * Make sure the specified version of the JRE is running.
224 *
225 * There are three things to note about the SelectVersion() routine:
226 * 1) If the version running isn't correct, this routine doesn't
227 * return (either the correct version has been exec'd or an error
228 * was issued).
229 * 2) Argc and Argv in this scope are *not* altered by this routine.
230 * It is the responsibility of subsequent code to ignore the
231 * arguments handled by this routine.
232 * 3) As a side-effect, the variable "main_class" is guaranteed to
233 * be set (if it should ever be set). This isn't exactly the
234 * poster child for structured programming, but it is a small
235 * price to pay for not processing a jar file operand twice.
236 * (Note: This side effect has been disabled. See comment on
237 * bugid 5030265 below.)
238 */
239 SelectVersion(argc, argv, &main_class);
240
ksrinie3ec45d2010-07-09 11:04:34 -0700241 if (JLI_IsTraceLauncher()) {
242 int i;
243 printf("Command line args:\n");
244 for (i = 0; i < argc ; i++) {
245 printf("argv[%d] = %s\n", i, argv[i]);
246 }
ksrini9636e2e2011-02-03 15:41:23 -0800247 AddOption("-Dsun.java.launcher.diag=true", NULL);
ksrinie3ec45d2010-07-09 11:04:34 -0700248 }
duke6e45e102007-12-01 00:00:00 +0000249
250 CreateExecutionEnvironment(&argc, &argv,
251 jrepath, sizeof(jrepath),
ksrinie3ec45d2010-07-09 11:04:34 -0700252 jvmpath, sizeof(jvmpath));
duke6e45e102007-12-01 00:00:00 +0000253
254 ifn.CreateJavaVM = 0;
255 ifn.GetDefaultJavaVMInitArgs = 0;
256
257 if (JLI_IsTraceLauncher()) {
258 start = CounterGet();
259 }
260
261 if (!LoadJavaVM(jvmpath, &ifn)) {
262 return(6);
263 }
264
265 if (JLI_IsTraceLauncher()) {
266 end = CounterGet();
267 }
268
269 JLI_TraceLauncher("%ld micro seconds to LoadJavaVM\n",
270 (long)(jint)Counter2Micros(end-start));
271
272 ++argv;
273 --argc;
274
275 if (IsJavaArgs()) {
276 /* Preprocess wrapper arguments */
277 TranslateApplicationArgs(jargc, jargv, &argc, &argv);
278 if (!AddApplicationOptions(appclassc, appclassv)) {
279 return(1);
280 }
281 } else {
282 /* Set default CLASSPATH */
283 cpath = getenv("CLASSPATH");
284 if (cpath == NULL) {
285 cpath = ".";
286 }
287 SetClassPath(cpath);
288 }
289
mchungea290e22011-01-21 09:43:57 -0800290 /* Parse command line options; if the return value of
291 * ParseArguments is false, the program should exit.
duke6e45e102007-12-01 00:00:00 +0000292 */
mchungea290e22011-01-21 09:43:57 -0800293 if (!ParseArguments(&argc, &argv, &mode, &what, &ret, jrepath))
294 {
duke6e45e102007-12-01 00:00:00 +0000295 return(ret);
296 }
297
298 /* Override class path if -jar flag was specified */
mchungea290e22011-01-21 09:43:57 -0800299 if (mode == LM_JAR) {
300 SetClassPath(what); /* Override class path */
duke6e45e102007-12-01 00:00:00 +0000301 }
302
303 /* set the -Dsun.java.command pseudo property */
mchungea290e22011-01-21 09:43:57 -0800304 SetJavaCommandLineProp(what, argc, argv);
duke6e45e102007-12-01 00:00:00 +0000305
306 /* Set the -Dsun.java.launcher pseudo property */
307 SetJavaLauncherProp();
308
309 /* set the -Dsun.java.launcher.* platform properties */
310 SetJavaLauncherPlatformProps();
311
312 /* Show the splash screen if needed */
313 ShowSplashScreen();
314
mchungea290e22011-01-21 09:43:57 -0800315 return ContinueInNewThread(&ifn, argc, argv, mode, what, ret);
duke6e45e102007-12-01 00:00:00 +0000316
317}
ksrinie3ec45d2010-07-09 11:04:34 -0700318/*
319 * Always detach the main thread so that it appears to have ended when
320 * the application's main method exits. This will invoke the
321 * uncaught exception handler machinery if main threw an
322 * exception. An uncaught exception handler cannot change the
323 * launcher's return code except by calling System.exit.
324 *
325 * Wait for all non-daemon threads to end, then destroy the VM.
326 * This will actually create a trivial new Java waiter thread
327 * named "DestroyJavaVM", but this will be seen as a different
328 * thread from the one that executed main, even though they are
329 * the same C thread. This allows mainThread.join() and
330 * mainThread.isAlive() to work as expected.
331 */
332#define LEAVE() \
333 if ((*vm)->DetachCurrentThread(vm) != 0) { \
334 JLI_ReportErrorMessage(JVM_ERROR2); \
335 ret = 1; \
336 } \
337 (*vm)->DestroyJavaVM(vm); \
338 return ret \
duke6e45e102007-12-01 00:00:00 +0000339
ksrini20a64b22008-09-24 15:07:41 -0700340#define CHECK_EXCEPTION_NULL_LEAVE(e) \
341 if ((*env)->ExceptionOccurred(env)) { \
342 JLI_ReportExceptionDescription(env); \
ksrinie3ec45d2010-07-09 11:04:34 -0700343 LEAVE(); \
ksrini20a64b22008-09-24 15:07:41 -0700344 } \
345 if ((e) == NULL) { \
346 JLI_ReportErrorMessage(JNI_ERROR); \
ksrinie3ec45d2010-07-09 11:04:34 -0700347 LEAVE(); \
ksrini20a64b22008-09-24 15:07:41 -0700348 }
349
350#define CHECK_EXCEPTION_LEAVE(rv) \
351 if ((*env)->ExceptionOccurred(env)) { \
352 JLI_ReportExceptionDescription(env); \
353 ret = (rv); \
ksrinie3ec45d2010-07-09 11:04:34 -0700354 LEAVE(); \
ksrini20a64b22008-09-24 15:07:41 -0700355 }
duke6e45e102007-12-01 00:00:00 +0000356
357int JNICALL
358JavaMain(void * _args)
359{
360 JavaMainArgs *args = (JavaMainArgs *)_args;
361 int argc = args->argc;
362 char **argv = args->argv;
mchungea290e22011-01-21 09:43:57 -0800363 int mode = args->mode;
364 char *what = args->what;
duke6e45e102007-12-01 00:00:00 +0000365 InvocationFunctions ifn = args->ifn;
366
367 JavaVM *vm = 0;
368 JNIEnv *env = 0;
mchungea290e22011-01-21 09:43:57 -0800369 jclass mainClass = NULL;
duke6e45e102007-12-01 00:00:00 +0000370 jmethodID mainID;
371 jobjectArray mainArgs;
372 int ret = 0;
373 jlong start, end;
374
duke6e45e102007-12-01 00:00:00 +0000375 /* Initialize the virtual machine */
duke6e45e102007-12-01 00:00:00 +0000376 start = CounterGet();
377 if (!InitializeJVM(&vm, &env, &ifn)) {
ksrini0e817162008-08-26 10:21:20 -0700378 JLI_ReportErrorMessage(JVM_ERROR1);
duke6e45e102007-12-01 00:00:00 +0000379 exit(1);
380 }
381
382 if (printVersion || showVersion) {
383 PrintJavaVersion(env, showVersion);
ksrini20a64b22008-09-24 15:07:41 -0700384 CHECK_EXCEPTION_LEAVE(0);
duke6e45e102007-12-01 00:00:00 +0000385 if (printVersion) {
ksrinie3ec45d2010-07-09 11:04:34 -0700386 LEAVE();
duke6e45e102007-12-01 00:00:00 +0000387 }
388 }
389
ksrini8e20e1c2010-11-23 16:52:39 -0800390 if (showSettings != NULL) {
391 ShowSettings(env, showSettings);
ksrini6d575f82010-12-23 13:51:30 -0800392 CHECK_EXCEPTION_LEAVE(1);
ksrini8e20e1c2010-11-23 16:52:39 -0800393 }
duke6e45e102007-12-01 00:00:00 +0000394 /* If the user specified neither a class name nor a JAR file */
mchungea290e22011-01-21 09:43:57 -0800395 if (printXUsage || printUsage || what == 0 || mode == LM_UNKNOWN) {
duke6e45e102007-12-01 00:00:00 +0000396 PrintUsage(env, printXUsage);
ksrini20a64b22008-09-24 15:07:41 -0700397 CHECK_EXCEPTION_LEAVE(1);
ksrinie3ec45d2010-07-09 11:04:34 -0700398 LEAVE();
duke6e45e102007-12-01 00:00:00 +0000399 }
400
401 FreeKnownVMs(); /* after last possible PrintUsage() */
402
403 if (JLI_IsTraceLauncher()) {
404 end = CounterGet();
405 JLI_TraceLauncher("%ld micro seconds to InitializeJVM\n",
406 (long)(jint)Counter2Micros(end-start));
407 }
408
mchungea290e22011-01-21 09:43:57 -0800409 /* At this stage, argc/argv have the application's arguments */
duke6e45e102007-12-01 00:00:00 +0000410 if (JLI_IsTraceLauncher()){
411 int i;
mchungea290e22011-01-21 09:43:57 -0800412 printf("%s is '%s'\n", launchModeNames[mode], what);
413 printf("App's argc is %d\n", argc);
duke6e45e102007-12-01 00:00:00 +0000414 for (i=0; i < argc; i++) {
415 printf(" argv[%2d] = '%s'\n", i, argv[i]);
416 }
417 }
418
419 ret = 1;
420
421 /*
422 * Get the application's main class.
423 *
424 * See bugid 5030265. The Main-Class name has already been parsed
425 * from the manifest, but not parsed properly for UTF-8 support.
426 * Hence the code here ignores the value previously extracted and
427 * uses the pre-existing code to reextract the value. This is
428 * possibly an end of release cycle expedient. However, it has
429 * also been discovered that passing some character sets through
430 * the environment has "strange" behavior on some variants of
431 * Windows. Hence, maybe the manifest parsing code local to the
432 * launcher should never be enhanced.
433 *
434 * Hence, future work should either:
435 * 1) Correct the local parsing code and verify that the
436 * Main-Class attribute gets properly passed through
437 * all environments,
438 * 2) Remove the vestages of maintaining main_class through
439 * the environment (and remove these comments).
440 */
mchungea290e22011-01-21 09:43:57 -0800441 mainClass = LoadMainClass(env, mode, what);
ksrini20a64b22008-09-24 15:07:41 -0700442 CHECK_EXCEPTION_NULL_LEAVE(mainClass);
duke6e45e102007-12-01 00:00:00 +0000443
ksrini20a64b22008-09-24 15:07:41 -0700444 /*
445 * The LoadMainClass not only loads the main class, it will also ensure
446 * that the main method's signature is correct, therefore further checking
447 * is not required. The main method is invoked here so that extraneous java
448 * stacks are not in the application stack trace.
449 */
duke6e45e102007-12-01 00:00:00 +0000450 mainID = (*env)->GetStaticMethodID(env, mainClass, "main",
451 "([Ljava/lang/String;)V");
ksrini20a64b22008-09-24 15:07:41 -0700452 CHECK_EXCEPTION_NULL_LEAVE(mainID);
duke6e45e102007-12-01 00:00:00 +0000453
454 /* Build argument array */
455 mainArgs = NewPlatformStringArray(env, argv, argc);
ksrini20a64b22008-09-24 15:07:41 -0700456 CHECK_EXCEPTION_NULL_LEAVE(mainArgs);
duke6e45e102007-12-01 00:00:00 +0000457
458 /* Invoke main method. */
459 (*env)->CallStaticVoidMethod(env, mainClass, mainID, mainArgs);
460
461 /*
462 * The launcher's exit code (in the absence of calls to
463 * System.exit) will be non-zero if main threw an exception.
464 */
465 ret = (*env)->ExceptionOccurred(env) == NULL ? 0 : 1;
ksrinie3ec45d2010-07-09 11:04:34 -0700466 LEAVE();
duke6e45e102007-12-01 00:00:00 +0000467}
468
duke6e45e102007-12-01 00:00:00 +0000469/*
470 * Checks the command line options to find which JVM type was
471 * specified. If no command line option was given for the JVM type,
472 * the default type is used. The environment variable
473 * JDK_ALTERNATE_VM and the command line option -XXaltjvm= are also
474 * checked as ways of specifying which JVM type to invoke.
475 */
476char *
477CheckJvmType(int *pargc, char ***argv, jboolean speculative) {
478 int i, argi;
479 int argc;
480 char **newArgv;
481 int newArgvIdx = 0;
482 int isVMType;
483 int jvmidx = -1;
484 char *jvmtype = getenv("JDK_ALTERNATE_VM");
485
486 argc = *pargc;
487
488 /* To make things simpler we always copy the argv array */
489 newArgv = JLI_MemAlloc((argc + 1) * sizeof(char *));
490
491 /* The program name is always present */
492 newArgv[newArgvIdx++] = (*argv)[0];
493
494 for (argi = 1; argi < argc; argi++) {
495 char *arg = (*argv)[argi];
496 isVMType = 0;
497
498 if (IsJavaArgs()) {
499 if (arg[0] != '-') {
500 newArgv[newArgvIdx++] = arg;
501 continue;
502 }
503 } else {
504 if (JLI_StrCmp(arg, "-classpath") == 0 ||
505 JLI_StrCmp(arg, "-cp") == 0) {
506 newArgv[newArgvIdx++] = arg;
507 argi++;
508 if (argi < argc) {
509 newArgv[newArgvIdx++] = (*argv)[argi];
510 }
511 continue;
512 }
513 if (arg[0] != '-') break;
514 }
515
516 /* Did the user pass an explicit VM type? */
517 i = KnownVMIndex(arg);
518 if (i >= 0) {
519 jvmtype = knownVMs[jvmidx = i].name + 1; /* skip the - */
520 isVMType = 1;
521 *pargc = *pargc - 1;
522 }
523
524 /* Did the user specify an "alternate" VM? */
525 else if (JLI_StrCCmp(arg, "-XXaltjvm=") == 0 || JLI_StrCCmp(arg, "-J-XXaltjvm=") == 0) {
526 isVMType = 1;
527 jvmtype = arg+((arg[1]=='X')? 10 : 12);
528 jvmidx = -1;
529 }
530
531 if (!isVMType) {
532 newArgv[newArgvIdx++] = arg;
533 }
534 }
535
536 /*
537 * Finish copying the arguments if we aborted the above loop.
538 * NOTE that if we aborted via "break" then we did NOT copy the
539 * last argument above, and in addition argi will be less than
540 * argc.
541 */
542 while (argi < argc) {
543 newArgv[newArgvIdx++] = (*argv)[argi];
544 argi++;
545 }
546
547 /* argv is null-terminated */
548 newArgv[newArgvIdx] = 0;
549
550 /* Copy back argv */
551 *argv = newArgv;
552 *pargc = newArgvIdx;
553
554 /* use the default VM type if not specified (no alias processing) */
555 if (jvmtype == NULL) {
556 char* result = knownVMs[0].name+1;
557 /* Use a different VM type if we are on a server class machine? */
558 if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) &&
559 (ServerClassMachine() == JNI_TRUE)) {
560 result = knownVMs[0].server_class+1;
561 }
562 JLI_TraceLauncher("Default VM: %s\n", result);
563 return result;
564 }
565
566 /* if using an alternate VM, no alias processing */
567 if (jvmidx < 0)
568 return jvmtype;
569
570 /* Resolve aliases first */
571 {
572 int loopCount = 0;
573 while (knownVMs[jvmidx].flag == VM_ALIASED_TO) {
574 int nextIdx = KnownVMIndex(knownVMs[jvmidx].alias);
575
576 if (loopCount > knownVMsCount) {
577 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700578 JLI_ReportErrorMessage(CFG_ERROR1);
duke6e45e102007-12-01 00:00:00 +0000579 exit(1);
580 } else {
581 return "ERROR";
582 /* break; */
583 }
584 }
585
586 if (nextIdx < 0) {
587 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700588 JLI_ReportErrorMessage(CFG_ERROR2, knownVMs[jvmidx].alias);
duke6e45e102007-12-01 00:00:00 +0000589 exit(1);
590 } else {
591 return "ERROR";
592 }
593 }
594 jvmidx = nextIdx;
595 jvmtype = knownVMs[jvmidx].name+1;
596 loopCount++;
597 }
598 }
599
600 switch (knownVMs[jvmidx].flag) {
601 case VM_WARN:
602 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700603 JLI_ReportErrorMessage(CFG_WARN1, jvmtype, knownVMs[0].name + 1);
duke6e45e102007-12-01 00:00:00 +0000604 }
605 /* fall through */
606 case VM_IGNORE:
607 jvmtype = knownVMs[jvmidx=0].name + 1;
608 /* fall through */
609 case VM_KNOWN:
610 break;
611 case VM_ERROR:
612 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700613 JLI_ReportErrorMessage(CFG_ERROR3, jvmtype);
duke6e45e102007-12-01 00:00:00 +0000614 exit(1);
615 } else {
616 return "ERROR";
617 }
618 }
619
620 return jvmtype;
621}
622
623/* copied from HotSpot function "atomll()" */
624static int
ksrini8e20e1c2010-11-23 16:52:39 -0800625parse_size(const char *s, jlong *result) {
duke6e45e102007-12-01 00:00:00 +0000626 jlong n = 0;
627 int args_read = sscanf(s, jlong_format_specifier(), &n);
628 if (args_read != 1) {
629 return 0;
630 }
631 while (*s != '\0' && *s >= '0' && *s <= '9') {
632 s++;
633 }
634 // 4705540: illegal if more characters are found after the first non-digit
635 if (JLI_StrLen(s) > 1) {
636 return 0;
637 }
638 switch (*s) {
639 case 'T': case 't':
640 *result = n * GB * KB;
641 return 1;
642 case 'G': case 'g':
643 *result = n * GB;
644 return 1;
645 case 'M': case 'm':
646 *result = n * MB;
647 return 1;
648 case 'K': case 'k':
649 *result = n * KB;
650 return 1;
651 case '\0':
652 *result = n;
653 return 1;
654 default:
655 /* Create JVM with default stack and let VM handle malformed -Xss string*/
656 return 0;
657 }
658}
659
660/*
661 * Adds a new VM option with the given given name and value.
662 */
663void
664AddOption(char *str, void *info)
665{
666 /*
667 * Expand options array if needed to accommodate at least one more
668 * VM option.
669 */
670 if (numOptions >= maxOptions) {
671 if (options == 0) {
672 maxOptions = 4;
673 options = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
674 } else {
675 JavaVMOption *tmp;
676 maxOptions *= 2;
677 tmp = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
678 memcpy(tmp, options, numOptions * sizeof(JavaVMOption));
679 JLI_MemFree(options);
680 options = tmp;
681 }
682 }
683 options[numOptions].optionString = str;
684 options[numOptions++].extraInfo = info;
685
686 if (JLI_StrCCmp(str, "-Xss") == 0) {
ksrini8e20e1c2010-11-23 16:52:39 -0800687 jlong tmp;
688 if (parse_size(str + 4, &tmp)) {
689 threadStackSize = tmp;
690 }
691 }
692
693 if (JLI_StrCCmp(str, "-Xmx") == 0) {
694 jlong tmp;
695 if (parse_size(str + 4, &tmp)) {
ksrini6d575f82010-12-23 13:51:30 -0800696 maxHeapSize = tmp;
697 }
698 }
699
700 if (JLI_StrCCmp(str, "-Xms") == 0) {
701 jlong tmp;
702 if (parse_size(str + 4, &tmp)) {
mchungea290e22011-01-21 09:43:57 -0800703 initialHeapSize = tmp;
ksrini8e20e1c2010-11-23 16:52:39 -0800704 }
duke6e45e102007-12-01 00:00:00 +0000705 }
706}
707
708static void
709SetClassPath(const char *s)
710{
711 char *def;
martinc0ca3352009-06-22 16:41:27 -0700712 const char *orig = s;
713 static const char format[] = "-Djava.class.path=%s";
duke6e45e102007-12-01 00:00:00 +0000714 s = JLI_WildcardExpandClasspath(s);
martinc0ca3352009-06-22 16:41:27 -0700715 def = JLI_MemAlloc(sizeof(format)
716 - 2 /* strlen("%s") */
717 + JLI_StrLen(s));
718 sprintf(def, format, s);
duke6e45e102007-12-01 00:00:00 +0000719 AddOption(def, NULL);
martinc0ca3352009-06-22 16:41:27 -0700720 if (s != orig)
721 JLI_MemFree((char *) s);
duke6e45e102007-12-01 00:00:00 +0000722}
723
724/*
725 * The SelectVersion() routine ensures that an appropriate version of
726 * the JRE is running. The specification for the appropriate version
727 * is obtained from either the manifest of a jar file (preferred) or
728 * from command line options.
729 * The routine also parses splash screen command line options and
730 * passes on their values in private environment variables.
731 */
732static void
733SelectVersion(int argc, char **argv, char **main_class)
734{
735 char *arg;
736 char **new_argv;
737 char **new_argp;
738 char *operand;
739 char *version = NULL;
740 char *jre = NULL;
741 int jarflag = 0;
742 int headlessflag = 0;
743 int restrict_search = -1; /* -1 implies not known */
744 manifest_info info;
745 char env_entry[MAXNAMELEN + 24] = ENV_ENTRY "=";
746 char *splash_file_name = NULL;
747 char *splash_jar_name = NULL;
748 char *env_in;
749 int res;
750
751 /*
752 * If the version has already been selected, set *main_class
753 * with the value passed through the environment (if any) and
754 * simply return.
755 */
756 if ((env_in = getenv(ENV_ENTRY)) != NULL) {
757 if (*env_in != '\0')
758 *main_class = JLI_StringDup(env_in);
759 return;
760 }
761
762 /*
763 * Scan through the arguments for options relevant to multiple JRE
764 * support. For reference, the command line syntax is defined as:
765 *
766 * SYNOPSIS
767 * java [options] class [argument...]
768 *
769 * java [options] -jar file.jar [argument...]
770 *
771 * As the scan is performed, make a copy of the argument list with
772 * the version specification options (new to 1.5) removed, so that
773 * a version less than 1.5 can be exec'd.
774 *
775 * Note that due to the syntax of the native Windows interface
776 * CreateProcess(), processing similar to the following exists in
777 * the Windows platform specific routine ExecJRE (in java_md.c).
778 * Changes here should be reproduced there.
779 */
780 new_argv = JLI_MemAlloc((argc + 1) * sizeof(char*));
781 new_argv[0] = argv[0];
782 new_argp = &new_argv[1];
783 argc--;
784 argv++;
785 while ((arg = *argv) != 0 && *arg == '-') {
786 if (JLI_StrCCmp(arg, "-version:") == 0) {
787 version = arg + 9;
788 } else if (JLI_StrCmp(arg, "-jre-restrict-search") == 0) {
789 restrict_search = 1;
790 } else if (JLI_StrCmp(arg, "-no-jre-restrict-search") == 0) {
791 restrict_search = 0;
792 } else {
793 if (JLI_StrCmp(arg, "-jar") == 0)
794 jarflag = 1;
795 /* deal with "unfortunate" classpath syntax */
796 if ((JLI_StrCmp(arg, "-classpath") == 0 || JLI_StrCmp(arg, "-cp") == 0) &&
797 (argc >= 2)) {
798 *new_argp++ = arg;
799 argc--;
800 argv++;
801 arg = *argv;
802 }
803
804 /*
805 * Checking for headless toolkit option in the some way as AWT does:
806 * "true" means true and any other value means false
807 */
808 if (JLI_StrCmp(arg, "-Djava.awt.headless=true") == 0) {
809 headlessflag = 1;
810 } else if (JLI_StrCCmp(arg, "-Djava.awt.headless=") == 0) {
811 headlessflag = 0;
812 } else if (JLI_StrCCmp(arg, "-splash:") == 0) {
813 splash_file_name = arg+8;
814 }
815 *new_argp++ = arg;
816 }
817 argc--;
818 argv++;
819 }
820 if (argc <= 0) { /* No operand? Possibly legit with -[full]version */
821 operand = NULL;
822 } else {
823 argc--;
824 *new_argp++ = operand = *argv++;
825 }
826 while (argc-- > 0) /* Copy over [argument...] */
827 *new_argp++ = *argv++;
828 *new_argp = NULL;
829
830 /*
831 * If there is a jar file, read the manifest. If the jarfile can't be
832 * read, the manifest can't be read from the jar file, or the manifest
833 * is corrupt, issue the appropriate error messages and exit.
834 *
835 * Even if there isn't a jar file, construct a manifest_info structure
836 * containing the command line information. It's a convenient way to carry
837 * this data around.
838 */
839 if (jarflag && operand) {
840 if ((res = JLI_ParseManifest(operand, &info)) != 0) {
841 if (res == -1)
ksrini0e817162008-08-26 10:21:20 -0700842 JLI_ReportErrorMessage(JAR_ERROR2, operand);
duke6e45e102007-12-01 00:00:00 +0000843 else
ksrini0e817162008-08-26 10:21:20 -0700844 JLI_ReportErrorMessage(JAR_ERROR3, operand);
duke6e45e102007-12-01 00:00:00 +0000845 exit(1);
846 }
847
848 /*
849 * Command line splash screen option should have precedence
850 * over the manifest, so the manifest data is used only if
851 * splash_file_name has not been initialized above during command
852 * line parsing
853 */
854 if (!headlessflag && !splash_file_name && info.splashscreen_image_file_name) {
855 splash_file_name = info.splashscreen_image_file_name;
856 splash_jar_name = operand;
857 }
858 } else {
859 info.manifest_version = NULL;
860 info.main_class = NULL;
861 info.jre_version = NULL;
862 info.jre_restrict_search = 0;
863 }
864
865 /*
866 * Passing on splash screen info in environment variables
867 */
868 if (splash_file_name && !headlessflag) {
869 char* splash_file_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_FILE_ENV_ENTRY "=")+JLI_StrLen(splash_file_name)+1);
870 JLI_StrCpy(splash_file_entry, SPLASH_FILE_ENV_ENTRY "=");
871 JLI_StrCat(splash_file_entry, splash_file_name);
872 putenv(splash_file_entry);
873 }
874 if (splash_jar_name && !headlessflag) {
875 char* splash_jar_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_JAR_ENV_ENTRY "=")+JLI_StrLen(splash_jar_name)+1);
876 JLI_StrCpy(splash_jar_entry, SPLASH_JAR_ENV_ENTRY "=");
877 JLI_StrCat(splash_jar_entry, splash_jar_name);
878 putenv(splash_jar_entry);
879 }
880
881 /*
882 * The JRE-Version and JRE-Restrict-Search values (if any) from the
883 * manifest are overwritten by any specified on the command line.
884 */
885 if (version != NULL)
886 info.jre_version = version;
887 if (restrict_search != -1)
888 info.jre_restrict_search = restrict_search;
889
890 /*
891 * "Valid" returns (other than unrecoverable errors) follow. Set
892 * main_class as a side-effect of this routine.
893 */
894 if (info.main_class != NULL)
895 *main_class = JLI_StringDup(info.main_class);
896
897 /*
898 * If no version selection information is found either on the command
899 * line or in the manifest, simply return.
900 */
901 if (info.jre_version == NULL) {
902 JLI_FreeManifest();
903 JLI_MemFree(new_argv);
904 return;
905 }
906
907 /*
908 * Check for correct syntax of the version specification (JSR 56).
909 */
910 if (!JLI_ValidVersionString(info.jre_version)) {
ksrini0e817162008-08-26 10:21:20 -0700911 JLI_ReportErrorMessage(SPC_ERROR1, info.jre_version);
duke6e45e102007-12-01 00:00:00 +0000912 exit(1);
913 }
914
915 /*
916 * Find the appropriate JVM on the system. Just to be as forgiving as
917 * possible, if the standard algorithms don't locate an appropriate
918 * jre, check to see if the one running will satisfy the requirements.
919 * This can happen on systems which haven't been set-up for multiple
920 * JRE support.
921 */
922 jre = LocateJRE(&info);
923 JLI_TraceLauncher("JRE-Version = %s, JRE-Restrict-Search = %s Selected = %s\n",
924 (info.jre_version?info.jre_version:"null"),
925 (info.jre_restrict_search?"true":"false"), (jre?jre:"null"));
926
927 if (jre == NULL) {
928 if (JLI_AcceptableRelease(GetFullVersion(), info.jre_version)) {
929 JLI_FreeManifest();
930 JLI_MemFree(new_argv);
931 return;
932 } else {
ksrini0e817162008-08-26 10:21:20 -0700933 JLI_ReportErrorMessage(CFG_ERROR4, info.jre_version);
duke6e45e102007-12-01 00:00:00 +0000934 exit(1);
935 }
936 }
937
938 /*
939 * If I'm not the chosen one, exec the chosen one. Returning from
940 * ExecJRE indicates that I am indeed the chosen one.
941 *
942 * The private environment variable _JAVA_VERSION_SET is used to
943 * prevent the chosen one from re-reading the manifest file and
944 * using the values found within to override the (potential) command
945 * line flags stripped from argv (because the target may not
946 * understand them). Passing the MainClass value is an optimization
947 * to avoid locating, expanding and parsing the manifest extra
948 * times.
949 */
ksrini8760ca92008-09-04 09:43:32 -0700950 if (info.main_class != NULL) {
951 if (JLI_StrLen(info.main_class) <= MAXNAMELEN) {
952 (void)JLI_StrCat(env_entry, info.main_class);
953 } else {
asaha80ccd422009-04-16 21:08:04 -0700954 JLI_ReportErrorMessage(CLS_ERROR5, MAXNAMELEN);
ksrini8760ca92008-09-04 09:43:32 -0700955 exit(1);
956 }
957 }
duke6e45e102007-12-01 00:00:00 +0000958 (void)putenv(env_entry);
959 ExecJRE(jre, new_argv);
960 JLI_FreeManifest();
961 JLI_MemFree(new_argv);
962 return;
963}
964
965/*
966 * Parses command line arguments. Returns JNI_FALSE if launcher
967 * should exit without starting vm, returns JNI_TRUE if vm needs
mchungea290e22011-01-21 09:43:57 -0800968 * to be started to process given options. *pret (the launcher
duke6e45e102007-12-01 00:00:00 +0000969 * process return value) is set to 0 for a normal exit.
970 */
971static jboolean
mchungea290e22011-01-21 09:43:57 -0800972ParseArguments(int *pargc, char ***pargv,
973 int *pmode, char **pwhat,
974 int *pret, const char *jrepath)
duke6e45e102007-12-01 00:00:00 +0000975{
976 int argc = *pargc;
977 char **argv = *pargv;
mchungea290e22011-01-21 09:43:57 -0800978 int mode = LM_UNKNOWN;
duke6e45e102007-12-01 00:00:00 +0000979 char *arg;
980
981 *pret = 0;
982
983 while ((arg = *argv) != 0 && *arg == '-') {
984 argv++; --argc;
985 if (JLI_StrCmp(arg, "-classpath") == 0 || JLI_StrCmp(arg, "-cp") == 0) {
986 ARG_CHECK (argc, ARG_ERROR1, arg);
987 SetClassPath(*argv);
mchungea290e22011-01-21 09:43:57 -0800988 mode = LM_CLASS;
duke6e45e102007-12-01 00:00:00 +0000989 argv++; --argc;
990 } else if (JLI_StrCmp(arg, "-jar") == 0) {
991 ARG_CHECK (argc, ARG_ERROR2, arg);
mchungea290e22011-01-21 09:43:57 -0800992 mode = LM_JAR;
duke6e45e102007-12-01 00:00:00 +0000993 } else if (JLI_StrCmp(arg, "-help") == 0 ||
994 JLI_StrCmp(arg, "-h") == 0 ||
995 JLI_StrCmp(arg, "-?") == 0) {
996 printUsage = JNI_TRUE;
997 return JNI_TRUE;
998 } else if (JLI_StrCmp(arg, "-version") == 0) {
999 printVersion = JNI_TRUE;
1000 return JNI_TRUE;
1001 } else if (JLI_StrCmp(arg, "-showversion") == 0) {
1002 showVersion = JNI_TRUE;
1003 } else if (JLI_StrCmp(arg, "-X") == 0) {
1004 printXUsage = JNI_TRUE;
1005 return JNI_TRUE;
1006/*
ksrini8e20e1c2010-11-23 16:52:39 -08001007 * The following case checks for -XshowSettings OR -XshowSetting:SUBOPT.
1008 * In the latter case, any SUBOPT value not recognized will default to "all"
1009 */
1010 } else if (JLI_StrCmp(arg, "-XshowSettings") == 0 ||
1011 JLI_StrCCmp(arg, "-XshowSettings:") == 0) {
1012 showSettings = arg;
ksrini9636e2e2011-02-03 15:41:23 -08001013 } else if (JLI_StrCmp(arg, "-Xdiag") == 0) {
1014 AddOption("-Dsun.java.launcher.diag=true", NULL);
ksrini8e20e1c2010-11-23 16:52:39 -08001015/*
duke6e45e102007-12-01 00:00:00 +00001016 * The following case provide backward compatibility with old-style
1017 * command line options.
1018 */
1019 } else if (JLI_StrCmp(arg, "-fullversion") == 0) {
ksrini0e817162008-08-26 10:21:20 -07001020 JLI_ReportMessage("%s full version \"%s\"", _launcher_name, GetFullVersion());
duke6e45e102007-12-01 00:00:00 +00001021 return JNI_FALSE;
1022 } else if (JLI_StrCmp(arg, "-verbosegc") == 0) {
1023 AddOption("-verbose:gc", NULL);
1024 } else if (JLI_StrCmp(arg, "-t") == 0) {
1025 AddOption("-Xt", NULL);
1026 } else if (JLI_StrCmp(arg, "-tm") == 0) {
1027 AddOption("-Xtm", NULL);
1028 } else if (JLI_StrCmp(arg, "-debug") == 0) {
1029 AddOption("-Xdebug", NULL);
1030 } else if (JLI_StrCmp(arg, "-noclassgc") == 0) {
1031 AddOption("-Xnoclassgc", NULL);
1032 } else if (JLI_StrCmp(arg, "-Xfuture") == 0) {
1033 AddOption("-Xverify:all", NULL);
1034 } else if (JLI_StrCmp(arg, "-verify") == 0) {
1035 AddOption("-Xverify:all", NULL);
1036 } else if (JLI_StrCmp(arg, "-verifyremote") == 0) {
1037 AddOption("-Xverify:remote", NULL);
1038 } else if (JLI_StrCmp(arg, "-noverify") == 0) {
1039 AddOption("-Xverify:none", NULL);
1040 } else if (JLI_StrCCmp(arg, "-prof") == 0) {
1041 char *p = arg + 5;
1042 char *tmp = JLI_MemAlloc(JLI_StrLen(arg) + 50);
1043 if (*p) {
1044 sprintf(tmp, "-Xrunhprof:cpu=old,file=%s", p + 1);
1045 } else {
1046 sprintf(tmp, "-Xrunhprof:cpu=old,file=java.prof");
1047 }
1048 AddOption(tmp, NULL);
1049 } else if (JLI_StrCCmp(arg, "-ss") == 0 ||
1050 JLI_StrCCmp(arg, "-oss") == 0 ||
1051 JLI_StrCCmp(arg, "-ms") == 0 ||
1052 JLI_StrCCmp(arg, "-mx") == 0) {
1053 char *tmp = JLI_MemAlloc(JLI_StrLen(arg) + 6);
1054 sprintf(tmp, "-X%s", arg + 1); /* skip '-' */
1055 AddOption(tmp, NULL);
1056 } else if (JLI_StrCmp(arg, "-checksource") == 0 ||
1057 JLI_StrCmp(arg, "-cs") == 0 ||
1058 JLI_StrCmp(arg, "-noasyncgc") == 0) {
1059 /* No longer supported */
ksrini0e817162008-08-26 10:21:20 -07001060 JLI_ReportErrorMessage(ARG_WARN, arg);
duke6e45e102007-12-01 00:00:00 +00001061 } else if (JLI_StrCCmp(arg, "-version:") == 0 ||
1062 JLI_StrCmp(arg, "-no-jre-restrict-search") == 0 ||
1063 JLI_StrCmp(arg, "-jre-restrict-search") == 0 ||
1064 JLI_StrCCmp(arg, "-splash:") == 0) {
1065 ; /* Ignore machine independent options already handled */
1066 } else if (RemovableOption(arg) ) {
1067 ; /* Do not pass option to vm. */
1068 } else {
1069 AddOption(arg, NULL);
1070 }
1071 }
1072
1073 if (--argc >= 0) {
mchungea290e22011-01-21 09:43:57 -08001074 *pwhat = *argv++;
1075 }
1076
1077 if (*pwhat == NULL) {
1078 *pret = 1;
1079 } else if (mode == LM_UNKNOWN) {
1080 /* default to LM_CLASS if -jar and -cp option are
1081 * not specified */
1082 mode = LM_CLASS;
1083 }
1084
1085 if (argc >= 0) {
duke6e45e102007-12-01 00:00:00 +00001086 *pargc = argc;
1087 *pargv = argv;
1088 }
mchungea290e22011-01-21 09:43:57 -08001089
1090 *pmode = mode;
1091
duke6e45e102007-12-01 00:00:00 +00001092 return JNI_TRUE;
1093}
1094
1095/*
1096 * Initializes the Java Virtual Machine. Also frees options array when
1097 * finished.
1098 */
1099static jboolean
1100InitializeJVM(JavaVM **pvm, JNIEnv **penv, InvocationFunctions *ifn)
1101{
1102 JavaVMInitArgs args;
1103 jint r;
1104
1105 memset(&args, 0, sizeof(args));
1106 args.version = JNI_VERSION_1_2;
1107 args.nOptions = numOptions;
1108 args.options = options;
1109 args.ignoreUnrecognized = JNI_FALSE;
1110
1111 if (JLI_IsTraceLauncher()) {
1112 int i = 0;
1113 printf("JavaVM args:\n ");
1114 printf("version 0x%08lx, ", (long)args.version);
1115 printf("ignoreUnrecognized is %s, ",
1116 args.ignoreUnrecognized ? "JNI_TRUE" : "JNI_FALSE");
1117 printf("nOptions is %ld\n", (long)args.nOptions);
1118 for (i = 0; i < numOptions; i++)
1119 printf(" option[%2d] = '%s'\n",
1120 i, args.options[i].optionString);
1121 }
1122
1123 r = ifn->CreateJavaVM(pvm, (void **)penv, &args);
1124 JLI_MemFree(options);
1125 return r == JNI_OK;
1126}
1127
1128
1129#define NULL_CHECK0(e) if ((e) == 0) { \
ksrini0e817162008-08-26 10:21:20 -07001130 JLI_ReportErrorMessage(JNI_ERROR); \
duke6e45e102007-12-01 00:00:00 +00001131 return 0; \
1132 }
1133
1134#define NULL_CHECK(e) if ((e) == 0) { \
ksrini0e817162008-08-26 10:21:20 -07001135 JLI_ReportErrorMessage(JNI_ERROR); \
duke6e45e102007-12-01 00:00:00 +00001136 return; \
1137 }
1138
1139static jstring platformEncoding = NULL;
1140static jstring getPlatformEncoding(JNIEnv *env) {
1141 if (platformEncoding == NULL) {
1142 jstring propname = (*env)->NewStringUTF(env, "sun.jnu.encoding");
1143 if (propname) {
1144 jclass cls;
1145 jmethodID mid;
ksrini20a64b22008-09-24 15:07:41 -07001146 NULL_CHECK0 (cls = FindBootStrapClass(env, "java/lang/System"));
duke6e45e102007-12-01 00:00:00 +00001147 NULL_CHECK0 (mid = (*env)->GetStaticMethodID(
1148 env, cls,
1149 "getProperty",
1150 "(Ljava/lang/String;)Ljava/lang/String;"));
1151 platformEncoding = (*env)->CallStaticObjectMethod (
1152 env, cls, mid, propname);
1153 }
1154 }
1155 return platformEncoding;
1156}
1157
1158static jboolean isEncodingSupported(JNIEnv *env, jstring enc) {
1159 jclass cls;
1160 jmethodID mid;
ksrini20a64b22008-09-24 15:07:41 -07001161 NULL_CHECK0 (cls = FindBootStrapClass(env, "java/nio/charset/Charset"));
duke6e45e102007-12-01 00:00:00 +00001162 NULL_CHECK0 (mid = (*env)->GetStaticMethodID(
1163 env, cls,
1164 "isSupported",
1165 "(Ljava/lang/String;)Z"));
1166 return (*env)->CallStaticBooleanMethod(env, cls, mid, enc);
1167}
1168
1169/*
1170 * Returns a new Java string object for the specified platform string.
1171 */
1172static jstring
1173NewPlatformString(JNIEnv *env, char *s)
1174{
1175 int len = (int)JLI_StrLen(s);
1176 jclass cls;
1177 jmethodID mid;
1178 jbyteArray ary;
1179 jstring enc;
1180
1181 if (s == NULL)
1182 return 0;
1183 enc = getPlatformEncoding(env);
1184
1185 ary = (*env)->NewByteArray(env, len);
1186 if (ary != 0) {
1187 jstring str = 0;
1188 (*env)->SetByteArrayRegion(env, ary, 0, len, (jbyte *)s);
1189 if (!(*env)->ExceptionOccurred(env)) {
ksrini20a64b22008-09-24 15:07:41 -07001190 NULL_CHECK0(cls = FindBootStrapClass(env, "java/lang/String"));
duke6e45e102007-12-01 00:00:00 +00001191 if (isEncodingSupported(env, enc) == JNI_TRUE) {
duke6e45e102007-12-01 00:00:00 +00001192 NULL_CHECK0(mid = (*env)->GetMethodID(env, cls, "<init>",
1193 "([BLjava/lang/String;)V"));
1194 str = (*env)->NewObject(env, cls, mid, ary, enc);
1195 } else {
1196 /*If the encoding specified in sun.jnu.encoding is not
1197 endorsed by "Charset.isSupported" we have to fall back
1198 to use String(byte[]) explicitly here without specifying
1199 the encoding name, in which the StringCoding class will
1200 pickup the iso-8859-1 as the fallback converter for us.
1201 */
duke6e45e102007-12-01 00:00:00 +00001202 NULL_CHECK0(mid = (*env)->GetMethodID(env, cls, "<init>",
1203 "([B)V"));
1204 str = (*env)->NewObject(env, cls, mid, ary);
1205 }
1206 (*env)->DeleteLocalRef(env, ary);
1207 return str;
1208 }
1209 }
1210 return 0;
1211}
1212
1213/*
1214 * Returns a new array of Java string objects for the specified
1215 * array of platform strings.
1216 */
1217static jobjectArray
1218NewPlatformStringArray(JNIEnv *env, char **strv, int strc)
1219{
1220 jarray cls;
1221 jarray ary;
1222 int i;
1223
ksrini20a64b22008-09-24 15:07:41 -07001224 NULL_CHECK0(cls = FindBootStrapClass(env, "java/lang/String"));
duke6e45e102007-12-01 00:00:00 +00001225 NULL_CHECK0(ary = (*env)->NewObjectArray(env, strc, cls, 0));
1226 for (i = 0; i < strc; i++) {
1227 jstring str = NewPlatformString(env, *strv++);
1228 NULL_CHECK0(str);
1229 (*env)->SetObjectArrayElement(env, ary, i, str);
1230 (*env)->DeleteLocalRef(env, str);
1231 }
1232 return ary;
1233}
1234
1235/*
ksrini20a64b22008-09-24 15:07:41 -07001236 * Loads a class and verifies that the main class is present and it is ok to
1237 * call it for more details refer to the java implementation.
duke6e45e102007-12-01 00:00:00 +00001238 */
1239static jclass
mchungea290e22011-01-21 09:43:57 -08001240LoadMainClass(JNIEnv *env, int mode, char *name)
duke6e45e102007-12-01 00:00:00 +00001241{
duke6e45e102007-12-01 00:00:00 +00001242 jclass cls;
ksrini20a64b22008-09-24 15:07:41 -07001243 jmethodID mid;
1244 jstring str;
1245 jobject result;
duke6e45e102007-12-01 00:00:00 +00001246 jlong start, end;
1247
ksrini20a64b22008-09-24 15:07:41 -07001248 if (JLI_IsTraceLauncher()) {
duke6e45e102007-12-01 00:00:00 +00001249 start = CounterGet();
ksrini20a64b22008-09-24 15:07:41 -07001250 }
1251 NULL_CHECK0(cls = FindBootStrapClass(env, "sun/launcher/LauncherHelper"));
1252 NULL_CHECK0(mid = (*env)->GetStaticMethodID(env, cls, "checkAndLoadMain",
mchungea290e22011-01-21 09:43:57 -08001253 "(ZILjava/lang/String;)Ljava/lang/Class;"));
ksrini20a64b22008-09-24 15:07:41 -07001254 str = (*env)->NewStringUTF(env, name);
mchungea290e22011-01-21 09:43:57 -08001255 result = (*env)->CallStaticObjectMethod(env, cls, mid, JNI_TRUE, mode, str);
duke6e45e102007-12-01 00:00:00 +00001256
1257 if (JLI_IsTraceLauncher()) {
1258 end = CounterGet();
1259 printf("%ld micro seconds to load main class\n",
1260 (long)(jint)Counter2Micros(end-start));
1261 printf("----_JAVA_LAUNCHER_DEBUG----\n");
1262 }
1263
ksrini20a64b22008-09-24 15:07:41 -07001264 return (jclass)result;
duke6e45e102007-12-01 00:00:00 +00001265}
1266
duke6e45e102007-12-01 00:00:00 +00001267/*
1268 * For tools, convert command line args thus:
1269 * javac -cp foo:foo/"*" -J-ms32m ...
1270 * java -ms32m -cp JLI_WildcardExpandClasspath(foo:foo/"*") ...
1271 *
1272 * Takes 4 parameters, and returns the populated arguments
1273 */
1274static void
1275TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv)
1276{
1277 int argc = *pargc;
1278 char **argv = *pargv;
1279 int nargc = argc + jargc;
1280 char **nargv = JLI_MemAlloc((nargc + 1) * sizeof(char *));
1281 int i;
1282
1283 *pargc = nargc;
1284 *pargv = nargv;
1285
1286 /* Copy the VM arguments (i.e. prefixed with -J) */
1287 for (i = 0; i < jargc; i++) {
1288 const char *arg = jargv[i];
1289 if (arg[0] == '-' && arg[1] == 'J') {
1290 *nargv++ = ((arg + 2) == NULL) ? NULL : JLI_StringDup(arg + 2);
1291 }
1292 }
1293
1294 for (i = 0; i < argc; i++) {
1295 char *arg = argv[i];
1296 if (arg[0] == '-' && arg[1] == 'J') {
1297 if (arg[2] == '\0') {
ksrini0e817162008-08-26 10:21:20 -07001298 JLI_ReportErrorMessage(ARG_ERROR3);
duke6e45e102007-12-01 00:00:00 +00001299 exit(1);
1300 }
1301 *nargv++ = arg + 2;
1302 }
1303 }
1304
1305 /* Copy the rest of the arguments */
1306 for (i = 0; i < jargc ; i++) {
1307 const char *arg = jargv[i];
1308 if (arg[0] != '-' || arg[1] != 'J') {
1309 *nargv++ = (arg == NULL) ? NULL : JLI_StringDup(arg);
1310 }
1311 }
1312 for (i = 0; i < argc; i++) {
1313 char *arg = argv[i];
1314 if (arg[0] == '-') {
1315 if (arg[1] == 'J')
1316 continue;
1317 if (IsWildCardEnabled() && arg[1] == 'c'
1318 && (JLI_StrCmp(arg, "-cp") == 0 ||
1319 JLI_StrCmp(arg, "-classpath") == 0)
1320 && i < argc - 1) {
1321 *nargv++ = arg;
1322 *nargv++ = (char *) JLI_WildcardExpandClasspath(argv[i+1]);
1323 i++;
1324 continue;
1325 }
1326 }
1327 *nargv++ = arg;
1328 }
1329 *nargv = 0;
1330}
1331
1332/*
1333 * For our tools, we try to add 3 VM options:
1334 * -Denv.class.path=<envcp>
1335 * -Dapplication.home=<apphome>
1336 * -Djava.class.path=<appcp>
1337 * <envcp> is the user's setting of CLASSPATH -- for instance the user
1338 * tells javac where to find binary classes through this environment
1339 * variable. Notice that users will be able to compile against our
1340 * tools classes (sun.tools.javac.Main) only if they explicitly add
1341 * tools.jar to CLASSPATH.
1342 * <apphome> is the directory where the application is installed.
1343 * <appcp> is the classpath to where our apps' classfiles are.
1344 */
1345static jboolean
1346AddApplicationOptions(int cpathc, const char **cpathv)
1347{
1348 char *envcp, *appcp, *apphome;
1349 char home[MAXPATHLEN]; /* application home */
1350 char separator[] = { PATH_SEPARATOR, '\0' };
1351 int size, i;
1352
1353 {
1354 const char *s = getenv("CLASSPATH");
1355 if (s) {
1356 s = (char *) JLI_WildcardExpandClasspath(s);
1357 /* 40 for -Denv.class.path= */
1358 envcp = (char *)JLI_MemAlloc(JLI_StrLen(s) + 40);
1359 sprintf(envcp, "-Denv.class.path=%s", s);
1360 AddOption(envcp, NULL);
1361 }
1362 }
1363
1364 if (!GetApplicationHome(home, sizeof(home))) {
ksrini0e817162008-08-26 10:21:20 -07001365 JLI_ReportErrorMessage(CFG_ERROR5);
duke6e45e102007-12-01 00:00:00 +00001366 return JNI_FALSE;
1367 }
1368
1369 /* 40 for '-Dapplication.home=' */
1370 apphome = (char *)JLI_MemAlloc(JLI_StrLen(home) + 40);
1371 sprintf(apphome, "-Dapplication.home=%s", home);
1372 AddOption(apphome, NULL);
1373
1374 /* How big is the application's classpath? */
1375 size = 40; /* 40: "-Djava.class.path=" */
1376 for (i = 0; i < cpathc; i++) {
1377 size += (int)JLI_StrLen(home) + (int)JLI_StrLen(cpathv[i]) + 1; /* 1: separator */
1378 }
1379 appcp = (char *)JLI_MemAlloc(size + 1);
1380 JLI_StrCpy(appcp, "-Djava.class.path=");
1381 for (i = 0; i < cpathc; i++) {
1382 JLI_StrCat(appcp, home); /* c:\program files\myapp */
1383 JLI_StrCat(appcp, cpathv[i]); /* \lib\myapp.jar */
1384 JLI_StrCat(appcp, separator); /* ; */
1385 }
1386 appcp[JLI_StrLen(appcp)-1] = '\0'; /* remove trailing path separator */
1387 AddOption(appcp, NULL);
1388 return JNI_TRUE;
1389}
1390
1391/*
1392 * inject the -Dsun.java.command pseudo property into the args structure
1393 * this pseudo property is used in the HotSpot VM to expose the
1394 * Java class name and arguments to the main method to the VM. The
1395 * HotSpot VM uses this pseudo property to store the Java class name
1396 * (or jar file name) and the arguments to the class's main method
1397 * to the instrumentation memory region. The sun.java.command pseudo
1398 * property is not exported by HotSpot to the Java layer.
1399 */
1400void
mchungea290e22011-01-21 09:43:57 -08001401SetJavaCommandLineProp(char *what, int argc, char **argv)
duke6e45e102007-12-01 00:00:00 +00001402{
1403
1404 int i = 0;
1405 size_t len = 0;
1406 char* javaCommand = NULL;
1407 char* dashDstr = "-Dsun.java.command=";
1408
mchungea290e22011-01-21 09:43:57 -08001409 if (what == NULL) {
duke6e45e102007-12-01 00:00:00 +00001410 /* unexpected, one of these should be set. just return without
1411 * setting the property
1412 */
1413 return;
1414 }
1415
duke6e45e102007-12-01 00:00:00 +00001416 /* determine the amount of memory to allocate assuming
1417 * the individual components will be space separated
1418 */
mchungea290e22011-01-21 09:43:57 -08001419 len = JLI_StrLen(what);
duke6e45e102007-12-01 00:00:00 +00001420 for (i = 0; i < argc; i++) {
1421 len += JLI_StrLen(argv[i]) + 1;
1422 }
1423
1424 /* allocate the memory */
1425 javaCommand = (char*) JLI_MemAlloc(len + JLI_StrLen(dashDstr) + 1);
1426
1427 /* build the -D string */
1428 *javaCommand = '\0';
1429 JLI_StrCat(javaCommand, dashDstr);
mchungea290e22011-01-21 09:43:57 -08001430 JLI_StrCat(javaCommand, what);
duke6e45e102007-12-01 00:00:00 +00001431
1432 for (i = 0; i < argc; i++) {
1433 /* the components of the string are space separated. In
1434 * the case of embedded white space, the relationship of
1435 * the white space separated components to their true
1436 * positional arguments will be ambiguous. This issue may
1437 * be addressed in a future release.
1438 */
1439 JLI_StrCat(javaCommand, " ");
1440 JLI_StrCat(javaCommand, argv[i]);
1441 }
1442
1443 AddOption(javaCommand, NULL);
1444}
1445
1446/*
1447 * JVM would like to know if it's created by a standard Sun launcher, or by
1448 * user native application, the following property indicates the former.
1449 */
mchungea290e22011-01-21 09:43:57 -08001450void
1451SetJavaLauncherProp() {
duke6e45e102007-12-01 00:00:00 +00001452 AddOption("-Dsun.java.launcher=SUN_STANDARD", NULL);
1453}
1454
1455/*
1456 * Prints the version information from the java.version and other properties.
1457 */
1458static void
1459PrintJavaVersion(JNIEnv *env, jboolean extraLF)
1460{
1461 jclass ver;
1462 jmethodID print;
1463
ksrini20a64b22008-09-24 15:07:41 -07001464 NULL_CHECK(ver = FindBootStrapClass(env, "sun/misc/Version"));
duke6e45e102007-12-01 00:00:00 +00001465 NULL_CHECK(print = (*env)->GetStaticMethodID(env,
1466 ver,
1467 (extraLF == JNI_TRUE) ? "println" : "print",
1468 "()V"
1469 )
1470 );
1471
1472 (*env)->CallStaticVoidMethod(env, ver, print);
1473}
1474
1475/*
ksrini8e20e1c2010-11-23 16:52:39 -08001476 * Prints all the Java settings, see the java implementation for more details.
1477 */
1478static void
1479ShowSettings(JNIEnv *env, char *optString)
1480{
1481 jclass cls;
1482 jmethodID showSettingsID;
1483 jstring joptString;
1484 NULL_CHECK(cls = FindBootStrapClass(env, "sun/launcher/LauncherHelper"));
1485 NULL_CHECK(showSettingsID = (*env)->GetStaticMethodID(env, cls,
ksrini6d575f82010-12-23 13:51:30 -08001486 "showSettings", "(ZLjava/lang/String;JJJZ)V"));
ksrini8e20e1c2010-11-23 16:52:39 -08001487 joptString = (*env)->NewStringUTF(env, optString);
1488 (*env)->CallStaticVoidMethod(env, cls, showSettingsID,
1489 JNI_TRUE,
1490 joptString,
ksrini6d575f82010-12-23 13:51:30 -08001491 (jlong)initialHeapSize,
1492 (jlong)maxHeapSize,
ksrini8e20e1c2010-11-23 16:52:39 -08001493 (jlong)threadStackSize,
1494 ServerClassMachine());
1495}
1496
1497/*
ksrini20a64b22008-09-24 15:07:41 -07001498 * Prints default usage or the Xusage message, see sun.launcher.LauncherHelper.java
duke6e45e102007-12-01 00:00:00 +00001499 */
1500static void
1501PrintUsage(JNIEnv* env, jboolean doXUsage)
1502{
1503 jclass cls;
1504 jmethodID initHelp, vmSelect, vmSynonym, vmErgo, printHelp, printXUsageMessage;
1505 jstring jprogname, vm1, vm2;
1506 int i;
1507
ksrini20a64b22008-09-24 15:07:41 -07001508 NULL_CHECK(cls = FindBootStrapClass(env, "sun/launcher/LauncherHelper"));
duke6e45e102007-12-01 00:00:00 +00001509
1510
1511 if (doXUsage) {
1512 NULL_CHECK(printXUsageMessage = (*env)->GetStaticMethodID(env, cls,
1513 "printXUsageMessage", "(Z)V"));
1514 (*env)->CallStaticVoidMethod(env, cls, printXUsageMessage, JNI_TRUE);
1515 } else {
1516 NULL_CHECK(initHelp = (*env)->GetStaticMethodID(env, cls,
1517 "initHelpMessage", "(Ljava/lang/String;)V"));
1518
1519 NULL_CHECK(vmSelect = (*env)->GetStaticMethodID(env, cls, "appendVmSelectMessage",
1520 "(Ljava/lang/String;Ljava/lang/String;)V"));
1521
1522 NULL_CHECK(vmSynonym = (*env)->GetStaticMethodID(env, cls,
1523 "appendVmSynonymMessage",
1524 "(Ljava/lang/String;Ljava/lang/String;)V"));
1525 NULL_CHECK(vmErgo = (*env)->GetStaticMethodID(env, cls,
1526 "appendVmErgoMessage", "(ZLjava/lang/String;)V"));
1527
1528 NULL_CHECK(printHelp = (*env)->GetStaticMethodID(env, cls,
1529 "printHelpMessage", "(Z)V"));
1530
1531 jprogname = (*env)->NewStringUTF(env, _program_name);
1532
1533 /* Initialize the usage message with the usual preamble */
1534 (*env)->CallStaticVoidMethod(env, cls, initHelp, jprogname);
1535
1536
1537 /* Assemble the other variant part of the usage */
1538 if ((knownVMs[0].flag == VM_KNOWN) ||
1539 (knownVMs[0].flag == VM_IF_SERVER_CLASS)) {
1540 vm1 = (*env)->NewStringUTF(env, knownVMs[0].name);
1541 vm2 = (*env)->NewStringUTF(env, knownVMs[0].name+1);
1542 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1543 }
1544 for (i=1; i<knownVMsCount; i++) {
1545 if (knownVMs[i].flag == VM_KNOWN) {
1546 vm1 = (*env)->NewStringUTF(env, knownVMs[i].name);
1547 vm2 = (*env)->NewStringUTF(env, knownVMs[i].name+1);
1548 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1549 }
1550 }
1551 for (i=1; i<knownVMsCount; i++) {
1552 if (knownVMs[i].flag == VM_ALIASED_TO) {
1553 vm1 = (*env)->NewStringUTF(env, knownVMs[i].name);
1554 vm2 = (*env)->NewStringUTF(env, knownVMs[i].alias+1);
1555 (*env)->CallStaticVoidMethod(env, cls, vmSynonym, vm1, vm2);
1556 }
1557 }
1558
1559 /* The first known VM is the default */
1560 {
1561 jboolean isServerClassMachine = ServerClassMachine();
1562
1563 const char* defaultVM = knownVMs[0].name+1;
1564 if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) && isServerClassMachine) {
1565 defaultVM = knownVMs[0].server_class+1;
1566 }
1567
1568 vm1 = (*env)->NewStringUTF(env, defaultVM);
1569 (*env)->CallStaticVoidMethod(env, cls, vmErgo, isServerClassMachine, vm1);
1570 }
1571
1572 /* Complete the usage message and print to stderr*/
1573 (*env)->CallStaticVoidMethod(env, cls, printHelp, JNI_TRUE);
1574 }
1575 return;
1576}
1577
1578/*
1579 * Read the jvm.cfg file and fill the knownJVMs[] array.
1580 *
1581 * The functionality of the jvm.cfg file is subject to change without
1582 * notice and the mechanism will be removed in the future.
1583 *
1584 * The lexical structure of the jvm.cfg file is as follows:
1585 *
1586 * jvmcfg := { vmLine }
1587 * vmLine := knownLine
1588 * | aliasLine
1589 * | warnLine
1590 * | ignoreLine
1591 * | errorLine
1592 * | predicateLine
1593 * | commentLine
1594 * knownLine := flag "KNOWN" EOL
1595 * warnLine := flag "WARN" EOL
1596 * ignoreLine := flag "IGNORE" EOL
1597 * errorLine := flag "ERROR" EOL
1598 * aliasLine := flag "ALIASED_TO" flag EOL
1599 * predicateLine := flag "IF_SERVER_CLASS" flag EOL
1600 * commentLine := "#" text EOL
1601 * flag := "-" identifier
1602 *
1603 * The semantics are that when someone specifies a flag on the command line:
1604 * - if the flag appears on a knownLine, then the identifier is used as
1605 * the name of the directory holding the JVM library (the name of the JVM).
1606 * - if the flag appears as the first flag on an aliasLine, the identifier
1607 * of the second flag is used as the name of the JVM.
1608 * - if the flag appears on a warnLine, the identifier is used as the
1609 * name of the JVM, but a warning is generated.
1610 * - if the flag appears on an ignoreLine, the identifier is recognized as the
1611 * name of a JVM, but the identifier is ignored and the default vm used
1612 * - if the flag appears on an errorLine, an error is generated.
1613 * - if the flag appears as the first flag on a predicateLine, and
1614 * the machine on which you are running passes the predicate indicated,
1615 * then the identifier of the second flag is used as the name of the JVM,
1616 * otherwise the identifier of the first flag is used as the name of the JVM.
1617 * If no flag is given on the command line, the first vmLine of the jvm.cfg
1618 * file determines the name of the JVM.
1619 * PredicateLines are only interpreted on first vmLine of a jvm.cfg file,
1620 * since they only make sense if someone hasn't specified the name of the
1621 * JVM on the command line.
1622 *
1623 * The intent of the jvm.cfg file is to allow several JVM libraries to
1624 * be installed in different subdirectories of a single JRE installation,
1625 * for space-savings and convenience in testing.
1626 * The intent is explicitly not to provide a full aliasing or predicate
1627 * mechanism.
1628 */
1629jint
1630ReadKnownVMs(const char *jrepath, const char * arch, jboolean speculative)
1631{
1632 FILE *jvmCfg;
1633 char jvmCfgName[MAXPATHLEN+20];
1634 char line[MAXPATHLEN+20];
1635 int cnt = 0;
1636 int lineno = 0;
1637 jlong start, end;
1638 int vmType;
1639 char *tmpPtr;
1640 char *altVMName = NULL;
1641 char *serverClassVMName = NULL;
1642 static char *whiteSpace = " \t";
1643 if (JLI_IsTraceLauncher()) {
1644 start = CounterGet();
1645 }
ksrini01740ec2010-09-09 11:50:40 -07001646 JLI_Snprintf(jvmCfgName, sizeof(jvmCfgName), "%s%slib%s%s%sjvm.cfg",
1647 jrepath, FILESEP, FILESEP, arch, FILESEP);
duke6e45e102007-12-01 00:00:00 +00001648
1649 jvmCfg = fopen(jvmCfgName, "r");
1650 if (jvmCfg == NULL) {
1651 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -07001652 JLI_ReportErrorMessage(CFG_ERROR6, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001653 exit(1);
1654 } else {
1655 return -1;
1656 }
1657 }
1658 while (fgets(line, sizeof(line), jvmCfg) != NULL) {
1659 vmType = VM_UNKNOWN;
1660 lineno++;
1661 if (line[0] == '#')
1662 continue;
1663 if (line[0] != '-') {
ksrini0e817162008-08-26 10:21:20 -07001664 JLI_ReportErrorMessage(CFG_WARN2, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001665 }
1666 if (cnt >= knownVMsLimit) {
1667 GrowKnownVMs(cnt);
1668 }
1669 line[JLI_StrLen(line)-1] = '\0'; /* remove trailing newline */
1670 tmpPtr = line + JLI_StrCSpn(line, whiteSpace);
1671 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001672 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001673 } else {
1674 /* Null-terminate this string for JLI_StringDup below */
1675 *tmpPtr++ = 0;
1676 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1677 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001678 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001679 } else {
1680 if (!JLI_StrCCmp(tmpPtr, "KNOWN")) {
1681 vmType = VM_KNOWN;
1682 } else if (!JLI_StrCCmp(tmpPtr, "ALIASED_TO")) {
1683 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1684 if (*tmpPtr != 0) {
1685 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1686 }
1687 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001688 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001689 } else {
1690 /* Null terminate altVMName */
1691 altVMName = tmpPtr;
1692 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1693 *tmpPtr = 0;
1694 vmType = VM_ALIASED_TO;
1695 }
1696 } else if (!JLI_StrCCmp(tmpPtr, "WARN")) {
1697 vmType = VM_WARN;
1698 } else if (!JLI_StrCCmp(tmpPtr, "IGNORE")) {
1699 vmType = VM_IGNORE;
1700 } else if (!JLI_StrCCmp(tmpPtr, "ERROR")) {
1701 vmType = VM_ERROR;
1702 } else if (!JLI_StrCCmp(tmpPtr, "IF_SERVER_CLASS")) {
1703 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1704 if (*tmpPtr != 0) {
1705 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1706 }
1707 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001708 JLI_ReportErrorMessage(CFG_WARN4, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001709 } else {
1710 /* Null terminate server class VM name */
1711 serverClassVMName = tmpPtr;
1712 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1713 *tmpPtr = 0;
1714 vmType = VM_IF_SERVER_CLASS;
1715 }
1716 } else {
ksrini0e817162008-08-26 10:21:20 -07001717 JLI_ReportErrorMessage(CFG_WARN5, lineno, &jvmCfgName[0]);
duke6e45e102007-12-01 00:00:00 +00001718 vmType = VM_KNOWN;
1719 }
1720 }
1721 }
1722
1723 JLI_TraceLauncher("jvm.cfg[%d] = ->%s<-\n", cnt, line);
1724 if (vmType != VM_UNKNOWN) {
1725 knownVMs[cnt].name = JLI_StringDup(line);
1726 knownVMs[cnt].flag = vmType;
1727 switch (vmType) {
1728 default:
1729 break;
1730 case VM_ALIASED_TO:
1731 knownVMs[cnt].alias = JLI_StringDup(altVMName);
1732 JLI_TraceLauncher(" name: %s vmType: %s alias: %s\n",
1733 knownVMs[cnt].name, "VM_ALIASED_TO", knownVMs[cnt].alias);
1734 break;
1735 case VM_IF_SERVER_CLASS:
1736 knownVMs[cnt].server_class = JLI_StringDup(serverClassVMName);
1737 JLI_TraceLauncher(" name: %s vmType: %s server_class: %s\n",
1738 knownVMs[cnt].name, "VM_IF_SERVER_CLASS", knownVMs[cnt].server_class);
1739 break;
1740 }
1741 cnt++;
1742 }
1743 }
1744 fclose(jvmCfg);
1745 knownVMsCount = cnt;
1746
1747 if (JLI_IsTraceLauncher()) {
1748 end = CounterGet();
1749 printf("%ld micro seconds to parse jvm.cfg\n",
1750 (long)(jint)Counter2Micros(end-start));
1751 }
1752
1753 return cnt;
1754}
1755
1756
1757static void
1758GrowKnownVMs(int minimum)
1759{
1760 struct vmdesc* newKnownVMs;
1761 int newMax;
1762
1763 newMax = (knownVMsLimit == 0 ? INIT_MAX_KNOWN_VMS : (2 * knownVMsLimit));
1764 if (newMax <= minimum) {
1765 newMax = minimum;
1766 }
1767 newKnownVMs = (struct vmdesc*) JLI_MemAlloc(newMax * sizeof(struct vmdesc));
1768 if (knownVMs != NULL) {
1769 memcpy(newKnownVMs, knownVMs, knownVMsLimit * sizeof(struct vmdesc));
1770 }
1771 JLI_MemFree(knownVMs);
1772 knownVMs = newKnownVMs;
1773 knownVMsLimit = newMax;
1774}
1775
1776
1777/* Returns index of VM or -1 if not found */
1778static int
1779KnownVMIndex(const char* name)
1780{
1781 int i;
1782 if (JLI_StrCCmp(name, "-J") == 0) name += 2;
1783 for (i = 0; i < knownVMsCount; i++) {
1784 if (!JLI_StrCmp(name, knownVMs[i].name)) {
1785 return i;
1786 }
1787 }
1788 return -1;
1789}
1790
1791static void
1792FreeKnownVMs()
1793{
1794 int i;
1795 for (i = 0; i < knownVMsCount; i++) {
1796 JLI_MemFree(knownVMs[i].name);
1797 knownVMs[i].name = NULL;
1798 }
1799 JLI_MemFree(knownVMs);
1800}
1801
1802
1803/*
1804 * Displays the splash screen according to the jar file name
1805 * and image file names stored in environment variables
1806 */
1807static void
1808ShowSplashScreen()
1809{
1810 const char *jar_name = getenv(SPLASH_JAR_ENV_ENTRY);
1811 const char *file_name = getenv(SPLASH_FILE_ENV_ENTRY);
1812 int data_size;
1813 void *image_data;
1814 if (jar_name) {
1815 image_data = JLI_JarUnpackFile(jar_name, file_name, &data_size);
1816 if (image_data) {
1817 DoSplashInit();
1818 DoSplashLoadMemory(image_data, data_size);
1819 JLI_MemFree(image_data);
1820 }
1821 } else if (file_name) {
1822 DoSplashInit();
1823 DoSplashLoadFile(file_name);
1824 } else {
1825 return;
1826 }
1827 DoSplashSetFileJarName(file_name, jar_name);
1828
1829 /*
1830 * Done with all command line processing and potential re-execs so
1831 * clean up the environment.
1832 */
1833 (void)UnsetEnv(ENV_ENTRY);
1834 (void)UnsetEnv(SPLASH_FILE_ENV_ENTRY);
1835 (void)UnsetEnv(SPLASH_JAR_ENV_ENTRY);
1836
1837 JLI_MemFree(splash_jar_entry);
1838 JLI_MemFree(splash_file_entry);
1839
1840}
1841
1842const char*
1843GetDotVersion()
1844{
1845 return _dVersion;
1846}
1847
1848const char*
1849GetFullVersion()
1850{
1851 return _fVersion;
1852}
1853
1854const char*
1855GetProgramName()
1856{
1857 return _program_name;
1858}
1859
1860const char*
1861GetLauncherName()
1862{
1863 return _launcher_name;
1864}
1865
1866jint
1867GetErgoPolicy()
1868{
1869 return _ergo_policy;
1870}
1871
1872jboolean
1873IsJavaArgs()
1874{
1875 return _is_java_args;
1876}
1877
1878static jboolean
1879IsWildCardEnabled()
1880{
1881 return _wc_enabled;
1882}
1883
1884static int
mchungea290e22011-01-21 09:43:57 -08001885ContinueInNewThread(InvocationFunctions* ifn, int argc, char **argv,
1886 int mode, char *what, int ret)
duke6e45e102007-12-01 00:00:00 +00001887{
1888
1889 /*
1890 * If user doesn't specify stack size, check if VM has a preference.
1891 * Note that HotSpot no longer supports JNI_VERSION_1_1 but it will
1892 * return its default stack size through the init args structure.
1893 */
1894 if (threadStackSize == 0) {
1895 struct JDK1_1InitArgs args1_1;
1896 memset((void*)&args1_1, 0, sizeof(args1_1));
1897 args1_1.version = JNI_VERSION_1_1;
1898 ifn->GetDefaultJavaVMInitArgs(&args1_1); /* ignore return value */
1899 if (args1_1.javaStackSize > 0) {
1900 threadStackSize = args1_1.javaStackSize;
1901 }
1902 }
1903
1904 { /* Create a new thread to create JVM and invoke main method */
1905 JavaMainArgs args;
1906 int rslt;
1907
1908 args.argc = argc;
1909 args.argv = argv;
mchungea290e22011-01-21 09:43:57 -08001910 args.mode = mode;
1911 args.what = what;
duke6e45e102007-12-01 00:00:00 +00001912 args.ifn = *ifn;
1913
1914 rslt = ContinueInNewThread0(JavaMain, threadStackSize, (void*)&args);
1915 /* If the caller has deemed there is an error we
1916 * simply return that, otherwise we return the value of
1917 * the callee
1918 */
1919 return (ret != 0) ? ret : rslt;
1920 }
1921}
1922
1923static void
1924DumpState()
1925{
1926 if (!JLI_IsTraceLauncher()) return ;
1927 printf("Launcher state:\n");
1928 printf("\tdebug:%s\n", (JLI_IsTraceLauncher() == JNI_TRUE) ? "on" : "off");
1929 printf("\tjavargs:%s\n", (_is_java_args == JNI_TRUE) ? "on" : "off");
1930 printf("\tprogram name:%s\n", GetProgramName());
1931 printf("\tlauncher name:%s\n", GetLauncherName());
1932 printf("\tjavaw:%s\n", (IsJavaw() == JNI_TRUE) ? "on" : "off");
1933 printf("\tfullversion:%s\n", GetFullVersion());
1934 printf("\tdotversion:%s\n", GetDotVersion());
1935 printf("\tergo_policy:");
1936 switch(GetErgoPolicy()) {
1937 case NEVER_SERVER_CLASS:
1938 printf("NEVER_ACT_AS_A_SERVER_CLASS_MACHINE\n");
1939 break;
1940 case ALWAYS_SERVER_CLASS:
1941 printf("ALWAYS_ACT_AS_A_SERVER_CLASS_MACHINE\n");
1942 break;
1943 default:
1944 printf("DEFAULT_ERGONOMICS_POLICY\n");
1945 }
1946}
1947
1948/*
1949 * Return JNI_TRUE for an option string that has no effect but should
1950 * _not_ be passed on to the vm; return JNI_FALSE otherwise. On
1951 * Solaris SPARC, this screening needs to be done if:
ksrini11e7f1b2009-11-20 11:01:32 -08001952 * -d32 or -d64 is passed to a binary with an unmatched data model
1953 * (the exec in CreateExecutionEnvironment removes -d<n> options and points the
1954 * exec to the proper binary). In the case of when the data model and the
1955 * requested version is matched, an exec would not occur, and these options
1956 * were erroneously passed to the vm.
duke6e45e102007-12-01 00:00:00 +00001957 */
1958jboolean
1959RemovableOption(char * option)
1960{
1961 /*
1962 * Unconditionally remove both -d32 and -d64 options since only
1963 * the last such options has an effect; e.g.
1964 * java -d32 -d64 -d32 -version
1965 * is equivalent to
1966 * java -d32 -version
1967 */
1968
1969 if( (JLI_StrCCmp(option, "-d32") == 0 ) ||
1970 (JLI_StrCCmp(option, "-d64") == 0 ) )
1971 return JNI_TRUE;
1972 else
1973 return JNI_FALSE;
1974}
1975
1976/*
1977 * A utility procedure to always print to stderr
1978 */
1979void
ksrini0e817162008-08-26 10:21:20 -07001980JLI_ReportMessage(const char* fmt, ...)
duke6e45e102007-12-01 00:00:00 +00001981{
1982 va_list vl;
1983 va_start(vl, fmt);
1984 vfprintf(stderr, fmt, vl);
1985 fprintf(stderr, "\n");
1986 va_end(vl);
1987}