0N/A/*
4680N/A * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#include <windows.h>
0N/A#include <io.h>
0N/A#include <process.h>
0N/A#include <stdlib.h>
0N/A#include <stdio.h>
0N/A#include <stdarg.h>
0N/A#include <string.h>
0N/A#include <sys/types.h>
0N/A#include <sys/stat.h>
0N/A#include <wtypes.h>
16N/A#include <commctrl.h>
0N/A
0N/A#include <jni.h>
0N/A#include "java.h"
0N/A#include "version_comp.h"
0N/A
0N/A#define JVM_DLL "jvm.dll"
0N/A#define JAVA_DLL "java.dll"
0N/A
0N/A/*
0N/A * Prototypes.
0N/A */
0N/Astatic jboolean GetPublicJREHome(char *path, jint pathsize);
0N/Astatic jboolean GetJVMPath(const char *jrepath, const char *jvmtype,
0N/A char *jvmpath, jint jvmpathsize);
0N/Astatic jboolean GetJREPath(char *path, jint pathsize);
0N/A
2963N/A/* We supports warmup for UI stack that is performed in parallel
2963N/A * to VM initialization.
2963N/A * This helps to improve startup of UI application as warmup phase
2963N/A * might be long due to initialization of OS or hardware resources.
2963N/A * It is not CPU bound and therefore it does not interfere with VM init.
2963N/A * Obviously such warmup only has sense for UI apps and therefore it needs
2963N/A * to be explicitly requested by passing -Dsun.awt.warmup=true property
2963N/A * (this is always the case for plugin/javaws).
2963N/A *
2963N/A * Implementation launches new thread after VM starts and use it to perform
2963N/A * warmup code (platform dependent).
2963N/A * This thread is later reused as AWT toolkit thread as graphics toolkit
2963N/A * often assume that they are used from the same thread they were launched on.
2963N/A *
2963N/A * At the moment we only support warmup for D3D. It only possible on windows
2963N/A * and only if other flags do not prohibit this (e.g. OpenGL support requested).
2963N/A */
2963N/A#undef ENABLE_AWT_PRELOAD
2963N/A#ifndef JAVA_ARGS /* turn off AWT preloading for javac, jar, etc */
3090N/A /* CR6999872: fastdebug crashes if awt library is loaded before JVM is
3090N/A * initialized*/
3090N/A #if !defined(DEBUG)
3090N/A #define ENABLE_AWT_PRELOAD
3090N/A #endif
2963N/A#endif
2963N/A
2963N/A#ifdef ENABLE_AWT_PRELOAD
2963N/A/* "AWT was preloaded" flag;
2963N/A * turned on by AWTPreload().
2963N/A */
2963N/Aint awtPreloaded = 0;
2963N/A
2963N/A/* Calls a function with the name specified
2963N/A * the function must be int(*fn)(void).
2963N/A */
2963N/Aint AWTPreload(const char *funcName);
2963N/A/* stops AWT preloading */
2963N/Avoid AWTPreloadStop();
2963N/A
2963N/A/* D3D preloading */
2963N/A/* -1: not initialized; 0: OFF, 1: ON */
2963N/Aint awtPreloadD3D = -1;
2963N/A/* command line parameter to swith D3D preloading on */
2963N/A#define PARAM_PRELOAD_D3D "-Dsun.awt.warmup"
2963N/A/* D3D/OpenGL management parameters */
2963N/A#define PARAM_NODDRAW "-Dsun.java2d.noddraw"
2963N/A#define PARAM_D3D "-Dsun.java2d.d3d"
2963N/A#define PARAM_OPENGL "-Dsun.java2d.opengl"
2963N/A/* funtion in awt.dll (src/windows/native/sun/java2d/d3d/D3DPipelineManager.cpp) */
2963N/A#define D3D_PRELOAD_FUNC "preloadD3D"
2963N/A
2963N/A/* Extracts value of a parameter with the specified name
2963N/A * from command line argument (returns pointer in the argument).
2963N/A * Returns NULL if the argument does not contains the parameter.
2963N/A * e.g.:
2963N/A * GetParamValue("theParam", "theParam=value") returns pointer to "value".
2963N/A */
2963N/Aconst char * GetParamValue(const char *paramName, const char *arg) {
2963N/A int nameLen = JLI_StrLen(paramName);
2963N/A if (JLI_StrNCmp(paramName, arg, nameLen) == 0) {
2963N/A /* arg[nameLen] is valid (may contain final NULL) */
2963N/A if (arg[nameLen] == '=') {
2963N/A return arg + nameLen + 1;
2963N/A }
2963N/A }
2963N/A return NULL;
2963N/A}
2963N/A
2963N/A/* Checks if commandline argument contains property specified
2963N/A * and analyze it as boolean property (true/false).
2963N/A * Returns -1 if the argument does not contain the parameter;
2963N/A * Returns 1 if the argument contains the parameter and its value is "true";
2963N/A * Returns 0 if the argument contains the parameter and its value is "false".
2963N/A */
2963N/Aint GetBoolParamValue(const char *paramName, const char *arg) {
2963N/A const char * paramValue = GetParamValue(paramName, arg);
2963N/A if (paramValue != NULL) {
2963N/A if (JLI_StrCaseCmp(paramValue, "true") == 0) {
2963N/A return 1;
2963N/A }
2963N/A if (JLI_StrCaseCmp(paramValue, "false") == 0) {
2963N/A return 0;
2963N/A }
2963N/A }
2963N/A return -1;
2963N/A}
2963N/A#endif /* ENABLE_AWT_PRELOAD */
2963N/A
2963N/A
0N/Astatic jboolean _isjavaw = JNI_FALSE;
0N/A
0N/A
0N/Ajboolean
0N/AIsJavaw()
0N/A{
0N/A return _isjavaw;
0N/A}
0N/A
0N/A/*
0N/A * Returns the arch path, to get the current arch use the
0N/A * macro GetArch, nbits here is ignored for now.
0N/A */
0N/Aconst char *
0N/AGetArchPath(int nbits)
0N/A{
0N/A#ifdef _M_AMD64
0N/A return "amd64";
0N/A#elif defined(_M_IA64)
0N/A return "ia64";
0N/A#else
0N/A return "i386";
0N/A#endif
0N/A}
0N/A
0N/A/*
0N/A *
0N/A */
0N/Avoid
2581N/ACreateExecutionEnvironment(int *pargc, char ***pargv,
2581N/A char *jrepath, jint so_jrepath,
4680N/A char *jvmpath, jint so_jvmpath,
4680N/A char *jvmcfg, jint so_jvmcfg) {
0N/A char * jvmtype;
0N/A int i = 0;
0N/A int running = CURRENT_DATA_MODEL;
0N/A
0N/A int wanted = running;
0N/A
2581N/A char** argv = *pargv;
2581N/A for (i = 0; i < *pargc ; i++) {
2581N/A if (JLI_StrCmp(argv[i], "-J-d64") == 0 || JLI_StrCmp(argv[i], "-d64") == 0) {
0N/A wanted = 64;
0N/A continue;
0N/A }
2581N/A if (JLI_StrCmp(argv[i], "-J-d32") == 0 || JLI_StrCmp(argv[i], "-d32") == 0) {
0N/A wanted = 32;
0N/A continue;
0N/A }
0N/A }
0N/A if (running != wanted) {
517N/A JLI_ReportErrorMessage(JRE_ERROR2, wanted);
0N/A exit(1);
0N/A }
0N/A
0N/A /* Find out where the JRE is that we will be using. */
0N/A if (!GetJREPath(jrepath, so_jrepath)) {
517N/A JLI_ReportErrorMessage(JRE_ERROR1);
0N/A exit(2);
0N/A }
0N/A
4680N/A JLI_Snprintf(jvmcfg, so_jvmcfg, "%s%slib%s%s%sjvm.cfg",
5694N/A jrepath, FILESEP, FILESEP, (char*)GetArch(), FILESEP);
4680N/A
0N/A /* Find the specified JVM type */
4680N/A if (ReadKnownVMs(jvmcfg, JNI_FALSE) < 1) {
517N/A JLI_ReportErrorMessage(CFG_ERROR7);
0N/A exit(1);
0N/A }
2581N/A
2581N/A jvmtype = CheckJvmType(pargc, pargv, JNI_FALSE);
2581N/A if (JLI_StrCmp(jvmtype, "ERROR") == 0) {
2581N/A JLI_ReportErrorMessage(CFG_ERROR9);
2581N/A exit(4);
2581N/A }
0N/A
0N/A jvmpath[0] = '\0';
0N/A if (!GetJVMPath(jrepath, jvmtype, jvmpath, so_jvmpath)) {
517N/A JLI_ReportErrorMessage(CFG_ERROR8, jvmtype, jvmpath);
0N/A exit(4);
0N/A }
0N/A /* If we got here, jvmpath has been correctly initialized. */
2963N/A
2963N/A /* Check if we need preload AWT */
2963N/A#ifdef ENABLE_AWT_PRELOAD
2963N/A argv = *pargv;
2963N/A for (i = 0; i < *pargc ; i++) {
2963N/A /* Tests the "turn on" parameter only if not set yet. */
2963N/A if (awtPreloadD3D < 0) {
2963N/A if (GetBoolParamValue(PARAM_PRELOAD_D3D, argv[i]) == 1) {
2963N/A awtPreloadD3D = 1;
2963N/A }
2963N/A }
2963N/A /* Test parameters which can disable preloading if not already disabled. */
2963N/A if (awtPreloadD3D != 0) {
2963N/A if (GetBoolParamValue(PARAM_NODDRAW, argv[i]) == 1
2963N/A || GetBoolParamValue(PARAM_D3D, argv[i]) == 0
2963N/A || GetBoolParamValue(PARAM_OPENGL, argv[i]) == 1)
2963N/A {
2963N/A awtPreloadD3D = 0;
2963N/A /* no need to test the rest of the parameters */
2963N/A break;
2963N/A }
2963N/A }
2963N/A }
2963N/A#endif /* ENABLE_AWT_PRELOAD */
0N/A}
0N/A
1365N/A
1365N/Astatic jboolean
1365N/ALoadMSVCRT()
1365N/A{
1365N/A // Only do this once
1365N/A static int loaded = 0;
1365N/A char crtpath[MAXPATHLEN];
1365N/A
1365N/A if (!loaded) {
1365N/A /*
1365N/A * The Microsoft C Runtime Library needs to be loaded first. A copy is
1365N/A * assumed to be present in the "JRE path" directory. If it is not found
1365N/A * there (or "JRE path" fails to resolve), skip the explicit load and let
1365N/A * nature take its course, which is likely to be a failure to execute.
2324N/A * This is clearly completely specific to the exact compiler version
2324N/A * which isn't very nice, but its hardly the only place.
2324N/A * No attempt to look for compiler versions in between 2003 and 2010
2324N/A * as we aren't supporting building with those.
1365N/A */
1365N/A#ifdef _MSC_VER
1365N/A#if _MSC_VER < 1400
1365N/A#define CRT_DLL "msvcr71.dll"
1365N/A#endif
2324N/A#if _MSC_VER >= 1600
2324N/A#define CRT_DLL "msvcr100.dll"
2324N/A#endif
1365N/A#ifdef CRT_DLL
1365N/A if (GetJREPath(crtpath, MAXPATHLEN)) {
5694N/A if (JLI_StrLen(crtpath) + JLI_StrLen("\\bin\\") +
5694N/A JLI_StrLen(CRT_DLL) >= MAXPATHLEN) {
2746N/A JLI_ReportErrorMessage(JRE_ERROR11);
2746N/A return JNI_FALSE;
2746N/A }
1365N/A (void)JLI_StrCat(crtpath, "\\bin\\" CRT_DLL); /* Add crt dll */
1365N/A JLI_TraceLauncher("CRT path is %s\n", crtpath);
1365N/A if (_access(crtpath, 0) == 0) {
1365N/A if (LoadLibrary(crtpath) == 0) {
1365N/A JLI_ReportErrorMessage(DLL_ERROR4, crtpath);
1365N/A return JNI_FALSE;
1365N/A }
1365N/A }
1365N/A }
1365N/A#endif /* CRT_DLL */
1365N/A#endif /* _MSC_VER */
1365N/A loaded = 1;
1365N/A }
1365N/A return JNI_TRUE;
1365N/A}
1365N/A
1365N/A
0N/A/*
0N/A * Find path to JRE based on .exe's location or registry settings.
0N/A */
0N/Ajboolean
0N/AGetJREPath(char *path, jint pathsize)
0N/A{
0N/A char javadll[MAXPATHLEN];
0N/A struct stat s;
0N/A
0N/A if (GetApplicationHome(path, pathsize)) {
0N/A /* Is JRE co-located with the application? */
2581N/A JLI_Snprintf(javadll, sizeof(javadll), "%s\\bin\\" JAVA_DLL, path);
0N/A if (stat(javadll, &s) == 0) {
2581N/A JLI_TraceLauncher("JRE path is %s\n", path);
2581N/A return JNI_TRUE;
0N/A }
0N/A
0N/A /* Does this app ship a private JRE in <apphome>\jre directory? */
2581N/A JLI_Snprintf(javadll, sizeof (javadll), "%s\\jre\\bin\\" JAVA_DLL, path);
0N/A if (stat(javadll, &s) == 0) {
0N/A JLI_StrCat(path, "\\jre");
2581N/A JLI_TraceLauncher("JRE path is %s\n", path);
2581N/A return JNI_TRUE;
0N/A }
0N/A }
0N/A
0N/A /* Look for a public JRE on this machine. */
0N/A if (GetPublicJREHome(path, pathsize)) {
2581N/A JLI_TraceLauncher("JRE path is %s\n", path);
2581N/A return JNI_TRUE;
0N/A }
0N/A
517N/A JLI_ReportErrorMessage(JRE_ERROR8 JAVA_DLL);
0N/A return JNI_FALSE;
0N/A
0N/A}
0N/A
0N/A/*
0N/A * Given a JRE location and a JVM type, construct what the name the
0N/A * JVM shared library will be. Return true, if such a library
0N/A * exists, false otherwise.
0N/A */
0N/Astatic jboolean
0N/AGetJVMPath(const char *jrepath, const char *jvmtype,
0N/A char *jvmpath, jint jvmpathsize)
0N/A{
0N/A struct stat s;
0N/A if (JLI_StrChr(jvmtype, '/') || JLI_StrChr(jvmtype, '\\')) {
2581N/A JLI_Snprintf(jvmpath, jvmpathsize, "%s\\" JVM_DLL, jvmtype);
0N/A } else {
5694N/A JLI_Snprintf(jvmpath, jvmpathsize, "%s\\bin\\%s\\" JVM_DLL,
5694N/A jrepath, jvmtype);
0N/A }
0N/A if (stat(jvmpath, &s) == 0) {
0N/A return JNI_TRUE;
0N/A } else {
0N/A return JNI_FALSE;
0N/A }
0N/A}
0N/A
0N/A/*
0N/A * Load a jvm from "jvmpath" and initialize the invocation functions.
0N/A */
0N/Ajboolean
0N/ALoadJavaVM(const char *jvmpath, InvocationFunctions *ifn)
0N/A{
0N/A HINSTANCE handle;
0N/A
0N/A JLI_TraceLauncher("JVM path is %s\n", jvmpath);
0N/A
0N/A /*
0N/A * The Microsoft C Runtime Library needs to be loaded first. A copy is
0N/A * assumed to be present in the "JRE path" directory. If it is not found
0N/A * there (or "JRE path" fails to resolve), skip the explicit load and let
0N/A * nature take its course, which is likely to be a failure to execute.
795N/A *
0N/A */
1365N/A LoadMSVCRT();
0N/A
0N/A /* Load the Java VM DLL */
0N/A if ((handle = LoadLibrary(jvmpath)) == 0) {
517N/A JLI_ReportErrorMessage(DLL_ERROR4, (char *)jvmpath);
0N/A return JNI_FALSE;
0N/A }
0N/A
0N/A /* Now get the function addresses */
0N/A ifn->CreateJavaVM =
0N/A (void *)GetProcAddress(handle, "JNI_CreateJavaVM");
0N/A ifn->GetDefaultJavaVMInitArgs =
0N/A (void *)GetProcAddress(handle, "JNI_GetDefaultJavaVMInitArgs");
0N/A if (ifn->CreateJavaVM == 0 || ifn->GetDefaultJavaVMInitArgs == 0) {
517N/A JLI_ReportErrorMessage(JNI_ERROR1, (char *)jvmpath);
0N/A return JNI_FALSE;
0N/A }
0N/A
0N/A return JNI_TRUE;
0N/A}
0N/A
0N/A/*
0N/A * If app is "c:\foo\bin\javac", then put "c:\foo" into buf.
0N/A */
0N/Ajboolean
0N/AGetApplicationHome(char *buf, jint bufsize)
0N/A{
0N/A char *cp;
0N/A GetModuleFileName(0, buf, bufsize);
0N/A *JLI_StrRChr(buf, '\\') = '\0'; /* remove .exe file name */
0N/A if ((cp = JLI_StrRChr(buf, '\\')) == 0) {
0N/A /* This happens if the application is in a drive root, and
0N/A * there is no bin directory. */
0N/A buf[0] = '\0';
0N/A return JNI_FALSE;
0N/A }
0N/A *cp = '\0'; /* remove the bin\ part */
0N/A return JNI_TRUE;
0N/A}
0N/A
0N/A/*
0N/A * Helpers to look in the registry for a public JRE.
0N/A */
0N/A /* Same for 1.5.0, 1.5.1, 1.5.2 etc. */
0N/A#define JRE_KEY "Software\\JavaSoft\\Java Runtime Environment"
0N/A
0N/Astatic jboolean
0N/AGetStringFromRegistry(HKEY key, const char *name, char *buf, jint bufsize)
0N/A{
0N/A DWORD type, size;
0N/A
0N/A if (RegQueryValueEx(key, name, 0, &type, 0, &size) == 0
0N/A && type == REG_SZ
0N/A && (size < (unsigned int)bufsize)) {
0N/A if (RegQueryValueEx(key, name, 0, 0, buf, &size) == 0) {
0N/A return JNI_TRUE;
0N/A }
0N/A }
0N/A return JNI_FALSE;
0N/A}
0N/A
0N/Astatic jboolean
0N/AGetPublicJREHome(char *buf, jint bufsize)
0N/A{
0N/A HKEY key, subkey;
0N/A char version[MAXPATHLEN];
0N/A
0N/A /*
0N/A * Note: There is a very similar implementation of the following
0N/A * registry reading code in the Windows java control panel (javacp.cpl).
0N/A * If there are bugs here, a similar bug probably exists there. Hence,
0N/A * changes here require inspection there.
0N/A */
0N/A
0N/A /* Find the current version of the JRE */
0N/A if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, JRE_KEY, 0, KEY_READ, &key) != 0) {
517N/A JLI_ReportErrorMessage(REG_ERROR1, JRE_KEY);
0N/A return JNI_FALSE;
0N/A }
0N/A
0N/A if (!GetStringFromRegistry(key, "CurrentVersion",
0N/A version, sizeof(version))) {
517N/A JLI_ReportErrorMessage(REG_ERROR2, JRE_KEY);
0N/A RegCloseKey(key);
0N/A return JNI_FALSE;
0N/A }
0N/A
0N/A if (JLI_StrCmp(version, GetDotVersion()) != 0) {
517N/A JLI_ReportErrorMessage(REG_ERROR3, JRE_KEY, version, GetDotVersion()
0N/A );
0N/A RegCloseKey(key);
0N/A return JNI_FALSE;
0N/A }
0N/A
0N/A /* Find directory where the current version is installed. */
0N/A if (RegOpenKeyEx(key, version, 0, KEY_READ, &subkey) != 0) {
517N/A JLI_ReportErrorMessage(REG_ERROR1, JRE_KEY, version);
0N/A RegCloseKey(key);
0N/A return JNI_FALSE;
0N/A }
0N/A
0N/A if (!GetStringFromRegistry(subkey, "JavaHome", buf, bufsize)) {
517N/A JLI_ReportErrorMessage(REG_ERROR4, JRE_KEY, version);
0N/A RegCloseKey(key);
0N/A RegCloseKey(subkey);
0N/A return JNI_FALSE;
0N/A }
0N/A
0N/A if (JLI_IsTraceLauncher()) {
0N/A char micro[MAXPATHLEN];
0N/A if (!GetStringFromRegistry(subkey, "MicroVersion", micro,
0N/A sizeof(micro))) {
0N/A printf("Warning: Can't read MicroVersion\n");
0N/A micro[0] = '\0';
0N/A }
0N/A printf("Version major.minor.micro = %s.%s\n", version, micro);
0N/A }
0N/A
0N/A RegCloseKey(key);
0N/A RegCloseKey(subkey);
0N/A return JNI_TRUE;
0N/A}
0N/A
0N/A/*
0N/A * Support for doing cheap, accurate interval timing.
0N/A */
0N/Astatic jboolean counterAvailable = JNI_FALSE;
0N/Astatic jboolean counterInitialized = JNI_FALSE;
0N/Astatic LARGE_INTEGER counterFrequency;
0N/A
0N/Ajlong CounterGet()
0N/A{
0N/A LARGE_INTEGER count;
0N/A
0N/A if (!counterInitialized) {
0N/A counterAvailable = QueryPerformanceFrequency(&counterFrequency);
0N/A counterInitialized = JNI_TRUE;
0N/A }
0N/A if (!counterAvailable) {
0N/A return 0;
0N/A }
0N/A QueryPerformanceCounter(&count);
0N/A return (jlong)(count.QuadPart);
0N/A}
0N/A
0N/Ajlong Counter2Micros(jlong counts)
0N/A{
0N/A if (!counterAvailable || !counterInitialized) {
0N/A return 0;
0N/A }
0N/A return (counts * 1000 * 1000)/counterFrequency.QuadPart;
0N/A}
5694N/A/*
5694N/A * windows snprintf does not guarantee a null terminator in the buffer,
5694N/A * if the computed size is equal to or greater than the buffer size,
5700N/A * as well as error conditions. This function guarantees a null terminator
5700N/A * under all these conditions. An unreasonable buffer or size will return
5700N/A * an error value. Under all other conditions this function will return the
5700N/A * size of the bytes actually written minus the null terminator, similar
5700N/A * to ansi snprintf api. Thus when calling this function the caller must
5700N/A * ensure storage for the null terminator.
5694N/A */
5700N/Aint
5700N/AJLI_Snprintf(char* buffer, size_t size, const char* format, ...) {
5700N/A int rc;
5694N/A va_list vl;
5700N/A if (size == 0 || buffer == NULL)
5694N/A return -1;
5700N/A buffer[0] = '\0';
5694N/A va_start(vl, format);
5700N/A rc = vsnprintf(buffer, size, format, vl);
5700N/A va_end(vl);
5694N/A /* force a null terminator, if something is amiss */
5700N/A if (rc < 0) {
5700N/A /* apply ansi semantics */
5694N/A buffer[size - 1] = '\0';
5700N/A return size;
5700N/A } else if (rc == size) {
5700N/A /* force a null terminator */
5700N/A buffer[size - 1] = '\0';
5700N/A }
5694N/A return rc;
5694N/A}
5694N/A
0N/Avoid
517N/AJLI_ReportErrorMessage(const char* fmt, ...) {
0N/A va_list vl;
0N/A va_start(vl,fmt);
0N/A
0N/A if (IsJavaw()) {
0N/A char *message;
0N/A
0N/A /* get the length of the string we need */
0N/A int n = _vscprintf(fmt, vl);
0N/A
0N/A message = (char *)JLI_MemAlloc(n + 1);
0N/A _vsnprintf(message, n, fmt, vl);
0N/A message[n]='\0';
0N/A MessageBox(NULL, message, "Java Virtual Machine Launcher",
0N/A (MB_OK|MB_ICONSTOP|MB_APPLMODAL));
0N/A JLI_MemFree(message);
0N/A } else {
0N/A vfprintf(stderr, fmt, vl);
0N/A fprintf(stderr, "\n");
0N/A }
0N/A va_end(vl);
0N/A}
0N/A
0N/A/*
517N/A * Just like JLI_ReportErrorMessage, except that it concatenates the system
0N/A * error message if any, its upto the calling routine to correctly
0N/A * format the separation of the messages.
0N/A */
0N/Avoid
517N/AJLI_ReportErrorMessageSys(const char *fmt, ...)
0N/A{
0N/A va_list vl;
0N/A
0N/A int save_errno = errno;
0N/A DWORD errval;
0N/A jboolean freeit = JNI_FALSE;
0N/A char *errtext = NULL;
0N/A
0N/A va_start(vl, fmt);
0N/A
0N/A if ((errval = GetLastError()) != 0) { /* Platform SDK / DOS Error */
0N/A int n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|
0N/A FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_ALLOCATE_BUFFER,
0N/A NULL, errval, 0, (LPTSTR)&errtext, 0, NULL);
0N/A if (errtext == NULL || n == 0) { /* Paranoia check */
0N/A errtext = "";
0N/A n = 0;
0N/A } else {
0N/A freeit = JNI_TRUE;
0N/A if (n > 2) { /* Drop final CR, LF */
0N/A if (errtext[n - 1] == '\n') n--;
0N/A if (errtext[n - 1] == '\r') n--;
0N/A errtext[n] = '\0';
0N/A }
0N/A }
0N/A } else { /* C runtime error that has no corresponding DOS error code */
0N/A errtext = strerror(save_errno);
0N/A }
0N/A
0N/A if (IsJavaw()) {
0N/A char *message;
0N/A int mlen;
0N/A /* get the length of the string we need */
0N/A int len = mlen = _vscprintf(fmt, vl) + 1;
0N/A if (freeit) {
2987N/A mlen += (int)JLI_StrLen(errtext);
0N/A }
0N/A
0N/A message = (char *)JLI_MemAlloc(mlen);
0N/A _vsnprintf(message, len, fmt, vl);
0N/A message[len]='\0';
0N/A
0N/A if (freeit) {
0N/A JLI_StrCat(message, errtext);
0N/A }
0N/A
0N/A MessageBox(NULL, message, "Java Virtual Machine Launcher",
0N/A (MB_OK|MB_ICONSTOP|MB_APPLMODAL));
0N/A
0N/A JLI_MemFree(message);
0N/A } else {
0N/A vfprintf(stderr, fmt, vl);
0N/A if (freeit) {
0N/A fprintf(stderr, "%s", errtext);
0N/A }
0N/A }
0N/A if (freeit) {
0N/A (void)LocalFree((HLOCAL)errtext);
0N/A }
0N/A va_end(vl);
0N/A}
0N/A
517N/Avoid JLI_ReportExceptionDescription(JNIEnv * env) {
0N/A if (IsJavaw()) {
0N/A /*
0N/A * This code should be replaced by code which opens a window with
0N/A * the exception detail message, for now atleast put a dialog up.
0N/A */
0N/A MessageBox(NULL, "A Java Exception has occurred.", "Java Virtual Machine Launcher",
0N/A (MB_OK|MB_ICONSTOP|MB_APPLMODAL));
0N/A } else {
0N/A (*env)->ExceptionDescribe(env);
0N/A }
0N/A}
0N/A
0N/Ajboolean
0N/AServerClassMachine() {
0N/A return (GetErgoPolicy() == ALWAYS_SERVER_CLASS) ? JNI_TRUE : JNI_FALSE;
0N/A}
0N/A
0N/A/*
0N/A * Determine if there is an acceptable JRE in the registry directory top_key.
0N/A * Upon locating the "best" one, return a fully qualified path to it.
0N/A * "Best" is defined as the most advanced JRE meeting the constraints
0N/A * contained in the manifest_info. If no JRE in this directory meets the
0N/A * constraints, return NULL.
0N/A *
0N/A * It doesn't matter if we get an error reading the registry, or we just
0N/A * don't find anything interesting in the directory. We just return NULL
0N/A * in either case.
0N/A */
0N/Astatic char *
0N/AProcessDir(manifest_info* info, HKEY top_key) {
0N/A DWORD index = 0;
0N/A HKEY ver_key;
0N/A char name[MAXNAMELEN];
0N/A int len;
0N/A char *best = NULL;
0N/A
0N/A /*
0N/A * Enumerate "<top_key>/SOFTWARE/JavaSoft/Java Runtime Environment"
0N/A * searching for the best available version.
0N/A */
0N/A while (RegEnumKey(top_key, index, name, MAXNAMELEN) == ERROR_SUCCESS) {
0N/A index++;
0N/A if (JLI_AcceptableRelease(name, info->jre_version))
0N/A if ((best == NULL) || (JLI_ExactVersionId(name, best) > 0)) {
0N/A if (best != NULL)
0N/A JLI_MemFree(best);
0N/A best = JLI_StringDup(name);
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Extract "JavaHome" from the "best" registry directory and return
0N/A * that path. If no appropriate version was located, or there is an
0N/A * error in extracting the "JavaHome" string, return null.
0N/A */
0N/A if (best == NULL)
0N/A return (NULL);
0N/A else {
0N/A if (RegOpenKeyEx(top_key, best, 0, KEY_READ, &ver_key)
0N/A != ERROR_SUCCESS) {
0N/A JLI_MemFree(best);
0N/A if (ver_key != NULL)
0N/A RegCloseKey(ver_key);
0N/A return (NULL);
0N/A }
0N/A JLI_MemFree(best);
0N/A len = MAXNAMELEN;
0N/A if (RegQueryValueEx(ver_key, "JavaHome", NULL, NULL, (LPBYTE)name, &len)
0N/A != ERROR_SUCCESS) {
0N/A if (ver_key != NULL)
0N/A RegCloseKey(ver_key);
0N/A return (NULL);
0N/A }
0N/A if (ver_key != NULL)
0N/A RegCloseKey(ver_key);
0N/A return (JLI_StringDup(name));
0N/A }
0N/A}
0N/A
0N/A/*
0N/A * This is the global entry point. It examines the host for the optimal
0N/A * JRE to be used by scanning a set of registry entries. This set of entries
0N/A * is hardwired on Windows as "Software\JavaSoft\Java Runtime Environment"
0N/A * under the set of roots "{ HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE }".
0N/A *
0N/A * This routine simply opens each of these registry directories before passing
0N/A * control onto ProcessDir().
0N/A */
0N/Achar *
0N/ALocateJRE(manifest_info* info) {
0N/A HKEY key = NULL;
0N/A char *path;
0N/A int key_index;
0N/A HKEY root_keys[2] = { HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE };
0N/A
0N/A for (key_index = 0; key_index <= 1; key_index++) {
0N/A if (RegOpenKeyEx(root_keys[key_index], JRE_KEY, 0, KEY_READ, &key)
0N/A == ERROR_SUCCESS)
0N/A if ((path = ProcessDir(info, key)) != NULL) {
0N/A if (key != NULL)
0N/A RegCloseKey(key);
0N/A return (path);
0N/A }
0N/A if (key != NULL)
0N/A RegCloseKey(key);
0N/A }
0N/A return NULL;
0N/A}
0N/A
0N/A/*
0N/A * Local helper routine to isolate a single token (option or argument)
0N/A * from the command line.
0N/A *
0N/A * This routine accepts a pointer to a character pointer. The first
0N/A * token (as defined by MSDN command-line argument syntax) is isolated
0N/A * from that string.
0N/A *
0N/A * Upon return, the input character pointer pointed to by the parameter s
0N/A * is updated to point to the remainding, unscanned, portion of the string,
0N/A * or to a null character if the entire string has been consummed.
0N/A *
0N/A * This function returns a pointer to a null-terminated string which
0N/A * contains the isolated first token, or to the null character if no
0N/A * token could be isolated.
0N/A *
0N/A * Note the side effect of modifying the input string s by the insertion
0N/A * of a null character, making it two strings.
0N/A *
0N/A * See "Parsing C Command-Line Arguments" in the MSDN Library for the
0N/A * parsing rule details. The rule summary from that specification is:
0N/A *
0N/A * * Arguments are delimited by white space, which is either a space or a tab.
0N/A *
0N/A * * A string surrounded by double quotation marks is interpreted as a single
0N/A * argument, regardless of white space contained within. A quoted string can
0N/A * be embedded in an argument. Note that the caret (^) is not recognized as
0N/A * an escape character or delimiter.
0N/A *
0N/A * * A double quotation mark preceded by a backslash, \", is interpreted as a
0N/A * literal double quotation mark (").
0N/A *
0N/A * * Backslashes are interpreted literally, unless they immediately precede a
0N/A * double quotation mark.
0N/A *
0N/A * * If an even number of backslashes is followed by a double quotation mark,
0N/A * then one backslash (\) is placed in the argv array for every pair of
0N/A * backslashes (\\), and the double quotation mark (") is interpreted as a
0N/A * string delimiter.
0N/A *
0N/A * * If an odd number of backslashes is followed by a double quotation mark,
0N/A * then one backslash (\) is placed in the argv array for every pair of
0N/A * backslashes (\\) and the double quotation mark is interpreted as an
0N/A * escape sequence by the remaining backslash, causing a literal double
0N/A * quotation mark (") to be placed in argv.
0N/A */
0N/Astatic char*
0N/Anextarg(char** s) {
0N/A char *p = *s;
0N/A char *head;
0N/A int slashes = 0;
0N/A int inquote = 0;
0N/A
0N/A /*
0N/A * Strip leading whitespace, which MSDN defines as only space or tab.
0N/A * (Hence, no locale specific "isspace" here.)
0N/A */
0N/A while (*p != (char)0 && (*p == ' ' || *p == '\t'))
0N/A p++;
0N/A head = p; /* Save the start of the token to return */
0N/A
0N/A /*
0N/A * Isolate a token from the command line.
0N/A */
0N/A while (*p != (char)0 && (inquote || !(*p == ' ' || *p == '\t'))) {
0N/A if (*p == '\\' && *(p+1) == '"' && slashes % 2 == 0)
0N/A p++;
0N/A else if (*p == '"')
0N/A inquote = !inquote;
0N/A slashes = (*p++ == '\\') ? slashes + 1 : 0;
0N/A }
0N/A
0N/A /*
0N/A * If the token isolated isn't already terminated in a "char zero",
0N/A * then replace the whitespace character with one and move to the
0N/A * next character.
0N/A */
0N/A if (*p != (char)0)
0N/A *p++ = (char)0;
0N/A
0N/A /*
0N/A * Update the parameter to point to the head of the remaining string
0N/A * reflecting the command line and return a pointer to the leading
0N/A * token which was isolated from the command line.
0N/A */
0N/A *s = p;
0N/A return (head);
0N/A}
0N/A
0N/A/*
0N/A * Local helper routine to return a string equivalent to the input string
0N/A * s, but with quotes removed so the result is a string as would be found
0N/A * in argv[]. The returned string should be freed by a call to JLI_MemFree().
0N/A *
0N/A * The rules for quoting (and escaped quotes) are:
0N/A *
0N/A * 1 A double quotation mark preceded by a backslash, \", is interpreted as a
0N/A * literal double quotation mark (").
0N/A *
0N/A * 2 Backslashes are interpreted literally, unless they immediately precede a
0N/A * double quotation mark.
0N/A *
0N/A * 3 If an even number of backslashes is followed by a double quotation mark,
0N/A * then one backslash (\) is placed in the argv array for every pair of
0N/A * backslashes (\\), and the double quotation mark (") is interpreted as a
0N/A * string delimiter.
0N/A *
0N/A * 4 If an odd number of backslashes is followed by a double quotation mark,
0N/A * then one backslash (\) is placed in the argv array for every pair of
0N/A * backslashes (\\) and the double quotation mark is interpreted as an
0N/A * escape sequence by the remaining backslash, causing a literal double
0N/A * quotation mark (") to be placed in argv.
0N/A */
0N/Astatic char*
0N/Aunquote(const char *s) {
0N/A const char *p = s; /* Pointer to the tail of the original string */
0N/A char *un = (char*)JLI_MemAlloc(JLI_StrLen(s) + 1); /* Ptr to unquoted string */
0N/A char *pun = un; /* Pointer to the tail of the unquoted string */
0N/A
0N/A while (*p != '\0') {
0N/A if (*p == '"') {
0N/A p++;
0N/A } else if (*p == '\\') {
0N/A const char *q = p + JLI_StrSpn(p,"\\");
0N/A if (*q == '"')
0N/A do {
0N/A *pun++ = '\\';
0N/A p += 2;
0N/A } while (*p == '\\' && p < q);
0N/A else
0N/A while (p < q)
0N/A *pun++ = *p++;
0N/A } else {
0N/A *pun++ = *p++;
0N/A }
0N/A }
0N/A *pun = '\0';
0N/A return un;
0N/A}
0N/A
0N/A/*
0N/A * Given a path to a jre to execute, this routine checks if this process
0N/A * is indeed that jre. If not, it exec's that jre.
0N/A *
0N/A * We want to actually check the paths rather than just the version string
0N/A * built into the executable, so that given version specification will yield
0N/A * the exact same Java environment, regardless of the version of the arbitrary
0N/A * launcher we start with.
0N/A */
0N/Avoid
0N/AExecJRE(char *jre, char **argv) {
5694N/A jint len;
0N/A char path[MAXPATHLEN + 1];
0N/A
0N/A const char *progname = GetProgramName();
0N/A
0N/A /*
0N/A * Resolve the real path to the currently running launcher.
0N/A */
0N/A len = GetModuleFileName(NULL, path, MAXPATHLEN + 1);
0N/A if (len == 0 || len > MAXPATHLEN) {
517N/A JLI_ReportErrorMessageSys(JRE_ERROR9, progname);
0N/A exit(1);
0N/A }
0N/A
0N/A JLI_TraceLauncher("ExecJRE: old: %s\n", path);
0N/A JLI_TraceLauncher("ExecJRE: new: %s\n", jre);
0N/A
0N/A /*
0N/A * If the path to the selected JRE directory is a match to the initial
0N/A * portion of the path to the currently executing JRE, we have a winner!
0N/A * If so, just return.
0N/A */
0N/A if (JLI_StrNCaseCmp(jre, path, JLI_StrLen(jre)) == 0)
0N/A return; /* I am the droid you were looking for */
0N/A
0N/A /*
0N/A * If this isn't the selected version, exec the selected version.
0N/A */
2746N/A JLI_Snprintf(path, sizeof(path), "%s\\bin\\%s.exe", jre, progname);
0N/A
0N/A /*
0N/A * Although Windows has an execv() entrypoint, it doesn't actually
0N/A * overlay a process: it can only create a new process and terminate
0N/A * the old process. Therefore, any processes waiting on the initial
0N/A * process wake up and they shouldn't. Hence, a chain of pseudo-zombie
0N/A * processes must be retained to maintain the proper wait semantics.
0N/A * Fortunately the image size of the launcher isn't too large at this
0N/A * time.
0N/A *
0N/A * If it weren't for this semantic flaw, the code below would be ...
0N/A *
0N/A * execv(path, argv);
517N/A * JLI_ReportErrorMessage("Error: Exec of %s failed\n", path);
0N/A * exit(1);
0N/A *
0N/A * The incorrect exec semantics could be addressed by:
0N/A *
0N/A * exit((int)spawnv(_P_WAIT, path, argv));
0N/A *
0N/A * Unfortunately, a bug in Windows spawn/exec impementation prevents
0N/A * this from completely working. All the Windows POSIX process creation
0N/A * interfaces are implemented as wrappers around the native Windows
0N/A * function CreateProcess(). CreateProcess() takes a single string
0N/A * to specify command line options and arguments, so the POSIX routine
0N/A * wrappers build a single string from the argv[] array and in the
0N/A * process, any quoting information is lost.
0N/A *
0N/A * The solution to this to get the original command line, to process it
0N/A * to remove the new multiple JRE options (if any) as was done for argv
0N/A * in the common SelectVersion() routine and finally to pass it directly
0N/A * to the native CreateProcess() Windows process control interface.
0N/A */
0N/A {
0N/A char *cmdline;
0N/A char *p;
0N/A char *np;
0N/A char *ocl;
0N/A char *ccl;
0N/A char *unquoted;
0N/A DWORD exitCode;
0N/A STARTUPINFO si;
0N/A PROCESS_INFORMATION pi;
0N/A
0N/A /*
0N/A * The following code block gets and processes the original command
0N/A * line, replacing the argv[0] equivalent in the command line with
0N/A * the path to the new executable and removing the appropriate
0N/A * Multiple JRE support options. Note that similar logic exists
0N/A * in the platform independent SelectVersion routine, but is
0N/A * replicated here due to the syntax of CreateProcess().
0N/A *
0N/A * The magic "+ 4" characters added to the command line length are
0N/A * 2 possible quotes around the path (argv[0]), a space after the
0N/A * path and a terminating null character.
0N/A */
0N/A ocl = GetCommandLine();
0N/A np = ccl = JLI_StringDup(ocl);
0N/A p = nextarg(&np); /* Discard argv[0] */
0N/A cmdline = (char *)JLI_MemAlloc(JLI_StrLen(path) + JLI_StrLen(np) + 4);
0N/A if (JLI_StrChr(path, (int)' ') == NULL && JLI_StrChr(path, (int)'\t') == NULL)
0N/A cmdline = JLI_StrCpy(cmdline, path);
0N/A else
0N/A cmdline = JLI_StrCat(JLI_StrCat(JLI_StrCpy(cmdline, "\""), path), "\"");
0N/A
0N/A while (*np != (char)0) { /* While more command-line */
0N/A p = nextarg(&np);
0N/A if (*p != (char)0) { /* If a token was isolated */
0N/A unquoted = unquote(p);
0N/A if (*unquoted == '-') { /* Looks like an option */
0N/A if (JLI_StrCmp(unquoted, "-classpath") == 0 ||
0N/A JLI_StrCmp(unquoted, "-cp") == 0) { /* Unique cp syntax */
0N/A cmdline = JLI_StrCat(JLI_StrCat(cmdline, " "), p);
0N/A p = nextarg(&np);
0N/A if (*p != (char)0) /* If a token was isolated */
0N/A cmdline = JLI_StrCat(JLI_StrCat(cmdline, " "), p);
0N/A } else if (JLI_StrNCmp(unquoted, "-version:", 9) != 0 &&
0N/A JLI_StrCmp(unquoted, "-jre-restrict-search") != 0 &&
0N/A JLI_StrCmp(unquoted, "-no-jre-restrict-search") != 0) {
0N/A cmdline = JLI_StrCat(JLI_StrCat(cmdline, " "), p);
0N/A }
0N/A } else { /* End of options */
0N/A cmdline = JLI_StrCat(JLI_StrCat(cmdline, " "), p);
0N/A cmdline = JLI_StrCat(JLI_StrCat(cmdline, " "), np);
0N/A JLI_MemFree((void *)unquoted);
0N/A break;
0N/A }
0N/A JLI_MemFree((void *)unquoted);
0N/A }
0N/A }
0N/A JLI_MemFree((void *)ccl);
0N/A
0N/A if (JLI_IsTraceLauncher()) {
0N/A np = ccl = JLI_StringDup(cmdline);
0N/A p = nextarg(&np);
0N/A printf("ReExec Command: %s (%s)\n", path, p);
0N/A printf("ReExec Args: %s\n", np);
0N/A JLI_MemFree((void *)ccl);
0N/A }
0N/A (void)fflush(stdout);
0N/A (void)fflush(stderr);
0N/A
0N/A /*
0N/A * The following code is modeled after a model presented in the
0N/A * Microsoft Technical Article "Moving Unix Applications to
0N/A * Windows NT" (March 6, 1994) and "Creating Processes" on MSDN
0N/A * (Februrary 2005). It approximates UNIX spawn semantics with
0N/A * the parent waiting for termination of the child.
0N/A */
0N/A memset(&si, 0, sizeof(si));
0N/A si.cb =sizeof(STARTUPINFO);
0N/A memset(&pi, 0, sizeof(pi));
0N/A
0N/A if (!CreateProcess((LPCTSTR)path, /* executable name */
0N/A (LPTSTR)cmdline, /* command line */
0N/A (LPSECURITY_ATTRIBUTES)NULL, /* process security attr. */
0N/A (LPSECURITY_ATTRIBUTES)NULL, /* thread security attr. */
0N/A (BOOL)TRUE, /* inherits system handles */
0N/A (DWORD)0, /* creation flags */
0N/A (LPVOID)NULL, /* environment block */
0N/A (LPCTSTR)NULL, /* current directory */
0N/A (LPSTARTUPINFO)&si, /* (in) startup information */
0N/A (LPPROCESS_INFORMATION)&pi)) { /* (out) process information */
517N/A JLI_ReportErrorMessageSys(SYS_ERROR1, path);
0N/A exit(1);
0N/A }
0N/A
0N/A if (WaitForSingleObject(pi.hProcess, INFINITE) != WAIT_FAILED) {
0N/A if (GetExitCodeProcess(pi.hProcess, &exitCode) == FALSE)
0N/A exitCode = 1;
0N/A } else {
517N/A JLI_ReportErrorMessage(SYS_ERROR2);
0N/A exitCode = 1;
0N/A }
0N/A
0N/A CloseHandle(pi.hThread);
0N/A CloseHandle(pi.hProcess);
0N/A
0N/A exit(exitCode);
0N/A }
0N/A}
0N/A
0N/A/*
0N/A * Wrapper for platform dependent unsetenv function.
0N/A */
0N/Aint
0N/AUnsetEnv(char *name)
0N/A{
0N/A int ret;
0N/A char *buf = JLI_MemAlloc(JLI_StrLen(name) + 2);
0N/A buf = JLI_StrCat(JLI_StrCpy(buf, name), "=");
0N/A ret = _putenv(buf);
0N/A JLI_MemFree(buf);
0N/A return (ret);
0N/A}
0N/A
0N/A/* --- Splash Screen shared library support --- */
0N/A
0N/Astatic const char* SPLASHSCREEN_SO = "\\bin\\splashscreen.dll";
0N/A
0N/Astatic HMODULE hSplashLib = NULL;
0N/A
0N/Avoid* SplashProcAddress(const char* name) {
0N/A char libraryPath[MAXPATHLEN]; /* some extra space for JLI_StrCat'ing SPLASHSCREEN_SO */
0N/A
0N/A if (!GetJREPath(libraryPath, MAXPATHLEN)) {
0N/A return NULL;
0N/A }
0N/A if (JLI_StrLen(libraryPath)+JLI_StrLen(SPLASHSCREEN_SO) >= MAXPATHLEN) {
0N/A return NULL;
0N/A }
0N/A JLI_StrCat(libraryPath, SPLASHSCREEN_SO);
0N/A
0N/A if (!hSplashLib) {
0N/A hSplashLib = LoadLibrary(libraryPath);
0N/A }
0N/A if (hSplashLib) {
0N/A return GetProcAddress(hSplashLib, name);
0N/A } else {
0N/A return NULL;
0N/A }
0N/A}
0N/A
0N/Avoid SplashFreeLibrary() {
0N/A if (hSplashLib) {
0N/A FreeLibrary(hSplashLib);
0N/A hSplashLib = NULL;
0N/A }
0N/A}
0N/A
0N/Aconst char *
0N/Ajlong_format_specifier() {
0N/A return "%I64d";
0N/A}
0N/A
0N/A/*
0N/A * Block current thread and continue execution in a new thread
0N/A */
0N/Aint
0N/AContinueInNewThread0(int (JNICALL *continuation)(void *), jlong stack_size, void * args) {
0N/A int rslt = 0;
0N/A unsigned thread_id;
0N/A
0N/A#ifndef STACK_SIZE_PARAM_IS_A_RESERVATION
0N/A#define STACK_SIZE_PARAM_IS_A_RESERVATION (0x10000)
0N/A#endif
0N/A
0N/A /*
0N/A * STACK_SIZE_PARAM_IS_A_RESERVATION is what we want, but it's not
0N/A * supported on older version of Windows. Try first with the flag; and
0N/A * if that fails try again without the flag. See MSDN document or HotSpot
0N/A * source (os_win32.cpp) for details.
0N/A */
0N/A HANDLE thread_handle =
0N/A (HANDLE)_beginthreadex(NULL,
0N/A (unsigned)stack_size,
0N/A continuation,
0N/A args,
0N/A STACK_SIZE_PARAM_IS_A_RESERVATION,
0N/A &thread_id);
0N/A if (thread_handle == NULL) {
0N/A thread_handle =
0N/A (HANDLE)_beginthreadex(NULL,
0N/A (unsigned)stack_size,
0N/A continuation,
0N/A args,
0N/A 0,
0N/A &thread_id);
0N/A }
2963N/A
2963N/A /* AWT preloading (AFTER main thread start) */
2963N/A#ifdef ENABLE_AWT_PRELOAD
2963N/A /* D3D preloading */
2963N/A if (awtPreloadD3D != 0) {
2963N/A char *envValue;
2963N/A /* D3D routines checks env.var J2D_D3D if no appropriate
2963N/A * command line params was specified
2963N/A */
2963N/A envValue = getenv("J2D_D3D");
2963N/A if (envValue != NULL && JLI_StrCaseCmp(envValue, "false") == 0) {
2963N/A awtPreloadD3D = 0;
2963N/A }
2963N/A /* Test that AWT preloading isn't disabled by J2D_D3D_PRELOAD env.var */
2963N/A envValue = getenv("J2D_D3D_PRELOAD");
2963N/A if (envValue != NULL && JLI_StrCaseCmp(envValue, "false") == 0) {
2963N/A awtPreloadD3D = 0;
2963N/A }
2963N/A if (awtPreloadD3D < 0) {
2963N/A /* If awtPreloadD3D is still undefined (-1), test
2963N/A * if it is turned on by J2D_D3D_PRELOAD env.var.
2963N/A * By default it's turned OFF.
2963N/A */
2963N/A awtPreloadD3D = 0;
2963N/A if (envValue != NULL && JLI_StrCaseCmp(envValue, "true") == 0) {
2963N/A awtPreloadD3D = 1;
2963N/A }
2963N/A }
2963N/A }
2963N/A if (awtPreloadD3D) {
2963N/A AWTPreload(D3D_PRELOAD_FUNC);
2963N/A }
2963N/A#endif /* ENABLE_AWT_PRELOAD */
2963N/A
0N/A if (thread_handle) {
0N/A WaitForSingleObject(thread_handle, INFINITE);
0N/A GetExitCodeThread(thread_handle, &rslt);
0N/A CloseHandle(thread_handle);
0N/A } else {
0N/A rslt = continuation(args);
0N/A }
2963N/A
2963N/A#ifdef ENABLE_AWT_PRELOAD
2963N/A if (awtPreloaded) {
2963N/A AWTPreloadStop();
2963N/A }
2963N/A#endif /* ENABLE_AWT_PRELOAD */
2963N/A
0N/A return rslt;
0N/A}
0N/A
647N/A/* Unix only, empty on windows. */
0N/Avoid SetJavaLauncherPlatformProps() {}
16N/A
647N/A/*
647N/A * The implementation for finding classes from the bootstrap
647N/A * class loader, refer to java.h
647N/A */
647N/Astatic FindClassFromBootLoader_t *findBootClass = NULL;
647N/A
647N/Ajclass FindBootStrapClass(JNIEnv *env, const char *classname)
647N/A{
647N/A HMODULE hJvm;
647N/A
647N/A if (findBootClass == NULL) {
647N/A hJvm = GetModuleHandle(JVM_DLL);
647N/A if (hJvm == NULL) return NULL;
647N/A /* need to use the demangled entry point */
647N/A findBootClass = (FindClassFromBootLoader_t *)GetProcAddress(hJvm,
1645N/A "JVM_FindClassFromBootLoader");
647N/A if (findBootClass == NULL) {
1645N/A JLI_ReportErrorMessage(DLL_ERROR4, "JVM_FindClassFromBootLoader");
647N/A return NULL;
647N/A }
647N/A }
1645N/A return findBootClass(env, classname);
647N/A}
647N/A
16N/Avoid
16N/AInitLauncher(boolean javaw)
16N/A{
16N/A INITCOMMONCONTROLSEX icx;
16N/A
16N/A /*
16N/A * Required for javaw mode MessageBox output as well as for
16N/A * HotSpot -XX:+ShowMessageBoxOnError in java mode, an empty
16N/A * flag field is sufficient to perform the basic UI initialization.
16N/A */
16N/A memset(&icx, 0, sizeof(INITCOMMONCONTROLSEX));
16N/A icx.dwSize = sizeof(INITCOMMONCONTROLSEX);
16N/A InitCommonControlsEx(&icx);
16N/A _isjavaw = javaw;
16N/A JLI_SetTraceLauncher();
16N/A}
2963N/A
2963N/A
2963N/A/* ============================== */
2963N/A/* AWT preloading */
2963N/A#ifdef ENABLE_AWT_PRELOAD
2963N/A
2963N/Atypedef int FnPreloadStart(void);
2963N/Atypedef void FnPreloadStop(void);
2963N/Astatic FnPreloadStop *fnPreloadStop = NULL;
2963N/Astatic HMODULE hPreloadAwt = NULL;
2963N/A
2963N/A/*
2963N/A * Starts AWT preloading
2963N/A */
2963N/Aint AWTPreload(const char *funcName)
2963N/A{
2963N/A int result = -1;
2963N/A /* load AWT library once (if several preload function should be called) */
2963N/A if (hPreloadAwt == NULL) {
2963N/A /* awt.dll is not loaded yet */
2963N/A char libraryPath[MAXPATHLEN];
2963N/A int jrePathLen = 0;
2963N/A HMODULE hJava = NULL;
2963N/A HMODULE hVerify = NULL;
2963N/A
2963N/A while (1) {
2963N/A /* awt.dll depends on jvm.dll & java.dll;
2963N/A * jvm.dll is already loaded, so we need only java.dll;
2963N/A * java.dll depends on MSVCRT lib & verify.dll.
2963N/A */
2963N/A if (!GetJREPath(libraryPath, MAXPATHLEN)) {
2963N/A break;
2963N/A }
2963N/A
2963N/A /* save path length */
2963N/A jrePathLen = JLI_StrLen(libraryPath);
2963N/A
2963N/A /* load msvcrt 1st */
2963N/A LoadMSVCRT();
2963N/A
2963N/A /* load verify.dll */
2963N/A JLI_StrCat(libraryPath, "\\bin\\verify.dll");
2963N/A hVerify = LoadLibrary(libraryPath);
2963N/A if (hVerify == NULL) {
2963N/A break;
2963N/A }
2963N/A
2963N/A /* restore jrePath */
2963N/A libraryPath[jrePathLen] = 0;
2963N/A /* load java.dll */
2963N/A JLI_StrCat(libraryPath, "\\bin\\" JAVA_DLL);
2963N/A hJava = LoadLibrary(libraryPath);
2963N/A if (hJava == NULL) {
2963N/A break;
2963N/A }
2963N/A
2963N/A /* restore jrePath */
2963N/A libraryPath[jrePathLen] = 0;
2963N/A /* load awt.dll */
2963N/A JLI_StrCat(libraryPath, "\\bin\\awt.dll");
2963N/A hPreloadAwt = LoadLibrary(libraryPath);
2963N/A if (hPreloadAwt == NULL) {
2963N/A break;
2963N/A }
2963N/A
2963N/A /* get "preloadStop" func ptr */
2963N/A fnPreloadStop = (FnPreloadStop *)GetProcAddress(hPreloadAwt, "preloadStop");
2963N/A
2963N/A break;
2963N/A }
2963N/A }
2963N/A
2963N/A if (hPreloadAwt != NULL) {
2963N/A FnPreloadStart *fnInit = (FnPreloadStart *)GetProcAddress(hPreloadAwt, funcName);
2963N/A if (fnInit != NULL) {
2963N/A /* don't forget to stop preloading */
2963N/A awtPreloaded = 1;
2963N/A
2963N/A result = fnInit();
2963N/A }
2963N/A }
2963N/A
2963N/A return result;
2963N/A}
2963N/A
2963N/A/*
2963N/A * Terminates AWT preloading
2963N/A */
2963N/Avoid AWTPreloadStop() {
2963N/A if (fnPreloadStop != NULL) {
2963N/A fnPreloadStop();
2963N/A }
2963N/A}
2963N/A
2963N/A#endif /* ENABLE_AWT_PRELOAD */
4680N/A
4680N/Aint
4680N/AJVMInit(InvocationFunctions* ifn, jlong threadStackSize,
4680N/A int argc, char **argv,
4680N/A int mode, char *what, int ret)
4680N/A{
4680N/A ShowSplashScreen();
4680N/A return ContinueInNewThread(ifn, threadStackSize, argc, argv, mode, what, ret);
4680N/A}
4680N/A
4680N/Avoid
4680N/APostJVMInit(JNIEnv *env, jstring mainClass, JavaVM *vm)
4680N/A{
4680N/A // stubbed out for windows and *nixes.
4680N/A}
4680N/A
4680N/Avoid
4680N/ARegisterThread()
4680N/A{
4680N/A // stubbed out for windows and *nixes.
4680N/A}
4680N/A
4680N/A/*
4680N/A * on windows, we return a false to indicate this option is not applicable
4680N/A */
4680N/Ajboolean
4680N/AProcessPlatformOption(const char *arg)
4680N/A{
4680N/A return JNI_FALSE;
4680N/A}
5563N/A
5563N/A/*
5563N/A * At this point we have the arguments to the application, and we need to
5563N/A * check with original stdargs in order to compare which of these truly
5563N/A * needs expansion. cmdtoargs will specify this if it finds a bare
5563N/A * (unquoted) argument containing a glob character(s) ie. * or ?
5563N/A */
5563N/AjobjectArray
5563N/ACreateApplicationArgs(JNIEnv *env, char **strv, int argc)
5563N/A{
5563N/A int i, j, idx, tlen;
5563N/A jobjectArray outArray, inArray;
5563N/A char *ostart, *astart, **nargv;
5563N/A jboolean needs_expansion = JNI_FALSE;
5563N/A jmethodID mid;
5563N/A int stdargc;
5563N/A StdArg *stdargs;
5563N/A jclass cls = GetLauncherHelperClass(env);
5563N/A NULL_CHECK0(cls);
5563N/A
5563N/A if (argc == 0) {
5563N/A return NewPlatformStringArray(env, strv, argc);
5563N/A }
5563N/A // the holy grail we need to compare with.
5563N/A stdargs = JLI_GetStdArgs();
5563N/A stdargc = JLI_GetStdArgc();
5563N/A
5563N/A // sanity check, this should never happen
5563N/A if (argc > stdargc) {
5563N/A JLI_TraceLauncher("Warning: app args is larger than the original, %d %d\n", argc, stdargc);
5563N/A JLI_TraceLauncher("passing arguments as-is.\n");
5563N/A return NewPlatformStringArray(env, strv, argc);
5563N/A }
5563N/A
5563N/A // sanity check, match the args we have, to the holy grail
5563N/A idx = stdargc - argc;
5563N/A ostart = stdargs[idx].arg;
5563N/A astart = strv[0];
5563N/A // sanity check, ensure that the first argument of the arrays are the same
5563N/A if (JLI_StrCmp(ostart, astart) != 0) {
5563N/A // some thing is amiss the args don't match
5563N/A JLI_TraceLauncher("Warning: app args parsing error\n");
5563N/A JLI_TraceLauncher("passing arguments as-is\n");
5563N/A return NewPlatformStringArray(env, strv, argc);
5563N/A }
5563N/A
5563N/A // make a copy of the args which will be expanded in java if required.
5563N/A nargv = (char **)JLI_MemAlloc(argc * sizeof(char*));
5563N/A for (i = 0, j = idx; i < argc; i++, j++) {
5563N/A jboolean arg_expand = (JLI_StrCmp(stdargs[j].arg, strv[i]) == 0)
5563N/A ? stdargs[j].has_wildcard
5563N/A : JNI_FALSE;
5563N/A if (needs_expansion == JNI_FALSE)
5563N/A needs_expansion = arg_expand;
5563N/A
5563N/A // indicator char + String + NULL terminator, the java method will strip
5563N/A // out the first character, the indicator character, so no matter what
5563N/A // we add the indicator
5563N/A tlen = 1 + JLI_StrLen(strv[i]) + 1;
5563N/A nargv[i] = (char *) JLI_MemAlloc(tlen);
5700N/A if (JLI_Snprintf(nargv[i], tlen, "%c%s", arg_expand ? 'T' : 'F',
5700N/A strv[i]) < 0) {
5700N/A return NULL;
5700N/A }
5563N/A JLI_TraceLauncher("%s\n", nargv[i]);
5563N/A }
5563N/A
5563N/A if (!needs_expansion) {
5563N/A // clean up any allocated memory and return back the old arguments
5563N/A for (i = 0 ; i < argc ; i++) {
5563N/A JLI_MemFree(nargv[i]);
5563N/A }
5563N/A JLI_MemFree(nargv);
5563N/A return NewPlatformStringArray(env, strv, argc);
5563N/A }
5563N/A NULL_CHECK0(mid = (*env)->GetStaticMethodID(env, cls,
5563N/A "expandArgs",
5563N/A "([Ljava/lang/String;)[Ljava/lang/String;"));
5563N/A
5563N/A // expand the arguments that require expansion, the java method will strip
5563N/A // out the indicator character.
5563N/A inArray = NewPlatformStringArray(env, nargv, argc);
5563N/A outArray = (*env)->CallStaticObjectMethod(env, cls, mid, inArray);
5563N/A for (i = 0; i < argc; i++) {
5563N/A JLI_MemFree(nargv[i]);
5563N/A }
5563N/A JLI_MemFree(nargv);
5563N/A return outArray;
5563N/A}