1 /*
   2  * Copyright (c) 2005, 2014, 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 "jni.h"
  27 #include "jni_util.h"
  28 #include "jvm.h"
  29 #include "jdk_util.h"
  30 
  31 #include "sun_misc_Version.h"
  32 
  33 #ifndef CHECK_ERROR_RETURN
  34 #define CHECK_ERROR_RETURN(x) if ((x) == -1) return;
  35 #endif
  36 
  37 #ifndef CHECK_ERROR_RETURNFALSE
  38 #define CHECK_ERROR_RETURNFALSE(x) if ((x) == -1) return JNI_FALSE;
  39 #endif
  40 
  41 char jvm_special_version = '\0';
  42 char jdk_special_version = '\0';
  43 static int setStaticIntField(JNIEnv* env, jclass cls, const char* name, jint value)
  44 {
  45     char errmsg[100];
  46     jfieldID fid;
  47     fid = (*env)->GetStaticFieldID(env, cls, name, "I");
  48     if (fid != 0) {
  49         (*env)->SetStaticIntField(env, cls, fid, value);
  50     } else {
  51         sprintf(errmsg, "Static int field %s not found", name);
  52         JNU_ThrowInternalError(env, errmsg);
  53         return -1;
  54     }
  55     return 0;
  56 }
  57 
  58 typedef void (JNICALL *GetJvmVersionInfo_fp)(JNIEnv*, jvm_version_info*, size_t);
  59 
  60 JNIEXPORT jboolean JNICALL
  61 Java_sun_misc_Version_getJvmVersionInfo(JNIEnv *env, jclass cls)
  62 {
  63     jvm_version_info info;
  64     GetJvmVersionInfo_fp func_p;
  65 
  66     if (!JDK_InitJvmHandle()) {
  67         JNU_ThrowInternalError(env, "Handle for JVM not found for symbol lookup");
  68         return JNI_FALSE;
  69     }
  70     func_p = (GetJvmVersionInfo_fp) JDK_FindJvmEntry("JVM_GetVersionInfo");
  71     if (func_p == NULL) {
  72         return JNI_FALSE;
  73     }
  74 
  75     (*func_p)(env, &info, sizeof(info));
  76     CHECK_ERROR_RETURNFALSE(setStaticIntField(env, cls, "jvm_major_version",
  77                             JVM_VERSION_MAJOR(info.jvm_version)));
  78     CHECK_ERROR_RETURNFALSE(setStaticIntField(env, cls, "jvm_minor_version",
  79                             JVM_VERSION_MINOR(info.jvm_version)));
  80     CHECK_ERROR_RETURNFALSE(setStaticIntField(env, cls, "jvm_micro_version",
  81                             JVM_VERSION_MICRO(info.jvm_version)));
  82     CHECK_ERROR_RETURNFALSE(setStaticIntField(env, cls, "jvm_build_number",
  83                             JVM_VERSION_BUILD(info.jvm_version)));
  84     CHECK_ERROR_RETURNFALSE(setStaticIntField(env, cls, "jvm_update_version",
  85                             info.update_version));
  86     jvm_special_version = info.special_update_version;
  87 
  88     return JNI_TRUE;
  89 }
  90 
  91 JNIEXPORT jstring JNICALL
  92 Java_sun_misc_Version_getJvmSpecialVersion(JNIEnv *env, jclass cls) {
  93     char s[2];
  94     jstring special;
  95     s[0] = jvm_special_version;
  96     s[1] = '\0';
  97     special = (*env)->NewStringUTF(env, s);
  98     return special;
  99 }
 100 
 101 JNIEXPORT void JNICALL
 102 Java_sun_misc_Version_getJdkVersionInfo(JNIEnv *env, jclass cls)
 103 {
 104     jdk_version_info info;
 105 
 106     JDK_GetVersionInfo0(&info, sizeof(info));
 107     CHECK_ERROR_RETURN(setStaticIntField(env, cls, "jdk_major_version",
 108                        JDK_VERSION_MAJOR(info.jdk_version)));
 109     CHECK_ERROR_RETURN(setStaticIntField(env, cls, "jdk_minor_version",
 110                        JDK_VERSION_MINOR(info.jdk_version)));
 111     CHECK_ERROR_RETURN(setStaticIntField(env, cls, "jdk_micro_version",
 112                        JDK_VERSION_MICRO(info.jdk_version)));
 113     CHECK_ERROR_RETURN(setStaticIntField(env, cls, "jdk_build_number",
 114                        JDK_VERSION_BUILD(info.jdk_version)));
 115     CHECK_ERROR_RETURN(setStaticIntField(env, cls, "jdk_update_version",
 116                        info.update_version));
 117     jdk_special_version = info.special_update_version;
 118 }
 119 
 120 JNIEXPORT jstring JNICALL
 121 Java_sun_misc_Version_getJdkSpecialVersion(JNIEnv *env, jclass cls) {
 122     char s[2];
 123     jstring special;
 124     s[0] = jdk_special_version;
 125     s[1] = '\0';
 126     special = (*env)->NewStringUTF(env, s);
 127     return special;
 128 }