/* * Determine if there is an acceptable JRE in the registry directory top_key. * Upon locating the "best" one, return a fully qualified path to it. * "Best" is defined as the most advanced JRE meeting the constraints * contained in the manifest_info. If no JRE in this directory meets the * constraints, return NULL. * * It doesn't matter if we get an error reading the registry, or we just * don't find anything interesting in the directory. We just return NULL * in either case. */ static char * ProcessDir(manifest_info* info, HKEY top_key) { DWORD index = 0; HKEY ver_key; char name[MAXNAMELEN]; int len; char *best = NULL; /* * Enumerate "<top_key>/SOFTWARE/JavaSoft/Java Runtime Environment" * searching for the best available version. */ while (RegEnumKey(top_key, index, name, MAXNAMELEN) == ERROR_SUCCESS) { index++; if (JLI_AcceptableRelease(name, info->jre_version)) if ((best == NULL) || (JLI_ExactVersionId(name, best) > 0)) { if (best != NULL) JLI_MemFree(best); best = JLI_StringDup(name); } } /* * Extract "JavaHome" from the "best" registry directory and return * that path. If no appropriate version was located, or there is an * error in extracting the "JavaHome" string, return null. */ if (best == NULL) return (NULL); else { if (RegOpenKeyEx(top_key, best, 0, KEY_READ, &ver_key) != ERROR_SUCCESS) { JLI_MemFree(best); if (ver_key != NULL) 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)); } }
/* * Compute the name of the executable * * In order to re-exec securely we need the absolute path of the * executable. On Solaris getexecname(3c) may not return an absolute * path so we use dladdr to get the filename of the executable and * then use realpath to derive an absolute path. From Solaris 9 * onwards the filename returned in DL_info structure from dladdr is * an absolute pathname so technically realpath isn't required. * On Linux we read the executable name from /proc/self/exe. * As a fallback, and for platforms other than Solaris and Linux, * we use FindExecName to compute the executable name. */ const char* SetExecname(char **argv) { char* exec_path = NULL; { Dl_info dlinfo; int (*fptr)(); fptr = (int (*)())dlsym(RTLD_DEFAULT, "main"); if (fptr == NULL) { JLI_ReportErrorMessage(DLL_ERROR3, dlerror()); return JNI_FALSE; } if (dladdr((void*)fptr, &dlinfo)) { char *resolved = (char*)JLI_MemAlloc(PATH_MAX+1); if (resolved != NULL) { exec_path = realpath(dlinfo.dli_fname, resolved); if (exec_path == NULL) { JLI_MemFree(resolved); } } } } if (exec_path == NULL) { exec_path = FindExecName(argv[0]); } execname = exec_path; return exec_path; }
/* * Determine if there is an acceptable JRE in the directory dirname. * Upon locating the "best" one, return a fully qualified path to * it. "Best" is defined as the most advanced JRE meeting the * constraints contained in the manifest_info. If no JRE in this * directory meets the constraints, return NULL. * * Note that we don't check for errors in reading the directory * (which would be done by checking errno). This is because it * doesn't matter if we get an error reading the directory, or * we just don't find anything interesting in the directory. We * just return NULL in either case. * * The historical names of j2sdk and j2re were changed to jdk and * jre respecively as part of the 1.5 rebranding effort. Since the * former names are legacy on Linux, they must be recognized for * all time. Fortunately, this is a minor cost. */ static char *ProcessDir(manifest_info *info, char *dirname) { DIR *dirp; struct dirent *dp; char *best = NULL; int offset; int best_offset = 0; char *ret_str = NULL; char buffer[PATH_MAX]; if ((dirp = opendir(dirname)) == NULL) return (NULL); do { if ((dp = readdir(dirp)) != NULL) { offset = 0; if ((JLI_StrNCmp(dp->d_name, "jre", 3) == 0) || (JLI_StrNCmp(dp->d_name, "jdk", 3) == 0)) offset = 3; else if (JLI_StrNCmp(dp->d_name, "j2re", 4) == 0) offset = 4; else if (JLI_StrNCmp(dp->d_name, "j2sdk", 5) == 0) offset = 5; if (offset > 0) { if ((JLI_AcceptableRelease(dp->d_name + offset, info->jre_version)) && CheckSanity(dirname, dp->d_name)) if ((best == NULL) || (JLI_ExactVersionId( dp->d_name + offset, best + best_offset) > 0)) { if (best != NULL) JLI_MemFree(best); best = JLI_StringDup(dp->d_name); best_offset = offset; } } } } while (dp != NULL); (void) closedir(dirp); if (best == NULL) return (NULL); else { ret_str = JLI_MemAlloc(JLI_StrLen(dirname) + JLI_StrLen(best) + 2); sprintf(ret_str, "%s/%s", dirname, best); JLI_MemFree(best); return (ret_str); } }
/* * contains a lib/$LIBARCH/{server,client}/libjvm.so ? */ static jboolean ContainsLibJVM(int wanted, const char *env) { char clientPattern[PATH_MAX + 1]; char serverPattern[PATH_MAX + 1]; char *envpath; char *path; jboolean clientPatternFound; jboolean serverPatternFound; /* fastest path */ if (env == NULL) { return JNI_FALSE; } /* the usual suspects */ JLI_Snprintf(clientPattern, PATH_MAX, "lib/%s/client", GetArchPath(wanted)); JLI_Snprintf(serverPattern, PATH_MAX, "lib/%s/server", GetArchPath(wanted)); /* to optimize for time, test if any of our usual suspects are present. */ clientPatternFound = JLI_StrStr(env, clientPattern) != NULL; serverPatternFound = JLI_StrStr(env, serverPattern) != NULL; if (clientPatternFound == JNI_FALSE && serverPatternFound == JNI_FALSE) { return JNI_FALSE; } /* * we have a suspicious path component, check if it contains a libjvm.so */ envpath = JLI_StringDup(env); for (path = JLI_StrTok(envpath, ":"); path != NULL; path = JLI_StrTok(NULL, ":")) { if (clientPatternFound && JLI_StrStr(path, clientPattern) != NULL) { if (JvmExists(path)) { JLI_MemFree(envpath); return JNI_TRUE; } } if (serverPatternFound && JLI_StrStr(path, serverPattern) != NULL) { if (JvmExists(path)) { JLI_MemFree(envpath); return JNI_TRUE; } } } JLI_MemFree(envpath); return JNI_FALSE; }
static void WildcardIterator_close(WildcardIterator it) { if (it) { closedir(it->dir); JLI_MemFree(it); } }
/* * Wrapper for platform dependent unsetenv function. */ int UnsetEnv(char *name) { int ret; char *buf = JLI_MemAlloc(strlen(name) + 2); buf = strcat(strcpy(buf, name), "="); ret = _putenv(buf); JLI_MemFree(buf); return (ret); }
/* * 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); }
/* * Checks if release is acceptable by the specification version-string. * Return true if this version-string (as defined in JSR 56) forms * an acceptable match. A version-string is the union (or) of multiple * elements. */ int JLI_AcceptableRelease(const char *release, char *version_string) { char *vs; char *m1; char *end; m1 = vs = JLI_StringDup(version_string); do { if ((end = JLI_StrChr(vs, ' ')) != NULL) *end = '\0'; if (acceptable_element(release, vs)) { JLI_MemFree(m1); return (1); } if (end != NULL) vs = end + 1; } while (end != NULL); JLI_MemFree(m1); return (0); }
/* * Modeled after strcmp(), compare two version-ids for an Exact * Match as defined in JSR 56. */ int JLI_ExactVersionId(const char *id1, char *id2) { char *s1 = JLI_StringDup(id1); char *s2 = JLI_StringDup(id2); char *m1 = s1; char *m2 = s2; char *end1 = NULL; char *end2 = NULL; int res = 0; do { if ((s1 != NULL) && ((end1 = JLI_StrPBrk(s1, separators)) != NULL)) *end1 = '\0'; if ((s2 != NULL) && ((end2 = JLI_StrPBrk(s2, separators)) != NULL)) *end2 = '\0'; if ((s1 != NULL) && (s2 == NULL)) res = comp_string(s1, zero_string); else if ((s1 == NULL) && (s2 != NULL)) res = comp_string(zero_string, s2); else res = comp_string(s1, s2); if (end1 != NULL) s1 = end1 + 1; else s1 = NULL; if (end2 != NULL) s2 = end2 + 1; else s2 = NULL; } while (res == 0 && ((s1 != NULL) || (s2 != NULL))); JLI_MemFree(m1); JLI_MemFree(m2); return (res); }
static WildcardIterator WildcardIterator_for(const char *wildcard) { WildcardIterator it = NEW_(WildcardIterator); HANDLE handle = FindFirstFile(wildcard, &find_data); if (handle == INVALID_HANDLE_VALUE) { JLI_MemFree(it); return NULL; } it->handle = handle; it->firstFile = find_data.cFileName; return it; }
/* * Validates a version string by the extended JSR 56 grammar. */ int JLI_ValidVersionString(char *version_string) { char *vs; char *m1; char *end; if ((version_string == NULL) || (JLI_StrLen(version_string) == 0)) return (0); m1 = vs = JLI_StringDup(version_string); do { if ((end = JLI_StrChr(vs, ' ')) != NULL) *end = '\0'; if (!valid_element(vs)) { JLI_MemFree(m1); return (0); } if (end != NULL) vs = end + 1; } while (end != NULL); JLI_MemFree(m1); return (1); }
/* * Compute the name of the executable * * In order to re-exec securely we need the absolute path of the * executable. On Solaris getexecname(3c) may not return an absolute * path so we use dladdr to get the filename of the executable and * then use realpath to derive an absolute path. From Solaris 9 * onwards the filename returned in DL_info structure from dladdr is * an absolute pathname so technically realpath isn't required. * On Linux we read the executable name from /proc/self/exe. * As a fallback, and for platforms other than Solaris and Linux, * we use FindExecName to compute the executable name. */ static const char* SetExecname(char **argv) { char* exec_path = NULL; #if defined(__solaris__) { Dl_info dlinfo; int (*fptr)(); fptr = (int (*)())dlsym(RTLD_DEFAULT, "main"); if (fptr == NULL) { JLI_ReportErrorMessage(DLL_ERROR3, dlerror()); return JNI_FALSE; } if (dladdr((void*)fptr, &dlinfo)) { char *resolved = (char*)JLI_MemAlloc(PATH_MAX+1); if (resolved != NULL) { exec_path = realpath(dlinfo.dli_fname, resolved); if (exec_path == NULL) { JLI_MemFree(resolved); } } } } #elif defined(__linux__) { const char* self = "/proc/self/exe"; char buf[PATH_MAX+1]; int len = readlink(self, buf, PATH_MAX); if (len >= 0) { buf[len] = '\0'; /* readlink doesn't nul terminate */ exec_path = JLI_StringDup(buf); } } #else /* !__solaris__ && !__linux */ { /* Not implemented */ } #endif if (exec_path == NULL) { exec_path = FindExecName(argv[0]); } execname = exec_path; return exec_path; }
/* * As ReportErrorMessage2 (above) except the system message (if any) * associated with this error is written to a second %s format specifier * in the format argument. */ void ReportSysErrorMessage2(char * format, char * string, jboolean always) { int save_errno = errno; DWORD errval; int freeit = 0; char *errtext = NULL; if ((errval = GetLastError()) != 0) { /* Platform SDK / DOS Error */ int n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM| FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, errval, 0, (LPTSTR)&errtext, 0, NULL); if (errtext == NULL || n == 0) { /* Paranoia check */ errtext = ""; n = 0; } else { freeit = 1; if (n > 2) { /* Drop final CR, LF */ if (errtext[n - 1] == '\n') n--; if (errtext[n - 1] == '\r') n--; errtext[n] = '\0'; } } } else /* C runtime error that has no corresponding DOS error code */ errtext = strerror(save_errno); #ifdef JAVAW { size_t size; char * message; size = strlen(format) + strlen(string) + strlen(errtext); message = (char*)JLI_MemAlloc(size*sizeof(char)); sprintf(message, (const char *)format, string, errtext); if (message != NULL) { MessageBox(NULL, message, "Java Virtual Machine Launcher", (MB_OK|MB_ICONSTOP|MB_APPLMODAL)); JLI_MemFree(message); } } #else if (always) { fprintf(stderr, (const char *)format, string, errtext); fprintf(stderr, "\n"); } #endif if (freeit) (void)LocalFree((HLOCAL)errtext); }
/* * 搜索指定的jre环境 */ char* LocateJRE(manifest_info* info) { char *path; char *home; char *target = NULL; char *dp; char *cp; /* * Start by getting JAVA_VERSION_PATH */ if (info->jre_restrict_search) { path = JLI_StringDup(system_dir); } else if ((path = getenv("JAVA_VERSION_PATH")) != NULL) { path = JLI_StringDup(path); } else { if ((home = getenv("HOME")) != NULL) { path = (char *)JLI_MemAlloc(JLI_StrLen(home) + \ JLI_StrLen(system_dir) + JLI_StrLen(user_dir) + 2); sprintf(path, "%s%s:%s", home, user_dir, system_dir); } else { path = JLI_StringDup(system_dir); } } /* * Step through each directory on the path. Terminate the scan with * the first directory with an acceptable JRE. */ cp = dp = path; while (dp != NULL) { cp = JLI_StrChr(dp, (int)':'); if (cp != NULL) *cp = '\0'; if ((target = ProcessDir(info, dp)) != NULL) break; dp = cp; if (dp != NULL) dp++; } JLI_MemFree(path); return (target); }
/* * Find a path for the executable */ char * FindExecName(char *program) { char cwdbuf[PATH_MAX+2]; char *path; char *tmp_path; char *f; char *result = NULL; /* absolute path? */ if (*program == FILE_SEPARATOR || (FILE_SEPARATOR=='\\' && JLI_StrRChr(program, ':'))) return Resolve("", program+1); /* relative path? */ if (JLI_StrRChr(program, FILE_SEPARATOR) != 0) { char buf[PATH_MAX+2]; return Resolve(getcwd(cwdbuf, sizeof(cwdbuf)), program); } /* from search path? */ path = getenv("PATH"); if (!path || !*path) path = "."; tmp_path = JLI_MemAlloc(JLI_StrLen(path) + 2); JLI_StrCpy(tmp_path, path); for (f=tmp_path; *f && result==0; ) { char *s = f; while (*f && (*f != PATH_SEPARATOR)) ++f; if (*f) *f++ = 0; if (*s == FILE_SEPARATOR) result = Resolve(s, program); else { /* relative path element */ char dir[2*PATH_MAX]; JLI_Snprintf(dir, sizeof(dir), "%s%c%s", getcwd(cwdbuf, sizeof(cwdbuf)), FILE_SEPARATOR, s); result = Resolve(dir, program); } if (result != 0) break; } JLI_MemFree(tmp_path); return result; }
static WildcardIterator WildcardIterator_for(const char *wildcard) { DIR *dir; int wildlen = JLI_StrLen(wildcard); if (wildlen < 2) { dir = opendir("."); } else { char *dirname = JLI_StringDup(wildcard); dirname[wildlen - 1] = '\0'; dir = opendir(dirname); JLI_MemFree(dirname); } if (dir == NULL) return NULL; else { WildcardIterator it = NEW_(WildcardIterator); it->dir = dir; return it; } }
void JLI_ReportErrorMessage(const char* fmt, ...) { va_list vl; va_start(vl,fmt); if (IsJavaw()) { char *message; /* get the length of the string we need */ int n = _vscprintf(fmt, vl); message = (char *)JLI_MemAlloc(n + 1); _vsnprintf(message, n, fmt, vl); message[n]='\0'; MessageBox(NULL, message, "Java Virtual Machine Launcher", (MB_OK|MB_ICONSTOP|MB_APPLMODAL)); JLI_MemFree(message); } else { vfprintf(stderr, fmt, vl); fprintf(stderr, "\n"); } va_end(vl); }
static void FileList_expandWildcards(FileList fl) { int i, j; for (i = 0; i < fl->size; i++) { if (isWildcard(fl->files[i])) { FileList expanded = wildcardFileList(fl->files[i]); if (expanded != NULL && expanded->size > 0) { JLI_MemFree(fl->files[i]); FileList_ensureCapacity(fl, fl->size + expanded->size); for (j = fl->size - 1; j >= i+1; j--) fl->files[j+expanded->size-1] = fl->files[j]; for (j = 0; j < expanded->size; j++) fl->files[i+j] = expanded->files[j]; i += expanded->size - 1; fl->size += expanded->size - 1; /* fl expropriates expanded's elements. */ expanded->size = 0; } FileList_free(expanded); } } }
void ReportErrorMessage2(char * format, char * string, jboolean always) { /* * The format argument must be a printf format string with one %s * argument, which is passed the string argument. */ #ifdef JAVAW size_t size; char * message; size = strlen(format) + strlen(string); message = (char*)JLI_MemAlloc(size*sizeof(char)); sprintf(message, (const char *)format, string); if (message != NULL) { MessageBox(NULL, message, "Java Virtual Machine Launcher", (MB_OK|MB_ICONSTOP|MB_APPLMODAL)); JLI_MemFree(message); } #else if (always) { fprintf(stderr, (const char *)format, string); fprintf(stderr, "\n"); } #endif }
/* * At this point we have the arguments to the application, and we need to * check with original stdargs in order to compare which of these truly * needs expansion. cmdtoargs will specify this if it finds a bare * (unquoted) argument containing a glob character(s) ie. * or ? */ jobjectArray CreateApplicationArgs(JNIEnv *env, char **strv, int argc) { int i, j, idx, tlen; jobjectArray outArray, inArray; char *ostart, *astart, **nargv; jboolean needs_expansion = JNI_FALSE; jmethodID mid; int stdargc; StdArg *stdargs; jclass cls = GetLauncherHelperClass(env); NULL_CHECK0(cls); if (argc == 0) { return NewPlatformStringArray(env, strv, argc); } // the holy grail we need to compare with. stdargs = JLI_GetStdArgs(); stdargc = JLI_GetStdArgc(); // sanity check, this should never happen if (argc > stdargc) { JLI_TraceLauncher("Warning: app args is larger than the original, %d %d\n", argc, stdargc); JLI_TraceLauncher("passing arguments as-is.\n"); return NewPlatformStringArray(env, strv, argc); } // sanity check, match the args we have, to the holy grail idx = stdargc - argc; ostart = stdargs[idx].arg; astart = strv[0]; // sanity check, ensure that the first argument of the arrays are the same if (JLI_StrCmp(ostart, astart) != 0) { // some thing is amiss the args don't match JLI_TraceLauncher("Warning: app args parsing error\n"); JLI_TraceLauncher("passing arguments as-is\n"); return NewPlatformStringArray(env, strv, argc); } // make a copy of the args which will be expanded in java if required. nargv = (char **)JLI_MemAlloc(argc * sizeof(char*)); for (i = 0, j = idx; i < argc; i++, j++) { jboolean arg_expand = (JLI_StrCmp(stdargs[j].arg, strv[i]) == 0) ? stdargs[j].has_wildcard : JNI_FALSE; if (needs_expansion == JNI_FALSE) needs_expansion = arg_expand; // indicator char + String + NULL terminator, the java method will strip // out the first character, the indicator character, so no matter what // we add the indicator tlen = 1 + JLI_StrLen(strv[i]) + 1; nargv[i] = (char *) JLI_MemAlloc(tlen); if (JLI_Snprintf(nargv[i], tlen, "%c%s", arg_expand ? 'T' : 'F', strv[i]) < 0) { return NULL; } JLI_TraceLauncher("%s\n", nargv[i]); } if (!needs_expansion) { // clean up any allocated memory and return back the old arguments for (i = 0 ; i < argc ; i++) { JLI_MemFree(nargv[i]); } JLI_MemFree(nargv); return NewPlatformStringArray(env, strv, argc); } NULL_CHECK0(mid = (*env)->GetStaticMethodID(env, cls, "expandArgs", "([Ljava/lang/String;)[Ljava/lang/String;")); // expand the arguments that require expansion, the java method will strip // out the indicator character. inArray = NewPlatformStringArray(env, nargv, argc); outArray = (*env)->CallStaticObjectMethod(env, cls, mid, inArray); for (i = 0; i < argc; i++) { JLI_MemFree(nargv[i]); } JLI_MemFree(nargv); return outArray; }
void CreateExecutionEnvironment(int *pargc, char ***pargv, char jrepath[], jint so_jrepath, char jvmpath[], jint so_jvmpath, char jvmcfg[], jint so_jvmcfg) { /* * First, determine if we are running the desired data model. If we * are running the desired data model, all the error messages * associated with calling GetJREPath, ReadKnownVMs, etc. should be * output, otherwise we simply exit with an error, as we no longer * support dual data models. */ jboolean jvmpathExists; /* Compute/set the name of the executable */ SetExecname(*pargv); /* Check data model flags, and exec process, if needed */ { char *arch = LIBARCHNAME; /* like sparc or sparcv9 */ char * jvmtype = NULL; int argc = *pargc; char **argv = *pargv; int running = CURRENT_DATA_MODEL; /* * As of jdk9, there is no support for dual mode operations, however * for legacy error reporting purposes and until -d options are supported * we need this. */ int wanted = running; #ifdef SETENV_REQUIRED jboolean mustsetenv = JNI_FALSE; char *runpath = NULL; /* existing effective LD_LIBRARY_PATH setting */ char* new_runpath = NULL; /* desired new LD_LIBRARY_PATH string */ char* newpath = NULL; /* path on new LD_LIBRARY_PATH */ char* lastslash = NULL; char** newenvp = NULL; /* current environment */ size_t new_runpath_size; #ifdef __solaris__ char* dmpath = NULL; /* data model specific LD_LIBRARY_PATH, Solaris only */ #endif /* __solaris__ */ #endif /* SETENV_REQUIRED */ char** newargv = NULL; int newargc = 0; /* * Starting in 1.5, all unix platforms accept the -d32 and -d64 * options. On platforms where only one data-model is supported * (e.g. ia-64 Linux), using the flag for the other data model is * an error and will terminate the program. */ { /* open new scope to declare local variables */ int i; newargv = (char **)JLI_MemAlloc((argc+1) * sizeof(char*)); newargv[newargc++] = argv[0]; /* scan for data model arguments and remove from argument list; last occurrence determines desired data model */ for (i=1; i < argc; i++) { if (JLI_StrCmp(argv[i], "-J-d64") == 0 || JLI_StrCmp(argv[i], "-d64") == 0) { wanted = 64; continue; } if (JLI_StrCmp(argv[i], "-J-d32") == 0 || JLI_StrCmp(argv[i], "-d32") == 0) { wanted = 32; continue; } newargv[newargc++] = argv[i]; if (IsJavaArgs()) { if (argv[i][0] != '-') continue; } else { if (JLI_StrCmp(argv[i], "-classpath") == 0 || JLI_StrCmp(argv[i], "-cp") == 0) { i++; if (i >= argc) break; newargv[newargc++] = argv[i]; continue; } if (argv[i][0] != '-') { i++; break; } } } /* copy rest of args [i .. argc) */ while (i < argc) { newargv[newargc++] = argv[i++]; } newargv[newargc] = NULL; /* * newargv has all proper arguments here */ argc = newargc; argv = newargv; } /* If the data model is not changing, it is an error if the jvmpath does not exist */ if (wanted == running) { /* Find out where the JRE is that we will be using. */ if (!GetJREPath(jrepath, so_jrepath, arch, JNI_FALSE) ) { JLI_ReportErrorMessage(JRE_ERROR1); exit(2); } JLI_Snprintf(jvmcfg, so_jvmcfg, "%s%slib%s%s%sjvm.cfg", jrepath, FILESEP, FILESEP, arch, FILESEP); /* Find the specified JVM type */ if (ReadKnownVMs(jvmcfg, JNI_FALSE) < 1) { JLI_ReportErrorMessage(CFG_ERROR7); exit(1); } jvmpath[0] = '\0'; jvmtype = CheckJvmType(pargc, pargv, JNI_FALSE); if (JLI_StrCmp(jvmtype, "ERROR") == 0) { JLI_ReportErrorMessage(CFG_ERROR9); exit(4); } if (!GetJVMPath(jrepath, jvmtype, jvmpath, so_jvmpath, arch, 0 )) { JLI_ReportErrorMessage(CFG_ERROR8, jvmtype, jvmpath); exit(4); } /* * we seem to have everything we need, so without further ado * we return back, otherwise proceed to set the environment. */ #ifdef SETENV_REQUIRED mustsetenv = RequiresSetenv(jvmpath); JLI_TraceLauncher("mustsetenv: %s\n", mustsetenv ? "TRUE" : "FALSE"); if (mustsetenv == JNI_FALSE) { JLI_MemFree(newargv); return; } #else JLI_MemFree(newargv); return; #endif /* SETENV_REQUIRED */ } else { /* do the same speculatively or exit */ JLI_ReportErrorMessage(JRE_ERROR2, wanted); exit(1); } #ifdef SETENV_REQUIRED if (mustsetenv) { /* * We will set the LD_LIBRARY_PATH as follows: * * o $JVMPATH (directory portion only) * o $JRE/lib/$LIBARCHNAME * o $JRE/../lib/$LIBARCHNAME * * followed by the user's previous effective LD_LIBRARY_PATH, if * any. */ #ifdef __solaris__ /* * Starting in Solaris 7, ld.so.1 supports three LD_LIBRARY_PATH * variables: * * 1. LD_LIBRARY_PATH -- used for 32 and 64 bit searches if * data-model specific variables are not set. * * 2. LD_LIBRARY_PATH_64 -- overrides and replaces LD_LIBRARY_PATH * for 64-bit binaries. * The vm uses LD_LIBRARY_PATH to set the java.library.path system * property. To shield the vm from the complication of multiple * LD_LIBRARY_PATH variables, if the appropriate data model * specific variable is set, we will act as if LD_LIBRARY_PATH had * the value of the data model specific variant and the data model * specific variant will be unset. Note that the variable for the * *wanted* data model must be used (if it is set), not simply the * current running data model. */ switch (wanted) { case 0: case 64: dmpath = getenv("LD_LIBRARY_PATH_64"); wanted = 64; break; default: JLI_ReportErrorMessage(JRE_ERROR3, __LINE__); exit(1); /* unknown value in wanted */ break; } /* * If dmpath is NULL, the relevant data model specific variable is * not set and normal LD_LIBRARY_PATH should be used. */ if (dmpath == NULL) { runpath = getenv("LD_LIBRARY_PATH"); } else { runpath = dmpath; } #else /* ! __solaris__ */ /* * If not on Solaris, assume only a single LD_LIBRARY_PATH * variable. */ runpath = getenv(LD_LIBRARY_PATH); #endif /* __solaris__ */ /* runpath contains current effective LD_LIBRARY_PATH setting */ jvmpath = JLI_StringDup(jvmpath); new_runpath_size = ((runpath != NULL) ? JLI_StrLen(runpath) : 0) + 2 * JLI_StrLen(jrepath) + 2 * JLI_StrLen(arch) + #ifdef AIX /* On AIX we additionally need 'jli' in the path because ld doesn't support $ORIGIN. */ JLI_StrLen(jrepath) + JLI_StrLen(arch) + JLI_StrLen("/lib//jli:") + #endif JLI_StrLen(jvmpath) + 52; new_runpath = JLI_MemAlloc(new_runpath_size); newpath = new_runpath + JLI_StrLen(LD_LIBRARY_PATH "="); /* * Create desired LD_LIBRARY_PATH value for target data model. */ { /* remove the name of the .so from the JVM path */ lastslash = JLI_StrRChr(jvmpath, '/'); if (lastslash) *lastslash = '\0'; sprintf(new_runpath, LD_LIBRARY_PATH "=" "%s:" "%s/lib/%s:" #ifdef AIX "%s/lib/%s/jli:" /* Needed on AIX because ld doesn't support $ORIGIN. */ #endif "%s/../lib/%s", jvmpath, jrepath, arch, #ifdef AIX jrepath, arch, #endif jrepath, arch ); /* * Check to make sure that the prefix of the current path is the * desired environment variable setting, though the RequiresSetenv * checks if the desired runpath exists, this logic does a more * comprehensive check. */ if (runpath != NULL && JLI_StrNCmp(newpath, runpath, JLI_StrLen(newpath)) == 0 && (runpath[JLI_StrLen(newpath)] == 0 || runpath[JLI_StrLen(newpath)] == ':') && (running == wanted) /* data model does not have to be changed */ #ifdef __solaris__ && (dmpath == NULL) /* data model specific variables not set */ #endif /* __solaris__ */ ) { JLI_MemFree(newargv); JLI_MemFree(new_runpath); return; } } /* * Place the desired environment setting onto the prefix of * LD_LIBRARY_PATH. Note that this prevents any possible infinite * loop of execv() because we test for the prefix, above. */ if (runpath != 0) { /* ensure storage for runpath + colon + NULL */ if ((JLI_StrLen(runpath) + 1 + 1) > new_runpath_size) { JLI_ReportErrorMessageSys(JRE_ERROR11); exit(1); } JLI_StrCat(new_runpath, ":"); JLI_StrCat(new_runpath, runpath); } if (putenv(new_runpath) != 0) { exit(1); /* problem allocating memory; LD_LIBRARY_PATH not set properly */ } /* * Unix systems document that they look at LD_LIBRARY_PATH only * once at startup, so we have to re-exec the current executable * to get the changed environment variable to have an effect. */ #ifdef __solaris__ /* * If dmpath is not NULL, remove the data model specific string * in the environment for the exec'ed child. */ if (dmpath != NULL) (void)UnsetEnv("LD_LIBRARY_PATH_64"); #endif /* __solaris */ newenvp = environ; } #endif /* SETENV_REQUIRED */ { char *newexec = execname; JLI_TraceLauncher("TRACER_MARKER:About to EXEC\n"); (void) fflush(stdout); (void) fflush(stderr); #ifdef SETENV_REQUIRED if (mustsetenv) { execve(newexec, argv, newenvp); } else { execv(newexec, argv); } #else /* !SETENV_REQUIRED */ execv(newexec, argv); #endif /* SETENV_REQUIRED */ JLI_ReportErrorMessageSys(JRE_ERROR4, newexec); } exit(1); } }
/* * At this point we have the arguments to the application, and we need to * check with original stdargs in order to compare which of these truly * needs expansion. cmdtoargs will specify this if it finds a bare * (unquoted) argument containing a glob character(s) ie. * or ? */ jobjectArray CreateApplicationArgs(JNIEnv *env, char **strv, int argc) { int i, j, idx; size_t tlen; jobjectArray outArray, inArray; char *arg, **nargv; jboolean needs_expansion = JNI_FALSE; jmethodID mid; int stdargc; StdArg *stdargs; int *appArgIdx; int isTool; jclass cls = GetLauncherHelperClass(env); NULL_CHECK0(cls); if (argc == 0) { return NewPlatformStringArray(env, strv, argc); } // the holy grail we need to compare with. stdargs = JLI_GetStdArgs(); stdargc = JLI_GetStdArgc(); // sanity check, this should never happen if (argc > stdargc) { JLI_TraceLauncher("Warning: app args is larger than the original, %d %d\n", argc, stdargc); JLI_TraceLauncher("passing arguments as-is.\n"); return NewPlatformStringArray(env, strv, argc); } // sanity check, match the args we have, to the holy grail idx = JLI_GetAppArgIndex(); isTool = (idx == 0); if (isTool) { idx++; } // skip tool name JLI_TraceLauncher("AppArgIndex: %d points to %s\n", idx, stdargs[idx].arg); appArgIdx = calloc(argc, sizeof(int)); for (i = idx, j = 0; i < stdargc; i++) { if (isTool) { // filter -J used by tools to pass JVM options arg = stdargs[i].arg; if (arg[0] == '-' && arg[1] == 'J') { continue; } } appArgIdx[j++] = i; } // sanity check, ensure same number of arguments for application if (j != argc) { JLI_TraceLauncher("Warning: app args count doesn't match, %d %d\n", j, argc); JLI_TraceLauncher("passing arguments as-is.\n"); JLI_MemFree(appArgIdx); return NewPlatformStringArray(env, strv, argc); } // make a copy of the args which will be expanded in java if required. nargv = (char **)JLI_MemAlloc(argc * sizeof(char*)); for (i = 0; i < argc; i++) { jboolean arg_expand; j = appArgIdx[i]; arg_expand = (JLI_StrCmp(stdargs[j].arg, strv[i]) == 0) ? stdargs[j].has_wildcard : JNI_FALSE; if (needs_expansion == JNI_FALSE) needs_expansion = arg_expand; // indicator char + String + NULL terminator, the java method will strip // out the first character, the indicator character, so no matter what // we add the indicator tlen = 1 + JLI_StrLen(strv[i]) + 1; nargv[i] = (char *) JLI_MemAlloc(tlen); if (JLI_Snprintf(nargv[i], tlen, "%c%s", arg_expand ? 'T' : 'F', strv[i]) < 0) { return NULL; } JLI_TraceLauncher("%s\n", nargv[i]); } if (!needs_expansion) { // clean up any allocated memory and return back the old arguments for (i = 0 ; i < argc ; i++) { JLI_MemFree(nargv[i]); } JLI_MemFree(nargv); JLI_MemFree(appArgIdx); return NewPlatformStringArray(env, strv, argc); } NULL_CHECK0(mid = (*env)->GetStaticMethodID(env, cls, "expandArgs", "([Ljava/lang/String;)[Ljava/lang/String;")); // expand the arguments that require expansion, the java method will strip // out the indicator character. NULL_CHECK0(inArray = NewPlatformStringArray(env, nargv, argc)); outArray = (*env)->CallStaticObjectMethod(env, cls, mid, inArray); for (i = 0; i < argc; i++) { JLI_MemFree(nargv[i]); } JLI_MemFree(nargv); JLI_MemFree(appArgIdx); return outArray; }
/* * Just like JLI_ReportErrorMessage, except that it concatenates the system * error message if any, its upto the calling routine to correctly * format the separation of the messages. */ void JLI_ReportErrorMessageSys(const char *fmt, ...) { va_list vl; int save_errno = errno; DWORD errval; jboolean freeit = JNI_FALSE; char *errtext = NULL; va_start(vl, fmt); if ((errval = GetLastError()) != 0) { /* Platform SDK / DOS Error */ int n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM| FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, errval, 0, (LPTSTR)&errtext, 0, NULL); if (errtext == NULL || n == 0) { /* Paranoia check */ errtext = ""; n = 0; } else { freeit = JNI_TRUE; if (n > 2) { /* Drop final CR, LF */ if (errtext[n - 1] == '\n') n--; if (errtext[n - 1] == '\r') n--; errtext[n] = '\0'; } } } else { /* C runtime error that has no corresponding DOS error code */ errtext = strerror(save_errno); } if (IsJavaw()) { char *message; int mlen; /* get the length of the string we need */ int len = mlen = _vscprintf(fmt, vl) + 1; if (freeit) { mlen += (int)JLI_StrLen(errtext); } message = (char *)JLI_MemAlloc(mlen); _vsnprintf(message, len, fmt, vl); message[len]='\0'; if (freeit) { JLI_StrCat(message, errtext); } MessageBox(NULL, message, "Java Virtual Machine Launcher", (MB_OK|MB_ICONSTOP|MB_APPLMODAL)); JLI_MemFree(message); } else { vfprintf(stderr, fmt, vl); if (freeit) { fprintf(stderr, "%s", errtext); } } if (freeit) { (void)LocalFree((HLOCAL)errtext); } va_end(vl); }
/* * 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 *progname; char path[MAXPATHLEN + 1]; /* * Determine the executable we are building (or in the rare case, running). */ #ifdef JAVA_ARGS /* javac, jar and friends. */ progname = "java"; #else /* java, oldjava, javaw and friends */ #ifdef PROGNAME progname = PROGNAME; #else { char *s; progname = *argv; if ((s = strrchr(progname, FILE_SEPARATOR)) != 0) { progname = s + 1; } } #endif /* PROGNAME */ #endif /* JAVA_ARGS */ /* * Resolve the real path to the currently running launcher. */ len = GetModuleFileName(NULL, path, MAXPATHLEN + 1); if (len == 0 || len > MAXPATHLEN) { ReportSysErrorMessage2( "Unable to resolve path to current %s executable: %s", progname, JNI_TRUE); exit(1); } if (_launcher_debug) { printf("ExecJRE: old: %s\n", path); printf("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. (strnicmp() is the Windows equiv. of strncasecmp().) */ if (strnicmp(jre, path, strlen(jre)) == 0) return; /* I am the droid you were looking for */ /* * If this isn't the selected version, exec the selected version. */ (void)strcat(strcat(strcpy(path, jre), "\\bin\\"), progname); (void)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); * ReportErrorMessage2("Exec of %s failed\n", path, JNI_TRUE); * 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(strlen(path) + strlen(np) + 4); if (strchr(path, (int)' ') == NULL && strchr(path, (int)'\t') == NULL) cmdline = strcpy(cmdline, path); else cmdline = strcat(strcat(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 (strcmp(unquoted, "-classpath") == 0 || strcmp(unquoted, "-cp") == 0) { /* Unique cp syntax */ cmdline = strcat(strcat(cmdline, " "), p); p = nextarg(&np); if (*p != (char)0) /* If a token was isolated */ cmdline = strcat(strcat(cmdline, " "), p); } else if (strncmp(unquoted, "-version:", 9) != 0 && strcmp(unquoted, "-jre-restrict-search") != 0 && strcmp(unquoted, "-no-jre-restrict-search") != 0) { cmdline = strcat(strcat(cmdline, " "), p); } } else { /* End of options */ cmdline = strcat(strcat(cmdline, " "), p); cmdline = strcat(strcat(cmdline, " "), np); JLI_MemFree((void *)unquoted); break; } JLI_MemFree((void *)unquoted); } } JLI_MemFree((void *)ccl); if (_launcher_debug) { 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 */ ReportSysErrorMessage2("CreateProcess(%s, ...) failed: %s", path, JNI_TRUE); exit(1); } if (WaitForSingleObject(pi.hProcess, INFINITE) != WAIT_FAILED) { if (GetExitCodeProcess(pi.hProcess, &exitCode) == FALSE) exitCode = 1; } else { ReportErrorMessage("WaitForSingleObject() failed.", JNI_TRUE); exitCode = 1; } CloseHandle(pi.hThread); CloseHandle(pi.hProcess); exit(exitCode); } }