/* * Copyright (c) 1998, 2022, 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 * 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.
*/
/* help jettison the LD_LIBRARY_PATH settings in the future */ #ifndef SETENV_REQUIRED #define SETENV_REQUIRED #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). * 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 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) * | * \|/ * CreateExecutionEnvironment * (determines desired data model) * | * | * \|/ * Have Desired Model ? --> NO --> Exit(with error) * | * | * \|/ * YES * | * | * \|/ * CheckJvmType * (removes -client, -server, etc.) * | * | * \|/ * TranslateDashJArgs... * (Prepare to pass args to vm) * | * | * \|/ * ParseArguments * | * | * \|/ * RequiresSetenv * Is LD_LIBRARY_PATH * and friends set ? --> NO --> Continue * YES * | * | * \|/ * Path is desired JRE ? YES --> Continue * NO * | * | * \|/ * Paths have well known * jvm paths ? --> NO --> Error/Exit * YES * | * | * \|/ * Does libjvm.so exist * in any of them ? --> NO --> Continue * YES * | * | * \|/ * Set the LD_LIBRARY_PATH * | * | * \|/ * Re-exec * | * | * \|/ * Main
*/
/* Store the name of the executable once computed */ staticchar *execname = NULL;
/* * execname accessor from other parts of platform dependent logic
*/ constchar *
GetExecName() { return execname;
}
/* 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 = strtok_r(envpath, ":", &save_ptr); path != NULL; path = strtok_r(NULL, ":", &save_ptr)) { 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;
}
/* * Test whether the environment variable needs to be set, see flowchart.
*/ static jboolean
RequiresSetenv(constchar *jvmpath) { char jpath[PATH_MAX + 1]; char *llp; char *dmllp = NULL; char *p; /* a utility pointer */
#ifdef MUSL_LIBC /* * The musl library loader requires LD_LIBRARY_PATH to be set in order * to correctly resolve the dependency libjava.so has on libjvm.so.
*/ return JNI_TRUE; #endif
#ifdef AIX /* We always have to set the LIBPATH on AIX because ld doesn't support $ORIGIN. */ return JNI_TRUE; #endif
llp = getenv("LD_LIBRARY_PATH"); /* no environment variable is a good environment variable */ if (llp == NULL && dmllp == NULL) { 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.
*/ if ((getgid() != getegid()) || (getuid() != geteuid())) { 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.
*/
JLI_StrNCpy(jpath, jvmpath, PATH_MAX);
p = JLI_StrRChr(jpath, '/');
*p = '\0'; if (llp != NULL && JLI_StrNCmp(llp, jpath, JLI_StrLen(jpath)) == 0) { return JNI_FALSE;
}
/* scrutinize all the paths further */ if (llp != NULL && ContainsLibJVM(llp)) { return JNI_TRUE;
} if (dmllp != NULL && ContainsLibJVM(dmllp)) { return JNI_TRUE;
} return JNI_FALSE;
} #endif/* SETENV_REQUIRED */
/* Compute/set the name of the executable */
SetExecname(*pargv);
/* Check to see if the jvmpath exists */ /* Find out where the JRE is that we will be using. */ if (!GetJREPath(jrepath, so_jrepath, JNI_FALSE)) {
JLI_ReportErrorMessage(JRE_ERROR1); exit(2);
}
JLI_Snprintf(jvmcfg, so_jvmcfg, "%s%slib%sjvm.cfg",
jrepath, FILESEP, FILESEP); /* Find the specified JVM type */ if (ReadKnownVMs(jvmcfg, JNI_FALSE) < 1) {
JLI_ReportErrorMessage(CFG_ERROR7); exit(1);
}
if (!GetJVMPath(jrepath, jvmtype, jvmpath, so_jvmpath)) {
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");
#ifdef SETENV_REQUIRED if (mustsetenv) { /* * We will set the LD_LIBRARY_PATH as follows: * * o $JVMPATH (directory portion only) * o $JRE/lib * o $JRE/../lib * * followed by the user's previous effective LD_LIBRARY_PATH, if * any.
*/
/* * Create desired LD_LIBRARY_PATH value for target data model.
*/
{ /* remove the name of the .so from the JVM path */
lastslash = JLI_StrRChr(new_jvmpath, '/'); if (lastslash)
*lastslash = '\0';
/* * 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)] == ':')) {
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) { /* problem allocating memory; LD_LIBRARY_PATH not set properly */ exit(1);
}
/* * 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.
*/
/* * 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.
*/ constchar*
SetExecname(char **argv)
{ char* exec_path = NULL; #ifdefined(__linux__)
{ constchar* self = "/proc/self/exe"; char buf[PATH_MAX+1]; int len = readlink(self, buf, PATH_MAX); if (len >= 0) {
buf[len] = '\0'; /* readlink(2) doesn't NUL terminate */
exec_path = JLI_StringDup(buf);
}
} #else/* !__linux__ */
{ /* Not implemented */
} #endif
/* * Signature adapter for pthread_create() or thr_create().
*/ staticvoid* ThreadJavaMain(void* args) { return (void*)(intptr_t)JavaMain(args);
}
static size_t adjustStackSize(size_t stack_size) { long page_size = sysconf(_SC_PAGESIZE); if (stack_size % page_size == 0) { return stack_size;
} else { long pages = stack_size / page_size; // Ensure we don't go over limit if (stack_size <= SIZE_MAX - page_size) {
pages++;
} return page_size * pages;
}
}
/* * Block current thread and continue execution in a new thread.
*/ int
CallJavaMainInNewThread(jlong stack_size, void* args) { int rslt;
pthread_t tid;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
size_t adjusted_stack_size;
if (stack_size > 0) { if (pthread_attr_setstacksize(&attr, stack_size) == EINVAL) { // System may require stack size to be multiple of page size // Retry with adjusted value
adjusted_stack_size = adjustStackSize(stack_size); if (adjusted_stack_size != (size_t) stack_size) {
pthread_attr_setstacksize(&attr, adjusted_stack_size);
}
}
}
pthread_attr_setguardsize(&attr, 0); // no pthread guard page on java threads
if (pthread_create(&tid, &attr, ThreadJavaMain, args) == 0) { void* tmp;
pthread_join(tid, &tmp);
rslt = (int)(intptr_t)tmp;
} else { /* * Continue execution in current thread if for some reason (e.g. out of * memory/LWP) a new thread can't be created. This will likely fail * later in JavaMain as JNI_CreateJavaVM needs to create quite a * few new threads, anyway, just give it a try..
*/
rslt = JavaMain(args);
}
pthread_attr_destroy(&attr); return rslt;
}
/* Coarse estimation of number of digits assuming the worst case is a 64-bit pid. */ #define MAX_PID_STR_SZ 20
int
JVMInit(InvocationFunctions* ifn, jlong threadStackSize, int argc, char **argv, int mode, char *what, int ret)
{
ShowSplashScreen(); return ContinueInNewThread(ifn, threadStackSize, argc, argv, mode, what, ret);
}
void
PostJVMInit(JNIEnv *env, jclass mainClass, JavaVM *vm)
{ // stubbed out for windows and *nixes.
}
void
RegisterThread()
{ // stubbed out for windows and *nixes.
}
/* * on unix, we return a false to indicate this option is not applicable
*/
jboolean
ProcessPlatformOption(constchar *arg)
{ return JNI_FALSE;
}
¤ Dauer der Verarbeitung: 0.38 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.