blob: 2d1149cd9204e3ffd21e304d5dd3c087f0b6e9f6 [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);
mchung1c2c2d42009-12-18 11:42:03 -080097static void SetModulesBootClassPath(const char *s);
duke6e45e102007-12-01 00:00:00 +000098static void SelectVersion(int argc, char **argv, char **main_class);
99static jboolean ParseArguments(int *pargc, char ***pargv, char **pjarfile,
100 char **pclassname, int *pret, const char *jvmpath);
101static 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);
ksrini20a64b22008-09-24 15:07:41 -0700105static jclass LoadMainClass(JNIEnv *env, jboolean isJar, 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 */
ksrini6d575f82010-12-23 13:51:30 -0800161static jlong threadStackSize = 0; /* stack size of the new thread */
162static 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
167typedef struct {
168 int argc;
169 char ** argv;
170 char * jarfile;
171 char * classname;
172 InvocationFunctions ifn;
173} JavaMainArgs;
174
175/*
176 * Entry point.
177 */
178int
179JLI_Launch(int argc, char ** argv, /* main argc, argc */
180 int jargc, const char** jargv, /* java args */
181 int appclassc, const char** appclassv, /* app classpath */
182 const char* fullversion, /* full version defined */
183 const char* dotversion, /* dot version defined */
184 const char* pname, /* program name */
185 const char* lname, /* launcher name */
186 jboolean javaargs, /* JAVA_ARGS */
187 jboolean cpwildcard, /* classpath wildcard*/
188 jboolean javaw, /* windows-only javaw */
189 jint ergo /* ergonomics class policy */
190)
191{
192 char *jarfile = 0;
193 char *classname = 0;
194 char *cpath = 0;
195 char *main_class = NULL;
196 int ret;
197 InvocationFunctions ifn;
198 jlong start, end;
ksrinie3ec45d2010-07-09 11:04:34 -0700199 char jvmpath[MAXPATHLEN];
200 char jrepath[MAXPATHLEN];
duke6e45e102007-12-01 00:00:00 +0000201
202 _fVersion = fullversion;
203 _dVersion = dotversion;
204 _launcher_name = lname;
205 _program_name = pname;
206 _is_java_args = javaargs;
207 _wc_enabled = cpwildcard;
208 _ergo_policy = ergo;
209
ksrini52cded22008-03-06 07:51:28 -0800210 InitLauncher(javaw);
duke6e45e102007-12-01 00:00:00 +0000211 DumpState();
212
213 /*
214 * Make sure the specified version of the JRE is running.
215 *
216 * There are three things to note about the SelectVersion() routine:
217 * 1) If the version running isn't correct, this routine doesn't
218 * return (either the correct version has been exec'd or an error
219 * was issued).
220 * 2) Argc and Argv in this scope are *not* altered by this routine.
221 * It is the responsibility of subsequent code to ignore the
222 * arguments handled by this routine.
223 * 3) As a side-effect, the variable "main_class" is guaranteed to
224 * be set (if it should ever be set). This isn't exactly the
225 * poster child for structured programming, but it is a small
226 * price to pay for not processing a jar file operand twice.
227 * (Note: This side effect has been disabled. See comment on
228 * bugid 5030265 below.)
229 */
230 SelectVersion(argc, argv, &main_class);
231
ksrinie3ec45d2010-07-09 11:04:34 -0700232 if (JLI_IsTraceLauncher()) {
233 int i;
234 printf("Command line args:\n");
235 for (i = 0; i < argc ; i++) {
236 printf("argv[%d] = %s\n", i, argv[i]);
237 }
238 }
duke6e45e102007-12-01 00:00:00 +0000239
240 CreateExecutionEnvironment(&argc, &argv,
241 jrepath, sizeof(jrepath),
ksrinie3ec45d2010-07-09 11:04:34 -0700242 jvmpath, sizeof(jvmpath));
duke6e45e102007-12-01 00:00:00 +0000243
244 ifn.CreateJavaVM = 0;
245 ifn.GetDefaultJavaVMInitArgs = 0;
246
247 if (JLI_IsTraceLauncher()) {
248 start = CounterGet();
249 }
250
251 if (!LoadJavaVM(jvmpath, &ifn)) {
252 return(6);
253 }
254
255 if (JLI_IsTraceLauncher()) {
256 end = CounterGet();
257 }
258
259 JLI_TraceLauncher("%ld micro seconds to LoadJavaVM\n",
260 (long)(jint)Counter2Micros(end-start));
261
262 ++argv;
263 --argc;
264
265 if (IsJavaArgs()) {
266 /* Preprocess wrapper arguments */
267 TranslateApplicationArgs(jargc, jargv, &argc, &argv);
268 if (!AddApplicationOptions(appclassc, appclassv)) {
269 return(1);
270 }
271 } else {
272 /* Set default CLASSPATH */
273 cpath = getenv("CLASSPATH");
274 if (cpath == NULL) {
275 cpath = ".";
276 }
277 SetClassPath(cpath);
278 }
279
280 /*
281 * Parse command line options; if the return value of
282 * ParseArguments is false, the program should exit.
283 */
284 if (!ParseArguments(&argc, &argv, &jarfile, &classname, &ret, jvmpath)) {
285 return(ret);
286 }
287
mchung1c2c2d42009-12-18 11:42:03 -0800288 /* Set bootclasspath for modules */
289 SetModulesBootClassPath(jrepath);
290
duke6e45e102007-12-01 00:00:00 +0000291 /* Override class path if -jar flag was specified */
292 if (jarfile != 0) {
293 SetClassPath(jarfile);
294 }
295
296 /* set the -Dsun.java.command pseudo property */
297 SetJavaCommandLineProp(classname, jarfile, argc, argv);
298
299 /* Set the -Dsun.java.launcher pseudo property */
300 SetJavaLauncherProp();
301
302 /* set the -Dsun.java.launcher.* platform properties */
303 SetJavaLauncherPlatformProps();
304
305 /* Show the splash screen if needed */
306 ShowSplashScreen();
307
308 return ContinueInNewThread(&ifn, argc, argv, jarfile, classname, ret);
309
310}
ksrinie3ec45d2010-07-09 11:04:34 -0700311/*
312 * Always detach the main thread so that it appears to have ended when
313 * the application's main method exits. This will invoke the
314 * uncaught exception handler machinery if main threw an
315 * exception. An uncaught exception handler cannot change the
316 * launcher's return code except by calling System.exit.
317 *
318 * Wait for all non-daemon threads to end, then destroy the VM.
319 * This will actually create a trivial new Java waiter thread
320 * named "DestroyJavaVM", but this will be seen as a different
321 * thread from the one that executed main, even though they are
322 * the same C thread. This allows mainThread.join() and
323 * mainThread.isAlive() to work as expected.
324 */
325#define LEAVE() \
326 if ((*vm)->DetachCurrentThread(vm) != 0) { \
327 JLI_ReportErrorMessage(JVM_ERROR2); \
328 ret = 1; \
329 } \
330 (*vm)->DestroyJavaVM(vm); \
331 return ret \
duke6e45e102007-12-01 00:00:00 +0000332
ksrini20a64b22008-09-24 15:07:41 -0700333#define CHECK_EXCEPTION_NULL_LEAVE(e) \
334 if ((*env)->ExceptionOccurred(env)) { \
335 JLI_ReportExceptionDescription(env); \
ksrinie3ec45d2010-07-09 11:04:34 -0700336 LEAVE(); \
ksrini20a64b22008-09-24 15:07:41 -0700337 } \
338 if ((e) == NULL) { \
339 JLI_ReportErrorMessage(JNI_ERROR); \
ksrinie3ec45d2010-07-09 11:04:34 -0700340 LEAVE(); \
ksrini20a64b22008-09-24 15:07:41 -0700341 }
342
343#define CHECK_EXCEPTION_LEAVE(rv) \
344 if ((*env)->ExceptionOccurred(env)) { \
345 JLI_ReportExceptionDescription(env); \
346 ret = (rv); \
ksrinie3ec45d2010-07-09 11:04:34 -0700347 LEAVE(); \
ksrini20a64b22008-09-24 15:07:41 -0700348 }
duke6e45e102007-12-01 00:00:00 +0000349
350int JNICALL
351JavaMain(void * _args)
352{
353 JavaMainArgs *args = (JavaMainArgs *)_args;
354 int argc = args->argc;
355 char **argv = args->argv;
356 char *jarfile = args->jarfile;
357 char *classname = args->classname;
358 InvocationFunctions ifn = args->ifn;
359
360 JavaVM *vm = 0;
361 JNIEnv *env = 0;
duke6e45e102007-12-01 00:00:00 +0000362 jclass mainClass;
363 jmethodID mainID;
364 jobjectArray mainArgs;
365 int ret = 0;
366 jlong start, end;
367
duke6e45e102007-12-01 00:00:00 +0000368 /* Initialize the virtual machine */
duke6e45e102007-12-01 00:00:00 +0000369 start = CounterGet();
370 if (!InitializeJVM(&vm, &env, &ifn)) {
ksrini0e817162008-08-26 10:21:20 -0700371 JLI_ReportErrorMessage(JVM_ERROR1);
duke6e45e102007-12-01 00:00:00 +0000372 exit(1);
373 }
374
375 if (printVersion || showVersion) {
376 PrintJavaVersion(env, showVersion);
ksrini20a64b22008-09-24 15:07:41 -0700377 CHECK_EXCEPTION_LEAVE(0);
duke6e45e102007-12-01 00:00:00 +0000378 if (printVersion) {
ksrinie3ec45d2010-07-09 11:04:34 -0700379 LEAVE();
duke6e45e102007-12-01 00:00:00 +0000380 }
381 }
382
ksrini8e20e1c2010-11-23 16:52:39 -0800383 if (showSettings != NULL) {
384 ShowSettings(env, showSettings);
ksrini6d575f82010-12-23 13:51:30 -0800385 CHECK_EXCEPTION_LEAVE(1);
ksrini8e20e1c2010-11-23 16:52:39 -0800386 }
duke6e45e102007-12-01 00:00:00 +0000387 /* If the user specified neither a class name nor a JAR file */
388 if (printXUsage || printUsage || (jarfile == 0 && classname == 0)) {
389 PrintUsage(env, printXUsage);
ksrini20a64b22008-09-24 15:07:41 -0700390 CHECK_EXCEPTION_LEAVE(1);
ksrinie3ec45d2010-07-09 11:04:34 -0700391 LEAVE();
duke6e45e102007-12-01 00:00:00 +0000392 }
393
394 FreeKnownVMs(); /* after last possible PrintUsage() */
395
396 if (JLI_IsTraceLauncher()) {
397 end = CounterGet();
398 JLI_TraceLauncher("%ld micro seconds to InitializeJVM\n",
399 (long)(jint)Counter2Micros(end-start));
400 }
401
402 /* At this stage, argc/argv have the applications' arguments */
403 if (JLI_IsTraceLauncher()){
404 int i;
405 printf("Main-Class is '%s'\n", classname ? classname : "");
406 printf("Apps' argc is %d\n", argc);
407 for (i=0; i < argc; i++) {
408 printf(" argv[%2d] = '%s'\n", i, argv[i]);
409 }
410 }
411
412 ret = 1;
413
414 /*
415 * Get the application's main class.
416 *
417 * See bugid 5030265. The Main-Class name has already been parsed
418 * from the manifest, but not parsed properly for UTF-8 support.
419 * Hence the code here ignores the value previously extracted and
420 * uses the pre-existing code to reextract the value. This is
421 * possibly an end of release cycle expedient. However, it has
422 * also been discovered that passing some character sets through
423 * the environment has "strange" behavior on some variants of
424 * Windows. Hence, maybe the manifest parsing code local to the
425 * launcher should never be enhanced.
426 *
427 * Hence, future work should either:
428 * 1) Correct the local parsing code and verify that the
429 * Main-Class attribute gets properly passed through
430 * all environments,
431 * 2) Remove the vestages of maintaining main_class through
432 * the environment (and remove these comments).
433 */
434 if (jarfile != 0) {
ksrini20a64b22008-09-24 15:07:41 -0700435 mainClass = LoadMainClass(env, JNI_TRUE, jarfile);
duke6e45e102007-12-01 00:00:00 +0000436 } else {
ksrini20a64b22008-09-24 15:07:41 -0700437 mainClass = LoadMainClass(env, JNI_FALSE, classname);
duke6e45e102007-12-01 00:00:00 +0000438 }
ksrini20a64b22008-09-24 15:07:41 -0700439 CHECK_EXCEPTION_NULL_LEAVE(mainClass);
duke6e45e102007-12-01 00:00:00 +0000440
ksrini20a64b22008-09-24 15:07:41 -0700441 /*
442 * The LoadMainClass not only loads the main class, it will also ensure
443 * that the main method's signature is correct, therefore further checking
444 * is not required. The main method is invoked here so that extraneous java
445 * stacks are not in the application stack trace.
446 */
duke6e45e102007-12-01 00:00:00 +0000447 mainID = (*env)->GetStaticMethodID(env, mainClass, "main",
448 "([Ljava/lang/String;)V");
ksrini20a64b22008-09-24 15:07:41 -0700449 CHECK_EXCEPTION_NULL_LEAVE(mainID);
duke6e45e102007-12-01 00:00:00 +0000450
451 /* Build argument array */
452 mainArgs = NewPlatformStringArray(env, argv, argc);
ksrini20a64b22008-09-24 15:07:41 -0700453 CHECK_EXCEPTION_NULL_LEAVE(mainArgs);
duke6e45e102007-12-01 00:00:00 +0000454
455 /* Invoke main method. */
456 (*env)->CallStaticVoidMethod(env, mainClass, mainID, mainArgs);
457
458 /*
459 * The launcher's exit code (in the absence of calls to
460 * System.exit) will be non-zero if main threw an exception.
461 */
462 ret = (*env)->ExceptionOccurred(env) == NULL ? 0 : 1;
ksrinie3ec45d2010-07-09 11:04:34 -0700463 LEAVE();
duke6e45e102007-12-01 00:00:00 +0000464}
465
duke6e45e102007-12-01 00:00:00 +0000466/*
467 * Checks the command line options to find which JVM type was
468 * specified. If no command line option was given for the JVM type,
469 * the default type is used. The environment variable
470 * JDK_ALTERNATE_VM and the command line option -XXaltjvm= are also
471 * checked as ways of specifying which JVM type to invoke.
472 */
473char *
474CheckJvmType(int *pargc, char ***argv, jboolean speculative) {
475 int i, argi;
476 int argc;
477 char **newArgv;
478 int newArgvIdx = 0;
479 int isVMType;
480 int jvmidx = -1;
481 char *jvmtype = getenv("JDK_ALTERNATE_VM");
482
483 argc = *pargc;
484
485 /* To make things simpler we always copy the argv array */
486 newArgv = JLI_MemAlloc((argc + 1) * sizeof(char *));
487
488 /* The program name is always present */
489 newArgv[newArgvIdx++] = (*argv)[0];
490
491 for (argi = 1; argi < argc; argi++) {
492 char *arg = (*argv)[argi];
493 isVMType = 0;
494
495 if (IsJavaArgs()) {
496 if (arg[0] != '-') {
497 newArgv[newArgvIdx++] = arg;
498 continue;
499 }
500 } else {
501 if (JLI_StrCmp(arg, "-classpath") == 0 ||
502 JLI_StrCmp(arg, "-cp") == 0) {
503 newArgv[newArgvIdx++] = arg;
504 argi++;
505 if (argi < argc) {
506 newArgv[newArgvIdx++] = (*argv)[argi];
507 }
508 continue;
509 }
510 if (arg[0] != '-') break;
511 }
512
513 /* Did the user pass an explicit VM type? */
514 i = KnownVMIndex(arg);
515 if (i >= 0) {
516 jvmtype = knownVMs[jvmidx = i].name + 1; /* skip the - */
517 isVMType = 1;
518 *pargc = *pargc - 1;
519 }
520
521 /* Did the user specify an "alternate" VM? */
522 else if (JLI_StrCCmp(arg, "-XXaltjvm=") == 0 || JLI_StrCCmp(arg, "-J-XXaltjvm=") == 0) {
523 isVMType = 1;
524 jvmtype = arg+((arg[1]=='X')? 10 : 12);
525 jvmidx = -1;
526 }
527
528 if (!isVMType) {
529 newArgv[newArgvIdx++] = arg;
530 }
531 }
532
533 /*
534 * Finish copying the arguments if we aborted the above loop.
535 * NOTE that if we aborted via "break" then we did NOT copy the
536 * last argument above, and in addition argi will be less than
537 * argc.
538 */
539 while (argi < argc) {
540 newArgv[newArgvIdx++] = (*argv)[argi];
541 argi++;
542 }
543
544 /* argv is null-terminated */
545 newArgv[newArgvIdx] = 0;
546
547 /* Copy back argv */
548 *argv = newArgv;
549 *pargc = newArgvIdx;
550
551 /* use the default VM type if not specified (no alias processing) */
552 if (jvmtype == NULL) {
553 char* result = knownVMs[0].name+1;
554 /* Use a different VM type if we are on a server class machine? */
555 if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) &&
556 (ServerClassMachine() == JNI_TRUE)) {
557 result = knownVMs[0].server_class+1;
558 }
559 JLI_TraceLauncher("Default VM: %s\n", result);
560 return result;
561 }
562
563 /* if using an alternate VM, no alias processing */
564 if (jvmidx < 0)
565 return jvmtype;
566
567 /* Resolve aliases first */
568 {
569 int loopCount = 0;
570 while (knownVMs[jvmidx].flag == VM_ALIASED_TO) {
571 int nextIdx = KnownVMIndex(knownVMs[jvmidx].alias);
572
573 if (loopCount > knownVMsCount) {
574 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700575 JLI_ReportErrorMessage(CFG_ERROR1);
duke6e45e102007-12-01 00:00:00 +0000576 exit(1);
577 } else {
578 return "ERROR";
579 /* break; */
580 }
581 }
582
583 if (nextIdx < 0) {
584 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700585 JLI_ReportErrorMessage(CFG_ERROR2, knownVMs[jvmidx].alias);
duke6e45e102007-12-01 00:00:00 +0000586 exit(1);
587 } else {
588 return "ERROR";
589 }
590 }
591 jvmidx = nextIdx;
592 jvmtype = knownVMs[jvmidx].name+1;
593 loopCount++;
594 }
595 }
596
597 switch (knownVMs[jvmidx].flag) {
598 case VM_WARN:
599 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700600 JLI_ReportErrorMessage(CFG_WARN1, jvmtype, knownVMs[0].name + 1);
duke6e45e102007-12-01 00:00:00 +0000601 }
602 /* fall through */
603 case VM_IGNORE:
604 jvmtype = knownVMs[jvmidx=0].name + 1;
605 /* fall through */
606 case VM_KNOWN:
607 break;
608 case VM_ERROR:
609 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700610 JLI_ReportErrorMessage(CFG_ERROR3, jvmtype);
duke6e45e102007-12-01 00:00:00 +0000611 exit(1);
612 } else {
613 return "ERROR";
614 }
615 }
616
617 return jvmtype;
618}
619
620/* copied from HotSpot function "atomll()" */
621static int
ksrini8e20e1c2010-11-23 16:52:39 -0800622parse_size(const char *s, jlong *result) {
duke6e45e102007-12-01 00:00:00 +0000623 jlong n = 0;
624 int args_read = sscanf(s, jlong_format_specifier(), &n);
625 if (args_read != 1) {
626 return 0;
627 }
628 while (*s != '\0' && *s >= '0' && *s <= '9') {
629 s++;
630 }
631 // 4705540: illegal if more characters are found after the first non-digit
632 if (JLI_StrLen(s) > 1) {
633 return 0;
634 }
635 switch (*s) {
636 case 'T': case 't':
637 *result = n * GB * KB;
638 return 1;
639 case 'G': case 'g':
640 *result = n * GB;
641 return 1;
642 case 'M': case 'm':
643 *result = n * MB;
644 return 1;
645 case 'K': case 'k':
646 *result = n * KB;
647 return 1;
648 case '\0':
649 *result = n;
650 return 1;
651 default:
652 /* Create JVM with default stack and let VM handle malformed -Xss string*/
653 return 0;
654 }
655}
656
657/*
658 * Adds a new VM option with the given given name and value.
659 */
660void
661AddOption(char *str, void *info)
662{
663 /*
664 * Expand options array if needed to accommodate at least one more
665 * VM option.
666 */
667 if (numOptions >= maxOptions) {
668 if (options == 0) {
669 maxOptions = 4;
670 options = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
671 } else {
672 JavaVMOption *tmp;
673 maxOptions *= 2;
674 tmp = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
675 memcpy(tmp, options, numOptions * sizeof(JavaVMOption));
676 JLI_MemFree(options);
677 options = tmp;
678 }
679 }
680 options[numOptions].optionString = str;
681 options[numOptions++].extraInfo = info;
682
683 if (JLI_StrCCmp(str, "-Xss") == 0) {
ksrini8e20e1c2010-11-23 16:52:39 -0800684 jlong tmp;
685 if (parse_size(str + 4, &tmp)) {
686 threadStackSize = tmp;
687 }
688 }
689
690 if (JLI_StrCCmp(str, "-Xmx") == 0) {
691 jlong tmp;
692 if (parse_size(str + 4, &tmp)) {
ksrini6d575f82010-12-23 13:51:30 -0800693 maxHeapSize = tmp;
694 }
695 }
696
697 if (JLI_StrCCmp(str, "-Xms") == 0) {
698 jlong tmp;
699 if (parse_size(str + 4, &tmp)) {
700 initialHeapSize = tmp;
ksrini8e20e1c2010-11-23 16:52:39 -0800701 }
duke6e45e102007-12-01 00:00:00 +0000702 }
703}
704
705static void
706SetClassPath(const char *s)
707{
708 char *def;
martinc0ca3352009-06-22 16:41:27 -0700709 const char *orig = s;
710 static const char format[] = "-Djava.class.path=%s";
duke6e45e102007-12-01 00:00:00 +0000711 s = JLI_WildcardExpandClasspath(s);
martinc0ca3352009-06-22 16:41:27 -0700712 def = JLI_MemAlloc(sizeof(format)
713 - 2 /* strlen("%s") */
714 + JLI_StrLen(s));
715 sprintf(def, format, s);
duke6e45e102007-12-01 00:00:00 +0000716 AddOption(def, NULL);
martinc0ca3352009-06-22 16:41:27 -0700717 if (s != orig)
718 JLI_MemFree((char *) s);
duke6e45e102007-12-01 00:00:00 +0000719}
720
721/*
mchung1c2c2d42009-12-18 11:42:03 -0800722 * Set the bootclasspath for modules.
723 * A temporary workaround until jigsaw is integrated into JDK 7.
724 */
725static void
726SetModulesBootClassPath(const char *jrepath)
727{
728 char *def, *s;
729 char pathname[MAXPATHLEN];
730 const char separator[] = { FILE_SEPARATOR, '\0' };
731 const char *orig = jrepath;
732 static const char format[] = "-Xbootclasspath/p:%s";
733 struct stat statbuf;
734
735 /* return if jre/lib/rt.jar exists */
ksrini01740ec2010-09-09 11:50:40 -0700736 JLI_Snprintf(pathname, sizeof(pathname), "%s%slib%srt.jar", jrepath, separator, separator);
mchung1c2c2d42009-12-18 11:42:03 -0800737 if (stat(pathname, &statbuf) == 0) {
738 return;
739 }
740
741 /* return if jre/classes exists */
ksrini01740ec2010-09-09 11:50:40 -0700742 JLI_Snprintf(pathname, sizeof(pathname), "%s%sclasses", jrepath, separator);
mchung1c2c2d42009-12-18 11:42:03 -0800743 if (stat(pathname, &statbuf) == 0) {
744 return;
745 }
746
747 /* modularized jre */
ksrini01740ec2010-09-09 11:50:40 -0700748 JLI_Snprintf(pathname, sizeof(pathname), "%s%slib%s*", jrepath, separator, separator);
mchung1c2c2d42009-12-18 11:42:03 -0800749 s = (char *) JLI_WildcardExpandClasspath(pathname);
750 def = JLI_MemAlloc(sizeof(format)
751 - 2 /* strlen("%s") */
752 + JLI_StrLen(s));
753 sprintf(def, format, s);
754 AddOption(def, NULL);
755 if (s != orig)
756 JLI_MemFree((char *) s);
757}
758
759/*
duke6e45e102007-12-01 00:00:00 +0000760 * The SelectVersion() routine ensures that an appropriate version of
761 * the JRE is running. The specification for the appropriate version
762 * is obtained from either the manifest of a jar file (preferred) or
763 * from command line options.
764 * The routine also parses splash screen command line options and
765 * passes on their values in private environment variables.
766 */
767static void
768SelectVersion(int argc, char **argv, char **main_class)
769{
770 char *arg;
771 char **new_argv;
772 char **new_argp;
773 char *operand;
774 char *version = NULL;
775 char *jre = NULL;
776 int jarflag = 0;
777 int headlessflag = 0;
778 int restrict_search = -1; /* -1 implies not known */
779 manifest_info info;
780 char env_entry[MAXNAMELEN + 24] = ENV_ENTRY "=";
781 char *splash_file_name = NULL;
782 char *splash_jar_name = NULL;
783 char *env_in;
784 int res;
785
786 /*
787 * If the version has already been selected, set *main_class
788 * with the value passed through the environment (if any) and
789 * simply return.
790 */
791 if ((env_in = getenv(ENV_ENTRY)) != NULL) {
792 if (*env_in != '\0')
793 *main_class = JLI_StringDup(env_in);
794 return;
795 }
796
797 /*
798 * Scan through the arguments for options relevant to multiple JRE
799 * support. For reference, the command line syntax is defined as:
800 *
801 * SYNOPSIS
802 * java [options] class [argument...]
803 *
804 * java [options] -jar file.jar [argument...]
805 *
806 * As the scan is performed, make a copy of the argument list with
807 * the version specification options (new to 1.5) removed, so that
808 * a version less than 1.5 can be exec'd.
809 *
810 * Note that due to the syntax of the native Windows interface
811 * CreateProcess(), processing similar to the following exists in
812 * the Windows platform specific routine ExecJRE (in java_md.c).
813 * Changes here should be reproduced there.
814 */
815 new_argv = JLI_MemAlloc((argc + 1) * sizeof(char*));
816 new_argv[0] = argv[0];
817 new_argp = &new_argv[1];
818 argc--;
819 argv++;
820 while ((arg = *argv) != 0 && *arg == '-') {
821 if (JLI_StrCCmp(arg, "-version:") == 0) {
822 version = arg + 9;
823 } else if (JLI_StrCmp(arg, "-jre-restrict-search") == 0) {
824 restrict_search = 1;
825 } else if (JLI_StrCmp(arg, "-no-jre-restrict-search") == 0) {
826 restrict_search = 0;
827 } else {
828 if (JLI_StrCmp(arg, "-jar") == 0)
829 jarflag = 1;
830 /* deal with "unfortunate" classpath syntax */
831 if ((JLI_StrCmp(arg, "-classpath") == 0 || JLI_StrCmp(arg, "-cp") == 0) &&
832 (argc >= 2)) {
833 *new_argp++ = arg;
834 argc--;
835 argv++;
836 arg = *argv;
837 }
838
839 /*
840 * Checking for headless toolkit option in the some way as AWT does:
841 * "true" means true and any other value means false
842 */
843 if (JLI_StrCmp(arg, "-Djava.awt.headless=true") == 0) {
844 headlessflag = 1;
845 } else if (JLI_StrCCmp(arg, "-Djava.awt.headless=") == 0) {
846 headlessflag = 0;
847 } else if (JLI_StrCCmp(arg, "-splash:") == 0) {
848 splash_file_name = arg+8;
849 }
850 *new_argp++ = arg;
851 }
852 argc--;
853 argv++;
854 }
855 if (argc <= 0) { /* No operand? Possibly legit with -[full]version */
856 operand = NULL;
857 } else {
858 argc--;
859 *new_argp++ = operand = *argv++;
860 }
861 while (argc-- > 0) /* Copy over [argument...] */
862 *new_argp++ = *argv++;
863 *new_argp = NULL;
864
865 /*
866 * If there is a jar file, read the manifest. If the jarfile can't be
867 * read, the manifest can't be read from the jar file, or the manifest
868 * is corrupt, issue the appropriate error messages and exit.
869 *
870 * Even if there isn't a jar file, construct a manifest_info structure
871 * containing the command line information. It's a convenient way to carry
872 * this data around.
873 */
874 if (jarflag && operand) {
875 if ((res = JLI_ParseManifest(operand, &info)) != 0) {
876 if (res == -1)
ksrini0e817162008-08-26 10:21:20 -0700877 JLI_ReportErrorMessage(JAR_ERROR2, operand);
duke6e45e102007-12-01 00:00:00 +0000878 else
ksrini0e817162008-08-26 10:21:20 -0700879 JLI_ReportErrorMessage(JAR_ERROR3, operand);
duke6e45e102007-12-01 00:00:00 +0000880 exit(1);
881 }
882
883 /*
884 * Command line splash screen option should have precedence
885 * over the manifest, so the manifest data is used only if
886 * splash_file_name has not been initialized above during command
887 * line parsing
888 */
889 if (!headlessflag && !splash_file_name && info.splashscreen_image_file_name) {
890 splash_file_name = info.splashscreen_image_file_name;
891 splash_jar_name = operand;
892 }
893 } else {
894 info.manifest_version = NULL;
895 info.main_class = NULL;
896 info.jre_version = NULL;
897 info.jre_restrict_search = 0;
898 }
899
900 /*
901 * Passing on splash screen info in environment variables
902 */
903 if (splash_file_name && !headlessflag) {
904 char* splash_file_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_FILE_ENV_ENTRY "=")+JLI_StrLen(splash_file_name)+1);
905 JLI_StrCpy(splash_file_entry, SPLASH_FILE_ENV_ENTRY "=");
906 JLI_StrCat(splash_file_entry, splash_file_name);
907 putenv(splash_file_entry);
908 }
909 if (splash_jar_name && !headlessflag) {
910 char* splash_jar_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_JAR_ENV_ENTRY "=")+JLI_StrLen(splash_jar_name)+1);
911 JLI_StrCpy(splash_jar_entry, SPLASH_JAR_ENV_ENTRY "=");
912 JLI_StrCat(splash_jar_entry, splash_jar_name);
913 putenv(splash_jar_entry);
914 }
915
916 /*
917 * The JRE-Version and JRE-Restrict-Search values (if any) from the
918 * manifest are overwritten by any specified on the command line.
919 */
920 if (version != NULL)
921 info.jre_version = version;
922 if (restrict_search != -1)
923 info.jre_restrict_search = restrict_search;
924
925 /*
926 * "Valid" returns (other than unrecoverable errors) follow. Set
927 * main_class as a side-effect of this routine.
928 */
929 if (info.main_class != NULL)
930 *main_class = JLI_StringDup(info.main_class);
931
932 /*
933 * If no version selection information is found either on the command
934 * line or in the manifest, simply return.
935 */
936 if (info.jre_version == NULL) {
937 JLI_FreeManifest();
938 JLI_MemFree(new_argv);
939 return;
940 }
941
942 /*
943 * Check for correct syntax of the version specification (JSR 56).
944 */
945 if (!JLI_ValidVersionString(info.jre_version)) {
ksrini0e817162008-08-26 10:21:20 -0700946 JLI_ReportErrorMessage(SPC_ERROR1, info.jre_version);
duke6e45e102007-12-01 00:00:00 +0000947 exit(1);
948 }
949
950 /*
951 * Find the appropriate JVM on the system. Just to be as forgiving as
952 * possible, if the standard algorithms don't locate an appropriate
953 * jre, check to see if the one running will satisfy the requirements.
954 * This can happen on systems which haven't been set-up for multiple
955 * JRE support.
956 */
957 jre = LocateJRE(&info);
958 JLI_TraceLauncher("JRE-Version = %s, JRE-Restrict-Search = %s Selected = %s\n",
959 (info.jre_version?info.jre_version:"null"),
960 (info.jre_restrict_search?"true":"false"), (jre?jre:"null"));
961
962 if (jre == NULL) {
963 if (JLI_AcceptableRelease(GetFullVersion(), info.jre_version)) {
964 JLI_FreeManifest();
965 JLI_MemFree(new_argv);
966 return;
967 } else {
ksrini0e817162008-08-26 10:21:20 -0700968 JLI_ReportErrorMessage(CFG_ERROR4, info.jre_version);
duke6e45e102007-12-01 00:00:00 +0000969 exit(1);
970 }
971 }
972
973 /*
974 * If I'm not the chosen one, exec the chosen one. Returning from
975 * ExecJRE indicates that I am indeed the chosen one.
976 *
977 * The private environment variable _JAVA_VERSION_SET is used to
978 * prevent the chosen one from re-reading the manifest file and
979 * using the values found within to override the (potential) command
980 * line flags stripped from argv (because the target may not
981 * understand them). Passing the MainClass value is an optimization
982 * to avoid locating, expanding and parsing the manifest extra
983 * times.
984 */
ksrini8760ca92008-09-04 09:43:32 -0700985 if (info.main_class != NULL) {
986 if (JLI_StrLen(info.main_class) <= MAXNAMELEN) {
987 (void)JLI_StrCat(env_entry, info.main_class);
988 } else {
asaha80ccd422009-04-16 21:08:04 -0700989 JLI_ReportErrorMessage(CLS_ERROR5, MAXNAMELEN);
ksrini8760ca92008-09-04 09:43:32 -0700990 exit(1);
991 }
992 }
duke6e45e102007-12-01 00:00:00 +0000993 (void)putenv(env_entry);
994 ExecJRE(jre, new_argv);
995 JLI_FreeManifest();
996 JLI_MemFree(new_argv);
997 return;
998}
999
1000/*
1001 * Parses command line arguments. Returns JNI_FALSE if launcher
1002 * should exit without starting vm, returns JNI_TRUE if vm needs
1003 * to be started to process given options. *pret (the launcher
1004 * process return value) is set to 0 for a normal exit.
1005 */
1006static jboolean
1007ParseArguments(int *pargc, char ***pargv, char **pjarfile,
1008 char **pclassname, int *pret, const char *jvmpath)
1009{
1010 int argc = *pargc;
1011 char **argv = *pargv;
1012 jboolean jarflag = JNI_FALSE;
1013 char *arg;
1014
1015 *pret = 0;
1016
1017 while ((arg = *argv) != 0 && *arg == '-') {
1018 argv++; --argc;
1019 if (JLI_StrCmp(arg, "-classpath") == 0 || JLI_StrCmp(arg, "-cp") == 0) {
1020 ARG_CHECK (argc, ARG_ERROR1, arg);
1021 SetClassPath(*argv);
1022 argv++; --argc;
1023 } else if (JLI_StrCmp(arg, "-jar") == 0) {
1024 ARG_CHECK (argc, ARG_ERROR2, arg);
1025 jarflag = JNI_TRUE;
1026 } else if (JLI_StrCmp(arg, "-help") == 0 ||
1027 JLI_StrCmp(arg, "-h") == 0 ||
1028 JLI_StrCmp(arg, "-?") == 0) {
1029 printUsage = JNI_TRUE;
1030 return JNI_TRUE;
1031 } else if (JLI_StrCmp(arg, "-version") == 0) {
1032 printVersion = JNI_TRUE;
1033 return JNI_TRUE;
1034 } else if (JLI_StrCmp(arg, "-showversion") == 0) {
1035 showVersion = JNI_TRUE;
1036 } else if (JLI_StrCmp(arg, "-X") == 0) {
1037 printXUsage = JNI_TRUE;
1038 return JNI_TRUE;
1039/*
ksrini8e20e1c2010-11-23 16:52:39 -08001040 * The following case checks for -XshowSettings OR -XshowSetting:SUBOPT.
1041 * In the latter case, any SUBOPT value not recognized will default to "all"
1042 */
1043 } else if (JLI_StrCmp(arg, "-XshowSettings") == 0 ||
1044 JLI_StrCCmp(arg, "-XshowSettings:") == 0) {
1045 showSettings = arg;
1046/*
duke6e45e102007-12-01 00:00:00 +00001047 * The following case provide backward compatibility with old-style
1048 * command line options.
1049 */
1050 } else if (JLI_StrCmp(arg, "-fullversion") == 0) {
ksrini0e817162008-08-26 10:21:20 -07001051 JLI_ReportMessage("%s full version \"%s\"", _launcher_name, GetFullVersion());
duke6e45e102007-12-01 00:00:00 +00001052 return JNI_FALSE;
1053 } else if (JLI_StrCmp(arg, "-verbosegc") == 0) {
1054 AddOption("-verbose:gc", NULL);
1055 } else if (JLI_StrCmp(arg, "-t") == 0) {
1056 AddOption("-Xt", NULL);
1057 } else if (JLI_StrCmp(arg, "-tm") == 0) {
1058 AddOption("-Xtm", NULL);
1059 } else if (JLI_StrCmp(arg, "-debug") == 0) {
1060 AddOption("-Xdebug", NULL);
1061 } else if (JLI_StrCmp(arg, "-noclassgc") == 0) {
1062 AddOption("-Xnoclassgc", NULL);
1063 } else if (JLI_StrCmp(arg, "-Xfuture") == 0) {
1064 AddOption("-Xverify:all", NULL);
1065 } else if (JLI_StrCmp(arg, "-verify") == 0) {
1066 AddOption("-Xverify:all", NULL);
1067 } else if (JLI_StrCmp(arg, "-verifyremote") == 0) {
1068 AddOption("-Xverify:remote", NULL);
1069 } else if (JLI_StrCmp(arg, "-noverify") == 0) {
1070 AddOption("-Xverify:none", NULL);
1071 } else if (JLI_StrCCmp(arg, "-prof") == 0) {
1072 char *p = arg + 5;
1073 char *tmp = JLI_MemAlloc(JLI_StrLen(arg) + 50);
1074 if (*p) {
1075 sprintf(tmp, "-Xrunhprof:cpu=old,file=%s", p + 1);
1076 } else {
1077 sprintf(tmp, "-Xrunhprof:cpu=old,file=java.prof");
1078 }
1079 AddOption(tmp, NULL);
1080 } else if (JLI_StrCCmp(arg, "-ss") == 0 ||
1081 JLI_StrCCmp(arg, "-oss") == 0 ||
1082 JLI_StrCCmp(arg, "-ms") == 0 ||
1083 JLI_StrCCmp(arg, "-mx") == 0) {
1084 char *tmp = JLI_MemAlloc(JLI_StrLen(arg) + 6);
1085 sprintf(tmp, "-X%s", arg + 1); /* skip '-' */
1086 AddOption(tmp, NULL);
1087 } else if (JLI_StrCmp(arg, "-checksource") == 0 ||
1088 JLI_StrCmp(arg, "-cs") == 0 ||
1089 JLI_StrCmp(arg, "-noasyncgc") == 0) {
1090 /* No longer supported */
ksrini0e817162008-08-26 10:21:20 -07001091 JLI_ReportErrorMessage(ARG_WARN, arg);
duke6e45e102007-12-01 00:00:00 +00001092 } else if (JLI_StrCCmp(arg, "-version:") == 0 ||
1093 JLI_StrCmp(arg, "-no-jre-restrict-search") == 0 ||
1094 JLI_StrCmp(arg, "-jre-restrict-search") == 0 ||
1095 JLI_StrCCmp(arg, "-splash:") == 0) {
1096 ; /* Ignore machine independent options already handled */
1097 } else if (RemovableOption(arg) ) {
1098 ; /* Do not pass option to vm. */
1099 } else {
1100 AddOption(arg, NULL);
1101 }
1102 }
1103
1104 if (--argc >= 0) {
1105 if (jarflag) {
1106 *pjarfile = *argv++;
ksrinie3ec45d2010-07-09 11:04:34 -07001107 *pclassname = NULL;
duke6e45e102007-12-01 00:00:00 +00001108 } else {
ksrinie3ec45d2010-07-09 11:04:34 -07001109 *pjarfile = NULL;
duke6e45e102007-12-01 00:00:00 +00001110 *pclassname = *argv++;
1111 }
1112 *pargc = argc;
1113 *pargv = argv;
1114 }
ksrinie3ec45d2010-07-09 11:04:34 -07001115 if (*pjarfile == NULL && *pclassname == NULL) {
1116 *pret = 1;
1117 }
duke6e45e102007-12-01 00:00:00 +00001118 return JNI_TRUE;
1119}
1120
1121/*
1122 * Initializes the Java Virtual Machine. Also frees options array when
1123 * finished.
1124 */
1125static jboolean
1126InitializeJVM(JavaVM **pvm, JNIEnv **penv, InvocationFunctions *ifn)
1127{
1128 JavaVMInitArgs args;
1129 jint r;
1130
1131 memset(&args, 0, sizeof(args));
1132 args.version = JNI_VERSION_1_2;
1133 args.nOptions = numOptions;
1134 args.options = options;
1135 args.ignoreUnrecognized = JNI_FALSE;
1136
1137 if (JLI_IsTraceLauncher()) {
1138 int i = 0;
1139 printf("JavaVM args:\n ");
1140 printf("version 0x%08lx, ", (long)args.version);
1141 printf("ignoreUnrecognized is %s, ",
1142 args.ignoreUnrecognized ? "JNI_TRUE" : "JNI_FALSE");
1143 printf("nOptions is %ld\n", (long)args.nOptions);
1144 for (i = 0; i < numOptions; i++)
1145 printf(" option[%2d] = '%s'\n",
1146 i, args.options[i].optionString);
1147 }
1148
1149 r = ifn->CreateJavaVM(pvm, (void **)penv, &args);
1150 JLI_MemFree(options);
1151 return r == JNI_OK;
1152}
1153
1154
1155#define NULL_CHECK0(e) if ((e) == 0) { \
ksrini0e817162008-08-26 10:21:20 -07001156 JLI_ReportErrorMessage(JNI_ERROR); \
duke6e45e102007-12-01 00:00:00 +00001157 return 0; \
1158 }
1159
1160#define NULL_CHECK(e) if ((e) == 0) { \
ksrini0e817162008-08-26 10:21:20 -07001161 JLI_ReportErrorMessage(JNI_ERROR); \
duke6e45e102007-12-01 00:00:00 +00001162 return; \
1163 }
1164
1165static jstring platformEncoding = NULL;
1166static jstring getPlatformEncoding(JNIEnv *env) {
1167 if (platformEncoding == NULL) {
1168 jstring propname = (*env)->NewStringUTF(env, "sun.jnu.encoding");
1169 if (propname) {
1170 jclass cls;
1171 jmethodID mid;
ksrini20a64b22008-09-24 15:07:41 -07001172 NULL_CHECK0 (cls = FindBootStrapClass(env, "java/lang/System"));
duke6e45e102007-12-01 00:00:00 +00001173 NULL_CHECK0 (mid = (*env)->GetStaticMethodID(
1174 env, cls,
1175 "getProperty",
1176 "(Ljava/lang/String;)Ljava/lang/String;"));
1177 platformEncoding = (*env)->CallStaticObjectMethod (
1178 env, cls, mid, propname);
1179 }
1180 }
1181 return platformEncoding;
1182}
1183
1184static jboolean isEncodingSupported(JNIEnv *env, jstring enc) {
1185 jclass cls;
1186 jmethodID mid;
ksrini20a64b22008-09-24 15:07:41 -07001187 NULL_CHECK0 (cls = FindBootStrapClass(env, "java/nio/charset/Charset"));
duke6e45e102007-12-01 00:00:00 +00001188 NULL_CHECK0 (mid = (*env)->GetStaticMethodID(
1189 env, cls,
1190 "isSupported",
1191 "(Ljava/lang/String;)Z"));
1192 return (*env)->CallStaticBooleanMethod(env, cls, mid, enc);
1193}
1194
1195/*
1196 * Returns a new Java string object for the specified platform string.
1197 */
1198static jstring
1199NewPlatformString(JNIEnv *env, char *s)
1200{
1201 int len = (int)JLI_StrLen(s);
1202 jclass cls;
1203 jmethodID mid;
1204 jbyteArray ary;
1205 jstring enc;
1206
1207 if (s == NULL)
1208 return 0;
1209 enc = getPlatformEncoding(env);
1210
1211 ary = (*env)->NewByteArray(env, len);
1212 if (ary != 0) {
1213 jstring str = 0;
1214 (*env)->SetByteArrayRegion(env, ary, 0, len, (jbyte *)s);
1215 if (!(*env)->ExceptionOccurred(env)) {
ksrini20a64b22008-09-24 15:07:41 -07001216 NULL_CHECK0(cls = FindBootStrapClass(env, "java/lang/String"));
duke6e45e102007-12-01 00:00:00 +00001217 if (isEncodingSupported(env, enc) == JNI_TRUE) {
duke6e45e102007-12-01 00:00:00 +00001218 NULL_CHECK0(mid = (*env)->GetMethodID(env, cls, "<init>",
1219 "([BLjava/lang/String;)V"));
1220 str = (*env)->NewObject(env, cls, mid, ary, enc);
1221 } else {
1222 /*If the encoding specified in sun.jnu.encoding is not
1223 endorsed by "Charset.isSupported" we have to fall back
1224 to use String(byte[]) explicitly here without specifying
1225 the encoding name, in which the StringCoding class will
1226 pickup the iso-8859-1 as the fallback converter for us.
1227 */
duke6e45e102007-12-01 00:00:00 +00001228 NULL_CHECK0(mid = (*env)->GetMethodID(env, cls, "<init>",
1229 "([B)V"));
1230 str = (*env)->NewObject(env, cls, mid, ary);
1231 }
1232 (*env)->DeleteLocalRef(env, ary);
1233 return str;
1234 }
1235 }
1236 return 0;
1237}
1238
1239/*
1240 * Returns a new array of Java string objects for the specified
1241 * array of platform strings.
1242 */
1243static jobjectArray
1244NewPlatformStringArray(JNIEnv *env, char **strv, int strc)
1245{
1246 jarray cls;
1247 jarray ary;
1248 int i;
1249
ksrini20a64b22008-09-24 15:07:41 -07001250 NULL_CHECK0(cls = FindBootStrapClass(env, "java/lang/String"));
duke6e45e102007-12-01 00:00:00 +00001251 NULL_CHECK0(ary = (*env)->NewObjectArray(env, strc, cls, 0));
1252 for (i = 0; i < strc; i++) {
1253 jstring str = NewPlatformString(env, *strv++);
1254 NULL_CHECK0(str);
1255 (*env)->SetObjectArrayElement(env, ary, i, str);
1256 (*env)->DeleteLocalRef(env, str);
1257 }
1258 return ary;
1259}
1260
1261/*
ksrini20a64b22008-09-24 15:07:41 -07001262 * Loads a class and verifies that the main class is present and it is ok to
1263 * call it for more details refer to the java implementation.
duke6e45e102007-12-01 00:00:00 +00001264 */
1265static jclass
ksrini20a64b22008-09-24 15:07:41 -07001266LoadMainClass(JNIEnv *env, jboolean isJar, char *name)
duke6e45e102007-12-01 00:00:00 +00001267{
duke6e45e102007-12-01 00:00:00 +00001268 jclass cls;
ksrini20a64b22008-09-24 15:07:41 -07001269 jmethodID mid;
1270 jstring str;
1271 jobject result;
duke6e45e102007-12-01 00:00:00 +00001272 jlong start, end;
1273
ksrini20a64b22008-09-24 15:07:41 -07001274 if (JLI_IsTraceLauncher()) {
duke6e45e102007-12-01 00:00:00 +00001275 start = CounterGet();
ksrini20a64b22008-09-24 15:07:41 -07001276 }
1277 NULL_CHECK0(cls = FindBootStrapClass(env, "sun/launcher/LauncherHelper"));
1278 NULL_CHECK0(mid = (*env)->GetStaticMethodID(env, cls, "checkAndLoadMain",
1279 "(ZZLjava/lang/String;)Ljava/lang/Object;"));
1280 str = (*env)->NewStringUTF(env, name);
1281 result = (*env)->CallStaticObjectMethod(env, cls, mid, JNI_TRUE, isJar, str);
duke6e45e102007-12-01 00:00:00 +00001282
1283 if (JLI_IsTraceLauncher()) {
1284 end = CounterGet();
1285 printf("%ld micro seconds to load main class\n",
1286 (long)(jint)Counter2Micros(end-start));
1287 printf("----_JAVA_LAUNCHER_DEBUG----\n");
1288 }
1289
ksrini20a64b22008-09-24 15:07:41 -07001290 return (jclass)result;
duke6e45e102007-12-01 00:00:00 +00001291}
1292
duke6e45e102007-12-01 00:00:00 +00001293/*
1294 * For tools, convert command line args thus:
1295 * javac -cp foo:foo/"*" -J-ms32m ...
1296 * java -ms32m -cp JLI_WildcardExpandClasspath(foo:foo/"*") ...
1297 *
1298 * Takes 4 parameters, and returns the populated arguments
1299 */
1300static void
1301TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv)
1302{
1303 int argc = *pargc;
1304 char **argv = *pargv;
1305 int nargc = argc + jargc;
1306 char **nargv = JLI_MemAlloc((nargc + 1) * sizeof(char *));
1307 int i;
1308
1309 *pargc = nargc;
1310 *pargv = nargv;
1311
1312 /* Copy the VM arguments (i.e. prefixed with -J) */
1313 for (i = 0; i < jargc; i++) {
1314 const char *arg = jargv[i];
1315 if (arg[0] == '-' && arg[1] == 'J') {
1316 *nargv++ = ((arg + 2) == NULL) ? NULL : JLI_StringDup(arg + 2);
1317 }
1318 }
1319
1320 for (i = 0; i < argc; i++) {
1321 char *arg = argv[i];
1322 if (arg[0] == '-' && arg[1] == 'J') {
1323 if (arg[2] == '\0') {
ksrini0e817162008-08-26 10:21:20 -07001324 JLI_ReportErrorMessage(ARG_ERROR3);
duke6e45e102007-12-01 00:00:00 +00001325 exit(1);
1326 }
1327 *nargv++ = arg + 2;
1328 }
1329 }
1330
1331 /* Copy the rest of the arguments */
1332 for (i = 0; i < jargc ; i++) {
1333 const char *arg = jargv[i];
1334 if (arg[0] != '-' || arg[1] != 'J') {
1335 *nargv++ = (arg == NULL) ? NULL : JLI_StringDup(arg);
1336 }
1337 }
1338 for (i = 0; i < argc; i++) {
1339 char *arg = argv[i];
1340 if (arg[0] == '-') {
1341 if (arg[1] == 'J')
1342 continue;
1343 if (IsWildCardEnabled() && arg[1] == 'c'
1344 && (JLI_StrCmp(arg, "-cp") == 0 ||
1345 JLI_StrCmp(arg, "-classpath") == 0)
1346 && i < argc - 1) {
1347 *nargv++ = arg;
1348 *nargv++ = (char *) JLI_WildcardExpandClasspath(argv[i+1]);
1349 i++;
1350 continue;
1351 }
1352 }
1353 *nargv++ = arg;
1354 }
1355 *nargv = 0;
1356}
1357
1358/*
1359 * For our tools, we try to add 3 VM options:
1360 * -Denv.class.path=<envcp>
1361 * -Dapplication.home=<apphome>
1362 * -Djava.class.path=<appcp>
1363 * <envcp> is the user's setting of CLASSPATH -- for instance the user
1364 * tells javac where to find binary classes through this environment
1365 * variable. Notice that users will be able to compile against our
1366 * tools classes (sun.tools.javac.Main) only if they explicitly add
1367 * tools.jar to CLASSPATH.
1368 * <apphome> is the directory where the application is installed.
1369 * <appcp> is the classpath to where our apps' classfiles are.
1370 */
1371static jboolean
1372AddApplicationOptions(int cpathc, const char **cpathv)
1373{
1374 char *envcp, *appcp, *apphome;
1375 char home[MAXPATHLEN]; /* application home */
1376 char separator[] = { PATH_SEPARATOR, '\0' };
1377 int size, i;
1378
1379 {
1380 const char *s = getenv("CLASSPATH");
1381 if (s) {
1382 s = (char *) JLI_WildcardExpandClasspath(s);
1383 /* 40 for -Denv.class.path= */
1384 envcp = (char *)JLI_MemAlloc(JLI_StrLen(s) + 40);
1385 sprintf(envcp, "-Denv.class.path=%s", s);
1386 AddOption(envcp, NULL);
1387 }
1388 }
1389
1390 if (!GetApplicationHome(home, sizeof(home))) {
ksrini0e817162008-08-26 10:21:20 -07001391 JLI_ReportErrorMessage(CFG_ERROR5);
duke6e45e102007-12-01 00:00:00 +00001392 return JNI_FALSE;
1393 }
1394
1395 /* 40 for '-Dapplication.home=' */
1396 apphome = (char *)JLI_MemAlloc(JLI_StrLen(home) + 40);
1397 sprintf(apphome, "-Dapplication.home=%s", home);
1398 AddOption(apphome, NULL);
1399
1400 /* How big is the application's classpath? */
1401 size = 40; /* 40: "-Djava.class.path=" */
1402 for (i = 0; i < cpathc; i++) {
1403 size += (int)JLI_StrLen(home) + (int)JLI_StrLen(cpathv[i]) + 1; /* 1: separator */
1404 }
1405 appcp = (char *)JLI_MemAlloc(size + 1);
1406 JLI_StrCpy(appcp, "-Djava.class.path=");
1407 for (i = 0; i < cpathc; i++) {
1408 JLI_StrCat(appcp, home); /* c:\program files\myapp */
1409 JLI_StrCat(appcp, cpathv[i]); /* \lib\myapp.jar */
1410 JLI_StrCat(appcp, separator); /* ; */
1411 }
1412 appcp[JLI_StrLen(appcp)-1] = '\0'; /* remove trailing path separator */
1413 AddOption(appcp, NULL);
1414 return JNI_TRUE;
1415}
1416
1417/*
1418 * inject the -Dsun.java.command pseudo property into the args structure
1419 * this pseudo property is used in the HotSpot VM to expose the
1420 * Java class name and arguments to the main method to the VM. The
1421 * HotSpot VM uses this pseudo property to store the Java class name
1422 * (or jar file name) and the arguments to the class's main method
1423 * to the instrumentation memory region. The sun.java.command pseudo
1424 * property is not exported by HotSpot to the Java layer.
1425 */
1426void
1427SetJavaCommandLineProp(char *classname, char *jarfile,
1428 int argc, char **argv)
1429{
1430
1431 int i = 0;
1432 size_t len = 0;
1433 char* javaCommand = NULL;
1434 char* dashDstr = "-Dsun.java.command=";
1435
1436 if (classname == NULL && jarfile == NULL) {
1437 /* unexpected, one of these should be set. just return without
1438 * setting the property
1439 */
1440 return;
1441 }
1442
1443 /* if the class name is not set, then use the jarfile name */
1444 if (classname == NULL) {
1445 classname = jarfile;
1446 }
1447
1448 /* determine the amount of memory to allocate assuming
1449 * the individual components will be space separated
1450 */
1451 len = JLI_StrLen(classname);
1452 for (i = 0; i < argc; i++) {
1453 len += JLI_StrLen(argv[i]) + 1;
1454 }
1455
1456 /* allocate the memory */
1457 javaCommand = (char*) JLI_MemAlloc(len + JLI_StrLen(dashDstr) + 1);
1458
1459 /* build the -D string */
1460 *javaCommand = '\0';
1461 JLI_StrCat(javaCommand, dashDstr);
1462 JLI_StrCat(javaCommand, classname);
1463
1464 for (i = 0; i < argc; i++) {
1465 /* the components of the string are space separated. In
1466 * the case of embedded white space, the relationship of
1467 * the white space separated components to their true
1468 * positional arguments will be ambiguous. This issue may
1469 * be addressed in a future release.
1470 */
1471 JLI_StrCat(javaCommand, " ");
1472 JLI_StrCat(javaCommand, argv[i]);
1473 }
1474
1475 AddOption(javaCommand, NULL);
1476}
1477
1478/*
1479 * JVM would like to know if it's created by a standard Sun launcher, or by
1480 * user native application, the following property indicates the former.
1481 */
1482void SetJavaLauncherProp() {
1483 AddOption("-Dsun.java.launcher=SUN_STANDARD", NULL);
1484}
1485
1486/*
1487 * Prints the version information from the java.version and other properties.
1488 */
1489static void
1490PrintJavaVersion(JNIEnv *env, jboolean extraLF)
1491{
1492 jclass ver;
1493 jmethodID print;
1494
ksrini20a64b22008-09-24 15:07:41 -07001495 NULL_CHECK(ver = FindBootStrapClass(env, "sun/misc/Version"));
duke6e45e102007-12-01 00:00:00 +00001496 NULL_CHECK(print = (*env)->GetStaticMethodID(env,
1497 ver,
1498 (extraLF == JNI_TRUE) ? "println" : "print",
1499 "()V"
1500 )
1501 );
1502
1503 (*env)->CallStaticVoidMethod(env, ver, print);
1504}
1505
1506/*
ksrini8e20e1c2010-11-23 16:52:39 -08001507 * Prints all the Java settings, see the java implementation for more details.
1508 */
1509static void
1510ShowSettings(JNIEnv *env, char *optString)
1511{
1512 jclass cls;
1513 jmethodID showSettingsID;
1514 jstring joptString;
1515 NULL_CHECK(cls = FindBootStrapClass(env, "sun/launcher/LauncherHelper"));
1516 NULL_CHECK(showSettingsID = (*env)->GetStaticMethodID(env, cls,
ksrini6d575f82010-12-23 13:51:30 -08001517 "showSettings", "(ZLjava/lang/String;JJJZ)V"));
ksrini8e20e1c2010-11-23 16:52:39 -08001518 joptString = (*env)->NewStringUTF(env, optString);
1519 (*env)->CallStaticVoidMethod(env, cls, showSettingsID,
1520 JNI_TRUE,
1521 joptString,
ksrini6d575f82010-12-23 13:51:30 -08001522 (jlong)initialHeapSize,
1523 (jlong)maxHeapSize,
ksrini8e20e1c2010-11-23 16:52:39 -08001524 (jlong)threadStackSize,
1525 ServerClassMachine());
1526}
1527
1528/*
ksrini20a64b22008-09-24 15:07:41 -07001529 * Prints default usage or the Xusage message, see sun.launcher.LauncherHelper.java
duke6e45e102007-12-01 00:00:00 +00001530 */
1531static void
1532PrintUsage(JNIEnv* env, jboolean doXUsage)
1533{
1534 jclass cls;
1535 jmethodID initHelp, vmSelect, vmSynonym, vmErgo, printHelp, printXUsageMessage;
1536 jstring jprogname, vm1, vm2;
1537 int i;
1538
ksrini20a64b22008-09-24 15:07:41 -07001539 NULL_CHECK(cls = FindBootStrapClass(env, "sun/launcher/LauncherHelper"));
duke6e45e102007-12-01 00:00:00 +00001540
1541
1542 if (doXUsage) {
1543 NULL_CHECK(printXUsageMessage = (*env)->GetStaticMethodID(env, cls,
1544 "printXUsageMessage", "(Z)V"));
1545 (*env)->CallStaticVoidMethod(env, cls, printXUsageMessage, JNI_TRUE);
1546 } else {
1547 NULL_CHECK(initHelp = (*env)->GetStaticMethodID(env, cls,
1548 "initHelpMessage", "(Ljava/lang/String;)V"));
1549
1550 NULL_CHECK(vmSelect = (*env)->GetStaticMethodID(env, cls, "appendVmSelectMessage",
1551 "(Ljava/lang/String;Ljava/lang/String;)V"));
1552
1553 NULL_CHECK(vmSynonym = (*env)->GetStaticMethodID(env, cls,
1554 "appendVmSynonymMessage",
1555 "(Ljava/lang/String;Ljava/lang/String;)V"));
1556 NULL_CHECK(vmErgo = (*env)->GetStaticMethodID(env, cls,
1557 "appendVmErgoMessage", "(ZLjava/lang/String;)V"));
1558
1559 NULL_CHECK(printHelp = (*env)->GetStaticMethodID(env, cls,
1560 "printHelpMessage", "(Z)V"));
1561
1562 jprogname = (*env)->NewStringUTF(env, _program_name);
1563
1564 /* Initialize the usage message with the usual preamble */
1565 (*env)->CallStaticVoidMethod(env, cls, initHelp, jprogname);
1566
1567
1568 /* Assemble the other variant part of the usage */
1569 if ((knownVMs[0].flag == VM_KNOWN) ||
1570 (knownVMs[0].flag == VM_IF_SERVER_CLASS)) {
1571 vm1 = (*env)->NewStringUTF(env, knownVMs[0].name);
1572 vm2 = (*env)->NewStringUTF(env, knownVMs[0].name+1);
1573 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1574 }
1575 for (i=1; i<knownVMsCount; i++) {
1576 if (knownVMs[i].flag == VM_KNOWN) {
1577 vm1 = (*env)->NewStringUTF(env, knownVMs[i].name);
1578 vm2 = (*env)->NewStringUTF(env, knownVMs[i].name+1);
1579 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1580 }
1581 }
1582 for (i=1; i<knownVMsCount; i++) {
1583 if (knownVMs[i].flag == VM_ALIASED_TO) {
1584 vm1 = (*env)->NewStringUTF(env, knownVMs[i].name);
1585 vm2 = (*env)->NewStringUTF(env, knownVMs[i].alias+1);
1586 (*env)->CallStaticVoidMethod(env, cls, vmSynonym, vm1, vm2);
1587 }
1588 }
1589
1590 /* The first known VM is the default */
1591 {
1592 jboolean isServerClassMachine = ServerClassMachine();
1593
1594 const char* defaultVM = knownVMs[0].name+1;
1595 if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) && isServerClassMachine) {
1596 defaultVM = knownVMs[0].server_class+1;
1597 }
1598
1599 vm1 = (*env)->NewStringUTF(env, defaultVM);
1600 (*env)->CallStaticVoidMethod(env, cls, vmErgo, isServerClassMachine, vm1);
1601 }
1602
1603 /* Complete the usage message and print to stderr*/
1604 (*env)->CallStaticVoidMethod(env, cls, printHelp, JNI_TRUE);
1605 }
1606 return;
1607}
1608
1609/*
1610 * Read the jvm.cfg file and fill the knownJVMs[] array.
1611 *
1612 * The functionality of the jvm.cfg file is subject to change without
1613 * notice and the mechanism will be removed in the future.
1614 *
1615 * The lexical structure of the jvm.cfg file is as follows:
1616 *
1617 * jvmcfg := { vmLine }
1618 * vmLine := knownLine
1619 * | aliasLine
1620 * | warnLine
1621 * | ignoreLine
1622 * | errorLine
1623 * | predicateLine
1624 * | commentLine
1625 * knownLine := flag "KNOWN" EOL
1626 * warnLine := flag "WARN" EOL
1627 * ignoreLine := flag "IGNORE" EOL
1628 * errorLine := flag "ERROR" EOL
1629 * aliasLine := flag "ALIASED_TO" flag EOL
1630 * predicateLine := flag "IF_SERVER_CLASS" flag EOL
1631 * commentLine := "#" text EOL
1632 * flag := "-" identifier
1633 *
1634 * The semantics are that when someone specifies a flag on the command line:
1635 * - if the flag appears on a knownLine, then the identifier is used as
1636 * the name of the directory holding the JVM library (the name of the JVM).
1637 * - if the flag appears as the first flag on an aliasLine, the identifier
1638 * of the second flag is used as the name of the JVM.
1639 * - if the flag appears on a warnLine, the identifier is used as the
1640 * name of the JVM, but a warning is generated.
1641 * - if the flag appears on an ignoreLine, the identifier is recognized as the
1642 * name of a JVM, but the identifier is ignored and the default vm used
1643 * - if the flag appears on an errorLine, an error is generated.
1644 * - if the flag appears as the first flag on a predicateLine, and
1645 * the machine on which you are running passes the predicate indicated,
1646 * then the identifier of the second flag is used as the name of the JVM,
1647 * otherwise the identifier of the first flag is used as the name of the JVM.
1648 * If no flag is given on the command line, the first vmLine of the jvm.cfg
1649 * file determines the name of the JVM.
1650 * PredicateLines are only interpreted on first vmLine of a jvm.cfg file,
1651 * since they only make sense if someone hasn't specified the name of the
1652 * JVM on the command line.
1653 *
1654 * The intent of the jvm.cfg file is to allow several JVM libraries to
1655 * be installed in different subdirectories of a single JRE installation,
1656 * for space-savings and convenience in testing.
1657 * The intent is explicitly not to provide a full aliasing or predicate
1658 * mechanism.
1659 */
1660jint
1661ReadKnownVMs(const char *jrepath, const char * arch, jboolean speculative)
1662{
1663 FILE *jvmCfg;
1664 char jvmCfgName[MAXPATHLEN+20];
1665 char line[MAXPATHLEN+20];
1666 int cnt = 0;
1667 int lineno = 0;
1668 jlong start, end;
1669 int vmType;
1670 char *tmpPtr;
1671 char *altVMName = NULL;
1672 char *serverClassVMName = NULL;
1673 static char *whiteSpace = " \t";
1674 if (JLI_IsTraceLauncher()) {
1675 start = CounterGet();
1676 }
ksrini01740ec2010-09-09 11:50:40 -07001677 JLI_Snprintf(jvmCfgName, sizeof(jvmCfgName), "%s%slib%s%s%sjvm.cfg",
1678 jrepath, FILESEP, FILESEP, arch, FILESEP);
duke6e45e102007-12-01 00:00:00 +00001679
1680 jvmCfg = fopen(jvmCfgName, "r");
1681 if (jvmCfg == NULL) {
1682 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -07001683 JLI_ReportErrorMessage(CFG_ERROR6, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001684 exit(1);
1685 } else {
1686 return -1;
1687 }
1688 }
1689 while (fgets(line, sizeof(line), jvmCfg) != NULL) {
1690 vmType = VM_UNKNOWN;
1691 lineno++;
1692 if (line[0] == '#')
1693 continue;
1694 if (line[0] != '-') {
ksrini0e817162008-08-26 10:21:20 -07001695 JLI_ReportErrorMessage(CFG_WARN2, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001696 }
1697 if (cnt >= knownVMsLimit) {
1698 GrowKnownVMs(cnt);
1699 }
1700 line[JLI_StrLen(line)-1] = '\0'; /* remove trailing newline */
1701 tmpPtr = line + JLI_StrCSpn(line, whiteSpace);
1702 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001703 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001704 } else {
1705 /* Null-terminate this string for JLI_StringDup below */
1706 *tmpPtr++ = 0;
1707 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1708 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001709 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001710 } else {
1711 if (!JLI_StrCCmp(tmpPtr, "KNOWN")) {
1712 vmType = VM_KNOWN;
1713 } else if (!JLI_StrCCmp(tmpPtr, "ALIASED_TO")) {
1714 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1715 if (*tmpPtr != 0) {
1716 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1717 }
1718 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001719 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001720 } else {
1721 /* Null terminate altVMName */
1722 altVMName = tmpPtr;
1723 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1724 *tmpPtr = 0;
1725 vmType = VM_ALIASED_TO;
1726 }
1727 } else if (!JLI_StrCCmp(tmpPtr, "WARN")) {
1728 vmType = VM_WARN;
1729 } else if (!JLI_StrCCmp(tmpPtr, "IGNORE")) {
1730 vmType = VM_IGNORE;
1731 } else if (!JLI_StrCCmp(tmpPtr, "ERROR")) {
1732 vmType = VM_ERROR;
1733 } else if (!JLI_StrCCmp(tmpPtr, "IF_SERVER_CLASS")) {
1734 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1735 if (*tmpPtr != 0) {
1736 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1737 }
1738 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001739 JLI_ReportErrorMessage(CFG_WARN4, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001740 } else {
1741 /* Null terminate server class VM name */
1742 serverClassVMName = tmpPtr;
1743 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1744 *tmpPtr = 0;
1745 vmType = VM_IF_SERVER_CLASS;
1746 }
1747 } else {
ksrini0e817162008-08-26 10:21:20 -07001748 JLI_ReportErrorMessage(CFG_WARN5, lineno, &jvmCfgName[0]);
duke6e45e102007-12-01 00:00:00 +00001749 vmType = VM_KNOWN;
1750 }
1751 }
1752 }
1753
1754 JLI_TraceLauncher("jvm.cfg[%d] = ->%s<-\n", cnt, line);
1755 if (vmType != VM_UNKNOWN) {
1756 knownVMs[cnt].name = JLI_StringDup(line);
1757 knownVMs[cnt].flag = vmType;
1758 switch (vmType) {
1759 default:
1760 break;
1761 case VM_ALIASED_TO:
1762 knownVMs[cnt].alias = JLI_StringDup(altVMName);
1763 JLI_TraceLauncher(" name: %s vmType: %s alias: %s\n",
1764 knownVMs[cnt].name, "VM_ALIASED_TO", knownVMs[cnt].alias);
1765 break;
1766 case VM_IF_SERVER_CLASS:
1767 knownVMs[cnt].server_class = JLI_StringDup(serverClassVMName);
1768 JLI_TraceLauncher(" name: %s vmType: %s server_class: %s\n",
1769 knownVMs[cnt].name, "VM_IF_SERVER_CLASS", knownVMs[cnt].server_class);
1770 break;
1771 }
1772 cnt++;
1773 }
1774 }
1775 fclose(jvmCfg);
1776 knownVMsCount = cnt;
1777
1778 if (JLI_IsTraceLauncher()) {
1779 end = CounterGet();
1780 printf("%ld micro seconds to parse jvm.cfg\n",
1781 (long)(jint)Counter2Micros(end-start));
1782 }
1783
1784 return cnt;
1785}
1786
1787
1788static void
1789GrowKnownVMs(int minimum)
1790{
1791 struct vmdesc* newKnownVMs;
1792 int newMax;
1793
1794 newMax = (knownVMsLimit == 0 ? INIT_MAX_KNOWN_VMS : (2 * knownVMsLimit));
1795 if (newMax <= minimum) {
1796 newMax = minimum;
1797 }
1798 newKnownVMs = (struct vmdesc*) JLI_MemAlloc(newMax * sizeof(struct vmdesc));
1799 if (knownVMs != NULL) {
1800 memcpy(newKnownVMs, knownVMs, knownVMsLimit * sizeof(struct vmdesc));
1801 }
1802 JLI_MemFree(knownVMs);
1803 knownVMs = newKnownVMs;
1804 knownVMsLimit = newMax;
1805}
1806
1807
1808/* Returns index of VM or -1 if not found */
1809static int
1810KnownVMIndex(const char* name)
1811{
1812 int i;
1813 if (JLI_StrCCmp(name, "-J") == 0) name += 2;
1814 for (i = 0; i < knownVMsCount; i++) {
1815 if (!JLI_StrCmp(name, knownVMs[i].name)) {
1816 return i;
1817 }
1818 }
1819 return -1;
1820}
1821
1822static void
1823FreeKnownVMs()
1824{
1825 int i;
1826 for (i = 0; i < knownVMsCount; i++) {
1827 JLI_MemFree(knownVMs[i].name);
1828 knownVMs[i].name = NULL;
1829 }
1830 JLI_MemFree(knownVMs);
1831}
1832
1833
1834/*
1835 * Displays the splash screen according to the jar file name
1836 * and image file names stored in environment variables
1837 */
1838static void
1839ShowSplashScreen()
1840{
1841 const char *jar_name = getenv(SPLASH_JAR_ENV_ENTRY);
1842 const char *file_name = getenv(SPLASH_FILE_ENV_ENTRY);
1843 int data_size;
1844 void *image_data;
1845 if (jar_name) {
1846 image_data = JLI_JarUnpackFile(jar_name, file_name, &data_size);
1847 if (image_data) {
1848 DoSplashInit();
1849 DoSplashLoadMemory(image_data, data_size);
1850 JLI_MemFree(image_data);
1851 }
1852 } else if (file_name) {
1853 DoSplashInit();
1854 DoSplashLoadFile(file_name);
1855 } else {
1856 return;
1857 }
1858 DoSplashSetFileJarName(file_name, jar_name);
1859
1860 /*
1861 * Done with all command line processing and potential re-execs so
1862 * clean up the environment.
1863 */
1864 (void)UnsetEnv(ENV_ENTRY);
1865 (void)UnsetEnv(SPLASH_FILE_ENV_ENTRY);
1866 (void)UnsetEnv(SPLASH_JAR_ENV_ENTRY);
1867
1868 JLI_MemFree(splash_jar_entry);
1869 JLI_MemFree(splash_file_entry);
1870
1871}
1872
1873const char*
1874GetDotVersion()
1875{
1876 return _dVersion;
1877}
1878
1879const char*
1880GetFullVersion()
1881{
1882 return _fVersion;
1883}
1884
1885const char*
1886GetProgramName()
1887{
1888 return _program_name;
1889}
1890
1891const char*
1892GetLauncherName()
1893{
1894 return _launcher_name;
1895}
1896
1897jint
1898GetErgoPolicy()
1899{
1900 return _ergo_policy;
1901}
1902
1903jboolean
1904IsJavaArgs()
1905{
1906 return _is_java_args;
1907}
1908
1909static jboolean
1910IsWildCardEnabled()
1911{
1912 return _wc_enabled;
1913}
1914
1915static int
1916ContinueInNewThread(InvocationFunctions* ifn, int argc,
1917 char **argv, char *jarfile, char *classname, int ret)
1918{
1919
1920 /*
1921 * If user doesn't specify stack size, check if VM has a preference.
1922 * Note that HotSpot no longer supports JNI_VERSION_1_1 but it will
1923 * return its default stack size through the init args structure.
1924 */
1925 if (threadStackSize == 0) {
1926 struct JDK1_1InitArgs args1_1;
1927 memset((void*)&args1_1, 0, sizeof(args1_1));
1928 args1_1.version = JNI_VERSION_1_1;
1929 ifn->GetDefaultJavaVMInitArgs(&args1_1); /* ignore return value */
1930 if (args1_1.javaStackSize > 0) {
1931 threadStackSize = args1_1.javaStackSize;
1932 }
1933 }
1934
1935 { /* Create a new thread to create JVM and invoke main method */
1936 JavaMainArgs args;
1937 int rslt;
1938
1939 args.argc = argc;
1940 args.argv = argv;
1941 args.jarfile = jarfile;
1942 args.classname = classname;
1943 args.ifn = *ifn;
1944
1945 rslt = ContinueInNewThread0(JavaMain, threadStackSize, (void*)&args);
1946 /* If the caller has deemed there is an error we
1947 * simply return that, otherwise we return the value of
1948 * the callee
1949 */
1950 return (ret != 0) ? ret : rslt;
1951 }
1952}
1953
1954static void
1955DumpState()
1956{
1957 if (!JLI_IsTraceLauncher()) return ;
1958 printf("Launcher state:\n");
1959 printf("\tdebug:%s\n", (JLI_IsTraceLauncher() == JNI_TRUE) ? "on" : "off");
1960 printf("\tjavargs:%s\n", (_is_java_args == JNI_TRUE) ? "on" : "off");
1961 printf("\tprogram name:%s\n", GetProgramName());
1962 printf("\tlauncher name:%s\n", GetLauncherName());
1963 printf("\tjavaw:%s\n", (IsJavaw() == JNI_TRUE) ? "on" : "off");
1964 printf("\tfullversion:%s\n", GetFullVersion());
1965 printf("\tdotversion:%s\n", GetDotVersion());
1966 printf("\tergo_policy:");
1967 switch(GetErgoPolicy()) {
1968 case NEVER_SERVER_CLASS:
1969 printf("NEVER_ACT_AS_A_SERVER_CLASS_MACHINE\n");
1970 break;
1971 case ALWAYS_SERVER_CLASS:
1972 printf("ALWAYS_ACT_AS_A_SERVER_CLASS_MACHINE\n");
1973 break;
1974 default:
1975 printf("DEFAULT_ERGONOMICS_POLICY\n");
1976 }
1977}
1978
1979/*
1980 * Return JNI_TRUE for an option string that has no effect but should
1981 * _not_ be passed on to the vm; return JNI_FALSE otherwise. On
1982 * Solaris SPARC, this screening needs to be done if:
ksrini11e7f1b2009-11-20 11:01:32 -08001983 * -d32 or -d64 is passed to a binary with an unmatched data model
1984 * (the exec in CreateExecutionEnvironment removes -d<n> options and points the
1985 * exec to the proper binary). In the case of when the data model and the
1986 * requested version is matched, an exec would not occur, and these options
1987 * were erroneously passed to the vm.
duke6e45e102007-12-01 00:00:00 +00001988 */
1989jboolean
1990RemovableOption(char * option)
1991{
1992 /*
1993 * Unconditionally remove both -d32 and -d64 options since only
1994 * the last such options has an effect; e.g.
1995 * java -d32 -d64 -d32 -version
1996 * is equivalent to
1997 * java -d32 -version
1998 */
1999
2000 if( (JLI_StrCCmp(option, "-d32") == 0 ) ||
2001 (JLI_StrCCmp(option, "-d64") == 0 ) )
2002 return JNI_TRUE;
2003 else
2004 return JNI_FALSE;
2005}
2006
2007/*
2008 * A utility procedure to always print to stderr
2009 */
2010void
ksrini0e817162008-08-26 10:21:20 -07002011JLI_ReportMessage(const char* fmt, ...)
duke6e45e102007-12-01 00:00:00 +00002012{
2013 va_list vl;
2014 va_start(vl, fmt);
2015 vfprintf(stderr, fmt, vl);
2016 fprintf(stderr, "\n");
2017 va_end(vl);
2018}