/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#include "java.h"
#include "jvm_md.h"
#include <dirent.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "manifest_info.h"
#include "version_comp.h"
/* help jettison the LD_LIBRARY_PATH settings in the future */
#ifndef SETENV_REQUIRED
#define SETENV_REQUIRED
#endif
/*
* If a processor / os combination has the ability to run binaries of
* models is supported, then DUAL_MODE is defined. When DUAL_MODE is
* defined, the architecture names for the narrow and wide version of
* the architecture are defined in LIBARCH64NAME and LIBARCH32NAME.
* current policy.
*/
#ifdef __solaris__
# define DUAL_MODE
# ifndef LIBARCH32NAME
# error "The macro LIBARCH32NAME was not defined on the compile line"
# endif
# ifndef LIBARCH64NAME
# error "The macro LIBARCH64NAME was not defined on the compile line"
# endif
# include <sys/systeminfo.h>
# include <stdio.h>
#endif
/*
* Flowchart of launcher execs and options processing on unix
*
* The selection of the proper vm shared library to open depends on
* several classes of command line options, including vm "flavor"
* options (-client, -server) and the data model options, -d32 and
* -d64, as well as a version specification which may have come from
* the command line or from the manifest of an executable jar file.
* The vm selection options are not passed to the running
* virtual machine; they must be screened out by the launcher.
*
* The version specification (if any) is processed first by the
* platform independent routine SelectVersion. This may result in
* the exec of the specified launcher version.
*
* Previously the launcher modified the LD_LIBRARY_PATH appropriately for the
* desired data model path, regardless if data models matched or not. The
* launcher subsequently exec'ed the desired executable, in order to make the
* LD_LIBRARY_PATH path available, for the runtime linker.
*
* Now, in most cases,the launcher will dlopen the target libjvm.so. All
* required libraries are loaded by the runtime linker, using the
* $RPATH/$ORIGIN baked into the shared libraries at compile time. Therefore,
* in most cases, the launcher will only exec, if the data models are
* mismatched, and will not set any environment variables, regardless of the
* data models.
*
* However, if the environment contains a LD_LIBRARY_PATH, this will cause the
* launcher to inspect the LD_LIBRARY_PATH. The launcher will check
* a. if the LD_LIBRARY_PATH's first component is the the path to the desired
* libjvm.so
* b. if any other libjvm.so is found in any of the paths.
* If case b is true, then the launcher will set the LD_LIBRARY_PATH to the
* desired JRE and reexec, in order to propagate the environment.
*
* Main
* (incoming argv)
* |
* \|/
* SelectVersion
* (selects the JRE version, note: not data model)
* |
* \|/
* CreateExecutionEnvironment
* (determines desired data model)
* |
* |
* \|/
* Have Desired Model ? --> NO --> Is Dual-Mode ? --> NO --> Exit(with error)
* | |
* | |
* | \|/
* | YES
* | |
* | |
* | \|/
* | CheckJvmType
* | (removes -client, -server etc.)
* | |
* | |
* \|/ \|/
* YES Find the desired executable/library
* | |
* | |
* \|/ \|/
* CheckJvmType RequiresSetenv
* (removes -client, -server, etc.)
* |
* |
* \|/
* TranslateDashJArgs...
* (Prepare to pass args to vm)
* |
* |
* \|/
* ParseArguments
* (removes -d32 and -d64 if any,
* processes version options,
* creates argument list for vm,
* etc.)
* |
* |
* \|/
* RequiresSetenv
* Is LD_LIBRARY_PATH
* and friends set ? --> NO --> Have Desired Model ? NO --> Re-exec --> Main
* YES YES --> Continue
* |
* |
* \|/
* Path is desired JRE ? YES --> Have Desired Model ? NO --> Re-exec --> Main
* NO YES --> Continue
* |
* |
* \|/
* Paths have well known
* jvm paths ? --> NO --> Have Desired Model ? NO --> Re-exec --> Main
* YES YES --> Continue
* |
* |
* \|/
* Does libjvm.so exit
* in any of them ? --> NO --> Have Desired Model ? NO --> Re-exec --> Main
* YES YES --> Continue
* |
* |
* \|/
* Set the LD_LIBRARY_PATH
* |
* |
* \|/
* Re-exec
* |
* |
* \|/
* Main
*/
/* Store the name of the executable once computed */
/*
* execname accessor from other parts of platform dependent logic
*/
const char *
GetExecName() {
return execname;
}
const char *
{
switch(nbits) {
#ifdef DUAL_MODE
case 32:
return LIBARCH32NAME;
case 64:
return LIBARCH64NAME;
#endif /* DUAL_MODE */
default:
return LIBARCHNAME;
}
}
#ifdef SETENV_REQUIRED
static jboolean
return JNI_TRUE;
}
return JNI_FALSE;
}
/*
* contains a lib/$LIBARCH/{server,client}/libjvm.so ?
*/
static jboolean
char *envpath;
char *path;
/* fastest path */
return JNI_FALSE;
}
/* the usual suspects */
/* to optimize for time, test if any of our usual suspects are present. */
return JNI_FALSE;
}
/*
* we have a suspicious path component, check if it contains a libjvm.so
*/
return JNI_TRUE;
}
}
return JNI_TRUE;
}
}
}
return JNI_FALSE;
}
/*
* Test whether the environment variable needs to be set, see flowchart.
*/
static jboolean
char *llp;
char *p; /* a utility pointer */
#ifdef __solaris__
? getenv("LD_LIBRARY_PATH_32")
: getenv("LD_LIBRARY_PATH_64");
#endif /* __solaris__ */
/* no environment variable is a good environment variable */
return JNI_FALSE;
}
#ifdef __linux
/*
* On linux, if a binary is running as sgid or suid, glibc sets
* LD_LIBRARY_PATH to the empty string for security purposes. (In contrast,
* on Solaris the LD_LIBRARY_PATH variable for a privileged binary does not
* lose its settings; but the dynamic linker does apply more scrutiny to the
* path.) The launcher uses the value of LD_LIBRARY_PATH to prevent an exec
* loop, here and further downstream. Therefore, if we are running sgid or
* suid, this function's setting of LD_LIBRARY_PATH will be ineffective and
* we should case a return from the calling function. Getting the right
* libraries will be handled by the RPATH. In reality, this check is
* redundant, as the previous check for a non-null LD_LIBRARY_PATH will
* return back to the calling function forthwith, it is left here to safe
* guard against any changes, in the glibc's existing security policy.
*/
return JNI_FALSE;
}
#endif /* __linux */
/*
* Prevent recursions. Since LD_LIBRARY_PATH is the one which will be set by
* previous versions of the JRE, thus it is the only path that matters here.
* So we check to see if the desired JRE is set.
*/
*p = '\0';
return JNI_FALSE;
}
/* scrutinize all the paths further */
return JNI_TRUE;
}
return JNI_TRUE;
}
return JNI_FALSE;
}
#endif /* SETENV_REQUIRED */
void
/*
* 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. However, if we are not running the desired data model,
* some of the errors should be suppressed since it is more
* informative to issue an error message based on whether or not the
*/
SetExecname(*pargv);
/* Check data model flags, and exec process, if needed */
{
asked for? Current model is
fine unless another model
is asked for */
#ifdef SETENV_REQUIRED
#ifdef __solaris__
Solaris only */
#endif /* __solaris__ */
#endif /* SETENV_REQUIRED */
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;
/* scan for data model arguments and remove from argument list;
last occurrence determines desired data model */
for (i=1; i < argc; i++) {
wanted = 64;
continue;
}
wanted = 32;
continue;
}
if (IsJavaArgs()) {
if (argv[i][0] != '-') continue;
} else {
i++;
if (i >= argc) break;
continue;
}
if (argv[i][0] != '-') { i++; break; }
}
}
/* copy rest of args [i .. argc) */
while (i < argc) {
}
/*
* newargv has all proper arguments here
*/
}
/* If the data model is not changing, it is an error if the
jvmpath does not exist */
/* Find out where the JRE is that we will be using. */
exit(2);
}
/* Find the specified JVM type */
exit(1);
}
jvmpath[0] = '\0';
exit(4);
}
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
if (mustsetenv == JNI_FALSE) {
return;
}
#else
return;
#endif /* SETENV_REQUIRED */
} else { /* do the same speculatively or exit */
#ifdef DUAL_MODE
/* Find out where the JRE is that we will be using. */
/* give up and let other code report error message */
exit(1);
}
/*
* Read in jvm.cfg for target data model and process vm
* selection options.
*/
/* give up and let other code report error message */
exit(1);
}
jvmpath[0] = '\0';
exit(4);
}
/* exec child can do error checking on the existence of the path */
#ifdef SETENV_REQUIRED
#endif /* SETENV_REQUIRED */
}
#else /* ! DUALMODE */
exit(1);
#endif /* DUAL_MODE */
}
#ifdef SETENV_REQUIRED
if (mustsetenv) {
/*
* We will set the LD_LIBRARY_PATH as follows:
*
* o $JVMPATH (directory portion only)
* 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.
*
* 3. LD_LIBRARY_PATH_32 -- overrides and replaces LD_LIBRARY_PATH
* for 32-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:
if (running == 32) {
wanted = 32;
} else {
wanted = 64;
}
break;
case 32:
break;
case 64:
break;
default:
break;
}
/*
* If dmpath is NULL, the relevant data model specific variable is
* not set and normal LD_LIBRARY_PATH should be used.
*/
} else {
}
#else /* ! __solaris__ */
/*
* If not on Solaris, assume only a single LD_LIBRARY_PATH
* variable.
*/
#endif /* __solaris__ */
/* runpath contains current effective LD_LIBRARY_PATH setting */
/*
* Create desired LD_LIBRARY_PATH value for target data model.
*/
{
/* remove the name of the .so from the JVM path */
if (lastslash)
*lastslash = '\0';
"%s:"
"%s/../lib/%s",
#ifdef DUAL_MODE
#else /* !DUAL_MODE */
#endif /* DUAL_MODE */
);
/*
* 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.
*/
#ifdef __solaris__
#endif /* __solaris__ */
) {
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) {
}
if (putenv(new_runpath) != 0) {
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.
*/
#endif /* __solaris */
}
#endif /* SETENV_REQUIRED */
{
#ifdef DUAL_MODE
/*
* If the data model is being changed, the path to the
* executable must be updated accordingly; the executable name
* and directory the executable resides in are separate. In the
* case of 32 => 64, the new bits are assumed to reside in, e.g.
* "olddir/LIBARCH64NAME/execname"; in the case of 64 => 32,
* the bits are assumed to be in "olddir/../execname". For example,
*
*
* for Solaris SPARC and Linux amd64, respectively.
*/
*oldbase++ = 0;
}
#endif /* DUAL_MODE */
JLI_TraceLauncher("TRACER_MARKER:About to EXEC\n");
#ifdef SETENV_REQUIRED
if (mustsetenv) {
} else {
}
#else /* !SETENV_REQUIRED */
#endif /* SETENV_REQUIRED */
#ifdef DUAL_MODE
#ifdef __solaris__
#ifdef __sparc
#else /* ! __sparc__ */
#endif /* __sparc */
#endif /* __solaris__ */
}
#endif /* DUAL_MODE */
}
exit(1);
}
}
/*
* On Solaris VM choosing is done by the launcher (java.c),
* bitsWanted is used by MacOSX, on Solaris and Linux this.
* parameter is unused.
*/
static jboolean
{
struct stat s;
} else {
}
JLI_TraceLauncher("yes.\n");
return JNI_TRUE;
} else {
JLI_TraceLauncher("no.\n");
return JNI_FALSE;
}
}
/*
* Find path to JRE based on .exe's location or registry settings.
*/
static jboolean
{
/* Is JRE co-located with the application? */
return JNI_TRUE;
}
/* Does the app ship a private JRE in <apphome>/jre directory? */
return JNI_TRUE;
}
}
if (!speculative)
return JNI_FALSE;
}
{
void *libjvm;
int count;
int location;
return JNI_FALSE;
}
/* read in elf header */
if (count < 1) {
return JNI_FALSE;
}
/*
* Check for running a server vm (compiled with -xarch=v8plus)
* on a stock v8 processor. In this case, the machine type in
* the elf header would not be included the architecture list
* provided by the isalist command, which is turn is gotten from
* sysinfo. This case cannot occur on 64-bit hardware and thus
* does not have to be checked for in binaries with an LP64 data
* model.
*/
page */
long length;
char* location;
if (length > 0) {
return JNI_FALSE;
}
}
}
#endif
return JNI_FALSE;
}
return JNI_FALSE;
}
return JNI_FALSE;
}
return JNI_FALSE;
}
return JNI_TRUE;
}
/*
* 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.
* As a fallback, and for platforms other than Solaris and Linux,
* we use FindExecName to compute the executable name.
*/
const char*
{
#if defined(__solaris__)
{
int (*fptr)();
return JNI_FALSE;
}
}
}
}
}
{
if (len >= 0) {
}
}
#else /* !__solaris__ && !__linux__ */
{
/* Not implemented */
}
#endif
}
return exec_path;
}
/* --- Splash Screen shared library support --- */
if (!hSplashLib) {
const char * splashLibPath;
}
if (hSplashLib) {
return sym;
} else {
return NULL;
}
}
void SplashFreeLibrary() {
if (hSplashLib) {
hSplashLib = NULL;
}
}
/*
* Block current thread and continue execution in a new thread
*/
int
int rslt;
#ifdef __solaris__
long flags = 0;
void * tmp;
} else {
/* See below. Continue in current thread if thr_create() failed */
}
#else /* ! __solaris__ */
if (stack_size > 0) {
}
void * tmp;
} else {
/*
* Continue execution in current thread if for some reason (e.g. out of
* later in continuation as JNI_CreateJavaVM needs to create quite a
* few new threads, anyway, just give it a try..
*/
}
#endif /* __solaris__ */
return rslt;
}
/* Coarse estimation of number of digits assuming the worst case is a 64-bit pid. */
void SetJavaLauncherPlatformProps() {
/* Linux only */
#ifdef __linux__
#endif /* __linux__ */
}
int
{
}
void
{
// stubbed out for windows and *nixes.
}
void
{
// stubbed out for windows and *nixes.
}
/*
* on unix, we return a false to indicate this option is not applicable
*/
{
return JNI_FALSE;
}