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