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