1 /* 2 * Copyright (c) 1998, 2016, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #include "java.h" 27 #include "jvm_md.h" 28 #include <dirent.h> 29 #include <dlfcn.h> 30 #include <fcntl.h> 31 #include <inttypes.h> 32 #include <stdio.h> 33 #include <string.h> 34 #include <stdlib.h> 35 #include <sys/stat.h> 36 #include <unistd.h> 37 #include <sys/types.h> 38 #include "manifest_info.h" 39 40 41 #define JVM_DLL "libjvm.so" 42 #define JAVA_DLL "libjava.so" 43 #ifdef AIX 44 #define LD_LIBRARY_PATH "LIBPATH" 45 #else 46 #define LD_LIBRARY_PATH "LD_LIBRARY_PATH" 47 #endif 48 49 /* help jettison the LD_LIBRARY_PATH settings in the future */ 50 #ifndef SETENV_REQUIRED 51 #define SETENV_REQUIRED 52 #endif 53 54 #ifdef __solaris__ 55 # include <sys/systeminfo.h> 56 # include <sys/elf.h> 57 # include <stdio.h> 58 #endif 59 60 /* 61 * Flowchart of launcher execs and options processing on unix 62 * 63 * The selection of the proper vm shared library to open depends on 64 * several classes of command line options, including vm "flavor" 65 * options (-client, -server) and the data model options, -d32 and 66 * -d64, as well as a version specification which may have come from 67 * the command line or from the manifest of an executable jar file. 68 * The vm selection options are not passed to the running 69 * virtual machine; they must be screened out by the launcher. 70 * 71 * The version specification (if any) is processed first by the 72 * platform independent routine SelectVersion. This may result in 73 * the exec of the specified launcher version. 74 * 75 * Previously the launcher modified the LD_LIBRARY_PATH appropriately for the 76 * desired data model path, regardless if data models matched or not. The 77 * launcher subsequently exec'ed the desired executable, in order to make the 78 * LD_LIBRARY_PATH path available, for the runtime linker. 79 * 80 * Now, in most cases,the launcher will dlopen the target libjvm.so. All 81 * required libraries are loaded by the runtime linker, using the 82 * $RPATH/$ORIGIN baked into the shared libraries at compile time. Therefore, 83 * in most cases, the launcher will only exec, if the data models are 84 * mismatched, and will not set any environment variables, regardless of the 85 * data models. 86 * 87 * However, if the environment contains a LD_LIBRARY_PATH, this will cause the 88 * launcher to inspect the LD_LIBRARY_PATH. The launcher will check 89 * a. if the LD_LIBRARY_PATH's first component is the path to the desired 90 * libjvm.so 91 * b. if any other libjvm.so is found in any of the paths. 92 * If case b is true, then the launcher will set the LD_LIBRARY_PATH to the 93 * desired JRE and reexec, in order to propagate the environment. 94 * 95 * Main 96 * (incoming argv) 97 * | 98 * \|/ 99 * CreateExecutionEnvironment 100 * (determines desired data model) 101 * | 102 * | 103 * \|/ 104 * Have Desired Model ? --> NO --> Exit(with error) 105 * | 106 * | 107 * \|/ 108 * YES 109 * | 110 * | 111 * \|/ 112 * CheckJvmType 113 * (removes -client, -server, etc.) 114 * | 115 * | 116 * \|/ 117 * TranslateDashJArgs... 118 * (Prepare to pass args to vm) 119 * | 120 * | 121 * \|/ 122 * ParseArguments 123 * (removes -d32 and -d64 if any, 124 * processes version options, 125 * creates argument list for vm, 126 * etc.) 127 * | 128 * | 129 * \|/ 130 * RequiresSetenv 131 * Is LD_LIBRARY_PATH 132 * and friends set ? --> NO --> Have Desired Model ? NO --> Error/Exit 133 * YES YES --> Continue 134 * | 135 * | 136 * \|/ 137 * Path is desired JRE ? YES --> Have Desired Model ? NO --> Error/Exit 138 * NO YES --> Continue 139 * | 140 * | 141 * \|/ 142 * Paths have well known 143 * jvm paths ? --> NO --> Have Desired Model ? NO --> Error/Exit 144 * YES YES --> Continue 145 * | 146 * | 147 * \|/ 148 * Does libjvm.so exit 149 * in any of them ? --> NO --> Have Desired Model ? NO --> Error/Exit 150 * YES YES --> Continue 151 * | 152 * | 153 * \|/ 154 * Set the LD_LIBRARY_PATH 155 * | 156 * | 157 * \|/ 158 * Re-exec 159 * | 160 * | 161 * \|/ 162 * Main 163 */ 164 165 /* Store the name of the executable once computed */ 166 static char *execname = NULL; 167 168 /* 169 * execname accessor from other parts of platform dependent logic 170 */ 171 const char * 172 GetExecName() { 173 return execname; 174 } 175 176 #ifdef SETENV_REQUIRED 177 static jboolean 178 JvmExists(const char *path) { 179 char tmp[PATH_MAX + 1]; 180 struct stat statbuf; 181 JLI_Snprintf(tmp, PATH_MAX, "%s/%s", path, JVM_DLL); 182 if (stat(tmp, &statbuf) == 0) { 183 return JNI_TRUE; 184 } 185 return JNI_FALSE; 186 } 187 /* 188 * contains a lib/{server,client}/libjvm.so ? 189 */ 190 static jboolean 191 ContainsLibJVM(const char *env) { 192 /* the usual suspects */ 193 char clientPattern[] = "lib/client"; 194 char serverPattern[] = "lib/server"; 195 char *envpath; 196 char *path; 197 jboolean clientPatternFound; 198 jboolean serverPatternFound; 199 200 /* fastest path */ 201 if (env == NULL) { 202 return JNI_FALSE; 203 } 204 205 /* to optimize for time, test if any of our usual suspects are present. */ 206 clientPatternFound = JLI_StrStr(env, clientPattern) != NULL; 207 serverPatternFound = JLI_StrStr(env, serverPattern) != NULL; 208 if (clientPatternFound == JNI_FALSE && serverPatternFound == JNI_FALSE) { 209 return JNI_FALSE; 210 } 211 212 /* 213 * we have a suspicious path component, check if it contains a libjvm.so 214 */ 215 envpath = JLI_StringDup(env); 216 for (path = JLI_StrTok(envpath, ":"); path != NULL; path = JLI_StrTok(NULL, ":")) { 217 if (clientPatternFound && JLI_StrStr(path, clientPattern) != NULL) { 218 if (JvmExists(path)) { 219 JLI_MemFree(envpath); 220 return JNI_TRUE; 221 } 222 } 223 if (serverPatternFound && JLI_StrStr(path, serverPattern) != NULL) { 224 if (JvmExists(path)) { 225 JLI_MemFree(envpath); 226 return JNI_TRUE; 227 } 228 } 229 } 230 JLI_MemFree(envpath); 231 return JNI_FALSE; 232 } 233 234 /* 235 * Test whether the environment variable needs to be set, see flowchart. 236 */ 237 static jboolean 238 RequiresSetenv(const char *jvmpath) { 239 char jpath[PATH_MAX + 1]; 240 char *llp; 241 char *dmllp = NULL; 242 char *p; /* a utility pointer */ 243 244 #ifdef __linux 245 #ifndef CLIB 246 #error "CLIB not set" 247 #endif 248 249 if (strcmp(CLIB, "musl") == 0) { 250 /* 251 * The musl library loader requires LD_LIBRARY_PATH to be set in 252 * order to correctly resolve the dependency libjava.so has on libjvm.so. 253 * 254 * Specifically, it differs from glibc in the sense that even if 255 * libjvm.so has already been loaded it will not be considered a 256 * candidate for resolving the dependency unless the *full* path 257 * of the already loaded library matches the dependency being loaded. 258 * 259 * libjvm.so is being loaded by the launcher using a long path to 260 * dlopen, not just the basename of the library. Typically this 261 * is something like "../lib/server/libjvm.so". However, if/when 262 * libjvm.so later tries to dlopen libjava.so (which it does in 263 * order to get access to a few functions implemented in 264 * libjava.so) the musl loader will, as part of loading 265 * dependent libraries, try to load libjvm.so using only its 266 * basename "libjvm.so". Since this does not match the longer 267 * path path it was first loaded with, the already loaded 268 * library is not considered a candidate, and the loader will 269 * instead look for libjvm.so elsewhere. If it's not in 270 * LD_LIBRARY_PATH the dependency load will fail, and libjava.so 271 * will therefore fail as well. 272 */ 273 return JNI_TRUE; 274 } 275 #endif 276 277 #ifdef AIX 278 /* We always have to set the LIBPATH on AIX because ld doesn't support $ORIGIN. */ 279 return JNI_TRUE; 280 #endif 281 282 llp = getenv("LD_LIBRARY_PATH"); 283 #ifdef __solaris__ 284 dmllp = getenv("LD_LIBRARY_PATH_64"); 285 #endif /* __solaris__ */ 286 /* no environment variable is a good environment variable */ 287 if (llp == NULL && dmllp == NULL) { 288 return JNI_FALSE; 289 } 290 #ifdef __linux 291 /* 292 * On linux, if a binary is running as sgid or suid, glibc sets 293 * LD_LIBRARY_PATH to the empty string for security purposes. (In contrast, 294 * on Solaris the LD_LIBRARY_PATH variable for a privileged binary does not 295 * lose its settings; but the dynamic linker does apply more scrutiny to the 296 * path.) The launcher uses the value of LD_LIBRARY_PATH to prevent an exec 297 * loop, here and further downstream. Therefore, if we are running sgid or 298 * suid, this function's setting of LD_LIBRARY_PATH will be ineffective and 299 * we should case a return from the calling function. Getting the right 300 * libraries will be handled by the RPATH. In reality, this check is 301 * redundant, as the previous check for a non-null LD_LIBRARY_PATH will 302 * return back to the calling function forthwith, it is left here to safe 303 * guard against any changes, in the glibc's existing security policy. 304 */ 305 if ((getgid() != getegid()) || (getuid() != geteuid())) { 306 return JNI_FALSE; 307 } 308 #endif /* __linux */ 309 310 /* 311 * Prevent recursions. Since LD_LIBRARY_PATH is the one which will be set by 312 * previous versions of the JRE, thus it is the only path that matters here. 313 * So we check to see if the desired JRE is set. 314 */ 315 JLI_StrNCpy(jpath, jvmpath, PATH_MAX); 316 p = JLI_StrRChr(jpath, '/'); 317 *p = '\0'; 318 if (llp != NULL && JLI_StrNCmp(llp, jpath, JLI_StrLen(jpath)) == 0) { 319 return JNI_FALSE; 320 } 321 322 /* scrutinize all the paths further */ 323 if (llp != NULL && ContainsLibJVM(llp)) { 324 return JNI_TRUE; 325 } 326 if (dmllp != NULL && ContainsLibJVM(dmllp)) { 327 return JNI_TRUE; 328 } 329 return JNI_FALSE; 330 } 331 #endif /* SETENV_REQUIRED */ 332 333 void 334 CreateExecutionEnvironment(int *pargc, char ***pargv, 335 char jrepath[], jint so_jrepath, 336 char jvmpath[], jint so_jvmpath, 337 char jvmcfg[], jint so_jvmcfg) { 338 /* 339 * First, determine if we are running the desired data model. If we 340 * are running the desired data model, all the error messages 341 * associated with calling GetJREPath, ReadKnownVMs, etc. should be 342 * output, otherwise we simply exit with an error, as we no longer 343 * support dual data models. 344 */ 345 jboolean jvmpathExists; 346 347 /* Compute/set the name of the executable */ 348 SetExecname(*pargv); 349 350 /* Check data model flags, and exec process, if needed */ 351 { 352 char * jvmtype = NULL; 353 int argc = *pargc; 354 char **argv = *pargv; 355 int running = CURRENT_DATA_MODEL; 356 /* 357 * As of jdk9, there is no support for dual mode operations, however 358 * for legacy error reporting purposes and until -d options are supported 359 * we need this. 360 */ 361 int wanted = running; 362 #ifdef SETENV_REQUIRED 363 jboolean mustsetenv = JNI_FALSE; 364 char *runpath = NULL; /* existing effective LD_LIBRARY_PATH setting */ 365 char* new_runpath = NULL; /* desired new LD_LIBRARY_PATH string */ 366 char* newpath = NULL; /* path on new LD_LIBRARY_PATH */ 367 char* lastslash = NULL; 368 char** newenvp = NULL; /* current environment */ 369 size_t new_runpath_size; 370 #ifdef __solaris__ 371 char* dmpath = NULL; /* data model specific LD_LIBRARY_PATH, 372 Solaris only */ 373 #endif /* __solaris__ */ 374 #endif /* SETENV_REQUIRED */ 375 376 char** newargv = NULL; 377 int newargc = 0; 378 379 /* 380 * Starting in 1.5, all unix platforms accept the -d32 and -d64 381 * options. On platforms where only one data-model is supported 382 * (e.g. ia-64 Linux), using the flag for the other data model is 383 * an error and will terminate the program. 384 */ 385 386 { /* open new scope to declare local variables */ 387 int i; 388 389 newargv = (char **)JLI_MemAlloc((argc+1) * sizeof(char*)); 390 newargv[newargc++] = argv[0]; 391 392 /* scan for data model arguments and remove from argument list; 393 last occurrence determines desired data model */ 394 for (i=1; i < argc; i++) { 395 396 if (JLI_StrCmp(argv[i], "-J-d64") == 0 || JLI_StrCmp(argv[i], "-d64") == 0) { 397 wanted = 64; 398 continue; 399 } 400 if (JLI_StrCmp(argv[i], "-J-d32") == 0 || JLI_StrCmp(argv[i], "-d32") == 0) { 401 wanted = 32; 402 continue; 403 } 404 newargv[newargc++] = argv[i]; 405 406 if (IsJavaArgs()) { 407 if (argv[i][0] != '-') continue; 408 } else { 409 if (JLI_StrCmp(argv[i], "-classpath") == 0 || JLI_StrCmp(argv[i], "-cp") == 0) { 410 i++; 411 if (i >= argc) break; 412 newargv[newargc++] = argv[i]; 413 continue; 414 } 415 if (argv[i][0] != '-') { i++; break; } 416 } 417 } 418 419 /* copy rest of args [i .. argc) */ 420 while (i < argc) { 421 newargv[newargc++] = argv[i++]; 422 } 423 newargv[newargc] = NULL; 424 425 /* 426 * newargv has all proper arguments here 427 */ 428 429 argc = newargc; 430 argv = newargv; 431 } 432 433 /* If the data model is not changing, it is an error if the 434 jvmpath does not exist */ 435 if (wanted == running) { 436 /* Find out where the JRE is that we will be using. */ 437 if (!GetJREPath(jrepath, so_jrepath, JNI_FALSE) ) { 438 JLI_ReportErrorMessage(JRE_ERROR1); 439 exit(2); 440 } 441 JLI_Snprintf(jvmcfg, so_jvmcfg, "%s%slib%s%sjvm.cfg", 442 jrepath, FILESEP, FILESEP, FILESEP); 443 /* Find the specified JVM type */ 444 if (ReadKnownVMs(jvmcfg, JNI_FALSE) < 1) { 445 JLI_ReportErrorMessage(CFG_ERROR7); 446 exit(1); 447 } 448 449 jvmpath[0] = '\0'; 450 jvmtype = CheckJvmType(pargc, pargv, JNI_FALSE); 451 if (JLI_StrCmp(jvmtype, "ERROR") == 0) { 452 JLI_ReportErrorMessage(CFG_ERROR9); 453 exit(4); 454 } 455 456 if (!GetJVMPath(jrepath, jvmtype, jvmpath, so_jvmpath, 0 )) { 457 JLI_ReportErrorMessage(CFG_ERROR8, jvmtype, jvmpath); 458 exit(4); 459 } 460 /* 461 * we seem to have everything we need, so without further ado 462 * we return back, otherwise proceed to set the environment. 463 */ 464 #ifdef SETENV_REQUIRED 465 mustsetenv = RequiresSetenv(jvmpath); 466 JLI_TraceLauncher("mustsetenv: %s\n", mustsetenv ? "TRUE" : "FALSE"); 467 468 if (mustsetenv == JNI_FALSE) { 469 JLI_MemFree(newargv); 470 return; 471 } 472 #else 473 JLI_MemFree(newargv); 474 return; 475 #endif /* SETENV_REQUIRED */ 476 } else { /* do the same speculatively or exit */ 477 JLI_ReportErrorMessage(JRE_ERROR2, wanted); 478 exit(1); 479 } 480 #ifdef SETENV_REQUIRED 481 if (mustsetenv) { 482 /* 483 * We will set the LD_LIBRARY_PATH as follows: 484 * 485 * o $JVMPATH (directory portion only) 486 * o $JRE/lib 487 * o $JRE/../lib 488 * 489 * followed by the user's previous effective LD_LIBRARY_PATH, if 490 * any. 491 */ 492 493 #ifdef __solaris__ 494 /* 495 * Starting in Solaris 7, ld.so.1 supports three LD_LIBRARY_PATH 496 * variables: 497 * 498 * 1. LD_LIBRARY_PATH -- used for 32 and 64 bit searches if 499 * data-model specific variables are not set. 500 * 501 * 2. LD_LIBRARY_PATH_64 -- overrides and replaces LD_LIBRARY_PATH 502 * for 64-bit binaries. 503 * The vm uses LD_LIBRARY_PATH to set the java.library.path system 504 * property. To shield the vm from the complication of multiple 505 * LD_LIBRARY_PATH variables, if the appropriate data model 506 * specific variable is set, we will act as if LD_LIBRARY_PATH had 507 * the value of the data model specific variant and the data model 508 * specific variant will be unset. Note that the variable for the 509 * *wanted* data model must be used (if it is set), not simply the 510 * current running data model. 511 */ 512 513 switch (wanted) { 514 case 0: 515 case 64: 516 dmpath = getenv("LD_LIBRARY_PATH_64"); 517 wanted = 64; 518 break; 519 520 default: 521 JLI_ReportErrorMessage(JRE_ERROR3, __LINE__); 522 exit(1); /* unknown value in wanted */ 523 break; 524 } 525 526 /* 527 * If dmpath is NULL, the relevant data model specific variable is 528 * not set and normal LD_LIBRARY_PATH should be used. 529 */ 530 if (dmpath == NULL) { 531 runpath = getenv("LD_LIBRARY_PATH"); 532 } else { 533 runpath = dmpath; 534 } 535 #else /* ! __solaris__ */ 536 /* 537 * If not on Solaris, assume only a single LD_LIBRARY_PATH 538 * variable. 539 */ 540 runpath = getenv(LD_LIBRARY_PATH); 541 #endif /* __solaris__ */ 542 543 /* runpath contains current effective LD_LIBRARY_PATH setting */ 544 { /* New scope to declare local variable */ 545 char *new_jvmpath = JLI_StringDup(jvmpath); 546 new_runpath_size = ((runpath != NULL) ? JLI_StrLen(runpath) : 0) + 547 2 * JLI_StrLen(jrepath) + 548 #ifdef AIX 549 /* On AIX we additionally need 'jli' in the path because ld doesn't support $ORIGIN. */ 550 JLI_StrLen(jrepath) + JLI_StrLen("/lib//jli:") + 551 #endif 552 JLI_StrLen(new_jvmpath) + 52; 553 new_runpath = JLI_MemAlloc(new_runpath_size); 554 newpath = new_runpath + JLI_StrLen(LD_LIBRARY_PATH "="); 555 556 557 /* 558 * Create desired LD_LIBRARY_PATH value for target data model. 559 */ 560 { 561 /* remove the name of the .so from the JVM path */ 562 lastslash = JLI_StrRChr(new_jvmpath, '/'); 563 if (lastslash) 564 *lastslash = '\0'; 565 566 sprintf(new_runpath, LD_LIBRARY_PATH "=" 567 "%s:" 568 "%s/lib:" 569 #ifdef AIX 570 "%s/lib/jli:" /* Needed on AIX because ld doesn't support $ORIGIN. */ 571 #endif 572 "%s/../lib", 573 new_jvmpath, 574 jrepath, 575 #ifdef AIX 576 jrepath, 577 #endif 578 jrepath 579 ); 580 581 JLI_MemFree(new_jvmpath); 582 583 /* 584 * Check to make sure that the prefix of the current path is the 585 * desired environment variable setting, though the RequiresSetenv 586 * checks if the desired runpath exists, this logic does a more 587 * comprehensive check. 588 */ 589 if (runpath != NULL && 590 JLI_StrNCmp(newpath, runpath, JLI_StrLen(newpath)) == 0 && 591 (runpath[JLI_StrLen(newpath)] == 0 || runpath[JLI_StrLen(newpath)] == ':') && 592 (running == wanted) /* data model does not have to be changed */ 593 #ifdef __solaris__ 594 && (dmpath == NULL) /* data model specific variables not set */ 595 #endif /* __solaris__ */ 596 ) { 597 JLI_MemFree(newargv); 598 JLI_MemFree(new_runpath); 599 return; 600 } 601 } 602 } 603 604 /* 605 * Place the desired environment setting onto the prefix of 606 * LD_LIBRARY_PATH. Note that this prevents any possible infinite 607 * loop of execv() because we test for the prefix, above. 608 */ 609 if (runpath != 0) { 610 /* ensure storage for runpath + colon + NULL */ 611 if ((JLI_StrLen(runpath) + 1 + 1) > new_runpath_size) { 612 JLI_ReportErrorMessageSys(JRE_ERROR11); 613 exit(1); 614 } 615 JLI_StrCat(new_runpath, ":"); 616 JLI_StrCat(new_runpath, runpath); 617 } 618 619 if (putenv(new_runpath) != 0) { 620 exit(1); /* problem allocating memory; LD_LIBRARY_PATH not set 621 properly */ 622 } 623 624 /* 625 * Unix systems document that they look at LD_LIBRARY_PATH only 626 * once at startup, so we have to re-exec the current executable 627 * to get the changed environment variable to have an effect. 628 */ 629 630 #ifdef __solaris__ 631 /* 632 * If dmpath is not NULL, remove the data model specific string 633 * in the environment for the exec'ed child. 634 */ 635 if (dmpath != NULL) 636 (void)UnsetEnv("LD_LIBRARY_PATH_64"); 637 #endif /* __solaris */ 638 639 newenvp = environ; 640 } 641 #endif /* SETENV_REQUIRED */ 642 { 643 char *newexec = execname; 644 JLI_TraceLauncher("TRACER_MARKER:About to EXEC\n"); 645 (void) fflush(stdout); 646 (void) fflush(stderr); 647 #ifdef SETENV_REQUIRED 648 if (mustsetenv) { 649 execve(newexec, argv, newenvp); 650 } else { 651 execv(newexec, argv); 652 } 653 #else /* !SETENV_REQUIRED */ 654 execv(newexec, argv); 655 #endif /* SETENV_REQUIRED */ 656 JLI_ReportErrorMessageSys(JRE_ERROR4, newexec); 657 } 658 exit(1); 659 } 660 } 661 662 /* 663 * On Solaris VM choosing is done by the launcher (java.c), 664 * bitsWanted is used by MacOSX, on Solaris and Linux this. 665 * parameter is unused. 666 */ 667 static jboolean 668 GetJVMPath(const char *jrepath, const char *jvmtype, 669 char *jvmpath, jint jvmpathsize, int bitsWanted) 670 { 671 struct stat s; 672 673 if (JLI_StrChr(jvmtype, '/')) { 674 JLI_Snprintf(jvmpath, jvmpathsize, "%s/" JVM_DLL, jvmtype); 675 } else { 676 JLI_Snprintf(jvmpath, jvmpathsize, "%s/lib/%s/" JVM_DLL, jrepath, jvmtype); 677 } 678 679 JLI_TraceLauncher("Does `%s' exist ... ", jvmpath); 680 681 if (stat(jvmpath, &s) == 0) { 682 JLI_TraceLauncher("yes.\n"); 683 return JNI_TRUE; 684 } else { 685 JLI_TraceLauncher("no.\n"); 686 return JNI_FALSE; 687 } 688 } 689 690 /* 691 * Find path to JRE based on .exe's location or registry settings. 692 */ 693 static jboolean 694 GetJREPath(char *path, jint pathsize, jboolean speculative) 695 { 696 char libjava[MAXPATHLEN]; 697 struct stat s; 698 699 if (GetApplicationHome(path, pathsize)) { 700 /* Is JRE co-located with the application? */ 701 JLI_Snprintf(libjava, sizeof(libjava), "%s/lib/" JAVA_DLL, path); 702 if (access(libjava, F_OK) == 0) { 703 JLI_TraceLauncher("JRE path is %s\n", path); 704 return JNI_TRUE; 705 } 706 /* ensure storage for path + /jre + NULL */ 707 if ((JLI_StrLen(path) + 4 + 1) > (size_t) pathsize) { 708 JLI_TraceLauncher("Insufficient space to store JRE path\n"); 709 return JNI_FALSE; 710 } 711 /* Does the app ship a private JRE in <apphome>/jre directory? */ 712 JLI_Snprintf(libjava, sizeof(libjava), "%s/jre/lib/" JAVA_DLL, path); 713 if (access(libjava, F_OK) == 0) { 714 JLI_StrCat(path, "/jre"); 715 JLI_TraceLauncher("JRE path is %s\n", path); 716 return JNI_TRUE; 717 } 718 } 719 720 if (GetApplicationHomeFromDll(path, pathsize)) { 721 JLI_Snprintf(libjava, sizeof(libjava), "%s/lib/" JAVA_DLL, path); 722 if (stat(libjava, &s) == 0) { 723 JLI_TraceLauncher("JRE path is %s\n", path); 724 return JNI_TRUE; 725 } 726 } 727 728 if (!speculative) 729 JLI_ReportErrorMessage(JRE_ERROR8 JAVA_DLL); 730 return JNI_FALSE; 731 } 732 733 jboolean 734 LoadJavaVM(const char *jvmpath, InvocationFunctions *ifn) 735 { 736 void *libjvm; 737 738 JLI_TraceLauncher("JVM path is %s\n", jvmpath); 739 740 libjvm = dlopen(jvmpath, RTLD_NOW + RTLD_GLOBAL); 741 if (libjvm == NULL) { 742 #if defined(__solaris__) && defined(__sparc) && !defined(_LP64) /* i.e. 32-bit sparc */ 743 FILE * fp; 744 Elf32_Ehdr elf_head; 745 int count; 746 int location; 747 748 fp = fopen(jvmpath, "r"); 749 if (fp == NULL) { 750 JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror()); 751 return JNI_FALSE; 752 } 753 754 /* read in elf header */ 755 count = fread((void*)(&elf_head), sizeof(Elf32_Ehdr), 1, fp); 756 fclose(fp); 757 if (count < 1) { 758 JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror()); 759 return JNI_FALSE; 760 } 761 762 /* 763 * Check for running a server vm (compiled with -xarch=v8plus) 764 * on a stock v8 processor. In this case, the machine type in 765 * the elf header would not be included the architecture list 766 * provided by the isalist command, which is turn is gotten from 767 * sysinfo. This case cannot occur on 64-bit hardware and thus 768 * does not have to be checked for in binaries with an LP64 data 769 * model. 770 */ 771 if (elf_head.e_machine == EM_SPARC32PLUS) { 772 char buf[257]; /* recommended buffer size from sysinfo man 773 page */ 774 long length; 775 char* location; 776 777 length = sysinfo(SI_ISALIST, buf, 257); 778 if (length > 0) { 779 location = JLI_StrStr(buf, "sparcv8plus "); 780 if (location == NULL) { 781 JLI_ReportErrorMessage(JVM_ERROR3); 782 return JNI_FALSE; 783 } 784 } 785 } 786 #endif 787 JLI_ReportErrorMessage(DLL_ERROR1, __LINE__); 788 JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror()); 789 return JNI_FALSE; 790 } 791 792 ifn->CreateJavaVM = (CreateJavaVM_t) 793 dlsym(libjvm, "JNI_CreateJavaVM"); 794 if (ifn->CreateJavaVM == NULL) { 795 JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror()); 796 return JNI_FALSE; 797 } 798 799 ifn->GetDefaultJavaVMInitArgs = (GetDefaultJavaVMInitArgs_t) 800 dlsym(libjvm, "JNI_GetDefaultJavaVMInitArgs"); 801 if (ifn->GetDefaultJavaVMInitArgs == NULL) { 802 JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror()); 803 return JNI_FALSE; 804 } 805 806 ifn->GetCreatedJavaVMs = (GetCreatedJavaVMs_t) 807 dlsym(libjvm, "JNI_GetCreatedJavaVMs"); 808 if (ifn->GetCreatedJavaVMs == NULL) { 809 JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror()); 810 return JNI_FALSE; 811 } 812 813 return JNI_TRUE; 814 } 815 816 /* 817 * Compute the name of the executable 818 * 819 * In order to re-exec securely we need the absolute path of the 820 * executable. On Solaris getexecname(3c) may not return an absolute 821 * path so we use dladdr to get the filename of the executable and 822 * then use realpath to derive an absolute path. From Solaris 9 823 * onwards the filename returned in DL_info structure from dladdr is 824 * an absolute pathname so technically realpath isn't required. 825 * On Linux we read the executable name from /proc/self/exe. 826 * As a fallback, and for platforms other than Solaris and Linux, 827 * we use FindExecName to compute the executable name. 828 */ 829 const char* 830 SetExecname(char **argv) 831 { 832 char* exec_path = NULL; 833 #if defined(__solaris__) 834 { 835 Dl_info dlinfo; 836 int (*fptr)(); 837 838 fptr = (int (*)())dlsym(RTLD_DEFAULT, "main"); 839 if (fptr == NULL) { 840 JLI_ReportErrorMessage(DLL_ERROR3, dlerror()); 841 return JNI_FALSE; 842 } 843 844 if (dladdr((void*)fptr, &dlinfo)) { 845 char *resolved = (char*)JLI_MemAlloc(PATH_MAX+1); 846 if (resolved != NULL) { 847 exec_path = realpath(dlinfo.dli_fname, resolved); 848 if (exec_path == NULL) { 849 JLI_MemFree(resolved); 850 } 851 } 852 } 853 } 854 #elif defined(__linux__) 855 { 856 const char* self = "/proc/self/exe"; 857 char buf[PATH_MAX+1]; 858 int len = readlink(self, buf, PATH_MAX); 859 if (len >= 0) { 860 buf[len] = '\0'; /* readlink(2) doesn't NUL terminate */ 861 exec_path = JLI_StringDup(buf); 862 } 863 } 864 #else /* !__solaris__ && !__linux__ */ 865 { 866 /* Not implemented */ 867 } 868 #endif 869 870 if (exec_path == NULL) { 871 exec_path = FindExecName(argv[0]); 872 } 873 execname = exec_path; 874 return exec_path; 875 } 876 877 /* --- Splash Screen shared library support --- */ 878 static const char* SPLASHSCREEN_SO = JNI_LIB_NAME("splashscreen"); 879 static void* hSplashLib = NULL; 880 881 void* SplashProcAddress(const char* name) { 882 if (!hSplashLib) { 883 int ret; 884 char jrePath[MAXPATHLEN]; 885 char splashPath[MAXPATHLEN]; 886 887 if (!GetJREPath(jrePath, sizeof(jrePath), JNI_FALSE)) { 888 JLI_ReportErrorMessage(JRE_ERROR1); 889 return NULL; 890 } 891 ret = JLI_Snprintf(splashPath, sizeof(splashPath), "%s/lib/%s", 892 jrePath, SPLASHSCREEN_SO); 893 894 if (ret >= (int) sizeof(splashPath)) { 895 JLI_ReportErrorMessage(JRE_ERROR11); 896 return NULL; 897 } 898 if (ret < 0) { 899 JLI_ReportErrorMessage(JRE_ERROR13); 900 return NULL; 901 } 902 hSplashLib = dlopen(splashPath, RTLD_LAZY | RTLD_GLOBAL); 903 JLI_TraceLauncher("Info: loaded %s\n", splashPath); 904 } 905 if (hSplashLib) { 906 void* sym = dlsym(hSplashLib, name); 907 return sym; 908 } else { 909 return NULL; 910 } 911 } 912 913 void SplashFreeLibrary() { 914 if (hSplashLib) { 915 dlclose(hSplashLib); 916 hSplashLib = NULL; 917 } 918 } 919 920 /* 921 * Block current thread and continue execution in a new thread 922 */ 923 int 924 ContinueInNewThread0(int (JNICALL *continuation)(void *), jlong stack_size, void * args) { 925 int rslt; 926 #ifndef __solaris__ 927 pthread_t tid; 928 pthread_attr_t attr; 929 pthread_attr_init(&attr); 930 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); 931 932 if (stack_size > 0) { 933 pthread_attr_setstacksize(&attr, stack_size); 934 } 935 936 if (pthread_create(&tid, &attr, (void *(*)(void*))continuation, (void*)args) == 0) { 937 void * tmp; 938 pthread_join(tid, &tmp); 939 rslt = (int)(intptr_t)tmp; 940 } else { 941 /* 942 * Continue execution in current thread if for some reason (e.g. out of 943 * memory/LWP) a new thread can't be created. This will likely fail 944 * later in continuation as JNI_CreateJavaVM needs to create quite a 945 * few new threads, anyway, just give it a try.. 946 */ 947 rslt = continuation(args); 948 } 949 950 pthread_attr_destroy(&attr); 951 #else /* __solaris__ */ 952 thread_t tid; 953 long flags = 0; 954 if (thr_create(NULL, stack_size, (void *(*)(void *))continuation, args, flags, &tid) == 0) { 955 void * tmp; 956 thr_join(tid, NULL, &tmp); 957 rslt = (int)(intptr_t)tmp; 958 } else { 959 /* See above. Continue in current thread if thr_create() failed */ 960 rslt = continuation(args); 961 } 962 #endif /* !__solaris__ */ 963 return rslt; 964 } 965 966 /* Coarse estimation of number of digits assuming the worst case is a 64-bit pid. */ 967 #define MAX_PID_STR_SZ 20 968 969 void SetJavaLauncherPlatformProps() { 970 /* Linux only */ 971 #ifdef __linux__ 972 const char *substr = "-Dsun.java.launcher.pid="; 973 char *pid_prop_str = (char *)JLI_MemAlloc(JLI_StrLen(substr) + MAX_PID_STR_SZ + 1); 974 sprintf(pid_prop_str, "%s%d", substr, getpid()); 975 AddOption(pid_prop_str, NULL); 976 #endif /* __linux__ */ 977 } 978 979 int 980 JVMInit(InvocationFunctions* ifn, jlong threadStackSize, 981 int argc, char **argv, 982 int mode, char *what, int ret) 983 { 984 ShowSplashScreen(); 985 return ContinueInNewThread(ifn, threadStackSize, argc, argv, mode, what, ret); 986 } 987 988 void 989 PostJVMInit(JNIEnv *env, jclass mainClass, JavaVM *vm) 990 { 991 // stubbed out for windows and *nixes. 992 } 993 994 void 995 RegisterThread() 996 { 997 // stubbed out for windows and *nixes. 998 } 999 1000 /* 1001 * on unix, we return a false to indicate this option is not applicable 1002 */ 1003 jboolean 1004 ProcessPlatformOption(const char *arg) 1005 { 1006 return JNI_FALSE; 1007 }