1 /* 2 * Copyright (c) 1994, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #include <string.h> 27 28 #include "jni.h" 29 #include "jni_util.h" 30 #include "jvm.h" 31 #include "java_props.h" 32 33 #include "java_lang_System.h" 34 #include "jdk_internal_util_SystemProps_Raw.h" 35 36 #define OBJ "Ljava/lang/Object;" 37 38 /* Only register the performance-critical methods */ 39 static JNINativeMethod methods[] = { 40 {"currentTimeMillis", "()J", (void *)&JVM_CurrentTimeMillis}, 41 {"nanoTime", "()J", (void *)&JVM_NanoTime}, 42 {"arraycopy", "(" OBJ "I" OBJ "II)V", (void *)&JVM_ArrayCopy}, 43 }; 44 45 #undef OBJ 46 47 JNIEXPORT void JNICALL 48 Java_java_lang_System_registerNatives(JNIEnv *env, jclass cls) 49 { 50 (*env)->RegisterNatives(env, cls, 51 methods, sizeof(methods)/sizeof(methods[0])); 52 } 53 54 JNIEXPORT jint JNICALL 55 Java_java_lang_System_identityHashCode(JNIEnv *env, jobject this, jobject x) 56 { 57 return JVM_IHashCode(env, x); 58 } 59 60 /* VENDOR, VENDOR_URL, VENDOR_URL_BUG are set in VersionProps.java.template. */ 61 62 /* 63 * Store the UTF-8 string encoding of the value in the array 64 * at the index if the value is non-null. Store nothing if the value is null. 65 * On any error, return from Java_jdk_internal_util_SystemProps_00024Raw_platformProperties. 66 */ 67 #define PUTPROP(array, prop_index, val) \ 68 if (val != NULL) { \ 69 jstring jval = (*env)->NewStringUTF(env, val); \ 70 if (jval == NULL) \ 71 return NULL; \ 72 (*env)->SetObjectArrayElement(env, array, jdk_internal_util_SystemProps_Raw_##prop_index, jval); \ 73 if ((*env)->ExceptionOccurred(env)) \ 74 return NULL; \ 75 (*env)->DeleteLocalRef(env, jval); \ 76 } 77 78 /* 79 * Store the Platform string encoding of the value in the array 80 * at the index if the value is non-null. Store nothing if the value is null. 81 * On any error, return from Java_jdk_internal_util_SystemProps_00024Raw_platformProperties. 82 */ 83 #define PUTPROP_PlatformString(array, prop_index, val) \ 84 if (val != NULL) { \ 85 jstring jval = GetStringPlatform(env, val); \ 86 if (jval == NULL) \ 87 return NULL; \ 88 (*env)->SetObjectArrayElement(env, array, jdk_internal_util_SystemProps_Raw_##prop_index, jval); \ 89 if ((*env)->ExceptionOccurred(env)) \ 90 return NULL; \ 91 (*env)->DeleteLocalRef(env, jval); \ 92 } 93 94 /* 95 * Gather the system properties and return as a String[]. 96 * The first FIXED_LENGTH entries are the platform defined property values, no names. 97 * The remaining array indices are alternating key/value pairs 98 * supplied by the VM including those defined on the command line 99 * using -Dkey=value that may override the platform defined value. 100 * The caller is responsible for replacing platform provided values as needed. 101 * 102 * Class: jdk_internal_util_SystemProps_Raw 103 * Method: platformProperties 104 * Signature: ()[Ljava/lang/String; 105 */ 106 JNIEXPORT jobjectArray JNICALL 107 Java_jdk_internal_util_SystemProps_00024Raw_platformProperties(JNIEnv *env, jclass cla) 108 { 109 java_props_t *sprops; 110 jobject propArray = NULL; 111 jclass classString; 112 int nstrings = jdk_internal_util_SystemProps_Raw_FIXED_LENGTH; 113 114 // Get the platform specific values 115 sprops = GetJavaProperties(env); 116 CHECK_NULL_RETURN(sprops, NULL); 117 118 /* 119 * !!! DO NOT call PUTPROP_PlatformString (NewStringPlatform) before this line !!! 120 */ 121 InitializeEncoding(env, sprops->sun_jnu_encoding); 122 123 // Ensure capacity for the array and for a string for each fixed length element 124 if ((*env)->EnsureLocalCapacity(env, nstrings + 2) < 0) { 125 return NULL; 126 } 127 128 // Allocate an array of String for all the well known props 129 classString = JNU_ClassString(env); 130 CHECK_NULL_RETURN(classString, NULL); 131 132 propArray = (*env)->NewObjectArray(env, nstrings, classString, NULL); 133 CHECK_NULL_RETURN(propArray, NULL); 134 135 /* os properties */ 136 PUTPROP(propArray, _os_name_NDX, sprops->os_name); 137 PUTPROP(propArray, _os_version_NDX, sprops->os_version); 138 PUTPROP(propArray, _os_arch_NDX, sprops->os_arch); 139 140 #ifdef JDK_ARCH_ABI_PROP_NAME 141 PUTPROP(propArray, _sun_arch_abi_NDX, sprops->sun_arch_abi); 142 #endif 143 144 /* file system properties */ 145 PUTPROP(propArray, _file_separator_NDX, sprops->file_separator); 146 PUTPROP(propArray, _path_separator_NDX, sprops->path_separator); 147 PUTPROP(propArray, _line_separator_NDX, sprops->line_separator); 148 149 PUTPROP(propArray, _file_encoding_NDX, sprops->encoding); 150 PUTPROP(propArray, _sun_jnu_encoding_NDX, sprops->sun_jnu_encoding); 151 152 /* 153 * file encoding for stdout and stderr 154 */ 155 PUTPROP(propArray, _sun_stdout_encoding_NDX, sprops->sun_stdout_encoding); 156 PUTPROP(propArray, _sun_stderr_encoding_NDX, sprops->sun_stderr_encoding); 157 158 /* unicode_encoding specifies the default endianness */ 159 PUTPROP(propArray, _sun_io_unicode_encoding_NDX, sprops->unicode_encoding); 160 PUTPROP(propArray, _sun_cpu_endian_NDX, sprops->cpu_endian); 161 PUTPROP(propArray, _sun_cpu_isalist_NDX, sprops->cpu_isalist); 162 163 #ifdef MACOSX 164 /* Proxy setting properties */ 165 if (sprops->httpProxyEnabled) { 166 PUTPROP(propArray, _http_proxyHost_NDX, sprops->httpHost); 167 PUTPROP(propArray, _http_proxyPort_NDX, sprops->httpPort); 168 } 169 170 if (sprops->httpsProxyEnabled) { 171 PUTPROP(propArray, _https_proxyHost_NDX, sprops->httpsHost); 172 PUTPROP(propArray, _https_proxyPort_NDX, sprops->httpsPort); 173 } 174 175 if (sprops->ftpProxyEnabled) { 176 PUTPROP(propArray, _ftp_proxyHost_NDX, sprops->ftpHost); 177 PUTPROP(propArray, _ftp_proxyPort_NDX, sprops->ftpPort); 178 } 179 180 if (sprops->socksProxyEnabled) { 181 PUTPROP(propArray, _socksProxyHost_NDX, sprops->socksHost); 182 PUTPROP(propArray, _socksProxyPort_NDX, sprops->socksPort); 183 } 184 185 // Mac OS X only has a single proxy exception list which applies 186 // to all protocols 187 if (sprops->exceptionList) { 188 PUTPROP(propArray, _http_nonProxyHosts_NDX, sprops->exceptionList); 189 PUTPROP(propArray, _ftp_nonProxyHosts_NDX, sprops->exceptionList); 190 PUTPROP(propArray, _socksNonProxyHosts_NDX, sprops->exceptionList); 191 } 192 #endif 193 194 /* data model */ 195 if (sizeof(sprops) == 4) { 196 sprops->data_model = "32"; 197 } else if (sizeof(sprops) == 8) { 198 sprops->data_model = "64"; 199 } else { 200 sprops->data_model = "unknown"; 201 } 202 PUTPROP(propArray, _sun_arch_data_model_NDX, sprops->data_model); 203 204 /* patch level */ 205 PUTPROP(propArray, _sun_os_patch_level_NDX, sprops->patch_level); 206 207 PUTPROP(propArray, _awt_toolkit_NDX, sprops->awt_toolkit); 208 209 PUTPROP_PlatformString(propArray, _java_io_tmpdir_NDX, sprops->tmp_dir); 210 211 PUTPROP_PlatformString(propArray, _user_name_NDX, sprops->user_name); 212 PUTPROP_PlatformString(propArray, _user_home_NDX, sprops->user_home); 213 PUTPROP_PlatformString(propArray, _user_dir_NDX, sprops->user_dir); 214 215 /* 216 * Set i18n related property fields from platform. 217 */ 218 PUTPROP(propArray, _display_language_NDX, sprops->display_language); 219 PUTPROP(propArray, _display_script_NDX, sprops->display_script); 220 PUTPROP(propArray, _display_country_NDX, sprops->display_country); 221 PUTPROP(propArray, _display_variant_NDX, sprops->display_variant); 222 223 PUTPROP(propArray, _format_language_NDX, sprops->format_language); 224 PUTPROP(propArray, _format_script_NDX, sprops->format_script); 225 PUTPROP(propArray, _format_country_NDX, sprops->format_country); 226 PUTPROP(propArray, _format_variant_NDX, sprops->format_variant); 227 228 return propArray; 229 } 230 231 /* 232 * Gather the VM and command line properties and return as a String[]. 233 * The array indices are alternating key/value pairs 234 * supplied by the VM including those defined on the command line 235 * using -Dkey=value that may override the platform defined value. 236 * 237 * Note: The platform encoding must have been set. 238 * 239 * Class: jdk_internal_util_SystemProps_Raw 240 * Method: vmProperties 241 * Signature: ()[Ljava/lang/String; 242 */ 243 JNIEXPORT jobjectArray JNICALL 244 Java_jdk_internal_util_SystemProps_00024Raw_vmProperties(JNIEnv *env, jclass cla) 245 { 246 jobjectArray cmdProps = JVM_GetProperties(env); 247 return cmdProps; 248 } 249 250 /* 251 * The following three functions implement setter methods for 252 * java.lang.System.{in, out, err}. They are natively implemented 253 * because they violate the semantics of the language (i.e. set final 254 * variable). 255 */ 256 JNIEXPORT void JNICALL 257 Java_java_lang_System_setIn0(JNIEnv *env, jclass cla, jobject stream) 258 { 259 jfieldID fid = 260 (*env)->GetStaticFieldID(env,cla,"in","Ljava/io/InputStream;"); 261 if (fid == 0) 262 return; 263 (*env)->SetStaticObjectField(env,cla,fid,stream); 264 } 265 266 JNIEXPORT void JNICALL 267 Java_java_lang_System_setOut0(JNIEnv *env, jclass cla, jobject stream) 268 { 269 jfieldID fid = 270 (*env)->GetStaticFieldID(env,cla,"out","Ljava/io/PrintStream;"); 271 if (fid == 0) 272 return; 273 (*env)->SetStaticObjectField(env,cla,fid,stream); 274 } 275 276 JNIEXPORT void JNICALL 277 Java_java_lang_System_setErr0(JNIEnv *env, jclass cla, jobject stream) 278 { 279 jfieldID fid = 280 (*env)->GetStaticFieldID(env,cla,"err","Ljava/io/PrintStream;"); 281 if (fid == 0) 282 return; 283 (*env)->SetStaticObjectField(env,cla,fid,stream); 284 } 285 286 static void cpchars(jchar *dst, char *src, int n) 287 { 288 int i; 289 for (i = 0; i < n; i++) { 290 dst[i] = src[i]; 291 } 292 } 293 294 JNIEXPORT jstring JNICALL 295 Java_java_lang_System_mapLibraryName(JNIEnv *env, jclass ign, jstring libname) 296 { 297 int len; 298 int prefix_len = (int) strlen(JNI_LIB_PREFIX); 299 int suffix_len = (int) strlen(JNI_LIB_SUFFIX); 300 301 jchar chars[256]; 302 if (libname == NULL) { 303 JNU_ThrowNullPointerException(env, 0); 304 return NULL; 305 } 306 len = (*env)->GetStringLength(env, libname); 307 if (len > 240) { 308 JNU_ThrowIllegalArgumentException(env, "name too long"); 309 return NULL; 310 } 311 cpchars(chars, JNI_LIB_PREFIX, prefix_len); 312 (*env)->GetStringRegion(env, libname, 0, len, chars + prefix_len); 313 len += prefix_len; 314 cpchars(chars + len, JNI_LIB_SUFFIX, suffix_len); 315 len += suffix_len; 316 317 return (*env)->NewString(env, chars, len); 318 }