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