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