4680N/A/*
4680N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
4680N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4680N/A *
4680N/A * This code is free software; you can redistribute it and/or modify it
4680N/A * under the terms of the GNU General Public License version 2 only, as
4680N/A * published by the Free Software Foundation. Oracle designates this
4680N/A * particular file as subject to the "Classpath" exception as provided
4680N/A * by Oracle in the LICENSE file that accompanied this code.
4680N/A *
4680N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4680N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4680N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4680N/A * version 2 for more details (a copy is included in the LICENSE file that
4680N/A * accompanied this code).
4680N/A *
4680N/A * You should have received a copy of the GNU General Public License version
4680N/A * 2 along with this work; if not, write to the Free Software Foundation,
4680N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4680N/A *
4680N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4680N/A * or visit www.oracle.com if you need additional information or have any
4680N/A * questions.
4680N/A */
4680N/A#include "java.h"
4680N/A
4680N/A/*
4680N/A * If app is "/foo/bin/javac", or "/foo/bin/sparcv9/javac" then put
4680N/A * "/foo" into buf.
4680N/A */
4680N/Ajboolean
4680N/AGetApplicationHome(char *buf, jint bufsize)
4680N/A{
4680N/A const char *execname = GetExecName();
4680N/A if (execname != NULL) {
4680N/A JLI_Snprintf(buf, bufsize, "%s", execname);
4680N/A buf[bufsize-1] = '\0';
4680N/A } else {
4680N/A return JNI_FALSE;
4680N/A }
4680N/A
4680N/A if (JLI_StrRChr(buf, '/') == 0) {
4680N/A buf[0] = '\0';
4680N/A return JNI_FALSE;
4680N/A }
4680N/A *(JLI_StrRChr(buf, '/')) = '\0'; /* executable file */
4680N/A if (JLI_StrLen(buf) < 4 || JLI_StrRChr(buf, '/') == 0) {
4680N/A buf[0] = '\0';
4680N/A return JNI_FALSE;
4680N/A }
4680N/A if (JLI_StrCmp("/bin", buf + JLI_StrLen(buf) - 4) != 0)
4680N/A *(JLI_StrRChr(buf, '/')) = '\0'; /* sparcv9 or amd64 */
4680N/A if (JLI_StrLen(buf) < 4 || JLI_StrCmp("/bin", buf + JLI_StrLen(buf) - 4) != 0) {
4680N/A buf[0] = '\0';
4680N/A return JNI_FALSE;
4680N/A }
4680N/A *(JLI_StrRChr(buf, '/')) = '\0'; /* bin */
4680N/A
4680N/A return JNI_TRUE;
4680N/A}
4680N/A/*
4680N/A * Return true if the named program exists
4680N/A */
4680N/Astatic int
4680N/AProgramExists(char *name)
4680N/A{
4680N/A struct stat sb;
4680N/A if (stat(name, &sb) != 0) return 0;
4680N/A if (S_ISDIR(sb.st_mode)) return 0;
4680N/A return (sb.st_mode & S_IEXEC) != 0;
4680N/A}
4680N/A
4680N/A/*
4680N/A * Find a command in a directory, returning the path.
4680N/A */
4680N/Astatic char *
4680N/AResolve(char *indir, char *cmd)
4680N/A{
4680N/A char name[PATH_MAX + 2], *real;
4680N/A
4680N/A if ((JLI_StrLen(indir) + JLI_StrLen(cmd) + 1) > PATH_MAX) return 0;
4680N/A JLI_Snprintf(name, sizeof(name), "%s%c%s", indir, FILE_SEPARATOR, cmd);
4680N/A if (!ProgramExists(name)) return 0;
4680N/A real = JLI_MemAlloc(PATH_MAX + 2);
4680N/A if (!realpath(name, real))
4680N/A JLI_StrCpy(real, name);
4680N/A return real;
4680N/A}
4680N/A
4680N/A/*
4680N/A * Find a path for the executable
4680N/A */
4680N/Achar *
4680N/AFindExecName(char *program)
4680N/A{
4680N/A char cwdbuf[PATH_MAX+2];
4680N/A char *path;
4680N/A char *tmp_path;
4680N/A char *f;
4680N/A char *result = NULL;
4680N/A
4680N/A /* absolute path? */
4680N/A if (*program == FILE_SEPARATOR ||
4680N/A (FILE_SEPARATOR=='\\' && JLI_StrRChr(program, ':')))
4680N/A return Resolve("", program+1);
4680N/A
4680N/A /* relative path? */
4680N/A if (JLI_StrRChr(program, FILE_SEPARATOR) != 0) {
4680N/A char buf[PATH_MAX+2];
4680N/A return Resolve(getcwd(cwdbuf, sizeof(cwdbuf)), program);
4680N/A }
4680N/A
4680N/A /* from search path? */
4680N/A path = getenv("PATH");
4680N/A if (!path || !*path) path = ".";
4680N/A tmp_path = JLI_MemAlloc(JLI_StrLen(path) + 2);
4680N/A JLI_StrCpy(tmp_path, path);
4680N/A
4680N/A for (f=tmp_path; *f && result==0; ) {
4680N/A char *s = f;
4680N/A while (*f && (*f != PATH_SEPARATOR)) ++f;
4680N/A if (*f) *f++ = 0;
4680N/A if (*s == FILE_SEPARATOR)
4680N/A result = Resolve(s, program);
4680N/A else {
4680N/A /* relative path element */
4680N/A char dir[2*PATH_MAX];
4680N/A JLI_Snprintf(dir, sizeof(dir), "%s%c%s", getcwd(cwdbuf, sizeof(cwdbuf)),
4680N/A FILE_SEPARATOR, s);
4680N/A result = Resolve(dir, program);
4680N/A }
4680N/A if (result != 0) break;
4680N/A }
4680N/A
4680N/A JLI_MemFree(tmp_path);
4680N/A return result;
4680N/A}
4680N/A
4680N/Avoid JLI_ReportErrorMessage(const char* fmt, ...) {
4680N/A va_list vl;
4680N/A va_start(vl, fmt);
4680N/A vfprintf(stderr, fmt, vl);
4680N/A fprintf(stderr, "\n");
4680N/A va_end(vl);
4680N/A}
4680N/A
4680N/Avoid JLI_ReportErrorMessageSys(const char* fmt, ...) {
4680N/A va_list vl;
4680N/A char *emsg;
4680N/A
4680N/A /*
4680N/A * TODO: its safer to use strerror_r but is not available on
4680N/A * Solaris 8. Until then....
4680N/A */
4680N/A emsg = strerror(errno);
4680N/A if (emsg != NULL) {
4680N/A fprintf(stderr, "%s\n", emsg);
4680N/A }
4680N/A
4680N/A va_start(vl, fmt);
4680N/A vfprintf(stderr, fmt, vl);
4680N/A fprintf(stderr, "\n");
4680N/A va_end(vl);
4680N/A}
4680N/A
4680N/Avoid JLI_ReportExceptionDescription(JNIEnv * env) {
4680N/A (*env)->ExceptionDescribe(env);
4680N/A}
4680N/A
4680N/A/*
4680N/A * Since using the file system as a registry is a bit risky, perform
4680N/A * additional sanity checks on the identified directory to validate
4680N/A * it as a valid jre/sdk.
4680N/A *
4680N/A * Return 0 if the tests fail; otherwise return non-zero (true).
4680N/A *
4680N/A * Note that checking for anything more than the existence of an
4680N/A * executable object at bin/java relative to the path being checked
4680N/A * will break the regression tests.
4680N/A */
4680N/Astatic int
4680N/ACheckSanity(char *path, char *dir)
4680N/A{
4680N/A char buffer[PATH_MAX];
4680N/A
4680N/A if (JLI_StrLen(path) + JLI_StrLen(dir) + 11 > PATH_MAX)
4680N/A return (0); /* Silently reject "impossibly" long paths */
4680N/A
4680N/A JLI_Snprintf(buffer, sizeof(buffer), "%s/%s/bin/java", path, dir);
4680N/A return ((access(buffer, X_OK) == 0) ? 1 : 0);
4680N/A}
4680N/A
4680N/A/*
4680N/A * Determine if there is an acceptable JRE in the directory dirname.
4680N/A * Upon locating the "best" one, return a fully qualified path to
4680N/A * it. "Best" is defined as the most advanced JRE meeting the
4680N/A * constraints contained in the manifest_info. If no JRE in this
4680N/A * directory meets the constraints, return NULL.
4680N/A *
4680N/A * Note that we don't check for errors in reading the directory
4680N/A * (which would be done by checking errno). This is because it
4680N/A * doesn't matter if we get an error reading the directory, or
4680N/A * we just don't find anything interesting in the directory. We
4680N/A * just return NULL in either case.
4680N/A *
4680N/A * The historical names of j2sdk and j2re were changed to jdk and
4680N/A * jre respecively as part of the 1.5 rebranding effort. Since the
4680N/A * former names are legacy on Linux, they must be recognized for
4680N/A * all time. Fortunately, this is a minor cost.
4680N/A */
4680N/Astatic char
4680N/A*ProcessDir(manifest_info *info, char *dirname)
4680N/A{
4680N/A DIR *dirp;
4680N/A struct dirent *dp;
4680N/A char *best = NULL;
4680N/A int offset;
4680N/A int best_offset = 0;
4680N/A char *ret_str = NULL;
4680N/A char buffer[PATH_MAX];
4680N/A
4680N/A if ((dirp = opendir(dirname)) == NULL)
4680N/A return (NULL);
4680N/A
4680N/A do {
4680N/A if ((dp = readdir(dirp)) != NULL) {
4680N/A offset = 0;
4680N/A if ((JLI_StrNCmp(dp->d_name, "jre", 3) == 0) ||
4680N/A (JLI_StrNCmp(dp->d_name, "jdk", 3) == 0))
4680N/A offset = 3;
4680N/A else if (JLI_StrNCmp(dp->d_name, "j2re", 4) == 0)
4680N/A offset = 4;
4680N/A else if (JLI_StrNCmp(dp->d_name, "j2sdk", 5) == 0)
4680N/A offset = 5;
4680N/A if (offset > 0) {
4680N/A if ((JLI_AcceptableRelease(dp->d_name + offset,
4680N/A info->jre_version)) && CheckSanity(dirname, dp->d_name))
4680N/A if ((best == NULL) || (JLI_ExactVersionId(
4680N/A dp->d_name + offset, best + best_offset) > 0)) {
4680N/A if (best != NULL)
4680N/A JLI_MemFree(best);
4680N/A best = JLI_StringDup(dp->d_name);
4680N/A best_offset = offset;
4680N/A }
4680N/A }
4680N/A }
4680N/A } while (dp != NULL);
4680N/A (void) closedir(dirp);
4680N/A if (best == NULL)
4680N/A return (NULL);
4680N/A else {
4680N/A ret_str = JLI_MemAlloc(JLI_StrLen(dirname) + JLI_StrLen(best) + 2);
4680N/A sprintf(ret_str, "%s/%s", dirname, best);
4680N/A JLI_MemFree(best);
4680N/A return (ret_str);
4680N/A }
4680N/A}
4680N/A
4680N/A/*
4680N/A * This is the global entry point. It examines the host for the optimal
4680N/A * JRE to be used by scanning a set of directories. The set of directories
4680N/A * is platform dependent and can be overridden by the environment
4680N/A * variable JAVA_VERSION_PATH.
4680N/A *
4680N/A * This routine itself simply determines the set of appropriate
4680N/A * directories before passing control onto ProcessDir().
4680N/A */
4680N/Achar*
4680N/ALocateJRE(manifest_info* info)
4680N/A{
4680N/A char *path;
4680N/A char *home;
4680N/A char *target = NULL;
4680N/A char *dp;
4680N/A char *cp;
4680N/A
4680N/A /*
4680N/A * Start by getting JAVA_VERSION_PATH
4680N/A */
4680N/A if (info->jre_restrict_search) {
4680N/A path = JLI_StringDup(system_dir);
4680N/A } else if ((path = getenv("JAVA_VERSION_PATH")) != NULL) {
4680N/A path = JLI_StringDup(path);
4680N/A } else {
4680N/A if ((home = getenv("HOME")) != NULL) {
4680N/A path = (char *)JLI_MemAlloc(JLI_StrLen(home) + \
4680N/A JLI_StrLen(system_dir) + JLI_StrLen(user_dir) + 2);
4680N/A sprintf(path, "%s%s:%s", home, user_dir, system_dir);
4680N/A } else {
4680N/A path = JLI_StringDup(system_dir);
4680N/A }
4680N/A }
4680N/A
4680N/A /*
4680N/A * Step through each directory on the path. Terminate the scan with
4680N/A * the first directory with an acceptable JRE.
4680N/A */
4680N/A cp = dp = path;
4680N/A while (dp != NULL) {
4680N/A cp = JLI_StrChr(dp, (int)':');
4680N/A if (cp != NULL)
4680N/A *cp = '\0';
4680N/A if ((target = ProcessDir(info, dp)) != NULL)
4680N/A break;
4680N/A dp = cp;
4680N/A if (dp != NULL)
4680N/A dp++;
4680N/A }
4680N/A JLI_MemFree(path);
4680N/A return (target);
4680N/A}
4680N/A
4680N/A/*
4680N/A * Given a path to a jre to execute, this routine checks if this process
4680N/A * is indeed that jre. If not, it exec's that jre.
4680N/A *
4680N/A * We want to actually check the paths rather than just the version string
4680N/A * built into the executable, so that given version specification (and
4680N/A * JAVA_VERSION_PATH) will yield the exact same Java environment, regardless
4680N/A * of the version of the arbitrary launcher we start with.
4680N/A */
4680N/Avoid
4680N/AExecJRE(char *jre, char **argv)
4680N/A{
4680N/A char wanted[PATH_MAX];
4680N/A const char* progname = GetProgramName();
4680N/A const char* execname = NULL;
4680N/A
4680N/A /*
4680N/A * Resolve the real path to the directory containing the selected JRE.
4680N/A */
4680N/A if (realpath(jre, wanted) == NULL) {
4680N/A JLI_ReportErrorMessage(JRE_ERROR9, jre);
4680N/A exit(1);
4680N/A }
4680N/A
4680N/A /*
4680N/A * Resolve the real path to the currently running launcher.
4680N/A */
4680N/A SetExecname(argv);
4680N/A execname = GetExecName();
4680N/A if (execname == NULL) {
4680N/A JLI_ReportErrorMessage(JRE_ERROR10);
4680N/A exit(1);
4680N/A }
4680N/A
4680N/A /*
4680N/A * If the path to the selected JRE directory is a match to the initial
4680N/A * portion of the path to the currently executing JRE, we have a winner!
4680N/A * If so, just return.
4680N/A */
4680N/A if (JLI_StrNCmp(wanted, execname, JLI_StrLen(wanted)) == 0)
4680N/A return; /* I am the droid you were looking for */
4680N/A
4680N/A
4680N/A /*
4680N/A * This should never happen (because of the selection code in SelectJRE),
4680N/A * but check for "impossibly" long path names just because buffer overruns
4680N/A * can be so deadly.
4680N/A */
4680N/A if (JLI_StrLen(wanted) + JLI_StrLen(progname) + 6 > PATH_MAX) {
4680N/A JLI_ReportErrorMessage(JRE_ERROR11);
4680N/A exit(1);
4680N/A }
4680N/A
4680N/A /*
4680N/A * Construct the path and exec it.
4680N/A */
4680N/A (void)JLI_StrCat(JLI_StrCat(wanted, "/bin/"), progname);
4680N/A argv[0] = JLI_StringDup(progname);
4680N/A if (JLI_IsTraceLauncher()) {
4680N/A int i;
4680N/A printf("ReExec Command: %s (%s)\n", wanted, argv[0]);
4680N/A printf("ReExec Args:");
4680N/A for (i = 1; argv[i] != NULL; i++)
4680N/A printf(" %s", argv[i]);
4680N/A printf("\n");
4680N/A }
4680N/A JLI_TraceLauncher("TRACER_MARKER:About to EXEC\n");
4680N/A (void)fflush(stdout);
4680N/A (void)fflush(stderr);
4680N/A execv(wanted, argv);
4680N/A JLI_ReportErrorMessageSys(JRE_ERROR12, wanted);
4680N/A exit(1);
4680N/A}
4680N/A
4680N/A/*
4680N/A * "Borrowed" from Solaris 10 where the unsetenv() function is being added
4680N/A * to libc thanks to SUSv3 (Standard Unix Specification, version 3). As
4680N/A * such, in the fullness of time this will appear in libc on all relevant
4680N/A * Solaris/Linux platforms and maybe even the Windows platform. At that
4680N/A * time, this stub can be removed.
4680N/A *
4680N/A * This implementation removes the environment locking for multithreaded
4680N/A * applications. (We don't have access to these mutexes within libc and
4680N/A * the launcher isn't multithreaded.) Note that what remains is platform
4680N/A * independent, because it only relies on attributes that a POSIX environment
4680N/A * defines.
4680N/A *
4680N/A * Returns 0 on success, -1 on failure.
4680N/A *
4680N/A * Also removed was the setting of errno. The only value of errno set
4680N/A * was EINVAL ("Invalid Argument").
4680N/A */
4680N/A
4680N/A/*
4680N/A * s1(environ) is name=value
4680N/A * s2(name) is name(not the form of name=value).
4680N/A * if names match, return value of 1, else return 0
4680N/A */
4680N/Astatic int
4680N/Amatch_noeq(const char *s1, const char *s2)
4680N/A{
4680N/A while (*s1 == *s2++) {
4680N/A if (*s1++ == '=')
4680N/A return (1);
4680N/A }
4680N/A if (*s1 == '=' && s2[-1] == '\0')
4680N/A return (1);
4680N/A return (0);
4680N/A}
4680N/A
4680N/A/*
4680N/A * added for SUSv3 standard
4680N/A *
4680N/A * Delete entry from environ.
4680N/A * Do not free() memory! Other threads may be using it.
4680N/A * Keep it around forever.
4680N/A */
4680N/Astatic int
4680N/Aborrowed_unsetenv(const char *name)
4680N/A{
4680N/A long idx; /* index into environ */
4680N/A
4680N/A if (name == NULL || *name == '\0' ||
4680N/A JLI_StrChr(name, '=') != NULL) {
4680N/A return (-1);
4680N/A }
4680N/A
4680N/A for (idx = 0; environ[idx] != NULL; idx++) {
4680N/A if (match_noeq(environ[idx], name))
4680N/A break;
4680N/A }
4680N/A if (environ[idx] == NULL) {
4680N/A /* name not found but still a success */
4680N/A return (0);
4680N/A }
4680N/A /* squeeze up one entry */
4680N/A do {
4680N/A environ[idx] = environ[idx+1];
4680N/A } while (environ[++idx] != NULL);
4680N/A
4680N/A return (0);
4680N/A}
4680N/A/* --- End of "borrowed" code --- */
4680N/A
4680N/A/*
4680N/A * Wrapper for unsetenv() function.
4680N/A */
4680N/Aint
4680N/AUnsetEnv(char *name)
4680N/A{
4680N/A return(borrowed_unsetenv(name));
4680N/A}
4680N/A
4680N/Aconst char *
4680N/Ajlong_format_specifier() {
4680N/A return "%lld";
4680N/A}
4680N/A
4680N/Ajboolean
4680N/AIsJavaw()
4680N/A{
4680N/A /* noop on UNIX */
4680N/A return JNI_FALSE;
4680N/A}
4680N/A
4680N/Avoid
4680N/AInitLauncher(jboolean javaw)
4680N/A{
4680N/A JLI_SetTraceLauncher();
4680N/A}
4680N/A
4680N/A/*
4680N/A * The implementation for finding classes from the bootstrap
4680N/A * class loader, refer to java.h
4680N/A */
4680N/Astatic FindClassFromBootLoader_t *findBootClass = NULL;
4680N/A
4680N/Ajclass
4680N/AFindBootStrapClass(JNIEnv *env, const char* classname)
4680N/A{
4680N/A if (findBootClass == NULL) {
4680N/A findBootClass = (FindClassFromBootLoader_t *)dlsym(RTLD_DEFAULT,
4680N/A "JVM_FindClassFromBootLoader");
4680N/A if (findBootClass == NULL) {
4680N/A JLI_ReportErrorMessage(DLL_ERROR4,
4680N/A "JVM_FindClassFromBootLoader");
4680N/A return NULL;
4680N/A }
4680N/A }
4680N/A return findBootClass(env, classname);
4680N/A}
4680N/A
5270N/AStdArg
5270N/A*JLI_GetStdArgs()
5270N/A{
5270N/A return NULL;
5270N/A}
5270N/A
5270N/Aint
5270N/AJLI_GetStdArgc() {
5270N/A return 0;
5270N/A}
5270N/A
5270N/AjobjectArray
5270N/ACreateApplicationArgs(JNIEnv *env, char **strv, int argc)
5270N/A{
5270N/A return NewPlatformStringArray(env, strv, argc);
5270N/A}