1 /* 2 * Copyright (c) 1994, 2017, 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 35 #define OBJ "Ljava/lang/Object;" 36 37 /* Only register the performance-critical methods */ 38 static 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 }; 43 44 #undef OBJ 45 46 JNIEXPORT void JNICALL 47 Java_java_lang_System_registerNatives(JNIEnv *env, jclass cls) 48 { 49 (*env)->RegisterNatives(env, cls, 50 methods, sizeof(methods)/sizeof(methods[0])); 51 } 52 53 JNIEXPORT jint JNICALL 54 Java_java_lang_System_identityHashCode(JNIEnv *env, jobject this, jobject x) 55 { 56 return JVM_IHashCode(env, x); 57 } 58 59 #define PUTPROP(props, key, val) \ 60 if (1) { \ 61 jstring jkey, jval; \ 62 jobject r; \ 63 jkey = (*env)->NewStringUTF(env, key); \ 64 if (jkey == NULL) return NULL; \ 65 jval = (*env)->NewStringUTF(env, val); \ 66 if (jval == NULL) return NULL; \ 67 r = (*env)->CallObjectMethod(env, props, putID, jkey, jval); \ 68 if ((*env)->ExceptionOccurred(env)) return NULL; \ 69 (*env)->DeleteLocalRef(env, jkey); \ 70 (*env)->DeleteLocalRef(env, jval); \ 71 (*env)->DeleteLocalRef(env, r); \ 72 } else ((void) 0) 73 74 /* "key" is a char type string with only ASCII character in it. 75 "val" is a nchar (typedefed in java_props.h) type string */ 76 77 #define PUTPROP_ForPlatformNString(props, key, val) \ 78 if (1) { \ 79 jstring jkey, jval; \ 80 jobject r; \ 81 jkey = (*env)->NewStringUTF(env, key); \ 82 if (jkey == NULL) return NULL; \ 83 jval = GetStringPlatform(env, val); \ 84 if (jval == NULL) return NULL; \ 85 r = (*env)->CallObjectMethod(env, props, putID, jkey, jval); \ 86 if ((*env)->ExceptionOccurred(env)) return NULL; \ 87 (*env)->DeleteLocalRef(env, jkey); \ 88 (*env)->DeleteLocalRef(env, jval); \ 89 (*env)->DeleteLocalRef(env, r); \ 90 } else ((void) 0) 91 #define REMOVEPROP(props, key) \ 92 if (1) { \ 93 jstring jkey; \ 94 jobject r; \ 95 jkey = JNU_NewStringPlatform(env, key); \ 96 if (jkey == NULL) return NULL; \ 97 r = (*env)->CallObjectMethod(env, props, removeID, jkey); \ 98 if ((*env)->ExceptionOccurred(env)) return NULL; \ 99 (*env)->DeleteLocalRef(env, jkey); \ 100 (*env)->DeleteLocalRef(env, r); \ 101 } else ((void) 0) 102 #define GETPROP(props, key, jret) \ 103 if (1) { \ 104 jstring jkey = JNU_NewStringPlatform(env, key); \ 105 if (jkey == NULL) return NULL; \ 106 jret = (*env)->CallObjectMethod(env, props, getPropID, jkey); \ 107 if ((*env)->ExceptionOccurred(env)) return NULL; \ 108 (*env)->DeleteLocalRef(env, jkey); \ 109 } else ((void) 0) 110 111 #ifndef VENDOR /* Third party may overwrite this. */ 112 #define VENDOR "Oracle Corporation" 113 #define VENDOR_URL "http://java.oracle.com/" 114 #define VENDOR_URL_BUG "http://bugreport.java.com/bugreport/" 115 #endif 116 117 #define JAVA_MAX_SUPPORTED_VERSION 54 118 #define JAVA_MAX_SUPPORTED_MINOR_VERSION 0 119 120 #ifdef JAVA_SPECIFICATION_VENDOR /* Third party may NOT overwrite this. */ 121 #error "ERROR: No override of JAVA_SPECIFICATION_VENDOR is allowed" 122 #else 123 #define JAVA_SPECIFICATION_VENDOR "Oracle Corporation" 124 #endif 125 126 static int fmtdefault; // boolean value 127 jobject fillI18nProps(JNIEnv *env, jobject props, char *baseKey, 128 char *platformDispVal, char *platformFmtVal, 129 jmethodID putID, jmethodID getPropID) { 130 jstring jVMBaseVal = NULL; 131 132 GETPROP(props, baseKey, jVMBaseVal); 133 if (jVMBaseVal) { 134 // user specified the base property. there's nothing to do here. 135 (*env)->DeleteLocalRef(env, jVMBaseVal); 136 } else { 137 char buf[64]; 138 jstring jVMVal = NULL; 139 const char *baseVal = ""; 140 141 /* user.xxx base property */ 142 if (fmtdefault) { 143 if (platformFmtVal) { 144 PUTPROP(props, baseKey, platformFmtVal); 145 baseVal = platformFmtVal; 146 } 147 } else { 148 if (platformDispVal) { 149 PUTPROP(props, baseKey, platformDispVal); 150 baseVal = platformDispVal; 151 } 152 } 153 154 /* user.xxx.display property */ 155 jio_snprintf(buf, sizeof(buf), "%s.display", baseKey); 156 GETPROP(props, buf, jVMVal); 157 if (jVMVal == NULL) { 158 if (platformDispVal && (strcmp(baseVal, platformDispVal) != 0)) { 159 PUTPROP(props, buf, platformDispVal); 160 } 161 } else { 162 (*env)->DeleteLocalRef(env, jVMVal); 163 } 164 165 /* user.xxx.format property */ 166 jio_snprintf(buf, sizeof(buf), "%s.format", baseKey); 167 GETPROP(props, buf, jVMVal); 168 if (jVMVal == NULL) { 169 if (platformFmtVal && (strcmp(baseVal, platformFmtVal) != 0)) { 170 PUTPROP(props, buf, platformFmtVal); 171 } 172 } else { 173 (*env)->DeleteLocalRef(env, jVMVal); 174 } 175 } 176 177 return NULL; 178 } 179 180 JNIEXPORT jobject JNICALL 181 Java_java_lang_System_initProperties(JNIEnv *env, jclass cla, jobject props) 182 { 183 char buf[128]; 184 java_props_t *sprops; 185 jmethodID putID, removeID, getPropID; 186 jobject ret = NULL; 187 jstring jVMVal = NULL; 188 189 if ((*env)->EnsureLocalCapacity(env, 50) < 0) { 190 return NULL; 191 } 192 sprops = GetJavaProperties(env); 193 CHECK_NULL_RETURN(sprops, NULL); 194 195 putID = (*env)->GetMethodID(env, 196 (*env)->GetObjectClass(env, props), 197 "put", 198 "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); 199 CHECK_NULL_RETURN(putID, NULL); 200 201 removeID = (*env)->GetMethodID(env, 202 (*env)->GetObjectClass(env, props), 203 "remove", 204 "(Ljava/lang/Object;)Ljava/lang/Object;"); 205 CHECK_NULL_RETURN(removeID, NULL); 206 207 getPropID = (*env)->GetMethodID(env, 208 (*env)->GetObjectClass(env, props), 209 "getProperty", 210 "(Ljava/lang/String;)Ljava/lang/String;"); 211 CHECK_NULL_RETURN(getPropID, NULL); 212 213 PUTPROP(props, "java.specification.version", 214 VERSION_SPECIFICATION); 215 PUTPROP(props, "java.specification.name", 216 "Java Platform API Specification"); 217 PUTPROP(props, "java.specification.vendor", 218 JAVA_SPECIFICATION_VENDOR); 219 220 PUTPROP(props, "java.version", VERSION_SHORT); 221 PUTPROP(props, "java.vendor", VENDOR); 222 PUTPROP(props, "java.vendor.url", VENDOR_URL); 223 PUTPROP(props, "java.vendor.url.bug", VENDOR_URL_BUG); 224 225 jio_snprintf(buf, sizeof(buf), "%d.%d", JAVA_MAX_SUPPORTED_VERSION, 226 JAVA_MAX_SUPPORTED_MINOR_VERSION); 227 PUTPROP(props, "java.class.version", buf); 228 229 if (sprops->awt_toolkit) { 230 PUTPROP(props, "awt.toolkit", sprops->awt_toolkit); 231 } 232 #ifdef MACOSX 233 if (sprops->awt_headless) { 234 PUTPROP(props, "java.awt.headless", sprops->awt_headless); 235 } 236 #endif 237 238 /* os properties */ 239 PUTPROP(props, "os.name", sprops->os_name); 240 PUTPROP(props, "os.version", sprops->os_version); 241 PUTPROP(props, "os.arch", sprops->os_arch); 242 243 #ifdef JDK_ARCH_ABI_PROP_NAME 244 PUTPROP(props, "sun.arch.abi", sprops->sun_arch_abi); 245 #endif 246 247 /* file system properties */ 248 PUTPROP(props, "file.separator", sprops->file_separator); 249 PUTPROP(props, "path.separator", sprops->path_separator); 250 PUTPROP(props, "line.separator", sprops->line_separator); 251 252 /* 253 * user.language 254 * user.script, user.country, user.variant (if user's environment specifies them) 255 * file.encoding 256 * file.encoding.pkg 257 */ 258 PUTPROP(props, "user.language", sprops->language); 259 if (sprops->script) { 260 PUTPROP(props, "user.script", sprops->script); 261 } 262 if (sprops->country) { 263 PUTPROP(props, "user.country", sprops->country); 264 } 265 if (sprops->variant) { 266 PUTPROP(props, "user.variant", sprops->variant); 267 } 268 PUTPROP(props, "file.encoding", sprops->encoding); 269 PUTPROP(props, "sun.jnu.encoding", sprops->sun_jnu_encoding); 270 if (sprops->sun_stdout_encoding != NULL) { 271 PUTPROP(props, "sun.stdout.encoding", sprops->sun_stdout_encoding); 272 } 273 if (sprops->sun_stderr_encoding != NULL) { 274 PUTPROP(props, "sun.stderr.encoding", sprops->sun_stderr_encoding); 275 } 276 PUTPROP(props, "file.encoding.pkg", "sun.io"); 277 278 /* unicode_encoding specifies the default endianness */ 279 PUTPROP(props, "sun.io.unicode.encoding", sprops->unicode_encoding); 280 PUTPROP(props, "sun.cpu.isalist", 281 (sprops->cpu_isalist ? sprops->cpu_isalist : "")); 282 PUTPROP(props, "sun.cpu.endian", sprops->cpu_endian); 283 284 285 #ifdef MACOSX 286 /* Proxy setting properties */ 287 if (sprops->httpProxyEnabled) { 288 PUTPROP(props, "http.proxyHost", sprops->httpHost); 289 PUTPROP(props, "http.proxyPort", sprops->httpPort); 290 } 291 292 if (sprops->httpsProxyEnabled) { 293 PUTPROP(props, "https.proxyHost", sprops->httpsHost); 294 PUTPROP(props, "https.proxyPort", sprops->httpsPort); 295 } 296 297 if (sprops->ftpProxyEnabled) { 298 PUTPROP(props, "ftp.proxyHost", sprops->ftpHost); 299 PUTPROP(props, "ftp.proxyPort", sprops->ftpPort); 300 } 301 302 if (sprops->socksProxyEnabled) { 303 PUTPROP(props, "socksProxyHost", sprops->socksHost); 304 PUTPROP(props, "socksProxyPort", sprops->socksPort); 305 } 306 307 if (sprops->gopherProxyEnabled) { 308 // The gopher client is different in that it expects an 'is this set?' flag that the others don't. 309 PUTPROP(props, "gopherProxySet", "true"); 310 PUTPROP(props, "gopherProxyHost", sprops->gopherHost); 311 PUTPROP(props, "gopherProxyPort", sprops->gopherPort); 312 } else { 313 PUTPROP(props, "gopherProxySet", "false"); 314 } 315 316 // Mac OS X only has a single proxy exception list which applies 317 // to all protocols 318 if (sprops->exceptionList) { 319 PUTPROP(props, "http.nonProxyHosts", sprops->exceptionList); 320 PUTPROP(props, "ftp.nonProxyHosts", sprops->exceptionList); 321 PUTPROP(props, "socksNonProxyHosts", sprops->exceptionList); 322 } 323 #endif 324 325 /* !!! DO NOT call PUTPROP_ForPlatformNString before this line !!! 326 * !!! I18n properties have not been set up yet !!! 327 */ 328 329 /* Printing properties */ 330 /* Note: java.awt.printerjob is an implementation private property which 331 * just happens to have a java.* name because it is referenced in 332 * a java.awt class. It is the mechanism by which the implementation 333 * finds the appropriate class in the JRE for the platform. 334 * It is explicitly not designed to be overridden by clients as 335 * a way of replacing the implementation class, and in any case 336 * the mechanism by which the class is loaded is constrained to only 337 * find and load classes that are part of the JRE. 338 * This property may be removed if that mechanism is redesigned 339 */ 340 PUTPROP(props, "java.awt.printerjob", sprops->printerJob); 341 342 /* data model */ 343 if (sizeof(sprops) == 4) { 344 sprops->data_model = "32"; 345 } else if (sizeof(sprops) == 8) { 346 sprops->data_model = "64"; 347 } else { 348 sprops->data_model = "unknown"; 349 } 350 PUTPROP(props, "sun.arch.data.model", \ 351 sprops->data_model); 352 353 /* patch level */ 354 PUTPROP(props, "sun.os.patch.level", \ 355 sprops->patch_level); 356 357 /* Java2D properties */ 358 /* Note: java.awt.graphicsenv is an implementation private property which 359 * just happens to have a java.* name because it is referenced in 360 * a java.awt class. It is the mechanism by which the implementation 361 * finds the appropriate class in the JRE for the platform. 362 * It is explicitly not designed to be overridden by clients as 363 * a way of replacing the implementation class, and in any case 364 * the mechanism by which the class is loaded is constrained to only 365 * find and load classes that are part of the JRE. 366 * This property may be removed if that mechanism is redesigned 367 */ 368 PUTPROP(props, "java.awt.graphicsenv", sprops->graphics_env); 369 if (sprops->font_dir != NULL) { 370 PUTPROP_ForPlatformNString(props, 371 "sun.java2d.fontpath", sprops->font_dir); 372 } 373 374 PUTPROP_ForPlatformNString(props, "java.io.tmpdir", sprops->tmp_dir); 375 376 PUTPROP_ForPlatformNString(props, "user.name", sprops->user_name); 377 PUTPROP_ForPlatformNString(props, "user.home", sprops->user_home); 378 379 PUTPROP(props, "user.timezone", sprops->timezone); 380 381 PUTPROP_ForPlatformNString(props, "user.dir", sprops->user_dir); 382 383 /* This is a sun. property as it is currently only set for Gnome and 384 * Windows desktops. 385 */ 386 if (sprops->desktop != NULL) { 387 PUTPROP(props, "sun.desktop", sprops->desktop); 388 } 389 390 /* 391 * unset "user.language", "user.script", "user.country", and "user.variant" 392 * in order to tell whether the command line option "-DXXXX=YYYY" is 393 * specified or not. They will be reset in fillI18nProps() below. 394 */ 395 REMOVEPROP(props, "user.language"); 396 REMOVEPROP(props, "user.script"); 397 REMOVEPROP(props, "user.country"); 398 REMOVEPROP(props, "user.variant"); 399 REMOVEPROP(props, "file.encoding"); 400 401 ret = JVM_InitProperties(env, props); 402 403 /* Check the compatibility flag */ 404 GETPROP(props, "sun.locale.formatasdefault", jVMVal); 405 if (jVMVal) { 406 const char * val = (*env)->GetStringUTFChars(env, jVMVal, 0); 407 CHECK_NULL_RETURN(val, NULL); 408 fmtdefault = !strcmp(val, "true"); 409 (*env)->ReleaseStringUTFChars(env, jVMVal, val); 410 (*env)->DeleteLocalRef(env, jVMVal); 411 } 412 413 /* reconstruct i18n related properties */ 414 fillI18nProps(env, props, "user.language", sprops->display_language, 415 sprops->format_language, putID, getPropID); 416 fillI18nProps(env, props, "user.script", 417 sprops->display_script, sprops->format_script, putID, getPropID); 418 fillI18nProps(env, props, "user.country", 419 sprops->display_country, sprops->format_country, putID, getPropID); 420 fillI18nProps(env, props, "user.variant", 421 sprops->display_variant, sprops->format_variant, putID, getPropID); 422 GETPROP(props, "file.encoding", jVMVal); 423 if (jVMVal == NULL) { 424 #ifdef MACOSX 425 /* 426 * Since sun_jnu_encoding is now hard-coded to UTF-8 on Mac, we don't 427 * want to use it to overwrite file.encoding 428 */ 429 PUTPROP(props, "file.encoding", sprops->encoding); 430 #else 431 if (fmtdefault) { 432 PUTPROP(props, "file.encoding", sprops->encoding); 433 } else { 434 PUTPROP(props, "file.encoding", sprops->sun_jnu_encoding); 435 } 436 #endif 437 } else { 438 (*env)->DeleteLocalRef(env, jVMVal); 439 } 440 441 return ret; 442 } 443 444 /* 445 * The following three functions implement setter methods for 446 * java.lang.System.{in, out, err}. They are natively implemented 447 * because they violate the semantics of the language (i.e. set final 448 * variable). 449 */ 450 JNIEXPORT void JNICALL 451 Java_java_lang_System_setIn0(JNIEnv *env, jclass cla, jobject stream) 452 { 453 jfieldID fid = 454 (*env)->GetStaticFieldID(env,cla,"in","Ljava/io/InputStream;"); 455 if (fid == 0) 456 return; 457 (*env)->SetStaticObjectField(env,cla,fid,stream); 458 } 459 460 JNIEXPORT void JNICALL 461 Java_java_lang_System_setOut0(JNIEnv *env, jclass cla, jobject stream) 462 { 463 jfieldID fid = 464 (*env)->GetStaticFieldID(env,cla,"out","Ljava/io/PrintStream;"); 465 if (fid == 0) 466 return; 467 (*env)->SetStaticObjectField(env,cla,fid,stream); 468 } 469 470 JNIEXPORT void JNICALL 471 Java_java_lang_System_setErr0(JNIEnv *env, jclass cla, jobject stream) 472 { 473 jfieldID fid = 474 (*env)->GetStaticFieldID(env,cla,"err","Ljava/io/PrintStream;"); 475 if (fid == 0) 476 return; 477 (*env)->SetStaticObjectField(env,cla,fid,stream); 478 } 479 480 static void cpchars(jchar *dst, char *src, int n) 481 { 482 int i; 483 for (i = 0; i < n; i++) { 484 dst[i] = src[i]; 485 } 486 } 487 488 JNIEXPORT jstring JNICALL 489 Java_java_lang_System_mapLibraryName(JNIEnv *env, jclass ign, jstring libname) 490 { 491 int len; 492 int prefix_len = (int) strlen(JNI_LIB_PREFIX); 493 int suffix_len = (int) strlen(JNI_LIB_SUFFIX); 494 495 jchar chars[256]; 496 if (libname == NULL) { 497 JNU_ThrowNullPointerException(env, 0); 498 return NULL; 499 } 500 len = (*env)->GetStringLength(env, libname); 501 if (len > 240) { 502 JNU_ThrowIllegalArgumentException(env, "name too long"); 503 return NULL; 504 } 505 cpchars(chars, JNI_LIB_PREFIX, prefix_len); 506 (*env)->GetStringRegion(env, libname, 0, len, chars + prefix_len); 507 len += prefix_len; 508 cpchars(chars + len, JNI_LIB_SUFFIX, suffix_len); 509 len += suffix_len; 510 511 return (*env)->NewString(env, chars, len); 512 }