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