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