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