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