src/java.base/unix/native/libjli/java_md_common.c
Print this page
*** 1,7 ****
/*
! * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
--- 1,7 ----
/*
! * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
*** 188,391 ****
JLI_Snprintf(buffer, sizeof(buffer), "%s/%s/bin/java", path, dir);
return ((access(buffer, X_OK) == 0) ? 1 : 0);
}
- /*
- * 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);
- }
- }
-
- /*
- * This is the global entry point. It examines the host for the optimal
- * JRE to be used by scanning a set of directories. The set of directories
- * is platform dependent and can be overridden by the environment
- * variable JAVA_VERSION_PATH.
- *
- * This routine itself simply determines the set of appropriate
- * directories before passing control onto ProcessDir().
- */
- 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);
- }
-
- /*
- * 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 (and
- * JAVA_VERSION_PATH) will yield the exact same Java environment, regardless
- * of the version of the arbitrary launcher we start with.
- */
- void
- ExecJRE(char *jre, char **argv)
- {
- char wanted[PATH_MAX];
- const char* progname = GetProgramName();
- const char* execname = NULL;
-
- /*
- * Resolve the real path to the directory containing the selected JRE.
- */
- if (realpath(jre, wanted) == NULL) {
- JLI_ReportErrorMessage(JRE_ERROR9, jre);
- exit(1);
- }
-
- /*
- * Resolve the real path to the currently running launcher.
- */
- SetExecname(argv);
- execname = GetExecName();
- if (execname == NULL) {
- JLI_ReportErrorMessage(JRE_ERROR10);
- exit(1);
- }
-
- /*
- * 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_StrNCmp(wanted, execname, JLI_StrLen(wanted)) == 0)
- return; /* I am the droid you were looking for */
-
-
- /*
- * This should never happen (because of the selection code in SelectJRE),
- * but check for "impossibly" long path names just because buffer overruns
- * can be so deadly.
- */
- if (JLI_StrLen(wanted) + JLI_StrLen(progname) + 6 > PATH_MAX) {
- JLI_ReportErrorMessage(JRE_ERROR11);
- exit(1);
- }
-
- /*
- * Construct the path and exec it.
- */
- (void)JLI_StrCat(JLI_StrCat(wanted, "/bin/"), progname);
- argv[0] = JLI_StringDup(progname);
- if (JLI_IsTraceLauncher()) {
- int i;
- printf("ReExec Command: %s (%s)\n", wanted, argv[0]);
- printf("ReExec Args:");
- for (i = 1; argv[i] != NULL; i++)
- printf(" %s", argv[i]);
- printf("\n");
- }
- JLI_TraceLauncher("TRACER_MARKER:About to EXEC\n");
- (void)fflush(stdout);
- (void)fflush(stderr);
- execv(wanted, argv);
- JLI_ReportErrorMessageSys(JRE_ERROR12, wanted);
- exit(1);
- }
-
/*
* "Borrowed" from Solaris 10 where the unsetenv() function is being added
* to libc thanks to SUSv3 (Standard Unix Specification, version 3). As
* such, in the fullness of time this will appear in libc on all relevant
* Solaris/Linux platforms and maybe even the Windows platform. At that
--- 188,197 ----