1892N/A/*
1892N/A * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
1892N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1892N/A *
1892N/A * This code is free software; you can redistribute it and/or modify it
1892N/A * under the terms of the GNU General Public License version 2 only, as
1892N/A * published by the Free Software Foundation.
1892N/A *
1892N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1892N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1892N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1892N/A * version 2 for more details (a copy is included in the LICENSE file that
1892N/A * accompanied this code).
1892N/A *
1892N/A * You should have received a copy of the GNU General Public License version
1892N/A * 2 along with this work; if not, write to the Free Software Foundation,
1892N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1892N/A *
1892N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1892N/A * or visit www.oracle.com if you need additional information or have any
1892N/A * questions.
1892N/A *
1892N/A */
1892N/A
1934N/A#include <ctype.h>
1892N/A#include <windows.h>
1892N/A#include <io.h>
1892N/A#include <process.h>
1892N/A#include <stdlib.h>
1892N/A#include <stdio.h>
1892N/A#include <string.h>
1892N/A#include <sys/types.h>
1892N/A#include <sys/stat.h>
1892N/A
1892N/A#include <jni.h>
1892N/A#include "java.h"
1892N/A#ifndef GAMMA
1892N/A#include "version_comp.h"
1892N/A#endif
1892N/A
1892N/A#define JVM_DLL "jvm.dll"
1892N/A#define JAVA_DLL "java.dll"
1892N/A#define CRT_DLL "msvcr71.dll"
1892N/A
1892N/A/*
1892N/A * Prototypes.
1892N/A */
1892N/Astatic jboolean GetPublicJREHome(char *path, jint pathsize);
1892N/Astatic jboolean GetJVMPath(const char *jrepath, const char *jvmtype,
1892N/A char *jvmpath, jint jvmpathsize);
1892N/Astatic jboolean GetJREPath(char *path, jint pathsize);
1892N/Astatic void EnsureJreInstallation(const char *jrepath);
1892N/A
1892N/A/* We supports warmup for UI stack that is performed in parallel
1892N/A * to VM initialization.
1892N/A * This helps to improve startup of UI application as warmup phase
1892N/A * might be long due to initialization of OS or hardware resources.
1892N/A * It is not CPU bound and therefore it does not interfere with VM init.
1892N/A * Obviously such warmup only has sense for UI apps and therefore it needs
1892N/A * to be explicitly requested by passing -Dsun.awt.warmup=true property
1892N/A * (this is always the case for plugin/javaws).
1892N/A *
1892N/A * Implementation launches new thread after VM starts and use it to perform
1892N/A * warmup code (platform dependent).
1892N/A * This thread is later reused as AWT toolkit thread as graphics toolkit
1892N/A * often assume that they are used from the same thread they were launched on.
1892N/A *
1892N/A * At the moment we only support warmup for D3D. It only possible on windows
1892N/A * and only if other flags do not prohibit this (e.g. OpenGL support requested).
1892N/A */
1892N/A#undef ENABLE_AWT_PRELOAD
1892N/A#ifndef JAVA_ARGS /* turn off AWT preloading for javac, jar, etc */
1892N/A #ifdef _X86_ /* for now disable AWT preloading for 64bit */
1892N/A #define ENABLE_AWT_PRELOAD
1892N/A #endif
1892N/A#endif
1892N/A
1892N/A#ifdef ENABLE_AWT_PRELOAD
1892N/A/* "AWT was preloaded" flag;
1892N/A * Turned on by AWTPreload().
1892N/A */
1892N/Aint awtPreloaded = 0;
1892N/A
1892N/A/* Calls a function with the name specified.
1892N/A * The function must be int(*fn)(void).
1892N/A */
1892N/Aint AWTPreload(const char *funcName);
1892N/A/* Stops AWT preloading. */
1892N/Avoid AWTPreloadStop();
1892N/A
1892N/A/* D3D preloading */
1892N/A/* -1: not initialized; 0: OFF, 1: ON */
1892N/Aint awtPreloadD3D = -1;
1892N/A/* Command line parameter to swith D3D preloading on. */
1892N/A#define PARAM_PRELOAD_D3D "-Dsun.awt.warmup"
1892N/A/* D3D/OpenGL management parameters (may disable D3D preloading) */
1892N/A#define PARAM_NODDRAW "-Dsun.java2d.noddraw"
1892N/A#define PARAM_D3D "-Dsun.java2d.d3d"
1892N/A#define PARAM_OPENGL "-Dsun.java2d.opengl"
1892N/A/* funtion in awt.dll (src/windows/native/sun/java2d/d3d/D3DPipelineManager.cpp) */
1892N/A#define D3D_PRELOAD_FUNC "preloadD3D"
1892N/A
1892N/A
1892N/A/* Extracts value of a parameter with the specified name
1892N/A * from command line argument (returns pointer in the argument).
1892N/A * Returns NULL if the argument does not contains the parameter.
1892N/A * e.g.:
1892N/A * GetParamValue("theParam", "theParam=value") returns pointer to "value".
1892N/A */
1892N/Aconst char * GetParamValue(const char *paramName, const char *arg) {
1892N/A int nameLen = strlen(paramName);
1892N/A if (strncmp(paramName, arg, nameLen) == 0) {
1892N/A // arg[nameLen] is valid (may contain final NULL)
1892N/A if (arg[nameLen] == '=') {
1892N/A return arg + nameLen + 1;
1892N/A }
1892N/A }
1892N/A return NULL;
1892N/A}
1892N/A
1892N/A/* Checks if commandline argument contains property specified
1892N/A * and analyze it as boolean property (true/false).
1892N/A * Returns -1 if the argument does not contain the parameter;
1892N/A * Returns 1 if the argument contains the parameter and its value is "true";
1892N/A * Returns 0 if the argument contains the parameter and its value is "false".
1892N/A */
1892N/Aint GetBoolParamValue(const char *paramName, const char *arg) {
1892N/A const char * paramValue = GetParamValue(paramName, arg);
1892N/A if (paramValue != NULL) {
1892N/A if (stricmp(paramValue, "true") == 0) {
1892N/A return 1;
1892N/A }
1892N/A if (stricmp(paramValue, "false") == 0) {
1892N/A return 0;
1892N/A }
1892N/A }
1892N/A return -1;
1892N/A}
1892N/A#endif /* ENABLE_AWT_PRELOAD */
1892N/A
1892N/A
1892N/Aconst char *
1892N/AGetArch()
1892N/A{
1892N/A
1892N/A#ifdef _M_AMD64
1892N/A return "amd64";
1892N/A#elif defined(_M_IA64)
1892N/A return "ia64";
1892N/A#else
1892N/A return "i386";
1892N/A#endif
1892N/A}
1892N/A
1892N/A/*
1892N/A *
1892N/A */
1892N/Avoid
1892N/ACreateExecutionEnvironment(int *_argc,
1892N/A char ***_argv,
1892N/A char jrepath[],
1892N/A jint so_jrepath,
1892N/A char jvmpath[],
1892N/A jint so_jvmpath,
1892N/A char **original_argv) {
1892N/A#ifndef GAMMA
1892N/A char * jvmtype;
1892N/A
1892N/A /* Find out where the JRE is that we will be using. */
1892N/A if (!GetJREPath(jrepath, so_jrepath)) {
1892N/A ReportErrorMessage("Error: could not find Java SE Runtime Environment.",
1892N/A JNI_TRUE);
1892N/A exit(2);
1892N/A }
1892N/A
1892N/A /* Do this before we read jvm.cfg */
1892N/A EnsureJreInstallation(jrepath);
1892N/A
1892N/A /* Find the specified JVM type */
1892N/A if (ReadKnownVMs(jrepath, (char*)GetArch(), JNI_FALSE) < 1) {
1892N/A ReportErrorMessage("Error: no known VMs. (check for corrupt jvm.cfg file)",
1892N/A JNI_TRUE);
1892N/A exit(1);
1892N/A }
1892N/A jvmtype = CheckJvmType(_argc, _argv, JNI_FALSE);
1892N/A
1892N/A jvmpath[0] = '\0';
1892N/A if (!GetJVMPath(jrepath, jvmtype, jvmpath, so_jvmpath)) {
1892N/A char * message=NULL;
1892N/A const char * format = "Error: no `%s' JVM at `%s'.";
1892N/A message = (char *)JLI_MemAlloc((strlen(format)+strlen(jvmtype)+
1892N/A strlen(jvmpath)) * sizeof(char));
1892N/A sprintf(message,format, jvmtype, jvmpath);
1892N/A ReportErrorMessage(message, JNI_TRUE);
1892N/A exit(4);
1892N/A }
1892N/A /* If we got here, jvmpath has been correctly initialized. */
1892N/A
1892N/A#else /* ifndef GAMMA */
1892N/A
1892N/A /*
1892N/A * gamma launcher is simpler in that it doesn't handle VM flavors, data
1892N/A * model, etc. Assuming everything is set-up correctly
1892N/A * all we need to do here is to return correct path names. See also
1892N/A * GetJVMPath() and GetApplicationHome().
1892N/A */
1892N/A
1892N/A {
1892N/A if (!GetJREPath(jrepath, so_jrepath) ) {
1892N/A ReportErrorMessage("Error: could not find Java SE Runtime Environment.",
1892N/A JNI_TRUE);
1892N/A exit(2);
1892N/A }
1892N/A
1892N/A if (!GetJVMPath(jrepath, NULL, jvmpath, so_jvmpath)) {
1892N/A char * message=NULL;
1892N/A const char * format = "Error: no JVM at `%s'.";
1892N/A message = (char *)JLI_MemAlloc((strlen(format)+
1892N/A strlen(jvmpath)) * sizeof(char));
1892N/A sprintf(message, format, jvmpath);
1892N/A ReportErrorMessage(message, JNI_TRUE);
1892N/A exit(4);
1892N/A }
1892N/A }
1892N/A
1892N/A#endif /* ifndef GAMMA */
1892N/A
1892N/A}
1892N/A
1892N/A
1892N/Astatic jboolean
1892N/ALoadMSVCRT()
1892N/A{
1892N/A // Only do this once
1892N/A static int loaded = 0;
1892N/A char crtpath[MAXPATHLEN];
1892N/A
1892N/A if (!loaded) {
1892N/A /*
1892N/A * The Microsoft C Runtime Library needs to be loaded first. A copy is
1892N/A * assumed to be present in the "JRE path" directory. If it is not found
1892N/A * there (or "JRE path" fails to resolve), skip the explicit load and let
1892N/A * nature take its course, which is likely to be a failure to execute.
1892N/A */
1892N/A if (GetJREPath(crtpath, MAXPATHLEN)) {
1892N/A (void)strcat(crtpath, "\\bin\\" CRT_DLL); /* Add crt dll */
1892N/A if (_launcher_debug) {
1892N/A printf("CRT path is %s\n", crtpath);
1892N/A }
1892N/A if (_access(crtpath, 0) == 0) {
1892N/A if (LoadLibrary(crtpath) == 0) {
1892N/A ReportErrorMessage2("Error loading: %s", crtpath, JNI_TRUE);
1892N/A return JNI_FALSE;
1892N/A }
1892N/A }
1892N/A }
1892N/A loaded = 1;
1892N/A }
1892N/A return JNI_TRUE;
1892N/A}
1892N/A
1892N/A/*
1892N/A * The preJVMStart is a function in the jkernel.dll, which
1892N/A * performs the final step of synthesizing back the decomposed
1892N/A * modules (partial install) to the full JRE. Any tool which
1892N/A * uses the JRE must peform this step to ensure the complete synthesis.
1892N/A * The EnsureJreInstallation function calls preJVMStart based on
1892N/A * the conditions outlined below, noting that the operation
1892N/A * will fail silently if any of conditions are not met.
1892N/A * NOTE: this call must be made before jvm.dll is loaded, or jvm.cfg
1892N/A * is read, since jvm.cfg will be modified by the preJVMStart.
1892N/A * 1. Are we on a supported platform.
1892N/A * 2. Find the location of the JRE or the Kernel JRE.
1892N/A * 3. check existence of JREHOME/lib/bundles
1892N/A * 4. check jkernel.dll and invoke the entry-point
1892N/A */
1892N/Atypedef VOID (WINAPI *PREJVMSTART)();
1892N/A
1892N/Astatic void
1892N/AEnsureJreInstallation(const char* jrepath)
1892N/A{
1892N/A HINSTANCE handle;
1892N/A char tmpbuf[MAXPATHLEN];
1892N/A PREJVMSTART PreJVMStart;
1892N/A struct stat s;
1892N/A
1892N/A /* 32 bit windows only please */
1892N/A if (strcmp(GetArch(), "i386") != 0 ) {
1892N/A if (_launcher_debug) {
1892N/A printf("EnsureJreInstallation:unsupported platform\n");
1892N/A }
1892N/A return;
1892N/A }
1892N/A /* Does our bundle directory exist ? */
1892N/A strcpy(tmpbuf, jrepath);
1892N/A strcat(tmpbuf, "\\lib\\bundles");
1892N/A if (stat(tmpbuf, &s) != 0) {
1892N/A if (_launcher_debug) {
1892N/A printf("EnsureJreInstallation:<%s>:not found\n", tmpbuf);
1892N/A }
1892N/A return;
1892N/A }
1892N/A /* Does our jkernel dll exist ? */
1892N/A strcpy(tmpbuf, jrepath);
1892N/A strcat(tmpbuf, "\\bin\\jkernel.dll");
1892N/A if (stat(tmpbuf, &s) != 0) {
1892N/A if (_launcher_debug) {
1892N/A printf("EnsureJreInstallation:<%s>:not found\n", tmpbuf);
1892N/A }
1892N/A return;
1892N/A }
1892N/A /* The Microsoft C Runtime Library needs to be loaded first. */
1892N/A if (!LoadMSVCRT()) {
1892N/A if (_launcher_debug) {
1892N/A printf("EnsureJreInstallation:could not load C runtime DLL\n");
1892N/A }
1892N/A return;
1892N/A }
1892N/A /* Load the jkernel.dll */
1892N/A if ((handle = LoadLibrary(tmpbuf)) == 0) {
1892N/A if (_launcher_debug) {
1892N/A printf("EnsureJreInstallation:%s:load failed\n", tmpbuf);
1892N/A }
1892N/A return;
1892N/A }
1892N/A /* Get the function address */
1892N/A PreJVMStart = (PREJVMSTART)GetProcAddress(handle, "preJVMStart");
1892N/A if (PreJVMStart == NULL) {
1892N/A if (_launcher_debug) {
1892N/A printf("EnsureJreInstallation:preJVMStart:function lookup failed\n");
1892N/A }
1892N/A FreeLibrary(handle);
1892N/A return;
1892N/A }
1892N/A PreJVMStart();
1892N/A if (_launcher_debug) {
1892N/A printf("EnsureJreInstallation:preJVMStart:called\n");
1892N/A }
1892N/A FreeLibrary(handle);
1892N/A return;
1892N/A}
1892N/A
1892N/A/*
1892N/A * Find path to JRE based on .exe's location or registry settings.
1892N/A */
1892N/Ajboolean
1892N/AGetJREPath(char *path, jint pathsize)
1892N/A{
1892N/A char javadll[MAXPATHLEN];
1892N/A struct stat s;
1892N/A
1892N/A if (GetApplicationHome(path, pathsize)) {
1892N/A /* Is JRE co-located with the application? */
1892N/A sprintf(javadll, "%s\\bin\\" JAVA_DLL, path);
1892N/A if (stat(javadll, &s) == 0) {
1892N/A goto found;
1892N/A }
1892N/A
1892N/A /* Does this app ship a private JRE in <apphome>\jre directory? */
1892N/A sprintf(javadll, "%s\\jre\\bin\\" JAVA_DLL, path);
1892N/A if (stat(javadll, &s) == 0) {
1892N/A strcat(path, "\\jre");
1892N/A goto found;
1892N/A }
1892N/A }
1892N/A
1892N/A#ifndef GAMMA
1892N/A /* Look for a public JRE on this machine. */
1892N/A if (GetPublicJREHome(path, pathsize)) {
1892N/A goto found;
1892N/A }
1892N/A#endif
1892N/A
1892N/A fprintf(stderr, "Error: could not find " JAVA_DLL "\n");
1892N/A return JNI_FALSE;
1892N/A
1892N/A found:
1892N/A if (_launcher_debug)
1892N/A printf("JRE path is %s\n", path);
1892N/A return JNI_TRUE;
1892N/A}
1892N/A
1892N/A/*
1892N/A * Given a JRE location and a JVM type, construct what the name the
1892N/A * JVM shared library will be. Return true, if such a library
1892N/A * exists, false otherwise.
1892N/A */
1892N/Astatic jboolean
1892N/AGetJVMPath(const char *jrepath, const char *jvmtype,
1892N/A char *jvmpath, jint jvmpathsize)
1892N/A{
1892N/A struct stat s;
1892N/A
1892N/A#ifndef GAMMA
1892N/A if (strchr(jvmtype, '/') || strchr(jvmtype, '\\')) {
1892N/A sprintf(jvmpath, "%s\\" JVM_DLL, jvmtype);
1892N/A } else {
1892N/A sprintf(jvmpath, "%s\\bin\\%s\\" JVM_DLL, jrepath, jvmtype);
1892N/A }
1892N/A#else
1892N/A /*
1892N/A * For gamma launcher, JVM is either built-in or in the same directory.
1892N/A * Either way we return "<exe_path>/jvm.dll" where <exe_path> is the
1892N/A * directory where gamma launcher is located.
1892N/A */
1892N/A
1892N/A char *p;
1892N/A GetModuleFileName(0, jvmpath, jvmpathsize);
1892N/A
1892N/A p = strrchr(jvmpath, '\\');
1892N/A if (p) {
1892N/A /* replace executable name with libjvm.so */
1892N/A snprintf(p + 1, jvmpathsize - (p + 1 - jvmpath), "%s", JVM_DLL);
1892N/A } else {
1892N/A /* this case shouldn't happen */
1892N/A snprintf(jvmpath, jvmpathsize, "%s", JVM_DLL);
1892N/A }
1892N/A#endif /* ifndef GAMMA */
1892N/A
1892N/A if (stat(jvmpath, &s) == 0) {
1892N/A return JNI_TRUE;
1892N/A } else {
1892N/A return JNI_FALSE;
1892N/A }
1892N/A}
1892N/A
1892N/A/*
1892N/A * Load a jvm from "jvmpath" and initialize the invocation functions.
1892N/A */
1892N/Ajboolean
1892N/ALoadJavaVM(const char *jvmpath, InvocationFunctions *ifn)
1892N/A{
1892N/A#ifdef GAMMA
1892N/A /* JVM is directly linked with gamma launcher; no Loadlibrary() */
1892N/A ifn->CreateJavaVM = JNI_CreateJavaVM;
1892N/A ifn->GetDefaultJavaVMInitArgs = JNI_GetDefaultJavaVMInitArgs;
1892N/A return JNI_TRUE;
1892N/A#else
1892N/A HINSTANCE handle;
1892N/A
1892N/A if (_launcher_debug) {
1892N/A printf("JVM path is %s\n", jvmpath);
1892N/A }
1892N/A
1892N/A /* The Microsoft C Runtime Library needs to be loaded first. */
1892N/A LoadMSVCRT();
1892N/A
1892N/A /* Load the Java VM DLL */
1892N/A if ((handle = LoadLibrary(jvmpath)) == 0) {
1892N/A ReportErrorMessage2("Error loading: %s", (char *)jvmpath, JNI_TRUE);
1892N/A return JNI_FALSE;
1892N/A }
1892N/A
1892N/A /* Now get the function addresses */
1892N/A ifn->CreateJavaVM =
1892N/A (void *)GetProcAddress(handle, "JNI_CreateJavaVM");
1892N/A ifn->GetDefaultJavaVMInitArgs =
1892N/A (void *)GetProcAddress(handle, "JNI_GetDefaultJavaVMInitArgs");
1892N/A if (ifn->CreateJavaVM == 0 || ifn->GetDefaultJavaVMInitArgs == 0) {
1892N/A ReportErrorMessage2("Error: can't find JNI interfaces in: %s",
1892N/A (char *)jvmpath, JNI_TRUE);
1892N/A return JNI_FALSE;
1892N/A }
1892N/A
1892N/A return JNI_TRUE;
1892N/A#endif /* ifndef GAMMA */
1892N/A}
1892N/A
1892N/A/*
1892N/A * If app is "c:\foo\bin\javac", then put "c:\foo" into buf.
1892N/A */
1892N/Ajboolean
1892N/AGetApplicationHome(char *buf, jint bufsize)
1892N/A{
1892N/A#ifndef GAMMA
1892N/A char *cp;
1892N/A GetModuleFileName(0, buf, bufsize);
1892N/A *strrchr(buf, '\\') = '\0'; /* remove .exe file name */
1892N/A if ((cp = strrchr(buf, '\\')) == 0) {
1892N/A /* This happens if the application is in a drive root, and
1892N/A * there is no bin directory. */
1892N/A buf[0] = '\0';
1892N/A return JNI_FALSE;
1892N/A }
1892N/A *cp = '\0'; /* remove the bin\ part */
1892N/A return JNI_TRUE;
1892N/A
1892N/A#else /* ifndef GAMMA */
1892N/A
1934N/A char env[MAXPATHLEN + 1];
1934N/A
1934N/A /* gamma launcher uses ALT_JAVA_HOME environment variable or jdkpath.txt file to find JDK/JRE */
1934N/A
1934N/A if (getenv("ALT_JAVA_HOME") != NULL) {
1934N/A snprintf(buf, bufsize, "%s", getenv("ALT_JAVA_HOME"));
1892N/A }
1934N/A else {
1934N/A char path[MAXPATHLEN + 1];
1934N/A char* p;
1934N/A int len;
1934N/A FILE* fp;
1934N/A
1934N/A // find the path to the currect executable
1934N/A len = GetModuleFileName(NULL, path, MAXPATHLEN + 1);
1934N/A if (len == 0 || len > MAXPATHLEN) {
1934N/A printf("Could not get directory of current executable.");
1934N/A return JNI_FALSE;
1934N/A }
1934N/A // remove last path component ("hotspot.exe")
1934N/A p = strrchr(path, '\\');
1934N/A if (p == NULL) {
1934N/A printf("Could not parse directory of current executable.\n");
1934N/A return JNI_FALSE;
1934N/A }
1934N/A *p = '\0';
1934N/A
1934N/A // open jdkpath.txt and read JAVA_HOME from it
1934N/A if (strlen(path) + strlen("\\jdkpath.txt") + 1 >= MAXPATHLEN) {
1934N/A printf("Path too long: %s\n", path);
1934N/A return JNI_FALSE;
1934N/A }
1934N/A strcat(path, "\\jdkpath.txt");
1934N/A fp = fopen(path, "r");
1934N/A if (fp == NULL) {
1934N/A printf("Could not open file %s to get path to JDK.\n", path);
1934N/A return JNI_FALSE;
1934N/A }
1934N/A
1934N/A if (fgets(buf, bufsize, fp) == NULL) {
1934N/A printf("Could not read from file %s to get path to JDK.\n", path);
1934N/A fclose(fp);
1934N/A return JNI_FALSE;
1934N/A }
1934N/A // trim the buffer
1934N/A p = buf + strlen(buf) - 1;
1934N/A while(isspace(*p)) {
1934N/A *p = '\0';
1934N/A p--;
1934N/A }
1934N/A fclose(fp);
1892N/A }
1934N/A
1934N/A _snprintf(env, MAXPATHLEN, "JAVA_HOME=%s", buf);
1934N/A _putenv(env);
1934N/A
1892N/A return JNI_TRUE;
1892N/A#endif /* ifndef GAMMA */
1892N/A}
1892N/A
1892N/A#ifdef JAVAW
1892N/A__declspec(dllimport) char **__initenv;
1892N/A
1892N/Aint WINAPI
1892N/AWinMain(HINSTANCE inst, HINSTANCE previnst, LPSTR cmdline, int cmdshow)
1892N/A{
1892N/A int ret;
1892N/A
1892N/A __initenv = _environ;
1892N/A ret = main(__argc, __argv);
1892N/A
1892N/A return ret;
1892N/A}
1892N/A#endif
1892N/A
1892N/A#ifndef GAMMA
1892N/A
1892N/A/*
1892N/A * Helpers to look in the registry for a public JRE.
1892N/A */
1892N/A /* Same for 1.5.0, 1.5.1, 1.5.2 etc. */
1892N/A#define DOTRELEASE JDK_MAJOR_VERSION "." JDK_MINOR_VERSION
1892N/A#define JRE_KEY "Software\\JavaSoft\\Java Runtime Environment"
1892N/A
1892N/Astatic jboolean
1892N/AGetStringFromRegistry(HKEY key, const char *name, char *buf, jint bufsize)
1892N/A{
1892N/A DWORD type, size;
1892N/A
1892N/A if (RegQueryValueEx(key, name, 0, &type, 0, &size) == 0
1892N/A && type == REG_SZ
1892N/A && (size < (unsigned int)bufsize)) {
1892N/A if (RegQueryValueEx(key, name, 0, 0, buf, &size) == 0) {
1892N/A return JNI_TRUE;
1892N/A }
1892N/A }
1892N/A return JNI_FALSE;
1892N/A}
1892N/A
1892N/Astatic jboolean
1892N/AGetPublicJREHome(char *buf, jint bufsize)
1892N/A{
1892N/A HKEY key, subkey;
1892N/A char version[MAXPATHLEN];
1892N/A
1892N/A /*
1892N/A * Note: There is a very similar implementation of the following
1892N/A * registry reading code in the Windows java control panel (javacp.cpl).
1892N/A * If there are bugs here, a similar bug probably exists there. Hence,
1892N/A * changes here require inspection there.
1892N/A */
1892N/A
1892N/A /* Find the current version of the JRE */
1892N/A if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, JRE_KEY, 0, KEY_READ, &key) != 0) {
1892N/A fprintf(stderr, "Error opening registry key '" JRE_KEY "'\n");
1892N/A return JNI_FALSE;
1892N/A }
1892N/A
1892N/A if (!GetStringFromRegistry(key, "CurrentVersion",
1892N/A version, sizeof(version))) {
1892N/A fprintf(stderr, "Failed reading value of registry key:\n\t"
1892N/A JRE_KEY "\\CurrentVersion\n");
1892N/A RegCloseKey(key);
1892N/A return JNI_FALSE;
1892N/A }
1892N/A
1892N/A if (strcmp(version, DOTRELEASE) != 0) {
1892N/A fprintf(stderr, "Registry key '" JRE_KEY "\\CurrentVersion'\nhas "
1892N/A "value '%s', but '" DOTRELEASE "' is required.\n", version);
1892N/A RegCloseKey(key);
1892N/A return JNI_FALSE;
1892N/A }
1892N/A
1892N/A /* Find directory where the current version is installed. */
1892N/A if (RegOpenKeyEx(key, version, 0, KEY_READ, &subkey) != 0) {
1892N/A fprintf(stderr, "Error opening registry key '"
1892N/A JRE_KEY "\\%s'\n", version);
1892N/A RegCloseKey(key);
1892N/A return JNI_FALSE;
1892N/A }
1892N/A
1892N/A if (!GetStringFromRegistry(subkey, "JavaHome", buf, bufsize)) {
1892N/A fprintf(stderr, "Failed reading value of registry key:\n\t"
1892N/A JRE_KEY "\\%s\\JavaHome\n", version);
1892N/A RegCloseKey(key);
1892N/A RegCloseKey(subkey);
1892N/A return JNI_FALSE;
1892N/A }
1892N/A
1892N/A if (_launcher_debug) {
1892N/A char micro[MAXPATHLEN];
1892N/A if (!GetStringFromRegistry(subkey, "MicroVersion", micro,
1892N/A sizeof(micro))) {
1892N/A printf("Warning: Can't read MicroVersion\n");
1892N/A micro[0] = '\0';
1892N/A }
1892N/A printf("Version major.minor.micro = %s.%s\n", version, micro);
1892N/A }
1892N/A
1892N/A RegCloseKey(key);
1892N/A RegCloseKey(subkey);
1892N/A return JNI_TRUE;
1892N/A}
1892N/A
1892N/A#endif /* ifndef GAMMA */
1892N/A
1892N/A/*
1892N/A * Support for doing cheap, accurate interval timing.
1892N/A */
1892N/Astatic jboolean counterAvailable = JNI_FALSE;
1892N/Astatic jboolean counterInitialized = JNI_FALSE;
1892N/Astatic LARGE_INTEGER counterFrequency;
1892N/A
1892N/Ajlong CounterGet()
1892N/A{
1892N/A LARGE_INTEGER count;
1892N/A
1892N/A if (!counterInitialized) {
1892N/A counterAvailable = QueryPerformanceFrequency(&counterFrequency);
1892N/A counterInitialized = JNI_TRUE;
1892N/A }
1892N/A if (!counterAvailable) {
1892N/A return 0;
1892N/A }
1892N/A QueryPerformanceCounter(&count);
1892N/A return (jlong)(count.QuadPart);
1892N/A}
1892N/A
1892N/Ajlong Counter2Micros(jlong counts)
1892N/A{
1892N/A if (!counterAvailable || !counterInitialized) {
1892N/A return 0;
1892N/A }
1892N/A return (counts * 1000 * 1000)/counterFrequency.QuadPart;
1892N/A}
1892N/A
1892N/Avoid ReportErrorMessage(char * message, jboolean always) {
1892N/A#ifdef JAVAW
1892N/A if (message != NULL) {
1892N/A MessageBox(NULL, message, "Java Virtual Machine Launcher",
1892N/A (MB_OK|MB_ICONSTOP|MB_APPLMODAL));
1892N/A }
1892N/A#else
1892N/A if (always) {
1892N/A fprintf(stderr, "%s\n", message);
1892N/A }
1892N/A#endif
1892N/A}
1892N/A
1892N/Avoid ReportErrorMessage2(char * format, char * string, jboolean always) {
1892N/A /*
1892N/A * The format argument must be a printf format string with one %s
1892N/A * argument, which is passed the string argument.
1892N/A */
1892N/A#ifdef JAVAW
1892N/A size_t size;
1892N/A char * message;
1892N/A size = strlen(format) + strlen(string);
1892N/A message = (char*)JLI_MemAlloc(size*sizeof(char));
1892N/A sprintf(message, (const char *)format, string);
1892N/A
1892N/A if (message != NULL) {
1892N/A MessageBox(NULL, message, "Java Virtual Machine Launcher",
1892N/A (MB_OK|MB_ICONSTOP|MB_APPLMODAL));
1892N/A JLI_MemFree(message);
1892N/A }
1892N/A#else
1892N/A if (always) {
1892N/A fprintf(stderr, (const char *)format, string);
1892N/A fprintf(stderr, "\n");
1892N/A }
1892N/A#endif
1892N/A}
1892N/A
1892N/A/*
1892N/A * As ReportErrorMessage2 (above) except the system message (if any)
1892N/A * associated with this error is written to a second %s format specifier
1892N/A * in the format argument.
1892N/A */
1892N/Avoid ReportSysErrorMessage2(char * format, char * string, jboolean always) {
1892N/A int save_errno = errno;
1892N/A DWORD errval;
1892N/A int freeit = 0;
1892N/A char *errtext = NULL;
1892N/A
1892N/A if ((errval = GetLastError()) != 0) { /* Platform SDK / DOS Error */
1892N/A int n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|
1892N/A FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_ALLOCATE_BUFFER,
1892N/A NULL, errval, 0, (LPTSTR)&errtext, 0, NULL);
1892N/A if (errtext == NULL || n == 0) { /* Paranoia check */
1892N/A errtext = "";
1892N/A n = 0;
1892N/A } else {
1892N/A freeit = 1;
1892N/A if (n > 2) { /* Drop final CR, LF */
1892N/A if (errtext[n - 1] == '\n') n--;
1892N/A if (errtext[n - 1] == '\r') n--;
1892N/A errtext[n] = '\0';
1892N/A }
1892N/A }
1892N/A } else /* C runtime error that has no corresponding DOS error code */
1892N/A errtext = strerror(save_errno);
1892N/A
1892N/A#ifdef JAVAW
1892N/A {
1892N/A size_t size;
1892N/A char * message;
1892N/A size = strlen(format) + strlen(string) + strlen(errtext);
1892N/A message = (char*)JLI_MemAlloc(size*sizeof(char));
1892N/A sprintf(message, (const char *)format, string, errtext);
1892N/A
1892N/A if (message != NULL) {
1892N/A MessageBox(NULL, message, "Java Virtual Machine Launcher",
1892N/A (MB_OK|MB_ICONSTOP|MB_APPLMODAL));
1892N/A JLI_MemFree(message);
1892N/A }
1892N/A }
1892N/A#else
1892N/A if (always) {
1892N/A fprintf(stderr, (const char *)format, string, errtext);
1892N/A fprintf(stderr, "\n");
1892N/A }
1892N/A#endif
1892N/A if (freeit)
1892N/A (void)LocalFree((HLOCAL)errtext);
1892N/A}
1892N/A
1892N/Avoid ReportExceptionDescription(JNIEnv * env) {
1892N/A#ifdef JAVAW
1892N/A /*
1892N/A * This code should be replaced by code which opens a window with
1892N/A * the exception detail message.
1892N/A */
1892N/A (*env)->ExceptionDescribe(env);
1892N/A#else
1892N/A (*env)->ExceptionDescribe(env);
1892N/A#endif
1892N/A}
1892N/A
1892N/A
1892N/A/*
1892N/A * Return JNI_TRUE for an option string that has no effect but should
1892N/A * _not_ be passed on to the vm; return JNI_FALSE otherwise. On
1892N/A * windows, there are no options that should be screened in this
1892N/A * manner.
1892N/A */
1892N/Ajboolean RemovableMachineDependentOption(char * option) {
1892N/A#ifdef ENABLE_AWT_PRELOAD
1892N/A if (awtPreloadD3D < 0) {
1892N/A /* Tests the command line parameter only if not set yet. */
1892N/A if (GetBoolParamValue(PARAM_PRELOAD_D3D, option) == 1) {
1892N/A awtPreloadD3D = 1;
1892N/A }
1892N/A }
1892N/A if (awtPreloadD3D != 0) {
1892N/A /* Don't test the command line parameters if already disabled. */
1892N/A if (GetBoolParamValue(PARAM_NODDRAW, option) == 1
1892N/A || GetBoolParamValue(PARAM_D3D, option) == 0
1892N/A || GetBoolParamValue(PARAM_OPENGL, option) == 1)
1892N/A {
1892N/A awtPreloadD3D = 0;
1892N/A }
1892N/A }
1892N/A#endif /* ENABLE_AWT_PRELOAD */
1892N/A
1892N/A return JNI_FALSE;
1892N/A}
1892N/A
1892N/Avoid PrintMachineDependentOptions() {
1892N/A return;
1892N/A}
1892N/A
1892N/A#ifndef GAMMA
1892N/A
1892N/Ajboolean
1892N/AServerClassMachine() {
1892N/A jboolean result = JNI_FALSE;
1892N/A#if defined(NEVER_ACT_AS_SERVER_CLASS_MACHINE)
1892N/A result = JNI_FALSE;
1892N/A#elif defined(ALWAYS_ACT_AS_SERVER_CLASS_MACHINE)
1892N/A result = JNI_TRUE;
1892N/A#endif
1892N/A return result;
1892N/A}
1892N/A
1892N/A/*
1892N/A * Determine if there is an acceptable JRE in the registry directory top_key.
1892N/A * Upon locating the "best" one, return a fully qualified path to it.
1892N/A * "Best" is defined as the most advanced JRE meeting the constraints
1892N/A * contained in the manifest_info. If no JRE in this directory meets the
1892N/A * constraints, return NULL.
1892N/A *
1892N/A * It doesn't matter if we get an error reading the registry, or we just
1892N/A * don't find anything interesting in the directory. We just return NULL
1892N/A * in either case.
1892N/A */
1892N/Astatic char *
1892N/AProcessDir(manifest_info* info, HKEY top_key) {
1892N/A DWORD index = 0;
1892N/A HKEY ver_key;
1892N/A char name[MAXNAMELEN];
1892N/A int len;
1892N/A char *best = NULL;
1892N/A
1892N/A /*
1892N/A * Enumerate "<top_key>/SOFTWARE/JavaSoft/Java Runtime Environment"
1892N/A * searching for the best available version.
1892N/A */
1892N/A while (RegEnumKey(top_key, index, name, MAXNAMELEN) == ERROR_SUCCESS) {
1892N/A index++;
1892N/A if (JLI_AcceptableRelease(name, info->jre_version))
1892N/A if ((best == NULL) || (JLI_ExactVersionId(name, best) > 0)) {
1892N/A if (best != NULL)
1892N/A JLI_MemFree(best);
1892N/A best = JLI_StringDup(name);
1892N/A }
1892N/A }
1892N/A
1892N/A /*
1892N/A * Extract "JavaHome" from the "best" registry directory and return
1892N/A * that path. If no appropriate version was located, or there is an
1892N/A * error in extracting the "JavaHome" string, return null.
1892N/A */
1892N/A if (best == NULL)
1892N/A return (NULL);
1892N/A else {
1892N/A if (RegOpenKeyEx(top_key, best, 0, KEY_READ, &ver_key)
1892N/A != ERROR_SUCCESS) {
1892N/A JLI_MemFree(best);
1892N/A if (ver_key != NULL)
1892N/A RegCloseKey(ver_key);
1892N/A return (NULL);
1892N/A }
1892N/A JLI_MemFree(best);
1892N/A len = MAXNAMELEN;
1892N/A if (RegQueryValueEx(ver_key, "JavaHome", NULL, NULL, (LPBYTE)name, &len)
1892N/A != ERROR_SUCCESS) {
1892N/A if (ver_key != NULL)
1892N/A RegCloseKey(ver_key);
1892N/A return (NULL);
1892N/A }
1892N/A if (ver_key != NULL)
1892N/A RegCloseKey(ver_key);
1892N/A return (JLI_StringDup(name));
1892N/A }
1892N/A}
1892N/A
1892N/A/*
1892N/A * This is the global entry point. It examines the host for the optimal
1892N/A * JRE to be used by scanning a set of registry entries. This set of entries
1892N/A * is hardwired on Windows as "Software\JavaSoft\Java Runtime Environment"
1892N/A * under the set of roots "{ HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE }".
1892N/A *
1892N/A * This routine simply opens each of these registry directories before passing
1892N/A * control onto ProcessDir().
1892N/A */
1892N/Achar *
1892N/ALocateJRE(manifest_info* info) {
1892N/A HKEY key = NULL;
1892N/A char *path;
1892N/A int key_index;
1892N/A HKEY root_keys[2] = { HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE };
1892N/A
1892N/A for (key_index = 0; key_index <= 1; key_index++) {
1892N/A if (RegOpenKeyEx(root_keys[key_index], JRE_KEY, 0, KEY_READ, &key)
1892N/A == ERROR_SUCCESS)
1892N/A if ((path = ProcessDir(info, key)) != NULL) {
1892N/A if (key != NULL)
1892N/A RegCloseKey(key);
1892N/A return (path);
1892N/A }
1892N/A if (key != NULL)
1892N/A RegCloseKey(key);
1892N/A }
1892N/A return NULL;
1892N/A}
1892N/A
1892N/A
1892N/A/*
1892N/A * Local helper routine to isolate a single token (option or argument)
1892N/A * from the command line.
1892N/A *
1892N/A * This routine accepts a pointer to a character pointer. The first
1892N/A * token (as defined by MSDN command-line argument syntax) is isolated
1892N/A * from that string.
1892N/A *
1892N/A * Upon return, the input character pointer pointed to by the parameter s
1892N/A * is updated to point to the remainding, unscanned, portion of the string,
1892N/A * or to a null character if the entire string has been consummed.
1892N/A *
1892N/A * This function returns a pointer to a null-terminated string which
1892N/A * contains the isolated first token, or to the null character if no
1892N/A * token could be isolated.
1892N/A *
1892N/A * Note the side effect of modifying the input string s by the insertion
1892N/A * of a null character, making it two strings.
1892N/A *
1892N/A * See "Parsing C Command-Line Arguments" in the MSDN Library for the
1892N/A * parsing rule details. The rule summary from that specification is:
1892N/A *
1892N/A * * Arguments are delimited by white space, which is either a space or a tab.
1892N/A *
1892N/A * * A string surrounded by double quotation marks is interpreted as a single
1892N/A * argument, regardless of white space contained within. A quoted string can
1892N/A * be embedded in an argument. Note that the caret (^) is not recognized as
1892N/A * an escape character or delimiter.
1892N/A *
1892N/A * * A double quotation mark preceded by a backslash, \", is interpreted as a
1892N/A * literal double quotation mark (").
1892N/A *
1892N/A * * Backslashes are interpreted literally, unless they immediately precede a
1892N/A * double quotation mark.
1892N/A *
1892N/A * * If an even number of backslashes is followed by a double quotation mark,
1892N/A * then one backslash (\) is placed in the argv array for every pair of
1892N/A * backslashes (\\), and the double quotation mark (") is interpreted as a
1892N/A * string delimiter.
1892N/A *
1892N/A * * If an odd number of backslashes is followed by a double quotation mark,
1892N/A * then one backslash (\) is placed in the argv array for every pair of
1892N/A * backslashes (\\) and the double quotation mark is interpreted as an
1892N/A * escape sequence by the remaining backslash, causing a literal double
1892N/A * quotation mark (") to be placed in argv.
1892N/A */
1892N/Astatic char*
1892N/Anextarg(char** s) {
1892N/A char *p = *s;
1892N/A char *head;
1892N/A int slashes = 0;
1892N/A int inquote = 0;
1892N/A
1892N/A /*
1892N/A * Strip leading whitespace, which MSDN defines as only space or tab.
1892N/A * (Hence, no locale specific "isspace" here.)
1892N/A */
1892N/A while (*p != (char)0 && (*p == ' ' || *p == '\t'))
1892N/A p++;
1892N/A head = p; /* Save the start of the token to return */
1892N/A
1892N/A /*
1892N/A * Isolate a token from the command line.
1892N/A */
1892N/A while (*p != (char)0 && (inquote || !(*p == ' ' || *p == '\t'))) {
1892N/A if (*p == '\\' && *(p+1) == '"' && slashes % 2 == 0)
1892N/A p++;
1892N/A else if (*p == '"')
1892N/A inquote = !inquote;
1892N/A slashes = (*p++ == '\\') ? slashes + 1 : 0;
1892N/A }
1892N/A
1892N/A /*
1892N/A * If the token isolated isn't already terminated in a "char zero",
1892N/A * then replace the whitespace character with one and move to the
1892N/A * next character.
1892N/A */
1892N/A if (*p != (char)0)
1892N/A *p++ = (char)0;
1892N/A
1892N/A /*
1892N/A * Update the parameter to point to the head of the remaining string
1892N/A * reflecting the command line and return a pointer to the leading
1892N/A * token which was isolated from the command line.
1892N/A */
1892N/A *s = p;
1892N/A return (head);
1892N/A}
1892N/A
1892N/A/*
1892N/A * Local helper routine to return a string equivalent to the input string
1892N/A * s, but with quotes removed so the result is a string as would be found
1892N/A * in argv[]. The returned string should be freed by a call to JLI_MemFree().
1892N/A *
1892N/A * The rules for quoting (and escaped quotes) are:
1892N/A *
1892N/A * 1 A double quotation mark preceded by a backslash, \", is interpreted as a
1892N/A * literal double quotation mark (").
1892N/A *
1892N/A * 2 Backslashes are interpreted literally, unless they immediately precede a
1892N/A * double quotation mark.
1892N/A *
1892N/A * 3 If an even number of backslashes is followed by a double quotation mark,
1892N/A * then one backslash (\) is placed in the argv array for every pair of
1892N/A * backslashes (\\), and the double quotation mark (") is interpreted as a
1892N/A * string delimiter.
1892N/A *
1892N/A * 4 If an odd number of backslashes is followed by a double quotation mark,
1892N/A * then one backslash (\) is placed in the argv array for every pair of
1892N/A * backslashes (\\) and the double quotation mark is interpreted as an
1892N/A * escape sequence by the remaining backslash, causing a literal double
1892N/A * quotation mark (") to be placed in argv.
1892N/A */
1892N/Astatic char*
1892N/Aunquote(const char *s) {
1892N/A const char *p = s; /* Pointer to the tail of the original string */
1892N/A char *un = (char*)JLI_MemAlloc(strlen(s) + 1); /* Ptr to unquoted string */
1892N/A char *pun = un; /* Pointer to the tail of the unquoted string */
1892N/A
1892N/A while (*p != '\0') {
1892N/A if (*p == '"') {
1892N/A p++;
1892N/A } else if (*p == '\\') {
1892N/A const char *q = p + strspn(p,"\\");
1892N/A if (*q == '"')
1892N/A do {
1892N/A *pun++ = '\\';
1892N/A p += 2;
1892N/A } while (*p == '\\' && p < q);
1892N/A else
1892N/A while (p < q)
1892N/A *pun++ = *p++;
1892N/A } else {
1892N/A *pun++ = *p++;
1892N/A }
1892N/A }
1892N/A *pun = '\0';
1892N/A return un;
1892N/A}
1892N/A
1892N/A/*
1892N/A * Given a path to a jre to execute, this routine checks if this process
1892N/A * is indeed that jre. If not, it exec's that jre.
1892N/A *
1892N/A * We want to actually check the paths rather than just the version string
1892N/A * built into the executable, so that given version specification will yield
1892N/A * the exact same Java environment, regardless of the version of the arbitrary
1892N/A * launcher we start with.
1892N/A */
1892N/Avoid
1892N/AExecJRE(char *jre, char **argv) {
1892N/A int len;
1892N/A char *progname;
1892N/A char path[MAXPATHLEN + 1];
1892N/A
1892N/A /*
1892N/A * Determine the executable we are building (or in the rare case, running).
1892N/A */
1892N/A#ifdef JAVA_ARGS /* javac, jar and friends. */
1892N/A progname = "java";
1892N/A#else /* java, oldjava, javaw and friends */
1892N/A#ifdef PROGNAME
1892N/A progname = PROGNAME;
1892N/A#else
1892N/A {
1892N/A char *s;
1892N/A progname = *argv;
1892N/A if ((s = strrchr(progname, FILE_SEPARATOR)) != 0) {
1892N/A progname = s + 1;
1892N/A }
1892N/A }
1892N/A#endif /* PROGNAME */
1892N/A#endif /* JAVA_ARGS */
1892N/A
1892N/A /*
1892N/A * Resolve the real path to the currently running launcher.
1892N/A */
1892N/A len = GetModuleFileName(NULL, path, MAXPATHLEN + 1);
1892N/A if (len == 0 || len > MAXPATHLEN) {
1892N/A ReportSysErrorMessage2(
1892N/A "Unable to resolve path to current %s executable: %s",
1892N/A progname, JNI_TRUE);
1892N/A exit(1);
1892N/A }
1892N/A
1892N/A if (_launcher_debug) {
1892N/A printf("ExecJRE: old: %s\n", path);
1892N/A printf("ExecJRE: new: %s\n", jre);
1892N/A }
1892N/A
1892N/A /*
1892N/A * If the path to the selected JRE directory is a match to the initial
1892N/A * portion of the path to the currently executing JRE, we have a winner!
1892N/A * If so, just return. (strnicmp() is the Windows equiv. of strncasecmp().)
1892N/A */
1892N/A if (strnicmp(jre, path, strlen(jre)) == 0)
1892N/A return; /* I am the droid you were looking for */
1892N/A
1892N/A /*
1892N/A * If this isn't the selected version, exec the selected version.
1892N/A */
1892N/A (void)strcat(strcat(strcpy(path, jre), "\\bin\\"), progname);
1892N/A (void)strcat(path, ".exe");
1892N/A
1892N/A /*
1892N/A * Although Windows has an execv() entrypoint, it doesn't actually
1892N/A * overlay a process: it can only create a new process and terminate
1892N/A * the old process. Therefore, any processes waiting on the initial
1892N/A * process wake up and they shouldn't. Hence, a chain of pseudo-zombie
1892N/A * processes must be retained to maintain the proper wait semantics.
1892N/A * Fortunately the image size of the launcher isn't too large at this
1892N/A * time.
1892N/A *
1892N/A * If it weren't for this semantic flaw, the code below would be ...
1892N/A *
1892N/A * execv(path, argv);
1892N/A * ReportErrorMessage2("Exec of %s failed\n", path, JNI_TRUE);
1892N/A * exit(1);
1892N/A *
1892N/A * The incorrect exec semantics could be addressed by:
1892N/A *
1892N/A * exit((int)spawnv(_P_WAIT, path, argv));
1892N/A *
1892N/A * Unfortunately, a bug in Windows spawn/exec impementation prevents
1892N/A * this from completely working. All the Windows POSIX process creation
1892N/A * interfaces are implemented as wrappers around the native Windows
1892N/A * function CreateProcess(). CreateProcess() takes a single string
1892N/A * to specify command line options and arguments, so the POSIX routine
1892N/A * wrappers build a single string from the argv[] array and in the
1892N/A * process, any quoting information is lost.
1892N/A *
1892N/A * The solution to this to get the original command line, to process it
1892N/A * to remove the new multiple JRE options (if any) as was done for argv
1892N/A * in the common SelectVersion() routine and finally to pass it directly
1892N/A * to the native CreateProcess() Windows process control interface.
1892N/A */
1892N/A {
1892N/A char *cmdline;
1892N/A char *p;
1892N/A char *np;
1892N/A char *ocl;
1892N/A char *ccl;
1892N/A char *unquoted;
1892N/A DWORD exitCode;
1892N/A STARTUPINFO si;
1892N/A PROCESS_INFORMATION pi;
1892N/A
1892N/A /*
1892N/A * The following code block gets and processes the original command
1892N/A * line, replacing the argv[0] equivalent in the command line with
1892N/A * the path to the new executable and removing the appropriate
1892N/A * Multiple JRE support options. Note that similar logic exists
1892N/A * in the platform independent SelectVersion routine, but is
1892N/A * replicated here due to the syntax of CreateProcess().
1892N/A *
1892N/A * The magic "+ 4" characters added to the command line length are
1892N/A * 2 possible quotes around the path (argv[0]), a space after the
1892N/A * path and a terminating null character.
1892N/A */
1892N/A ocl = GetCommandLine();
1892N/A np = ccl = JLI_StringDup(ocl);
1892N/A p = nextarg(&np); /* Discard argv[0] */
1892N/A cmdline = (char *)JLI_MemAlloc(strlen(path) + strlen(np) + 4);
1892N/A if (strchr(path, (int)' ') == NULL && strchr(path, (int)'\t') == NULL)
1892N/A cmdline = strcpy(cmdline, path);
1892N/A else
1892N/A cmdline = strcat(strcat(strcpy(cmdline, "\""), path), "\"");
1892N/A
1892N/A while (*np != (char)0) { /* While more command-line */
1892N/A p = nextarg(&np);
1892N/A if (*p != (char)0) { /* If a token was isolated */
1892N/A unquoted = unquote(p);
1892N/A if (*unquoted == '-') { /* Looks like an option */
1892N/A if (strcmp(unquoted, "-classpath") == 0 ||
1892N/A strcmp(unquoted, "-cp") == 0) { /* Unique cp syntax */
1892N/A cmdline = strcat(strcat(cmdline, " "), p);
1892N/A p = nextarg(&np);
1892N/A if (*p != (char)0) /* If a token was isolated */
1892N/A cmdline = strcat(strcat(cmdline, " "), p);
1892N/A } else if (strncmp(unquoted, "-version:", 9) != 0 &&
1892N/A strcmp(unquoted, "-jre-restrict-search") != 0 &&
1892N/A strcmp(unquoted, "-no-jre-restrict-search") != 0) {
1892N/A cmdline = strcat(strcat(cmdline, " "), p);
1892N/A }
1892N/A } else { /* End of options */
1892N/A cmdline = strcat(strcat(cmdline, " "), p);
1892N/A cmdline = strcat(strcat(cmdline, " "), np);
1892N/A JLI_MemFree((void *)unquoted);
1892N/A break;
1892N/A }
1892N/A JLI_MemFree((void *)unquoted);
1892N/A }
1892N/A }
1892N/A JLI_MemFree((void *)ccl);
1892N/A
1892N/A if (_launcher_debug) {
1892N/A np = ccl = JLI_StringDup(cmdline);
1892N/A p = nextarg(&np);
1892N/A printf("ReExec Command: %s (%s)\n", path, p);
1892N/A printf("ReExec Args: %s\n", np);
1892N/A JLI_MemFree((void *)ccl);
1892N/A }
1892N/A (void)fflush(stdout);
1892N/A (void)fflush(stderr);
1892N/A
1892N/A /*
1892N/A * The following code is modeled after a model presented in the
1892N/A * Microsoft Technical Article "Moving Unix Applications to
1892N/A * Windows NT" (March 6, 1994) and "Creating Processes" on MSDN
1892N/A * (Februrary 2005). It approximates UNIX spawn semantics with
1892N/A * the parent waiting for termination of the child.
1892N/A */
1892N/A memset(&si, 0, sizeof(si));
1892N/A si.cb =sizeof(STARTUPINFO);
1892N/A memset(&pi, 0, sizeof(pi));
1892N/A
1892N/A if (!CreateProcess((LPCTSTR)path, /* executable name */
1892N/A (LPTSTR)cmdline, /* command line */
1892N/A (LPSECURITY_ATTRIBUTES)NULL, /* process security attr. */
1892N/A (LPSECURITY_ATTRIBUTES)NULL, /* thread security attr. */
1892N/A (BOOL)TRUE, /* inherits system handles */
1892N/A (DWORD)0, /* creation flags */
1892N/A (LPVOID)NULL, /* environment block */
1892N/A (LPCTSTR)NULL, /* current directory */
1892N/A (LPSTARTUPINFO)&si, /* (in) startup information */
1892N/A (LPPROCESS_INFORMATION)&pi)) { /* (out) process information */
1892N/A ReportSysErrorMessage2("CreateProcess(%s, ...) failed: %s",
1892N/A path, JNI_TRUE);
1892N/A exit(1);
1892N/A }
1892N/A
1892N/A if (WaitForSingleObject(pi.hProcess, INFINITE) != WAIT_FAILED) {
1892N/A if (GetExitCodeProcess(pi.hProcess, &exitCode) == FALSE)
1892N/A exitCode = 1;
1892N/A } else {
1892N/A ReportErrorMessage("WaitForSingleObject() failed.", JNI_TRUE);
1892N/A exitCode = 1;
1892N/A }
1892N/A
1892N/A CloseHandle(pi.hThread);
1892N/A CloseHandle(pi.hProcess);
1892N/A
1892N/A exit(exitCode);
1892N/A }
1892N/A
1892N/A}
1892N/A
1892N/A#endif /* ifndef GAMMA */
1892N/A
1892N/A
1892N/A/*
1892N/A * Wrapper for platform dependent unsetenv function.
1892N/A */
1892N/Aint
1892N/AUnsetEnv(char *name)
1892N/A{
1892N/A int ret;
1892N/A char *buf = JLI_MemAlloc(strlen(name) + 2);
1892N/A buf = strcat(strcpy(buf, name), "=");
1892N/A ret = _putenv(buf);
1892N/A JLI_MemFree(buf);
1892N/A return (ret);
1892N/A}
1892N/A
1892N/A/* --- Splash Screen shared library support --- */
1892N/A
1892N/Astatic const char* SPLASHSCREEN_SO = "\\bin\\splashscreen.dll";
1892N/A
1892N/Astatic HMODULE hSplashLib = NULL;
1892N/A
1892N/Avoid* SplashProcAddress(const char* name) {
1892N/A char libraryPath[MAXPATHLEN]; /* some extra space for strcat'ing SPLASHSCREEN_SO */
1892N/A
1892N/A if (!GetJREPath(libraryPath, MAXPATHLEN)) {
1892N/A return NULL;
1892N/A }
1892N/A if (strlen(libraryPath)+strlen(SPLASHSCREEN_SO) >= MAXPATHLEN) {
1892N/A return NULL;
1892N/A }
1892N/A strcat(libraryPath, SPLASHSCREEN_SO);
1892N/A
1892N/A if (!hSplashLib) {
1892N/A hSplashLib = LoadLibrary(libraryPath);
1892N/A }
1892N/A if (hSplashLib) {
1892N/A return GetProcAddress(hSplashLib, name);
1892N/A } else {
1892N/A return NULL;
1892N/A }
1892N/A}
1892N/A
1892N/Avoid SplashFreeLibrary() {
1892N/A if (hSplashLib) {
1892N/A FreeLibrary(hSplashLib);
1892N/A hSplashLib = NULL;
1892N/A }
1892N/A}
1892N/A
1892N/Aconst char *
1892N/Ajlong_format_specifier() {
1892N/A return "%I64d";
1892N/A}
1892N/A
1892N/A/*
1892N/A * Block current thread and continue execution in a new thread
1892N/A */
1892N/Aint
1892N/AContinueInNewThread(int (JNICALL *continuation)(void *), jlong stack_size, void * args) {
1892N/A int rslt = 0;
1892N/A unsigned thread_id;
1892N/A
1892N/A#ifndef STACK_SIZE_PARAM_IS_A_RESERVATION
1892N/A#define STACK_SIZE_PARAM_IS_A_RESERVATION (0x10000)
1892N/A#endif
1892N/A
1892N/A /*
1892N/A * STACK_SIZE_PARAM_IS_A_RESERVATION is what we want, but it's not
1892N/A * supported on older version of Windows. Try first with the flag; and
1892N/A * if that fails try again without the flag. See MSDN document or HotSpot
1892N/A * source (os_win32.cpp) for details.
1892N/A */
1892N/A HANDLE thread_handle =
1892N/A (HANDLE)_beginthreadex(NULL,
1892N/A (unsigned)stack_size,
1892N/A continuation,
1892N/A args,
1892N/A STACK_SIZE_PARAM_IS_A_RESERVATION,
1892N/A &thread_id);
1892N/A if (thread_handle == NULL) {
1892N/A thread_handle =
1892N/A (HANDLE)_beginthreadex(NULL,
1892N/A (unsigned)stack_size,
1892N/A continuation,
1892N/A args,
1892N/A 0,
1892N/A &thread_id);
1892N/A }
1892N/A
1892N/A /* AWT preloading (AFTER main thread start) */
1892N/A#ifdef ENABLE_AWT_PRELOAD
1892N/A /* D3D preloading */
1892N/A if (awtPreloadD3D != 0) {
1892N/A char *envValue;
1892N/A /* D3D routines checks env.var J2D_D3D if no appropriate
1892N/A * command line params was specified
1892N/A */
1892N/A envValue = getenv("J2D_D3D");
1892N/A if (envValue != NULL && stricmp(envValue, "false") == 0) {
1892N/A awtPreloadD3D = 0;
1892N/A }
1892N/A /* Test that AWT preloading isn't disabled by J2D_D3D_PRELOAD env.var */
1892N/A envValue = getenv("J2D_D3D_PRELOAD");
1892N/A if (envValue != NULL && stricmp(envValue, "false") == 0) {
1892N/A awtPreloadD3D = 0;
1892N/A }
1892N/A if (awtPreloadD3D < 0) {
1892N/A /* If awtPreloadD3D is still undefined (-1), test
1892N/A * if it is turned on by J2D_D3D_PRELOAD env.var.
1892N/A * By default it's turned OFF.
1892N/A */
1892N/A awtPreloadD3D = 0;
1892N/A if (envValue != NULL && stricmp(envValue, "true") == 0) {
1892N/A awtPreloadD3D = 1;
1892N/A }
1892N/A }
1892N/A }
1892N/A if (awtPreloadD3D) {
1892N/A AWTPreload(D3D_PRELOAD_FUNC);
1892N/A }
1892N/A#endif /* ENABLE_AWT_PRELOAD */
1892N/A
1892N/A if (thread_handle) {
1892N/A WaitForSingleObject(thread_handle, INFINITE);
1892N/A GetExitCodeThread(thread_handle, &rslt);
1892N/A CloseHandle(thread_handle);
1892N/A } else {
1892N/A rslt = continuation(args);
1892N/A }
1892N/A
1892N/A#ifdef ENABLE_AWT_PRELOAD
1892N/A if (awtPreloaded) {
1892N/A AWTPreloadStop();
1892N/A }
1892N/A#endif /* ENABLE_AWT_PRELOAD */
1892N/A
1892N/A return rslt;
1892N/A}
1892N/A
1892N/A/* Linux only, empty on windows. */
1892N/Avoid SetJavaLauncherPlatformProps() {}
1892N/A
1892N/A
1892N/A//==============================
1892N/A// AWT preloading
1892N/A#ifdef ENABLE_AWT_PRELOAD
1892N/A
1892N/Atypedef int FnPreloadStart(void);
1892N/Atypedef void FnPreloadStop(void);
1892N/Astatic FnPreloadStop *fnPreloadStop = NULL;
1892N/Astatic HMODULE hPreloadAwt = NULL;
1892N/A
1892N/A/*
1892N/A * Starts AWT preloading
1892N/A */
1892N/Aint AWTPreload(const char *funcName)
1892N/A{
1892N/A int result = -1;
1892N/A
1892N/A // load AWT library once (if several preload function should be called)
1892N/A if (hPreloadAwt == NULL) {
1892N/A // awt.dll is not loaded yet
1892N/A char libraryPath[MAXPATHLEN];
1892N/A int jrePathLen = 0;
1892N/A HMODULE hJava = NULL;
1892N/A HMODULE hVerify = NULL;
1892N/A
1892N/A while (1) {
1892N/A // awt.dll depends on jvm.dll & java.dll;
1892N/A // jvm.dll is already loaded, so we need only java.dll;
1892N/A // java.dll depends on MSVCRT lib & verify.dll.
1892N/A if (!GetJREPath(libraryPath, MAXPATHLEN)) {
1892N/A break;
1892N/A }
1892N/A
1892N/A // save path length
1892N/A jrePathLen = strlen(libraryPath);
1892N/A
1892N/A // load msvcrt 1st
1892N/A LoadMSVCRT();
1892N/A
1892N/A // load verify.dll
1892N/A strcat(libraryPath, "\\bin\\verify.dll");
1892N/A hVerify = LoadLibrary(libraryPath);
1892N/A if (hVerify == NULL) {
1892N/A break;
1892N/A }
1892N/A
1892N/A // restore jrePath
1892N/A libraryPath[jrePathLen] = 0;
1892N/A // load java.dll
1892N/A strcat(libraryPath, "\\bin\\" JAVA_DLL);
1892N/A hJava = LoadLibrary(libraryPath);
1892N/A if (hJava == NULL) {
1892N/A break;
1892N/A }
1892N/A
1892N/A // restore jrePath
1892N/A libraryPath[jrePathLen] = 0;
1892N/A // load awt.dll
1892N/A strcat(libraryPath, "\\bin\\awt.dll");
1892N/A hPreloadAwt = LoadLibrary(libraryPath);
1892N/A if (hPreloadAwt == NULL) {
1892N/A break;
1892N/A }
1892N/A
1892N/A // get "preloadStop" func ptr
1892N/A fnPreloadStop = (FnPreloadStop *)GetProcAddress(hPreloadAwt, "preloadStop");
1892N/A
1892N/A break;
1892N/A }
1892N/A }
1892N/A
1892N/A if (hPreloadAwt != NULL) {
1892N/A FnPreloadStart *fnInit = (FnPreloadStart *)GetProcAddress(hPreloadAwt, funcName);
1892N/A if (fnInit != NULL) {
1892N/A // don't forget to stop preloading
1892N/A awtPreloaded = 1;
1892N/A
1892N/A result = fnInit();
1892N/A }
1892N/A }
1892N/A
1892N/A return result;
1892N/A}
1892N/A
1892N/A/*
1892N/A * Terminates AWT preloading
1892N/A */
1892N/Avoid AWTPreloadStop() {
1892N/A if (fnPreloadStop != NULL) {
1892N/A fnPreloadStop();
1892N/A }
1892N/A}
1892N/A
1892N/A#endif /* ENABLE_AWT_PRELOAD */