java_md.c revision 0
0N/A/*
3909N/A * Copyright 1997-2007 Sun Microsystems, Inc. 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. Sun designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
2362N/A * CA 95054 USA or visit www.sun.com if you need additional information or
2362N/A * have any 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>
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#define CRT_DLL "msvcr71.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
0N/Astatic jboolean _isjavaw = JNI_FALSE;
0N/A
0N/Avoid
0N/ASetJavaw()
4022N/A{
0N/A _isjavaw = JNI_TRUE;
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 *_argc,
0N/A char ***_argv,
0N/A char jrepath[],
0N/A jint so_jrepath,
0N/A char jvmpath[],
0N/A jint so_jvmpath,
0N/A char **original_argv) {
0N/A char * jvmtype;
0N/A int i = 0;
0N/A char** pargv = *_argv;
0N/A int running = CURRENT_DATA_MODEL;
0N/A
0N/A int wanted = running;
0N/A
0N/A for (i = 0; i < *_argc ; i++) {
0N/A if (JLI_StrCmp(pargv[i], "-J-d64") == 0 || JLI_StrCmp(pargv[i], "-d64") == 0) {
0N/A wanted = 64;
0N/A continue;
0N/A }
0N/A if (JLI_StrCmp(pargv[i], "-J-d32") == 0 || JLI_StrCmp(pargv[i], "-d32") == 0) {
0N/A wanted = 32;
0N/A continue;
0N/A }
0N/A }
0N/A if (running != wanted) {
0N/A 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)) {
0N/A ReportErrorMessage(JRE_ERROR1);
0N/A exit(2);
0N/A }
0N/A
0N/A /* Find the specified JVM type */
0N/A if (ReadKnownVMs(jrepath, (char*)GetArch(), JNI_FALSE) < 1) {
0N/A ReportErrorMessage(CFG_ERROR7);
0N/A exit(1);
0N/A }
0N/A jvmtype = CheckJvmType(_argc, _argv, JNI_FALSE);
0N/A
0N/A jvmpath[0] = '\0';
0N/A if (!GetJVMPath(jrepath, jvmtype, jvmpath, so_jvmpath)) {
0N/A 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}
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 sprintf(javadll, "%s\\bin\\" JAVA_DLL, path);
0N/A if (stat(javadll, &s) == 0) {
0N/A goto found;
0N/A }
0N/A
0N/A /* Does this app ship a private JRE in <apphome>\jre directory? */
0N/A sprintf(javadll, "%s\\jre\\bin\\" JAVA_DLL, path);
0N/A if (stat(javadll, &s) == 0) {
0N/A JLI_StrCat(path, "\\jre");
2751N/A goto found;
2751N/A }
2751N/A }
2751N/A
0N/A /* Look for a public JRE on this machine. */
0N/A if (GetPublicJREHome(path, pathsize)) {
2751N/A goto found;
0N/A }
0N/A
2751N/A ReportErrorMessage(JRE_ERROR8 JAVA_DLL);
2751N/A return JNI_FALSE;
2751N/A
2751N/A found:
2751N/A JLI_TraceLauncher("JRE path is %s\n", path);
0N/A return JNI_TRUE;
0N/A}
0N/A
2751N/A/*
2751N/A * Given a JRE location and a JVM type, construct what the name the
2751N/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 sprintf(jvmpath, "%s\\" JVM_DLL, jvmtype);
0N/A } else {
0N/A sprintf(jvmpath, "%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{
0N/A HINSTANCE handle;
0N/A char crtpath[MAXPATHLEN];
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.
0N/A */
0N/A if (GetJREPath(crtpath, MAXPATHLEN)) {
0N/A (void)JLI_StrCat(crtpath, "\\bin\\" CRT_DLL); /* Add crt dll */
0N/A JLI_TraceLauncher("CRT path is %s\n", crtpath);
2751N/A if (_access(crtpath, 0) == 0) {
2751N/A if (LoadLibrary(crtpath) == 0) {
2751N/A ReportErrorMessage(DLL_ERROR4, crtpath);
2751N/A return JNI_FALSE;
2751N/A }
2751N/A }
2751N/A }
2751N/A
2751N/A /* Load the Java VM DLL */
2751N/A if ((handle = LoadLibrary(jvmpath)) == 0) {
2751N/A ReportErrorMessage(DLL_ERROR4, (char *)jvmpath);
2751N/A return JNI_FALSE;
2751N/A }
2751N/A
2751N/A /* Now get the function addresses */
2751N/A ifn->CreateJavaVM =
2751N/A (void *)GetProcAddress(handle, "JNI_CreateJavaVM");
2751N/A ifn->GetDefaultJavaVMInitArgs =
0N/A (void *)GetProcAddress(handle, "JNI_GetDefaultJavaVMInitArgs");
0N/A if (ifn->CreateJavaVM == 0 || ifn->GetDefaultJavaVMInitArgs == 0) {
0N/A 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 */
2751N/A return JNI_TRUE;
0N/A}
0N/A
2751N/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) {
0N/A 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))) {
0N/A 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) {
0N/A 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) {
0N/A 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 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}
0N/A
0N/Avoid
0N/AReportErrorMessage(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/*
0N/A * Just like 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/AReportErrorMessageSys(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) {
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
0N/Avoid 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",
3385N/A (MB_OK|MB_ICONSTOP|MB_APPLMODAL));
3385N/A } else {
3385N/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
2751N/A * constraints, return NULL.
0N/A *
2751N/A * It doesn't matter if we get an error reading the registry, or we just
2751N/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 }
4022N/A }
4022N/A
4022N/A /*
4022N/A * Extract "JavaHome" from the "best" registry directory and return
4022N/A * that path. If no appropriate version was located, or there is an
4022N/A * error in extracting the "JavaHome" string, return null.
4022N/A */
4022N/A if (best == NULL)
4022N/A return (NULL);
4022N/A else {
4022N/A if (RegOpenKeyEx(top_key, best, 0, KEY_READ, &ver_key)
4022N/A != ERROR_SUCCESS) {
0N/A JLI_MemFree(best);
0N/A if (ver_key != NULL)
0N/A RegCloseKey(ver_key);
return (NULL);
}
JLI_MemFree(best);
len = MAXNAMELEN;
if (RegQueryValueEx(ver_key, "JavaHome", NULL, NULL, (LPBYTE)name, &len)
!= ERROR_SUCCESS) {
if (ver_key != NULL)
RegCloseKey(ver_key);
return (NULL);
}
if (ver_key != NULL)
RegCloseKey(ver_key);
return (JLI_StringDup(name));
}
}
/*
* This is the global entry point. It examines the host for the optimal
* JRE to be used by scanning a set of registry entries. This set of entries
* is hardwired on Windows as "Software\JavaSoft\Java Runtime Environment"
* under the set of roots "{ HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE }".
*
* This routine simply opens each of these registry directories before passing
* control onto ProcessDir().
*/
char *
LocateJRE(manifest_info* info) {
HKEY key = NULL;
char *path;
int key_index;
HKEY root_keys[2] = { HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE };
for (key_index = 0; key_index <= 1; key_index++) {
if (RegOpenKeyEx(root_keys[key_index], JRE_KEY, 0, KEY_READ, &key)
== ERROR_SUCCESS)
if ((path = ProcessDir(info, key)) != NULL) {
if (key != NULL)
RegCloseKey(key);
return (path);
}
if (key != NULL)
RegCloseKey(key);
}
return NULL;
}
/*
* Local helper routine to isolate a single token (option or argument)
* from the command line.
*
* This routine accepts a pointer to a character pointer. The first
* token (as defined by MSDN command-line argument syntax) is isolated
* from that string.
*
* Upon return, the input character pointer pointed to by the parameter s
* is updated to point to the remainding, unscanned, portion of the string,
* or to a null character if the entire string has been consummed.
*
* This function returns a pointer to a null-terminated string which
* contains the isolated first token, or to the null character if no
* token could be isolated.
*
* Note the side effect of modifying the input string s by the insertion
* of a null character, making it two strings.
*
* See "Parsing C Command-Line Arguments" in the MSDN Library for the
* parsing rule details. The rule summary from that specification is:
*
* * Arguments are delimited by white space, which is either a space or a tab.
*
* * A string surrounded by double quotation marks is interpreted as a single
* argument, regardless of white space contained within. A quoted string can
* be embedded in an argument. Note that the caret (^) is not recognized as
* an escape character or delimiter.
*
* * A double quotation mark preceded by a backslash, \", is interpreted as a
* literal double quotation mark (").
*
* * Backslashes are interpreted literally, unless they immediately precede a
* double quotation mark.
*
* * If an even number of backslashes is followed by a double quotation mark,
* then one backslash (\) is placed in the argv array for every pair of
* backslashes (\\), and the double quotation mark (") is interpreted as a
* string delimiter.
*
* * If an odd number of backslashes is followed by a double quotation mark,
* then one backslash (\) is placed in the argv array for every pair of
* backslashes (\\) and the double quotation mark is interpreted as an
* escape sequence by the remaining backslash, causing a literal double
* quotation mark (") to be placed in argv.
*/
static char*
nextarg(char** s) {
char *p = *s;
char *head;
int slashes = 0;
int inquote = 0;
/*
* Strip leading whitespace, which MSDN defines as only space or tab.
* (Hence, no locale specific "isspace" here.)
*/
while (*p != (char)0 && (*p == ' ' || *p == '\t'))
p++;
head = p; /* Save the start of the token to return */
/*
* Isolate a token from the command line.
*/
while (*p != (char)0 && (inquote || !(*p == ' ' || *p == '\t'))) {
if (*p == '\\' && *(p+1) == '"' && slashes % 2 == 0)
p++;
else if (*p == '"')
inquote = !inquote;
slashes = (*p++ == '\\') ? slashes + 1 : 0;
}
/*
* If the token isolated isn't already terminated in a "char zero",
* then replace the whitespace character with one and move to the
* next character.
*/
if (*p != (char)0)
*p++ = (char)0;
/*
* Update the parameter to point to the head of the remaining string
* reflecting the command line and return a pointer to the leading
* token which was isolated from the command line.
*/
*s = p;
return (head);
}
/*
* Local helper routine to return a string equivalent to the input string
* s, but with quotes removed so the result is a string as would be found
* in argv[]. The returned string should be freed by a call to JLI_MemFree().
*
* The rules for quoting (and escaped quotes) are:
*
* 1 A double quotation mark preceded by a backslash, \", is interpreted as a
* literal double quotation mark (").
*
* 2 Backslashes are interpreted literally, unless they immediately precede a
* double quotation mark.
*
* 3 If an even number of backslashes is followed by a double quotation mark,
* then one backslash (\) is placed in the argv array for every pair of
* backslashes (\\), and the double quotation mark (") is interpreted as a
* string delimiter.
*
* 4 If an odd number of backslashes is followed by a double quotation mark,
* then one backslash (\) is placed in the argv array for every pair of
* backslashes (\\) and the double quotation mark is interpreted as an
* escape sequence by the remaining backslash, causing a literal double
* quotation mark (") to be placed in argv.
*/
static char*
unquote(const char *s) {
const char *p = s; /* Pointer to the tail of the original string */
char *un = (char*)JLI_MemAlloc(JLI_StrLen(s) + 1); /* Ptr to unquoted string */
char *pun = un; /* Pointer to the tail of the unquoted string */
while (*p != '\0') {
if (*p == '"') {
p++;
} else if (*p == '\\') {
const char *q = p + JLI_StrSpn(p,"\\");
if (*q == '"')
do {
*pun++ = '\\';
p += 2;
} while (*p == '\\' && p < q);
else
while (p < q)
*pun++ = *p++;
} else {
*pun++ = *p++;
}
}
*pun = '\0';
return un;
}
/*
* Given a path to a jre to execute, this routine checks if this process
* is indeed that jre. If not, it exec's that jre.
*
* We want to actually check the paths rather than just the version string
* built into the executable, so that given version specification will yield
* the exact same Java environment, regardless of the version of the arbitrary
* launcher we start with.
*/
void
ExecJRE(char *jre, char **argv) {
int len;
char path[MAXPATHLEN + 1];
const char *progname = GetProgramName();
/*
* Resolve the real path to the currently running launcher.
*/
len = GetModuleFileName(NULL, path, MAXPATHLEN + 1);
if (len == 0 || len > MAXPATHLEN) {
ReportErrorMessageSys(JRE_ERROR9, progname);
exit(1);
}
JLI_TraceLauncher("ExecJRE: old: %s\n", path);
JLI_TraceLauncher("ExecJRE: new: %s\n", jre);
/*
* If the path to the selected JRE directory is a match to the initial
* portion of the path to the currently executing JRE, we have a winner!
* If so, just return.
*/
if (JLI_StrNCaseCmp(jre, path, JLI_StrLen(jre)) == 0)
return; /* I am the droid you were looking for */
/*
* If this isn't the selected version, exec the selected version.
*/
(void)JLI_StrCat(JLI_StrCat(JLI_StrCpy(path, jre), "\\bin\\"), progname);
(void)JLI_StrCat(path, ".exe");
/*
* Although Windows has an execv() entrypoint, it doesn't actually
* overlay a process: it can only create a new process and terminate
* the old process. Therefore, any processes waiting on the initial
* process wake up and they shouldn't. Hence, a chain of pseudo-zombie
* processes must be retained to maintain the proper wait semantics.
* Fortunately the image size of the launcher isn't too large at this
* time.
*
* If it weren't for this semantic flaw, the code below would be ...
*
* execv(path, argv);
* ReportErrorMessage("Error: Exec of %s failed\n", path);
* exit(1);
*
* The incorrect exec semantics could be addressed by:
*
* exit((int)spawnv(_P_WAIT, path, argv));
*
* Unfortunately, a bug in Windows spawn/exec impementation prevents
* this from completely working. All the Windows POSIX process creation
* interfaces are implemented as wrappers around the native Windows
* function CreateProcess(). CreateProcess() takes a single string
* to specify command line options and arguments, so the POSIX routine
* wrappers build a single string from the argv[] array and in the
* process, any quoting information is lost.
*
* The solution to this to get the original command line, to process it
* to remove the new multiple JRE options (if any) as was done for argv
* in the common SelectVersion() routine and finally to pass it directly
* to the native CreateProcess() Windows process control interface.
*/
{
char *cmdline;
char *p;
char *np;
char *ocl;
char *ccl;
char *unquoted;
DWORD exitCode;
STARTUPINFO si;
PROCESS_INFORMATION pi;
/*
* The following code block gets and processes the original command
* line, replacing the argv[0] equivalent in the command line with
* the path to the new executable and removing the appropriate
* Multiple JRE support options. Note that similar logic exists
* in the platform independent SelectVersion routine, but is
* replicated here due to the syntax of CreateProcess().
*
* The magic "+ 4" characters added to the command line length are
* 2 possible quotes around the path (argv[0]), a space after the
* path and a terminating null character.
*/
ocl = GetCommandLine();
np = ccl = JLI_StringDup(ocl);
p = nextarg(&np); /* Discard argv[0] */
cmdline = (char *)JLI_MemAlloc(JLI_StrLen(path) + JLI_StrLen(np) + 4);
if (JLI_StrChr(path, (int)' ') == NULL && JLI_StrChr(path, (int)'\t') == NULL)
cmdline = JLI_StrCpy(cmdline, path);
else
cmdline = JLI_StrCat(JLI_StrCat(JLI_StrCpy(cmdline, "\""), path), "\"");
while (*np != (char)0) { /* While more command-line */
p = nextarg(&np);
if (*p != (char)0) { /* If a token was isolated */
unquoted = unquote(p);
if (*unquoted == '-') { /* Looks like an option */
if (JLI_StrCmp(unquoted, "-classpath") == 0 ||
JLI_StrCmp(unquoted, "-cp") == 0) { /* Unique cp syntax */
cmdline = JLI_StrCat(JLI_StrCat(cmdline, " "), p);
p = nextarg(&np);
if (*p != (char)0) /* If a token was isolated */
cmdline = JLI_StrCat(JLI_StrCat(cmdline, " "), p);
} else if (JLI_StrNCmp(unquoted, "-version:", 9) != 0 &&
JLI_StrCmp(unquoted, "-jre-restrict-search") != 0 &&
JLI_StrCmp(unquoted, "-no-jre-restrict-search") != 0) {
cmdline = JLI_StrCat(JLI_StrCat(cmdline, " "), p);
}
} else { /* End of options */
cmdline = JLI_StrCat(JLI_StrCat(cmdline, " "), p);
cmdline = JLI_StrCat(JLI_StrCat(cmdline, " "), np);
JLI_MemFree((void *)unquoted);
break;
}
JLI_MemFree((void *)unquoted);
}
}
JLI_MemFree((void *)ccl);
if (JLI_IsTraceLauncher()) {
np = ccl = JLI_StringDup(cmdline);
p = nextarg(&np);
printf("ReExec Command: %s (%s)\n", path, p);
printf("ReExec Args: %s\n", np);
JLI_MemFree((void *)ccl);
}
(void)fflush(stdout);
(void)fflush(stderr);
/*
* The following code is modeled after a model presented in the
* Microsoft Technical Article "Moving Unix Applications to
* Windows NT" (March 6, 1994) and "Creating Processes" on MSDN
* (Februrary 2005). It approximates UNIX spawn semantics with
* the parent waiting for termination of the child.
*/
memset(&si, 0, sizeof(si));
si.cb =sizeof(STARTUPINFO);
memset(&pi, 0, sizeof(pi));
if (!CreateProcess((LPCTSTR)path, /* executable name */
(LPTSTR)cmdline, /* command line */
(LPSECURITY_ATTRIBUTES)NULL, /* process security attr. */
(LPSECURITY_ATTRIBUTES)NULL, /* thread security attr. */
(BOOL)TRUE, /* inherits system handles */
(DWORD)0, /* creation flags */
(LPVOID)NULL, /* environment block */
(LPCTSTR)NULL, /* current directory */
(LPSTARTUPINFO)&si, /* (in) startup information */
(LPPROCESS_INFORMATION)&pi)) { /* (out) process information */
ReportErrorMessageSys(SYS_ERROR1, path);
exit(1);
}
if (WaitForSingleObject(pi.hProcess, INFINITE) != WAIT_FAILED) {
if (GetExitCodeProcess(pi.hProcess, &exitCode) == FALSE)
exitCode = 1;
} else {
ReportErrorMessage(SYS_ERROR2);
exitCode = 1;
}
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
exit(exitCode);
}
}
/*
* Wrapper for platform dependent unsetenv function.
*/
int
UnsetEnv(char *name)
{
int ret;
char *buf = JLI_MemAlloc(JLI_StrLen(name) + 2);
buf = JLI_StrCat(JLI_StrCpy(buf, name), "=");
ret = _putenv(buf);
JLI_MemFree(buf);
return (ret);
}
/* --- Splash Screen shared library support --- */
static const char* SPLASHSCREEN_SO = "\\bin\\splashscreen.dll";
static HMODULE hSplashLib = NULL;
void* SplashProcAddress(const char* name) {
char libraryPath[MAXPATHLEN]; /* some extra space for JLI_StrCat'ing SPLASHSCREEN_SO */
if (!GetJREPath(libraryPath, MAXPATHLEN)) {
return NULL;
}
if (JLI_StrLen(libraryPath)+JLI_StrLen(SPLASHSCREEN_SO) >= MAXPATHLEN) {
return NULL;
}
JLI_StrCat(libraryPath, SPLASHSCREEN_SO);
if (!hSplashLib) {
hSplashLib = LoadLibrary(libraryPath);
}
if (hSplashLib) {
return GetProcAddress(hSplashLib, name);
} else {
return NULL;
}
}
void SplashFreeLibrary() {
if (hSplashLib) {
FreeLibrary(hSplashLib);
hSplashLib = NULL;
}
}
const char *
jlong_format_specifier() {
return "%I64d";
}
/*
* Block current thread and continue execution in a new thread
*/
int
ContinueInNewThread0(int (JNICALL *continuation)(void *), jlong stack_size, void * args) {
int rslt = 0;
unsigned thread_id;
#ifndef STACK_SIZE_PARAM_IS_A_RESERVATION
#define STACK_SIZE_PARAM_IS_A_RESERVATION (0x10000)
#endif
/*
* STACK_SIZE_PARAM_IS_A_RESERVATION is what we want, but it's not
* supported on older version of Windows. Try first with the flag; and
* if that fails try again without the flag. See MSDN document or HotSpot
* source (os_win32.cpp) for details.
*/
HANDLE thread_handle =
(HANDLE)_beginthreadex(NULL,
(unsigned)stack_size,
continuation,
args,
STACK_SIZE_PARAM_IS_A_RESERVATION,
&thread_id);
if (thread_handle == NULL) {
thread_handle =
(HANDLE)_beginthreadex(NULL,
(unsigned)stack_size,
continuation,
args,
0,
&thread_id);
}
if (thread_handle) {
WaitForSingleObject(thread_handle, INFINITE);
GetExitCodeThread(thread_handle, &rslt);
CloseHandle(thread_handle);
} else {
rslt = continuation(args);
}
return rslt;
}
/* Linux only, empty on windows. */
void SetJavaLauncherPlatformProps() {}