blob: 01eeb2a0547ab076b0db300e7e2eed29c301f54f [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*/
68
69static const char *_program_name;
70static const char *_launcher_name;
71static jboolean _is_java_args = JNI_FALSE;
72static const char *_fVersion;
73static const char *_dVersion;
74static jboolean _wc_enabled = JNI_FALSE;
75static jint _ergo_policy = DEFAULT_POLICY;
76
77/*
78 * Entries for splash screen environment variables.
79 * putenv is performed in SelectVersion. We need
80 * them in memory until UnsetEnv, so they are made static
81 * global instead of auto local.
82 */
83static char* splash_file_entry = NULL;
84static char* splash_jar_entry = NULL;
85
86/*
87 * List of VM options to be specified when the VM is created.
88 */
89static JavaVMOption *options;
90static int numOptions, maxOptions;
91
92/*
93 * Prototypes for functions internal to launcher.
94 */
95static void SetClassPath(const char *s);
mchung1c2c2d42009-12-18 11:42:03 -080096static void SetModulesBootClassPath(const char *s);
duke6e45e102007-12-01 00:00:00 +000097static void SelectVersion(int argc, char **argv, char **main_class);
98static jboolean ParseArguments(int *pargc, char ***pargv, char **pjarfile,
99 char **pclassname, int *pret, const char *jvmpath);
100static jboolean InitializeJVM(JavaVM **pvm, JNIEnv **penv,
101 InvocationFunctions *ifn);
102static jstring NewPlatformString(JNIEnv *env, char *s);
103static jobjectArray NewPlatformStringArray(JNIEnv *env, char **strv, int strc);
ksrini20a64b22008-09-24 15:07:41 -0700104static jclass LoadMainClass(JNIEnv *env, jboolean isJar, char *name);
duke6e45e102007-12-01 00:00:00 +0000105
106static void TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv);
107static jboolean AddApplicationOptions(int cpathc, const char **cpathv);
108static void SetApplicationClassPath(const char**);
109
110static void PrintJavaVersion(JNIEnv *env, jboolean extraLF);
111static void PrintUsage(JNIEnv* env, jboolean doXUsage);
112
113static void SetPaths(int argc, char **argv);
114
115static void DumpState();
116static jboolean RemovableOption(char *option);
117
118/* Maximum supported entries from jvm.cfg. */
119#define INIT_MAX_KNOWN_VMS 10
120
121/* Values for vmdesc.flag */
122enum vmdesc_flag {
123 VM_UNKNOWN = -1,
124 VM_KNOWN,
125 VM_ALIASED_TO,
126 VM_WARN,
127 VM_ERROR,
128 VM_IF_SERVER_CLASS,
129 VM_IGNORE
130};
131
132struct vmdesc {
133 char *name;
134 int flag;
135 char *alias;
136 char *server_class;
137};
138static struct vmdesc *knownVMs = NULL;
139static int knownVMsCount = 0;
140static int knownVMsLimit = 0;
141
142static void GrowKnownVMs();
143static int KnownVMIndex(const char* name);
144static void FreeKnownVMs();
145static void ShowSplashScreen();
146static jboolean IsWildCardEnabled();
147
148#define ARG_CHECK(n, f, a) if (n < 1) { \
ksrini0e817162008-08-26 10:21:20 -0700149 JLI_ReportErrorMessage(f, a); \
duke6e45e102007-12-01 00:00:00 +0000150 printUsage = JNI_TRUE; \
151 *pret = 1; \
152 return JNI_TRUE; \
153}
154
155/*
156 * Running Java code in primordial thread caused many problems. We will
157 * create a new thread to invoke JVM. See 6316197 for more information.
158 */
159static jlong threadStackSize = 0; /* stack size of the new thread */
160
161int JNICALL JavaMain(void * args); /* entry point */
162
163typedef struct {
164 int argc;
165 char ** argv;
166 char * jarfile;
167 char * classname;
168 InvocationFunctions ifn;
169} JavaMainArgs;
170
171/*
172 * Entry point.
173 */
174int
175JLI_Launch(int argc, char ** argv, /* main argc, argc */
176 int jargc, const char** jargv, /* java args */
177 int appclassc, const char** appclassv, /* app classpath */
178 const char* fullversion, /* full version defined */
179 const char* dotversion, /* dot version defined */
180 const char* pname, /* program name */
181 const char* lname, /* launcher name */
182 jboolean javaargs, /* JAVA_ARGS */
183 jboolean cpwildcard, /* classpath wildcard*/
184 jboolean javaw, /* windows-only javaw */
185 jint ergo /* ergonomics class policy */
186)
187{
188 char *jarfile = 0;
189 char *classname = 0;
190 char *cpath = 0;
191 char *main_class = NULL;
192 int ret;
193 InvocationFunctions ifn;
194 jlong start, end;
ksrinie3ec45d2010-07-09 11:04:34 -0700195 char jvmpath[MAXPATHLEN];
196 char jrepath[MAXPATHLEN];
duke6e45e102007-12-01 00:00:00 +0000197
198 _fVersion = fullversion;
199 _dVersion = dotversion;
200 _launcher_name = lname;
201 _program_name = pname;
202 _is_java_args = javaargs;
203 _wc_enabled = cpwildcard;
204 _ergo_policy = ergo;
205
ksrini52cded22008-03-06 07:51:28 -0800206 InitLauncher(javaw);
duke6e45e102007-12-01 00:00:00 +0000207 DumpState();
208
209 /*
210 * Make sure the specified version of the JRE is running.
211 *
212 * There are three things to note about the SelectVersion() routine:
213 * 1) If the version running isn't correct, this routine doesn't
214 * return (either the correct version has been exec'd or an error
215 * was issued).
216 * 2) Argc and Argv in this scope are *not* altered by this routine.
217 * It is the responsibility of subsequent code to ignore the
218 * arguments handled by this routine.
219 * 3) As a side-effect, the variable "main_class" is guaranteed to
220 * be set (if it should ever be set). This isn't exactly the
221 * poster child for structured programming, but it is a small
222 * price to pay for not processing a jar file operand twice.
223 * (Note: This side effect has been disabled. See comment on
224 * bugid 5030265 below.)
225 */
226 SelectVersion(argc, argv, &main_class);
227
ksrinie3ec45d2010-07-09 11:04:34 -0700228 if (JLI_IsTraceLauncher()) {
229 int i;
230 printf("Command line args:\n");
231 for (i = 0; i < argc ; i++) {
232 printf("argv[%d] = %s\n", i, argv[i]);
233 }
234 }
duke6e45e102007-12-01 00:00:00 +0000235
236 CreateExecutionEnvironment(&argc, &argv,
237 jrepath, sizeof(jrepath),
ksrinie3ec45d2010-07-09 11:04:34 -0700238 jvmpath, sizeof(jvmpath));
duke6e45e102007-12-01 00:00:00 +0000239
240 ifn.CreateJavaVM = 0;
241 ifn.GetDefaultJavaVMInitArgs = 0;
242
243 if (JLI_IsTraceLauncher()) {
244 start = CounterGet();
245 }
246
247 if (!LoadJavaVM(jvmpath, &ifn)) {
248 return(6);
249 }
250
251 if (JLI_IsTraceLauncher()) {
252 end = CounterGet();
253 }
254
255 JLI_TraceLauncher("%ld micro seconds to LoadJavaVM\n",
256 (long)(jint)Counter2Micros(end-start));
257
258 ++argv;
259 --argc;
260
261 if (IsJavaArgs()) {
262 /* Preprocess wrapper arguments */
263 TranslateApplicationArgs(jargc, jargv, &argc, &argv);
264 if (!AddApplicationOptions(appclassc, appclassv)) {
265 return(1);
266 }
267 } else {
268 /* Set default CLASSPATH */
269 cpath = getenv("CLASSPATH");
270 if (cpath == NULL) {
271 cpath = ".";
272 }
273 SetClassPath(cpath);
274 }
275
276 /*
277 * Parse command line options; if the return value of
278 * ParseArguments is false, the program should exit.
279 */
280 if (!ParseArguments(&argc, &argv, &jarfile, &classname, &ret, jvmpath)) {
281 return(ret);
282 }
283
mchung1c2c2d42009-12-18 11:42:03 -0800284 /* Set bootclasspath for modules */
285 SetModulesBootClassPath(jrepath);
286
duke6e45e102007-12-01 00:00:00 +0000287 /* Override class path if -jar flag was specified */
288 if (jarfile != 0) {
289 SetClassPath(jarfile);
290 }
291
292 /* set the -Dsun.java.command pseudo property */
293 SetJavaCommandLineProp(classname, jarfile, argc, argv);
294
295 /* Set the -Dsun.java.launcher pseudo property */
296 SetJavaLauncherProp();
297
298 /* set the -Dsun.java.launcher.* platform properties */
299 SetJavaLauncherPlatformProps();
300
301 /* Show the splash screen if needed */
302 ShowSplashScreen();
303
304 return ContinueInNewThread(&ifn, argc, argv, jarfile, classname, ret);
305
306}
ksrinie3ec45d2010-07-09 11:04:34 -0700307/*
308 * Always detach the main thread so that it appears to have ended when
309 * the application's main method exits. This will invoke the
310 * uncaught exception handler machinery if main threw an
311 * exception. An uncaught exception handler cannot change the
312 * launcher's return code except by calling System.exit.
313 *
314 * Wait for all non-daemon threads to end, then destroy the VM.
315 * This will actually create a trivial new Java waiter thread
316 * named "DestroyJavaVM", but this will be seen as a different
317 * thread from the one that executed main, even though they are
318 * the same C thread. This allows mainThread.join() and
319 * mainThread.isAlive() to work as expected.
320 */
321#define LEAVE() \
322 if ((*vm)->DetachCurrentThread(vm) != 0) { \
323 JLI_ReportErrorMessage(JVM_ERROR2); \
324 ret = 1; \
325 } \
326 (*vm)->DestroyJavaVM(vm); \
327 return ret \
duke6e45e102007-12-01 00:00:00 +0000328
ksrini20a64b22008-09-24 15:07:41 -0700329#define CHECK_EXCEPTION_NULL_LEAVE(e) \
330 if ((*env)->ExceptionOccurred(env)) { \
331 JLI_ReportExceptionDescription(env); \
ksrinie3ec45d2010-07-09 11:04:34 -0700332 LEAVE(); \
ksrini20a64b22008-09-24 15:07:41 -0700333 } \
334 if ((e) == NULL) { \
335 JLI_ReportErrorMessage(JNI_ERROR); \
ksrinie3ec45d2010-07-09 11:04:34 -0700336 LEAVE(); \
ksrini20a64b22008-09-24 15:07:41 -0700337 }
338
339#define CHECK_EXCEPTION_LEAVE(rv) \
340 if ((*env)->ExceptionOccurred(env)) { \
341 JLI_ReportExceptionDescription(env); \
342 ret = (rv); \
ksrinie3ec45d2010-07-09 11:04:34 -0700343 LEAVE(); \
ksrini20a64b22008-09-24 15:07:41 -0700344 }
duke6e45e102007-12-01 00:00:00 +0000345
346int JNICALL
347JavaMain(void * _args)
348{
349 JavaMainArgs *args = (JavaMainArgs *)_args;
350 int argc = args->argc;
351 char **argv = args->argv;
352 char *jarfile = args->jarfile;
353 char *classname = args->classname;
354 InvocationFunctions ifn = args->ifn;
355
356 JavaVM *vm = 0;
357 JNIEnv *env = 0;
duke6e45e102007-12-01 00:00:00 +0000358 jclass mainClass;
359 jmethodID mainID;
360 jobjectArray mainArgs;
361 int ret = 0;
362 jlong start, end;
363
duke6e45e102007-12-01 00:00:00 +0000364 /* Initialize the virtual machine */
duke6e45e102007-12-01 00:00:00 +0000365 start = CounterGet();
366 if (!InitializeJVM(&vm, &env, &ifn)) {
ksrini0e817162008-08-26 10:21:20 -0700367 JLI_ReportErrorMessage(JVM_ERROR1);
duke6e45e102007-12-01 00:00:00 +0000368 exit(1);
369 }
370
371 if (printVersion || showVersion) {
372 PrintJavaVersion(env, showVersion);
ksrini20a64b22008-09-24 15:07:41 -0700373 CHECK_EXCEPTION_LEAVE(0);
duke6e45e102007-12-01 00:00:00 +0000374 if (printVersion) {
ksrinie3ec45d2010-07-09 11:04:34 -0700375 LEAVE();
duke6e45e102007-12-01 00:00:00 +0000376 }
377 }
378
379 /* If the user specified neither a class name nor a JAR file */
380 if (printXUsage || printUsage || (jarfile == 0 && classname == 0)) {
381 PrintUsage(env, printXUsage);
ksrini20a64b22008-09-24 15:07:41 -0700382 CHECK_EXCEPTION_LEAVE(1);
ksrinie3ec45d2010-07-09 11:04:34 -0700383 LEAVE();
duke6e45e102007-12-01 00:00:00 +0000384 }
385
386 FreeKnownVMs(); /* after last possible PrintUsage() */
387
388 if (JLI_IsTraceLauncher()) {
389 end = CounterGet();
390 JLI_TraceLauncher("%ld micro seconds to InitializeJVM\n",
391 (long)(jint)Counter2Micros(end-start));
392 }
393
394 /* At this stage, argc/argv have the applications' arguments */
395 if (JLI_IsTraceLauncher()){
396 int i;
397 printf("Main-Class is '%s'\n", classname ? classname : "");
398 printf("Apps' argc is %d\n", argc);
399 for (i=0; i < argc; i++) {
400 printf(" argv[%2d] = '%s'\n", i, argv[i]);
401 }
402 }
403
404 ret = 1;
405
406 /*
407 * Get the application's main class.
408 *
409 * See bugid 5030265. The Main-Class name has already been parsed
410 * from the manifest, but not parsed properly for UTF-8 support.
411 * Hence the code here ignores the value previously extracted and
412 * uses the pre-existing code to reextract the value. This is
413 * possibly an end of release cycle expedient. However, it has
414 * also been discovered that passing some character sets through
415 * the environment has "strange" behavior on some variants of
416 * Windows. Hence, maybe the manifest parsing code local to the
417 * launcher should never be enhanced.
418 *
419 * Hence, future work should either:
420 * 1) Correct the local parsing code and verify that the
421 * Main-Class attribute gets properly passed through
422 * all environments,
423 * 2) Remove the vestages of maintaining main_class through
424 * the environment (and remove these comments).
425 */
426 if (jarfile != 0) {
ksrini20a64b22008-09-24 15:07:41 -0700427 mainClass = LoadMainClass(env, JNI_TRUE, jarfile);
duke6e45e102007-12-01 00:00:00 +0000428 } else {
ksrini20a64b22008-09-24 15:07:41 -0700429 mainClass = LoadMainClass(env, JNI_FALSE, classname);
duke6e45e102007-12-01 00:00:00 +0000430 }
ksrini20a64b22008-09-24 15:07:41 -0700431 CHECK_EXCEPTION_NULL_LEAVE(mainClass);
duke6e45e102007-12-01 00:00:00 +0000432
ksrini20a64b22008-09-24 15:07:41 -0700433 /*
434 * The LoadMainClass not only loads the main class, it will also ensure
435 * that the main method's signature is correct, therefore further checking
436 * is not required. The main method is invoked here so that extraneous java
437 * stacks are not in the application stack trace.
438 */
duke6e45e102007-12-01 00:00:00 +0000439 mainID = (*env)->GetStaticMethodID(env, mainClass, "main",
440 "([Ljava/lang/String;)V");
ksrini20a64b22008-09-24 15:07:41 -0700441 CHECK_EXCEPTION_NULL_LEAVE(mainID);
duke6e45e102007-12-01 00:00:00 +0000442
443 /* Build argument array */
444 mainArgs = NewPlatformStringArray(env, argv, argc);
ksrini20a64b22008-09-24 15:07:41 -0700445 CHECK_EXCEPTION_NULL_LEAVE(mainArgs);
duke6e45e102007-12-01 00:00:00 +0000446
447 /* Invoke main method. */
448 (*env)->CallStaticVoidMethod(env, mainClass, mainID, mainArgs);
449
450 /*
451 * The launcher's exit code (in the absence of calls to
452 * System.exit) will be non-zero if main threw an exception.
453 */
454 ret = (*env)->ExceptionOccurred(env) == NULL ? 0 : 1;
ksrinie3ec45d2010-07-09 11:04:34 -0700455 LEAVE();
duke6e45e102007-12-01 00:00:00 +0000456}
457
duke6e45e102007-12-01 00:00:00 +0000458/*
459 * Checks the command line options to find which JVM type was
460 * specified. If no command line option was given for the JVM type,
461 * the default type is used. The environment variable
462 * JDK_ALTERNATE_VM and the command line option -XXaltjvm= are also
463 * checked as ways of specifying which JVM type to invoke.
464 */
465char *
466CheckJvmType(int *pargc, char ***argv, jboolean speculative) {
467 int i, argi;
468 int argc;
469 char **newArgv;
470 int newArgvIdx = 0;
471 int isVMType;
472 int jvmidx = -1;
473 char *jvmtype = getenv("JDK_ALTERNATE_VM");
474
475 argc = *pargc;
476
477 /* To make things simpler we always copy the argv array */
478 newArgv = JLI_MemAlloc((argc + 1) * sizeof(char *));
479
480 /* The program name is always present */
481 newArgv[newArgvIdx++] = (*argv)[0];
482
483 for (argi = 1; argi < argc; argi++) {
484 char *arg = (*argv)[argi];
485 isVMType = 0;
486
487 if (IsJavaArgs()) {
488 if (arg[0] != '-') {
489 newArgv[newArgvIdx++] = arg;
490 continue;
491 }
492 } else {
493 if (JLI_StrCmp(arg, "-classpath") == 0 ||
494 JLI_StrCmp(arg, "-cp") == 0) {
495 newArgv[newArgvIdx++] = arg;
496 argi++;
497 if (argi < argc) {
498 newArgv[newArgvIdx++] = (*argv)[argi];
499 }
500 continue;
501 }
502 if (arg[0] != '-') break;
503 }
504
505 /* Did the user pass an explicit VM type? */
506 i = KnownVMIndex(arg);
507 if (i >= 0) {
508 jvmtype = knownVMs[jvmidx = i].name + 1; /* skip the - */
509 isVMType = 1;
510 *pargc = *pargc - 1;
511 }
512
513 /* Did the user specify an "alternate" VM? */
514 else if (JLI_StrCCmp(arg, "-XXaltjvm=") == 0 || JLI_StrCCmp(arg, "-J-XXaltjvm=") == 0) {
515 isVMType = 1;
516 jvmtype = arg+((arg[1]=='X')? 10 : 12);
517 jvmidx = -1;
518 }
519
520 if (!isVMType) {
521 newArgv[newArgvIdx++] = arg;
522 }
523 }
524
525 /*
526 * Finish copying the arguments if we aborted the above loop.
527 * NOTE that if we aborted via "break" then we did NOT copy the
528 * last argument above, and in addition argi will be less than
529 * argc.
530 */
531 while (argi < argc) {
532 newArgv[newArgvIdx++] = (*argv)[argi];
533 argi++;
534 }
535
536 /* argv is null-terminated */
537 newArgv[newArgvIdx] = 0;
538
539 /* Copy back argv */
540 *argv = newArgv;
541 *pargc = newArgvIdx;
542
543 /* use the default VM type if not specified (no alias processing) */
544 if (jvmtype == NULL) {
545 char* result = knownVMs[0].name+1;
546 /* Use a different VM type if we are on a server class machine? */
547 if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) &&
548 (ServerClassMachine() == JNI_TRUE)) {
549 result = knownVMs[0].server_class+1;
550 }
551 JLI_TraceLauncher("Default VM: %s\n", result);
552 return result;
553 }
554
555 /* if using an alternate VM, no alias processing */
556 if (jvmidx < 0)
557 return jvmtype;
558
559 /* Resolve aliases first */
560 {
561 int loopCount = 0;
562 while (knownVMs[jvmidx].flag == VM_ALIASED_TO) {
563 int nextIdx = KnownVMIndex(knownVMs[jvmidx].alias);
564
565 if (loopCount > knownVMsCount) {
566 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700567 JLI_ReportErrorMessage(CFG_ERROR1);
duke6e45e102007-12-01 00:00:00 +0000568 exit(1);
569 } else {
570 return "ERROR";
571 /* break; */
572 }
573 }
574
575 if (nextIdx < 0) {
576 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700577 JLI_ReportErrorMessage(CFG_ERROR2, knownVMs[jvmidx].alias);
duke6e45e102007-12-01 00:00:00 +0000578 exit(1);
579 } else {
580 return "ERROR";
581 }
582 }
583 jvmidx = nextIdx;
584 jvmtype = knownVMs[jvmidx].name+1;
585 loopCount++;
586 }
587 }
588
589 switch (knownVMs[jvmidx].flag) {
590 case VM_WARN:
591 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700592 JLI_ReportErrorMessage(CFG_WARN1, jvmtype, knownVMs[0].name + 1);
duke6e45e102007-12-01 00:00:00 +0000593 }
594 /* fall through */
595 case VM_IGNORE:
596 jvmtype = knownVMs[jvmidx=0].name + 1;
597 /* fall through */
598 case VM_KNOWN:
599 break;
600 case VM_ERROR:
601 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700602 JLI_ReportErrorMessage(CFG_ERROR3, jvmtype);
duke6e45e102007-12-01 00:00:00 +0000603 exit(1);
604 } else {
605 return "ERROR";
606 }
607 }
608
609 return jvmtype;
610}
611
612/* copied from HotSpot function "atomll()" */
613static int
614parse_stack_size(const char *s, jlong *result) {
615 jlong n = 0;
616 int args_read = sscanf(s, jlong_format_specifier(), &n);
617 if (args_read != 1) {
618 return 0;
619 }
620 while (*s != '\0' && *s >= '0' && *s <= '9') {
621 s++;
622 }
623 // 4705540: illegal if more characters are found after the first non-digit
624 if (JLI_StrLen(s) > 1) {
625 return 0;
626 }
627 switch (*s) {
628 case 'T': case 't':
629 *result = n * GB * KB;
630 return 1;
631 case 'G': case 'g':
632 *result = n * GB;
633 return 1;
634 case 'M': case 'm':
635 *result = n * MB;
636 return 1;
637 case 'K': case 'k':
638 *result = n * KB;
639 return 1;
640 case '\0':
641 *result = n;
642 return 1;
643 default:
644 /* Create JVM with default stack and let VM handle malformed -Xss string*/
645 return 0;
646 }
647}
648
649/*
650 * Adds a new VM option with the given given name and value.
651 */
652void
653AddOption(char *str, void *info)
654{
655 /*
656 * Expand options array if needed to accommodate at least one more
657 * VM option.
658 */
659 if (numOptions >= maxOptions) {
660 if (options == 0) {
661 maxOptions = 4;
662 options = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
663 } else {
664 JavaVMOption *tmp;
665 maxOptions *= 2;
666 tmp = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
667 memcpy(tmp, options, numOptions * sizeof(JavaVMOption));
668 JLI_MemFree(options);
669 options = tmp;
670 }
671 }
672 options[numOptions].optionString = str;
673 options[numOptions++].extraInfo = info;
674
675 if (JLI_StrCCmp(str, "-Xss") == 0) {
676 jlong tmp;
677 if (parse_stack_size(str + 4, &tmp)) {
678 threadStackSize = tmp;
679 }
680 }
681}
682
683static void
684SetClassPath(const char *s)
685{
686 char *def;
martinc0ca3352009-06-22 16:41:27 -0700687 const char *orig = s;
688 static const char format[] = "-Djava.class.path=%s";
duke6e45e102007-12-01 00:00:00 +0000689 s = JLI_WildcardExpandClasspath(s);
martinc0ca3352009-06-22 16:41:27 -0700690 def = JLI_MemAlloc(sizeof(format)
691 - 2 /* strlen("%s") */
692 + JLI_StrLen(s));
693 sprintf(def, format, s);
duke6e45e102007-12-01 00:00:00 +0000694 AddOption(def, NULL);
martinc0ca3352009-06-22 16:41:27 -0700695 if (s != orig)
696 JLI_MemFree((char *) s);
duke6e45e102007-12-01 00:00:00 +0000697}
698
699/*
mchung1c2c2d42009-12-18 11:42:03 -0800700 * Set the bootclasspath for modules.
701 * A temporary workaround until jigsaw is integrated into JDK 7.
702 */
703static void
704SetModulesBootClassPath(const char *jrepath)
705{
706 char *def, *s;
707 char pathname[MAXPATHLEN];
708 const char separator[] = { FILE_SEPARATOR, '\0' };
709 const char *orig = jrepath;
710 static const char format[] = "-Xbootclasspath/p:%s";
711 struct stat statbuf;
712
713 /* return if jre/lib/rt.jar exists */
ksrini01740ec2010-09-09 11:50:40 -0700714 JLI_Snprintf(pathname, sizeof(pathname), "%s%slib%srt.jar", jrepath, separator, separator);
mchung1c2c2d42009-12-18 11:42:03 -0800715 if (stat(pathname, &statbuf) == 0) {
716 return;
717 }
718
719 /* return if jre/classes exists */
ksrini01740ec2010-09-09 11:50:40 -0700720 JLI_Snprintf(pathname, sizeof(pathname), "%s%sclasses", jrepath, separator);
mchung1c2c2d42009-12-18 11:42:03 -0800721 if (stat(pathname, &statbuf) == 0) {
722 return;
723 }
724
725 /* modularized jre */
ksrini01740ec2010-09-09 11:50:40 -0700726 JLI_Snprintf(pathname, sizeof(pathname), "%s%slib%s*", jrepath, separator, separator);
mchung1c2c2d42009-12-18 11:42:03 -0800727 s = (char *) JLI_WildcardExpandClasspath(pathname);
728 def = JLI_MemAlloc(sizeof(format)
729 - 2 /* strlen("%s") */
730 + JLI_StrLen(s));
731 sprintf(def, format, s);
732 AddOption(def, NULL);
733 if (s != orig)
734 JLI_MemFree((char *) s);
735}
736
737/*
duke6e45e102007-12-01 00:00:00 +0000738 * The SelectVersion() routine ensures that an appropriate version of
739 * the JRE is running. The specification for the appropriate version
740 * is obtained from either the manifest of a jar file (preferred) or
741 * from command line options.
742 * The routine also parses splash screen command line options and
743 * passes on their values in private environment variables.
744 */
745static void
746SelectVersion(int argc, char **argv, char **main_class)
747{
748 char *arg;
749 char **new_argv;
750 char **new_argp;
751 char *operand;
752 char *version = NULL;
753 char *jre = NULL;
754 int jarflag = 0;
755 int headlessflag = 0;
756 int restrict_search = -1; /* -1 implies not known */
757 manifest_info info;
758 char env_entry[MAXNAMELEN + 24] = ENV_ENTRY "=";
759 char *splash_file_name = NULL;
760 char *splash_jar_name = NULL;
761 char *env_in;
762 int res;
763
764 /*
765 * If the version has already been selected, set *main_class
766 * with the value passed through the environment (if any) and
767 * simply return.
768 */
769 if ((env_in = getenv(ENV_ENTRY)) != NULL) {
770 if (*env_in != '\0')
771 *main_class = JLI_StringDup(env_in);
772 return;
773 }
774
775 /*
776 * Scan through the arguments for options relevant to multiple JRE
777 * support. For reference, the command line syntax is defined as:
778 *
779 * SYNOPSIS
780 * java [options] class [argument...]
781 *
782 * java [options] -jar file.jar [argument...]
783 *
784 * As the scan is performed, make a copy of the argument list with
785 * the version specification options (new to 1.5) removed, so that
786 * a version less than 1.5 can be exec'd.
787 *
788 * Note that due to the syntax of the native Windows interface
789 * CreateProcess(), processing similar to the following exists in
790 * the Windows platform specific routine ExecJRE (in java_md.c).
791 * Changes here should be reproduced there.
792 */
793 new_argv = JLI_MemAlloc((argc + 1) * sizeof(char*));
794 new_argv[0] = argv[0];
795 new_argp = &new_argv[1];
796 argc--;
797 argv++;
798 while ((arg = *argv) != 0 && *arg == '-') {
799 if (JLI_StrCCmp(arg, "-version:") == 0) {
800 version = arg + 9;
801 } else if (JLI_StrCmp(arg, "-jre-restrict-search") == 0) {
802 restrict_search = 1;
803 } else if (JLI_StrCmp(arg, "-no-jre-restrict-search") == 0) {
804 restrict_search = 0;
805 } else {
806 if (JLI_StrCmp(arg, "-jar") == 0)
807 jarflag = 1;
808 /* deal with "unfortunate" classpath syntax */
809 if ((JLI_StrCmp(arg, "-classpath") == 0 || JLI_StrCmp(arg, "-cp") == 0) &&
810 (argc >= 2)) {
811 *new_argp++ = arg;
812 argc--;
813 argv++;
814 arg = *argv;
815 }
816
817 /*
818 * Checking for headless toolkit option in the some way as AWT does:
819 * "true" means true and any other value means false
820 */
821 if (JLI_StrCmp(arg, "-Djava.awt.headless=true") == 0) {
822 headlessflag = 1;
823 } else if (JLI_StrCCmp(arg, "-Djava.awt.headless=") == 0) {
824 headlessflag = 0;
825 } else if (JLI_StrCCmp(arg, "-splash:") == 0) {
826 splash_file_name = arg+8;
827 }
828 *new_argp++ = arg;
829 }
830 argc--;
831 argv++;
832 }
833 if (argc <= 0) { /* No operand? Possibly legit with -[full]version */
834 operand = NULL;
835 } else {
836 argc--;
837 *new_argp++ = operand = *argv++;
838 }
839 while (argc-- > 0) /* Copy over [argument...] */
840 *new_argp++ = *argv++;
841 *new_argp = NULL;
842
843 /*
844 * If there is a jar file, read the manifest. If the jarfile can't be
845 * read, the manifest can't be read from the jar file, or the manifest
846 * is corrupt, issue the appropriate error messages and exit.
847 *
848 * Even if there isn't a jar file, construct a manifest_info structure
849 * containing the command line information. It's a convenient way to carry
850 * this data around.
851 */
852 if (jarflag && operand) {
853 if ((res = JLI_ParseManifest(operand, &info)) != 0) {
854 if (res == -1)
ksrini0e817162008-08-26 10:21:20 -0700855 JLI_ReportErrorMessage(JAR_ERROR2, operand);
duke6e45e102007-12-01 00:00:00 +0000856 else
ksrini0e817162008-08-26 10:21:20 -0700857 JLI_ReportErrorMessage(JAR_ERROR3, operand);
duke6e45e102007-12-01 00:00:00 +0000858 exit(1);
859 }
860
861 /*
862 * Command line splash screen option should have precedence
863 * over the manifest, so the manifest data is used only if
864 * splash_file_name has not been initialized above during command
865 * line parsing
866 */
867 if (!headlessflag && !splash_file_name && info.splashscreen_image_file_name) {
868 splash_file_name = info.splashscreen_image_file_name;
869 splash_jar_name = operand;
870 }
871 } else {
872 info.manifest_version = NULL;
873 info.main_class = NULL;
874 info.jre_version = NULL;
875 info.jre_restrict_search = 0;
876 }
877
878 /*
879 * Passing on splash screen info in environment variables
880 */
881 if (splash_file_name && !headlessflag) {
882 char* splash_file_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_FILE_ENV_ENTRY "=")+JLI_StrLen(splash_file_name)+1);
883 JLI_StrCpy(splash_file_entry, SPLASH_FILE_ENV_ENTRY "=");
884 JLI_StrCat(splash_file_entry, splash_file_name);
885 putenv(splash_file_entry);
886 }
887 if (splash_jar_name && !headlessflag) {
888 char* splash_jar_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_JAR_ENV_ENTRY "=")+JLI_StrLen(splash_jar_name)+1);
889 JLI_StrCpy(splash_jar_entry, SPLASH_JAR_ENV_ENTRY "=");
890 JLI_StrCat(splash_jar_entry, splash_jar_name);
891 putenv(splash_jar_entry);
892 }
893
894 /*
895 * The JRE-Version and JRE-Restrict-Search values (if any) from the
896 * manifest are overwritten by any specified on the command line.
897 */
898 if (version != NULL)
899 info.jre_version = version;
900 if (restrict_search != -1)
901 info.jre_restrict_search = restrict_search;
902
903 /*
904 * "Valid" returns (other than unrecoverable errors) follow. Set
905 * main_class as a side-effect of this routine.
906 */
907 if (info.main_class != NULL)
908 *main_class = JLI_StringDup(info.main_class);
909
910 /*
911 * If no version selection information is found either on the command
912 * line or in the manifest, simply return.
913 */
914 if (info.jre_version == NULL) {
915 JLI_FreeManifest();
916 JLI_MemFree(new_argv);
917 return;
918 }
919
920 /*
921 * Check for correct syntax of the version specification (JSR 56).
922 */
923 if (!JLI_ValidVersionString(info.jre_version)) {
ksrini0e817162008-08-26 10:21:20 -0700924 JLI_ReportErrorMessage(SPC_ERROR1, info.jre_version);
duke6e45e102007-12-01 00:00:00 +0000925 exit(1);
926 }
927
928 /*
929 * Find the appropriate JVM on the system. Just to be as forgiving as
930 * possible, if the standard algorithms don't locate an appropriate
931 * jre, check to see if the one running will satisfy the requirements.
932 * This can happen on systems which haven't been set-up for multiple
933 * JRE support.
934 */
935 jre = LocateJRE(&info);
936 JLI_TraceLauncher("JRE-Version = %s, JRE-Restrict-Search = %s Selected = %s\n",
937 (info.jre_version?info.jre_version:"null"),
938 (info.jre_restrict_search?"true":"false"), (jre?jre:"null"));
939
940 if (jre == NULL) {
941 if (JLI_AcceptableRelease(GetFullVersion(), info.jre_version)) {
942 JLI_FreeManifest();
943 JLI_MemFree(new_argv);
944 return;
945 } else {
ksrini0e817162008-08-26 10:21:20 -0700946 JLI_ReportErrorMessage(CFG_ERROR4, info.jre_version);
duke6e45e102007-12-01 00:00:00 +0000947 exit(1);
948 }
949 }
950
951 /*
952 * If I'm not the chosen one, exec the chosen one. Returning from
953 * ExecJRE indicates that I am indeed the chosen one.
954 *
955 * The private environment variable _JAVA_VERSION_SET is used to
956 * prevent the chosen one from re-reading the manifest file and
957 * using the values found within to override the (potential) command
958 * line flags stripped from argv (because the target may not
959 * understand them). Passing the MainClass value is an optimization
960 * to avoid locating, expanding and parsing the manifest extra
961 * times.
962 */
ksrini8760ca92008-09-04 09:43:32 -0700963 if (info.main_class != NULL) {
964 if (JLI_StrLen(info.main_class) <= MAXNAMELEN) {
965 (void)JLI_StrCat(env_entry, info.main_class);
966 } else {
asaha80ccd422009-04-16 21:08:04 -0700967 JLI_ReportErrorMessage(CLS_ERROR5, MAXNAMELEN);
ksrini8760ca92008-09-04 09:43:32 -0700968 exit(1);
969 }
970 }
duke6e45e102007-12-01 00:00:00 +0000971 (void)putenv(env_entry);
972 ExecJRE(jre, new_argv);
973 JLI_FreeManifest();
974 JLI_MemFree(new_argv);
975 return;
976}
977
978/*
979 * Parses command line arguments. Returns JNI_FALSE if launcher
980 * should exit without starting vm, returns JNI_TRUE if vm needs
981 * to be started to process given options. *pret (the launcher
982 * process return value) is set to 0 for a normal exit.
983 */
984static jboolean
985ParseArguments(int *pargc, char ***pargv, char **pjarfile,
986 char **pclassname, int *pret, const char *jvmpath)
987{
988 int argc = *pargc;
989 char **argv = *pargv;
990 jboolean jarflag = JNI_FALSE;
991 char *arg;
992
993 *pret = 0;
994
995 while ((arg = *argv) != 0 && *arg == '-') {
996 argv++; --argc;
997 if (JLI_StrCmp(arg, "-classpath") == 0 || JLI_StrCmp(arg, "-cp") == 0) {
998 ARG_CHECK (argc, ARG_ERROR1, arg);
999 SetClassPath(*argv);
1000 argv++; --argc;
1001 } else if (JLI_StrCmp(arg, "-jar") == 0) {
1002 ARG_CHECK (argc, ARG_ERROR2, arg);
1003 jarflag = JNI_TRUE;
1004 } else if (JLI_StrCmp(arg, "-help") == 0 ||
1005 JLI_StrCmp(arg, "-h") == 0 ||
1006 JLI_StrCmp(arg, "-?") == 0) {
1007 printUsage = JNI_TRUE;
1008 return JNI_TRUE;
1009 } else if (JLI_StrCmp(arg, "-version") == 0) {
1010 printVersion = JNI_TRUE;
1011 return JNI_TRUE;
1012 } else if (JLI_StrCmp(arg, "-showversion") == 0) {
1013 showVersion = JNI_TRUE;
1014 } else if (JLI_StrCmp(arg, "-X") == 0) {
1015 printXUsage = JNI_TRUE;
1016 return JNI_TRUE;
1017/*
1018 * The following case provide backward compatibility with old-style
1019 * command line options.
1020 */
1021 } else if (JLI_StrCmp(arg, "-fullversion") == 0) {
ksrini0e817162008-08-26 10:21:20 -07001022 JLI_ReportMessage("%s full version \"%s\"", _launcher_name, GetFullVersion());
duke6e45e102007-12-01 00:00:00 +00001023 return JNI_FALSE;
1024 } else if (JLI_StrCmp(arg, "-verbosegc") == 0) {
1025 AddOption("-verbose:gc", NULL);
1026 } else if (JLI_StrCmp(arg, "-t") == 0) {
1027 AddOption("-Xt", NULL);
1028 } else if (JLI_StrCmp(arg, "-tm") == 0) {
1029 AddOption("-Xtm", NULL);
1030 } else if (JLI_StrCmp(arg, "-debug") == 0) {
1031 AddOption("-Xdebug", NULL);
1032 } else if (JLI_StrCmp(arg, "-noclassgc") == 0) {
1033 AddOption("-Xnoclassgc", NULL);
1034 } else if (JLI_StrCmp(arg, "-Xfuture") == 0) {
1035 AddOption("-Xverify:all", NULL);
1036 } else if (JLI_StrCmp(arg, "-verify") == 0) {
1037 AddOption("-Xverify:all", NULL);
1038 } else if (JLI_StrCmp(arg, "-verifyremote") == 0) {
1039 AddOption("-Xverify:remote", NULL);
1040 } else if (JLI_StrCmp(arg, "-noverify") == 0) {
1041 AddOption("-Xverify:none", NULL);
1042 } else if (JLI_StrCCmp(arg, "-prof") == 0) {
1043 char *p = arg + 5;
1044 char *tmp = JLI_MemAlloc(JLI_StrLen(arg) + 50);
1045 if (*p) {
1046 sprintf(tmp, "-Xrunhprof:cpu=old,file=%s", p + 1);
1047 } else {
1048 sprintf(tmp, "-Xrunhprof:cpu=old,file=java.prof");
1049 }
1050 AddOption(tmp, NULL);
1051 } else if (JLI_StrCCmp(arg, "-ss") == 0 ||
1052 JLI_StrCCmp(arg, "-oss") == 0 ||
1053 JLI_StrCCmp(arg, "-ms") == 0 ||
1054 JLI_StrCCmp(arg, "-mx") == 0) {
1055 char *tmp = JLI_MemAlloc(JLI_StrLen(arg) + 6);
1056 sprintf(tmp, "-X%s", arg + 1); /* skip '-' */
1057 AddOption(tmp, NULL);
1058 } else if (JLI_StrCmp(arg, "-checksource") == 0 ||
1059 JLI_StrCmp(arg, "-cs") == 0 ||
1060 JLI_StrCmp(arg, "-noasyncgc") == 0) {
1061 /* No longer supported */
ksrini0e817162008-08-26 10:21:20 -07001062 JLI_ReportErrorMessage(ARG_WARN, arg);
duke6e45e102007-12-01 00:00:00 +00001063 } else if (JLI_StrCCmp(arg, "-version:") == 0 ||
1064 JLI_StrCmp(arg, "-no-jre-restrict-search") == 0 ||
1065 JLI_StrCmp(arg, "-jre-restrict-search") == 0 ||
1066 JLI_StrCCmp(arg, "-splash:") == 0) {
1067 ; /* Ignore machine independent options already handled */
1068 } else if (RemovableOption(arg) ) {
1069 ; /* Do not pass option to vm. */
1070 } else {
1071 AddOption(arg, NULL);
1072 }
1073 }
1074
1075 if (--argc >= 0) {
1076 if (jarflag) {
1077 *pjarfile = *argv++;
ksrinie3ec45d2010-07-09 11:04:34 -07001078 *pclassname = NULL;
duke6e45e102007-12-01 00:00:00 +00001079 } else {
ksrinie3ec45d2010-07-09 11:04:34 -07001080 *pjarfile = NULL;
duke6e45e102007-12-01 00:00:00 +00001081 *pclassname = *argv++;
1082 }
1083 *pargc = argc;
1084 *pargv = argv;
1085 }
ksrinie3ec45d2010-07-09 11:04:34 -07001086 if (*pjarfile == NULL && *pclassname == NULL) {
1087 *pret = 1;
1088 }
duke6e45e102007-12-01 00:00:00 +00001089 return JNI_TRUE;
1090}
1091
1092/*
1093 * Initializes the Java Virtual Machine. Also frees options array when
1094 * finished.
1095 */
1096static jboolean
1097InitializeJVM(JavaVM **pvm, JNIEnv **penv, InvocationFunctions *ifn)
1098{
1099 JavaVMInitArgs args;
1100 jint r;
1101
1102 memset(&args, 0, sizeof(args));
1103 args.version = JNI_VERSION_1_2;
1104 args.nOptions = numOptions;
1105 args.options = options;
1106 args.ignoreUnrecognized = JNI_FALSE;
1107
1108 if (JLI_IsTraceLauncher()) {
1109 int i = 0;
1110 printf("JavaVM args:\n ");
1111 printf("version 0x%08lx, ", (long)args.version);
1112 printf("ignoreUnrecognized is %s, ",
1113 args.ignoreUnrecognized ? "JNI_TRUE" : "JNI_FALSE");
1114 printf("nOptions is %ld\n", (long)args.nOptions);
1115 for (i = 0; i < numOptions; i++)
1116 printf(" option[%2d] = '%s'\n",
1117 i, args.options[i].optionString);
1118 }
1119
1120 r = ifn->CreateJavaVM(pvm, (void **)penv, &args);
1121 JLI_MemFree(options);
1122 return r == JNI_OK;
1123}
1124
1125
1126#define NULL_CHECK0(e) if ((e) == 0) { \
ksrini0e817162008-08-26 10:21:20 -07001127 JLI_ReportErrorMessage(JNI_ERROR); \
duke6e45e102007-12-01 00:00:00 +00001128 return 0; \
1129 }
1130
1131#define NULL_CHECK(e) if ((e) == 0) { \
ksrini0e817162008-08-26 10:21:20 -07001132 JLI_ReportErrorMessage(JNI_ERROR); \
duke6e45e102007-12-01 00:00:00 +00001133 return; \
1134 }
1135
1136static jstring platformEncoding = NULL;
1137static jstring getPlatformEncoding(JNIEnv *env) {
1138 if (platformEncoding == NULL) {
1139 jstring propname = (*env)->NewStringUTF(env, "sun.jnu.encoding");
1140 if (propname) {
1141 jclass cls;
1142 jmethodID mid;
ksrini20a64b22008-09-24 15:07:41 -07001143 NULL_CHECK0 (cls = FindBootStrapClass(env, "java/lang/System"));
duke6e45e102007-12-01 00:00:00 +00001144 NULL_CHECK0 (mid = (*env)->GetStaticMethodID(
1145 env, cls,
1146 "getProperty",
1147 "(Ljava/lang/String;)Ljava/lang/String;"));
1148 platformEncoding = (*env)->CallStaticObjectMethod (
1149 env, cls, mid, propname);
1150 }
1151 }
1152 return platformEncoding;
1153}
1154
1155static jboolean isEncodingSupported(JNIEnv *env, jstring enc) {
1156 jclass cls;
1157 jmethodID mid;
ksrini20a64b22008-09-24 15:07:41 -07001158 NULL_CHECK0 (cls = FindBootStrapClass(env, "java/nio/charset/Charset"));
duke6e45e102007-12-01 00:00:00 +00001159 NULL_CHECK0 (mid = (*env)->GetStaticMethodID(
1160 env, cls,
1161 "isSupported",
1162 "(Ljava/lang/String;)Z"));
1163 return (*env)->CallStaticBooleanMethod(env, cls, mid, enc);
1164}
1165
1166/*
1167 * Returns a new Java string object for the specified platform string.
1168 */
1169static jstring
1170NewPlatformString(JNIEnv *env, char *s)
1171{
1172 int len = (int)JLI_StrLen(s);
1173 jclass cls;
1174 jmethodID mid;
1175 jbyteArray ary;
1176 jstring enc;
1177
1178 if (s == NULL)
1179 return 0;
1180 enc = getPlatformEncoding(env);
1181
1182 ary = (*env)->NewByteArray(env, len);
1183 if (ary != 0) {
1184 jstring str = 0;
1185 (*env)->SetByteArrayRegion(env, ary, 0, len, (jbyte *)s);
1186 if (!(*env)->ExceptionOccurred(env)) {
ksrini20a64b22008-09-24 15:07:41 -07001187 NULL_CHECK0(cls = FindBootStrapClass(env, "java/lang/String"));
duke6e45e102007-12-01 00:00:00 +00001188 if (isEncodingSupported(env, enc) == JNI_TRUE) {
duke6e45e102007-12-01 00:00:00 +00001189 NULL_CHECK0(mid = (*env)->GetMethodID(env, cls, "<init>",
1190 "([BLjava/lang/String;)V"));
1191 str = (*env)->NewObject(env, cls, mid, ary, enc);
1192 } else {
1193 /*If the encoding specified in sun.jnu.encoding is not
1194 endorsed by "Charset.isSupported" we have to fall back
1195 to use String(byte[]) explicitly here without specifying
1196 the encoding name, in which the StringCoding class will
1197 pickup the iso-8859-1 as the fallback converter for us.
1198 */
duke6e45e102007-12-01 00:00:00 +00001199 NULL_CHECK0(mid = (*env)->GetMethodID(env, cls, "<init>",
1200 "([B)V"));
1201 str = (*env)->NewObject(env, cls, mid, ary);
1202 }
1203 (*env)->DeleteLocalRef(env, ary);
1204 return str;
1205 }
1206 }
1207 return 0;
1208}
1209
1210/*
1211 * Returns a new array of Java string objects for the specified
1212 * array of platform strings.
1213 */
1214static jobjectArray
1215NewPlatformStringArray(JNIEnv *env, char **strv, int strc)
1216{
1217 jarray cls;
1218 jarray ary;
1219 int i;
1220
ksrini20a64b22008-09-24 15:07:41 -07001221 NULL_CHECK0(cls = FindBootStrapClass(env, "java/lang/String"));
duke6e45e102007-12-01 00:00:00 +00001222 NULL_CHECK0(ary = (*env)->NewObjectArray(env, strc, cls, 0));
1223 for (i = 0; i < strc; i++) {
1224 jstring str = NewPlatformString(env, *strv++);
1225 NULL_CHECK0(str);
1226 (*env)->SetObjectArrayElement(env, ary, i, str);
1227 (*env)->DeleteLocalRef(env, str);
1228 }
1229 return ary;
1230}
1231
1232/*
ksrini20a64b22008-09-24 15:07:41 -07001233 * Loads a class and verifies that the main class is present and it is ok to
1234 * call it for more details refer to the java implementation.
duke6e45e102007-12-01 00:00:00 +00001235 */
1236static jclass
ksrini20a64b22008-09-24 15:07:41 -07001237LoadMainClass(JNIEnv *env, jboolean isJar, char *name)
duke6e45e102007-12-01 00:00:00 +00001238{
duke6e45e102007-12-01 00:00:00 +00001239 jclass cls;
ksrini20a64b22008-09-24 15:07:41 -07001240 jmethodID mid;
1241 jstring str;
1242 jobject result;
duke6e45e102007-12-01 00:00:00 +00001243 jlong start, end;
1244
ksrini20a64b22008-09-24 15:07:41 -07001245 if (JLI_IsTraceLauncher()) {
duke6e45e102007-12-01 00:00:00 +00001246 start = CounterGet();
ksrini20a64b22008-09-24 15:07:41 -07001247 }
1248 NULL_CHECK0(cls = FindBootStrapClass(env, "sun/launcher/LauncherHelper"));
1249 NULL_CHECK0(mid = (*env)->GetStaticMethodID(env, cls, "checkAndLoadMain",
1250 "(ZZLjava/lang/String;)Ljava/lang/Object;"));
1251 str = (*env)->NewStringUTF(env, name);
1252 result = (*env)->CallStaticObjectMethod(env, cls, mid, JNI_TRUE, isJar, str);
duke6e45e102007-12-01 00:00:00 +00001253
1254 if (JLI_IsTraceLauncher()) {
1255 end = CounterGet();
1256 printf("%ld micro seconds to load main class\n",
1257 (long)(jint)Counter2Micros(end-start));
1258 printf("----_JAVA_LAUNCHER_DEBUG----\n");
1259 }
1260
ksrini20a64b22008-09-24 15:07:41 -07001261 return (jclass)result;
duke6e45e102007-12-01 00:00:00 +00001262}
1263
duke6e45e102007-12-01 00:00:00 +00001264/*
1265 * For tools, convert command line args thus:
1266 * javac -cp foo:foo/"*" -J-ms32m ...
1267 * java -ms32m -cp JLI_WildcardExpandClasspath(foo:foo/"*") ...
1268 *
1269 * Takes 4 parameters, and returns the populated arguments
1270 */
1271static void
1272TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv)
1273{
1274 int argc = *pargc;
1275 char **argv = *pargv;
1276 int nargc = argc + jargc;
1277 char **nargv = JLI_MemAlloc((nargc + 1) * sizeof(char *));
1278 int i;
1279
1280 *pargc = nargc;
1281 *pargv = nargv;
1282
1283 /* Copy the VM arguments (i.e. prefixed with -J) */
1284 for (i = 0; i < jargc; i++) {
1285 const char *arg = jargv[i];
1286 if (arg[0] == '-' && arg[1] == 'J') {
1287 *nargv++ = ((arg + 2) == NULL) ? NULL : JLI_StringDup(arg + 2);
1288 }
1289 }
1290
1291 for (i = 0; i < argc; i++) {
1292 char *arg = argv[i];
1293 if (arg[0] == '-' && arg[1] == 'J') {
1294 if (arg[2] == '\0') {
ksrini0e817162008-08-26 10:21:20 -07001295 JLI_ReportErrorMessage(ARG_ERROR3);
duke6e45e102007-12-01 00:00:00 +00001296 exit(1);
1297 }
1298 *nargv++ = arg + 2;
1299 }
1300 }
1301
1302 /* Copy the rest of the arguments */
1303 for (i = 0; i < jargc ; i++) {
1304 const char *arg = jargv[i];
1305 if (arg[0] != '-' || arg[1] != 'J') {
1306 *nargv++ = (arg == NULL) ? NULL : JLI_StringDup(arg);
1307 }
1308 }
1309 for (i = 0; i < argc; i++) {
1310 char *arg = argv[i];
1311 if (arg[0] == '-') {
1312 if (arg[1] == 'J')
1313 continue;
1314 if (IsWildCardEnabled() && arg[1] == 'c'
1315 && (JLI_StrCmp(arg, "-cp") == 0 ||
1316 JLI_StrCmp(arg, "-classpath") == 0)
1317 && i < argc - 1) {
1318 *nargv++ = arg;
1319 *nargv++ = (char *) JLI_WildcardExpandClasspath(argv[i+1]);
1320 i++;
1321 continue;
1322 }
1323 }
1324 *nargv++ = arg;
1325 }
1326 *nargv = 0;
1327}
1328
1329/*
1330 * For our tools, we try to add 3 VM options:
1331 * -Denv.class.path=<envcp>
1332 * -Dapplication.home=<apphome>
1333 * -Djava.class.path=<appcp>
1334 * <envcp> is the user's setting of CLASSPATH -- for instance the user
1335 * tells javac where to find binary classes through this environment
1336 * variable. Notice that users will be able to compile against our
1337 * tools classes (sun.tools.javac.Main) only if they explicitly add
1338 * tools.jar to CLASSPATH.
1339 * <apphome> is the directory where the application is installed.
1340 * <appcp> is the classpath to where our apps' classfiles are.
1341 */
1342static jboolean
1343AddApplicationOptions(int cpathc, const char **cpathv)
1344{
1345 char *envcp, *appcp, *apphome;
1346 char home[MAXPATHLEN]; /* application home */
1347 char separator[] = { PATH_SEPARATOR, '\0' };
1348 int size, i;
1349
1350 {
1351 const char *s = getenv("CLASSPATH");
1352 if (s) {
1353 s = (char *) JLI_WildcardExpandClasspath(s);
1354 /* 40 for -Denv.class.path= */
1355 envcp = (char *)JLI_MemAlloc(JLI_StrLen(s) + 40);
1356 sprintf(envcp, "-Denv.class.path=%s", s);
1357 AddOption(envcp, NULL);
1358 }
1359 }
1360
1361 if (!GetApplicationHome(home, sizeof(home))) {
ksrini0e817162008-08-26 10:21:20 -07001362 JLI_ReportErrorMessage(CFG_ERROR5);
duke6e45e102007-12-01 00:00:00 +00001363 return JNI_FALSE;
1364 }
1365
1366 /* 40 for '-Dapplication.home=' */
1367 apphome = (char *)JLI_MemAlloc(JLI_StrLen(home) + 40);
1368 sprintf(apphome, "-Dapplication.home=%s", home);
1369 AddOption(apphome, NULL);
1370
1371 /* How big is the application's classpath? */
1372 size = 40; /* 40: "-Djava.class.path=" */
1373 for (i = 0; i < cpathc; i++) {
1374 size += (int)JLI_StrLen(home) + (int)JLI_StrLen(cpathv[i]) + 1; /* 1: separator */
1375 }
1376 appcp = (char *)JLI_MemAlloc(size + 1);
1377 JLI_StrCpy(appcp, "-Djava.class.path=");
1378 for (i = 0; i < cpathc; i++) {
1379 JLI_StrCat(appcp, home); /* c:\program files\myapp */
1380 JLI_StrCat(appcp, cpathv[i]); /* \lib\myapp.jar */
1381 JLI_StrCat(appcp, separator); /* ; */
1382 }
1383 appcp[JLI_StrLen(appcp)-1] = '\0'; /* remove trailing path separator */
1384 AddOption(appcp, NULL);
1385 return JNI_TRUE;
1386}
1387
1388/*
1389 * inject the -Dsun.java.command pseudo property into the args structure
1390 * this pseudo property is used in the HotSpot VM to expose the
1391 * Java class name and arguments to the main method to the VM. The
1392 * HotSpot VM uses this pseudo property to store the Java class name
1393 * (or jar file name) and the arguments to the class's main method
1394 * to the instrumentation memory region. The sun.java.command pseudo
1395 * property is not exported by HotSpot to the Java layer.
1396 */
1397void
1398SetJavaCommandLineProp(char *classname, char *jarfile,
1399 int argc, char **argv)
1400{
1401
1402 int i = 0;
1403 size_t len = 0;
1404 char* javaCommand = NULL;
1405 char* dashDstr = "-Dsun.java.command=";
1406
1407 if (classname == NULL && jarfile == NULL) {
1408 /* unexpected, one of these should be set. just return without
1409 * setting the property
1410 */
1411 return;
1412 }
1413
1414 /* if the class name is not set, then use the jarfile name */
1415 if (classname == NULL) {
1416 classname = jarfile;
1417 }
1418
1419 /* determine the amount of memory to allocate assuming
1420 * the individual components will be space separated
1421 */
1422 len = JLI_StrLen(classname);
1423 for (i = 0; i < argc; i++) {
1424 len += JLI_StrLen(argv[i]) + 1;
1425 }
1426
1427 /* allocate the memory */
1428 javaCommand = (char*) JLI_MemAlloc(len + JLI_StrLen(dashDstr) + 1);
1429
1430 /* build the -D string */
1431 *javaCommand = '\0';
1432 JLI_StrCat(javaCommand, dashDstr);
1433 JLI_StrCat(javaCommand, classname);
1434
1435 for (i = 0; i < argc; i++) {
1436 /* the components of the string are space separated. In
1437 * the case of embedded white space, the relationship of
1438 * the white space separated components to their true
1439 * positional arguments will be ambiguous. This issue may
1440 * be addressed in a future release.
1441 */
1442 JLI_StrCat(javaCommand, " ");
1443 JLI_StrCat(javaCommand, argv[i]);
1444 }
1445
1446 AddOption(javaCommand, NULL);
1447}
1448
1449/*
1450 * JVM would like to know if it's created by a standard Sun launcher, or by
1451 * user native application, the following property indicates the former.
1452 */
1453void SetJavaLauncherProp() {
1454 AddOption("-Dsun.java.launcher=SUN_STANDARD", NULL);
1455}
1456
1457/*
1458 * Prints the version information from the java.version and other properties.
1459 */
1460static void
1461PrintJavaVersion(JNIEnv *env, jboolean extraLF)
1462{
1463 jclass ver;
1464 jmethodID print;
1465
ksrini20a64b22008-09-24 15:07:41 -07001466 NULL_CHECK(ver = FindBootStrapClass(env, "sun/misc/Version"));
duke6e45e102007-12-01 00:00:00 +00001467 NULL_CHECK(print = (*env)->GetStaticMethodID(env,
1468 ver,
1469 (extraLF == JNI_TRUE) ? "println" : "print",
1470 "()V"
1471 )
1472 );
1473
1474 (*env)->CallStaticVoidMethod(env, ver, print);
1475}
1476
1477/*
ksrini20a64b22008-09-24 15:07:41 -07001478 * Prints default usage or the Xusage message, see sun.launcher.LauncherHelper.java
duke6e45e102007-12-01 00:00:00 +00001479 */
1480static void
1481PrintUsage(JNIEnv* env, jboolean doXUsage)
1482{
1483 jclass cls;
1484 jmethodID initHelp, vmSelect, vmSynonym, vmErgo, printHelp, printXUsageMessage;
1485 jstring jprogname, vm1, vm2;
1486 int i;
1487
ksrini20a64b22008-09-24 15:07:41 -07001488 NULL_CHECK(cls = FindBootStrapClass(env, "sun/launcher/LauncherHelper"));
duke6e45e102007-12-01 00:00:00 +00001489
1490
1491 if (doXUsage) {
1492 NULL_CHECK(printXUsageMessage = (*env)->GetStaticMethodID(env, cls,
1493 "printXUsageMessage", "(Z)V"));
1494 (*env)->CallStaticVoidMethod(env, cls, printXUsageMessage, JNI_TRUE);
1495 } else {
1496 NULL_CHECK(initHelp = (*env)->GetStaticMethodID(env, cls,
1497 "initHelpMessage", "(Ljava/lang/String;)V"));
1498
1499 NULL_CHECK(vmSelect = (*env)->GetStaticMethodID(env, cls, "appendVmSelectMessage",
1500 "(Ljava/lang/String;Ljava/lang/String;)V"));
1501
1502 NULL_CHECK(vmSynonym = (*env)->GetStaticMethodID(env, cls,
1503 "appendVmSynonymMessage",
1504 "(Ljava/lang/String;Ljava/lang/String;)V"));
1505 NULL_CHECK(vmErgo = (*env)->GetStaticMethodID(env, cls,
1506 "appendVmErgoMessage", "(ZLjava/lang/String;)V"));
1507
1508 NULL_CHECK(printHelp = (*env)->GetStaticMethodID(env, cls,
1509 "printHelpMessage", "(Z)V"));
1510
1511 jprogname = (*env)->NewStringUTF(env, _program_name);
1512
1513 /* Initialize the usage message with the usual preamble */
1514 (*env)->CallStaticVoidMethod(env, cls, initHelp, jprogname);
1515
1516
1517 /* Assemble the other variant part of the usage */
1518 if ((knownVMs[0].flag == VM_KNOWN) ||
1519 (knownVMs[0].flag == VM_IF_SERVER_CLASS)) {
1520 vm1 = (*env)->NewStringUTF(env, knownVMs[0].name);
1521 vm2 = (*env)->NewStringUTF(env, knownVMs[0].name+1);
1522 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1523 }
1524 for (i=1; i<knownVMsCount; i++) {
1525 if (knownVMs[i].flag == VM_KNOWN) {
1526 vm1 = (*env)->NewStringUTF(env, knownVMs[i].name);
1527 vm2 = (*env)->NewStringUTF(env, knownVMs[i].name+1);
1528 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1529 }
1530 }
1531 for (i=1; i<knownVMsCount; i++) {
1532 if (knownVMs[i].flag == VM_ALIASED_TO) {
1533 vm1 = (*env)->NewStringUTF(env, knownVMs[i].name);
1534 vm2 = (*env)->NewStringUTF(env, knownVMs[i].alias+1);
1535 (*env)->CallStaticVoidMethod(env, cls, vmSynonym, vm1, vm2);
1536 }
1537 }
1538
1539 /* The first known VM is the default */
1540 {
1541 jboolean isServerClassMachine = ServerClassMachine();
1542
1543 const char* defaultVM = knownVMs[0].name+1;
1544 if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) && isServerClassMachine) {
1545 defaultVM = knownVMs[0].server_class+1;
1546 }
1547
1548 vm1 = (*env)->NewStringUTF(env, defaultVM);
1549 (*env)->CallStaticVoidMethod(env, cls, vmErgo, isServerClassMachine, vm1);
1550 }
1551
1552 /* Complete the usage message and print to stderr*/
1553 (*env)->CallStaticVoidMethod(env, cls, printHelp, JNI_TRUE);
1554 }
1555 return;
1556}
1557
1558/*
1559 * Read the jvm.cfg file and fill the knownJVMs[] array.
1560 *
1561 * The functionality of the jvm.cfg file is subject to change without
1562 * notice and the mechanism will be removed in the future.
1563 *
1564 * The lexical structure of the jvm.cfg file is as follows:
1565 *
1566 * jvmcfg := { vmLine }
1567 * vmLine := knownLine
1568 * | aliasLine
1569 * | warnLine
1570 * | ignoreLine
1571 * | errorLine
1572 * | predicateLine
1573 * | commentLine
1574 * knownLine := flag "KNOWN" EOL
1575 * warnLine := flag "WARN" EOL
1576 * ignoreLine := flag "IGNORE" EOL
1577 * errorLine := flag "ERROR" EOL
1578 * aliasLine := flag "ALIASED_TO" flag EOL
1579 * predicateLine := flag "IF_SERVER_CLASS" flag EOL
1580 * commentLine := "#" text EOL
1581 * flag := "-" identifier
1582 *
1583 * The semantics are that when someone specifies a flag on the command line:
1584 * - if the flag appears on a knownLine, then the identifier is used as
1585 * the name of the directory holding the JVM library (the name of the JVM).
1586 * - if the flag appears as the first flag on an aliasLine, the identifier
1587 * of the second flag is used as the name of the JVM.
1588 * - if the flag appears on a warnLine, the identifier is used as the
1589 * name of the JVM, but a warning is generated.
1590 * - if the flag appears on an ignoreLine, the identifier is recognized as the
1591 * name of a JVM, but the identifier is ignored and the default vm used
1592 * - if the flag appears on an errorLine, an error is generated.
1593 * - if the flag appears as the first flag on a predicateLine, and
1594 * the machine on which you are running passes the predicate indicated,
1595 * then the identifier of the second flag is used as the name of the JVM,
1596 * otherwise the identifier of the first flag is used as the name of the JVM.
1597 * If no flag is given on the command line, the first vmLine of the jvm.cfg
1598 * file determines the name of the JVM.
1599 * PredicateLines are only interpreted on first vmLine of a jvm.cfg file,
1600 * since they only make sense if someone hasn't specified the name of the
1601 * JVM on the command line.
1602 *
1603 * The intent of the jvm.cfg file is to allow several JVM libraries to
1604 * be installed in different subdirectories of a single JRE installation,
1605 * for space-savings and convenience in testing.
1606 * The intent is explicitly not to provide a full aliasing or predicate
1607 * mechanism.
1608 */
1609jint
1610ReadKnownVMs(const char *jrepath, const char * arch, jboolean speculative)
1611{
1612 FILE *jvmCfg;
1613 char jvmCfgName[MAXPATHLEN+20];
1614 char line[MAXPATHLEN+20];
1615 int cnt = 0;
1616 int lineno = 0;
1617 jlong start, end;
1618 int vmType;
1619 char *tmpPtr;
1620 char *altVMName = NULL;
1621 char *serverClassVMName = NULL;
1622 static char *whiteSpace = " \t";
1623 if (JLI_IsTraceLauncher()) {
1624 start = CounterGet();
1625 }
ksrini01740ec2010-09-09 11:50:40 -07001626 JLI_Snprintf(jvmCfgName, sizeof(jvmCfgName), "%s%slib%s%s%sjvm.cfg",
1627 jrepath, FILESEP, FILESEP, arch, FILESEP);
duke6e45e102007-12-01 00:00:00 +00001628
1629 jvmCfg = fopen(jvmCfgName, "r");
1630 if (jvmCfg == NULL) {
1631 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -07001632 JLI_ReportErrorMessage(CFG_ERROR6, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001633 exit(1);
1634 } else {
1635 return -1;
1636 }
1637 }
1638 while (fgets(line, sizeof(line), jvmCfg) != NULL) {
1639 vmType = VM_UNKNOWN;
1640 lineno++;
1641 if (line[0] == '#')
1642 continue;
1643 if (line[0] != '-') {
ksrini0e817162008-08-26 10:21:20 -07001644 JLI_ReportErrorMessage(CFG_WARN2, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001645 }
1646 if (cnt >= knownVMsLimit) {
1647 GrowKnownVMs(cnt);
1648 }
1649 line[JLI_StrLen(line)-1] = '\0'; /* remove trailing newline */
1650 tmpPtr = line + JLI_StrCSpn(line, whiteSpace);
1651 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001652 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001653 } else {
1654 /* Null-terminate this string for JLI_StringDup below */
1655 *tmpPtr++ = 0;
1656 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1657 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001658 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001659 } else {
1660 if (!JLI_StrCCmp(tmpPtr, "KNOWN")) {
1661 vmType = VM_KNOWN;
1662 } else if (!JLI_StrCCmp(tmpPtr, "ALIASED_TO")) {
1663 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1664 if (*tmpPtr != 0) {
1665 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1666 }
1667 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001668 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001669 } else {
1670 /* Null terminate altVMName */
1671 altVMName = tmpPtr;
1672 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1673 *tmpPtr = 0;
1674 vmType = VM_ALIASED_TO;
1675 }
1676 } else if (!JLI_StrCCmp(tmpPtr, "WARN")) {
1677 vmType = VM_WARN;
1678 } else if (!JLI_StrCCmp(tmpPtr, "IGNORE")) {
1679 vmType = VM_IGNORE;
1680 } else if (!JLI_StrCCmp(tmpPtr, "ERROR")) {
1681 vmType = VM_ERROR;
1682 } else if (!JLI_StrCCmp(tmpPtr, "IF_SERVER_CLASS")) {
1683 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1684 if (*tmpPtr != 0) {
1685 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1686 }
1687 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001688 JLI_ReportErrorMessage(CFG_WARN4, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001689 } else {
1690 /* Null terminate server class VM name */
1691 serverClassVMName = tmpPtr;
1692 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1693 *tmpPtr = 0;
1694 vmType = VM_IF_SERVER_CLASS;
1695 }
1696 } else {
ksrini0e817162008-08-26 10:21:20 -07001697 JLI_ReportErrorMessage(CFG_WARN5, lineno, &jvmCfgName[0]);
duke6e45e102007-12-01 00:00:00 +00001698 vmType = VM_KNOWN;
1699 }
1700 }
1701 }
1702
1703 JLI_TraceLauncher("jvm.cfg[%d] = ->%s<-\n", cnt, line);
1704 if (vmType != VM_UNKNOWN) {
1705 knownVMs[cnt].name = JLI_StringDup(line);
1706 knownVMs[cnt].flag = vmType;
1707 switch (vmType) {
1708 default:
1709 break;
1710 case VM_ALIASED_TO:
1711 knownVMs[cnt].alias = JLI_StringDup(altVMName);
1712 JLI_TraceLauncher(" name: %s vmType: %s alias: %s\n",
1713 knownVMs[cnt].name, "VM_ALIASED_TO", knownVMs[cnt].alias);
1714 break;
1715 case VM_IF_SERVER_CLASS:
1716 knownVMs[cnt].server_class = JLI_StringDup(serverClassVMName);
1717 JLI_TraceLauncher(" name: %s vmType: %s server_class: %s\n",
1718 knownVMs[cnt].name, "VM_IF_SERVER_CLASS", knownVMs[cnt].server_class);
1719 break;
1720 }
1721 cnt++;
1722 }
1723 }
1724 fclose(jvmCfg);
1725 knownVMsCount = cnt;
1726
1727 if (JLI_IsTraceLauncher()) {
1728 end = CounterGet();
1729 printf("%ld micro seconds to parse jvm.cfg\n",
1730 (long)(jint)Counter2Micros(end-start));
1731 }
1732
1733 return cnt;
1734}
1735
1736
1737static void
1738GrowKnownVMs(int minimum)
1739{
1740 struct vmdesc* newKnownVMs;
1741 int newMax;
1742
1743 newMax = (knownVMsLimit == 0 ? INIT_MAX_KNOWN_VMS : (2 * knownVMsLimit));
1744 if (newMax <= minimum) {
1745 newMax = minimum;
1746 }
1747 newKnownVMs = (struct vmdesc*) JLI_MemAlloc(newMax * sizeof(struct vmdesc));
1748 if (knownVMs != NULL) {
1749 memcpy(newKnownVMs, knownVMs, knownVMsLimit * sizeof(struct vmdesc));
1750 }
1751 JLI_MemFree(knownVMs);
1752 knownVMs = newKnownVMs;
1753 knownVMsLimit = newMax;
1754}
1755
1756
1757/* Returns index of VM or -1 if not found */
1758static int
1759KnownVMIndex(const char* name)
1760{
1761 int i;
1762 if (JLI_StrCCmp(name, "-J") == 0) name += 2;
1763 for (i = 0; i < knownVMsCount; i++) {
1764 if (!JLI_StrCmp(name, knownVMs[i].name)) {
1765 return i;
1766 }
1767 }
1768 return -1;
1769}
1770
1771static void
1772FreeKnownVMs()
1773{
1774 int i;
1775 for (i = 0; i < knownVMsCount; i++) {
1776 JLI_MemFree(knownVMs[i].name);
1777 knownVMs[i].name = NULL;
1778 }
1779 JLI_MemFree(knownVMs);
1780}
1781
1782
1783/*
1784 * Displays the splash screen according to the jar file name
1785 * and image file names stored in environment variables
1786 */
1787static void
1788ShowSplashScreen()
1789{
1790 const char *jar_name = getenv(SPLASH_JAR_ENV_ENTRY);
1791 const char *file_name = getenv(SPLASH_FILE_ENV_ENTRY);
1792 int data_size;
1793 void *image_data;
1794 if (jar_name) {
1795 image_data = JLI_JarUnpackFile(jar_name, file_name, &data_size);
1796 if (image_data) {
1797 DoSplashInit();
1798 DoSplashLoadMemory(image_data, data_size);
1799 JLI_MemFree(image_data);
1800 }
1801 } else if (file_name) {
1802 DoSplashInit();
1803 DoSplashLoadFile(file_name);
1804 } else {
1805 return;
1806 }
1807 DoSplashSetFileJarName(file_name, jar_name);
1808
1809 /*
1810 * Done with all command line processing and potential re-execs so
1811 * clean up the environment.
1812 */
1813 (void)UnsetEnv(ENV_ENTRY);
1814 (void)UnsetEnv(SPLASH_FILE_ENV_ENTRY);
1815 (void)UnsetEnv(SPLASH_JAR_ENV_ENTRY);
1816
1817 JLI_MemFree(splash_jar_entry);
1818 JLI_MemFree(splash_file_entry);
1819
1820}
1821
1822const char*
1823GetDotVersion()
1824{
1825 return _dVersion;
1826}
1827
1828const char*
1829GetFullVersion()
1830{
1831 return _fVersion;
1832}
1833
1834const char*
1835GetProgramName()
1836{
1837 return _program_name;
1838}
1839
1840const char*
1841GetLauncherName()
1842{
1843 return _launcher_name;
1844}
1845
1846jint
1847GetErgoPolicy()
1848{
1849 return _ergo_policy;
1850}
1851
1852jboolean
1853IsJavaArgs()
1854{
1855 return _is_java_args;
1856}
1857
1858static jboolean
1859IsWildCardEnabled()
1860{
1861 return _wc_enabled;
1862}
1863
1864static int
1865ContinueInNewThread(InvocationFunctions* ifn, int argc,
1866 char **argv, char *jarfile, char *classname, int ret)
1867{
1868
1869 /*
1870 * If user doesn't specify stack size, check if VM has a preference.
1871 * Note that HotSpot no longer supports JNI_VERSION_1_1 but it will
1872 * return its default stack size through the init args structure.
1873 */
1874 if (threadStackSize == 0) {
1875 struct JDK1_1InitArgs args1_1;
1876 memset((void*)&args1_1, 0, sizeof(args1_1));
1877 args1_1.version = JNI_VERSION_1_1;
1878 ifn->GetDefaultJavaVMInitArgs(&args1_1); /* ignore return value */
1879 if (args1_1.javaStackSize > 0) {
1880 threadStackSize = args1_1.javaStackSize;
1881 }
1882 }
1883
1884 { /* Create a new thread to create JVM and invoke main method */
1885 JavaMainArgs args;
1886 int rslt;
1887
1888 args.argc = argc;
1889 args.argv = argv;
1890 args.jarfile = jarfile;
1891 args.classname = classname;
1892 args.ifn = *ifn;
1893
1894 rslt = ContinueInNewThread0(JavaMain, threadStackSize, (void*)&args);
1895 /* If the caller has deemed there is an error we
1896 * simply return that, otherwise we return the value of
1897 * the callee
1898 */
1899 return (ret != 0) ? ret : rslt;
1900 }
1901}
1902
1903static void
1904DumpState()
1905{
1906 if (!JLI_IsTraceLauncher()) return ;
1907 printf("Launcher state:\n");
1908 printf("\tdebug:%s\n", (JLI_IsTraceLauncher() == JNI_TRUE) ? "on" : "off");
1909 printf("\tjavargs:%s\n", (_is_java_args == JNI_TRUE) ? "on" : "off");
1910 printf("\tprogram name:%s\n", GetProgramName());
1911 printf("\tlauncher name:%s\n", GetLauncherName());
1912 printf("\tjavaw:%s\n", (IsJavaw() == JNI_TRUE) ? "on" : "off");
1913 printf("\tfullversion:%s\n", GetFullVersion());
1914 printf("\tdotversion:%s\n", GetDotVersion());
1915 printf("\tergo_policy:");
1916 switch(GetErgoPolicy()) {
1917 case NEVER_SERVER_CLASS:
1918 printf("NEVER_ACT_AS_A_SERVER_CLASS_MACHINE\n");
1919 break;
1920 case ALWAYS_SERVER_CLASS:
1921 printf("ALWAYS_ACT_AS_A_SERVER_CLASS_MACHINE\n");
1922 break;
1923 default:
1924 printf("DEFAULT_ERGONOMICS_POLICY\n");
1925 }
1926}
1927
1928/*
1929 * Return JNI_TRUE for an option string that has no effect but should
1930 * _not_ be passed on to the vm; return JNI_FALSE otherwise. On
1931 * Solaris SPARC, this screening needs to be done if:
ksrini11e7f1b2009-11-20 11:01:32 -08001932 * -d32 or -d64 is passed to a binary with an unmatched data model
1933 * (the exec in CreateExecutionEnvironment removes -d<n> options and points the
1934 * exec to the proper binary). In the case of when the data model and the
1935 * requested version is matched, an exec would not occur, and these options
1936 * were erroneously passed to the vm.
duke6e45e102007-12-01 00:00:00 +00001937 */
1938jboolean
1939RemovableOption(char * option)
1940{
1941 /*
1942 * Unconditionally remove both -d32 and -d64 options since only
1943 * the last such options has an effect; e.g.
1944 * java -d32 -d64 -d32 -version
1945 * is equivalent to
1946 * java -d32 -version
1947 */
1948
1949 if( (JLI_StrCCmp(option, "-d32") == 0 ) ||
1950 (JLI_StrCCmp(option, "-d64") == 0 ) )
1951 return JNI_TRUE;
1952 else
1953 return JNI_FALSE;
1954}
1955
1956/*
1957 * A utility procedure to always print to stderr
1958 */
1959void
ksrini0e817162008-08-26 10:21:20 -07001960JLI_ReportMessage(const char* fmt, ...)
duke6e45e102007-12-01 00:00:00 +00001961{
1962 va_list vl;
1963 va_start(vl, fmt);
1964 vfprintf(stderr, fmt, vl);
1965 fprintf(stderr, "\n");
1966 va_end(vl);
1967}