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