Elliott Hughes | 94367e0 | 2009-10-23 18:10:44 -0700 | [diff] [blame] | 1 | /* |
| 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 Hughes | abf945f | 2010-06-03 19:55:20 -0700 | [diff] [blame] | 17 | #define LOG_TAG "System" |
| 18 | |
Elliott Hughes | 94367e0 | 2009-10-23 18:10:44 -0700 | [diff] [blame] | 19 | #include "JNIHelp.h" |
Elliott Hughes | e22935d | 2010-08-12 17:27:27 -0700 | [diff] [blame] | 20 | #include "JniConstants.h" |
Elliott Hughes | 0596087 | 2010-05-26 17:45:07 -0700 | [diff] [blame] | 21 | #include "ScopedUtfChars.h" |
Elliott Hughes | 8545b83 | 2011-06-08 15:31:22 -0700 | [diff] [blame] | 22 | #include "cutils/log.h" |
Elliott Hughes | 2ef6871 | 2011-02-18 10:47:17 -0800 | [diff] [blame] | 23 | #include "openssl/opensslv.h" |
Elliott Hughes | ddafeb1 | 2011-02-18 15:14:29 -0800 | [diff] [blame] | 24 | #include "toStringArray.h" |
Elliott Hughes | 2ef6871 | 2011-02-18 10:47:17 -0800 | [diff] [blame] | 25 | #include "zlib.h" |
| 26 | |
| 27 | #include <string> |
| 28 | #include <vector> |
| 29 | |
| 30 | #include <limits.h> |
Elliott Hughes | 94367e0 | 2009-10-23 18:10:44 -0700 | [diff] [blame] | 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
Elliott Hughes | 22ccdd0 | 2011-08-31 10:28:27 -0700 | [diff] [blame] | 33 | #include <sys/time.h> |
| 34 | #include <time.h> |
Elliott Hughes | 2ef6871 | 2011-02-18 10:47:17 -0800 | [diff] [blame] | 35 | #include <unistd.h> |
| 36 | |
Elliott Hughes | a7ef552 | 2011-02-22 14:47:35 -0800 | [diff] [blame] | 37 | static 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 Block | 679cf68 | 2012-01-08 10:18:49 +0000 | [diff] [blame] | 41 | ALOGE("message.c_str() == NULL"); |
Elliott Hughes | a7ef552 | 2011-02-22 14:47:35 -0800 | [diff] [blame] | 42 | 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 Hughes | 0596087 | 2010-05-26 17:45:07 -0700 | [diff] [blame] | 61 | // Sets a field via JNI. Used for the standard streams, which are read-only otherwise. |
| 62 | static 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 Hughes | 94367e0 | 2009-10-23 18:10:44 -0700 | [diff] [blame] | 73 | env->SetStaticObjectField(clazz, fieldID, object); |
Elliott Hughes | 94367e0 | 2009-10-23 18:10:44 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Elliott Hughes | 2ef6871 | 2011-02-18 10:47:17 -0800 | [diff] [blame] | 76 | static jobjectArray System_specialProperties(JNIEnv* env, jclass) { |
| 77 | std::vector<std::string> properties; |
| 78 | |
Elliott Hughes | 2ef6871 | 2011-02-18 10:47:17 -0800 | [diff] [blame] | 79 | 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 Hughes | 22ccdd0 | 2011-08-31 10:28:27 -0700 | [diff] [blame] | 88 | static 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 | |
| 95 | static jlong System_nanoTime(JNIEnv*, jclass) { |
Elliott Hughes | 0b10fe2 | 2012-01-20 22:34:52 -0800 | [diff] [blame] | 96 | #if defined(HAVE_POSIX_CLOCKS) |
Elliott Hughes | 22ccdd0 | 2011-08-31 10:28:27 -0700 | [diff] [blame] | 97 | timespec now; |
Elliott Hughes | 508bf13 | 2013-06-21 00:52:43 +0000 | [diff] [blame] | 98 | clock_gettime(CLOCK_MONOTONIC, &now); |
Elliott Hughes | 22ccdd0 | 2011-08-31 10:28:27 -0700 | [diff] [blame] | 99 | return now.tv_sec * 1000000000LL + now.tv_nsec; |
Elliott Hughes | 0b10fe2 | 2012-01-20 22:34:52 -0800 | [diff] [blame] | 100 | #else |
| 101 | timeval now; |
| 102 | gettimeofday(&now, NULL); |
| 103 | return static_cast<jlong>(now.tv_sec) * 1000000000LL + now.tv_usec * 1000LL; |
| 104 | #endif |
Elliott Hughes | 22ccdd0 | 2011-08-31 10:28:27 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static 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 Hughes | 94367e0 | 2009-10-23 18:10:44 -0700 | [diff] [blame] | 119 | static JNINativeMethod gMethods[] = { |
Ian Rogers | 1707e28 | 2013-10-25 16:05:21 -0700 | [diff] [blame] | 120 | NATIVE_METHOD(System, currentTimeMillis, "!()J"), |
Elliott Hughes | a7ef552 | 2011-02-22 14:47:35 -0800 | [diff] [blame] | 121 | NATIVE_METHOD(System, log, "(CLjava/lang/String;Ljava/lang/Throwable;)V"), |
Elliott Hughes | 22ccdd0 | 2011-08-31 10:28:27 -0700 | [diff] [blame] | 122 | NATIVE_METHOD(System, mapLibraryName, "(Ljava/lang/String;)Ljava/lang/String;"), |
Ian Rogers | 1707e28 | 2013-10-25 16:05:21 -0700 | [diff] [blame] | 123 | NATIVE_METHOD(System, nanoTime, "!()J"), |
Elliott Hughes | e22935d | 2010-08-12 17:27:27 -0700 | [diff] [blame] | 124 | NATIVE_METHOD(System, setFieldImpl, "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;)V"), |
Elliott Hughes | 2ef6871 | 2011-02-18 10:47:17 -0800 | [diff] [blame] | 125 | NATIVE_METHOD(System, specialProperties, "()[Ljava/lang/String;"), |
Elliott Hughes | 94367e0 | 2009-10-23 18:10:44 -0700 | [diff] [blame] | 126 | }; |
Elliott Hughes | 7cd6760 | 2012-05-03 17:21:04 -0700 | [diff] [blame] | 127 | void register_java_lang_System(JNIEnv* env) { |
| 128 | jniRegisterNativeMethods(env, "java/lang/System", gMethods, NELEM(gMethods)); |
Elliott Hughes | 94367e0 | 2009-10-23 18:10:44 -0700 | [diff] [blame] | 129 | } |