1 /*
   2  * Copyright (c) 1998, 2019, 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 #include "java.h"
  27 #include "jvm_md.h"
  28 #include <dirent.h>
  29 #include <dlfcn.h>
  30 #include <fcntl.h>
  31 #include <inttypes.h>
  32 #include <stdio.h>
  33 #include <string.h>
  34 #include <stdlib.h>
  35 #include <sys/stat.h>
  36 #include <unistd.h>
  37 #include <sys/types.h>
  38 #include "manifest_info.h"
  39 
  40 
  41 #define JVM_DLL "libjvm.so"
  42 #define JAVA_DLL "libjava.so"
  43 #ifdef AIX
  44 #define LD_LIBRARY_PATH "LIBPATH"
  45 #else
  46 #define LD_LIBRARY_PATH "LD_LIBRARY_PATH"
  47 #endif
  48 
  49 /* help jettison the LD_LIBRARY_PATH settings in the future */
  50 #ifndef SETENV_REQUIRED
  51 #define SETENV_REQUIRED
  52 #endif
  53 
  54 #ifdef __solaris__
  55 #  include <sys/systeminfo.h>
  56 #  include <sys/elf.h>
  57 #  include <stdio.h>
  58 #endif
  59 
  60 /*
  61  * Flowchart of launcher execs and options processing on unix
  62  *
  63  * The selection of the proper vm shared library to open depends on
  64  * several classes of command line options, including vm "flavor"
  65  * options (-client, -server).
  66  * The vm selection options are not passed to the running
  67  * virtual machine; they must be screened out by the launcher.
  68  *
  69  * The version specification (if any) is processed first by the
  70  * platform independent routine SelectVersion.  This may result in
  71  * the exec of the specified launcher version.
  72  *
  73  * Previously the launcher modified the LD_LIBRARY_PATH appropriately for the
  74  * desired data model path, regardless if data models matched or not. The
  75  * launcher subsequently exec'ed the desired executable, in order to make the
  76  * LD_LIBRARY_PATH path available, for the runtime linker.
  77  *
  78  * Now, in most cases,the launcher will dlopen the target libjvm.so. All
  79  * required libraries are loaded by the runtime linker, using the
  80  * $RPATH/$ORIGIN baked into the shared libraries at compile time. Therefore,
  81  * in most cases, the launcher will only exec, if the data models are
  82  * mismatched, and will not set any environment variables, regardless of the
  83  * data models.
  84  *
  85  * However, if the environment contains a LD_LIBRARY_PATH, this will cause the
  86  * launcher to inspect the LD_LIBRARY_PATH. The launcher will check
  87  *  a. if the LD_LIBRARY_PATH's first component is the path to the desired
  88  *     libjvm.so
  89  *  b. if any other libjvm.so is found in any of the paths.
  90  * If case b is true, then the launcher will set the LD_LIBRARY_PATH to the
  91  * desired JRE and reexec, in order to propagate the environment.
  92  *
  93  *  Main
  94  *  (incoming argv)
  95  *  |
  96  * \|/
  97  * CreateExecutionEnvironment
  98  * (determines desired data model)
  99  *  |
 100  *  |
 101  * \|/
 102  *  Have Desired Model ? --> NO --> Exit(with error)
 103  *  |
 104  *  |
 105  * \|/
 106  * YES
 107  *  |
 108  *  |
 109  * \|/
 110  * CheckJvmType
 111  * (removes -client, -server, etc.)
 112  *  |
 113  *  |
 114  * \|/
 115  * TranslateDashJArgs...
 116  * (Prepare to pass args to vm)
 117  *  |
 118  *  |
 119  * \|/
 120  * ParseArguments
 121  *   |
 122  *   |
 123  *  \|/
 124  * RequiresSetenv
 125  * Is LD_LIBRARY_PATH
 126  * and friends set ? --> NO --> Continue
 127  *  YES
 128  *   |
 129  *   |
 130  *  \|/
 131  * Path is desired JRE ? YES --> Continue
 132  *  NO
 133  *   |
 134  *   |
 135  *  \|/
 136  * Paths have well known
 137  * jvm paths ?       --> NO --> Error/Exit
 138  *  YES
 139  *   |
 140  *   |
 141  *  \|/
 142  *  Does libjvm.so exist
 143  *  in any of them ? --> NO  --> Continue
 144  *   YES
 145  *   |
 146  *   |
 147  *  \|/
 148  *  Set the LD_LIBRARY_PATH
 149  *   |
 150  *   |
 151  *  \|/
 152  * Re-exec
 153  *   |
 154  *   |
 155  *  \|/
 156  * Main
 157  */
 158 
 159 /* Store the name of the executable once computed */
 160 static char *execname = NULL;
 161 
 162 /*
 163  * execname accessor from other parts of platform dependent logic
 164  */
 165 const char *
 166 GetExecName() {
 167     return execname;
 168 }
 169 
 170 #ifdef SETENV_REQUIRED
 171 static jboolean
 172 JvmExists(const char *path) {
 173     char tmp[PATH_MAX + 1];
 174     struct stat statbuf;
 175     JLI_Snprintf(tmp, PATH_MAX, "%s/%s", path, JVM_DLL);
 176     if (stat(tmp, &statbuf) == 0) {
 177         return JNI_TRUE;
 178     }
 179     return JNI_FALSE;
 180 }
 181 /*
 182  * contains a lib/{server,client}/libjvm.so ?
 183  */
 184 static jboolean
 185 ContainsLibJVM(const char *env) {
 186     /* the usual suspects */
 187     char clientPattern[] = "lib/client";
 188     char serverPattern[] = "lib/server";
 189     char *envpath;
 190     char *path;
 191     jboolean clientPatternFound;
 192     jboolean serverPatternFound;
 193 
 194     /* fastest path */
 195     if (env == NULL) {
 196         return JNI_FALSE;
 197     }
 198 
 199     /* to optimize for time, test if any of our usual suspects are present. */
 200     clientPatternFound = JLI_StrStr(env, clientPattern) != NULL;
 201     serverPatternFound = JLI_StrStr(env, serverPattern) != NULL;
 202     if (clientPatternFound == JNI_FALSE && serverPatternFound == JNI_FALSE) {
 203         return JNI_FALSE;
 204     }
 205 
 206     /*
 207      * we have a suspicious path component, check if it contains a libjvm.so
 208      */
 209     envpath = JLI_StringDup(env);
 210     for (path = JLI_StrTok(envpath, ":"); path != NULL; path = JLI_StrTok(NULL, ":")) {
 211         if (clientPatternFound && JLI_StrStr(path, clientPattern) != NULL) {
 212             if (JvmExists(path)) {
 213                 JLI_MemFree(envpath);
 214                 return JNI_TRUE;
 215             }
 216         }
 217         if (serverPatternFound && JLI_StrStr(path, serverPattern)  != NULL) {
 218             if (JvmExists(path)) {
 219                 JLI_MemFree(envpath);
 220                 return JNI_TRUE;
 221             }
 222         }
 223     }
 224     JLI_MemFree(envpath);
 225     return JNI_FALSE;
 226 }
 227 
 228 /*
 229  * Test whether the environment variable needs to be set, see flowchart.
 230  */
 231 static jboolean
 232 RequiresSetenv(const char *jvmpath) {
 233     char jpath[PATH_MAX + 1];
 234     char *llp;
 235     char *dmllp = NULL;
 236     char *p; /* a utility pointer */
 237 
 238 #ifdef __linux
 239 #ifndef LIBC
 240 #error "LIBC not set"
 241 #endif
 242 
 243     if (strcmp(LIBC, "musl") == 0) {
 244       /*
 245        * The musl library loader requires LD_LIBRARY_PATH to be set in
 246        * order to correctly resolve the dependency libjava.so has on libjvm.so.
 247        *
 248        * Specifically, it differs from glibc in the sense that even if
 249        * libjvm.so has already been loaded it will not be considered a
 250        * candidate for resolving the dependency unless the *full* path
 251        * of the already loaded library matches the dependency being loaded.
 252        *
 253        * libjvm.so is being loaded by the launcher using a long path to
 254        * dlopen, not just the basename of the library. Typically this
 255        * is something like "../lib/server/libjvm.so". However, if/when
 256        * libjvm.so later tries to dlopen libjava.so (which it does in
 257        * order to get access to a few functions implemented in
 258        * libjava.so) the musl loader will, as part of loading
 259        * dependent libraries, try to load libjvm.so using only its
 260        * basename "libjvm.so". Since this does not match the longer
 261        * path path it was first loaded with, the already loaded
 262        * library is not considered a candidate, and the loader will
 263        * instead look for libjvm.so elsewhere. If it's not in
 264        * LD_LIBRARY_PATH the dependency load will fail, and libjava.so
 265        * will therefore fail as well.
 266        */
 267       return JNI_TRUE;
 268     }
 269 #endif
 270 
 271 #ifdef AIX
 272     /* We always have to set the LIBPATH on AIX because ld doesn't support $ORIGIN. */
 273     return JNI_TRUE;
 274 #endif
 275 
 276     llp = getenv("LD_LIBRARY_PATH");
 277 #ifdef __solaris__
 278     dmllp = getenv("LD_LIBRARY_PATH_64");
 279 #endif /* __solaris__ */
 280     /* no environment variable is a good environment variable */
 281     if (llp == NULL && dmllp == NULL) {
 282         return JNI_FALSE;
 283     }
 284 #ifdef __linux
 285     /*
 286      * On linux, if a binary is running as sgid or suid, glibc sets
 287      * LD_LIBRARY_PATH to the empty string for security purposes. (In contrast,
 288      * on Solaris the LD_LIBRARY_PATH variable for a privileged binary does not
 289      * lose its settings; but the dynamic linker does apply more scrutiny to the
 290      * path.) The launcher uses the value of LD_LIBRARY_PATH to prevent an exec
 291      * loop, here and further downstream. Therefore, if we are running sgid or
 292      * suid, this function's setting of LD_LIBRARY_PATH will be ineffective and
 293      * we should case a return from the calling function.  Getting the right
 294      * libraries will be handled by the RPATH. In reality, this check is
 295      * redundant, as the previous check for a non-null LD_LIBRARY_PATH will
 296      * return back to the calling function forthwith, it is left here to safe
 297      * guard against any changes, in the glibc's existing security policy.
 298      */
 299     if ((getgid() != getegid()) || (getuid() != geteuid())) {
 300         return JNI_FALSE;
 301     }
 302 #endif /* __linux */
 303 
 304     /*
 305      * Prevent recursions. Since LD_LIBRARY_PATH is the one which will be set by
 306      * previous versions of the JRE, thus it is the only path that matters here.
 307      * So we check to see if the desired JRE is set.
 308      */
 309     JLI_StrNCpy(jpath, jvmpath, PATH_MAX);
 310     p = JLI_StrRChr(jpath, '/');
 311     *p = '\0';
 312     if (llp != NULL && JLI_StrNCmp(llp, jpath, JLI_StrLen(jpath)) == 0) {
 313         return JNI_FALSE;
 314     }
 315 
 316     /* scrutinize all the paths further */
 317     if (llp != NULL &&  ContainsLibJVM(llp)) {
 318         return JNI_TRUE;
 319     }
 320     if (dmllp != NULL && ContainsLibJVM(dmllp)) {
 321         return JNI_TRUE;
 322     }
 323     return JNI_FALSE;
 324 }
 325 #endif /* SETENV_REQUIRED */
 326 
 327 void
 328 CreateExecutionEnvironment(int *pargc, char ***pargv,
 329                            char jrepath[], jint so_jrepath,
 330                            char jvmpath[], jint so_jvmpath,
 331                            char jvmcfg[],  jint so_jvmcfg) {
 332 
 333     char * jvmtype = NULL;
 334     int argc = *pargc;
 335     char **argv = *pargv;
 336 
 337 #ifdef SETENV_REQUIRED
 338     jboolean mustsetenv = JNI_FALSE;
 339 #ifdef __solaris__
 340     char *llp64 = NULL; /* existing LD_LIBRARY_PATH_64 setting */
 341 #endif // __solaris__
 342     char *runpath = NULL; /* existing effective LD_LIBRARY_PATH setting */
 343     char* new_runpath = NULL; /* desired new LD_LIBRARY_PATH string */
 344     char* newpath = NULL; /* path on new LD_LIBRARY_PATH */
 345     char* lastslash = NULL;
 346     char** newenvp = NULL; /* current environment */
 347     size_t new_runpath_size;
 348 #endif  /* SETENV_REQUIRED */
 349 
 350     /* Compute/set the name of the executable */
 351     SetExecname(*pargv);
 352 
 353     /* Check to see if the jvmpath exists */
 354     /* Find out where the JRE is that we will be using. */
 355     if (!GetJREPath(jrepath, so_jrepath, JNI_FALSE)) {
 356         JLI_ReportErrorMessage(JRE_ERROR1);
 357         exit(2);
 358     }
 359     JLI_Snprintf(jvmcfg, so_jvmcfg, "%s%slib%sjvm.cfg",
 360             jrepath, FILESEP, FILESEP);
 361     /* Find the specified JVM type */
 362     if (ReadKnownVMs(jvmcfg, JNI_FALSE) < 1) {
 363         JLI_ReportErrorMessage(CFG_ERROR7);
 364         exit(1);
 365     }
 366 
 367     jvmpath[0] = '\0';
 368     jvmtype = CheckJvmType(pargc, pargv, JNI_FALSE);
 369     if (JLI_StrCmp(jvmtype, "ERROR") == 0) {
 370         JLI_ReportErrorMessage(CFG_ERROR9);
 371         exit(4);
 372     }
 373 
 374     if (!GetJVMPath(jrepath, jvmtype, jvmpath, so_jvmpath)) {
 375         JLI_ReportErrorMessage(CFG_ERROR8, jvmtype, jvmpath);
 376         exit(4);
 377     }
 378     /*
 379      * we seem to have everything we need, so without further ado
 380      * we return back, otherwise proceed to set the environment.
 381      */
 382 #ifdef SETENV_REQUIRED
 383     mustsetenv = RequiresSetenv(jvmpath);
 384     JLI_TraceLauncher("mustsetenv: %s\n", mustsetenv ? "TRUE" : "FALSE");
 385 
 386     if (mustsetenv == JNI_FALSE) {
 387         return;
 388     }
 389 #else
 390     return;
 391 #endif /* SETENV_REQUIRED */
 392 
 393 #ifdef SETENV_REQUIRED
 394     if (mustsetenv) {
 395         /*
 396          * We will set the LD_LIBRARY_PATH as follows:
 397          *
 398          *     o          $JVMPATH (directory portion only)
 399          *     o          $JRE/lib
 400          *     o          $JRE/../lib
 401          *
 402          * followed by the user's previous effective LD_LIBRARY_PATH, if
 403          * any.
 404          */
 405 
 406 #ifdef __solaris__
 407         llp64 = getenv("LD_LIBRARY_PATH_64");
 408         runpath = (llp64 == NULL) ? getenv(LD_LIBRARY_PATH) : llp64;
 409 #else
 410         runpath = getenv(LD_LIBRARY_PATH);
 411 #endif /* __solaris__ */
 412 
 413         /* runpath contains current effective LD_LIBRARY_PATH setting */
 414         { /* New scope to declare local variable */
 415             char *new_jvmpath = JLI_StringDup(jvmpath);
 416             new_runpath_size = ((runpath != NULL) ? JLI_StrLen(runpath) : 0) +
 417                     2 * JLI_StrLen(jrepath) +
 418                     JLI_StrLen(new_jvmpath) + 52;
 419             new_runpath = JLI_MemAlloc(new_runpath_size);
 420             newpath = new_runpath + JLI_StrLen(LD_LIBRARY_PATH "=");
 421 
 422 
 423             /*
 424              * Create desired LD_LIBRARY_PATH value for target data model.
 425              */
 426             {
 427                 /* remove the name of the .so from the JVM path */
 428                 lastslash = JLI_StrRChr(new_jvmpath, '/');
 429                 if (lastslash)
 430                     *lastslash = '\0';
 431 
 432                 sprintf(new_runpath, LD_LIBRARY_PATH "="
 433                         "%s:"
 434                         "%s/lib:"
 435                         "%s/../lib",
 436                         new_jvmpath,
 437                         jrepath,
 438                         jrepath
 439                         );
 440 
 441                 JLI_MemFree(new_jvmpath);
 442 
 443                 /*
 444                  * Check to make sure that the prefix of the current path is the
 445                  * desired environment variable setting, though the RequiresSetenv
 446                  * checks if the desired runpath exists, this logic does a more
 447                  * comprehensive check.
 448                  */
 449                 if (runpath != NULL &&
 450                         JLI_StrNCmp(newpath, runpath, JLI_StrLen(newpath)) == 0 &&
 451                         (runpath[JLI_StrLen(newpath)] == 0 ||
 452                         runpath[JLI_StrLen(newpath)] == ':')) {
 453                     JLI_MemFree(new_runpath);
 454                     return;
 455                 }
 456             }
 457         }
 458 
 459         /*
 460          * Place the desired environment setting onto the prefix of
 461          * LD_LIBRARY_PATH.  Note that this prevents any possible infinite
 462          * loop of execv() because we test for the prefix, above.
 463          */
 464         if (runpath != 0) {
 465             /* ensure storage for runpath + colon + NULL */
 466             if ((JLI_StrLen(runpath) + 1 + 1) > new_runpath_size) {
 467                 JLI_ReportErrorMessageSys(JRE_ERROR11);
 468                 exit(1);
 469             }
 470             JLI_StrCat(new_runpath, ":");
 471             JLI_StrCat(new_runpath, runpath);
 472         }
 473 
 474         if (putenv(new_runpath) != 0) {
 475             /* problem allocating memory; LD_LIBRARY_PATH not set properly */
 476             exit(1);
 477         }
 478 
 479         /*
 480          * Unix systems document that they look at LD_LIBRARY_PATH only
 481          * once at startup, so we have to re-exec the current executable
 482          * to get the changed environment variable to have an effect.
 483          */
 484 #ifdef __solaris__
 485         /*
 486          * new LD_LIBRARY_PATH took over for LD_LIBRARY_PATH_64
 487          */
 488         if (llp64 != NULL) {
 489             UnsetEnv("LD_LIBRARY_PATH_64");
 490         }
 491 #endif // __solaris__
 492 
 493         newenvp = environ;
 494     }
 495 #endif /* SETENV_REQUIRED */
 496     {
 497         char *newexec = execname;
 498         JLI_TraceLauncher("TRACER_MARKER:About to EXEC\n");
 499         (void) fflush(stdout);
 500         (void) fflush(stderr);
 501 #ifdef SETENV_REQUIRED
 502         if (mustsetenv) {
 503             execve(newexec, argv, newenvp);
 504         } else {
 505             execv(newexec, argv);
 506         }
 507 #else /* !SETENV_REQUIRED */
 508         execv(newexec, argv);
 509 #endif /* SETENV_REQUIRED */
 510         JLI_ReportErrorMessageSys(JRE_ERROR4, newexec);
 511     }
 512     exit(1);
 513 }
 514 
 515 
 516 static jboolean
 517 GetJVMPath(const char *jrepath, const char *jvmtype,
 518            char *jvmpath, jint jvmpathsize)
 519 {
 520     struct stat s;
 521 
 522     if (JLI_StrChr(jvmtype, '/')) {
 523         JLI_Snprintf(jvmpath, jvmpathsize, "%s/" JVM_DLL, jvmtype);
 524     } else {
 525         JLI_Snprintf(jvmpath, jvmpathsize, "%s/lib/%s/" JVM_DLL, jrepath, jvmtype);
 526     }
 527 
 528     JLI_TraceLauncher("Does `%s' exist ... ", jvmpath);
 529 
 530     if (stat(jvmpath, &s) == 0) {
 531         JLI_TraceLauncher("yes.\n");
 532         return JNI_TRUE;
 533     } else {
 534         JLI_TraceLauncher("no.\n");
 535         return JNI_FALSE;
 536     }
 537 }
 538 
 539 /*
 540  * Find path to JRE based on .exe's location or registry settings.
 541  */
 542 static jboolean
 543 GetJREPath(char *path, jint pathsize, jboolean speculative)
 544 {
 545     char libjava[MAXPATHLEN];
 546     struct stat s;
 547 
 548     if (GetApplicationHome(path, pathsize)) {
 549         /* Is JRE co-located with the application? */
 550         JLI_Snprintf(libjava, sizeof(libjava), "%s/lib/" JAVA_DLL, path);
 551         if (access(libjava, F_OK) == 0) {
 552             JLI_TraceLauncher("JRE path is %s\n", path);
 553             return JNI_TRUE;
 554         }
 555         /* ensure storage for path + /jre + NULL */
 556         if ((JLI_StrLen(path) + 4  + 1) > (size_t) pathsize) {
 557             JLI_TraceLauncher("Insufficient space to store JRE path\n");
 558             return JNI_FALSE;
 559         }
 560         /* Does the app ship a private JRE in <apphome>/jre directory? */
 561         JLI_Snprintf(libjava, sizeof(libjava), "%s/jre/lib/" JAVA_DLL, path);
 562         if (access(libjava, F_OK) == 0) {
 563             JLI_StrCat(path, "/jre");
 564             JLI_TraceLauncher("JRE path is %s\n", path);
 565             return JNI_TRUE;
 566         }
 567     }
 568 
 569     if (GetApplicationHomeFromDll(path, pathsize)) {
 570         JLI_Snprintf(libjava, sizeof(libjava), "%s/lib/" JAVA_DLL, path);
 571         if (stat(libjava, &s) == 0) {
 572             JLI_TraceLauncher("JRE path is %s\n", path);
 573             return JNI_TRUE;
 574         }
 575     }
 576 
 577     if (!speculative)
 578       JLI_ReportErrorMessage(JRE_ERROR8 JAVA_DLL);
 579     return JNI_FALSE;
 580 }
 581 
 582 jboolean
 583 LoadJavaVM(const char *jvmpath, InvocationFunctions *ifn)
 584 {
 585     void *libjvm;
 586 
 587     JLI_TraceLauncher("JVM path is %s\n", jvmpath);
 588 
 589     libjvm = dlopen(jvmpath, RTLD_NOW + RTLD_GLOBAL);
 590     if (libjvm == NULL) {
 591 #if defined(__solaris__) && defined(__sparc) && !defined(_LP64) /* i.e. 32-bit sparc */
 592       FILE * fp;
 593       Elf32_Ehdr elf_head;
 594       int count;
 595       int location;
 596 
 597       fp = fopen(jvmpath, "r");
 598       if (fp == NULL) {
 599         JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror());
 600         return JNI_FALSE;
 601       }
 602 
 603       /* read in elf header */
 604       count = fread((void*)(&elf_head), sizeof(Elf32_Ehdr), 1, fp);
 605       fclose(fp);
 606       if (count < 1) {
 607         JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror());
 608         return JNI_FALSE;
 609       }
 610 
 611       /*
 612        * Check for running a server vm (compiled with -xarch=v8plus)
 613        * on a stock v8 processor.  In this case, the machine type in
 614        * the elf header would not be included the architecture list
 615        * provided by the isalist command, which is turn is gotten from
 616        * sysinfo.  This case cannot occur on 64-bit hardware and thus
 617        * does not have to be checked for in binaries with an LP64 data
 618        * model.
 619        */
 620       if (elf_head.e_machine == EM_SPARC32PLUS) {
 621         char buf[257];  /* recommended buffer size from sysinfo man
 622                            page */
 623         long length;
 624         char* location;
 625 
 626         length = sysinfo(SI_ISALIST, buf, 257);
 627         if (length > 0) {
 628             location = JLI_StrStr(buf, "sparcv8plus ");
 629           if (location == NULL) {
 630             JLI_ReportErrorMessage(JVM_ERROR3);
 631             return JNI_FALSE;
 632           }
 633         }
 634       }
 635 #endif
 636         JLI_ReportErrorMessage(DLL_ERROR1, __LINE__);
 637         JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror());
 638         return JNI_FALSE;
 639     }
 640 
 641     ifn->CreateJavaVM = (CreateJavaVM_t)
 642         dlsym(libjvm, "JNI_CreateJavaVM");
 643     if (ifn->CreateJavaVM == NULL) {
 644         JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror());
 645         return JNI_FALSE;
 646     }
 647 
 648     ifn->GetDefaultJavaVMInitArgs = (GetDefaultJavaVMInitArgs_t)
 649         dlsym(libjvm, "JNI_GetDefaultJavaVMInitArgs");
 650     if (ifn->GetDefaultJavaVMInitArgs == NULL) {
 651         JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror());
 652         return JNI_FALSE;
 653     }
 654 
 655     ifn->GetCreatedJavaVMs = (GetCreatedJavaVMs_t)
 656         dlsym(libjvm, "JNI_GetCreatedJavaVMs");
 657     if (ifn->GetCreatedJavaVMs == NULL) {
 658         JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror());
 659         return JNI_FALSE;
 660     }
 661 
 662     return JNI_TRUE;
 663 }
 664 
 665 /*
 666  * Compute the name of the executable
 667  *
 668  * In order to re-exec securely we need the absolute path of the
 669  * executable. On Solaris getexecname(3c) may not return an absolute
 670  * path so we use dladdr to get the filename of the executable and
 671  * then use realpath to derive an absolute path. From Solaris 9
 672  * onwards the filename returned in DL_info structure from dladdr is
 673  * an absolute pathname so technically realpath isn't required.
 674  * On Linux we read the executable name from /proc/self/exe.
 675  * As a fallback, and for platforms other than Solaris and Linux,
 676  * we use FindExecName to compute the executable name.
 677  */
 678 const char*
 679 SetExecname(char **argv)
 680 {
 681     char* exec_path = NULL;
 682 #if defined(__solaris__)
 683     {
 684         Dl_info dlinfo;
 685         int (*fptr)();
 686 
 687         fptr = (int (*)())dlsym(RTLD_DEFAULT, "main");
 688         if (fptr == NULL) {
 689             JLI_ReportErrorMessage(DLL_ERROR3, dlerror());
 690             return JNI_FALSE;
 691         }
 692 
 693         if (dladdr((void*)fptr, &dlinfo)) {
 694             char *resolved = (char*)JLI_MemAlloc(PATH_MAX+1);
 695             if (resolved != NULL) {
 696                 exec_path = realpath(dlinfo.dli_fname, resolved);
 697                 if (exec_path == NULL) {
 698                     JLI_MemFree(resolved);
 699                 }
 700             }
 701         }
 702     }
 703 #elif defined(__linux__)
 704     {
 705         const char* self = "/proc/self/exe";
 706         char buf[PATH_MAX+1];
 707         int len = readlink(self, buf, PATH_MAX);
 708         if (len >= 0) {
 709             buf[len] = '\0';            /* readlink(2) doesn't NUL terminate */
 710             exec_path = JLI_StringDup(buf);
 711         }
 712     }
 713 #else /* !__solaris__ && !__linux__ */
 714     {
 715         /* Not implemented */
 716     }
 717 #endif
 718 
 719     if (exec_path == NULL) {
 720         exec_path = FindExecName(argv[0]);
 721     }
 722     execname = exec_path;
 723     return exec_path;
 724 }
 725 
 726 /* --- Splash Screen shared library support --- */
 727 static const char* SPLASHSCREEN_SO = JNI_LIB_NAME("splashscreen");
 728 static void* hSplashLib = NULL;
 729 
 730 void* SplashProcAddress(const char* name) {
 731     if (!hSplashLib) {
 732         int ret;
 733         char jrePath[MAXPATHLEN];
 734         char splashPath[MAXPATHLEN];
 735 
 736         if (!GetJREPath(jrePath, sizeof(jrePath), JNI_FALSE)) {
 737             JLI_ReportErrorMessage(JRE_ERROR1);
 738             return NULL;
 739         }
 740         ret = JLI_Snprintf(splashPath, sizeof(splashPath), "%s/lib/%s",
 741                      jrePath, SPLASHSCREEN_SO);
 742 
 743         if (ret >= (int) sizeof(splashPath)) {
 744             JLI_ReportErrorMessage(JRE_ERROR11);
 745             return NULL;
 746         }
 747         if (ret < 0) {
 748             JLI_ReportErrorMessage(JRE_ERROR13);
 749             return NULL;
 750         }
 751         hSplashLib = dlopen(splashPath, RTLD_LAZY | RTLD_GLOBAL);
 752         JLI_TraceLauncher("Info: loaded %s\n", splashPath);
 753     }
 754     if (hSplashLib) {
 755         void* sym = dlsym(hSplashLib, name);
 756         return sym;
 757     } else {
 758         return NULL;
 759     }
 760 }
 761 
 762 void SplashFreeLibrary() {
 763     if (hSplashLib) {
 764         dlclose(hSplashLib);
 765         hSplashLib = NULL;
 766     }
 767 }
 768 
 769 /*
 770  * Signature adapter for pthread_create() or thr_create().
 771  */
 772 static void* ThreadJavaMain(void* args) {
 773     return (void*)(intptr_t)JavaMain(args);
 774 }
 775 
 776 /*
 777  * Block current thread and continue execution in a new thread.
 778  */
 779 int
 780 CallJavaMainInNewThread(jlong stack_size, void* args) {
 781     int rslt;
 782 #ifndef __solaris__
 783     pthread_t tid;
 784     pthread_attr_t attr;
 785     pthread_attr_init(&attr);
 786     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
 787 
 788     if (stack_size > 0) {
 789         pthread_attr_setstacksize(&attr, stack_size);
 790     }
 791     pthread_attr_setguardsize(&attr, 0); // no pthread guard page on java threads
 792 
 793     if (pthread_create(&tid, &attr, ThreadJavaMain, args) == 0) {
 794         void* tmp;
 795         pthread_join(tid, &tmp);
 796         rslt = (int)(intptr_t)tmp;
 797     } else {
 798        /*
 799         * Continue execution in current thread if for some reason (e.g. out of
 800         * memory/LWP)  a new thread can't be created. This will likely fail
 801         * later in JavaMain as JNI_CreateJavaVM needs to create quite a
 802         * few new threads, anyway, just give it a try..
 803         */
 804         rslt = JavaMain(args);
 805     }
 806 
 807     pthread_attr_destroy(&attr);
 808 #else /* __solaris__ */
 809     thread_t tid;
 810     long flags = 0;
 811     if (thr_create(NULL, stack_size, ThreadJavaMain, args, flags, &tid) == 0) {
 812         void* tmp;
 813         thr_join(tid, NULL, &tmp);
 814         rslt = (int)(intptr_t)tmp;
 815     } else {
 816         /* See above. Continue in current thread if thr_create() failed */
 817         rslt = JavaMain(args);
 818     }
 819 #endif /* !__solaris__ */
 820     return rslt;
 821 }
 822 
 823 /* Coarse estimation of number of digits assuming the worst case is a 64-bit pid. */
 824 #define MAX_PID_STR_SZ   20
 825 
 826 void SetJavaLauncherPlatformProps() {
 827    /* Linux only */
 828 #ifdef __linux__
 829     const char *substr = "-Dsun.java.launcher.pid=";
 830     char *pid_prop_str = (char *)JLI_MemAlloc(JLI_StrLen(substr) + MAX_PID_STR_SZ + 1);
 831     sprintf(pid_prop_str, "%s%d", substr, getpid());
 832     AddOption(pid_prop_str, NULL);
 833 #endif /* __linux__ */
 834 }
 835 
 836 int
 837 JVMInit(InvocationFunctions* ifn, jlong threadStackSize,
 838         int argc, char **argv,
 839         int mode, char *what, int ret)
 840 {
 841     ShowSplashScreen();
 842     return ContinueInNewThread(ifn, threadStackSize, argc, argv, mode, what, ret);
 843 }
 844 
 845 void
 846 PostJVMInit(JNIEnv *env, jclass mainClass, JavaVM *vm)
 847 {
 848     // stubbed out for windows and *nixes.
 849 }
 850 
 851 void
 852 RegisterThread()
 853 {
 854     // stubbed out for windows and *nixes.
 855 }
 856 
 857 /*
 858  * on unix, we return a false to indicate this option is not applicable
 859  */
 860 jboolean
 861 ProcessPlatformOption(const char *arg)
 862 {
 863     return JNI_FALSE;
 864 }