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