1/* 2 * Copyright (c) 2012, 2020, 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 <sys/time.h> 39 40#include "manifest_info.h" 41 42/* Support Cocoa event loop on the main thread */ 43#include <Cocoa/Cocoa.h> 44#include <objc/objc-runtime.h> 45#include <objc/objc-auto.h> 46 47#include <errno.h> 48#include <spawn.h> 49 50struct NSAppArgs { 51 int argc; 52 char **argv; 53}; 54 55#define JVM_DLL "libjvm.dylib" 56#define JAVA_DLL "libjava.dylib" 57/* FALLBACK avoids naming conflicts with system libraries 58 * (eg, ImageIO's libJPEG.dylib) */ 59#define LD_LIBRARY_PATH "DYLD_FALLBACK_LIBRARY_PATH" 60 61/* 62 * If a processor / os combination has the ability to run binaries of 63 * two data models and cohabitation of jre/jdk bits with both data 64 * models is supported, then DUAL_MODE is defined. MacOSX is a hybrid 65 * system in that, the universal library can contain all types of libraries 66 * 32/64 and client/server, thus the spawn is capable of linking with the 67 * appropriate library as requested. 68 * 69 * Notes: 70 * 1. VM. DUAL_MODE is disabled, and not supported, however, it is left here in 71 * for experimentation and perhaps enable it in the future. 72 * 2. At the time of this writing, the universal library contains only 73 * a server 64-bit server JVM. 74 * 3. "-client" command line option is supported merely as a command line flag, 75 * for, compatibility reasons, however, a server VM will be launched. 76 */ 77 78/* 79 * Flowchart of launcher execs and options processing on unix 80 * 81 * The selection of the proper vm shared library to open depends on 82 * several classes of command line options, including vm "flavor" 83 * options (-client, -server) and the data model options, -d32 and 84 * -d64, as well as a version specification which may have come from 85 * the command line or from the manifest of an executable jar file. 86 * The vm selection options are not passed to the running 87 * virtual machine; they must be screened out by the launcher. 88 * 89 * The version specification (if any) is processed first by the 90 * platform independent routine SelectVersion. This may result in 91 * the exec of the specified launcher version. 92 * 93 * Now, in most cases,the launcher will dlopen the target libjvm.so. All 94 * required libraries are loaded by the runtime linker, using the known paths 95 * baked into the shared libraries at compile time. Therefore, 96 * in most cases, the launcher will only exec, if the data models are 97 * mismatched, and will not set any environment variables, regardless of the 98 * data models. 99 * 100 * 101 * 102 * Main 103 * (incoming argv) 104 * | 105 * \|/ 106 * CreateExecutionEnvironment 107 * (determines desired data model) 108 * | 109 * | 110 * \|/ 111 * Have Desired Model ? --> NO --> Is Dual-Mode ? --> NO --> Exit(with error) 112 * | | 113 * | | 114 * | \|/ 115 * | YES 116 * | | 117 * | | 118 * | \|/ 119 * | CheckJvmType 120 * | (removes -client, -server etc.) 121 * | | 122 * | | 123 * \|/ \|/ 124 * YES Find the desired executable/library 125 * | | 126 * | | 127 * \|/ \|/ 128 * CheckJvmType POINT A 129 * (removes -client, -server, etc.) 130 * | 131 * | 132 * \|/ 133 * TranslateDashJArgs... 134 * (Prepare to pass args to vm) 135 * | 136 * | 137 * \|/ 138 * ParseArguments 139 * (processes version options, 140 * creates argument list for vm, 141 * etc.) 142 * | 143 * | 144 * \|/ 145 * POINT A 146 * | 147 * | 148 * \|/ 149 * Path is desired JRE ? YES --> Continue 150 * NO 151 * | 152 * | 153 * \|/ 154 * Paths have well known 155 * jvm paths ? --> NO --> Continue 156 * YES 157 * | 158 * | 159 * \|/ 160 * Does libjvm.so exist 161 * in any of them ? --> NO --> Continue 162 * YES 163 * | 164 * | 165 * \|/ 166 * Re-exec / Spawn 167 * | 168 * | 169 * \|/ 170 * Main 171 */ 172 173/* Store the name of the executable once computed */ 174static char *execname = NULL; 175 176/* 177 * execname accessor from other parts of platform dependent logic 178 */ 179const char * 180GetExecName() { 181 return execname; 182} 183 184/* 185 * Exports the JNI interface from libjli 186 * 187 * This allows client code to link against the .jre/.jdk bundles, 188 * and not worry about trying to pick a HotSpot to link against. 189 * 190 * Switching architectures is unsupported, since client code has 191 * made that choice before the JVM was requested. 192 */ 193 194static InvocationFunctions *sExportedJNIFunctions = NULL; 195static char *sPreferredJVMType = NULL; 196 197static InvocationFunctions *GetExportedJNIFunctions() { 198 if (sExportedJNIFunctions != NULL) return sExportedJNIFunctions; 199 200 char jrePath[PATH_MAX]; 201 jboolean gotJREPath = GetJREPath(jrePath, sizeof(jrePath), JNI_FALSE); 202 if (!gotJREPath) { 203 JLI_ReportErrorMessage("Failed to GetJREPath()"); 204 return NULL; 205 } 206 207 char *preferredJVM = sPreferredJVMType; 208 if (preferredJVM == NULL) { 209#if defined(__i386__) 210 preferredJVM = "client"; 211#elif defined(__x86_64__) 212 preferredJVM = "server"; 213#else 214#error "Unknown architecture - needs definition" 215#endif 216 } 217 218 char jvmPath[PATH_MAX]; 219 jboolean gotJVMPath = GetJVMPath(jrePath, preferredJVM, jvmPath, sizeof(jvmPath)); 220 if (!gotJVMPath) { 221 JLI_ReportErrorMessage("Failed to GetJVMPath()"); 222 return NULL; 223 } 224 225 InvocationFunctions *fxns = malloc(sizeof(InvocationFunctions)); 226 jboolean vmLoaded = LoadJavaVM(jvmPath, fxns); 227 if (!vmLoaded) { 228 JLI_ReportErrorMessage("Failed to LoadJavaVM()"); 229 return NULL; 230 } 231 232 return sExportedJNIFunctions = fxns; 233} 234 235#ifndef STATIC_BUILD 236 237JNIEXPORT jint JNICALL 238JNI_GetDefaultJavaVMInitArgs(void *args) { 239 InvocationFunctions *ifn = GetExportedJNIFunctions(); 240 if (ifn == NULL) return JNI_ERR; 241 return ifn->GetDefaultJavaVMInitArgs(args); 242} 243 244JNIEXPORT jint JNICALL 245JNI_CreateJavaVM(JavaVM **pvm, void **penv, void *args) { 246 InvocationFunctions *ifn = GetExportedJNIFunctions(); 247 if (ifn == NULL) return JNI_ERR; 248 return ifn->CreateJavaVM(pvm, penv, args); 249} 250 251JNIEXPORT jint JNICALL 252JNI_GetCreatedJavaVMs(JavaVM **vmBuf, jsize bufLen, jsize *nVMs) { 253 InvocationFunctions *ifn = GetExportedJNIFunctions(); 254 if (ifn == NULL) return JNI_ERR; 255 return ifn->GetCreatedJavaVMs(vmBuf, bufLen, nVMs); 256} 257#endif 258 259/* 260 * Allow JLI-aware launchers to specify a client/server preference 261 */ 262JNIEXPORT void JNICALL 263JLI_SetPreferredJVM(const char *prefJVM) { 264 if (sPreferredJVMType != NULL) { 265 free(sPreferredJVMType); 266 sPreferredJVMType = NULL; 267 } 268 269 if (prefJVM == NULL) return; 270 sPreferredJVMType = strdup(prefJVM); 271} 272 273static BOOL awtLoaded = NO; 274static pthread_mutex_t awtLoaded_mutex = PTHREAD_MUTEX_INITIALIZER; 275static pthread_cond_t awtLoaded_cv = PTHREAD_COND_INITIALIZER; 276 277JNIEXPORT void JNICALL 278JLI_NotifyAWTLoaded() 279{ 280 pthread_mutex_lock(&awtLoaded_mutex); 281 awtLoaded = YES; 282 pthread_cond_signal(&awtLoaded_cv); 283 pthread_mutex_unlock(&awtLoaded_mutex); 284} 285 286static int (*main_fptr)(int argc, char **argv) = NULL; 287 288/* 289 * Unwrap the arguments and re-run main() 290 */ 291static void *apple_main (void *arg) 292{ 293 if (main_fptr == NULL) { 294#ifdef STATIC_BUILD 295 extern int main(int argc, char **argv); 296 main_fptr = &main; 297#else 298 main_fptr = (int (*)())dlsym(RTLD_DEFAULT, "main"); 299#endif 300 if (main_fptr == NULL) { 301 JLI_ReportErrorMessageSys("error locating main entrypoint\n"); 302 exit(1); 303 } 304 } 305 306 struct NSAppArgs *args = (struct NSAppArgs *) arg; 307 exit(main_fptr(args->argc, args->argv)); 308} 309 310static void dummyTimer(CFRunLoopTimerRef timer, void *info) {} 311 312static void ParkEventLoop() { 313 // RunLoop needs at least one source, and 1e20 is pretty far into the future 314 CFRunLoopTimerRef t = CFRunLoopTimerCreate(kCFAllocatorDefault, 1.0e20, 0.0, 0, 0, dummyTimer, NULL); 315 CFRunLoopAddTimer(CFRunLoopGetCurrent(), t, kCFRunLoopDefaultMode); 316 CFRelease(t); 317 318 // Park this thread in the main run loop. 319 int32_t result; 320 do { 321 result = CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0e20, false); 322 } while (result != kCFRunLoopRunFinished); 323} 324 325/* 326 * Mac OS X mandates that the GUI event loop run on very first thread of 327 * an application. This requires that we re-call Java's main() on a new 328 * thread, reserving the 'main' thread for Cocoa. 329 */ 330static void MacOSXStartup(int argc, char *argv[]) { 331 // Thread already started? 332 static jboolean started = false; 333 if (started) { 334 return; 335 } 336 started = true; 337 338 // Hand off arguments 339 struct NSAppArgs args; 340 args.argc = argc; 341 args.argv = argv; 342 343 // Fire up the main thread 344 pthread_t main_thr; 345 if (pthread_create(&main_thr, NULL, &apple_main, &args) != 0) { 346 JLI_ReportErrorMessageSys("Could not create main thread: %s\n", strerror(errno)); 347 exit(1); 348 } 349 if (pthread_detach(main_thr)) { 350 JLI_ReportErrorMessageSys("pthread_detach() failed: %s\n", strerror(errno)); 351 exit(1); 352 } 353 354 ParkEventLoop(); 355} 356 357void 358CreateExecutionEnvironment(int *pargc, char ***pargv, 359 char jrepath[], jint so_jrepath, 360 char jvmpath[], jint so_jvmpath, 361 char jvmcfg[], jint so_jvmcfg) { 362 jboolean jvmpathExists; 363 364 /* Compute/set the name of the executable */ 365 SetExecname(*pargv); 366 367 char * jvmtype = NULL; 368 int argc = *pargc; 369 char **argv = *pargv; 370 371 /* Find out where the JRE is that we will be using. */ 372 if (!GetJREPath(jrepath, so_jrepath, JNI_FALSE) ) { 373 JLI_ReportErrorMessage(JRE_ERROR1); 374 exit(2); 375 } 376 JLI_Snprintf(jvmcfg, so_jvmcfg, "%s%slib%sjvm.cfg", 377 jrepath, FILESEP, FILESEP); 378 /* Find the specified JVM type */ 379 if (ReadKnownVMs(jvmcfg, JNI_FALSE) < 1) { 380 JLI_ReportErrorMessage(CFG_ERROR7); 381 exit(1); 382 } 383 384 jvmpath[0] = '\0'; 385 jvmtype = CheckJvmType(pargc, pargv, JNI_FALSE); 386 if (JLI_StrCmp(jvmtype, "ERROR") == 0) { 387 JLI_ReportErrorMessage(CFG_ERROR9); 388 exit(4); 389 } 390 391 if (!GetJVMPath(jrepath, jvmtype, jvmpath, so_jvmpath)) { 392 JLI_ReportErrorMessage(CFG_ERROR8, jvmtype, jvmpath); 393 exit(4); 394 } 395 396 /* 397 * Mac OS X requires the Cocoa event loop to be run on the "main" 398 * thread. Spawn off a new thread to run main() and pass 399 * this thread off to the Cocoa event loop. 400 */ 401 MacOSXStartup(argc, argv); 402 403 /* 404 * we seem to have everything we need 405 */ 406 return; 407} 408 409/* 410 * VM choosing is done by the launcher (java.c). 411 */ 412static jboolean 413GetJVMPath(const char *jrepath, const char *jvmtype, 414 char *jvmpath, jint jvmpathsize) 415{ 416 struct stat s; 417 418 if (JLI_StrChr(jvmtype, '/')) { 419 JLI_Snprintf(jvmpath, jvmpathsize, "%s/" JVM_DLL, jvmtype); 420 } else { 421 /* 422 * macosx client library is built thin, i386 only. 423 * 64 bit client requests must load server library 424 */ 425 JLI_Snprintf(jvmpath, jvmpathsize, "%s/lib/%s/" JVM_DLL, jrepath, jvmtype); 426 } 427 428 JLI_TraceLauncher("Does `%s' exist ... ", jvmpath); 429 430#ifdef STATIC_BUILD 431 return JNI_TRUE; 432#else 433 if (stat(jvmpath, &s) == 0) { 434 JLI_TraceLauncher("yes.\n"); 435 return JNI_TRUE; 436 } else { 437 JLI_TraceLauncher("no.\n"); 438 return JNI_FALSE; 439 } 440#endif 441} 442 443/* 444 * Find path to JRE based on .exe's location or registry settings. 445 */ 446static jboolean 447GetJREPath(char *path, jint pathsize, jboolean speculative) 448{ 449 char libjava[MAXPATHLEN]; 450 451 if (GetApplicationHome(path, pathsize)) { 452 /* Is JRE co-located with the application? */ 453#ifdef STATIC_BUILD 454 char jvm_cfg[MAXPATHLEN]; 455 JLI_Snprintf(jvm_cfg, sizeof(jvm_cfg), "%s/lib/jvm.cfg", path); 456 if (access(jvm_cfg, F_OK) == 0) { 457 return JNI_TRUE; 458 } 459#else 460 JLI_Snprintf(libjava, sizeof(libjava), "%s/lib/" JAVA_DLL, path); 461 if (access(libjava, F_OK) == 0) { 462 return JNI_TRUE; 463 } 464#endif 465 /* ensure storage for path + /jre + NULL */ 466 if ((JLI_StrLen(path) + 4 + 1) > (size_t) pathsize) { 467 JLI_TraceLauncher("Insufficient space to store JRE path\n"); 468 return JNI_FALSE; 469 } 470 /* Does the app ship a private JRE in <apphome>/jre directory? */ 471 JLI_Snprintf(libjava, sizeof(libjava), "%s/jre/lib/" JAVA_DLL, path); 472 if (access(libjava, F_OK) == 0) { 473 JLI_StrCat(path, "/jre"); 474 JLI_TraceLauncher("JRE path is %s\n", path); 475 return JNI_TRUE; 476 } 477 } 478 479 /* try to find ourselves instead */ 480 Dl_info selfInfo; 481 dladdr(&GetJREPath, &selfInfo); 482 483#ifdef STATIC_BUILD 484 char jvm_cfg[MAXPATHLEN]; 485 char *p = NULL; 486 strncpy(jvm_cfg, selfInfo.dli_fname, MAXPATHLEN); 487 p = strrchr(jvm_cfg, '/'); *p = '\0'; 488 p = strrchr(jvm_cfg, '/'); 489 if (strcmp(p, "/.") == 0) { 490 *p = '\0'; 491 p = strrchr(jvm_cfg, '/'); *p = '\0'; 492 } 493 else *p = '\0'; 494 strncpy(path, jvm_cfg, pathsize); 495 strncat(jvm_cfg, "/lib/jvm.cfg", MAXPATHLEN); 496 if (access(jvm_cfg, F_OK) == 0) { 497 return JNI_TRUE; 498 } 499#endif 500 501 char *realPathToSelf = realpath(selfInfo.dli_fname, path); 502 if (realPathToSelf != path) { 503 return JNI_FALSE; 504 } 505 506 size_t pathLen = strlen(realPathToSelf); 507 if (pathLen == 0) { 508 return JNI_FALSE; 509 } 510 511 const char lastPathComponent[] = "/lib/libjli.dylib"; 512 size_t sizeOfLastPathComponent = sizeof(lastPathComponent) - 1; 513 if (pathLen < sizeOfLastPathComponent) { 514 return JNI_FALSE; 515 } 516 517 size_t indexOfLastPathComponent = pathLen - sizeOfLastPathComponent; 518 if (0 == strncmp(realPathToSelf + indexOfLastPathComponent, lastPathComponent, sizeOfLastPathComponent)) { 519 realPathToSelf[indexOfLastPathComponent + 1] = '\0'; 520 return JNI_TRUE; 521 } 522 523 // If libjli.dylib is loaded from a macos bundle MacOS dir, find the JRE dir 524 // in ../Home. 525 const char altLastPathComponent[] = "/MacOS/libjli.dylib"; 526 size_t sizeOfAltLastPathComponent = sizeof(altLastPathComponent) - 1; 527 if (pathLen < sizeOfLastPathComponent) { 528 return JNI_FALSE; 529 } 530 531 size_t indexOfAltLastPathComponent = pathLen - sizeOfAltLastPathComponent; 532 if (0 == strncmp(realPathToSelf + indexOfAltLastPathComponent, altLastPathComponent, sizeOfAltLastPathComponent)) { 533 JLI_Snprintf(realPathToSelf + indexOfAltLastPathComponent, sizeOfAltLastPathComponent, "%s", "/Home"); 534 if (access(realPathToSelf, F_OK) == 0) { 535 return JNI_TRUE; 536 } 537 } 538 539 if (!speculative) 540 JLI_ReportErrorMessage(JRE_ERROR8 JAVA_DLL); 541 return JNI_FALSE; 542} 543 544jboolean 545LoadJavaVM(const char *jvmpath, InvocationFunctions *ifn) 546{ 547 Dl_info dlinfo; 548 void *libjvm; 549 550 JLI_TraceLauncher("JVM path is %s\n", jvmpath); 551 552#ifndef STATIC_BUILD 553 libjvm = dlopen(jvmpath, RTLD_NOW + RTLD_GLOBAL); 554#else 555 libjvm = dlopen(NULL, RTLD_FIRST); 556#endif 557 if (libjvm == NULL) { 558 JLI_ReportErrorMessage(DLL_ERROR1, __LINE__); 559 JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror()); 560 return JNI_FALSE; 561 } 562 563 ifn->CreateJavaVM = (CreateJavaVM_t) 564 dlsym(libjvm, "JNI_CreateJavaVM"); 565 if (ifn->CreateJavaVM == NULL) { 566 JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror()); 567 return JNI_FALSE; 568 } 569 570 ifn->GetDefaultJavaVMInitArgs = (GetDefaultJavaVMInitArgs_t) 571 dlsym(libjvm, "JNI_GetDefaultJavaVMInitArgs"); 572 if (ifn->GetDefaultJavaVMInitArgs == NULL) { 573 JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror()); 574 return JNI_FALSE; 575 } 576 577 ifn->GetCreatedJavaVMs = (GetCreatedJavaVMs_t) 578 dlsym(libjvm, "JNI_GetCreatedJavaVMs"); 579 if (ifn->GetCreatedJavaVMs == NULL) { 580 JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror()); 581 return JNI_FALSE; 582 } 583 584 return JNI_TRUE; 585} 586 587/* 588 * Compute the name of the executable 589 * 590 * In order to re-exec securely we need the absolute path of the 591 * executable. On Solaris getexecname(3c) may not return an absolute 592 * path so we use dladdr to get the filename of the executable and 593 * then use realpath to derive an absolute path. From Solaris 9 594 * onwards the filename returned in DL_info structure from dladdr is 595 * an absolute pathname so technically realpath isn't required. 596 * On Linux we read the executable name from /proc/self/exe. 597 * As a fallback, and for platforms other than Solaris and Linux, 598 * we use FindExecName to compute the executable name. 599 */ 600const char* 601SetExecname(char **argv) 602{ 603 char* exec_path = NULL; 604 { 605 Dl_info dlinfo; 606 607#ifdef STATIC_BUILD 608 void *fptr; 609 fptr = (void *)&SetExecname; 610#else 611 int (*fptr)(); 612 fptr = (int (*)())dlsym(RTLD_DEFAULT, "main"); 613#endif 614 if (fptr == NULL) { 615 JLI_ReportErrorMessage(DLL_ERROR3, dlerror()); 616 return JNI_FALSE; 617 } 618 619 if (dladdr((void*)fptr, &dlinfo)) { 620 char *resolved = (char*)JLI_MemAlloc(PATH_MAX+1); 621 if (resolved != NULL) { 622 exec_path = realpath(dlinfo.dli_fname, resolved); 623 if (exec_path == NULL) { 624 JLI_MemFree(resolved); 625 } 626 } 627 } 628 } 629 if (exec_path == NULL) { 630 exec_path = FindExecName(argv[0]); 631 } 632 execname = exec_path; 633 return exec_path; 634} 635 636/* --- Splash Screen shared library support --- */ 637 638static JavaVM* SetJavaVMValue() 639{ 640 JavaVM * jvm = NULL; 641 642 // The handle is good for both the launcher and the libosxapp.dylib 643 void * handle = dlopen(NULL, RTLD_LAZY | RTLD_GLOBAL); 644 if (handle) { 645 typedef JavaVM* (*JLI_GetJavaVMInstance_t)(); 646 647 JLI_GetJavaVMInstance_t JLI_GetJavaVMInstance = 648 (JLI_GetJavaVMInstance_t)dlsym(handle, 649 "JLI_GetJavaVMInstance"); 650 if (JLI_GetJavaVMInstance) { 651 jvm = JLI_GetJavaVMInstance(); 652 } 653 654 if (jvm) { 655 typedef void (*OSXAPP_SetJavaVM_t)(JavaVM*); 656 657 OSXAPP_SetJavaVM_t OSXAPP_SetJavaVM = 658 (OSXAPP_SetJavaVM_t)dlsym(handle, "OSXAPP_SetJavaVM"); 659 if (OSXAPP_SetJavaVM) { 660 OSXAPP_SetJavaVM(jvm); 661 } else { 662 jvm = NULL; 663 } 664 } 665 666 dlclose(handle); 667 } 668 669 return jvm; 670} 671 672static const char* SPLASHSCREEN_SO = JNI_LIB_NAME("splashscreen"); 673 674static void* hSplashLib = NULL; 675 676void* SplashProcAddress(const char* name) { 677 if (!hSplashLib) { 678 char jrePath[PATH_MAX]; 679 if (!GetJREPath(jrePath, sizeof(jrePath), JNI_FALSE)) { 680 JLI_ReportErrorMessage(JRE_ERROR1); 681 return NULL; 682 } 683 684 char splashPath[PATH_MAX]; 685 const int ret = JLI_Snprintf(splashPath, sizeof(splashPath), 686 "%s/lib/%s", jrePath, SPLASHSCREEN_SO); 687 if (ret >= (int)sizeof(splashPath)) { 688 JLI_ReportErrorMessage(JRE_ERROR11); 689 return NULL; 690 } 691 if (ret < 0) { 692 JLI_ReportErrorMessage(JRE_ERROR13); 693 return NULL; 694 } 695 696 hSplashLib = dlopen(splashPath, RTLD_LAZY | RTLD_GLOBAL); 697 // It's OK if dlopen() fails. The splash screen library binary file 698 // might have been stripped out from the JRE image to reduce its size 699 // (e.g. on embedded platforms). 700 701 if (hSplashLib) { 702 if (!SetJavaVMValue()) { 703 dlclose(hSplashLib); 704 hSplashLib = NULL; 705 } 706 } 707 } 708 if (hSplashLib) { 709 void* sym = dlsym(hSplashLib, name); 710 return sym; 711 } else { 712 return NULL; 713 } 714} 715 716/* 717 * Signature adapter for pthread_create(). 718 */ 719static void* ThreadJavaMain(void* args) { 720 return (void*)(intptr_t)JavaMain(args); 721} 722 723/* 724 * Block current thread and continue execution in a new thread. 725 */ 726int 727CallJavaMainInNewThread(jlong stack_size, void* args) { 728 int rslt; 729 pthread_t tid; 730 pthread_attr_t attr; 731 pthread_attr_init(&attr); 732 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); 733 734 if (stack_size > 0) { 735 pthread_attr_setstacksize(&attr, stack_size); 736 } 737 pthread_attr_setguardsize(&attr, 0); // no pthread guard page on java threads 738 739 if (pthread_create(&tid, &attr, ThreadJavaMain, args) == 0) { 740 void* tmp; 741 pthread_join(tid, &tmp); 742 rslt = (int)(intptr_t)tmp; 743 } else { 744 /* 745 * Continue execution in current thread if for some reason (e.g. out of 746 * memory/LWP) a new thread can't be created. This will likely fail 747 * later in JavaMain as JNI_CreateJavaVM needs to create quite a 748 * few new threads, anyway, just give it a try.. 749 */ 750 rslt = JavaMain(args); 751 } 752 753 pthread_attr_destroy(&attr); 754 return rslt; 755} 756 757static JavaVM* jvmInstance = NULL; 758static jboolean sameThread = JNI_FALSE; /* start VM in current thread */ 759 760/* 761 * Note there is a callback on this function from the splashscreen logic, 762 * this as well SetJavaVMValue() needs to be simplified. 763 */ 764JNIEXPORT JavaVM* JNICALL 765JLI_GetJavaVMInstance() 766{ 767 return jvmInstance; 768} 769 770void 771RegisterThread() 772{ 773 // stubbed out for windows and *nixes. 774} 775 776static void 777SetXDockArgForAWT(const char *arg) 778{ 779 char envVar[80]; 780 if (strstr(arg, "-Xdock:name=") == arg) { 781 /* 782 * The APP_NAME_<pid> environment variable is used to pass 783 * an application name as specified with the -Xdock:name command 784 * line option from Java launcher code to the AWT code in order 785 * to assign this name to the app's dock tile on the Mac. 786 * The _<pid> part is added to avoid collisions with child processes. 787 * 788 * WARNING: This environment variable is an implementation detail and 789 * isn't meant for use outside of the core platform. The mechanism for 790 * passing this information from Java launcher to other modules may 791 * change drastically between update release, and it may even be 792 * removed or replaced with another mechanism. 793 * 794 * NOTE: It is used by SWT, and JavaFX. 795 */ 796 snprintf(envVar, sizeof(envVar), "APP_NAME_%d", getpid()); 797 setenv(envVar, (arg + 12), 1); 798 } 799 800 if (strstr(arg, "-Xdock:icon=") == arg) { 801 /* 802 * The APP_ICON_<pid> environment variable is used to pass 803 * an application icon as specified with the -Xdock:icon command 804 * line option from Java launcher code to the AWT code in order 805 * to assign this icon to the app's dock tile on the Mac. 806 * The _<pid> part is added to avoid collisions with child processes. 807 * 808 * WARNING: This environment variable is an implementation detail and 809 * isn't meant for use outside of the core platform. The mechanism for 810 * passing this information from Java launcher to other modules may 811 * change drastically between update release, and it may even be 812 * removed or replaced with another mechanism. 813 * 814 * NOTE: It is used by SWT, and JavaFX. 815 */ 816 snprintf(envVar, sizeof(envVar), "APP_ICON_%d", getpid()); 817 setenv(envVar, (arg + 12), 1); 818 } 819} 820 821static void 822SetMainClassForAWT(JNIEnv *env, jclass mainClass) { 823 jclass classClass = NULL; 824 NULL_CHECK(classClass = FindBootStrapClass(env, "java/lang/Class")); 825 826 jmethodID getCanonicalNameMID = NULL; 827 NULL_CHECK(getCanonicalNameMID = (*env)->GetMethodID(env, classClass, "getCanonicalName", "()Ljava/lang/String;")); 828 829 jstring mainClassString = (*env)->CallObjectMethod(env, mainClass, getCanonicalNameMID); 830 if ((*env)->ExceptionCheck(env)) { 831 /* 832 * Clears all errors caused by getCanonicalName() on the mainclass and 833 * leaves the JAVA_MAIN_CLASS__<pid> empty. 834 */ 835 (*env)->ExceptionClear(env); 836 return; 837 } 838 839 const char *mainClassName = NULL; 840 NULL_CHECK(mainClassName = (*env)->GetStringUTFChars(env, mainClassString, NULL)); 841 842 char envVar[80]; 843 /* 844 * The JAVA_MAIN_CLASS_<pid> environment variable is used to pass 845 * the name of a Java class whose main() method is invoked by 846 * the Java launcher code to start the application, to the AWT code 847 * in order to assign the name to the Apple menu bar when the app 848 * is active on the Mac. 849 * The _<pid> part is added to avoid collisions with child processes. 850 * 851 * WARNING: This environment variable is an implementation detail and 852 * isn't meant for use outside of the core platform. The mechanism for 853 * passing this information from Java launcher to other modules may 854 * change drastically between update release, and it may even be 855 * removed or replaced with another mechanism. 856 * 857 * NOTE: It is used by SWT, and JavaFX. 858 */ 859 snprintf(envVar, sizeof(envVar), "JAVA_MAIN_CLASS_%d", getpid()); 860 setenv(envVar, mainClassName, 1); 861 862 (*env)->ReleaseStringUTFChars(env, mainClassString, mainClassName); 863} 864 865void 866SetXStartOnFirstThreadArg() 867{ 868 // XXX: BEGIN HACK 869 // short circuit hack for <https://bugs.eclipse.org/bugs/show_bug.cgi?id=211625> 870 // need a way to get AWT/Swing apps launched when spawned from Eclipse, 871 // which currently has no UI to not pass the -XstartOnFirstThread option 872 if (getenv("HACK_IGNORE_START_ON_FIRST_THREAD") != NULL) return; 873 // XXX: END HACK 874 875 sameThread = JNI_TRUE; 876 // Set a variable that tells us we started on the main thread. 877 // This is used by the AWT during startup. (See LWCToolkit.m) 878 char envVar[80]; 879 snprintf(envVar, sizeof(envVar), "JAVA_STARTED_ON_FIRST_THREAD_%d", getpid()); 880 setenv(envVar, "1", 1); 881} 882 883// MacOSX we may continue in the same thread 884int 885JVMInit(InvocationFunctions* ifn, jlong threadStackSize, 886 int argc, char **argv, 887 int mode, char *what, int ret) { 888 if (sameThread) { 889 JLI_TraceLauncher("In same thread\n"); 890 // need to block this thread against the main thread 891 // so signals get caught correctly 892 __block int rslt = 0; 893 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 894 { 895 NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock: ^{ 896 JavaMainArgs args; 897 args.argc = argc; 898 args.argv = argv; 899 args.mode = mode; 900 args.what = what; 901 args.ifn = *ifn; 902 rslt = JavaMain(&args); 903 }]; 904 905 /* 906 * We cannot use dispatch_sync here, because it blocks the main dispatch queue. 907 * Using the main NSRunLoop allows the dispatch queue to run properly once 908 * SWT (or whatever toolkit this is needed for) kicks off it's own NSRunLoop 909 * and starts running. 910 */ 911 [op performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:YES]; 912 } 913 [pool drain]; 914 return rslt; 915 } else { 916 return ContinueInNewThread(ifn, threadStackSize, argc, argv, mode, what, ret); 917 } 918} 919 920/* 921 * Note the jvmInstance must be initialized first before entering into 922 * ShowSplashScreen, as there is a callback into the JLI_GetJavaVMInstance. 923 */ 924void PostJVMInit(JNIEnv *env, jclass mainClass, JavaVM *vm) { 925 jvmInstance = vm; 926 SetMainClassForAWT(env, mainClass); 927 CHECK_EXCEPTION_RETURN(); 928 ShowSplashScreen(); 929} 930 931jboolean 932ProcessPlatformOption(const char* arg) 933{ 934 if (JLI_StrCmp(arg, "-XstartOnFirstThread") == 0) { 935 SetXStartOnFirstThreadArg(); 936 return JNI_TRUE; 937 } else if (JLI_StrCCmp(arg, "-Xdock:") == 0) { 938 SetXDockArgForAWT(arg); 939 return JNI_TRUE; 940 } 941 // arguments we know not 942 return JNI_FALSE; 943} 944