Path: blob/master/src/java.base/share/native/libjava/System.c
67766 views
/*1* Copyright (c) 1994, 2021, 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);147148#ifdef MACOSX149/*150* Since sun_jnu_encoding is now hard-coded to UTF-8 on Mac, we don't151* want to use it to overwrite file.encoding152*/153PUTPROP(propArray, _file_encoding_NDX, sprops->encoding);154#else155PUTPROP(propArray, _file_encoding_NDX, sprops->sun_jnu_encoding);156#endif157PUTPROP(propArray, _sun_jnu_encoding_NDX, sprops->sun_jnu_encoding);158159/*160* file encoding for stdout and stderr161*/162PUTPROP(propArray, _sun_stdout_encoding_NDX, sprops->sun_stdout_encoding);163PUTPROP(propArray, _sun_stderr_encoding_NDX, sprops->sun_stderr_encoding);164165/* unicode_encoding specifies the default endianness */166PUTPROP(propArray, _sun_io_unicode_encoding_NDX, sprops->unicode_encoding);167PUTPROP(propArray, _sun_cpu_endian_NDX, sprops->cpu_endian);168PUTPROP(propArray, _sun_cpu_isalist_NDX, sprops->cpu_isalist);169170#ifdef MACOSX171/* Proxy setting properties */172if (sprops->httpProxyEnabled) {173PUTPROP(propArray, _http_proxyHost_NDX, sprops->httpHost);174PUTPROP(propArray, _http_proxyPort_NDX, sprops->httpPort);175}176177if (sprops->httpsProxyEnabled) {178PUTPROP(propArray, _https_proxyHost_NDX, sprops->httpsHost);179PUTPROP(propArray, _https_proxyPort_NDX, sprops->httpsPort);180}181182if (sprops->ftpProxyEnabled) {183PUTPROP(propArray, _ftp_proxyHost_NDX, sprops->ftpHost);184PUTPROP(propArray, _ftp_proxyPort_NDX, sprops->ftpPort);185}186187if (sprops->socksProxyEnabled) {188PUTPROP(propArray, _socksProxyHost_NDX, sprops->socksHost);189PUTPROP(propArray, _socksProxyPort_NDX, sprops->socksPort);190}191192// Mac OS X only has a single proxy exception list which applies193// to all protocols194if (sprops->exceptionList) {195PUTPROP(propArray, _http_nonProxyHosts_NDX, sprops->exceptionList);196PUTPROP(propArray, _ftp_nonProxyHosts_NDX, sprops->exceptionList);197PUTPROP(propArray, _socksNonProxyHosts_NDX, sprops->exceptionList);198}199#endif200201/* data model */202if (sizeof(sprops) == 4) {203sprops->data_model = "32";204} else if (sizeof(sprops) == 8) {205sprops->data_model = "64";206} else {207sprops->data_model = "unknown";208}209PUTPROP(propArray, _sun_arch_data_model_NDX, sprops->data_model);210211/* patch level */212PUTPROP(propArray, _sun_os_patch_level_NDX, sprops->patch_level);213214PUTPROP_PlatformString(propArray, _java_io_tmpdir_NDX, sprops->tmp_dir);215216PUTPROP_PlatformString(propArray, _user_name_NDX, sprops->user_name);217PUTPROP_PlatformString(propArray, _user_home_NDX, sprops->user_home);218PUTPROP_PlatformString(propArray, _user_dir_NDX, sprops->user_dir);219220/*221* Set i18n related property fields from platform.222*/223PUTPROP(propArray, _display_language_NDX, sprops->display_language);224PUTPROP(propArray, _display_script_NDX, sprops->display_script);225PUTPROP(propArray, _display_country_NDX, sprops->display_country);226PUTPROP(propArray, _display_variant_NDX, sprops->display_variant);227228PUTPROP(propArray, _format_language_NDX, sprops->format_language);229PUTPROP(propArray, _format_script_NDX, sprops->format_script);230PUTPROP(propArray, _format_country_NDX, sprops->format_country);231PUTPROP(propArray, _format_variant_NDX, sprops->format_variant);232233return propArray;234}235236/*237* Gather the VM and command line properties and return as a String[].238* The array indices are alternating key/value pairs239* supplied by the VM including those defined on the command line240* using -Dkey=value that may override the platform defined value.241*242* Note: The platform encoding must have been set.243*244* Class: jdk_internal_util_SystemProps_Raw245* Method: vmProperties246* Signature: ()[Ljava/lang/String;247*/248JNIEXPORT jobjectArray JNICALL249Java_jdk_internal_util_SystemProps_00024Raw_vmProperties(JNIEnv *env, jclass cla)250{251jobjectArray cmdProps = JVM_GetProperties(env);252return cmdProps;253}254255/*256* The following three functions implement setter methods for257* java.lang.System.{in, out, err}. They are natively implemented258* because they violate the semantics of the language (i.e. set final259* variable).260*/261JNIEXPORT void JNICALL262Java_java_lang_System_setIn0(JNIEnv *env, jclass cla, jobject stream)263{264jfieldID fid =265(*env)->GetStaticFieldID(env,cla,"in","Ljava/io/InputStream;");266if (fid == 0)267return;268(*env)->SetStaticObjectField(env,cla,fid,stream);269}270271JNIEXPORT void JNICALL272Java_java_lang_System_setOut0(JNIEnv *env, jclass cla, jobject stream)273{274jfieldID fid =275(*env)->GetStaticFieldID(env,cla,"out","Ljava/io/PrintStream;");276if (fid == 0)277return;278(*env)->SetStaticObjectField(env,cla,fid,stream);279}280281JNIEXPORT void JNICALL282Java_java_lang_System_setErr0(JNIEnv *env, jclass cla, jobject stream)283{284jfieldID fid =285(*env)->GetStaticFieldID(env,cla,"err","Ljava/io/PrintStream;");286if (fid == 0)287return;288(*env)->SetStaticObjectField(env,cla,fid,stream);289}290291static void cpchars(jchar *dst, char *src, int n)292{293int i;294for (i = 0; i < n; i++) {295dst[i] = src[i];296}297}298299JNIEXPORT jstring JNICALL300Java_java_lang_System_mapLibraryName(JNIEnv *env, jclass ign, jstring libname)301{302int len;303int prefix_len = (int) strlen(JNI_LIB_PREFIX);304int suffix_len = (int) strlen(JNI_LIB_SUFFIX);305306jchar chars[256];307if (libname == NULL) {308JNU_ThrowNullPointerException(env, 0);309return NULL;310}311len = (*env)->GetStringLength(env, libname);312if (len > 240) {313JNU_ThrowIllegalArgumentException(env, "name too long");314return NULL;315}316cpchars(chars, JNI_LIB_PREFIX, prefix_len);317(*env)->GetStringRegion(env, libname, 0, len, chars + prefix_len);318len += prefix_len;319cpchars(chars + len, JNI_LIB_SUFFIX, suffix_len);320len += suffix_len;321322return (*env)->NewString(env, chars, len);323}324325326