Path: blob/master/src/java.base/share/native/libjava/System.c
41149 views
/*1* Copyright (c) 1994, 2018, 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*/2425#include <string.h>2627#include "jni.h"28#include "jni_util.h"29#include "jvm.h"30#include "java_props.h"3132#include "java_lang_System.h"33#include "jdk_internal_util_SystemProps_Raw.h"3435#define OBJ "Ljava/lang/Object;"3637/* Only register the performance-critical methods */38static JNINativeMethod methods[] = {39{"currentTimeMillis", "()J", (void *)&JVM_CurrentTimeMillis},40{"nanoTime", "()J", (void *)&JVM_NanoTime},41{"arraycopy", "(" OBJ "I" OBJ "II)V", (void *)&JVM_ArrayCopy},42};4344#undef OBJ4546JNIEXPORT void JNICALL47Java_java_lang_System_registerNatives(JNIEnv *env, jclass cls)48{49(*env)->RegisterNatives(env, cls,50methods, sizeof(methods)/sizeof(methods[0]));51}5253JNIEXPORT jint JNICALL54Java_java_lang_System_identityHashCode(JNIEnv *env, jobject this, jobject x)55{56return JVM_IHashCode(env, x);57}5859/* VENDOR, VENDOR_URL, VENDOR_URL_BUG are set in VersionProps.java.template. */6061/*62* Store the UTF-8 string encoding of the value in the array63* at the index if the value is non-null. Store nothing if the value is null.64* On any error, return from Java_jdk_internal_util_SystemProps_00024Raw_platformProperties.65*/66#define PUTPROP(array, prop_index, val) \67if (val != NULL) { \68jstring jval = (*env)->NewStringUTF(env, val); \69if (jval == NULL) \70return NULL; \71(*env)->SetObjectArrayElement(env, array, jdk_internal_util_SystemProps_Raw_##prop_index, jval); \72if ((*env)->ExceptionOccurred(env)) \73return NULL; \74(*env)->DeleteLocalRef(env, jval); \75}7677/*78* Store the Platform string encoding of the value in the array79* at the index if the value is non-null. Store nothing if the value is null.80* On any error, return from Java_jdk_internal_util_SystemProps_00024Raw_platformProperties.81*/82#define PUTPROP_PlatformString(array, prop_index, val) \83if (val != NULL) { \84jstring jval = GetStringPlatform(env, val); \85if (jval == NULL) \86return NULL; \87(*env)->SetObjectArrayElement(env, array, jdk_internal_util_SystemProps_Raw_##prop_index, jval); \88if ((*env)->ExceptionOccurred(env)) \89return NULL; \90(*env)->DeleteLocalRef(env, jval); \91}9293/*94* Gather the system properties and return as a String[].95* The first FIXED_LENGTH entries are the platform defined property values, no names.96* The remaining array indices are alternating key/value pairs97* supplied by the VM including those defined on the command line98* using -Dkey=value that may override the platform defined value.99* The caller is responsible for replacing platform provided values as needed.100*101* Class: jdk_internal_util_SystemProps_Raw102* Method: platformProperties103* Signature: ()[Ljava/lang/String;104*/105JNIEXPORT jobjectArray JNICALL106Java_jdk_internal_util_SystemProps_00024Raw_platformProperties(JNIEnv *env, jclass cla)107{108java_props_t *sprops;109jobject propArray = NULL;110jclass classString;111int nstrings = jdk_internal_util_SystemProps_Raw_FIXED_LENGTH;112113// Get the platform specific values114sprops = GetJavaProperties(env);115CHECK_NULL_RETURN(sprops, NULL);116117/*118* !!! DO NOT call PUTPROP_PlatformString (NewStringPlatform) before this line !!!119*/120InitializeEncoding(env, sprops->sun_jnu_encoding);121122// Ensure capacity for the array and for a string for each fixed length element123if ((*env)->EnsureLocalCapacity(env, nstrings + 2) < 0) {124return NULL;125}126127// Allocate an array of String for all the well known props128classString = JNU_ClassString(env);129CHECK_NULL_RETURN(classString, NULL);130131propArray = (*env)->NewObjectArray(env, nstrings, classString, NULL);132CHECK_NULL_RETURN(propArray, NULL);133134/* os properties */135PUTPROP(propArray, _os_name_NDX, sprops->os_name);136PUTPROP(propArray, _os_version_NDX, sprops->os_version);137PUTPROP(propArray, _os_arch_NDX, sprops->os_arch);138139#ifdef JDK_ARCH_ABI_PROP_NAME140PUTPROP(propArray, _sun_arch_abi_NDX, sprops->sun_arch_abi);141#endif142143/* file system properties */144PUTPROP(propArray, _file_separator_NDX, sprops->file_separator);145PUTPROP(propArray, _path_separator_NDX, sprops->path_separator);146PUTPROP(propArray, _line_separator_NDX, sprops->line_separator);147148PUTPROP(propArray, _file_encoding_NDX, sprops->encoding);149PUTPROP(propArray, _sun_jnu_encoding_NDX, sprops->sun_jnu_encoding);150151/*152* file encoding for stdout and stderr153*/154PUTPROP(propArray, _sun_stdout_encoding_NDX, sprops->sun_stdout_encoding);155PUTPROP(propArray, _sun_stderr_encoding_NDX, sprops->sun_stderr_encoding);156157/* unicode_encoding specifies the default endianness */158PUTPROP(propArray, _sun_io_unicode_encoding_NDX, sprops->unicode_encoding);159PUTPROP(propArray, _sun_cpu_endian_NDX, sprops->cpu_endian);160PUTPROP(propArray, _sun_cpu_isalist_NDX, sprops->cpu_isalist);161162#ifdef MACOSX163/* Proxy setting properties */164if (sprops->httpProxyEnabled) {165PUTPROP(propArray, _http_proxyHost_NDX, sprops->httpHost);166PUTPROP(propArray, _http_proxyPort_NDX, sprops->httpPort);167}168169if (sprops->httpsProxyEnabled) {170PUTPROP(propArray, _https_proxyHost_NDX, sprops->httpsHost);171PUTPROP(propArray, _https_proxyPort_NDX, sprops->httpsPort);172}173174if (sprops->ftpProxyEnabled) {175PUTPROP(propArray, _ftp_proxyHost_NDX, sprops->ftpHost);176PUTPROP(propArray, _ftp_proxyPort_NDX, sprops->ftpPort);177}178179if (sprops->socksProxyEnabled) {180PUTPROP(propArray, _socksProxyHost_NDX, sprops->socksHost);181PUTPROP(propArray, _socksProxyPort_NDX, sprops->socksPort);182}183184// Mac OS X only has a single proxy exception list which applies185// to all protocols186if (sprops->exceptionList) {187PUTPROP(propArray, _http_nonProxyHosts_NDX, sprops->exceptionList);188PUTPROP(propArray, _ftp_nonProxyHosts_NDX, sprops->exceptionList);189PUTPROP(propArray, _socksNonProxyHosts_NDX, sprops->exceptionList);190}191#endif192193/* data model */194if (sizeof(sprops) == 4) {195sprops->data_model = "32";196} else if (sizeof(sprops) == 8) {197sprops->data_model = "64";198} else {199sprops->data_model = "unknown";200}201PUTPROP(propArray, _sun_arch_data_model_NDX, sprops->data_model);202203/* patch level */204PUTPROP(propArray, _sun_os_patch_level_NDX, sprops->patch_level);205206PUTPROP_PlatformString(propArray, _java_io_tmpdir_NDX, sprops->tmp_dir);207208PUTPROP_PlatformString(propArray, _user_name_NDX, sprops->user_name);209PUTPROP_PlatformString(propArray, _user_home_NDX, sprops->user_home);210PUTPROP_PlatformString(propArray, _user_dir_NDX, sprops->user_dir);211212/*213* Set i18n related property fields from platform.214*/215PUTPROP(propArray, _display_language_NDX, sprops->display_language);216PUTPROP(propArray, _display_script_NDX, sprops->display_script);217PUTPROP(propArray, _display_country_NDX, sprops->display_country);218PUTPROP(propArray, _display_variant_NDX, sprops->display_variant);219220PUTPROP(propArray, _format_language_NDX, sprops->format_language);221PUTPROP(propArray, _format_script_NDX, sprops->format_script);222PUTPROP(propArray, _format_country_NDX, sprops->format_country);223PUTPROP(propArray, _format_variant_NDX, sprops->format_variant);224225return propArray;226}227228/*229* Gather the VM and command line properties and return as a String[].230* The array indices are alternating key/value pairs231* supplied by the VM including those defined on the command line232* using -Dkey=value that may override the platform defined value.233*234* Note: The platform encoding must have been set.235*236* Class: jdk_internal_util_SystemProps_Raw237* Method: vmProperties238* Signature: ()[Ljava/lang/String;239*/240JNIEXPORT jobjectArray JNICALL241Java_jdk_internal_util_SystemProps_00024Raw_vmProperties(JNIEnv *env, jclass cla)242{243jobjectArray cmdProps = JVM_GetProperties(env);244return cmdProps;245}246247/*248* The following three functions implement setter methods for249* java.lang.System.{in, out, err}. They are natively implemented250* because they violate the semantics of the language (i.e. set final251* variable).252*/253JNIEXPORT void JNICALL254Java_java_lang_System_setIn0(JNIEnv *env, jclass cla, jobject stream)255{256jfieldID fid =257(*env)->GetStaticFieldID(env,cla,"in","Ljava/io/InputStream;");258if (fid == 0)259return;260(*env)->SetStaticObjectField(env,cla,fid,stream);261}262263JNIEXPORT void JNICALL264Java_java_lang_System_setOut0(JNIEnv *env, jclass cla, jobject stream)265{266jfieldID fid =267(*env)->GetStaticFieldID(env,cla,"out","Ljava/io/PrintStream;");268if (fid == 0)269return;270(*env)->SetStaticObjectField(env,cla,fid,stream);271}272273JNIEXPORT void JNICALL274Java_java_lang_System_setErr0(JNIEnv *env, jclass cla, jobject stream)275{276jfieldID fid =277(*env)->GetStaticFieldID(env,cla,"err","Ljava/io/PrintStream;");278if (fid == 0)279return;280(*env)->SetStaticObjectField(env,cla,fid,stream);281}282283static void cpchars(jchar *dst, char *src, int n)284{285int i;286for (i = 0; i < n; i++) {287dst[i] = src[i];288}289}290291JNIEXPORT jstring JNICALL292Java_java_lang_System_mapLibraryName(JNIEnv *env, jclass ign, jstring libname)293{294int len;295int prefix_len = (int) strlen(JNI_LIB_PREFIX);296int suffix_len = (int) strlen(JNI_LIB_SUFFIX);297298jchar chars[256];299if (libname == NULL) {300JNU_ThrowNullPointerException(env, 0);301return NULL;302}303len = (*env)->GetStringLength(env, libname);304if (len > 240) {305JNU_ThrowIllegalArgumentException(env, "name too long");306return NULL;307}308cpchars(chars, JNI_LIB_PREFIX, prefix_len);309(*env)->GetStringRegion(env, libname, 0, len, chars + prefix_len);310len += prefix_len;311cpchars(chars + len, JNI_LIB_SUFFIX, suffix_len);312len += suffix_len;313314return (*env)->NewString(env, chars, len);315}316317318