src/os/posix/launcher/java_md.c

Wed, 22 Aug 2012 10:01:51 +0200

author
sla
date
Wed, 22 Aug 2012 10:01:51 +0200
changeset 4013
be82ef218872
parent 3473
e8a4934564b2
child 4153
b9a9ed0f8eeb
permissions
-rw-r--r--

7192916: Hotspot development launcher should use DYLD_LIBRARY_PATH on OS X
Reviewed-by: dholmes, dsamersoff, nloodin

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

mercurial