src/os/linux/launcher/java_md.c

Wed, 08 Oct 2008 08:10:51 -0700

author
ksrini
date
Wed, 08 Oct 2008 08:10:51 -0700
changeset 823
f008d3631bd1
parent 435
a61af66fc99e
child 905
ad8c8ca4ab0f
permissions
-rw-r--r--

6755845: JVM_FindClassFromBoot triggers assertions
Summary: Fixes assertions caused by one jvm_entry calling another, solved by refactoring code and modified gamma test.
Reviewed-by: dholmes, xlu

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

mercurial