blob: c2a6ab99249a05a53e044c0bf7a1a337950e8915 [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 }
247 }
duke6e45e102007-12-01 00:00:00 +0000248
249 CreateExecutionEnvironment(&argc, &argv,
250 jrepath, sizeof(jrepath),
ksrinie3ec45d2010-07-09 11:04:34 -0700251 jvmpath, sizeof(jvmpath));
duke6e45e102007-12-01 00:00:00 +0000252
253 ifn.CreateJavaVM = 0;
254 ifn.GetDefaultJavaVMInitArgs = 0;
255
256 if (JLI_IsTraceLauncher()) {
257 start = CounterGet();
258 }
259
260 if (!LoadJavaVM(jvmpath, &ifn)) {
261 return(6);
262 }
263
264 if (JLI_IsTraceLauncher()) {
265 end = CounterGet();
266 }
267
268 JLI_TraceLauncher("%ld micro seconds to LoadJavaVM\n",
269 (long)(jint)Counter2Micros(end-start));
270
271 ++argv;
272 --argc;
273
274 if (IsJavaArgs()) {
275 /* Preprocess wrapper arguments */
276 TranslateApplicationArgs(jargc, jargv, &argc, &argv);
277 if (!AddApplicationOptions(appclassc, appclassv)) {
278 return(1);
279 }
280 } else {
281 /* Set default CLASSPATH */
282 cpath = getenv("CLASSPATH");
283 if (cpath == NULL) {
284 cpath = ".";
285 }
286 SetClassPath(cpath);
287 }
288
mchungea290e22011-01-21 09:43:57 -0800289 /* Parse command line options; if the return value of
290 * ParseArguments is false, the program should exit.
duke6e45e102007-12-01 00:00:00 +0000291 */
mchungea290e22011-01-21 09:43:57 -0800292 if (!ParseArguments(&argc, &argv, &mode, &what, &ret, jrepath))
293 {
duke6e45e102007-12-01 00:00:00 +0000294 return(ret);
295 }
296
297 /* Override class path if -jar flag was specified */
mchungea290e22011-01-21 09:43:57 -0800298 if (mode == LM_JAR) {
299 SetClassPath(what); /* Override class path */
duke6e45e102007-12-01 00:00:00 +0000300 }
301
302 /* set the -Dsun.java.command pseudo property */
mchungea290e22011-01-21 09:43:57 -0800303 SetJavaCommandLineProp(what, argc, argv);
duke6e45e102007-12-01 00:00:00 +0000304
305 /* Set the -Dsun.java.launcher pseudo property */
306 SetJavaLauncherProp();
307
308 /* set the -Dsun.java.launcher.* platform properties */
309 SetJavaLauncherPlatformProps();
310
311 /* Show the splash screen if needed */
312 ShowSplashScreen();
313
mchungea290e22011-01-21 09:43:57 -0800314 return ContinueInNewThread(&ifn, argc, argv, mode, what, ret);
duke6e45e102007-12-01 00:00:00 +0000315
316}
ksrinie3ec45d2010-07-09 11:04:34 -0700317/*
318 * Always detach the main thread so that it appears to have ended when
319 * the application's main method exits. This will invoke the
320 * uncaught exception handler machinery if main threw an
321 * exception. An uncaught exception handler cannot change the
322 * launcher's return code except by calling System.exit.
323 *
324 * Wait for all non-daemon threads to end, then destroy the VM.
325 * This will actually create a trivial new Java waiter thread
326 * named "DestroyJavaVM", but this will be seen as a different
327 * thread from the one that executed main, even though they are
328 * the same C thread. This allows mainThread.join() and
329 * mainThread.isAlive() to work as expected.
330 */
331#define LEAVE() \
332 if ((*vm)->DetachCurrentThread(vm) != 0) { \
333 JLI_ReportErrorMessage(JVM_ERROR2); \
334 ret = 1; \
335 } \
336 (*vm)->DestroyJavaVM(vm); \
337 return ret \
duke6e45e102007-12-01 00:00:00 +0000338
ksrini20a64b22008-09-24 15:07:41 -0700339#define CHECK_EXCEPTION_NULL_LEAVE(e) \
340 if ((*env)->ExceptionOccurred(env)) { \
341 JLI_ReportExceptionDescription(env); \
ksrinie3ec45d2010-07-09 11:04:34 -0700342 LEAVE(); \
ksrini20a64b22008-09-24 15:07:41 -0700343 } \
344 if ((e) == NULL) { \
345 JLI_ReportErrorMessage(JNI_ERROR); \
ksrinie3ec45d2010-07-09 11:04:34 -0700346 LEAVE(); \
ksrini20a64b22008-09-24 15:07:41 -0700347 }
348
349#define CHECK_EXCEPTION_LEAVE(rv) \
350 if ((*env)->ExceptionOccurred(env)) { \
351 JLI_ReportExceptionDescription(env); \
352 ret = (rv); \
ksrinie3ec45d2010-07-09 11:04:34 -0700353 LEAVE(); \
ksrini20a64b22008-09-24 15:07:41 -0700354 }
duke6e45e102007-12-01 00:00:00 +0000355
356int JNICALL
357JavaMain(void * _args)
358{
359 JavaMainArgs *args = (JavaMainArgs *)_args;
360 int argc = args->argc;
361 char **argv = args->argv;
mchungea290e22011-01-21 09:43:57 -0800362 int mode = args->mode;
363 char *what = args->what;
duke6e45e102007-12-01 00:00:00 +0000364 InvocationFunctions ifn = args->ifn;
365
366 JavaVM *vm = 0;
367 JNIEnv *env = 0;
mchungea290e22011-01-21 09:43:57 -0800368 jclass mainClass = NULL;
duke6e45e102007-12-01 00:00:00 +0000369 jmethodID mainID;
370 jobjectArray mainArgs;
371 int ret = 0;
372 jlong start, end;
373
duke6e45e102007-12-01 00:00:00 +0000374 /* Initialize the virtual machine */
duke6e45e102007-12-01 00:00:00 +0000375 start = CounterGet();
376 if (!InitializeJVM(&vm, &env, &ifn)) {
ksrini0e817162008-08-26 10:21:20 -0700377 JLI_ReportErrorMessage(JVM_ERROR1);
duke6e45e102007-12-01 00:00:00 +0000378 exit(1);
379 }
380
381 if (printVersion || showVersion) {
382 PrintJavaVersion(env, showVersion);
ksrini20a64b22008-09-24 15:07:41 -0700383 CHECK_EXCEPTION_LEAVE(0);
duke6e45e102007-12-01 00:00:00 +0000384 if (printVersion) {
ksrinie3ec45d2010-07-09 11:04:34 -0700385 LEAVE();
duke6e45e102007-12-01 00:00:00 +0000386 }
387 }
388
ksrini8e20e1c2010-11-23 16:52:39 -0800389 if (showSettings != NULL) {
390 ShowSettings(env, showSettings);
ksrini6d575f82010-12-23 13:51:30 -0800391 CHECK_EXCEPTION_LEAVE(1);
ksrini8e20e1c2010-11-23 16:52:39 -0800392 }
duke6e45e102007-12-01 00:00:00 +0000393 /* If the user specified neither a class name nor a JAR file */
mchungea290e22011-01-21 09:43:57 -0800394 if (printXUsage || printUsage || what == 0 || mode == LM_UNKNOWN) {
duke6e45e102007-12-01 00:00:00 +0000395 PrintUsage(env, printXUsage);
ksrini20a64b22008-09-24 15:07:41 -0700396 CHECK_EXCEPTION_LEAVE(1);
ksrinie3ec45d2010-07-09 11:04:34 -0700397 LEAVE();
duke6e45e102007-12-01 00:00:00 +0000398 }
399
400 FreeKnownVMs(); /* after last possible PrintUsage() */
401
402 if (JLI_IsTraceLauncher()) {
403 end = CounterGet();
404 JLI_TraceLauncher("%ld micro seconds to InitializeJVM\n",
405 (long)(jint)Counter2Micros(end-start));
406 }
407
mchungea290e22011-01-21 09:43:57 -0800408 /* At this stage, argc/argv have the application's arguments */
duke6e45e102007-12-01 00:00:00 +0000409 if (JLI_IsTraceLauncher()){
410 int i;
mchungea290e22011-01-21 09:43:57 -0800411 printf("%s is '%s'\n", launchModeNames[mode], what);
412 printf("App's argc is %d\n", argc);
duke6e45e102007-12-01 00:00:00 +0000413 for (i=0; i < argc; i++) {
414 printf(" argv[%2d] = '%s'\n", i, argv[i]);
415 }
416 }
417
418 ret = 1;
419
420 /*
421 * Get the application's main class.
422 *
423 * See bugid 5030265. The Main-Class name has already been parsed
424 * from the manifest, but not parsed properly for UTF-8 support.
425 * Hence the code here ignores the value previously extracted and
426 * uses the pre-existing code to reextract the value. This is
427 * possibly an end of release cycle expedient. However, it has
428 * also been discovered that passing some character sets through
429 * the environment has "strange" behavior on some variants of
430 * Windows. Hence, maybe the manifest parsing code local to the
431 * launcher should never be enhanced.
432 *
433 * Hence, future work should either:
434 * 1) Correct the local parsing code and verify that the
435 * Main-Class attribute gets properly passed through
436 * all environments,
437 * 2) Remove the vestages of maintaining main_class through
438 * the environment (and remove these comments).
439 */
mchungea290e22011-01-21 09:43:57 -0800440 mainClass = LoadMainClass(env, mode, what);
ksrini20a64b22008-09-24 15:07:41 -0700441 CHECK_EXCEPTION_NULL_LEAVE(mainClass);
duke6e45e102007-12-01 00:00:00 +0000442
ksrini20a64b22008-09-24 15:07:41 -0700443 /*
444 * The LoadMainClass not only loads the main class, it will also ensure
445 * that the main method's signature is correct, therefore further checking
446 * is not required. The main method is invoked here so that extraneous java
447 * stacks are not in the application stack trace.
448 */
duke6e45e102007-12-01 00:00:00 +0000449 mainID = (*env)->GetStaticMethodID(env, mainClass, "main",
450 "([Ljava/lang/String;)V");
ksrini20a64b22008-09-24 15:07:41 -0700451 CHECK_EXCEPTION_NULL_LEAVE(mainID);
duke6e45e102007-12-01 00:00:00 +0000452
453 /* Build argument array */
454 mainArgs = NewPlatformStringArray(env, argv, argc);
ksrini20a64b22008-09-24 15:07:41 -0700455 CHECK_EXCEPTION_NULL_LEAVE(mainArgs);
duke6e45e102007-12-01 00:00:00 +0000456
457 /* Invoke main method. */
458 (*env)->CallStaticVoidMethod(env, mainClass, mainID, mainArgs);
459
460 /*
461 * The launcher's exit code (in the absence of calls to
462 * System.exit) will be non-zero if main threw an exception.
463 */
464 ret = (*env)->ExceptionOccurred(env) == NULL ? 0 : 1;
ksrinie3ec45d2010-07-09 11:04:34 -0700465 LEAVE();
duke6e45e102007-12-01 00:00:00 +0000466}
467
duke6e45e102007-12-01 00:00:00 +0000468/*
469 * Checks the command line options to find which JVM type was
470 * specified. If no command line option was given for the JVM type,
471 * the default type is used. The environment variable
472 * JDK_ALTERNATE_VM and the command line option -XXaltjvm= are also
473 * checked as ways of specifying which JVM type to invoke.
474 */
475char *
476CheckJvmType(int *pargc, char ***argv, jboolean speculative) {
477 int i, argi;
478 int argc;
479 char **newArgv;
480 int newArgvIdx = 0;
481 int isVMType;
482 int jvmidx = -1;
483 char *jvmtype = getenv("JDK_ALTERNATE_VM");
484
485 argc = *pargc;
486
487 /* To make things simpler we always copy the argv array */
488 newArgv = JLI_MemAlloc((argc + 1) * sizeof(char *));
489
490 /* The program name is always present */
491 newArgv[newArgvIdx++] = (*argv)[0];
492
493 for (argi = 1; argi < argc; argi++) {
494 char *arg = (*argv)[argi];
495 isVMType = 0;
496
497 if (IsJavaArgs()) {
498 if (arg[0] != '-') {
499 newArgv[newArgvIdx++] = arg;
500 continue;
501 }
502 } else {
503 if (JLI_StrCmp(arg, "-classpath") == 0 ||
504 JLI_StrCmp(arg, "-cp") == 0) {
505 newArgv[newArgvIdx++] = arg;
506 argi++;
507 if (argi < argc) {
508 newArgv[newArgvIdx++] = (*argv)[argi];
509 }
510 continue;
511 }
512 if (arg[0] != '-') break;
513 }
514
515 /* Did the user pass an explicit VM type? */
516 i = KnownVMIndex(arg);
517 if (i >= 0) {
518 jvmtype = knownVMs[jvmidx = i].name + 1; /* skip the - */
519 isVMType = 1;
520 *pargc = *pargc - 1;
521 }
522
523 /* Did the user specify an "alternate" VM? */
524 else if (JLI_StrCCmp(arg, "-XXaltjvm=") == 0 || JLI_StrCCmp(arg, "-J-XXaltjvm=") == 0) {
525 isVMType = 1;
526 jvmtype = arg+((arg[1]=='X')? 10 : 12);
527 jvmidx = -1;
528 }
529
530 if (!isVMType) {
531 newArgv[newArgvIdx++] = arg;
532 }
533 }
534
535 /*
536 * Finish copying the arguments if we aborted the above loop.
537 * NOTE that if we aborted via "break" then we did NOT copy the
538 * last argument above, and in addition argi will be less than
539 * argc.
540 */
541 while (argi < argc) {
542 newArgv[newArgvIdx++] = (*argv)[argi];
543 argi++;
544 }
545
546 /* argv is null-terminated */
547 newArgv[newArgvIdx] = 0;
548
549 /* Copy back argv */
550 *argv = newArgv;
551 *pargc = newArgvIdx;
552
553 /* use the default VM type if not specified (no alias processing) */
554 if (jvmtype == NULL) {
555 char* result = knownVMs[0].name+1;
556 /* Use a different VM type if we are on a server class machine? */
557 if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) &&
558 (ServerClassMachine() == JNI_TRUE)) {
559 result = knownVMs[0].server_class+1;
560 }
561 JLI_TraceLauncher("Default VM: %s\n", result);
562 return result;
563 }
564
565 /* if using an alternate VM, no alias processing */
566 if (jvmidx < 0)
567 return jvmtype;
568
569 /* Resolve aliases first */
570 {
571 int loopCount = 0;
572 while (knownVMs[jvmidx].flag == VM_ALIASED_TO) {
573 int nextIdx = KnownVMIndex(knownVMs[jvmidx].alias);
574
575 if (loopCount > knownVMsCount) {
576 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700577 JLI_ReportErrorMessage(CFG_ERROR1);
duke6e45e102007-12-01 00:00:00 +0000578 exit(1);
579 } else {
580 return "ERROR";
581 /* break; */
582 }
583 }
584
585 if (nextIdx < 0) {
586 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700587 JLI_ReportErrorMessage(CFG_ERROR2, knownVMs[jvmidx].alias);
duke6e45e102007-12-01 00:00:00 +0000588 exit(1);
589 } else {
590 return "ERROR";
591 }
592 }
593 jvmidx = nextIdx;
594 jvmtype = knownVMs[jvmidx].name+1;
595 loopCount++;
596 }
597 }
598
599 switch (knownVMs[jvmidx].flag) {
600 case VM_WARN:
601 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700602 JLI_ReportErrorMessage(CFG_WARN1, jvmtype, knownVMs[0].name + 1);
duke6e45e102007-12-01 00:00:00 +0000603 }
604 /* fall through */
605 case VM_IGNORE:
606 jvmtype = knownVMs[jvmidx=0].name + 1;
607 /* fall through */
608 case VM_KNOWN:
609 break;
610 case VM_ERROR:
611 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700612 JLI_ReportErrorMessage(CFG_ERROR3, jvmtype);
duke6e45e102007-12-01 00:00:00 +0000613 exit(1);
614 } else {
615 return "ERROR";
616 }
617 }
618
619 return jvmtype;
620}
621
622/* copied from HotSpot function "atomll()" */
623static int
ksrini8e20e1c2010-11-23 16:52:39 -0800624parse_size(const char *s, jlong *result) {
duke6e45e102007-12-01 00:00:00 +0000625 jlong n = 0;
626 int args_read = sscanf(s, jlong_format_specifier(), &n);
627 if (args_read != 1) {
628 return 0;
629 }
630 while (*s != '\0' && *s >= '0' && *s <= '9') {
631 s++;
632 }
633 // 4705540: illegal if more characters are found after the first non-digit
634 if (JLI_StrLen(s) > 1) {
635 return 0;
636 }
637 switch (*s) {
638 case 'T': case 't':
639 *result = n * GB * KB;
640 return 1;
641 case 'G': case 'g':
642 *result = n * GB;
643 return 1;
644 case 'M': case 'm':
645 *result = n * MB;
646 return 1;
647 case 'K': case 'k':
648 *result = n * KB;
649 return 1;
650 case '\0':
651 *result = n;
652 return 1;
653 default:
654 /* Create JVM with default stack and let VM handle malformed -Xss string*/
655 return 0;
656 }
657}
658
659/*
660 * Adds a new VM option with the given given name and value.
661 */
662void
663AddOption(char *str, void *info)
664{
665 /*
666 * Expand options array if needed to accommodate at least one more
667 * VM option.
668 */
669 if (numOptions >= maxOptions) {
670 if (options == 0) {
671 maxOptions = 4;
672 options = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
673 } else {
674 JavaVMOption *tmp;
675 maxOptions *= 2;
676 tmp = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
677 memcpy(tmp, options, numOptions * sizeof(JavaVMOption));
678 JLI_MemFree(options);
679 options = tmp;
680 }
681 }
682 options[numOptions].optionString = str;
683 options[numOptions++].extraInfo = info;
684
685 if (JLI_StrCCmp(str, "-Xss") == 0) {
ksrini8e20e1c2010-11-23 16:52:39 -0800686 jlong tmp;
687 if (parse_size(str + 4, &tmp)) {
688 threadStackSize = tmp;
689 }
690 }
691
692 if (JLI_StrCCmp(str, "-Xmx") == 0) {
693 jlong tmp;
694 if (parse_size(str + 4, &tmp)) {
ksrini6d575f82010-12-23 13:51:30 -0800695 maxHeapSize = tmp;
696 }
697 }
698
699 if (JLI_StrCCmp(str, "-Xms") == 0) {
700 jlong tmp;
701 if (parse_size(str + 4, &tmp)) {
mchungea290e22011-01-21 09:43:57 -0800702 initialHeapSize = tmp;
ksrini8e20e1c2010-11-23 16:52:39 -0800703 }
duke6e45e102007-12-01 00:00:00 +0000704 }
705}
706
707static void
708SetClassPath(const char *s)
709{
710 char *def;
martinc0ca3352009-06-22 16:41:27 -0700711 const char *orig = s;
712 static const char format[] = "-Djava.class.path=%s";
duke6e45e102007-12-01 00:00:00 +0000713 s = JLI_WildcardExpandClasspath(s);
martinc0ca3352009-06-22 16:41:27 -0700714 def = JLI_MemAlloc(sizeof(format)
715 - 2 /* strlen("%s") */
716 + JLI_StrLen(s));
717 sprintf(def, format, s);
duke6e45e102007-12-01 00:00:00 +0000718 AddOption(def, NULL);
martinc0ca3352009-06-22 16:41:27 -0700719 if (s != orig)
720 JLI_MemFree((char *) s);
duke6e45e102007-12-01 00:00:00 +0000721}
722
723/*
724 * The SelectVersion() routine ensures that an appropriate version of
725 * the JRE is running. The specification for the appropriate version
726 * is obtained from either the manifest of a jar file (preferred) or
727 * from command line options.
728 * The routine also parses splash screen command line options and
729 * passes on their values in private environment variables.
730 */
731static void
732SelectVersion(int argc, char **argv, char **main_class)
733{
734 char *arg;
735 char **new_argv;
736 char **new_argp;
737 char *operand;
738 char *version = NULL;
739 char *jre = NULL;
740 int jarflag = 0;
741 int headlessflag = 0;
742 int restrict_search = -1; /* -1 implies not known */
743 manifest_info info;
744 char env_entry[MAXNAMELEN + 24] = ENV_ENTRY "=";
745 char *splash_file_name = NULL;
746 char *splash_jar_name = NULL;
747 char *env_in;
748 int res;
749
750 /*
751 * If the version has already been selected, set *main_class
752 * with the value passed through the environment (if any) and
753 * simply return.
754 */
755 if ((env_in = getenv(ENV_ENTRY)) != NULL) {
756 if (*env_in != '\0')
757 *main_class = JLI_StringDup(env_in);
758 return;
759 }
760
761 /*
762 * Scan through the arguments for options relevant to multiple JRE
763 * support. For reference, the command line syntax is defined as:
764 *
765 * SYNOPSIS
766 * java [options] class [argument...]
767 *
768 * java [options] -jar file.jar [argument...]
769 *
770 * As the scan is performed, make a copy of the argument list with
771 * the version specification options (new to 1.5) removed, so that
772 * a version less than 1.5 can be exec'd.
773 *
774 * Note that due to the syntax of the native Windows interface
775 * CreateProcess(), processing similar to the following exists in
776 * the Windows platform specific routine ExecJRE (in java_md.c).
777 * Changes here should be reproduced there.
778 */
779 new_argv = JLI_MemAlloc((argc + 1) * sizeof(char*));
780 new_argv[0] = argv[0];
781 new_argp = &new_argv[1];
782 argc--;
783 argv++;
784 while ((arg = *argv) != 0 && *arg == '-') {
785 if (JLI_StrCCmp(arg, "-version:") == 0) {
786 version = arg + 9;
787 } else if (JLI_StrCmp(arg, "-jre-restrict-search") == 0) {
788 restrict_search = 1;
789 } else if (JLI_StrCmp(arg, "-no-jre-restrict-search") == 0) {
790 restrict_search = 0;
791 } else {
792 if (JLI_StrCmp(arg, "-jar") == 0)
793 jarflag = 1;
794 /* deal with "unfortunate" classpath syntax */
795 if ((JLI_StrCmp(arg, "-classpath") == 0 || JLI_StrCmp(arg, "-cp") == 0) &&
796 (argc >= 2)) {
797 *new_argp++ = arg;
798 argc--;
799 argv++;
800 arg = *argv;
801 }
802
803 /*
804 * Checking for headless toolkit option in the some way as AWT does:
805 * "true" means true and any other value means false
806 */
807 if (JLI_StrCmp(arg, "-Djava.awt.headless=true") == 0) {
808 headlessflag = 1;
809 } else if (JLI_StrCCmp(arg, "-Djava.awt.headless=") == 0) {
810 headlessflag = 0;
811 } else if (JLI_StrCCmp(arg, "-splash:") == 0) {
812 splash_file_name = arg+8;
813 }
814 *new_argp++ = arg;
815 }
816 argc--;
817 argv++;
818 }
819 if (argc <= 0) { /* No operand? Possibly legit with -[full]version */
820 operand = NULL;
821 } else {
822 argc--;
823 *new_argp++ = operand = *argv++;
824 }
825 while (argc-- > 0) /* Copy over [argument...] */
826 *new_argp++ = *argv++;
827 *new_argp = NULL;
828
829 /*
830 * If there is a jar file, read the manifest. If the jarfile can't be
831 * read, the manifest can't be read from the jar file, or the manifest
832 * is corrupt, issue the appropriate error messages and exit.
833 *
834 * Even if there isn't a jar file, construct a manifest_info structure
835 * containing the command line information. It's a convenient way to carry
836 * this data around.
837 */
838 if (jarflag && operand) {
839 if ((res = JLI_ParseManifest(operand, &info)) != 0) {
840 if (res == -1)
ksrini0e817162008-08-26 10:21:20 -0700841 JLI_ReportErrorMessage(JAR_ERROR2, operand);
duke6e45e102007-12-01 00:00:00 +0000842 else
ksrini0e817162008-08-26 10:21:20 -0700843 JLI_ReportErrorMessage(JAR_ERROR3, operand);
duke6e45e102007-12-01 00:00:00 +0000844 exit(1);
845 }
846
847 /*
848 * Command line splash screen option should have precedence
849 * over the manifest, so the manifest data is used only if
850 * splash_file_name has not been initialized above during command
851 * line parsing
852 */
853 if (!headlessflag && !splash_file_name && info.splashscreen_image_file_name) {
854 splash_file_name = info.splashscreen_image_file_name;
855 splash_jar_name = operand;
856 }
857 } else {
858 info.manifest_version = NULL;
859 info.main_class = NULL;
860 info.jre_version = NULL;
861 info.jre_restrict_search = 0;
862 }
863
864 /*
865 * Passing on splash screen info in environment variables
866 */
867 if (splash_file_name && !headlessflag) {
868 char* splash_file_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_FILE_ENV_ENTRY "=")+JLI_StrLen(splash_file_name)+1);
869 JLI_StrCpy(splash_file_entry, SPLASH_FILE_ENV_ENTRY "=");
870 JLI_StrCat(splash_file_entry, splash_file_name);
871 putenv(splash_file_entry);
872 }
873 if (splash_jar_name && !headlessflag) {
874 char* splash_jar_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_JAR_ENV_ENTRY "=")+JLI_StrLen(splash_jar_name)+1);
875 JLI_StrCpy(splash_jar_entry, SPLASH_JAR_ENV_ENTRY "=");
876 JLI_StrCat(splash_jar_entry, splash_jar_name);
877 putenv(splash_jar_entry);
878 }
879
880 /*
881 * The JRE-Version and JRE-Restrict-Search values (if any) from the
882 * manifest are overwritten by any specified on the command line.
883 */
884 if (version != NULL)
885 info.jre_version = version;
886 if (restrict_search != -1)
887 info.jre_restrict_search = restrict_search;
888
889 /*
890 * "Valid" returns (other than unrecoverable errors) follow. Set
891 * main_class as a side-effect of this routine.
892 */
893 if (info.main_class != NULL)
894 *main_class = JLI_StringDup(info.main_class);
895
896 /*
897 * If no version selection information is found either on the command
898 * line or in the manifest, simply return.
899 */
900 if (info.jre_version == NULL) {
901 JLI_FreeManifest();
902 JLI_MemFree(new_argv);
903 return;
904 }
905
906 /*
907 * Check for correct syntax of the version specification (JSR 56).
908 */
909 if (!JLI_ValidVersionString(info.jre_version)) {
ksrini0e817162008-08-26 10:21:20 -0700910 JLI_ReportErrorMessage(SPC_ERROR1, info.jre_version);
duke6e45e102007-12-01 00:00:00 +0000911 exit(1);
912 }
913
914 /*
915 * Find the appropriate JVM on the system. Just to be as forgiving as
916 * possible, if the standard algorithms don't locate an appropriate
917 * jre, check to see if the one running will satisfy the requirements.
918 * This can happen on systems which haven't been set-up for multiple
919 * JRE support.
920 */
921 jre = LocateJRE(&info);
922 JLI_TraceLauncher("JRE-Version = %s, JRE-Restrict-Search = %s Selected = %s\n",
923 (info.jre_version?info.jre_version:"null"),
924 (info.jre_restrict_search?"true":"false"), (jre?jre:"null"));
925
926 if (jre == NULL) {
927 if (JLI_AcceptableRelease(GetFullVersion(), info.jre_version)) {
928 JLI_FreeManifest();
929 JLI_MemFree(new_argv);
930 return;
931 } else {
ksrini0e817162008-08-26 10:21:20 -0700932 JLI_ReportErrorMessage(CFG_ERROR4, info.jre_version);
duke6e45e102007-12-01 00:00:00 +0000933 exit(1);
934 }
935 }
936
937 /*
938 * If I'm not the chosen one, exec the chosen one. Returning from
939 * ExecJRE indicates that I am indeed the chosen one.
940 *
941 * The private environment variable _JAVA_VERSION_SET is used to
942 * prevent the chosen one from re-reading the manifest file and
943 * using the values found within to override the (potential) command
944 * line flags stripped from argv (because the target may not
945 * understand them). Passing the MainClass value is an optimization
946 * to avoid locating, expanding and parsing the manifest extra
947 * times.
948 */
ksrini8760ca92008-09-04 09:43:32 -0700949 if (info.main_class != NULL) {
950 if (JLI_StrLen(info.main_class) <= MAXNAMELEN) {
951 (void)JLI_StrCat(env_entry, info.main_class);
952 } else {
asaha80ccd422009-04-16 21:08:04 -0700953 JLI_ReportErrorMessage(CLS_ERROR5, MAXNAMELEN);
ksrini8760ca92008-09-04 09:43:32 -0700954 exit(1);
955 }
956 }
duke6e45e102007-12-01 00:00:00 +0000957 (void)putenv(env_entry);
958 ExecJRE(jre, new_argv);
959 JLI_FreeManifest();
960 JLI_MemFree(new_argv);
961 return;
962}
963
964/*
965 * Parses command line arguments. Returns JNI_FALSE if launcher
966 * should exit without starting vm, returns JNI_TRUE if vm needs
mchungea290e22011-01-21 09:43:57 -0800967 * to be started to process given options. *pret (the launcher
duke6e45e102007-12-01 00:00:00 +0000968 * process return value) is set to 0 for a normal exit.
969 */
970static jboolean
mchungea290e22011-01-21 09:43:57 -0800971ParseArguments(int *pargc, char ***pargv,
972 int *pmode, char **pwhat,
973 int *pret, const char *jrepath)
duke6e45e102007-12-01 00:00:00 +0000974{
975 int argc = *pargc;
976 char **argv = *pargv;
mchungea290e22011-01-21 09:43:57 -0800977 int mode = LM_UNKNOWN;
duke6e45e102007-12-01 00:00:00 +0000978 char *arg;
979
980 *pret = 0;
981
982 while ((arg = *argv) != 0 && *arg == '-') {
983 argv++; --argc;
984 if (JLI_StrCmp(arg, "-classpath") == 0 || JLI_StrCmp(arg, "-cp") == 0) {
985 ARG_CHECK (argc, ARG_ERROR1, arg);
986 SetClassPath(*argv);
mchungea290e22011-01-21 09:43:57 -0800987 mode = LM_CLASS;
duke6e45e102007-12-01 00:00:00 +0000988 argv++; --argc;
989 } else if (JLI_StrCmp(arg, "-jar") == 0) {
990 ARG_CHECK (argc, ARG_ERROR2, arg);
mchungea290e22011-01-21 09:43:57 -0800991 mode = LM_JAR;
duke6e45e102007-12-01 00:00:00 +0000992 } else if (JLI_StrCmp(arg, "-help") == 0 ||
993 JLI_StrCmp(arg, "-h") == 0 ||
994 JLI_StrCmp(arg, "-?") == 0) {
995 printUsage = JNI_TRUE;
996 return JNI_TRUE;
997 } else if (JLI_StrCmp(arg, "-version") == 0) {
998 printVersion = JNI_TRUE;
999 return JNI_TRUE;
1000 } else if (JLI_StrCmp(arg, "-showversion") == 0) {
1001 showVersion = JNI_TRUE;
1002 } else if (JLI_StrCmp(arg, "-X") == 0) {
1003 printXUsage = JNI_TRUE;
1004 return JNI_TRUE;
1005/*
ksrini8e20e1c2010-11-23 16:52:39 -08001006 * The following case checks for -XshowSettings OR -XshowSetting:SUBOPT.
1007 * In the latter case, any SUBOPT value not recognized will default to "all"
1008 */
1009 } else if (JLI_StrCmp(arg, "-XshowSettings") == 0 ||
1010 JLI_StrCCmp(arg, "-XshowSettings:") == 0) {
1011 showSettings = arg;
1012/*
duke6e45e102007-12-01 00:00:00 +00001013 * The following case provide backward compatibility with old-style
1014 * command line options.
1015 */
1016 } else if (JLI_StrCmp(arg, "-fullversion") == 0) {
ksrini0e817162008-08-26 10:21:20 -07001017 JLI_ReportMessage("%s full version \"%s\"", _launcher_name, GetFullVersion());
duke6e45e102007-12-01 00:00:00 +00001018 return JNI_FALSE;
1019 } else if (JLI_StrCmp(arg, "-verbosegc") == 0) {
1020 AddOption("-verbose:gc", NULL);
1021 } else if (JLI_StrCmp(arg, "-t") == 0) {
1022 AddOption("-Xt", NULL);
1023 } else if (JLI_StrCmp(arg, "-tm") == 0) {
1024 AddOption("-Xtm", NULL);
1025 } else if (JLI_StrCmp(arg, "-debug") == 0) {
1026 AddOption("-Xdebug", NULL);
1027 } else if (JLI_StrCmp(arg, "-noclassgc") == 0) {
1028 AddOption("-Xnoclassgc", NULL);
1029 } else if (JLI_StrCmp(arg, "-Xfuture") == 0) {
1030 AddOption("-Xverify:all", NULL);
1031 } else if (JLI_StrCmp(arg, "-verify") == 0) {
1032 AddOption("-Xverify:all", NULL);
1033 } else if (JLI_StrCmp(arg, "-verifyremote") == 0) {
1034 AddOption("-Xverify:remote", NULL);
1035 } else if (JLI_StrCmp(arg, "-noverify") == 0) {
1036 AddOption("-Xverify:none", NULL);
1037 } else if (JLI_StrCCmp(arg, "-prof") == 0) {
1038 char *p = arg + 5;
1039 char *tmp = JLI_MemAlloc(JLI_StrLen(arg) + 50);
1040 if (*p) {
1041 sprintf(tmp, "-Xrunhprof:cpu=old,file=%s", p + 1);
1042 } else {
1043 sprintf(tmp, "-Xrunhprof:cpu=old,file=java.prof");
1044 }
1045 AddOption(tmp, NULL);
1046 } else if (JLI_StrCCmp(arg, "-ss") == 0 ||
1047 JLI_StrCCmp(arg, "-oss") == 0 ||
1048 JLI_StrCCmp(arg, "-ms") == 0 ||
1049 JLI_StrCCmp(arg, "-mx") == 0) {
1050 char *tmp = JLI_MemAlloc(JLI_StrLen(arg) + 6);
1051 sprintf(tmp, "-X%s", arg + 1); /* skip '-' */
1052 AddOption(tmp, NULL);
1053 } else if (JLI_StrCmp(arg, "-checksource") == 0 ||
1054 JLI_StrCmp(arg, "-cs") == 0 ||
1055 JLI_StrCmp(arg, "-noasyncgc") == 0) {
1056 /* No longer supported */
ksrini0e817162008-08-26 10:21:20 -07001057 JLI_ReportErrorMessage(ARG_WARN, arg);
duke6e45e102007-12-01 00:00:00 +00001058 } else if (JLI_StrCCmp(arg, "-version:") == 0 ||
1059 JLI_StrCmp(arg, "-no-jre-restrict-search") == 0 ||
1060 JLI_StrCmp(arg, "-jre-restrict-search") == 0 ||
1061 JLI_StrCCmp(arg, "-splash:") == 0) {
1062 ; /* Ignore machine independent options already handled */
1063 } else if (RemovableOption(arg) ) {
1064 ; /* Do not pass option to vm. */
1065 } else {
1066 AddOption(arg, NULL);
1067 }
1068 }
1069
1070 if (--argc >= 0) {
mchungea290e22011-01-21 09:43:57 -08001071 *pwhat = *argv++;
1072 }
1073
1074 if (*pwhat == NULL) {
1075 *pret = 1;
1076 } else if (mode == LM_UNKNOWN) {
1077 /* default to LM_CLASS if -jar and -cp option are
1078 * not specified */
1079 mode = LM_CLASS;
1080 }
1081
1082 if (argc >= 0) {
duke6e45e102007-12-01 00:00:00 +00001083 *pargc = argc;
1084 *pargv = argv;
1085 }
mchungea290e22011-01-21 09:43:57 -08001086
1087 *pmode = mode;
1088
duke6e45e102007-12-01 00:00:00 +00001089 return JNI_TRUE;
1090}
1091
1092/*
1093 * Initializes the Java Virtual Machine. Also frees options array when
1094 * finished.
1095 */
1096static jboolean
1097InitializeJVM(JavaVM **pvm, JNIEnv **penv, InvocationFunctions *ifn)
1098{
1099 JavaVMInitArgs args;
1100 jint r;
1101
1102 memset(&args, 0, sizeof(args));
1103 args.version = JNI_VERSION_1_2;
1104 args.nOptions = numOptions;
1105 args.options = options;
1106 args.ignoreUnrecognized = JNI_FALSE;
1107
1108 if (JLI_IsTraceLauncher()) {
1109 int i = 0;
1110 printf("JavaVM args:\n ");
1111 printf("version 0x%08lx, ", (long)args.version);
1112 printf("ignoreUnrecognized is %s, ",
1113 args.ignoreUnrecognized ? "JNI_TRUE" : "JNI_FALSE");
1114 printf("nOptions is %ld\n", (long)args.nOptions);
1115 for (i = 0; i < numOptions; i++)
1116 printf(" option[%2d] = '%s'\n",
1117 i, args.options[i].optionString);
1118 }
1119
1120 r = ifn->CreateJavaVM(pvm, (void **)penv, &args);
1121 JLI_MemFree(options);
1122 return r == JNI_OK;
1123}
1124
1125
1126#define NULL_CHECK0(e) if ((e) == 0) { \
ksrini0e817162008-08-26 10:21:20 -07001127 JLI_ReportErrorMessage(JNI_ERROR); \
duke6e45e102007-12-01 00:00:00 +00001128 return 0; \
1129 }
1130
1131#define NULL_CHECK(e) if ((e) == 0) { \
ksrini0e817162008-08-26 10:21:20 -07001132 JLI_ReportErrorMessage(JNI_ERROR); \
duke6e45e102007-12-01 00:00:00 +00001133 return; \
1134 }
1135
1136static jstring platformEncoding = NULL;
1137static jstring getPlatformEncoding(JNIEnv *env) {
1138 if (platformEncoding == NULL) {
1139 jstring propname = (*env)->NewStringUTF(env, "sun.jnu.encoding");
1140 if (propname) {
1141 jclass cls;
1142 jmethodID mid;
ksrini20a64b22008-09-24 15:07:41 -07001143 NULL_CHECK0 (cls = FindBootStrapClass(env, "java/lang/System"));
duke6e45e102007-12-01 00:00:00 +00001144 NULL_CHECK0 (mid = (*env)->GetStaticMethodID(
1145 env, cls,
1146 "getProperty",
1147 "(Ljava/lang/String;)Ljava/lang/String;"));
1148 platformEncoding = (*env)->CallStaticObjectMethod (
1149 env, cls, mid, propname);
1150 }
1151 }
1152 return platformEncoding;
1153}
1154
1155static jboolean isEncodingSupported(JNIEnv *env, jstring enc) {
1156 jclass cls;
1157 jmethodID mid;
ksrini20a64b22008-09-24 15:07:41 -07001158 NULL_CHECK0 (cls = FindBootStrapClass(env, "java/nio/charset/Charset"));
duke6e45e102007-12-01 00:00:00 +00001159 NULL_CHECK0 (mid = (*env)->GetStaticMethodID(
1160 env, cls,
1161 "isSupported",
1162 "(Ljava/lang/String;)Z"));
1163 return (*env)->CallStaticBooleanMethod(env, cls, mid, enc);
1164}
1165
1166/*
1167 * Returns a new Java string object for the specified platform string.
1168 */
1169static jstring
1170NewPlatformString(JNIEnv *env, char *s)
1171{
1172 int len = (int)JLI_StrLen(s);
1173 jclass cls;
1174 jmethodID mid;
1175 jbyteArray ary;
1176 jstring enc;
1177
1178 if (s == NULL)
1179 return 0;
1180 enc = getPlatformEncoding(env);
1181
1182 ary = (*env)->NewByteArray(env, len);
1183 if (ary != 0) {
1184 jstring str = 0;
1185 (*env)->SetByteArrayRegion(env, ary, 0, len, (jbyte *)s);
1186 if (!(*env)->ExceptionOccurred(env)) {
ksrini20a64b22008-09-24 15:07:41 -07001187 NULL_CHECK0(cls = FindBootStrapClass(env, "java/lang/String"));
duke6e45e102007-12-01 00:00:00 +00001188 if (isEncodingSupported(env, enc) == JNI_TRUE) {
duke6e45e102007-12-01 00:00:00 +00001189 NULL_CHECK0(mid = (*env)->GetMethodID(env, cls, "<init>",
1190 "([BLjava/lang/String;)V"));
1191 str = (*env)->NewObject(env, cls, mid, ary, enc);
1192 } else {
1193 /*If the encoding specified in sun.jnu.encoding is not
1194 endorsed by "Charset.isSupported" we have to fall back
1195 to use String(byte[]) explicitly here without specifying
1196 the encoding name, in which the StringCoding class will
1197 pickup the iso-8859-1 as the fallback converter for us.
1198 */
duke6e45e102007-12-01 00:00:00 +00001199 NULL_CHECK0(mid = (*env)->GetMethodID(env, cls, "<init>",
1200 "([B)V"));
1201 str = (*env)->NewObject(env, cls, mid, ary);
1202 }
1203 (*env)->DeleteLocalRef(env, ary);
1204 return str;
1205 }
1206 }
1207 return 0;
1208}
1209
1210/*
1211 * Returns a new array of Java string objects for the specified
1212 * array of platform strings.
1213 */
1214static jobjectArray
1215NewPlatformStringArray(JNIEnv *env, char **strv, int strc)
1216{
1217 jarray cls;
1218 jarray ary;
1219 int i;
1220
ksrini20a64b22008-09-24 15:07:41 -07001221 NULL_CHECK0(cls = FindBootStrapClass(env, "java/lang/String"));
duke6e45e102007-12-01 00:00:00 +00001222 NULL_CHECK0(ary = (*env)->NewObjectArray(env, strc, cls, 0));
1223 for (i = 0; i < strc; i++) {
1224 jstring str = NewPlatformString(env, *strv++);
1225 NULL_CHECK0(str);
1226 (*env)->SetObjectArrayElement(env, ary, i, str);
1227 (*env)->DeleteLocalRef(env, str);
1228 }
1229 return ary;
1230}
1231
1232/*
ksrini20a64b22008-09-24 15:07:41 -07001233 * Loads a class and verifies that the main class is present and it is ok to
1234 * call it for more details refer to the java implementation.
duke6e45e102007-12-01 00:00:00 +00001235 */
1236static jclass
mchungea290e22011-01-21 09:43:57 -08001237LoadMainClass(JNIEnv *env, int mode, char *name)
duke6e45e102007-12-01 00:00:00 +00001238{
duke6e45e102007-12-01 00:00:00 +00001239 jclass cls;
ksrini20a64b22008-09-24 15:07:41 -07001240 jmethodID mid;
1241 jstring str;
1242 jobject result;
duke6e45e102007-12-01 00:00:00 +00001243 jlong start, end;
1244
ksrini20a64b22008-09-24 15:07:41 -07001245 if (JLI_IsTraceLauncher()) {
duke6e45e102007-12-01 00:00:00 +00001246 start = CounterGet();
ksrini20a64b22008-09-24 15:07:41 -07001247 }
1248 NULL_CHECK0(cls = FindBootStrapClass(env, "sun/launcher/LauncherHelper"));
1249 NULL_CHECK0(mid = (*env)->GetStaticMethodID(env, cls, "checkAndLoadMain",
mchungea290e22011-01-21 09:43:57 -08001250 "(ZILjava/lang/String;)Ljava/lang/Class;"));
ksrini20a64b22008-09-24 15:07:41 -07001251 str = (*env)->NewStringUTF(env, name);
mchungea290e22011-01-21 09:43:57 -08001252 result = (*env)->CallStaticObjectMethod(env, cls, mid, JNI_TRUE, mode, str);
duke6e45e102007-12-01 00:00:00 +00001253
1254 if (JLI_IsTraceLauncher()) {
1255 end = CounterGet();
1256 printf("%ld micro seconds to load main class\n",
1257 (long)(jint)Counter2Micros(end-start));
1258 printf("----_JAVA_LAUNCHER_DEBUG----\n");
1259 }
1260
ksrini20a64b22008-09-24 15:07:41 -07001261 return (jclass)result;
duke6e45e102007-12-01 00:00:00 +00001262}
1263
duke6e45e102007-12-01 00:00:00 +00001264/*
1265 * For tools, convert command line args thus:
1266 * javac -cp foo:foo/"*" -J-ms32m ...
1267 * java -ms32m -cp JLI_WildcardExpandClasspath(foo:foo/"*") ...
1268 *
1269 * Takes 4 parameters, and returns the populated arguments
1270 */
1271static void
1272TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv)
1273{
1274 int argc = *pargc;
1275 char **argv = *pargv;
1276 int nargc = argc + jargc;
1277 char **nargv = JLI_MemAlloc((nargc + 1) * sizeof(char *));
1278 int i;
1279
1280 *pargc = nargc;
1281 *pargv = nargv;
1282
1283 /* Copy the VM arguments (i.e. prefixed with -J) */
1284 for (i = 0; i < jargc; i++) {
1285 const char *arg = jargv[i];
1286 if (arg[0] == '-' && arg[1] == 'J') {
1287 *nargv++ = ((arg + 2) == NULL) ? NULL : JLI_StringDup(arg + 2);
1288 }
1289 }
1290
1291 for (i = 0; i < argc; i++) {
1292 char *arg = argv[i];
1293 if (arg[0] == '-' && arg[1] == 'J') {
1294 if (arg[2] == '\0') {
ksrini0e817162008-08-26 10:21:20 -07001295 JLI_ReportErrorMessage(ARG_ERROR3);
duke6e45e102007-12-01 00:00:00 +00001296 exit(1);
1297 }
1298 *nargv++ = arg + 2;
1299 }
1300 }
1301
1302 /* Copy the rest of the arguments */
1303 for (i = 0; i < jargc ; i++) {
1304 const char *arg = jargv[i];
1305 if (arg[0] != '-' || arg[1] != 'J') {
1306 *nargv++ = (arg == NULL) ? NULL : JLI_StringDup(arg);
1307 }
1308 }
1309 for (i = 0; i < argc; i++) {
1310 char *arg = argv[i];
1311 if (arg[0] == '-') {
1312 if (arg[1] == 'J')
1313 continue;
1314 if (IsWildCardEnabled() && arg[1] == 'c'
1315 && (JLI_StrCmp(arg, "-cp") == 0 ||
1316 JLI_StrCmp(arg, "-classpath") == 0)
1317 && i < argc - 1) {
1318 *nargv++ = arg;
1319 *nargv++ = (char *) JLI_WildcardExpandClasspath(argv[i+1]);
1320 i++;
1321 continue;
1322 }
1323 }
1324 *nargv++ = arg;
1325 }
1326 *nargv = 0;
1327}
1328
1329/*
1330 * For our tools, we try to add 3 VM options:
1331 * -Denv.class.path=<envcp>
1332 * -Dapplication.home=<apphome>
1333 * -Djava.class.path=<appcp>
1334 * <envcp> is the user's setting of CLASSPATH -- for instance the user
1335 * tells javac where to find binary classes through this environment
1336 * variable. Notice that users will be able to compile against our
1337 * tools classes (sun.tools.javac.Main) only if they explicitly add
1338 * tools.jar to CLASSPATH.
1339 * <apphome> is the directory where the application is installed.
1340 * <appcp> is the classpath to where our apps' classfiles are.
1341 */
1342static jboolean
1343AddApplicationOptions(int cpathc, const char **cpathv)
1344{
1345 char *envcp, *appcp, *apphome;
1346 char home[MAXPATHLEN]; /* application home */
1347 char separator[] = { PATH_SEPARATOR, '\0' };
1348 int size, i;
1349
1350 {
1351 const char *s = getenv("CLASSPATH");
1352 if (s) {
1353 s = (char *) JLI_WildcardExpandClasspath(s);
1354 /* 40 for -Denv.class.path= */
1355 envcp = (char *)JLI_MemAlloc(JLI_StrLen(s) + 40);
1356 sprintf(envcp, "-Denv.class.path=%s", s);
1357 AddOption(envcp, NULL);
1358 }
1359 }
1360
1361 if (!GetApplicationHome(home, sizeof(home))) {
ksrini0e817162008-08-26 10:21:20 -07001362 JLI_ReportErrorMessage(CFG_ERROR5);
duke6e45e102007-12-01 00:00:00 +00001363 return JNI_FALSE;
1364 }
1365
1366 /* 40 for '-Dapplication.home=' */
1367 apphome = (char *)JLI_MemAlloc(JLI_StrLen(home) + 40);
1368 sprintf(apphome, "-Dapplication.home=%s", home);
1369 AddOption(apphome, NULL);
1370
1371 /* How big is the application's classpath? */
1372 size = 40; /* 40: "-Djava.class.path=" */
1373 for (i = 0; i < cpathc; i++) {
1374 size += (int)JLI_StrLen(home) + (int)JLI_StrLen(cpathv[i]) + 1; /* 1: separator */
1375 }
1376 appcp = (char *)JLI_MemAlloc(size + 1);
1377 JLI_StrCpy(appcp, "-Djava.class.path=");
1378 for (i = 0; i < cpathc; i++) {
1379 JLI_StrCat(appcp, home); /* c:\program files\myapp */
1380 JLI_StrCat(appcp, cpathv[i]); /* \lib\myapp.jar */
1381 JLI_StrCat(appcp, separator); /* ; */
1382 }
1383 appcp[JLI_StrLen(appcp)-1] = '\0'; /* remove trailing path separator */
1384 AddOption(appcp, NULL);
1385 return JNI_TRUE;
1386}
1387
1388/*
1389 * inject the -Dsun.java.command pseudo property into the args structure
1390 * this pseudo property is used in the HotSpot VM to expose the
1391 * Java class name and arguments to the main method to the VM. The
1392 * HotSpot VM uses this pseudo property to store the Java class name
1393 * (or jar file name) and the arguments to the class's main method
1394 * to the instrumentation memory region. The sun.java.command pseudo
1395 * property is not exported by HotSpot to the Java layer.
1396 */
1397void
mchungea290e22011-01-21 09:43:57 -08001398SetJavaCommandLineProp(char *what, int argc, char **argv)
duke6e45e102007-12-01 00:00:00 +00001399{
1400
1401 int i = 0;
1402 size_t len = 0;
1403 char* javaCommand = NULL;
1404 char* dashDstr = "-Dsun.java.command=";
1405
mchungea290e22011-01-21 09:43:57 -08001406 if (what == NULL) {
duke6e45e102007-12-01 00:00:00 +00001407 /* unexpected, one of these should be set. just return without
1408 * setting the property
1409 */
1410 return;
1411 }
1412
duke6e45e102007-12-01 00:00:00 +00001413 /* determine the amount of memory to allocate assuming
1414 * the individual components will be space separated
1415 */
mchungea290e22011-01-21 09:43:57 -08001416 len = JLI_StrLen(what);
duke6e45e102007-12-01 00:00:00 +00001417 for (i = 0; i < argc; i++) {
1418 len += JLI_StrLen(argv[i]) + 1;
1419 }
1420
1421 /* allocate the memory */
1422 javaCommand = (char*) JLI_MemAlloc(len + JLI_StrLen(dashDstr) + 1);
1423
1424 /* build the -D string */
1425 *javaCommand = '\0';
1426 JLI_StrCat(javaCommand, dashDstr);
mchungea290e22011-01-21 09:43:57 -08001427 JLI_StrCat(javaCommand, what);
duke6e45e102007-12-01 00:00:00 +00001428
1429 for (i = 0; i < argc; i++) {
1430 /* the components of the string are space separated. In
1431 * the case of embedded white space, the relationship of
1432 * the white space separated components to their true
1433 * positional arguments will be ambiguous. This issue may
1434 * be addressed in a future release.
1435 */
1436 JLI_StrCat(javaCommand, " ");
1437 JLI_StrCat(javaCommand, argv[i]);
1438 }
1439
1440 AddOption(javaCommand, NULL);
1441}
1442
1443/*
1444 * JVM would like to know if it's created by a standard Sun launcher, or by
1445 * user native application, the following property indicates the former.
1446 */
mchungea290e22011-01-21 09:43:57 -08001447void
1448SetJavaLauncherProp() {
duke6e45e102007-12-01 00:00:00 +00001449 AddOption("-Dsun.java.launcher=SUN_STANDARD", NULL);
1450}
1451
1452/*
1453 * Prints the version information from the java.version and other properties.
1454 */
1455static void
1456PrintJavaVersion(JNIEnv *env, jboolean extraLF)
1457{
1458 jclass ver;
1459 jmethodID print;
1460
ksrini20a64b22008-09-24 15:07:41 -07001461 NULL_CHECK(ver = FindBootStrapClass(env, "sun/misc/Version"));
duke6e45e102007-12-01 00:00:00 +00001462 NULL_CHECK(print = (*env)->GetStaticMethodID(env,
1463 ver,
1464 (extraLF == JNI_TRUE) ? "println" : "print",
1465 "()V"
1466 )
1467 );
1468
1469 (*env)->CallStaticVoidMethod(env, ver, print);
1470}
1471
1472/*
ksrini8e20e1c2010-11-23 16:52:39 -08001473 * Prints all the Java settings, see the java implementation for more details.
1474 */
1475static void
1476ShowSettings(JNIEnv *env, char *optString)
1477{
1478 jclass cls;
1479 jmethodID showSettingsID;
1480 jstring joptString;
1481 NULL_CHECK(cls = FindBootStrapClass(env, "sun/launcher/LauncherHelper"));
1482 NULL_CHECK(showSettingsID = (*env)->GetStaticMethodID(env, cls,
ksrini6d575f82010-12-23 13:51:30 -08001483 "showSettings", "(ZLjava/lang/String;JJJZ)V"));
ksrini8e20e1c2010-11-23 16:52:39 -08001484 joptString = (*env)->NewStringUTF(env, optString);
1485 (*env)->CallStaticVoidMethod(env, cls, showSettingsID,
1486 JNI_TRUE,
1487 joptString,
ksrini6d575f82010-12-23 13:51:30 -08001488 (jlong)initialHeapSize,
1489 (jlong)maxHeapSize,
ksrini8e20e1c2010-11-23 16:52:39 -08001490 (jlong)threadStackSize,
1491 ServerClassMachine());
1492}
1493
1494/*
ksrini20a64b22008-09-24 15:07:41 -07001495 * Prints default usage or the Xusage message, see sun.launcher.LauncherHelper.java
duke6e45e102007-12-01 00:00:00 +00001496 */
1497static void
1498PrintUsage(JNIEnv* env, jboolean doXUsage)
1499{
1500 jclass cls;
1501 jmethodID initHelp, vmSelect, vmSynonym, vmErgo, printHelp, printXUsageMessage;
1502 jstring jprogname, vm1, vm2;
1503 int i;
1504
ksrini20a64b22008-09-24 15:07:41 -07001505 NULL_CHECK(cls = FindBootStrapClass(env, "sun/launcher/LauncherHelper"));
duke6e45e102007-12-01 00:00:00 +00001506
1507
1508 if (doXUsage) {
1509 NULL_CHECK(printXUsageMessage = (*env)->GetStaticMethodID(env, cls,
1510 "printXUsageMessage", "(Z)V"));
1511 (*env)->CallStaticVoidMethod(env, cls, printXUsageMessage, JNI_TRUE);
1512 } else {
1513 NULL_CHECK(initHelp = (*env)->GetStaticMethodID(env, cls,
1514 "initHelpMessage", "(Ljava/lang/String;)V"));
1515
1516 NULL_CHECK(vmSelect = (*env)->GetStaticMethodID(env, cls, "appendVmSelectMessage",
1517 "(Ljava/lang/String;Ljava/lang/String;)V"));
1518
1519 NULL_CHECK(vmSynonym = (*env)->GetStaticMethodID(env, cls,
1520 "appendVmSynonymMessage",
1521 "(Ljava/lang/String;Ljava/lang/String;)V"));
1522 NULL_CHECK(vmErgo = (*env)->GetStaticMethodID(env, cls,
1523 "appendVmErgoMessage", "(ZLjava/lang/String;)V"));
1524
1525 NULL_CHECK(printHelp = (*env)->GetStaticMethodID(env, cls,
1526 "printHelpMessage", "(Z)V"));
1527
1528 jprogname = (*env)->NewStringUTF(env, _program_name);
1529
1530 /* Initialize the usage message with the usual preamble */
1531 (*env)->CallStaticVoidMethod(env, cls, initHelp, jprogname);
1532
1533
1534 /* Assemble the other variant part of the usage */
1535 if ((knownVMs[0].flag == VM_KNOWN) ||
1536 (knownVMs[0].flag == VM_IF_SERVER_CLASS)) {
1537 vm1 = (*env)->NewStringUTF(env, knownVMs[0].name);
1538 vm2 = (*env)->NewStringUTF(env, knownVMs[0].name+1);
1539 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1540 }
1541 for (i=1; i<knownVMsCount; i++) {
1542 if (knownVMs[i].flag == VM_KNOWN) {
1543 vm1 = (*env)->NewStringUTF(env, knownVMs[i].name);
1544 vm2 = (*env)->NewStringUTF(env, knownVMs[i].name+1);
1545 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1546 }
1547 }
1548 for (i=1; i<knownVMsCount; i++) {
1549 if (knownVMs[i].flag == VM_ALIASED_TO) {
1550 vm1 = (*env)->NewStringUTF(env, knownVMs[i].name);
1551 vm2 = (*env)->NewStringUTF(env, knownVMs[i].alias+1);
1552 (*env)->CallStaticVoidMethod(env, cls, vmSynonym, vm1, vm2);
1553 }
1554 }
1555
1556 /* The first known VM is the default */
1557 {
1558 jboolean isServerClassMachine = ServerClassMachine();
1559
1560 const char* defaultVM = knownVMs[0].name+1;
1561 if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) && isServerClassMachine) {
1562 defaultVM = knownVMs[0].server_class+1;
1563 }
1564
1565 vm1 = (*env)->NewStringUTF(env, defaultVM);
1566 (*env)->CallStaticVoidMethod(env, cls, vmErgo, isServerClassMachine, vm1);
1567 }
1568
1569 /* Complete the usage message and print to stderr*/
1570 (*env)->CallStaticVoidMethod(env, cls, printHelp, JNI_TRUE);
1571 }
1572 return;
1573}
1574
1575/*
1576 * Read the jvm.cfg file and fill the knownJVMs[] array.
1577 *
1578 * The functionality of the jvm.cfg file is subject to change without
1579 * notice and the mechanism will be removed in the future.
1580 *
1581 * The lexical structure of the jvm.cfg file is as follows:
1582 *
1583 * jvmcfg := { vmLine }
1584 * vmLine := knownLine
1585 * | aliasLine
1586 * | warnLine
1587 * | ignoreLine
1588 * | errorLine
1589 * | predicateLine
1590 * | commentLine
1591 * knownLine := flag "KNOWN" EOL
1592 * warnLine := flag "WARN" EOL
1593 * ignoreLine := flag "IGNORE" EOL
1594 * errorLine := flag "ERROR" EOL
1595 * aliasLine := flag "ALIASED_TO" flag EOL
1596 * predicateLine := flag "IF_SERVER_CLASS" flag EOL
1597 * commentLine := "#" text EOL
1598 * flag := "-" identifier
1599 *
1600 * The semantics are that when someone specifies a flag on the command line:
1601 * - if the flag appears on a knownLine, then the identifier is used as
1602 * the name of the directory holding the JVM library (the name of the JVM).
1603 * - if the flag appears as the first flag on an aliasLine, the identifier
1604 * of the second flag is used as the name of the JVM.
1605 * - if the flag appears on a warnLine, the identifier is used as the
1606 * name of the JVM, but a warning is generated.
1607 * - if the flag appears on an ignoreLine, the identifier is recognized as the
1608 * name of a JVM, but the identifier is ignored and the default vm used
1609 * - if the flag appears on an errorLine, an error is generated.
1610 * - if the flag appears as the first flag on a predicateLine, and
1611 * the machine on which you are running passes the predicate indicated,
1612 * then the identifier of the second flag is used as the name of the JVM,
1613 * otherwise the identifier of the first flag is used as the name of the JVM.
1614 * If no flag is given on the command line, the first vmLine of the jvm.cfg
1615 * file determines the name of the JVM.
1616 * PredicateLines are only interpreted on first vmLine of a jvm.cfg file,
1617 * since they only make sense if someone hasn't specified the name of the
1618 * JVM on the command line.
1619 *
1620 * The intent of the jvm.cfg file is to allow several JVM libraries to
1621 * be installed in different subdirectories of a single JRE installation,
1622 * for space-savings and convenience in testing.
1623 * The intent is explicitly not to provide a full aliasing or predicate
1624 * mechanism.
1625 */
1626jint
1627ReadKnownVMs(const char *jrepath, const char * arch, jboolean speculative)
1628{
1629 FILE *jvmCfg;
1630 char jvmCfgName[MAXPATHLEN+20];
1631 char line[MAXPATHLEN+20];
1632 int cnt = 0;
1633 int lineno = 0;
1634 jlong start, end;
1635 int vmType;
1636 char *tmpPtr;
1637 char *altVMName = NULL;
1638 char *serverClassVMName = NULL;
1639 static char *whiteSpace = " \t";
1640 if (JLI_IsTraceLauncher()) {
1641 start = CounterGet();
1642 }
ksrini01740ec2010-09-09 11:50:40 -07001643 JLI_Snprintf(jvmCfgName, sizeof(jvmCfgName), "%s%slib%s%s%sjvm.cfg",
1644 jrepath, FILESEP, FILESEP, arch, FILESEP);
duke6e45e102007-12-01 00:00:00 +00001645
1646 jvmCfg = fopen(jvmCfgName, "r");
1647 if (jvmCfg == NULL) {
1648 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -07001649 JLI_ReportErrorMessage(CFG_ERROR6, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001650 exit(1);
1651 } else {
1652 return -1;
1653 }
1654 }
1655 while (fgets(line, sizeof(line), jvmCfg) != NULL) {
1656 vmType = VM_UNKNOWN;
1657 lineno++;
1658 if (line[0] == '#')
1659 continue;
1660 if (line[0] != '-') {
ksrini0e817162008-08-26 10:21:20 -07001661 JLI_ReportErrorMessage(CFG_WARN2, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001662 }
1663 if (cnt >= knownVMsLimit) {
1664 GrowKnownVMs(cnt);
1665 }
1666 line[JLI_StrLen(line)-1] = '\0'; /* remove trailing newline */
1667 tmpPtr = line + JLI_StrCSpn(line, whiteSpace);
1668 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001669 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001670 } else {
1671 /* Null-terminate this string for JLI_StringDup below */
1672 *tmpPtr++ = 0;
1673 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1674 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001675 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001676 } else {
1677 if (!JLI_StrCCmp(tmpPtr, "KNOWN")) {
1678 vmType = VM_KNOWN;
1679 } else if (!JLI_StrCCmp(tmpPtr, "ALIASED_TO")) {
1680 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1681 if (*tmpPtr != 0) {
1682 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1683 }
1684 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001685 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001686 } else {
1687 /* Null terminate altVMName */
1688 altVMName = tmpPtr;
1689 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1690 *tmpPtr = 0;
1691 vmType = VM_ALIASED_TO;
1692 }
1693 } else if (!JLI_StrCCmp(tmpPtr, "WARN")) {
1694 vmType = VM_WARN;
1695 } else if (!JLI_StrCCmp(tmpPtr, "IGNORE")) {
1696 vmType = VM_IGNORE;
1697 } else if (!JLI_StrCCmp(tmpPtr, "ERROR")) {
1698 vmType = VM_ERROR;
1699 } else if (!JLI_StrCCmp(tmpPtr, "IF_SERVER_CLASS")) {
1700 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1701 if (*tmpPtr != 0) {
1702 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1703 }
1704 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001705 JLI_ReportErrorMessage(CFG_WARN4, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001706 } else {
1707 /* Null terminate server class VM name */
1708 serverClassVMName = tmpPtr;
1709 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1710 *tmpPtr = 0;
1711 vmType = VM_IF_SERVER_CLASS;
1712 }
1713 } else {
ksrini0e817162008-08-26 10:21:20 -07001714 JLI_ReportErrorMessage(CFG_WARN5, lineno, &jvmCfgName[0]);
duke6e45e102007-12-01 00:00:00 +00001715 vmType = VM_KNOWN;
1716 }
1717 }
1718 }
1719
1720 JLI_TraceLauncher("jvm.cfg[%d] = ->%s<-\n", cnt, line);
1721 if (vmType != VM_UNKNOWN) {
1722 knownVMs[cnt].name = JLI_StringDup(line);
1723 knownVMs[cnt].flag = vmType;
1724 switch (vmType) {
1725 default:
1726 break;
1727 case VM_ALIASED_TO:
1728 knownVMs[cnt].alias = JLI_StringDup(altVMName);
1729 JLI_TraceLauncher(" name: %s vmType: %s alias: %s\n",
1730 knownVMs[cnt].name, "VM_ALIASED_TO", knownVMs[cnt].alias);
1731 break;
1732 case VM_IF_SERVER_CLASS:
1733 knownVMs[cnt].server_class = JLI_StringDup(serverClassVMName);
1734 JLI_TraceLauncher(" name: %s vmType: %s server_class: %s\n",
1735 knownVMs[cnt].name, "VM_IF_SERVER_CLASS", knownVMs[cnt].server_class);
1736 break;
1737 }
1738 cnt++;
1739 }
1740 }
1741 fclose(jvmCfg);
1742 knownVMsCount = cnt;
1743
1744 if (JLI_IsTraceLauncher()) {
1745 end = CounterGet();
1746 printf("%ld micro seconds to parse jvm.cfg\n",
1747 (long)(jint)Counter2Micros(end-start));
1748 }
1749
1750 return cnt;
1751}
1752
1753
1754static void
1755GrowKnownVMs(int minimum)
1756{
1757 struct vmdesc* newKnownVMs;
1758 int newMax;
1759
1760 newMax = (knownVMsLimit == 0 ? INIT_MAX_KNOWN_VMS : (2 * knownVMsLimit));
1761 if (newMax <= minimum) {
1762 newMax = minimum;
1763 }
1764 newKnownVMs = (struct vmdesc*) JLI_MemAlloc(newMax * sizeof(struct vmdesc));
1765 if (knownVMs != NULL) {
1766 memcpy(newKnownVMs, knownVMs, knownVMsLimit * sizeof(struct vmdesc));
1767 }
1768 JLI_MemFree(knownVMs);
1769 knownVMs = newKnownVMs;
1770 knownVMsLimit = newMax;
1771}
1772
1773
1774/* Returns index of VM or -1 if not found */
1775static int
1776KnownVMIndex(const char* name)
1777{
1778 int i;
1779 if (JLI_StrCCmp(name, "-J") == 0) name += 2;
1780 for (i = 0; i < knownVMsCount; i++) {
1781 if (!JLI_StrCmp(name, knownVMs[i].name)) {
1782 return i;
1783 }
1784 }
1785 return -1;
1786}
1787
1788static void
1789FreeKnownVMs()
1790{
1791 int i;
1792 for (i = 0; i < knownVMsCount; i++) {
1793 JLI_MemFree(knownVMs[i].name);
1794 knownVMs[i].name = NULL;
1795 }
1796 JLI_MemFree(knownVMs);
1797}
1798
1799
1800/*
1801 * Displays the splash screen according to the jar file name
1802 * and image file names stored in environment variables
1803 */
1804static void
1805ShowSplashScreen()
1806{
1807 const char *jar_name = getenv(SPLASH_JAR_ENV_ENTRY);
1808 const char *file_name = getenv(SPLASH_FILE_ENV_ENTRY);
1809 int data_size;
1810 void *image_data;
1811 if (jar_name) {
1812 image_data = JLI_JarUnpackFile(jar_name, file_name, &data_size);
1813 if (image_data) {
1814 DoSplashInit();
1815 DoSplashLoadMemory(image_data, data_size);
1816 JLI_MemFree(image_data);
1817 }
1818 } else if (file_name) {
1819 DoSplashInit();
1820 DoSplashLoadFile(file_name);
1821 } else {
1822 return;
1823 }
1824 DoSplashSetFileJarName(file_name, jar_name);
1825
1826 /*
1827 * Done with all command line processing and potential re-execs so
1828 * clean up the environment.
1829 */
1830 (void)UnsetEnv(ENV_ENTRY);
1831 (void)UnsetEnv(SPLASH_FILE_ENV_ENTRY);
1832 (void)UnsetEnv(SPLASH_JAR_ENV_ENTRY);
1833
1834 JLI_MemFree(splash_jar_entry);
1835 JLI_MemFree(splash_file_entry);
1836
1837}
1838
1839const char*
1840GetDotVersion()
1841{
1842 return _dVersion;
1843}
1844
1845const char*
1846GetFullVersion()
1847{
1848 return _fVersion;
1849}
1850
1851const char*
1852GetProgramName()
1853{
1854 return _program_name;
1855}
1856
1857const char*
1858GetLauncherName()
1859{
1860 return _launcher_name;
1861}
1862
1863jint
1864GetErgoPolicy()
1865{
1866 return _ergo_policy;
1867}
1868
1869jboolean
1870IsJavaArgs()
1871{
1872 return _is_java_args;
1873}
1874
1875static jboolean
1876IsWildCardEnabled()
1877{
1878 return _wc_enabled;
1879}
1880
1881static int
mchungea290e22011-01-21 09:43:57 -08001882ContinueInNewThread(InvocationFunctions* ifn, int argc, char **argv,
1883 int mode, char *what, int ret)
duke6e45e102007-12-01 00:00:00 +00001884{
1885
1886 /*
1887 * If user doesn't specify stack size, check if VM has a preference.
1888 * Note that HotSpot no longer supports JNI_VERSION_1_1 but it will
1889 * return its default stack size through the init args structure.
1890 */
1891 if (threadStackSize == 0) {
1892 struct JDK1_1InitArgs args1_1;
1893 memset((void*)&args1_1, 0, sizeof(args1_1));
1894 args1_1.version = JNI_VERSION_1_1;
1895 ifn->GetDefaultJavaVMInitArgs(&args1_1); /* ignore return value */
1896 if (args1_1.javaStackSize > 0) {
1897 threadStackSize = args1_1.javaStackSize;
1898 }
1899 }
1900
1901 { /* Create a new thread to create JVM and invoke main method */
1902 JavaMainArgs args;
1903 int rslt;
1904
1905 args.argc = argc;
1906 args.argv = argv;
mchungea290e22011-01-21 09:43:57 -08001907 args.mode = mode;
1908 args.what = what;
duke6e45e102007-12-01 00:00:00 +00001909 args.ifn = *ifn;
1910
1911 rslt = ContinueInNewThread0(JavaMain, threadStackSize, (void*)&args);
1912 /* If the caller has deemed there is an error we
1913 * simply return that, otherwise we return the value of
1914 * the callee
1915 */
1916 return (ret != 0) ? ret : rslt;
1917 }
1918}
1919
1920static void
1921DumpState()
1922{
1923 if (!JLI_IsTraceLauncher()) return ;
1924 printf("Launcher state:\n");
1925 printf("\tdebug:%s\n", (JLI_IsTraceLauncher() == JNI_TRUE) ? "on" : "off");
1926 printf("\tjavargs:%s\n", (_is_java_args == JNI_TRUE) ? "on" : "off");
1927 printf("\tprogram name:%s\n", GetProgramName());
1928 printf("\tlauncher name:%s\n", GetLauncherName());
1929 printf("\tjavaw:%s\n", (IsJavaw() == JNI_TRUE) ? "on" : "off");
1930 printf("\tfullversion:%s\n", GetFullVersion());
1931 printf("\tdotversion:%s\n", GetDotVersion());
1932 printf("\tergo_policy:");
1933 switch(GetErgoPolicy()) {
1934 case NEVER_SERVER_CLASS:
1935 printf("NEVER_ACT_AS_A_SERVER_CLASS_MACHINE\n");
1936 break;
1937 case ALWAYS_SERVER_CLASS:
1938 printf("ALWAYS_ACT_AS_A_SERVER_CLASS_MACHINE\n");
1939 break;
1940 default:
1941 printf("DEFAULT_ERGONOMICS_POLICY\n");
1942 }
1943}
1944
1945/*
1946 * Return JNI_TRUE for an option string that has no effect but should
1947 * _not_ be passed on to the vm; return JNI_FALSE otherwise. On
1948 * Solaris SPARC, this screening needs to be done if:
ksrini11e7f1b2009-11-20 11:01:32 -08001949 * -d32 or -d64 is passed to a binary with an unmatched data model
1950 * (the exec in CreateExecutionEnvironment removes -d<n> options and points the
1951 * exec to the proper binary). In the case of when the data model and the
1952 * requested version is matched, an exec would not occur, and these options
1953 * were erroneously passed to the vm.
duke6e45e102007-12-01 00:00:00 +00001954 */
1955jboolean
1956RemovableOption(char * option)
1957{
1958 /*
1959 * Unconditionally remove both -d32 and -d64 options since only
1960 * the last such options has an effect; e.g.
1961 * java -d32 -d64 -d32 -version
1962 * is equivalent to
1963 * java -d32 -version
1964 */
1965
1966 if( (JLI_StrCCmp(option, "-d32") == 0 ) ||
1967 (JLI_StrCCmp(option, "-d64") == 0 ) )
1968 return JNI_TRUE;
1969 else
1970 return JNI_FALSE;
1971}
1972
1973/*
1974 * A utility procedure to always print to stderr
1975 */
1976void
ksrini0e817162008-08-26 10:21:20 -07001977JLI_ReportMessage(const char* fmt, ...)
duke6e45e102007-12-01 00:00:00 +00001978{
1979 va_list vl;
1980 va_start(vl, fmt);
1981 vfprintf(stderr, fmt, vl);
1982 fprintf(stderr, "\n");
1983 va_end(vl);
1984}