blob: f8b98b1ba7815284f0d224378e5d2cfdaf490bc6 [file] [log] [blame]
Elliott Hughes94367e02009-10-23 18:10:44 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughesabf945f2010-06-03 19:55:20 -070017#define LOG_TAG "System"
18
Elliott Hughes94367e02009-10-23 18:10:44 -070019#include "JNIHelp.h"
Elliott Hughese22935d2010-08-12 17:27:27 -070020#include "JniConstants.h"
Elliott Hughes05960872010-05-26 17:45:07 -070021#include "ScopedUtfChars.h"
Elliott Hughes8545b832011-06-08 15:31:22 -070022#include "cutils/log.h"
Elliott Hughes2ef68712011-02-18 10:47:17 -080023#include "openssl/opensslv.h"
Elliott Hughesddafeb12011-02-18 15:14:29 -080024#include "toStringArray.h"
Elliott Hughes2ef68712011-02-18 10:47:17 -080025#include "zlib.h"
26
27#include <string>
28#include <vector>
29
30#include <limits.h>
Elliott Hughes94367e02009-10-23 18:10:44 -070031#include <stdlib.h>
32#include <string.h>
Elliott Hughes22ccdd02011-08-31 10:28:27 -070033#include <sys/time.h>
34#include <time.h>
Elliott Hughes2ef68712011-02-18 10:47:17 -080035#include <unistd.h>
36
Elliott Hughesa7ef5522011-02-22 14:47:35 -080037static void System_log(JNIEnv* env, jclass, jchar type, jstring javaMessage, jthrowable exception) {
38 ScopedUtfChars message(env, javaMessage);
39 if (message.c_str() == NULL) {
40 // Since this function is used for last-gasp debugging output, be noisy on failure.
Steve Block679cf682012-01-08 10:18:49 +000041 ALOGE("message.c_str() == NULL");
Elliott Hughesa7ef5522011-02-22 14:47:35 -080042 return;
43 }
44 int priority;
45 switch (type) {
46 case 'D': case 'd': priority = ANDROID_LOG_DEBUG; break;
47 case 'E': case 'e': priority = ANDROID_LOG_ERROR; break;
48 case 'F': case 'f': priority = ANDROID_LOG_FATAL; break;
49 case 'I': case 'i': priority = ANDROID_LOG_INFO; break;
50 case 'S': case 's': priority = ANDROID_LOG_SILENT; break;
51 case 'V': case 'v': priority = ANDROID_LOG_VERBOSE; break;
52 case 'W': case 'w': priority = ANDROID_LOG_WARN; break;
53 default: priority = ANDROID_LOG_DEFAULT; break;
54 }
55 LOG_PRI(priority, LOG_TAG, "%s", message.c_str());
56 if (exception != NULL) {
57 jniLogException(env, priority, LOG_TAG, exception);
58 }
59}
60
Elliott Hughes05960872010-05-26 17:45:07 -070061// Sets a field via JNI. Used for the standard streams, which are read-only otherwise.
62static void System_setFieldImpl(JNIEnv* env, jclass clazz,
63 jstring javaName, jstring javaSignature, jobject object) {
64 ScopedUtfChars name(env, javaName);
65 if (name.c_str() == NULL) {
66 return;
67 }
68 ScopedUtfChars signature(env, javaSignature);
69 if (signature.c_str() == NULL) {
70 return;
71 }
72 jfieldID fieldID = env->GetStaticFieldID(clazz, name.c_str(), signature.c_str());
Elliott Hughes94367e02009-10-23 18:10:44 -070073 env->SetStaticObjectField(clazz, fieldID, object);
Elliott Hughes94367e02009-10-23 18:10:44 -070074}
75
Elliott Hughes2ef68712011-02-18 10:47:17 -080076static jobjectArray System_specialProperties(JNIEnv* env, jclass) {
77 std::vector<std::string> properties;
78
Elliott Hughes2ef68712011-02-18 10:47:17 -080079 char path[PATH_MAX];
80 properties.push_back(std::string("user.dir=") + getcwd(path, sizeof(path)));
81
82 properties.push_back("android.zlib.version=" ZLIB_VERSION);
83 properties.push_back("android.openssl.version=" OPENSSL_VERSION_TEXT);
84
85 return toStringArray(env, properties);
86}
87
Elliott Hughes22ccdd02011-08-31 10:28:27 -070088static jlong System_currentTimeMillis(JNIEnv*, jclass) {
89 timeval now;
90 gettimeofday(&now, NULL);
91 jlong when = now.tv_sec * 1000LL + now.tv_usec / 1000;
92 return when;
93}
94
95static jlong System_nanoTime(JNIEnv*, jclass) {
Elliott Hughes0b10fe22012-01-20 22:34:52 -080096#if defined(HAVE_POSIX_CLOCKS)
Elliott Hughes22ccdd02011-08-31 10:28:27 -070097 timespec now;
Elliott Hughes508bf132013-06-21 00:52:43 +000098 clock_gettime(CLOCK_MONOTONIC, &now);
Elliott Hughes22ccdd02011-08-31 10:28:27 -070099 return now.tv_sec * 1000000000LL + now.tv_nsec;
Elliott Hughes0b10fe22012-01-20 22:34:52 -0800100#else
101 timeval now;
102 gettimeofday(&now, NULL);
103 return static_cast<jlong>(now.tv_sec) * 1000000000LL + now.tv_usec * 1000LL;
104#endif
Elliott Hughes22ccdd02011-08-31 10:28:27 -0700105}
106
107static jstring System_mapLibraryName(JNIEnv* env, jclass, jstring javaName) {
108 ScopedUtfChars name(env, javaName);
109 if (name.c_str() == NULL) {
110 return NULL;
111 }
112 char* mappedName = NULL;
113 asprintf(&mappedName, OS_SHARED_LIB_FORMAT_STR, name.c_str());
114 jstring result = env->NewStringUTF(mappedName);
115 free(mappedName);
116 return result;
117}
118
Elliott Hughes94367e02009-10-23 18:10:44 -0700119static JNINativeMethod gMethods[] = {
Ian Rogers1707e282013-10-25 16:05:21 -0700120 NATIVE_METHOD(System, currentTimeMillis, "!()J"),
Elliott Hughesa7ef5522011-02-22 14:47:35 -0800121 NATIVE_METHOD(System, log, "(CLjava/lang/String;Ljava/lang/Throwable;)V"),
Elliott Hughes22ccdd02011-08-31 10:28:27 -0700122 NATIVE_METHOD(System, mapLibraryName, "(Ljava/lang/String;)Ljava/lang/String;"),
Ian Rogers1707e282013-10-25 16:05:21 -0700123 NATIVE_METHOD(System, nanoTime, "!()J"),
Elliott Hughese22935d2010-08-12 17:27:27 -0700124 NATIVE_METHOD(System, setFieldImpl, "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;)V"),
Elliott Hughes2ef68712011-02-18 10:47:17 -0800125 NATIVE_METHOD(System, specialProperties, "()[Ljava/lang/String;"),
Elliott Hughes94367e02009-10-23 18:10:44 -0700126};
Elliott Hughes7cd67602012-05-03 17:21:04 -0700127void register_java_lang_System(JNIEnv* env) {
128 jniRegisterNativeMethods(env, "java/lang/System", gMethods, NELEM(gMethods));
Elliott Hughes94367e02009-10-23 18:10:44 -0700129}