1 /*
   2  * Copyright (c) 1995, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * 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
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  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  *
  21  * 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.
  24  */
  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
  44  * 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.
  51  */
  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 
  64 /* we always print to stderr */
  65 #define USE_STDERR JNI_TRUE
  66 
  67 static jboolean printVersion = JNI_FALSE; /* print and exit */
  68 static jboolean showVersion = JNI_FALSE;  /* print but continue */
  69 static jboolean printUsage = JNI_FALSE;   /* print and exit*/
  70 static jboolean printXUsage = JNI_FALSE;  /* print and exit*/
  71 static char     *showSettings = NULL;      /* print but continue */
  72 
  73 static const char *_program_name;
  74 static const char *_launcher_name;
  75 static jboolean _is_java_args = JNI_FALSE;
  76 static const char *_fVersion;
  77 static const char *_dVersion;
  78 static jboolean _wc_enabled = JNI_FALSE;
  79 static jint _ergo_policy = DEFAULT_POLICY;
  80 
  81 /*
  82  * Entries for splash screen environment variables.
  83  * putenv is performed in SelectVersion. We need
  84  * them in memory until UnsetEnv, so they are made static
  85  * global instead of auto local.
  86  */
  87 static char* splash_file_entry = NULL;
  88 static char* splash_jar_entry = NULL;
  89 
  90 /*
  91  * List of VM options to be specified when the VM is created.
  92  */
  93 static JavaVMOption *options;
  94 static int numOptions, maxOptions;
  95 
  96 /*
  97  * Prototypes for functions internal to launcher.
  98  */
  99 static void SetClassPath(const char *s);
 100 static void SelectVersion(int argc, char **argv, char **main_class);
 101 static void SetJvmEnvironment(int argc, char **argv);
 102 static jboolean ParseArguments(int *pargc, char ***pargv,
 103                                int *pmode, char **pwhat,
 104                                int *pret, const char *jrepath);
 105 static jboolean InitializeJVM(JavaVM **pvm, JNIEnv **penv,
 106                               InvocationFunctions *ifn);
 107 static jstring NewPlatformString(JNIEnv *env, char *s);
 108 static jclass LoadMainClass(JNIEnv *env, int mode, char *name);
 109 static jclass GetApplicationClass(JNIEnv *env);
 110 
 111 static void TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv);
 112 static jboolean AddApplicationOptions(int cpathc, const char **cpathv);
 113 static void SetApplicationClassPath(const char**);
 114 
 115 static void PrintJavaVersion(JNIEnv *env, jboolean extraLF);
 116 static void PrintUsage(JNIEnv* env, jboolean doXUsage);
 117 static void ShowSettings(JNIEnv* env, char *optString);
 118 
 119 static void SetPaths(int argc, char **argv);
 120 
 121 static void DumpState();
 122 static jboolean RemovableOption(char *option);
 123 
 124 /* Maximum supported entries from jvm.cfg. */
 125 #define INIT_MAX_KNOWN_VMS      10
 126 
 127 /* Values for vmdesc.flag */
 128 enum vmdesc_flag {
 129     VM_UNKNOWN = -1,
 130     VM_KNOWN,
 131     VM_ALIASED_TO,
 132     VM_WARN,
 133     VM_ERROR,
 134     VM_IF_SERVER_CLASS,
 135     VM_IGNORE
 136 };
 137 
 138 struct vmdesc {
 139     char *name;
 140     int flag;
 141     char *alias;
 142     char *server_class;
 143 };
 144 static struct vmdesc *knownVMs = NULL;
 145 static int knownVMsCount = 0;
 146 static int knownVMsLimit = 0;
 147 
 148 static void GrowKnownVMs();
 149 static int  KnownVMIndex(const char* name);
 150 static void FreeKnownVMs();
 151 static jboolean IsWildCardEnabled();
 152 
 153 #define ARG_CHECK(AC_arg_count, AC_failure_message, AC_questionable_arg) \
 154     do { \
 155         if (AC_arg_count < 1) { \
 156             JLI_ReportErrorMessage(AC_failure_message, AC_questionable_arg); \
 157             printUsage = JNI_TRUE; \
 158             *pret = 1; \
 159             return JNI_TRUE; \
 160         } \
 161     } while (JNI_FALSE)
 162 
 163 /*
 164  * Running Java code in primordial thread caused many problems. We will
 165  * create a new thread to invoke JVM. See 6316197 for more information.
 166  */
 167 static jlong threadStackSize    = 0;  /* stack size of the new thread */
 168 static jlong maxHeapSize        = 0;  /* max heap size */
 169 static jlong initialHeapSize    = 0;  /* inital heap size */
 170 
 171 /*
 172  * A minimum -Xss stack size suitable for all platforms.
 173  */
 174 #ifndef STACK_SIZE_MINIMUM
 175 #define STACK_SIZE_MINIMUM (32 * KB)
 176 #endif
 177 
 178 /*
 179  * Entry point.
 180  */
 181 int
 182 JLI_Launch(int argc, char ** argv,              /* main argc, argc */
 183         int jargc, const char** jargv,          /* java args */
 184         int appclassc, const char** appclassv,  /* app classpath */
 185         const char* fullversion,                /* full version defined */
 186         const char* dotversion,                 /* dot version defined */
 187         const char* pname,                      /* program name */
 188         const char* lname,                      /* launcher name */
 189         jboolean javaargs,                      /* JAVA_ARGS */
 190         jboolean cpwildcard,                    /* classpath wildcard*/
 191         jboolean javaw,                         /* windows-only javaw */
 192         jint ergo                               /* ergonomics class policy */
 193 )
 194 {
 195     int mode = LM_UNKNOWN;
 196     char *what = NULL;
 197     char *cpath = 0;
 198     char *main_class = NULL;
 199     int ret;
 200     InvocationFunctions ifn;
 201     jlong start, end;
 202     char jvmpath[MAXPATHLEN];
 203     char jrepath[MAXPATHLEN];
 204     char jvmcfg[MAXPATHLEN];
 205 
 206     _fVersion = fullversion;
 207     _dVersion = dotversion;
 208     _launcher_name = lname;
 209     _program_name = pname;
 210     _is_java_args = javaargs;
 211     _wc_enabled = cpwildcard;
 212     _ergo_policy = ergo;
 213 
 214     InitLauncher(javaw);
 215     DumpState();
 216     if (JLI_IsTraceLauncher()) {
 217         int i;
 218         printf("Command line args:\n");
 219         for (i = 0; i < argc ; i++) {
 220             printf("argv[%d] = %s\n", i, argv[i]);
 221         }
 222         AddOption("-Dsun.java.launcher.diag=true", NULL);
 223     }
 224 
 225     /*
 226      * SelectVersion() has several responsibilities:
 227      *
 228      *  1) Disallow specification of another JRE.  With 1.9, another
 229      *     version of the JRE cannot be invoked.
 230      *  2) Allow for a JRE version to invoke JDK 1.9 or later.  Since
 231      *     all mJRE directives have been stripped from the request but
 232      *     the pre 1.9 JRE [ 1.6 thru 1.8 ], it is as if 1.9+ has been
 233      *     invoked from the command line.
 234      */
 235     SelectVersion(argc, argv, &main_class);
 236 
 237     CreateExecutionEnvironment(&argc, &argv,
 238                                jrepath, sizeof(jrepath),
 239                                jvmpath, sizeof(jvmpath),
 240                                jvmcfg,  sizeof(jvmcfg));
 241 
 242     if (!IsJavaArgs()) {
 243         SetJvmEnvironment(argc,argv);
 244     }
 245 
 246     ifn.CreateJavaVM = 0;
 247     ifn.GetDefaultJavaVMInitArgs = 0;
 248 
 249     if (JLI_IsTraceLauncher()) {
 250         start = CounterGet();
 251     }
 252 
 253     if (!LoadJavaVM(jvmpath, &ifn)) {
 254         return(6);
 255     }
 256 
 257     if (JLI_IsTraceLauncher()) {
 258         end   = CounterGet();
 259     }
 260 
 261     JLI_TraceLauncher("%ld micro seconds to LoadJavaVM\n",
 262              (long)(jint)Counter2Micros(end-start));
 263 
 264     ++argv;
 265     --argc;
 266 
 267     if (IsJavaArgs()) {
 268         /* Preprocess wrapper arguments */
 269         TranslateApplicationArgs(jargc, jargv, &argc, &argv);
 270         if (!AddApplicationOptions(appclassc, appclassv)) {
 271             return(1);
 272         }
 273     } else {
 274         /* Set default CLASSPATH */
 275         cpath = getenv("CLASSPATH");
 276         if (cpath == NULL) {
 277             cpath = ".";
 278         }
 279         SetClassPath(cpath);
 280     }
 281 
 282     /* Parse command line options; if the return value of
 283      * ParseArguments is false, the program should exit.
 284      */
 285     if (!ParseArguments(&argc, &argv, &mode, &what, &ret, jrepath))
 286     {
 287         return(ret);
 288     }
 289 
 290     /* Override class path if -jar flag was specified */
 291     if (mode == LM_JAR) {
 292         SetClassPath(what);     /* Override class path */
 293     }
 294 
 295     /* set the -Dsun.java.command pseudo property */
 296     SetJavaCommandLineProp(what, 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     return JVMInit(&ifn, threadStackSize, argc, argv, mode, what, ret);
 305 }
 306 /*
 307  * Always detach the main thread so that it appears to have ended when
 308  * the application's main method exits.  This will invoke the
 309  * uncaught exception handler machinery if main threw an
 310  * exception.  An uncaught exception handler cannot change the
 311  * launcher's return code except by calling System.exit.
 312  *
 313  * Wait for all non-daemon threads to end, then destroy the VM.
 314  * This will actually create a trivial new Java waiter thread
 315  * named "DestroyJavaVM", but this will be seen as a different
 316  * thread from the one that executed main, even though they are
 317  * the same C thread.  This allows mainThread.join() and
 318  * mainThread.isAlive() to work as expected.
 319  */
 320 #define LEAVE() \
 321     do { \
 322         if ((*vm)->DetachCurrentThread(vm) != JNI_OK) { \
 323             JLI_ReportErrorMessage(JVM_ERROR2); \
 324             ret = 1; \
 325         } \
 326         if (JNI_TRUE) { \
 327             (*vm)->DestroyJavaVM(vm); \
 328             return ret; \
 329         } \
 330     } while (JNI_FALSE)
 331 
 332 #define CHECK_EXCEPTION_NULL_LEAVE(CENL_exception) \
 333     do { \
 334         if ((*env)->ExceptionOccurred(env)) { \
 335             JLI_ReportExceptionDescription(env); \
 336             LEAVE(); \
 337         } \
 338         if ((CENL_exception) == NULL) { \
 339             JLI_ReportErrorMessage(JNI_ERROR); \
 340             LEAVE(); \
 341         } \
 342     } while (JNI_FALSE)
 343 
 344 #define CHECK_EXCEPTION_LEAVE(CEL_return_value) \
 345     do { \
 346         if ((*env)->ExceptionOccurred(env)) { \
 347             JLI_ReportExceptionDescription(env); \
 348             ret = (CEL_return_value); \
 349             LEAVE(); \
 350         } \
 351     } while (JNI_FALSE)
 352 
 353 
 354 int JNICALL
 355 JavaMain(void * _args)
 356 {
 357     JavaMainArgs *args = (JavaMainArgs *)_args;
 358     int argc = args->argc;
 359     char **argv = args->argv;
 360     int mode = args->mode;
 361     char *what = args->what;
 362     InvocationFunctions ifn = args->ifn;
 363 
 364     JavaVM *vm = 0;
 365     JNIEnv *env = 0;
 366     jclass mainClass = NULL;
 367     jclass appClass = NULL; // actual application class being launched
 368     jmethodID mainID;
 369     jobjectArray mainArgs;
 370     int ret = 0;
 371     jlong start, end;
 372 
 373     RegisterThread();
 374 
 375     /* Initialize the virtual machine */
 376     start = CounterGet();
 377     if (!InitializeJVM(&vm, &env, &ifn)) {
 378         JLI_ReportErrorMessage(JVM_ERROR1);
 379         exit(1);
 380     }
 381 
 382     if (showSettings != NULL) {
 383         ShowSettings(env, showSettings);
 384         CHECK_EXCEPTION_LEAVE(1);
 385     }
 386 
 387     if (printVersion || showVersion) {
 388         PrintJavaVersion(env, showVersion);
 389         CHECK_EXCEPTION_LEAVE(0);
 390         if (printVersion) {
 391             LEAVE();
 392         }
 393     }
 394 
 395     /* If the user specified neither a class name nor a JAR file */
 396     if (printXUsage || printUsage || what == 0 || mode == LM_UNKNOWN) {
 397         PrintUsage(env, printXUsage);
 398         CHECK_EXCEPTION_LEAVE(1);
 399         LEAVE();
 400     }
 401 
 402     FreeKnownVMs();  /* after last possible PrintUsage() */
 403 
 404     if (JLI_IsTraceLauncher()) {
 405         end = CounterGet();
 406         JLI_TraceLauncher("%ld micro seconds to InitializeJVM\n",
 407                (long)(jint)Counter2Micros(end-start));
 408     }
 409 
 410     /* At this stage, argc/argv have the application's arguments */
 411     if (JLI_IsTraceLauncher()){
 412         int i;
 413         printf("%s is '%s'\n", launchModeNames[mode], what);
 414         printf("App's argc is %d\n", argc);
 415         for (i=0; i < argc; i++) {
 416             printf("    argv[%2d] = '%s'\n", i, argv[i]);
 417         }
 418     }
 419 
 420     ret = 1;
 421 
 422     /*
 423      * Get the application's main class.
 424      *
 425      * See bugid 5030265.  The Main-Class name has already been parsed
 426      * from the manifest, but not parsed properly for UTF-8 support.
 427      * Hence the code here ignores the value previously extracted and
 428      * uses the pre-existing code to reextract the value.  This is
 429      * possibly an end of release cycle expedient.  However, it has
 430      * also been discovered that passing some character sets through
 431      * the environment has "strange" behavior on some variants of
 432      * Windows.  Hence, maybe the manifest parsing code local to the
 433      * launcher should never be enhanced.
 434      *
 435      * Hence, future work should either:
 436      *     1)   Correct the local parsing code and verify that the
 437      *          Main-Class attribute gets properly passed through
 438      *          all environments,
 439      *     2)   Remove the vestages of maintaining main_class through
 440      *          the environment (and remove these comments).
 441      *
 442      * This method also correctly handles launching existing JavaFX
 443      * applications that may or may not have a Main-Class manifest entry.
 444      */
 445     mainClass = LoadMainClass(env, mode, what);
 446     CHECK_EXCEPTION_NULL_LEAVE(mainClass);
 447     /*
 448      * In some cases when launching an application that needs a helper, e.g., a
 449      * JavaFX application with no main method, the mainClass will not be the
 450      * applications own main class but rather a helper class. To keep things
 451      * consistent in the UI we need to track and report the application main class.
 452      */
 453     appClass = GetApplicationClass(env);
 454     NULL_CHECK_RETURN_VALUE(appClass, -1);
 455     /*
 456      * PostJVMInit uses the class name as the application name for GUI purposes,
 457      * for example, on OSX this sets the application name in the menu bar for
 458      * both SWT and JavaFX. So we'll pass the actual application class here
 459      * instead of mainClass as that may be a launcher or helper class instead
 460      * of the application class.
 461      */
 462     PostJVMInit(env, appClass, vm);
 463     CHECK_EXCEPTION_LEAVE(1);
 464     /*
 465      * The LoadMainClass not only loads the main class, it will also ensure
 466      * that the main method's signature is correct, therefore further checking
 467      * is not required. The main method is invoked here so that extraneous java
 468      * stacks are not in the application stack trace.
 469      */
 470     mainID = (*env)->GetStaticMethodID(env, mainClass, "main",
 471                                        "([Ljava/lang/String;)V");
 472     CHECK_EXCEPTION_NULL_LEAVE(mainID);
 473 
 474     /* Build platform specific argument array */
 475     mainArgs = CreateApplicationArgs(env, argv, argc);
 476     CHECK_EXCEPTION_NULL_LEAVE(mainArgs);
 477 
 478     /* Invoke main method. */
 479     (*env)->CallStaticVoidMethod(env, mainClass, mainID, mainArgs);
 480 
 481     /*
 482      * The launcher's exit code (in the absence of calls to
 483      * System.exit) will be non-zero if main threw an exception.
 484      */
 485     ret = (*env)->ExceptionOccurred(env) == NULL ? 0 : 1;
 486     LEAVE();
 487 }
 488 
 489 /*
 490  * Checks the command line options to find which JVM type was
 491  * specified.  If no command line option was given for the JVM type,
 492  * the default type is used.  The environment variable
 493  * JDK_ALTERNATE_VM and the command line option -XXaltjvm= are also
 494  * checked as ways of specifying which JVM type to invoke.
 495  */
 496 char *
 497 CheckJvmType(int *pargc, char ***argv, jboolean speculative) {
 498     int i, argi;
 499     int argc;
 500     char **newArgv;
 501     int newArgvIdx = 0;
 502     int isVMType;
 503     int jvmidx = -1;
 504     char *jvmtype = getenv("JDK_ALTERNATE_VM");
 505 
 506     argc = *pargc;
 507 
 508     /* To make things simpler we always copy the argv array */
 509     newArgv = JLI_MemAlloc((argc + 1) * sizeof(char *));
 510 
 511     /* The program name is always present */
 512     newArgv[newArgvIdx++] = (*argv)[0];
 513 
 514     for (argi = 1; argi < argc; argi++) {
 515         char *arg = (*argv)[argi];
 516         isVMType = 0;
 517 
 518         if (IsJavaArgs()) {
 519             if (arg[0] != '-') {
 520                 newArgv[newArgvIdx++] = arg;
 521                 continue;
 522             }
 523         } else {
 524             if (JLI_StrCmp(arg, "-classpath") == 0 ||
 525                 JLI_StrCmp(arg, "-cp") == 0) {
 526                 newArgv[newArgvIdx++] = arg;
 527                 argi++;
 528                 if (argi < argc) {
 529                     newArgv[newArgvIdx++] = (*argv)[argi];
 530                 }
 531                 continue;
 532             }
 533             if (arg[0] != '-') break;
 534         }
 535 
 536         /* Did the user pass an explicit VM type? */
 537         i = KnownVMIndex(arg);
 538         if (i >= 0) {
 539             jvmtype = knownVMs[jvmidx = i].name + 1; /* skip the - */
 540             isVMType = 1;
 541             *pargc = *pargc - 1;
 542         }
 543 
 544         /* Did the user specify an "alternate" VM? */
 545         else if (JLI_StrCCmp(arg, "-XXaltjvm=") == 0 || JLI_StrCCmp(arg, "-J-XXaltjvm=") == 0) {
 546             isVMType = 1;
 547             jvmtype = arg+((arg[1]=='X')? 10 : 12);
 548             jvmidx = -1;
 549         }
 550 
 551         if (!isVMType) {
 552             newArgv[newArgvIdx++] = arg;
 553         }
 554     }
 555 
 556     /*
 557      * Finish copying the arguments if we aborted the above loop.
 558      * NOTE that if we aborted via "break" then we did NOT copy the
 559      * last argument above, and in addition argi will be less than
 560      * argc.
 561      */
 562     while (argi < argc) {
 563         newArgv[newArgvIdx++] = (*argv)[argi];
 564         argi++;
 565     }
 566 
 567     /* argv is null-terminated */
 568     newArgv[newArgvIdx] = 0;
 569 
 570     /* Copy back argv */
 571     *argv = newArgv;
 572     *pargc = newArgvIdx;
 573 
 574     /* use the default VM type if not specified (no alias processing) */
 575     if (jvmtype == NULL) {
 576       char* result = knownVMs[0].name+1;
 577       /* Use a different VM type if we are on a server class machine? */
 578       if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) &&
 579           (ServerClassMachine() == JNI_TRUE)) {
 580         result = knownVMs[0].server_class+1;
 581       }
 582       JLI_TraceLauncher("Default VM: %s\n", result);
 583       return result;
 584     }
 585 
 586     /* if using an alternate VM, no alias processing */
 587     if (jvmidx < 0)
 588       return jvmtype;
 589 
 590     /* Resolve aliases first */
 591     {
 592       int loopCount = 0;
 593       while (knownVMs[jvmidx].flag == VM_ALIASED_TO) {
 594         int nextIdx = KnownVMIndex(knownVMs[jvmidx].alias);
 595 
 596         if (loopCount > knownVMsCount) {
 597           if (!speculative) {
 598             JLI_ReportErrorMessage(CFG_ERROR1);
 599             exit(1);
 600           } else {
 601             return "ERROR";
 602             /* break; */
 603           }
 604         }
 605 
 606         if (nextIdx < 0) {
 607           if (!speculative) {
 608             JLI_ReportErrorMessage(CFG_ERROR2, knownVMs[jvmidx].alias);
 609             exit(1);
 610           } else {
 611             return "ERROR";
 612           }
 613         }
 614         jvmidx = nextIdx;
 615         jvmtype = knownVMs[jvmidx].name+1;
 616         loopCount++;
 617       }
 618     }
 619 
 620     switch (knownVMs[jvmidx].flag) {
 621     case VM_WARN:
 622         if (!speculative) {
 623             JLI_ReportErrorMessage(CFG_WARN1, jvmtype, knownVMs[0].name + 1);
 624         }
 625         /* fall through */
 626     case VM_IGNORE:
 627         jvmtype = knownVMs[jvmidx=0].name + 1;
 628         /* fall through */
 629     case VM_KNOWN:
 630         break;
 631     case VM_ERROR:
 632         if (!speculative) {
 633             JLI_ReportErrorMessage(CFG_ERROR3, jvmtype);
 634             exit(1);
 635         } else {
 636             return "ERROR";
 637         }
 638     }
 639 
 640     return jvmtype;
 641 }
 642 
 643 /*
 644  * static void SetJvmEnvironment(int argc, char **argv);
 645  *   Is called just before the JVM is loaded.  We can set env variables
 646  *   that are consumed by the JVM.  This function is non-destructive,
 647  *   leaving the arg list intact.  The first use is for the JVM flag
 648  *   -XX:NativeMemoryTracking=value.
 649  */
 650 static void
 651 SetJvmEnvironment(int argc, char **argv) {
 652 
 653     static const char*  NMT_Env_Name    = "NMT_LEVEL_";
 654 
 655     int i;
 656     for (i = 0; i < argc; i++) {
 657         /*
 658          * The following case checks for "-XX:NativeMemoryTracking=value".
 659          * If value is non null, an environmental variable set to this value
 660          * will be created to be used by the JVM.
 661          * The argument is passed to the JVM, which will check validity.
 662          * The JVM is responsible for removing the env variable.
 663          */
 664         char *arg = argv[i];
 665         if (JLI_StrCCmp(arg, "-XX:NativeMemoryTracking=") == 0) {
 666             int retval;
 667             // get what follows this parameter, include "="
 668             size_t pnlen = JLI_StrLen("-XX:NativeMemoryTracking=");
 669             if (JLI_StrLen(arg) > pnlen) {
 670                 char* value = arg + pnlen;
 671                 size_t pbuflen = pnlen + JLI_StrLen(value) + 10; // 10 max pid digits
 672 
 673                 /*
 674                  * ensures that malloc successful
 675                  * DONT JLI_MemFree() pbuf.  JLI_PutEnv() uses system call
 676                  *   that could store the address.
 677                  */
 678                 char * pbuf = (char*)JLI_MemAlloc(pbuflen);
 679 
 680                 JLI_Snprintf(pbuf, pbuflen, "%s%d=%s", NMT_Env_Name, JLI_GetPid(), value);
 681                 retval = JLI_PutEnv(pbuf);
 682                 if (JLI_IsTraceLauncher()) {
 683                     char* envName;
 684                     char* envBuf;
 685 
 686                     // ensures that malloc successful
 687                     envName = (char*)JLI_MemAlloc(pbuflen);
 688                     JLI_Snprintf(envName, pbuflen, "%s%d", NMT_Env_Name, JLI_GetPid());
 689 
 690                     printf("TRACER_MARKER: NativeMemoryTracking: env var is %s\n",envName);
 691                     printf("TRACER_MARKER: NativeMemoryTracking: putenv arg %s\n",pbuf);
 692                     envBuf = getenv(envName);
 693                     printf("TRACER_MARKER: NativeMemoryTracking: got value %s\n",envBuf);
 694                     free(envName);
 695                 }
 696 
 697             }
 698 
 699         }
 700 
 701     }
 702 }
 703 
 704 /* copied from HotSpot function "atomll()" */
 705 static int
 706 parse_size(const char *s, jlong *result) {
 707   jlong n = 0;
 708   int args_read = sscanf(s, jlong_format_specifier(), &n);
 709   if (args_read != 1) {
 710     return 0;
 711   }
 712   while (*s != '\0' && *s >= '0' && *s <= '9') {
 713     s++;
 714   }
 715   // 4705540: illegal if more characters are found after the first non-digit
 716   if (JLI_StrLen(s) > 1) {
 717     return 0;
 718   }
 719   switch (*s) {
 720     case 'T': case 't':
 721       *result = n * GB * KB;
 722       return 1;
 723     case 'G': case 'g':
 724       *result = n * GB;
 725       return 1;
 726     case 'M': case 'm':
 727       *result = n * MB;
 728       return 1;
 729     case 'K': case 'k':
 730       *result = n * KB;
 731       return 1;
 732     case '\0':
 733       *result = n;
 734       return 1;
 735     default:
 736       /* Create JVM with default stack and let VM handle malformed -Xss string*/
 737       return 0;
 738   }
 739 }
 740 
 741 /*
 742  * Adds a new VM option with the given name and value.
 743  */
 744 void
 745 AddOption(char *str, void *info)
 746 {
 747     /*
 748      * Expand options array if needed to accommodate at least one more
 749      * VM option.
 750      */
 751     if (numOptions >= maxOptions) {
 752         if (options == 0) {
 753             maxOptions = 4;
 754             options = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
 755         } else {
 756             JavaVMOption *tmp;
 757             maxOptions *= 2;
 758             tmp = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
 759             memcpy(tmp, options, numOptions * sizeof(JavaVMOption));
 760             JLI_MemFree(options);
 761             options = tmp;
 762         }
 763     }
 764     options[numOptions].optionString = str;
 765     options[numOptions++].extraInfo = info;
 766 
 767     if (JLI_StrCCmp(str, "-Xss") == 0) {
 768         jlong tmp;
 769         if (parse_size(str + 4, &tmp)) {
 770             threadStackSize = tmp;
 771             /*
 772              * Make sure the thread stack size is big enough that we won't get a stack
 773              * overflow before the JVM startup code can check to make sure the stack
 774              * is big enough.
 775              */
 776             if (threadStackSize < STACK_SIZE_MINIMUM) {
 777                 threadStackSize = STACK_SIZE_MINIMUM;
 778             }
 779         }
 780     }
 781 
 782     if (JLI_StrCCmp(str, "-Xmx") == 0) {
 783         jlong tmp;
 784         if (parse_size(str + 4, &tmp)) {
 785             maxHeapSize = tmp;
 786         }
 787     }
 788 
 789     if (JLI_StrCCmp(str, "-Xms") == 0) {
 790         jlong tmp;
 791         if (parse_size(str + 4, &tmp)) {
 792            initialHeapSize = tmp;
 793         }
 794     }
 795 }
 796 
 797 static void
 798 SetClassPath(const char *s)
 799 {
 800     char *def;
 801     const char *orig = s;
 802     static const char format[] = "-Djava.class.path=%s";
 803     /*
 804      * usually we should not get a null pointer, but there are cases where
 805      * we might just get one, in which case we simply ignore it, and let the
 806      * caller deal with it
 807      */
 808     if (s == NULL)
 809         return;
 810     s = JLI_WildcardExpandClasspath(s);
 811     if (sizeof(format) - 2 + JLI_StrLen(s) < JLI_StrLen(s))
 812         // s is became corrupted after expanding wildcards
 813         return;
 814     def = JLI_MemAlloc(sizeof(format)
 815                        - 2 /* strlen("%s") */
 816                        + JLI_StrLen(s));
 817     sprintf(def, format, s);
 818     AddOption(def, NULL);
 819     if (s != orig)
 820         JLI_MemFree((char *) s);
 821 }
 822 
 823 /*
 824  * The SelectVersion() routine ensures that an appropriate version of
 825  * the JRE is running.  The specification for the appropriate version
 826  * is obtained from either the manifest of a jar file (preferred) or
 827  * from command line options.
 828  * The routine also parses splash screen command line options and
 829  * passes on their values in private environment variables.
 830  */
 831 static void
 832 SelectVersion(int argc, char **argv, char **main_class)
 833 {
 834     char    *arg;
 835     char    *operand;
 836     char    *version = NULL;
 837     char    *jre = NULL;
 838     int     jarflag = 0;
 839     int     headlessflag = 0;
 840     int     restrict_search = -1;               /* -1 implies not known */
 841     manifest_info info;
 842     char    env_entry[MAXNAMELEN + 24] = ENV_ENTRY "=";
 843     char    *splash_file_name = NULL;
 844     char    *splash_jar_name = NULL;
 845     char    *env_in;
 846     int     res;
 847 
 848     /*
 849      * If the version has already been selected, set *main_class
 850      * with the value passed through the environment (if any) and
 851      * simply return.
 852      */
 853 
 854     /*
 855      * This environmental variable can be set by mJRE capable JREs
 856      * [ 1.5 thru 1.8 ].  All other aspects of mJRE processing have been
 857      * stripped by those JREs.  This environmental variable allows 1.9+
 858      * JREs to be started by these mJRE capable JREs.
 859      * Note that mJRE directives in the jar manifest file would have been
 860      * ignored for a JRE started by another JRE...
 861      * .. skipped for JRE 1.5 and beyond.
 862      * .. not even checked for pre 1.5.
 863      */
 864     if ((env_in = getenv(ENV_ENTRY)) != NULL) {
 865         if (*env_in != '\0')
 866             *main_class = JLI_StringDup(env_in);
 867         return;
 868     }
 869 
 870     /*
 871      * Scan through the arguments for options relevant to multiple JRE
 872      * support.  Multiple JRE support existed in JRE versions 1.5 thru 1.8.
 873      *
 874      * This capability is no longer available with JRE versions 1.9 and later.
 875      * These command line options are reported as errors.
 876      */
 877     argc--;
 878     argv++;
 879     while ((arg = *argv) != 0 && *arg == '-') {
 880         if (JLI_StrCCmp(arg, "-version:") == 0) {
 881             JLI_ReportErrorMessage(SPC_ERROR1);
 882         } else if (JLI_StrCmp(arg, "-jre-restrict-search") == 0) {
 883             JLI_ReportErrorMessage(SPC_ERROR2);
 884         } else if (JLI_StrCmp(arg, "-jre-no-restrict-search") == 0) {
 885             JLI_ReportErrorMessage(SPC_ERROR2);
 886         } else {
 887             if (JLI_StrCmp(arg, "-jar") == 0)
 888                 jarflag = 1;
 889             /* deal with "unfortunate" classpath syntax */
 890             if ((JLI_StrCmp(arg, "-classpath") == 0 || JLI_StrCmp(arg, "-cp") == 0) &&
 891               (argc >= 2)) {
 892                 argc--;
 893                 argv++;
 894                 arg = *argv;
 895             }
 896 
 897             /*
 898              * Checking for headless toolkit option in the some way as AWT does:
 899              * "true" means true and any other value means false
 900              */
 901             if (JLI_StrCmp(arg, "-Djava.awt.headless=true") == 0) {
 902                 headlessflag = 1;
 903             } else if (JLI_StrCCmp(arg, "-Djava.awt.headless=") == 0) {
 904                 headlessflag = 0;
 905             } else if (JLI_StrCCmp(arg, "-splash:") == 0) {
 906                 splash_file_name = arg+8;
 907             }
 908         }
 909         argc--;
 910         argv++;
 911     }
 912     if (argc <= 0) {    /* No operand? Possibly legit with -[full]version */
 913         operand = NULL;
 914     } else {
 915         argc--;
 916         operand = *argv++;
 917     }
 918 
 919     /*
 920      * If there is a jar file, read the manifest. If the jarfile can't be
 921      * read, the manifest can't be read from the jar file, or the manifest
 922      * is corrupt, issue the appropriate error messages and exit.
 923      *
 924      * Even if there isn't a jar file, construct a manifest_info structure
 925      * containing the command line information.  It's a convenient way to carry
 926      * this data around.
 927      */
 928     if (jarflag && operand) {
 929         if ((res = JLI_ParseManifest(operand, &info)) != 0) {
 930             if (res == -1)
 931                 JLI_ReportErrorMessage(JAR_ERROR2, operand);
 932             else
 933                 JLI_ReportErrorMessage(JAR_ERROR3, operand);
 934             exit(1);
 935         }
 936 
 937         /*
 938          * Command line splash screen option should have precedence
 939          * over the manifest, so the manifest data is used only if
 940          * splash_file_name has not been initialized above during command
 941          * line parsing
 942          */
 943         if (!headlessflag && !splash_file_name && info.splashscreen_image_file_name) {
 944             splash_file_name = info.splashscreen_image_file_name;
 945             splash_jar_name = operand;
 946         }
 947     } else {
 948         info.manifest_version = NULL;
 949         info.main_class = NULL;
 950         info.jre_version = NULL;
 951         info.jre_restrict_search = 0;
 952     }
 953 
 954     /*
 955      * Passing on splash screen info in environment variables
 956      */
 957     if (splash_file_name && !headlessflag) {
 958         char* splash_file_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_FILE_ENV_ENTRY "=")+JLI_StrLen(splash_file_name)+1);
 959         JLI_StrCpy(splash_file_entry, SPLASH_FILE_ENV_ENTRY "=");
 960         JLI_StrCat(splash_file_entry, splash_file_name);
 961         putenv(splash_file_entry);
 962     }
 963     if (splash_jar_name && !headlessflag) {
 964         char* splash_jar_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_JAR_ENV_ENTRY "=")+JLI_StrLen(splash_jar_name)+1);
 965         JLI_StrCpy(splash_jar_entry, SPLASH_JAR_ENV_ENTRY "=");
 966         JLI_StrCat(splash_jar_entry, splash_jar_name);
 967         putenv(splash_jar_entry);
 968     }
 969 
 970 
 971     /*
 972      * "Valid" returns (other than unrecoverable errors) follow.  Set
 973      * main_class as a side-effect of this routine.
 974      */
 975     if (info.main_class != NULL)
 976         *main_class = JLI_StringDup(info.main_class);
 977 
 978     if (info.jre_version == NULL) {
 979         JLI_FreeManifest();
 980         return;
 981     }
 982 
 983 }
 984 
 985 /*
 986  * Parses command line arguments.  Returns JNI_FALSE if launcher
 987  * should exit without starting vm, returns JNI_TRUE if vm needs
 988  * to be started to process given options.  *pret (the launcher
 989  * process return value) is set to 0 for a normal exit.
 990  */
 991 static jboolean
 992 ParseArguments(int *pargc, char ***pargv,
 993                int *pmode, char **pwhat,
 994                int *pret, const char *jrepath)
 995 {
 996     int argc = *pargc;
 997     char **argv = *pargv;
 998     int mode = LM_UNKNOWN;
 999     char *arg;
1000 
1001     *pret = 0;
1002 
1003     while ((arg = *argv) != 0 && *arg == '-') {
1004         argv++; --argc;
1005         if (JLI_StrCmp(arg, "-classpath") == 0 || JLI_StrCmp(arg, "-cp") == 0) {
1006             ARG_CHECK (argc, ARG_ERROR1, arg);
1007             SetClassPath(*argv);
1008             mode = LM_CLASS;
1009             argv++; --argc;
1010         } else if (JLI_StrCmp(arg, "-jar") == 0) {
1011             ARG_CHECK (argc, ARG_ERROR2, arg);
1012             mode = LM_JAR;
1013         } else if (JLI_StrCmp(arg, "-help") == 0 ||
1014                    JLI_StrCmp(arg, "-h") == 0 ||
1015                    JLI_StrCmp(arg, "-?") == 0) {
1016             printUsage = JNI_TRUE;
1017             return JNI_TRUE;
1018         } else if (JLI_StrCmp(arg, "-version") == 0) {
1019             printVersion = JNI_TRUE;
1020             return JNI_TRUE;
1021         } else if (JLI_StrCmp(arg, "-showversion") == 0) {
1022             showVersion = JNI_TRUE;
1023         } else if (JLI_StrCmp(arg, "-X") == 0) {
1024             printXUsage = JNI_TRUE;
1025             return JNI_TRUE;
1026 /*
1027  * The following case checks for -XshowSettings OR -XshowSetting:SUBOPT.
1028  * In the latter case, any SUBOPT value not recognized will default to "all"
1029  */
1030         } else if (JLI_StrCmp(arg, "-XshowSettings") == 0 ||
1031                 JLI_StrCCmp(arg, "-XshowSettings:") == 0) {
1032             showSettings = arg;
1033         } else if (JLI_StrCmp(arg, "-Xdiag") == 0) {
1034             AddOption("-Dsun.java.launcher.diag=true", NULL);
1035 /*
1036  * The following case provide backward compatibility with old-style
1037  * command line options.
1038  */
1039         } else if (JLI_StrCmp(arg, "-fullversion") == 0) {
1040             JLI_ReportMessage("%s full version \"%s\"", _launcher_name, GetFullVersion());
1041             return JNI_FALSE;
1042         } else if (JLI_StrCmp(arg, "-verbosegc") == 0) {
1043             AddOption("-verbose:gc", NULL);
1044         } else if (JLI_StrCmp(arg, "-t") == 0) {
1045             AddOption("-Xt", NULL);
1046         } else if (JLI_StrCmp(arg, "-tm") == 0) {
1047             AddOption("-Xtm", NULL);
1048         } else if (JLI_StrCmp(arg, "-debug") == 0) {
1049             AddOption("-Xdebug", NULL);
1050         } else if (JLI_StrCmp(arg, "-noclassgc") == 0) {
1051             AddOption("-Xnoclassgc", NULL);
1052         } else if (JLI_StrCmp(arg, "-Xfuture") == 0) {
1053             AddOption("-Xverify:all", NULL);
1054         } else if (JLI_StrCmp(arg, "-verify") == 0) {
1055             AddOption("-Xverify:all", NULL);
1056         } else if (JLI_StrCmp(arg, "-verifyremote") == 0) {
1057             AddOption("-Xverify:remote", NULL);
1058         } else if (JLI_StrCmp(arg, "-noverify") == 0) {
1059             AddOption("-Xverify:none", NULL);
1060         } else if (JLI_StrCCmp(arg, "-prof") == 0) {
1061             char *p = arg + 5;
1062             char *tmp = JLI_MemAlloc(JLI_StrLen(arg) + 50);
1063             if (*p) {
1064                 sprintf(tmp, "-Xrunhprof:cpu=old,file=%s", p + 1);
1065             } else {
1066                 sprintf(tmp, "-Xrunhprof:cpu=old,file=java.prof");
1067             }
1068             AddOption(tmp, NULL);
1069         } else if (JLI_StrCCmp(arg, "-ss") == 0 ||
1070                    JLI_StrCCmp(arg, "-oss") == 0 ||
1071                    JLI_StrCCmp(arg, "-ms") == 0 ||
1072                    JLI_StrCCmp(arg, "-mx") == 0) {
1073             char *tmp = JLI_MemAlloc(JLI_StrLen(arg) + 6);
1074             sprintf(tmp, "-X%s", arg + 1); /* skip '-' */
1075             AddOption(tmp, NULL);
1076         } else if (JLI_StrCmp(arg, "-checksource") == 0 ||
1077                    JLI_StrCmp(arg, "-cs") == 0 ||
1078                    JLI_StrCmp(arg, "-noasyncgc") == 0) {
1079             /* No longer supported */
1080             JLI_ReportErrorMessage(ARG_WARN, arg);
1081         } else if (JLI_StrCCmp(arg, "-splash:") == 0) {
1082             ; /* Ignore machine independent options already handled */
1083         } else if (ProcessPlatformOption(arg)) {
1084             ; /* Processing of platform dependent options */
1085         } else if (RemovableOption(arg)) {
1086             ; /* Do not pass option to vm. */
1087         } else {
1088             AddOption(arg, NULL);
1089         }
1090     }
1091 
1092     if (--argc >= 0) {
1093         *pwhat = *argv++;
1094     }
1095 
1096     if (*pwhat == NULL) {
1097         *pret = 1;
1098     } else if (mode == LM_UNKNOWN) {
1099         /* default to LM_CLASS if -jar and -cp option are
1100          * not specified */
1101         mode = LM_CLASS;
1102     }
1103 
1104     if (argc >= 0) {
1105         *pargc = argc;
1106         *pargv = argv;
1107     }
1108 
1109     *pmode = mode;
1110 
1111     return JNI_TRUE;
1112 }
1113 
1114 /*
1115  * Initializes the Java Virtual Machine. Also frees options array when
1116  * finished.
1117  */
1118 static jboolean
1119 InitializeJVM(JavaVM **pvm, JNIEnv **penv, InvocationFunctions *ifn)
1120 {
1121     JavaVMInitArgs args;
1122     jint r;
1123 
1124     memset(&args, 0, sizeof(args));
1125     args.version  = JNI_VERSION_1_2;
1126     args.nOptions = numOptions;
1127     args.options  = options;
1128     args.ignoreUnrecognized = JNI_FALSE;
1129 
1130     if (JLI_IsTraceLauncher()) {
1131         int i = 0;
1132         printf("JavaVM args:\n    ");
1133         printf("version 0x%08lx, ", (long)args.version);
1134         printf("ignoreUnrecognized is %s, ",
1135                args.ignoreUnrecognized ? "JNI_TRUE" : "JNI_FALSE");
1136         printf("nOptions is %ld\n", (long)args.nOptions);
1137         for (i = 0; i < numOptions; i++)
1138             printf("    option[%2d] = '%s'\n",
1139                    i, args.options[i].optionString);
1140     }
1141 
1142     r = ifn->CreateJavaVM(pvm, (void **)penv, &args);
1143     JLI_MemFree(options);
1144     return r == JNI_OK;
1145 }
1146 
1147 static jclass helperClass = NULL;
1148 
1149 jclass
1150 GetLauncherHelperClass(JNIEnv *env)
1151 {
1152     if (helperClass == NULL) {
1153         NULL_CHECK0(helperClass = FindBootStrapClass(env,
1154                 "sun/launcher/LauncherHelper"));
1155     }
1156     return helperClass;
1157 }
1158 
1159 static jmethodID makePlatformStringMID = NULL;
1160 /*
1161  * Returns a new Java string object for the specified platform string.
1162  */
1163 static jstring
1164 NewPlatformString(JNIEnv *env, char *s)
1165 {
1166     int len = (int)JLI_StrLen(s);
1167     jbyteArray ary;
1168     jclass cls = GetLauncherHelperClass(env);
1169     NULL_CHECK0(cls);
1170     if (s == NULL)
1171         return 0;
1172 
1173     ary = (*env)->NewByteArray(env, len);
1174     if (ary != 0) {
1175         jstring str = 0;
1176         (*env)->SetByteArrayRegion(env, ary, 0, len, (jbyte *)s);
1177         if (!(*env)->ExceptionOccurred(env)) {
1178             if (makePlatformStringMID == NULL) {
1179                 NULL_CHECK0(makePlatformStringMID = (*env)->GetStaticMethodID(env,
1180                         cls, "makePlatformString", "(Z[B)Ljava/lang/String;"));
1181             }
1182             str = (*env)->CallStaticObjectMethod(env, cls,
1183                     makePlatformStringMID, USE_STDERR, ary);
1184             (*env)->DeleteLocalRef(env, ary);
1185             return str;
1186         }
1187     }
1188     return 0;
1189 }
1190 
1191 /*
1192  * Returns a new array of Java string objects for the specified
1193  * array of platform strings.
1194  */
1195 jobjectArray
1196 NewPlatformStringArray(JNIEnv *env, char **strv, int strc)
1197 {
1198     jarray cls;
1199     jarray ary;
1200     int i;
1201 
1202     NULL_CHECK0(cls = FindBootStrapClass(env, "java/lang/String"));
1203     NULL_CHECK0(ary = (*env)->NewObjectArray(env, strc, cls, 0));
1204     for (i = 0; i < strc; i++) {
1205         jstring str = NewPlatformString(env, *strv++);
1206         NULL_CHECK0(str);
1207         (*env)->SetObjectArrayElement(env, ary, i, str);
1208         (*env)->DeleteLocalRef(env, str);
1209     }
1210     return ary;
1211 }
1212 
1213 /*
1214  * Loads a class and verifies that the main class is present and it is ok to
1215  * call it for more details refer to the java implementation.
1216  */
1217 static jclass
1218 LoadMainClass(JNIEnv *env, int mode, char *name)
1219 {
1220     jmethodID mid;
1221     jstring str;
1222     jobject result;
1223     jlong start, end;
1224     jclass cls = GetLauncherHelperClass(env);
1225     NULL_CHECK0(cls);
1226     if (JLI_IsTraceLauncher()) {
1227         start = CounterGet();
1228     }
1229     NULL_CHECK0(mid = (*env)->GetStaticMethodID(env, cls,
1230                 "checkAndLoadMain",
1231                 "(ZILjava/lang/String;)Ljava/lang/Class;"));
1232 
1233     NULL_CHECK0(str = NewPlatformString(env, name));
1234     NULL_CHECK0(result = (*env)->CallStaticObjectMethod(env, cls, mid,
1235                                                         USE_STDERR, mode, str));
1236 
1237     if (JLI_IsTraceLauncher()) {
1238         end   = CounterGet();
1239         printf("%ld micro seconds to load main class\n",
1240                (long)(jint)Counter2Micros(end-start));
1241         printf("----%s----\n", JLDEBUG_ENV_ENTRY);
1242     }
1243 
1244     return (jclass)result;
1245 }
1246 
1247 static jclass
1248 GetApplicationClass(JNIEnv *env)
1249 {
1250     jmethodID mid;
1251     jclass cls = GetLauncherHelperClass(env);
1252     NULL_CHECK0(cls);
1253     NULL_CHECK0(mid = (*env)->GetStaticMethodID(env, cls,
1254                 "getApplicationClass",
1255                 "()Ljava/lang/Class;"));
1256 
1257     return (*env)->CallStaticObjectMethod(env, cls, mid);
1258 }
1259 
1260 /*
1261  * For tools, convert command line args thus:
1262  *   javac -cp foo:foo/"*" -J-ms32m ...
1263  *   java -ms32m -cp JLI_WildcardExpandClasspath(foo:foo/"*") ...
1264  *
1265  * Takes 4 parameters, and returns the populated arguments
1266  */
1267 static void
1268 TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv)
1269 {
1270     int argc = *pargc;
1271     char **argv = *pargv;
1272     int nargc = argc + jargc;
1273     char **nargv = JLI_MemAlloc((nargc + 1) * sizeof(char *));
1274     int i;
1275 
1276     *pargc = nargc;
1277     *pargv = nargv;
1278 
1279     /* Copy the VM arguments (i.e. prefixed with -J) */
1280     for (i = 0; i < jargc; i++) {
1281         const char *arg = jargv[i];
1282         if (arg[0] == '-' && arg[1] == 'J') {
1283             *nargv++ = ((arg + 2) == NULL) ? NULL : JLI_StringDup(arg + 2);
1284         }
1285     }
1286 
1287     for (i = 0; i < argc; i++) {
1288         char *arg = argv[i];
1289         if (arg[0] == '-' && arg[1] == 'J') {
1290             if (arg[2] == '\0') {
1291                 JLI_ReportErrorMessage(ARG_ERROR3);
1292                 exit(1);
1293             }
1294             *nargv++ = arg + 2;
1295         }
1296     }
1297 
1298     /* Copy the rest of the arguments */
1299     for (i = 0; i < jargc ; i++) {
1300         const char *arg = jargv[i];
1301         if (arg[0] != '-' || arg[1] != 'J') {
1302             *nargv++ = (arg == NULL) ? NULL : JLI_StringDup(arg);
1303         }
1304     }
1305     for (i = 0; i < argc; i++) {
1306         char *arg = argv[i];
1307         if (arg[0] == '-') {
1308             if (arg[1] == 'J')
1309                 continue;
1310             if (IsWildCardEnabled() && arg[1] == 'c'
1311                 && (JLI_StrCmp(arg, "-cp") == 0 ||
1312                     JLI_StrCmp(arg, "-classpath") == 0)
1313                 && i < argc - 1) {
1314                 *nargv++ = arg;
1315                 *nargv++ = (char *) JLI_WildcardExpandClasspath(argv[i+1]);
1316                 i++;
1317                 continue;
1318             }
1319         }
1320         *nargv++ = arg;
1321     }
1322     *nargv = 0;
1323 }
1324 
1325 /*
1326  * For our tools, we try to add 3 VM options:
1327  *      -Denv.class.path=<envcp>
1328  *      -Dapplication.home=<apphome>
1329  *      -Djava.class.path=<appcp>
1330  * <envcp>   is the user's setting of CLASSPATH -- for instance the user
1331  *           tells javac where to find binary classes through this environment
1332  *           variable.  Notice that users will be able to compile against our
1333  *           tools classes (sun.tools.javac.Main) only if they explicitly add
1334  *           tools.jar to CLASSPATH.
1335  * <apphome> is the directory where the application is installed.
1336  * <appcp>   is the classpath to where our apps' classfiles are.
1337  */
1338 static jboolean
1339 AddApplicationOptions(int cpathc, const char **cpathv)
1340 {
1341     char *envcp, *appcp, *apphome;
1342     char home[MAXPATHLEN]; /* application home */
1343     char separator[] = { PATH_SEPARATOR, '\0' };
1344     int size, i;
1345 
1346     {
1347         const char *s = getenv("CLASSPATH");
1348         if (s) {
1349             s = (char *) JLI_WildcardExpandClasspath(s);
1350             /* 40 for -Denv.class.path= */
1351             if (JLI_StrLen(s) + 40 > JLI_StrLen(s)) { // Safeguard from overflow
1352                 envcp = (char *)JLI_MemAlloc(JLI_StrLen(s) + 40);
1353                 sprintf(envcp, "-Denv.class.path=%s", s);
1354                 AddOption(envcp, NULL);
1355             }
1356         }
1357     }
1358 
1359     if (!GetApplicationHome(home, sizeof(home))) {
1360         JLI_ReportErrorMessage(CFG_ERROR5);
1361         return JNI_FALSE;
1362     }
1363 
1364     /* 40 for '-Dapplication.home=' */
1365     apphome = (char *)JLI_MemAlloc(JLI_StrLen(home) + 40);
1366     sprintf(apphome, "-Dapplication.home=%s", home);
1367     AddOption(apphome, NULL);
1368 
1369     /* How big is the application's classpath? */
1370     size = 40;                                 /* 40: "-Djava.class.path=" */
1371     for (i = 0; i < cpathc; i++) {
1372         size += (int)JLI_StrLen(home) + (int)JLI_StrLen(cpathv[i]) + 1; /* 1: separator */
1373     }
1374     appcp = (char *)JLI_MemAlloc(size + 1);
1375     JLI_StrCpy(appcp, "-Djava.class.path=");
1376     for (i = 0; i < cpathc; i++) {
1377         JLI_StrCat(appcp, home);                        /* c:\program files\myapp */
1378         JLI_StrCat(appcp, cpathv[i]);           /* \lib\myapp.jar         */
1379         JLI_StrCat(appcp, separator);           /* ;                      */
1380     }
1381     appcp[JLI_StrLen(appcp)-1] = '\0';  /* remove trailing path separator */
1382     AddOption(appcp, NULL);
1383     return JNI_TRUE;
1384 }
1385 
1386 /*
1387  * inject the -Dsun.java.command pseudo property into the args structure
1388  * this pseudo property is used in the HotSpot VM to expose the
1389  * Java class name and arguments to the main method to the VM. The
1390  * HotSpot VM uses this pseudo property to store the Java class name
1391  * (or jar file name) and the arguments to the class's main method
1392  * to the instrumentation memory region. The sun.java.command pseudo
1393  * property is not exported by HotSpot to the Java layer.
1394  */
1395 void
1396 SetJavaCommandLineProp(char *what, int argc, char **argv)
1397 {
1398 
1399     int i = 0;
1400     size_t len = 0;
1401     char* javaCommand = NULL;
1402     char* dashDstr = "-Dsun.java.command=";
1403 
1404     if (what == NULL) {
1405         /* unexpected, one of these should be set. just return without
1406          * setting the property
1407          */
1408         return;
1409     }
1410 
1411     /* determine the amount of memory to allocate assuming
1412      * the individual components will be space separated
1413      */
1414     len = JLI_StrLen(what);
1415     for (i = 0; i < argc; i++) {
1416         len += JLI_StrLen(argv[i]) + 1;
1417     }
1418 
1419     /* allocate the memory */
1420     javaCommand = (char*) JLI_MemAlloc(len + JLI_StrLen(dashDstr) + 1);
1421 
1422     /* build the -D string */
1423     *javaCommand = '\0';
1424     JLI_StrCat(javaCommand, dashDstr);
1425     JLI_StrCat(javaCommand, what);
1426 
1427     for (i = 0; i < argc; i++) {
1428         /* the components of the string are space separated. In
1429          * the case of embedded white space, the relationship of
1430          * the white space separated components to their true
1431          * positional arguments will be ambiguous. This issue may
1432          * be addressed in a future release.
1433          */
1434         JLI_StrCat(javaCommand, " ");
1435         JLI_StrCat(javaCommand, argv[i]);
1436     }
1437 
1438     AddOption(javaCommand, NULL);
1439 }
1440 
1441 /*
1442  * JVM would like to know if it's created by a standard Sun launcher, or by
1443  * user native application, the following property indicates the former.
1444  */
1445 void
1446 SetJavaLauncherProp() {
1447   AddOption("-Dsun.java.launcher=SUN_STANDARD", NULL);
1448 }
1449 
1450 /*
1451  * Prints the version information from the java.version and other properties.
1452  */
1453 static void
1454 PrintJavaVersion(JNIEnv *env, jboolean extraLF)
1455 {
1456     jclass ver;
1457     jmethodID print;
1458 
1459     NULL_CHECK(ver = FindBootStrapClass(env, "sun/misc/Version"));
1460     NULL_CHECK(print = (*env)->GetStaticMethodID(env,
1461                                                  ver,
1462                                                  (extraLF == JNI_TRUE) ? "println" : "print",
1463                                                  "()V"
1464                                                  )
1465               );
1466 
1467     (*env)->CallStaticVoidMethod(env, ver, print);
1468 }
1469 
1470 /*
1471  * Prints all the Java settings, see the java implementation for more details.
1472  */
1473 static void
1474 ShowSettings(JNIEnv *env, char *optString)
1475 {
1476     jmethodID showSettingsID;
1477     jstring joptString;
1478     jclass cls = GetLauncherHelperClass(env);
1479     NULL_CHECK(cls);
1480     NULL_CHECK(showSettingsID = (*env)->GetStaticMethodID(env, cls,
1481             "showSettings", "(ZLjava/lang/String;JJJZ)V"));
1482     NULL_CHECK(joptString = (*env)->NewStringUTF(env, optString));
1483     (*env)->CallStaticVoidMethod(env, cls, showSettingsID,
1484                                  USE_STDERR,
1485                                  joptString,
1486                                  (jlong)initialHeapSize,
1487                                  (jlong)maxHeapSize,
1488                                  (jlong)threadStackSize,
1489                                  ServerClassMachine());
1490 }
1491 
1492 /*
1493  * Prints default usage or the Xusage message, see sun.launcher.LauncherHelper.java
1494  */
1495 static void
1496 PrintUsage(JNIEnv* env, jboolean doXUsage)
1497 {
1498   jmethodID initHelp, vmSelect, vmSynonym, vmErgo, printHelp, printXUsageMessage;
1499   jstring jprogname, vm1, vm2;
1500   int i;
1501   jclass cls = GetLauncherHelperClass(env);
1502   NULL_CHECK(cls);
1503   if (doXUsage) {
1504     NULL_CHECK(printXUsageMessage = (*env)->GetStaticMethodID(env, cls,
1505                                         "printXUsageMessage", "(Z)V"));
1506     (*env)->CallStaticVoidMethod(env, cls, printXUsageMessage, USE_STDERR);
1507   } else {
1508     NULL_CHECK(initHelp = (*env)->GetStaticMethodID(env, cls,
1509                                         "initHelpMessage", "(Ljava/lang/String;)V"));
1510 
1511     NULL_CHECK(vmSelect = (*env)->GetStaticMethodID(env, cls, "appendVmSelectMessage",
1512                                         "(Ljava/lang/String;Ljava/lang/String;)V"));
1513 
1514     NULL_CHECK(vmSynonym = (*env)->GetStaticMethodID(env, cls,
1515                                         "appendVmSynonymMessage",
1516                                         "(Ljava/lang/String;Ljava/lang/String;)V"));
1517     NULL_CHECK(vmErgo = (*env)->GetStaticMethodID(env, cls,
1518                                         "appendVmErgoMessage", "(ZLjava/lang/String;)V"));
1519 
1520     NULL_CHECK(printHelp = (*env)->GetStaticMethodID(env, cls,
1521                                         "printHelpMessage", "(Z)V"));
1522 
1523     NULL_CHECK(jprogname = (*env)->NewStringUTF(env, _program_name));
1524 
1525     /* Initialize the usage message with the usual preamble */
1526     (*env)->CallStaticVoidMethod(env, cls, initHelp, jprogname);
1527     CHECK_EXCEPTION_RETURN();
1528 
1529 
1530     /* Assemble the other variant part of the usage */
1531     if ((knownVMs[0].flag == VM_KNOWN) ||
1532         (knownVMs[0].flag == VM_IF_SERVER_CLASS)) {
1533       NULL_CHECK(vm1 = (*env)->NewStringUTF(env, knownVMs[0].name));
1534       NULL_CHECK(vm2 =  (*env)->NewStringUTF(env, knownVMs[0].name+1));
1535       (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1536       CHECK_EXCEPTION_RETURN();
1537     }
1538     for (i=1; i<knownVMsCount; i++) {
1539       if (knownVMs[i].flag == VM_KNOWN) {
1540         NULL_CHECK(vm1 =  (*env)->NewStringUTF(env, knownVMs[i].name));
1541         NULL_CHECK(vm2 =  (*env)->NewStringUTF(env, knownVMs[i].name+1));
1542         (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1543         CHECK_EXCEPTION_RETURN();
1544       }
1545     }
1546     for (i=1; i<knownVMsCount; i++) {
1547       if (knownVMs[i].flag == VM_ALIASED_TO) {
1548         NULL_CHECK(vm1 =  (*env)->NewStringUTF(env, knownVMs[i].name));
1549         NULL_CHECK(vm2 =  (*env)->NewStringUTF(env, knownVMs[i].alias+1));
1550         (*env)->CallStaticVoidMethod(env, cls, vmSynonym, vm1, vm2);
1551         CHECK_EXCEPTION_RETURN();
1552       }
1553     }
1554 
1555     /* The first known VM is the default */
1556     {
1557       jboolean isServerClassMachine = ServerClassMachine();
1558 
1559       const char* defaultVM  =  knownVMs[0].name+1;
1560       if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) && isServerClassMachine) {
1561         defaultVM = knownVMs[0].server_class+1;
1562       }
1563 
1564       NULL_CHECK(vm1 =  (*env)->NewStringUTF(env, defaultVM));
1565       (*env)->CallStaticVoidMethod(env, cls, vmErgo, isServerClassMachine,  vm1);
1566       CHECK_EXCEPTION_RETURN();
1567     }
1568 
1569     /* Complete the usage message and print to stderr*/
1570     (*env)->CallStaticVoidMethod(env, cls, printHelp, USE_STDERR);
1571   }
1572   return;
1573 }
1574 
1575 /*
1576  * Read the jvm.cfg file and fill the knownJVMs[] array.
1577  *
1578  * The functionality of the jvm.cfg file is subject to change without
1579  * notice and the mechanism will be removed in the future.
1580  *
1581  * The lexical structure of the jvm.cfg file is as follows:
1582  *
1583  *     jvmcfg         :=  { vmLine }
1584  *     vmLine         :=  knownLine
1585  *                    |   aliasLine
1586  *                    |   warnLine
1587  *                    |   ignoreLine
1588  *                    |   errorLine
1589  *                    |   predicateLine
1590  *                    |   commentLine
1591  *     knownLine      :=  flag  "KNOWN"                  EOL
1592  *     warnLine       :=  flag  "WARN"                   EOL
1593  *     ignoreLine     :=  flag  "IGNORE"                 EOL
1594  *     errorLine      :=  flag  "ERROR"                  EOL
1595  *     aliasLine      :=  flag  "ALIASED_TO"       flag  EOL
1596  *     predicateLine  :=  flag  "IF_SERVER_CLASS"  flag  EOL
1597  *     commentLine    :=  "#" text                       EOL
1598  *     flag           :=  "-" identifier
1599  *
1600  * The semantics are that when someone specifies a flag on the command line:
1601  * - if the flag appears on a knownLine, then the identifier is used as
1602  *   the name of the directory holding the JVM library (the name of the JVM).
1603  * - if the flag appears as the first flag on an aliasLine, the identifier
1604  *   of the second flag is used as the name of the JVM.
1605  * - if the flag appears on a warnLine, the identifier is used as the
1606  *   name of the JVM, but a warning is generated.
1607  * - if the flag appears on an ignoreLine, the identifier is recognized as the
1608  *   name of a JVM, but the identifier is ignored and the default vm used
1609  * - if the flag appears on an errorLine, an error is generated.
1610  * - if the flag appears as the first flag on a predicateLine, and
1611  *   the machine on which you are running passes the predicate indicated,
1612  *   then the identifier of the second flag is used as the name of the JVM,
1613  *   otherwise the identifier of the first flag is used as the name of the JVM.
1614  * If no flag is given on the command line, the first vmLine of the jvm.cfg
1615  * file determines the name of the JVM.
1616  * PredicateLines are only interpreted on first vmLine of a jvm.cfg file,
1617  * since they only make sense if someone hasn't specified the name of the
1618  * JVM on the command line.
1619  *
1620  * The intent of the jvm.cfg file is to allow several JVM libraries to
1621  * be installed in different subdirectories of a single JRE installation,
1622  * for space-savings and convenience in testing.
1623  * The intent is explicitly not to provide a full aliasing or predicate
1624  * mechanism.
1625  */
1626 jint
1627 ReadKnownVMs(const char *jvmCfgName, jboolean speculative)
1628 {
1629     FILE *jvmCfg;
1630     char line[MAXPATHLEN+20];
1631     int cnt = 0;
1632     int lineno = 0;
1633     jlong start, end;
1634     int vmType;
1635     char *tmpPtr;
1636     char *altVMName = NULL;
1637     char *serverClassVMName = NULL;
1638     static char *whiteSpace = " \t";
1639     if (JLI_IsTraceLauncher()) {
1640         start = CounterGet();
1641     }
1642 
1643     jvmCfg = fopen(jvmCfgName, "r");
1644     if (jvmCfg == NULL) {
1645       if (!speculative) {
1646         JLI_ReportErrorMessage(CFG_ERROR6, jvmCfgName);
1647         exit(1);
1648       } else {
1649         return -1;
1650       }
1651     }
1652     while (fgets(line, sizeof(line), jvmCfg) != NULL) {
1653         vmType = VM_UNKNOWN;
1654         lineno++;
1655         if (line[0] == '#')
1656             continue;
1657         if (line[0] != '-') {
1658             JLI_ReportErrorMessage(CFG_WARN2, lineno, jvmCfgName);
1659         }
1660         if (cnt >= knownVMsLimit) {
1661             GrowKnownVMs(cnt);
1662         }
1663         line[JLI_StrLen(line)-1] = '\0'; /* remove trailing newline */
1664         tmpPtr = line + JLI_StrCSpn(line, whiteSpace);
1665         if (*tmpPtr == 0) {
1666             JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
1667         } else {
1668             /* Null-terminate this string for JLI_StringDup below */
1669             *tmpPtr++ = 0;
1670             tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1671             if (*tmpPtr == 0) {
1672                 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
1673             } else {
1674                 if (!JLI_StrCCmp(tmpPtr, "KNOWN")) {
1675                     vmType = VM_KNOWN;
1676                 } else if (!JLI_StrCCmp(tmpPtr, "ALIASED_TO")) {
1677                     tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1678                     if (*tmpPtr != 0) {
1679                         tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1680                     }
1681                     if (*tmpPtr == 0) {
1682                         JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
1683                     } else {
1684                         /* Null terminate altVMName */
1685                         altVMName = tmpPtr;
1686                         tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1687                         *tmpPtr = 0;
1688                         vmType = VM_ALIASED_TO;
1689                     }
1690                 } else if (!JLI_StrCCmp(tmpPtr, "WARN")) {
1691                     vmType = VM_WARN;
1692                 } else if (!JLI_StrCCmp(tmpPtr, "IGNORE")) {
1693                     vmType = VM_IGNORE;
1694                 } else if (!JLI_StrCCmp(tmpPtr, "ERROR")) {
1695                     vmType = VM_ERROR;
1696                 } else if (!JLI_StrCCmp(tmpPtr, "IF_SERVER_CLASS")) {
1697                     tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1698                     if (*tmpPtr != 0) {
1699                         tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1700                     }
1701                     if (*tmpPtr == 0) {
1702                         JLI_ReportErrorMessage(CFG_WARN4, lineno, jvmCfgName);
1703                     } else {
1704                         /* Null terminate server class VM name */
1705                         serverClassVMName = tmpPtr;
1706                         tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1707                         *tmpPtr = 0;
1708                         vmType = VM_IF_SERVER_CLASS;
1709                     }
1710                 } else {
1711                     JLI_ReportErrorMessage(CFG_WARN5, lineno, &jvmCfgName[0]);
1712                     vmType = VM_KNOWN;
1713                 }
1714             }
1715         }
1716 
1717         JLI_TraceLauncher("jvm.cfg[%d] = ->%s<-\n", cnt, line);
1718         if (vmType != VM_UNKNOWN) {
1719             knownVMs[cnt].name = JLI_StringDup(line);
1720             knownVMs[cnt].flag = vmType;
1721             switch (vmType) {
1722             default:
1723                 break;
1724             case VM_ALIASED_TO:
1725                 knownVMs[cnt].alias = JLI_StringDup(altVMName);
1726                 JLI_TraceLauncher("    name: %s  vmType: %s  alias: %s\n",
1727                    knownVMs[cnt].name, "VM_ALIASED_TO", knownVMs[cnt].alias);
1728                 break;
1729             case VM_IF_SERVER_CLASS:
1730                 knownVMs[cnt].server_class = JLI_StringDup(serverClassVMName);
1731                 JLI_TraceLauncher("    name: %s  vmType: %s  server_class: %s\n",
1732                     knownVMs[cnt].name, "VM_IF_SERVER_CLASS", knownVMs[cnt].server_class);
1733                 break;
1734             }
1735             cnt++;
1736         }
1737     }
1738     fclose(jvmCfg);
1739     knownVMsCount = cnt;
1740 
1741     if (JLI_IsTraceLauncher()) {
1742         end   = CounterGet();
1743         printf("%ld micro seconds to parse jvm.cfg\n",
1744                (long)(jint)Counter2Micros(end-start));
1745     }
1746 
1747     return cnt;
1748 }
1749 
1750 
1751 static void
1752 GrowKnownVMs(int minimum)
1753 {
1754     struct vmdesc* newKnownVMs;
1755     int newMax;
1756 
1757     newMax = (knownVMsLimit == 0 ? INIT_MAX_KNOWN_VMS : (2 * knownVMsLimit));
1758     if (newMax <= minimum) {
1759         newMax = minimum;
1760     }
1761     newKnownVMs = (struct vmdesc*) JLI_MemAlloc(newMax * sizeof(struct vmdesc));
1762     if (knownVMs != NULL) {
1763         memcpy(newKnownVMs, knownVMs, knownVMsLimit * sizeof(struct vmdesc));
1764     }
1765     JLI_MemFree(knownVMs);
1766     knownVMs = newKnownVMs;
1767     knownVMsLimit = newMax;
1768 }
1769 
1770 
1771 /* Returns index of VM or -1 if not found */
1772 static int
1773 KnownVMIndex(const char* name)
1774 {
1775     int i;
1776     if (JLI_StrCCmp(name, "-J") == 0) name += 2;
1777     for (i = 0; i < knownVMsCount; i++) {
1778         if (!JLI_StrCmp(name, knownVMs[i].name)) {
1779             return i;
1780         }
1781     }
1782     return -1;
1783 }
1784 
1785 static void
1786 FreeKnownVMs()
1787 {
1788     int i;
1789     for (i = 0; i < knownVMsCount; i++) {
1790         JLI_MemFree(knownVMs[i].name);
1791         knownVMs[i].name = NULL;
1792     }
1793     JLI_MemFree(knownVMs);
1794 }
1795 
1796 /*
1797  * Displays the splash screen according to the jar file name
1798  * and image file names stored in environment variables
1799  */
1800 void
1801 ShowSplashScreen()
1802 {
1803     const char *jar_name = getenv(SPLASH_JAR_ENV_ENTRY);
1804     const char *file_name = getenv(SPLASH_FILE_ENV_ENTRY);
1805     int data_size;
1806     void *image_data = NULL;
1807     float scale_factor = 1;
1808     char *scaled_splash_name = NULL;
1809 
1810     if (file_name == NULL){
1811         return;
1812     }
1813 
1814     scaled_splash_name = DoSplashGetScaledImageName(
1815                         jar_name, file_name, &scale_factor);
1816     if (jar_name) {
1817 
1818         if (scaled_splash_name) {
1819             image_data = JLI_JarUnpackFile(
1820                     jar_name, scaled_splash_name, &data_size);
1821         }
1822 
1823         if (!image_data) {
1824             scale_factor = 1;
1825             image_data = JLI_JarUnpackFile(
1826                             jar_name, file_name, &data_size);
1827         }
1828         if (image_data) {
1829             DoSplashInit();
1830             DoSplashSetScaleFactor(scale_factor);
1831             DoSplashLoadMemory(image_data, data_size);
1832             JLI_MemFree(image_data);
1833         }
1834     } else {
1835         DoSplashInit();
1836         if (scaled_splash_name) {
1837             DoSplashSetScaleFactor(scale_factor);
1838             DoSplashLoadFile(scaled_splash_name);
1839         } else {
1840             DoSplashLoadFile(file_name);
1841         }
1842     }
1843 
1844     if (scaled_splash_name) {
1845         JLI_MemFree(scaled_splash_name);
1846     }
1847 
1848     DoSplashSetFileJarName(file_name, jar_name);
1849 
1850     /*
1851      * Done with all command line processing and potential re-execs so
1852      * clean up the environment.
1853      */
1854     (void)UnsetEnv(ENV_ENTRY);
1855     (void)UnsetEnv(SPLASH_FILE_ENV_ENTRY);
1856     (void)UnsetEnv(SPLASH_JAR_ENV_ENTRY);
1857 
1858     JLI_MemFree(splash_jar_entry);
1859     JLI_MemFree(splash_file_entry);
1860 
1861 }
1862 
1863 const char*
1864 GetDotVersion()
1865 {
1866     return _dVersion;
1867 }
1868 
1869 const char*
1870 GetFullVersion()
1871 {
1872     return _fVersion;
1873 }
1874 
1875 const char*
1876 GetProgramName()
1877 {
1878     return _program_name;
1879 }
1880 
1881 const char*
1882 GetLauncherName()
1883 {
1884     return _launcher_name;
1885 }
1886 
1887 jint
1888 GetErgoPolicy()
1889 {
1890     return _ergo_policy;
1891 }
1892 
1893 jboolean
1894 IsJavaArgs()
1895 {
1896     return _is_java_args;
1897 }
1898 
1899 static jboolean
1900 IsWildCardEnabled()
1901 {
1902     return _wc_enabled;
1903 }
1904 
1905 int
1906 ContinueInNewThread(InvocationFunctions* ifn, jlong threadStackSize,
1907                     int argc, char **argv,
1908                     int mode, char *what, 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.mode = mode;
1933       args.what = what;
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 
1945 static void
1946 DumpState()
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:
1974  *    -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.
1979  */
1980 jboolean
1981 RemovableOption(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  */
2001 void
2002 JLI_ReportMessage(const char* fmt, ...)
2003 {
2004     va_list vl;
2005     va_start(vl, fmt);
2006     vfprintf(stderr, fmt, vl);
2007     fprintf(stderr, "\n");
2008     va_end(vl);
2009 }