Path: blob/master/src/java.base/unix/native/libjli/java_md_common.c
41119 views
/*1* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24#include <sys/time.h>25#include "java.h"2627/*28* Find the last occurrence of a string29*/30static char* findLastPathComponent(char *buffer, const char *comp) {31char* t = buffer;32char* p = NULL;33size_t l = JLI_StrLen(comp);34t = JLI_StrStr(t, comp);3536while (t != NULL) {37p = t;38t += l;39t = JLI_StrStr(t, comp);40}41return p;42}4344/*45* Removes the trailing file name and any intermediate platform46* directories, if any, and its enclosing directory.47* Ex: if a buffer contains "/foo/bin/javac" or "/foo/bin/x64/javac", the48* truncated resulting buffer will contain "/foo".49*/50static jboolean51TruncatePath(char *buf)52{53// try bin directory, maybe an executable54char *p = findLastPathComponent(buf, "/bin/");55if (p != NULL) {56*p = '\0';57return JNI_TRUE;58}59// try lib directory, maybe a library60p = findLastPathComponent(buf, "/lib/");61if (p != NULL) {62*p = '\0';63return JNI_TRUE;64}65return JNI_FALSE;66}6768/*69* Retrieves the path to the JRE home by locating the executable file70* of the current process and then truncating the path to the executable71*/72jboolean73GetApplicationHome(char *buf, jint bufsize)74{75const char *execname = GetExecName();76if (execname != NULL) {77JLI_Snprintf(buf, bufsize, "%s", execname);78buf[bufsize-1] = '\0';79} else {80return JNI_FALSE;81}82return TruncatePath(buf);83}8485/*86* Retrieves the path to the JRE home by locating the87* shared library and then truncating the path to it.88*/89jboolean90GetApplicationHomeFromDll(char *buf, jint bufsize)91{92/* try to find ourselves instead */93Dl_info info;94if (dladdr((void*)&GetApplicationHomeFromDll, &info) != 0) {95char *path = realpath(info.dli_fname, buf);96if (path == buf) {97return TruncatePath(buf);98}99}100return JNI_FALSE;101}102103/*104* Return true if the named program exists105*/106static int107ProgramExists(char *name)108{109struct stat sb;110if (stat(name, &sb) != 0) return 0;111if (S_ISDIR(sb.st_mode)) return 0;112return (sb.st_mode & S_IEXEC) != 0;113}114115/*116* Find a command in a directory, returning the path.117*/118static char *119Resolve(char *indir, char *cmd)120{121char name[PATH_MAX + 2], *real;122123if ((JLI_StrLen(indir) + JLI_StrLen(cmd) + 1) > PATH_MAX) return 0;124JLI_Snprintf(name, sizeof(name), "%s%c%s", indir, FILE_SEPARATOR, cmd);125if (!ProgramExists(name)) return 0;126real = JLI_MemAlloc(PATH_MAX + 2);127if (!realpath(name, real))128JLI_StrCpy(real, name);129return real;130}131132/*133* Find a path for the executable134*/135char *136FindExecName(char *program)137{138char cwdbuf[PATH_MAX+2];139char *path;140char *tmp_path;141char *f;142char *result = NULL;143144/* absolute path? */145if (*program == FILE_SEPARATOR ||146(FILE_SEPARATOR=='\\' && JLI_StrRChr(program, ':')))147return Resolve("", program+1);148149/* relative path? */150if (JLI_StrRChr(program, FILE_SEPARATOR) != 0) {151char buf[PATH_MAX+2];152return Resolve(getcwd(cwdbuf, sizeof(cwdbuf)), program);153}154155/* from search path? */156path = getenv("PATH");157if (!path || !*path) path = ".";158tmp_path = JLI_MemAlloc(JLI_StrLen(path) + 2);159JLI_StrCpy(tmp_path, path);160161for (f=tmp_path; *f && result==0; ) {162char *s = f;163while (*f && (*f != PATH_SEPARATOR)) ++f;164if (*f) *f++ = 0;165if (*s == FILE_SEPARATOR)166result = Resolve(s, program);167else {168/* relative path element */169char dir[2*PATH_MAX];170JLI_Snprintf(dir, sizeof(dir), "%s%c%s", getcwd(cwdbuf, sizeof(cwdbuf)),171FILE_SEPARATOR, s);172result = Resolve(dir, program);173}174if (result != 0) break;175}176177JLI_MemFree(tmp_path);178return result;179}180181JNIEXPORT void JNICALL182JLI_ReportErrorMessage(const char* fmt, ...) {183va_list vl;184va_start(vl, fmt);185vfprintf(stderr, fmt, vl);186fprintf(stderr, "\n");187va_end(vl);188}189190JNIEXPORT void JNICALL191JLI_ReportErrorMessageSys(const char* fmt, ...) {192va_list vl;193char *emsg;194195/*196* TODO: its safer to use strerror_r but is not available on197* Solaris 8. Until then....198*/199emsg = strerror(errno);200if (emsg != NULL) {201fprintf(stderr, "%s\n", emsg);202}203204va_start(vl, fmt);205vfprintf(stderr, fmt, vl);206fprintf(stderr, "\n");207va_end(vl);208}209210JNIEXPORT void JNICALL211JLI_ReportExceptionDescription(JNIEnv * env) {212(*env)->ExceptionDescribe(env);213}214215/*216* Since using the file system as a registry is a bit risky, perform217* additional sanity checks on the identified directory to validate218* it as a valid jre/sdk.219*220* Return 0 if the tests fail; otherwise return non-zero (true).221*222* Note that checking for anything more than the existence of an223* executable object at bin/java relative to the path being checked224* will break the regression tests.225*/226static int227CheckSanity(char *path, char *dir)228{229char buffer[PATH_MAX];230231if (JLI_StrLen(path) + JLI_StrLen(dir) + 11 > PATH_MAX)232return (0); /* Silently reject "impossibly" long paths */233234JLI_Snprintf(buffer, sizeof(buffer), "%s/%s/bin/java", path, dir);235return ((access(buffer, X_OK) == 0) ? 1 : 0);236}237238/*239* "Borrowed" from Solaris 10 where the unsetenv() function is being added240* to libc thanks to SUSv3 (Standard Unix Specification, version 3). As241* such, in the fullness of time this will appear in libc on all relevant242* Solaris/Linux platforms and maybe even the Windows platform. At that243* time, this stub can be removed.244*245* This implementation removes the environment locking for multithreaded246* applications. (We don't have access to these mutexes within libc and247* the launcher isn't multithreaded.) Note that what remains is platform248* independent, because it only relies on attributes that a POSIX environment249* defines.250*251* Returns 0 on success, -1 on failure.252*253* Also removed was the setting of errno. The only value of errno set254* was EINVAL ("Invalid Argument").255*/256257/*258* s1(environ) is name=value259* s2(name) is name(not the form of name=value).260* if names match, return value of 1, else return 0261*/262static int263match_noeq(const char *s1, const char *s2)264{265while (*s1 == *s2++) {266if (*s1++ == '=')267return (1);268}269if (*s1 == '=' && s2[-1] == '\0')270return (1);271return (0);272}273274/*275* added for SUSv3 standard276*277* Delete entry from environ.278* Do not free() memory! Other threads may be using it.279* Keep it around forever.280*/281static int282borrowed_unsetenv(const char *name)283{284long idx; /* index into environ */285286if (name == NULL || *name == '\0' ||287JLI_StrChr(name, '=') != NULL) {288return (-1);289}290291for (idx = 0; environ[idx] != NULL; idx++) {292if (match_noeq(environ[idx], name))293break;294}295if (environ[idx] == NULL) {296/* name not found but still a success */297return (0);298}299/* squeeze up one entry */300do {301environ[idx] = environ[idx+1];302} while (environ[++idx] != NULL);303304return (0);305}306/* --- End of "borrowed" code --- */307308/*309* Wrapper for unsetenv() function.310*/311int312UnsetEnv(char *name)313{314return(borrowed_unsetenv(name));315}316317jboolean318IsJavaw()319{320/* noop on UNIX */321return JNI_FALSE;322}323324void325InitLauncher(jboolean javaw)326{327JLI_SetTraceLauncher();328}329330/*331* The implementation for finding classes from the bootstrap332* class loader, refer to java.h333*/334static FindClassFromBootLoader_t *findBootClass = NULL;335336jclass337FindBootStrapClass(JNIEnv *env, const char* classname)338{339if (findBootClass == NULL) {340findBootClass = (FindClassFromBootLoader_t *)dlsym(RTLD_DEFAULT,341"JVM_FindClassFromBootLoader");342if (findBootClass == NULL) {343JLI_ReportErrorMessage(DLL_ERROR4,344"JVM_FindClassFromBootLoader");345return NULL;346}347}348return findBootClass(env, classname);349}350351JNIEXPORT StdArg JNICALL352*JLI_GetStdArgs()353{354return NULL;355}356357JNIEXPORT int JNICALL358JLI_GetStdArgc() {359return 0;360}361362jobjectArray363CreateApplicationArgs(JNIEnv *env, char **strv, int argc)364{365return NewPlatformStringArray(env, strv, argc);366}367368/*369* Provide a CurrentTimeMicros() implementation based on gettimeofday() which370* is universally available, even though it may not be 'high resolution'371* compared to platforms that provide gethrtime() (like Solaris). It is372* also subject to time-of-day changes, but alternatives may not be373* known to be available at either build time or run time.374*/375jlong CurrentTimeMicros() {376jlong result = 0;377struct timeval tv;378if (gettimeofday(&tv, NULL) != -1) {379result = 1000000LL * (jlong)tv.tv_sec;380result += (jlong)tv.tv_usec;381}382return result;383}384385386