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
17#define LOG_TAG "System"
18
19#include "JNIHelp.h"
20#include "JniConstants.h"
21#include "ScopedUtfChars.h"
22#include "cutils/log.h"
23#include "openssl/opensslv.h"
24#include "toStringArray.h"
25#include "zlib.h"
26
27#include <string>
28#include <vector>
29
30#include <limits.h>
31#include <stdlib.h>
32#include <string.h>
33#include <sys/time.h>
34#include <time.h>
35#include <unistd.h>
36
37static 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.
41        ALOGE("message.c_str() == NULL");
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
61// 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());
73    env->SetStaticObjectField(clazz, fieldID, object);
74}
75
76static jobjectArray System_specialProperties(JNIEnv* env, jclass) {
77    std::vector<std::string> properties;
78
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
88static 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) {
96    timespec now;
97    clock_gettime(CLOCK_MONOTONIC, &now);
98    return now.tv_sec * 1000000000LL + now.tv_nsec;
99}
100
101static jstring System_mapLibraryName(JNIEnv* env, jclass, jstring javaName) {
102    ScopedUtfChars name(env, javaName);
103    if (name.c_str() == NULL) {
104        return NULL;
105    }
106    char* mappedName = NULL;
107    asprintf(&mappedName, OS_SHARED_LIB_FORMAT_STR, name.c_str());
108    jstring result = env->NewStringUTF(mappedName);
109    free(mappedName);
110    return result;
111}
112
113static JNINativeMethod gMethods[] = {
114    NATIVE_METHOD(System, currentTimeMillis, "()J"),
115    NATIVE_METHOD(System, log, "(CLjava/lang/String;Ljava/lang/Throwable;)V"),
116    NATIVE_METHOD(System, mapLibraryName, "(Ljava/lang/String;)Ljava/lang/String;"),
117    NATIVE_METHOD(System, nanoTime, "()J"),
118    NATIVE_METHOD(System, setFieldImpl, "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;)V"),
119    NATIVE_METHOD(System, specialProperties, "()[Ljava/lang/String;"),
120};
121void register_java_lang_System(JNIEnv* env) {
122    jniRegisterNativeMethods(env, "java/lang/System", gMethods, NELEM(gMethods));
123}
124