blob: ed45f085de941f862f4f01ceb09a39f35b57468b [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
ksrini07e328d2013-05-07 13:15:28 -07002 * Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.
duke6e45e102007-12-01 00:00:00 +00003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
ohair2283b9d2010-05-25 15:58:33 -07007 * published by the Free Software Foundation. Oracle designates this
duke6e45e102007-12-01 00:00:00 +00008 * particular file as subject to the "Classpath" exception as provided
ohair2283b9d2010-05-25 15:58:33 -07009 * by Oracle in the LICENSE file that accompanied this code.
duke6e45e102007-12-01 00:00:00 +000010 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
ohair2283b9d2010-05-25 15:58:33 -070021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
duke6e45e102007-12-01 00:00:00 +000024 */
25
26/*
27 * Shared source for 'java' command line tool.
28 *
29 * If JAVA_ARGS is defined, then acts as a launcher for applications. For
30 * instance, the JDK command line tools such as javac and javadoc (see
31 * makefiles for more details) are built with this program. Any arguments
32 * prefixed with '-J' will be passed directly to the 'java' command.
33 */
34
35/*
36 * One job of the launcher is to remove command line options which the
37 * vm does not understand and will not process. These options include
38 * options which select which style of vm is run (e.g. -client and
39 * -server) as well as options which select the data model to use.
40 * Additionally, for tools which invoke an underlying vm "-J-foo"
41 * options are turned into "-foo" options to the vm. This option
42 * filtering is handled in a number of places in the launcher, some of
43 * it in machine-dependent code. In this file, the function
ksrini11e7f1b2009-11-20 11:01:32 -080044 * CheckJvmType removes vm style options and TranslateApplicationArgs
45 * removes "-J" prefixes. The CreateExecutionEnvironment function processes
46 * and removes -d<n> options. On unix, there is a possibility that the running
47 * data model may not match to the desired data model, in this case an exec is
48 * required to start the desired model. If the data models match, then
49 * ParseArguments will remove the -d<n> flags. If the data models do not match
50 * the CreateExecutionEnviroment will remove the -d<n> flags.
duke6e45e102007-12-01 00:00:00 +000051 */
52
53
54#include "java.h"
55
56/*
57 * A NOTE TO DEVELOPERS: For performance reasons it is important that
58 * the program image remain relatively small until after SelectVersion
59 * CreateExecutionEnvironment have finished their possibly recursive
60 * processing. Watch everything, but resist all temptations to use Java
61 * interfaces.
62 */
63
ksrini7d9872e2011-03-20 08:41:33 -070064/* we always print to stderr */
65#define USE_STDERR JNI_TRUE
66
duke6e45e102007-12-01 00:00:00 +000067static jboolean printVersion = JNI_FALSE; /* print and exit */
68static jboolean showVersion = JNI_FALSE; /* print but continue */
69static jboolean printUsage = JNI_FALSE; /* print and exit*/
70static jboolean printXUsage = JNI_FALSE; /* print and exit*/
ksrini8e20e1c2010-11-23 16:52:39 -080071static char *showSettings = NULL; /* print but continue */
duke6e45e102007-12-01 00:00:00 +000072
73static const char *_program_name;
74static const char *_launcher_name;
75static jboolean _is_java_args = JNI_FALSE;
76static const char *_fVersion;
77static const char *_dVersion;
78static jboolean _wc_enabled = JNI_FALSE;
79static jint _ergo_policy = DEFAULT_POLICY;
80
81/*
82 * Entries for splash screen environment variables.
83 * putenv is performed in SelectVersion. We need
84 * them in memory until UnsetEnv, so they are made static
85 * global instead of auto local.
86 */
87static char* splash_file_entry = NULL;
88static char* splash_jar_entry = NULL;
89
90/*
91 * List of VM options to be specified when the VM is created.
92 */
93static JavaVMOption *options;
94static int numOptions, maxOptions;
95
96/*
97 * Prototypes for functions internal to launcher.
98 */
99static void SetClassPath(const char *s);
100static void SelectVersion(int argc, char **argv, char **main_class);
mchungea290e22011-01-21 09:43:57 -0800101static jboolean ParseArguments(int *pargc, char ***pargv,
102 int *pmode, char **pwhat,
103 int *pret, const char *jrepath);
duke6e45e102007-12-01 00:00:00 +0000104static jboolean InitializeJVM(JavaVM **pvm, JNIEnv **penv,
105 InvocationFunctions *ifn);
106static jstring NewPlatformString(JNIEnv *env, char *s);
mchungea290e22011-01-21 09:43:57 -0800107static jclass LoadMainClass(JNIEnv *env, int mode, char *name);
ksrini6b41ee82012-11-19 19:49:38 -0800108static jclass GetApplicationClass(JNIEnv *env);
duke6e45e102007-12-01 00:00:00 +0000109
110static void TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv);
111static jboolean AddApplicationOptions(int cpathc, const char **cpathv);
112static void SetApplicationClassPath(const char**);
113
114static void PrintJavaVersion(JNIEnv *env, jboolean extraLF);
115static void PrintUsage(JNIEnv* env, jboolean doXUsage);
ksrini8e20e1c2010-11-23 16:52:39 -0800116static void ShowSettings(JNIEnv* env, char *optString);
duke6e45e102007-12-01 00:00:00 +0000117
118static void SetPaths(int argc, char **argv);
119
120static void DumpState();
121static jboolean RemovableOption(char *option);
122
123/* Maximum supported entries from jvm.cfg. */
124#define INIT_MAX_KNOWN_VMS 10
125
126/* Values for vmdesc.flag */
127enum vmdesc_flag {
128 VM_UNKNOWN = -1,
129 VM_KNOWN,
130 VM_ALIASED_TO,
131 VM_WARN,
132 VM_ERROR,
133 VM_IF_SERVER_CLASS,
134 VM_IGNORE
135};
136
137struct vmdesc {
138 char *name;
139 int flag;
140 char *alias;
141 char *server_class;
142};
143static struct vmdesc *knownVMs = NULL;
144static int knownVMsCount = 0;
145static int knownVMsLimit = 0;
146
147static void GrowKnownVMs();
148static int KnownVMIndex(const char* name);
149static void FreeKnownVMs();
duke6e45e102007-12-01 00:00:00 +0000150static jboolean IsWildCardEnabled();
151
ksrini07e328d2013-05-07 13:15:28 -0700152#define ARG_CHECK(AC_arg_count, AC_failure_message, AC_questionable_arg) \
153 do { \
154 if (AC_arg_count < 1) { \
155 JLI_ReportErrorMessage(AC_failure_message, AC_questionable_arg); \
156 printUsage = JNI_TRUE; \
157 *pret = 1; \
158 return JNI_TRUE; \
159 } \
160 } while (JNI_FALSE)
duke6e45e102007-12-01 00:00:00 +0000161
162/*
163 * Running Java code in primordial thread caused many problems. We will
164 * create a new thread to invoke JVM. See 6316197 for more information.
165 */
ksrinie97d4002012-07-31 06:14:28 -0700166static jlong threadStackSize = 0; /* stack size of the new thread */
ksrini6d575f82010-12-23 13:51:30 -0800167static jlong maxHeapSize = 0; /* max heap size */
168static jlong initialHeapSize = 0; /* inital heap size */
duke6e45e102007-12-01 00:00:00 +0000169
duke6e45e102007-12-01 00:00:00 +0000170/*
171 * Entry point.
172 */
173int
174JLI_Launch(int argc, char ** argv, /* main argc, argc */
175 int jargc, const char** jargv, /* java args */
176 int appclassc, const char** appclassv, /* app classpath */
177 const char* fullversion, /* full version defined */
178 const char* dotversion, /* dot version defined */
179 const char* pname, /* program name */
180 const char* lname, /* launcher name */
181 jboolean javaargs, /* JAVA_ARGS */
182 jboolean cpwildcard, /* classpath wildcard*/
183 jboolean javaw, /* windows-only javaw */
184 jint ergo /* ergonomics class policy */
185)
186{
mchungea290e22011-01-21 09:43:57 -0800187 int mode = LM_UNKNOWN;
188 char *what = NULL;
duke6e45e102007-12-01 00:00:00 +0000189 char *cpath = 0;
190 char *main_class = NULL;
191 int ret;
192 InvocationFunctions ifn;
193 jlong start, end;
ksrinie3ec45d2010-07-09 11:04:34 -0700194 char jvmpath[MAXPATHLEN];
195 char jrepath[MAXPATHLEN];
michaelm5ac8c152012-03-06 20:34:38 +0000196 char jvmcfg[MAXPATHLEN];
duke6e45e102007-12-01 00:00:00 +0000197
198 _fVersion = fullversion;
199 _dVersion = dotversion;
200 _launcher_name = lname;
201 _program_name = pname;
202 _is_java_args = javaargs;
203 _wc_enabled = cpwildcard;
204 _ergo_policy = ergo;
205
ksrini52cded22008-03-06 07:51:28 -0800206 InitLauncher(javaw);
duke6e45e102007-12-01 00:00:00 +0000207 DumpState();
ksrinie97d4002012-07-31 06:14:28 -0700208 if (JLI_IsTraceLauncher()) {
209 int i;
210 printf("Command line args:\n");
211 for (i = 0; i < argc ; i++) {
212 printf("argv[%d] = %s\n", i, argv[i]);
213 }
214 AddOption("-Dsun.java.launcher.diag=true", NULL);
215 }
duke6e45e102007-12-01 00:00:00 +0000216
217 /*
218 * Make sure the specified version of the JRE is running.
219 *
220 * There are three things to note about the SelectVersion() routine:
221 * 1) If the version running isn't correct, this routine doesn't
222 * return (either the correct version has been exec'd or an error
223 * was issued).
224 * 2) Argc and Argv in this scope are *not* altered by this routine.
225 * It is the responsibility of subsequent code to ignore the
226 * arguments handled by this routine.
227 * 3) As a side-effect, the variable "main_class" is guaranteed to
228 * be set (if it should ever be set). This isn't exactly the
229 * poster child for structured programming, but it is a small
230 * price to pay for not processing a jar file operand twice.
231 * (Note: This side effect has been disabled. See comment on
232 * bugid 5030265 below.)
233 */
234 SelectVersion(argc, argv, &main_class);
235
duke6e45e102007-12-01 00:00:00 +0000236 CreateExecutionEnvironment(&argc, &argv,
237 jrepath, sizeof(jrepath),
michaelm5ac8c152012-03-06 20:34:38 +0000238 jvmpath, sizeof(jvmpath),
239 jvmcfg, sizeof(jvmcfg));
duke6e45e102007-12-01 00:00:00 +0000240
241 ifn.CreateJavaVM = 0;
242 ifn.GetDefaultJavaVMInitArgs = 0;
243
244 if (JLI_IsTraceLauncher()) {
245 start = CounterGet();
246 }
247
248 if (!LoadJavaVM(jvmpath, &ifn)) {
249 return(6);
250 }
251
252 if (JLI_IsTraceLauncher()) {
253 end = CounterGet();
254 }
255
256 JLI_TraceLauncher("%ld micro seconds to LoadJavaVM\n",
257 (long)(jint)Counter2Micros(end-start));
258
259 ++argv;
260 --argc;
261
262 if (IsJavaArgs()) {
263 /* Preprocess wrapper arguments */
264 TranslateApplicationArgs(jargc, jargv, &argc, &argv);
265 if (!AddApplicationOptions(appclassc, appclassv)) {
266 return(1);
267 }
268 } else {
269 /* Set default CLASSPATH */
270 cpath = getenv("CLASSPATH");
271 if (cpath == NULL) {
272 cpath = ".";
273 }
274 SetClassPath(cpath);
275 }
276
mchungea290e22011-01-21 09:43:57 -0800277 /* Parse command line options; if the return value of
278 * ParseArguments is false, the program should exit.
duke6e45e102007-12-01 00:00:00 +0000279 */
mchungea290e22011-01-21 09:43:57 -0800280 if (!ParseArguments(&argc, &argv, &mode, &what, &ret, jrepath))
281 {
duke6e45e102007-12-01 00:00:00 +0000282 return(ret);
283 }
284
285 /* Override class path if -jar flag was specified */
mchungea290e22011-01-21 09:43:57 -0800286 if (mode == LM_JAR) {
287 SetClassPath(what); /* Override class path */
duke6e45e102007-12-01 00:00:00 +0000288 }
289
290 /* set the -Dsun.java.command pseudo property */
mchungea290e22011-01-21 09:43:57 -0800291 SetJavaCommandLineProp(what, argc, argv);
duke6e45e102007-12-01 00:00:00 +0000292
293 /* Set the -Dsun.java.launcher pseudo property */
294 SetJavaLauncherProp();
295
296 /* set the -Dsun.java.launcher.* platform properties */
297 SetJavaLauncherPlatformProps();
298
michaelm5ac8c152012-03-06 20:34:38 +0000299 return JVMInit(&ifn, threadStackSize, argc, argv, mode, what, ret);
duke6e45e102007-12-01 00:00:00 +0000300}
ksrinie3ec45d2010-07-09 11:04:34 -0700301/*
302 * Always detach the main thread so that it appears to have ended when
303 * the application's main method exits. This will invoke the
304 * uncaught exception handler machinery if main threw an
305 * exception. An uncaught exception handler cannot change the
306 * launcher's return code except by calling System.exit.
307 *
308 * Wait for all non-daemon threads to end, then destroy the VM.
309 * This will actually create a trivial new Java waiter thread
310 * named "DestroyJavaVM", but this will be seen as a different
311 * thread from the one that executed main, even though they are
312 * the same C thread. This allows mainThread.join() and
313 * mainThread.isAlive() to work as expected.
314 */
315#define LEAVE() \
ksrini07e328d2013-05-07 13:15:28 -0700316 do { \
317 if ((*vm)->DetachCurrentThread(vm) != JNI_OK) { \
318 JLI_ReportErrorMessage(JVM_ERROR2); \
319 ret = 1; \
320 } \
321 if (JNI_TRUE) { \
322 (*vm)->DestroyJavaVM(vm); \
323 return ret; \
324 } \
325 } while (JNI_FALSE)
duke6e45e102007-12-01 00:00:00 +0000326
ksrini07e328d2013-05-07 13:15:28 -0700327#define CHECK_EXCEPTION_NULL_LEAVE(CENL_exception) \
328 do { \
329 if ((*env)->ExceptionOccurred(env)) { \
330 JLI_ReportExceptionDescription(env); \
331 LEAVE(); \
332 } \
333 if ((CENL_exception) == NULL) { \
334 JLI_ReportErrorMessage(JNI_ERROR); \
335 LEAVE(); \
336 } \
337 } while (JNI_FALSE)
ksrini20a64b22008-09-24 15:07:41 -0700338
ksrini07e328d2013-05-07 13:15:28 -0700339#define CHECK_EXCEPTION_LEAVE(CEL_return_value) \
340 do { \
341 if ((*env)->ExceptionOccurred(env)) { \
342 JLI_ReportExceptionDescription(env); \
343 ret = (CEL_return_value); \
344 LEAVE(); \
345 } \
346 } while (JNI_FALSE)
duke6e45e102007-12-01 00:00:00 +0000347
348int JNICALL
349JavaMain(void * _args)
350{
351 JavaMainArgs *args = (JavaMainArgs *)_args;
352 int argc = args->argc;
353 char **argv = args->argv;
mchungea290e22011-01-21 09:43:57 -0800354 int mode = args->mode;
355 char *what = args->what;
duke6e45e102007-12-01 00:00:00 +0000356 InvocationFunctions ifn = args->ifn;
357
358 JavaVM *vm = 0;
359 JNIEnv *env = 0;
mchungea290e22011-01-21 09:43:57 -0800360 jclass mainClass = NULL;
ksrini6b41ee82012-11-19 19:49:38 -0800361 jclass appClass = NULL; // actual application class being launched
duke6e45e102007-12-01 00:00:00 +0000362 jmethodID mainID;
363 jobjectArray mainArgs;
364 int ret = 0;
365 jlong start, end;
366
michaelm5ac8c152012-03-06 20:34:38 +0000367 RegisterThread();
368
duke6e45e102007-12-01 00:00:00 +0000369 /* Initialize the virtual machine */
duke6e45e102007-12-01 00:00:00 +0000370 start = CounterGet();
371 if (!InitializeJVM(&vm, &env, &ifn)) {
ksrini0e817162008-08-26 10:21:20 -0700372 JLI_ReportErrorMessage(JVM_ERROR1);
duke6e45e102007-12-01 00:00:00 +0000373 exit(1);
374 }
375
ksrini05fdd5a2012-01-03 08:27:37 -0800376 if (showSettings != NULL) {
377 ShowSettings(env, showSettings);
378 CHECK_EXCEPTION_LEAVE(1);
379 }
380
duke6e45e102007-12-01 00:00:00 +0000381 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
389 /* If the user specified neither a class name nor a JAR file */
mchungea290e22011-01-21 09:43:57 -0800390 if (printXUsage || printUsage || what == 0 || mode == LM_UNKNOWN) {
duke6e45e102007-12-01 00:00:00 +0000391 PrintUsage(env, printXUsage);
ksrini20a64b22008-09-24 15:07:41 -0700392 CHECK_EXCEPTION_LEAVE(1);
ksrinie3ec45d2010-07-09 11:04:34 -0700393 LEAVE();
duke6e45e102007-12-01 00:00:00 +0000394 }
395
396 FreeKnownVMs(); /* after last possible PrintUsage() */
397
398 if (JLI_IsTraceLauncher()) {
399 end = CounterGet();
400 JLI_TraceLauncher("%ld micro seconds to InitializeJVM\n",
401 (long)(jint)Counter2Micros(end-start));
402 }
403
mchungea290e22011-01-21 09:43:57 -0800404 /* At this stage, argc/argv have the application's arguments */
duke6e45e102007-12-01 00:00:00 +0000405 if (JLI_IsTraceLauncher()){
406 int i;
mchungea290e22011-01-21 09:43:57 -0800407 printf("%s is '%s'\n", launchModeNames[mode], what);
408 printf("App's argc is %d\n", argc);
duke6e45e102007-12-01 00:00:00 +0000409 for (i=0; i < argc; i++) {
410 printf(" argv[%2d] = '%s'\n", i, argv[i]);
411 }
412 }
413
414 ret = 1;
415
416 /*
417 * Get the application's main class.
418 *
419 * See bugid 5030265. The Main-Class name has already been parsed
420 * from the manifest, but not parsed properly for UTF-8 support.
421 * Hence the code here ignores the value previously extracted and
422 * uses the pre-existing code to reextract the value. This is
423 * possibly an end of release cycle expedient. However, it has
424 * also been discovered that passing some character sets through
425 * the environment has "strange" behavior on some variants of
426 * Windows. Hence, maybe the manifest parsing code local to the
427 * launcher should never be enhanced.
428 *
429 * Hence, future work should either:
430 * 1) Correct the local parsing code and verify that the
431 * Main-Class attribute gets properly passed through
432 * all environments,
433 * 2) Remove the vestages of maintaining main_class through
434 * the environment (and remove these comments).
ksrini6b41ee82012-11-19 19:49:38 -0800435 *
436 * This method also correctly handles launching existing JavaFX
437 * applications that may or may not have a Main-Class manifest entry.
duke6e45e102007-12-01 00:00:00 +0000438 */
mchungea290e22011-01-21 09:43:57 -0800439 mainClass = LoadMainClass(env, mode, what);
ksrini20a64b22008-09-24 15:07:41 -0700440 CHECK_EXCEPTION_NULL_LEAVE(mainClass);
ksrini6b41ee82012-11-19 19:49:38 -0800441 /*
442 * In some cases when launching an application that needs a helper, e.g., a
443 * JavaFX application with no main method, the mainClass will not be the
444 * applications own main class but rather a helper class. To keep things
445 * consistent in the UI we need to track and report the application main class.
446 */
447 appClass = GetApplicationClass(env);
ksrini07e328d2013-05-07 13:15:28 -0700448 NULL_CHECK_RETURN_VALUE(appClass, -1);
ksrini6b41ee82012-11-19 19:49:38 -0800449 /*
450 * PostJVMInit uses the class name as the application name for GUI purposes,
451 * for example, on OSX this sets the application name in the menu bar for
452 * both SWT and JavaFX. So we'll pass the actual application class here
453 * instead of mainClass as that may be a launcher or helper class instead
454 * of the application class.
455 */
456 PostJVMInit(env, appClass, vm);
ksrini20a64b22008-09-24 15:07:41 -0700457 /*
458 * The LoadMainClass not only loads the main class, it will also ensure
459 * that the main method's signature is correct, therefore further checking
460 * is not required. The main method is invoked here so that extraneous java
461 * stacks are not in the application stack trace.
462 */
duke6e45e102007-12-01 00:00:00 +0000463 mainID = (*env)->GetStaticMethodID(env, mainClass, "main",
464 "([Ljava/lang/String;)V");
ksrini20a64b22008-09-24 15:07:41 -0700465 CHECK_EXCEPTION_NULL_LEAVE(mainID);
duke6e45e102007-12-01 00:00:00 +0000466
ksrinie97d4002012-07-31 06:14:28 -0700467 /* Build platform specific argument array */
468 mainArgs = CreateApplicationArgs(env, argv, argc);
ksrini20a64b22008-09-24 15:07:41 -0700469 CHECK_EXCEPTION_NULL_LEAVE(mainArgs);
duke6e45e102007-12-01 00:00:00 +0000470
471 /* Invoke main method. */
472 (*env)->CallStaticVoidMethod(env, mainClass, mainID, mainArgs);
473
474 /*
475 * The launcher's exit code (in the absence of calls to
476 * System.exit) will be non-zero if main threw an exception.
477 */
478 ret = (*env)->ExceptionOccurred(env) == NULL ? 0 : 1;
ksrinie3ec45d2010-07-09 11:04:34 -0700479 LEAVE();
duke6e45e102007-12-01 00:00:00 +0000480}
481
duke6e45e102007-12-01 00:00:00 +0000482/*
483 * Checks the command line options to find which JVM type was
484 * specified. If no command line option was given for the JVM type,
485 * the default type is used. The environment variable
486 * JDK_ALTERNATE_VM and the command line option -XXaltjvm= are also
487 * checked as ways of specifying which JVM type to invoke.
488 */
489char *
490CheckJvmType(int *pargc, char ***argv, jboolean speculative) {
491 int i, argi;
492 int argc;
493 char **newArgv;
494 int newArgvIdx = 0;
495 int isVMType;
496 int jvmidx = -1;
497 char *jvmtype = getenv("JDK_ALTERNATE_VM");
498
499 argc = *pargc;
500
501 /* To make things simpler we always copy the argv array */
502 newArgv = JLI_MemAlloc((argc + 1) * sizeof(char *));
503
504 /* The program name is always present */
505 newArgv[newArgvIdx++] = (*argv)[0];
506
507 for (argi = 1; argi < argc; argi++) {
508 char *arg = (*argv)[argi];
509 isVMType = 0;
510
511 if (IsJavaArgs()) {
512 if (arg[0] != '-') {
513 newArgv[newArgvIdx++] = arg;
514 continue;
515 }
516 } else {
517 if (JLI_StrCmp(arg, "-classpath") == 0 ||
518 JLI_StrCmp(arg, "-cp") == 0) {
519 newArgv[newArgvIdx++] = arg;
520 argi++;
521 if (argi < argc) {
522 newArgv[newArgvIdx++] = (*argv)[argi];
523 }
524 continue;
525 }
526 if (arg[0] != '-') break;
527 }
528
529 /* Did the user pass an explicit VM type? */
530 i = KnownVMIndex(arg);
531 if (i >= 0) {
532 jvmtype = knownVMs[jvmidx = i].name + 1; /* skip the - */
533 isVMType = 1;
534 *pargc = *pargc - 1;
535 }
536
537 /* Did the user specify an "alternate" VM? */
538 else if (JLI_StrCCmp(arg, "-XXaltjvm=") == 0 || JLI_StrCCmp(arg, "-J-XXaltjvm=") == 0) {
539 isVMType = 1;
540 jvmtype = arg+((arg[1]=='X')? 10 : 12);
541 jvmidx = -1;
542 }
543
544 if (!isVMType) {
545 newArgv[newArgvIdx++] = arg;
546 }
547 }
548
549 /*
550 * Finish copying the arguments if we aborted the above loop.
551 * NOTE that if we aborted via "break" then we did NOT copy the
552 * last argument above, and in addition argi will be less than
553 * argc.
554 */
555 while (argi < argc) {
556 newArgv[newArgvIdx++] = (*argv)[argi];
557 argi++;
558 }
559
560 /* argv is null-terminated */
561 newArgv[newArgvIdx] = 0;
562
563 /* Copy back argv */
564 *argv = newArgv;
565 *pargc = newArgvIdx;
566
567 /* use the default VM type if not specified (no alias processing) */
568 if (jvmtype == NULL) {
569 char* result = knownVMs[0].name+1;
570 /* Use a different VM type if we are on a server class machine? */
571 if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) &&
572 (ServerClassMachine() == JNI_TRUE)) {
573 result = knownVMs[0].server_class+1;
574 }
575 JLI_TraceLauncher("Default VM: %s\n", result);
576 return result;
577 }
578
579 /* if using an alternate VM, no alias processing */
580 if (jvmidx < 0)
581 return jvmtype;
582
583 /* Resolve aliases first */
584 {
585 int loopCount = 0;
586 while (knownVMs[jvmidx].flag == VM_ALIASED_TO) {
587 int nextIdx = KnownVMIndex(knownVMs[jvmidx].alias);
588
589 if (loopCount > knownVMsCount) {
590 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700591 JLI_ReportErrorMessage(CFG_ERROR1);
duke6e45e102007-12-01 00:00:00 +0000592 exit(1);
593 } else {
594 return "ERROR";
595 /* break; */
596 }
597 }
598
599 if (nextIdx < 0) {
600 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700601 JLI_ReportErrorMessage(CFG_ERROR2, knownVMs[jvmidx].alias);
duke6e45e102007-12-01 00:00:00 +0000602 exit(1);
603 } else {
604 return "ERROR";
605 }
606 }
607 jvmidx = nextIdx;
608 jvmtype = knownVMs[jvmidx].name+1;
609 loopCount++;
610 }
611 }
612
613 switch (knownVMs[jvmidx].flag) {
614 case VM_WARN:
615 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700616 JLI_ReportErrorMessage(CFG_WARN1, jvmtype, knownVMs[0].name + 1);
duke6e45e102007-12-01 00:00:00 +0000617 }
618 /* fall through */
619 case VM_IGNORE:
620 jvmtype = knownVMs[jvmidx=0].name + 1;
621 /* fall through */
622 case VM_KNOWN:
623 break;
624 case VM_ERROR:
625 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700626 JLI_ReportErrorMessage(CFG_ERROR3, jvmtype);
duke6e45e102007-12-01 00:00:00 +0000627 exit(1);
628 } else {
629 return "ERROR";
630 }
631 }
632
633 return jvmtype;
634}
635
636/* copied from HotSpot function "atomll()" */
637static int
ksrini8e20e1c2010-11-23 16:52:39 -0800638parse_size(const char *s, jlong *result) {
duke6e45e102007-12-01 00:00:00 +0000639 jlong n = 0;
640 int args_read = sscanf(s, jlong_format_specifier(), &n);
641 if (args_read != 1) {
642 return 0;
643 }
644 while (*s != '\0' && *s >= '0' && *s <= '9') {
645 s++;
646 }
647 // 4705540: illegal if more characters are found after the first non-digit
648 if (JLI_StrLen(s) > 1) {
649 return 0;
650 }
651 switch (*s) {
652 case 'T': case 't':
653 *result = n * GB * KB;
654 return 1;
655 case 'G': case 'g':
656 *result = n * GB;
657 return 1;
658 case 'M': case 'm':
659 *result = n * MB;
660 return 1;
661 case 'K': case 'k':
662 *result = n * KB;
663 return 1;
664 case '\0':
665 *result = n;
666 return 1;
667 default:
668 /* Create JVM with default stack and let VM handle malformed -Xss string*/
669 return 0;
670 }
671}
672
673/*
674 * Adds a new VM option with the given given name and value.
675 */
676void
677AddOption(char *str, void *info)
678{
679 /*
680 * Expand options array if needed to accommodate at least one more
681 * VM option.
682 */
683 if (numOptions >= maxOptions) {
684 if (options == 0) {
685 maxOptions = 4;
686 options = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
687 } else {
688 JavaVMOption *tmp;
689 maxOptions *= 2;
690 tmp = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
691 memcpy(tmp, options, numOptions * sizeof(JavaVMOption));
692 JLI_MemFree(options);
693 options = tmp;
694 }
695 }
696 options[numOptions].optionString = str;
697 options[numOptions++].extraInfo = info;
698
699 if (JLI_StrCCmp(str, "-Xss") == 0) {
ksrini8e20e1c2010-11-23 16:52:39 -0800700 jlong tmp;
701 if (parse_size(str + 4, &tmp)) {
702 threadStackSize = tmp;
703 }
704 }
705
706 if (JLI_StrCCmp(str, "-Xmx") == 0) {
707 jlong tmp;
708 if (parse_size(str + 4, &tmp)) {
ksrini6d575f82010-12-23 13:51:30 -0800709 maxHeapSize = tmp;
710 }
711 }
712
713 if (JLI_StrCCmp(str, "-Xms") == 0) {
714 jlong tmp;
715 if (parse_size(str + 4, &tmp)) {
mchungea290e22011-01-21 09:43:57 -0800716 initialHeapSize = tmp;
ksrini8e20e1c2010-11-23 16:52:39 -0800717 }
duke6e45e102007-12-01 00:00:00 +0000718 }
719}
720
721static void
722SetClassPath(const char *s)
723{
724 char *def;
martinc0ca3352009-06-22 16:41:27 -0700725 const char *orig = s;
726 static const char format[] = "-Djava.class.path=%s";
ksrinie1a34382012-04-24 10:37:01 -0700727 /*
728 * usually we should not get a null pointer, but there are cases where
729 * we might just get one, in which case we simply ignore it, and let the
730 * caller deal with it
731 */
732 if (s == NULL)
733 return;
duke6e45e102007-12-01 00:00:00 +0000734 s = JLI_WildcardExpandClasspath(s);
kizune0fc19542014-05-23 20:46:44 +0400735 if (sizeof(format) - 2 + JLI_StrLen(s) < JLI_StrLen(s))
736 // s is corrupted after wildcard expansion
737 return;
martinc0ca3352009-06-22 16:41:27 -0700738 def = JLI_MemAlloc(sizeof(format)
739 - 2 /* strlen("%s") */
740 + JLI_StrLen(s));
741 sprintf(def, format, s);
duke6e45e102007-12-01 00:00:00 +0000742 AddOption(def, NULL);
martinc0ca3352009-06-22 16:41:27 -0700743 if (s != orig)
744 JLI_MemFree((char *) s);
duke6e45e102007-12-01 00:00:00 +0000745}
746
747/*
748 * The SelectVersion() routine ensures that an appropriate version of
749 * the JRE is running. The specification for the appropriate version
750 * is obtained from either the manifest of a jar file (preferred) or
751 * from command line options.
752 * The routine also parses splash screen command line options and
753 * passes on their values in private environment variables.
754 */
755static void
756SelectVersion(int argc, char **argv, char **main_class)
757{
758 char *arg;
759 char **new_argv;
760 char **new_argp;
761 char *operand;
762 char *version = NULL;
763 char *jre = NULL;
764 int jarflag = 0;
765 int headlessflag = 0;
766 int restrict_search = -1; /* -1 implies not known */
767 manifest_info info;
768 char env_entry[MAXNAMELEN + 24] = ENV_ENTRY "=";
769 char *splash_file_name = NULL;
770 char *splash_jar_name = NULL;
771 char *env_in;
772 int res;
773
774 /*
775 * If the version has already been selected, set *main_class
776 * with the value passed through the environment (if any) and
777 * simply return.
778 */
779 if ((env_in = getenv(ENV_ENTRY)) != NULL) {
780 if (*env_in != '\0')
781 *main_class = JLI_StringDup(env_in);
782 return;
783 }
784
785 /*
786 * Scan through the arguments for options relevant to multiple JRE
787 * support. For reference, the command line syntax is defined as:
788 *
789 * SYNOPSIS
790 * java [options] class [argument...]
791 *
792 * java [options] -jar file.jar [argument...]
793 *
794 * As the scan is performed, make a copy of the argument list with
795 * the version specification options (new to 1.5) removed, so that
796 * a version less than 1.5 can be exec'd.
797 *
798 * Note that due to the syntax of the native Windows interface
799 * CreateProcess(), processing similar to the following exists in
800 * the Windows platform specific routine ExecJRE (in java_md.c).
801 * Changes here should be reproduced there.
802 */
803 new_argv = JLI_MemAlloc((argc + 1) * sizeof(char*));
804 new_argv[0] = argv[0];
805 new_argp = &new_argv[1];
806 argc--;
807 argv++;
808 while ((arg = *argv) != 0 && *arg == '-') {
809 if (JLI_StrCCmp(arg, "-version:") == 0) {
810 version = arg + 9;
811 } else if (JLI_StrCmp(arg, "-jre-restrict-search") == 0) {
812 restrict_search = 1;
813 } else if (JLI_StrCmp(arg, "-no-jre-restrict-search") == 0) {
814 restrict_search = 0;
815 } else {
816 if (JLI_StrCmp(arg, "-jar") == 0)
817 jarflag = 1;
818 /* deal with "unfortunate" classpath syntax */
819 if ((JLI_StrCmp(arg, "-classpath") == 0 || JLI_StrCmp(arg, "-cp") == 0) &&
820 (argc >= 2)) {
821 *new_argp++ = arg;
822 argc--;
823 argv++;
824 arg = *argv;
825 }
826
827 /*
828 * Checking for headless toolkit option in the some way as AWT does:
829 * "true" means true and any other value means false
830 */
831 if (JLI_StrCmp(arg, "-Djava.awt.headless=true") == 0) {
832 headlessflag = 1;
833 } else if (JLI_StrCCmp(arg, "-Djava.awt.headless=") == 0) {
834 headlessflag = 0;
835 } else if (JLI_StrCCmp(arg, "-splash:") == 0) {
836 splash_file_name = arg+8;
837 }
838 *new_argp++ = arg;
839 }
840 argc--;
841 argv++;
842 }
843 if (argc <= 0) { /* No operand? Possibly legit with -[full]version */
844 operand = NULL;
845 } else {
846 argc--;
847 *new_argp++ = operand = *argv++;
848 }
849 while (argc-- > 0) /* Copy over [argument...] */
850 *new_argp++ = *argv++;
851 *new_argp = NULL;
852
853 /*
854 * If there is a jar file, read the manifest. If the jarfile can't be
855 * read, the manifest can't be read from the jar file, or the manifest
856 * is corrupt, issue the appropriate error messages and exit.
857 *
858 * Even if there isn't a jar file, construct a manifest_info structure
859 * containing the command line information. It's a convenient way to carry
860 * this data around.
861 */
862 if (jarflag && operand) {
863 if ((res = JLI_ParseManifest(operand, &info)) != 0) {
864 if (res == -1)
ksrini0e817162008-08-26 10:21:20 -0700865 JLI_ReportErrorMessage(JAR_ERROR2, operand);
duke6e45e102007-12-01 00:00:00 +0000866 else
ksrini0e817162008-08-26 10:21:20 -0700867 JLI_ReportErrorMessage(JAR_ERROR3, operand);
duke6e45e102007-12-01 00:00:00 +0000868 exit(1);
869 }
870
871 /*
872 * Command line splash screen option should have precedence
873 * over the manifest, so the manifest data is used only if
874 * splash_file_name has not been initialized above during command
875 * line parsing
876 */
877 if (!headlessflag && !splash_file_name && info.splashscreen_image_file_name) {
878 splash_file_name = info.splashscreen_image_file_name;
879 splash_jar_name = operand;
880 }
881 } else {
882 info.manifest_version = NULL;
883 info.main_class = NULL;
884 info.jre_version = NULL;
885 info.jre_restrict_search = 0;
886 }
887
888 /*
889 * Passing on splash screen info in environment variables
890 */
891 if (splash_file_name && !headlessflag) {
892 char* splash_file_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_FILE_ENV_ENTRY "=")+JLI_StrLen(splash_file_name)+1);
893 JLI_StrCpy(splash_file_entry, SPLASH_FILE_ENV_ENTRY "=");
894 JLI_StrCat(splash_file_entry, splash_file_name);
895 putenv(splash_file_entry);
896 }
897 if (splash_jar_name && !headlessflag) {
898 char* splash_jar_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_JAR_ENV_ENTRY "=")+JLI_StrLen(splash_jar_name)+1);
899 JLI_StrCpy(splash_jar_entry, SPLASH_JAR_ENV_ENTRY "=");
900 JLI_StrCat(splash_jar_entry, splash_jar_name);
901 putenv(splash_jar_entry);
902 }
903
904 /*
905 * The JRE-Version and JRE-Restrict-Search values (if any) from the
906 * manifest are overwritten by any specified on the command line.
907 */
908 if (version != NULL)
909 info.jre_version = version;
910 if (restrict_search != -1)
911 info.jre_restrict_search = restrict_search;
912
913 /*
914 * "Valid" returns (other than unrecoverable errors) follow. Set
915 * main_class as a side-effect of this routine.
916 */
917 if (info.main_class != NULL)
918 *main_class = JLI_StringDup(info.main_class);
919
920 /*
921 * If no version selection information is found either on the command
922 * line or in the manifest, simply return.
923 */
924 if (info.jre_version == NULL) {
925 JLI_FreeManifest();
926 JLI_MemFree(new_argv);
927 return;
928 }
929
930 /*
931 * Check for correct syntax of the version specification (JSR 56).
932 */
933 if (!JLI_ValidVersionString(info.jre_version)) {
ksrini0e817162008-08-26 10:21:20 -0700934 JLI_ReportErrorMessage(SPC_ERROR1, info.jre_version);
duke6e45e102007-12-01 00:00:00 +0000935 exit(1);
936 }
937
938 /*
939 * Find the appropriate JVM on the system. Just to be as forgiving as
940 * possible, if the standard algorithms don't locate an appropriate
941 * jre, check to see if the one running will satisfy the requirements.
942 * This can happen on systems which haven't been set-up for multiple
943 * JRE support.
944 */
945 jre = LocateJRE(&info);
946 JLI_TraceLauncher("JRE-Version = %s, JRE-Restrict-Search = %s Selected = %s\n",
947 (info.jre_version?info.jre_version:"null"),
948 (info.jre_restrict_search?"true":"false"), (jre?jre:"null"));
949
950 if (jre == NULL) {
951 if (JLI_AcceptableRelease(GetFullVersion(), info.jre_version)) {
952 JLI_FreeManifest();
953 JLI_MemFree(new_argv);
954 return;
955 } else {
ksrini0e817162008-08-26 10:21:20 -0700956 JLI_ReportErrorMessage(CFG_ERROR4, info.jre_version);
duke6e45e102007-12-01 00:00:00 +0000957 exit(1);
958 }
959 }
960
961 /*
962 * If I'm not the chosen one, exec the chosen one. Returning from
963 * ExecJRE indicates that I am indeed the chosen one.
964 *
965 * The private environment variable _JAVA_VERSION_SET is used to
966 * prevent the chosen one from re-reading the manifest file and
967 * using the values found within to override the (potential) command
968 * line flags stripped from argv (because the target may not
969 * understand them). Passing the MainClass value is an optimization
970 * to avoid locating, expanding and parsing the manifest extra
971 * times.
972 */
ksrini8760ca92008-09-04 09:43:32 -0700973 if (info.main_class != NULL) {
974 if (JLI_StrLen(info.main_class) <= MAXNAMELEN) {
975 (void)JLI_StrCat(env_entry, info.main_class);
976 } else {
asaha80ccd422009-04-16 21:08:04 -0700977 JLI_ReportErrorMessage(CLS_ERROR5, MAXNAMELEN);
ksrini8760ca92008-09-04 09:43:32 -0700978 exit(1);
979 }
980 }
duke6e45e102007-12-01 00:00:00 +0000981 (void)putenv(env_entry);
982 ExecJRE(jre, new_argv);
983 JLI_FreeManifest();
984 JLI_MemFree(new_argv);
985 return;
986}
987
988/*
989 * Parses command line arguments. Returns JNI_FALSE if launcher
990 * should exit without starting vm, returns JNI_TRUE if vm needs
mchungea290e22011-01-21 09:43:57 -0800991 * to be started to process given options. *pret (the launcher
duke6e45e102007-12-01 00:00:00 +0000992 * process return value) is set to 0 for a normal exit.
993 */
994static jboolean
mchungea290e22011-01-21 09:43:57 -0800995ParseArguments(int *pargc, char ***pargv,
996 int *pmode, char **pwhat,
997 int *pret, const char *jrepath)
duke6e45e102007-12-01 00:00:00 +0000998{
999 int argc = *pargc;
1000 char **argv = *pargv;
mchungea290e22011-01-21 09:43:57 -08001001 int mode = LM_UNKNOWN;
duke6e45e102007-12-01 00:00:00 +00001002 char *arg;
1003
1004 *pret = 0;
1005
1006 while ((arg = *argv) != 0 && *arg == '-') {
1007 argv++; --argc;
1008 if (JLI_StrCmp(arg, "-classpath") == 0 || JLI_StrCmp(arg, "-cp") == 0) {
1009 ARG_CHECK (argc, ARG_ERROR1, arg);
1010 SetClassPath(*argv);
mchungea290e22011-01-21 09:43:57 -08001011 mode = LM_CLASS;
duke6e45e102007-12-01 00:00:00 +00001012 argv++; --argc;
1013 } else if (JLI_StrCmp(arg, "-jar") == 0) {
1014 ARG_CHECK (argc, ARG_ERROR2, arg);
mchungea290e22011-01-21 09:43:57 -08001015 mode = LM_JAR;
duke6e45e102007-12-01 00:00:00 +00001016 } else if (JLI_StrCmp(arg, "-help") == 0 ||
1017 JLI_StrCmp(arg, "-h") == 0 ||
1018 JLI_StrCmp(arg, "-?") == 0) {
1019 printUsage = JNI_TRUE;
1020 return JNI_TRUE;
1021 } else if (JLI_StrCmp(arg, "-version") == 0) {
1022 printVersion = JNI_TRUE;
1023 return JNI_TRUE;
1024 } else if (JLI_StrCmp(arg, "-showversion") == 0) {
1025 showVersion = JNI_TRUE;
1026 } else if (JLI_StrCmp(arg, "-X") == 0) {
1027 printXUsage = JNI_TRUE;
1028 return JNI_TRUE;
1029/*
ksrini8e20e1c2010-11-23 16:52:39 -08001030 * The following case checks for -XshowSettings OR -XshowSetting:SUBOPT.
1031 * In the latter case, any SUBOPT value not recognized will default to "all"
1032 */
1033 } else if (JLI_StrCmp(arg, "-XshowSettings") == 0 ||
1034 JLI_StrCCmp(arg, "-XshowSettings:") == 0) {
1035 showSettings = arg;
ksrini9636e2e2011-02-03 15:41:23 -08001036 } else if (JLI_StrCmp(arg, "-Xdiag") == 0) {
1037 AddOption("-Dsun.java.launcher.diag=true", NULL);
ksrini8e20e1c2010-11-23 16:52:39 -08001038/*
duke6e45e102007-12-01 00:00:00 +00001039 * The following case provide backward compatibility with old-style
1040 * command line options.
1041 */
1042 } else if (JLI_StrCmp(arg, "-fullversion") == 0) {
ksrini0e817162008-08-26 10:21:20 -07001043 JLI_ReportMessage("%s full version \"%s\"", _launcher_name, GetFullVersion());
duke6e45e102007-12-01 00:00:00 +00001044 return JNI_FALSE;
1045 } else if (JLI_StrCmp(arg, "-verbosegc") == 0) {
1046 AddOption("-verbose:gc", NULL);
1047 } else if (JLI_StrCmp(arg, "-t") == 0) {
1048 AddOption("-Xt", NULL);
1049 } else if (JLI_StrCmp(arg, "-tm") == 0) {
1050 AddOption("-Xtm", NULL);
1051 } else if (JLI_StrCmp(arg, "-debug") == 0) {
1052 AddOption("-Xdebug", NULL);
1053 } else if (JLI_StrCmp(arg, "-noclassgc") == 0) {
1054 AddOption("-Xnoclassgc", NULL);
1055 } else if (JLI_StrCmp(arg, "-Xfuture") == 0) {
1056 AddOption("-Xverify:all", NULL);
1057 } else if (JLI_StrCmp(arg, "-verify") == 0) {
1058 AddOption("-Xverify:all", NULL);
1059 } else if (JLI_StrCmp(arg, "-verifyremote") == 0) {
1060 AddOption("-Xverify:remote", NULL);
1061 } else if (JLI_StrCmp(arg, "-noverify") == 0) {
1062 AddOption("-Xverify:none", NULL);
1063 } else if (JLI_StrCCmp(arg, "-prof") == 0) {
1064 char *p = arg + 5;
1065 char *tmp = JLI_MemAlloc(JLI_StrLen(arg) + 50);
1066 if (*p) {
1067 sprintf(tmp, "-Xrunhprof:cpu=old,file=%s", p + 1);
1068 } else {
1069 sprintf(tmp, "-Xrunhprof:cpu=old,file=java.prof");
1070 }
1071 AddOption(tmp, NULL);
1072 } else if (JLI_StrCCmp(arg, "-ss") == 0 ||
1073 JLI_StrCCmp(arg, "-oss") == 0 ||
1074 JLI_StrCCmp(arg, "-ms") == 0 ||
1075 JLI_StrCCmp(arg, "-mx") == 0) {
1076 char *tmp = JLI_MemAlloc(JLI_StrLen(arg) + 6);
1077 sprintf(tmp, "-X%s", arg + 1); /* skip '-' */
1078 AddOption(tmp, NULL);
1079 } else if (JLI_StrCmp(arg, "-checksource") == 0 ||
1080 JLI_StrCmp(arg, "-cs") == 0 ||
1081 JLI_StrCmp(arg, "-noasyncgc") == 0) {
1082 /* No longer supported */
ksrini0e817162008-08-26 10:21:20 -07001083 JLI_ReportErrorMessage(ARG_WARN, arg);
duke6e45e102007-12-01 00:00:00 +00001084 } else if (JLI_StrCCmp(arg, "-version:") == 0 ||
1085 JLI_StrCmp(arg, "-no-jre-restrict-search") == 0 ||
1086 JLI_StrCmp(arg, "-jre-restrict-search") == 0 ||
1087 JLI_StrCCmp(arg, "-splash:") == 0) {
1088 ; /* Ignore machine independent options already handled */
michaelm5ac8c152012-03-06 20:34:38 +00001089 } else if (ProcessPlatformOption(arg)) {
1090 ; /* Processing of platform dependent options */
1091 } else if (RemovableOption(arg)) {
duke6e45e102007-12-01 00:00:00 +00001092 ; /* Do not pass option to vm. */
1093 } else {
1094 AddOption(arg, NULL);
1095 }
1096 }
1097
1098 if (--argc >= 0) {
mchungea290e22011-01-21 09:43:57 -08001099 *pwhat = *argv++;
1100 }
1101
1102 if (*pwhat == NULL) {
1103 *pret = 1;
1104 } else if (mode == LM_UNKNOWN) {
1105 /* default to LM_CLASS if -jar and -cp option are
1106 * not specified */
1107 mode = LM_CLASS;
1108 }
1109
1110 if (argc >= 0) {
duke6e45e102007-12-01 00:00:00 +00001111 *pargc = argc;
1112 *pargv = argv;
1113 }
mchungea290e22011-01-21 09:43:57 -08001114
1115 *pmode = mode;
1116
duke6e45e102007-12-01 00:00:00 +00001117 return JNI_TRUE;
1118}
1119
1120/*
1121 * Initializes the Java Virtual Machine. Also frees options array when
1122 * finished.
1123 */
1124static jboolean
1125InitializeJVM(JavaVM **pvm, JNIEnv **penv, InvocationFunctions *ifn)
1126{
1127 JavaVMInitArgs args;
1128 jint r;
1129
1130 memset(&args, 0, sizeof(args));
1131 args.version = JNI_VERSION_1_2;
1132 args.nOptions = numOptions;
1133 args.options = options;
1134 args.ignoreUnrecognized = JNI_FALSE;
1135
1136 if (JLI_IsTraceLauncher()) {
1137 int i = 0;
1138 printf("JavaVM args:\n ");
1139 printf("version 0x%08lx, ", (long)args.version);
1140 printf("ignoreUnrecognized is %s, ",
1141 args.ignoreUnrecognized ? "JNI_TRUE" : "JNI_FALSE");
1142 printf("nOptions is %ld\n", (long)args.nOptions);
1143 for (i = 0; i < numOptions; i++)
1144 printf(" option[%2d] = '%s'\n",
1145 i, args.options[i].optionString);
1146 }
1147
1148 r = ifn->CreateJavaVM(pvm, (void **)penv, &args);
1149 JLI_MemFree(options);
1150 return r == JNI_OK;
1151}
1152
ksrini7d9872e2011-03-20 08:41:33 -07001153static jclass helperClass = NULL;
1154
ksrinie97d4002012-07-31 06:14:28 -07001155jclass
1156GetLauncherHelperClass(JNIEnv *env)
1157{
ksrini7d9872e2011-03-20 08:41:33 -07001158 if (helperClass == NULL) {
1159 NULL_CHECK0(helperClass = FindBootStrapClass(env,
1160 "sun/launcher/LauncherHelper"));
duke6e45e102007-12-01 00:00:00 +00001161 }
ksrini7d9872e2011-03-20 08:41:33 -07001162 return helperClass;
duke6e45e102007-12-01 00:00:00 +00001163}
1164
ksrini7d9872e2011-03-20 08:41:33 -07001165static jmethodID makePlatformStringMID = NULL;
duke6e45e102007-12-01 00:00:00 +00001166/*
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);
duke6e45e102007-12-01 00:00:00 +00001173 jbyteArray ary;
ksrini7d9872e2011-03-20 08:41:33 -07001174 jclass cls = GetLauncherHelperClass(env);
1175 NULL_CHECK0(cls);
duke6e45e102007-12-01 00:00:00 +00001176 if (s == NULL)
1177 return 0;
duke6e45e102007-12-01 00:00:00 +00001178
1179 ary = (*env)->NewByteArray(env, len);
1180 if (ary != 0) {
1181 jstring str = 0;
1182 (*env)->SetByteArrayRegion(env, ary, 0, len, (jbyte *)s);
1183 if (!(*env)->ExceptionOccurred(env)) {
ksrini7d9872e2011-03-20 08:41:33 -07001184 if (makePlatformStringMID == NULL) {
1185 NULL_CHECK0(makePlatformStringMID = (*env)->GetStaticMethodID(env,
1186 cls, "makePlatformString", "(Z[B)Ljava/lang/String;"));
duke6e45e102007-12-01 00:00:00 +00001187 }
ksrini7d9872e2011-03-20 08:41:33 -07001188 str = (*env)->CallStaticObjectMethod(env, cls,
1189 makePlatformStringMID, USE_STDERR, ary);
duke6e45e102007-12-01 00:00:00 +00001190 (*env)->DeleteLocalRef(env, ary);
1191 return str;
1192 }
1193 }
1194 return 0;
1195}
1196
1197/*
1198 * Returns a new array of Java string objects for the specified
1199 * array of platform strings.
1200 */
ksrinie97d4002012-07-31 06:14:28 -07001201jobjectArray
duke6e45e102007-12-01 00:00:00 +00001202NewPlatformStringArray(JNIEnv *env, char **strv, int strc)
1203{
1204 jarray cls;
1205 jarray ary;
1206 int i;
1207
ksrini20a64b22008-09-24 15:07:41 -07001208 NULL_CHECK0(cls = FindBootStrapClass(env, "java/lang/String"));
duke6e45e102007-12-01 00:00:00 +00001209 NULL_CHECK0(ary = (*env)->NewObjectArray(env, strc, cls, 0));
1210 for (i = 0; i < strc; i++) {
1211 jstring str = NewPlatformString(env, *strv++);
1212 NULL_CHECK0(str);
1213 (*env)->SetObjectArrayElement(env, ary, i, str);
1214 (*env)->DeleteLocalRef(env, str);
1215 }
1216 return ary;
1217}
1218
1219/*
ksrini20a64b22008-09-24 15:07:41 -07001220 * Loads a class and verifies that the main class is present and it is ok to
1221 * call it for more details refer to the java implementation.
duke6e45e102007-12-01 00:00:00 +00001222 */
1223static jclass
mchungea290e22011-01-21 09:43:57 -08001224LoadMainClass(JNIEnv *env, int mode, char *name)
duke6e45e102007-12-01 00:00:00 +00001225{
ksrini20a64b22008-09-24 15:07:41 -07001226 jmethodID mid;
1227 jstring str;
1228 jobject result;
duke6e45e102007-12-01 00:00:00 +00001229 jlong start, end;
ksrini7d9872e2011-03-20 08:41:33 -07001230 jclass cls = GetLauncherHelperClass(env);
1231 NULL_CHECK0(cls);
ksrini20a64b22008-09-24 15:07:41 -07001232 if (JLI_IsTraceLauncher()) {
duke6e45e102007-12-01 00:00:00 +00001233 start = CounterGet();
ksrini20a64b22008-09-24 15:07:41 -07001234 }
ksrini7d9872e2011-03-20 08:41:33 -07001235 NULL_CHECK0(mid = (*env)->GetStaticMethodID(env, cls,
1236 "checkAndLoadMain",
1237 "(ZILjava/lang/String;)Ljava/lang/Class;"));
1238
ksriniac5e02f2012-01-11 08:14:47 -08001239 str = NewPlatformString(env, name);
ksrini7d9872e2011-03-20 08:41:33 -07001240 result = (*env)->CallStaticObjectMethod(env, cls, mid, USE_STDERR, mode, str);
duke6e45e102007-12-01 00:00:00 +00001241
1242 if (JLI_IsTraceLauncher()) {
1243 end = CounterGet();
1244 printf("%ld micro seconds to load main class\n",
1245 (long)(jint)Counter2Micros(end-start));
ksrinie97d4002012-07-31 06:14:28 -07001246 printf("----%s----\n", JLDEBUG_ENV_ENTRY);
duke6e45e102007-12-01 00:00:00 +00001247 }
1248
ksrini20a64b22008-09-24 15:07:41 -07001249 return (jclass)result;
duke6e45e102007-12-01 00:00:00 +00001250}
1251
ksrini6b41ee82012-11-19 19:49:38 -08001252static jclass
1253GetApplicationClass(JNIEnv *env)
1254{
1255 jmethodID mid;
1256 jobject result;
1257 jclass cls = GetLauncherHelperClass(env);
1258 NULL_CHECK0(cls);
1259 NULL_CHECK0(mid = (*env)->GetStaticMethodID(env, cls,
1260 "getApplicationClass",
1261 "()Ljava/lang/Class;"));
1262
1263 return (*env)->CallStaticObjectMethod(env, cls, mid);
1264}
1265
duke6e45e102007-12-01 00:00:00 +00001266/*
1267 * For tools, convert command line args thus:
1268 * javac -cp foo:foo/"*" -J-ms32m ...
1269 * java -ms32m -cp JLI_WildcardExpandClasspath(foo:foo/"*") ...
1270 *
1271 * Takes 4 parameters, and returns the populated arguments
1272 */
1273static void
1274TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv)
1275{
1276 int argc = *pargc;
1277 char **argv = *pargv;
1278 int nargc = argc + jargc;
1279 char **nargv = JLI_MemAlloc((nargc + 1) * sizeof(char *));
1280 int i;
1281
1282 *pargc = nargc;
1283 *pargv = nargv;
1284
1285 /* Copy the VM arguments (i.e. prefixed with -J) */
1286 for (i = 0; i < jargc; i++) {
1287 const char *arg = jargv[i];
1288 if (arg[0] == '-' && arg[1] == 'J') {
1289 *nargv++ = ((arg + 2) == NULL) ? NULL : JLI_StringDup(arg + 2);
1290 }
1291 }
1292
1293 for (i = 0; i < argc; i++) {
1294 char *arg = argv[i];
1295 if (arg[0] == '-' && arg[1] == 'J') {
1296 if (arg[2] == '\0') {
ksrini0e817162008-08-26 10:21:20 -07001297 JLI_ReportErrorMessage(ARG_ERROR3);
duke6e45e102007-12-01 00:00:00 +00001298 exit(1);
1299 }
1300 *nargv++ = arg + 2;
1301 }
1302 }
1303
1304 /* Copy the rest of the arguments */
1305 for (i = 0; i < jargc ; i++) {
1306 const char *arg = jargv[i];
1307 if (arg[0] != '-' || arg[1] != 'J') {
1308 *nargv++ = (arg == NULL) ? NULL : JLI_StringDup(arg);
1309 }
1310 }
1311 for (i = 0; i < argc; i++) {
1312 char *arg = argv[i];
1313 if (arg[0] == '-') {
1314 if (arg[1] == 'J')
1315 continue;
1316 if (IsWildCardEnabled() && arg[1] == 'c'
1317 && (JLI_StrCmp(arg, "-cp") == 0 ||
1318 JLI_StrCmp(arg, "-classpath") == 0)
1319 && i < argc - 1) {
1320 *nargv++ = arg;
1321 *nargv++ = (char *) JLI_WildcardExpandClasspath(argv[i+1]);
1322 i++;
1323 continue;
1324 }
1325 }
1326 *nargv++ = arg;
1327 }
1328 *nargv = 0;
1329}
1330
1331/*
1332 * For our tools, we try to add 3 VM options:
1333 * -Denv.class.path=<envcp>
1334 * -Dapplication.home=<apphome>
1335 * -Djava.class.path=<appcp>
1336 * <envcp> is the user's setting of CLASSPATH -- for instance the user
1337 * tells javac where to find binary classes through this environment
1338 * variable. Notice that users will be able to compile against our
1339 * tools classes (sun.tools.javac.Main) only if they explicitly add
1340 * tools.jar to CLASSPATH.
1341 * <apphome> is the directory where the application is installed.
1342 * <appcp> is the classpath to where our apps' classfiles are.
1343 */
1344static jboolean
1345AddApplicationOptions(int cpathc, const char **cpathv)
1346{
1347 char *envcp, *appcp, *apphome;
1348 char home[MAXPATHLEN]; /* application home */
1349 char separator[] = { PATH_SEPARATOR, '\0' };
1350 int size, i;
1351
1352 {
1353 const char *s = getenv("CLASSPATH");
1354 if (s) {
1355 s = (char *) JLI_WildcardExpandClasspath(s);
1356 /* 40 for -Denv.class.path= */
kizune0fc19542014-05-23 20:46:44 +04001357 if (JLI_StrLen(s) + 40 > JLI_StrLen(s)) { // Safeguard from overflow
1358 envcp = (char *)JLI_MemAlloc(JLI_StrLen(s) + 40);
1359 sprintf(envcp, "-Denv.class.path=%s", s);
1360 AddOption(envcp, NULL);
1361 }
duke6e45e102007-12-01 00:00:00 +00001362 }
1363 }
1364
1365 if (!GetApplicationHome(home, sizeof(home))) {
ksrini0e817162008-08-26 10:21:20 -07001366 JLI_ReportErrorMessage(CFG_ERROR5);
duke6e45e102007-12-01 00:00:00 +00001367 return JNI_FALSE;
1368 }
1369
1370 /* 40 for '-Dapplication.home=' */
1371 apphome = (char *)JLI_MemAlloc(JLI_StrLen(home) + 40);
1372 sprintf(apphome, "-Dapplication.home=%s", home);
1373 AddOption(apphome, NULL);
1374
1375 /* How big is the application's classpath? */
1376 size = 40; /* 40: "-Djava.class.path=" */
1377 for (i = 0; i < cpathc; i++) {
1378 size += (int)JLI_StrLen(home) + (int)JLI_StrLen(cpathv[i]) + 1; /* 1: separator */
1379 }
1380 appcp = (char *)JLI_MemAlloc(size + 1);
1381 JLI_StrCpy(appcp, "-Djava.class.path=");
1382 for (i = 0; i < cpathc; i++) {
1383 JLI_StrCat(appcp, home); /* c:\program files\myapp */
1384 JLI_StrCat(appcp, cpathv[i]); /* \lib\myapp.jar */
1385 JLI_StrCat(appcp, separator); /* ; */
1386 }
1387 appcp[JLI_StrLen(appcp)-1] = '\0'; /* remove trailing path separator */
1388 AddOption(appcp, NULL);
1389 return JNI_TRUE;
1390}
1391
1392/*
1393 * inject the -Dsun.java.command pseudo property into the args structure
1394 * this pseudo property is used in the HotSpot VM to expose the
1395 * Java class name and arguments to the main method to the VM. The
1396 * HotSpot VM uses this pseudo property to store the Java class name
1397 * (or jar file name) and the arguments to the class's main method
1398 * to the instrumentation memory region. The sun.java.command pseudo
1399 * property is not exported by HotSpot to the Java layer.
1400 */
1401void
mchungea290e22011-01-21 09:43:57 -08001402SetJavaCommandLineProp(char *what, int argc, char **argv)
duke6e45e102007-12-01 00:00:00 +00001403{
1404
1405 int i = 0;
1406 size_t len = 0;
1407 char* javaCommand = NULL;
1408 char* dashDstr = "-Dsun.java.command=";
1409
mchungea290e22011-01-21 09:43:57 -08001410 if (what == NULL) {
duke6e45e102007-12-01 00:00:00 +00001411 /* unexpected, one of these should be set. just return without
1412 * setting the property
1413 */
1414 return;
1415 }
1416
duke6e45e102007-12-01 00:00:00 +00001417 /* determine the amount of memory to allocate assuming
1418 * the individual components will be space separated
1419 */
mchungea290e22011-01-21 09:43:57 -08001420 len = JLI_StrLen(what);
duke6e45e102007-12-01 00:00:00 +00001421 for (i = 0; i < argc; i++) {
1422 len += JLI_StrLen(argv[i]) + 1;
1423 }
1424
1425 /* allocate the memory */
1426 javaCommand = (char*) JLI_MemAlloc(len + JLI_StrLen(dashDstr) + 1);
1427
1428 /* build the -D string */
1429 *javaCommand = '\0';
1430 JLI_StrCat(javaCommand, dashDstr);
mchungea290e22011-01-21 09:43:57 -08001431 JLI_StrCat(javaCommand, what);
duke6e45e102007-12-01 00:00:00 +00001432
1433 for (i = 0; i < argc; i++) {
1434 /* the components of the string are space separated. In
1435 * the case of embedded white space, the relationship of
1436 * the white space separated components to their true
1437 * positional arguments will be ambiguous. This issue may
1438 * be addressed in a future release.
1439 */
1440 JLI_StrCat(javaCommand, " ");
1441 JLI_StrCat(javaCommand, argv[i]);
1442 }
1443
1444 AddOption(javaCommand, NULL);
1445}
1446
1447/*
1448 * JVM would like to know if it's created by a standard Sun launcher, or by
1449 * user native application, the following property indicates the former.
1450 */
mchungea290e22011-01-21 09:43:57 -08001451void
1452SetJavaLauncherProp() {
duke6e45e102007-12-01 00:00:00 +00001453 AddOption("-Dsun.java.launcher=SUN_STANDARD", NULL);
1454}
1455
1456/*
1457 * Prints the version information from the java.version and other properties.
1458 */
1459static void
1460PrintJavaVersion(JNIEnv *env, jboolean extraLF)
1461{
1462 jclass ver;
1463 jmethodID print;
1464
ksrini20a64b22008-09-24 15:07:41 -07001465 NULL_CHECK(ver = FindBootStrapClass(env, "sun/misc/Version"));
duke6e45e102007-12-01 00:00:00 +00001466 NULL_CHECK(print = (*env)->GetStaticMethodID(env,
1467 ver,
1468 (extraLF == JNI_TRUE) ? "println" : "print",
1469 "()V"
1470 )
1471 );
1472
1473 (*env)->CallStaticVoidMethod(env, ver, print);
1474}
1475
1476/*
ksrini8e20e1c2010-11-23 16:52:39 -08001477 * Prints all the Java settings, see the java implementation for more details.
1478 */
1479static void
1480ShowSettings(JNIEnv *env, char *optString)
1481{
ksrini8e20e1c2010-11-23 16:52:39 -08001482 jmethodID showSettingsID;
1483 jstring joptString;
ksrini7d9872e2011-03-20 08:41:33 -07001484 jclass cls = GetLauncherHelperClass(env);
1485 NULL_CHECK(cls);
ksrini8e20e1c2010-11-23 16:52:39 -08001486 NULL_CHECK(showSettingsID = (*env)->GetStaticMethodID(env, cls,
ksrini6d575f82010-12-23 13:51:30 -08001487 "showSettings", "(ZLjava/lang/String;JJJZ)V"));
ksrini8e20e1c2010-11-23 16:52:39 -08001488 joptString = (*env)->NewStringUTF(env, optString);
1489 (*env)->CallStaticVoidMethod(env, cls, showSettingsID,
ksrini7d9872e2011-03-20 08:41:33 -07001490 USE_STDERR,
ksrini8e20e1c2010-11-23 16:52:39 -08001491 joptString,
ksrini6d575f82010-12-23 13:51:30 -08001492 (jlong)initialHeapSize,
1493 (jlong)maxHeapSize,
ksrini8e20e1c2010-11-23 16:52:39 -08001494 (jlong)threadStackSize,
1495 ServerClassMachine());
1496}
1497
1498/*
ksrini20a64b22008-09-24 15:07:41 -07001499 * Prints default usage or the Xusage message, see sun.launcher.LauncherHelper.java
duke6e45e102007-12-01 00:00:00 +00001500 */
1501static void
1502PrintUsage(JNIEnv* env, jboolean doXUsage)
1503{
duke6e45e102007-12-01 00:00:00 +00001504 jmethodID initHelp, vmSelect, vmSynonym, vmErgo, printHelp, printXUsageMessage;
1505 jstring jprogname, vm1, vm2;
1506 int i;
ksrini7d9872e2011-03-20 08:41:33 -07001507 jclass cls = GetLauncherHelperClass(env);
1508 NULL_CHECK(cls);
duke6e45e102007-12-01 00:00:00 +00001509 if (doXUsage) {
1510 NULL_CHECK(printXUsageMessage = (*env)->GetStaticMethodID(env, cls,
1511 "printXUsageMessage", "(Z)V"));
ksrini7d9872e2011-03-20 08:41:33 -07001512 (*env)->CallStaticVoidMethod(env, cls, printXUsageMessage, USE_STDERR);
duke6e45e102007-12-01 00:00:00 +00001513 } else {
1514 NULL_CHECK(initHelp = (*env)->GetStaticMethodID(env, cls,
1515 "initHelpMessage", "(Ljava/lang/String;)V"));
1516
1517 NULL_CHECK(vmSelect = (*env)->GetStaticMethodID(env, cls, "appendVmSelectMessage",
1518 "(Ljava/lang/String;Ljava/lang/String;)V"));
1519
1520 NULL_CHECK(vmSynonym = (*env)->GetStaticMethodID(env, cls,
1521 "appendVmSynonymMessage",
1522 "(Ljava/lang/String;Ljava/lang/String;)V"));
1523 NULL_CHECK(vmErgo = (*env)->GetStaticMethodID(env, cls,
1524 "appendVmErgoMessage", "(ZLjava/lang/String;)V"));
1525
1526 NULL_CHECK(printHelp = (*env)->GetStaticMethodID(env, cls,
1527 "printHelpMessage", "(Z)V"));
1528
1529 jprogname = (*env)->NewStringUTF(env, _program_name);
1530
1531 /* Initialize the usage message with the usual preamble */
1532 (*env)->CallStaticVoidMethod(env, cls, initHelp, jprogname);
1533
1534
1535 /* Assemble the other variant part of the usage */
1536 if ((knownVMs[0].flag == VM_KNOWN) ||
1537 (knownVMs[0].flag == VM_IF_SERVER_CLASS)) {
1538 vm1 = (*env)->NewStringUTF(env, knownVMs[0].name);
1539 vm2 = (*env)->NewStringUTF(env, knownVMs[0].name+1);
1540 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1541 }
1542 for (i=1; i<knownVMsCount; i++) {
1543 if (knownVMs[i].flag == VM_KNOWN) {
1544 vm1 = (*env)->NewStringUTF(env, knownVMs[i].name);
1545 vm2 = (*env)->NewStringUTF(env, knownVMs[i].name+1);
1546 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1547 }
1548 }
1549 for (i=1; i<knownVMsCount; i++) {
1550 if (knownVMs[i].flag == VM_ALIASED_TO) {
1551 vm1 = (*env)->NewStringUTF(env, knownVMs[i].name);
1552 vm2 = (*env)->NewStringUTF(env, knownVMs[i].alias+1);
1553 (*env)->CallStaticVoidMethod(env, cls, vmSynonym, vm1, vm2);
1554 }
1555 }
1556
1557 /* The first known VM is the default */
1558 {
1559 jboolean isServerClassMachine = ServerClassMachine();
1560
1561 const char* defaultVM = knownVMs[0].name+1;
1562 if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) && isServerClassMachine) {
1563 defaultVM = knownVMs[0].server_class+1;
1564 }
1565
1566 vm1 = (*env)->NewStringUTF(env, defaultVM);
1567 (*env)->CallStaticVoidMethod(env, cls, vmErgo, isServerClassMachine, vm1);
1568 }
1569
1570 /* Complete the usage message and print to stderr*/
ksrini7d9872e2011-03-20 08:41:33 -07001571 (*env)->CallStaticVoidMethod(env, cls, printHelp, USE_STDERR);
duke6e45e102007-12-01 00:00:00 +00001572 }
1573 return;
1574}
1575
1576/*
1577 * Read the jvm.cfg file and fill the knownJVMs[] array.
1578 *
1579 * The functionality of the jvm.cfg file is subject to change without
1580 * notice and the mechanism will be removed in the future.
1581 *
1582 * The lexical structure of the jvm.cfg file is as follows:
1583 *
1584 * jvmcfg := { vmLine }
1585 * vmLine := knownLine
1586 * | aliasLine
1587 * | warnLine
1588 * | ignoreLine
1589 * | errorLine
1590 * | predicateLine
1591 * | commentLine
1592 * knownLine := flag "KNOWN" EOL
1593 * warnLine := flag "WARN" EOL
1594 * ignoreLine := flag "IGNORE" EOL
1595 * errorLine := flag "ERROR" EOL
1596 * aliasLine := flag "ALIASED_TO" flag EOL
1597 * predicateLine := flag "IF_SERVER_CLASS" flag EOL
1598 * commentLine := "#" text EOL
1599 * flag := "-" identifier
1600 *
1601 * The semantics are that when someone specifies a flag on the command line:
1602 * - if the flag appears on a knownLine, then the identifier is used as
1603 * the name of the directory holding the JVM library (the name of the JVM).
1604 * - if the flag appears as the first flag on an aliasLine, the identifier
1605 * of the second flag is used as the name of the JVM.
1606 * - if the flag appears on a warnLine, the identifier is used as the
1607 * name of the JVM, but a warning is generated.
1608 * - if the flag appears on an ignoreLine, the identifier is recognized as the
1609 * name of a JVM, but the identifier is ignored and the default vm used
1610 * - if the flag appears on an errorLine, an error is generated.
1611 * - if the flag appears as the first flag on a predicateLine, and
1612 * the machine on which you are running passes the predicate indicated,
1613 * then the identifier of the second flag is used as the name of the JVM,
1614 * otherwise the identifier of the first flag is used as the name of the JVM.
1615 * If no flag is given on the command line, the first vmLine of the jvm.cfg
1616 * file determines the name of the JVM.
1617 * PredicateLines are only interpreted on first vmLine of a jvm.cfg file,
1618 * since they only make sense if someone hasn't specified the name of the
1619 * JVM on the command line.
1620 *
1621 * The intent of the jvm.cfg file is to allow several JVM libraries to
1622 * be installed in different subdirectories of a single JRE installation,
1623 * for space-savings and convenience in testing.
1624 * The intent is explicitly not to provide a full aliasing or predicate
1625 * mechanism.
1626 */
1627jint
michaelm5ac8c152012-03-06 20:34:38 +00001628ReadKnownVMs(const char *jvmCfgName, jboolean speculative)
duke6e45e102007-12-01 00:00:00 +00001629{
1630 FILE *jvmCfg;
duke6e45e102007-12-01 00:00:00 +00001631 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 }
duke6e45e102007-12-01 00:00:00 +00001643
1644 jvmCfg = fopen(jvmCfgName, "r");
1645 if (jvmCfg == NULL) {
1646 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -07001647 JLI_ReportErrorMessage(CFG_ERROR6, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001648 exit(1);
1649 } else {
1650 return -1;
1651 }
1652 }
1653 while (fgets(line, sizeof(line), jvmCfg) != NULL) {
1654 vmType = VM_UNKNOWN;
1655 lineno++;
1656 if (line[0] == '#')
1657 continue;
1658 if (line[0] != '-') {
ksrini0e817162008-08-26 10:21:20 -07001659 JLI_ReportErrorMessage(CFG_WARN2, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001660 }
1661 if (cnt >= knownVMsLimit) {
1662 GrowKnownVMs(cnt);
1663 }
1664 line[JLI_StrLen(line)-1] = '\0'; /* remove trailing newline */
1665 tmpPtr = line + JLI_StrCSpn(line, whiteSpace);
1666 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001667 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001668 } else {
1669 /* Null-terminate this string for JLI_StringDup below */
1670 *tmpPtr++ = 0;
1671 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1672 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001673 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001674 } else {
1675 if (!JLI_StrCCmp(tmpPtr, "KNOWN")) {
1676 vmType = VM_KNOWN;
1677 } else if (!JLI_StrCCmp(tmpPtr, "ALIASED_TO")) {
1678 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1679 if (*tmpPtr != 0) {
1680 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1681 }
1682 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001683 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001684 } else {
1685 /* Null terminate altVMName */
1686 altVMName = tmpPtr;
1687 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1688 *tmpPtr = 0;
1689 vmType = VM_ALIASED_TO;
1690 }
1691 } else if (!JLI_StrCCmp(tmpPtr, "WARN")) {
1692 vmType = VM_WARN;
1693 } else if (!JLI_StrCCmp(tmpPtr, "IGNORE")) {
1694 vmType = VM_IGNORE;
1695 } else if (!JLI_StrCCmp(tmpPtr, "ERROR")) {
1696 vmType = VM_ERROR;
1697 } else if (!JLI_StrCCmp(tmpPtr, "IF_SERVER_CLASS")) {
1698 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1699 if (*tmpPtr != 0) {
1700 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1701 }
1702 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001703 JLI_ReportErrorMessage(CFG_WARN4, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001704 } else {
1705 /* Null terminate server class VM name */
1706 serverClassVMName = tmpPtr;
1707 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1708 *tmpPtr = 0;
1709 vmType = VM_IF_SERVER_CLASS;
1710 }
1711 } else {
ksrini0e817162008-08-26 10:21:20 -07001712 JLI_ReportErrorMessage(CFG_WARN5, lineno, &jvmCfgName[0]);
duke6e45e102007-12-01 00:00:00 +00001713 vmType = VM_KNOWN;
1714 }
1715 }
1716 }
1717
1718 JLI_TraceLauncher("jvm.cfg[%d] = ->%s<-\n", cnt, line);
1719 if (vmType != VM_UNKNOWN) {
1720 knownVMs[cnt].name = JLI_StringDup(line);
1721 knownVMs[cnt].flag = vmType;
1722 switch (vmType) {
1723 default:
1724 break;
1725 case VM_ALIASED_TO:
1726 knownVMs[cnt].alias = JLI_StringDup(altVMName);
1727 JLI_TraceLauncher(" name: %s vmType: %s alias: %s\n",
1728 knownVMs[cnt].name, "VM_ALIASED_TO", knownVMs[cnt].alias);
1729 break;
1730 case VM_IF_SERVER_CLASS:
1731 knownVMs[cnt].server_class = JLI_StringDup(serverClassVMName);
1732 JLI_TraceLauncher(" name: %s vmType: %s server_class: %s\n",
1733 knownVMs[cnt].name, "VM_IF_SERVER_CLASS", knownVMs[cnt].server_class);
1734 break;
1735 }
1736 cnt++;
1737 }
1738 }
1739 fclose(jvmCfg);
1740 knownVMsCount = cnt;
1741
1742 if (JLI_IsTraceLauncher()) {
1743 end = CounterGet();
1744 printf("%ld micro seconds to parse jvm.cfg\n",
1745 (long)(jint)Counter2Micros(end-start));
1746 }
1747
1748 return cnt;
1749}
1750
1751
1752static void
1753GrowKnownVMs(int minimum)
1754{
1755 struct vmdesc* newKnownVMs;
1756 int newMax;
1757
1758 newMax = (knownVMsLimit == 0 ? INIT_MAX_KNOWN_VMS : (2 * knownVMsLimit));
1759 if (newMax <= minimum) {
1760 newMax = minimum;
1761 }
1762 newKnownVMs = (struct vmdesc*) JLI_MemAlloc(newMax * sizeof(struct vmdesc));
1763 if (knownVMs != NULL) {
1764 memcpy(newKnownVMs, knownVMs, knownVMsLimit * sizeof(struct vmdesc));
1765 }
1766 JLI_MemFree(knownVMs);
1767 knownVMs = newKnownVMs;
1768 knownVMsLimit = newMax;
1769}
1770
1771
1772/* Returns index of VM or -1 if not found */
1773static int
1774KnownVMIndex(const char* name)
1775{
1776 int i;
1777 if (JLI_StrCCmp(name, "-J") == 0) name += 2;
1778 for (i = 0; i < knownVMsCount; i++) {
1779 if (!JLI_StrCmp(name, knownVMs[i].name)) {
1780 return i;
1781 }
1782 }
1783 return -1;
1784}
1785
1786static void
1787FreeKnownVMs()
1788{
1789 int i;
1790 for (i = 0; i < knownVMsCount; i++) {
1791 JLI_MemFree(knownVMs[i].name);
1792 knownVMs[i].name = NULL;
1793 }
1794 JLI_MemFree(knownVMs);
1795}
1796
duke6e45e102007-12-01 00:00:00 +00001797/*
1798 * Displays the splash screen according to the jar file name
1799 * and image file names stored in environment variables
1800 */
michaelm5ac8c152012-03-06 20:34:38 +00001801void
duke6e45e102007-12-01 00:00:00 +00001802ShowSplashScreen()
1803{
1804 const char *jar_name = getenv(SPLASH_JAR_ENV_ENTRY);
1805 const char *file_name = getenv(SPLASH_FILE_ENV_ENTRY);
1806 int data_size;
1807 void *image_data;
1808 if (jar_name) {
1809 image_data = JLI_JarUnpackFile(jar_name, file_name, &data_size);
1810 if (image_data) {
1811 DoSplashInit();
1812 DoSplashLoadMemory(image_data, data_size);
1813 JLI_MemFree(image_data);
1814 }
1815 } else if (file_name) {
1816 DoSplashInit();
1817 DoSplashLoadFile(file_name);
1818 } else {
1819 return;
1820 }
1821 DoSplashSetFileJarName(file_name, jar_name);
1822
1823 /*
1824 * Done with all command line processing and potential re-execs so
1825 * clean up the environment.
1826 */
1827 (void)UnsetEnv(ENV_ENTRY);
1828 (void)UnsetEnv(SPLASH_FILE_ENV_ENTRY);
1829 (void)UnsetEnv(SPLASH_JAR_ENV_ENTRY);
1830
1831 JLI_MemFree(splash_jar_entry);
1832 JLI_MemFree(splash_file_entry);
1833
1834}
1835
1836const char*
1837GetDotVersion()
1838{
1839 return _dVersion;
1840}
1841
1842const char*
1843GetFullVersion()
1844{
1845 return _fVersion;
1846}
1847
1848const char*
1849GetProgramName()
1850{
1851 return _program_name;
1852}
1853
1854const char*
1855GetLauncherName()
1856{
1857 return _launcher_name;
1858}
1859
1860jint
1861GetErgoPolicy()
1862{
1863 return _ergo_policy;
1864}
1865
1866jboolean
1867IsJavaArgs()
1868{
1869 return _is_java_args;
1870}
1871
1872static jboolean
1873IsWildCardEnabled()
1874{
1875 return _wc_enabled;
1876}
1877
michaelm5ac8c152012-03-06 20:34:38 +00001878int
1879ContinueInNewThread(InvocationFunctions* ifn, jlong threadStackSize,
1880 int argc, char **argv,
mchungea290e22011-01-21 09:43:57 -08001881 int mode, char *what, int ret)
duke6e45e102007-12-01 00:00:00 +00001882{
1883
1884 /*
1885 * If user doesn't specify stack size, check if VM has a preference.
1886 * Note that HotSpot no longer supports JNI_VERSION_1_1 but it will
1887 * return its default stack size through the init args structure.
1888 */
1889 if (threadStackSize == 0) {
1890 struct JDK1_1InitArgs args1_1;
1891 memset((void*)&args1_1, 0, sizeof(args1_1));
1892 args1_1.version = JNI_VERSION_1_1;
1893 ifn->GetDefaultJavaVMInitArgs(&args1_1); /* ignore return value */
1894 if (args1_1.javaStackSize > 0) {
1895 threadStackSize = args1_1.javaStackSize;
1896 }
1897 }
1898
1899 { /* Create a new thread to create JVM and invoke main method */
1900 JavaMainArgs args;
1901 int rslt;
1902
1903 args.argc = argc;
1904 args.argv = argv;
mchungea290e22011-01-21 09:43:57 -08001905 args.mode = mode;
1906 args.what = what;
duke6e45e102007-12-01 00:00:00 +00001907 args.ifn = *ifn;
1908
1909 rslt = ContinueInNewThread0(JavaMain, threadStackSize, (void*)&args);
1910 /* If the caller has deemed there is an error we
1911 * simply return that, otherwise we return the value of
1912 * the callee
1913 */
1914 return (ret != 0) ? ret : rslt;
1915 }
1916}
1917
1918static void
1919DumpState()
1920{
1921 if (!JLI_IsTraceLauncher()) return ;
1922 printf("Launcher state:\n");
1923 printf("\tdebug:%s\n", (JLI_IsTraceLauncher() == JNI_TRUE) ? "on" : "off");
1924 printf("\tjavargs:%s\n", (_is_java_args == JNI_TRUE) ? "on" : "off");
1925 printf("\tprogram name:%s\n", GetProgramName());
1926 printf("\tlauncher name:%s\n", GetLauncherName());
1927 printf("\tjavaw:%s\n", (IsJavaw() == JNI_TRUE) ? "on" : "off");
1928 printf("\tfullversion:%s\n", GetFullVersion());
1929 printf("\tdotversion:%s\n", GetDotVersion());
1930 printf("\tergo_policy:");
1931 switch(GetErgoPolicy()) {
1932 case NEVER_SERVER_CLASS:
1933 printf("NEVER_ACT_AS_A_SERVER_CLASS_MACHINE\n");
1934 break;
1935 case ALWAYS_SERVER_CLASS:
1936 printf("ALWAYS_ACT_AS_A_SERVER_CLASS_MACHINE\n");
1937 break;
1938 default:
1939 printf("DEFAULT_ERGONOMICS_POLICY\n");
1940 }
1941}
1942
1943/*
1944 * Return JNI_TRUE for an option string that has no effect but should
1945 * _not_ be passed on to the vm; return JNI_FALSE otherwise. On
1946 * Solaris SPARC, this screening needs to be done if:
ksrini11e7f1b2009-11-20 11:01:32 -08001947 * -d32 or -d64 is passed to a binary with an unmatched data model
1948 * (the exec in CreateExecutionEnvironment removes -d<n> options and points the
1949 * exec to the proper binary). In the case of when the data model and the
1950 * requested version is matched, an exec would not occur, and these options
1951 * were erroneously passed to the vm.
duke6e45e102007-12-01 00:00:00 +00001952 */
1953jboolean
1954RemovableOption(char * option)
1955{
1956 /*
1957 * Unconditionally remove both -d32 and -d64 options since only
1958 * the last such options has an effect; e.g.
1959 * java -d32 -d64 -d32 -version
1960 * is equivalent to
1961 * java -d32 -version
1962 */
1963
1964 if( (JLI_StrCCmp(option, "-d32") == 0 ) ||
1965 (JLI_StrCCmp(option, "-d64") == 0 ) )
1966 return JNI_TRUE;
1967 else
1968 return JNI_FALSE;
1969}
1970
1971/*
1972 * A utility procedure to always print to stderr
1973 */
1974void
ksrini0e817162008-08-26 10:21:20 -07001975JLI_ReportMessage(const char* fmt, ...)
duke6e45e102007-12-01 00:00:00 +00001976{
1977 va_list vl;
1978 va_start(vl, fmt);
1979 vfprintf(stderr, fmt, vl);
1980 fprintf(stderr, "\n");
1981 va_end(vl);
1982}