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