hotspot/src/os/posix/launcher/java_md.c
changeset 17937 eb32dc973706
parent 17936 2a33a19ee977
parent 17670 8339e359fd41
child 17938 af1b01dfea42
equal deleted inserted replaced
17936:2a33a19ee977 17937:eb32dc973706
     1 /*
       
     2  * Copyright (c) 1999, 2013, 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.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  *
       
    23  */
       
    24 
       
    25 
       
    26 #include "java.h"
       
    27 #include <dirent.h>
       
    28 #include <dlfcn.h>
       
    29 #include <fcntl.h>
       
    30 #include <inttypes.h>
       
    31 #include <stdio.h>
       
    32 #include <string.h>
       
    33 #include <stdlib.h>
       
    34 #include <limits.h>
       
    35 #include <sys/stat.h>
       
    36 #include <unistd.h>
       
    37 #include <sys/types.h>
       
    38 
       
    39 #ifndef GAMMA
       
    40 #include "manifest_info.h"
       
    41 #include "version_comp.h"
       
    42 #endif
       
    43 
       
    44 #if defined(__linux__) || defined(_ALLBSD_SOURCE)
       
    45 #include <pthread.h>
       
    46 #else
       
    47 #include <thread.h>
       
    48 #endif
       
    49 
       
    50 #ifdef __APPLE__
       
    51 #define JVM_DLL "libjvm.dylib"
       
    52 #define JAVA_DLL "libjava.dylib"
       
    53 #define LD_LIBRARY_PATH "DYLD_LIBRARY_PATH"
       
    54 #else
       
    55 #define JVM_DLL "libjvm.so"
       
    56 #define JAVA_DLL "libjava.so"
       
    57 #define LD_LIBRARY_PATH "LD_LIBRARY_PATH"
       
    58 #endif
       
    59 
       
    60 #ifndef GAMMA   /* launcher.make defines ARCH */
       
    61 /*
       
    62  * If a processor / os combination has the ability to run binaries of
       
    63  * two data models and cohabitation of jre/jdk bits with both data
       
    64  * models is supported, then DUAL_MODE is defined.  When DUAL_MODE is
       
    65  * defined, the architecture names for the narrow and wide version of
       
    66  * the architecture are defined in LIBARCH64NAME and LIBARCH32NAME.  Currently
       
    67  * only Solaris on sparc/sparcv9 and i586/amd64 is DUAL_MODE; linux
       
    68  * i586/amd64 could be defined as DUAL_MODE but that is not the
       
    69  * current policy.
       
    70  */
       
    71 
       
    72 #ifndef LIBARCHNAME
       
    73 #  error "The macro LIBARCHNAME was not defined on the compile line"
       
    74 #endif
       
    75 
       
    76 #ifdef __sun
       
    77 #  define DUAL_MODE
       
    78 #  ifndef LIBARCH32NAME
       
    79 #    error "The macro LIBARCH32NAME was not defined on the compile line"
       
    80 #  endif
       
    81 #  ifndef LIBARCH64NAME
       
    82 #    error "The macro LIBARCH64NAME was not defined on the compile line"
       
    83 #  endif
       
    84 #  include <sys/systeminfo.h>
       
    85 #  include <sys/elf.h>
       
    86 #  include <stdio.h>
       
    87 #endif
       
    88 
       
    89 #endif /* ifndef GAMMA */
       
    90 
       
    91 /* pointer to environment */
       
    92 extern char **environ;
       
    93 
       
    94 #ifndef GAMMA
       
    95 /*
       
    96  *      A collection of useful strings. One should think of these as #define
       
    97  *      entries, but actual strings can be more efficient (with many compilers).
       
    98  */
       
    99 #ifdef __linux__
       
   100 static const char *system_dir   = "/usr/java";
       
   101 static const char *user_dir     = "/java";
       
   102 #else /* Solaris */
       
   103 static const char *system_dir   = "/usr/jdk";
       
   104 static const char *user_dir     = "/jdk";
       
   105 #endif
       
   106 
       
   107 #endif /* ifndef GAMMA */
       
   108 
       
   109 /*
       
   110  * Flowchart of launcher execs and options processing on unix
       
   111  *
       
   112  * The selection of the proper vm shared library to open depends on
       
   113  * several classes of command line options, including vm "flavor"
       
   114  * options (-client, -server) and the data model options, -d32  and
       
   115  * -d64, as well as a version specification which may have come from
       
   116  * the command line or from the manifest of an executable jar file.
       
   117  * The vm selection options are not passed to the running
       
   118  * virtual machine; they must be screened out by the launcher.
       
   119  *
       
   120  * The version specification (if any) is processed first by the
       
   121  * platform independent routine SelectVersion.  This may result in
       
   122  * the exec of the specified launcher version.
       
   123  *
       
   124  * Typically, the launcher execs at least once to ensure a suitable
       
   125  * LD_LIBRARY_PATH is in effect for the process.  The first exec
       
   126  * screens out all the data model options; leaving the choice of data
       
   127  * model implicit in the binary selected to run.  However, in case no
       
   128  * exec is done, the data model options are screened out before the vm
       
   129  * is invoked.
       
   130  *
       
   131  *  incoming argv ------------------------------
       
   132  *  |                                          |
       
   133  * \|/                                         |
       
   134  * CheckJVMType                                |
       
   135  * (removes -client, -server, etc.)            |
       
   136  *                                            \|/
       
   137  *                                            CreateExecutionEnvironment
       
   138  *                                            (removes -d32 and -d64,
       
   139  *                                             determines desired data model,
       
   140  *                                             sets up LD_LIBRARY_PATH,
       
   141  *                                             and exec's)
       
   142  *                                             |
       
   143  *  --------------------------------------------
       
   144  *  |
       
   145  * \|/
       
   146  * exec child 1 incoming argv -----------------
       
   147  *  |                                          |
       
   148  * \|/                                         |
       
   149  * CheckJVMType                                |
       
   150  * (removes -client, -server, etc.)            |
       
   151  *  |                                         \|/
       
   152  *  |                                          CreateExecutionEnvironment
       
   153  *  |                                          (verifies desired data model
       
   154  *  |                                           is running and acceptable
       
   155  *  |                                           LD_LIBRARY_PATH;
       
   156  *  |                                           no-op in child)
       
   157  *  |
       
   158  * \|/
       
   159  * TranslateDashJArgs...
       
   160  * (Prepare to pass args to vm)
       
   161  *  |
       
   162  *  |
       
   163  *  |
       
   164  * \|/
       
   165  * ParseArguments
       
   166  * (ignores -d32 and -d64,
       
   167  *  processes version options,
       
   168  *  creates argument list for vm,
       
   169  *  etc.)
       
   170  *
       
   171  */
       
   172 
       
   173 static char *SetExecname(char **argv);
       
   174 static char * GetExecname();
       
   175 static jboolean GetJVMPath(const char *jrepath, const char *jvmtype,
       
   176                            char *jvmpath, jint jvmpathsize, char * arch);
       
   177 static jboolean GetJREPath(char *path, jint pathsize, char * arch, jboolean speculative);
       
   178 
       
   179 #ifndef GAMMA
       
   180 const char *
       
   181 GetArch()
       
   182 {
       
   183     return LIBARCHNAME;
       
   184 }
       
   185 #endif /* ifndef GAMMA */
       
   186 
       
   187 void
       
   188 CreateExecutionEnvironment(int *_argcp,
       
   189                            char ***_argvp,
       
   190                            char jrepath[],
       
   191                            jint so_jrepath,
       
   192                            char jvmpath[],
       
   193                            jint so_jvmpath,
       
   194                            char **original_argv) {
       
   195   /*
       
   196    * First, determine if we are running the desired data model.  If we
       
   197    * are running the desired data model, all the error messages
       
   198    * associated with calling GetJREPath, ReadKnownVMs, etc. should be
       
   199    * output.  However, if we are not running the desired data model,
       
   200    * some of the errors should be suppressed since it is more
       
   201    * informative to issue an error message based on whether or not the
       
   202    * os/processor combination has dual mode capabilities.
       
   203    */
       
   204 
       
   205     char *execname = NULL;
       
   206     int original_argc = *_argcp;
       
   207     jboolean jvmpathExists;
       
   208 
       
   209     /* Compute the name of the executable */
       
   210     execname = SetExecname(*_argvp);
       
   211 
       
   212 #ifndef GAMMA
       
   213     /* Set the LD_LIBRARY_PATH environment variable, check data model
       
   214        flags, and exec process, if needed */
       
   215     {
       
   216       char *arch        = (char *)GetArch(); /* like sparc or sparcv9 */
       
   217       char * jvmtype    = NULL;
       
   218       int argc          = *_argcp;
       
   219       char **argv       = original_argv;
       
   220 
       
   221       char *runpath     = NULL; /* existing effective LD_LIBRARY_PATH
       
   222                                    setting */
       
   223 
       
   224       int running       =       /* What data model is being ILP32 =>
       
   225                                    32 bit vm; LP64 => 64 bit vm */
       
   226 #ifdef _LP64
       
   227         64;
       
   228 #else
       
   229       32;
       
   230 #endif
       
   231 
       
   232       int wanted        = running;      /* What data mode is being
       
   233                                            asked for? Current model is
       
   234                                            fine unless another model
       
   235                                            is asked for */
       
   236 
       
   237       char* new_runpath = NULL; /* desired new LD_LIBRARY_PATH string */
       
   238       char* newpath     = NULL; /* path on new LD_LIBRARY_PATH */
       
   239       char* lastslash   = NULL;
       
   240 
       
   241       char** newenvp    = NULL; /* current environment */
       
   242 
       
   243       char** newargv    = NULL;
       
   244       int    newargc    = 0;
       
   245 #ifdef __sun
       
   246       char*  dmpath     = NULL;  /* data model specific LD_LIBRARY_PATH,
       
   247                                     Solaris only */
       
   248 #endif
       
   249 
       
   250       /*
       
   251        * Starting in 1.5, all unix platforms accept the -d32 and -d64
       
   252        * options.  On platforms where only one data-model is supported
       
   253        * (e.g. ia-64 Linux), using the flag for the other data model is
       
   254        * an error and will terminate the program.
       
   255        */
       
   256 
       
   257       { /* open new scope to declare local variables */
       
   258         int i;
       
   259 
       
   260         newargv = (char **)JLI_MemAlloc((argc+1) * sizeof(*newargv));
       
   261         newargv[newargc++] = argv[0];
       
   262 
       
   263         /* scan for data model arguments and remove from argument list;
       
   264            last occurrence determines desired data model */
       
   265         for (i=1; i < argc; i++) {
       
   266 
       
   267           if (strcmp(argv[i], "-J-d64") == 0 || strcmp(argv[i], "-d64") == 0) {
       
   268             wanted = 64;
       
   269             continue;
       
   270           }
       
   271           if (strcmp(argv[i], "-J-d32") == 0 || strcmp(argv[i], "-d32") == 0) {
       
   272             wanted = 32;
       
   273             continue;
       
   274           }
       
   275           newargv[newargc++] = argv[i];
       
   276 
       
   277 #ifdef JAVA_ARGS
       
   278           if (argv[i][0] != '-')
       
   279             continue;
       
   280 #else
       
   281           if (strcmp(argv[i], "-classpath") == 0 || strcmp(argv[i], "-cp") == 0) {
       
   282             i++;
       
   283             if (i >= argc) break;
       
   284             newargv[newargc++] = argv[i];
       
   285             continue;
       
   286           }
       
   287           if (argv[i][0] != '-') { i++; break; }
       
   288 #endif
       
   289         }
       
   290 
       
   291         /* copy rest of args [i .. argc) */
       
   292         while (i < argc) {
       
   293           newargv[newargc++] = argv[i++];
       
   294         }
       
   295         newargv[newargc] = NULL;
       
   296 
       
   297         /*
       
   298          * newargv has all proper arguments here
       
   299          */
       
   300 
       
   301         argc = newargc;
       
   302         argv = newargv;
       
   303       }
       
   304 
       
   305       /* If the data model is not changing, it is an error if the
       
   306          jvmpath does not exist */
       
   307       if (wanted == running) {
       
   308         /* Find out where the JRE is that we will be using. */
       
   309         if (!GetJREPath(jrepath, so_jrepath, arch, JNI_FALSE) ) {
       
   310           fprintf(stderr, "Error: could not find Java 2 Runtime Environment.\n");
       
   311           exit(2);
       
   312         }
       
   313 
       
   314         /* Find the specified JVM type */
       
   315         if (ReadKnownVMs(jrepath, arch, JNI_FALSE) < 1) {
       
   316           fprintf(stderr, "Error: no known VMs. (check for corrupt jvm.cfg file)\n");
       
   317           exit(1);
       
   318         }
       
   319 
       
   320         jvmpath[0] = '\0';
       
   321         jvmtype = CheckJvmType(_argcp, _argvp, JNI_FALSE);
       
   322 
       
   323         if (!GetJVMPath(jrepath, jvmtype, jvmpath, so_jvmpath, arch )) {
       
   324           fprintf(stderr, "Error: no `%s' JVM at `%s'.\n", jvmtype, jvmpath);
       
   325           exit(4);
       
   326         }
       
   327       } else {  /* do the same speculatively or exit */
       
   328 #ifdef DUAL_MODE
       
   329         if (running != wanted) {
       
   330           /* Find out where the JRE is that we will be using. */
       
   331           if (!GetJREPath(jrepath, so_jrepath, ((wanted==64)?LIBARCH64NAME:LIBARCH32NAME), JNI_TRUE)) {
       
   332             goto EndDataModelSpeculate;
       
   333           }
       
   334 
       
   335           /*
       
   336            * Read in jvm.cfg for target data model and process vm
       
   337            * selection options.
       
   338            */
       
   339           if (ReadKnownVMs(jrepath, ((wanted==64)?LIBARCH64NAME:LIBARCH32NAME), JNI_TRUE) < 1) {
       
   340             goto EndDataModelSpeculate;
       
   341           }
       
   342           jvmpath[0] = '\0';
       
   343           jvmtype = CheckJvmType(_argcp, _argvp, JNI_TRUE);
       
   344           /* exec child can do error checking on the existence of the path */
       
   345           jvmpathExists = GetJVMPath(jrepath, jvmtype, jvmpath, so_jvmpath,
       
   346                                      ((wanted==64)?LIBARCH64NAME:LIBARCH32NAME));
       
   347 
       
   348         }
       
   349       EndDataModelSpeculate: /* give up and let other code report error message */
       
   350         ;
       
   351 #else
       
   352         fprintf(stderr, "Running a %d-bit JVM is not supported on this platform.\n", wanted);
       
   353         exit(1);
       
   354 #endif
       
   355       }
       
   356 
       
   357       /*
       
   358        * We will set the LD_LIBRARY_PATH as follows:
       
   359        *
       
   360        *     o          $JVMPATH (directory portion only)
       
   361        *     o          $JRE/lib/$LIBARCHNAME
       
   362        *     o          $JRE/../lib/$LIBARCHNAME
       
   363        *
       
   364        * followed by the user's previous effective LD_LIBRARY_PATH, if
       
   365        * any.
       
   366        */
       
   367 
       
   368 #ifdef __sun
       
   369       /*
       
   370        * Starting in Solaris 7, ld.so.1 supports three LD_LIBRARY_PATH
       
   371        * variables:
       
   372        *
       
   373        * 1. LD_LIBRARY_PATH -- used for 32 and 64 bit searches if
       
   374        * data-model specific variables are not set.
       
   375        *
       
   376        * 2. LD_LIBRARY_PATH_64 -- overrides and replaces LD_LIBRARY_PATH
       
   377        * for 64-bit binaries.
       
   378        *
       
   379        * 3. LD_LIBRARY_PATH_32 -- overrides and replaces LD_LIBRARY_PATH
       
   380        * for 32-bit binaries.
       
   381        *
       
   382        * The vm uses LD_LIBRARY_PATH to set the java.library.path system
       
   383        * property.  To shield the vm from the complication of multiple
       
   384        * LD_LIBRARY_PATH variables, if the appropriate data model
       
   385        * specific variable is set, we will act as if LD_LIBRARY_PATH had
       
   386        * the value of the data model specific variant and the data model
       
   387        * specific variant will be unset.  Note that the variable for the
       
   388        * *wanted* data model must be used (if it is set), not simply the
       
   389        * current running data model.
       
   390        */
       
   391 
       
   392       switch(wanted) {
       
   393       case 0:
       
   394         if(running == 32) {
       
   395           dmpath = getenv("LD_LIBRARY_PATH_32");
       
   396           wanted = 32;
       
   397         }
       
   398         else {
       
   399           dmpath = getenv("LD_LIBRARY_PATH_64");
       
   400           wanted = 64;
       
   401         }
       
   402         break;
       
   403 
       
   404       case 32:
       
   405         dmpath = getenv("LD_LIBRARY_PATH_32");
       
   406         break;
       
   407 
       
   408       case 64:
       
   409         dmpath = getenv("LD_LIBRARY_PATH_64");
       
   410         break;
       
   411 
       
   412       default:
       
   413         fprintf(stderr, "Improper value at line %d.", __LINE__);
       
   414         exit(1); /* unknown value in wanted */
       
   415         break;
       
   416       }
       
   417 
       
   418       /*
       
   419        * If dmpath is NULL, the relevant data model specific variable is
       
   420        * not set and normal LD_LIBRARY_PATH should be used.
       
   421        */
       
   422       if( dmpath == NULL) {
       
   423         runpath = getenv("LD_LIBRARY_PATH");
       
   424       }
       
   425       else {
       
   426         runpath = dmpath;
       
   427       }
       
   428 #else
       
   429       /*
       
   430        * If not on Solaris, assume only a single LD_LIBRARY_PATH
       
   431        * variable.
       
   432        */
       
   433       runpath = getenv(LD_LIBRARY_PATH);
       
   434 #endif /* __sun */
       
   435 
       
   436 #if defined(__linux__)
       
   437       /*
       
   438        * On linux, if a binary is running as sgid or suid, glibc sets
       
   439        * LD_LIBRARY_PATH to the empty string for security purposes.  (In
       
   440        * contrast, on Solaris the LD_LIBRARY_PATH variable for a
       
   441        * privileged binary does not lose its settings; but the dynamic
       
   442        * linker does apply more scrutiny to the path.) The launcher uses
       
   443        * the value of LD_LIBRARY_PATH to prevent an exec loop.
       
   444        * Therefore, if we are running sgid or suid, this function's
       
   445        * setting of LD_LIBRARY_PATH will be ineffective and we should
       
   446        * return from the function now.  Getting the right libraries to
       
   447        * be found must be handled through other mechanisms.
       
   448        */
       
   449       if((getgid() != getegid()) || (getuid() != geteuid()) ) {
       
   450         return;
       
   451       }
       
   452 #elif defined(_ALLBSD_SOURCE)
       
   453       /*
       
   454        * On BSD, if a binary is running as sgid or suid, libc sets
       
   455        * LD_LIBRARY_PATH to the empty string for security purposes.  (In
       
   456        * contrast, on Solaris the LD_LIBRARY_PATH variable for a
       
   457        * privileged binary does not lose its settings; but the dynamic
       
   458        * linker does apply more scrutiny to the path.) The launcher uses
       
   459        * the value of LD_LIBRARY_PATH to prevent an exec loop.
       
   460        * Therefore, if we are running sgid or suid, this function's
       
   461        * setting of LD_LIBRARY_PATH will be ineffective and we should
       
   462        * return from the function now.  Getting the right libraries to
       
   463        * be found must be handled through other mechanisms.
       
   464        */
       
   465       if(issetugid()) {
       
   466         return;
       
   467       }
       
   468 #endif
       
   469 
       
   470       /* runpath contains current effective LD_LIBRARY_PATH setting */
       
   471 
       
   472       jvmpath = JLI_StringDup(jvmpath);
       
   473       new_runpath = JLI_MemAlloc( ((runpath!=NULL)?strlen(runpath):0) +
       
   474                               2*strlen(jrepath) + 2*strlen(arch) +
       
   475                               strlen(jvmpath) + 52);
       
   476       newpath = new_runpath + strlen(LD_LIBRARY_PATH "=");
       
   477 
       
   478 
       
   479       /*
       
   480        * Create desired LD_LIBRARY_PATH value for target data model.
       
   481        */
       
   482       {
       
   483         /* remove the name of the .so from the JVM path */
       
   484         lastslash = strrchr(jvmpath, '/');
       
   485         if (lastslash)
       
   486           *lastslash = '\0';
       
   487 
       
   488 
       
   489         /* jvmpath, ((running != wanted)?((wanted==64)?"/"LIBARCH64NAME:"/.."):""), */
       
   490 
       
   491         sprintf(new_runpath, LD_LIBRARY_PATH "="
       
   492                 "%s:"
       
   493                 "%s/lib/%s:"
       
   494                 "%s/../lib/%s",
       
   495                 jvmpath,
       
   496 #ifdef DUAL_MODE
       
   497                 jrepath, ((wanted==64)?LIBARCH64NAME:LIBARCH32NAME),
       
   498                 jrepath, ((wanted==64)?LIBARCH64NAME:LIBARCH32NAME)
       
   499 #else
       
   500                 jrepath, arch,
       
   501                 jrepath, arch
       
   502 #endif
       
   503                 );
       
   504 
       
   505 
       
   506         /*
       
   507          * Check to make sure that the prefix of the current path is the
       
   508          * desired environment variable setting.
       
   509          */
       
   510         if (runpath != NULL &&
       
   511             strncmp(newpath, runpath, strlen(newpath))==0 &&
       
   512             (runpath[strlen(newpath)] == 0 || runpath[strlen(newpath)] == ':') &&
       
   513             (running == wanted) /* data model does not have to be changed */
       
   514 #ifdef __sun
       
   515             && (dmpath == NULL)    /* data model specific variables not set  */
       
   516 #endif
       
   517             ) {
       
   518 
       
   519           return;
       
   520 
       
   521         }
       
   522       }
       
   523 
       
   524       /*
       
   525        * Place the desired environment setting onto the prefix of
       
   526        * LD_LIBRARY_PATH.  Note that this prevents any possible infinite
       
   527        * loop of execv() because we test for the prefix, above.
       
   528        */
       
   529       if (runpath != 0) {
       
   530         strcat(new_runpath, ":");
       
   531         strcat(new_runpath, runpath);
       
   532       }
       
   533 
       
   534       if( putenv(new_runpath) != 0) {
       
   535         exit(1); /* problem allocating memory; LD_LIBRARY_PATH not set
       
   536                     properly */
       
   537       }
       
   538 
       
   539       /*
       
   540        * Unix systems document that they look at LD_LIBRARY_PATH only
       
   541        * once at startup, so we have to re-exec the current executable
       
   542        * to get the changed environment variable to have an effect.
       
   543        */
       
   544 
       
   545 #ifdef __sun
       
   546       /*
       
   547        * If dmpath is not NULL, remove the data model specific string
       
   548        * in the environment for the exec'ed child.
       
   549        */
       
   550 
       
   551       if( dmpath != NULL)
       
   552         (void)UnsetEnv((wanted==32)?"LD_LIBRARY_PATH_32":"LD_LIBRARY_PATH_64");
       
   553 #endif
       
   554 
       
   555       newenvp = environ;
       
   556 
       
   557       {
       
   558         char *newexec = execname;
       
   559 #ifdef DUAL_MODE
       
   560         /*
       
   561          * If the data model is being changed, the path to the
       
   562          * executable must be updated accordingly; the executable name
       
   563          * and directory the executable resides in are separate.  In the
       
   564          * case of 32 => 64, the new bits are assumed to reside in, e.g.
       
   565          * "olddir/LIBARCH64NAME/execname"; in the case of 64 => 32,
       
   566          * the bits are assumed to be in "olddir/../execname".  For example,
       
   567          *
       
   568          * olddir/sparcv9/execname
       
   569          * olddir/amd64/execname
       
   570          *
       
   571          * for Solaris SPARC and Linux amd64, respectively.
       
   572          */
       
   573 
       
   574         if (running != wanted) {
       
   575           char *oldexec = strcpy(JLI_MemAlloc(strlen(execname) + 1), execname);
       
   576           char *olddir = oldexec;
       
   577           char *oldbase = strrchr(oldexec, '/');
       
   578 
       
   579 
       
   580           newexec = JLI_MemAlloc(strlen(execname) + 20);
       
   581           *oldbase++ = 0;
       
   582           sprintf(newexec, "%s/%s/%s", olddir,
       
   583                   ((wanted==64) ? LIBARCH64NAME : ".."), oldbase);
       
   584           argv[0] = newexec;
       
   585         }
       
   586 #endif
       
   587 
       
   588         (void)fflush(stdout);
       
   589         (void)fflush(stderr);
       
   590         execve(newexec, argv, newenvp);
       
   591         perror("execve()");
       
   592 
       
   593         fprintf(stderr, "Error trying to exec %s.\n", newexec);
       
   594         fprintf(stderr, "Check if file exists and permissions are set correctly.\n");
       
   595 
       
   596 #ifdef DUAL_MODE
       
   597         if (running != wanted) {
       
   598           fprintf(stderr, "Failed to start a %d-bit JVM process from a %d-bit JVM.\n",
       
   599                   wanted, running);
       
   600 #  ifdef __sun
       
   601 
       
   602 #    ifdef __sparc
       
   603           fprintf(stderr, "Verify all necessary J2SE components have been installed.\n" );
       
   604           fprintf(stderr,
       
   605                   "(Solaris SPARC 64-bit components must be installed after 32-bit components.)\n" );
       
   606 #    else
       
   607           fprintf(stderr, "Either 64-bit processes are not supported by this platform\n");
       
   608           fprintf(stderr, "or the 64-bit components have not been installed.\n");
       
   609 #    endif
       
   610         }
       
   611 #  endif
       
   612 #endif
       
   613 
       
   614       }
       
   615 
       
   616       exit(1);
       
   617     }
       
   618 
       
   619 #else  /* ifndef GAMMA */
       
   620 
       
   621   /*
       
   622    * gamma launcher is simpler in that it doesn't handle VM flavors, data
       
   623    * model, LD_LIBRARY_PATH, etc. Assuming everything is set-up correctly
       
   624    * all we need to do here is to return correct path names. See also
       
   625    * GetJVMPath() and GetApplicationHome().
       
   626    */
       
   627 
       
   628   { char *arch = (char *) ARCH; /* like sparc or sparcv9 */
       
   629     char *p;
       
   630 
       
   631     if (!GetJREPath(jrepath, so_jrepath, arch, JNI_FALSE) ) {
       
   632       fprintf(stderr, "Error: could not find Java 2 Runtime Environment.\n");
       
   633       exit(2);
       
   634     }
       
   635 
       
   636     if (!GetJVMPath(jrepath, NULL, jvmpath, so_jvmpath, arch )) {
       
   637       fprintf(stderr, "Error: no JVM at `%s'.\n", jvmpath);
       
   638       exit(4);
       
   639     }
       
   640   }
       
   641 
       
   642 #endif  /* ifndef GAMMA */
       
   643 }
       
   644 
       
   645 
       
   646 /*
       
   647  * On Solaris VM choosing is done by the launcher (java.c).
       
   648  */
       
   649 static jboolean
       
   650 GetJVMPath(const char *jrepath, const char *jvmtype,
       
   651            char *jvmpath, jint jvmpathsize, char * arch)
       
   652 {
       
   653     struct stat s;
       
   654 
       
   655 #ifndef GAMMA
       
   656     if (strchr(jvmtype, '/')) {
       
   657         sprintf(jvmpath, "%s/" JVM_DLL, jvmtype);
       
   658     } else {
       
   659         sprintf(jvmpath, "%s/lib/%s/%s/" JVM_DLL, jrepath, arch, jvmtype);
       
   660     }
       
   661 #else
       
   662     /*
       
   663      * For gamma launcher, JVM is either built-in or in the same directory.
       
   664      * Either way we return "<exe_path>/libjvm.so" where <exe_path> is the
       
   665      * directory where gamma launcher is located.
       
   666      */
       
   667 
       
   668     char *p;
       
   669 
       
   670     snprintf(jvmpath, jvmpathsize, "%s", GetExecname());
       
   671     p = strrchr(jvmpath, '/');
       
   672     if (p) {
       
   673        /* replace executable name with libjvm.so */
       
   674        snprintf(p + 1, jvmpathsize - (p + 1 - jvmpath), "%s", JVM_DLL);
       
   675     } else {
       
   676        /* this case shouldn't happen */
       
   677        snprintf(jvmpath, jvmpathsize, "%s", JVM_DLL);
       
   678     }
       
   679 #endif /* ifndef GAMMA */
       
   680 
       
   681     if (_launcher_debug)
       
   682       printf("Does `%s' exist ... ", jvmpath);
       
   683 
       
   684     if (stat(jvmpath, &s) == 0) {
       
   685         if (_launcher_debug)
       
   686           printf("yes.\n");
       
   687         return JNI_TRUE;
       
   688     } else {
       
   689         if (_launcher_debug)
       
   690           printf("no.\n");
       
   691         return JNI_FALSE;
       
   692     }
       
   693 }
       
   694 
       
   695 /*
       
   696  * Find path to JRE based on .exe's location or registry settings.
       
   697  */
       
   698 static jboolean
       
   699 GetJREPath(char *path, jint pathsize, char * arch, jboolean speculative)
       
   700 {
       
   701     char libjava[MAXPATHLEN];
       
   702 
       
   703     if (GetApplicationHome(path, pathsize)) {
       
   704 
       
   705         /* Is the JRE universal, i.e. no arch dir? */
       
   706         sprintf(libjava, "%s/jre/lib/" JAVA_DLL, path);
       
   707         if (access(libjava, F_OK) == 0) {
       
   708             strcat(path, "/jre");
       
   709             goto found;
       
   710         }
       
   711 
       
   712         /* Is JRE co-located with the application? */
       
   713         sprintf(libjava, "%s/lib/%s/" JAVA_DLL, path, arch);
       
   714         if (access(libjava, F_OK) == 0) {
       
   715             goto found;
       
   716         }
       
   717 
       
   718         /* Does the app ship a private JRE in <apphome>/jre directory? */
       
   719         sprintf(libjava, "%s/jre/lib/%s/" JAVA_DLL, path, arch);
       
   720         if (access(libjava, F_OK) == 0) {
       
   721             strcat(path, "/jre");
       
   722             goto found;
       
   723         }
       
   724     }
       
   725 
       
   726     if (!speculative)
       
   727       fprintf(stderr, "Error: could not find " JAVA_DLL "\n");
       
   728     return JNI_FALSE;
       
   729 
       
   730  found:
       
   731     if (_launcher_debug)
       
   732       printf("JRE path is %s\n", path);
       
   733     return JNI_TRUE;
       
   734 }
       
   735 
       
   736 jboolean
       
   737 LoadJavaVM(const char *jvmpath, InvocationFunctions *ifn)
       
   738 {
       
   739 #ifdef GAMMA
       
   740     /* JVM is directly linked with gamma launcher; no dlopen() */
       
   741     ifn->CreateJavaVM = JNI_CreateJavaVM;
       
   742     ifn->GetDefaultJavaVMInitArgs = JNI_GetDefaultJavaVMInitArgs;
       
   743     return JNI_TRUE;
       
   744 #else
       
   745     Dl_info dlinfo;
       
   746     void *libjvm;
       
   747 
       
   748     if (_launcher_debug) {
       
   749         printf("JVM path is %s\n", jvmpath);
       
   750     }
       
   751 
       
   752     libjvm = dlopen(jvmpath, RTLD_NOW + RTLD_GLOBAL);
       
   753     if (libjvm == NULL) {
       
   754 #if defined(__sparc) && !defined(_LP64) /* i.e. 32-bit sparc */
       
   755       FILE * fp;
       
   756       Elf32_Ehdr elf_head;
       
   757       int count;
       
   758       int location;
       
   759 
       
   760       fp = fopen(jvmpath, "r");
       
   761       if(fp == NULL)
       
   762         goto error;
       
   763 
       
   764       /* read in elf header */
       
   765       count = fread((void*)(&elf_head), sizeof(Elf32_Ehdr), 1, fp);
       
   766       fclose(fp);
       
   767       if(count < 1)
       
   768         goto error;
       
   769 
       
   770       /*
       
   771        * Check for running a server vm (compiled with -xarch=v8plus)
       
   772        * on a stock v8 processor.  In this case, the machine type in
       
   773        * the elf header would not be included the architecture list
       
   774        * provided by the isalist command, which is turn is gotten from
       
   775        * sysinfo.  This case cannot occur on 64-bit hardware and thus
       
   776        * does not have to be checked for in binaries with an LP64 data
       
   777        * model.
       
   778        */
       
   779       if(elf_head.e_machine == EM_SPARC32PLUS) {
       
   780         char buf[257];  /* recommended buffer size from sysinfo man
       
   781                            page */
       
   782         long length;
       
   783         char* location;
       
   784 
       
   785         length = sysinfo(SI_ISALIST, buf, 257);
       
   786         if(length > 0) {
       
   787           location = strstr(buf, "sparcv8plus ");
       
   788           if(location == NULL) {
       
   789             fprintf(stderr, "SPARC V8 processor detected; Server compiler requires V9 or better.\n");
       
   790             fprintf(stderr, "Use Client compiler on V8 processors.\n");
       
   791             fprintf(stderr, "Could not create the Java virtual machine.\n");
       
   792             return JNI_FALSE;
       
   793           }
       
   794         }
       
   795       }
       
   796 #endif
       
   797       fprintf(stderr, "dl failure on line %d", __LINE__);
       
   798       goto error;
       
   799     }
       
   800 
       
   801     ifn->CreateJavaVM = (CreateJavaVM_t)
       
   802       dlsym(libjvm, "JNI_CreateJavaVM");
       
   803     if (ifn->CreateJavaVM == NULL)
       
   804         goto error;
       
   805 
       
   806     ifn->GetDefaultJavaVMInitArgs = (GetDefaultJavaVMInitArgs_t)
       
   807         dlsym(libjvm, "JNI_GetDefaultJavaVMInitArgs");
       
   808     if (ifn->GetDefaultJavaVMInitArgs == NULL)
       
   809       goto error;
       
   810 
       
   811     return JNI_TRUE;
       
   812 
       
   813 error:
       
   814     fprintf(stderr, "Error: failed %s, because %s\n", jvmpath, dlerror());
       
   815     return JNI_FALSE;
       
   816 #endif /* ifndef GAMMA */
       
   817 }
       
   818 
       
   819 /*
       
   820  * If app is "/foo/bin/javac", or "/foo/bin/sparcv9/javac" then put
       
   821  * "/foo" into buf.
       
   822  */
       
   823 jboolean
       
   824 GetApplicationHome(char *buf, jint bufsize)
       
   825 {
       
   826 #if defined(__linux__) || defined(_ALLBSD_SOURCE)
       
   827     char *execname = GetExecname();
       
   828     if (execname) {
       
   829         strncpy(buf, execname, bufsize-1);
       
   830         buf[bufsize-1] = '\0';
       
   831     } else {
       
   832         return JNI_FALSE;
       
   833     }
       
   834 #else
       
   835     Dl_info dlinfo;
       
   836 
       
   837     dladdr((void *)GetApplicationHome, &dlinfo);
       
   838     if (realpath(dlinfo.dli_fname, buf) == NULL) {
       
   839         fprintf(stderr, "Error: realpath(`%s') failed.\n", dlinfo.dli_fname);
       
   840         return JNI_FALSE;
       
   841     }
       
   842 #endif
       
   843 
       
   844 #ifdef GAMMA
       
   845     {
       
   846        /* gamma launcher uses JAVA_HOME environment variable to find JDK/JRE */
       
   847        char* java_home_var = getenv("JAVA_HOME");
       
   848        if (java_home_var == NULL) {
       
   849           printf("JAVA_HOME must point to a valid JDK/JRE to run gamma\n");
       
   850           return JNI_FALSE;
       
   851        }
       
   852        snprintf(buf, bufsize, "%s", java_home_var);
       
   853     }
       
   854 #else
       
   855     if (strrchr(buf, '/') == 0) {
       
   856         buf[0] = '\0';
       
   857         return JNI_FALSE;
       
   858     }
       
   859     *(strrchr(buf, '/')) = '\0';        /* executable file      */
       
   860     if (strlen(buf) < 4 || strrchr(buf, '/') == 0) {
       
   861         buf[0] = '\0';
       
   862         return JNI_FALSE;
       
   863     }
       
   864     if (strcmp("/bin", buf + strlen(buf) - 4) != 0)
       
   865         *(strrchr(buf, '/')) = '\0';    /* sparcv9 or amd64     */
       
   866     if (strlen(buf) < 4 || strcmp("/bin", buf + strlen(buf) - 4) != 0) {
       
   867         buf[0] = '\0';
       
   868         return JNI_FALSE;
       
   869     }
       
   870     *(strrchr(buf, '/')) = '\0';        /* bin                  */
       
   871 #endif /* ifndef GAMMA */
       
   872 
       
   873     return JNI_TRUE;
       
   874 }
       
   875 
       
   876 
       
   877 /*
       
   878  * Return true if the named program exists
       
   879  */
       
   880 static int
       
   881 ProgramExists(char *name)
       
   882 {
       
   883     struct stat sb;
       
   884     if (stat(name, &sb) != 0) return 0;
       
   885     if (S_ISDIR(sb.st_mode)) return 0;
       
   886     return (sb.st_mode & S_IEXEC) != 0;
       
   887 }
       
   888 
       
   889 
       
   890 /*
       
   891  * Find a command in a directory, returning the path.
       
   892  */
       
   893 static char *
       
   894 Resolve(char *indir, char *cmd)
       
   895 {
       
   896     char name[PATH_MAX + 2], *real;
       
   897 
       
   898     if ((strlen(indir) + strlen(cmd) + 1)  > PATH_MAX) return 0;
       
   899     sprintf(name, "%s%c%s", indir, FILE_SEPARATOR, cmd);
       
   900     if (!ProgramExists(name)) return 0;
       
   901     real = JLI_MemAlloc(PATH_MAX + 2);
       
   902     if (!realpath(name, real))
       
   903         strcpy(real, name);
       
   904     return real;
       
   905 }
       
   906 
       
   907 
       
   908 /*
       
   909  * Find a path for the executable
       
   910  */
       
   911 static char *
       
   912 FindExecName(char *program)
       
   913 {
       
   914     char cwdbuf[PATH_MAX+2];
       
   915     char *path;
       
   916     char *tmp_path;
       
   917     char *f;
       
   918     char *result = NULL;
       
   919 
       
   920     /* absolute path? */
       
   921     if (*program == FILE_SEPARATOR ||
       
   922         (FILE_SEPARATOR=='\\' && strrchr(program, ':')))
       
   923         return Resolve("", program+1);
       
   924 
       
   925     /* relative path? */
       
   926     if (strrchr(program, FILE_SEPARATOR) != 0) {
       
   927         char buf[PATH_MAX+2];
       
   928         return Resolve(getcwd(cwdbuf, sizeof(cwdbuf)), program);
       
   929     }
       
   930 
       
   931     /* from search path? */
       
   932     path = getenv("PATH");
       
   933     if (!path || !*path) path = ".";
       
   934     tmp_path = JLI_MemAlloc(strlen(path) + 2);
       
   935     strcpy(tmp_path, path);
       
   936 
       
   937     for (f=tmp_path; *f && result==0; ) {
       
   938         char *s = f;
       
   939         while (*f && (*f != PATH_SEPARATOR)) ++f;
       
   940         if (*f) *f++ = 0;
       
   941         if (*s == FILE_SEPARATOR)
       
   942             result = Resolve(s, program);
       
   943         else {
       
   944             /* relative path element */
       
   945             char dir[2*PATH_MAX];
       
   946             sprintf(dir, "%s%c%s", getcwd(cwdbuf, sizeof(cwdbuf)),
       
   947                     FILE_SEPARATOR, s);
       
   948             result = Resolve(dir, program);
       
   949         }
       
   950         if (result != 0) break;
       
   951     }
       
   952 
       
   953     JLI_MemFree(tmp_path);
       
   954     return result;
       
   955 }
       
   956 
       
   957 
       
   958 /* Store the name of the executable once computed */
       
   959 static char *execname = NULL;
       
   960 
       
   961 /*
       
   962  * Compute the name of the executable
       
   963  *
       
   964  * In order to re-exec securely we need the absolute path of the
       
   965  * executable. On Solaris getexecname(3c) may not return an absolute
       
   966  * path so we use dladdr to get the filename of the executable and
       
   967  * then use realpath to derive an absolute path. From Solaris 9
       
   968  * onwards the filename returned in DL_info structure from dladdr is
       
   969  * an absolute pathname so technically realpath isn't required.
       
   970  * On Linux we read the executable name from /proc/self/exe.
       
   971  * As a fallback, and for platforms other than Solaris and Linux,
       
   972  * we use FindExecName to compute the executable name.
       
   973  */
       
   974 static char *
       
   975 SetExecname(char **argv)
       
   976 {
       
   977     char* exec_path = NULL;
       
   978 
       
   979     if (execname != NULL)       /* Already determined */
       
   980         return (execname);
       
   981 
       
   982 #if defined(__sun)
       
   983     {
       
   984         Dl_info dlinfo;
       
   985         if (dladdr((void*)&SetExecname, &dlinfo)) {
       
   986             char *resolved = (char*)JLI_MemAlloc(PATH_MAX+1);
       
   987             if (resolved != NULL) {
       
   988                 exec_path = realpath(dlinfo.dli_fname, resolved);
       
   989                 if (exec_path == NULL) {
       
   990                     JLI_MemFree(resolved);
       
   991                 }
       
   992             }
       
   993         }
       
   994     }
       
   995 #elif defined(__linux__)
       
   996     {
       
   997         const char* self = "/proc/self/exe";
       
   998         char buf[PATH_MAX+1];
       
   999         int len = readlink(self, buf, PATH_MAX);
       
  1000         if (len >= 0) {
       
  1001             buf[len] = '\0';            /* readlink doesn't nul terminate */
       
  1002             exec_path = JLI_StringDup(buf);
       
  1003         }
       
  1004     }
       
  1005 #else /* !__sun && !__linux */
       
  1006     {
       
  1007         /* Not implemented */
       
  1008     }
       
  1009 #endif
       
  1010 
       
  1011     if (exec_path == NULL) {
       
  1012         exec_path = FindExecName(argv[0]);
       
  1013     }
       
  1014     execname = exec_path;
       
  1015     return exec_path;
       
  1016 }
       
  1017 
       
  1018 /*
       
  1019  * Return the name of the executable.  Used in java_md.c to find the JRE area.
       
  1020  */
       
  1021 static char *
       
  1022 GetExecname() {
       
  1023   return execname;
       
  1024 }
       
  1025 
       
  1026 void ReportErrorMessage(char * message, jboolean always) {
       
  1027   if (always) {
       
  1028     fprintf(stderr, "%s\n", message);
       
  1029   }
       
  1030 }
       
  1031 
       
  1032 void ReportErrorMessage2(char * format, char * string, jboolean always) {
       
  1033   if (always) {
       
  1034     fprintf(stderr, format, string);
       
  1035     fprintf(stderr, "\n");
       
  1036   }
       
  1037 }
       
  1038 
       
  1039 void  ReportExceptionDescription(JNIEnv * env) {
       
  1040   (*env)->ExceptionDescribe(env);
       
  1041 }
       
  1042 
       
  1043 /*
       
  1044  * Return JNI_TRUE for an option string that has no effect but should
       
  1045  * _not_ be passed on to the vm; return JNI_FALSE otherwise.  On
       
  1046  * Solaris SPARC, this screening needs to be done if:
       
  1047  * 1) LD_LIBRARY_PATH does _not_ need to be reset and
       
  1048  * 2) -d32 or -d64 is passed to a binary with a matching data model
       
  1049  *    (the exec in SetLibraryPath removes -d<n> options and points the
       
  1050  *    exec to the proper binary).  When this exec is not done, these options
       
  1051  *    would end up getting passed onto the vm.
       
  1052  */
       
  1053 jboolean RemovableMachineDependentOption(char * option) {
       
  1054   /*
       
  1055    * Unconditionally remove both -d32 and -d64 options since only
       
  1056    * the last such options has an effect; e.g.
       
  1057    * java -d32 -d64 -d32 -version
       
  1058    * is equivalent to
       
  1059    * java -d32 -version
       
  1060    */
       
  1061 
       
  1062   if( (strcmp(option, "-d32")  == 0 ) ||
       
  1063       (strcmp(option, "-d64")  == 0 ))
       
  1064     return JNI_TRUE;
       
  1065   else
       
  1066     return JNI_FALSE;
       
  1067 }
       
  1068 
       
  1069 void PrintMachineDependentOptions() {
       
  1070       fprintf(stdout,
       
  1071         "    -d32          use a 32-bit data model if available\n"
       
  1072         "\n"
       
  1073         "    -d64          use a 64-bit data model if available\n");
       
  1074       return;
       
  1075 }
       
  1076 
       
  1077 #ifndef GAMMA
       
  1078 /*
       
  1079  * The following methods (down to ServerClassMachine()) answer
       
  1080  * the question about whether a machine is a "server-class"
       
  1081  * machine.  A server-class machine is loosely defined as one
       
  1082  * with 2 or more processors and 2 gigabytes or more physical
       
  1083  * memory.  The definition of a processor is a physical package,
       
  1084  * not a hyperthreaded chip masquerading as a multi-processor.
       
  1085  * The definition of memory is also somewhat fuzzy, since x86
       
  1086  * machines seem not to report all the memory in their DIMMs, we
       
  1087  * think because of memory mapping of graphics cards, etc.
       
  1088  *
       
  1089  * This code is somewhat more confused with #ifdef's than we'd
       
  1090  * like because this file is used by both Solaris and Linux
       
  1091  * platforms, and so needs to be parameterized for SPARC and
       
  1092  * i586 hardware.  The other Linux platforms (amd64 and ia64)
       
  1093  * don't even ask this question, because they only come with
       
  1094  * server JVMs.  */
       
  1095 
       
  1096 # define KB (1024UL)
       
  1097 # define MB (1024UL * KB)
       
  1098 # define GB (1024UL * MB)
       
  1099 
       
  1100 /* Compute physical memory by asking the OS */
       
  1101 uint64_t
       
  1102 physical_memory(void) {
       
  1103   const uint64_t pages     = (uint64_t) sysconf(_SC_PHYS_PAGES);
       
  1104   const uint64_t page_size = (uint64_t) sysconf(_SC_PAGESIZE);
       
  1105   const uint64_t result    = pages * page_size;
       
  1106 # define UINT64_FORMAT "%" PRIu64
       
  1107 
       
  1108   if (_launcher_debug) {
       
  1109     printf("pages: " UINT64_FORMAT
       
  1110            "  page_size: " UINT64_FORMAT
       
  1111            "  physical memory: " UINT64_FORMAT " (%.3fGB)\n",
       
  1112            pages, page_size, result, result / (double) GB);
       
  1113   }
       
  1114   return result;
       
  1115 }
       
  1116 
       
  1117 #if defined(__sun) && defined(__sparc)
       
  1118 
       
  1119 /* Methods for solaris-sparc: these are easy. */
       
  1120 
       
  1121 /* Ask the OS how many processors there are. */
       
  1122 unsigned long
       
  1123 physical_processors(void) {
       
  1124   const unsigned long sys_processors = sysconf(_SC_NPROCESSORS_CONF);
       
  1125 
       
  1126   if (_launcher_debug) {
       
  1127     printf("sysconf(_SC_NPROCESSORS_CONF): %lu\n", sys_processors);
       
  1128   }
       
  1129   return sys_processors;
       
  1130 }
       
  1131 
       
  1132 /* The solaris-sparc version of the "server-class" predicate. */
       
  1133 jboolean
       
  1134 solaris_sparc_ServerClassMachine(void) {
       
  1135   jboolean            result            = JNI_FALSE;
       
  1136   /* How big is a server class machine? */
       
  1137   const unsigned long server_processors = 2UL;
       
  1138   const uint64_t      server_memory     = 2UL * GB;
       
  1139   const uint64_t      actual_memory     = physical_memory();
       
  1140 
       
  1141   /* Is this a server class machine? */
       
  1142   if (actual_memory >= server_memory) {
       
  1143     const unsigned long actual_processors = physical_processors();
       
  1144     if (actual_processors >= server_processors) {
       
  1145       result = JNI_TRUE;
       
  1146     }
       
  1147   }
       
  1148   if (_launcher_debug) {
       
  1149     printf("solaris_" LIBARCHNAME "_ServerClassMachine: %s\n",
       
  1150            (result == JNI_TRUE ? "JNI_TRUE" : "JNI_FALSE"));
       
  1151   }
       
  1152   return result;
       
  1153 }
       
  1154 
       
  1155 #endif /* __sun && __sparc */
       
  1156 
       
  1157 #if defined(__sun) && defined(i586)
       
  1158 
       
  1159 /*
       
  1160  * A utility method for asking the CPU about itself.
       
  1161  * There's a corresponding version of linux-i586
       
  1162  * because the compilers are different.
       
  1163  */
       
  1164 void
       
  1165 get_cpuid(uint32_t arg,
       
  1166           uint32_t* eaxp,
       
  1167           uint32_t* ebxp,
       
  1168           uint32_t* ecxp,
       
  1169           uint32_t* edxp) {
       
  1170 #ifdef _LP64
       
  1171   asm(
       
  1172   /* rbx is a callee-saved register */
       
  1173       " movq    %rbx, %r11  \n"
       
  1174   /* rdx and rcx are 3rd and 4th argument registers */
       
  1175       " movq    %rdx, %r10  \n"
       
  1176       " movq    %rcx, %r9   \n"
       
  1177       " movl    %edi, %eax  \n"
       
  1178       " cpuid               \n"
       
  1179       " movl    %eax, (%rsi)\n"
       
  1180       " movl    %ebx, (%r10)\n"
       
  1181       " movl    %ecx, (%r9) \n"
       
  1182       " movl    %edx, (%r8) \n"
       
  1183   /* Restore rbx */
       
  1184       " movq    %r11, %rbx");
       
  1185 #else
       
  1186   /* EBX is a callee-saved register */
       
  1187   asm(" pushl   %ebx");
       
  1188   /* Need ESI for storing through arguments */
       
  1189   asm(" pushl   %esi");
       
  1190   asm(" movl    8(%ebp), %eax   \n"
       
  1191       " cpuid                   \n"
       
  1192       " movl    12(%ebp), %esi  \n"
       
  1193       " movl    %eax, (%esi)    \n"
       
  1194       " movl    16(%ebp), %esi  \n"
       
  1195       " movl    %ebx, (%esi)    \n"
       
  1196       " movl    20(%ebp), %esi  \n"
       
  1197       " movl    %ecx, (%esi)    \n"
       
  1198       " movl    24(%ebp), %esi  \n"
       
  1199       " movl    %edx, (%esi)      ");
       
  1200   /* Restore ESI and EBX */
       
  1201   asm(" popl    %esi");
       
  1202   /* Restore EBX */
       
  1203   asm(" popl    %ebx");
       
  1204 #endif
       
  1205 }
       
  1206 
       
  1207 #endif /* __sun && i586 */
       
  1208 
       
  1209 #if (defined(__linux__) || defined(_ALLBSD_SOURCE)) && defined(i586)
       
  1210 
       
  1211 /*
       
  1212  * A utility method for asking the CPU about itself.
       
  1213  * There's a corresponding version of solaris-i586
       
  1214  * because the compilers are different.
       
  1215  */
       
  1216 void
       
  1217 get_cpuid(uint32_t arg,
       
  1218           uint32_t* eaxp,
       
  1219           uint32_t* ebxp,
       
  1220           uint32_t* ecxp,
       
  1221           uint32_t* edxp) {
       
  1222 #ifdef _LP64
       
  1223   __asm__ volatile (/* Instructions */
       
  1224                     "   movl    %4, %%eax  \n"
       
  1225                     "   cpuid              \n"
       
  1226                     "   movl    %%eax, (%0)\n"
       
  1227                     "   movl    %%ebx, (%1)\n"
       
  1228                     "   movl    %%ecx, (%2)\n"
       
  1229                     "   movl    %%edx, (%3)\n"
       
  1230                     : /* Outputs */
       
  1231                     : /* Inputs */
       
  1232                     "r" (eaxp),
       
  1233                     "r" (ebxp),
       
  1234                     "r" (ecxp),
       
  1235                     "r" (edxp),
       
  1236                     "r" (arg)
       
  1237                     : /* Clobbers */
       
  1238                     "%rax", "%rbx", "%rcx", "%rdx", "memory"
       
  1239                     );
       
  1240 #else
       
  1241   uint32_t value_of_eax = 0;
       
  1242   uint32_t value_of_ebx = 0;
       
  1243   uint32_t value_of_ecx = 0;
       
  1244   uint32_t value_of_edx = 0;
       
  1245   __asm__ volatile (/* Instructions */
       
  1246                         /* ebx is callee-save, so push it */
       
  1247                     "   pushl   %%ebx      \n"
       
  1248                     "   movl    %4, %%eax  \n"
       
  1249                     "   cpuid              \n"
       
  1250                     "   movl    %%eax, %0  \n"
       
  1251                     "   movl    %%ebx, %1  \n"
       
  1252                     "   movl    %%ecx, %2  \n"
       
  1253                     "   movl    %%edx, %3  \n"
       
  1254                         /* restore ebx */
       
  1255                     "   popl    %%ebx      \n"
       
  1256 
       
  1257                     : /* Outputs */
       
  1258                     "=m" (value_of_eax),
       
  1259                     "=m" (value_of_ebx),
       
  1260                     "=m" (value_of_ecx),
       
  1261                     "=m" (value_of_edx)
       
  1262                     : /* Inputs */
       
  1263                     "m" (arg)
       
  1264                     : /* Clobbers */
       
  1265                     "%eax", "%ecx", "%edx"
       
  1266                     );
       
  1267   *eaxp = value_of_eax;
       
  1268   *ebxp = value_of_ebx;
       
  1269   *ecxp = value_of_ecx;
       
  1270   *edxp = value_of_edx;
       
  1271 #endif
       
  1272 }
       
  1273 
       
  1274 #endif /* __linux__ && i586 */
       
  1275 
       
  1276 #ifdef i586
       
  1277 /*
       
  1278  * Routines shared by solaris-i586 and linux-i586.
       
  1279  */
       
  1280 
       
  1281 enum HyperThreadingSupport_enum {
       
  1282   hts_supported        =  1,
       
  1283   hts_too_soon_to_tell =  0,
       
  1284   hts_not_supported    = -1,
       
  1285   hts_not_pentium4     = -2,
       
  1286   hts_not_intel        = -3
       
  1287 };
       
  1288 typedef enum HyperThreadingSupport_enum HyperThreadingSupport;
       
  1289 
       
  1290 /* Determine if hyperthreading is supported */
       
  1291 HyperThreadingSupport
       
  1292 hyperthreading_support(void) {
       
  1293   HyperThreadingSupport result = hts_too_soon_to_tell;
       
  1294   /* Bits 11 through 8 is family processor id */
       
  1295 # define FAMILY_ID_SHIFT 8
       
  1296 # define FAMILY_ID_MASK 0xf
       
  1297   /* Bits 23 through 20 is extended family processor id */
       
  1298 # define EXT_FAMILY_ID_SHIFT 20
       
  1299 # define EXT_FAMILY_ID_MASK 0xf
       
  1300   /* Pentium 4 family processor id */
       
  1301 # define PENTIUM4_FAMILY_ID 0xf
       
  1302   /* Bit 28 indicates Hyper-Threading Technology support */
       
  1303 # define HT_BIT_SHIFT 28
       
  1304 # define HT_BIT_MASK 1
       
  1305   uint32_t vendor_id[3] = { 0U, 0U, 0U };
       
  1306   uint32_t value_of_eax = 0U;
       
  1307   uint32_t value_of_edx = 0U;
       
  1308   uint32_t dummy        = 0U;
       
  1309 
       
  1310   /* Yes, this is supposed to be [0], [2], [1] */
       
  1311   get_cpuid(0, &dummy, &vendor_id[0], &vendor_id[2], &vendor_id[1]);
       
  1312   if (_launcher_debug) {
       
  1313     printf("vendor: %c %c %c %c %c %c %c %c %c %c %c %c \n",
       
  1314            ((vendor_id[0] >>  0) & 0xff),
       
  1315            ((vendor_id[0] >>  8) & 0xff),
       
  1316            ((vendor_id[0] >> 16) & 0xff),
       
  1317            ((vendor_id[0] >> 24) & 0xff),
       
  1318            ((vendor_id[1] >>  0) & 0xff),
       
  1319            ((vendor_id[1] >>  8) & 0xff),
       
  1320            ((vendor_id[1] >> 16) & 0xff),
       
  1321            ((vendor_id[1] >> 24) & 0xff),
       
  1322            ((vendor_id[2] >>  0) & 0xff),
       
  1323            ((vendor_id[2] >>  8) & 0xff),
       
  1324            ((vendor_id[2] >> 16) & 0xff),
       
  1325            ((vendor_id[2] >> 24) & 0xff));
       
  1326   }
       
  1327   get_cpuid(1, &value_of_eax, &dummy, &dummy, &value_of_edx);
       
  1328   if (_launcher_debug) {
       
  1329     printf("value_of_eax: 0x%x  value_of_edx: 0x%x\n",
       
  1330            value_of_eax, value_of_edx);
       
  1331   }
       
  1332   if ((((value_of_eax >> FAMILY_ID_SHIFT) & FAMILY_ID_MASK) == PENTIUM4_FAMILY_ID) ||
       
  1333       (((value_of_eax >> EXT_FAMILY_ID_SHIFT) & EXT_FAMILY_ID_MASK) != 0)) {
       
  1334     if ((((vendor_id[0] >>  0) & 0xff) == 'G') &&
       
  1335         (((vendor_id[0] >>  8) & 0xff) == 'e') &&
       
  1336         (((vendor_id[0] >> 16) & 0xff) == 'n') &&
       
  1337         (((vendor_id[0] >> 24) & 0xff) == 'u') &&
       
  1338         (((vendor_id[1] >>  0) & 0xff) == 'i') &&
       
  1339         (((vendor_id[1] >>  8) & 0xff) == 'n') &&
       
  1340         (((vendor_id[1] >> 16) & 0xff) == 'e') &&
       
  1341         (((vendor_id[1] >> 24) & 0xff) == 'I') &&
       
  1342         (((vendor_id[2] >>  0) & 0xff) == 'n') &&
       
  1343         (((vendor_id[2] >>  8) & 0xff) == 't') &&
       
  1344         (((vendor_id[2] >> 16) & 0xff) == 'e') &&
       
  1345         (((vendor_id[2] >> 24) & 0xff) == 'l')) {
       
  1346       if (((value_of_edx >> HT_BIT_SHIFT) & HT_BIT_MASK) == HT_BIT_MASK) {
       
  1347         if (_launcher_debug) {
       
  1348           printf("Hyperthreading supported\n");
       
  1349         }
       
  1350         result = hts_supported;
       
  1351       } else {
       
  1352         if (_launcher_debug) {
       
  1353           printf("Hyperthreading not supported\n");
       
  1354         }
       
  1355         result = hts_not_supported;
       
  1356       }
       
  1357     } else {
       
  1358       if (_launcher_debug) {
       
  1359         printf("Not GenuineIntel\n");
       
  1360       }
       
  1361       result = hts_not_intel;
       
  1362     }
       
  1363   } else {
       
  1364     if (_launcher_debug) {
       
  1365       printf("not Pentium 4 or extended\n");
       
  1366     }
       
  1367     result = hts_not_pentium4;
       
  1368   }
       
  1369   return result;
       
  1370 }
       
  1371 
       
  1372 /* Determine how many logical processors there are per CPU */
       
  1373 unsigned int
       
  1374 logical_processors_per_package(void) {
       
  1375   /*
       
  1376    * After CPUID with EAX==1, register EBX bits 23 through 16
       
  1377    * indicate the number of logical processors per package
       
  1378    */
       
  1379 # define NUM_LOGICAL_SHIFT 16
       
  1380 # define NUM_LOGICAL_MASK 0xff
       
  1381   unsigned int result                        = 1U;
       
  1382   const HyperThreadingSupport hyperthreading = hyperthreading_support();
       
  1383 
       
  1384   if (hyperthreading == hts_supported) {
       
  1385     uint32_t value_of_ebx = 0U;
       
  1386     uint32_t dummy        = 0U;
       
  1387 
       
  1388     get_cpuid(1, &dummy, &value_of_ebx, &dummy, &dummy);
       
  1389     result = (value_of_ebx >> NUM_LOGICAL_SHIFT) & NUM_LOGICAL_MASK;
       
  1390     if (_launcher_debug) {
       
  1391       printf("logical processors per package: %u\n", result);
       
  1392     }
       
  1393   }
       
  1394   return result;
       
  1395 }
       
  1396 
       
  1397 /* Compute the number of physical processors, not logical processors */
       
  1398 unsigned long
       
  1399 physical_processors(void) {
       
  1400   const long sys_processors = sysconf(_SC_NPROCESSORS_CONF);
       
  1401   unsigned long result      = sys_processors;
       
  1402 
       
  1403   if (_launcher_debug) {
       
  1404     printf("sysconf(_SC_NPROCESSORS_CONF): %lu\n", sys_processors);
       
  1405   }
       
  1406   if (sys_processors > 1) {
       
  1407     unsigned int logical_processors = logical_processors_per_package();
       
  1408     if (logical_processors > 1) {
       
  1409       result = (unsigned long) sys_processors / logical_processors;
       
  1410     }
       
  1411   }
       
  1412   if (_launcher_debug) {
       
  1413     printf("physical processors: %lu\n", result);
       
  1414   }
       
  1415   return result;
       
  1416 }
       
  1417 
       
  1418 #endif /* i586 */
       
  1419 
       
  1420 #if defined(__sun) && defined(i586)
       
  1421 
       
  1422 /* The definition of a server-class machine for solaris-i586/amd64 */
       
  1423 jboolean
       
  1424 solaris_i586_ServerClassMachine(void) {
       
  1425   jboolean            result            = JNI_FALSE;
       
  1426   /* How big is a server class machine? */
       
  1427   const unsigned long server_processors = 2UL;
       
  1428   const uint64_t      server_memory     = 2UL * GB;
       
  1429   /*
       
  1430    * We seem not to get our full complement of memory.
       
  1431    *     We allow some part (1/8?) of the memory to be "missing",
       
  1432    *     based on the sizes of DIMMs, and maybe graphics cards.
       
  1433    */
       
  1434   const uint64_t      missing_memory    = 256UL * MB;
       
  1435   const uint64_t      actual_memory     = physical_memory();
       
  1436 
       
  1437   /* Is this a server class machine? */
       
  1438   if (actual_memory >= (server_memory - missing_memory)) {
       
  1439     const unsigned long actual_processors = physical_processors();
       
  1440     if (actual_processors >= server_processors) {
       
  1441       result = JNI_TRUE;
       
  1442     }
       
  1443   }
       
  1444   if (_launcher_debug) {
       
  1445     printf("solaris_" LIBARCHNAME "_ServerClassMachine: %s\n",
       
  1446            (result == JNI_TRUE ? "true" : "false"));
       
  1447   }
       
  1448   return result;
       
  1449 }
       
  1450 
       
  1451 #endif /* __sun && i586 */
       
  1452 
       
  1453 #if defined(__linux__) && defined(i586)
       
  1454 
       
  1455 /* The definition of a server-class machine for linux-i586 */
       
  1456 jboolean
       
  1457 linux_i586_ServerClassMachine(void) {
       
  1458   jboolean            result            = JNI_FALSE;
       
  1459   /* How big is a server class machine? */
       
  1460   const unsigned long server_processors = 2UL;
       
  1461   const uint64_t      server_memory     = 2UL * GB;
       
  1462   /*
       
  1463    * We seem not to get our full complement of memory.
       
  1464    *     We allow some part (1/8?) of the memory to be "missing",
       
  1465    *     based on the sizes of DIMMs, and maybe graphics cards.
       
  1466    */
       
  1467   const uint64_t      missing_memory    = 256UL * MB;
       
  1468   const uint64_t      actual_memory     = physical_memory();
       
  1469 
       
  1470   /* Is this a server class machine? */
       
  1471   if (actual_memory >= (server_memory - missing_memory)) {
       
  1472     const unsigned long actual_processors = physical_processors();
       
  1473     if (actual_processors >= server_processors) {
       
  1474       result = JNI_TRUE;
       
  1475     }
       
  1476   }
       
  1477   if (_launcher_debug) {
       
  1478     printf("linux_" LIBARCHNAME "_ServerClassMachine: %s\n",
       
  1479            (result == JNI_TRUE ? "true" : "false"));
       
  1480   }
       
  1481   return result;
       
  1482 }
       
  1483 
       
  1484 #endif /* __linux__ && i586 */
       
  1485 
       
  1486 #if defined(_ALLBSD_SOURCE) && defined(i586)
       
  1487 
       
  1488 /* The definition of a server-class machine for bsd-i586 */
       
  1489 jboolean
       
  1490 bsd_i586_ServerClassMachine(void) {
       
  1491   jboolean            result            = JNI_FALSE;
       
  1492   /* How big is a server class machine? */
       
  1493   const unsigned long server_processors = 2UL;
       
  1494   const uint64_t      server_memory     = 2UL * GB;
       
  1495   /*
       
  1496    * We seem not to get our full complement of memory.
       
  1497    *     We allow some part (1/8?) of the memory to be "missing",
       
  1498    *     based on the sizes of DIMMs, and maybe graphics cards.
       
  1499    */
       
  1500   const uint64_t      missing_memory    = 256UL * MB;
       
  1501   const uint64_t      actual_memory     = physical_memory();
       
  1502 
       
  1503   /* Is this a server class machine? */
       
  1504   if (actual_memory >= (server_memory - missing_memory)) {
       
  1505     const unsigned long actual_processors = physical_processors();
       
  1506     if (actual_processors >= server_processors) {
       
  1507       result = JNI_TRUE;
       
  1508     }
       
  1509   }
       
  1510   if (_launcher_debug) {
       
  1511     printf("linux_" LIBARCHNAME "_ServerClassMachine: %s\n",
       
  1512            (result == JNI_TRUE ? "true" : "false"));
       
  1513   }
       
  1514   return result;
       
  1515 }
       
  1516 
       
  1517 #endif /* _ALLBSD_SOURCE && i586 */
       
  1518 
       
  1519 /* Dispatch to the platform-specific definition of "server-class" */
       
  1520 jboolean
       
  1521 ServerClassMachine(void) {
       
  1522   jboolean result = JNI_FALSE;
       
  1523 #if   defined(NEVER_ACT_AS_SERVER_CLASS_MACHINE)
       
  1524   result = JNI_FALSE;
       
  1525 #elif defined(ALWAYS_ACT_AS_SERVER_CLASS_MACHINE)
       
  1526   result = JNI_TRUE;
       
  1527 #elif defined(__sun) && defined(__sparc)
       
  1528   result = solaris_sparc_ServerClassMachine();
       
  1529 #elif defined(__sun) && defined(i586)
       
  1530   result = solaris_i586_ServerClassMachine();
       
  1531 #elif defined(__linux__) && defined(i586)
       
  1532   result = linux_i586_ServerClassMachine();
       
  1533 #elif defined(_ALLBSD_SOURCE) && defined(i586)
       
  1534   result = bsd_i586_ServerClassMachine();
       
  1535 #else
       
  1536   if (_launcher_debug) {
       
  1537     printf("ServerClassMachine: returns default value of %s\n",
       
  1538            (result == JNI_TRUE ? "true" : "false"));
       
  1539   }
       
  1540 #endif
       
  1541   return result;
       
  1542 }
       
  1543 
       
  1544 /*
       
  1545  *      Since using the file system as a registry is a bit risky, perform
       
  1546  *      additional sanity checks on the identified directory to validate
       
  1547  *      it as a valid jre/sdk.
       
  1548  *
       
  1549  *      Return 0 if the tests fail; otherwise return non-zero (true).
       
  1550  *
       
  1551  *      Note that checking for anything more than the existence of an
       
  1552  *      executable object at bin/java relative to the path being checked
       
  1553  *      will break the regression tests.
       
  1554  */
       
  1555 static int
       
  1556 CheckSanity(char *path, char *dir)
       
  1557 {
       
  1558     char    buffer[PATH_MAX];
       
  1559 
       
  1560     if (strlen(path) + strlen(dir) + 11 > PATH_MAX)
       
  1561         return (0);     /* Silently reject "impossibly" long paths */
       
  1562 
       
  1563     (void)strcat(strcat(strcat(strcpy(buffer, path), "/"), dir), "/bin/java");
       
  1564     return ((access(buffer, X_OK) == 0) ? 1 : 0);
       
  1565 }
       
  1566 
       
  1567 /*
       
  1568  *      Determine if there is an acceptable JRE in the directory dirname.
       
  1569  *      Upon locating the "best" one, return a fully qualified path to
       
  1570  *      it. "Best" is defined as the most advanced JRE meeting the
       
  1571  *      constraints contained in the manifest_info. If no JRE in this
       
  1572  *      directory meets the constraints, return NULL.
       
  1573  *
       
  1574  *      Note that we don't check for errors in reading the directory
       
  1575  *      (which would be done by checking errno).  This is because it
       
  1576  *      doesn't matter if we get an error reading the directory, or
       
  1577  *      we just don't find anything interesting in the directory.  We
       
  1578  *      just return NULL in either case.
       
  1579  *
       
  1580  *      The historical names of j2sdk and j2re were changed to jdk and
       
  1581  *      jre respecively as part of the 1.5 rebranding effort.  Since the
       
  1582  *      former names are legacy on Linux, they must be recognized for
       
  1583  *      all time.  Fortunately, this is a minor cost.
       
  1584  */
       
  1585 static char
       
  1586 *ProcessDir(manifest_info *info, char *dirname)
       
  1587 {
       
  1588     DIR     *dirp;
       
  1589     struct dirent *dp;
       
  1590     char    *best = NULL;
       
  1591     int     offset;
       
  1592     int     best_offset = 0;
       
  1593     char    *ret_str = NULL;
       
  1594     char    buffer[PATH_MAX];
       
  1595 
       
  1596     if ((dirp = opendir(dirname)) == NULL)
       
  1597         return (NULL);
       
  1598 
       
  1599     do {
       
  1600         if ((dp = readdir(dirp)) != NULL) {
       
  1601             offset = 0;
       
  1602             if ((strncmp(dp->d_name, "jre", 3) == 0) ||
       
  1603                 (strncmp(dp->d_name, "jdk", 3) == 0))
       
  1604                 offset = 3;
       
  1605             else if (strncmp(dp->d_name, "j2re", 4) == 0)
       
  1606                 offset = 4;
       
  1607             else if (strncmp(dp->d_name, "j2sdk", 5) == 0)
       
  1608                 offset = 5;
       
  1609             if (offset > 0) {
       
  1610                 if ((JLI_AcceptableRelease(dp->d_name + offset,
       
  1611                     info->jre_version)) && CheckSanity(dirname, dp->d_name))
       
  1612                     if ((best == NULL) || (JLI_ExactVersionId(
       
  1613                       dp->d_name + offset, best + best_offset) > 0)) {
       
  1614                         if (best != NULL)
       
  1615                             JLI_MemFree(best);
       
  1616                         best = JLI_StringDup(dp->d_name);
       
  1617                         best_offset = offset;
       
  1618                     }
       
  1619             }
       
  1620         }
       
  1621     } while (dp != NULL);
       
  1622     (void) closedir(dirp);
       
  1623     if (best == NULL)
       
  1624         return (NULL);
       
  1625     else {
       
  1626         ret_str = JLI_MemAlloc(strlen(dirname) + strlen(best) + 2);
       
  1627         ret_str = strcat(strcat(strcpy(ret_str, dirname), "/"), best);
       
  1628         JLI_MemFree(best);
       
  1629         return (ret_str);
       
  1630     }
       
  1631 }
       
  1632 
       
  1633 /*
       
  1634  *      This is the global entry point. It examines the host for the optimal
       
  1635  *      JRE to be used by scanning a set of directories.  The set of directories
       
  1636  *      is platform dependent and can be overridden by the environment
       
  1637  *      variable JAVA_VERSION_PATH.
       
  1638  *
       
  1639  *      This routine itself simply determines the set of appropriate
       
  1640  *      directories before passing control onto ProcessDir().
       
  1641  */
       
  1642 char*
       
  1643 LocateJRE(manifest_info* info)
       
  1644 {
       
  1645     char        *path;
       
  1646     char        *home;
       
  1647     char        *target = NULL;
       
  1648     char        *dp;
       
  1649     char        *cp;
       
  1650 
       
  1651     /*
       
  1652      * Start by getting JAVA_VERSION_PATH
       
  1653      */
       
  1654     if (info->jre_restrict_search)
       
  1655         path = JLI_StringDup(system_dir);
       
  1656     else if ((path = getenv("JAVA_VERSION_PATH")) != NULL)
       
  1657         path = JLI_StringDup(path);
       
  1658     else
       
  1659         if ((home = getenv("HOME")) != NULL) {
       
  1660             path = (char *)JLI_MemAlloc(strlen(home) + strlen(system_dir) +
       
  1661                 strlen(user_dir) + 2);
       
  1662             path = strcat(strcat(strcat(strcpy(path, home),
       
  1663                 user_dir), ":"), system_dir);
       
  1664         } else
       
  1665             path = JLI_StringDup(system_dir);
       
  1666 
       
  1667     /*
       
  1668      * Step through each directory on the path. Terminate the scan with
       
  1669      * the first directory with an acceptable JRE.
       
  1670      */
       
  1671     cp = dp = path;
       
  1672     while (dp != NULL) {
       
  1673         cp = strchr(dp, (int)':');
       
  1674         if (cp != NULL)
       
  1675             *cp = (char)NULL;
       
  1676         if ((target = ProcessDir(info, dp)) != NULL)
       
  1677             break;
       
  1678         dp = cp;
       
  1679         if (dp != NULL)
       
  1680             dp++;
       
  1681     }
       
  1682     JLI_MemFree(path);
       
  1683     return (target);
       
  1684 }
       
  1685 
       
  1686 /*
       
  1687  * Given a path to a jre to execute, this routine checks if this process
       
  1688  * is indeed that jre.  If not, it exec's that jre.
       
  1689  *
       
  1690  * We want to actually check the paths rather than just the version string
       
  1691  * built into the executable, so that given version specification (and
       
  1692  * JAVA_VERSION_PATH) will yield the exact same Java environment, regardless
       
  1693  * of the version of the arbitrary launcher we start with.
       
  1694  */
       
  1695 void
       
  1696 ExecJRE(char *jre, char **argv)
       
  1697 {
       
  1698     char    wanted[PATH_MAX];
       
  1699     char    *execname;
       
  1700     char    *progname;
       
  1701 
       
  1702     /*
       
  1703      * Resolve the real path to the directory containing the selected JRE.
       
  1704      */
       
  1705     if (realpath(jre, wanted) == NULL) {
       
  1706         fprintf(stderr, "Unable to resolve %s\n", jre);
       
  1707         exit(1);
       
  1708     }
       
  1709 
       
  1710     /*
       
  1711      * Resolve the real path to the currently running launcher.
       
  1712      */
       
  1713     execname = SetExecname(argv);
       
  1714     if (execname == NULL) {
       
  1715         fprintf(stderr, "Unable to resolve current executable\n");
       
  1716         exit(1);
       
  1717     }
       
  1718 
       
  1719     /*
       
  1720      * If the path to the selected JRE directory is a match to the initial
       
  1721      * portion of the path to the currently executing JRE, we have a winner!
       
  1722      * If so, just return.
       
  1723      */
       
  1724     if (strncmp(wanted, execname, strlen(wanted)) == 0)
       
  1725         return;                 /* I am the droid you were looking for */
       
  1726 
       
  1727     /*
       
  1728      * If this isn't the selected version, exec the selected version.
       
  1729      */
       
  1730 #ifdef JAVA_ARGS  /* javac, jar and friends. */
       
  1731     progname = "java";
       
  1732 #else             /* java, oldjava, javaw and friends */
       
  1733 #ifdef PROGNAME
       
  1734     progname = PROGNAME;
       
  1735 #else
       
  1736     progname = *argv;
       
  1737     if ((s = strrchr(progname, FILE_SEPARATOR)) != 0) {
       
  1738         progname = s + 1;
       
  1739     }
       
  1740 #endif /* PROGNAME */
       
  1741 #endif /* JAVA_ARGS */
       
  1742 
       
  1743     /*
       
  1744      * This should never happen (because of the selection code in SelectJRE),
       
  1745      * but check for "impossibly" long path names just because buffer overruns
       
  1746      * can be so deadly.
       
  1747      */
       
  1748     if (strlen(wanted) + strlen(progname) + 6 > PATH_MAX) {
       
  1749         fprintf(stderr, "Path length exceeds maximum length (PATH_MAX)\n");
       
  1750         exit(1);
       
  1751     }
       
  1752 
       
  1753     /*
       
  1754      * Construct the path and exec it.
       
  1755      */
       
  1756     (void)strcat(strcat(wanted, "/bin/"), progname);
       
  1757     argv[0] = progname;
       
  1758     if (_launcher_debug) {
       
  1759         int i;
       
  1760         printf("ReExec Command: %s (%s)\n", wanted, argv[0]);
       
  1761         printf("ReExec Args:");
       
  1762         for (i = 1; argv[i] != NULL; i++)
       
  1763             printf(" %s", argv[i]);
       
  1764         printf("\n");
       
  1765     }
       
  1766     (void)fflush(stdout);
       
  1767     (void)fflush(stderr);
       
  1768     execv(wanted, argv);
       
  1769     perror("execv()");
       
  1770     fprintf(stderr, "Exec of %s failed\n", wanted);
       
  1771     exit(1);
       
  1772 }
       
  1773 #endif /* ifndef GAMMA */
       
  1774 
       
  1775 /*
       
  1776  * "Borrowed" from Solaris 10 where the unsetenv() function is being added
       
  1777  * to libc thanks to SUSv3 (Standard Unix Specification, version 3). As
       
  1778  * such, in the fullness of time this will appear in libc on all relevant
       
  1779  * Solaris/Linux platforms and maybe even the Windows platform.  At that
       
  1780  * time, this stub can be removed.
       
  1781  *
       
  1782  * This implementation removes the environment locking for multithreaded
       
  1783  * applications.  (We don't have access to these mutexes within libc and
       
  1784  * the launcher isn't multithreaded.)  Note that what remains is platform
       
  1785  * independent, because it only relies on attributes that a POSIX environment
       
  1786  * defines.
       
  1787  *
       
  1788  * Returns 0 on success, -1 on failure.
       
  1789  *
       
  1790  * Also removed was the setting of errno.  The only value of errno set
       
  1791  * was EINVAL ("Invalid Argument").
       
  1792  */
       
  1793 
       
  1794 /*
       
  1795  * s1(environ) is name=value
       
  1796  * s2(name) is name(not the form of name=value).
       
  1797  * if names match, return value of 1, else return 0
       
  1798  */
       
  1799 static int
       
  1800 match_noeq(const char *s1, const char *s2)
       
  1801 {
       
  1802         while (*s1 == *s2++) {
       
  1803                 if (*s1++ == '=')
       
  1804                         return (1);
       
  1805         }
       
  1806         if (*s1 == '=' && s2[-1] == '\0')
       
  1807                 return (1);
       
  1808         return (0);
       
  1809 }
       
  1810 
       
  1811 /*
       
  1812  * added for SUSv3 standard
       
  1813  *
       
  1814  * Delete entry from environ.
       
  1815  * Do not free() memory!  Other threads may be using it.
       
  1816  * Keep it around forever.
       
  1817  */
       
  1818 static int
       
  1819 borrowed_unsetenv(const char *name)
       
  1820 {
       
  1821         long    idx;            /* index into environ */
       
  1822 
       
  1823         if (name == NULL || *name == '\0' ||
       
  1824             strchr(name, '=') != NULL) {
       
  1825                 return (-1);
       
  1826         }
       
  1827 
       
  1828         for (idx = 0; environ[idx] != NULL; idx++) {
       
  1829                 if (match_noeq(environ[idx], name))
       
  1830                         break;
       
  1831         }
       
  1832         if (environ[idx] == NULL) {
       
  1833                 /* name not found but still a success */
       
  1834                 return (0);
       
  1835         }
       
  1836         /* squeeze up one entry */
       
  1837         do {
       
  1838                 environ[idx] = environ[idx+1];
       
  1839         } while (environ[++idx] != NULL);
       
  1840 
       
  1841         return (0);
       
  1842 }
       
  1843 /* --- End of "borrowed" code --- */
       
  1844 
       
  1845 /*
       
  1846  * Wrapper for unsetenv() function.
       
  1847  */
       
  1848 int
       
  1849 UnsetEnv(char *name)
       
  1850 {
       
  1851     return(borrowed_unsetenv(name));
       
  1852 }
       
  1853 
       
  1854 /* --- Splash Screen shared library support --- */
       
  1855 
       
  1856 static const char* SPLASHSCREEN_SO = "libsplashscreen.so";
       
  1857 
       
  1858 static void* hSplashLib = NULL;
       
  1859 
       
  1860 void* SplashProcAddress(const char* name) {
       
  1861     if (!hSplashLib) {
       
  1862         hSplashLib = dlopen(SPLASHSCREEN_SO, RTLD_LAZY | RTLD_GLOBAL);
       
  1863     }
       
  1864     if (hSplashLib) {
       
  1865         void* sym = dlsym(hSplashLib, name);
       
  1866         return sym;
       
  1867     } else {
       
  1868         return NULL;
       
  1869     }
       
  1870 }
       
  1871 
       
  1872 void SplashFreeLibrary() {
       
  1873     if (hSplashLib) {
       
  1874         dlclose(hSplashLib);
       
  1875         hSplashLib = NULL;
       
  1876     }
       
  1877 }
       
  1878 
       
  1879 /*
       
  1880  * Block current thread and continue execution in a new thread
       
  1881  */
       
  1882 int
       
  1883 ContinueInNewThread(int (JNICALL *continuation)(void *), jlong stack_size, void * args) {
       
  1884     int rslt;
       
  1885 #if defined(__linux__) || defined(_ALLBSD_SOURCE)
       
  1886     pthread_t tid;
       
  1887     pthread_attr_t attr;
       
  1888     pthread_attr_init(&attr);
       
  1889     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
       
  1890 
       
  1891     if (stack_size > 0) {
       
  1892       pthread_attr_setstacksize(&attr, stack_size);
       
  1893     }
       
  1894 
       
  1895     if (pthread_create(&tid, &attr, (void *(*)(void*))continuation, (void*)args) == 0) {
       
  1896       void * tmp;
       
  1897       pthread_join(tid, &tmp);
       
  1898       rslt = (int)(intptr_t)tmp;
       
  1899     } else {
       
  1900      /*
       
  1901       * Continue execution in current thread if for some reason (e.g. out of
       
  1902       * memory/LWP)  a new thread can't be created. This will likely fail
       
  1903       * later in continuation as JNI_CreateJavaVM needs to create quite a
       
  1904       * few new threads, anyway, just give it a try..
       
  1905       */
       
  1906       rslt = continuation(args);
       
  1907     }
       
  1908 
       
  1909     pthread_attr_destroy(&attr);
       
  1910 #else
       
  1911     thread_t tid;
       
  1912     long flags = 0;
       
  1913     if (thr_create(NULL, stack_size, (void *(*)(void *))continuation, args, flags, &tid) == 0) {
       
  1914       void * tmp;
       
  1915       thr_join(tid, NULL, &tmp);
       
  1916       rslt = (int)(intptr_t)tmp;
       
  1917     } else {
       
  1918       /* See above. Continue in current thread if thr_create() failed */
       
  1919       rslt = continuation(args);
       
  1920     }
       
  1921 #endif
       
  1922     return rslt;
       
  1923 }
       
  1924 
       
  1925 /* Coarse estimation of number of digits assuming the worst case is a 64-bit pid. */
       
  1926 #define MAX_PID_STR_SZ   20
       
  1927 
       
  1928 void SetJavaLauncherPlatformProps() {
       
  1929    /* Linux only */
       
  1930 #ifdef __linux__
       
  1931     const char *substr = "-Dsun.java.launcher.pid=";
       
  1932     char *pid_prop_str = (char *)JLI_MemAlloc(strlen(substr) + MAX_PID_STR_SZ + 1);
       
  1933     sprintf(pid_prop_str, "%s%d", substr, getpid());
       
  1934     AddOption(pid_prop_str, NULL);
       
  1935 #endif
       
  1936 }