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 */
39static 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
47JNIEXPORT void JNICALL
48Java_java_lang_System_registerNatives(JNIEnv *env, jclass cls)
49{
50 (*env)->RegisterNatives(env, cls,
51 methods, sizeof(methods)/sizeof(methods[0]));
52}
53
54JNIEXPORT jint JNICALL
55Java_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 */
106JNIEXPORT jobjectArray JNICALL
107Java_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 /* data model */
195 if (sizeof(sprops) == 4) {
196 sprops->data_model = "32";
197 } else if (sizeof(sprops) == 8) {
198 sprops->data_model = "64";
199 } else {
200 sprops->data_model = "unknown";
201 }
202 PUTPROP(propArray, _sun_arch_data_model_NDX, sprops->data_model);
203
204 /* patch level */
205 PUTPROP(propArray, _sun_os_patch_level_NDX, sprops->patch_level);
206
207 PUTPROP_PlatformString(propArray, _java_io_tmpdir_NDX, sprops->tmp_dir);
208
209 PUTPROP_PlatformString(propArray, _user_name_NDX, sprops->user_name);
210 PUTPROP_PlatformString(propArray, _user_home_NDX, sprops->user_home);
211 PUTPROP_PlatformString(propArray, _user_dir_NDX, sprops->user_dir);
212
213 /*
214 * Set i18n related property fields from platform.
215 */
216 PUTPROP(propArray, _display_language_NDX, sprops->display_language);
217 PUTPROP(propArray, _display_script_NDX, sprops->display_script);
218 PUTPROP(propArray, _display_country_NDX, sprops->display_country);
219 PUTPROP(propArray, _display_variant_NDX, sprops->display_variant);
220
221 PUTPROP(propArray, _format_language_NDX, sprops->format_language);
222 PUTPROP(propArray, _format_script_NDX, sprops->format_script);
223 PUTPROP(propArray, _format_country_NDX, sprops->format_country);
224 PUTPROP(propArray, _format_variant_NDX, sprops->format_variant);
225
226 return propArray;
227}
228
229/*
230 * Gather the VM and command line properties and return as a String[].
231 * The array indices are alternating key/value pairs
232 * supplied by the VM including those defined on the command line
233 * using -Dkey=value that may override the platform defined value.
234 *
235 * Note: The platform encoding must have been set.
236 *
237 * Class: jdk_internal_util_SystemProps_Raw
238 * Method: vmProperties
239 * Signature: ()[Ljava/lang/String;
240 */
241JNIEXPORT jobjectArray JNICALL
242Java_jdk_internal_util_SystemProps_00024Raw_vmProperties(JNIEnv *env, jclass cla)
243{
244 jobjectArray cmdProps = JVM_GetProperties(env);
245 return cmdProps;
246}
247
248/*
249 * The following three functions implement setter methods for
250 * java.lang.System.{in, out, err}. They are natively implemented
251 * because they violate the semantics of the language (i.e. set final
252 * variable).
253 */
254JNIEXPORT void JNICALL
255Java_java_lang_System_setIn0(JNIEnv *env, jclass cla, jobject stream)
256{
257 jfieldID fid =
258 (*env)->GetStaticFieldID(env,cla,"in","Ljava/io/InputStream;");
259 if (fid == 0)
260 return;
261 (*env)->SetStaticObjectField(env,cla,fid,stream);
262}
263
264JNIEXPORT void JNICALL
265Java_java_lang_System_setOut0(JNIEnv *env, jclass cla, jobject stream)
266{
267 jfieldID fid =
268 (*env)->GetStaticFieldID(env,cla,"out","Ljava/io/PrintStream;");
269 if (fid == 0)
270 return;
271 (*env)->SetStaticObjectField(env,cla,fid,stream);
272}
273
274JNIEXPORT void JNICALL
275Java_java_lang_System_setErr0(JNIEnv *env, jclass cla, jobject stream)
276{
277 jfieldID fid =
278 (*env)->GetStaticFieldID(env,cla,"err","Ljava/io/PrintStream;");
279 if (fid == 0)
280 return;
281 (*env)->SetStaticObjectField(env,cla,fid,stream);
282}
283
284static void cpchars(jchar *dst, char *src, int n)
285{
286 int i;
287 for (i = 0; i < n; i++) {
288 dst[i] = src[i];
289 }
290}
291
292JNIEXPORT jstring JNICALL
293Java_java_lang_System_mapLibraryName(JNIEnv *env, jclass ign, jstring libname)
294{
295 int len;
296 int prefix_len = (int) strlen(JNI_LIB_PREFIX);
297 int suffix_len = (int) strlen(JNI_LIB_SUFFIX);
298
299 jchar chars[256];
300 if (libname == NULL) {
301 JNU_ThrowNullPointerException(env, 0);
302 return NULL;
303 }
304 len = (*env)->GetStringLength(env, libname);
305 if (len > 240) {
306 JNU_ThrowIllegalArgumentException(env, "name too long");
307 return NULL;
308 }
309 cpchars(chars, JNI_LIB_PREFIX, prefix_len);
310 (*env)->GetStringRegion(env, libname, 0, len, chars + prefix_len);
311 len += prefix_len;
312 cpchars(chars + len, JNI_LIB_SUFFIX, suffix_len);
313 len += suffix_len;
314
315 return (*env)->NewString(env, chars, len);
316}
317