1 /* 2 * Copyright (c) 1994, 2024, 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)->ExceptionCheck(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)->ExceptionCheck(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 #ifdef MACOSX 150 /* 151 * Since sun_jnu_encoding is now hard-coded to UTF-8 on Mac, we don't 152 * want to use it to overwrite file.encoding 153 */ 154 PUTPROP(propArray, _file_encoding_NDX, sprops->encoding); 155 #else 156 PUTPROP(propArray, _file_encoding_NDX, sprops->sun_jnu_encoding); 157 #endif 158 PUTPROP(propArray, _sun_jnu_encoding_NDX, sprops->sun_jnu_encoding); 159 160 /* 161 * file encoding for stdout and stderr 162 */ 163 PUTPROP(propArray, _stdout_encoding_NDX, sprops->stdout_encoding); 164 PUTPROP(propArray, _stderr_encoding_NDX, sprops->stderr_encoding); 165 166 /* unicode_encoding specifies the default endianness */ 167 PUTPROP(propArray, _sun_io_unicode_encoding_NDX, sprops->unicode_encoding); 168 PUTPROP(propArray, _sun_cpu_endian_NDX, sprops->cpu_endian); 169 PUTPROP(propArray, _sun_cpu_isalist_NDX, sprops->cpu_isalist); 170 171 #ifdef MACOSX 172 /* Proxy setting properties */ 173 if (sprops->httpProxyEnabled) { 174 PUTPROP(propArray, _http_proxyHost_NDX, sprops->httpHost); 175 PUTPROP(propArray, _http_proxyPort_NDX, sprops->httpPort); 176 } 177 178 if (sprops->httpsProxyEnabled) { 179 PUTPROP(propArray, _https_proxyHost_NDX, sprops->httpsHost); 180 PUTPROP(propArray, _https_proxyPort_NDX, sprops->httpsPort); 181 } 182 183 if (sprops->ftpProxyEnabled) { 184 PUTPROP(propArray, _ftp_proxyHost_NDX, sprops->ftpHost); 185 PUTPROP(propArray, _ftp_proxyPort_NDX, sprops->ftpPort); 186 } 187 188 if (sprops->socksProxyEnabled) { 189 PUTPROP(propArray, _socksProxyHost_NDX, sprops->socksHost); 190 PUTPROP(propArray, _socksProxyPort_NDX, sprops->socksPort); 191 } 192 193 // Mac OS X only has a single proxy exception list which applies 194 // to all protocols 195 if (sprops->exceptionList) { 196 PUTPROP(propArray, _http_nonProxyHosts_NDX, sprops->exceptionList); 197 PUTPROP(propArray, _ftp_nonProxyHosts_NDX, sprops->exceptionList); 198 PUTPROP(propArray, _socksNonProxyHosts_NDX, sprops->exceptionList); 199 } 200 #endif 201 202 /* data model */ 203 if (sizeof(sprops) == 4) { 204 sprops->data_model = "32"; 205 } else if (sizeof(sprops) == 8) { 206 sprops->data_model = "64"; 207 } else { 208 sprops->data_model = "unknown"; 209 } 210 PUTPROP(propArray, _sun_arch_data_model_NDX, sprops->data_model); 211 212 /* patch level */ 213 PUTPROP(propArray, _sun_os_patch_level_NDX, sprops->patch_level); 214 215 PUTPROP_PlatformString(propArray, _java_io_tmpdir_NDX, sprops->tmp_dir); 216 217 PUTPROP_PlatformString(propArray, _user_name_NDX, sprops->user_name); 218 PUTPROP_PlatformString(propArray, _user_home_NDX, sprops->user_home); 219 PUTPROP_PlatformString(propArray, _user_dir_NDX, sprops->user_dir); 220 221 /* 222 * Set i18n related property fields from platform. 223 */ 224 PUTPROP(propArray, _display_language_NDX, sprops->display_language); 225 PUTPROP(propArray, _display_script_NDX, sprops->display_script); 226 PUTPROP(propArray, _display_country_NDX, sprops->display_country); 227 PUTPROP(propArray, _display_variant_NDX, sprops->display_variant); 228 229 PUTPROP(propArray, _format_language_NDX, sprops->format_language); 230 PUTPROP(propArray, _format_script_NDX, sprops->format_script); 231 PUTPROP(propArray, _format_country_NDX, sprops->format_country); 232 PUTPROP(propArray, _format_variant_NDX, sprops->format_variant); 233 234 return propArray; 235 } 236 237 /* 238 * Gather the VM and command line properties and return as a String[]. 239 * The array indices are alternating key/value pairs 240 * supplied by the VM including those defined on the command line 241 * using -Dkey=value that may override the platform defined value. 242 * 243 * Note: The platform encoding must have been set. 244 * 245 * Class: jdk_internal_util_SystemProps_Raw 246 * Method: vmProperties 247 * Signature: ()[Ljava/lang/String; 248 */ 249 JNIEXPORT jobjectArray JNICALL 250 Java_jdk_internal_util_SystemProps_00024Raw_vmProperties(JNIEnv *env, jclass cla) 251 { 252 jobjectArray cmdProps = JVM_GetProperties(env); 253 return cmdProps; 254 } 255 256 /* 257 * The following three functions implement setter methods for 258 * java.lang.System.{in, out, err}. They are natively implemented 259 * because they violate the semantics of the language (i.e. set final 260 * variable). 261 */ 262 JNIEXPORT void JNICALL 263 Java_java_lang_System_setIn0(JNIEnv *env, jclass cla, jobject stream) 264 { 265 jfieldID fid = 266 (*env)->GetStaticFieldID(env,cla,"in","Ljava/io/InputStream;"); 267 if (fid == 0) 268 return; 269 (*env)->SetStaticObjectField(env,cla,fid,stream); 270 } 271 272 JNIEXPORT void JNICALL 273 Java_java_lang_System_setOut0(JNIEnv *env, jclass cla, jobject stream) 274 { 275 jfieldID fid = 276 (*env)->GetStaticFieldID(env,cla,"out","Ljava/io/PrintStream;"); 277 if (fid == 0) 278 return; 279 (*env)->SetStaticObjectField(env,cla,fid,stream); 280 } 281 282 JNIEXPORT void JNICALL 283 Java_java_lang_System_setErr0(JNIEnv *env, jclass cla, jobject stream) 284 { 285 jfieldID fid = 286 (*env)->GetStaticFieldID(env,cla,"err","Ljava/io/PrintStream;"); 287 if (fid == 0) 288 return; 289 (*env)->SetStaticObjectField(env,cla,fid,stream); 290 } 291 292 static void cpchars(jchar *dst, char *src, int n) 293 { 294 int i; 295 for (i = 0; i < n; i++) { 296 dst[i] = src[i]; 297 } 298 } 299 300 JNIEXPORT jstring JNICALL 301 Java_java_lang_System_mapLibraryName(JNIEnv *env, jclass ign, jstring libname) 302 { 303 int len; 304 int prefix_len = (int) strlen(JNI_LIB_PREFIX); 305 int suffix_len = (int) strlen(JNI_LIB_SUFFIX); 306 307 jchar chars[256]; 308 if (libname == NULL) { 309 JNU_ThrowNullPointerException(env, 0); 310 return NULL; 311 } 312 len = (*env)->GetStringLength(env, libname); 313 if (len > 240) { 314 JNU_ThrowIllegalArgumentException(env, "name too long"); 315 return NULL; 316 } 317 cpchars(chars, JNI_LIB_PREFIX, prefix_len); 318 (*env)->GetStringRegion(env, libname, 0, len, chars + prefix_len); 319 len += prefix_len; 320 cpchars(chars + len, JNI_LIB_SUFFIX, suffix_len); 321 len += suffix_len; 322 323 return (*env)->NewString(env, chars, len); 324 }