java_md.c revision 2581
0N/A/*
381N/A * Copyright (c) 1997, 2010, 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
0N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/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 *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
0N/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);
1365N/Astatic void EnsureJreInstallation(const char *jrepath);
0N/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
0N/ACreateExecutionEnvironment(int *pargc, char ***pargv,
0N/A char *jrepath, jint so_jrepath,
0N/A char *jvmpath, jint so_jvmpath) {
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
0N/A char** argv = *pargv;
0N/A for (i = 0; i < *pargc ; i++) {
0N/A if (JLI_StrCmp(argv[i], "-J-d64") == 0 || JLI_StrCmp(argv[i], "-d64") == 0) {
0N/A wanted = 64;
0N/A continue;
0N/A }
0N/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) {
0N/A JLI_ReportErrorMessage(JRE_ERROR2, wanted);
0N/A exit(1);
0N/A }
0N/A
517N/A /* Do this before we read jvm.cfg */
0N/A EnsureJreInstallation(jrepath);
0N/A
0N/A /* Find out where the JRE is that we will be using. */
1365N/A if (!GetJREPath(jrepath, so_jrepath)) {
1365N/A JLI_ReportErrorMessage(JRE_ERROR1);
1365N/A exit(2);
0N/A }
0N/A
517N/A /* Find the specified JVM type */
0N/A if (ReadKnownVMs(jrepath, (char*)GetArch(), JNI_FALSE) < 1) {
0N/A JLI_ReportErrorMessage(CFG_ERROR7);
0N/A exit(1);
0N/A }
0N/A
517N/A jvmtype = CheckJvmType(pargc, pargv, JNI_FALSE);
0N/A if (JLI_StrCmp(jvmtype, "ERROR") == 0) {
0N/A JLI_ReportErrorMessage(CFG_ERROR9);
0N/A exit(4);
0N/A }
0N/A
0N/A jvmpath[0] = '\0';
517N/A if (!GetJVMPath(jrepath, jvmtype, jvmpath, so_jvmpath)) {
0N/A JLI_ReportErrorMessage(CFG_ERROR8, jvmtype, jvmpath);
0N/A exit(4);
0N/A }
0N/A /* If we got here, jvmpath has been correctly initialized. */
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.
1365N/A * This is clearly completely specific to the exact compiler version
1365N/A * which isn't very nice, but its hardly the only place.
1365N/A * No attempt to look for compiler versions in between 2003 and 2010
1365N/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
1365N/A#if _MSC_VER >= 1600
1365N/A#define CRT_DLL "msvcr100.dll"
1365N/A#endif
1365N/A#ifdef CRT_DLL
1365N/A if (GetJREPath(crtpath, MAXPATHLEN)) {
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/*
1365N/A * The preJVMStart is a function in the jkernel.dll, which
1365N/A * performs the final step of synthesizing back the decomposed
1365N/A * modules (partial install) to the full JRE. Any tool which
1365N/A * uses the JRE must peform this step to ensure the complete synthesis.
1365N/A * The EnsureJreInstallation function calls preJVMStart based on
1365N/A * the conditions outlined below, noting that the operation
1365N/A * will fail silently if any of conditions are not met.
1365N/A * NOTE: this call must be made before jvm.dll is loaded, or jvm.cfg
1365N/A * is read, since jvm.cfg will be modified by the preJVMStart.
1365N/A * 1. Are we on a supported platform.
1365N/A * 2. Find the location of the JRE or the Kernel JRE.
1365N/A * 3. check existence of JREHOME/lib/bundles
1365N/A * 4. check jkernel.dll and invoke the entry-point
1365N/A */
1365N/Atypedef VOID (WINAPI *PREJVMSTART)();
1365N/A
1365N/Astatic void
1365N/AEnsureJreInstallation(const char* jrepath)
1365N/A{
1365N/A HINSTANCE handle;
1365N/A char tmpbuf[MAXPATHLEN];
1365N/A PREJVMSTART PreJVMStart;
1365N/A struct stat s;
1365N/A
1365N/A /* Make sure the jrepath contains something */
1365N/A if (jrepath[0] == NULL) {
1365N/A return;
1365N/A }
1365N/A /* 32 bit windows only please */
1365N/A if (JLI_StrCmp(GetArch(), "i386") != 0 ) {
1365N/A return;
1365N/A }
1365N/A /* Does our bundle directory exist ? */
1365N/A JLI_Snprintf(tmpbuf, sizeof(tmpbuf), "%s\\lib\\bundles", jrepath);
1365N/A if (stat(tmpbuf, &s) != 0) {
1365N/A return;
1365N/A }
1365N/A /* Does our jkernel dll exist ? */
1365N/A JLI_Snprintf(tmpbuf, sizeof(tmpbuf), "%s\\bin\\jkernel.dll", jrepath);
1365N/A if (stat(tmpbuf, &s) != 0) {
1365N/A return;
1365N/A }
1365N/A /* The Microsoft C Runtime Library needs to be loaded first. */
1365N/A if (!LoadMSVCRT()) {
1365N/A return;
1365N/A }
1365N/A /* Load the jkernel.dll */
1365N/A if ((handle = LoadLibrary(tmpbuf)) == 0) {
1365N/A return;
1365N/A }
1365N/A /* Get the function address */
1365N/A PreJVMStart = (PREJVMSTART)GetProcAddress(handle, "preJVMStart");
0N/A if (PreJVMStart == NULL) {
0N/A FreeLibrary(handle);
0N/A return;
0N/A }
0N/A PreJVMStart();
0N/A FreeLibrary(handle);
0N/A return;
0N/A}
0N/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? */
0N/A JLI_Snprintf(javadll, sizeof(javadll), "%s\\bin\\" JAVA_DLL, path);
0N/A if (stat(javadll, &s) == 0) {
0N/A JLI_TraceLauncher("JRE path is %s\n", path);
0N/A return JNI_TRUE;
0N/A }
0N/A
0N/A /* Does this app ship a private JRE in <apphome>\jre directory? */
0N/A JLI_Snprintf(javadll, sizeof (javadll), "%s\\jre\\bin\\" JAVA_DLL, path);
0N/A if (stat(javadll, &s) == 0) {
517N/A JLI_StrCat(path, "\\jre");
0N/A JLI_TraceLauncher("JRE path is %s\n", path);
0N/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)) {
0N/A JLI_TraceLauncher("JRE path is %s\n", path);
0N/A return JNI_TRUE;
0N/A }
0N/A
0N/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, '\\')) {
0N/A JLI_Snprintf(jvmpath, jvmpathsize, "%s\\" JVM_DLL, jvmtype);
0N/A } else {
0N/A JLI_Snprintf(jvmpath, jvmpathsize, "%s\\bin\\%s\\" JVM_DLL, 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{
795N/A HINSTANCE handle;
0N/A
1365N/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
517N/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.
0N/A *
0N/A */
0N/A LoadMSVCRT();
0N/A
0N/A /* Load the Java VM DLL */
0N/A if ((handle = LoadLibrary(jvmpath)) == 0) {
0N/A JLI_ReportErrorMessage(DLL_ERROR4, (char *)jvmpath);
517N/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) {
0N/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
517N/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 */
517N/A
0N/A /* Find the current version of the JRE */
0N/A if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, JRE_KEY, 0, KEY_READ, &key) != 0) {
0N/A JLI_ReportErrorMessage(REG_ERROR1, JRE_KEY);
0N/A return JNI_FALSE;
0N/A }
517N/A
0N/A if (!GetStringFromRegistry(key, "CurrentVersion",
0N/A version, sizeof(version))) {
0N/A JLI_ReportErrorMessage(REG_ERROR2, JRE_KEY);
0N/A RegCloseKey(key);
0N/A return JNI_FALSE;
0N/A }
0N/A
517N/A if (JLI_StrCmp(version, GetDotVersion()) != 0) {
0N/A JLI_ReportErrorMessage(REG_ERROR3, JRE_KEY, version, GetDotVersion()
0N/A );
0N/A RegCloseKey(key);
0N/A return JNI_FALSE;
0N/A }
517N/A
0N/A /* Find directory where the current version is installed. */
0N/A if (RegOpenKeyEx(key, version, 0, KEY_READ, &subkey) != 0) {
0N/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)) {
0N/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
517N/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}
0N/A
0N/Avoid
0N/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));
517N/A JLI_MemFree(message);
0N/A } else {
0N/A vfprintf(stderr, fmt, vl);
0N/A fprintf(stderr, "\n");
0N/A }
517N/A va_end(vl);
0N/A}
0N/A
0N/A/*
0N/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
0N/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) {
0N/A mlen += 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) {
517N/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
0N/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) {
0N/A int len;
517N/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) {
0N/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 */
0N/A (void)JLI_StrCat(JLI_StrCat(JLI_StrCpy(path, jre), "\\bin\\"), progname);
0N/A (void)JLI_StrCat(path, ".exe");
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
517N/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);
0N/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 */
517N/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 */
517N/A (LPPROCESS_INFORMATION)&pi)) { /* (out) process information */
0N/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 {
0N/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/*
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 }
0N/A if (thread_handle) {
647N/A WaitForSingleObject(thread_handle, INFINITE);
0N/A GetExitCodeThread(thread_handle, &rslt);
16N/A CloseHandle(thread_handle);
647N/A } else {
647N/A rslt = continuation(args);
647N/A }
647N/A return rslt;
647N/A}
647N/A
647N/A/* Unix only, empty on windows. */
647N/Avoid SetJavaLauncherPlatformProps() {}
647N/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
1645N/Ajclass FindBootStrapClass(JNIEnv *env, const char *classname)
647N/A{
1645N/A HMODULE hJvm;
647N/A
647N/A if (findBootClass == NULL) {
647N/A hJvm = GetModuleHandle(JVM_DLL);
1645N/A if (hJvm == NULL) return NULL;
647N/A /* need to use the demangled entry point */
647N/A findBootClass = (FindClassFromBootLoader_t *)GetProcAddress(hJvm,
16N/A "JVM_FindClassFromBootLoader");
16N/A if (findBootClass == NULL) {
16N/A JLI_ReportErrorMessage(DLL_ERROR4, "JVM_FindClassFromBootLoader");
16N/A return NULL;
16N/A }
16N/A }
16N/A return findBootClass(env, classname);
16N/A}
16N/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
* HotSpot -XX:+ShowMessageBoxOnError in java mode, an empty
* flag field is sufficient to perform the basic UI initialization.
*/
memset(&icx, 0, sizeof(INITCOMMONCONTROLSEX));
icx.dwSize = sizeof(INITCOMMONCONTROLSEX);
InitCommonControlsEx(&icx);
_isjavaw = javaw;
JLI_SetTraceLauncher();
}